Skip to content

fix(client): preserve inbox events during snapshot reads - #49106

Merged
kitlangton merged 1 commit into
v2from
remote-inbox
Sep 15, 2026
Merged

kitlangton merged 1 commit into
v2from
remote-inbox

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

Why

An already-answered question can jump to the bottom of the conversation and remain marked as pending when a remote client reconnects. A delayed inbox HTTP response overwrites newer delivery events, even though the server has already consumed the input and answered subsequent messages.

The affected data layer is shared by the TUI and desktop app.

What changes

While an inbox read is in flight When its response arrives
A prompt is delivered or cancelled Keep it out of pending input
New input is enqueued Retain the newer admission
Input changes between queue and steer Retain the newer delivery mode
Local submissions are still awaiting acknowledgement Preserve their submission order and existing rollback behavior

Track inbox changes only for the lifetime of the read and apply them over its snapshot before publishing it. Optimistic submissions remain owned by the existing outbox rather than being treated as authoritative events. The read journal is released after reconciliation or failure.

Demo

remote-inbox-before-after.mp4

Left: base 4fc5ba1358. Right: d6492f41d1. Both are the real production TUI driven by the same OpenCode Drive script with a simulated model. A local HTTP proxy holds one actual, non-empty inbox response across reconnect while allowing the event stream and subsequent answers through. Releasing that response reproduces the misplaced pending question on the left; the right keeps it in the conversation. The server inbox is verified empty on both sides before release.

The clips are aligned to response release, run at normal speed, and retain the native capture resolution. Drive required a disposable current-client compatibility shim; that separate issue is tracked in anomalyco/opencode-drive#87.

Scope

Shared client inbox hydration and six focused regression cases. The deterministic visible reproduction covers the TUI; the desktop app consumes the same fixed store.

Verification

# Repository root, Bun 1.4.2
bun run check

# packages/client
bun run test test/solid-inbox-race.test.ts test/solid-data.test.ts test/solid-compaction.test.ts test/solid-eviction.test.ts
bun run test
  • Root lint/typecheck and the pre-push check pass.
  • All 59 focused tests pass, including delivered/cancelled input, concurrent enqueue/delivery changes, optimistic submission order, and compaction coalescing after a failed POST.
  • The delivered-input regression failed before the fix. The matched Drive scenario reproduces the visible defect on the base and passes on the committed fix.
  • Drive's seed-42, 12-step network gauntlet also completed: four submissions, two latency windows, two blackhole windows, and one steer.
  • The broader client suite has three existing failures, reproduced separately on unchanged 4fc5ba1358: session methods retain decoded Effect inputs and outputs, public import boundaries > isolates each public entrypoint, and session methods use the public HTTP contract. Those broad-suite/base comparisons used Bun 1.4.0; the final focused suite and root check also pass with the repository-required Bun 1.4.2.

@kitlangton
kitlangton enabled auto-merge (squash) September 15, 2026 04:19
@kitlangton
kitlangton merged commit 1aaa806 into v2 Sep 15, 2026
14 of 15 checks passed
@kitlangton
kitlangton deleted the remote-inbox branch September 15, 2026 04:28

@holny holny left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the diff and ran the suite locally in a fresh worktree at d6492f4: solid-inbox-race 6/6 pass, typecheck clean. Full packages/client run is 164 pass / 3 fail, and those 3 fail identically on origin/v2 (effect/zod drift in promise.test.ts + import-boundaries), so not from this change.

The journal approach looks right. Things I specifically checked:

  • Coverage: every write into session.pending goes through removePending, updatePending, or the enqueued handler, so the journal sees all local observations. The only direct setStore sites are the delivery update at data.ts:316 and the reconcile at :1415.
  • Same-session reads can't interleave: createSync joins the active run or queues a restart (data.ts:168-174), so two journals for one session never overlap, and the identity check at data.ts:1419 keeps a superseded journal from deleting its successor.
  • Field completeness: the enqueued echo plus inboxID/sessionID/event.created rebuilds an exact SessionInboxInfo row (generated/types.ts:2163, variants at :2110), so the full replace in the reconcile can't silently drop fields versus the list snapshot.
  • Ordering: Map insertion order means a delivered tombstone that arrives after an enqueued echo wins at reconcile time, which matches consume-after-admit causality. And this lines up with the server model where session_inbox only holds unconsumed work, so treating the SSE echo as newer-than-snapshot is the right direction.

Two small things, take or leave:

  1. data.ts:255 — the journal value packs three meanings (full item = admission, raw string = delivery change, undefined = tombstone) discriminated by the typeof check in the reconcile. A one-line comment on the declaration would save the next reader the archaeology.
  2. The failure path ("journal is released after reconciliation or failure") is only covered implicitly by the tests. A cheap extra case — reject the response while a delivered event landed mid-read, then assert pending stays empty — would pin the finally behavior.

Nice repro in the demo, by the way. Holding one real inbox response behind a proxy while letting events through makes the failure mode obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants