Skip to content

id(search): search_terms re-calls the subject_kind lookup once per SubjectKind term per subject — its doc promises at most once per subject, so a two-kind-term search does twice the store reads #3013

Closed

Description

@dezren39

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 on search_terms says: "subject_kind resolves 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 a for term in terms loop, and the Term::SubjectKind(kind) arm (lines 135-141) filters all subjects by calling subject_kind(&Hash::from_bytes(*subject)) for each one. There is no memoization across terms, so the per-subject call count is exactly the number of SubjectKind terms in the query.

  • Verified this run on the laptop with a probe carrying the search_terms loop verbatim (stub TagIndex/Hash/Claim types only; search.rs md5 59987199ef6eb85becd589f51c434c86, 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 SubjectKind term 2 ≤ 2 ✓
    two different SubjectKind terms 4 ≤ 2 ✗
    the same SubjectKind term twice 4 ≤ 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_terms calls subject_kind at most once per distinct subject, regardless of how many SubjectKind terms 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

  1. added
    kind/bugSomething isn't working (prefixed form of the classic bug label)
    priority/lowNice to have; do when convenient
    source/creatorFiled by the new-issue-creator job
    task/triageQueue packet kind: verify, reproduce, and route this item
    app/idPackage/app: id (pkgs/ or apps/ in this repo)
    effort/xsTrivial: expected under 30 minutes of work
    on Oct 31, 2026
  2. dezren39 commented on Oct 31, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · filed

    • Agent: new-issue-creator (new-issue-creator-code-b)
    • Session / ID: cron new-issue-creator-code-b run 2026-10-31 04:50 CDT
    • Found via: filed this run
    • Task: Filed this issue: search_terms re-calls subject_kind once per SubjectKind term per subject, against its documented at-most-once-per-subject contract (laptop verbatim-loop probe: 2 subjects × 2 kind terms = 4 calls).
    • Expectation: effort/xs triage — memoize the per-subject kind resolution in search_terms; results unchanged; counting-lookup unit test pins the contract.
    • Links: queue packet packet-3013-triage.json (repo-health-code-pilot)
  3. dezren39 commented on Nov 1, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  4. added
    work/assignedQueue worker lifecycle state
    queue/claimedQueue worker lifecycle state
    and removed on Nov 1, 2026
  5. 56 remaining items

  6. added
    queue/claimedQueue worker lifecycle state
    and removed
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    on Nov 2, 2026
  7. dezren39 commented on Nov 2, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · picked up

  8. added
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    status/parkedDeliberately paused with a recorded resume condition
    and removed
    queue/claimedQueue worker lifecycle state
    on Nov 2, 2026
  9. dezren39 commented on Nov 2, 2026

    @dezren39
    MemberAuthor

    🤖 Agent note · parked

  10. added a commit that references this issue on Nov 2, 2026
  11. dezren39 commented on Nov 2, 2026

    @dezren39
    MemberAuthor

    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

  12. removed
    status/parkedDeliberately paused with a recorded resume condition
    queue/parkedQueue task is parked (waiting on CI, time, or an event)
    work/assignedQueue worker lifecycle state
    on Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

app/idPackage/app: id (pkgs/ or apps/ in this repo)effort/xsTrivial: expected under 30 minutes of workkind/bugSomething isn't working (prefixed form of the classic bug label)priority/lowNice to have; do when convenientsource/creatorFiled by the new-issue-creator jobtask/triageQueue packet kind: verify, reproduce, and route this item

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions