import type { ZCodeAutomationRunOutcome, ZCodeAutomationTrigger, } from "@zcode/shared"; export interface CronRunLifecycleRepo { ensureRunClaimed(params: { runId: string; automationId: string; workspaceKey: string; scheduledAt: number | null; trigger: ZCodeAutomationTrigger; }): Promise; markRunOutcome( runId: string, outcome: ZCodeAutomationRunOutcome, error?: string, ): Promise; markRunDispatch(params: { runId: string; dispatchStatus: "failed_to_dispatch"; error: string; }): Promise; touchManualClaim(automationId: string, workspaceKey: string): Promise; releaseManualClaim(automationId: string, workspaceKey: string): Promise; } export interface CronRunLifecycleIdentity { runId: string; automationId: string; workspaceKey: string; scheduledAt: number | null; trigger: ZCodeAutomationTrigger; } type LogWarn = (message: string, error: unknown) => void; const MANUAL_CLAIM_HEARTBEAT_MS = 60_000; export function startManualClaimHeartbeat( params: Pick & { repo: Pick; logWarn: LogWarn; intervalMs?: number; }, ): { dispose(): void } { const timer = setInterval(() => { void params.repo .touchManualClaim(params.automationId, params.workspaceKey) .catch((error) => params.logWarn( `续租 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`, error, ), ); }, params.intervalMs ?? MANUAL_CLAIM_HEARTBEAT_MS); return { dispose: () => clearInterval(timer) }; } export async function recordCronRunOutcomeBestEffort( params: CronRunLifecycleIdentity & { repo: CronRunLifecycleRepo; outcome: ZCodeAutomationRunOutcome; error?: string; logWarn: LogWarn; }, ): Promise { try { await params.repo.ensureRunClaimed(params); await params.repo.markRunOutcome(params.runId, params.outcome, params.error); } catch (error) { params.logWarn( `回写定时任务运行结果失败 automation=${params.automationId} runId=${params.runId}`, error, ); } } export async function releaseManualClaimBestEffort( params: Pick & { repo: Pick; logWarn: LogWarn; }, ): Promise { try { await params.repo.releaseManualClaim(params.automationId, params.workspaceKey); } catch (error) { params.logWarn( `释放 manual automation claim 失败 automation=${params.automationId} runId=${params.runId}`, error, ); } } /** 派发失败清理永不覆盖调用方持有的原始 dispatch error。 */ export async function settleManualDispatchFailureBestEffort( params: CronRunLifecycleIdentity & { repo: CronRunLifecycleRepo; dispatchError: unknown; logWarn: LogWarn; }, ): Promise { const errorMessage = params.dispatchError instanceof Error ? params.dispatchError.message : String(params.dispatchError); try { await params.repo.markRunDispatch({ runId: params.runId, dispatchStatus: "failed_to_dispatch", error: errorMessage, }); } catch (error) { params.logWarn( `回写 manual automation 派发失败状态失败 automation=${params.automationId} runId=${params.runId}`, error, ); } await releaseManualClaimBestEffort(params); } /** manual claim 覆盖 queue 等待和 turn 执行,只能在真实终态后释放。 */ export async function settleCronRunTerminalOutcome( params: CronRunLifecycleIdentity & { repo: CronRunLifecycleRepo; outcome: Exclude; error?: string; logWarn: LogWarn; }, ): Promise { await recordCronRunOutcomeBestEffort(params); if (params.trigger !== "manual") return; await releaseManualClaimBestEffort(params); }