Files
PaperClipAI/server/src/modules/active-run-watchdog/index.ts
T
2026-10-31 16:00:35 -05:00

63 lines
2.3 KiB
TypeScript

import { registerActiveRunSilenceWork } from "./adapters/deadline-work.js";
import type { Db } from "@paperclipai/db";
import { createPostgresWatchdogAdapter } from "./adapters/postgres.js";
import { createProcessAdapter } from "./adapters/process.js";
import {
createBuildRunOutputSilence,
createFoldSourceResolvedRun,
createRecordWatchdogDecision,
createScanSilentActiveRuns,
} from "./application/use-cases.js";
export type ActiveRunWatchdogConfig = {
suspicionThresholdMs: number;
criticalThresholdMs: number;
continueRearmMs: number;
};
/**
* Composes the active-run output watchdog module: the Postgres adapter,
* the process adapter, and the four use cases. The recovery service holds
* the only caller.
*/
export function createActiveRunWatchdog(db: Db, config: ActiveRunWatchdogConfig) {
const postgresAdapter = createPostgresWatchdogAdapter(db);
const processController = createProcessAdapter();
const foldSourceResolvedRun = createFoldSourceResolvedRun({
reader: postgresAdapter,
writer: postgresAdapter,
processController,
});
const scanSilentActiveRuns = createScanSilentActiveRuns({ reader: postgresAdapter, foldSourceResolvedRun,
suspicionThresholdMs: config.suspicionThresholdMs });
return {
registerSilenceWork: (coordinator: Parameters<typeof registerActiveRunSilenceWork>[1], canRun: () => boolean,
scan = scanSilentActiveRuns, enabled = async () => true) => registerActiveRunSilenceWork(db, coordinator, { reader: postgresAdapter,
scan, enabled, suspicionThresholdMs: config.suspicionThresholdMs }, canRun),
buildRunOutputSilence: createBuildRunOutputSilence({
reader: postgresAdapter,
suspicionThresholdMs: config.suspicionThresholdMs,
criticalThresholdMs: config.criticalThresholdMs,
}),
scanSilentActiveRuns,
recordWatchdogDecision: createRecordWatchdogDecision({
reader: postgresAdapter,
writer: postgresAdapter,
continueRearmMs: config.continueRearmMs,
}),
};
}
export type ActiveRunWatchdog = ReturnType<typeof createActiveRunWatchdog>;
export type {
RunOutputSilenceSummary,
ScanSilentActiveRunsResult,
WatchdogDecisionActor,
WatchdogDecisionRecord,
} from "./application/types.js";
export { WatchdogDecisionApplicationError } from "./application/types.js";