Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(sync): a caller's cancel keeps banking admitted work; only the governor's abort throws at a step
time-attackec8713fa71b5
diff --git a/src/core/persistence/drain-step.ts b/src/core/persistence/drain-step.tsindex 6a6366da92..854593e61c 100644--- a/src/core/persistence/drain-step.ts+++ b/src/core/persistence/drain-step.ts@@ -10,9 +10,15 @@ export interface DrainStepStamp { step: DrainStep; since: number; last_sql: { lalet current: DrainStepStamp | null = null;-/** Enters `step` and throws the pass's abort reason, so an abandoned pass admits and writes nothing more. */+/** The reason the in-pass governor aborts the pass it abandons. */+export class DrainPassAbandoned extends Error {}++/**+ * Enters `step`; a pass the governor abandoned throws here, so it admits and writes nothing more. The caller's own+ * cancellation is not thrown here: the pass's existing cancel handling still banks work it already admitted.+ */export function stampDrainStep(step: DrainStep, signal?: AbortSignal, now = Date.now()): void {- signal?.throwIfAborted();+ if (signal?.aborted && signal.reason instanceof DrainPassAbandoned) throw signal.reason;current = { step, since: now, last_sql: null };}/** Records the statement the current step is about to send (label only, never text or parameters). */diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.tsindex 00e4b7ddb3..77d7dec721 100644--- a/src/core/persistence/sync-drain.ts+++ b/src/core/persistence/sync-drain.ts@@ -23,7 +23,7 @@ import { ERROR_CATALOGUE, type CatalogueName } from '../error-catalogue.ts';import { cliRenderContext, renderAction, type Action, type RenderedAction } from '../agent-output.ts';import { managedSyncResumeArgs, syncResumeCommand } from '../sync-reconcile.ts';import { isWriteCapacityWait, outstandingCapacityOf } from './admission-retry.ts';-import { clearDrainStep, readDrainStep } from './drain-step.ts';+import { clearDrainStep, DrainPassAbandoned, readDrainStep } from './drain-step.ts';import { PUBLICATION_CEILING_DEFAULT_MS, readPublicationCeilingMs } from './publication-deadline.ts';import { isOwnerThisProcess, stampLastSql, WAITING_ON, type ClaimLastSql, type WaitingOn } from './claim-phase.ts';@@ -386,7 +386,7 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {const running = Promise.resolve().then(() => input.pass(passSignal, onProgress));let timer: ReturnType<typeof setInterval> | null = null, probing = false, key: string | null = null, keySince = passStartedAt;const stopped = new Promise<{ stall: DrainStall }>(resolve => {- const stop = (stall: DrainStall) => { if (timer) clearInterval(timer); timer = null; controller.abort(new Error(`drain_stalled: ${stall.cause}`)); resolve({ stall }); };+ const stop = (stall: DrainStall) => { if (timer) clearInterval(timer); timer = null; controller.abort(new DrainPassAbandoned(`drain_stalled: ${stall.cause}`)); resolve({ stall }); };timer = setInterval(() => {const quietSince = Math.max(lastCommitAt, passStartedAt);if (probing || Date.now() - quietSince < Math.min(noProgressMs, ceilingMs)) return;