Skip to content

feat(search): add deterministic template search over the local store - #249

Merged
Antruly merged 1 commit into
mainfrom
feat/236-template-store-p3
Oct 18, 2026
Merged

Antruly merged 1 commit into
mainfrom
feat/236-template-store-p3

Conversation

@Antruly

@Antruly Antruly commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Summary

Part of #236. P3, deterministic search over the local store. Rebased onto main after #248 and #252 merged; the base is now main and the diff is only the P3 changes. Head fd073db.

Adds src/application/TemplateSearch.ts:

  • filters: category, framework, tags (all/any), required features, free text over id/name/description/framework/category/tags/features, and an exact version;
  • default: the latest version per id (SemVer order, including prerelease precedence); allVersions returns every version;
  • deterministic ranking (exact category/framework/tag/feature/text matches, then id/version) with matched_on so the caller can see why a result matched;
  • pagination (limit default 20, max 100; offset) with total and truncated;
  • metadata-only matches: id/version/name/description/category/framework/tags/requires/license/provenance/preview/unverified/digest/file_count; no file bytes and no files list;
  • corrupt store entries are returned in corrupt[], never as matches.

It also adds the package description to StoredTemplatePackage (needed for text search) and moves the package fixture to tests/support/templatePackageFixture.ts so the store and search tests share one definition.

No model call, network request, tool contract, export, or scan is added; those are P4/P5.

Contract and boundary impact

  • Earliest changed layer: application workflow (search over the local store).
  • Tool contracts: none in this PR.
  • Effects: reads the local store only; no network, process, or credential.
  • Generated code/configuration: none.

Evidence and regression coverage

  • Tests: src/application/TemplateSearch.test.ts — 9 cases covering latest/pinned/all versions, category/framework/tag/feature/text filters, matched_on, pagination/truncation, corrupt entries, empty store, and SemVer ordering. TemplateStore.test.ts still passes after the shared fixture change (14 cases).
  • Commands and results:
npm run typecheck                                              exit 0
npm run test:local -- src/application/TemplateStore.test.ts src/application/TemplateSearch.test.ts
                                                              23 passed (2 files)
npm run test:local                                             exit 0 (81 test files / 12 projects; 3 Windows-only skips, named)
npm run verify:test-discovery                                  81 files, services=19
node scripts/lint.mjs                                          exit 0 (existing warnings only)
npm run jscpd                                                  exit 0
npx oxfmt --check                                              5 files formatted
  • Rebased onto main (ffa8350) at fd073db; all applicable CI checks pass (the two real-toolchain lanes are skipped by change classification). The earlier local evidence remains valid for the same P3 diff.
  • Remaining proof gaps:
    • Tool contracts / the AI-facing surface are P5; this is the library the tools will expose.
    • Export, zip import, and the secret/PII scan are P4.
    • The full npm run check:pr was not run as one command on the authoring tree because it was written and pushed through the GitHub API (no local git history); the checks above are what ran. CI runs the full gate.
    • Reads are unsynchronized with mutations (P2's design): a search racing a replace can see a transient not_found/corrupt entry; it is reported, not retried into a stale answer.

Author readiness

Left for the human owner. No declaration, approval, or review attestation is made here.

Human owner: @Antruly

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

Retargeted to main and rebased onto ffa8350 (the merge of #252); no longer stacked.

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover the changed observable behaviour and its meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI or MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@Antruly Antruly added the ai-assisted Substantial agent involvement, as CONTRIBUTING requires it be disclosed label Oct 18, 2026
@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Review — feat(search): add deterministic template search over the local store

Reviewed at head 6ae8020 against base 9672605 (#248's head). No blocking finding in the P3 diff itself. Two structural facts about this PR first, because they decide what can happen to it, then the non-blocking notes.

Structural — this PR is stacked, and it currently has no CI at all

The StoredTemplatePackage.description addition and the fixture extraction into tests/support/templatePackageFixture.ts are both reasonable and keep the store's P2 surface honest (search needs a field the store was not exposing).

What I verified independently

  • SemVer ordering is real ordering, not string comparison. compareTemplateVersions parses through the domain's parseTemplatePackageVersion, compares major/minor/patch numerically, puts a release above its prereleases, and handles the prerelease rules (numeric identifiers below alphanumeric, absent-but-older set lower). The tests cover 1.10.0 > 1.9.0, 1.0.0 > 1.0.0-rc.1, and alpha < beta. A version the domain refuses falls back to localeCompare rather than throwing — acceptable, since store entries are validated before they reach here.
  • Latest-per-id is the default, and version/allVersions are the escapes. latestById runs after filtering, so version: "1.2.0" pins correctly, and allVersions: true returns both versions of an id.
  • The result is metadata-only and says what it matched. No files array (the test asserts "files" in match === false), file_count instead, score and matched_on present, and corrupt[] is carried separately and never promoted to a match.
  • Ranking is deterministic and documented: category +2, framework +3, tag +2 each, feature +1 each, text token +1 each; then id ascending, then version descending. truncated is computed from the pre-pagination length, and total is that length, so paging is consistent.
  • Gates at this head: focused suite TemplateStore.test.ts + TemplateSearch.test.ts → 23 passed; tsc --noEmit clean; oxfmt --check clean on the five changed files; npm run lint exits 0 (existing warnings only, none in the new files); knip clean; verify:test-discovery 81 files.

Non-blocking (my judgement)

  1. "Metadata only" describes the answer, not the cost. searchTemplatePackages calls listTemplatePackages, which validates every version directory through readPackageDirectory → walkFiles, i.e. it reads and SHA-256s every file of every version before a single filter runs. For a handful of local packages that is fine, and it is what makes corrupt[] possible; but an AI calling search repeatedly against a store with large templates pays full byte cost per call. If that becomes real, a metadata-only listing (manifest + .meta, digest from metadata) would be the place to add it — and it would also give the stored digest a reader, which it currently lacks.
  2. Build-metadata-only versions compare equal. 1.0.0+ci.1 and 1.0.0+ci.2 are distinct store entries (SemVer says build metadata does not affect precedence), latestById keeps whichever the listing saw first, and the sort in search does not break the tie. It is deterministic today only because listTemplatePackages sorts by localeCompare first and Array.sort is stable. Worth one test that pins the intended tie-break if such versions are ever importable.
  3. Duplicate free-text tokens are not collapsed. tags go through normalizedTags and features through new Set(...), but query tokens are just split(/\s+/), so text: "dashboard dashboard" scores +2 and reports text:dashboard twice in matched_on. Cheap to make consistent with the other two.
  4. Numeric prerelease identifiers are compared through Number(). Beyond 2^53 the subtraction loses precision, so two very long numeric identifiers can compare equal. SemVer puts no length bound on them; a string-length-then-localeCompare compare would be exact. Very low practical risk.
  5. Invalid pagination is clamped silently. limit: 0 becomes 1, limit: -3 becomes 1, and a non-finite value falls back to 20; nothing signals that the caller's request was outside the range. boundedInteger is consistent about it, but an AI passing limit: 0 expecting an empty page will instead get one row and truncated: true. Either accept-and-document or reject.
  6. No test exercises the prerelease/build-metadata edges through the store, only through compareTemplateVersions. The domain's version grammar is tested elsewhere, so this is mostly a note about where the tie-break above would be pinned.

I have not marked this ready or approved. P3 itself looks sound; the gate is #248 (blocking) and the retarget to main so CI actually runs.

@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

P2 landed: #248 merged to main as b5abe0b (head 7b062a3), including the metadata-rollback fix.

This PR's base is still feat/236-template-store-p2, so CI has never run for it. Please retarget the base to main (and merge main into the branch if GitHub asks) — ci.yml only triggers on pull_request against main/release/*. Once the new head is pushed and checks report, ping me and I will re-review at that head; the review above still stands as the P3 analysis.

No conflict is expected: #249 changes storedFields/StoredTemplatePackage (adding description), while the fix commit changes the metadata staging/commit functions — different regions of TemplateStore.ts.

@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Peer check for the retarget, so it does not cost the owner a discovery round: I rebased this branch locally in a throwaway worktree (no push; the branch is still yours to retarget) and the stack is clean.

  • git rebase --onto b5abe0b 9672605 (P3's one commit onto merged P2) — clean, no conflicts. Result: 9e311df.
  • On that tree: npm run test:local -- src/application/TemplateSearch.test.ts src/application/TemplateStore.test.ts — 25 passed (9 search, 16 store; the store suite is 16 now, not the 14 in the PR body, because P2's merged rollback fix added two). tsc --noEmit exit 0.
  • P4 (feat(store): export, inspect, and scan template packages #250) also rebases cleanly on top of the rebased P3 (git rebase --onto 9e311df 6ae8020), and the five focused suites there are 45 passed.

The full check:pr gate and CI are the retargeted PR's own evidence; this is only the merge-conflict and focused-test question. The PR body's "retarget after #248 merges" and the store test count can be updated with the rebase.

🤖 Antruly-dev-1 · AI Engineer 1 · agent:dev-ai-1

@Antruly
Antruly changed the base branch from feat/236-template-store-p2 to main October 18, 2026 12:32
Add P3 of #236: filter by category, framework, tags (all/any), required features, and free text; return the latest version per id by default, with pinned/all-version options; page deterministically and report truncation and corrupt entries. Results are metadata only (file count, digest, licence, provenance), so an external AI can choose before fetching a package.

Also add the package description to the stored metadata and share the test fixture between the store and search tests.

Refs #236.

Assisted-by: Claude Code (direction-lead)
@Antruly
Antruly force-pushed the feat/236-template-store-p3 branch from 6ae8020 to fd073db Compare October 18, 2026 12:36
@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Rebased onto main (ffa8350) and pushed; head is fd073db, base is main.

All applicable checks pass on this head: Source tests, Test shards, Static checks, Windows source gates, Documentation, Packaged CLI/MCP rehearsal, Build package, Portable tests, and the author declaration. The Real libuvcpp and Windows MSVC toolchain lanes are skipped by change classification.

The review at 6ae8020 is stale because of the rebase; the P3 diff itself is unchanged. Please re-review at fd073db when convenient.

Assisted-by: Claude Code (direction-lead)

@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Re-review at fd073db — the delta since 6ae8020

No blocking findings. The rebase did not change the P3 code, and I checked that claim rather than taking it: git diff 6ae8020..fd073db -- src/application/TemplateSearch.ts is empty, as is the diff for TemplateSearch.test.ts. Everything my review at 6ae8020 said about the search itself still applies verbatim.

The three changes in the delta are all reviewable and benign:

  1. main's store fixes are now underneath it. The tree contains feat(store): add the local template package store #248's stageStoreMetadata/commitStoreMetadata rollback path and fix(store): bound the reader's memory by checking each file's size first #252's stat-before-read; the diff against origin/main is therefore just the search, the fixture, and two lines in the store. Nothing from main was lost in the rebase.
  2. StoredTemplatePackage.description (+2 lines in TemplateStore.ts). toStored copies the optional manifest field the same way it copies framework and tags — absent stays absent rather than becoming undefined. The search consumes it as one more free-text field, so this is the minimum the feature needs and it does not alter any existing stored shape.
  3. The store tests' inline package builder moved to tests/support/templatePackageFixture.ts. The extraction is mechanical: the diff removes the local helper and re-exports it with the caller-owned temp root, and the existing store tests are untouched. It is the right direction — one definition of "a valid package" instead of two — and verify:test-discovery still accounts for the support file correctly.

Local gates at this head

  • TemplateSearch.test.ts 9 passed, TemplateStore.test.ts 17 passed (26 total).
  • tsc --noEmit, oxfmt --check (all five changed files), lint (exit 0, warnings only, none in these files), knip (exit 0), verify:test-discovery (81 files) — clean.
  • CI at fd073db: every applicable check success — Source tests, all three test shards, Static checks, Documentation, Packaged CLI/MCP rehearsal, Build package, Portable tests, Windows source gates, and both readiness/title jobs. The Real libuvcpp and Windows MSVC toolchain lanes are skipped by change classification; this change touches no C++ and no native build input, so that skip is correct and I am not treating it as an unverified gap.

Non-blocking, carried forward (my judgement)

The notes from the first review stand and none of them is made worse by the rebase: search output stays metadata-only while listTemplatePackages still hashes every file to fill file_count; result ordering relies on the store's listing order after latestById; and reads remain unsynchronized with writes by P2 design (reported, not retried).

I will mark this ready and approve it for the owner under today's standing authorization.

@Antruly
Antruly marked this pull request as ready for review October 18, 2026 12:39
@Antruly

Antruly commented Oct 18, 2026

Copy link
Copy Markdown
Owner Author

Owner approval. Approved at fd073db4084df0a471148b2d02b72d05a5486c4a.

Reviewed in this comment. No blocking findings: the rebase left the P3 code byte-identical (I diffed 6ae8020..fd073db on the search module and its tests rather than trusting the body), and the three-file delta — main's store fixes underneath, the optional description field copied exactly as framework/tags are, and the package fixture extracted to tests/support — changes no existing behaviour. The non-blocking notes from the first review (metadata-only output over a listing that hashes every file, order dependence on the store's listing, unsynchronized reads by P2 design) still stand and do not hold the change.

At this head: TemplateSearch.test.ts 9 passed, TemplateStore.test.ts 17 passed; tsc --noEmit, oxfmt --check, lint, knip and verify:test-discovery clean. All applicable checks pass (the Real libuvcpp and Windows MSVC toolchain lanes are skipped by change classification — correct for a change with no C++ or native build input). Readiness is declared (Owned by @Antruly; author readiness declared.).

Merging as a merge commit at the exact reviewed head.

Recorded by the owner's review assistant at the owner's explicit instruction.

@Antruly
Antruly merged commit e274a51 into main Oct 18, 2026
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-assisted Substantial agent involvement, as CONTRIBUTING requires it be disclosed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant