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: keep a paused run paused when the runner re-adopts it, and record a refused TUI handoff (#4540)

[._.]/ Adam Eivy9deaec4f7a60
diff --git a/server/lib/agentRunEvents.js b/server/lib/agentRunEvents.js
index 05363f815b..37fcb22fcc 100644
--- a/server/lib/agentRunEvents.js
+++ b/server/lib/agentRunEvents.js
@@ -437,6 +437,24 @@ export function projectRunStates(events) {
*/
const isTerminal = (state) => state.status === 'completed' || state.status === 'failed';
+/**
+ * May an observation of live activity (a re-adoption, a stream re-attach) put
+ * this run back to `running`?
+ *
+ * Not if it is finished, and not if it is PAUSED. Pause is the one non-terminal
+ * state with its own explicit exit event (`run.resumed`): a paused run's process
+ * has been stopped, so nothing should be observing it live, and letting one
+ * flip the status would leave a projection reading `running` with `paused: true`
+ * beside it.
+ *
+ * `orphaned` and `interrupted` deliberately DO yield. Both mean a stop was
+ * observed or requested and neither guarantees it landed — a run re-adopted from
+ * the runner after the sweep called it dead, or after a kill it ignored, really
+ * is running, and that contradiction is the finding. The booleans stay true, so
+ * the history is not lost either way.
+ */
+const canObserveRunning = (state) => !isTerminal(state) && !state.paused;
+
function applyKind(state, event) {
const data = event.data ?? {};
switch (event.kind) {
@@ -455,7 +473,7 @@ function applyKind(state, event) {
// A survivor is still running — recovery is an annotation on a live run,
// not a terminal state. Only the count changes so a diagnostic can show
// "this run has been re-adopted N times".
- if (!isTerminal(state)) state.status = 'running';
+ if (canObserveRunning(state)) state.status = 'running';
state.recoveryCount += 1;
break;
case 'run.orphan-recovered':
@@ -471,7 +489,7 @@ function applyKind(state, event) {
break;
case 'run.reconnected':
state.reconnectCount += 1;
- if (!isTerminal(state)) state.status = 'running';
+ if (canObserveRunning(state)) state.status = 'running';
break;
case 'run.output':
// Sizes only — the bytes themselves never enter the ledger.
diff --git a/server/lib/agentRunEvents.test.js b/server/lib/agentRunEvents.test.js
index 947611dde7..78ce26e2de 100644
--- a/server/lib/agentRunEvents.test.js
+++ b/server/lib/agentRunEvents.test.js
@@ -371,6 +371,29 @@ describe('projectRunStates — the second-slice boundaries (#4540)', () => {
expect(state.startedAt).toBe(at('10:00'));
});
+ it('does not let a re-adoption talk a PAUSED run back into running', () => {
+ // Pause stops the process, so nothing should be observing it live; a
+ // projection reading `running` next to `paused: true` is self-contradictory.
+ const [state] = projectRunStates([
+ buildRunEvent({ kind: 'run.spawned', runId: 'r1', at: at('10:00') }),
+ buildRunEvent({ kind: 'run.paused', runId: 'r1', at: at('10:30') }),
+ buildRunEvent({ kind: 'run.runner-recovered', runId: 'r1', at: at('10:40') })
+ ]);
+ expect(state).toMatchObject({ status: 'paused', paused: true, recoveryCount: 1 });
+ });
+
+ it('DOES let a re-adoption revive a run the orphan sweep called dead', () => {
+ // The other direction on purpose: the sweep observed no process, the runner
+ // says otherwise, and that contradiction is the finding — not something to
+ // hide by pinning the status to the sweep's reading. The flag stays true.
+ const [state] = projectRunStates([
+ buildRunEvent({ kind: 'run.spawned', runId: 'r1', at: at('10:00') }),
+ buildRunEvent({ kind: 'run.orphan-recovered', runId: 'r1', at: at('10:30') }),
+ buildRunEvent({ kind: 'run.runner-recovered', runId: 'r1', at: at('10:40') })
+ ]);
+ expect(state).toMatchObject({ status: 'running', orphaned: true });
+ });
+
it('records a REJECTED handoff without claiming an owner', () => {
const [state] = projectRunStates([
buildRunEvent({ kind: 'run.spawned', runId: 'r1', at: at('10:00') }),
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 48522458b5..8d114a378a 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -1203,6 +1203,19 @@ export async function spawnTuiAgent({
const reason = useDurableRunner && /^Command executable unavailable:/i.test(message)
? 'command-not-found'
: useDurableRunner ? 'spawn-rejected' : 'spawn-error';
+ if (useDurableRunner) {
+ // A handoff the runner REFUSED (#4540), recorded like the CLI path's. A
+ // LOCAL PTY that won't open is a host problem, not a handoff, so it is
+ // deliberately not recorded here.
+ await appendRunEvent({
+ kind: 'run.handoff',
+ runId,
+ agentId,
+ taskId: task.id,
+ eventId: `handoff:${agentId}:${runId || 'no-run'}:rejected`,
+ data: { to: 'none', accepted: false, kind: 'tui', reason: message },
+ });
+ }
await finish({
success: false,
exitCode: 1,