fix(tui): guard messages.data in session.sync against undefined#26566
Merged
Conversation
The session.sync function in context/sync.tsx had a non-null assertion on messages.data while the same function defensively guarded todo.data and diff.data with `?? []`. When the messages endpoint surfaces a non-2xx response (e.g. transient errors, post-Hono Schema validation failures), the SDK leaves data undefined and `.map(...)` crashes the whole TUI before the user can see anything. Use the same `?? []` guard pattern already in use two lines above and below. Includes a reproducer that fails on dev with the TypeError and passes after this change. Closes #26560
…sage walk Test review found the new reproducer was 175 lines that mostly duplicated the existing sync.test.tsx scaffolding. Extract mount, createFetch, wait, Probe, and worktree/directory constants into a shared sync-fixture.tsx so both tests stay light and the override hook lets each test customize only what it needs. Also collapse the .map + for in sync.session.sync into a single pass — saves an allocation and a traversal on the hot path. No behavior change to the fix itself.
This was referenced May 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes #26560.
packages/opencode/src/cli/cmd/tui/context/sync.tsxhad a non-null assertion onmessages.datawhile the same function defensively guardedtodo.dataanddiff.datawith?? []. When the messages endpoint returns a non-2xx response, the SDK leavesdataundefined andmessages.data!.map(...)crashes the whole TUI before the user can see anything — making affected sessions completely inaccessible.Why this surfaces now
Pre-Hono-migration the messages endpoint always returned a JSON array body. After the move to Effect HttpApi, edge cases (Schema validation failures, transient 4xx/5xx) can produce response shapes the TUI client wasn't defensively coded for. Issue reporter @LouisYu04 did excellent forensics — extracted the crashing code from the binary, traced it to source, and proposed exactly this fix.
Change
Apply the same
?? []guard pattern already in use on the lines immediately above (todo.data ?? []) and below (diff.data ?? []).Reproducer
New test
packages/opencode/test/cli/cmd/tui/sync-undefined-messages.test.tsxmounts the SyncProvider with a stubbed fetch where/session/<id>/messagesreturns 500. Pre-fix it fails with the exactTypeErrorfrom the bug report; post-fix the sync resolves cleanly. Verified red → green → red → green locally.Test
bun run test test/cli/cmd/tui/sync-undefined-messages.test.tsx— passes (would fail without the fix)bun run test test/cli/cmd/tui/sync.test.tsx— existing sync tests still pass