Stream updates for sessions started outside the web UI
Problem
A session that omp writes from a terminal — or that a harness starts by launching omp — never updates while it is open in ompweb. The file grows, the transcript stays frozen, and you have to reload the page to see the turn that just landed.
The cause is that live updates are only wired to sessions ompweb spawns itself: /api/agent/running/events reports getRunningRpcSessionIds(), and nothing watches the session files. For an externally-owned session there is no RPC stream, so no signal exists that it advanced.
We hit this running omp under a wrapper: the session shows up in the sidebar and reads correctly, but watching a run means refreshing repeatedly.
Change
lib/session-watcher.ts— a debounced (250 ms) recursive watch of<agentDir>/sessions. On change it callsinvalidateSessionListCache(), resolves the changed paths throughresolveSessionIdByPath, and reports the session ids. Created on first subscribe, closed on last unsubscribe.app/api/agent/running/events/route.ts— forwards those ids as asessions-changedframe alongside the existingrunningframes, withrefreshSessionList: true.lib/session-change-bus.ts— a tiny in-process pub/sub, because the SSE is consumed bySessionSidebarwhile the open transcript lives inuseAgentSessionunder a sibling component. This avoids threading a callback throughAppShellandChatWindow.SessionSidebar— onsessions-changed, reuses the existing 300 ms-debounced list refresh and republishes the ids.useAgentSession— reloads the open transcript when the changed ids include the current session and no event stream is attached, so a session ompweb owns keeps its RPC stream as the single authority and never sees a competing refetch.
Notes on the approach
/api/agent/[id]/events looked like the natural place, but it calls startRpcSession on a cache miss — subscribing there for an externally-owned session would spawn a second omp against a JSONL the running omp already owns, which is exactly what the README warns about. Hence the global stream plus the ownership guard.
Failure is soft: if the sessions directory does not exist yet, or a platform refuses a recursive watch, the watcher stays null and behaviour is unchanged.
Verification
Against a production build (npm run build), appending a line to a session file ompweb did not start:
data: {"type":"running","runningSessionIds":[]}
data: {"type":"sessions-changed","sessionIds":["01a0139b-c66b-7000-a3ee-09e7b0a23a25"],"refreshSessionList":true}
— naming exactly the session whose file changed.
npm run typecheck and eslint are clean. npm test passes with three new cases for the bus (fan-out, unsubscribe/empty batches, and a throwing subscriber not stopping others). One pre-existing failure is unrelated: lib/worktree.test.mjs fails on macOS before this change too, comparing /var/... against its /private/var/... realpath.
I deliberately did not add a test for the watcher itself — fs.watch tests are timing-dependent and I did not see a precedent for them in the suite. Happy to add one if you would like it.
Summary by cubic
Streams live updates for sessions started outside the web UI by watching session files and forwarding changes over the running-events SSE. Previously, these sessions stayed frozen until a manual reload; now the sidebar refreshes and the open transcript reloads when their files grow, while RPC-owned sessions keep their stream as the single authority.
- Adds lib/session-watcher.ts: debounced (250 ms) recursive watch of /sessions; on change invalidates the session-list cache, resolves changed paths to session IDs, and notifies subscribers. Created on first subscribe and closed on last; hardened to retry on watch errors or missing dirs (5 s backoff) and to rescan on coalesced/overflow events.
- Updates app/api/agent/running/events/route.ts: subscribes to the watcher and emits {"type":"sessions-changed","sessionIds":[...],"refreshSessionList":true} frames alongside "running"; cleans up both subscriptions.
- Adds lib/session-change-bus.ts (+ tests): in-process pub/sub to relay changed IDs from the sidebar to useAgentSession; tests cover fan-out, unsubscribe/empty batches, and error isolation.
- Updates components/SessionSidebar.tsx: handles "sessions-changed", reuses the existing debounced list refresh, and republishes IDs on the bus.
- Updates hooks/useAgentSession.ts: on bus events, reloads the open transcript only when its ID changed and no event stream is attached (avoids competing refetches for RPC-owned sessions).
- Avoids using
/api/agent/[id]/eventsbecause it starts an RPC session on cache miss, which could spawn a secondompon a file already owned by a running process.
Summary by CodeRabbit
-
New Features
- Session lists now refresh automatically when session files change.
- Active session transcripts update when modified externally.
- Live updates identify affected sessions for faster, more targeted refreshes.
-
Bug Fixes
- Improved resilience when session monitoring encounters errors or unavailable directories.
- Prevented duplicate refreshes and ensured updates continue if one listener fails.