Skip to content

fix(recall): close SessionStart keyword echo chamber + add hook-path token budget (v0.3.1)#32

Merged
tznthou merged 3 commits into
mainfrom
fix/session-start-keyword-echo
May 13, 2026
Merged

fix(recall): close SessionStart keyword echo chamber + add hook-path token budget (v0.3.1)#32
tznthou merged 3 commits into
mainfrom
fix/session-start-keyword-echo

Conversation

@tznthou
Copy link
Copy Markdown
Owner

@tznthou tznthou commented May 13, 2026

Summary

  • Root cause (memory #119 / tz-find 2026-05-13): hooks/session-start.mjs used projectNameFromCwd(cwd) as the sole FTS5 keyword, so only memories whose content literally contained the project name could surface. Manual atomic-knowledge entries stayed 100% cold while project-name-quoting prompt fragments were repeatedly injected and inflated access_count on noise.
  • Latent bomb fixed: GET /memory/query and the SessionStart hook had no token budget — a single 5KB row could silently consume ~1500 tokens per session start.
  • Approach: pi-plan cross-provider (Codex + Gemini) consensus on B1 daemon-side budget + A3-lite 3-tier selection. plan-critic --deep applied (5 WARN → minimal-version cut).

Changes

  • src/core/token-budget.ts: new applyRowBudget() (CJK-aware per-row + cumulative cap)
  • src/api/routes.ts:
    • GET /memory/query now accepts optional maxTokens; touches only budget-emitted rows
    • new GET /memory/startup?project=&limit=&maxTokens=&q=<fallback> endpoint
  • src/core/database.ts: new getStartupMemories(projectId, limit, fallbackKeyword?) — 3-tier selection (cold project-scoped → recent-confidence fill → FTS fallback)
  • hooks/session-start.mjs: env-flag dispatch via CCRECALL_SESSION_START_STRATEGY (legacy default | startup-v1 | off); startup-v1 writes JSONL telemetry to ~/.ccrecall/startup-recall.log.jsonl

Verification

  • pnpm vitest run578/578 passing (baseline 562 + 16 new)
  • pnpm build clean
  • Local smoke on alt port (7750): /memory/startup returns 3 manual atomic-knowledge entries that do not contain "ccRecall" (#118 「WebFetch 查 Claude Code 版本」, #116 「漸進披露探索法」, #115 「私有 skill 名禁入公開產出」). Token budget enforced (totalTokenEstimate: 286 < 300), truncated: true.
  • DB backup ~/.ccrecall/ccrecall.db.pre-startup-v1.bak (51 MB) created before any access_count changes.
  • 7-day dogfood post-merge: set CCRECALL_SESSION_START_STRATEGY=startup-v1, observe ~/.ccrecall/startup-recall.log.jsonl daily, evaluate at 5/20 14d gate.

Test plan

  • applyRowBudget unit tests — pass-through, per-row truncation, cumulative drop, field preservation, empty input, budget-exceeded-by-first-row
  • /memory/query maxTokens integration test — truncation + dropped rows not touched
  • /memory/startup integration tests — surfaces memory NOT containing project name; rejects missing project param
  • Hook dispatch tests — legacy / startup-v1 / off routing; JSONL telemetry written on startup-v1

Migration

No schema migration. Existing hooks continue to work unchanged. New strategy is opt-in via env var only — default legacy preserves v0.3.0 observation period.

Rollback

CCRECALL_SESSION_START_STRATEGY=legacy (or off) reverts behavior without redeploy. DB backup retains pre-fix baseline for access_count comparison if 5/20 gate evaluates negative.

🤖 Generated with Claude Code

tznthou and others added 3 commits May 13, 2026 12:13
…token budget

Root cause (memory #119, pi-plan 2026-05-13): hooks/session-start.mjs queried
/memory/query with projectNameFromCwd(cwd) as the sole FTS5 keyword, so only
memories whose content literally contained the project name could surface.
Manual atomic-knowledge entries stayed cold while user-prompt fragments
quoting the project name were repeatedly injected, inflating access_count
on noise and producing inverted recall signal.

Changes:
- src/core/token-budget.ts: add applyRowBudget() helper (CJK-aware per-row
  truncation + cumulative cap)
- src/api/routes.ts: GET /memory/query accepts optional maxTokens; touches
  only budget-emitted rows so dropped rows do not poison access_count
- src/core/database.ts: add Database.getStartupMemories(projectId, limit,
  fallbackKeyword) with 3-tier selection — cold (access=0, exclude
  type=query) → recent-confidence fill → FTS fallback
- src/api/routes.ts: add GET /memory/startup endpoint wiring the above with
  full token budget protection
- hooks/session-start.mjs: dispatch on CCRECALL_SESSION_START_STRATEGY
  (legacy | startup-v1 | off, default legacy). startup-v1 writes JSONL
  telemetry (emittedIds, droppedCount) to ~/.ccrecall/startup-recall.log.jsonl

Default strategy remains legacy so the v0.3.0 observation period is not
disturbed. Set CCRECALL_SESSION_START_STRATEGY=startup-v1 to opt into the
new path; evaluate via JSONL telemetry by the 5/20 14d node.

Tests: 562 → 578 passing (16 new: applyRowBudget × 6, /memory/query
maxTokens × 1, /memory/startup × 2, hook env-flag dispatch × 3,
hook telemetry JSONL × 1, plus updated baseline).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
… inference

The `[key: string]: unknown` on BudgetRow forced TypeScript to widen R
to BudgetRow inside applyRowBudget, losing concrete field types (Memory.id
became unknown). Memory does not declare a string index signature so it
was structurally incompatible. Narrowing BudgetRow to `{ content: string }`
lets `R extends BudgetRow` preserve Memory's full shape.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…en budget

See CHANGELOG.md / CHANGELOG_ZH.md for details. No schema migration.
Default SessionStart strategy remains `legacy`; opt-in via
CCRECALL_SESSION_START_STRATEGY=startup-v1 to use the new retrieval path.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@tznthou tznthou merged commit 639aa64 into main May 13, 2026
0 of 3 checks passed
@tznthou tznthou deleted the fix/session-start-keyword-echo branch May 13, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant