# GATE 3 PRE-ANSWERED — DO NOT ASK AGAIN

Spec folder: `.opencode/specs/system-spec-kit/026-graph-and-context-optimization/014-local-embeddings-setup-a/022-local-llm-legacy-remediation`
Choice: A) Use existing spec folder
Reason: This IS the active remediation packet. Its plan.md authorizes the file edits below.

PROCEED DIRECTLY. Never emit the A/B/C/D/E spec-folder question. Never bail on Gate 3.

Also: --approval-policy=never is set. Do NOT ask the user any clarifying questions. Fall back to the safest interpretation if a detail is ambiguous.

---

# Batch 3 — Hardcoded sqlite paths -> profile-derived resolution

CONTEXT:
Packet 014 changed the Memory MCP sqlite filename from a generic `context-index.sqlite` to a profile-keyed scheme: `context-index__<provider>__<model-with-slashes-as-underscores>__<dims>__<dtype>.sqlite`. Several eval/checkpoint/cleanup/setup scripts still hardcode the OLD generic filename or the OLD Voyage 1024 profile, so they silently target a deleted/wrong sqlite file. Replace with active-profile resolution via the existing `shared/embeddings/profile.ts` helpers (look for `resolveCanonicalDbPath`, `getDbPathForProfile`, `getCurrentProfile`, or similar — read profile.ts first to find the right helper name).

CANONICAL DEFAULTS:
- Default profile (no env override): hf-local + onnx-community/embeddinggemma-300m-ONNX + 768 + q8
  -> derived filename: `context-index__hf-local__onnx-community_embeddinggemma-300m-onnx__768__q8.sqlite`

ALLOWED WRITE PATHS (touch ONLY these files):
- `.opencode/skills/system-spec-kit/shared/embeddings/profile.ts` (line ~79 legacy generic branch — fix the fall-through)
- `.opencode/skills/system-spec-kit/mcp_server/lib/eval/memory-state-baseline.ts` (line ~65)
- `.opencode/skills/system-spec-kit/scripts/evals/map-ground-truth-ids.ts` (line ~34)
- `.opencode/skills/system-spec-kit/scripts/evals/run-ablation.ts` (line ~50)
- `.opencode/skills/system-spec-kit/scripts/evals/run-bm25-baseline.ts` (line ~39)
- `.opencode/skills/system-spec-kit/scripts/memory/cleanup-index-scope-violations.ts` (line ~77 Voyage 1024 hardcode)
- `.opencode/skills/system-spec-kit/mcp_server/scripts/migrations/create-checkpoint.ts` (line ~37)
- `.opencode/skills/system-spec-kit/mcp_server/scripts/migrations/restore-checkpoint.ts` (line ~36)
- `.opencode/skills/system-spec-kit/scripts/setup/install.sh` (line ~253 local_fallback_db_path probe)

PRE-EDIT EXPLORATION (read first):
1. Read `.opencode/skills/system-spec-kit/shared/embeddings/profile.ts` end-to-end to find the right exported helper(s) for resolving the active profile DB path. Look for functions named like `resolveDbPathFromProfile`, `getActiveProfileDbPath`, `getDbPathForActiveProfile`, `computeProfileDbPath`, etc.
2. If no clean helper exists, add a minimal one to `profile.ts` named `resolveActiveProfileDbPath(profile?: EmbeddingProfile, dbDir?: string): string` that:
   - Takes an optional explicit profile (defaults to the current active profile from env)
   - Takes an optional dbDir override (defaults to the canonical `mcp_server/database/` resolved from cwd or env)
   - Returns the full filename-keyed sqlite path
3. Re-use this helper across all 9 call sites listed above.

BANNED OPERATIONS (RM-8 Layer 1):
- NO rm, NO rm -rf, NO git rm, NO mv, NO sed -i, NO rmdir, NO find ... -delete
- NO git add, NO git commit, NO git push, NO branch changes
- NO writes outside ALLOWED WRITE PATHS
- DO NOT touch the 021 review packet or the 022 remediation packet
- DO NOT touch any test fixtures or vitest files (batch 5 territory)
- DO NOT touch factory.ts or provider implementations (Q1=A — code is correct)
- If you detect a need to mutate outside scope, RECORD it as "SCOPE_VIOLATION: <path>: <why>" instead of executing

REQUIREMENTS:

(A) For each script call site, replace the literal `context-index.sqlite` (or `context-index__voyage__voyage-4__1024.sqlite`) string with a call to the resolver helper.

(B) For the `cleanup-index-scope-violations.ts:77` Voyage hardcode: change to require an explicit `--db` CLI argument (or accept `process.env.MEMORY_DB_PATH`). Print a clear error if the user invokes it without specifying.

(C) For `install.sh:253` (`local_fallback_db_path` shell probe): change to print the derived path or accept an env override. The probe is currently:
   `local local_fallback_db_path="${canonical_db_dir}/context-index.sqlite"`
   Replace with one of:
   - Shell out to Node to compute the derived path via `node -e "console.log(require('./shared/embeddings/profile.js').resolveActiveProfileDbPath())"` (or whatever interface exists)
   - OR remove the literal-named probe and replace with a directory-existence check that lists existing context-index*.sqlite files

(D) Preserve all imports + exports + existing function signatures unless adding the new helper. The new helper goes at the END of profile.ts (no reordering of existing code).

ACCEPTANCE CRITERIA (you should verify before reporting):
- `grep -rn "'context-index\\.sqlite'\\|\"context-index\\.sqlite\"" .opencode/skills/system-spec-kit/{scripts,mcp_server,shared}` returns 0 hits
- `grep -rn "context-index__voyage__voyage-4__1024" .opencode/skills/system-spec-kit/{scripts,mcp_server,shared}` returns 0 hits

AFTER ALL EDITS, output:

```
BATCH 3 REPORT
File: <path> — <N edits> — <one-line summary>
...
New helpers added: <list>
Scope violations: <none | list>
Blocked writes: <none | list>
Total edits: <N>
```

Do NOT commit. Do NOT run git. Stop after writing the report.
