Skip to content

refactor(repl): extract setMessages routing (audit §1.1.1 slice #16) - #186

Merged
dahai80 merged 1 commit into
mainfrom
audit/repl-split-slice16-setmessages
Aug 28, 2026
Merged

dahai80 merged 1 commit into
mainfrom
audit/repl-split-slice16-setmessages

Conversation

@dahai80

@dahai80 dahai80 commented Aug 28, 2026

Copy link
Copy Markdown
Owner

Audit §1.1.1 god-module split — slice #16

Extract the setMessages state-sync wrapper from src/screens/REPL.tsx into a pure helper.

What

  • NEW src/utils/messagesSetter.ts — applySetMessages(action, setters): no React hooks, no JSX, no async. Side effects: mutate 3 refs (messagesRef/userInputBaselineRef/userMessagePendingRef) + call rawSetMessages. Byte-identical to the inlined useCallback body.
  • MOD src/screens/REPL.tsx — setMessages useCallback replaced with thin wrapper; deps array [] unchanged verbatim (all deps are refs + rawSetMessages captured in the setters object, no closures). Downstream reads same const name.
  • NEW src/__tests__/utils/messagesSetter.test.ts — 8 cases: direct-value vs functional action, shrink clamp, grow-with-human (clears pending), grow-without-human (bumps baseline), grow-without-pending (no-op), prepend slice path, prev-empty guard. Real isHumanTurn predicate exercised via minimal fake messages.

Body semantics (preserved verbatim)

  1. Compute next (functional action or direct value), sync messagesRef.current.
  2. If next shrank (compact/rewind/clear) → clamp userInputBaselineRef to 0 (placeholder length check can't go stale).
  3. If next grew while userMessagePending → inspect added slice: contains human turn → clear pending (user message landed); otherwise bump baseline to next.length (placeholder stays visible for async bridge/hook/scheduled messages landing ahead of the user message).

Gates (all pass)

Gate Result
bun run typecheck 0 errors
bun test src/__tests__/ 873 pass / 0 fail
bunx biome check (new files) clean (autofixed import-order + condition wrap)
bun run build:dev pass

LOC

REPL 6455 → 6434 (−21). Cumulative across 16 slices: −662.

byte-identical

  • useCallback deps array [] unchanged verbatim → closure identity preserved.
  • Helper body is the inlined body lifted verbatim → behavior identical.
  • isHumanTurn import kept in REPL (still used at L1492).

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

Extract the setMessages state-sync wrapper from REPL.tsx into a pure
helper src/utils/messagesSetter.ts (applySetMessages). No React hooks,
no JSX, no await — only side effects are mutating 3 refs
(messagesRef/userInputBaselineRef/userMessagePendingRef) and calling
rawSetMessages, byte-identical to the inlined useCallback body.

The body maintains a ref mirror of the messages array plus two
placeholder-tracking refs: (1) compute next (functional action or direct
value) and sync messagesRef.current; (2) if next shrank
(compact/rewind/clear) clamp baseline to 0 so the placeholder length
check can't go stale; (3) if next grew while userMessagePending, inspect
the added slice — if it contains a human turn, clear pending (user
message landed); otherwise bump baseline to next.length so the
placeholder stays visible for async bridge/hook/scheduled messages
landing ahead of the user message.

REPL keeps a thin useCallback wrapper whose deps array [] is unchanged
verbatim (all deps are refs + rawSetMessages captured in the setters
object, no closures). Downstream reads the same const name.

Tests (src/__tests__/utils/messagesSetter.test.ts, 8 cases) cover:
direct-value vs functional action, shrink clamp, grow-with-human
(clears pending), grow-without-human (bumps baseline), grow-without-
pending (no-op), prepend slice path, and the prev-empty guard. Real
isHumanTurn predicate (type==='user' && !isMeta && toolUseResult===undefined)
exercised via minimal fake messages.

Gates: typecheck 0 / 873 tests / biome clean / build:dev pass.
REPL 6455 -> 6434 (-21). Cumulative across 16 slices: -662.

Co-Authored-By: Claude Fable 5 <[email protected]>
@dahai80
dahai80 merged commit 333299d into main Aug 28, 2026
1 check passed
@dahai80
dahai80 deleted the audit/repl-split-slice16-setmessages branch August 28, 2026 14:18
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