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 (agy): match generation chrome anywhere in a PTY chunk

[._.]/ Adam Eivy94ebff568a44
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index 1b05c127f5..1ec328674b 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -466,10 +466,14 @@ export function createWorkActivityTracker() {
export const GENERATION_ACTIVITY_PATTERN =
new RegExp(`esc to cancel|Generating\\s*[.·•…]|${WORK_COUNTER_PATTERN.source}`, 'i');
-// Rolling tail kept across chunks so in-flight chrome split by a PTY chunk
-// boundary (`esc to ` + `cancel`) still matches. The sibling trackers below each
-// learned this the hard way — see createReviewLoopTracker's docblock.
-const GENERATION_ACTIVITY_TAIL_CAP = 64;
+// Carry-over kept across chunks so in-flight chrome split by a PTY chunk boundary
+// (`esc to ` + `cancel`) still matches. The sibling trackers below each learned
+// this the hard way — see createReviewLoopTracker's docblock.
+//
+// Only needs to span the longest pattern alternative, because `observe` tests the
+// FULL `carry + chunk` window and trims only afterwards (see below) — the cap
+// bounds retained state, it never bounds what gets matched.
+const GENERATION_ACTIVITY_CARRY_CAP = 64;
/**
* Stateful latch for "the TUI is actively generating a response". Feed it each
@@ -494,8 +498,14 @@ export function createGenerationActivityTracker() {
observe(strippedText) {
if (active) return true;
if (typeof strippedText !== 'string' || !strippedText) return false;
- tail = (tail + strippedText).slice(-GENERATION_ACTIVITY_TAIL_CAP);
- if (GENERATION_ACTIVITY_PATTERN.test(tail)) active = true;
+ // Test the WHOLE window, then trim. Trimming first (the sibling trackers'
+ // shape) throws away everything but the last CAP characters of the chunk,
+ // so chrome anywhere earlier in a typical multi-hundred-byte PTY repaint
+ // is never matched at all — the tracker would miss a provider that HAD
+ // recovered and the run would be failed over to a fallback for nothing.
+ const window = tail + strippedText;
+ if (GENERATION_ACTIVITY_PATTERN.test(window)) active = true;
+ tail = window.slice(-GENERATION_ACTIVITY_CARRY_CAP);
return active;
},
get active() { return active; },
@@ -561,8 +571,18 @@ export function createSelfClearingSignalGate() {
recovered = true;
return true;
},
+ // Returns the analysis to fail over with, or null while still waiting (or
+ // after a recovery). `nowMs` is OPTIONAL: a poller passes the clock and gets
+ // a deadline comparison, while a consumer whose own `setTimeout` fired for
+ // exactly this window omits it and force-expires. That distinction matters —
+ // a timer scheduled for `graceMs` can fire a hair before `Date.now()` reaches
+ // `deadlineAt` (ms-resolution clock vs. timer rounding), and since the timer
+ // is one-shot with nothing to reschedule it, a null there would strand the
+ // gate armed: idle-completion suppressed and no fail-over, until the run's
+ // hard timeout. Force-expiry has no such edge.
takeExpired(nowMs) {
- if (!armed || nowMs < armed.deadlineAt) return null;
+ if (!armed) return null;
+ if (nowMs !== undefined && nowMs < armed.deadlineAt) return null;
const { analysis } = armed;
armed = null;
activity = null;
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index 3ce5f177a2..82e0b5803f 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -299,6 +299,22 @@ describe('tuiHandshake — paste timing constants', () => {
expect(tracker.active).toBe(true);
});
+ // A real TUI repaint is hundreds of bytes and the chrome lands wherever the
+ // frame puts it. Trimming to the carry cap BEFORE matching (rather than after)
+ // discards everything but the last 64 chars, so the tracker misses a provider
+ // that HAD recovered and the run gets failed over for nothing.
+ it('createGenerationActivityTracker matches chrome early in a large chunk', () => {
+ const tracker = createGenerationActivityTracker();
+ tracker.observe(`Generating...${'x'.repeat(500)}`);
+ expect(tracker.active).toBe(true);
+ });
+
+ it('createGenerationActivityTracker matches chrome mid-way through a large chunk', () => {
+ const tracker = createGenerationActivityTracker();
+ tracker.observe(`${'a'.repeat(300)}esc to cancel${'b'.repeat(300)}`);
+ expect(tracker.active).toBe(true);
+ });
+
describe('createSelfClearingSignalGate', () => {
const BANNER = { message: 'eligibility', graceMs: 60000 };
@@ -355,6 +371,25 @@ describe('tuiHandshake — paste timing constants', () => {
expect(gate.takeExpired(61001)).toBe(BANNER);
});
+ // A consumer whose own one-shot setTimeout fired for exactly this window
+ // omits the clock. A deadline re-check that came up a millisecond short
+ // (ms-resolution clock vs. timer rounding) would strand the gate armed with
+ // nothing left to retry it — idle-completion suppressed and no fail-over.
+ it('force-expires when called with no clock, even a hair before the deadline', () => {
+ const gate = createSelfClearingSignalGate();
+ gate.arm(BANNER, 1000);
+ expect(gate.takeExpired(60999)).toBeNull(); // polling form: still waiting
+ expect(gate.takeExpired()).toBe(BANNER); // timer form: fires regardless
+ expect(gate.armed).toBe(false);
+ });
+
+ it('force-expiry still returns null once the provider recovered', () => {
+ const gate = createSelfClearingSignalGate();
+ gate.arm(BANNER, 1000);
+ gate.observe('Generating...');
+ expect(gate.takeExpired()).toBeNull();
+ });
+
it('honors a per-signal grace window rather than one global constant', () => {
const gate = createSelfClearingSignalGate();
gate.arm({ message: 'slow warmup', graceMs: 5000 }, 0);
diff --git a/server/lib/tuiPromptRunner.js b/server/lib/tuiPromptRunner.js
index c5451d3699..605d0f9de1 100644
--- a/server/lib/tuiPromptRunner.js
+++ b/server/lib/tuiPromptRunner.js
@@ -508,7 +508,10 @@ ${prompt}`;
console.log(`⏳ TUI run ${runId} holding for a self-clearing provider signal (${Math.round(fallbackSignal.graceMs / 1000)}s): ${fallbackSignal.message}`);
selfClearingTimer = setTimeout(() => {
selfClearingTimer = null;
- const expired = selfClearingGate.takeExpired(Date.now());
+ // No clock argument: this timer IS this window's deadline, and it
+ // is one-shot — a deadline re-check that came up a millisecond
+ // short would strand the gate armed with nothing left to retry it.
+ const expired = selfClearingGate.takeExpired();
if (finalized || !expired) return;
finishWithFallbackSignal(expired).catch((err) => {
console.error(`❌ TUI run ${runId} deferred fallback-signal handling failed: ${err?.message || err}`);