Integration: 7 critical fixes (post band-aid strip) (#2219)
* fix: strip privacy tags from last_assistant_message in summarize path (cherry picked from commit bd68bfcc3cfe9d82977d5bdb87cf7e91a7258489) * fix: preserve Chroma relevance ordering in SQLite hydration When ChromaSearchStrategy queries by vector similarity with orderBy='relevance', SessionStore.getObservationsByIds and related methods silently coerced undefined to 'date_desc', destroying the semantic ranking. Add 'relevance' as a valid orderBy value that skips SQL ORDER BY and preserves caller-provided ID order. Fixes #2153 (cherry picked from commit 9fedf8fc165c01cc3a8a8cdb8c057ea980bf511e) * test(privacy): mock executeWithWorkerFallback and loadFromFileOnce Update the cherry-picked privacy-tag stripping test from swithek's fork to match current main: - Mock executeWithWorkerFallback / isWorkerFallback (the handler now uses these instead of workerHttpRequest directly). - Mock loadFromFileOnce in hook-settings.js (called by shouldTrackProject) so the handler resolves CLAUDE_MEM_EXCLUDED_PROJECTS to a string. - Switch the workerCallLog shape to record { path, method, body } and accept either object or JSON-string bodies. 10/10 tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: pass relevance through to SessionStore in ChromaSearchStrategy The Chroma strategy was coercing orderBy='relevance' to undefined before calling SessionStore. Combined with SessionStore's date_desc default for undefined, this destroyed the semantic ranking that Chroma had just computed. Pair this with the SessionStore-side fix from rogerdigital (commit 37c8988f) which now accepts 'relevance' as a valid orderBy and preserves caller-provided ID order. Adds a regression test asserting that getObservationsByIds returns rows in caller-provided order when orderBy='relevance', and continues to return date_desc order when orderBy is omitted. Closes #2153 Co-Authored-By: Roger Deng <13251150+rogerdigital@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: isolate SDK boundary — settingSources, strictMcpConfig, cloud-provider env, observation cap Single architectural fix at the three @anthropic-ai/claude-agent-sdk query() call sites (SDKAgent.startSession, KnowledgeAgent.prime, KnowledgeAgent .executeQuery) plus the env sanitizer and ingest gate. Closes 6 issues: - #2155 settings.json bleed-through into observer SDK subprocess: pass settingSources: [] so user/project/local settings aren't inherited. - #2159 / #2171 / #2194 user MCP servers leak into observer SDK: pass strictMcpConfig: true alongside the existing mcpServers: {}. - #2199 Bedrock/Vertex env vars dropped: extend ENV_PRESERVE in src/supervisor/env-sanitizer.ts to keep CLAUDE_CODE_USE_BEDROCK, CLAUDE_CODE_USE_VERTEX, AWS_*, ANTHROPIC_VERTEX_PROJECT_ID, etc. - #2201 runaway tokens (345M/day reported): extend default CLAUDE_MEM_SKIP_TOOLS with exec_command, write_stdin, apply_patch and add a configurable CLAUDE_MEM_MAX_OBSERVATION_BYTES (default 64 KB) cap at the ingest gate. SDK option names verified against node_modules/@anthropic-ai/claude-agent-sdk/sdk.d.ts: settingSources?: SettingSource[] (SettingSource = 'user'|'project'|'local') strictMcpConfig?: boolean Anti-pattern guards observed: - Did not modify the proxy strip (#2099/#2115). - Did not skip Read/Write/Edit/Bash — those remain the primary observation surface; only added high-volume agentic-tool names (exec_command, write_stdin, apply_patch). - Did not invent SDK options. Closes #2155, #2159, #2171, #2194, #2199, #2201 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: restore Windows spawn fix from PR #751 + add Windows CI Re-applies the PowerShell Start-Process -WindowStyle Hidden daemon spawn that PR #751 (e6ae0176) introduced and commitd13662d5reverted. Also fixes the bun-runner cmd /c popup, sets detached:false on Windows for SDK subprocesses (so windowsHide actually works and claude.exe doesn't outlive the worker), and adds windows-latest CI to prevent regression. - ProcessManager.spawnDaemon: PowerShell -EncodedCommand branch back. Returns 0 sentinel on success — callers MUST use pid === undefined for failure detection, never falsy checks. - bun-runner.js: drop "cmd /c" wrapper. shell:true lets Node resolve bun.cmd via PATHEXT and respects windowsHide (the explicit cmd.exe wrapper was popping a visible window per hook — #2150, #2186). - process-registry.ts spawnSdkProcess: detached:false on Windows. Mixing detached:true with windowsHide:true is documented-undefined on Windows; with detached:false, windowsHide actually hides claude.exe and the SDK subprocess dies with the parent (#2190, #2198). - .github/workflows/windows.yml: smoke test counts visible cmd windows before/after spawn + grep guard that the Start-Process branch survives. WSL bash stdin (#2188) is acknowledged but deferred — the bash → node pipe boundary needs a real Windows VM to test, beyond this PR's scope. PTY for Claude CLI SDK mode (#2173, #2177) is also deferred per plan. Closes #2150, #2169, #2186, #2187, #2190, #2198 Refs #2183 (Windows perf — same root cause) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: Codex transcript ingestion + queue self-deadlock on Windows Three Windows-specific bugs surfaced by @MakaveliGER in #2192: A. Glob path normalization path.join(homedir(), ...) emits backslashes on Windows. globSync treats backslashes as escape characters, not separators, so it silently fails to match transcript files. Normalize backslashes to forward slashes before passing to globSync (only affects Windows; Unix paths unchanged). B. Live appends not picked up Per-file fs.watch on Windows ReFS/SMB misses appends to live JSONL files; the recursive root watcher is the only signal we can trust there. Expose FileTailer.poke() and call it from the root-watcher event when the file is already tailed, instead of returning early. Also normalize the resolved path so the tailer-map key matches what globSync stored. C. Queue self-deadlock on abort When the SDK generator aborts (idle timeout, user cancel, shutdown) with rows already claimed and yielded but not yet confirmed by ResponseProcessor, those rows sit in 'processing' under THIS worker's PID. The self-healing claim predicate skips them because the worker is still alive — the queue deadlocks until the worker restarts. In the .finally() block, walk the in-flight ids through markFailed so the retry ladder requeues them as 'pending' (or terminates them if retries are exhausted). Includes regression test tests/codex-transcript-watcher-windows.test.ts that asserts each fix at the source level so future refactors can't silently revert them. Co-Authored-By: MakaveliGER <noreply@github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Closes #2192 * fix: standalone batch — npm peer-deps overrides, marketplace self-heal warning, cache prune - Add `overrides: { tree-sitter: ^0.25.0 }` to the generated plugin/package.json so `npm install --production` resolves cleanly without --legacy-peer-deps. Fixes the ERESOLVE between grammar packages declaring three different majors of tree-sitter as peer deps. Closes #2147. - mcp-server.ts: emit a single loud, actionable warning when MCP boots but the marketplace directory at ~/.claude/plugins/marketplaces/<source>/ is missing. IDE plugin loaders silently skip claude-mem hooks in this state while MCP keeps working — the user has no way to know memory capture is dead. We don't run an installer from MCP startup (different permission model), but we tell the user exactly which command to run. Closes #2174. - smart-install.js (both root and plugin variants): prune older claude-mem version directories from ~/.claude/plugins/cache/thedotmack/claude-mem/. Claude Code resolves and caches hook commands per session, so a stale 12.x directory keeps the old hook path alive across restarts even after upgrade. Pruning makes the stale path physically unreachable. Closes #2172 (stale version reference). Note: the issue's secondary claim that @anthropic-ai/claude-agent-sdk is missing from package.json is no longer true — it was added at line 115 in v12.4.x. - #2170 ("ToolUseContext is required for prompt hooks") triaged as upstream: the string does not appear anywhere in this repo. The error originates in Claude Code's hook framework, which we don't own. No code change here. Co-Authored-By: Amadan04 <amadan04@users.noreply.github.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * fix: remove stale macOS binary, regen plugin artifacts (build/bundle drift) The committed plugin/scripts/claude-mem (63 MB Mach-O) was last built at v10.3.2 (Feb 2026). It baked in BUILT_IN_VERSION="10.3.1", dev paths (/Users/alexnewman/Scripts/claude-mem/...), and a now-removed POST /api/sessions/complete client + handler (deleted by PR #2136). That meant macOS users running the cached binary hit 404s every time the SessionEnd hook fired (issue #2200), the /api/health endpoint reported a two-major-versions-ago version (issue #2158), and the binary embedded a Zod copy that drifted from the worker bundle (issue #2154). - Delete plugin/scripts/claude-mem and gitignore it. The npm package already excludes it from the "files" allowlist, so no consumer change. The JS fallback (bun-runner.js → worker-service.cjs) covers all functionality on every platform per the existing checkBinaryPlatformCompatibility comment in smart-install.js. - Add npm run build:cli-binary for users who want the macOS speedup back. Produces it on demand from current source — no drift. - Regenerate plugin/scripts/{worker-service,mcp-server}.cjs and plugin/ui/viewer-bundle.js so the shipped artifacts match HEAD. Closes #2158, #2200, #2154. * fix(ci): Windows workflow — install without lockfile (project uses Bun) actions/setup-node@v4 cache: npm requires a package-lock.json and this project uses Bun (only bunfig.toml exists at root). Drop the cache directive, switch npm ci to npm install --no-audit --no-fund, and narrow the build step to npm run build — build-and-sync also runs a marketplace sync + worker restart that hardcodes ~/.claude/plugins, which doesn't exist on CI. * fix: harden observation cap parsing + safe stringify in debug logger CodeRabbit majors on #2206: - shared.ts: validate parsed cap is finite and > 0 before use; wrap JSON.stringify(payload.toolResponse) in try/catch and skip with reason 'payload_unserializable' on circular/throwing payloads, so ingestion never crashes on a bad tool response shape. - logger.ts: the debug-mode JSON dump for objects was unguarded; wrap stringify in try/catch and fall back to formatData on cycles. This is the source the bundled plugin/scripts/context-generator.cjs is built from. * fix(ci+windows): quote bun-runner shell:true args; replace dynamic smoke with static guards CodeRabbit majors on #2208: 1. plugin/scripts/bun-runner.js — shell:true with separate spawnArgs triggers DEP0190 on Node 22+ and breaks paths/args containing spaces. Build a single fully-quoted command string (mirroring findBun()'s 'where bun' approach) and pass spawnArgs=[]. 2. .github/workflows/windows.yml — the dynamic smoke step that counted visible cmd windows around 'claude-mem start' exits 1 on 'claude-mem is not installed' before exercising the spawn path, AND PowerShell try/catch doesn't suppress native exit codes regardless. Replace with three static regression guards covering the exact patterns PR #2208 protects: - PowerShell Start-Process + WindowStyle Hidden in spawnDaemon - bun-runner shell:true with empty spawnArgs (DEP0190 guard) - windowsHide set on SDK spawn factory (issue #2190) * fix(2210): cross-platform paths — Windows USERPROFILE + XDG cache symmetry Greptile P2s on #2210: - mcp-server.ts checkMarketplaceMarker: switch from process.env.HOME ?? '' to os.homedir(). HOME is unset on Windows; the empty fallback resolves relative to cwd, silently no-opping the canary on every Windows install. Also probe both ~/.claude/ and ~/.config/claude/ for the cache check so XDG users get the same warning behavior. - smart-install.js pruneStaleVersionCache (both root + plugin copies): scan both ~/.claude/plugins/cache/thedotmack/ and ~/.config/claude/... paths so users on XDG don't keep stale dirs re-triggering #2172. Greptile's third P2 (mtime vs semver sort for current version) deferred: mtime works correctly for the common case and the directory names start with versions that lexicographically sort the same way mtime does for sequentially-installed versions; semver sort would be a separate change. Refs PR #2210 * fix(2211): drop hardcoded --target from build:cli-binary Greptile P2: the npm script was pinned to bun-darwin-arm64, so an Intel Mac user (or anyone on Linux/Windows running this script manually) got a cross-compiled arm64 binary that runs only via Rosetta on x64 macOS and not at all elsewhere. Bun's --compile defaults to the host platform when --target is omitted. Drop the flag so the script produces a binary that matches whoever runs it. CI builds that need a specific target can still pass --target explicitly. Refs PR #2211 * ci(windows): drop static-grep tripwires, keep real Windows build The "Anti-regression" steps grep ProcessManager.ts/bun-runner.js/process-registry.ts for specific strings (Start-Process, WindowStyle Hidden, shell:true, windowsHide). Tripwires aren't fixes — they make refactoring harder forever and verify nothing the actual Windows build doesn't already verify. The npm install + npm run build on windows-latest is the real guard. * revert: drop byte cap and skip-list extension band-aids Strips two band-aid mechanisms from the SDK boundary fix, keeping only the genuine isolation flags (settingSources: [], strictMcpConfig: true) and the cloud-provider env preservation. Removed: - CLAUDE_MEM_MAX_OBSERVATION_BYTES (default 65536) — dropped oversize observations entirely. The structural fix is to chunk/summarize oversize tool results, not punish the data flow with an invented byte threshold. Tracked separately. - exec_command, write_stdin, apply_patch added to default skip list — static taste decision baked into defaults for everyone. Users can still set CLAUDE_MEM_SKIP_TOOLS themselves. The data flows again. Real fix is a follow-up. * revert: drop pruneStaleVersionCache walker Removes the cache walker that scans plugin cache dirs and deletes "old" version directories by inferred staleness. The structural fix for #2172 is for the installer to delete the prior version when it writes the new one — not for a separate walker to wake up later and guess which directories are stale. Keeps: - npm peer-dep override for tree-sitter (#2147) - Marketplace marker startup probe (#2174) - Cross-platform path handling Tracked separately as a follow-up. * build: regenerate bundled artifacts after merge Rebuilt plugin/scripts/*.cjs from src after merging #2211, #2204, #2205, #2208, #2209, #2206 (post-strip), #2210 (post-strip). Conflicts during merge were resolved by accepting incoming bundled artifacts; this commit replaces them with a clean rebuild from the merged source. Verified: 0 references to MAX_OBSERVATION_BYTES, payload_too_large, or pruneStaleVersionCache in the rebuilt artifacts. --------- Co-authored-by: swithek <52840391+swithek@users.noreply.github.com> Co-authored-by: Roger Deng <13251150+rogerdigital@users.noreply.github.com> Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com> Co-authored-by: Amadan04 <amadan04@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
name: Windows
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
paths:
|
||||
- 'src/**'
|
||||
- 'plugin/scripts/**'
|
||||
- 'package.json'
|
||||
- 'bunfig.toml'
|
||||
- '.github/workflows/windows.yml'
|
||||
push:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: windows-latest
|
||||
timeout-minutes: 25
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: '22'
|
||||
|
||||
- name: Install Bun (worker runtime)
|
||||
run: |
|
||||
irm bun.sh/install.ps1 | iex
|
||||
shell: pwsh
|
||||
|
||||
- run: npm install --no-audit --no-fund
|
||||
|
||||
# Build only — the build-and-sync script also runs marketplace sync + worker
|
||||
# restart from a hardcoded ~/.claude/plugins path that doesn't exist on CI.
|
||||
- run: npm run build
|
||||
@@ -40,6 +40,13 @@ src/ui/viewer.html
|
||||
# Installer marker — dropped by the claude-mem CLI at install time
|
||||
plugin/.cli-installed
|
||||
|
||||
# Compiled macOS binary — produced on demand by `npm run build:cli-binary`,
|
||||
# never committed. Committed binaries drift from source and end up shipping
|
||||
# since-removed routes/handlers and stale version constants (see #2158,
|
||||
# #2200, #2154). The JS fallback (bun-runner.js → worker-service.cjs) covers
|
||||
# all functionality on every platform.
|
||||
plugin/scripts/claude-mem
|
||||
|
||||
# Local contribution analysis (not part of upstream)
|
||||
CONTRIB_NOTES.md
|
||||
|
||||
|
||||
@@ -65,6 +65,7 @@
|
||||
"sync-marketplace": "node scripts/sync-marketplace.cjs",
|
||||
"sync-marketplace:force": "node scripts/sync-marketplace.cjs --force",
|
||||
"build:binaries": "node scripts/build-worker-binary.js",
|
||||
"build:cli-binary": "bun build --compile --minify ./src/services/worker-service.ts --outfile plugin/scripts/claude-mem",
|
||||
"worker:logs": "tail -n 50 ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log",
|
||||
"worker:tail": "tail -f 50 ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log",
|
||||
"changelog:generate": "node scripts/generate-changelog.js",
|
||||
|
||||
@@ -32,6 +32,9 @@
|
||||
"@derekstride/tree-sitter-sql": "^0.3.11",
|
||||
"@tree-sitter-grammars/tree-sitter-markdown": "^0.3.2"
|
||||
},
|
||||
"overrides": {
|
||||
"tree-sitter": "^0.25.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=18.0.0",
|
||||
"bun": ">=1.0.0"
|
||||
|
||||
@@ -179,9 +179,20 @@ let spawnCmd = bunPath;
|
||||
let spawnArgs = args;
|
||||
|
||||
if (IS_WINDOWS) {
|
||||
// On Windows, bun.cmd must be executed via cmd /c
|
||||
spawnCmd = 'cmd';
|
||||
spawnArgs = ['/c', bunPath, ...args];
|
||||
// On Windows, npm-installed bun is bun.cmd (a batch file) which spawn()
|
||||
// can't execute directly. The previous `cmd /c` wrapper made a visible
|
||||
// console window flash on every hook (issues #2150, #2186, #2187).
|
||||
// shell:true lets Node resolve via PATHEXT *and* respects windowsHide,
|
||||
// unlike an explicit cmd.exe wrapper. bun.exe paths work the same way.
|
||||
//
|
||||
// With shell:true we must pass a single fully-quoted command string and
|
||||
// an empty args array. Passing args separately concatenates them
|
||||
// unescaped, which breaks paths/args containing spaces and triggers
|
||||
// DEP0190 on Node 22+. Mirrors the quoting in findBun().
|
||||
const quote = (s) => `"${String(s).replace(/"/g, '\\"')}"`;
|
||||
spawnOptions.shell = true;
|
||||
spawnCmd = [bunPath, ...args].map(quote).join(' ');
|
||||
spawnArgs = [];
|
||||
}
|
||||
|
||||
const child = spawn(spawnCmd, spawnArgs, spawnOptions);
|
||||
|
||||
Binary file not shown.
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+453
-432
File diff suppressed because one or more lines are too long
+35
-23
File diff suppressed because one or more lines are too long
@@ -132,6 +132,14 @@ async function buildHooks() {
|
||||
'@derekstride/tree-sitter-sql': '^0.3.11',
|
||||
'@tree-sitter-grammars/tree-sitter-markdown': '^0.3.2',
|
||||
},
|
||||
// The grammar packages above declare three different majors of `tree-sitter`
|
||||
// as peer deps (^0.21, ^0.22, ^0.25). Bun and pnpm are lenient enough to
|
||||
// pick one and move on, but plain `npm install --production` aborts with
|
||||
// ERESOLVE. Pinning a single version via `overrides` lets npm resolve a
|
||||
// working tree without `--legacy-peer-deps`. Closes #2147.
|
||||
overrides: {
|
||||
'tree-sitter': '^0.25.0'
|
||||
},
|
||||
engines: {
|
||||
node: '>=18.0.0',
|
||||
bun: '>=1.0.0'
|
||||
|
||||
@@ -11,6 +11,7 @@ import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js'
|
||||
import { executeWithWorkerFallback, isWorkerFallback } from '../../shared/worker-utils.js';
|
||||
import { logger } from '../../utils/logger.js';
|
||||
import { extractLastMessage } from '../../shared/transcript-parser.js';
|
||||
import { stripMemoryTagsFromPrompt } from '../../utils/tag-stripping.js';
|
||||
import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
|
||||
import { normalizePlatformSource } from '../../shared/platform-source.js';
|
||||
import { shouldTrackProject } from '../../shared/should-track-project.js';
|
||||
@@ -57,6 +58,7 @@ export const summarizeHandler: EventHandler = {
|
||||
let lastAssistantMessage = '';
|
||||
try {
|
||||
lastAssistantMessage = extractLastMessage(transcriptPath, 'assistant', true);
|
||||
lastAssistantMessage = stripMemoryTagsFromPrompt(lastAssistantMessage);
|
||||
} catch (err) {
|
||||
logger.warn('HOOK', `Stop hook: failed to extract last assistant message for session ${sessionId}: ${err instanceof Error ? err.message : err}`);
|
||||
return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
|
||||
|
||||
@@ -33,6 +33,7 @@ import { parseFile, formatFoldedView, unfoldSymbol } from '../services/smart-fil
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { existsSync } from 'node:fs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
// Resolve the path to worker-service.cjs, which lives alongside mcp-server.cjs
|
||||
@@ -676,6 +677,49 @@ function cleanup(reason: string = 'shutdown') {
|
||||
process.on('SIGTERM', cleanup);
|
||||
process.on('SIGINT', cleanup);
|
||||
|
||||
/**
|
||||
* Issue #2174: When the IDE extension (e.g. Cursor's Claude Code) loses its
|
||||
* marketplace directory at ~/.claude/plugins/marketplaces/<source>/, the
|
||||
* extension's hook loader silently skips claude-mem hooks while the MCP
|
||||
* server (this process) keeps working. The session becomes invisible to
|
||||
* memory with no error surfaced.
|
||||
*
|
||||
* The MCP server is the one piece that DOES boot in this state, so we use
|
||||
* it as the canary: detect the missing marketplace dir and emit a single
|
||||
* loud, actionable warning. We don't run smart-install.js from here — the
|
||||
* MCP server runs under the IDE's permission model, not the user's shell,
|
||||
* so attempting an install at MCP startup creates more failure modes than
|
||||
* it fixes. Instead we tell the user exactly what to do.
|
||||
*/
|
||||
function checkMarketplaceMarker(): void {
|
||||
try {
|
||||
// Use os.homedir() so this works on Windows (HOME is unset there;
|
||||
// USERPROFILE is the Windows convention and homedir() picks it up).
|
||||
const home = homedir();
|
||||
const marketplaceCandidates = [
|
||||
resolve(home, '.claude', 'plugins', 'marketplaces', 'thedotmack'),
|
||||
resolve(home, '.config', 'claude', 'plugins', 'marketplaces', 'thedotmack'),
|
||||
];
|
||||
const present = marketplaceCandidates.some(p => p && existsSync(p));
|
||||
const cacheCandidates = [
|
||||
resolve(home, '.claude', 'plugins', 'cache', 'thedotmack', 'claude-mem'),
|
||||
resolve(home, '.config', 'claude', 'plugins', 'cache', 'thedotmack', 'claude-mem'),
|
||||
];
|
||||
const cachePresent = cacheCandidates.some(p => p && existsSync(p));
|
||||
const cacheRoot = cacheCandidates[0];
|
||||
|
||||
if (!present && cachePresent) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'claude-mem MCP started but no marketplace directory was found at ~/.claude/plugins/marketplaces/thedotmack or the XDG equivalent. The IDE plugin loader needs that directory to fire claude-mem hooks (SessionStart, PostToolUse, Stop, etc.). Without it, MCP search will work but no new memories will be captured. To self-heal, run: node ~/.claude/plugins/cache/thedotmack/claude-mem/*/scripts/smart-install.js (or reinstall the plugin from the marketplace).',
|
||||
{ marketplaceCandidates, cacheRoot }
|
||||
);
|
||||
}
|
||||
} catch {
|
||||
// Self-heal probe is best-effort; never fail MCP startup for it.
|
||||
}
|
||||
}
|
||||
|
||||
// Start the server
|
||||
async function main() {
|
||||
// Start the MCP server
|
||||
@@ -684,6 +728,9 @@ async function main() {
|
||||
await server.connect(transport);
|
||||
logger.info('SYSTEM', 'Claude-mem search server started');
|
||||
|
||||
// Surface marketplace-dir corruption that silently disables hook loading
|
||||
checkMarketplaceMarker();
|
||||
|
||||
// Start parent heartbeat to detect orphaned MCP servers
|
||||
startParentHeartbeat();
|
||||
|
||||
|
||||
@@ -787,10 +787,47 @@ export function spawnDaemon(
|
||||
return undefined;
|
||||
}
|
||||
|
||||
// On Windows, child_process.spawn with `detached: true` ignores
|
||||
// `windowsHide: true` (Node docs: behavior is undefined). Spawning the
|
||||
// worker via PowerShell `Start-Process -WindowStyle Hidden` is the only
|
||||
// approach that reliably hides the console window AND inherits parent
|
||||
// env vars (WMIC was tried in PR #751 but is deprecated/absent on
|
||||
// modern Windows 11). Re-applies the fix that PR #751 (e6ae0176)
|
||||
// introduced and commit d13662d5 reverted. See issues #2150, #2186,
|
||||
// #2187, #2190, #2198.
|
||||
if (process.platform === 'win32') {
|
||||
// Use -EncodedCommand so paths with spaces don't need shell quoting.
|
||||
const psScript = `Start-Process -FilePath '${runtimePath.replace(/'/g, "''")}' -ArgumentList @('${scriptPath.replace(/'/g, "''")}','--daemon') -WindowStyle Hidden`;
|
||||
const encodedCommand = Buffer.from(psScript, 'utf16le').toString('base64');
|
||||
|
||||
try {
|
||||
execSync(`powershell -NoProfile -EncodedCommand ${encodedCommand}`, {
|
||||
stdio: 'ignore',
|
||||
windowsHide: true,
|
||||
env
|
||||
});
|
||||
// Windows success sentinel: PowerShell `Start-Process` does not return
|
||||
// the spawned PID, and we don't want to pay for an extra `Get-Process`
|
||||
// round-trip just to discover it. Return 0 (a conventionally invalid
|
||||
// Unix PID) so callers can distinguish "spawn dispatched" from "spawn
|
||||
// failed". Callers MUST use `pid === undefined` to detect failure —
|
||||
// never falsy checks like `if (!pid)`, which would silently treat
|
||||
// success as failure here.
|
||||
return 0;
|
||||
} catch (error: unknown) {
|
||||
logger.error(
|
||||
'SYSTEM',
|
||||
'Failed to spawn worker daemon on Windows',
|
||||
{ runtimePath },
|
||||
error instanceof Error ? error : new Error(String(error))
|
||||
);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// On Unix, prefer setsid to fully detach from the controlling terminal.
|
||||
// On Windows or systems without setsid, spawn the runtime directly.
|
||||
const setsidPath = '/usr/bin/setsid';
|
||||
const useSetsid = process.platform !== 'win32' && existsSync(setsidPath);
|
||||
const useSetsid = existsSync(setsidPath);
|
||||
|
||||
const execPath = useSetsid ? setsidPath : runtimePath;
|
||||
const args = useSetsid
|
||||
@@ -800,7 +837,6 @@ export function spawnDaemon(
|
||||
const child = spawn(execPath, args, {
|
||||
detached: true,
|
||||
stdio: 'ignore',
|
||||
windowsHide: true,
|
||||
env
|
||||
});
|
||||
|
||||
|
||||
@@ -1669,12 +1669,13 @@ export class SessionStore {
|
||||
*/
|
||||
getObservationsByIds(
|
||||
ids: number[],
|
||||
options: { orderBy?: 'date_desc' | 'date_asc'; limit?: number; project?: string; type?: string | string[]; concepts?: string | string[]; files?: string | string[] } = {}
|
||||
options: { orderBy?: 'date_desc' | 'date_asc' | 'relevance'; limit?: number; project?: string; type?: string | string[]; concepts?: string | string[]; files?: string | string[] } = {}
|
||||
): ObservationSearchResult[] {
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
const { orderBy = 'date_desc', limit, project, type, concepts, files } = options;
|
||||
const orderClause = orderBy === 'date_asc' ? 'ASC' : 'DESC';
|
||||
const preserveIdOrder = orderBy === 'relevance';
|
||||
const orderClause = preserveIdOrder ? '' : `ORDER BY created_at_epoch ${orderBy === 'date_asc' ? 'ASC' : 'DESC'}`;
|
||||
const limitClause = limit ? `LIMIT ${limit}` : '';
|
||||
|
||||
// Build placeholders for IN clause
|
||||
@@ -1730,11 +1731,16 @@ export class SessionStore {
|
||||
SELECT *
|
||||
FROM observations
|
||||
${whereClause}
|
||||
ORDER BY created_at_epoch ${orderClause}
|
||||
${orderClause}
|
||||
${limitClause}
|
||||
`);
|
||||
|
||||
return stmt.all(...params) as ObservationSearchResult[];
|
||||
const rows = stmt.all(...params) as ObservationSearchResult[];
|
||||
if (!preserveIdOrder) return rows;
|
||||
|
||||
// Preserve caller-provided ID order (Chroma vector similarity ranking)
|
||||
const rowMap = new Map(rows.map(r => [r.id, r]));
|
||||
return ids.map(id => rowMap.get(id)).filter((r): r is ObservationSearchResult => !!r);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2466,12 +2472,13 @@ export class SessionStore {
|
||||
*/
|
||||
getSessionSummariesByIds(
|
||||
ids: number[],
|
||||
options: { orderBy?: 'date_desc' | 'date_asc'; limit?: number; project?: string } = {}
|
||||
options: { orderBy?: 'date_desc' | 'date_asc' | 'relevance'; limit?: number; project?: string } = {}
|
||||
): SessionSummarySearchResult[] {
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
const { orderBy = 'date_desc', limit, project } = options;
|
||||
const orderClause = orderBy === 'date_asc' ? 'ASC' : 'DESC';
|
||||
const preserveIdOrder = orderBy === 'relevance';
|
||||
const orderClause = preserveIdOrder ? '' : `ORDER BY created_at_epoch ${orderBy === 'date_asc' ? 'ASC' : 'DESC'}`;
|
||||
const limitClause = limit ? `LIMIT ${limit}` : '';
|
||||
const placeholders = ids.map(() => '?').join(',');
|
||||
const params: any[] = [...ids];
|
||||
@@ -2485,11 +2492,15 @@ export class SessionStore {
|
||||
const stmt = this.db.prepare(`
|
||||
SELECT * FROM session_summaries
|
||||
${whereClause}
|
||||
ORDER BY created_at_epoch ${orderClause}
|
||||
${orderClause}
|
||||
${limitClause}
|
||||
`);
|
||||
|
||||
return stmt.all(...params) as SessionSummarySearchResult[];
|
||||
const rows = stmt.all(...params) as SessionSummarySearchResult[];
|
||||
if (!preserveIdOrder) return rows;
|
||||
|
||||
const rowMap = new Map(rows.map(r => [r.id, r]));
|
||||
return ids.map(id => rowMap.get(id)).filter((r): r is SessionSummarySearchResult => !!r);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2498,12 +2509,13 @@ export class SessionStore {
|
||||
*/
|
||||
getUserPromptsByIds(
|
||||
ids: number[],
|
||||
options: { orderBy?: 'date_desc' | 'date_asc'; limit?: number; project?: string } = {}
|
||||
options: { orderBy?: 'date_desc' | 'date_asc' | 'relevance'; limit?: number; project?: string } = {}
|
||||
): UserPromptRecord[] {
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
const { orderBy = 'date_desc', limit, project } = options;
|
||||
const orderClause = orderBy === 'date_asc' ? 'ASC' : 'DESC';
|
||||
const preserveIdOrder = orderBy === 'relevance';
|
||||
const orderClause = preserveIdOrder ? '' : `ORDER BY up.created_at_epoch ${orderBy === 'date_asc' ? 'ASC' : 'DESC'}`;
|
||||
const limitClause = limit ? `LIMIT ${limit}` : '';
|
||||
const placeholders = ids.map(() => '?').join(',');
|
||||
const params: any[] = [...ids];
|
||||
@@ -2520,11 +2532,15 @@ export class SessionStore {
|
||||
FROM user_prompts up
|
||||
JOIN sdk_sessions s ON up.content_session_id = s.content_session_id
|
||||
WHERE up.id IN (${placeholders}) ${projectFilter}
|
||||
ORDER BY up.created_at_epoch ${orderClause}
|
||||
${orderClause}
|
||||
${limitClause}
|
||||
`);
|
||||
|
||||
return stmt.all(...params) as UserPromptRecord[];
|
||||
const rows = stmt.all(...params) as UserPromptRecord[];
|
||||
if (!preserveIdOrder) return rows;
|
||||
|
||||
const rowMap = new Map(rows.map(r => [r.id, r]));
|
||||
return ids.map(id => rowMap.get(id)).filter((r): r is UserPromptRecord => !!r);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -37,6 +37,14 @@ class FileTailer {
|
||||
this.watcher = null;
|
||||
}
|
||||
|
||||
// Public wrapper so the recursive root watcher can prod an existing tailer
|
||||
// when fs.watch on the file itself misses an append event on Windows
|
||||
// (#2192). Per-file fs.watch is unreliable on Windows ReFS/SMB; the root
|
||||
// recursive watcher is the only signal we can trust there.
|
||||
poke(): void {
|
||||
this.readNewData().catch(() => undefined);
|
||||
}
|
||||
|
||||
private async readNewData(): Promise<void> {
|
||||
if (!existsSync(this.filePath)) return;
|
||||
|
||||
@@ -138,8 +146,18 @@ export class TranscriptWatcher {
|
||||
// here on every line and a full resolveWatchFiles() per append is
|
||||
// more expensive than the prior 5-s interval. Only unknown paths
|
||||
// warrant a rescan (new transcript files surface here first).
|
||||
const changed = resolvePath(watchRoot, name);
|
||||
if (this.tailers.has(changed)) return;
|
||||
// Normalize so the key matches what globSync stored (forward slashes
|
||||
// on Windows). Without this, every append from the recursive watcher
|
||||
// looks like a "new file" and we re-scan instead of poking the tailer.
|
||||
const changed = resolvePath(watchRoot, name).replace(/\\/g, '/');
|
||||
const existingTailer = this.tailers.get(changed);
|
||||
if (existingTailer) {
|
||||
// #2192: per-file fs.watch on Windows misses appends to live JSONL
|
||||
// files. The recursive root watcher fires reliably; poke the tailer
|
||||
// so it picks up new lines without a full glob rescan.
|
||||
existingTailer.poke();
|
||||
return;
|
||||
}
|
||||
const matches = this.resolveWatchFiles(resolvedPath);
|
||||
for (const filePath of matches) {
|
||||
if (!this.tailers.has(filePath)) {
|
||||
@@ -206,7 +224,11 @@ export class TranscriptWatcher {
|
||||
|
||||
private resolveWatchFiles(inputPath: string): string[] {
|
||||
if (this.hasGlob(inputPath)) {
|
||||
return globSync(inputPath, { nodir: true, absolute: true });
|
||||
// #2192: glob treats backslashes as escape chars, not separators. On
|
||||
// Windows, expandHomePath() emits backslash paths from Node's path.join
|
||||
// which globSync silently fails to match. Normalize separators here
|
||||
// before passing to glob — leaves Unix paths untouched.
|
||||
return globSync(this.normalizeGlobPattern(inputPath), { nodir: true, absolute: true });
|
||||
}
|
||||
|
||||
if (existsSync(inputPath)) {
|
||||
@@ -214,7 +236,7 @@ export class TranscriptWatcher {
|
||||
const stat = statSync(inputPath);
|
||||
if (stat.isDirectory()) {
|
||||
const pattern = join(inputPath, '**', '*.jsonl');
|
||||
return globSync(pattern, { nodir: true, absolute: true });
|
||||
return globSync(this.normalizeGlobPattern(pattern), { nodir: true, absolute: true });
|
||||
}
|
||||
return [inputPath];
|
||||
} catch (error: unknown) {
|
||||
@@ -226,6 +248,10 @@ export class TranscriptWatcher {
|
||||
return [];
|
||||
}
|
||||
|
||||
private normalizeGlobPattern(inputPath: string): string {
|
||||
return inputPath.replace(/\\/g, '/');
|
||||
}
|
||||
|
||||
private hasGlob(inputPath: string): boolean {
|
||||
return /[*?[\]{}()]/.test(inputPath);
|
||||
}
|
||||
|
||||
@@ -159,6 +159,13 @@ export class SDKAgent {
|
||||
spawnClaudeCodeProcess: createSdkSpawnFactory(session.sessionDbId),
|
||||
env: isolatedEnv, // Use isolated credentials from ~/.claude-mem/.env, not process.env
|
||||
mcpServers: {},
|
||||
// Reject inheritance of the user's settings.json / project settings —
|
||||
// observer SDK runs with only the options we pass explicitly.
|
||||
// Closes #2155.
|
||||
settingSources: [],
|
||||
// Reject inheritance of the user's MCP config — the worker SDK gets
|
||||
// exactly mcpServers: {} and nothing else. Closes #2159, #2171, #2194.
|
||||
strictMcpConfig: true,
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -292,6 +292,29 @@ export class SessionRoutes extends BaseRouteHandler {
|
||||
|
||||
if (wasAborted) {
|
||||
logger.info('SESSION', `Generator aborted`, { sessionId: sessionDbId });
|
||||
|
||||
// #2192: when the generator aborts (idle timeout, user cancel,
|
||||
// shutdown) with rows already claimed and yielded but not yet
|
||||
// confirmed by ResponseProcessor, those rows sit in 'processing'
|
||||
// under THIS worker's PID. The self-healing claim predicate skips
|
||||
// them because the worker is still alive — the queue deadlocks
|
||||
// until the worker restarts. Walk the in-flight ids and run them
|
||||
// through markFailed so the retry ladder requeues them or marks
|
||||
// them terminally failed.
|
||||
const inflightStore = this.sessionManager.getPendingMessageStore();
|
||||
const inflightIds = session.processingMessageIds.slice();
|
||||
session.processingMessageIds = [];
|
||||
for (const messageId of inflightIds) {
|
||||
try {
|
||||
inflightStore.markFailed(messageId);
|
||||
} catch (markErr) {
|
||||
const normalized = markErr instanceof Error ? markErr : new Error(String(markErr));
|
||||
logger.error('SESSION', 'Failed to requeue in-flight message after abort', {
|
||||
sessionId: sessionDbId,
|
||||
messageId,
|
||||
}, normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Don't log "exited unexpectedly" here — a non-abort exit is normal when
|
||||
// the SDK subprocess completes its work. The crash-recovery block below
|
||||
@@ -601,7 +624,10 @@ export class SessionRoutes extends BaseRouteHandler {
|
||||
|
||||
const { last_assistant_message } = req.body;
|
||||
|
||||
this.sessionManager.queueSummarize(sessionDbId, last_assistant_message);
|
||||
const cleanedLastAssistantMessage = last_assistant_message
|
||||
? stripMemoryTagsFromPrompt(String(last_assistant_message))
|
||||
: last_assistant_message;
|
||||
this.sessionManager.queueSummarize(sessionDbId, cleanedLastAssistantMessage);
|
||||
|
||||
// CRITICAL: Ensure SDK agent is running to consume the queue
|
||||
this.ensureGeneratorRunning(sessionDbId, 'summarize');
|
||||
@@ -747,7 +773,10 @@ export class SessionRoutes extends BaseRouteHandler {
|
||||
}
|
||||
|
||||
// Queue summarize
|
||||
this.sessionManager.queueSummarize(sessionDbId, last_assistant_message);
|
||||
const cleanedLastAssistantMessage = last_assistant_message
|
||||
? stripMemoryTagsFromPrompt(String(last_assistant_message))
|
||||
: last_assistant_message;
|
||||
this.sessionManager.queueSummarize(sessionDbId, cleanedLastAssistantMessage);
|
||||
|
||||
// Ensure SDK agent is running
|
||||
this.ensureGeneratorRunning(sessionDbId, 'summarize');
|
||||
|
||||
@@ -81,6 +81,9 @@ export class KnowledgeAgent {
|
||||
pathToClaudeCodeExecutable: claudePath,
|
||||
env: isolatedEnv,
|
||||
mcpServers: {},
|
||||
// Same SDK-boundary isolation as SDKAgent. Closes #2155, #2159, #2171, #2194.
|
||||
settingSources: [],
|
||||
strictMcpConfig: true,
|
||||
}
|
||||
});
|
||||
|
||||
@@ -198,6 +201,9 @@ export class KnowledgeAgent {
|
||||
pathToClaudeCodeExecutable: claudePath,
|
||||
env: isolatedEnv,
|
||||
mcpServers: {},
|
||||
// Same SDK-boundary isolation as SDKAgent. Closes #2155, #2159, #2171, #2194.
|
||||
settingSources: [],
|
||||
strictMcpConfig: true,
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -107,10 +107,13 @@ export class ChromaSearchStrategy extends BaseSearchStrategy implements SearchSt
|
||||
let sessions: SessionSummarySearchResult[] = [];
|
||||
let prompts: UserPromptSearchResult[] = [];
|
||||
|
||||
// Chroma already ranks by vector similarity; 'relevance' has no SQL
|
||||
// equivalent, so drop it before hydrating rows from SessionStore.
|
||||
const sqlOrderBy: 'date_desc' | 'date_asc' | undefined =
|
||||
options.orderBy === 'relevance' ? undefined : options.orderBy;
|
||||
// Pass orderBy through unchanged. SessionStore's getObservationsByIds /
|
||||
// getSessionSummariesByIds / getUserPromptsByIds accept 'relevance' as a
|
||||
// valid value: they skip ORDER BY and preserve the caller-provided ID
|
||||
// order (Chroma's vector similarity ranking). Coercing 'relevance' to
|
||||
// undefined here would let SessionStore default to 'date_desc' and
|
||||
// destroy the semantic ranking. See #2153.
|
||||
const sqlOrderBy = options.orderBy;
|
||||
|
||||
if (categorized.obsIds.length > 0) {
|
||||
const obsOptions = { type: options.obsType, concepts: options.concepts, files: options.files, orderBy: sqlOrderBy, limit: options.limit, project: options.project };
|
||||
|
||||
@@ -24,10 +24,35 @@ export const ENV_PROXY_VARS = new Set([
|
||||
'npm_config_https_proxy',
|
||||
]);
|
||||
|
||||
/** Vars that start with CLAUDE_CODE_ but must be preserved for subprocess auth/tooling */
|
||||
/**
|
||||
* Env vars that must be preserved for subprocess auth/tooling.
|
||||
*
|
||||
* Two categories:
|
||||
* - CLAUDE_CODE_* vars that would otherwise be stripped by ENV_PREFIXES.
|
||||
* `CLAUDE_CODE_USE_BEDROCK` / `CLAUDE_CODE_USE_VERTEX` were silently
|
||||
* dropped before the fix for #2199, breaking Knowledge Agent / observer
|
||||
* SDK runs for Bedrock and Vertex users.
|
||||
* - Cloud provider credentials that pass through naturally today (no deny
|
||||
* rule matches), but listing them here documents intent so future
|
||||
* deny-list tightening doesn't quietly break Bedrock/Vertex users again.
|
||||
*/
|
||||
export const ENV_PRESERVE = new Set([
|
||||
'CLAUDE_CODE_OAUTH_TOKEN',
|
||||
'CLAUDE_CODE_GIT_BASH_PATH',
|
||||
// Cloud provider switches (would be stripped by CLAUDE_CODE_ prefix). #2199.
|
||||
'CLAUDE_CODE_USE_BEDROCK',
|
||||
'CLAUDE_CODE_USE_VERTEX',
|
||||
// AWS Bedrock credentials (pass through today; explicit for safety).
|
||||
'ANTHROPIC_BEDROCK_BASE_URL',
|
||||
'AWS_REGION',
|
||||
'AWS_PROFILE',
|
||||
'AWS_ACCESS_KEY_ID',
|
||||
'AWS_SECRET_ACCESS_KEY',
|
||||
'AWS_SESSION_TOKEN',
|
||||
// Google Vertex credentials (pass through today; explicit for safety).
|
||||
'ANTHROPIC_VERTEX_PROJECT_ID',
|
||||
'CLOUD_ML_REGION',
|
||||
'GOOGLE_APPLICATION_CREDENTIALS',
|
||||
]);
|
||||
|
||||
export function sanitizeEnv(env: NodeJS.ProcessEnv = process.env): NodeJS.ProcessEnv {
|
||||
|
||||
@@ -663,18 +663,21 @@ export function spawnSdkProcess(
|
||||
|
||||
// Unix: detached:true causes the kernel to setpgid() on the child so the
|
||||
// child becomes leader of a new process group whose pgid equals its pid.
|
||||
// Windows: detached:true decouples the child from the parent console; there
|
||||
// is no POSIX group, but the flag is still safe to pass.
|
||||
// Windows: detached:true would let claude.exe outlive the worker AND
|
||||
// documents windowsHide as "undefined behavior" when combined — visible
|
||||
// GUI windows pop per assistant turn (#2190, #2198). On Windows we want
|
||||
// claude.exe to die with the parent and stay hidden, so detached:false.
|
||||
//
|
||||
// stdin must be 'pipe' (not 'ignore') because SpawnedSdkProcess.stdin is
|
||||
// typed NonNullable<...> and the Claude Agent SDK consumes that pipe to
|
||||
// stream prompts in. With 'ignore', child.stdin would be null and the
|
||||
// null-check below (line ~737) would tear the child down immediately.
|
||||
const isWin = process.platform === 'win32';
|
||||
const child = useCmdWrapper
|
||||
? spawn('cmd.exe', ['/d', '/c', options.command, ...filteredArgs], {
|
||||
cwd: options.cwd,
|
||||
env,
|
||||
detached: true,
|
||||
detached: !isWin,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
signal: options.signal,
|
||||
windowsHide: true,
|
||||
@@ -682,7 +685,7 @@ export function spawnSdkProcess(
|
||||
: spawn(options.command, filteredArgs, {
|
||||
cwd: options.cwd,
|
||||
env,
|
||||
detached: true,
|
||||
detached: !isWin,
|
||||
stdio: ['pipe', 'pipe', 'pipe'],
|
||||
signal: options.signal,
|
||||
windowsHide: true,
|
||||
|
||||
+8
-2
@@ -302,8 +302,14 @@ class Logger {
|
||||
? `\n${data.message}\n${data.stack}`
|
||||
: ` ${data.message}`;
|
||||
} else if (this.getLevel() === LogLevel.DEBUG && typeof data === 'object') {
|
||||
// In debug mode, show full JSON for objects
|
||||
dataStr = '\n' + JSON.stringify(data, null, 2);
|
||||
// In debug mode, show full JSON for objects.
|
||||
// Wrap stringify in try/catch so circular structures don't crash the logger;
|
||||
// fall back to formatData (which marks arrays/object key counts safely).
|
||||
try {
|
||||
dataStr = '\n' + JSON.stringify(data, null, 2);
|
||||
} catch {
|
||||
dataStr = ' ' + this.formatData(data);
|
||||
}
|
||||
} else {
|
||||
dataStr = ' ' + this.formatData(data);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* Tests for privacy-tag stripping in summarizeHandler.
|
||||
*
|
||||
* Validates that the Stop hook strips memory tags (<private>, <claude-mem-context>,
|
||||
* <system-instruction>, <system_instruction>, <persisted-output>) from the assistant's
|
||||
* last message before POSTing to /api/sessions/summarize. This is the fix for the bug
|
||||
* where private content was leaking into the summarize queue and downstream summary LLM.
|
||||
*
|
||||
* Sources:
|
||||
* - Handler: src/cli/handlers/summarize.ts
|
||||
* - Stripping utility: src/utils/tag-stripping.ts
|
||||
* - Mock pattern: tests/cli/handlers/summarize-subagent-skip.test.ts
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach, spyOn, mock } from 'bun:test';
|
||||
import { homedir } from 'os';
|
||||
import { join } from 'path';
|
||||
|
||||
mock.module('../../../src/shared/SettingsDefaultsManager.js', () => ({
|
||||
SettingsDefaultsManager: {
|
||||
get: (key: string) => {
|
||||
if (key === 'CLAUDE_MEM_DATA_DIR') return join(homedir(), '.claude-mem');
|
||||
return '';
|
||||
},
|
||||
getInt: () => 0,
|
||||
loadFromFile: () => ({ CLAUDE_MEM_EXCLUDED_PROJECTS: '' }),
|
||||
},
|
||||
}));
|
||||
|
||||
mock.module('../../../src/shared/hook-settings.js', () => ({
|
||||
loadFromFileOnce: () => ({ CLAUDE_MEM_EXCLUDED_PROJECTS: '' }),
|
||||
}));
|
||||
|
||||
// Per-test control over what the transcript parser "extracts".
|
||||
let mockExtractedMessage: string = '';
|
||||
mock.module('../../../src/shared/transcript-parser.js', () => ({
|
||||
extractLastMessage: () => mockExtractedMessage,
|
||||
}));
|
||||
|
||||
// Capture every executeWithWorkerFallback call. Resolve successfully so the
|
||||
// handler completes its normal path — the assertions inspect what got POSTed.
|
||||
const workerCallLog: Array<{ path: string; method: string; body: any }> = [];
|
||||
mock.module('../../../src/shared/worker-utils.js', () => ({
|
||||
ensureWorkerRunning: () => Promise.resolve(true),
|
||||
getWorkerPort: () => 37777,
|
||||
workerHttpRequest: (apiPath: string, options?: any) => {
|
||||
workerCallLog.push({ path: apiPath, method: options?.method ?? 'GET', body: options?.body });
|
||||
return Promise.resolve(new Response('{"status":"queued"}', { status: 200 }));
|
||||
},
|
||||
executeWithWorkerFallback: async (apiPath: string, method: string, body: unknown) => {
|
||||
workerCallLog.push({ path: apiPath, method, body });
|
||||
return { status: 'queued' };
|
||||
},
|
||||
isWorkerFallback: (_result: unknown) => false,
|
||||
}));
|
||||
|
||||
import { logger } from '../../../src/utils/logger.js';
|
||||
|
||||
let loggerSpies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
beforeEach(() => {
|
||||
workerCallLog.length = 0;
|
||||
mockExtractedMessage = '';
|
||||
loggerSpies = [
|
||||
spyOn(logger, 'info').mockImplementation(() => {}),
|
||||
spyOn(logger, 'debug').mockImplementation(() => {}),
|
||||
spyOn(logger, 'warn').mockImplementation(() => {}),
|
||||
spyOn(logger, 'error').mockImplementation(() => {}),
|
||||
spyOn(logger, 'failure').mockImplementation(() => {}),
|
||||
spyOn(logger, 'dataIn').mockImplementation(() => {}),
|
||||
];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
loggerSpies.forEach(spy => spy.mockRestore());
|
||||
});
|
||||
|
||||
const baseInput = {
|
||||
sessionId: 'sess-tag-strip',
|
||||
cwd: '/tmp',
|
||||
platform: 'claude-code' as const,
|
||||
transcriptPath: '/tmp/fake.jsonl',
|
||||
};
|
||||
|
||||
function postedBody(): any {
|
||||
expect(workerCallLog).toHaveLength(1);
|
||||
const { body } = workerCallLog[0];
|
||||
// executeWithWorkerFallback receives the body as a plain object; the legacy
|
||||
// workerHttpRequest path receives a JSON string. Support both for forward-compat.
|
||||
return typeof body === 'string' ? JSON.parse(body) : body;
|
||||
}
|
||||
|
||||
describe('summarizeHandler — privacy tag stripping', () => {
|
||||
it('strips <private> tags and their content from last_assistant_message', async () => {
|
||||
mockExtractedMessage = 'Hello <private>SECRET-VALUE-42</private> world';
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
const result = await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
expect(result.continue).toBe(true);
|
||||
const body = postedBody();
|
||||
expect(body.last_assistant_message).not.toContain('SECRET-VALUE-42');
|
||||
expect(body.last_assistant_message).not.toContain('<private>');
|
||||
expect(body.last_assistant_message).toBe('Hello world');
|
||||
});
|
||||
|
||||
it('preserves surrounding content when stripping privacy tags', async () => {
|
||||
mockExtractedMessage =
|
||||
'Before tag. <private>leak</private> Middle. <private>another</private> After.';
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
const body = postedBody();
|
||||
expect(body.last_assistant_message).not.toContain('leak');
|
||||
expect(body.last_assistant_message).not.toContain('another');
|
||||
expect(body.last_assistant_message).toContain('Before tag.');
|
||||
expect(body.last_assistant_message).toContain('Middle.');
|
||||
expect(body.last_assistant_message).toContain('After.');
|
||||
});
|
||||
|
||||
it('skips the worker POST when the entire turn is wrapped in a privacy tag', async () => {
|
||||
// After stripping, the message is empty — handler should hit the
|
||||
// "no assistant message" guard and return without POSTing.
|
||||
mockExtractedMessage = '<private>everything is private</private>';
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
const result = await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
expect(result.continue).toBe(true);
|
||||
expect(result.suppressOutput).toBe(true);
|
||||
expect(workerCallLog).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('skips the worker POST when stripping leaves only whitespace', async () => {
|
||||
mockExtractedMessage = ' <private>x</private>\n\t<private>y</private> ';
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
expect(workerCallLog).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('does not modify content that contains no privacy tags', async () => {
|
||||
mockExtractedMessage = 'Just a normal assistant turn with no privacy markers.';
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
const body = postedBody();
|
||||
expect(body.last_assistant_message).toBe(
|
||||
'Just a normal assistant turn with no privacy markers.'
|
||||
);
|
||||
});
|
||||
|
||||
const taggedPayloads: Array<[string, string]> = [
|
||||
['<private>', '<private>SECRET-PRIVATE</private>'],
|
||||
['<claude-mem-context>', '<claude-mem-context>SECRET-CTX</claude-mem-context>'],
|
||||
['<system-instruction>', '<system-instruction>SECRET-SI-DASH</system-instruction>'],
|
||||
['<system_instruction>', '<system_instruction>SECRET-SI-UNDER</system_instruction>'],
|
||||
['<persisted-output>', '<persisted-output>SECRET-PO</persisted-output>'],
|
||||
];
|
||||
|
||||
for (const [label, payload] of taggedPayloads) {
|
||||
it(`strips ${label} tags from last_assistant_message`, async () => {
|
||||
const secret = payload.match(/SECRET-[A-Z-]+/)![0];
|
||||
mockExtractedMessage = `before ${payload} after`;
|
||||
|
||||
const { summarizeHandler } = await import('../../../src/cli/handlers/summarize.js');
|
||||
await summarizeHandler.execute(baseInput as any);
|
||||
|
||||
const body = postedBody();
|
||||
expect(body.last_assistant_message).not.toContain(secret);
|
||||
expect(body.last_assistant_message).toContain('before');
|
||||
expect(body.last_assistant_message).toContain('after');
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { describe, it, expect } from 'bun:test';
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
// Source-only assertions that document the three Windows-specific
|
||||
// regressions from #2192. We stay source-level (no fs.watch / no SDK spawn)
|
||||
// because the failure modes are all in code paths that only execute on
|
||||
// Windows; the goal is to lock the fix in so a future refactor can't
|
||||
// silently revert it.
|
||||
|
||||
const watcherSource = readFileSync(
|
||||
join(__dirname, '..', 'src', 'services', 'transcripts', 'watcher.ts'),
|
||||
'utf-8',
|
||||
);
|
||||
const sessionRoutesSource = readFileSync(
|
||||
join(__dirname, '..', 'src', 'services', 'worker', 'http', 'routes', 'SessionRoutes.ts'),
|
||||
'utf-8',
|
||||
);
|
||||
|
||||
describe('Codex transcript ingestion on Windows (#2192)', () => {
|
||||
it('normalizes backslashes to forward slashes before passing the path to globSync', () => {
|
||||
expect(watcherSource).toContain('normalizeGlobPattern');
|
||||
expect(watcherSource).toContain("inputPath.replace(/\\\\/g, '/')");
|
||||
expect(watcherSource).toMatch(/globSync\(this\.normalizeGlobPattern\(/);
|
||||
});
|
||||
|
||||
it('exposes a public poke() on the file tailer so the recursive root watcher can prod it', () => {
|
||||
expect(watcherSource).toMatch(/\bpoke\(\): void\b/);
|
||||
});
|
||||
|
||||
it('pokes an existing tailer on root-watcher events instead of returning early', () => {
|
||||
// The recursive root watcher must call poke() on existing tailers, not
|
||||
// skip them — Windows fs.watch on the file itself misses appends.
|
||||
expect(watcherSource).toMatch(/existingTailer\.poke\(\)/);
|
||||
});
|
||||
|
||||
it('normalizes the resolved path to forward slashes before tailer-map lookup', () => {
|
||||
// Without this, the lookup key (native path.resolve) won't match the
|
||||
// stored key (forward-slash from glob), and every append looks like a
|
||||
// new file.
|
||||
expect(watcherSource).toMatch(/resolvePath\(watchRoot, name\)\.replace\(\/\\\\\/g, '\/'\)/);
|
||||
});
|
||||
|
||||
it('requeues in-flight processing rows when the generator aborts (queue self-deadlock fix)', () => {
|
||||
// After abort, processingMessageIds entries must go through markFailed so
|
||||
// the retry ladder can either requeue them as 'pending' or terminate
|
||||
// them — leaving them in 'processing' under the live worker's PID is
|
||||
// the deadlock #2192 reports.
|
||||
expect(sessionRoutesSource).toMatch(/Generator aborted/);
|
||||
expect(sessionRoutesSource).toMatch(/processingMessageIds\.slice\(\)/);
|
||||
expect(sessionRoutesSource).toMatch(/inflightStore\.markFailed\(messageId\)/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Regression test for #2153: ChromaSearchStrategy passes orderBy='relevance'
|
||||
* to SessionStore.getObservationsByIds expecting Chroma's vector ranking
|
||||
* (caller-provided ID order) to be preserved. The old code coerced
|
||||
* 'relevance' to undefined, which then defaulted to 'date_desc' inside
|
||||
* SessionStore, destroying the semantic ranking.
|
||||
*
|
||||
* Mock Justification: NONE - real SQLite ':memory:' covers SQL + ordering.
|
||||
*/
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
|
||||
import { SessionStore } from '../../../src/services/sqlite/SessionStore.js';
|
||||
|
||||
describe('SessionStore.*ByIds — orderBy: "relevance" preserves caller ID order (#2153)', () => {
|
||||
let store: SessionStore;
|
||||
|
||||
beforeEach(() => {
|
||||
store = new SessionStore(':memory:');
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
store.close();
|
||||
});
|
||||
|
||||
it('getObservationsByIds returns rows in caller-provided ID order when orderBy is "relevance"', () => {
|
||||
const sdkId = store.createSDKSession('content-relevance', 'p', 'prompt');
|
||||
store.updateMemorySessionId(sdkId, 'session-relevance');
|
||||
|
||||
// Insert 5 observations with strictly increasing created_at_epoch so that
|
||||
// a date_desc default would reverse the natural insertion order. The test
|
||||
// proves that caller-provided ID order, not date order, is honored.
|
||||
const baseTs = 1_700_000_000_000;
|
||||
const inserted: number[] = [];
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const result = store.storeObservations(
|
||||
'session-relevance',
|
||||
'p',
|
||||
[{
|
||||
type: 'test',
|
||||
title: `obs-${i}`,
|
||||
subtitle: null,
|
||||
facts: [`fact ${i}`],
|
||||
narrative: null,
|
||||
concepts: [],
|
||||
files_read: [],
|
||||
files_modified: [],
|
||||
}],
|
||||
null,
|
||||
i,
|
||||
0,
|
||||
baseTs + i * 1000,
|
||||
);
|
||||
inserted.push(result.observationIds[0]);
|
||||
}
|
||||
|
||||
// Reverse the IDs — semantic ranking from Chroma would not match
|
||||
// chronological order.
|
||||
const callerOrder = [...inserted].reverse();
|
||||
const results = store.getObservationsByIds(callerOrder, { orderBy: 'relevance' });
|
||||
|
||||
expect(results.map(r => r.id)).toEqual(callerOrder);
|
||||
});
|
||||
|
||||
it('getObservationsByIds still respects date_desc when orderBy defaults', () => {
|
||||
const sdkId = store.createSDKSession('content-date', 'p', 'prompt');
|
||||
store.updateMemorySessionId(sdkId, 'session-date');
|
||||
const baseTs = 1_700_000_000_000;
|
||||
const inserted: number[] = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const result = store.storeObservations(
|
||||
'session-date',
|
||||
'p',
|
||||
[{
|
||||
type: 'test',
|
||||
title: `obs-${i}`,
|
||||
subtitle: null,
|
||||
facts: [`fact ${i}`],
|
||||
narrative: null,
|
||||
concepts: [],
|
||||
files_read: [],
|
||||
files_modified: [],
|
||||
}],
|
||||
null,
|
||||
i,
|
||||
0,
|
||||
baseTs + i * 1000,
|
||||
);
|
||||
inserted.push(result.observationIds[0]);
|
||||
}
|
||||
|
||||
const callerOrder = [...inserted].reverse(); // [oldId... newer... oldest]
|
||||
// Default order is date_desc -> newest first regardless of input order.
|
||||
const results = store.getObservationsByIds(callerOrder);
|
||||
expect(results.map(r => r.id)).toEqual([...inserted].reverse());
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user