Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([issue-3202]): handle a shutdown-marker rejection and keep the paused branch on pause+restart races

[._.]/ Adam Eivy6d7656f90892
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index c4b506098b..2354c15983 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -442,12 +442,17 @@ export async function spawnTuiAgent({
// from being written down as completed AND keeps its worktree (which
// finalize's cleanup would delete) intact for the resume (#3202).
//
- // Two exceptions finalize normally. An agent that already wrote its
- // `.agent-done` sentinel has given a valid completion signal. And a run the
- // user terminated must reach finalizeAgent to be recorded `user-terminated`
- // — abandoning it would leave the record `running` with no such mark, and
- // boot recovery's user-terminated skip would miss it and resurrect the run.
- if (isHostShuttingDown() && !sentinelPresent() && !userTerminatedAgents.has(agentId)) {
+ // Three exceptions keep their normal path. An agent that already wrote its
+ // `.agent-done` sentinel has given a valid completion signal. A run the user
+ // terminated must reach finalizeAgent to be recorded `user-terminated` —
+ // abandoning it would leave the record `running` with no such mark, and boot
+ // recovery's user-terminated skip would miss it and resurrect the run. And a
+ // run the user paused already has its own don't-finalize branch below, which
+ // owns the paused bookkeeping (pid unregister, activeAgents delete).
+ if (isHostShuttingDown()
+ && !sentinelPresent()
+ && !userTerminatedAgents.has(agentId)
+ && !pausedAgents.has(agentId)) {
await abandonForHostShutdown();
return;
}
diff --git a/server/services/bootstrap.js b/server/services/bootstrap.js
index 572eceff89..5a3ee3e0df 100644
--- a/server/services/bootstrap.js
+++ b/server/services/bootstrap.js
@@ -880,14 +880,16 @@ export const registerShutdownHandlers = ({ io, httpServer, localHttpServer }) =>
// fault the agent didn't cause (#3202). Only `activeAgents` — the ones whose
// child processes live in THIS process's tree — belong here; runner-mode
// agents are owned by portos-cos and survive this restart untouched.
- // Best-effort and bounded: a marker we fail to write only degrades recovery
- // to the pre-existing orphan path.
- // Started here (the agent snapshot must be taken before anything can await)
- // but awaited just before exit, so a stalled filesystem spends none of the
- // graceful budget ahead of the teardown below. writeHostShutdownMarker logs
- // both outcomes itself and never rejects, so `finish` needs no message.
+ //
+ // Started here so the agent snapshot is taken before anything can await, but
+ // awaited just before exit so a stalled filesystem spends none of the graceful
+ // budget ahead of the teardown below. Best-effort throughout: the helper logs
+ // both outcomes and never rejects (hence `finish()` with no message), the
+ // `.catch` is the belt-and-suspenders for an out-of-lifecycle rejection, and a
+ // marker we fail to write only degrades recovery to the pre-existing orphan path.
const markerWritten = withGrace('Host-shutdown marker', 1500, (finish) =>
- writeHostShutdownMarker({ agentIds: [...activeAgents.keys()], signal }).then(() => finish()));
+ writeHostShutdownMarker({ agentIds: [...activeAgents.keys()], signal })
+ .then(() => finish(), (err) => finish(`⚠️ Host-shutdown marker failed: ${err.message}`, true)));
// Drop existing long-lived sockets (SSE + keep-alive) up front so the closes
// below don't wait on connections that never end on their own.