mirror of
https://github.com/garrytan/gbrain.git
synced 2026-07-27 22:15:33 +00:00
* test(supervisor): pin LOCK_HELD fence-exit is never counted as a crash (#2227) A duplicate supervisor loses the queue-scoped DB singleton lock (#1849) and exits LOCK_HELD before spawning a worker or emitting 'started'. summarizeCrashes counts only worker_exited, so the fence path is structurally uncountable. Pin it so a future refactor that logs worker_exited on the fence path fails here instead of silently re-introducing the crash-budget breaker-trip loop. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(autopilot): per-source cycle binds FS phases to source.local_path, not global repo (#2194 #2227) A per-source autopilot-cycle inherited the global sync.repo_path as brainDir while stamping DB freshness for source_id — mixed scope. FS phases (sync/lint/extract) ran against the wrong tree, so the failure-cooldown and freshness gates would attribute work to the wrong source. Resolve the source's local_path in the handler (reuse the archive-recheck SELECT) and bind brainDir to it; a pure-DB source gets null (FS phases skip) instead of falling through to the global checkout. Legacy no-source dispatch keeps the global repoPath. Prerequisite for the cooldown/split commits (codex outside-voice #8). Resolves TODOS:634. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(supervisor): detect a live supervisor via the DB lock under split $HOME (#2227) jobs supervisor status + doctor read the HOME-derived pidfile, so a supervisor started under a different $HOME (keeper=/root vs ops=/data) read as 'not running' while healthy — the false signal that drives an operator to spawn a duplicate. Both surfaces now fall back to the queue-scoped DB singleton lock (#1849), the HOME-independent authority, when the pidfile shows nothing. New isLockHolderLive keys on lock freshness (ttl + heartbeat steal-grace), never process.kill, so PID reuse can't false-positive (pid-liveness-alone-pid-reuse). Status surfaces the holder host/pid + recorded concurrency/max-rss from the latest started event. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(supervisor): degraded retry instead of permanent give-up on crash storm (#1994 #2227) max_crashes_exceeded gave up forever, so a transient DB-pooler blip that tripped the soft budget wedged the queue until a human restart (#2227's breaker-trips tail). Crossing the soft budget now enters degraded mode: keep respawning with capped exponential backoff (60s cap — a paced retry, not a hot loop) and emit a loud crash_budget_degraded health_warn. The existing stable-run reset clears the count once a respawn survives >5min, so a recovered DB self-heals. Permanent give-up fires only at a much-higher hard ceiling (maxCrashes × 10), tunable/disablable via GBRAIN_SUPERVISOR_HARD_STOP_CRASHES (0 = never). Resolves TODOS:92. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autopilot): clamp fan-out to worker concurrency + doctor warning (#2194) Fan-out resolved to 4 (Postgres) regardless of worker --concurrency, so surplus cycles queued behind the worker and raced the stalled-sweeper. Two fixes for the same mismatch: - resolveEffectiveFanoutMax clamps to max(1, concurrency-1) (reserve a slot), gated on a LIVE DB-lock holder so a stale started-audit row can't shrink throughput (codex #9/D5); no live holder → unknown → unclamped base. Escape hatch autopilot.fanout_clamp_to_concurrency. - doctor's autopilot_fanout_concurrency check warns when fan-out exceeds effective slots — the misconfig was silent before. Advisory (started-event concurrency), wired into both doctor surfaces. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autopilot): per-source failure cooldown — break the dead-job storm (#2194) Only SUCCESS gated dispatch, so a source whose cycle kept failing/timing-out re-fanned-out every 5-min tick forever (200+ dead jobs/24h). Now a failed source backs off with bounded exponential cooldown (10→120min). Read at DISPATCH from minion_jobs dead/failed rows (timeouts/RSS-kills dead-letter via SQL and never run handler code, so a write-only hook would miss them) AND re-checked at CLAIM time in the handler (codex #5: already-queued/retrying jobs). A success clears it (codex #7); null-source rows excluded (codex #6); engine-parity via executeRaw. Disable with autopilot.failure_cooldown_min=0. Fail-open if config/history reads error. Surfaced via fanout_cooldown_skipped + the fanout summary. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(autopilot): split the cycle — per-source phases + one global-maintenance job (#2194 #2227) N per-source cycles each ran the brain-wide global phases (embed-all/orphans/ purge/…) concurrently, thrashing the same rows and taking the worker 4→10GB in <60s → RSS-kill → orphaned stalls. Split them: per-source jobs now run only source-scoped (+ mixed) phases and stamp last_source_cycle_at; a new autopilot-global-maintenance job runs the global phases ONCE per window (idempotency_key + maxWaiting:1 = structural single-flight) and stamps autopilot.last_global_at. This is the codex-endorsed design that replaced the rejected skip-and-stamp-fresh approach (codex #1/#2): no freshness poisoning, no starvation — global work always runs as its own job, never marked done when it wasn't. PHASE_SCOPE is now a runtime partition (GLOBAL ∪ NON_GLOBAL == ALL). last_full_cycle_at still written for doctor/legacy (no longer a global gate). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(doctor): guard nullable engine in supervisor DB-lock fallback (#2227) Follow-up to the supervisor-visibility commit: doctor's engine binding is BrainEngine | null, so the inspectLock fallback must guard on a non-null engine (tsc TS2345). No behavior change — a null engine simply skips the DB-lock probe and falls back to the pidfile reading, as before. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(doctor): categorize autopilot_fanout_concurrency check as ops (#2194) Follow-up to the fan-out/concurrency commit: the doctor-categories drift guard requires every check name in doctor.ts to belong to exactly one category set. Add the new autopilot_fanout_concurrency check to OPS_CHECK_NAMES (infrastructure liveness, alongside wedged_queue/supervisor). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: update KEY_FILES for the autopilot cycle split + supervisor degraded-retry (#2194 #2227) Post-ship document-release: refresh the KEY_FILES current-state entries that drifted — cycle.ts (GLOBAL/NON_GLOBAL phase split + last_source_cycle_at / autopilot.last_global_at), jobs.ts (per-source local_path brainDir, claim-time cooldown, autopilot-global-maintenance handler), supervisor.ts + child-worker (degraded retry instead of permanent give-up; hard ceiling), db-lock.ts (isLockHolderLive), handler-timeouts (new handler). Regenerated llms bundle. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(minions): handleTimeouts counts the timed-out run as a spent attempt (#1737) The per-job timeout_at dead-letter (handleTimeouts) set status='dead' without incrementing attempts_made, unlike the wall-clock and stall dead-letter siblings. It is the FIRST killer to fire for the long-lane handlers (subagent / embed-backfill / autopilot-cycle) because timeout_ms is stamped at submit, so a timed-out long job reported `attempts: 0/N (started: N)`. Mirror the siblings with attempts_made + 1 (terminal, no retry). Safe against double-count: the worker sweep runs handleStalled -> handleTimeouts -> handleWallClockTimeouts sequentially and awaited, each guarded on status='active', so the first to dead-letter excludes the row from the rest. Regression assertions added (test/minions.test.ts + e2e/minions-resilience.test.ts) so the increment can't be silently dropped. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(agent): recognize trailing switches in `agent run`, keep prompts freeform (#1738) parseRunFlags() broke flag parsing at the first positional token, so any flag after the prompt (`gbrain agent run "do X" --detach`) was swallowed into the prompt string and silently ignored. Now the no-value switches --detach/--follow/ --no-follow are hoisted when they trail the prompt, while everything else stays verbatim: an unknown --word is treated as prompt text (no "unknown flag" throw), a --switch mid-prompt is preserved, and `--` suppresses hoisting entirely for a literal escape. Value-flags now reject a missing or flag-shaped value (and --max-turns/--timeout-ms a non-number) instead of capturing undefined/NaN. Contract change: a prompt that starts with or trails an unguarded --word no longer errors; a literal trailing --detach needs `--`. Help text updated; tests revised + extended (test/agent-cli.test.ts). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(sync): honest live-sync status + progress-aware stall-abort (#1950) Finishes the #2255 honest-freshness story for two gaps it left. (a) `gbrain sources status` printed "idle" while a sync proc held the per-source lock (the reported bug). New shared liveSyncStatus() helper in db-lock.ts reads the SAME live-lock signal `gbrain doctor` uses; runStatus now shows "running" (BACKFILL column + a sync_running field in --json) and suppresses the misleading "never synced" warning while a sync is live. One helper, so the surfaces can't drift (doctor/status retrofit tracked as a follow-up). (b) A sync wedged-but-alive kept refreshing its lock heartbeat (it fires on its own timer) and hadn't hit the wall-clock deadline, so only a manual pkill freed it. New in-band stall watchdog keys off FORWARD IMPORT PROGRESS (progress.tick), not the heartbeat: if no file completes for GBRAIN_SYNC_STALL_ABORT_SECONDS (default 900s), it aborts via a controller composed into opts.signal, so the drain returns partial() (last_commit unchanged, next run resumes from the checkpoint) and withRefreshingLock releases the lock. Limits, documented in code: a single file slower than the window trips it; a fully starved event loop won't fire the timer (the wall-clock hard deadline is that backstop). Tests: liveSyncStatus (live/expired/none/per-source) in db-lock-inspect; the resolveStallAbortSeconds env matrix in sync-hard-deadline. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(status): version field + per-section --deadline-ms budget (#1984) `gbrain status` had no version in its JSON envelope and could hang on a slow connection with no way to get a partial answer. Two additions: - version: the StatusReport JSON now carries the local gbrain CLI version so a poller can pin behavior to a build. Thin-client also surfaces remote_version (the brain server's version), and the get_status_snapshot MCP op reports its version for that parity. - --deadline-ms=N / --fast: a shared wall-clock budget. Each section is raced against the REMAINING budget via Promise.race (NOT process-watchdog, which SIGKILLs and can't return partial output), so one slow/hung section can't strand the snapshot — it's marked stale and the rest still return. The envelope gains partial:true + stale_sections[]; exit code stays 0 (a snapshot was produced). Invalid --deadline-ms → exit 2. Tests: parseDeadlineFlag + withSectionDeadline (hermetic), the usage-error exit, version presence in the PGLite envelope, and the op's version key. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(sync): report stall_timeout distinctly + document in-flight limit (#1950) Pre-landing review (codex + adversarial): the stall watchdog aborted opts.signal but the per-iteration abort checks returned partial('timeout'), collapsing a wedge-reap into a user --timeout/SIGINT so JSON consumers couldn't tell them apart. Add a 'stall_timeout' reason (set via a stallAborted flag) on the three import-loop abort sites; deletes/renames-phase and checkpoint sites stay 'timeout'. Sharpen the watchdog comment: the abort is observed BETWEEN files, so a hang inside a single importFile is not interrupted until it returns (TODO: thread a cancellation signal through importFile). * fix(agent): `--` escape suppresses trailing-switch hoisting anywhere (#1738) Pre-landing review: the leading-flag loop breaks at the first positional, so the `escaped` flag only fired for a leading `--`. A `--` placed after a positional left trailing-switch hoisting active, so `agent run note -- body --detach` silently detached and dropped the `--` as junk. Suppress hoisting whenever a literal `--` appears in the prompt. Regression test added. * fix(status): deadline-ms usage-error + scoped stale_sections + cancel losing remote call (#1984) Pre-landing review (codex): (1) bare `--deadline-ms` with no value silently fell through to no-budget/--fast instead of a usage error; (2) thin-client timeout reported both sync+cycle stale even under `--section sync`, naming a section the caller excluded (local path was already correct); (3) the section race abandoned the remote promise locally but didn't cancel the in-flight MCP call — pass the budget as timeoutMs so the losing side actually cancels. Regression test added. * v0.42.52.0 fix(reliability): autopilot dead-job storm + supervisor wedge + sync/status/minion reliability (#2194 #2227 #1994 #1737 #1738 #1950 #1984) Bundles the already-reviewed autopilot/supervisor stabilization (#2194 #2227 #1994: cycle split, per-source failure cooldown, fan-out clamp, degraded supervisor retry, DB-lock live-supervisor detection) with four operational fixes: minion timeout attempt-accounting (#1737), agent-run trailing-flag parsing (#1738), honest live-sync sources status + progress-aware stall watchdog (#1950), and status version + --deadline-ms partial result (#1984). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs: document GBRAIN_SYNC_STALL_ABORT_SECONDS env knob (#1950) Post-ship doc sync (/document-release): add the sync stall watchdog env var to the CLAUDE.md sync-tuning table (Five → Six knobs) + regenerate the llms bundle. * test: quarantine #2249 fanout tests as *.serial (R1 env-isolation) (#2194) The cherry-picked autopilot-fanout-clamp + doctor-autopilot-fanout-concurrency tests mutate process.env.GBRAIN_AUDIT_DIR in beforeEach/afterEach, which the check:test-isolation R1 lint flags (parallel shards load multiple files per process). Rename to *.serial.test.ts (sanctioned quarantine — they run under --max-concurrency=1) instead of restructuring the reviewed test bodies. No logic change; both files stay green (9 tests). Fixes the failing verify CI check. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
267 lines
11 KiB
TypeScript
267 lines
11 KiB
TypeScript
/**
|
|
* #1849: queue-scoped DB supervisor singleton.
|
|
*
|
|
* The pidfile guard is mutually exclusive only per pidfile PATH; the DB lock
|
|
* makes the (database, queue) pair the mutex domain so two supervisors with
|
|
* different $HOME / --pid-file can't both run on one queue. These tests pin:
|
|
* - the lock id keys on DB identity + queue (T2)
|
|
* - a second acquire of the same (db, queue) lock is refused (the singleton)
|
|
* - different queues don't collide
|
|
* - refresh-failure past the threshold fails SAFE (exits non-zero) (F1A)
|
|
*/
|
|
import { describe, test, expect, beforeAll, afterAll, beforeEach, spyOn } from 'bun:test';
|
|
import { existsSync, unlinkSync } from 'fs';
|
|
import { join } from 'path';
|
|
import { tmpdir } from 'os';
|
|
import { PGLiteEngine } from '../src/core/pglite-engine.ts';
|
|
import { tryAcquireDbLock, inspectLock, isLockHolderLive } from '../src/core/db-lock.ts';
|
|
import { MinionSupervisor, ExitCodes, supervisorLockId, classifySupervisorSingleton, SUPERVISOR_LOCK_TTL_MIN } from '../src/core/minions/supervisor.ts';
|
|
import type { DbLockHandle, LockSnapshot } from '../src/core/db-lock.ts';
|
|
|
|
// Build a LockSnapshot fixture for the isLockHolderLive matrix. Only ttl_expired
|
|
// and ms_since_last_refresh are consulted; the rest are filled for shape.
|
|
function snap(over: Partial<LockSnapshot>): LockSnapshot {
|
|
return {
|
|
id: 'gbrain-supervisor:default',
|
|
holder_pid: 4242,
|
|
holder_host: 'box',
|
|
acquired_at: new Date(),
|
|
ttl_expires_at: new Date(),
|
|
age_ms: 1000,
|
|
ttl_expired: false,
|
|
last_refreshed_at: new Date(),
|
|
ms_since_last_refresh: 0,
|
|
...over,
|
|
};
|
|
}
|
|
|
|
let engine: PGLiteEngine;
|
|
|
|
beforeAll(async () => {
|
|
engine = new PGLiteEngine();
|
|
await engine.connect({});
|
|
await engine.initSchema();
|
|
}, 30000);
|
|
|
|
afterAll(async () => {
|
|
await engine.disconnect();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
await engine.executeRaw(`DELETE FROM gbrain_cycle_locks WHERE id LIKE 'gbrain-supervisor:%'`);
|
|
});
|
|
|
|
describe('#1849 supervisorLockId', () => {
|
|
test('keys on queue ONLY (DB scoping is physical — the lock row lives in the DB)', () => {
|
|
expect(supervisorLockId('default')).toBe('gbrain-supervisor:default');
|
|
expect(supervisorLockId('shell')).toBe('gbrain-supervisor:shell');
|
|
// Different queues → different locks.
|
|
expect(supervisorLockId('default')).not.toBe(supervisorLockId('shell'));
|
|
// Regression (the bug this fixes): the id must NOT depend on how the same
|
|
// physical DB was addressed. Two supervisors on one DB via different URLs
|
|
// must compute the SAME id so they collide on the one shared locks table.
|
|
// The function takes no DB-identity arg precisely so it can't diverge.
|
|
expect(supervisorLockId.length).toBe(1);
|
|
});
|
|
});
|
|
|
|
describe('#1849 classifySupervisorSingleton (doctor)', () => {
|
|
test('no live lock → no_lock', () => {
|
|
expect(classifySupervisorSingleton({
|
|
lockLive: false, lockHolderHost: 'h', lockHolderPid: 1, localHost: 'h', localPid: 1,
|
|
})).toBe('no_lock');
|
|
});
|
|
|
|
test('live lock held by the local (host,pid) → single', () => {
|
|
expect(classifySupervisorSingleton({
|
|
lockLive: true, lockHolderHost: 'box', lockHolderPid: 42, localHost: 'box', localPid: 42,
|
|
})).toBe('single');
|
|
});
|
|
|
|
test('live lock held by a DIFFERENT pid → mismatch (rogue second supervisor)', () => {
|
|
expect(classifySupervisorSingleton({
|
|
lockLive: true, lockHolderHost: 'box', lockHolderPid: 99, localHost: 'box', localPid: 42,
|
|
})).toBe('mismatch');
|
|
});
|
|
|
|
test('same pid but DIFFERENT host → mismatch (bare pid is meaningless cross-host)', () => {
|
|
expect(classifySupervisorSingleton({
|
|
lockLive: true, lockHolderHost: 'other', lockHolderPid: 42, localHost: 'box', localPid: 42,
|
|
})).toBe('mismatch');
|
|
});
|
|
|
|
test('live lock but no local pidfile → mismatch', () => {
|
|
expect(classifySupervisorSingleton({
|
|
lockLive: true, lockHolderHost: 'box', lockHolderPid: 42, localHost: 'box', localPid: null,
|
|
})).toBe('mismatch');
|
|
});
|
|
});
|
|
|
|
describe('#1849 DB lock is the real singleton', () => {
|
|
test('second acquire of the same (db, queue) lock is refused', async () => {
|
|
const id = supervisorLockId('default');
|
|
const first = await tryAcquireDbLock(engine, id, 5);
|
|
expect(first).not.toBeNull();
|
|
// A second supervisor (different pidfile, same db+queue) gets null → exit 2.
|
|
const second = await tryAcquireDbLock(engine, id, 5);
|
|
expect(second).toBeNull();
|
|
// After release, a fresh supervisor can take over.
|
|
await first!.release();
|
|
const third = await tryAcquireDbLock(engine, id, 5);
|
|
expect(third).not.toBeNull();
|
|
await third!.release();
|
|
});
|
|
|
|
test('different queues on the same DB do not collide', async () => {
|
|
const a = await tryAcquireDbLock(engine, supervisorLockId('default'), 5);
|
|
const b = await tryAcquireDbLock(engine, supervisorLockId('shell'), 5);
|
|
expect(a).not.toBeNull();
|
|
expect(b).not.toBeNull();
|
|
await a!.release();
|
|
await b!.release();
|
|
});
|
|
});
|
|
|
|
describe('#1849 LOCK_HELD path does not strand the pidfile', () => {
|
|
test('the pidfile-cleanup exit listener is installed BEFORE the DB-lock acquire', async () => {
|
|
// Supervisor A already holds the queue lock.
|
|
const holderA = await tryAcquireDbLock(engine, supervisorLockId('default'), 5);
|
|
expect(holderA).not.toBeNull();
|
|
|
|
const pidFile = join(tmpdir(), `gbrain-sup-stranded-${process.pid}-${Math.random().toString(36).slice(2)}.pid`);
|
|
const sup = new MinionSupervisor(engine, { cliPath: '/bin/sh', healthInterval: 0, json: true, pidFile });
|
|
|
|
// Capture the 'exit' listener start() registers (if any) and stop execution
|
|
// at the first process.exit (the LOCK_HELD path) the way the real exit would.
|
|
let exitListener: ((...a: unknown[]) => void) | null = null;
|
|
const onSpy = spyOn(process, 'on').mockImplementation(((event: string, cb: (...a: unknown[]) => void) => {
|
|
if (event === 'exit') exitListener = cb;
|
|
return process;
|
|
}) as never);
|
|
const exitSpy = spyOn(process, 'exit').mockImplementation(((code?: number) => {
|
|
throw new Error(`exit:${code}`);
|
|
}) as never);
|
|
|
|
try {
|
|
try { await sup.start(); } catch { /* exit stub throws at LOCK_HELD */ }
|
|
|
|
expect(exitSpy).toHaveBeenCalledWith(ExitCodes.LOCK_HELD);
|
|
// The bug: the exit listener was registered AFTER the DB-lock exit, so
|
|
// start() threw before reaching it and the pidfile this process created
|
|
// is stranded. The fix installs it first → it's captured here.
|
|
expect(exitListener).not.toBeNull();
|
|
// And it actually cleans up the pidfile we created (contents match our pid).
|
|
expect(existsSync(pidFile)).toBe(true);
|
|
exitListener!();
|
|
expect(existsSync(pidFile)).toBe(false);
|
|
} finally {
|
|
onSpy.mockRestore();
|
|
exitSpy.mockRestore();
|
|
if (existsSync(pidFile)) unlinkSync(pidFile);
|
|
await holderA!.release();
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
describe('#2227 isLockHolderLive — PID-reuse-safe supervisor liveness', () => {
|
|
test('fresh TTL → live (the normal running case)', () => {
|
|
expect(isLockHolderLive(snap({ ttl_expired: false }), SUPERVISOR_LOCK_TTL_MIN)).toBe(true);
|
|
});
|
|
|
|
test('expired TTL but refreshed within the steal grace → live (starved-but-alive #1794)', () => {
|
|
// ttl lapsed but the holder heartbeat is recent → it is alive, just starved.
|
|
expect(isLockHolderLive(snap({ ttl_expired: true, ms_since_last_refresh: 5_000 }), SUPERVISOR_LOCK_TTL_MIN)).toBe(true);
|
|
});
|
|
|
|
test('expired TTL and stale heartbeat → dead (a gone supervisor stops refreshing)', () => {
|
|
expect(isLockHolderLive(snap({ ttl_expired: true, ms_since_last_refresh: 36_000_000 }), SUPERVISOR_LOCK_TTL_MIN)).toBe(false);
|
|
});
|
|
|
|
test('expired TTL and no heartbeat column → dead', () => {
|
|
expect(isLockHolderLive(snap({ ttl_expired: true, ms_since_last_refresh: null }), SUPERVISOR_LOCK_TTL_MIN)).toBe(false);
|
|
});
|
|
|
|
test('liveness NEVER consults process.kill (PID reuse cannot false-positive)', () => {
|
|
// A row whose holder_pid happens to be a live, unrelated process (PID reuse)
|
|
// but whose lock is stale must read as NOT live — proving freshness, not the
|
|
// PID probe, is the signal. holder_pid=1 (init, always alive) + expired/stale.
|
|
expect(isLockHolderLive(snap({ holder_pid: 1, ttl_expired: true, ms_since_last_refresh: 36_000_000 }), SUPERVISOR_LOCK_TTL_MIN)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('#2227 status detects a live supervisor via the DB lock (split-$HOME)', () => {
|
|
test('a live queue lock with no local pidfile reads as running via inspectLock', async () => {
|
|
// Simulate the keeper holding the queue lock under a different $HOME: there
|
|
// is a live lock row but the local pidfile path is empty.
|
|
const holder = await tryAcquireDbLock(engine, supervisorLockId('default'), SUPERVISOR_LOCK_TTL_MIN);
|
|
expect(holder).not.toBeNull();
|
|
const live = await inspectLock(engine, supervisorLockId('default'));
|
|
expect(live).not.toBeNull();
|
|
expect(isLockHolderLive(live!, SUPERVISOR_LOCK_TTL_MIN)).toBe(true);
|
|
await holder!.release();
|
|
// After release the row is gone → not running.
|
|
const gone = await inspectLock(engine, supervisorLockId('default'));
|
|
expect(gone).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('#1849 refresh-failure fails safe (F1A)', () => {
|
|
test('exits LOCK_LOST after the failure threshold; tolerates a single blip', async () => {
|
|
const sup = new MinionSupervisor(engine, { cliPath: '/bin/sh', healthInterval: 0, json: true });
|
|
const exitSpy = spyOn(process, 'exit').mockImplementation(((_code?: number) => {
|
|
throw new Error(`exit:${_code}`); // stop execution like the real exit would
|
|
}) as never);
|
|
|
|
let refreshCalls = 0;
|
|
const failingLock: DbLockHandle = {
|
|
id: 'x',
|
|
refresh: async () => { refreshCalls++; throw new Error('pooler down'); },
|
|
release: async () => {},
|
|
};
|
|
sup._setDbLockForTests(failingLock);
|
|
|
|
try {
|
|
// First two failures: tolerated (counter climbs, no exit).
|
|
await sup._refreshDbLockForTests();
|
|
await sup._refreshDbLockForTests();
|
|
expect(exitSpy).not.toHaveBeenCalled();
|
|
// Third failure crosses the threshold → shutdown → process.exit(LOCK_LOST).
|
|
try { await sup._refreshDbLockForTests(); } catch { /* exit stub throws */ }
|
|
expect(exitSpy).toHaveBeenCalledWith(ExitCodes.LOCK_LOST);
|
|
expect(refreshCalls).toBe(3);
|
|
} finally {
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
|
|
test('a successful refresh resets the failure counter', async () => {
|
|
const sup = new MinionSupervisor(engine, { cliPath: '/bin/sh', healthInterval: 0, json: true });
|
|
const exitSpy = spyOn(process, 'exit').mockImplementation(((_code?: number) => {
|
|
throw new Error(`exit:${_code}`);
|
|
}) as never);
|
|
|
|
let mode: 'fail' | 'ok' = 'fail';
|
|
const flakyLock: DbLockHandle = {
|
|
id: 'x',
|
|
refresh: async () => { if (mode === 'fail') throw new Error('blip'); },
|
|
release: async () => {},
|
|
};
|
|
sup._setDbLockForTests(flakyLock);
|
|
|
|
try {
|
|
await sup._refreshDbLockForTests(); // fail 1
|
|
await sup._refreshDbLockForTests(); // fail 2
|
|
mode = 'ok';
|
|
await sup._refreshDbLockForTests(); // success → reset
|
|
mode = 'fail';
|
|
await sup._refreshDbLockForTests(); // fail 1 again
|
|
await sup._refreshDbLockForTests(); // fail 2
|
|
// Counter was reset, so we are NOT past threshold yet.
|
|
expect(exitSpy).not.toHaveBeenCalled();
|
|
} finally {
|
|
exitSpy.mockRestore();
|
|
}
|
|
});
|
|
});
|