Skip to content

refactor(channel): run target-message on createMachine - #6036

Merged
synoet merged 12 commits into
mainfrom
synoet/target-message-state-machine-1221
Sep 19, 2026
Merged

synoet merged 12 commits into
mainfrom
synoet/target-message-state-machine-1221

Conversation

@synoet

@synoet synoet commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Why

createTargetMessageController already runs a deep-link lifecycle (navigate → load around → scroll → flash → release). That lifecycle lived in five independent | undefined fields, then in a five-phase Mealy table with two world-to-event effects. Both encodings made readiness a state and left a race: ChannelThread can ack a root-only target before a viewport-ready effect fires, and the five-phase table ignored that ack.

This rewrite writes the machine down as three states and tests it as values. Readiness is a derivation. The flash timer is a state-scoped lifetime. There are no createEffects.

What landed

@macro-inc/machine (packages/machine)

  • MachineDef<S, E, C> — one required entry per state; on returns { state, commands? } or undefined (ignored). No dispatch in on.
  • createMachine — signal + per-state scopes (dispose/remount on every transition, including same-t new payload) + execute commands + serialized dispatch with a cycle cap.
  • step / simulate — pure table helpers. No Solid, no timers.
  • nix-support/node_modules-hashes.json updated so the fixed-output .#js-node-modules tree includes the new workspace package.

Target-message machine (apps/web/src/features/channel/Channel/target-message.ts)

Lives next to the controller, same as other Channel helpers. Events and selectors are exhaustive ts-pattern matches.

  • States: idle | targeting | flashing, each carrying orthogonal loadAround.
  • Events: navigate, root-scroll-done, reply-scroll-done, flash-elapsed, pagination-restored, release, reset.
  • One command: restore-default-pagination, emitted on the ack → flashing arrow when an around-query is anchored. Cleared only by a successful restore handshake or reset.
  • Selectors encode the ChannelThread contract. A root-only target stays pending until ack (readiness does not clear it). A nested target's root row clears on readiness so the reply scroll is the only viewport movement.

Controller (create-target-message-controller.ts)

  • Public API unchanged, including #6080's hasPendingElementScroll.
  • Channel.tsx / ThreadList untouched.
  • Flash timer lives in the flashing scope (onCleanup cancels on navigate / release / reset / unmount).
  • Readiness is a function over navigation, didInitialScroll, and messageKeys — not a memo, because Channel.tsx constructs the controller before messageIndex exists.
  • goToMessage untracks messageKeys and ready.

Behavior change

Restore moves from "viewport ready, before the scroll" to "after the scroll is acknowledged." Between those moments Channel.tsx keeps sourcing from the around query — which it already did for the whole pre-ready window. loadAroundMessageId still selects useChannelMessagesQuery; the missing-message effect still watches it; threadPaginator still wraps that query.

An early root ack now does restore (the original skipped that path). By the time a row can ack it is mounted, so the around data is present.

Tests

  • Primitive: disposal order, self-transition remount, queued dispatch from scope/command/cleanup, cycle error, post-dispose no-op, matches — 15.
  • Machine: it.each tables per event, ChannelThread selector table, simulate sequences (root, nested with ignored root ack, rapid-nav, failed restore, mid-flight reset) — 48.
  • Container: timer lifetime, readiness derivation, cache restore on ack, construct-time TDZ — 9.

72 tests passed locally. Machine and container ran against solid-js's browser build. This Cloud VM cannot start the apps/web vitest workspace runner (EINVAL: scandir '//proc/.../net' during project init); CI's bunx vitest from apps/web includes the new machine project plus --project channel.

Out of scope

  • No changes to Channel.tsx, ThreadList, or clearStaleRestoredChannelData.
  • Email undo-send and ThreadList initial-scroll are the next createMachine candidates, not this PR.
Open in Web Open in Cursor 

cursoragent and others added 2 commits August 29, 2026 03:16
The highlight lifecycle is a state machine already. This commit writes
the table down as a pure reduce so later controller work can match it
instead of five optional store fields.

Co-authored-by: teo <synoet@users.noreply.github.com>
The public accessors stay selectors over MachineState. Methods dispatch
events. The container keeps one timer and the two world-to-event
effects. Channel.tsx is unchanged.

Co-authored-by: teo <synoet@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository: macro-inc/macro/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: cc038503-8a59-4535-a5aa-f5dd5273bc93

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: afd94e88-58c5-483e-bd67-c695b7a7dcd2

📥 Commits

Reviewing files that changed from the base of the PR and between e56ac85 and c9d7f2e.

📒 Files selected for processing (4)
  • apps/web/src/features/channel/Channel/create-target-message-controller.ts
  • apps/web/src/features/channel/Channel/tests/create-target-message-controller.test.ts
  • apps/web/src/features/channel/domain/target-message.test.ts
  • apps/web/src/features/channel/domain/target-message.ts

Included review availability: Your plan provides up to 8 included reviews per hour; 7 remain after this review.


📝 Summary

Summary by CodeRabbit

  • Bug Fixes

    • Improved navigation to targeted messages and replies in channels.
    • Added more reliable scrolling, highlighting, and flash behavior when entering or changing targets.
    • Improved pagination restoration after loading targeted messages.
    • Prevented stale or mismatched navigation events from affecting the current view.
  • Tests

    • Expanded coverage for target-message navigation, scrolling, highlighting, cleanup, and viewport readiness.

Walkthrough

Target-message navigation now uses a reducer-based state machine with loading, viewport-waiting, scrolling, and flashing states. The controller dispatches navigation and scroll events, executes flash and pagination commands, and reacts to message-key and initial-scroll signals. Tests cover reducer transitions, selectors, stale events, deduplication, flash cleanup, target loading, and viewport readiness.

Merge Risk: ⚪ Minimal · up to c9d7f

This refactor makes target-message navigation transitions explicit while preserving the existing controller interface and client-side scope. No actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title uses valid Conventional Commits format with the refactor(channel) prefix, describes the state-machine change, and is 69 characters long.
Description check ✅ Passed The description clearly explains the reducer state machine, controller refactor, scope, tradeoffs, testing, and verification limitations. It is directly related to the changeset.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Export makeTarget for the table helpers. Make initialState.targetLoaded
optional so the controller can seed without reading messageKeys.
root-scroll-done stays a no-op until control is scrolling.

Co-authored-by: teo <synoet@users.noreply.github.com>
@github-actions

github-actions Bot commented Aug 29, 2026 •

Copy link
Copy Markdown

Co-authored-by: teo <synoet@users.noreply.github.com>
@synoet
synoet marked this pull request as ready for review August 29, 2026 03:24
Comment thread apps/web/src/features/channel/domain/target-message.ts Outdated
Keep the target-message reduce table. Add hasPendingElementScroll from #6080 as a selector over pending root and reply scroll.

Co-authored-by: teo <synoet@users.noreply.github.com>
cursoragent and others added 2 commits September 4, 2026 16:49
Pure MachineDef table plus a Solid runner with per-state scopes,
serialized dispatch, and command execution. Domain code stays free of
owners; tests exercise the def as values via step/simulate.

Co-authored-by: teo <synoet@users.noreply.github.com>
Replace the five-phase Mealy table and two world-to-event effects with
idle/targeting/flashing, a readiness derivation, and one restore
command issued on scroll ack. Public controller API is unchanged.

Co-authored-by: teo <synoet@users.noreply.github.com>
@cursor cursor Bot changed the title refactor(channel): encode target-message navigation as a reduce table refactor(channel): run target-message on createMachine Sep 4, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 2 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cffa8a7. Configure here.

Comment thread apps/web/src/features/channel/domain/target-message.ts Outdated
Comment thread apps/web/src/features/channel/domain/target-message.ts Outdated
cursoragent and others added 5 commits September 4, 2026 17:11
Keep the machine next to its controller instead of a new domain/
folder. Match events and selectors exhaustively with ts-pattern.

Co-authored-by: teo <synoet@users.noreply.github.com>
Adding @macro-inc/machine changed the fixed-output Bun tree.
Linux hash verified locally; Darwin hash from the CI rebuild.

Co-authored-by: teo <synoet@users.noreply.github.com>
…-state-machine-1221

# Conflicts:
#	apps/web/src/features/channel/Channel/create-target-message-controller.ts
#	apps/web/src/features/channel/Channel/tests/create-target-message-controller.test.ts
#	nix-support/node_modules-hashes.json
@synoet
synoet merged commit fb31152 into main Sep 19, 2026
29 checks passed
@synoet
synoet deleted the synoet/target-message-state-machine-1221 branch September 19, 2026 22:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants