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: don't reap CoS agent TUI sessions a viewer is actively watching

Adam Eivyc7b8908329b2
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 7945785041..7ebd843b50 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -2,6 +2,7 @@
## CoS Agents
+- **A live agent TUI you're actively watching in the Shell page no longer gets reaped mid-paste.** The idle-timeout reaper only tracks `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 out from under the viewer, showing "[Session removed externally]" with the paste seemingly vanishing. The one-shot `tuiPromptRunner.js` idle-watch already guarded against this via `isExternalSessionAttached()`; the long-running CoS-agent idle timer in `agentTuiSpawning.js` now takes the same guard, skipping the reap entirely whenever a Shell viewer is bound to the session. (`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.
- **[issue-2191] TUI agents with zero file changes are now marked failed instead of silently succeeding.** A TUI agent that idled out after showing working indicators but made no uncommitted file changes was previously finalized as `success: idle-complete` — deleting the worktree and any evidence of what happened. Now the idle-complete path checks `git status` in the worktree: if the tree is clean, it finalizes as `failure: idle-no-changes` with the TUI buffer tail as error context. Any uncommitted diff is also captured to `worktree-diff.txt` in the agent archive dir before cleanup, so post-mortems can see what (if anything) was left behind.
diff --git a/server/services/agentTuiSpawning.js b/server/services/agentTuiSpawning.js
index 0ea2813ce2..34b8b17799 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -1043,6 +1043,13 @@ export async function spawnTuiAgent({
const idleTimer = setInterval(() => {
if (!promptSentAt || finalized) return;
+ // Don't reap a session a human is actively watching in the Shell page —
+ // they may be mid-paste or reading output. A big bracketed paste can sit
+ // in a silent reflow/commit window with no PTY output yet, which looks
+ // identical to "idle" to this timer otherwise (issue: paste into a live
+ // agent TUI got reaped mid-paste, same class of bug as #2074/#2084 but for
+ // an attached viewer instead of a merge-queue/review-loop wait).
+ if (sessionId && shellService.isExternalSessionAttached(sessionId)) return;
const runtime = Date.now() - promptSentAt;
const idle = Date.now() - lastOutputAt;
if (runtime < DEFAULT_TUI_MIN_RUNTIME_MS) return;
diff --git a/server/services/agentTuiSpawning.test.js b/server/services/agentTuiSpawning.test.js
index 49a4cbb6e8..1d33d08c75 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -8,7 +8,8 @@ vi.mock('./shell.js', () => ({
writeToSession: vi.fn(),
killSession: vi.fn(),
getSession: vi.fn(),
- getSessionProcess: vi.fn()
+ getSessionProcess: vi.fn(),
+ isExternalSessionAttached: vi.fn().mockReturnValue(false)
}));
vi.mock('./cosEvents.js', () => ({
@@ -396,6 +397,11 @@ describe('spawnTuiAgent runtime', () => {
// Tests that want to exercise the idle-no-changes failure path override this.
vi.mocked(gitService.getStatus).mockResolvedValue({ clean: false, files: [{ path: 'file.txt', status: 'M' }] });
vi.mocked(gitService.getDiff).mockResolvedValue('diff content here');
+
+ // Reset viewer-attached state: no Shell viewer bound by default. The
+ // viewer-attached test overrides this — clearAllMocks doesn't undo a
+ // mockReturnValue override, so it must be reset explicitly here.
+ vi.mocked(shellService.isExternalSessionAttached).mockReturnValue(false);
});
afterEach(() => {
@@ -479,6 +485,42 @@ describe('spawnTuiAgent runtime', () => {
);
});
+ // ── 1b. Idle timer must not reap a session a human is actively viewing ──────
+ // A large bracketed paste into a live agent TUI can sit in a silent
+ // reflow/commit window with no PTY output yet, which looks identical to
+ // "idle" to this timer. If a viewer is attached (Shell page open on this
+ // session), the idle reaper must not fire at all — mirrors the guard
+ // tuiPromptRunner.js already has for its one-shot idle-watch timer.
+ it('idle timer does not reap when isExternalSessionAttached returns true', async () => {
+ vi.mocked(shellService.isExternalSessionAttached).mockReturnValue(true);
+
+ runSpawn();
+ await flushMicrotasks();
+
+ await capturedOnData(Buffer.from('Codex booting...\n'));
+ await flushMicrotasks();
+
+ await vi.advanceTimersByTimeAsync(2000);
+ await flushMicrotasks();
+
+ await capturedOnData(Buffer.from('do the thing\n'));
+ await flushMicrotasks();
+
+ await vi.advanceTimersByTimeAsync(3600);
+ await flushMicrotasks();
+
+ 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 well past DEFAULT_TUI_MIN_RUNTIME_MS + idleTimeoutMs — the
+ // un-guarded timer would have reaped by now (see test 1 above).
+ await vi.advanceTimersByTimeAsync(21000);
+ await flushMicrotasks();
+
+ expect(agentLifecycle.finalizeAgent).not.toHaveBeenCalled();
+ });
+
// ── 1a. Idle-out with NO work activity → failure (issue #1229) ───────────────
// The bug: when the prompt never submits, the TUI keeps repainting its banner /
// status line, so `lastOutputAt > promptSentAt` passes on pure chrome churn and