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 6 (codex P2): strip the prompt before counting its paste markers

[._.]/ Adam Eivyf1fee52ccb3b
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index 1da4250679..67ed491b58 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -126,6 +126,24 @@ describe('tuiHandshake — paste timing constants', () => {
expect(countPasteMarkers('[Pastedtext#1+35lines]') > countPasteMarkers('do the thing')).toBe(true);
});
+ it('the gate must count a STRIPPED prompt — a raw cursor-positioned marker echoes back stripped', () => {
+ // Round-6 review: a pasted RAW transcript can carry the cursor-positioned form.
+ // Unstripped it counts as 0 (escapes break the match), but it echoes back as
+ // the stripped form (count 1) — so counting the RAW prompt would undercount and
+ // fire the fast path early. The prompt must be stripped the same way the
+ // post-paste buffer is. (stripAnsi behavior is covered in ansiStrip.test.js;
+ // here we pin the count asymmetry the consumers must avoid.)
+ const rawMarkerPrompt = 'analyze: [Pasted\x1b[11Gtext\x1b[16G#1\x1b[19G+35\x1b[23Glines]';
+ const strippedPrompt = rawMarkerPrompt.replace(/\x1b\[[0-9;?]*[a-zA-Z]/g, '');
+ expect(countPasteMarkers(rawMarkerPrompt)).toBe(0); // raw → undercount (the bug)
+ expect(countPasteMarkers(strippedPrompt)).toBe(1); // stripped → correct count
+ // What the echo produces in the (stripped) post-paste buffer:
+ const echoInStrippedBuffer = countPasteMarkers('[Pastedtext#1+35lines]'); // 1
+ // Gating on the RAW count fires early (1 > 0); gating on the STRIPPED count does not (1 > 1 == false).
+ expect(echoInStrippedBuffer > countPasteMarkers(rawMarkerPrompt)).toBe(true);
+ expect(echoInStrippedBuffer > countPasteMarkers(strippedPrompt)).toBe(false);
+ });
+
it('extractWorkCounterSeconds parses the TUI bullet-suffixed working counter', () => {
// Claude Code: `(1s · …`; Codex: `(57s • …`.
expect(extractWorkCounterSeconds('(1s · thinking with high effort)')).toEqual([1]);
diff --git a/server/lib/tuiPromptRunner.js b/server/lib/tuiPromptRunner.js
index 3f87c9ed1e..e3c255033c 100644
--- a/server/lib/tuiPromptRunner.js
+++ b/server/lib/tuiPromptRunner.js
@@ -36,7 +36,7 @@ import { spawn as ptySpawn } from 'node-pty';
import { join, resolve } from 'path';
import { ensureDir, PATHS, tryReadFile } from './fileUtils.js';
-import { createStreamingAnsiStripper } from './ansiStrip.js';
+import { createStreamingAnsiStripper, stripAnsi } from './ansiStrip.js';
import { createImmediateFallbackSignalDetector } from './aiToolkit/errorDetection.js';
import { getRunsPath, finalizeRunRecord, emitRunStarted, registerActiveRun, unregisterActiveRun } from '../services/runner.js';
import { registerExternalSession, unregisterExternalSession, isExternalSessionAttached } from '../services/shell.js';
@@ -153,7 +153,9 @@ ${prompt}`;
// task can echo `[Pasted text #N]` back into the post-paste stream. The fast
// path must wait for the TUI's OWN marker (the count to EXCEED this), so an
// echoed marker doesn't fire the submit-Enter mid-reflow (issue #1229 review).
- const promptMarkerCount = countPasteMarkers(wrappedPrompt);
+ // STRIP first so a pasted RAW transcript's cursor-positioned marker (counts as 0
+ // unstripped) is counted the same way the stripped post-paste buffer is.
+ const promptMarkerCount = countPasteMarkers(stripAnsi(wrappedPrompt));
// CLAUDECODE is set when PortOS itself runs inside Claude Code; passing it
// through to a spawned Claude Code TUI would make the child think it's
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index d986abbc32..22bd8a9ebe 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -18,7 +18,7 @@ import { finalizeAgent, releaseAgentLane } from './agentLifecycle.js';
import { activeAgents, userTerminatedAgents, pausedAgents } from './agentState.js';
import { PATHS } from '../lib/fileUtils.js';
import { resolveCliModel } from '../lib/providerModels.js';
-import { createStreamingAnsiStripper } from '../lib/ansiStrip.js';
+import { createStreamingAnsiStripper, stripAnsi } from '../lib/ansiStrip.js';
import { createImmediateFallbackSignalDetector } from '../lib/aiToolkit/errorDetection.js';
import { isAntigravityCommand } from '../lib/antigravity.js';
import {
@@ -200,8 +200,12 @@ export async function spawnTuiAgent({
// Markers already present in the prompt text itself (a transcript-analysis task
// can echo `[Pasted text #N]` back). The paste-commit fast path must wait for
// the TUI's OWN marker — i.e. the count to EXCEED this — so an echoed marker
- // doesn't fire the submit-Enters mid-reflow (issue #1229 review).
- const promptMarkerCount = countPasteMarkers(prompt);
+ // doesn't fire the submit-Enters mid-reflow (issue #1229 review). STRIP the
+ // prompt first: a pasted RAW transcript may carry the cursor-positioned marker
+ // form (`[Pasted\x1b[11Gtext…`), which counts as 0 unstripped but echoes back as
+ // the stripped `[Pastedtext#…]` (count 1) — so we must count the prompt the same
+ // way the post-paste buffer is counted, or the gate undercounts and fires early.
+ const promptMarkerCount = countPasteMarkers(stripAnsi(prompt));
let outputBuffer = '';
let finalized = false;