context-mode 1.0.148 → 1.0.149

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.149"
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.149",
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.149",
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.149",
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.149",
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.149",
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",
@@ -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) {
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;
@@ -0,0 +1,254 @@
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
+ import { readdirSync, statSync, lstatSync, realpathSync, existsSync, readFileSync, } from "node:fs";
23
+ import { join, extname, relative, sep, resolve } from "node:path";
24
+ const DEFAULT_EXCLUDES = [
25
+ "node_modules",
26
+ ".git",
27
+ "dist",
28
+ "build",
29
+ ".next",
30
+ "coverage",
31
+ ".venv",
32
+ "__pycache__",
33
+ ".DS_Store",
34
+ ];
35
+ const DEFAULT_EXTENSIONS = [
36
+ ".md",
37
+ ".mdx",
38
+ ".txt",
39
+ ".json",
40
+ ".yaml",
41
+ ".yml",
42
+ ".ts",
43
+ ".tsx",
44
+ ".js",
45
+ ".jsx",
46
+ ".py",
47
+ ".rs",
48
+ ".go",
49
+ ".sh",
50
+ ];
51
+ const DEFAULT_MAX_DEPTH = 5;
52
+ const DEFAULT_MAX_FILES = 200;
53
+ /**
54
+ * Convert a simple glob pattern (`*`, `**`, `?`) to a RegExp. Anchors at
55
+ * boundaries so `node_modules` matches `node_modules` AND `node_modules/pkg`.
56
+ * Patterns are matched against POSIX-style relative paths (forward slashes)
57
+ * to give consistent behavior across macOS / Windows.
58
+ */
59
+ function globToRegExp(pattern) {
60
+ // Escape regex metachars except glob ones.
61
+ let re = "";
62
+ for (let i = 0; i < pattern.length; i++) {
63
+ const c = pattern[i];
64
+ if (c === "*") {
65
+ if (pattern[i + 1] === "*") {
66
+ re += ".*";
67
+ i++;
68
+ }
69
+ else {
70
+ re += "[^/]*";
71
+ }
72
+ }
73
+ else if (c === "?") {
74
+ re += "[^/]";
75
+ }
76
+ else if ("\\^$.|+()[]{}".includes(c)) {
77
+ re += "\\" + c;
78
+ }
79
+ else {
80
+ re += c;
81
+ }
82
+ }
83
+ return new RegExp(`^${re}$`);
84
+ }
85
+ /** Match a posix-style relative path against any of the patterns. */
86
+ function matchesAny(relPosix, patterns) {
87
+ if (patterns.length === 0)
88
+ return false;
89
+ const basename = relPosix.split("/").pop() ?? relPosix;
90
+ for (const p of patterns) {
91
+ // Bare names match basename OR any path segment.
92
+ if (!p.includes("/") && !p.includes("*")) {
93
+ if (basename === p)
94
+ return true;
95
+ if (relPosix.split("/").includes(p))
96
+ return true;
97
+ continue;
98
+ }
99
+ const re = globToRegExp(p);
100
+ if (re.test(relPosix))
101
+ return true;
102
+ if (re.test(basename))
103
+ return true;
104
+ }
105
+ return false;
106
+ }
107
+ /**
108
+ * Parse a .gitignore file into a list of patterns. Comments and blank lines
109
+ * are stripped. Negation (`!`) is not supported — kept conservative.
110
+ */
111
+ function parseGitignore(rootPath) {
112
+ const giPath = join(rootPath, ".gitignore");
113
+ if (!existsSync(giPath))
114
+ return [];
115
+ try {
116
+ const text = readFileSync(giPath, "utf-8");
117
+ return text
118
+ .split(/\r?\n/)
119
+ .map(l => l.trim())
120
+ .filter(l => l.length > 0 && !l.startsWith("#") && !l.startsWith("!"))
121
+ .map(l => l.replace(/^\//, "").replace(/\/$/, ""));
122
+ }
123
+ catch {
124
+ return [];
125
+ }
126
+ }
127
+ /**
128
+ * Convert an absolute path under rootPath to a POSIX-style relative path
129
+ * so glob matching is identical across macOS/Linux/Windows.
130
+ */
131
+ function toPosixRel(rootPath, absPath) {
132
+ const rel = relative(rootPath, absPath);
133
+ return rel.split(sep).join("/");
134
+ }
135
+ /**
136
+ * Walk `rootPath` recursively under the given bounds and return absolute file
137
+ * paths matching the filters. Pure synchronous traversal — no allocations
138
+ * beyond the result array. Symlink cycles are detected via a resolved-path
139
+ * Set; symlink escapes (resolving outside rootPath) are silently skipped.
140
+ */
141
+ export function walkDirectory(rootPath, opts = {}) {
142
+ return walkDirectoryDetailed(rootPath, opts).files;
143
+ }
144
+ /**
145
+ * Same as walkDirectory but returns capped + totalSeen so callers can surface
146
+ * a "capped at N files" notice in their response.
147
+ */
148
+ export function walkDirectoryDetailed(rootPath, opts = {}) {
149
+ const { include, exclude, maxDepth = DEFAULT_MAX_DEPTH, maxFiles = DEFAULT_MAX_FILES, extensions, respectGitignore = true, followSymlinks = false, } = opts;
150
+ // Normalize rootPath to its real path so symlink-escape detection is sound.
151
+ let rootReal;
152
+ try {
153
+ rootReal = realpathSync(rootPath);
154
+ }
155
+ catch {
156
+ return { files: [], capped: false, totalSeen: 0 };
157
+ }
158
+ const exts = (extensions && extensions.length > 0 ? extensions : DEFAULT_EXTENSIONS)
159
+ .map(e => (e.startsWith(".") ? e : "." + e).toLowerCase());
160
+ const excludes = [
161
+ ...DEFAULT_EXCLUDES,
162
+ ...(exclude ?? []),
163
+ ...(respectGitignore ? parseGitignore(rootReal) : []),
164
+ ];
165
+ const includes = include ?? [];
166
+ const out = [];
167
+ const visited = new Set([rootReal]);
168
+ let totalSeen = 0;
169
+ let capped = false;
170
+ function walk(absDir, depth) {
171
+ if (capped)
172
+ return;
173
+ if (depth > maxDepth)
174
+ return;
175
+ let entries;
176
+ try {
177
+ entries = readdirSync(absDir, { withFileTypes: true });
178
+ }
179
+ catch {
180
+ return; // unreadable directory — skip silently
181
+ }
182
+ for (const ent of entries) {
183
+ if (capped)
184
+ return;
185
+ const absChild = join(absDir, ent.name);
186
+ const relPosix = toPosixRel(rootReal, absChild);
187
+ // Exclude check applies to both files and dirs — early prune.
188
+ if (matchesAny(relPosix, excludes))
189
+ continue;
190
+ // Include filter applies to files only — see below.
191
+ // Resolve symlinks once; reject escapes; track for cycle detection.
192
+ let isDirChild = ent.isDirectory();
193
+ let isFileChild = ent.isFile();
194
+ let isSymlink = false;
195
+ try {
196
+ const lst = lstatSync(absChild);
197
+ isSymlink = lst.isSymbolicLink();
198
+ }
199
+ catch {
200
+ continue;
201
+ }
202
+ if (isSymlink) {
203
+ if (!followSymlinks)
204
+ continue;
205
+ let resolved;
206
+ try {
207
+ resolved = realpathSync(absChild);
208
+ }
209
+ catch {
210
+ continue; // dangling
211
+ }
212
+ // Symlink-escape: refuse to follow if the resolved target leaves rootReal.
213
+ const escapeRel = relative(rootReal, resolved);
214
+ if (escapeRel.startsWith("..") || resolve(escapeRel) === resolved) {
215
+ // resolve(absolute) === absolute → target is absolute outside root
216
+ if (escapeRel.startsWith(".."))
217
+ continue;
218
+ }
219
+ if (visited.has(resolved))
220
+ continue;
221
+ visited.add(resolved);
222
+ try {
223
+ const st = statSync(resolved);
224
+ isDirChild = st.isDirectory();
225
+ isFileChild = st.isFile();
226
+ }
227
+ catch {
228
+ continue;
229
+ }
230
+ }
231
+ if (isDirChild) {
232
+ walk(absChild, depth + 1);
233
+ continue;
234
+ }
235
+ if (!isFileChild)
236
+ continue;
237
+ // Extension filter.
238
+ const ext = extname(absChild).toLowerCase();
239
+ if (!exts.includes(ext))
240
+ continue;
241
+ // Include filter (if any): file must match at least one include pattern.
242
+ if (includes.length > 0 && !matchesAny(relPosix, includes))
243
+ continue;
244
+ totalSeen++;
245
+ if (out.length >= maxFiles) {
246
+ capped = true;
247
+ return;
248
+ }
249
+ out.push(absChild);
250
+ }
251
+ }
252
+ walk(rootReal, 0);
253
+ return { files: out, capped, totalSeen };
254
+ }
package/build/store.d.ts CHANGED
@@ -7,6 +7,7 @@
7
7
  * Use for documentation, API references, and any content where
8
8
  * you need EXACT text later — not summaries.
9
9
  */
10
+ import { type WalkOptions } from "./store-directory.js";
10
11
  type SourceMatchMode = "like" | "exact";
11
12
  import type { IndexResult, SearchResult, StoreStats } from "./types.js";
12
13
  export type { IndexResult, SearchResult, StoreStats } from "./types.js";
@@ -51,6 +52,34 @@ export declare class ContentStore {
51
52
  eventId?: string;
52
53
  };
53
54
  }): IndexResult;
55
+ /**
56
+ * Index every file under a directory by walking it with `walkDirectory` and
57
+ * delegating each discovered file to `this.index({ path })`. The per-file
58
+ * `openSync + fstatSync.isFile()` security gate at line ~845 stays active
59
+ * for every file — directory support never bypasses the TOCTOU defense
60
+ * from #442 round-3.
61
+ *
62
+ * Reported by @matiasduartee in #687.
63
+ */
64
+ indexDirectory(opts: {
65
+ path: string;
66
+ source?: string;
67
+ attribution?: {
68
+ sessionId?: string;
69
+ eventId?: string;
70
+ };
71
+ /** Optional per-file deny check — runs INSIDE the walk loop so a denied
72
+ * file does not even open a fd. Returns true to deny. */
73
+ perFileDeny?: (absPath: string) => boolean;
74
+ } & WalkOptions): {
75
+ filesIndexed: number;
76
+ totalChunks: number;
77
+ capped: boolean;
78
+ totalSeen: number;
79
+ denied: number;
80
+ failed: number;
81
+ label: string;
82
+ };
54
83
  /**
55
84
  * Index plain-text output (logs, build output, test results) by splitting
56
85
  * into fixed-size line groups. Unlike markdown indexing, this does not
package/build/store.js CHANGED
@@ -13,6 +13,7 @@ import { readFileSync, readdirSync, unlinkSync, existsSync, statSync, openSync,
13
13
  import { createHash } from "node:crypto";
14
14
  import { tmpdir } from "node:os";
15
15
  import { join } from "node:path";
16
+ import { walkDirectoryDetailed } from "./store-directory.js";
16
17
  // ─────────────────────────────────────────────────────────
17
18
  // Constants
18
19
  // ─────────────────────────────────────────────────────────
@@ -756,6 +757,51 @@ export class ContentStore {
756
757
  const contentHash = filePath ? createHash("sha256").update(text).digest("hex") : undefined;
757
758
  return withRetry(() => this.#insertChunks(chunks, label, text, filePath, contentHash, attribution));
758
759
  }
760
+ // ── Index Directory (#687) ──
761
+ /**
762
+ * Index every file under a directory by walking it with `walkDirectory` and
763
+ * delegating each discovered file to `this.index({ path })`. The per-file
764
+ * `openSync + fstatSync.isFile()` security gate at line ~845 stays active
765
+ * for every file — directory support never bypasses the TOCTOU defense
766
+ * from #442 round-3.
767
+ *
768
+ * Reported by @matiasduartee in #687.
769
+ */
770
+ indexDirectory(opts) {
771
+ const { path: rootPath, source, attribution, perFileDeny, ...walkOpts } = opts;
772
+ const walked = walkDirectoryDetailed(rootPath, walkOpts);
773
+ let filesIndexed = 0;
774
+ let totalChunks = 0;
775
+ let denied = 0;
776
+ let failed = 0;
777
+ for (const file of walked.files) {
778
+ if (perFileDeny && perFileDeny(file)) {
779
+ denied++;
780
+ continue;
781
+ }
782
+ try {
783
+ // Per-file source label so ctx_search(source: "<file>") still works.
784
+ const fileSource = source ? `${source}:${file}` : file;
785
+ const r = this.index({ path: file, source: fileSource, attribution });
786
+ filesIndexed++;
787
+ totalChunks += r.totalChunks;
788
+ }
789
+ catch {
790
+ // Per-file failure (e.g. fd-bound fstat rejection of a non-regular
791
+ // file that races between walk and read) — count + continue.
792
+ failed++;
793
+ }
794
+ }
795
+ return {
796
+ filesIndexed,
797
+ totalChunks,
798
+ capped: walked.capped,
799
+ totalSeen: walked.totalSeen,
800
+ denied,
801
+ failed,
802
+ label: source ?? rootPath,
803
+ };
804
+ }
759
805
  // ── Index Plain Text ──
760
806
  /**
761
807
  * Index plain-text output (logs, build output, test results) by splitting