Repository navigation
feat(events): SessionEvent type + 旁路写 shadow write (S2.1, PR #7) - #160
Merged
Merged
Conversation
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]>
4 tasks done
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]>
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
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) —SessionEventTypeunion (8 types: user_message/assistant_message/tool_use/tool_result/compact/error/turn_start/turn_end),SessionEventinterface (seq/data/surfaceOp/sourceEventSeqs),SessionEventLogtype,serializeLoghelper. Pure types, no runtime side-effect.src/services/events/eventLog.ts(NEW) —appendEventimmutable pure function,SessionEventRecorderclass (per-session, monotonic seq counter, fail-open try/catch+log),isEventSourcingEnabledgate,NOOP_RECORDER(unconditional no-op subclass → byte-identical off even if env flips mid-session).src/QueryEngine.ts— per-sessioneventRecorderfield, init in ctor (on =new SessionEventRecorder(getSessionId()), off =NOOP_RECORDER). Shadow-record 8mutableMessages.pushpoints (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
deriveMessagesprojection, no assertions (those are PR feat: SDK control protocol for code.* and artifact_chat APIs #8/S2.2).FUSION_CODE_EVENT_SOURCING=1enables; off = recorder no-op, empty log, mutableMessages byte-identical.NOOP_RECORDERis an unconditional no-op subclass — earlier draft was a liveSessionEventRecorderthat would record if env flipped on mid-session, breaking the byte-identical-off claim. Subclass overridesrecord/recordCompactto unconditional no-op.Acceptance (ar-plan lines 318-322)
FUSION_CODE_EVENT_SOURCING=1→ eventLog has ordered events, seq monotonicGates
bun run typecheck— 0 errorsbun 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— passTest 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]