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: cap lifetime stall nudges so a looping TUI agent stops being told to continue

Adam Eivycd989849e2ca
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index 672da7384c..9c68aa50b4 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -1569,6 +1569,13 @@ export const STALL_NUDGE_RECOVERY_MS = 30000;
// three of them is wedged below the composer (a dead child, a TUI that stopped
// reading its PTY), and pasting into it forever only fills raw.txt.
export const STALL_NUDGE_MAX_ATTEMPTS = 3;
+// Lifetime ceiling across every streak of one session. The recovery reset above
+// trusts "it printed for 30s" as proof the nudge worked, but a model that has
+// lost the thread answers every nudge with a paragraph ("the story is finished")
+// and then stops again — each reply clears the streak, so the per-streak budget
+// never runs out and the run loops for hours on a local GPU. A run that has
+// needed this many nudges is not going to finish on the next one.
+export const STALL_NUDGE_MAX_TOTAL = 6;
// What gets pasted. It leads with the literal word a human used, for the literal
// reason it worked — the TUI still holds the whole conversation and the model
// only needs a turn — and then closes the two doors that produce this state in
@@ -1591,20 +1598,26 @@ export const STALL_NUDGE_TEXT = 'continue — this session went quiet with its t
* it, and the sibling gates all announce their verdict.
* - 0 — nothing to do
*
+ * Recovery refills the per-streak budget but never the lifetime one
+ * (`maxTotal`), so a session that answers every nudge and then stops again is
+ * still reported as wedged instead of being nudged forever.
+ *
* The post-nudge wait is measured from the NUDGE, not from output, and that is
* the load-bearing detail: a session wedged below its composer never echoes the
* paste, so a purely output-based gate would re-fire on the very next poll tick
* and paste continuously until the budget ran out seconds later.
*
- * @param {{ idleMs?: number, recoveryMs?: number, maxAttempts?: number }} [options]
- * @returns {{ takeNudge: (nowMs: number, lastOutputAtMs: number) => number|'exhausted' }}
+ * @param {{ idleMs?: number, recoveryMs?: number, maxAttempts?: number, maxTotal?: number }} [options]
+ * @returns {{ takeNudge: (nowMs: number, lastOutputAtMs: number) => number|'exhausted', readonly nudgesSent: number }}
*/
export function createStallNudgeGate({
idleMs = STALL_NUDGE_IDLE_MS,
recoveryMs = STALL_NUDGE_RECOVERY_MS,
maxAttempts = STALL_NUDGE_MAX_ATTEMPTS,
+ maxTotal = STALL_NUDGE_MAX_TOTAL,
} = {}) {
let attempts = 0;
+ let total = 0;
let nudgedAt = null;
let reportedExhausted = false;
return {
@@ -1619,14 +1632,16 @@ export function createStallNudgeGate({
}
if (nowMs - lastOutputAtMs < idleMs) return 0;
if (nudgedAt !== null && nowMs - nudgedAt < idleMs) return 0;
- if (attempts >= maxAttempts) {
+ if (attempts >= maxAttempts || total >= maxTotal) {
if (reportedExhausted) return 0;
reportedExhausted = true;
return 'exhausted';
}
attempts += 1;
+ total += 1;
nudgedAt = nowMs;
return attempts;
},
+ get nudgesSent() { return total; },
};
}
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index d12334f480..220d61eb1a 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -15,6 +15,7 @@ import {
OOM_NUDGE_COOLDOWN_MS,
OOM_NUDGE_MAX_ATTEMPTS,
createStallNudgeGate,
+ STALL_NUDGE_MAX_TOTAL,
STALL_NUDGE_IDLE_MS,
STALL_NUDGE_RECOVERY_MS,
STALL_NUDGE_MAX_ATTEMPTS,
@@ -2097,6 +2098,24 @@ describe('createStallNudgeGate', () => {
const revivedAt = now + STALL_NUDGE_RECOVERY_MS + 1;
expect(gate.takeNudge(revivedAt + STALL_NUDGE_IDLE_MS, revivedAt)).toBe(1);
});
+
+ it('stops nudging a session that answers every nudge and then stalls again', () => {
+ // A local model that has lost the thread replies to each nudge with a
+ // paragraph and goes quiet again. Every reply clears the streak, so only the
+ // lifetime ceiling ends the loop instead of nudging it for hours.
+ const gate = createStallNudgeGate();
+ let lastOutputAt = 0;
+ let now = STALL_NUDGE_IDLE_MS;
+ for (let i = 1; i <= STALL_NUDGE_MAX_TOTAL; i += 1) {
+ expect(gate.takeNudge(now, lastOutputAt)).toBe(1);
+ lastOutputAt = now + STALL_NUDGE_RECOVERY_MS + 1;
+ now = lastOutputAt + STALL_NUDGE_IDLE_MS;
+ }
+ expect(gate.nudgesSent).toBe(STALL_NUDGE_MAX_TOTAL);
+ expect(gate.takeNudge(now, lastOutputAt)).toBe('exhausted');
+ expect(gate.takeNudge(now + STALL_NUDGE_IDLE_MS, lastOutputAt)).toBe(0);
+ expect(gate.nudgesSent).toBe(STALL_NUDGE_MAX_TOTAL);
+ });
});
// isPasteConfirmed answers "may I stop treating this as swallowed?", so an
diff --git a/server/services/agentTuiSpawning/sessionController.js b/server/services/agentTuiSpawning/sessionController.js
index a56a83ec27..1380c95651 100644
--- a/server/services/agentTuiSpawning/sessionController.js
+++ b/server/services/agentTuiSpawning/sessionController.js
@@ -57,6 +57,7 @@ import {
createStallNudgeGate,
STALL_NUDGE_MAX_ATTEMPTS,
STALL_NUDGE_TEXT,
+ STALL_NUDGE_MAX_TOTAL,
createMcpBootTracker,
MCP_BOOT_PASTE_DEADLINE_MS,
MCP_BOOT_PASTE_RETRY_DELAY_MS,
@@ -1602,12 +1603,13 @@ export function createTuiSessionController({
// this is the only moment the answer is acted on.
if (sentinelPresent()) return;
if (stalled === 'exhausted') {
- // Every nudge was ignored, so the session is wedged below its composer
- // rather than merely stopped. There is no ceiling left to reap it, so say
+ // Every nudge in the streak was ignored (wedged below its composer), or the
+ // run spent its lifetime budget answering nudges without ever finishing
+ // (a model looping on "continue"). Either way more nudges won't help. There is no ceiling left to reap it, so say
// so loudly and badge the card — an agent nobody can see is stuck is the
// condition this gate exists to end, and only a human can end this one.
- appendLine(`🛑 Session still idle after ${STALL_NUDGE_MAX_ATTEMPTS} nudges — it is not responding; open the Shell tab to take it over`);
- emitLog('warn', `🛑 TUI agent ${agentId} is wedged — ${STALL_NUDGE_MAX_ATTEMPTS} stall nudges went unanswered`, { agentId });
+ appendLine(`🛑 Session still idle after ${stallNudgeGate.nudgesSent} nudges — it is not finishing; open the Shell tab to take it over`);
+ emitLog('warn', `🛑 TUI agent ${agentId} is wedged — still idle after ${stallNudgeGate.nudgesSent} stall nudges`, { agentId });
persistence.updateAgent(agentId, { metadata: { phase: 'stalled' } }).catch((err) =>
emitLog('error', `TUI agent ${agentId} stalled-phase update failed: ${err?.message || err}`, { agentId }));
// Re-arm the onData handler's one-shot "phase: working" write, so a
@@ -1617,7 +1619,7 @@ export function createTuiSessionController({
return;
}
if (pasteController?.resubmit({ text: STALL_NUDGE_TEXT, label: 'stalled-session nudge' })) {
- appendLine(`🔁 Session idle with the task unfinished — nudged it to continue (attempt ${stalled}/${STALL_NUDGE_MAX_ATTEMPTS})`);
+ appendLine(`🔁 Session idle with the task unfinished — nudged it to continue (attempt ${stalled}/${STALL_NUDGE_MAX_ATTEMPTS}, ${stallNudgeGate.nudgesSent}/${STALL_NUDGE_MAX_TOTAL} this run)`);
}
}, PROVIDER_SIGNAL_POLL_MS);