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 5 (codex P2): ignore paste markers echoed from the prompt

[._.]/ Adam Eivy7c626455d438
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index 1ecc27a4b7..08d67c0eb0 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -46,16 +46,43 @@ export const PASTE_TO_ENTER_MIN_DELAY_MS = 200;
export const PASTE_TO_ENTER_FALLBACK_MS = 3500;
/**
- * True when `strippedText` contains Claude Code's `[Pasted text #N …]` paste-
- * commit marker. Callers MUST pass ANSI-STRIPPED output (see PASTE_MARKER_PATTERN
- * above for why the raw stream never matches). Shared by both TUI consumers so
- * the strip-then-match contract can't drift between them.
+ * Count the `[Pasted text #N …]` paste-commit markers in `strippedText`.
+ * Callers MUST pass ANSI-STRIPPED output (see PASTE_MARKER_PATTERN above for why
+ * the raw stream never matches). Shared by both TUI consumers so the
+ * strip-then-match contract can't drift between them.
+ *
+ * Why count rather than just detect-presence: when the pasted PROMPT itself
+ * contains a paste-marker (a transcript-analysis task — plausible here, since
+ * #1229 is about TUI transcripts), the echoed prompt carries that marker into
+ * the post-paste stream BEFORE the TUI emits its own commit marker. A bare
+ * presence check would then fire the submit-Enters ~200ms in, while the paste is
+ * still reflowing, reintroducing the unsent-prompt bug (issue #1229 review). So
+ * callers gate on the count EXCEEDING the count already present in the prompt —
+ * the TUI's genuine (N+1)th marker. (A normal prompt has 0, so the common case
+ * is unchanged; a large transcript paste that Claude Code collapses to its own
+ * single marker simply falls back to the timer, which is safe.)
+ *
+ * @param {string} strippedText — ANSI-stripped text (prompt or post-paste output).
+ * @returns {number}
+ */
+export function countPasteMarkers(strippedText) {
+ if (typeof strippedText !== 'string' || !strippedText) return 0;
+ const re = new RegExp(PASTE_MARKER_PATTERN.source, 'gi');
+ const m = strippedText.match(re);
+ return m ? m.length : 0;
+}
+
+/**
+ * True when `strippedText` contains at least one paste-commit marker. Thin
+ * presence wrapper over `countPasteMarkers`. Callers that must ignore markers
+ * echoed from the prompt should compare `countPasteMarkers(output)` against
+ * `countPasteMarkers(prompt)` instead of using this.
*
* @param {string} strippedText — ANSI-stripped post-paste output accumulator.
* @returns {boolean}
*/
export function detectPasteMarker(strippedText) {
- return typeof strippedText === 'string' && PASTE_MARKER_PATTERN.test(strippedText);
+ return countPasteMarkers(strippedText) > 0;
}
// "The model is actively processing a submitted prompt" signal. A TUI repaints
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index f483c275f9..1da4250679 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -6,6 +6,7 @@ import {
PASTE_MARKER_POLL_MS,
PASTE_MARKER_PATTERN,
detectPasteMarker,
+ countPasteMarkers,
WORK_COUNTER_PATTERN,
MIN_WORK_COUNTER_SAMPLES,
MIN_WORK_COUNTER_SPAN_MS,
@@ -101,6 +102,30 @@ describe('tuiHandshake — paste timing constants', () => {
expect(detectPasteMarker('[Pasted text #1 +3 lines]')).toBe(true);
});
+ it('countPasteMarkers counts markers (so an echoed-prompt marker can be subtracted)', () => {
+ expect(countPasteMarkers('')).toBe(0);
+ expect(countPasteMarkers(null)).toBe(0);
+ expect(countPasteMarkers('no marker here')).toBe(0);
+ expect(countPasteMarkers('[Pasted text #1 +3 lines]')).toBe(1);
+ // Collapsed (stripped) + spaced forms both count.
+ expect(countPasteMarkers('[Pastedtext#1+35lines] then [Pasted text #2 +1 lines]')).toBe(2);
+ });
+
+ it('countPasteMarkers underpins the echoed-marker gate (count must EXCEED the prompt count)', () => {
+ // A transcript-analysis prompt that itself contains a paste marker. The fast
+ // path must wait for the TUI's OWN (N+1)th marker, not fire on the echo
+ // (issue #1229 round-5 review).
+ const prompt = 'analyze this transcript: "[Pasted text #1 +35 lines]" and report';
+ const promptMarkers = countPasteMarkers(prompt); // 1
+ expect(promptMarkers).toBe(1);
+ // Echo of the prompt alone — count does NOT exceed the prompt's own count.
+ expect(countPasteMarkers(prompt) > promptMarkers).toBe(false);
+ // Once the TUI appends its real commit marker, the count exceeds it → fire.
+ expect(countPasteMarkers(`${prompt} [Pastedtext#2+40lines]`) > promptMarkers).toBe(true);
+ // A NORMAL prompt (0 markers) keeps the original presence behavior.
+ expect(countPasteMarkers('[Pastedtext#1+35lines]') > countPasteMarkers('do the thing')).toBe(true);
+ });
+
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 bdf8d06af9..3f87c9ed1e 100644
--- a/server/lib/tuiPromptRunner.js
+++ b/server/lib/tuiPromptRunner.js
@@ -43,7 +43,7 @@ import { registerExternalSession, unregisterExternalSession, isExternalSessionAt
import {
DEFAULT_TUI_PROMPT_DELAY_MS,
PASTE_MARKER_POLL_MS,
- detectPasteMarker,
+ countPasteMarkers,
PASTE_TO_ENTER_MIN_DELAY_MS,
PASTE_TO_ENTER_FALLBACK_MS,
scheduleSubmitEnters,
@@ -149,6 +149,12 @@ ${prompt}`;
console.log(`📟 Executing TUI: ${command} ${args.join(' ')} (${wrappedPrompt.length} chars via paste, response→${responseFilePath})`);
+ // Markers already present in the (wrapped) prompt itself — a transcript-analysis
+ // 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);
+
// 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
// nested. Other AI spawn paths (runner.js, agentCliSpawning.js) strip it
@@ -438,7 +444,7 @@ ${prompt}`;
pasteEnterTimer = setInterval(() => {
if (finalized) { clearInterval(pasteEnterTimer); pasteEnterTimer = null; postPasteStripped = null; return; }
const elapsed = Date.now() - pasteSentAt;
- const markerSeen = detectPasteMarker(postPasteStripped);
+ const markerSeen = countPasteMarkers(postPasteStripped) > promptMarkerCount;
if ((markerSeen && elapsed >= PASTE_TO_ENTER_MIN_DELAY_MS)
|| elapsed >= PASTE_TO_ENTER_FALLBACK_MS) {
clearInterval(pasteEnterTimer);
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 51c44e701f..d986abbc32 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -27,7 +27,7 @@ import {
READY_POLL_INTERVAL_MS,
READY_IDLE_THRESHOLD_MS,
PASTE_MARKER_POLL_MS,
- detectPasteMarker,
+ countPasteMarkers,
createWorkActivityTracker,
rendersWorkCounter,
PASTE_TO_ENTER_MIN_DELAY_MS,
@@ -197,6 +197,11 @@ export async function spawnTuiAgent({
const doneSentinelPath = workspacePath ? join(workspacePath, DONE_SENTINEL_NAME) : null;
const promptPreview = prompt.replace(/\s+/g, ' ').slice(0, 100);
const commandName = tuiConfig.command.split('/').pop();
+ // 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);
let outputBuffer = '';
let finalized = false;
@@ -540,7 +545,7 @@ export async function spawnTuiAgent({
scheduleRawFlush();
// Accumulate the ANSI-STRIPPED chunk (not the raw text): the paste marker
// is rendered with absolute-column cursor moves between glyphs, so it only
- // matches after stripping (see detectPasteMarker). Appending raw text here
+ // matches after stripping (see countPasteMarkers). Appending raw text here
// — as this did before #1229 — left the marker unmatchable and the fast
// path dead.
if (postPasteBuffer !== null && stripped) postPasteBuffer += stripped;
@@ -684,7 +689,7 @@ export async function spawnTuiAgent({
return;
}
const elapsed = Date.now() - pasteSentAt;
- const markerSeen = detectPasteMarker(postPasteBuffer);
+ const markerSeen = countPasteMarkers(postPasteBuffer) > promptMarkerCount;
// Submit when EITHER the paste-commit marker appears (preferred) or
// the fallback window elapses (covers small prompts that don't render
// the marker).