From eebe12b917158f5e3d08d55135af20782b1d4fe3 Mon Sep 17 00:00:00 2001 From: Thomas Jung Date: Sat, 21 Nov 2026 04:53:01 -0500 Subject: [PATCH 01/33] docs(navigator): design + plan for #3112 search count-guard fix --- ...-11-21-navigator-search-count-guard-fix.md | 153 ++++++++++++++ ...navigator-search-count-guard-fix-design.md | 186 ++++++++++++++++++ 2 files changed, 339 insertions(+) create mode 100644 docs/superpowers/plans/2026-11-21-navigator-search-count-guard-fix.md create mode 100644 docs/superpowers/specs/2026-11-21-navigator-search-count-guard-fix-design.md diff --git a/docs/superpowers/plans/2026-11-21-navigator-search-count-guard-fix.md b/docs/superpowers/plans/2026-11-21-navigator-search-count-guard-fix.md new file mode 100644 index 000000000..c0b3cd340 --- /dev/null +++ b/docs/superpowers/plans/2026-11-21-navigator-search-count-guard-fix.md @@ -0,0 +1,153 @@ +# Navigator Search Count-Guard Fix + Pinned Filters — Implementation Plan + +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Make a matched mission/group actually render in navigator search results (fix #3112, which PR #3123 failed to fix), and pin selected filters above each facet list. + +**Architecture:** One root cause — the `before('READ', SearchableItems)` hook bails on `$count=true` (`srv/search-service.js:283`), which the navigator always sends, so the word-boundary filter + rank never run on the live cards query. Fix: apply the filter on all reads; gate only the `_searchRank` column+orderBy behind `!sel.count`. Plus a client-side per-column pinned selected-chips block in the navigator Vue island. + +**Tech Stack:** CAP Node.js (`@sap/cds` 10.2.2), Vitest + in-memory SQLite (unit), CAP OData over SQLite, Vue 3 island (`hugo-apps/`), Playwright (e2e). + +**Spec:** `docs/superpowers/specs/2026-11-21-navigator-search-count-guard-fix-design.md` + +## Global Constraints + +- Branch from fresh `origin/DEV`; PR targets `DEV` (not `main` — no main-hotfix path). Already on branch `worktree-3112-navigator-search-count-guard` off `origin/DEV`. +- Never change match semantics (word-boundary tokenization, stopwords, KG/community rerank). The fix is about *when* the filter/rank run, not *what* matches. +- Anti-goal: never push `_searchRank` onto a `COUNT(*)` projection (the guard's original real concern). +- CDS/API work: verify against cds-mcp before changing CDS; no raw SQL outside the existing `applyWordBoundarySearch`/`attachSearchRank` string-built clauses (which are pre-existing and out of scope to rewrite). +- Fresh worktree: `srv/lib/_shared/mcp.bundle.mjs` must exist before unit tests run — generate with `node scripts/bundle-shared.cjs` (already run this session; re-run after a clean). +- Full unit suite is hook-blocked locally; run scoped: `npm test -- ` / `npx vitest run `. + +## Review Focus + +- **Count path with no `$search`** (`$count=true` alone): must still short-circuit via the phrase guard and return the full-corpus count unchanged — pinned by Task 1 test "plain $count unaffected". +- **Internal `srv.run(SELECT…search…)` callers with no `sel.columns`** (MCP `search_tutorials`, semantic search): `attachSearchRank` self-bails at `:232`; the guard change must not break them — pinned by Task 1 test "internal search-without-columns still filters". +- **getFacets vs pager count agreement** after the fix (both should be the word-boundary set): a residual off-by-set would show a lozenge the grid can't fill — pinned by Task 1 test "count agrees with getFacets totalCount". +- **e2e self-skip masking the bug**: the existing G2 test uses `SAP` and skips if the lozenge is 0, so it can stay green before and after — Task 3 switches it to `Device` and asserts the pre→post flip. +- **Pinned-chip removal wiring**: clicking a pinned chip must clear exactly that selection (not toggle a neighbor) — pinned by Task 2 test "removing a pinned chip clears only that filter". + +--- + +## Task 1: Server — split the count guard so search filter + rank apply on the cards query + +**Files:** +- Modify: `srv/search-service.js:279-323` (the `before('READ', SearchableItems)` hook) +- Test: `test/search-service.test.js` (add cases to the existing `describe('SearchableItems'…)` block) + +**Interfaces:** +- Consumes: existing `applyWordBoundarySearch(query, term) -> string[]` (`:137`), `attachSearchRank(query, tokens, kgFragment?, communityFragment?)` (`:191`, self-bails when `!sel.columns`). +- Produces: nothing new; behavior change only. After this task, a `$search`+`$count=true` read returns the word-boundary-filtered count and (on the data read) rank-ordered rows with `searchScore` populated. + +- [ ] **Step 1: Write the failing tests** in `test/search-service.test.js` (SearchableItems block). Use the existing fixtures (`search-t2` cap-getting-started, `search-m1` full-stack-mission both match `CAP`; `search-mprobe` mission matches `missionprobe`): + +```js +it('$search + $count=true returns the word-boundary-filtered count (not 0, not whole corpus)', async () => { + const { data } = await project.get('/search/SearchableItems?$search=CAP&$count=true&$top=48'); + const slugs = data.value.map(i => i.slug); + expect(data['@odata.count']).toBeGreaterThan(0); + expect(slugs).toContain('cap-getting-started'); + expect(slugs).toContain('full-stack-mission'); + expect(slugs).not.toContain('hana-cloud-setup'); // no CAP word → filter really applied +}); + +it('$search + $count=true ranks a matching MISSION ahead of tutorial body-only matches', async () => { + // missionprobe: mission body-match (2+2 type-boost=4) must out-rank tutorial body-only (2). + const { data } = await project.get('/search/SearchableItems?$search=missionprobe&$count=true&$top=48'); + const idx = data.value.findIndex(i => i.slug === 'missionprobe-mission'); + expect(idx).toBe(0); + expect(data.value[0].searchScore).toBeGreaterThanOrEqual(4); +}); + +it('plain $count=true with no $search is unaffected (full corpus count)', async () => { + const { data } = await project.get('/search/SearchableItems?$count=true'); + expect(data['@odata.count']).toBeGreaterThanOrEqual(5); +}); +``` + +- [ ] **Step 2: Run tests to verify they FAIL** — Run: `npx vitest run test/search-service.test.js -t "count"`. Expected: the first two FAIL on current code (count=0 / mission not at idx 0, because the guard bails); the third PASSES already. + +- [ ] **Step 3: Implement the guard split** in `srv/search-service.js:279-323`. Change `if (!sel || sel.count) return;` (`:283`) to `if (!sel) return;`. Leave the phrase guard (`:284-287`) and `applyWordBoundarySearch` call (`:288-289`) unconditional. Wrap the KG/community fragment block (`:291-320`) **and** the `attachSearchRank(...)` call (`:322`) in a single `if (!sel.count) { … }` so the rank column/orderBy and the KG work run only on the data read, never the count projection. Update the `:281-282` comment to describe the split (filter always; rank only when not a count projection). + +- [ ] **Step 4: Run tests to verify they PASS** — Run: `npx vitest run test/search-service.test.js -t "count"`. Expected: all three PASS. + +- [ ] **Step 5: Run the full search-service suite for regressions** — Run: `npx vitest run test/search-service.test.js`. Expected: all green (existing `$search`, pagination, bodyText, word-boundary cases still pass). + +- [ ] **Step 6: Commit** + +```bash +git add srv/search-service.js test/search-service.test.js +git commit -m "fix(search): apply word-boundary filter+rank on \$count reads so missions surface (#3112)" +``` + +--- + +## Task 2: Client — per-column pinned selected-filter chips above each facet list + +**Files:** +- Modify: `hugo-apps/src/navigator/TutorialNavigator.vue` (filter section `:391-476`) +- Modify: `hugo/assets/css/home.css` (add `.filter-selected-pins`) +- Test: `hugo-apps/src/navigator/__tests__/TutorialNavigator.*` if present, else add a focused Vitest component test alongside the island (check for existing test dir first). + +**Interfaces:** +- Consumes: existing reactive `filters.topics/products/levels/types` arrays and the toggles `toggleFilter(dimension, value)` / `toggleProduct(value)` / `isProductSelected(value)` from `useNavigatorFilters.ts` (`:630-646`); the `ordered*` computeds (`:208-227`) and `partitionSelected` stay as-is (keep the in-list float). +- Produces: a `.filter-selected-pins` block per column; no new composable API. + +- [ ] **Step 1: Write the failing component test** asserting that when a topic/product/level/type is selected, a chip with its label renders in the `.filter-selected-pins` block for that column, and clicking the chip's remove control calls the matching toggle so the selection clears. (If no island component-test harness exists, add a minimal Vitest + `@vue/test-utils` mount test; name it `renders and removes pinned selected filter chips`.) + +- [ ] **Step 2: Run test to verify it FAILS** — Run: `npm --prefix hugo-apps run test -- TutorialNavigator` (or the discovered test command). Expected: FAIL (no `.filter-selected-pins` element). + +- [ ] **Step 3: Implement the pinned block.** In `TutorialNavigator.vue`, above each `.filter-list` in the four facet columns (`:394-460`), add a `.filter-selected-pins` block that `v-for`s over the selected values for that dimension (derive from `filters.*`), rendering a removable chip per selection whose click invokes the existing toggle for that dimension. Keep the existing in-list `ordered*`/`partitionSelected` float untouched. Add `.filter-selected-pins` + chip styles in `home.css` using existing Horizon token variables; do not change the `.filter-list` `max-height`. + +- [ ] **Step 4: Run test to verify it PASSES** — Run the same command. Expected: PASS. + +- [ ] **Step 5: Typecheck/lint the island** — Run: `npm --prefix hugo-apps run build` (or `lint`/`typecheck` if faster). Expected: no errors. + +- [ ] **Step 6: Commit** + +```bash +git add hugo-apps/src/navigator/TutorialNavigator.vue hugo/assets/css/home.css hugo-apps/src/navigator/__tests__/ +git commit -m "feat(navigator): pin selected filters above each facet list (#3112)" +``` + +--- + +## Task 3: e2e — make the G2 mission-search test actually exercise the dropped-mission path + +**Files:** +- Modify: `test/e2e/navigator-search-filter.spec.ts:106-173` (G2 test) + +**Interfaces:** +- Consumes: deployed navigator (post-fix on DEV). No code interface. + +- [ ] **Step 1: Change the search term and tighten the assertion.** Replace the `searchInput.fill('SAP')` (`:140`) with `'Device'` — the proven repro term that matches a mission buried past `$top=48` pre-fix and ranked onto page 1 post-fix. Keep the "lozenge ≥ 1 ⇒ mission card MUST be visible" invariant. Add a comment citing #3112 and the design doc. Do **not** widen the graceful-skip; if `Device` yields a 0 lozenge on the target env the test should still skip (content varies) — but on DEV/PROD with the Devtoberfest mission present it will assert. + +- [ ] **Step 2: Verify the test flips.** Against the current PROD (pre-fix) the assertion fails; against a post-fix DEV deploy it passes. Document the manual check: `SMOKE_BASE_URL= npx playwright test test/e2e/navigator-search-filter.spec.ts -g "G2"`. (This runs post-deploy, not in CI PRs — note it in the PR description.) + +- [ ] **Step 3: Commit** + +```bash +git add test/e2e/navigator-search-filter.spec.ts +git commit -m "test(e2e): search 'Device' so G2 exercises the buried-mission path (#3112)" +``` + +--- + +## Task 4: Hybrid confirmation + PR + +**Files:** none (verification + PR). + +- [ ] **Step 1: Hybrid assertion (real HANA)** — if a hybrid search test exists (`test/*hybrid*`/`npm run test:hybrid`), add/confirm an assertion that `$search=Device&$top=48&$count=true` returns `@odata.count` 56 (or current PROD value) with a MISSION at `searchScore` 7 on page 1, and that `getFacets(search='Device')` totalCount equals that `@odata.count`. Requires `cf login`. If no hybrid harness fits cheaply, record the two-row curl proof from the design doc against the DEV deploy in the PR instead. + +- [ ] **Step 2: Full scoped verification** — Run: `npx vitest run test/search-service.test.js` and the island test. Expected: green. Re-generate the shared bundle first if the worktree was cleaned (`node scripts/bundle-shared.cjs`). + +- [ ] **Step 3: Open the PR to DEV** — `gh pr create --base DEV` with a body covering: the real root cause (guard shadowing #3123's boost), the proof table, what #3123 got wrong, the pinned-filters default, and the post-deploy e2e/hybrid steps. Link #3112. + +--- + +## Notes carried from the design (decided, not open) + +- One PR for both server fix and pinned filters (Tom, 2026-11-21). +- Keep the in-list `partitionSelected` float (dropping it breaks the existing G1 e2e). +- getFacets product-mirroring already works — no re-fix, locked by Task 1's count-agreement test. +- Q1 spike **already run this session**: the bug is unit-reproducible on SQLite (`$search=CAP&$count=true` → count 0 on buggy code), so the count assertion lives in the unit test (Task 1), with hybrid as confirmation (Task 4). diff --git a/docs/superpowers/specs/2026-11-21-navigator-search-count-guard-fix-design.md b/docs/superpowers/specs/2026-11-21-navigator-search-count-guard-fix-design.md new file mode 100644 index 000000000..a8cb9e57c --- /dev/null +++ b/docs/superpowers/specs/2026-11-21-navigator-search-count-guard-fix-design.md @@ -0,0 +1,186 @@ +# Navigator search: count-guard fix + pinned selected filters (#3112 follow-up) + +**Date:** 2026-11-21 +**Issue:** sap-tutorials/tutorials-ims#3112 (follow-up comment 2026-11-18 by `thecodester`) +**Supersedes the search half of:** `2026-11-12-navigator-search-filter-improvements-design.md` (PR #3123, merged + promoted to `main` 2026-11-12) — whose G2 fix did not work in PROD. + +## Intent and why + +Issue #3112's follow-up (2026-11-18) reports that search **still** drops missions in +production, five days after PR #3123 ("fully delivered in production and live") merged and +promoted. Tom's repro: on `/tutorial-navigator/?...&q=Device` the mission lozenge shows a +count but no mission card renders. He also restated the filter ask: selected filters should +be visible **outside** the scrolling facet box, not merely floated to the top inside it. He +confirmed the completed-tutorials filter is good and de-prioritized the filter-visibility ask +("not really that important"), but it is cheap and worth doing. + +The point of this work: make a matched mission actually render in the search grid, and keep +the selected filters in view. + +## Root cause (proven, not theorized) + +The #3123 design attributed the dropped mission to three things — low mission rank, missing +search-mode pagination, and `getFacets` ignoring `$filter` — and fixed all three. **None was +the cause.** The real cause is a single guard: + +``` +srv/search-service.js:283 if (!sel || sel.count) return; // before('READ', SearchableItems) +``` + +The island's cards query **always** sends `$count=true` (`hugo-apps/src/navigator/useSearch.ts:201-212`; +no `$select`, no `$orderby`). In CAP 10.2.2 the OData adapter dispatches `$count=true` as a +**single** `READ` with `SELECT.count` truthy on the same query object (`@sap/cds` `libx/odata/middleware/read.js:262`, +grammar `:906`); the db-service derives the count by cloning that **already-processed** query +(`@cap-js/db-service` `SQLService.js:170-172, 336-361`). So the guard makes the `before('READ')` +hook bail on **every** island search, which means: + +- `applyWordBoundarySearch` never runs → `sel.search` survives → CAP's default `$search` + translation fires (broad, wrong match-set: **count 91**, not the word-boundary **56**). +- `attachSearchRank` never runs → no `_searchRank` column, no `_searchRank DESC` orderBy → + **raw UNION-ALL view order**; `searchScore` is `null` on every row. +- The mission that would rank **#0 (score 7 = 3 title + 2 desc + 2 type-boost)** is buried + behind ~80 concepts, past the `$top=48` page → "counted but never shown." + +The #3123 rank boost (`search-service.js:214`) is correct but **dead code** on the live path, +because this guard shadows it. + +### Proof (PROD, developers.sap.com, 2026-11-21) + +| Request | Count | Order | Mission | +|---|---|---|---| +| `$search=Device&$top=48&$count=true` (island shape) | 91 | raw (searchScore null ×48) | absent | +| `$search=Device&$top=48` (no `$count` → hook runs) | 56 | ranked | **#0, score 7** | + +### What is NOT broken (verified, so do not "re-fix") + +- **getFacets product-mirroring already works.** `getFacets(search='Device', products=['software-product>sap-build-apps'])` + returns totalCount 1 / TUTORIAL:1 / **no mission** — agrees with the items query under the + same filter (count 1, TUTORIAL only). #3123 wired `products`/`isNew` into `getFacets` + (`search-service.js:559-564`) and it functions. Bug "B" (lozenge ≠ grid under a product + filter) does **not** reproduce today; Tom's 2026-11-18 screenshot predates that wiring + reaching PROD or shows a stale lozenge. We lock it with a regression test, not a new fix. + +## Goals / non-goals / anti-goals + +**Goals** +- G1 — A mission/group that matches a free-text query **renders in the grid**, at its ranked + position, on the island's real request shape (`$search=…&$top=48&$skip=0&$count=true`). +- G2 — The `@odata.count` returned for that request reflects the **word-boundary** match-set + (the 56-set), not CAP's default-`$search` 91-set, so the pager total is honest. +- G3 — Selected facets are visible **outside** the capped scroll box, in all applicable + columns, removed by clicking the pinned chip. + +**Non-goals** +- Changing what counts as a match (the word-boundary tokenization, stopwords, KG/community + rerank). Match semantics stay identical. +- Re-touching `getFacets` filtering (already correct). +- The homepage `/` flip or `/browse/` (shares the composable but not this bug). + +**Anti-goals** +- Do **not** add `_searchRank` to a `COUNT(*)` projection — the guard's original concern + (comment `search-service.js:281-282`) is real: a count-clone keeps `columns`, so a pushed + rank column could break the count SQL. +- Do not make the count honest in the wrong direction (render a non-matching mission, or + double-count). + +## Constraints + +- CAP `@sap/cds` 10.2.2; UNION-ALL view `SearchableItems` (`db/views.cds:98-166`). +- `searchScore` is a virtual (`srv/search-service.cds:61`) populated only by the after-hook + from `_searchRank`. +- Must not regress `getFacets`, the MCP `search_tutorials`/semantic-search paths, or internal + `srv.run(SELECT…search…)` callers (which arrive with no `sel.columns` → `attachSearchRank` + already self-bails at `:232`). +- Full suite is hook-blocked locally; scope tests with `npm test -- `. e2e is post-deploy + only (`test/e2e/navigator-search-filter.spec.ts`). + +## Approaches considered + +1. **Surgical guard split (CHOSEN).** Apply `applyWordBoundarySearch` (the *filter*) on all + reads, including when `sel.count` is truthy; gate only `attachSearchRank` (the *column + + orderBy*) behind `!sel.count`. The count-clone keeps the `.where` from the filter (so count + = 56, honest) and drops `orderBy`/`limit`; the data read gets both filter and rank. Smallest + change, preserves the guard's real intent (no rank column on a count projection), fixes G1+G2 + together. + - *Trade-off:* relies on CAP keeping `where` (not `orderBy`) in the count-clone — verified in + SQLService.js and reproducible via the no-`$count` repro. A future CAP change to count-clone + behavior would need a re-check; pinned by a hybrid test. + +2. **Remove the guard outright.** Let rewrite + rank run on every read. Simpler diff, but pushes + `_searchRank` into `sel.columns` on the count path — exactly what the "MANDATORY" comment + warns against; risks a `COUNT(*)`-projection SQL error on HANA. Rejected. + +3. **Client-side: stop sending `$count`, derive total from a separate count call.** Moves the + fix to `useSearch.ts`. Larger blast radius (pager math, two round-trips), and leaves the + server hook wrong for any other `$count` caller. Rejected. + +**Chosen: Approach 1.** + +## The how (decided) + +### Server — `srv/search-service.js`, `before('READ', SearchableItems)` (~:279-323) + +- Replace `if (!sel || sel.count) return;` with `if (!sel) return;`. The existing phrase guard + (`:285-287`) already short-circuits any read with no usable `$search` phrase, so a plain + `$count` with no search term stays untouched. +- Keep `applyWordBoundarySearch(req.query, phrase)` unconditional (runs for count + data). +- Wrap the KG/community fragment computation (`:291-320`) and the `attachSearchRank(…)` call + (`:322`) in `if (!sel.count) { … }`, so the rank column + orderBy are added **only** on the + data read, never the count projection. Skipping KG/community on the count path is also a free + latency win. +- `after('READ')` (`:251-268`) is unchanged — on the count path it early-returns on the numeric + result (`:253`). + +### Server — no change to `getFacets` (already correct). Add a regression test only. + +### Client — `hugo-apps/src/navigator/*` + `hugo/assets/css/home.css` + +- G3 pinned selected filters: render a **per-column** selected-chips block **above** each + `.filter-list` (inside `