Repository navigation
feat(events): deriveMessages projection + dual-write dev assertion (S2.2) - #161
Merged
Merged
Conversation
…2.2) 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 PR #8 (S2.2): build on the S2.1 shadow write (PR #160) with a pure projection + dev-only dual-write assertion. Closes the event-sourcing M3 half (#7 shadow write + #8 derive/assert).
Changes
src/services/events/deriveMessages.ts(NEW) — two exports:deriveMessages(log): Message[]— pure projection. Message-bearing events (user_message/assistant_message/tool_result) project toMessage[]; arrays flatten.compact= boundary (truncate prior accumulation).turn_start/turn_end/errorskipped.seq-unordered input → ordered output.assertDualWrite(log, mutableMessages, turnId)— dev +FUSION_CODE_EVENT_SOURCINGonly; prod early-returns byte-identical.deepStrictEqualderived vs mutable; drift →logForDebugging+ throw (fail-visible in dev). Called once per turn-end (not per push) to bound cost.src/QueryEngine.ts— wireassertDualWritebefore final yield. TDZ-safe:const turnResultUuid = randomUUID()declared first, then assert, then yield (turn_end record reuses the uuid).deriveMessages.test.ts(7): empty log, single user_message, array spread, ordered multi-type, compact boundary truncation, skip non-message events, seq-unordered → ordered.assertDualWrite.test.ts(6): prod no-op on drift, dev+env-off no-op, dev+env-on match passes, drift throws fail-visible, missing push-point (mutable longer) throws, empty boundary.Design notes
mutableMessages; projection is dev-assertion + unit-test only. Default-off + env gate preserves byte-identical-off (PR 请求: CLAUDE.md 解析库抽离为独立模块 #7NOOP_RECORDERunconditional-no-op pattern continues).turnResultUuidis already captured there forturn_end), not on every push point — bounds cost to O(1)/turn.assertDualWrite.ts, but both are small, share imports, andassertDualWritecallsderiveMessages; one file avoids an import cycle and duplicate imports.Gates
bun run typecheck— 0 errorsbun test src/__tests__/— 638 pass, 0 fail (16 new)bunx biome check(changed files) — cleanbun run build:dev— pass; byte-identical default-off (env gate off → early return, no behavior change)Test plan
FUSION_CODE_EVENT_SOURCING=1→ every turn-end assertionderived === mutableMessagesCo-Authored-By: Claude Fable 5 [email protected]