|
| 1 | +# §5 lexical OR-fallback — pre-registration (2026-09-15) |
| 2 | + |
| 3 | +**Frozen before any measurement run.** Branch `feat/lexical-or-fallback` off `main` `a0272a52` |
| 4 | +(the SEALED-outcome commit); RED tests at `90964e9c` |
| 5 | +(`packages/agent-session-tools/tests/test_query_planner_or_fallback.py`). Written under |
| 6 | +council decision D-12 (`docs/architecture/plan-integration/council/arbitration-plan-round1-2026-09-15.md`) |
| 7 | +and design §7 (`openspec/changes/plan-application-seam/design.md`). Changing anything in this file |
| 8 | +after the run is a new pre-registration, not an edit. |
| 9 | + |
| 10 | +## Hypothesis |
| 11 | + |
| 12 | +The archived `feat/knowledge-proof` branch's `plan_prose_query` — every raw whitespace token of the |
| 13 | +question quoted (embedded `"` doubled, Unicode `Cc`/`Cs` characters stripped, tokens with no |
| 14 | +alphanumeric dropped) and joined with `OR`; **no stop list, no length filter** — produced the |
| 15 | +historical +0.142 (DEV) / +0.168 recall@5 lifts against the *Stage 1* shipped planner |
| 16 | +(`council-stage4-2026-09-10.md`, F-B0-1). Those numbers were measured against a different store, a |
| 17 | +different corpus and a planner that has since been replaced (Stage 2), so they are **prioritisation |
| 18 | +evidence, not confirmation** (D-12). The question here is whether the same construction helps the |
| 19 | +*current* shipped planner when used in its narrowest possible position. |
| 20 | + |
| 21 | +## Candidate — Grok's narrow form (D-12), stated exactly |
| 22 | + |
| 23 | +The shipped natural-language planner at `a0272a52` is `retrieval.plan_natural_language` |
| 24 | +(`packages/agent-session-tools/src/agent_session_tools/retrieval.py`): double-quoted spans are |
| 25 | +lifted as phrase terms; the remainder is tokenised by `query_planner._terms` (`[a-zA-Z0-9_./-]+`, |
| 26 | +lower-cased, the 62-word `STOP` set and `len(token) <= 2` dropped); every term is quoted; the |
| 27 | +`MATCH` strings tried are `AND`-joined first, then — only when the `AND` form returns zero rows — |
| 28 | +`OR`-joined (the *widen* step). `retrieval.plan_query` stands in front: the `fts:` prefix or an |
| 29 | +uppercase `AND|OR|NOT|NEAR` outside every double-quoted span is explicit FTS5, passed through |
| 30 | +verbatim, never planned. (`query_planner.plan` carries the same AND/OR construction as a pure |
| 31 | +function without phrase handling; nothing on the serving path calls it today.) |
| 32 | + |
| 33 | +The candidate `and_then_prose_or` changes **one thing**: the widen string. Instead of the OR of the |
| 34 | +*filtered* quoted terms, it is `query_planner.prose_or_query(<raw question>)` — the branch |
| 35 | +function's output over the whole raw text. Everything else is unchanged and this is binding: |
| 36 | + |
| 37 | +- the `AND` arm, its `STOP` set and its `len(token) > 2` filter stay exactly as shipped; |
| 38 | +- the widen still runs **only** when the `AND` arm returned zero rows; |
| 39 | +- a question with **no content terms** (e.g. `what is the?`) still returns `plan="none"` and is not |
| 40 | + searched — the widen step is never reached without an `AND` arm in front of it; |
| 41 | +- the shipped de-duplication stays: when the widen string equals the `AND` string only one query |
| 42 | + is tried; |
| 43 | +- the explicit door stays in front and is **never** reached by the candidate (S.1 tests |
| 44 | + `test_explicit_fts_prefix_is_verbatim`, `test_uppercase_operator_outside_quotes_is_verbatim`, |
| 45 | + `test_quoted_operator_is_not_explicit`); |
| 46 | +- `retrieval_status.terms` keeps reporting the `AND` arm's content terms; `queries` lists what was |
| 47 | + actually tried, so a reader can see the widen string. |
| 48 | + |
| 49 | +## Corpus |
| 50 | + |
| 51 | +| item | value | |
| 52 | +|---|---| |
| 53 | +| Live database | `~/.config/studyloop/sessions.db` — 912,318,464 bytes, mtime 2026-09-15T19:18:50+01:00, sha256 `d164906e560586da72d52fb26ff7748d43fa7e635064d83e334e351423bcf5c7` (main file only; the database is in WAL mode with a live 54,664,192-byte `-wal` still receiving other agents' session exports, so the main file's digest alone does not name the readable corpus) | |
| 54 | +| **Measured corpus** | a snapshot clone `~/.local/share/studyloop/eval-clones/lexical-or-fallback-20260915/sessions.db`, taken 2026-09-15 ≈21:25 BST by `VACUUM INTO` from a `file:…?mode=ro` connection (main + WAL, one consistent read), `chmod 0444` — 901,582,848 bytes, sha256 `53b881b040555a45dcf6e83892e7e31f12f52dd839d25b1eda7b5f762bee4db5`, `journal_mode=delete`, `user_version=48` | |
| 55 | +| Harness fingerprint (`eval.receipt.db_fingerprint`) | `469824ce96f5877bdc70b2b69a9d5a23f80509bcfb3963a1a4d92a65f910290c` — identical for the clone and the live database at snapshot time | |
| 56 | +| Visibility (`eval.receipt.resolved_visibility`) | admitted sources `claude_code, codex, grok, kiro_cli, opencode, pi, study_mentor`; visible 2,205 of 2,205 sessions; 62,267 messages; `message_embeddings` 42,191 rows pinned to `bge-small-en-v1.5` (irrelevant here — every arm runs lexical) | |
| 57 | + |
| 58 | +Why a clone rather than the live path: the live file is being written to during this window |
| 59 | +(parallel agents export sessions), and five arms must see one corpus for the paired comparison to |
| 60 | +be paired. The clone *is* the live corpus at one instant; both digests are recorded so either can |
| 61 | +be checked. Every arm opens it read-only (`?mode=ro`), as the harness always has. |
| 62 | + |
| 63 | +**Ceiling, a property of this corpus and not of any arm.** The live database is the *hot* tier |
| 64 | +(`docs/session-db-tiering.md`); sessions have been pruned to the full tier since the gold set was |
| 65 | +built (Stage 4 saw 5,880 sessions; the SEALED clone this morning had 2,173). In this snapshot only |
| 66 | +**61 of 91** DEV items have at least one `gold_session_id` present in `sessions` — K 22/33, |
| 67 | +P 18/29, R 21/29. The other 30 (`A1-6 A1-13 A1-37 A1-49 A1-63 A1-70 A1-71 A1-76 A1-77 A1-81 A1-85 |
| 68 | +A1-88 A1-89 A1-106 A2-1 A2-8 A2-13 A2-25 A2-33 A2-38 A2-40 A2-41 A2-48 A2-56 A3-3 A3-10 A3-20 |
| 69 | +A3-21 A3-31 A3-41`) are unwinnable by every arm and **stay in the denominator** (freeze rule: the |
| 70 | +ruler is not shrunk). Paired deltas on them are exactly zero; the effect is on power, not on sign. |
| 71 | + |
| 72 | +## Gold |
| 73 | + |
| 74 | +`docs/architecture/session-memory/receipts/gold-v2-dev.json` — sha256 |
| 75 | +`5632cd2b02a77dbd95ded3fae3aa32fa1599cd43929f43e44aa132343c3c6098`, 58,842 bytes, `gold_version` |
| 76 | +v2, set DEV, **91 items**, 57 clusters, strata K 33 / P 29 / R 29, 4 items with more than one gold |
| 77 | +session. The SEALED set is **not** used: it was spent on 2026-09-15 (`stage5-sealed-bge-gold.json`) |
| 78 | +and D-12 rejects re-running it. This is a DEV-only measurement and will be labelled as such. |
| 79 | + |
| 80 | +## Arms — planner variants, orthogonal to the transport arms |
| 81 | + |
| 82 | +All five run through the **`mcp` transport arm** (`eval.arms.McpArm`: the real `session_search` |
| 83 | +tool via FastMCP `call_tool`, `STUDYLOOP_RETRIEVAL_MODE=lexical`, `rows=10` message rows before |
| 84 | +the collapse to sessions, `k=5`), so the only thing that differs between arms is the natural- |
| 85 | +language planner. The variant is applied by substituting `retrieval.plan_natural_language` for |
| 86 | +the duration of the arm's call — after `plan_query` has classified the string, so the explicit |
| 87 | +door is identical in all five. Arm names in the receipt are `mcp:<planner>`; `mcp` alone is the |
| 88 | +shipped planner. |
| 89 | + |
| 90 | +| arm | `MATCH` strings tried, in order | tokens | |
| 91 | +|---|---|---| |
| 92 | +| `shipped` (`mcp`) | `AND` of filtered quoted terms → `OR` of the same | phrases + `_terms` (STOP, len>2) | |
| 93 | +| `or_first_filtered` | `OR` of filtered quoted terms, alone | phrases + `_terms` | |
| 94 | +| `and_first_unfiltered` | `AND` of every raw token quoted → `OR` of the same | `prose_or_query` tokenisation (no STOP, no length filter) | |
| 95 | +| `or_only_unfiltered` | `prose_or_query(raw)` alone — the branch function as it was | raw tokens | |
| 96 | +| **`and_then_prose_or`** (candidate) | shipped `AND` → `prose_or_query(raw)` | AND: filtered; widen: raw | |
| 97 | + |
| 98 | +For the two unfiltered arms a question whose raw tokenisation is empty (punctuation only) is |
| 99 | +`plan="none"`, mirroring the shipped no-content-terms return. |
| 100 | + |
| 101 | +## Metrics and inference |
| 102 | + |
| 103 | +- **Primary:** macro recall@5 over K/P/R (`eval.metrics.recall_at_k`, hit = any gold session in the |
| 104 | + first 5 distinct sessions). |
| 105 | +- **Guardrails (reported, and clause 2 below):** precision@5 = |gold sessions ∩ first 5 distinct |
| 106 | + sessions returned| / 5 per item, macro-averaged over strata exactly like recall (the denominator |
| 107 | + is 5 even when fewer sessions come back — an empty result is precision 0, not undefined); |
| 108 | + MRR@5 macro (`eval.metrics.mrr_at_k`). |
| 109 | +- **Also reported:** crashes by `ArmError` kind (a crash is a miss in the denominator); latency |
| 110 | + p50/p95 per arm (reported, never compared). |
| 111 | +- **Inference:** paired **cluster** bootstrap, cluster = gold `cluster` (57), **10,000** resamples, |
| 112 | + seed **20260910**, percentile CI95 — the frozen Stage 1 ruler (`eval/__init__.py`: `RESAMPLES`, |
| 113 | + `SEED`). Recall uses the existing `eval.metrics.cluster_bootstrap`; precision and MRR use the same |
| 114 | + resampling over per-item values. The K-stratum non-inferiority entry the `gold` subcommand |
| 115 | + already emits is recorded for every pair. |
| 116 | +- Every ordered pair of the five arms is compared; the pair that decides is |
| 117 | + **`mcp:and_then_prose_or` vs `mcp`**. |
| 118 | + |
| 119 | +## Adopt rule — frozen |
| 120 | + |
| 121 | +Adopt `and_then_prose_or` (S.4: one commit swapping only the widen string in |
| 122 | +`retrieval.plan_natural_language` and `query_planner.plan`, plus a new golden for the widen path) |
| 123 | +**if and only if all four hold**: |
| 124 | + |
| 125 | +1. DEV macro recall@5 paired-bootstrap delta (`and_then_prose_or − shipped`) has **CI95 lower bound |
| 126 | + > 0** (strictly). *Note:* this is weaker than the programme's "established lift" (lower bound |
| 127 | + ≥ +0.05); D-12 chose it and it is recorded as such — the receipt reports both. |
| 128 | +2. Macro precision@5 drop (`shipped − and_then_prose_or`, point estimate) **≤ 0.05 absolute**. |
| 129 | +3. The explicit-door tests pass on the tree that produced the receipt: |
| 130 | + `test_query_planner_or_fallback.py::test_explicit_fts_prefix_is_verbatim`, |
| 131 | + `::test_uppercase_operator_outside_quotes_is_verbatim`, `::test_quoted_operator_is_not_explicit`. |
| 132 | +4. `packages/agent-session-tools/tests/golden/session_search_pre_planner.json` is byte-identical |
| 133 | + to its committed form: sha256 `7152dae40af4918dffd6a51cc4b7d399c433384a3caa9a7ca64164e7a56795f6` |
| 134 | + (`::test_pre_planner_golden_unchanged`). |
| 135 | + |
| 136 | +Anything else — including "the lift is positive but the interval touches zero", "crashes appeared", |
| 137 | +or "a different arm won" — is **reject**: the shipped planner is left alone, the helper and its |
| 138 | +tests stay as measured code, and the receipt records the numbers. No threshold is revisited after |
| 139 | +seeing the numbers; a different arm that looks better is a *new* hypothesis for a new |
| 140 | +pre-registration, not an adoption under this one. |
| 141 | + |
| 142 | +## How the run is made and what it leaves behind |
| 143 | + |
| 144 | +``` |
| 145 | +# one run, five arms, one clone, one receipt (raw, outside the repo) |
| 146 | +uv run --group dev python -m agent_session_tools.eval gold \ |
| 147 | + --db ~/.local/share/studyloop/eval-clones/lexical-or-fallback-20260915/sessions.db \ |
| 148 | + --arms mcp,mcp:or_first_filtered,mcp:and_first_unfiltered,mcp:or_only_unfiltered,mcp:and_then_prose_or \ |
| 149 | + --rows 10 --k 5 \ |
| 150 | + --out ~/.local/share/studyloop/eval-clones/lexical-or-fallback-20260915/or-fallback-dev-2026-09-15.raw.json |
| 151 | +
|
| 152 | +# the verdict is computed from the receipt by code, not read off by eye |
| 153 | +uv run --group dev python -m agent_session_tools.eval lexical-verdict \ |
| 154 | + --receipt ~/.local/share/studyloop/eval-clones/lexical-or-fallback-20260915/or-fallback-dev-2026-09-15.raw.json \ |
| 155 | + --candidate mcp:and_then_prose_or --control mcp \ |
| 156 | + --out docs/architecture/session-memory/receipts/lexical/or-fallback-dev-2026-09-15.json |
| 157 | +``` |
| 158 | + |
| 159 | +- The committed receipt `receipts/lexical/or-fallback-dev-2026-09-15.json` carries every per-arm |
| 160 | + metric block, every per-item row (`ranked`, `hit`, `rr`, `rank`, `error_kind`, latency) and every |
| 161 | + comparison from the raw receipt, plus a `verdict` block naming each clause and its truth value. |
| 162 | + Digests inside it are written in `sha256:<hex>` notation and the commit as `git:<sha>`: the |
| 163 | + repository's `detect-secrets` hook flags any bare quoted hex string (a 16-character prefix |
| 164 | + included — tested before this was written), and a prefixed digest is both hook-clean and |
| 165 | + verifiable in full. The raw receipt's own sha256 is recorded in the `.md` reading. |
| 166 | +- `receipts/lexical/or-fallback-dev-2026-09-15.md` is the reading: the per-arm table, the CI95 for |
| 167 | + the deciding pair, an explicit `adopt: true|false` line and the clause(s) that decided it. |
| 168 | +- Numbers in both files come from the commands above and nowhere else. If the harness cannot run |
| 169 | + against the clone, the exact error is recorded and the stream stops; nothing is estimated. |
| 170 | + |
| 171 | +## Not measured here, said up front |
| 172 | + |
| 173 | +- SEALED confirmation: spent; not available (D-12). |
| 174 | +- Learner benefit: a ranking measurement says nothing about learning (D-16 wording applies). |
| 175 | +- The `hybrid` mode: the candidate changes the lexical arm only; a fused effect is out of scope. |
| 176 | +- The 91-item ruler at a 61-item ceiling has wide intervals; a null result here is "not |
| 177 | + established on DEV", not "no effect". |
0 commit comments