Repository navigation
Description
Summary
search_terms documents that its subject_kind lookup "is called at most once per subject", but the lookup is re-run once per subject per SubjectKind term — a search with two kind terms calls it twice for every subject, three kind terms three times, and so on.
Evidence (main 6bbb065)
-
pkgs/id/src/search.rs:85-88— the doc comment onsearch_termssays: "subject_kindresolves a subject to the kind of the event it names, for [Term::SubjectKind]; it is called at most once per subject, and a subject it cannot resolve (None) never matches a kind term." -
pkgs/id/src/search.rs:108-145— the implementation builds the matched-subject set in afor term in termsloop, and theTerm::SubjectKind(kind)arm (lines 135-141) filters all subjects by callingsubject_kind(&Hash::from_bytes(*subject))for each one. There is no memoization across terms, so the per-subject call count is exactly the number ofSubjectKindterms in the query. -
Verified this run on the laptop with a probe carrying the
search_termsloop verbatim (stubTagIndex/Hash/Claimtypes only;search.rsmd559987199ef6eb85becd589f51c434c86, identical on both clones; probe~/git/fleet/probe-2026-10-31-laneB-searchkind/). Two subjects (one claim each) and a counting lookup:terms lookup calls doc promise one SubjectKindterm2 ≤ 2 ✓ two different SubjectKindterms4 ≤ 2 ✗ the same SubjectKindterm twice4 ≤ 2 ✗
Why it matters
The "at most once per subject" sentence is the contract a caller relies on when supplying an expensive lookup — the natural implementation reads the subject's event from the store (that is exactly what search_store_terms wires it to, EventStore::event_by_id). search_store_terms happens to pre-resolve and cache kinds before calling search_terms, so the store caller is shielded today; any direct caller of the public search_terms who follows the documented contract with an uncached lookup pays one store read per subject per kind term instead of one per subject. Multi-kind queries are also the case where the extra work is pure waste: a subject has exactly one kind, so two distinct kind terms can never both match — yet every subject is still looked up for both. The fix is small and local: resolve each subject's kind once (memoize in a map) before/within the term loop and reuse it for every kind term.
Related but distinct: #2779 covers search_store_terms resolving every subject's kind even when no kind term is present (wasted work on the caller side). This issue is the dual gap inside search_terms itself: with kind terms present, the lookup runs more than the documented once per subject.
Acceptance criteria
search_termscallssubject_kindat most once per distinct subject, regardless of how manySubjectKindterms the query contains (memoize the resolved kind per subject).- Search results are unchanged for all existing cases: single kind term, multiple kind terms (AND semantics — distinct kinds still match nothing), kind-only term sets, and mixed name/author/kind term sets.
- A unit test with a counting lookup pins the contract: N subjects × K kind terms (K ≥ 2) results in at most N lookup calls, and the duplicate-same-kind-term case is covered.
- The doc comment and the implementation agree after the fix.
Dedup: issue search for search_terms/subject_kind returned only #2779 (the search_store_terms no-kind-term over-resolution — a different function and the opposite waste); no open or closed issue covers the per-term re-calling inside search_terms. NOT self-solved: filed-only this run (time discipline); the memoization is a small, clearly scoped fix for the queue.
Activity
dezren39 commentedon Oct 31, 2026
🤖 Agent note · filed
dezren39 commentedon Nov 1, 2026
🤖 Agent note · picked up
56 remaining items
dezren39 commentedon Nov 2, 2026
🤖 Agent note · picked up
gh pr viewresponse included status-check/review-summary fields before the saved restriction was noticed; none were acted on.dezren39 commentedon Nov 2, 2026
🤖 Agent note · parked
work/assignedremains.Merge pull request #3688 from developing-today/queue/5518-3013-memoiz…
dezren39 commentedon Nov 2, 2026
agent-note:v1
event: done
task: 5594 (repo-health-code-pilot, repo-worker-code — OpenChamber scheduled lane 9aca43b4-5439-4939-9f71-323f5d32521b)
state: Shepherd complete — PR #3688 MERGED at 54cbdaf (merged 2026-11-02T15:57:09Z) from queue/5518-3013-memoize-subject-kind; issue #3013 is CLOSED. Acceptance met: PR merged, issue state matches, no separate Codex/PR disposition needed beyond the recorded merge. No code, branch, or worktree touched by this lane (metadata-only revalidation of the Drew-owned artifacts after the fact).
links: #3688