Skip to content

fix: preserve visibility when completing duplicate updates - #180

Merged
abogartz merged 5 commits into
abogartz:mainfrom
WhollyMindless:fix/update-visibility-175
Oct 29, 2026
Merged

abogartz merged 5 commits into
abogartz:mainfrom
WhollyMindless:fix/update-visibility-175

Conversation

@WhollyMindless

@WhollyMindless WhollyMindless commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

memory_update could retire a target into a duplicate with the same repository key but a different checkout path. After an origin change, a checkout relying on path fallback then lost its only active visible row. A session move during the gate could also select a survivor outside the reader captured by the call and expose that survivor's metadata in the refusal.

Recheck the survivor against the captured reader before using it, and require matching effective paths as well as repository keys for private-row folds. Path mismatches leave both rows active with an explicit diagnostic and point to memory_merge_review for a deliberate choice of survivor. Global rows continue to ignore paths, and absent/empty paths are equivalent under the existing visibility rule.

Add ten regression/control cases covering real same-origin Git checkouts, recall after an origin change, session moves through the plugin listener, private path mismatches, and global/legacy compatibility. The refusal test also verifies that the suggested explicit merge retires only the chosen row. The change is confined to duplicate completion and its tests; gate-wide reader consistency is tracked separately in #183.

Closes #175.

Validation on Windows / Bun 1.4.4:

  • Full bun run gate: typecheck and lint pass; 702 unit tests pass, 12 existing skips; all 11 real-host end-to-end tests pass. Existing lint warnings remain; the new test file lints cleanly.
  • Confirmed six new regression cases fail before the fix.
  • Tests reject removal of either guard and removal of the global-path exception; all temporary mutations were restored.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Verdict: APPROVE

Both items in #175 are closed, each with the stronger of the two options the issue offered, and every validation claim in the PR body reproduces on a different OS and Bun version than the author used. I could not find a defect. The five items below are non-blocking (four nits + one follow-up worth a ticket); none of them changes the verdict.

Reviewed head: dfe2312 (only commit on the branch), true diff git diff origin/main...HEAD (merge-base 7e72e2f), 225 insertions / 2 deletions across src/index.ts and test/update-collapse-visibility.test.ts — matches gh pr view 180 --json additions,deletions,changedFiles.

Platform for everything below: macOS (darwin) / Bun 1.4.3 (bun --version), i.e. not the author's Windows / Bun 1.4.4.


1. Body claims, checked against the tree

Claim Result Evidence
Recheck the survivor against the captured reader before using it verified src/index.ts:760-762 adds isVisibleTo(m, reader) to the lookup; reader is captured at src/index.ts:554, before the update-order lock, and is never re-read. Matches the existing precedent at src/index.ts:1032-1035 (memory_merge_review), so the codebase now has one pattern instead of two.
Require matching effective paths as well as repository keys for private-row folds verified src/index.ts:767-768 (samePathVisibility) conjoined into the fold predicate at src/index.ts:784.
Path mismatch → both rows stay active with an explicit diagnostic verified src/index.ts:812 pushes the reason; markSuperseded is only reachable through the if at :778. Measured (probe A): verdict ... was NOT retired — ... (repository path visibility differs) ..., target.superseded_by undefined, 2 active rows unchanged.
Global rows keep ignoring paths verified and sound src/index.ts:767. The bypass cannot widen or narrow anything because :783 still requires requested.repo === (survivor.repo ?? "global"), so the bypass is only ever reached when both rows are global/unstamped — where isVisibleTo (src/memory.ts:717-722) makes paths irrelevant for both. Probe B, six combinations: private↔global (either direction) → refused; legacy↔global → folded with no reader losing the statement; global rows carrying foreign repoPath values → folded.
Absent/empty paths equivalent verified ?? "" on both sides at :768, pinned by the absent and empty private paths case (test/update-collapse-visibility.test.ts:191).
Six new regression cases fail before the fix verified, count exact Scratch worktree at merge-base 7e72e2f + the new test file: 4 pass / 6 fail — same-origin checkouts with different paths do not fold…, a fold preserves another checkout's path-fallback recall after its origin changes, a session move cannot retire a target into an entry-invisible survivor, a session move cannot expose invisible survivor metadata in a refusal, collapse visibility: absent target path, collapse visibility: absent survivor path. The 4 that pass at base are the controls.
Tests reject removal of either guard and of the global exception verified by mutation table in §3.
702 unit pass / 12 skip / 11 e2e pass, existing lint warnings, new file lints clean verified on darwin/Bun 1.4.3 bun run gate → merge gate: all green; unit 702 pass / 12 skip / 0 fail (714 tests, 70 files), e2e 11 pass / 0 fail. Base worktree: 692 / 12 / 0 (704 tests, 69 files) and 11 / 0 e2e — delta is exactly the new file, nothing moved elsewhere. bun x tsc --noEmit -p tsconfig.json → exit 0 on both. bun x biome lint → 400 warnings / 119 infos on both (141 files vs 140); bun x biome lint test/update-collapse-visibility.test.ts → 0 diagnostics; src/index.ts → 15 warnings on both.
Confined to duplicate completion and its tests verified 2 files changed; collapsedWith has exactly one consumer in the tree (src/index.ts:756), so the recheck covers the whole use, not one branch of it.

2. Does it close #175?

  • Item 1 — closed, and better than the issue asked. #175 offered a guard or a comment; this is the guard (the option the reporter said they'd take) at the exact lookup the issue quoted, plus a comment stating why. The race is real, not theoretical: loadScope in the gate resolves its reader from ctx.sessionID at gate time (src/gates.ts:379-390), while the update captured its reader at src/index.ts:554, so a session.moved in between can hand collapsedWith a row the caller cannot read. At base the failure output literally reads Duplicate: context/planning already stands active as 2026-10-29T20:37:55.966Z; … was retired in its favor for a survivor outside the captured reader.
  • Item 2 — closed by comparing repoPath, the issue's first option, and it is a live defect, not a latent one. #175 filed both items as "neither is a defect today"; the origin-change regression case (test/update-collapse-visibility.test.ts:144) shows the repo-key-implies-path assumption already breaks: after git remote set-url the surviving checkout reaches its row only via the path fallback in src/memory.ts:721, and a same-key fold from the other checkout silently strips its only visible active row. So this PR fixes more than the issue claimed needed fixing.
  • The guard is symmetric (it also refuses folds that would widen visibility, e.g. private target with no path → private survivor with a path, absent target path). That matches the issue's own direction rule ("folding a global row into a repo-private survivor narrows visibility, the reverse leaks") rather than contradicting it.

3. Mutation results (scratch worktree at dfe2312, restored after each — git diff --exit-code clean)

Mutation Caught by
M1 revert guard 1 (drop isVisibleTo from the lookup) 2 tests: both session-move cases
M2 revert guard 2 (drop samePathVisibility) 4 tests: same-origin checkouts…, …path-fallback recall after its origin changes, absent target path, absent survivor path
M3a remove the global exception 2 tests: explicit global rows, legacy global target
M3b invert the global exception 6 tests
M4 re-resolve the reader inside the lookup (readerFromSid(sid)) instead of the captured one 2 tests: both move cases — so the suite pins captured, not merely a reader
M5 drop the diagnostic string 4 tests
M6 alternative design that only refuses narrowing 1 test: absent target path
M7 compute the global bypass from survivor.repo instead of requested.repo nothing

M7 is the one case that cannot fail, and it is genuinely equivalent, not test theater: :783 already forces the two repo stamps to agree, so the only observable difference is which reasons appear in the diagnostic for a combination no test covers. No action needed; recorded for completeness.

4. Attacks the fix survived

  • "Both active forever" is not an unbounded duplicate stream. Probe A (post-refusal, probe store, same mechanism as the tests): an identical retry returns a byte-identical verdict, adds zero store rows, and one dedup_collapse evidence line per call — the line every collapsed write already emits. Nothing in the plugin calls memory_update on its own (grepped: src/index.ts registers it; no internal caller), and decay does not fold — findStaleOpen (src/portable.ts:363-371) only lists open rows past the cutoff and retires them. Also note the two surviving rows hold different prose (the target keeps the old text), so no reader sees the same statement twice. What the refusal actually leaves behind is the pre-call state, not a new contradiction.
  • Convergence for the case the fold exists for is intact. The #155 PARTIAL-retry fold has target and survivor in one checkout (same path), so it still folds; the positive controls (same private path, absent and empty private paths) and test/update-partial-failure.test.ts / update-supersede-truth.test.ts all stay green.
  • Test hygiene / ordering. beforeEach/afterEach (test/update-collapse-visibility.test.ts:31-57) save/restore both env vars, call resetState() + resetSessionScopes() + setRepoScope(null), clean up every loaded plugin instance, restore the evidence.record spy via restoreRecord, and guard the rmSync target. Ran green in every ordering I could construct: alone (10/10); nine neighbor combinations in both directions (update-targeting, update-provenance, update-partial-failure+update-supersede-truth, security-merge-ownership+security-session-isolation, repo-scope+repokey-path, export-visibility+convergence); full suite serial 702/12/0; --parallel=8 702/12/0; --randomize --seed=7 and --seed=101 (serial, non-isolated — the contamination-sensitive mode) 702/12/0; --rerun-each=2 with plugin, sessions, repo-scope-serve-host 38/38. The move cases also assert moved() === true, so the spyOn(evidence,"record") seam cannot silently degrade into a passing no-op.

5. Non-blocking nits

N1 — the refusal's advice is wrong for the new reason (src/index.ts:817-818). The message tells the caller to "restate them onto <survivorTs> with a memory_update, or memory_forget the row that should lose". Measured, in order: restating onto the survivor is a no-op (Duplicate ignored: context/planning, still 2 active rows) — it is the same call that was just refused; memory_forget is keyed by scope+type, not by row (probe deleted 3 rows). The remedy that actually retires exactly one row is memory_merge_review(loser=<target>, survivor=<survivorTs>), which succeeded in the same fixture and is visible to the same reader. The wording predates this PR, but this PR is what routes path mismatches into it, and for path mismatch it is circular. One-line fix: name memory_merge_review.

N2 — the invariant is automatic-fold-only, and nothing says so. The exact end state #175 describes is still reachable on request from the other checkout: probe A ran memory_merge_review(loser=<A's row>, survivor=<B's row>) from checkout B (both rows readable there, so the #131 authorization contract is satisfied) and checkout A then recalls No memories found — A's only active visible row is gone. I do not think that should be blocked (an explicit decision by a session that can read both rows is the documented contract, README:100), but the new comment at src/index.ts:764-766 states the path rule as if it protected the store. One sentence there, plus a ticket recording where the boundary is intentional, would save the next reader the archaeology.

N3 — the comparison is raw strings, not normalizeDirPath. src/repokey.ts:67-77 normalizes (realpath, separator unification, case-fold on win32/darwin) when hashing keys, and isVisibleTo compares stored repoPath raw — but for the fold a near-miss is now permanent. Probe D on darwin: target stamped with the resolved path, survivor with a symlink alias of the same directory → normalizeDirPath equal, raw compare unequal → was NOT retired … (repository path visibility differs), 2 rows still active for that reader, fold can never complete. Suggest normalizeDirPath(target.repoPath ?? "") === normalizeDirPath(survivor.repoPath ?? ""), or a comment saying raw compare is deliberate. (Inherited weakness, not invented here — but the fold turns a visibility near-miss into a permanent no-fold, and 8.3/long-path aliases make this likelier on the author's Windows than on macOS.)

N4 — the same move race still leaks through a sibling message in the same call (pre-existing, same class as what the body claims to close). The gate keeps resolving its own reader from the sid (src/gates.ts:379-390), so a session.moved between src/index.ts:554 and GATE-3 lets the superseded_overlap hint (src/gates.ts:687-696) describe a row the captured reader cannot see. Reproduced with the PR's own move mechanism: Write blocked at GATE (superseded_overlap). A live decision row 2026-10-29T20:51:34.912Z in arch competes with this write (overlap 0.73). … use memory_update with query="2026-10-29T20:51:34.912Z" — for a row invisible to the caller (the rival's issue field stayed hidden; the id and scope did not). Not a regression and arguably outside #175's wording, so not blocking; the clean fix is threading the captured Reader into GateContext (src/index.ts:290) instead of re-resolving it in the gate.

N5 — nit: the two new comments don't cite #175 while nearly every other load-bearing invariant in this file cites its issue (#154, #155, #127, #152). The test file does (test/update-collapse-visibility.test.ts:1).

6. What I could not verify

  • Windows / Bun 1.4.4. Every number above is darwin/Bun 1.4.3, including the full gate. The hosted test-windows job is green at this head but I did not re-run it, and N3 is the kind of thing that behaves differently there.
  • A live host race. The move is delivered through the plugin's real session.moved listener in the harness (as in the PR's own tests), driven by a spy on evidence.record; I did not observe a genuine mid-tool-call move on a running opencode server.
  • Whether "refuse across checkouts" is the product-preferred answer versus a directional rule (fold only when the survivor's visibility ⊇ the target's, which would still fold no-path → has-path). I verified only that what is implemented is the safe direction, is consistent with #175's own narrowing/leaking rule, and is pinned by tests (M6).
  • Anything about other PRs or issues; I stayed inside #180 and #175 as instructed.

7. Repro

# red on base, exactly 6 of 10
git worktree add --detach /tmp/base-180 7e72e2f
cp test/update-collapse-visibility.test.ts /tmp/base-180/test/
cd /tmp/base-180 && bun test ./test/update-collapse-visibility.test.ts   # 4 pass / 6 fail

# green at head, plus gate
cd <pr-180 worktree>
bun test ./test/update-collapse-visibility.test.ts    # 10 pass / 0 fail
bun test ./test/                                      # 702 pass / 12 skip / 0 fail
bun test ./test/ --parallel=8 ; bun test ./test/ --randomize --seed=101
bun x tsc --noEmit -p tsconfig.json                   # exit 0
bun x biome lint                                      # 400 warnings / 119 infos (same as base)
bun run gate                                          # all green; e2e 11 pass / 0 fail

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Addressed the small follow-ups in 3502299:

The full gate passes again on Windows / Bun 1.4.4: 702 unit tests pass, 12 existing skips, and 11 real-host e2e tests pass; typecheck and lint pass with the existing warning counts unchanged. The PR description now includes the explicit-merge remedy and links the broader follow-up.

@abogartz
abogartz merged commit 364c408 into abogartz:main Oct 29, 2026
4 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.14.21 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

P3: the collapse-completion trusts an unstated visibility invariant; repo compared without repoPath

2 participants