Skip to content

refactor(store): resolve a batch of links with one lookup per name - #215

Merged
bketelsen merged 5 commits into
mainfrom
groom/resolve-links-once
Nov 12, 2026
Merged

bketelsen merged 5 commits into
mainfrom
groom/resolve-links-once

Conversation

@frostyard-machines

@frostyard-machines frostyard-machines commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Link resolution in internal/store ran the same SQL again and again inside 1 transaction. ResolveLink asks markdown.Resolve for the files with a name 1 to 4 times per link (filePathsByName), then reads the resolved path (FileByPath). Two loops call it once per link:

  • indexLinks (index.go), for every link of the note it indexes;
  • resolveFileLinks (files.go), when a file is added, restored, moved or deleted. All the links it re-resolves name 1 of only 2 names (the old name or the new name), so each backlink repeated the same lookups.

Neither loop changes the files table: the first only inserts links rows, the second only updates links.dest_id. So this PR adds an unexported linkResolver that caches each name lookup and each path lookup for the length of 1 loop, and both loops use 1 resolver. ResolveLink keeps its signature, doc and error texts, and resolves through a new, empty resolver. An error is never cached.

No change in behavior. Each link resolves to the same file, from the same queries in the same transaction; only repeats of a query whose answer cannot change are left out.

Left out on purpose: a cache that spans more than 1 loop (a scan's batch inserts files between notes, so it would not be safe), and the other findings of this pass, which are issues (#208 to #213).

Risk tier

Risk tier: 1 — a refactor of the index's link resolution with no change in behavior; no path in .github/risk-paths.json.

Verification

  • make check green (host), on the base b9c6c90 and on the head. The existing tests of resolution (TestResolveLink, TestPutFileResolvesWaitingLinks, TestMoveFile, TestDeleteFile, TestNoteLinksResolveLater in internal/store, and the vault and engine tests over moves and backlinks) are unchanged and pass on both.
  • make vault-it green on 370d5ab (TestIKillNotesd, 302 s); the later commits only add the context check below and a test.
  • Blocking review point fixed: the cross-review at 370d5ab found that a cache hit skipped the context check that the query did, so a cancelled resolution failed later and with another error text. A cache hit now returns the error the query gives once ctx is done. Guard: TestACachedLinkLookupStillSeesACancelledContext (internal/store/resolve_test.go, a file of its own, since files_test.go is T-0014's contract test), and 1 line in .memory/corrections.jsonl.
  • Revert test of that guard: in a scratch worktree at 828b04f with internal/store/files.go from 370d5ab, go test ./internal/store/ -run TestACachedLinkLookupStillSeesACancelledContext -count=1 fails: resolve "Inbox.md" -> "Plan" from the cache after cancel returned no error, want resolve link Plan: find files named plan.md: context canceled.
  • The refactor itself: revert test exempt (no new behavior; the proof is the same tests green before and after).

The first commit adds 2 benchmarks only (internal/store/resolve_bench_test.go). go test ./internal/store -run '^$' -bench 'ResolveFileLinks|IndexNoteRepeatedLinks' -benchtime 20x -count 2 -benchmem:

Benchmark Base (9491008, benchmarks on the old code) Head
ResolveFileLinks (move a note that 300 notes link to 3 times, and back) 697–815 ms/op, 256,520 allocs/op 25–36 ms/op, 52,290 allocs/op
IndexNoteRepeatedLinks (re-index 1 note with 50 links to 5 targets) 10.1 ms/op, 8,320 allocs/op 1.6 ms/op, 2,461 allocs/op

Coder model: deepseek/deepseek-flash.

@github-actions github-actions Bot added the level:L3 Skim label Nov 12, 2026
@frostyard-machines

Copy link
Copy Markdown
Contributor Author

Cross-review record (reviewer openai/gpt-6.1-sol, coder deepseek/deepseek-flash; base b9c6c90)

  • Initial review at 370d5ab: Not ready. 1 blocking point: a cache hit skipped the context check that the query did, so a cancelled resolution failed at a later statement with another error text. Fixed in 317f00a (a cache hit returns the error the query gives once ctx is done), with the guard TestACachedLinkLookupStillSeesACancelledContext and 1 line in .memory/corrections.jsonl. 828b04f moves the guard to internal/store/resolve_test.go, because files_test.go is T-0014's contract test.
  • Re-review 1 at 828b04f: the cancellation case is fixed. New point: with a cancelled transaction context and a live query context, a cached lookup succeeds where a fresh query fails with sql.ErrTxDone.
  • Re-review 2 at 828b04f: Ready, Blocking: none. That point was withdrawn on 2 facts. First, on the base, database/sql rolls back such a transaction asynchronously, so a lookup that runs before the rollback also succeeds there, and the next statement fails the same way. Second, every production caller (internal/vault: apply.go, folders.go, move.go, scan.go, write.go) uses the transaction's own context, or WithoutCancel outside with the cancellable context inside, the case re-review 1 covered.
  • Revert test: with internal/store/files.go from 370d5ab, the guard fails (... from the cache after cancel returned no error, want resolve link Plan: find files named plan.md: context canceled).

@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 12, 2026
bketelsen
bketelsen previously approved these changes Nov 12, 2026
@frostyard-machines frostyard-machines removed the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 12, 2026
@frostyard-machines frostyard-machines added the ready-for-human Reviewed, CI green: ready for Brian to review and merge label Nov 12, 2026
@bketelsen
bketelsen merged commit 4ceb7fe into main Nov 12, 2026
11 checks passed
@bketelsen
bketelsen deleted the groom/resolve-links-once branch November 12, 2026 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

level:L3 Skim ready-for-human Reviewed, CI green: ready for Brian to review and merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants