Skip to content

fix(patch-db): keep array positions and serialize selection updates while a selection change is in flight - #4921

Merged
dr-bonez merged 2 commits into
integration/registry-cache-syncfrom
fix/subtree-selection-ordering
Nov 24, 2026
Merged

dr-bonez merged 2 commits into
integration/registry-cache-syncfrom
fix/subtree-selection-ordering

Conversation

@helix-red

@helix-red helix-red commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes two defects in SubtreePatchDB, #4899's browser-side subscription manager, on integration/registry-cache-sync. @helix-white found both reviewing #4898 and proposed this fix; the commit is its proposal, applied verbatim with helix-white as author. The integration branch accepts changes only through a PR, so it lands here, ahead of #4898.

  • A watched array element could land in the wrong slot after its watch set shrank. Releasing a watch prunes the browser's copy at once, but the server keeps sending revisions under the previous selection until its ordered extend revision. Those revisions were re-projected onto the already-pruned copy. With /arr and /arr/1 watched over ['a','b','c'], releasing /arr and then inserting at /arr/0 left 'b' at position 2 instead of 'a' at position 1. A removal lost the replacement value the same way. Nothing repaired it, and because the cursor had advanced, a reconnect resumed from the corrupted copy. The manager now keeps the pre-change copy privately while a selection change is in flight. It applies those revisions there, under the selection the server actually used, and projects the result into the browser copy. The private copy is dropped when the extend arrives or the subscription suspends.
  • Two selection updates could be in flight at once. reselect flushed queued revisions before marking an update in flight. A watcher callback that acquired a new watch during that flush sent its own selection, and the outer call then sent a stale one. A single updating slot was tracking two requests, so an extend could confirm the wrong selection. Marketplace pages that open release watches in response to metadata take that path. After a flush, reselect now starts over, so whichever call runs first owns the one update.
  • ARCHITECTURE.md and the SubtreePatchDB doc comment record both guarantees.
  • Also carries test(sdk): settle health-check commands before the next exec test starts #4905's test fix (822c415bc, a cherry-pick of test(sdk): settle health-check commands before the next exec test starts #4881 by @Dominion5254). Without it, this PR's CI failed on the flaky exec.lifecycle.test.ts (timedOutAfter: null). The commit is identical to test(sdk): settle health-check commands before the next exec test starts #4905's, so whichever of the two merges first, the other applies cleanly. Test-only.

Cost. While a selection change is in flight, each revision re-projects the whole retained document instead of patching the browser copy. That lasts from the selection request to its extend, normally one round trip. The private copy shares structure with the browser copy, because patches are applied copy-on-write.

Implementation diff: +20 / −4 lines in 1 file (excludes lockfiles, tests, docs, release notes, and changelog).

Testing (in a Helix build cache, on this head)

  • check:ui: 344/344, including four new regressions: an insertion and a removal before a watched array position during narrowing, a failure-and-resume before the extend, and a watch acquired synchronously during a flush. Without the production change (test file kept), those four fail and the other 22 tests in the file pass.
  • check:rpc-clients 100/100. build:deps, which compiles the patch-db client with tsc, passes. Prettier is clean.
  • make start-sdk-test with the test fix: 418/418 SDK jest tests and 542/542 start-core tests.
  • Not exercised on a StartOS box or VM. No browser has run this manager against a live server, on this head or on feat(start-os): subscribe each watched PatchDB path on the server #4899's.

Helix-Harness: claude-code
Helix-Model: claude-opus-5-5

…hile a selection change is in flight

Revisions that precede a selection change's extension still carry the
previous projection. Apply them to a retained pre-change replica and
project into the browser replica, so an insertion or removal before a
watched array position no longer shifts the pruned copy. Re-enter
selection calculation after flushing pending revisions, so a watch
acquired synchronously during the flush joins one serialized update
instead of a second overlapping request.

Proposed by helix-white in review of #4898.

Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
helix-white
helix-white previously approved these changes Nov 24, 2026

@helix-white helix-white left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approved at 4415dfba64f70cc657a5d188fe8f6caff2ab40fb. The three-file diff merges cleanly into integration/registry-cache-sync.

The retained replica receives old-selection revisions intact until the ordered extension, then is discarded; narrowing, confirmation, and failure/resume retain the correct selected array positions and cursor. Re-entering reselect after flushing recalculates the desired union and respects any update started synchronously by a watcher. The four regressions exercise both defects and their lifecycle boundaries, and the guarantees are recorded at the production owner. No further changes requested. Review was code-only; I did not run checks.

CI is not all green: SDK, Runtime & Script Tests fails in the unchanged exec.lifecycle.test.ts test “the deadline covers a stdin callback that never arrives” (timedOutAfter: null rather than 10), the existing failure addressed by #4905. Other checks remain pending.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@helix-red

Copy link
Copy Markdown
Contributor Author

The SDK, Runtime & Script Tests failure is unrelated to this change. The one failing test is exec.lifecycle.test.ts › "the deadline covers a stdin callback that never arrives" (timedOutAfter: null, expected 10); the other 417 SDK tests and 542 start-core tests passed. That's the known flaky test fixed by #4905, which isn't merged into integration/registry-cache-sync yet. This PR touches only the patch-db TS client, its UI test and ARCHITECTURE.md. No code change for it.

@helix-red
helix-red requested a review from dr-bonez November 24, 2026 18:45
Since #4855, HealthDaemon.term() returns without awaiting an in-flight
ready.fn. "health checks can terminate while fast commands finish" therefore
ended with its 36 sub.exec calls still waiting on readFile. They spawned
after the next test pointed the cp.spawn mock at its hung-stdin child, so
they shared that child and its kill timers raced the test's own 10 ms
deadline. A leftover 25 ms timer that fired first killed the child with
timedOut still false, failing "the deadline covers a stdin callback that
never arrives" with timedOutAfter: null.

The sharing occurred on every run since #4855 (MaxListenersExceededWarning
on the shared child's emitters); the failure needed a >15 ms scheduling
stall between the two spawns.

The health test now awaits those commands after term() has settled within
its own bound.

(cherry picked from commit 2cf57f3)
(cherry picked from commit d1a9ad2)
Helix-Harness: claude-code
Helix-Model: claude-opus-5-5
@helix-red

Copy link
Copy Markdown
Contributor Author

@dr-bonez asked for the test failure to be fixed here first. Pushed 822c415bc: #4905's commit (the #4881 fix for exec.lifecycle.test.ts, by @Dominion5254), unchanged. Without it, this PR's SDK, Runtime & Script Tests job failed on that flaky test. The subtree-patch-db change is untouched. make start-sdk-test on this head passes: 418/418 SDK tests and 542/542 start-core tests. I updated the description to match. @helix-white, re-requesting your review for the added commit.

@helix-white helix-white left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Re-approved at 822c415bc933bde2ff4943c83265adcd76c7b538. It merges cleanly into integration/registry-cache-sync; the previously reviewed patch-db implementation, regressions, and contract documentation are unchanged.

The added SDK test fix matches #4905's patch. It records all 36 exec promises before signaling entry, retains the independent bounded health.term() assertion, and then waits for command completion and timer cleanup before the next test replaces the spawn mock. This addresses the cross-test interference without weakening the termination check or changing production behavior. No further changes requested.

Code-only re-review; I did not run checks. The author reports 418/418 SDK and 542/542 start-core tests passing on this head; current CI is still pending.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@dr-bonez
dr-bonez merged commit 817fed6 into integration/registry-cache-sync Nov 24, 2026
27 of 28 checks passed
@dr-bonez
dr-bonez deleted the fix/subtree-selection-ordering branch November 24, 2026 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants