Env kill switch with raw-evidence retention, idempotent backfill, in-place dead-letter replay with reconciliation notes, command lease recovery on pull, ack terminal-state guards, and an operations runbook. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
75 lines
2.5 KiB
TypeScript
75 lines
2.5 KiB
TypeScript
import type { Payload, PayloadRequest } from "payload";
|
|
import { hasPermission } from "@/utils/access-control/hasPermission";
|
|
import { processOperationEvent } from "./process";
|
|
import { toRawSyncEvent } from "./reconcile";
|
|
|
|
type PayloadType = Awaited<ReturnType<typeof import("payload").getPayload>>;
|
|
|
|
export const OPERATION_LEDGER_FLAG = "OPERATION_LEDGER_ENABLED";
|
|
|
|
/** Staged-activation kill switch: env `OPERATION_LEDGER_ENABLED=false` disables
|
|
* derived-effect processing while raw evidence ingestion continues. */
|
|
export function isOperationLedgerEnabled(): boolean {
|
|
return process.env[OPERATION_LEDGER_FLAG] !== "false";
|
|
}
|
|
|
|
export interface BackfillResult {
|
|
processed: number;
|
|
skipped: number;
|
|
forbidden: boolean;
|
|
}
|
|
|
|
/**
|
|
* Replay raw sync events that have no ledger row yet (for example events
|
|
* ingested while the ledger flag was disabled). Idempotent: processOperationEvent
|
|
* skips source events that already have a row.
|
|
*/
|
|
export async function backfillOperationEvents(
|
|
payload: PayloadType,
|
|
user: { id: number } | null | undefined,
|
|
options?: { limit?: number; req?: PayloadRequest },
|
|
): Promise<BackfillResult> {
|
|
const allowed = await hasPermission(payload, user, "operation-events:update");
|
|
if (!allowed) return { processed: 0, skipped: 0, forbidden: true };
|
|
|
|
const limit = options?.limit ?? 200;
|
|
const raw = await payload.find({
|
|
collection: "arma-sync-events",
|
|
sort: "-createdAt",
|
|
limit,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
req: options?.req,
|
|
});
|
|
|
|
const rawIds = raw.docs.map((doc) => doc.id as number);
|
|
const existing = rawIds.length
|
|
? await payload.find({
|
|
collection: "operation-events",
|
|
where: { sourceEvent: { in: rawIds } },
|
|
limit: rawIds.length,
|
|
depth: 0,
|
|
overrideAccess: true,
|
|
req: options?.req,
|
|
})
|
|
: { docs: [] as { sourceEvent: unknown }[] };
|
|
const alreadyLedgered = new Set(
|
|
existing.docs.map((doc) => {
|
|
const source = (doc as { sourceEvent: unknown }).sourceEvent;
|
|
return typeof source === "number" ? source : null;
|
|
}),
|
|
);
|
|
|
|
let processed = 0;
|
|
let skipped = 0;
|
|
for (const doc of raw.docs) {
|
|
if (alreadyLedgered.has(doc.id as number)) {
|
|
skipped += 1;
|
|
continue;
|
|
}
|
|
await processOperationEvent(payload, toRawSyncEvent(doc as unknown as Record<string, unknown>), options?.req);
|
|
processed += 1;
|
|
}
|
|
payload.logger.info(`[Operations] Backfill processed ${processed} events (${skipped} already ledgered)`);
|
|
return { processed, skipped, forbidden: false };
|
|
}
|