GeezerVison is showing github.com — feat(session): add viewed state by kitlangton · Pull Request #42811 · anomalyco/opencode
Conversation
kitlangton
commented
Aug 15, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Problem
Unread state previously lived in each TUI's local tab file. That made attention state client-specific instead of a fact about the Session:
sessionFinishes()
clientA.unread = true
clientB.opensSession()
clientB.unread = false
clientA.unread = true // the clients still disagree
The state also disappeared with local storage and could not be reconstructed by replaying server history.
Change
Sessions now expose two server-owned timestamps plus the last terminal outcome, and the TUI derives unread state from them:
function isUnread(session: Session.Info) {
if (session.time.idle === undefined) return false
if (session.time.viewed === undefined) return true
return session.time.idle > session.time.viewed
}
time.idle records the latest user-visible terminal execution. time.viewed records the latest idle transition acknowledged through POST /api/session/:sessionID/view. outcome records whether the run recorded at time.idle succeeded, failed, or was interrupted, so failed background sessions keep their distinct error treatment across restarts and clients.
How It Works
function projectTerminal(event: ExecutionTerminal) {
projectExecution(event)
if (event.type === "session.execution.interrupted" && event.reason === "shutdown") return
// Strictly advance even when two terminal events share a millisecond.
session.time.idle = Math.max(event.created, (session.time.idle ?? event.created - 1) + 1)
session.outcome = terminalOutcome(event.type) // "succeeded" | "failed" | "interrupted"
preserve(session.time.updated)
}
function viewSession(sessionID: Session.ID) {
const session = select(sessionID, ["time.idle", "time.viewed"])
if (!session) throw new Session.NotFoundError({ sessionID })
if (session.time.idle === undefined) return
if (session.time.viewed !== undefined && session.time.viewed >= session.time.idle) return
// The event carries the idle watermark the viewer observed, so a completion
// that lands between this check and projection stays unread.
publish({ type: "session.viewed", sessionID, idle: session.time.idle })
}
function projectViewed(event: SessionViewed) {
// Monotone: duplicates and stale watermarks never regress the projection.
session.time.viewed = Math.max(event.idle, session.time.viewed ?? event.idle)
preserve(session.time.updated)
}
function tabUnread(rootID: Session.ID) {
// Unread reads the root session only: background subagent completions wake the
// parent, whose own idle transition then carries the signal.
const root = session(rootID)
if (!isUnread(root)) return undefined
return root.outcome === "failed" ? "error" : "activity"
}
function acknowledgeVisibleSession(rootID: Session.ID) {
// Runs even with tabs disabled: viewed state is server-global.
if (!terminal.focused) return
const root = session(rootID)
if (!isUnread(root) || acknowledged.get(rootID) === root.time.idle) return
return api.session.view({ sessionID: rootID })
}
The TUI no longer persists or mutates its own unread map. It refreshes authoritative Session info after terminal and viewed events for sessions it has loaded, derives the tab marker from the root session's own timestamps, and marks the root viewed when that session is visible in a focused terminal.
Example
// No completed execution
{ idle: undefined, viewed: undefined } // read
// An execution fails
{ idle: 100, viewed: undefined, outcome: "failed" } // tab unread with error styling
// Any focused TUI opens the root tab
{ idle: 100, viewed: 100 } // every client converges to read
// A later execution finishes
{ idle: 200, viewed: 100, outcome: "succeeded" } // unread again, activity styling
Flow
sequenceDiagram
participant Runner
participant Bus as Durable event bus
participant Store as Session projection
participant B as Viewing TUI
participant A as Other TUI
Runner->>Bus: execution terminal
Bus->>Store: advance time.idle, record outcome
Bus-->>A: terminal event, refresh Session
Bus-->>B: terminal event, refresh Session
B->>Store: POST /session/:id/view
Store->>Bus: session.viewed (observed idle watermark)
Bus->>Store: time.viewed = max(watermark, time.viewed)
Bus-->>A: viewed event, refresh Session
Bus-->>B: viewed event, refresh Session
Loading
Reviewer Map
packages/schema defines the timestamps, outcome, and the durable session.viewed event with its idle watermark.
packages/core owns persistence, migration, terminal/viewed projection, replay, and Session.view.
packages/protocol, packages/server, and generated clients expose the HTTP operation.
packages/client/src/solid/data.ts refreshes shared Solid Session info after authoritative events, for sessions the client already loaded.
packages/tui/src/context/session-tabs.tsx removes local unread ownership and derives/acknowledges the root session's state.
Demo
Two wall-clock-aligned OpenCode Drive TUIs share one isolated server. The left client stays on Active workspace; the right client opens unread tab 1. The purple unread marker appears in both clients, then clears in the left client as soon as the right client views the completed background session.
session-viewed-two-clients-synced.mp4
Verification
TUI: 735 passed / 5 skipped. Core: 1856 passed / 16 skipped, including viewed-watermark race, outcome projection, replay, migration, and transfer coverage. Client, Server, Schema suites and Client, Core, Protocol, TUI, generated, and workspace typechecks pass. Two packages/client test failures and one packages/schema failure reproduce on a pristine origin/v2 checkout and are unrelated to this branch.
Scope
outcome is projected purely from existing terminal event types; no new durable event was added for it, and "running" liveness deliberately stays with the execution claim and live event stream rather than this field.
- The
session.viewed event carries the observed idle watermark, so a delayed or duplicate view request can never mark a newer completion viewed; concurrent requests append harmless monotone facts rather than introducing a process-local lock.
- Client event handling refreshes only sessions already loaded by that client, matching sibling event handlers.
- Acknowledgement is decoupled from the tabs feature: a TUI with
tabs.enabled: false still reports viewed state that other clients depend on.
- Tab unread deliberately reads the root session only. Background subagent completions notify and wake the parent, whose own idle transition carries the signal; subagent sessions' viewed state is currently unconsumed, and per-subagent unread (plus wake durability across server restarts) is out of scope.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
AI code review — automated review for reference; please use your judgment.
Diff too large for automated review (36 files, ~490KB, including generated client/schema/migration artifacts) — recommend human review.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|