fix(recall): close SessionStart keyword echo chamber + add hook-path token budget (v0.3.1)#32
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
hooks/session-start.mjsusedprojectNameFromCwd(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 inflatedaccess_counton noise.GET /memory/queryand the SessionStart hook had no token budget — a single 5KB row could silently consume ~1500 tokens per session start.Changes
src/core/token-budget.ts: newapplyRowBudget()(CJK-aware per-row + cumulative cap)src/api/routes.ts:GET /memory/querynow accepts optionalmaxTokens; touches only budget-emitted rowsGET /memory/startup?project=&limit=&maxTokens=&q=<fallback>endpointsrc/core/database.ts: newgetStartupMemories(projectId, limit, fallbackKeyword?)— 3-tier selection (cold project-scoped → recent-confidence fill → FTS fallback)hooks/session-start.mjs: env-flag dispatch viaCCRECALL_SESSION_START_STRATEGY(legacydefault |startup-v1|off); startup-v1 writes JSONL telemetry to~/.ccrecall/startup-recall.log.jsonlVerification
pnpm vitest run— 578/578 passing (baseline 562 + 16 new)pnpm buildclean/memory/startupreturns 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.~/.ccrecall/ccrecall.db.pre-startup-v1.bak(51 MB) created before any access_count changes.CCRECALL_SESSION_START_STRATEGY=startup-v1, observe~/.ccrecall/startup-recall.log.jsonldaily, evaluate at 5/20 14d gate.Test plan
applyRowBudgetunit tests — pass-through, per-row truncation, cumulative drop, field preservation, empty input, budget-exceeded-by-first-row/memory/querymaxTokens integration test — truncation + dropped rows not touched/memory/startupintegration tests — surfaces memory NOT containing project name; rejects missing project paramMigration
No schema migration. Existing hooks continue to work unchanged. New strategy is opt-in via env var only — default
legacypreserves v0.3.0 observation period.Rollback
CCRECALL_SESSION_START_STRATEGY=legacy(oroff) reverts behavior without redeploy. DB backup retains pre-fix baseline foraccess_countcomparison if 5/20 gate evaluates negative.🤖 Generated with Claude Code