Showcase: Distributed Agent Recovery (Zombie Worker & LIFO Sagas)
Ghost in the Machine: The Zombie AI Worker
It lost its lease. Another worker took over. Then the zombie came back.
The worker didn’t survive. The workflow did.
agbossjs/distributed-agent-recovery is a standalone, multi-process distributed AI release pipeline built on @agboss/core@0.1.0 and @agboss/cli@0.1.0. It demonstrates how AGBoss coordinates multiple OS worker processes, survives mid-execution worker freezes (SIGSTOP / lease expiry), rolls back partial external commits via deterministic LIFO sagas, and delegates root-cause recovery planning to an LLM agent without ever letting a probabilistic model manage distributed locks or cleanup state.
- GitHub Repository:
github.com/agbossjs/distributed-agent-recovery - Recorded Live LLM + Real-Clock
SIGSTOPEvidence:docs/evidence/live-real-clock-cluster-run.json(openai/gpt-4o-mini,$0.00036255total spend, 2 OS worker PIDs, 30s lease takeoverepoch 1 -> 2, linear unbranched checkpoint chain) - Automated CI Gates:
$0deterministic offline suite (testWith&mockLlm), real-clock multi-process POSIXSIGSTOP/SIGCONTrecovery (--cluster --real-clock), 4-container Docker Compose + hard worker container death (exit 137) failover, and single-node Kubernetes (kind).
Why Multi-Worker AI Pipelines Need Deterministic Recovery
Section titled “Why Multi-Worker AI Pipelines Need Deterministic Recovery”When teams move AI workflows from single-process prototypes to multi-worker production clusters, two distributed failure modes appear immediately:
- Partial External Commits Before a Gate Failure: Multiple worker processes concurrently compile and write real artifacts to disk and a shared database, and a downstream integration smoke check fails. Asking an LLM to “clean up whatever failed” via tool calls is non-deterministic and unsafe; orphaned files corrupt the next attempt. AGBoss solves this with statically verified LIFO Saga Compensation (
@Action({ compensateWith })). - The Zombie Worker (GC Pause, I/O Stall, or Container Freeze): Worker A claims a child task under
leaseEpoch = 1, commits its Step 1 checkpoint, and freezes during Step 2 until its 30-second execution lease expires. Worker B claims the orphaned child execution underleaseEpoch = 2, resumes from Worker A’s Step 1 checkpoint (execution.recovered), and commits Step 2. Then Worker A wakes up and attempts to commit its stale continuation. AGBoss prevents split-brain corruption through monotonicleaseEpochfencing at both the external side-effect boundary and the runtime checkpoint saver.
End-to-End 12-Step Execution Architecture
Section titled “End-to-End 12-Step Execution Architecture”The showcase runs a two-attempt software release pipeline for three modules (auth-service, payment-engine, and telemetry-collector) across a Coordinator and two independent Worker OS processes sharing a SQLite WAL cluster store and external release ledger:
Coordinator Worker A (PID 1) Worker B (PID 2) Shared SQLite WAL + Ledger─────────── ──────────────── ──────────────── ──────────────────────────ATTEMPT 1: Parallel Staging, Gate Failure, LIFO Saga Rollback & AI Recovery Plan1. DecomposeReleaseTasks ───────────────────────────────────────────────────► 3 module tasks created2. toParallelWorkflows(ModuleWorkerWorkflow, ChildPlacement.AnyInstance) ───► enqueue 3 child runs 3a. Claim & stage 3b. Claim & stage auth-service telemetry-collector (+ payment-engine) (.artifact.json)4. LockReleaseCandidate (compensateWith: CompensateReleaseCandidateAction) ─► candidate LOCKED5. VerifyReleaseSmokeGate ──► throws ReleaseGateFailedError (missing --abi-compat=v2)6. .onError(ReleaseGateFailedError, ...) ├─ CompensateReleaseCandidateAction ─────────────────────────────────────► deletes 3 staged files, │ marks Attempt 1 COMPENSATED └─ ReleaseRecoveryPlannerAgent (openai/gpt-4o-mini) └─ calls InspectFailedAttemptTool ────────────────────────────────────► verifies allCompensated=true, emits RecoveryPlanDto
ATTEMPT 2: Same-Task Checkpoint Takeover & Two-Layer Zombie Worker Fencing7. Retry with AI-corrected flags (--opt-level=3 --deterministic --abi-compat=v2) 8a. Claim payment-engine (executionId E1, leaseEpoch=1) & commit Step 1 checkpoint (PrepareModuleCandidate) 8b. Freeze in Step 2 (SIGSTOP / stall > 30s TTL) 9a. Reclaim SAME executionId E1 with leaseEpoch=2 (execution.recovered) from Worker A's Step 1 checkpoint 9b. Commit Step 2 under leaseEpoch=2 10. Wake up (SIGCONT) with stale leaseEpoch=1 11a. Layer 1 Fence: Ledger rejects epoch 1 < 2 (ZOMBIE_WRITE_REJECTED / StaleLeaseFenceError) 11b. Layer 2 Fence: LeasedCheckpointSaver rejects renew(epoch=1) ──► ExecutionLeaseLostError12. VerifyReleaseSmokeGate passes ──► PromoteReleaseArtifactsAction ────────► release-manifest.jsonHow AGBoss Works Under the Hood
Section titled “How AGBoss Works Under the Hood”1. Declarative Distributed Fan-Out, LIFO Saga & AI Error Recovery
Section titled “1. Declarative Distributed Fan-Out, LIFO Saga & AI Error Recovery”ReleaseCoordinatorWorkflow expresses the entire distributed release lifecycle in one statically verified HarnessDSL (HDSL) graph:
- Distributed child fan-out (
ChildPlacement.AnyInstance):.toParallelWorkflows(ModuleWorkerWorkflow, { placement: ChildPlacement.AnyInstance })writes child workflow tasks toSqliteChildExecutionQueueRepositoryand yields the parent’s own execution lease while waiting so the coordinator never blocks worker slots. - Compile-time compensation enforcement: Because
LockReleaseCandidateActiondeclarescompensateWith: CompensateReleaseCandidateAction,agboss checkstatically verifies that every.onError(...)path leavingVerifyReleaseSmokeGateActioninvokesCompensateReleaseCandidateActionbefore reachingReleaseRecoveryPlannedWorkflowFinish(otherwise compilation fails with[flow.uncompensated-action]). - Role segregation (
@Actionvs@Agent): Deterministic steps (DecomposeReleaseTasksAction,CompensateReleaseCandidateAction,PromoteReleaseArtifactsAction) handle filesystem and database mutations, whileReleaseRecoveryPlannerAgent(model: "openai/gpt-4o-mini") runs only after compensation completes, querying diagnostics viaInspectFailedAttemptToolto produce a typedRecoveryPlanDto.
import { ChildPlacement, StepFailedError, Workflow, action, start, toAction,} from "@agboss/core";
@Workflow({ name: "module-worker-workflow", version: "1.0.0", flow: [ start(ModuleWorkerWorkflowStart) .returns(ModuleTaskDto) .toAction(PrepareModuleCandidateAction) .onError(StepFailedError, toAction(MarkModuleFailedAction)) .returns(PreparedModuleCandidateDto) .toAction(StageModuleArtifactAction) .onError(StepFailedError, toAction(MarkModuleFailedAction)) .returns(StagedModuleArtifactDto) .toFinish(ModuleWorkerWorkflowFinish),
action(MarkModuleFailedAction) .returns(ModuleFailedDto) .toFinish(ModuleFailedWorkflowFinish), ],})export class ModuleWorkerWorkflow {}
@Workflow({ name: "release-coordinator-workflow", version: "1.0.0", flow: [ start(ReleaseCoordinatorWorkflowStart) .returns(ReleaseRequestDto) .toAction(DecomposeReleaseTasksAction) .returns(listOf(ModuleTaskDto)) .toParallelWorkflows(ModuleWorkerWorkflow, { maxConcurrency: 4, maxItems: 8, placement: ChildPlacement.AnyInstance, }) .join() .returns(listOf(oneOf(StagedModuleArtifactDto, ModuleFailedDto))) .toAction(LockReleaseCandidateAction) .returns(LockedReleaseCandidateDto) .toAction(VerifyReleaseSmokeGateAction) .onError( ReleaseGateFailedError, toAction(CompensateReleaseCandidateAction) .toAction(SummarizeCompensatedAttemptAction) .returns(CompensatedReleaseCandidateDto) .toAgent(ReleaseRecoveryPlannerAgent) .returns(RecoveryPlanDto) .toFinish(ReleaseRecoveryPlannedWorkflowFinish), ) .toAction(PromoteReleaseArtifactsAction) .returns(ReleaseSummaryDto) .toFinish(ReleaseCoordinatorWorkflowFinish), ],})export class ReleaseCoordinatorWorkflow {}2. Two-Layer Zombie Worker Lease Fencing (leaseEpoch + LeasedCheckpointSaver)
Section titled “2. Two-Layer Zombie Worker Lease Fencing (leaseEpoch + LeasedCheckpointSaver)”A critical distributed-systems detail demonstrated by this showcase is why two fencing layers are required when an @Action mutates external state (such as a database or object store):
- Layer 1 — External Side-Effect Boundary (
leaseEpochFencing Token +step.idempotencyKey):
AGBoss’s runtimeLeasedCheckpointSaverrenews and validates the 30-second execution lease when a step finishes and writes its LangGraph checkpoint. However, an@Action’s external side effect runs insideexecute()—before returning control to LangGraph. If Worker A freezes insideexecute()right before writing to the external database, Worker B will reclaim the expired lease withleaseEpoch = 2, resume from Worker A’s Step 1 checkpoint (PrepareModuleCandidateAction), and commit Step 2.
To prevent Worker A’s delayed write from overwriting Worker B’s committed artifact,StageModuleArtifactActioncaptures the active(instanceId, leaseEpoch)token at step entry and validatescapturedLeaseEpoch >= activeLeaseEpochtransactionally inside the SQLite release ledger alongsideexecutionContext.step.idempotencyKey. When Zombie Worker A wakes up,1 < 2triggersStaleLeaseFenceError(ZOMBIE_WRITE_REJECTED), andexecutionContext.signal.abortedconfirms lease loss. - Layer 2 — AGBoss Runtime Checkpoint Boundary (
LeasedCheckpointSaver):
After catchingStaleLeaseFenceErrorand returning from the action, LangGraph attempts to persist Worker A’s checkpoint viaLeasedCheckpointSaver.putWrites(). That callsSqliteExecutionLeaseRepository.renew({ leaseEpoch: 1 }), which returnsfalse(RUNTIME_LEASE_FENCED) and throwsExecutionLeaseLostError(execution.lease-lost)—terminating the zombie execution without running.onErrorhandlers or touching Worker B’s completed state.
import { AGBossError, Action, ApplicationContext, ExecutionContext, Injectable, type ActionHandler, type InputOf, type OutputOf,} from "@agboss/core";
export class StaleLeaseFenceError extends AGBossError { static override readonly code = "release.stale_lease_fence";
constructor(executionId: string, capturedEpoch: number, activeEpoch: number) { super( `Fenced stale write for ${executionId}: epoch ${String(capturedEpoch)} < ${String(activeEpoch)}`, ); this.name = "StaleLeaseFenceError"; }}
@Injectable({ deps: [ApplicationContext] })export class ReleaseLedgerFencingService { constructor(private readonly appContext: ApplicationContext) {}
async captureActiveLease(executionId: string): Promise<{ instanceId: string; leaseEpoch: number }> { void executionId; return { instanceId: this.appContext.instanceId, leaseEpoch: 1 }; }
async commitStagedArtifactFenced(params: { logicalTaskId: string; executionId: string; capturedInstanceId: string; capturedLeaseEpoch: number; idempotencyKey: string; activeLeaseEpoch?: number; }): Promise<{ artifactId: string }> { const currentEpoch = params.activeLeaseEpoch ?? params.capturedLeaseEpoch; if (params.capturedLeaseEpoch < currentEpoch) { throw new StaleLeaseFenceError( params.executionId, params.capturedLeaseEpoch, currentEpoch, ); } return { artifactId: `art:${params.logicalTaskId}:${params.idempotencyKey}` }; }}
@Action({ name: "stage-module-artifact-action", description: "Commits a lease-fenced artifact record using leaseEpoch and step.idempotencyKey", input: { preparedCandidate: PreparedModuleCandidateDto }, output: StagedModuleArtifactDto, deps: [ReleaseLedgerFencingService, ExecutionContext],})export class StageModuleArtifactAction implements ActionHandler<StageModuleArtifactAction> { constructor( private readonly ledger: ReleaseLedgerFencingService, private readonly executionContext: ExecutionContext, ) {}
async execute( input: InputOf<StageModuleArtifactAction>, ): Promise<OutputOf<StageModuleArtifactAction>> { const candidate = input.preparedCandidate.value; const lease = await this.ledger.captureActiveLease(this.executionContext.executionId);
// Layer 1 Fence: transactional leaseEpoch + step.idempotencyKey check at external DB boundary. // Layer 2 Fence: LeasedCheckpointSaver subsequently verifies renew(leaseEpoch) on checkpoint write. const staged = await this.ledger.commitStagedArtifactFenced({ logicalTaskId: candidate.logicalTaskId, executionId: this.executionContext.executionId, capturedInstanceId: lease.instanceId, capturedLeaseEpoch: lease.leaseEpoch, idempotencyKey: this.executionContext.step.idempotencyKey, });
return { logicalTaskId: candidate.logicalTaskId, artifactId: staged.artifactId, }; }}3. Deterministic $0 Offline Testing (testWith & mockLlm)
Section titled “3. Deterministic $0 Offline Testing (testWith & mockLlm)”Every failure mode in the showcase—smoke gate failure, LIFO saga rollback, tool-assisted AI recovery planning, and multi-instance child takeover—is verified offline in milliseconds with testWith and mockLlm:
import { Application, EmptyEnvironment } from "@agboss/core";import { callTool, replyWith, testWith } from "@agboss/core/testing";import { expect } from "vitest";
@Application({ name: "distributed-agent-recovery-app", version: "1.0.0", environment: EmptyEnvironment, workflows: [ReleaseCoordinatorWorkflow], budgets: [ShowcaseAppBudget, ShowcaseTenantBudget, ShowcaseExecBudget],})export class DistributedAgentRecoveryApp {}
const test = testWith(DistributedAgentRecoveryApp);
test("compensates Attempt 1 on smoke gate failure, plans AI recovery, and promotes Attempt 2", async ({ app, mockLlm,}) => { const correctedFlags = "--opt-level=3 --deterministic --abi-compat=v2"; mockLlm(ReleaseRecoveryPlannerAgent).thenReturn( callTool(InspectFailedAttemptTool, { releaseId: "rel-2026.10.0" }), replyWith(RecoveryPlanDto, { correctedBuildFlags: correctedFlags, }), );
// Attempt 1 omits --abi-compat=v2 -> triggers LIFO compensation + AI recovery plan const attempt1 = await app.execute( ReleaseCoordinatorWorkflowStart, { releaseId: "rel-2026.10.0", buildFlags: "--opt-level=3 --deterministic", }, { record: true }, ); expect(attempt1).toFinishWith(ReleaseRecoveryPlannedWorkflowFinish, { correctedBuildFlags: correctedFlags, }); expect(attempt1).toHaveCompensated(LockReleaseCandidateAction, { by: CompensateReleaseCandidateAction, }); expect(attempt1).toHaveCalledTools([InspectFailedAttemptTool]);
// Attempt 2 applies the AI-corrected flags -> passes smoke gate & promotes release const attempt2 = await app.execute(ReleaseCoordinatorWorkflowStart, { releaseId: "rel-2026.10.0", buildFlags: correctedFlags, }); expect(attempt2.finish).toBe(ReleaseCoordinatorWorkflowFinish);});Multi-Process Load Balancing & Real 30s Wall-Clock Proof
Section titled “Multi-Process Load Balancing & Real 30s Wall-Clock Proof”In addition to fast virtual-clock tests, agbossjs/distributed-agent-recovery runs as a compiled multi-process OS application (node dist/src/demo.js --cluster --real-clock) spawning independent node dist/worker/main.js processes over a shared SQLite WAL database with AGBoss’s untouched 30-second sweep timer (Clock) and real POSIX SIGSTOP / SIGCONT process signals:
- Cross-Process Sibling Fairness (
ClusterSqliteChildExecutionQueueRepository): In multi-process clusters, in-memoryEventEmitterwakeups do not cross OS process boundaries; workers discover queuedChildPlacement.AnyInstancetasks on their periodic sweep ticks. By measuring a sibling fairness grace window frommin(claimed_at)of the batch, the first awakened worker claims its share and yields remaining sibling tasks so peer worker PIDs participate concurrently (1 / 2task split across Worker Alpha and Worker Beta in both Attempt 1 and Attempt 2). - Expired-Lease
RunningChild Reclamation: When Worker Alpha freezes (kill -STOP <pid>) after checkpointing Step 1 ofpayment-engine, its row inchild_execution_queueremainsRunningwhileexecution_leases.expires_atpasses the 30-second wall-clock TTL. On the next sweep, Worker Beta atomically requeues the expired sibling row, acquiresleaseEpoch = 2, logsexecution.recovered, loads Worker Alpha’s Step 1 checkpoint without re-running Step 1, and commits Step 2. - Live OpenRouter Verification (
--live): WithOPENROUTER_API_KEYset,ReleaseRecoveryPlannerAgentcallsopenai/gpt-4o-minilive (~$0.00036total run cost), invokesInspectFailedAttemptToolagainst SQLite to verify Attempt 1 compensation, and returns the structuredRecoveryPlanDtothat drives Attempt 2 to completion.
The 8 Automated Invariants
Section titled “The 8 Automated Invariants”Every demo and CI run executes ReleaseInvariantVerifierService against both the application’s SQLite ledger tables (release_artifacts, release_candidate_locks, fencing_tokens, ledger_events), AGBoss’s internal SQLite cluster tables (execution_leases, checkpoints, child_execution_queue) via ClusterSnapshotRepository, and on-disk *.manifest.json files, exiting non-zero if any invariant is violated:
| # | Invariant ID | What It Proves |
|---|---|---|
| 1 | INV-1-ALL-MODULES-COMPLETED |
All 3 expected modules (auth-service, payment-engine, telemetry-collector) reached PROMOTED status in Attempt 2. |
| 2 | INV-2-FINAL-ARTIFACTS-EXIST |
release-manifest.json and all 3 promoted *.manifest.json files exist on disk and match SQLite ledger records. |
| 3 | INV-3-NO-FAILED-ATTEMPT-ARTIFACTS-ACTIVE |
Zero Attempt 1 rows remain active in SQLite and zero Attempt 1 .artifact.json staging files remain on disk. |
| 4 | INV-4-SAGA-COMPENSATIONS-RECORDED-AND-APPLIED |
CompensateReleaseCandidateAction marked Attempt 1’s candidate lock and all 3 module artifacts COMPENSATED with its step.idempotencyKey. |
| 5 | INV-5-NO-DUPLICATE-EXTERNAL-COMMITS |
Every committed row carries a unique step.idempotencyKey and at most one active artifact exists per (release_id, attempt_number, logical_task_id). |
| 6 | INV-6-MULTI-INSTANCE-PARTICIPATION |
At least 2 distinct AGBoss worker instanceIds executed child workflows across the fixed worker pool. |
| 7 | INV-7-ZOMBIE-WORKER-FENCED |
Verifies Layer 1 (ZOMBIE_WRITE_REJECTED) and Layer 2 (RUNTIME_LEASE_FENCED when the zombie resumes, or hard process death) plus independent verification of AGBoss’s execution_leases (lease_epoch >= 2, holder_instance_id !== stalledInstanceId, released = 1) and checkpoints (strictly linear unbranched parent_checkpoint_id chain proving zero split-brain zombie checkpoint writes). |
| 8 | INV-8-ORIGINAL-LOGICAL-TASK-COMPLETED-AFTER-TAKEOVER |
rel-2026.10.0:payment-engine preserved the exact same childExecutionId and idempotencyKey, with Step 1 prepared by Worker A (leaseEpoch = 1), Step 2 completed by Worker B (leaseEpoch = 2), execution_leases.holder_instance_id matching the committer, and 0 orphaned rows in child_execution_queue. |
Quickstart Commands
Section titled “Quickstart Commands”Clone agbossjs/distributed-agent-recovery (requires Node.js >= 22.13.0 for built-in node:sqlite):
git clone https://github.com/agbossjs/distributed-agent-recovery.gitcd distributed-agent-recovery
# 1. Recommended first-time workflow (100% offline default, $0 cost, no API key needed)npm cinpm run checknpm testnpx tsx src/demo.ts --clusternpx tsx src/demo.ts --verify-only
# 2. Optional: Live OpenRouter inference (openai/gpt-4o-mini + InspectFailedAttemptTool)OPENROUTER_API_KEY="sk-or-v1-..." npx tsx src/demo.ts --live
# 3. Optional: Compiled multi-process OS cluster with untouched 30s lease TTL & POSIX SIGSTOP/SIGCONTnpm run buildnode dist/src/demo.js --cluster --real-clock
# 4. Optional: Combine real-clock POSIX SIGSTOP cluster with live OpenRouter inferenceOPENROUTER_API_KEY="sk-or-v1-..." node dist/src/demo.js --cluster --live --real-clock
# 5. Optional: 4-container Docker Compose cluster & clean volume teardowndocker compose -f deploy/compose/docker-compose.yml up -d --build --wait worker-alpha worker-betadocker compose -f deploy/compose/docker-compose.yml run --rm -T --no-deps coordinator node dist/src/demo.jsdocker compose -f deploy/compose/docker-compose.yml down -v --remove-orphansCanonical Directory Layout & Guarantee Boundaries
Section titled “Canonical Directory Layout & Guarantee Boundaries”The repository enforces strict <kind>/<name>.<kind>.<ext> role separation (1 class = 1 file, < 200 lines per file, Drizzle ORM repositories in src/repositories/, Drizzle table schemas in src/schemas/, pure .sql DDL migrations in src/sql/, domain errors in src/errors/, stateless @Injectable services in src/services/, and symmetric tests/ mirroring src/).