Skip to content

perf(repo): resolve reference lookups via one shared index - #67

Merged
Ian-stetsenko merged 2 commits into
Ian-stetsenko:mainfrom
TommyC81:perf/indexed-reference-resolution
Oct 29, 2026
Merged

Ian-stetsenko merged 2 commits into
Ian-stetsenko:mainfrom
TommyC81:perf/indexed-reference-resolution

Conversation

@TommyC81

@TommyC81 TommyC81 commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

PR 2: perf(repo): resolve reference lookups via one shared index

Addresses the trace half of #65 (the log half is #66). Stacked on #PR1 (branch based on perf/single-pass-files-changed; land after it (or rebase onto main if #66 lands first)).

What

AtomRepository.getLoreIdIndex() builds a Lore-id to atom map from a single git log --all pass - lazily, at most once per invocation, newest-first with first-wins. trace BFS edges, resolveFollowLinks, and validateReferenceExistence look ids up against the index instead of running one full-history git log --grep per referenced id. trace keeps its root lookup on findByLoreId so unknown ids fail fast, and atoms without references never build the index.

Why

Reference-heavy traversals re-grepped the full history once per visited edge. Tracing one well-connected atom in the production repo from the issue executed 631 grep spawns and took 148s; after this change the same trace runs in 0.86s. Lookup semantics are unchanged: --all scope, newest match wins, same as the grep-based findByLoreId (relevant to #57's discussion).

Tests

  • npm run build and npm run typecheck clean.
  • vitest: 428 passed (7 new: index construction, newest-wins, build-once, follow-links single-pass, trace command root/edge/not-found/no-refs, validator via index); the 2 failures are pre-existing Windows tmp-path tests, identical on clean main.
  • Live: full-depth lore trace on the 840-atom repo, 148s before, 0.86s after.

Lore-id: (from commit 6e2aafb on the branch)
Confidence: high
Scope-risk: narrow
Tested: see above
Assisted-by: AI coding agent (diagnosis and implementation; human-reviewed)

Every lore query fetched changed files with one git diff-tree spawn per lore commit (batched 20-wide). On Windows each spawn costs roughly 100ms, so a single lore log on a repo with 840 atoms spent over half a minute in child process creation alone. GitClient.log now runs one extra git log --name-only pass over the same range, parses hash-to-files records separated by 0x1E, and attaches filesChanged to every RawCommit; AtomRepository reads it straight off the raw commit. The -L path cannot use --format, so its already-parsed hashes go through one --no-walk call instead. Behavior parity: merge commits list no files either way; root commits now list theirs, which plain diff-tree without --root never showed. Measured on the 840-atom repo: lore log full corpus 36s before, 0.7s after; small repos are unchanged.

Lore-id: 43057933
Constraint: git format strings stay owned by GitClient; AtomRepository keeps consuming RawCommit only
Constraint: merge commits list no files in both old and new paths; root commits now list files (diff-tree --root gap fixed)
Constraint: getFilesChanged stays on IGitClient for single-commit callers; AtomRepository no longer calls it
Tested: npm run build and npm run typecheck clean
Tested: vitest 421 passed; 2 failures are pre-existing Windows tmp-path tests, identical on clean main
Tested: live on an 840-atom production repo: full-corpus lore log 36s before, 0.7s after; instrumented exec count is exactly 2 git calls
Confidence: high
Scope-risk: narrow
Reversibility: clean
trace BFS, resolveFollowLinks, and reference validation each ran one full-history git log --grep per referenced Lore-id. On a dense corpus that is hundreds of greps per traversal: tracing one id in a repo with 840 atoms and 2741 cross-references executed 631 grep spawns and took 148 seconds. AtomRepository.getLoreIdIndex() now builds a Lore-id to atom map from a single git log --all pass - lazily, once per invocation, newest-first with first-wins so the index returns the same atom a grep-based findByLoreId would. trace keeps its root lookup on findByLoreId so unknown ids still fail fast, and atoms without references never build the index at all. Measured on the same repo: full-depth trace 148s before, 0.86s after.

Lore-id: b8c4c63c
Constraint: lookup semantics unchanged: --all scope, newest match wins, matching findByLoreId behavior
Constraint: trace root lookup stays on findByLoreId for fast not-found errors
Constraint: index builds lazily and at most once per CLI invocation; unreferenced atoms never pay for it
Tested: npm run build and npm run typecheck clean
Tested: vitest 428 passed; 2 failures are pre-existing Windows tmp-path tests, identical on clean main; 7 new tests cover the index, follow-links, trace command, and validator
Tested: live on an 840-atom repo: full-depth lore trace 148s before, 0.86s after
Related: 43057933
Confidence: high
Scope-risk: narrow
Reversibility: clean
@Ian-stetsenko
Ian-stetsenko merged commit b066fd1 into Ian-stetsenko:main Oct 29, 2026
@Ian-stetsenko

Copy link
Copy Markdown
Owner

Merged on top of #66, thanks. Full-depth lore trace on this repo now runs in ~0.3s locally.

One follow-up for later: getLoreIdIndex() runs log(['--all']) unfiltered, so on a repository with millions of non-Lore commits it still pays the full scan. Once a Lore-id --grep lands (see the split proposed on #55) it should be applied there too.

thoroc pushed a commit to pantheon-org/lore-protocol that referenced this pull request Nov 9, 2026
Implements option 1 from issue Ian-stetsenko#69: `lore validate --from <ref>` now validates the revision range `<ref>..HEAD`, giving validate an explicit range flag while `--since` stays a date everywhere else. `--since <ref>` keeps its ref semantics on validate but prints a deprecation warning to stderr naming removal in 0.7 and pointing to `--from`.

Interaction with the positional [range]: passing both a positional range and `--from` now throws an actionable LoreError (exit 1) instead of silently dropping one input, matching the conflicting-input convention from the Ian-stetsenko#48 fix. All other precedence is unchanged: positional still beats a lone `--since`, and `--from` beats `--since` when both are given (the deprecated alias loses), with the warning firing whenever `--since` is present. The deprecation warning goes to stderr so `--json` stdout parsing is unaffected. No date filtering was added to validate.

Review hardening: the deprecation warning now routes through IOutputFormatter (console.error(formatError(0, [severity: warning])), matching the hooks.ts precedent) and fires whenever --since is present, including hook mode (--commit-msg-file); the --from/--since range branches collapse to one resolved ref (from ?? since). Docs follow the contributor convention verified from merge history (Ian-stetsenko#55, Ian-stetsenko#56, Ian-stetsenko#59, Ian-stetsenko#66, Ian-stetsenko#67): README flag references and PROJECT_ARCHITECTURE updated in-PR; CHANGELOG.md entries were removed because the changelog is maintainer-owned, written at release time with contributor credits.

Lore-id: bb1ac354
Constraint: --since on validate keeps its ref semantics during deprecation; removal in 0.7
Constraint: No new date filtering on validate (out of scope per issue Ian-stetsenko#69)
Constraint: Deprecation warning must go to stderr so --json stdout stays parseable
Constraint: Deprecation warning must route through IOutputFormatter (console.error(formatError(0, ...))); raw console.error strings bypass formatter selection (CLAUDE.md: all output goes through the formatter)
Constraint: CHANGELOG.md is maintainer-owned release notes; contributors update README and PROJECT_ARCHITECTURE in-PR (verified from merge history of Ian-stetsenko#55/Ian-stetsenko#56/Ian-stetsenko#59/Ian-stetsenko#66/Ian-stetsenko#67)
Rejected: Option 2 from the issue: keep --since as a ref on validate only and document the inconsistency
Rejected: Let --from silently win over the positional range: silently discarded input is the bug class fixed in Ian-stetsenko#48
Rejected: Turning --since on validate into a date now: would break existing users mid-deprecation
Directive: Remove validate --since entirely in 0.7 once the deprecation window closes
Tested: npm run build
Tested: npm run typecheck
Tested: npm test: 477 passing (468 baseline + 9 new)
Tested: Unit tests: --from routes to <ref>..HEAD, equals the positional equivalent, conflict rejects with LoreError, --since warns on stderr (via formatter) in normal AND hook mode, precedence guards for positional/--last/default
Tested: CLI smoke: --from HEAD~1, --since HEAD~1 warning on stderr, positional + --from conflict exits 1
Confidence: high
Scope-risk: narrow
Reversibility: clean
thoroc pushed a commit to pantheon-org/lore-protocol that referenced this pull request Nov 9, 2026
Implements the maintainer's accepted proposal for issue Ian-stetsenko#57. Previously findByLoreId ran a single git log --all grep, so the newest commit by date across every ref won: a future-dated commit on a side branch sharing a Lore-id shadowed the legitimate current-branch atom in lore trace, while lore doctor (current-branch scan only) stayed silent.

(a) findByLoreId now greps the HEAD-reachable history first and consults --all only on a miss, so a side branch can never shadow the current branch. The Ian-stetsenko#67 Lore-id index follows the same rule: one HEAD pass seeds every id, then an --all pass fills only missing ids, keeping trace BFS, follow-link resolution, and reference validation consistent with the grep path. A failing HEAD pass (unborn branch on a fresh repo) is treated as a miss so fresh-repo behavior is unchanged.

(b) lore doctor --all opts the Lore-id uniqueness check into scanning every ref (findAll allRefs: --all) and reports duplicates found across refs as 'Lore-id uniqueness (all refs)'. Default output is unchanged; off by default because backup branches make cross-ref duplicates noisy.

(c) not implemented: no collision guard in lore commit, per the proposal.

Review hardening: the duplicated maxCommits bound in doctor is extracted to the DOCTOR_SCAN_LIMIT constant. Docs follow the contributor convention verified from merge history (Ian-stetsenko#55, Ian-stetsenko#56, Ian-stetsenko#59, Ian-stetsenko#66, Ian-stetsenko#67): README doctor row and PROJECT_ARCHITECTURE lookup-semantics notes added in-PR; CHANGELOG.md left untouched because the changelog is maintainer-owned, written at release time with contributor credits.

Lore-id: d181468e
Constraint: findByLoreId stays a targeted git log --grep per id; no full-index build for single lookups
Constraint: getLoreIdIndex keeps its single ReadonlyMap API so trace BFS, validator, and follow-link consumers stay unchanged
Constraint: doctor default output must stay byte-equivalent to before; --all only widens the uniqueness scan
Constraint: fresh-repo (unborn HEAD) behavior preserved: lookups return null/empty, not a git error
Constraint: CHANGELOG.md is maintainer-owned release notes; contributors update README and PROJECT_ARCHITECTURE in-PR (verified from merge history of Ian-stetsenko#55/Ian-stetsenko#56/Ian-stetsenko#59/Ian-stetsenko#66/Ian-stetsenko#67)
Rejected: Keeping the single --all pass with newest-first ordering: that is the reported bug (side-branch shadowing)
Rejected: Collision guard in lore commit: maintainer explicitly deferred it as (c)
Rejected: Replacing the index API with a two-tier lazy lookup method: ripples through trace and validator consumers for no behavioral gain
Rejected: Swallowing --all pass failures: real git errors must stay loud; only the HEAD pass falls through
Directive: If a commit-time collision guard is ever added, keep it a warning; the maintainer deliberately deferred it
Directive: Keep lookup HEAD-first when extending to new consumers; do not reintroduce bare --all-first resolution
Tested: npm run build
Tested: npm run typecheck
Tested: npm test: 484 passing (467 baseline + 17 new)
Tested: integration repro of issue Ian-stetsenko#57: trace resolves the HEAD-reachable atom over a 2030-dated poison commit, doctor default silent, doctor --all reports the duplicate
Tested: unit: HEAD-hit skips --all, miss falls back to --all, unborn-HEAD HEAD-pass failure falls back, index HEAD-preference and off-branch fill
Tested: integration: fresh repo (no commits) returns null and empty index
Tested: review-fix re-run: full suite 484 green after DOCTOR_SCAN_LIMIT extraction
Not-tested: performance on very large corpora with many refs (two log passes per index build)
Not-tested: repositories with a detached HEAD
Confidence: high
Scope-risk: moderate
Reversibility: clean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants