Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(sync): drain_stalled fires at the ceiling or on a wedged same-host owner, never at the allowance; owner_wedged_here envelope with retry_after_ms (#6317 lane 2, B2/T2)

garrytan68d61b890ed6
diff --git a/src/core/persistence/sync-drain.ts b/src/core/persistence/sync-drain.ts
index 29345b8260..6d2458ed61 100644
--- a/src/core/persistence/sync-drain.ts
+++ b/src/core/persistence/sync-drain.ts
@@ -20,8 +20,10 @@ import { getCode, isRetryableConnError, isStatementTimeoutError } from '../retry
import { currentRunDeadline, noteForwardProgress } from '../forward-progress.ts';
import { serr } from '../console-prefix.ts';
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 { isOwnerThisProcess, stampLastSql, type ClaimLastSql } from './claim-phase.ts';
export type DrainOutcome = 'synced' | 'resumable' | 'blocked';
/** Why a drain ended short of `synced`. Each value has an error-catalogue entry (DX-A4). */
@@ -45,10 +47,27 @@ export interface DrainClaim {
claim_age_ms: number | null;
/** The lease expired (nobody renews it): a dead owner or a transaction holding the row, never a live preparation. */
lapsed: boolean;
- /** The owner's pid when it stamped one; this process's pid means the drain's own consumer holds it. */
+ /** The owner's pid when it stamped one; this process's pid (and nonce, #6317) means the drain's own consumer holds it. */
owner_pid: number | null;
+ /** #6317: the owner's command kind and per-process nonce from the stamp (null on a stamp from an owner older than #6317). */
+ owner_kind: string | null;
+ owner_nonce: string | null;
+ /** #6317: the last raw statement the owner's preparation issued (label and age), when its stamp recorded one. */
+ last_sql: ClaimLastSql | null;
/** How long a live preparation may hold the head before the drain calls it stalled: its budget plus a grace, capped by the ceiling allowance. */
allowance_ms: number;
+ /** #6317: `persistence.preparation_ceiling_ms`, measured from the claim: past it #6298 frees the root and the drain stops waiting. */
+ ceiling_ms: number;
+}
+/** #6317: what the head owner's heartbeat row (`persistence_consumers`, same host) says about it; null when it wrote none. */
+export interface DrainOwnerRow {
+ kind: string;
+ pid: number;
+ nonce: string | null;
+ mode: string;
+ live: boolean;
+ restart_required: boolean;
+ root_barrier_age_ms: number | null;
}
export interface DrainStall {
request_id: string;
@@ -63,8 +82,22 @@ export interface DrainStall {
phase?: DrainClaim['phase'];
step?: string | null;
waiting_on?: DrainClaim['waiting_on'];
- /** #6278: why the drain called it a stall: no renewal (`owner_missing`), a preparation or publication past its allowance, or no change at all. */
- cause?: 'owner_missing' | 'preparation_overdue' | 'publication_overdue' | 'no_progress';
+ /**
+ * #6278: why the drain called it a stall: no renewal (`owner_missing`), a preparation or publication past its allowance, or no change at all.
+ * #6317: `owner_wedged_here` is a live same-host owner whose preparation passed the ceiling, or whose own heartbeat row says it is wedged
+ * (`restart_required`, a root barrier past the ceiling); `preparation_overdue` is kept for an owner on another host.
+ */
+ cause?: 'owner_missing' | 'preparation_overdue' | 'publication_overdue' | 'no_progress' | 'owner_wedged_here';
+ /** #6317: the head owner as the stamp names it, so the stop can say which process to restart. */
+ owner_pid?: number | null;
+ owner_kind?: string | null;
+ owner_nonce?: string | null;
+ last_sql?: ClaimLastSql | null;
+ /** #6317: the owner's heartbeat row, when the stop read one (`owner_wedged_here`). */
+ owner_row?: DrainOwnerRow | null;
+ /** #6317: whether the stop came at the ceiling (true) or on the owner's own wedged verdict before it (false), and the ceiling judged against. */
+ past_ceiling?: boolean;
+ ceiling_ms?: number;
}
export interface DrainReport {
outcome: DrainOutcome;
@@ -196,6 +229,10 @@ export interface StallProbe {
fingerprint(result: SyncResult): Promise<{ key: string; stall: Omit<DrainStall, 'stalled_seconds'>; claim?: DrainClaim | null } | null>;
/** #6278: the source's head while a pass runs (its oldest running request, else its oldest queued one); null when nothing is unfinished. Bounded; may reject. */
head?(): Promise<{ head_state: string; claim: DrainClaim | null } | null>;
+ /** #6317: the head owner's heartbeat row on this host, or null (no row: an older owner, PGLite, the table absent, or another host). Never rejects. */
+ owner?(claim: DrainClaim): Promise<DrainOwnerRow | null>;
+ /** #6317: whether a consumer here could reclaim a lapsed head: this process's own, or a live full row on this host. Never rejects. */
+ reclaimableHere?(): Promise<boolean>;
}
export interface DrainInput {
@@ -214,11 +251,19 @@ export interface DrainInput {
progressMs?: number;
}
-/** #6278: the line the drain prints while nothing commits, naming the head's step, wait cause and allowance. */
+/**
+ * #6278: the line the drain prints while nothing commits, naming the head's step, wait cause and allowance. #6317: past the
+ * allowance it names the owner (kind, pid), the last statement and the ceiling the drain waits for (B2, C3).
+ */
function stallText(index: number, total: number | null, stalledMs: number, claim: DrainClaim | null, headState: string | null): string {
+ const live = !!claim && !claim.lapsed && claim.phase === 'preparing';
+ const overdue = live && stalledMs >= claim.allowance_ms;
+ const owner = claim?.owner_pid !== null && claim?.owner_pid !== undefined ? `${claim.owner_kind ?? 'owner'} pid ${claim.owner_pid}` : null;
return `[sync] ${index}/${total ?? '?'} processed · stalled ${Math.round(stalledMs / 1000)}s on ${claim?.step ?? claim?.phase ?? headState ?? 'the writer head'}`
+ `${claim?.waiting_on && claim.waiting_on !== 'unknown' ? ` (waiting on ${claim.waiting_on})` : ''}${claim?.lapsed ? ' (claim lapsed: owner missing)' : ''}`
- + `${claim && !claim.lapsed && claim.phase === 'preparing' ? ` · allowed ${formatDuration(Math.round(claim.allowance_ms / 1000))}` : ''}`;
+ + `${overdue && owner ? ` · held by ${owner}` : ''}${overdue && claim.last_sql ? ` · last_sql ${claim.last_sql.label}${claim.last_sql.age_ms === null ? '' : ` ${Math.round(claim.last_sql.age_ms / 1000)}s ago`}` : ''}`
+ + `${live ? overdue ? ` · past the ${formatDuration(Math.round(claim.allowance_ms / 1000))} allowance; the root is freed at the ${formatDuration(Math.round(claim.ceiling_ms / 1000))} ceiling`
+ : ` · allowed ${formatDuration(Math.round(claim.allowance_ms / 1000))}` : ''}`;
}
/** Re-enter `pass` until the managed cursor is done, the caller stops it, or it is blocked. */
@@ -229,7 +274,8 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {
let passes = 0, attempt = 0, readFailures = 0, refreshWaitedMs = 0, written = 0, waived = 0, index = 0, total: number | null = null;
let announcedStart = false, lastLine = 0, groups = 0, groupedPages = 0, largestGroup = 0, admittedAhead = 0;
let lanes: { effective: number; stepDown: string | null; overlapped: number; fallbacks: number } | null = null;
- let stall: { key: string; since: number; passes: number; claim: DrainClaim | null; stallInfo: Omit<DrainStall, 'stalled_seconds'> } | null = null;
+ // #6317: `extended` marks the one extra window a lapsed head gets while a consumer here can reclaim it; `ownerAt`/`owner` throttle the owner-row read to one per progress interval.
+ let stall: { key: string; since: number; passes: number; claim: DrainClaim | null; stallInfo: Omit<DrainStall, 'stalled_seconds'>; extended?: boolean; ownerAt?: number; owner?: DrainOwnerRow | null } | null = null;
let lastCommitAt = startedAt, inPass = false, readingHead = false;
// #6278 (B7): a pass refused for write capacity keeps the last pass's result for the report; `capacity` spans the refusals since the last pass that ran.
let last: SyncResult | undefined, capacity: { since: number; attempts: number } | null = null;
@@ -359,12 +405,35 @@ export async function runDrain(input: DrainInput): Promise<SyncResult> {
const live = !!claim && !claim.lapsed && claim.phase !== null;
const window = live ? Math.max(input.stallMs ?? STALL_MS, claim.allowance_ms) : input.stallMs ?? STALL_MS;
const elapsed = Math.max(Date.now() - stall.since, live ? claim.step_age_ms ?? 0 : 0);
- if (++stall.passes >= STALL_PASSES && elapsed >= window && !print.stall.claimable_here) {
+ // #6317 (B2): `claimable_here` no longer gates the stop (it sees queued rows only, and a same-host owner made it always false).
+ if (++stall.passes >= STALL_PASSES && elapsed >= window) {
const cause: DrainStall['cause'] = claim?.lapsed ? 'owner_missing' : live ? claim.phase === 'publishing' ? 'publication_overdue' : 'preparation_overdue' : 'no_progress';
- const detail: DrainStall = { ...print.stall, stalled_seconds: Math.round(elapsed / 1000), phase: claim?.phase ?? null, step: claim?.step ?? null, waiting_on: claim?.waiting_on ?? null, cause };
+ const detail: DrainStall = { ...print.stall, stalled_seconds: Math.round(elapsed / 1000), phase: claim?.phase ?? null, step: claim?.step ?? null, waiting_on: claim?.waiting_on ?? null, cause,
+ owner_pid: claim?.owner_pid ?? null, owner_kind: claim?.owner_kind ?? null, owner_nonce: claim?.owner_nonce ?? null, last_sql: claim?.last_sql ?? null };
// A preparation this process's own consumer holds past its allowance ends with the process: resumable, and the next pass holds the entry.
- if (cause === 'preparation_overdue' && claim?.owner_pid === process.pid) return finish(result, 'resumable', 'preparation_abandoned', { stall: detail });
- return finish(result, 'blocked', 'drain_stalled', { stall: detail });
+ if (cause === 'preparation_overdue' && claim && isOwnerThisProcess({ pid: claim.owner_pid ?? -1, ...(claim.owner_nonce ? { nonce: claim.owner_nonce } : {}) })) {
+ return finish(result, 'resumable', 'preparation_abandoned', { stall: detail });
+ }
+ // #6317: a lapsed head a consumer on this host can reclaim (`head_lapsed` + owner host + a live consumer) gets exactly one more window.
+ if (cause === 'owner_missing' && !stall.extended && print.stall.owner_is_this_host !== false && await (input.probe.reclaimableHere?.().catch(() => false) ?? false)) {
+ stall.extended = true; stall.since = Date.now(); stall.passes = 0;
+ } else if (cause === 'preparation_overdue' && claim && print.stall.owner_is_this_host !== false) {
+ // #6317 (B2, G5): a live same-host owner keeps the drain running past the allowance (the stall line names it every interval);
+ // the drain stops only past the ceiling, where #6298 frees the root and the next pass holds the entry, or when the owner's own
+ // heartbeat row says it is wedged (restart_required, a root barrier past the ceiling). The envelope then says how long until
+ // the ceiling (`retry_after_ms`), so a looping caller waits instead of hammering.
+ if (stall.ownerAt === undefined || Date.now() - stall.ownerAt >= every) {
+ stall.ownerAt = Date.now();
+ stall.owner = await (input.probe.owner?.(claim).catch(() => null) ?? null);
+ }
+ const age = claim.claim_age_ms ?? elapsed;
+ const pastCeiling = age >= claim.ceiling_ms;
+ const wedged = !!stall.owner && (stall.owner.restart_required || (stall.owner.root_barrier_age_ms !== null && stall.owner.root_barrier_age_ms > claim.ceiling_ms));
+ if (pastCeiling || wedged) {
+ return finish(result, 'blocked', 'drain_stalled', { stall: { ...detail, cause: 'owner_wedged_here', owner_row: stall.owner ?? null, past_ceiling: pastCeiling, ceiling_ms: claim.ceiling_ms },
+ retry_after_ms: Math.max(0, claim.ceiling_ms - age) });
+ }
+ } else return finish(result, 'blocked', 'drain_stalled', { stall: detail });
}
}
}
@@ -388,11 +457,13 @@ export function drainClaimOf(row: { head_state: string | null; head_claim_phase:
const budget = String(row.head_kind ?? '').startsWith('managed_sync_') ? budgets.syncMs : budgets.maintenanceMs;
const phase = own ? stamp!.phase === 'publishing' ? 'publishing' : 'preparing' : null;
const waiting = own ? stamp!.waiting_on : null;
+ const owner = own && stamp!.owner && typeof stamp!.owner === 'object' ? stamp!.owner as Record<string, unknown> : null;
return { phase, step: own && typeof stamp!.step === 'string' ? stamp!.step : null,
waiting_on: typeof waiting === 'string' && ['git', 'fs', 'db', 'pool', 'unknown'].includes(waiting) ? waiting as DrainClaim['waiting_on'] : own ? 'unknown' : null,
step_age_ms: own ? age(stamp!.step_since ?? stamp!.since) : null, claim_age_ms: own ? age(stamp!.claimed_at) : null, lapsed: row.head_lapsed === true,
- owner_pid: own ? ownerPid(stamp!) : null,
- allowance_ms: Math.min(budgets.ceilingMs + STALL_GRACE_MS, budget + STALL_GRACE_MS) };
+ owner_pid: own ? ownerPid(stamp!) : null, owner_kind: owner && typeof owner.kind === 'string' ? owner.kind : null, owner_nonce: owner && typeof owner.nonce === 'string' ? owner.nonce : null,
+ last_sql: own ? stampLastSql(stamp!.last_sql, now) : null,
+ allowance_ms: Math.min(budgets.ceilingMs + STALL_GRACE_MS, budget + STALL_GRACE_MS), ceiling_ms: budgets.ceilingMs };
}
function safeJson(text: string): unknown { try { return JSON.parse(text); } catch { return null; } }
/** The stamp stores the owner as `owner: { kind, pid, version }` (claim-phase.ts); a top-level `pid` is the pre-release shape test fixtures used. */
@@ -455,6 +526,22 @@ export function engineStallProbe(engine: BrainEngine, sourceId?: string): StallP
return { key: [row.state, row.blocked_reason, row.head_id, row.head_state, claim?.phase ?? '', claim?.step ?? '', stamp?.since ?? '', row.head_lapsed ? 'lapsed' : ''].join('|'),
stall: await describe(found.requestId, row), claim };
},
+ // #6317: the owner's heartbeat row by pid (and nonce when both sides have one); a consumer here that could reclaim a lapsed head.
+ async owner(claim) {
+ if (claim.owner_pid === null) return null;
+ const { localHostId } = await import('./identity.ts');
+ const { listHostConsumers, consumerLive } = await import('./consumer-heartbeat.ts');
+ const rows = await listHostConsumers(engine, localHostId());
+ const row = rows.find(r => r.pid === claim.owner_pid && (claim.owner_nonce === null || r.nonce === claim.owner_nonce));
+ return row ? { kind: row.kind, pid: row.pid, nonce: row.nonce, mode: row.mode, live: consumerLive(row), restart_required: row.restart_required, root_barrier_age_ms: row.root_barrier_age_ms } : null;
+ },
+ async reclaimableHere() {
+ const { persistenceConsumerStatus } = await import('./service.ts');
+ if (persistenceConsumerStatus(engine).state === 'open') return true;
+ const { localHostId } = await import('./identity.ts');
+ const { listHostConsumers, consumerLive } = await import('./consumer-heartbeat.ts');
+ return (await listHostConsumers(engine, localHostId())).some(row => (row.mode === 'full' || row.mode === 'promoted') && consumerLive(row));
+ },
...(sourceId ? { async head() {
const [row] = await engine.executeRaw<{ head_state: string; head_claim_phase: unknown; head_token: string | null; head_lapsed: boolean | null; head_kind: string | null }>(
`SELECT h.state AS head_state, h.claim_phase AS head_claim_phase, h.execution_token::text AS head_token,
@@ -530,6 +617,10 @@ export interface DrainNext {
rate_pages_per_min: number | null;
why: string;
docs?: string;
+ /** #6317: the stop's code and cause, and its agent-operator fix (`next: wait` while the ceiling is ahead, `tell_user_to_run` once it passed). */
+ code?: string;
+ cause?: DrainStall['cause'];
+ fix?: RenderedAction;
}
const STOP_DOCS: Record<DrainStopReason, CatalogueName> = {
@@ -577,15 +668,36 @@ export function drainNext(result: SyncResult, resumeCommand: string, sourceId: s
why: `The drain could not read its write's state from the database${wait ? ` (${wait.reason}: ${wait.why})` : ''}. The accepted write keeps its request ID. Fix database access, then rerun.`,
...(docs ? { docs } : {}) };
}
+ // #6317 (B2): a live same-host owner stopped the drain at the ceiling or on its own wedged verdict; the fix is to wait for the ceiling
+ // (the root is freed there and the next pass holds the entry) or, past it, to restart the named process.
+ if (d?.stop_reason === 'drain_stalled' && d.stall?.cause === 'owner_wedged_here') {
+ const stall = d.stall, owner = `${stall.owner_kind ?? 'owner'} pid ${stall.owner_pid ?? '?'}`;
+ const where = `${stall.step ? ` at step ${stall.step}` : ''}${stall.waiting_on && stall.waiting_on !== 'unknown' ? `, waiting on ${stall.waiting_on}` : ''}${stall.last_sql ? `, last statement ${stall.last_sql.label}` : ''}`;
+ const ahead = !stall.past_ceiling && (d.retry_after_ms ?? 0) > 0;
+ const action: Action = ahead
+ ? { argv: resumeCommand.split(' '), consent: [], actor: 'provider', requires_exclusive: false,
+ why: `The ${owner} on this host still renews the claim; its own budget frees the root at the ceiling in ${formatDuration(Math.ceil((d.retry_after_ms ?? 0) / 1000))}, after which the same command resumes and holds the entry if it stalls again.`,
+ verify: { argv: ['gbrain', 'sources', 'writer', 'status', '--source', sourceId, '--json'] } }
+ : { argv: ['gbrain', 'sources', 'writer', 'status', '--source', sourceId, '--json'], consent: [], actor: 'host_admin', requires_exclusive: false,
+ why: `The ${owner} on this host holds the claim past the ceiling${stall.owner_row?.restart_required ? ' and reports restart_required' : ''}; only restarting that process ends the preparation it is parked in. Status names the request, step and owner, read-only.`,
+ user_message: `Restart the gbrain ${stall.owner_kind ?? 'owner'} process (pid ${stall.owner_pid ?? '?'}) on this host, then run: ${resumeCommand}`,
+ verify: { argv: ['gbrain', 'sources', 'writer', 'movement', sourceId, '--json'] } };
+ return { command: ahead ? resumeCommand : `gbrain sources writer status --source ${sourceId} --json`, safe_to_loop: ahead, retry_after_ms: d.retry_after_ms ?? 0, ...estimate,
+ why: `The write at the head of this source has been preparing for ${stall.stalled_seconds}s${where}, held by the ${owner} on this host`
+ + `${stall.past_ceiling ? `, past the ${stall.ceiling_ms ? formatDuration(Math.round(stall.ceiling_ms / 1000)) : 'preparation'} ceiling` : stall.owner_row?.restart_required ? ', which reports restart_required' : ', whose heartbeat reports a root barrier past the ceiling'}. `
+ + (ahead ? `Wait ${formatDuration(Math.ceil((d.retry_after_ms ?? 0) / 1000))} for the owner's own budget to free the root, then rerun: ${resumeCommand}`
+ : `Restart that process, then rerun: ${resumeCommand}`),
+ code: 'drain_stalled', cause: 'owner_wedged_here', fix: renderAction(action, cliRenderContext()), ...(docs ? { docs } : {}) };
+ }
const writerBlocked = d?.stop_reason && d.stop_reason !== 'blocked_by_failures' && d.stop_reason !== 'deadline';
if (writerBlocked) {
return { command: `gbrain sources writer status ${sourceId}`, safe_to_loop: false, retry_after_ms: 0, ...estimate,
why: d!.stop_reason === 'drain_stalled'
? `No write for this source made progress for ${d!.stall?.stalled_seconds ?? 30}s${d!.stall?.step ? ` (stuck at step ${d!.stall.step}${d!.stall.waiting_on && d!.stall.waiting_on !== 'unknown' ? `, waiting on ${d!.stall.waiting_on}` : ''})` : ''}`
- + `${d!.stall?.cause === 'owner_missing' ? '; its claim lapsed, so its owner is gone or a transaction holds the row' : d!.stall?.cause === 'preparation_overdue' ? '; its owner keeps renewing the claim past the preparation budget (an owner that predates deadlines, or one that ignores cancellation)' : ''}`
- + ` and nothing here can claim it. Inspect the writer, fix what it names, then rerun: ${resumeCommand}`
+ + `${d!.stall?.cause === 'owner_missing' ? '; its claim lapsed and no consumer here reclaimed it, so its owner is gone or a transaction holds the row' : d!.stall?.cause === 'preparation_overdue' ? `; its owner${d!.stall.owner_pid ? ` (${d!.stall.owner_kind ?? 'process'} pid ${d!.stall.owner_pid} on another host)` : ''} keeps renewing the claim past the preparation budget (an owner that predates deadlines, or one that ignores cancellation)` : ''}`
+ + `. Inspect the writer, fix what it names, then rerun: ${resumeCommand}`
: `The source's writer needs intervention (${d!.stop_reason}) before more pages can publish. Inspect it, fix what it names, then rerun: ${resumeCommand}`,
- ...(docs ? { docs } : {}) };
+ ...(d!.stop_reason === 'drain_stalled' ? { code: 'drain_stalled', cause: d!.stall?.cause } : {}), ...(docs ? { docs } : {}) };
}
const retry = resumeCommand.includes(' --retry-failed') ? resumeCommand : `${resumeCommand} --retry-failed`;
return { command: retry, safe_to_loop: false, retry_after_ms: 0, ...estimate,
@@ -608,9 +720,11 @@ export function formatDrainSummary(result: SyncResult, resumeCommand: string, so
+ (d.remaining ? `, ${d.remaining} remaining` : '') + (d.rate_pages_per_min !== null ? `, ${d.rate_pages_per_min} pages/min` : '')
+ (d.remaining && d.eta_seconds !== null ? `, indexing ETA ${formatDuration(d.eta_seconds)}` : '') + '.'];
if (d.capacity) lines.push(` Waited ${d.capacity.waited_seconds}s for write capacity: ${d.capacity.outstanding ?? '?'} of ${d.capacity.limit ?? '?'} ${d.capacity.scope ?? 'principal'} outstanding-request slots in use.`);
+ // #6317 (C3): the summary line carries the same step / waiting_on / last_sql triple as the log lines, and the owner process.
if (d.stall) lines.push(` Oldest unfinished request ${d.stall.head_request_id ?? d.stall.request_id} (${d.stall.head_state ?? d.stall.state})`
+ `${d.stall.blocked_reason ? `, blocked_reason=${d.stall.blocked_reason}` : ''}${d.stall.step ? `, step=${d.stall.step}` : ''}${d.stall.waiting_on ? `, waiting_on=${d.stall.waiting_on}` : ''}`
- + `${d.stall.cause ? `, cause=${d.stall.cause}` : ''}; claimable here: ${d.stall.claimable_here ? 'yes' : 'no'}.`);
+ + `${d.stall.last_sql ? `, last_sql=${d.stall.last_sql.label}${d.stall.last_sql.age_ms === null ? '' : ` (${Math.round(d.stall.last_sql.age_ms / 1000)}s ago)`}` : ''}`
+ + `${d.stall.cause ? `, cause=${d.stall.cause}` : ''}${d.stall.owner_pid ? `, owner=${d.stall.owner_kind ?? 'process'} pid ${d.stall.owner_pid}` : ''}; claimable here: ${d.stall.claimable_here ? 'yes' : 'no'}.`);
const next = drainNext(result, resumeCommand, sourceId);
if (next) lines.push(` Next: ${next.command}${next.safe_to_loop ? ' (safe to rerun in a loop)' : ''}`, ` Why: ${next.why}`);
return lines;
diff --git a/test/sync-drain-claims.test.ts b/test/sync-drain-claims.test.ts
index ad7c2051dd..3c1cd621b7 100644
--- a/test/sync-drain-claims.test.ts
+++ b/test/sync-drain-claims.test.ts
@@ -1,4 +1,14 @@
/**
+ * #6317 (B2, T2): a live preparation owned by another process on this host no
+ * longer stops the drain at its allowance (that stop turned #6298's
+ * run-continuing containment into a `blocked` exit for every caller); the
+ * drain prints the stall line naming the owner from the allowance on and
+ * stops `drain_stalled` with `cause: owner_wedged_here` only past the ceiling
+ * or when the owner's heartbeat row says it is wedged, with `retry_after_ms`
+ * to the ceiling. A lapsed head a consumer here can reclaim gets exactly one
+ * more window. The same-process check compares the nonce. A foreign-host
+ * owner and an overdue publication keep the allowance stop.
+ *
* #6278 (B4): the drain's no-progress detector is claim-aware. Its fingerprint
* keys on state, blocked reason, head id/state and the head claim's
* phase/step/since, never on the lease columns every renewal bumps (before
@@ -41,8 +51,10 @@ const done = (total = 10): SyncResult => ({ ...base, managedCursor: { index: tot
const RESUME = 'gbrain sync --source s --no-pull';
const stall = { request_id: 'r', state: 'queued', blocked_reason: null, head_request_id: 'h', head_state: 'running', claimable_here: false, owner_is_this_host: true };
const claim = (over: Partial<DrainClaim> = {}): DrainClaim => ({ phase: 'preparing', step: 'origin_check', waiting_on: 'db', step_age_ms: 45_000, claim_age_ms: 50_000, lapsed: false,
- owner_pid: null, allowance_ms: 150_000, ...over });
-const probeWith = (c: DrainClaim | null, key = 'same'): StallProbe => ({ blockedHead: async () => null, fingerprint: async () => ({ key, stall, claim: c }) });
+ owner_pid: null, owner_kind: null, owner_nonce: null, last_sql: null, allowance_ms: 150_000, ceiling_ms: 600_000, ...over });
+const probeWith = (c: DrainClaim | null, key = 'same', extra: Partial<StallProbe> = {}): StallProbe => ({ blockedHead: async () => null, fingerprint: async () => ({ key, stall, claim: c }), ...extra });
+const foreign = { ...stall, owner_is_this_host: false };
+const foreignProbe = (c: DrainClaim | null): StallProbe => ({ blockedHead: async () => null, fingerprint: async () => ({ key: 'same', stall: foreign, claim: c }) });
describe('claim-aware no-progress window', () => {
test('a healthy preparation longer than the 30 s window and shorter than its budget is not cut off', async () => {
@@ -54,17 +66,88 @@ describe('claim-aware no-progress window', () => {
expect(result.drain).toMatchObject({ outcome: 'synced', passes: 12 });
});
- test('a live preparation past its allowance, owned elsewhere, stops blocked as drain_stalled with cause, step and wait cause', async () => {
- const result = await runDrain({ pass: async () => pending(2), probe: probeWith(claim({ step_age_ms: 160_000, owner_pid: process.pid + 1 })), pauseMs: 1, stallMs: 20 });
+ test('a live preparation past its allowance, owned on another host, stops blocked as drain_stalled with cause, step and wait cause', async () => {
+ const result = await runDrain({ pass: async () => pending(2), probe: foreignProbe(claim({ step_age_ms: 160_000, owner_pid: process.pid + 1, owner_kind: 'serve' })), pauseMs: 1, stallMs: 20 });
expect(result.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled',
- stall: { cause: 'preparation_overdue', step: 'origin_check', waiting_on: 'db', phase: 'preparing', stalled_seconds: 160 } });
+ stall: { cause: 'preparation_overdue', step: 'origin_check', waiting_on: 'db', phase: 'preparing', stalled_seconds: 160, owner_pid: process.pid + 1, owner_kind: 'serve' } });
const next = drainNext(result, RESUME, 's')!;
- expect(next).toMatchObject({ command: 'gbrain sources writer status s', safe_to_loop: false, docs: 'docs/guides/write-refusals.md#drain-stalled' });
+ expect(next).toMatchObject({ command: 'gbrain sources writer status s', safe_to_loop: false, docs: 'docs/guides/write-refusals.md#drain-stalled', code: 'drain_stalled', cause: 'preparation_overdue' });
expect(next.why).toContain('stuck at step origin_check, waiting on db');
expect(next.why).toContain('renewing the claim past the preparation budget');
expect(formatDrainSummary(result, RESUME, 's').join('\n')).toContain('step=origin_check, waiting_on=db, cause=preparation_overdue');
});
+ describe('#6317 (B2, T2): a same-host owner that renews a preparing claim forever', () => {
+ const other = { owner_pid: process.pid + 1, owner_kind: 'serve', owner_nonce: 'other-nonce', last_sql: { label: 'SELECT pages', age_ms: 155_000 } };
+
+ test('past the allowance but inside the ceiling the drain keeps running (the containment, not a blocked exit) and the stall line names the owner and the triple', async () => {
+ const err = captureStderr();
+ try {
+ let passes = 0;
+ // Eight passes past the 150 s allowance: before #6317 the fourth pass stopped the drain as drain_stalled / preparation_overdue.
+ const result = await runDrain({ announce: true, progressMs: 30, pass: async () => { await new Promise(r => setTimeout(r, 12)); return ++passes < 8 ? pending(2) : done(); },
+ probe: probeWith(claim({ step_age_ms: 160_000, claim_age_ms: 165_000, ...other })), pauseMs: 1, stallMs: 20 });
+ expect(result.drain).toMatchObject({ outcome: 'synced', passes: 8 });
+ expect(err.stalled().length).toBeGreaterThanOrEqual(1);
+ expect(err.stalled()[0]).toContain('stalled 160s on origin_check (waiting on db) · held by serve pid ' + (process.pid + 1) + ' · last_sql SELECT pages 155s ago · past the 2m30s allowance; the root is freed at the 10m00s ceiling');
+ } finally { err.restore(); }
+ });
+
+ test('past the ceiling it stops drain_stalled / owner_wedged_here naming the owner pid, within 2x the window, with the restart as the fix', async () => {
+ const started = Date.now();
+ const result = await runDrain({ pass: async () => pending(2), probe: probeWith(claim({ step_age_ms: 600_000, claim_age_ms: 610_000, ...other })), pauseMs: 1, stallMs: 20 });
+ expect(Date.now() - started).toBeLessThan(2 * 20 + 500);
+ expect(result.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled', retry_after_ms: 0,
+ stall: { cause: 'owner_wedged_here', owner_pid: process.pid + 1, owner_kind: 'serve', owner_nonce: 'other-nonce', step: 'origin_check', waiting_on: 'db', past_ceiling: true, ceiling_ms: 600_000, last_sql: { label: 'SELECT pages' } } });
+ const next = drainNext(result, RESUME, 's')!;
+ expect(next).toMatchObject({ code: 'drain_stalled', cause: 'owner_wedged_here', safe_to_loop: false, retry_after_ms: 0, command: 'gbrain sources writer status --source s --json' });
+ expect(next.fix).toMatchObject({ next: 'tell_user_to_run', actor: 'host_admin' });
+ expect(next.fix!.user_message).toContain(`pid ${process.pid + 1}`);
+ expect(next.why).toContain(`held by the serve pid ${process.pid + 1} on this host, past the 10m00s ceiling`);
+ expect(formatDrainSummary(result, RESUME, 's').join('\n')).toContain(`last_sql=SELECT pages (155s ago), cause=owner_wedged_here, owner=serve pid ${process.pid + 1}`);
+ expect(drainJsonFields(result, RESUME, 's')).toMatchObject({ outcome: 'blocked', next: { fix: { next: 'tell_user_to_run' } } });
+ });
+
+ test('an owner whose heartbeat row is wedged stops the drain before the ceiling with retry_after_ms to it and fix.next wait (safe to loop)', async () => {
+ const owner = async () => ({ kind: 'serve', pid: process.pid + 1, nonce: 'other-nonce', mode: 'full', live: true, restart_required: true, root_barrier_age_ms: null });
+ const result = await runDrain({ pass: async () => pending(2), probe: probeWith(claim({ step_age_ms: 160_000, claim_age_ms: 200_000, ...other }), 'same', { owner }), pauseMs: 1, stallMs: 20 });
+ expect(result.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled', stall: { cause: 'owner_wedged_here', past_ceiling: false, owner_row: { restart_required: true } } });
+ expect(result.drain!.retry_after_ms).toBeGreaterThan(390_000);
+ expect(result.drain!.retry_after_ms).toBeLessThanOrEqual(400_000);
+ const next = drainNext(result, RESUME, 's')!;
+ expect(next).toMatchObject({ safe_to_loop: true, command: RESUME, cause: 'owner_wedged_here' });
+ expect(next.fix).toMatchObject({ next: 'wait', actor: 'provider' });
+ expect(next.why).toContain('reports restart_required');
+ // A live, healthy row keeps the drain running.
+ let passes = 0;
+ const healthy = async () => ({ ...await owner(), restart_required: false, root_barrier_age_ms: 10_000 });
+ const kept = await runDrain({ pass: async () => (++passes < 8 ? pending(2) : done()), probe: probeWith(claim({ step_age_ms: 160_000, claim_age_ms: 200_000, ...other }), 'same', { owner: healthy }), pauseMs: 1, stallMs: 20 });
+ expect(kept.drain).toMatchObject({ outcome: 'synced', passes: 8 });
+ });
+
+ test('a stamp with this pid but another nonce is another process (pid reuse), never preparation_abandoned', async () => {
+ const result = await runDrain({ pass: async () => pending(2), probe: probeWith(claim({ step_age_ms: 700_000, claim_age_ms: 700_000, owner_pid: process.pid, owner_nonce: 'not-ours', owner_kind: 'sync' })), pauseMs: 1, stallMs: 20 });
+ expect(result.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled', stall: { cause: 'owner_wedged_here', owner_pid: process.pid } });
+ });
+
+ test('a lapsed head a consumer here can reclaim gets exactly one extra window, then drain_stalled / owner_missing; without one it stops at once', async () => {
+ let reclaimable = true;
+ const probe = probeWith(claim({ lapsed: true, step_age_ms: 5_000 }), 'same', { reclaimableHere: async () => reclaimable });
+ const passesAt: number[] = [];
+ const extended = await runDrain({ pass: async () => { passesAt.push(Date.now()); return pending(2); }, probe, pauseMs: 1, stallMs: 40 });
+ expect(extended.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled', stall: { cause: 'owner_missing' } });
+ // Two windows of 40 ms (three passes each at least), not one.
+ expect(passesAt[passesAt.length - 1]! - passesAt[0]!).toBeGreaterThanOrEqual(80);
+ expect(extended.drain!.passes).toBeGreaterThanOrEqual(6);
+ reclaimable = false;
+ const at = Date.now();
+ const immediate = await runDrain({ pass: async () => pending(2), probe, pauseMs: 1, stallMs: 40 });
+ expect(immediate.drain).toMatchObject({ outcome: 'blocked', stop_reason: 'drain_stalled', stall: { cause: 'owner_missing' } });
+ expect(Date.now() - at).toBeLessThan(80 + 300);
+ expect(drainNext(immediate, RESUME, 's')!.why).toContain('no consumer here reclaimed it');
+ });
+ });
+
test('a preparation this process owns past its allowance ends the drain resumable as preparation_abandoned (exit 0, safe to loop)', async () => {
const result = await runDrain({ pass: async () => pending(2), probe: probeWith(claim({ step_age_ms: 200_000, owner_pid: process.pid })), pauseMs: 1, stallMs: 20 });
expect(result.drain).toMatchObject({ outcome: 'resumable', stop_reason: 'preparation_abandoned', stall: { cause: 'preparation_overdue', step: 'origin_check' } });
@@ -113,7 +196,11 @@ describe('drainClaimOf', () => {
// The stamp claim-phase.ts writes: the pid lives under `owner`, and the step has its own `step_since`.
const c = drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ step: 'origin_check', step_since: '2026-10-07T11:59:30Z', waiting_on: 'pool', owner: { kind: 'sync', pid: 4242, version: '0.60.109.0' } }),
head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now);
- expect(c).toEqual({ phase: 'preparing', step: 'origin_check', waiting_on: 'pool', step_age_ms: 30_000, claim_age_ms: 120_000, lapsed: false, owner_pid: 4242, allowance_ms: 120_000 + STALL_GRACE_MS });
+ expect(c).toEqual({ phase: 'preparing', step: 'origin_check', waiting_on: 'pool', step_age_ms: 30_000, claim_age_ms: 120_000, lapsed: false, owner_pid: 4242, owner_kind: 'sync', owner_nonce: null, last_sql: null,
+ allowance_ms: 120_000 + STALL_GRACE_MS, ceiling_ms: 600_000 });
+ // #6317: the nonce and the last statement ride the stamp when the owner recorded them.
+ expect(drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ owner: { kind: 'serve', pid: 7, version: 'v', nonce: 'n1' }, last_sql: { label: 'SELECT pages', at: '2026-10-07T11:59:50Z' } }),
+ head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now)).toMatchObject({ owner_pid: 7, owner_kind: 'serve', owner_nonce: 'n1', last_sql: { label: 'SELECT pages', age_ms: 10_000 } });
// A stamp without `step_since` falls back to the phase's `since`; a pre-release top-level `pid` is still read.
expect(drainClaimOf({ head_state: 'running', head_claim_phase: stamp({ step: 'origin_check', pid: 4243 }), head_token: 't', head_lapsed: false, head_kind: 'managed_sync_import' }, budgets, now))
.toMatchObject({ step_age_ms: 60_000, owner_pid: 4243 });