Skip to content

feat(events): deriveMessages projection + dual-write dev assertion (S2.2) - #161

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

dahai80 merged 1 commit into
mainfrom
feat/event-sourcing-derive-assert

Conversation

@dahai80

@dahai80 dahai80 commented Aug 27, 2026

Copy link
Copy Markdown
Owner

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 to Message[]; arrays flatten. compact = boundary (truncate prior accumulation). 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.
  • src/QueryEngine.ts — wire assertDualWrite before final yield. TDZ-safe: const turnResultUuid = randomUUID() declared first, then assert, then yield (turn_end record reuses the uuid).
  • Tests (NEW, 16 tests):
    • 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

  • No read-path switch. Prod still reads mutableMessages; projection is dev-assertion + unit-test only. Default-off + env gate preserves byte-identical-off (PR 请求: CLAUDE.md 解析库抽离为独立模块 #7 NOOP_RECORDER unconditional-no-op pattern continues).
  • Per-turn, not per-push. Assertion runs once at turn-end (the turnResultUuid is already captured there for turn_end), not on every push point — bounds cost to O(1)/turn.
  • Combined file — spec listed a separate assertDualWrite.ts, but both are small, share imports, and assertDualWrite calls deriveMessages; one file avoids an import cycle and duplicate imports.

Gates

  • bun run typecheck — 0 errors
  • bun test src/__tests__/ — 638 pass, 0 fail (16 new)
  • bunx biome check (changed files) — clean
  • bun run build:dev — pass; byte-identical default-off (env gate off → early return, no behavior change)

Test plan

  • dev + FUSION_CODE_EVENT_SOURCING=1 → every turn-end assertion derived === mutableMessages
  • missing push-point → assertion catches drift (fail-visible dev)
  • prod (non-dev) + env unset → byte-identical (early return)
  • 4 gates pass

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

…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]>
@dahai80
dahai80 merged commit 019dde6 into main Aug 27, 2026
1 check passed
@dahai80
dahai80 deleted the feat/event-sourcing-derive-assert branch August 27, 2026 08:50
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