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
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review round 3 (codex P2): require the work counter to tick across wall-clock time
[._.]/ Adam Eivy5523f2fc82b2
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.jsindex 8c36f6b475..db671a9139 100644--- a/server/lib/tuiHandshake.js+++ b/server/lib/tuiHandshake.js@@ -80,15 +80,27 @@ export function detectPasteMarker(strippedText) {// verified in real transcripts: `(1s · thinking…`.)// 2. We require ≥ MIN_WORK_COUNTER_SAMPLES DISTINCT second-counts — the live// counter passes through many values; a static echoed literal is just one.+// There is one residual echo vector even with the bullet requirement: a task+// that asks the agent to ANALYZE A TUI TRANSCRIPT can paste content that itself+// contains two distinct bulleted counters (`(1s · …` and `(2s · …`), and the+// whole prompt is echoed at paste time — before Enter is known to have submitted+// (flagged in review of #1229). The defining difference is TIMING: the live+// counter is rendered ONE value at a time as wall-clock seconds pass, whereas an+// echoed transcript's counters all arrive together in the paste render. So the+// tracker activates only once it has seen ≥ MIN_WORK_COUNTER_SAMPLES distinct+// values whose observations SPAN ≥ MIN_WORK_COUNTER_SPAN_MS of real time — a+// span a single paste-render burst can't fake. `observe()` therefore takes the+// current timestamp.// Verified against real transcripts: the working run cycled through many counter-// values; the two confirmed stuck runs (`agent-92ed2c56`, `agent-30a3ab56`) had-// none. Heuristic by nature, so it gates only the FALLBACK idle-complete path on-// the long-running agent path — the authoritative success signal remains the-// `.agent-done` sentinel. (The one-shot runner is deliberately NOT gated: its-// idle-complete legitimately captures inline output that may carry no counter,-// and its authoritative path is the response file.)+// values across its runtime; the two confirmed stuck runs (`agent-92ed2c56`,+// `agent-30a3ab56`) had none. Heuristic by nature, so it gates only the FALLBACK+// idle-complete path on the long-running agent path — the authoritative success+// signal remains the `.agent-done` sentinel. (The one-shot runner is deliberately+// NOT gated: its idle-complete legitimately captures inline output that may carry+// no counter, and its authoritative path is the response file.)export const WORK_COUNTER_PATTERN = /\(\s*(\d+)\s*s\s*[·•]/g;export const MIN_WORK_COUNTER_SAMPLES = 2;+export const MIN_WORK_COUNTER_SPAN_MS = 750;/*** Extract every elapsed-second value from the TUI working counter in@@ -113,23 +125,42 @@ export function extractWorkCounterSeconds(strippedText) {/*** Stateful tracker for the "model is actively working" signal. Feed it each- * ANSI-stripped post-paste chunk via `observe()`; it becomes (and stays)- * `active` once it has seen ≥ MIN_WORK_COUNTER_SAMPLES DISTINCT elapsed-second- * counter values — i.e. the working counter actually advanced, which a static- * echoed prompt cannot fake. Shared by both TUI consumers so the echo-proof- * detection logic can't drift between them.+ * ANSI-stripped post-paste chunk via `observe(strippedText, nowMs)`; it becomes+ * (and stays) `active` once it has seen ≥ MIN_WORK_COUNTER_SAMPLES DISTINCT+ * bulleted elapsed-second counter values whose observations span ≥+ * MIN_WORK_COUNTER_SPAN_MS of wall-clock time — i.e. the live counter actually+ * ticked across real seconds, which neither a static echoed prompt nor an echoed+ * transcript's counters (all rendered in one paste burst) can fake. Used by the+ * long-running agent path; lives here so the echo-proof logic is unit-testable.*- * @returns {{ observe: (strippedText: string) => boolean, readonly active: boolean }}+ * @returns {{ observe: (strippedText: string, nowMs: number) => boolean, readonly active: boolean }}*/export function createWorkActivityTracker() {const seconds = new Set();- const isActive = () => seconds.size >= MIN_WORK_COUNTER_SAMPLES;+ let firstSeenAt = null; // wall-clock ms when the FIRST distinct counter appeared+ let active = false;return {- observe(strippedText) {- for (const s of extractWorkCounterSeconds(strippedText)) seconds.add(s);- return isActive();+ observe(strippedText, nowMs) {+ if (active) return true;+ for (const s of extractWorkCounterSeconds(strippedText)) {+ if (seconds.has(s)) continue;+ seconds.add(s);+ if (firstSeenAt === null) {+ firstSeenAt = nowMs;+ } else if (+ seconds.size >= MIN_WORK_COUNTER_SAMPLES &&+ typeof nowMs === 'number' && typeof firstSeenAt === 'number' &&+ nowMs - firstSeenAt >= MIN_WORK_COUNTER_SPAN_MS+ ) {+ // A later distinct value, far enough after the first — the live counter+ // advanced across wall-clock time. A one-shot paste-render burst can't.+ active = true;+ return true;+ }+ }+ return active;},- get active() { return isActive(); },+ get active() { return active; },};}diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.jsindex 141cd82620..069a1260ee 100644--- a/server/lib/tuiHandshake.test.js+++ b/server/lib/tuiHandshake.test.js@@ -8,6 +8,7 @@ import {detectPasteMarker,WORK_COUNTER_PATTERN,MIN_WORK_COUNTER_SAMPLES,+ MIN_WORK_COUNTER_SPAN_MS,extractWorkCounterSeconds,createWorkActivityTracker,PASTE_TO_ENTER_MIN_DELAY_MS,@@ -125,38 +126,48 @@ describe('tuiHandshake — paste timing constants', () => {expect(extractWorkCounterSeconds('(4s · x')).toEqual([4]);});- it('createWorkActivityTracker activates only after the counter ADVANCES (echo-proof)', () => {+ it('createWorkActivityTracker activates only when the counter ticks across real time (echo-proof)', () => {const tracker = createWorkActivityTracker();expect(tracker.active).toBe(false);- // Bare duration literals echoed from the prompt — even two distinct ones —- // must NOT activate (no bullet separator → not the TUI counter).- expect(tracker.observe('finish within (1s) and definitely under (2s)')).toBe(false);+ // Bare duration literals echoed from the prompt — no bullet → not the counter.+ expect(tracker.observe('finish within (1s) and definitely under (2s)', 0)).toBe(false);+ // A single bulleted counter value must NOT activate (one sample).+ expect(tracker.observe('(5s · thinking)', 1000)).toBe(false);+ expect(tracker.observe('(5s · thinking)', 1100)).toBe(false);expect(tracker.active).toBe(false);- // A single bulleted counter value also must NOT activate (one sample).- expect(tracker.observe('(5s · thinking)')).toBe(false);- expect(tracker.observe('(5s · thinking)')).toBe(false);- expect(tracker.active).toBe(false);- // A second DISTINCT bulleted value — the live counter actually advanced.- expect(tracker.observe('(6s · thinking)')).toBe(true);+ // A second DISTINCT bulleted value — and ≥750ms after the first — activates.+ expect(tracker.observe('(6s · thinking)', 2000)).toBe(true);expect(tracker.active).toBe(true);// Stays active once tripped.- expect(tracker.observe('● high · /effort')).toBe(true);+ expect(tracker.observe('● high · /effort', 3000)).toBe(true);+ });++ it('createWorkActivityTracker rejects an echoed transcript with two distinct counters arriving at once', () => {+ // The #1229 round-3 review case: a task that pastes a TUI transcript can echo+ // two distinct bulleted counters — but they all arrive in the same paste-render+ // burst (same instant), so the time-span requirement keeps them from faking work.+ const tracker = createWorkActivityTracker();+ expect(tracker.observe('analyze this log: (1s · thinking) then (2s · thinking)', 5000)).toBe(false);+ // Even repainted later as a whole (still the SAME two values, not new ones).+ expect(tracker.observe('analyze this log: (1s · thinking) then (2s · thinking)', 9000)).toBe(false);+ expect(tracker.active).toBe(false);});it('createWorkActivityTracker stays inactive on pure stuck/idle chrome', () => {const tracker = createWorkActivityTracker();// The exact chrome from the #1229 false-success transcript (no counter).- tracker.observe('⏵⏵ bypass permissions on (shift+tab to cycle)');- tracker.observe('● high · /effort');- tracker.observe('paste again to expand');- tracker.observe('Begin working on the task now.');- tracker.observe('Opus 4.8 │ agent-92ed2c56');+ tracker.observe('⏵⏵ bypass permissions on (shift+tab to cycle)', 0);+ tracker.observe('● high · /effort', 1000);+ tracker.observe('paste again to expand', 2000);+ tracker.observe('Begin working on the task now.', 3000);+ tracker.observe('Opus 4.8 │ agent-92ed2c56', 4000);expect(tracker.active).toBe(false);});it('pins work-activity detection constants', () => {expect(WORK_COUNTER_PATTERN).toBeInstanceOf(RegExp);expect(MIN_WORK_COUNTER_SAMPLES).toBe(2);+ expect(MIN_WORK_COUNTER_SPAN_MS).toBe(750);});it('pins provider-default constants', () => {diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.jsindex a54f3a9d26..379f3d7721 100644--- a/server/services/agentTuiSpawning.js+++ b/server/services/agentTuiSpawning.js@@ -537,12 +537,15 @@ export async function spawnTuiAgent({// — as this did before #1229 — left the marker unmatchable and the fast// 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,// so we can't trust mere PTY activity — only the working counter advancing- // confirms the prompt left the input box. Gates idle-complete (issue #1229).- if (promptSentAt && stripped && !workActivity.active) workActivity.observe(stripped);- lastOutputAt = Date.now();+ // 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);+ lastOutputAt = now;if (firstOutputAt === null) firstOutputAt = lastOutputAt;if (!hasStartedWorking) {diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.jsindex 14187ff5d7..d18f28cca2 100644--- a/server/services/agentTuiSpawning.test.js+++ b/server/services/agentTuiSpawning.test.js@@ -349,12 +349,14 @@ describe('spawnTuiAgent runtime', () => {await flushMicrotasks();// Feed PTY chunks AFTER the paste that prove the model is actually WORKING —- // the elapsed working counter ADVANCING through two distinct values. 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, or a single echoed counter value, must NOT count; see the- // no-activity test below).+ // 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).await capturedOnData(Buffer.from('(1s · thinking with high effort)\n'));+ await vi.advanceTimersByTimeAsync(800);await capturedOnData(Buffer.from('(2s · thinking with high effort)\n'));// Advance past DEFAULT_TUI_MIN_RUNTIME_MS (15 000ms) + idleTimeoutMs (50ms).@@ -713,6 +715,7 @@ describe('spawnTuiAgent runtime', () => {await vi.advanceTimersByTimeAsync(2000);await flushMicrotasks();await capturedOnData(Buffer.from('(1s · thinking with high effort)\n'));+ await vi.advanceTimersByTimeAsync(800); // counter must tick across ≥750ms to count as workawait capturedOnData(Buffer.from('(2s · thinking with high effort)\n'));await vi.advanceTimersByTimeAsync(21000);vi.useRealTimers();