context-mode 1.0.148 → 1.0.150

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,14 +6,14 @@
6
6
  },
7
7
  "metadata": {
8
8
  "description": "Claude Code plugins by Mert Koseoğlu",
9
- "version": "1.0.148"
9
+ "version": "1.0.150"
10
10
  },
11
11
  "plugins": [
12
12
  {
13
13
  "name": "context-mode",
14
14
  "source": "./",
15
15
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
16
- "version": "1.0.148",
16
+ "version": "1.0.150",
17
17
  "author": {
18
18
  "name": "Mert Koseoğlu"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.148",
3
+ "version": "1.0.150",
4
4
  "description": "MCP server that saves 98% of your context window with session continuity. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and automatic state restore across compactions.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.148",
3
+ "version": "1.0.150",
4
4
  "description": "MCP server that saves 98% of your context window with session continuity. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and automatic state restore across compactions.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -3,7 +3,7 @@
3
3
  "name": "Context Mode",
4
4
  "kind": "tool",
5
5
  "description": "OpenClaw plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
6
- "version": "1.0.148",
6
+ "version": "1.0.150",
7
7
  "sandbox": {
8
8
  "mode": "permissive",
9
9
  "filesystem_access": "full",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.148",
3
+ "version": "1.0.150",
4
4
  "description": "OpenClaw plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
3
+ *
4
+ * Claude Code's plugin auto-update can leave installed_plugins.json pointing
5
+ * to a non-existent directory (anthropics/claude-code#46915). This module
6
+ * detects and repairs the mismatch by creating symlinks.
7
+ *
8
+ * 4-layer defense:
9
+ * 1. start.mjs startup — reverse heal (registry → symlink to us)
10
+ * 2. server.ts first tool call — mid-session heal
11
+ * 3. postinstall.mjs — backward symlink on new install
12
+ * 4. global hook auto-deploy — survives total plugin cache breakage
13
+ */
14
+ export interface HealResult {
15
+ healed: boolean;
16
+ action?: "symlink" | "global-hook" | "none";
17
+ from?: string;
18
+ to?: string;
19
+ }
20
+ /**
21
+ * Core heal: if installed_plugins.json points to a non-existent directory,
22
+ * create a symlink from that path to our actual directory.
23
+ *
24
+ * @param currentDir - The directory we're actually running from
25
+ * @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
26
+ */
27
+ export declare function healRegistryMismatch(currentDir: string, installedPluginsPath?: string): HealResult;
28
+ /**
29
+ * Deploy a global SessionStart hook that heals plugin cache mismatches.
30
+ * This hook lives outside the plugin directory, so it survives cache breakage.
31
+ *
32
+ * Written to ~/.claude/hooks/context-mode-cache-heal.sh
33
+ */
34
+ export declare function deployGlobalHealHook(): HealResult;
35
+ /**
36
+ * Backward symlink: during postinstall, if the registry points to a
37
+ * non-existent OLD path, create a symlink from old → new (our directory).
38
+ * Same as healRegistryMismatch but called from postinstall context.
39
+ */
40
+ export { healRegistryMismatch as healBackwardCompat };
41
+ /**
42
+ * Mid-session heal — call on first MCP tool invocation.
43
+ * Checks if registry path differs from our running directory.
44
+ * Creates symlink if needed. Runs only once per process.
45
+ */
46
+ export declare function healMidSession(currentDir: string): HealResult;
47
+ /** Reset mid-session flag (for testing only) */
48
+ export declare function _resetMidSession(): void;
@@ -0,0 +1,150 @@
1
+ /**
2
+ * Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
3
+ *
4
+ * Claude Code's plugin auto-update can leave installed_plugins.json pointing
5
+ * to a non-existent directory (anthropics/claude-code#46915). This module
6
+ * detects and repairs the mismatch by creating symlinks.
7
+ *
8
+ * 4-layer defense:
9
+ * 1. start.mjs startup — reverse heal (registry → symlink to us)
10
+ * 2. server.ts first tool call — mid-session heal
11
+ * 3. postinstall.mjs — backward symlink on new install
12
+ * 4. global hook auto-deploy — survives total plugin cache breakage
13
+ */
14
+ import { existsSync, readFileSync, symlinkSync, mkdirSync, writeFileSync } from "node:fs";
15
+ import { resolve, dirname } from "node:path";
16
+ import { homedir } from "node:os";
17
+ /**
18
+ * Core heal: if installed_plugins.json points to a non-existent directory,
19
+ * create a symlink from that path to our actual directory.
20
+ *
21
+ * @param currentDir - The directory we're actually running from
22
+ * @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
23
+ */
24
+ export function healRegistryMismatch(currentDir, installedPluginsPath) {
25
+ const ipPath = installedPluginsPath ?? resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
26
+ if (!existsSync(ipPath))
27
+ return { healed: false, action: "none" };
28
+ if (!existsSync(currentDir))
29
+ return { healed: false, action: "none" };
30
+ let ip;
31
+ try {
32
+ ip = JSON.parse(readFileSync(ipPath, "utf-8"));
33
+ }
34
+ catch {
35
+ return { healed: false, action: "none" };
36
+ }
37
+ for (const [key, entries] of Object.entries(ip.plugins ?? {})) {
38
+ if (!key.toLowerCase().includes("context-mode"))
39
+ continue;
40
+ for (const entry of entries) {
41
+ const registryPath = entry.installPath;
42
+ if (!registryPath)
43
+ continue;
44
+ // Registry path exists — no healing needed
45
+ if (existsSync(registryPath))
46
+ continue;
47
+ // Registry path doesn't exist — create symlink to our directory
48
+ try {
49
+ const parent = dirname(registryPath);
50
+ if (!existsSync(parent))
51
+ mkdirSync(parent, { recursive: true });
52
+ if (process.platform === "win32") {
53
+ // Windows: use junction (no admin required)
54
+ symlinkSync(currentDir, registryPath, "junction");
55
+ }
56
+ else {
57
+ symlinkSync(currentDir, registryPath);
58
+ }
59
+ return { healed: true, action: "symlink", from: registryPath, to: currentDir };
60
+ }
61
+ catch {
62
+ return { healed: false, action: "none" };
63
+ }
64
+ }
65
+ }
66
+ return { healed: false, action: "none" };
67
+ }
68
+ /**
69
+ * Deploy a global SessionStart hook that heals plugin cache mismatches.
70
+ * This hook lives outside the plugin directory, so it survives cache breakage.
71
+ *
72
+ * Written to ~/.claude/hooks/context-mode-cache-heal.sh
73
+ */
74
+ export function deployGlobalHealHook() {
75
+ const hooksDir = resolve(homedir(), ".claude", "hooks");
76
+ const hookPath = resolve(hooksDir, "context-mode-cache-heal.sh");
77
+ // Already deployed
78
+ if (existsSync(hookPath))
79
+ return { healed: false, action: "none" };
80
+ try {
81
+ if (!existsSync(hooksDir))
82
+ mkdirSync(hooksDir, { recursive: true });
83
+ const script = `#!/usr/bin/env bash
84
+ # context-mode plugin cache self-heal — auto-deployed by context-mode MCP server
85
+ # Fixes anthropics/claude-code#46915: auto-update breaks CLAUDE_PLUGIN_ROOT
86
+ # This hook runs at SessionStart (global, not plugin-level) so it works even
87
+ # when the plugin cache is broken.
88
+
89
+ set -euo pipefail
90
+
91
+ PLUGINS_FILE="$HOME/.claude/plugins/installed_plugins.json"
92
+ [[ -f "$PLUGINS_FILE" ]] || exit 0
93
+
94
+ # Find context-mode entries and heal missing directories
95
+ node -e '
96
+ const fs = require("fs");
97
+ const path = require("path");
98
+ try {
99
+ const ip = JSON.parse(fs.readFileSync(process.argv[1], "utf-8"));
100
+ for (const [key, entries] of Object.entries(ip.plugins || {})) {
101
+ if (!key.toLowerCase().includes("context-mode")) continue;
102
+ for (const entry of entries) {
103
+ const p = entry.installPath;
104
+ if (!p || fs.existsSync(p)) continue;
105
+ const parent = path.dirname(p);
106
+ if (!fs.existsSync(parent)) continue;
107
+ const dirs = fs.readdirSync(parent).filter(d => /^\\d+\\.\\d+/.test(d) && fs.statSync(path.join(parent, d)).isDirectory());
108
+ if (dirs.length === 0) continue;
109
+ dirs.sort((a, b) => {
110
+ const pa = a.split(".").map(Number), pb = b.split(".").map(Number);
111
+ for (let i = 0; i < 3; i++) { if ((pa[i]||0) !== (pb[i]||0)) return (pa[i]||0) - (pb[i]||0); }
112
+ return 0;
113
+ });
114
+ const target = path.join(parent, dirs[dirs.length - 1]);
115
+ try { fs.symlinkSync(target, p); } catch {}
116
+ }
117
+ }
118
+ } catch {}
119
+ ' "$PLUGINS_FILE" 2>/dev/null || true
120
+ `;
121
+ writeFileSync(hookPath, script, { mode: 0o755 });
122
+ return { healed: true, action: "global-hook", from: hookPath };
123
+ }
124
+ catch {
125
+ return { healed: false, action: "none" };
126
+ }
127
+ }
128
+ /**
129
+ * Backward symlink: during postinstall, if the registry points to a
130
+ * non-existent OLD path, create a symlink from old → new (our directory).
131
+ * Same as healRegistryMismatch but called from postinstall context.
132
+ */
133
+ export { healRegistryMismatch as healBackwardCompat };
134
+ /** One-shot flag for mid-session heal in server.ts */
135
+ let _midSessionHealed = false;
136
+ /**
137
+ * Mid-session heal — call on first MCP tool invocation.
138
+ * Checks if registry path differs from our running directory.
139
+ * Creates symlink if needed. Runs only once per process.
140
+ */
141
+ export function healMidSession(currentDir) {
142
+ if (_midSessionHealed)
143
+ return { healed: false, action: "none" };
144
+ _midSessionHealed = true;
145
+ return healRegistryMismatch(currentDir);
146
+ }
147
+ /** Reset mid-session flag (for testing only) */
148
+ export function _resetMidSession() {
149
+ _midSessionHealed = false;
150
+ }
@@ -19,6 +19,15 @@ interface ExecuteOptions {
19
19
  timeout?: number;
20
20
  /** Keep process running after timeout instead of killing it. */
21
21
  background?: boolean;
22
+ /**
23
+ * Issue #45 — per-call cwd override for the shell language. When set,
24
+ * the shell script runs in this directory instead of `#projectRoot`.
25
+ * Non-shell languages keep their tmpDir sandbox cwd regardless (the
26
+ * script file lives there). Used by Codex MCP handlers to pin shell
27
+ * commands to a resolved project root when the spawning host inherited
28
+ * a non-project cwd (e.g. $HOME).
29
+ */
30
+ cwd?: string;
22
31
  }
23
32
  interface ExecuteFileOptions extends ExecuteOptions {
24
33
  path: string;
package/build/executor.js CHANGED
@@ -130,7 +130,7 @@ export class PolyglotExecutor {
130
130
  this.#backgroundedPids.clear();
131
131
  }
132
132
  async execute(opts) {
133
- const { language, code, timeout, background = false } = opts;
133
+ const { language, code, timeout, background = false, cwd: cwdOverride } = opts;
134
134
  const tmpDir = mkdtempSync(join(OS_TMPDIR, ".ctx-mode-"));
135
135
  try {
136
136
  const filePath = this.#writeScript(tmpDir, code, language);
@@ -142,7 +142,11 @@ export class PolyglotExecutor {
142
142
  // Shell commands run in the project directory so git, relative paths,
143
143
  // and other project-aware tools work naturally. Non-shell languages
144
144
  // run in the temp directory where their script file is written.
145
- const cwd = language === "shell" ? this.#projectRoot : tmpDir;
145
+ // Issue #45 `cwdOverride` lets per-call sites (Codex MCP handlers)
146
+ // pin shell cwd without mutating process-wide state.
147
+ const cwd = language === "shell"
148
+ ? (cwdOverride ?? this.#projectRoot)
149
+ : tmpDir;
146
150
  const result = await this.#spawn(cmd, cwd, tmpDir, timeout, background);
147
151
  // Skip tmpDir cleanup if process was backgrounded — it may still need files
148
152
  if (!result.backgrounded) {
@@ -179,7 +179,7 @@ async function createContextModePlugin(ctx) {
179
179
  const toolInput = output.args ?? {};
180
180
  let decision;
181
181
  try {
182
- decision = routing.routePreToolUse(toolName, toolInput, projectDir, getPlatform());
182
+ decision = routing.routePreToolUse(toolName, toolInput, projectDir, platform);
183
183
  }
184
184
  catch {
185
185
  return; // Routing failure → allow passthrough
@@ -194,7 +194,10 @@ async function createContextModePlugin(ctx) {
194
194
  // Mutate output.args — OpenCode reads the mutated output object
195
195
  Object.assign(output.args, decision.updatedInput);
196
196
  }
197
- // "context" action no-op (OpenCode doesn't support context injection)
197
+ if (decision.action === "context" && decision.additionalContext) {
198
+ // Mutate output.args — OpenCode reads the mutated output object
199
+ output.args.additionalContext = decision.additionalContext;
200
+ }
198
201
  },
199
202
  // ── PostToolUse: Session event capture ──────────────
200
203
  "tool.execute.after": async (input, output) => {
@@ -0,0 +1,8 @@
1
+ import type { ToolNamer } from "./tool-naming.js";
2
+ export interface RoutingBlockOptions {
3
+ includeCommands?: boolean;
4
+ }
5
+ export declare function createRoutingBlock(t: ToolNamer, options?: RoutingBlockOptions): string;
6
+ export declare function createReadGuidance(t: ToolNamer): string;
7
+ export declare function createGrepGuidance(t: ToolNamer): string;
8
+ export declare function createBashGuidance(t: ToolNamer): string;
@@ -0,0 +1,86 @@
1
+ export function createRoutingBlock(t, options = {}) {
2
+ const { includeCommands = true } = options;
3
+ return `
4
+ <context_window_protection>
5
+ <priority_instructions>
6
+ Raw tool output floods context window. MUST use context-mode MCP tools. Keep raw data in sandbox.
7
+ </priority_instructions>
8
+
9
+ <tool_selection_hierarchy>
10
+ 0. MEMORY: ${t("ctx_search")}(sort: "timeline")
11
+ - After resume, check prior context before asking user.
12
+ 1. GATHER: ${t("ctx_batch_execute")}(commands, queries)
13
+ - Primary research tool. Runs commands, auto-indexes, searches. ONE call replaces many steps.
14
+ - Each command: {label: "section header", command: "shell command"}
15
+ - label becomes FTS5 chunk title — descriptive labels improve search.
16
+ 2. FOLLOW-UP: ${t("ctx_search")}(queries: ["q1", "q2", ...])
17
+ - All follow-up questions. ONE call, many queries (default relevance mode).
18
+ 3. PROCESSING: ${t("ctx_execute")}(language, code) | ${t("ctx_execute_file")}(path, language, code)
19
+ - API calls, log analysis, data processing.
20
+ </tool_selection_hierarchy>
21
+
22
+ <forbidden_actions>
23
+ - NO Bash for commands producing >20 lines output.
24
+ - NO Read for analysis — use execute_file. Read IS correct for files you intend to Edit.
25
+ - NO WebFetch — use ${t("ctx_fetch_and_index")}.
26
+ - Bash ONLY for git/mkdir/rm/mv/navigation.
27
+ - NO ${t("ctx_execute")} or ${t("ctx_execute_file")} for file creation/modification.
28
+ ctx_execute is for analysis, processing, computation only.
29
+ </forbidden_actions>
30
+
31
+ <file_writing_policy>
32
+ ALWAYS use native Write/Edit tools for file creation/modification.
33
+ NEVER use ${t("ctx_execute")}, ${t("ctx_execute_file")}, or Bash to write files.
34
+ Applies to all file types: code, configs, plans, specs, YAML, JSON, markdown.
35
+ </file_writing_policy>
36
+
37
+ <output_constraints>
38
+ <communication_style>
39
+ Terse like caveman. Technical substance exact. Only fluff die.
40
+ Use fragments when clear. Short synonyms (fix not "implement a solution for").
41
+ Technical terms exact. Code blocks unchanged.
42
+ Auto-expand for: security warnings, irreversible actions, user confusion.
43
+ </communication_style>
44
+ <artifact_policy>
45
+ Write artifacts (code, configs, PRDs) to FILES. NEVER inline.
46
+ Return only: file path + 1-line description.
47
+ </artifact_policy>
48
+ <response_format>
49
+ Concise summary:
50
+ - Actions taken (2-3 bullets)
51
+ - File paths created/modified
52
+ - Key findings
53
+ </response_format>
54
+ </output_constraints>
55
+ <session_continuity>
56
+ Skills, roles, and decisions set during this session remain active until the user revokes them.
57
+ Do not drop behavioral directives as context grows.
58
+ </session_continuity>
59
+ ${includeCommands ? `
60
+ <ctx_commands>
61
+ "ctx stats" | "ctx-stats" | "/ctx-stats" | context savings question
62
+ → Call stats MCP tool, display full output verbatim.
63
+
64
+ "ctx doctor" | "ctx-doctor" | "/ctx-doctor" | diagnose context-mode
65
+ → Call doctor MCP tool, run returned shell command, display as checklist.
66
+
67
+ "ctx upgrade" | "ctx-upgrade" | "/ctx-upgrade" | update context-mode
68
+ → Call upgrade MCP tool, run returned shell command, display as checklist.
69
+
70
+ "ctx purge" | "ctx-purge" | "/ctx-purge" | wipe/reset knowledge base
71
+ → Call purge MCP tool with confirm: true. Warn: irreversible.
72
+
73
+ After /clear or /compact: knowledge base preserved. Tell user: "context-mode knowledge base preserved. Use \`ctx purge\` to start fresh."
74
+ </ctx_commands>
75
+ ` : ''}
76
+ </context_window_protection>`;
77
+ }
78
+ export function createReadGuidance(t) {
79
+ return '<context_guidance>\n <tip>\n Reading to Edit? Read is correct — Edit needs content in context.\n Reading to analyze/explore? Use ' + t("ctx_execute_file") + '(path, language, code) — only printed summary enters context.\n </tip>\n</context_guidance>';
80
+ }
81
+ export function createGrepGuidance(t) {
82
+ return '<context_guidance>\n <tip>\n May flood context. Use ' + t("ctx_execute") + '(language: "shell", code: "...") to run searches in sandbox. Only printed summary enters context.\n </tip>\n</context_guidance>';
83
+ }
84
+ export function createBashGuidance(t) {
85
+ return '<context_guidance>\n <tip>\n May produce large output. Use ' + t("ctx_batch_execute") + '(commands, queries) for multiple commands, ' + t("ctx_execute") + '(language: "shell", code: "...") for single. Only printed summary enters context. Bash only for: git, mkdir, rm, mv, navigation.\n </tip>\n</context_guidance>';
86
+ }
package/build/server.d.ts CHANGED
@@ -78,6 +78,18 @@ export declare function resolveSessionIdFromSessionDB(opts?: {
78
78
  sessionsDir?: string;
79
79
  bypassCache?: boolean;
80
80
  }): string | undefined;
81
+ /**
82
+ * Project directory detection across supported platforms.
83
+ *
84
+ * Priority:
85
+ * 1. Platform-specific env var (set by host IDE before MCP server spawn)
86
+ * 2. CONTEXT_MODE_PROJECT_DIR (set by start.mjs for ALL platforms — universal)
87
+ * 3. process.cwd() (last resort)
88
+ *
89
+ * CONTEXT_MODE_PROJECT_DIR guarantees correct projectDir even for platforms
90
+ * that don't set their own env var (Cursor, OpenClaw, Codex, Kiro, Zed).
91
+ */
92
+ export declare function getProjectDir(): string;
81
93
  /**
82
94
  * Parse FTS5 highlight markers to find match positions in the
83
95
  * original (marker-free) text. Returns character offsets into the
package/build/server.js CHANGED
@@ -465,7 +465,7 @@ function getSessionDir() {
465
465
  * CONTEXT_MODE_PROJECT_DIR guarantees correct projectDir even for platforms
466
466
  * that don't set their own env var (Cursor, OpenClaw, Codex, Kiro, Zed).
467
467
  */
468
- function getProjectDir() {
468
+ export function getProjectDir() {
469
469
  const override = projectDirOverride.getStore();
470
470
  if (override)
471
471
  return override.projectDir;
@@ -500,14 +500,22 @@ function getProjectDir() {
500
500
  // and the legacy literal cascade is preserved there for semver safety.
501
501
  let transcriptsRoot;
502
502
  let strictPlatform;
503
+ let codexHome;
503
504
  try {
504
505
  const detected = detectPlatform().platform;
505
506
  strictPlatform = detected;
506
507
  if (detected === "claude-code") {
507
508
  transcriptsRoot = join(homedir(), ".claude", "projects");
508
509
  }
510
+ // Issue #45 — Codex publishes no workspace env var, so the resolver
511
+ // reads `meta.cwd` from the most-recently-modified session.jsonl under
512
+ // `${codexHome}/sessions/`. Wire codexHome at the call site so the
513
+ // resolver can be exercised under test without process-level mutation.
514
+ if (detected === "codex") {
515
+ codexHome = process.env.CODEX_HOME ?? join(homedir(), ".codex");
516
+ }
509
517
  }
510
- catch { /* detection failure — leave both undefined, resolver uses legacy cascade */ }
518
+ catch { /* detection failure — leave undefined, resolver uses legacy cascade */ }
511
519
  return resolveProjectDir({
512
520
  env: process.env,
513
521
  cwd: process.cwd(),
@@ -515,6 +523,7 @@ function getProjectDir() {
515
523
  transcriptsRoot,
516
524
  transcriptMaxAgeMs: 5 * 60 * 1000,
517
525
  strictPlatform,
526
+ codexHome,
518
527
  });
519
528
  }
520
529
  /**
@@ -1731,13 +1740,20 @@ EXAMPLE: ctx_index(path: "/path/to/large-spec.md", source: "openapi-v2-spec")`,
1731
1740
  path: z
1732
1741
  .string()
1733
1742
  .optional()
1734
- .describe("File path to read and index (content never enters context). Provide this OR content."),
1743
+ .describe("File OR directory path to read and index (content never enters context). Provide this OR content. Directory paths trigger a bounded recursive walk (#687)."),
1735
1744
  source: z
1736
1745
  .string()
1737
1746
  .optional()
1738
1747
  .describe("Label for the indexed content (e.g., 'Context7: React useEffect', 'Skill: frontend-design')"),
1748
+ include: z.array(z.string()).optional().describe("Directory-only: glob patterns to include (default: all matching extensions)."),
1749
+ exclude: z.array(z.string()).optional().describe("Directory-only: glob patterns to exclude. Merged with defaults (node_modules, .git, dist, build, .next, coverage, .venv, __pycache__, .DS_Store)."),
1750
+ maxDepth: z.number().int().min(0).optional().describe("Directory-only: max recursion depth from root (default: 5)."),
1751
+ maxFiles: z.number().int().min(1).optional().describe("Directory-only: hard cap on files indexed (default: 200) — FTS5 blow-up guard."),
1752
+ extensions: z.array(z.string()).optional().describe("Directory-only: allowed file extensions (default: .md .mdx .txt .json .yaml .yml .ts .tsx .js .jsx .py .rs .go .sh)."),
1753
+ respectGitignore: z.boolean().optional().describe("Directory-only: apply nearest .gitignore (default: true)."),
1754
+ followSymlinks: z.boolean().optional().describe("Directory-only: follow directory symlinks (default: false — cycle hazard + escape risk)."),
1739
1755
  }),
1740
- }, async ({ content, path, source }) => {
1756
+ }, async ({ content, path, source, include, exclude, maxDepth, maxFiles, extensions, respectGitignore, followSymlinks }) => {
1741
1757
  if (!content && !path) {
1742
1758
  return trackResponse("ctx_index", {
1743
1759
  content: [
@@ -1760,6 +1776,54 @@ EXAMPLE: ctx_index(path: "/path/to/large-spec.md", source: "openapi-v2-spec")`,
1760
1776
  }
1761
1777
  try {
1762
1778
  const resolvedPath = path ? resolveProjectPath(path) : undefined;
1779
+ // Directory dispatch (#687, reported by @matiasduartee). When the
1780
+ // resolved path is a directory, walk it bounded and re-enter `index()`
1781
+ // per-file so the security gate at store.ts:845 (TOCTOU defense from
1782
+ // #442 round-3) keeps running for every file.
1783
+ if (resolvedPath && existsSync(resolvedPath) && statSync(resolvedPath).isDirectory()) {
1784
+ const store = getStore();
1785
+ const projectDir = getProjectDir();
1786
+ const denyGlobs = readToolDenyPatterns("Read", projectDir);
1787
+ const isWin32 = process.platform === "win32";
1788
+ const perFileDeny = (absPath) => {
1789
+ try {
1790
+ return evaluateFilePath(absPath, denyGlobs, isWin32, projectDir).denied;
1791
+ }
1792
+ catch {
1793
+ return false; // fail-open consistent with checkFilePathDenyPolicy
1794
+ }
1795
+ };
1796
+ const dirResult = store.indexDirectory({
1797
+ path: resolvedPath,
1798
+ source: source ?? resolvedPath,
1799
+ attribution: currentAttribution(),
1800
+ perFileDeny,
1801
+ include,
1802
+ exclude,
1803
+ maxDepth,
1804
+ maxFiles,
1805
+ extensions,
1806
+ respectGitignore,
1807
+ followSymlinks,
1808
+ });
1809
+ const capNote = dirResult.capped
1810
+ ? ` (cap reached — only first ${dirResult.filesIndexed} of ${dirResult.totalSeen}+ files; raise maxFiles to index more)`
1811
+ : "";
1812
+ const denyNote = dirResult.denied > 0
1813
+ ? ` (${dirResult.denied} file${dirResult.denied === 1 ? "" : "s"} blocked by Read deny policy)`
1814
+ : "";
1815
+ const failNote = dirResult.failed > 0
1816
+ ? ` (${dirResult.failed} file${dirResult.failed === 1 ? "" : "s"} failed to read)`
1817
+ : "";
1818
+ return trackResponse("ctx_index", {
1819
+ content: [
1820
+ {
1821
+ type: "text",
1822
+ text: `Indexed ${dirResult.filesIndexed} file${dirResult.filesIndexed === 1 ? "" : "s"} (${dirResult.totalChunks} sections) from directory: ${dirResult.label}${capNote}${denyNote}${failNote}\nUse ctx_search(queries: ["..."]) to query this content.`,
1823
+ },
1824
+ ],
1825
+ });
1826
+ }
1763
1827
  // Track the raw bytes being indexed (content or file)
1764
1828
  if (content)
1765
1829
  trackIndexed(Buffer.byteLength(content));
@@ -0,0 +1,56 @@
1
+ /**
2
+ * walkDirectory — bounded recursive directory walker for ctx_index (#687).
3
+ *
4
+ * Issue: ctx_index refused directory paths via the security gate at
5
+ * src/store.ts:845 ("refusing to index <path>: not a regular file"). The gate
6
+ * is a TOCTOU defense from #442 round-3 and MUST be preserved — directory
7
+ * support is layered as a separate concern here. Each file produced by
8
+ * walkDirectory is then read via the existing per-file
9
+ * `openSync + fstatSync.isFile()` invariant in `ContentStore.index()`.
10
+ *
11
+ * Reported by @matiasduartee across 4 clients × Windows 11.
12
+ * https://github.com/anthropic-experimental/context-mode/issues/687
13
+ *
14
+ * Design constraints:
15
+ * - No new dependencies (avoid the `ignore` package — issue #687 Diagnose).
16
+ * - Cross-OS: path.sep / path.join everywhere, never raw "/" string ops.
17
+ * - Symlink cycle detection via a resolved-path Set.
18
+ * - Symlink-escape rejection: refuse to follow symlinks that resolve outside
19
+ * the rootPath (defense-in-depth alongside per-file checkFilePathDenyPolicy).
20
+ * - FTS5-blowup guard: hard cap maxFiles (default 200, per Architect).
21
+ */
22
+ export interface WalkOptions {
23
+ /** Glob-ish include patterns. Empty/undefined means include all (subject to extensions). */
24
+ include?: string[];
25
+ /** Glob-ish exclude patterns. Merged with sensible defaults. */
26
+ exclude?: string[];
27
+ /** Max recursion depth from rootPath (0 = root only). Default 5. */
28
+ maxDepth?: number;
29
+ /** Hard cap on total files. Default 200 — FTS5 blow-up guard. */
30
+ maxFiles?: number;
31
+ /** Allowed file extensions (with leading dot). Empty/undefined means default set. */
32
+ extensions?: string[];
33
+ /** Apply nearest .gitignore rules during walk. Default true. */
34
+ respectGitignore?: boolean;
35
+ /** Follow directory symlinks. Default false (cycle hazard + escape risk). */
36
+ followSymlinks?: boolean;
37
+ }
38
+ export interface WalkResult {
39
+ files: string[];
40
+ /** True when maxFiles cap was hit and traversal halted early. */
41
+ capped: boolean;
42
+ /** Total files discovered before cap (for reporting). */
43
+ totalSeen: number;
44
+ }
45
+ /**
46
+ * Walk `rootPath` recursively under the given bounds and return absolute file
47
+ * paths matching the filters. Pure synchronous traversal — no allocations
48
+ * beyond the result array. Symlink cycles are detected via a resolved-path
49
+ * Set; symlink escapes (resolving outside rootPath) are silently skipped.
50
+ */
51
+ export declare function walkDirectory(rootPath: string, opts?: WalkOptions): string[];
52
+ /**
53
+ * Same as walkDirectory but returns capped + totalSeen so callers can surface
54
+ * a "capped at N files" notice in their response.
55
+ */
56
+ export declare function walkDirectoryDetailed(rootPath: string, opts?: WalkOptions): WalkResult;