Files
accounted/app/api/events/cleanup/cron/route.ts
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

68 lines
2.6 KiB
TypeScript

import { createServiceClient } from '@/lib/supabase/server'
import { NextResponse } from 'next/server'
import { withCronContext } from '@/lib/api/with-cron-context'
import { errorResponse } from '@/lib/errors/get-structured-error'
/**
* GET /api/events/cleanup/cron: daily 02:00 UTC.
*
* Differentiated retention:
* - Delivery events (invoice.created, transaction.synced, …): 30 days. They
* exist for external automation polling (n8n/Make/Zapier) and go stale fast.
* - Agent telemetry (mcp.*, agent.*): 180 days. Error-rate trends and
* skill-load correlation need more than one month of signal: a 30-day
* window made it impossible to tell whether a tool or skill change actually
* moved failure rates.
*
* Retention is declared in .compliance/ropa.yaml (id: mcp.telemetry).
*/
const DELIVERY_RETENTION_DAYS = 30
const TELEMETRY_RETENTION_DAYS = 180
export const GET = withCronContext('cron.events_cleanup', async (_request, ctx) => {
const supabase = createServiceClient()
const deliveryCutoff = new Date()
deliveryCutoff.setDate(deliveryCutoff.getDate() - DELIVERY_RETENTION_DAYS)
const telemetryCutoff = new Date()
telemetryCutoff.setDate(telemetryCutoff.getDate() - TELEMETRY_RETENTION_DAYS)
// Pass 1: delivery events past 30 days. Telemetry (mcp.*, agent.*) is
// excluded here and swept by the 180-day pass below.
const { error: deliveryError, count: deliveryCount } = await supabase
.from('event_log')
.delete({ count: 'exact' })
.lt('created_at', deliveryCutoff.toISOString())
.not('event_type', 'like', 'mcp.%')
.not('event_type', 'like', 'agent.%')
if (deliveryError) {
ctx.log.error('event log delivery cleanup failed', deliveryError)
return errorResponse(deliveryError, ctx.log, { requestId: ctx.requestId })
}
// Pass 2: everything past 180 days, catches the telemetry rows pass 1 skipped.
const { error: telemetryError, count: telemetryCount } = await supabase
.from('event_log')
.delete({ count: 'exact' })
.lt('created_at', telemetryCutoff.toISOString())
if (telemetryError) {
ctx.log.error('event log telemetry cleanup failed', telemetryError)
return errorResponse(telemetryError, ctx.log, { requestId: ctx.requestId })
}
const deletedDelivery = deliveryCount ?? 0
const deletedTelemetry = telemetryCount ?? 0
const deleted = deletedDelivery + deletedTelemetry
ctx.log.info('event log cleanup summary', {
deleted,
deletedDelivery,
deletedTelemetry,
deliveryCutoff: deliveryCutoff.toISOString(),
telemetryCutoff: telemetryCutoff.toISOString(),
})
return NextResponse.json({ success: true, deleted, deletedDelivery, deletedTelemetry })
})