Skip to content

feat(events): SessionEvent type + 旁路写 shadow write (S2.1, PR #7) - #160

Merged
dahai80 merged 1 commit into
mainfrom
feat/event-sourcing-sessionevent
Aug 27, 2026
Merged

dahai80 merged 1 commit into
mainfrom
feat/event-sourcing-sessionevent

Conversation

@dahai80

@dahai80 dahai80 commented Aug 27, 2026

Copy link
Copy Markdown
Owner

Summary

ar-plan §4 P3/S2.1 (PR #7 of 9): introduce event-sourcing types + shadow write. First step of the 渐进 migration to event-sourcing — mutableMessages stays truth source, event log = write-only shadow.

Changes

  • src/services/events/SessionEvent.ts (NEW) — SessionEventType union (8 types: user_message/assistant_message/tool_use/tool_result/compact/error/turn_start/turn_end), SessionEvent interface (seq/data/surfaceOp/sourceEventSeqs), SessionEventLog type, serializeLog helper. Pure types, no runtime side-effect.
  • src/services/events/eventLog.ts (NEW) — appendEvent immutable pure function, SessionEventRecorder class (per-session, monotonic seq counter, fail-open try/catch+log), isEventSourcingEnabled gate, NOOP_RECORDER (unconditional no-op subclass → byte-identical off even if env flips mid-session).
  • src/QueryEngine.ts — per-session eventRecorder field, init in ctor (on = new SessionEventRecorder(getSessionId()), off = NOOP_RECORDER). Shadow-record 8 mutableMessages.push points (user_message, assistant_message ×3, tool_result ×2, compact ×2) + turn_start/turn_end boundaries. No push logic changed — record is a bypass after each push.

Design

  • Shadow write only (S2.1): write event log, don't read it. No deriveMessages projection, no assertions (those are PR feat: SDK control protocol for code.* and artifact_chat APIs #8/S2.2).
  • mutableMessages stays truth source — event log is a shadow. FUSION_CODE_EVENT_SOURCING=1 enables; off = recorder no-op, empty log, mutableMessages byte-identical.
  • Fail-open: recorder errors are caught + logged (Rule 12 declares), never block the main push path.
  • In-memory only (S2.1); persistence to disk = S2.3 (later PR).
  • NOOP_RECORDER is an unconditional no-op subclass — earlier draft was a live SessionEventRecorder that would record if env flipped on mid-session, breaking the byte-identical-off claim. Subclass overrides record/recordCompact to unconditional no-op.

Acceptance (ar-plan lines 318-322)

  • FUSION_CODE_EVENT_SOURCING=1 → eventLog has ordered events, seq monotonic
  • env unset → recorder no-op, eventLog empty, mutableMessages byte-identical
  • shadow write errors don't block main path (try/catch + log, fail-open)
  • 4 gates pass

Gates

  • bun run typecheck — 0 errors
  • bun test src/__tests__/ — 625/625 pass (615 + 10 new)
  • bunx biome check — new files clean (QueryEngine pre-existing crypto/non-null lint not touched)
  • bun run build:dev — pass

Test coverage

src/__tests__/services/events/eventLog.test.ts — 10 tests: appendEvent immutability + ordering, recorder off no-op, seq monotonic, surfaceOp/sourceEventSeqs, fail-open (bad data no throw), NOOP_RECORDER unconditional no-op, isEventSourcingEnabled env parsing.

Co-Authored-By: Claude Fable 5 [email protected]

ar-plan §4 P3/S2.1: introduce event-sourcing types + shadow write.

- src/services/events/SessionEvent.ts (NEW): SessionEventType union (8
  types), SessionEvent interface (seq/data/surfaceOp/sourceEventSeqs),
  SessionEventLog type, serializeLog helper. Pure types, no runtime
  side-effect.
- src/services/events/eventLog.ts (NEW): appendEvent immutable pure
  function, SessionEventRecorder class (per-session, monotonic seq,
  fail-open try/catch+log), isEventSourcingEnabled gate,
  NOOP_RECORDER (unconditional no-op subclass for byte-identical off).
- src/QueryEngine.ts: per-session eventRecorder field, init in ctor
  (on=new SessionEventRecorder(getSessionId()), off=NOOP_RECORDER).
  Shadow-record 8 mutableMessages push points (user_message 456,
  assistant_message 817/823/869, tool_result 838/936, compact 1020/1024)
  + turn_start/turn_end boundaries. mutableMessages stays truth source;
  event log = shadow (write-only, no read switch, no assertions).

Default-off (FUSION_CODE_EVENT_SOURCING=1). Off = recorder no-op, empty
log, mutableMessages byte-identical. Fail-open: recorder errors log but
don't block main path. In-memory only (S2.1; persistence = S2.3 later).

Gates: typecheck 0, 625/625 test, lint clean (new files), build:dev pass.

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 22eb14e into main Aug 27, 2026
1 check passed
@dahai80
dahai80 deleted the feat/event-sourcing-sessionevent branch August 27, 2026 08:33
dahai80 added a commit that referenced this pull request Aug 27, 2026
…2.2) (#161)

ar-plan PR #8: build on the S2.1 shadow write (PR #160) with a pure
projection + dev-only assertion that verifies the event stream is complete.

- deriveMessages(log): Message[] — pure projection. Message-bearing events
  (user_message/assistant_message/tool_result) project to Message[];
  arrays flatten. compact = boundary (truncate prior). turn_start/turn_end/
  error skipped. seq-unordered input → ordered output.
- assertDualWrite(log, mutableMessages, turnId) — dev + FUSION_CODE_EVENT_SOURCING
  only; prod early-returns byte-identical. deepStrictEqual derived vs mutable;
  drift → logForDebugging + throw (fail-visible in dev). Called once per
  turn-end (not per push) to bound cost.
- QueryEngine: wire assertDualWrite before final yield (after capturing
  turnResultUuid — TDZ-safe: declare const first, then assert).

Still no read-path switch — prod reads mutableMessages as before; projection
is dev-assertion + unit-test only. Default-off + env gate preserves byte-
identical-off (PR #7 NOOP_RECORDER pattern continues).

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant