fix(engine): tombstone on node-completed release instead of deleting
The defect
relaycast#309 gave the local release path (dispatchRelease → completeLocally) a tombstone, because four FKs reference agents.id with no ON DELETE action — messages.agent_id, channels.created_by, files.uploaded_by, webhooks.created_by — so a bare DELETE is refused for any agent that has ever spoken.
The node-completed path (applyReleaseCompletionEffect) kept the bare DELETE. It runs inside the completion's atomic unit, so that FK refusal aborts the invocation completion along with it. The result is the worst shape available: the invocation sits at dispatched forever, the seat and the name stay claimed, and the caller receives a plausible-looking receipt for work that never happened.
Evidence from production
Observed 2026-08-15 on a live workspace while recovering a restarted node. Across five agents the split was exactly "has authored messages / has not":
| agent | channel posts | remove |
|---|---|---|
relay-e2e | 8 | FAILS |
relay-terminal | 8 | FAILS |
relay-terminal2 | 2 | FAILS |
relay-dmresolve | 0 | succeeds |
relay-e2epr | 0 | succeeds |
The two that succeeded had posted nothing only because a separate outage meant they never received their briefs — an accidental but clean control group. Three seats remain stuck on that workspace.
On the CLI side this surfaced as a raw SQL leak straight to the operator:
Failed query: delete from "agents" where "agents"."id" = ? params: 214376604123013120
Operationally this is backwards. Only agents that never spoke can be reclaimed, while the ones worth reclaiming are exactly the ones that did work — so a node whose agents were productive cannot be fully recovered after a restart. This is what pinned every subsequent fleet spawn at pending (activeAgents: 6 against zero live processes).
The fix
Apply the same tombstone the local path already proved correct: rename via releasedAgentName (freeing the unique (workspace_id, name) immediately), rotate token_hash so the surviving row's credential stops authenticating, set RELEASED_AGENT_STATUS, and clear the routing location. Every FK target stays valid and every message keeps its sender.
No new mechanism is introduced — this reuses #309's primitives so both paths now agree.
Verification
nodeCompletedRelease.test.ts, a must-fire / must-not-fire pair driven over the node control channel (action.result) — the engine correctly refuses off-channel completion of node-owned invocations, so the test has to do it the way a broker does.
Both directions demonstrated, not asserted:
- without this change: the has-history case fails, the no-history case still passes
- with it: both pass
The no-history arm exists so the fix cannot pass by changing the common path.
Known unrelated failures
a2aFederation and providerAttachRace fail in the full suite both with and without this change, and the failing set varies between runs (twoNodeWriteContention appeared in one run, not another). Pre-existing flakiness in the concurrency tests; not caused by this fix and not addressed here.
Not merged
Khaliq owns the merge gate.
🤖 Generated with Claude Code