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

fix: confirm collapsed TUI paste even when prompt embeds marker literals (#2228)

[._.]/ Adam Eivy50d6ebce65d5
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 65e8a18226..d73759b859 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -3,6 +3,8 @@
## CoS Agents
- **Claude Code TUI agents no longer fail `paste-not-rendered` on every multi-line prompt.** The #2192 paste-verification guard confirmed a paste had landed by looking for the prompt's literal text in the TUI buffer — but Claude Code collapses a multi-line bracketed paste into a `[Pasted text #N]` chip and *hides* the pasted body text, and it reflows single lines with cursor-positioning escapes instead of literal space bytes between words. Both made the text check false-negative, so every `claude-code-tui` CoS agent retried 3× and died with `paste-not-rendered` despite the paste landing perfectly (100% reproduction across real runs, 2026-07-05). The gate now treats the TUI's own paste-commit **marker** as authoritative proof of delivery (checked first, via a new `isPasteConfirmed` helper), and only falls back to text matching for the markerless path — where the match is now whitespace-insensitive so a space-less reflowed line still matches. Captured transcripts from the real incident are pinned as regression tests. (`server/lib/tuiHandshake.js`, `server/services/agentTuiSpawning.js`)
+
+- **[issue-2228] A TUI-transcript-analysis prompt that itself embeds a `[Pasted text #N]` literal no longer false-fails the paste.** The marker-authoritative gate above subtracts the marker count the prompt itself carried, so an echoed `[Pasted text #N]` isn't mistaken for the TUI's own commit — correct for the inline (uncollapsed) path, but wrong when Claude Code *collapses* a multi-line self-marker-containing prompt into its own single chip and hides the body: the buffer then shows one marker while the prompt also carried one (`1 > 1` is false), and the hidden body also defeats the text fallback — so the agent died `paste-not-rendered` despite the paste landing. Paste confirmation now also recognizes Claude's collapsed-chip shape (a marker present alongside the "paste again to expand" affordance) as authoritative proof of delivery — chrome the TUI only renders on its own genuine commit, so it can't re-introduce the echoed-marker false-positive the subtraction guards. A misleading code comment claiming the collapsed transcript paste "simply falls back to the timer, which is safe" is corrected. (`server/lib/tuiHandshake.js`)
- **A live agent TUI no longer gets reaped mid-paste.** The idle-timeout reaper only tracked `lastOutputAt` (PTY output), not client input, so pasting a large multi-line command into a running agent's session could sit in a silent reflow/commit window with no echo yet — indistinguishable from "idle" to the 5s poll. If the session happened to already be near its idle threshold, the reaper killed the PTY mid-paste, showing "[Session removed externally]" in the Shell page. The idle timer in `agentTuiSpawning.js` now also checks a new `shell.js` export, `getLastInputAt()`, and skips the reap while input arrived within the last 15s. This is deliberately keyed on input *recency*, not "is a Shell viewer attached": a regular (non-external) session's socket stays bound after its viewer navigates away — only external one-shot `tuiPromptRunner.js` runs get released via `shell:release-views` — so an attachment-based guard would have permanently suppressed idle-complete for any agent glanced at once in the Shell UI. (`server/services/shell.js`, `server/services/agentTuiSpawning.js`)
- **Swarm claim-issue runs now write the completion sentinel so the task actually finishes.** In `/do:next --swarm` mode the orchestrator ships every issue via a PR and never touches the working tree, so it was skipping the `.agent-done` completion sentinel — leaving the CoS task hanging with no summary as if the run never ended. Phase C of the swarm directive now explicitly instructs the orchestrator to write the sentinel (with a run summary of the issues claimed, their PRs, and merge outcomes) after the serialized merges, while still skipping the per-agent `/simplify`/push steps. (`server/services/cosTaskGenerator.js`)
- **[issue-2190] OpenCode Ollama providers now accept any --model flag.** The static `OPENCODE_CONFIG_CONTENT` lacked a models map, so OpenCode rejected every model with "not valid" and the agent sat idle. The config is now built dynamically at spawn time, declaring the provider's configured models (plus the model being run) under `provider.ollama.models` with bare ids — the exact shape OpenCode's `@ai-sdk/openai-compatible` provider validates `--model ollama/<id>` against. Applied across every OpenCode spawn path (CoS CLI/TUI agents, the toolkit CLI/TUI runners, and the light fire-and-collect CLI run), and a migration backfills existing installs' `data/providers.json` so a configured model's map is persisted too. Fixes the TUI eval path for local Ollama models.
diff --git a/server/lib/tuiHandshake.js b/server/lib/tuiHandshake.js
index b5fe21ecbf..a85c462d46 100644
--- a/server/lib/tuiHandshake.js
+++ b/server/lib/tuiHandshake.js
@@ -132,9 +132,14 @@ export function verifyPasteRendered(strippedBuffer, prefix) {
* 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.)
+ * the TUI's genuine (N+1)th marker. A normal prompt has 0, so the common case is
+ * unchanged. When Claude Code COLLAPSES a self-marker-containing multi-line
+ * prompt to its own single chip, this count-only comparison false-negatives
+ * (`1 > 1` is false) even though the paste landed — `isCollapsedPasteChip` below
+ * is what rescues that case in isPasteConfirmed (issue #2228). It is NOT safe to
+ * "simply fall back to the timer" there: the collapse HIDES the body, so the
+ * verifyPasteRendered text fallback also fails and the agent dies
+ * `paste-not-rendered`.
*
* @param {string} strippedText — ANSI-stripped text (prompt or post-paste output).
* @returns {number}
@@ -159,6 +164,33 @@ export function detectPasteMarker(strippedText) {
return countPasteMarkers(strippedText) > 0;
}
+// Claude Code's "this chip is a collapsed multi-line paste, click to see the
+// body" affordance, rendered right next to the `[Pasted text #N]` chip whenever
+// it folds a multi-line paste and HIDES the body. Matched against ANSI-stripped
+// output; whitespace-tolerant for the same inter-glyph-cursor-move reason as
+// PASTE_MARKER_PATTERN. This chrome is emitted by the TUI ITSELF — it never
+// appears in a raw prompt's own echoed `[Pasted text #N]` literal — so it's the
+// discriminator that separates issue #2228's genuine collapse (confirm) from the
+// echoed-marker false-positive the promptMarkerCount subtraction guards (reject).
+export const COLLAPSED_PASTE_CHIP_PATTERN = /paste\s*again\s*to\s*expand/i;
+
+/**
+ * True when `strippedText` shows Claude Code's COLLAPSED-paste chip shape: a
+ * paste-commit marker present alongside the "paste again to expand" affordance
+ * the TUI renders when it folds a multi-line paste and hides the body. A
+ * collapsed chip is the TUI's own commit by construction, so its mere presence
+ * proves delivery even when the marker count doesn't exceed promptMarkerCount
+ * (the self-marker-containing multi-line prompt case, issue #2228). Callers MUST
+ * pass an ANSI-STRIPPED buffer.
+ *
+ * @param {string} strippedText — ANSI-stripped post-paste output accumulator.
+ * @returns {boolean}
+ */
+export function isCollapsedPasteChip(strippedText) {
+ if (typeof strippedText !== 'string' || !strippedText) return false;
+ return countPasteMarkers(strippedText) >= 1 && COLLAPSED_PASTE_CHIP_PATTERN.test(strippedText);
+}
+
/**
* True when a post-paste buffer proves the TUI actually RECEIVED the prompt
* paste — the gate before sending the submit Enter(s). Two independent signals,
@@ -173,6 +205,16 @@ export function detectPasteMarker(strippedText) {
* claude-code-tui CoS agent failed `paste-not-rendered` after 3 retries
* because #2192's text-only check never saw the collapsed body — while the
* marker was sitting right there. (agent-656efa6e et al.)
+ * 1b. The COLLAPSED-CHIP shape (`isCollapsedPasteChip`) — a marker present
+ * alongside Claude's "paste again to expand" affordance. This rescues the
+ * subtraction's blind spot: when the prompt ITSELF embeds `[Pasted text #N]`
+ * literals AND is multi-line, Claude folds it into its own single chip and
+ * hides the body, so `count (1) > promptMarkerCount (1)` is false — yet the
+ * collapse chrome proves the visible marker is the TUI's own commit. The
+ * chrome never rides in on an echoed prompt literal, so this can't
+ * re-introduce the echoed-marker false-positive the subtraction guards
+ * (issue #2228; the inline/uncollapsed echo path has no such chrome and keeps
+ * subtracting).
* 2. Fallback for the MARKERLESS path — a paste too small to render the marker,
* or one genuinely SWALLOWED by a still-initializing TUI (the #2192 case,
* which renders no marker at all): fall back to confirming the prompt text
@@ -188,6 +230,7 @@ export function detectPasteMarker(strippedText) {
*/
export function isPasteConfirmed(strippedBuffer, { verifiablePrefix = null, promptMarkerCount = 0 } = {}) {
if (countPasteMarkers(strippedBuffer) > promptMarkerCount) return true; // marker is authoritative
+ if (isCollapsedPasteChip(strippedBuffer)) return true; // collapsed chip is the TUI's own commit (#2228)
if (!verifiablePrefix) return true; // nothing to verify against
return verifyPasteRendered(strippedBuffer, verifiablePrefix);
}
diff --git a/server/lib/tuiHandshake.test.js b/server/lib/tuiHandshake.test.js
index 1118e09ffa..e278f5d750 100644
--- a/server/lib/tuiHandshake.test.js
+++ b/server/lib/tuiHandshake.test.js
@@ -41,6 +41,7 @@ import {
extractVerifiablePromptPrefix,
verifyPasteRendered,
isPasteConfirmed,
+ isCollapsedPasteChip,
} from './tuiHandshake.js';
import { CODEX_CONFIGURED_DEFAULT } from './providerModels.js';
@@ -762,8 +763,64 @@ describe('tuiHandshake.isPasteConfirmed', () => {
expect(isPasteConfirmed(echoPlusCommit, { verifiablePrefix: prefix, promptMarkerCount: 1 })).toBe(true);
});
+ // Issue #2228: a MULTI-LINE prompt that itself embeds a `[Pasted text #N]`
+ // literal (a TUI-transcript-analysis task — the promptMarkerCount defense was
+ // added for exactly this domain). Claude Code collapses the whole multi-line
+ // paste into its OWN single chip and hides the body — including the prompt's
+ // embedded marker. So the buffer carries only Claude's 1 chip while
+ // promptMarkerCount is also 1: `count (1) > promptMarkerCount (1)` is false,
+ // AND the hidden body defeats the verifyPasteRendered text fallback. Before the
+ // fix this false-negatived and the agent died `paste-not-rendered` despite the
+ // paste landing. The collapsed-chip chrome ("paste again to expand") proves the
+ // visible marker is the TUI's own commit, so this MUST confirm.
+ it('confirms a collapsed multi-line paste even when the prompt embeds a marker literal (#2228)', () => {
+ // Prompt is multi-line AND embeds a `[Pasted text #1]` literal → promptMarkerCount = 1.
+ const selfMarkerPrompt = 'Analyze this TUI transcript where the agent hit a paste bug:\n\n[Pasted text #1 +40 lines]\n\nExplain why the paste false-negatived.';
+ const selfMarkerPrefix = extractVerifiablePromptPrefix(selfMarkerPrompt);
+ const promptMarkerCount = 1;
+ // Claude collapsed the whole thing to ITS OWN chip and hid the body — only the
+ // marker + collapse affordance survive; the prompt body is genuinely gone.
+ const collapsedBuffer = 'Opus 4.8 │ agent-2228abcd ❯ [Pastedtext#1+42lines] paste again to expand ──────── ⏵⏵ bypass permissions on (shift+tab to cycle)';
+ // The count-only comparison false-negatives (1 is not > 1)…
+ expect(countPasteMarkers(collapsedBuffer) > promptMarkerCount).toBe(false);
+ // …and the hidden body defeats the text fallback too…
+ expect(verifyPasteRendered(collapsedBuffer, selfMarkerPrefix)).toBe(false);
+ // …but the collapsed-chip shape proves the paste landed, so this MUST confirm.
+ expect(isPasteConfirmed(collapsedBuffer, { verifiablePrefix: selfMarkerPrefix, promptMarkerCount })).toBe(true);
+ });
+
+ it('does NOT re-introduce the echoed-marker false-positive: inline (uncollapsed) echo without collapse chrome still rejects (#2228)', () => {
+ // The prompt's `[Pasted text #1]` echoed INLINE (uncollapsed) with no
+ // "paste again to expand" chrome — the paste has NOT committed. The
+ // collapsed-chip rescue must not fire here; the subtraction must still reject.
+ const inlineEchoBuffer = '❯ [Pastedtext#1+40lines] analyze this TUI transcript where the agent hit a paste bug';
+ expect(isCollapsedPasteChip(inlineEchoBuffer)).toBe(false);
+ expect(isPasteConfirmed(inlineEchoBuffer, { verifiablePrefix: prefix, promptMarkerCount: 1 })).toBe(false);
+ });
+
it('confirms when there is nothing to verify against (no verifiable prefix)', () => {
expect(isPasteConfirmed('anything at all', { verifiablePrefix: null, promptMarkerCount: 0 })).toBe(true);
expect(isPasteConfirmed('anything at all', {})).toBe(true);
});
});
+
+describe('tuiHandshake.isCollapsedPasteChip', () => {
+ it('is true only when a marker AND the "paste again to expand" affordance are both present', () => {
+ expect(isCollapsedPasteChip('[Pastedtext#1+3lines] paste again to expand')).toBe(true);
+ // Marker but no collapse affordance → not a collapsed chip.
+ expect(isCollapsedPasteChip('[Pastedtext#1+3lines] analyze this transcript')).toBe(false);
+ // Collapse affordance but no marker → nothing committed.
+ expect(isCollapsedPasteChip('paste again to expand')).toBe(false);
+ });
+
+ it('tolerates the inter-glyph whitespace Claude renders (ANSI-stripped)', () => {
+ expect(isCollapsedPasteChip('[Pastedtext#2+9lines] pasteagaintoexpand')).toBe(true);
+ });
+
+ it('returns false for non-string / empty input', () => {
+ expect(isCollapsedPasteChip(null)).toBe(false);
+ expect(isCollapsedPasteChip(undefined)).toBe(false);
+ expect(isCollapsedPasteChip('')).toBe(false);
+ expect(isCollapsedPasteChip(123)).toBe(false);
+ });
+});