Repository navigation
fix(scripts): repair rows whose cited paths moved - #445
Conversation
The gate checks path references on writes only, so a row that was correct when it was saved goes stale when a later refactor moves the file. The src/ moves invalidated refs in 33 active rows on the maintainer's store. ref-fixer repoints a path only when git records a rename whose chain ends at a file that exists today and whose basename is unchanged, so the tokens a query matches on survive. Renames that also renamed the file, and paths with no rename in history, are reported rather than rewritten: a row about a deliberately retired artifact is accurate history. Apply goes through the same gated update path as symptom-fixer: batch lock, supersedesTs, snapshot first, memory_undo stays the recovery path.
WhollyMindless
left a comment
There was a problem hiding this comment.
I ran the fixer at da8c43a with two disposable Git/store fixtures. In both cases --apply reported one repair and superseded the original row, but the successor cited the wrong file.
- An edit also rewrites an already-valid citation. A row cited both
capture/runtime.tsanddocs/capture/runtime.ts. The plan listed only the first citation. The saved successor changed the second todocs/src/capture/runtime.tsas well. That file existed in the fixture, so the gate accepted it. Applying edits to the matched path spans in the original text would keep the plan and the write in sync. This fixture also had multiple prefix matches forcapture/runtime.ts, yet the planner pickedsrc/without checking uniqueness. - A reused source path picks the oldest rename. I moved
src/lock.tstolegacy/lock.ts, added a different file atsrc/lock.ts, then moved that one tocurrent/lock.ts. Both destinations still existed. A row describing the second file was rewritten tolegacy/lock.ts. Git emits the newer rename first and the map overwrites it with the older one. Reporting reused paths as ambiguous would protect rows from either generation.
I’d address these before applying the fixer to more stored rows. Both fixtures would make useful regression tests against the persisted successor.
An adversarial review found two blocking defects in the first version. The rewrite was content.split(from).join(to). It hit a ref nested inside a longer one, so a row citing both gate/gate.ts and src/gate/gate.ts came out as src/src/gate/gate.ts, and it rewrote absolute paths that checkablePathRefs deliberately skips - damage the gate can never refuse, because it never sees those. checkablePathRefSpans now returns each checked ref with the offsets it occupies, checkablePathRefs delegates to it, and the fixer edits only those spans: one source of truth, so the script cannot drift from what the gate validates. The docs also claimed a stricter rule than the code had. Rule 2 repoints paths with no rename in history, so the text now states both rules and why each is narrow: R90+ renames only (this repo's moves are R090-R100; the R058/R072 outliers are rewrites), and a loose citation requires that the path never existed in history and has exactly one tracked match, or the gate's hallucination refusal becomes a verified ref. Also: git exit codes checked, a zero-row scan says so instead of reading clean, cycles and unresolved chains return null, the dry run predicts every gate refusal before writing, refusals exit 1, --limit is validated, and the PARTIAL message keeps the duplicate warning. 11 new tests pin the rewrite and the resolver.
A path renamed away, recreated at the same path and renamed away again has two generations of rows citing one string, describing two different files. Picking the newest rename is a guess with the confidence of a repair, so the chain walk now stops on any hop whose source was renamed more than once and the path is reported instead. Two regression tests from the review fixtures: a row citing both a loose capture/runtime.ts and a valid docs/capture/runtime.ts must have only the loose one rewritten, and a reused source path must resolve to nothing.
WhollyMindless
left a comment
There was a problem hiding this comment.
Rechecked 72dea23. All 11 ref-fixer tests passed, and the CLI now leaves my ambiguous suffix fixture untouched. The span-based rewrite resolves the nested-citation finding.
One case still writes the wrong successor. In the same disposable Git history, the first src/lock.ts moved to legacy/lock.ts. A different file later occupied src/lock.ts and moved to current/lock.ts. Both moves are R100 and both destinations still exist. This time I applied the fixer to a row describing the first component. It reported one repair, persisted current/lock.ts and superseded the original row. Choosing the newest rename fixes the second component's citation but misattributes the first one's.
Could the planner retain both destinations and leave a reused source path for human review when the row's file generation cannot be established? A CLI regression with one row for each generation would cover both sides.
|
Both reproduced, both fixed. Your review ran against 1. An edit also rewrote an already-valid citation. Fixed in Uniqueness is checked now too. Rule 2 requires exactly one tracked file whose path ends with the citation ( 2. A reused source path picked the oldest rename. The oldest-wins part was real: git emits newest-first and the map overwrote the good entry with the old one. Also changed from a separate adversarial pass, since you asked for the plan and the write to stay in sync: the dry run now prints Status: |
WhollyMindless
left a comment
There was a problem hiding this comment.
The two-rename fixture now passes at 8c63a26. I ran --apply with a row for each generation of src/lock.ts. It reported both and wrote zero successors. All 15 ref-fixer tests passed.
There is a related retirement case that still writes a wrong successor. In a disposable Git repo I moved the first component from src/lock.ts to legacy/lock.ts (R100), added a different component at src/lock.ts, then deleted that second component. For a row describing the second component, --apply reported one repair, superseded the original, and persisted “The second component uses legacy/lock.ts for its locking behavior.” That file belongs to the first component.
The history query filters to renames, so it sees the R100 and misses the later creation and deletion. Could the planner leave this path for human review when a later generation occupied it? The persisted row above would make a regression test for the documented retirement rule.
A rename source that history touches again after its newest rename held a second generation: moved away, recreated, then deleted. No second rename record appears, so counting rename records reports a clean single move and a row describing the second occupant is repointed onto the first occupant's new path. Reproduced in a disposable repo: src/lock.ts moved to legacy/lock.ts, a different component added at src/lock.ts, then deleted. --apply rewrote the row to "the second component uses legacy/lock.ts", which is the first component's file. Detection is one pass over the unfiltered name-status log: for every rename source, is the newest commit touching it later than its newest rename? If so the path is reported for a human, alongside the shapes already refused. The check lived in main() and no test reached it, which is how the first version of this rule survived review. indexRenames now takes the two log texts and returns the index, so the rename floor, the multi-rename count and the reuse pass are all unit-testable without a git binary. Verified against the maintainer's store: 5 repointable rows and 18 dead paths, byte-identical before and after, and no path in this history is reused. bun test ./test/ 1525 pass / 21 skip / 0 fail, test:e2e 35 pass.
|
Reproduced, fixed at Reproduction. Disposable repo, four commits: So yes — it claimed a clean repair and would have persisted a row attributing the second component to the first component's file. The rule. Your suggestion is the right one and it is what shipped: refuse and report when a later generation occupied the path. Detection is not a rename count — the recreate-then-delete shape leaves no second rename record, which is exactly why Why the first version survived you. The reuse check lived inside Your point about the persisted row becoming the regression test — taken, though as a unit fixture rather than a live row: the store is yours and Not a behavior change on your store. Same script, before and after, against the real history:
|
WhollyMindless
left a comment
There was a problem hiding this comment.
I reran the original rename → recreate → delete fixture at c76a431. With --apply the row stayed byte-identical and there was no successor. All 20 focused tests passed.
Reversing which generation was deleted exposes a remaining wrong write. In a disposable repo I deleted the first src/lock.ts, committed a different component at the same path, then moved only that second file to current/lock.ts (R100). I applied the fixer to a row about the retired first component. It reported one repair, superseded that row, and persisted “The retired first component uses current/lock.ts for its locking behavior.”
Here the rename really is the newest touch, so the new check passes even though the row could describe either generation. Could the reuse check also consider whether the source path had multiple lifetimes before its final rename? A paired regression with the deletion on either side of the rename would exercise this without a live store.
The mirror of the previous fixture still wrote a wrong row. Delete the first src/lock.ts, commit a different component at the same path, move only that second file: the rename is now the newest touch of the path, so "was it touched after its rename" answers no, and a row about the retired first occupant was superseded and rewritten onto the second occupant's path. Both shapes are one path with two creations, so the rule is now a creation count: an add, or a rename or copy landing on the path, more than once. That subsumes both rules it replaces -- a path cannot be renamed, deleted or written again without being created again first -- so the rename count and the newest-touch pass are gone rather than stacked into a third check. The full-history log is now a checked call. An empty log reads as "no path was ever created twice", which would disable the rule silently and repoint anyway. Tests pair the two shapes, deletion on either side of the rename, plus a path that arrived by rename and was recreated at it -- no `A` for its first lifetime, so a rule counting adds alone would read one creation. Fixtures are written the way git prints the unfiltered log, a move as a delete plus an add. Verified: both disposable repos now refuse and leave the row byte-identical under --apply, and the maintainer's store is unchanged at 5 repointable rows and 18 dead paths, with no path in this history created twice. bun test ./test/ 1527 pass / 21 skip / 0 fail, test:e2e 35 pass.
|
Reproduced at Reproduction. Disposable repo, four commits: Why your suggestion was not enough on its own. "Also consider whether the source path had multiple lifetimes before its final rename" is correct, and it is the case I missed — but implementing it as a second question next to the first gives two rules that each cover half the shapes. Laid out, the shapes are one property: So the rule is now a single creation count: an Your fixture as a paired regression. Both shapes are in One more hole, found while rewriting. The full-history log was an unchecked Verified on both fixtures. Each now refuses, and under Not a behavior change on your store.
|
WhollyMindless
left a comment
There was a problem hiding this comment.
Both source-reuse fixtures now leave the stored row byte-identical under --apply, including the delete-before-rename case. All 22 focused tests passed at 4d95fc8.
The terminal destination can also have multiple lifetimes. I moved src/lock.ts to legacy/lock.ts (R100), deleted the moved file, then committed an unrelated component at legacy/lock.ts. A row describing the original component still cited src/lock.ts. The fixer reported one repair, superseded the row, and persisted “The original component uses legacy/lock.ts for its locking behavior.” The current file at that path has never contained the original component.
The full history already records both creations of the destination. The reuse set only considers keys from hopScore, so a terminal path that was never itself renamed cannot enter it. Could the ambiguity check cover the destination as well? The existing chain walk visits that path before accepting it.
The creation count only covered rename SOURCES, because `reused` was built from the keys of `hopScore`. A destination is never one of those, so the third shape review found wrote a wrong row: move src/lock.ts to legacy/lock.ts (R100), delete the moved file, commit an unrelated component at legacy/lock.ts. The chain ends on a path the original component never occupied, and the row was superseded onto it. The set is now every path with more than one creation, and the check applies to whatever the chain walks -- source, intermediate, or destination -- which the walk already visited before accepting. `ambiguousHop` reports WHICH path in the chain has two lifetimes: telling a human that src/lock.ts was reused, when legacy/lock.ts is the path with two creations, sends them to the wrong file. chainEnd now shares one hop walk with it rather than keeping its own copy, so the two cannot disagree about what the chain contains. Verified on a third disposable repo: refuses, names legacy/lock.ts, and the row stays byte-identical under --apply. The two earlier fixtures still refuse and still name src/lock.ts. The maintainer's store is unchanged at 5 repointable rows and 21 dead paths, with no reuse section either way. bun test ./test/ 1531 pass / 21 skip / 0 fail, test:e2e 35 pass.
|
Reproduced and fixed at Reproduction. Four commits: Why your diagnosis was exact. The report names the right file. Telling a human "
Tests. Four added: All three fixtures, and your store. Each refuses and names the path it should — the two earlier ones still name Worth naming the pattern across the three rounds. Each finding was a rule scoped to the thing I was looking at instead of the thing the row touches: first the rename count, then the newest touch, then rename sources only. The property that actually matters is "can this path be cited by rows written against two different files", and it belongs to every path in the chain. A fourth round would have to break that, and I do not see how — but the last three rounds also said that.
|
WhollyMindless
left a comment
There was a problem hiding this comment.
I reran --apply at 882618c against four disposable Git/store fixtures. In both source-reuse histories and the destination-reuse history, the original row stayed byte-identical and the script wrote zero successors. The report named legacy/lock.ts when the destination was the ambiguous path. As a positive control, the same R100 move without reuse produced one successor citing legacy/lock.ts. All 26 focused tests passed. This resolves my wrong-write findings.
|
🎉 This PR is included in version 0.33.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Problem
The write gate refuses a new settled claim that cites a path the repository does not have (
ref_path_missing). That check runs on writes only, so a row that was correct when it was saved goes stale silently the moment a later refactor moves the file. Nothing notices, and recall keeps quoting a path the reader cannot open.This is self-inflicted and measurable. The
src/moves — domain structure, the telemetry merge,command/ensure.ts→init/— invalidated refs in rows already on disk.Measured on the maintainer's store
bun scripts/ref-fixer.tsagainst this repository, 2026-11-19:--applyThe refusals are the interesting output and the script does not paper over them: 4 rows cite a second dead path the rules will not touch, and 1 is a
learningrow where a content change brings the symptom-first rule into play.Two rules, each narrow for a stated reason
R90whose chain ends at a file that exists today and whose basename is unchanged. The floor is measured, not guessed: this repository's renames cluster atR090–R100(the domain restructure, where the only content change is the import lines inside the moved file) with outliers atR058,R072,R080,R086,R088— files git scored as renames after much of their content was rewritten. Same basename is the safety property:src/lock.ts→src/store/lock.tsstill says "lock", so the tokens a query matches on survive.gate/gate.ts→src/gate/gate.ts). Both halves are load-bearing: without the history check a path the gate once refused as hallucinated gets handed a verified ref, and without the single-candidate check the script picks a directory on the row's behalf.Everything else is reported only. A row about a deliberately retired artifact —
poc-409/, a retired bench runner — is accurate history: the file is gone on purpose and the row is the record that it existed.One consequence worth knowing: the retirement convention used in
docs/pre-release-ritual.md(recover it with git log -- bench/runners/<name>.ts) cannot be written into a memory row, because the gate reads the path inside it as a live reference. Filed as #447.Review findings that changed this PR
An adversarial review found two blocking defects in the first version. Both are fixed and both are now pinned by
test/ref-fixer.test.ts(11 tests).The rewrite was a blind substring replace.
content.split(from).join(to)also hit a ref nested inside a longer one: a row citing bothgate/gate.tsandsrc/gate/gate.tscame out assrc/src/gate/gate.ts. Worse, it rewrote absolute paths thatcheckablePathRefsdeliberately skips — so the gate could never refuse the damage, because it never sees those. The fix is span-based:checkablePathRefSpansinsrc/gate/resolver.tsreturns each checked ref with the offsets it occupies,checkablePathRefsnow delegates to it, and the fixer edits only those spans. One source of truth, so the script cannot drift from what the gate validates.The docs described a stricter rule than the code had. Rule 2 repointed paths with no rename in history, which the text claimed never happened. The docs now state both rules and why each is narrow.
Also from the review, all fixed: the rename map ignored git's similarity score (now
R90+, printed per row); the chain resolver returned a wrong path on a cycle or an unresolved intermediate (now null);gitexit codes were ignored, so a run outside a repository reported a clean scan (now exits 2); a zero-row scan looked like success (now says NOTHING SCANNED and names--repo); the "exactly one candidate" the header promised was unimplemented (now a tracked-surface suffix match); rule 2 could bless a retired path (now requires the path never existed in history); the dry run never showed the rewritten text, so a bad rewrite was invisible until--apply(it now printswill be REFUSED: still cites …for every row the gate will stop); a run with refusals exited 0 (now 1);--limit abcsilently printed nothing (now rejected); and the PARTIAL message dropped symptom-fixer's warning that a re-run reports the duplicate (restored).The PR body's earlier claim that comment-lint does not cover
scripts/was wrong — it diffs*.tswith no path filter. The0 added comment line(s)I reported came from running with a base equal to HEAD. WithCOMMENT_LINT_BASE=origin/mainit checks this script's comments.A reused path produced a confident wrong write. Two review rounds, one shape each. A path renamed away, recreated with different content, then deleted leaves no second rename record, so counting renames reads it as a clean single move and a row about the second occupant was rewritten onto the first occupant's new path. Its mirror — delete the first occupant, add a different component at the same path, then move only that second file — has the rename as the path's newest touch, so a "was it touched after its rename" check passes and the row about the retired occupant was rewritten onto the second one's path.
Both are one path with two creations, so the rule is a creation count: an
A, or a rename or copy landing on the path, more than once. That subsumes both rules it replaces — a path cannot be renamed, deleted or written again without being created again first — so they were deleted rather than stacked into a third check.indexRenames(renameLog, fullLog)is now a pure function over the two log texts: the check had lived insidemain()behind twogitcalls, unreachable from a test, which is how the first version of the rule survived review at all. Tests pair the two shapes and add a path that arrived by rename and was later recreated at it, where noAexists for the first lifetime. The full-history log is now a checked call: an empty log reads as "no path was ever created twice" and would disable the rule silently.A reused destination is not a safe target either. A third round found that the creation count was scoped to rename sources —
reusedwas built from the keys ofhopScore, and a destination is never one of those. Movesrc/lock.tstolegacy/lock.ts, delete the moved file, commit something unrelated atlegacy/lock.ts: the chain ends on a path the original component never occupied, and a row about the original component was superseded onto it. The set is now every path with more than one creation, and the check applies to whatever the chain walks.ambiguousHopreports WHICH path in the chain has two lifetimes, because telling a human thatsrc/lock.tswas reused whenlegacy/lock.tsis the ambiguous one sends them to the wrong file.chainEndshares one hop walk with it rather than keeping its own copy — a duplicated walk is how the destination got omitted.Across the three rounds the same mistake recurs: a rule scoped to the thing being looked at (the rename count, then the newest touch, then rename sources) instead of the property the row needs ("can this path be cited by rows written against two different files", which belongs to every path in the chain).
This history has 39 rename sources, 34 followed at
R90+, and 0 created more than once, so the stricter rule changes nothing on the maintainer's store — both fixes came from the reviewer's fixtures.Apply path
Identical to
scripts/symptom-fixer.ts, deliberately: one row at a time under the store's batch lock, through the real write pipeline withsupersedesTsset — thememory_updatecontract. The successor inherits the target's repository stamp, so the script forges no identity. The old row is superseded and never edited,markSupersededsnapshots the day file first, andmemory_undostays the recovery path. A row whose content changed between scan and apply is skipped rather than clobbered.Refs resolve against the repository the row describes (
repoPath), not the store: that is the only tree where the new path exists.Scope, stated because silence reads as a clean report: rows are selected by repository key, defaulting to the current directory. Rows stamped for another repository, and rows with no stamp at all, are never scanned — the dry run prints how many it skipped.
Verification
bun run typecheckclean.bun x biome formatclean.COMMENT_LINT_BASE=origin/main bun run lint:commentsclean, 48 added comment lines checked.bun test ./test/— 1537 pass / 21 skip / 0 fail, including 11 new tests for the rewrite and the chain resolver.bun run test:e2e— 35 pass / 0 fail. Run this time becausesrc/gate/resolver.tschanged.Not included
The 18 dead paths that need judgement are listed by the script and left alone. The 5 rows the gate refuses stay stale until a human rewords them;
--applyis a no-op for each.