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 (claude): use a real viewer-attached check, not external-only

Adam Eivy61db9da0892a
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 7ebd843b50..97b381d640 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -2,7 +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`)
+- **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 long-running CoS-agent idle timer in `agentTuiSpawning.js` now checks a new `shell.js` export, `isSessionViewed()`, before reaping — true whenever any socket is bound to the session — and skips the reap entirely whenever a Shell viewer is attached. (The existing `isExternalSessionAttached()`, which the one-shot `tuiPromptRunner.js` idle-watch already uses, only covers sessions registered via `registerExternalSession` and would never have fired for a regular agent-tui session, so a plain reuse of it would have been a no-op guard.) (`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.
- **[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 34b8b17799..f00193400d 100644
--- a/server/services/agentTuiSpawning.js
+++ b/server/services/agentTuiSpawning.js
@@ -1049,7 +1049,7 @@ export async function spawnTuiAgent({
// 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;
+ if (sessionId && shellService.isSessionViewed(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 1d33d08c75..9586cf1e5f 100644
--- a/server/services/agentTuiSpawning.test.js
+++ b/server/services/agentTuiSpawning.test.js
@@ -9,7 +9,7 @@ vi.mock('./shell.js', () => ({
killSession: vi.fn(),
getSession: vi.fn(),
getSessionProcess: vi.fn(),
- isExternalSessionAttached: vi.fn().mockReturnValue(false)
+ isSessionViewed: vi.fn().mockReturnValue(false)
}));
vi.mock('./cosEvents.js', () => ({
@@ -401,7 +401,7 @@ describe('spawnTuiAgent runtime', () => {
// 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);
+ vi.mocked(shellService.isSessionViewed).mockReturnValue(false);
});
afterEach(() => {
@@ -491,8 +491,8 @@ describe('spawnTuiAgent runtime', () => {
// "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);
+ it('idle timer does not reap when isSessionViewed returns true', async () => {
+ vi.mocked(shellService.isSessionViewed).mockReturnValue(true);
runSpawn();
await flushMicrotasks();
diff --git a/server/services/shell.js b/server/services/shell.js
index 2cf57b03b6..10e5c7a2ff 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -343,6 +343,19 @@ export function isExternalSessionAttached(sessionId) {
return !!(session && session.external && session.socket);
}
+/**
+ * True when ANY socket is currently attached to this session — an interactive
+ * Shell viewer or an external TUI-run viewer. Broader than
+ * `isExternalSessionAttached`, which only covers sessions registered via
+ * `registerExternalSession` (the one-shot tuiPromptRunner path); this one also
+ * covers regular sessions (agent-tui, plain interactive shells) viewed via
+ * `attachSession`, which never sets `external`.
+ */
+export function isSessionViewed(sessionId) {
+ const session = shellSessions.get(sessionId);
+ return !!(session && session.socket);
+}
+
/**
* Public "stopped viewing" signal — the client emits this when it leaves the
* Shell page (the SocketProvider socket persists across navigations, so a plain
diff --git a/server/services/shell.test.js b/server/services/shell.test.js
index 97172fe41c..b4400e5b1a 100644
--- a/server/services/shell.test.js
+++ b/server/services/shell.test.js
@@ -475,6 +475,38 @@ describe('registerExternalSession / unregisterExternalSession', () => {
expect(shell.isExternalSessionAttached(shellId)).toBe(false);
});
+ it('isSessionViewed is true for a plain interactive/agent-tui session with a bound viewer (unlike isExternalSessionAttached)', () => {
+ // The gap isExternalSessionAttached has: it only ever returns true for
+ // sessions registered via registerExternalSession (external: true) — a
+ // regular createShellSession session (what CoS agent-tui sessions use)
+ // never sets `external`, so isExternalSessionAttached is permanently
+ // false for it even with a live viewer attached. isSessionViewed covers
+ // both cases by keying purely on whether a socket is bound.
+ const owner = makeSocket('owner');
+ const shellId = shell.createShellSession(owner);
+ expect(shell.isSessionViewed(shellId)).toBe(true); // createShellSession binds the creating socket
+ expect(shell.isExternalSessionAttached(shellId)).toBe(false); // still false — not an external session
+
+ shell.detachSocketSessions(owner);
+ expect(shell.isSessionViewed(shellId)).toBe(false);
+
+ const viewer = makeSocket('viewer');
+ shell.attachSession(shellId, viewer);
+ expect(shell.isSessionViewed(shellId)).toBe(true);
+ expect(shell.isExternalSessionAttached(shellId)).toBe(false); // attachSession never sets external either
+
+ shell.detachSocketSessions(viewer);
+ expect(shell.isSessionViewed(shellId)).toBe(false);
+
+ // Never true for unknown ids.
+ expect(shell.isSessionViewed('missing')).toBe(false);
+
+ // Still true for an external (TUI-run) session with a viewer — isSessionViewed is a superset.
+ const runId = shell.registerExternalSession('run-viewed', makeFakePty(), {});
+ shell.attachSession(runId, viewer);
+ expect(shell.isSessionViewed(runId)).toBe(true);
+ });
+
it('releases a watched run when the same socket attaches to another session', () => {
const viewer = makeSocket('viewer');
const runId = shell.registerExternalSession('run-watch', makeFakePty(), {});