cloudaxe-opencode/packages/opencode/src
Kit Langton ab31c41fee refactor(bus): one GlobalBus emit per event, with optional sync metadata
Today every sync.run produces TWO GlobalBus events:

  1. From bus.publish itself — the projection view:
     { payload: { id, type: "session.created", properties } }

  2. From sync/index.ts — the source-of-truth envelope:
     { payload: { type: "sync", syncEvent: { type: "session.created.1", id, seq, aggregateID, data } } }

These two emits live in different files and discriminate via
`payload.type` — either a real event type or the sentinel "sync".
That collision is the source of confusion in the codebase:
consumers have to know which shape they're looking at.

The duality dates back to:
  - d88912abf0 (Dec 2025): Dax added GlobalBus.emit inside bus.publish.
  - b22add292c (#22347, Apr 2026): James consolidated sync events onto
    GlobalBus by ADDING a second emit with the source envelope.

#22347's stated intent was "consolidate events into a single stream".
Two emits with different shapes on one channel isn't quite that. One
emit per event with a unified shape is.

This PR collapses the two emits into one:

  { payload: { id, type: "session.created", properties, sync?: { name, seq, aggregateID, data } } }

- bus.publish accepts an optional `sync?: SyncMetadata` option.
- sync/index.ts passes it; its own GlobalBus.emit is removed.
- BusEvent.effectPayloads() adds an optional `sync` field to every
  event schema.
- SyncEvent.effectPayloads() is no longer included in the wire schema
  (it's now subsumed by the optional field).
- Consumers migrate from `payload.type === "sync"` filtering to
  `payload.sync != null` filtering. control-plane/workspace.ts
  reconstructs SerializedEvent from { id, sync.* } for replay.

Note: this is a BREAKING SDK wire-format change. External consumers
that read `payload.type === "sync"` or `payload.syncEvent` need to
migrate. Internal opencode consumers all migrated in this PR.

Side observation: the runtime emit was using `payload.syncEvent`
(nested) while the SDK schema declared the fields at top level under
`type: "sync"` — a silent schema drift. Collapsing to one emit also
fixes that drift by definition.

Verified:
  - bun typecheck — clean
  - bun run test test/sync/index.test.ts test/bus/bus-effect.test.ts
    test/server/httpapi-event.test.ts
    test/server/httpapi-event-diagnostics.test.ts — 29/29 green
  - bun run test test/server/httpapi-sdk.test.ts -t "streams sync-backed" — green
2026-05-18 13:23:30 -04:00
..
account refactor: remove module barrels (#24554) 2026-04-27 14:33:33 -04:00
acp refactor(acp): extract runtime reentry (#27769) 2026-05-15 23:58:52 +05:30
agent fix(provider): type model not found errors (#27334) 2026-05-13 22:23:09 +00:00
auth Drop unused opencode Zod statics (#26935) 2026-05-11 17:14:18 -04:00
background feat(core): add background job service (#27033) 2026-05-12 15:22:38 +05:30
bus refactor(bus): one GlobalBus emit per event, with optional sync metadata 2026-05-18 13:23:30 -04:00
cli refactor(bus): one GlobalBus emit per event, with optional sync metadata 2026-05-18 13:23:30 -04:00
command refactor(project): import instance context directly (#27714) 2026-05-15 15:59:56 +05:30
config refactor(reference): normalize config entries (#28178) 2026-05-18 20:42:41 +05:30
control-plane refactor(bus): one GlobalBus emit per event, with optional sync metadata 2026-05-18 13:23:30 -04:00
effect Load models.dev snapshot from build global (#28077) 2026-05-17 21:59:44 -04:00
env refactor: remove module barrels (#24554) 2026-04-27 14:33:33 -04:00
file fix: preserve bus instance context (#28051) 2026-05-17 12:56:42 -04:00
format refactor(project): import instance context directly (#27714) 2026-05-15 15:59:56 +05:30
git effect(core): track stderr truncation; polish AppProcess callers (#27353) 2026-05-13 20:31:03 -04:00
id feat(core): add background job service (#27033) 2026-05-12 15:22:38 +05:30
ide Remove Zod from named errors (#26982) 2026-05-12 09:20:15 -04:00
image fix: image resizer wasm loading, reenable image resizing (#26805) 2026-05-13 23:06:07 -05:00
installation effect(core): track stderr truncation; polish AppProcess callers (#27353) 2026-05-13 20:31:03 -04:00
lsp fix: preserve bus instance context (#28051) 2026-05-17 12:56:42 -04:00
mcp Remove local MCP Zod schema (#27095) 2026-05-12 12:38:27 -04:00
patch chore: generate 2026-05-14 00:33:32 +00:00
permission Drop unused opencode Zod statics (#26935) 2026-05-11 17:14:18 -04:00
plugin fix(bus): acquire PubSub subscription eagerly to close /event race (#27959) 2026-05-18 11:38:05 -04:00
project fix(bus): acquire PubSub subscription eagerly to close /event race (#27959) 2026-05-18 11:38:05 -04:00
provider Load models.dev snapshot from build global (#28077) 2026-05-17 21:59:44 -04:00
pty Drop unused domain Zod statics (#26927) 2026-05-11 16:10:58 -04:00
question Drop unused opencode Zod statics (#26935) 2026-05-11 17:14:18 -04:00
reference refactor(session): extract reference prompt helpers (#28197) 2026-05-18 22:32:18 +05:30
server refactor(bus): one GlobalBus emit per event, with optional sync metadata 2026-05-18 13:23:30 -04:00
session chore: generate 2026-05-18 17:03:44 +00:00
share fix(bus): acquire PubSub subscription eagerly to close /event race (#27959) 2026-05-18 11:38:05 -04:00
shell refactor: remove module barrels (#24554) 2026-04-27 14:33:33 -04:00
skill refactor(flags): migrate external skills flag (#27685) 2026-05-15 13:24:56 +05:30
snapshot effect(core): add stdin option to AppProcess.run; migrate snapshot+clipboard (#27224) 2026-05-13 11:10:23 -04:00
storage refactor(instance): remove remaining bind call sites (#27731) 2026-05-15 20:04:42 +05:30
sync refactor(bus): one GlobalBus emit per event, with optional sync metadata 2026-05-18 13:23:30 -04:00
tool refactor(repository): type cache failures (#28188) 2026-05-18 21:46:18 +05:30
util refactor(repository): clarify reference domain (#28182) 2026-05-18 21:04:44 +05:30
v2 fix: sort v2 session list by updated time (#27954) 2026-05-18 09:01:55 +10:00
worktree effect(worktree): migrate to AppProcess.run (#27186) 2026-05-13 11:39:35 -04:00
audio.d.ts Add TUI notifications and attention sounds (disabled by default) (#26980) 2026-05-13 03:26:25 +02:00
data-migration.sql.ts Add background code migration service (#26652) 2026-05-11 00:16:42 +00:00
data-migration.ts fix(session): preserve usage update timestamps (#27094) 2026-05-12 22:10:28 +05:30
event-v2-bridge.ts test fixes 2026-05-14 22:32:38 -04:00
index.ts Remove Zod from named errors (#26982) 2026-05-12 09:20:15 -04:00
markdown.d.ts feat(skill): built-in opencode-meta skill (#26617) 2026-05-09 22:05:37 -04:00
node.ts refactor: remove module barrels (#24554) 2026-04-27 14:33:33 -04:00
sql.d.ts
temporary.ts core: consolidate shared infrastructure into core package 2026-04-25 13:30:37 -04:00