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

address review round 4 (codex P2s): observe work only post-submit + scope failure to counter-rendering TUIs

[._.]/ Adam Eivy57ed53b481ed
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index db671a9139..1ecc27a4b7 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -250,6 +250,27 @@ export function inferTuiCommand(id) {
return 'claude';
}
+/**
+ * True when the given TUI command renders the elapsed working counter
+ * (`(Ns ·` / `(Ns •`) that `createWorkActivityTracker` keys on. Only Claude
+ * Code and Codex are known to render it; Antigravity/Gemini/other TUIs do not.
+ *
+ * The work-activity idle gate (issue #1229) must consult this before
+ * downgrading a sentinel-less idle-out to failure: on a provider that never
+ * renders the counter, absence of the signal proves nothing, so the original
+ * permissive idle-complete=success behavior must be preserved (otherwise every
+ * sentinel-less completion on those providers would falsely fail).
+ *
+ * @param {string} commandName — the spawned binary's basename (e.g. `claude`,
+ * `codex`, `agy`, `gemini`).
+ * @returns {boolean}
+ */
+export function rendersWorkCounter(commandName) {
+ if (typeof commandName !== 'string') return false;
+ const lower = commandName.toLowerCase();
+ return lower.includes('claude') || lower.includes('codex');
+}
+
// Codex TUI blocks on every tool approval AND sandboxes file/network writes
// unless we run it fully bypassed. There's no human-at-keyboard for headless
// calls (one-shot OR agent), so inject the full-yolo flag — the same posture
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index 069a1260ee..f483c275f9 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -11,6 +11,7 @@ import {
MIN_WORK_COUNTER_SPAN_MS,
extractWorkCounterSeconds,
createWorkActivityTracker,
+ rendersWorkCounter,
PASTE_TO_ENTER_MIN_DELAY_MS,
PASTE_TO_ENTER_FALLBACK_MS,
SUBMIT_ENTER_ATTEMPTS,
@@ -170,6 +171,18 @@ describe('tuiHandshake — paste timing constants', () => {
expect(MIN_WORK_COUNTER_SPAN_MS).toBe(750);
});
+ it('rendersWorkCounter is true only for the counter-rendering TUIs (Claude Code / Codex)', () => {
+ // The idle gate may downgrade to failure ONLY for providers that render the
+ // counter; others must keep the permissive idle-complete (codex P2 / #1229).
+ expect(rendersWorkCounter('claude')).toBe(true);
+ expect(rendersWorkCounter('codex')).toBe(true);
+ expect(rendersWorkCounter('/usr/local/bin/claude')).toBe(true);
+ expect(rendersWorkCounter('agy')).toBe(false);
+ expect(rendersWorkCounter('gemini')).toBe(false);
+ expect(rendersWorkCounter('')).toBe(false);
+ expect(rendersWorkCounter(null)).toBe(false);
+ });
+
it('pins provider-default constants', () => {
expect(DEFAULT_TUI_PROMPT_DELAY_MS).toBe(2500);
expect(DEFAULT_TUI_IDLE_TIMEOUT_MS).toBe(180000);
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 379f3d7721..51c44e701f 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -29,6 +29,7 @@ import {
PASTE_MARKER_POLL_MS,
detectPasteMarker,
createWorkActivityTracker,
+ rendersWorkCounter,
PASTE_TO_ENTER_MIN_DELAY_MS,
PASTE_TO_ENTER_FALLBACK_MS,
scheduleSubmitEnters,
@@ -207,6 +208,12 @@ export async function spawnTuiAgent({
let sentinelIngested = false;
let hasStartedWorking = false;
let promptSentAt = null;
+ // When the submit-Enter is first written (NOT when the paste starts). Work-
+ // activity observation keys on this so the prompt ECHO — which renders during
+ // the paste→Enter window, before this is set — is never scanned for the working
+ // counter (issue #1229 review: a pasted transcript could otherwise echo counters
+ // that fake work before the prompt is actually submitted).
+ let promptSubmittedAt = null;
// Tracks evidence the model is actively processing the SUBMITTED prompt — the
// TUI's elapsed working counter advancing through ≥2 distinct values (see
// createWorkActivityTracker; echo-proof, unlike word-matching the prompt text).
@@ -538,13 +545,14 @@ export async function spawnTuiAgent({
// path dead.
if (postPasteBuffer !== null && stripped) postPasteBuffer += stripped;
const now = Date.now();
- // Once the prompt is submitted, watch for proof the model is actually
- // working. The TUI repaints chrome continuously even with an unsent prompt,
+ // Once the prompt is SUBMITTED (Enter sent — not merely pasted), watch for
+ // proof the model is actually working. Keying on promptSubmittedAt (not
+ // promptSentAt) excludes the prompt echo rendered during the paste→Enter
+ // window. The TUI repaints chrome continuously even with an unsent prompt,
// so we can't trust mere PTY activity — only the working counter advancing
- // across wall-clock time confirms the prompt left the input box (a paste-
- // echoed counter, even from a pasted transcript, arrives all at once and is
- // rejected by the time-span requirement). Gates idle-complete (issue #1229).
- if (promptSentAt && stripped && !workActivity.active) workActivity.observe(stripped, now);
+ // across wall-clock time confirms real work (a static echo's counters all
+ // arrive together and fail the time-span). Gates idle-complete (#1229).
+ if (promptSubmittedAt && stripped && !workActivity.active) workActivity.observe(stripped, now);
lastOutputAt = now;
if (firstOutputAt === null) firstOutputAt = lastOutputAt;
@@ -658,6 +666,9 @@ export async function spawnTuiAgent({
// prompt unsent (the "I had to hit Enter myself" bug). Tracked in
// submitEnterTimer so finish() can cancel pending retries if the agent ends.
const submitEnter = () => {
+ // Mark submission so work-activity observation begins only now — after the
+ // Enter is written, past the prompt-echo window (issue #1229 review).
+ if (promptSubmittedAt === null) promptSubmittedAt = Date.now();
submitEnterTimer = scheduleSubmitEnters(
() => shellService.writeToSession(sessionId, '\r'),
() => finalized
@@ -719,23 +730,29 @@ export async function spawnTuiAgent({
// Distinguish a real (sentinel-less) completion from a never-submitted
// prompt that just idled out. `lastOutputAt > promptSentAt` only proves
// the TUI repainted SOMETHING — banner/status chrome churns even with the
- // prompt sitting unsent. Only finalize as success when we actually saw the
- // model working (workActivity.active); otherwise this is the #1229 failure
- // mode (prompt never submitted, zero work done) and must be recorded as a
- // FAILURE so the orchestrator doesn't treat a no-op run as done.
- if (workActivity.active) {
- finish({ success: true, exitCode: 0, reason: 'idle-complete' }).catch(err => {
- emitLog('error', `Failed to finalize TUI agent ${agentId}: ${err.message}`, { agentId });
- });
- } else {
+ // prompt sitting unsent. The #1229 failure mode (prompt never submitted,
+ // zero work done) must be recorded as a FAILURE so the orchestrator doesn't
+ // treat a no-op run as done — but the work-counter signal exists ONLY on
+ // Claude Code / Codex. On a provider that never renders the counter
+ // (Antigravity/Gemini), its absence proves nothing, so we must preserve the
+ // original permissive idle-complete=success there (else every sentinel-less
+ // completion on those providers would falsely fail). So: downgrade to
+ // failure only when the provider DOES render the counter and we never saw
+ // it advance.
+ const noWorkButCounterExpected = !workActivity.active && rendersWorkCounter(commandName);
+ if (noWorkButCounterExpected) {
finish({
success: false,
exitCode: 1,
- error: 'TUI agent idled out with no sign of work — the prompt likely never submitted (no working indicator ever appeared after paste).',
+ error: 'TUI agent idled out with no sign of work — the prompt likely never submitted (no working indicator ever appeared after submit).',
reason: 'idle-no-activity',
}).catch(err => {
emitLog('error', `Failed to finalize TUI agent ${agentId}: ${err.message}`, { agentId });
});
+ } else {
+ finish({ success: true, exitCode: 0, reason: 'idle-complete' }).catch(err => {
+ emitLog('error', `Failed to finalize TUI agent ${agentId}: ${err.message}`, { agentId });
+ });
}
}
}, 5000);
diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.js
index d18f28cca2..355e7a1861 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -348,10 +348,16 @@ describe('spawnTuiAgent runtime', () => {
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
- // Feed PTY chunks AFTER the paste that prove the model is actually WORKING —
- // the elapsed working counter ADVANCING through two distinct values SPACED
- // ACROSS WALL-CLOCK TIME (≥750ms apart). This sets lastOutputAt > promptSentAt
- // AND trips the work-activity tracker, which the idle gate now requires before
+ // Advance past PASTE_TO_ENTER_FALLBACK_MS (3500ms) so the submit-Enter fires
+ // and promptSubmittedAt is set — work-activity is only observed AFTER submit
+ // (the prompt echo before that must not be scanned; issue #1229 review).
+ await vi.advanceTimersByTimeAsync(3600);
+ await flushMicrotasks();
+
+ // Feed PTY chunks AFTER submit that prove the model is actually WORKING — the
+ // elapsed working counter ADVANCING through two distinct values SPACED ACROSS
+ // WALL-CLOCK TIME (≥750ms apart). This sets lastOutputAt > promptSentAt AND
+ // trips the work-activity tracker, which the idle gate now requires before
// finalizing as success (issue #1229 — pure chrome churn, a single counter
// value, or two counters arriving at once must NOT count; see the no-activity
// and echoed-transcript tests).
@@ -420,6 +426,42 @@ describe('spawnTuiAgent runtime', () => {
);
});
+ // ── 1a-bis. Non-counter TUI provider keeps the permissive idle-complete ──────
+ // The work-counter signal only exists on Claude Code / Codex. On a provider
+ // that never renders it (Antigravity/Gemini), absence proves nothing — so a
+ // sentinel-less idle-out must stay SUCCESS (the original behavior), not be
+ // downgraded to failure. Regression guard for #1229 review (codex P2): gating
+ // idle-complete solely on a Claude/Codex screen pattern would falsely fail
+ // every sentinel-less completion on the other supported TUI providers.
+ it('idle-complete: a non-counter provider (gemini) stays success even with no work-counter signal', async () => {
+ let resolveComplete;
+ const completeDone = new Promise((r) => { resolveComplete = r; });
+ vi.mocked(agentLifecycle.finalizeAgent).mockImplementation(async () => { resolveComplete(); });
+
+ runSpawn({ tuiConfig: { command: 'gemini', args: [], commandLine: 'gemini', promptDelayMs: 100, idleTimeoutMs: 50 } });
+ await flushMicrotasks();
+
+ await capturedOnData(Buffer.from('Gemini booting...\n'));
+ await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(2000);
+ await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(3600); // submit fires
+ await flushMicrotasks();
+ // Real work output, but NO `(Ns ·` counter (gemini doesn't render one).
+ await capturedOnData(Buffer.from('Editing src/foo.js …\n'));
+ await vi.advanceTimersByTimeAsync(21000);
+ vi.useRealTimers();
+ await completeDone;
+
+ expect(agentLifecycle.finalizeAgent).toHaveBeenCalledWith(
+ expect.objectContaining({
+ agentId: 'agent-1',
+ success: true,
+ completionReason: 'idle-complete',
+ })
+ );
+ });
+
// ── 1b. Submit-Enter retries ─────────────────────────────────────────────────
// A single `\r` after a large bracketed paste can be swallowed mid-paste-
// commit, stranding the prompt unsent (the "I had to hit Enter myself" bug,
@@ -714,6 +756,8 @@ describe('spawnTuiAgent runtime', () => {
await flushMicrotasks();
await vi.advanceTimersByTimeAsync(2000);
await flushMicrotasks();
+ await vi.advanceTimersByTimeAsync(3600); // submit-Enter fires → promptSubmittedAt set
+ await flushMicrotasks();
await capturedOnData(Buffer.from('(1s · thinking with high effort)\n'));
await vi.advanceTimersByTimeAsync(800); // counter must tick across ≥750ms to count as work
await capturedOnData(Buffer.from('(2s · thinking with high effort)\n'));