Skip to content

fix(tui): guard messages.data in session.sync against undefined#26566

Merged
kitlangton merged 2 commits into
devfrom
kit/fix-26560-session-sync-undefined-data
May 9, 2026
Merged

fix(tui): guard messages.data in session.sync against undefined#26566
kitlangton merged 2 commits into
devfrom
kit/fix-26560-session-sync-undefined-data

Conversation

@kitlangton
Copy link
Copy Markdown
Contributor

Summary

Closes #26560.

packages/opencode/src/cli/cmd/tui/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 returns a non-2xx response, the SDK leaves data undefined and messages.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.tsx mounts the SyncProvider with a stubbed fetch where /session/<id>/messages returns 500. Pre-fix it fails with the exact TypeError from 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

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.
@kitlangton kitlangton enabled auto-merge (squash) May 9, 2026 19:56
@kitlangton kitlangton merged commit 6c2dfd2 into dev May 9, 2026
10 checks passed
@kitlangton kitlangton deleted the kit/fix-26560-session-sync-undefined-data branch May 9, 2026 19:58
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.

Bug Report: TypeError: undefined is not an object (evaluating f.data.map) when entering certain sessions

1 participant