Sangeetha-Grantha

Metadata Value
Status Completed
Version 1.2.0
Last Updated 2026-11-02
Author Sangeetha Grantha Team

Track: Macro Search Follow-ups

ID: TRACK-157 Status: Completed (2026-11-02) Owner: Seshadri Created: 2026-11-01 Updated: 2026-11-02

Goal

Close the open items TRACK-152 left on the collection and kshetra cards in Rasika search: make the relevance floor robust, get a human decision on the cards that appeared on non-macro probes, and give other environments a safe, checkable way to bring macro embeddings up to date.

Context

Intent

Status: Accepted Accepted by: Seshadri (chat instruction; live runs not authorised) Accepted at: 2026-11-02

Problem

  1. The floor sits almost on top of the weakest real hit. The weakest macro hit scores 0.836 against a floor of 0.83 (a 0.006 margin) while the exact-title kriti probes peak at 0.787 (a 0.043 margin on the other side). A re-embedded overview, a reworded overview or a new embedding profile can move a score by more than 0.006 and silently remove a correct card.
  2. Three non-macro probes, plus a Ratnatrayam query, now show cards that nobody has looked at (EVAL-22, SS-146-02, SS-146-03). Some may be right; none has been judged.
  3. Other environments are one step behind. The dev database re-embedded 6 updated overviews; every other environment needs make migrate and a macro re-embed. The only documented command for it does not run (see Spec), and the obvious script flag walks the whole catalogue.
  4. A Gemini usage question is open (about 1,000 paid calls beyond what was authorised, per the TRACK-152 session’s own disclosure). That check is Seshadri’s, in the Google console.

Proposed outcome

  1. A floor rule with a stated, tested margin on both sides, calibrated from recorded per-probe scores rather than two data points, and tied to the embedding profile it was calibrated on.
  2. A reviewed table for the cards on non-macro probes, with Seshadri’s keep or drop decision on each, and a rule for any “drop” (floor change, or a recorded exception) that does not hurt the macro probes.
  3. A runbook step and an automatic check that prove macro embeddings are up to date in any environment, using no paid call for the check.
  4. The Gemini usage check recorded as done or not done by Seshadri.

Constraints

Open questions

  1. Which direction should the floor take if the recorded distribution is ambiguous: prefer fewer cards (high floor) or more cards (low floor)? Draft answer: fewer; a missing card is recoverable by the kriti results, a wrong card is noise on top of the list.
  2. Is a per-profile floor stored in code (a map keyed by model and dimensions) acceptable, or should it live in the database beside embedding_profiles? Draft answer: code, because the repo has no config surface for it and a migration is not warranted.
  3. Who judges the cards on non-macro probes? Draft answer: Seshadri, with the carnatic-musicologist subagent only if a card makes a lakshana claim (none expected; cards show stored overview text).

Spec

Status: Accepted Accepted by: Seshadri (chat instruction; live runs not authorised) Accepted at: 2026-11-02

Evidence read (all from the repo and TRACK-152 notes)

Findings

F1. The calibration rests on two positives and six negatives, and the track records disagree about which positive is weakest.

F2. An absolute cosine threshold is specific to one embedding model, dimension and document text. 0.83 has no meaning after a profile change, and a re-embed of the 6 updated overviews already moved scores once (TRACK-152 notes). Nothing in the code ties the floor to embedding_profiles, so a profile switch would keep a threshold calibrated on a different space.

F3. Asymmetric margin. With the recorded numbers the floor is 0.043 above the strongest recorded negative and 0.006 below the weakest recorded positive. The midpoint of 0.787 and 0.836 is about 0.8115, which would give about 0.025 on each side. Moving there without the full distribution would probably admit more cards on non-macro probes (item 2 below), so the midpoint is a candidate to test, not a decision.

F4. The negatives are the wrong negatives. The six exact-title probes are queries a user would type to find a specific kriti, so they are easy negatives. The probes that actually produced unreviewed cards (EVAL-22, SS-146-02, SS-146-03, and a Ratnatrayam query) are composer/form queries that sit near a cycle overview in embedding space. The floor has never been measured against those; they are exactly the hard cases.

F5. Rank-based rules alone cannot say “no card”. A rule such as “top 3 of 105” always returns cards; the floor exists because the group should often be empty (TRACK-152 Spec 13). A rank or gap rule is useful only on top of an absolute guard.

Options for the floor (to be chosen on recorded data)

Option Idea For Against
A. Per-profile absolute floor at the measured midpoint Keep similarity >= f, but keep f in a map keyed by profile (model, dimensions); set f to the midpoint between the best-scoring labelled negative and worst-scoring labelled positive Smallest code change; same behaviour shape; fixes F2 and F3 Still an absolute number; needs the full distribution (F1, F4) and a re-check per profile
B. Relative to the query’s own score distribution Require similarity >= mean + k * sd over all ~105 macro scores of that query (already computed in SQL), plus a low absolute guard Adapts to the profile and to broad queries where everything scores high New moving part; needs k calibrated; harder to explain; a query that is legitimately close to several overviews can suppress a true card
C. Gap rule Show the best candidate only if it beats the 4th candidate (or the best kriti’s similarity) by a margin, plus a low absolute guard Directly encodes “stands out from the pack”; no per-profile constant except the guard Weak when two cycles are both right (Ratnatrayam and Navaratnamalika); compares scores across different documents types for the kriti variant
D. Margin test only Keep 0.83; add a test that fails when recorded labelled scores come within 0.02 of the floor Cheapest; makes the thinness visible Does not make the floor robust; fails today (0.006) and would force A anyway

Draft recommendation: A plus D. Move to a per-profile absolute floor at the measured midpoint of the recorded labelled distributions, and add a committed margin test that reads the recorded scores file and fails below a 0.02 margin on either side. Treat B and C as fallbacks, taken only if the recorded distribution shows no absolute value achieving 0.02 on both sides. The choice is confirmed in Plan step 4 from the data, not from this draft.

Requirements

A. Floor robustness

  1. A recorded scores file (tools/krithi-extract-enrich-worker/evals/macro_scores.json) holds, per probe, the dense similarity and the lexical score (both mode-independent; the hybrid rank is not needed because the floor reads only these two) of every candidate the macro query returned (all eligible anchors, not only those clearing the floor), the profile id/model/dimensions, and the date. It is data, committed, and regenerated only by an authorised run.
  2. The floor is chosen by a stated rule: all labelled positives (macro probes that currently HIT, plus any added with Seshadri’s approval) clear it, the six exact-title probes return no card, and the margin to the nearest labelled positive and the nearest labelled negative is at least 0.02 on each side. If no value satisfies this, the Plan stops and reports to Seshadri before any code changes (options B/C).
  3. The floor constants are keyed by embedding profile (model and dimensions). A profile with no entry returns an empty macro group and logs once at WARN, rather than reusing another profile’s number.
  4. An offline unit test (no network, no database) reads macro_scores.json and asserts requirement 2 against the constants in code, so a change to either fails the build.
  5. TY-147-11 is diagnosed: it is either explained and recorded as a known non-hit with its reason, or fixed in a way that does not touch overview text without approval.
  6. search.md, the RasikaSearchService comment and the track-145 note are corrected to the recorded numbers.

B. Cards on non-macro probes

  1. A review table lists, for EVAL-22, SS-146-02, SS-146-03 and the Ratnatrayam query, the probe text, the card(s) shown, the card’s similarity and lexical score, and a keep or drop recommendation. The table is produced from macro_scores.json and the production floor (not typed by hand) and is committed in this track file.
  2. A card is “keep” when the probe’s own expected kriti is a member of the card’s collection or sits at the card’s kshetra (checkable from krithi_cycle_memberships / krithis.temple_id with expected_krithi_id, which the Syama Sastri probes carry), or when Seshadri says so. Otherwise “drop”.
  3. A “drop” is implemented only through requirement 2 (floor) or a recorded exception list in the Plan; never by special-casing a probe id in service code.
  4. Seshadri’s decision on each row is recorded in the Progress Log. Rows are not auto-counted as failures.

C. Environment follow-up

  1. A runbook step in application_documentation/08-operations/runbooks/ (new file macro-embeddings-runbook.md) and a pointer from search.md describes: make migrate, a no-spend dry run, an authorised macro-only re-embed, and the check below.
  2. A macro-only mode exists in the worker (embed_catalogue.py --macro-only) that never fetches krithi candidates and never embeds a kriti document. --macro-only --dry-run makes no API call and prints how many cycle and kshetra overviews would be embedded. Today there is no such mode: see C-finding below.
  3. A freshness check (embed_catalogue.py --macro-only --check) exits non-zero when any published macro document is stale, using only database reads and local hashing (no embedding call). A document is stale when any of these holds for the active profile: no embedding row exists; document_embeddings.content_hash <> search_documents.content_hash; search_documents.content_hash <> md5(indexed_content); or the overview text regenerated from current source data hashes differently from the stored content_hash. The last case is what catches a source-data change (for example a TRACK-154 temple update) that has not yet been re-indexed. A read-only SQL form of the first three conditions is also given in the runbook for environments without the worker.
  4. The runbook includes the expected success output (stale = 0, counts of cycle and kshetra documents by kind) and what to do on failure.

D. Gemini usage item

  1. Recorded as an open item for Seshadri only. This track makes no attempt to read or verify usage. It is closed by a Progress Log line from Seshadri.

C-finding: the documented macro-only command does not work and the nearby flag is unsafe

Amendment (2026-11-02, accepted by Seshadri in chat after the Phase 1 recording)

The Phase 1 recording showed that requirement 2 cannot be met by an absolute similarity floor alone (see Results). Seshadri adopted option C layered on A, so requirements 2 to 4 read:

  1. (amended) A card needs, per profile, similarity at or above minSimilarity and a gap (its similarity minus the 4th-best anchor’s) at or above minGap, or (hybrid) a lexical score at or above minLexical. For gemini-embedding-2 / 768: 0.81, 0.03, 0.8. All labelled positives clear; the six exact-title probes and the four wrong-looking cards (provisional labels) show no card. Margins: at least 0.02 on similarity to the weakest positive and to the exact-title peak, at least 0.01 on gap to the weakest positive, at least 0.015 on gap to the widest wrong-looking card. The 0.02 margin on the gap dimension is not achievable with three positives (actual: 0.014 and 0.017).
  2. (unchanged) Floors are keyed by profile (model name and dimensions); an unknown profile shows no macro cards and logs one WARN. With no active profile only the lexical route applies.
  3. (amended) The offline test is MacroFloorsMarginTest, reading the recorded file from the repository.

Design

Flagged concerns

Open questions carried forward

The three Intent questions, with draft answers above.

Plan

Status: Accepted Accepted by: Seshadri (chat instruction; live runs not authorised) Accepted at: 2026-11-02

Nothing is implemented. Do not start until Seshadri accepts this Plan. Phases land separately.

Files that change

Path Phase Change
tools/krithi-extract-enrich-worker/scripts/evaluate_retrieval.py 1 --dump-macro-scores <path> on the DB path; full macro candidate list per probe and mode, profile metadata
tools/krithi-extract-enrich-worker/evals/macro_scores.json (new) 1 Recorded output of the authorised run
tools/krithi-extract-enrich-worker/tests/test_evaluate_retrieval_sql.py (or a new test) 1 Dump format test with a fake embedder
modules/backend/api/.../services/RasikaSearchService.kt 2 Profile-keyed floor map; empty group and WARN for an unknown profile; corrected comment
modules/backend/api/src/test/.../RasikaSearchFloorTest.kt (new) and a test resource copy of macro_scores.json 2 Offline margin test (requirement 4)
tools/krithi-extract-enrich-worker/scripts/embed_catalogue.py, src/embeddings/macro_indexer.py 3 --macro-only, --check, shared text-regeneration helper
tools/krithi-extract-enrich-worker/tests/test_macro_indexer.py (extend) 3 --macro-only fetches no krithis; --check stale and fresh cases with a fake connection
application_documentation/08-operations/runbooks/macro-embeddings-runbook.md (new), runbooks/README.md, application_documentation/03-api/search.md 3 Runbook, corrected command, corrected numbers
application_documentation/10-implementations/track-145-dikshitar-kshetra-musicological-metadata.md 2 Correct the EVAL-31 figure only if the recorded run disagrees with it
conductor/tracks/TRACK-157-macro-search-followups.md, conductor/tracks.md all Review table, decisions, status

No migration. No change to kriti SQL, V1, DTOs or mobile code. No .claude/ or CLAUDE.md change (so make agent-evals is not needed).

Live runs (need Seshadri’s explicit authorisation, each one)

These spend Gemini quota and are not run by anyone until Seshadri says so for that run.

  1. Scores capture. One embedding call per distinct probe query (53 probes loaded; about 53 calls, shared between modes by the Phase 1 change). From the worker directory, against the intended database and with the credentials the worker already uses:
    uv run python scripts/evaluate_retrieval.py --dump-macro-scores evals/macro_scores.json
    

    The dump mode embeds each distinct probe query once and exits; it does not take --mode. Needs DATABASE_URL (or --db-url) for the intended database and the worker’s Gemini credentials.

  2. End-to-end proof after Phase 2 (calls the backend, which embeds each probe query: about 53 calls per mode, the backend’s own cache applies within a run):
    uv run python scripts/evaluate_retrieval.py --mode all --via-api http://localhost:8080
    
  3. Macro re-embed, per environment (a few calls: only overviews whose text changed; see runbook):
    uv run python scripts/embed_catalogue.py --macro-only --check
    uv run python scripts/embed_catalogue.py --macro-only --dry-run
    uv run python scripts/embed_catalogue.py --macro-only
    

    The first two make no API call. Run the third only after reading their output.

Order of work

Phase 0: sign-off (no code). Seshadri accepts Intent, Spec and Plan, or edits them. Seshadri also checks Gemini usage in the Google console (Progress Log, item 15).

Phase 1: record the scores (worker only).

  1. Write the dump format test (red), then implement --dump-macro-scores.
  2. With authorisation, run live run 1 and commit macro_scores.json.
  3. From the file, fill the “Results” subsection below: per-probe macro scores for the four macro probes, the six exact-title probes and every non-macro probe that has any candidate above 0.80; reconcile the 0.836 / 0.837 / 0.854 discrepancy; explain TY-147-11.

Phase 2: choose and test the floor (backend).

  1. Compute the floor options A to D on the recorded file with a short script run offline (no API): for each option list pass/fail per labelled probe and the margins. Pick A if it meets requirement 2; otherwise stop and report to Seshadri with the numbers (options B or C need an amended Spec).
  2. Write RasikaSearchFloorTest (red), then the profile-keyed floor and the empty-group-for-unknown-profile behaviour. Existing route and repository tests unchanged.
  3. Generate the review table (requirement 7) from the recorded file using the final floor, with keep/drop per requirement 8 (membership check by read-only SQL, no paid call). Put it in this file and ask Seshadri to decide each row.
  4. With authorisation, run live run 2 and record that macro probes equal the Phase 1 outcomes and items parity shows zero differences.
  5. Correct the documented numbers (requirement 6).

Phase 3: environments and the freshness check (worker, docs).

  1. Write tests (red), then --macro-only and --check.
  2. Write the runbook and the pointers; replace the broken snippet in search.md.
  3. Run --macro-only --check against the dev database (read-only) and paste the output; with authorisation, run it in each other environment after its make migrate and re-embed, and paste the outputs.

Review table (from macro_scores.json, recorded 2026-11-02)

Cards that a floor of 0.83 would show, per probe, using each probe’s best macro document per anchor (dense similarity, lexical score, gap4 = top similarity minus the 4th-best anchor’s). The recording is not eligibility-filtered, so a row can disappear in the API if its anchor is ineligible. Keep or drop is provisional until Seshadri decides; the first four rows follow requirement 8 (the expected kriti is a member of the card’s collection, checked by a read-only query on krithi_cycle_memberships).

Probe Probe text Card Sim / lex / gap4 Recommendation
EVAL-22 Swarajathi Kritis of Syama Sastri Syama Sastri Swarajathi Ratnatrayam (collection) 0.838 / 0.69 / 0.044 Keep: the form by this composer is that set (no expected_krithi_id; judgement)
SS-146-03 Syama Sastri Swarajathi in Yadukulakambhoji … Kamakshi Ni Padayugamu same 0.839 / 0.45 / 0.043 Keep: expected kriti is a member
SS-146-02 Syama Sastri Swarajathi in Todi … Rave Himagiri Kumari same 0.831 / 0.38 / 0.049 Keep: expected kriti is a member
SS-146-01 (“Ratnatrayam” query) Syama Sastri Swarajathi Ratnatrayam first gem in Bhairavi … same 0.824 / 0.47 / 0.042 Keep: expected kriti is a member (below 0.83, so no card today; shows at floor 0.82)
EVAL-05 Dikshitar krithi in Chidambaram describing the cosmic dance Nataraja Temple (kshetra) 0.849 / 0.30 / 0.041 Keep, plausible (Seshadri to confirm)
EVAL-20 Praise of Lord Shiva Nataraja in Kedaram raga Nataraja Temple (kshetra) 0.834 / 0.33 / 0.038 Keep, plausible
EVAL-27 Fifth enclosure Kamalamba Navavarnam Panchami vibhakti Bhairavi Kamalamba Sannidhi (kshetra) 0.843 / 0.34 / 0.038 Keep, plausible (a Kamalamba Navavarnam collection card would fit better; it is second, gap2 0.005)
EVAL-24 Muthuswami Dikshitar Agni lingam fire element at Tiruvannamalai Arunachaleswarar Temple (kshetra) 0.8304 / 0.45 / 0.047 Keep: the TRACK-152 Spec expected this card; only 0.0004 above the floor
EVAL-06 Abhayamba series krithi in Kalyani raga Abhayamba Vibhakti Krithis (collection) 0.8305 / 0.34 / 0.020 Keep, plausible; weak separation (gap4 0.020)

Unreviewed cards the TRACK-152 note did not mention appear in this recording at 0.83 (EVAL-05, EVAL-20, EVAL-27, EVAL-24, EVAL-06): either they are filtered by eligibility in the API or the note was incomplete; the --via-api run settles which.

Probes just under 0.83 whose top card looks wrong (so a lower absolute floor would add them): EVAL-08 Tiruvarur Pancha Linga Krithis for “Dikshitar krithi in Anandabhairavi” (0.823, gap4 0.009), TY-147-10 (0.821, gap4 0.004), EVAL-29 (0.819, gap4 0.013) and TY-147-04 (0.819, gap4 0.012), all Anandavalli Sametha Thanjapureeswarar Temple. These are my reading of the probe text, not labels.

Results (Phase 1 live run, 2026-11-02)

Authorised by Seshadri in chat; 53 query embeddings; recorded in tools/krithi-extract-enrich-worker/evals/macro_scores.json (profile gemini-embedding-2, 768, id 01a07605-793b-7209-bbd2-f173e41b6d92; 106 macro anchors per probe).

Results (Phase 2, live run 2: --via-api, 2026-11-02)

Authorised by Seshadri in chat. This branch’s backend ran on port 8081 against the local database (the 8080 stack was left alone and uses older code), then evaluate_retrieval.py --mode all --via-api http://localhost:8081 posted 53 probes to V2 and V1 in both modes (about 106 query embeddings). The backend was stopped afterwards.

Risks

Completion evidence (fixed)

Implementation Plan

Open items for Seshadri (not done by this track)

  1. Check Gemini usage in the Google console Done 2026-11-02 (see Progress Log): net ₹0.00.
  2. Authorise each live run Done: runs 1 and 2 and the dev re-embed were authorised.
  3. Confirm which probe the “Syama Sastri Ratnatrayam” query is Recorded as SS-146-01 in the review table; Seshadri kept all rows.

Progress Log