dsh-context-mode 0.1.3 → 0.2.0

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.
Files changed (58) hide show
  1. package/LICENSING.md +37 -0
  2. package/README.md +39 -13
  3. package/lib/types/cjk.d.ts +54 -0
  4. package/lib/types/cjk.d.ts.map +1 -0
  5. package/lib/types/cjk.js +64 -0
  6. package/lib/types/index.d.ts.map +1 -1
  7. package/lib/types/index.js +65 -21
  8. package/lib/types/output-containment.d.ts +35 -0
  9. package/lib/types/output-containment.d.ts.map +1 -0
  10. package/lib/types/output-containment.js +103 -0
  11. package/lib/types/routing.d.ts +3 -1
  12. package/lib/types/routing.d.ts.map +1 -1
  13. package/lib/types/routing.js +81 -6
  14. package/package.json +9 -5
  15. package/skills/context-mode/SKILL.md +104 -11
  16. package/vendor/context-mode/LICENSE +94 -0
  17. package/vendor/context-mode/server.bundle.mjs +1126 -0
  18. package/vendor/context-mode/src/cli.ts +2040 -0
  19. package/vendor/context-mode/src/db-base.ts +617 -0
  20. package/vendor/context-mode/src/executor.ts +785 -0
  21. package/vendor/context-mode/src/exit-classify.ts +33 -0
  22. package/vendor/context-mode/src/fetch-cache.ts +15 -0
  23. package/vendor/context-mode/src/lifecycle.ts +305 -0
  24. package/vendor/context-mode/src/platform/client-map.ts +45 -0
  25. package/vendor/context-mode/src/platform/detect.ts +645 -0
  26. package/vendor/context-mode/src/platform/dsh.ts +206 -0
  27. package/vendor/context-mode/src/platform/types.ts +503 -0
  28. package/vendor/context-mode/src/runPool.ts +81 -0
  29. package/vendor/context-mode/src/runtime.ts +765 -0
  30. package/vendor/context-mode/src/search/auto-memory.ts +200 -0
  31. package/vendor/context-mode/src/search/ctx-search-schema.ts +143 -0
  32. package/vendor/context-mode/src/search/flood-guard.ts +111 -0
  33. package/vendor/context-mode/src/search/unified.ts +176 -0
  34. package/vendor/context-mode/src/security.ts +889 -0
  35. package/vendor/context-mode/src/server.ts +4991 -0
  36. package/vendor/context-mode/src/session/analytics.ts +3085 -0
  37. package/vendor/context-mode/src/session/db.ts +1726 -0
  38. package/vendor/context-mode/src/session/error-classifier.ts +392 -0
  39. package/vendor/context-mode/src/session/event-emit.ts +132 -0
  40. package/vendor/context-mode/src/session/extract.ts +2958 -0
  41. package/vendor/context-mode/src/session/index.ts +130 -0
  42. package/vendor/context-mode/src/session/model-prices.json +429 -0
  43. package/vendor/context-mode/src/session/persist-tool-calls.ts +128 -0
  44. package/vendor/context-mode/src/session/pricing.ts +191 -0
  45. package/vendor/context-mode/src/session/project-attribution.ts +309 -0
  46. package/vendor/context-mode/src/session/purge.ts +338 -0
  47. package/vendor/context-mode/src/session/retrieval-marker.ts +65 -0
  48. package/vendor/context-mode/src/session/snapshot.ts +577 -0
  49. package/vendor/context-mode/src/store-directory.ts +290 -0
  50. package/vendor/context-mode/src/store.ts +2071 -0
  51. package/vendor/context-mode/src/truncate.ts +154 -0
  52. package/vendor/context-mode/src/types.ts +147 -0
  53. package/vendor/context-mode/src/util/claude-config.ts +95 -0
  54. package/vendor/context-mode/src/util/hook-config.ts +78 -0
  55. package/vendor/context-mode/src/util/jsonc.ts +70 -0
  56. package/vendor/context-mode/src/util/plugin-cache-integrity.ts +167 -0
  57. package/vendor/context-mode/src/util/project-dir.ts +347 -0
  58. package/vendor/context-mode/src/util/sibling-mcp.ts +228 -0
@@ -0,0 +1,290 @@
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
+
23
+ import {
24
+ readdirSync,
25
+ statSync,
26
+ lstatSync,
27
+ realpathSync,
28
+ existsSync,
29
+ readFileSync,
30
+ } from "node:fs";
31
+ import { join, extname, relative, sep, resolve } from "node:path";
32
+
33
+ export interface WalkOptions {
34
+ /** Glob-ish include patterns. Empty/undefined means include all (subject to extensions). */
35
+ include?: string[];
36
+ /** Glob-ish exclude patterns. Merged with sensible defaults. */
37
+ exclude?: string[];
38
+ /** Max recursion depth from rootPath (0 = root only). Default 5. */
39
+ maxDepth?: number;
40
+ /** Hard cap on total files. Default 200 — FTS5 blow-up guard. */
41
+ maxFiles?: number;
42
+ /** Allowed file extensions (with leading dot). Empty/undefined means default set. */
43
+ extensions?: string[];
44
+ /** Apply nearest .gitignore rules during walk. Default true. */
45
+ respectGitignore?: boolean;
46
+ /** Follow directory symlinks. Default false (cycle hazard + escape risk). */
47
+ followSymlinks?: boolean;
48
+ }
49
+
50
+ export interface WalkResult {
51
+ files: string[];
52
+ /** True when maxFiles cap was hit and traversal halted early. */
53
+ capped: boolean;
54
+ /** Total files discovered before cap (for reporting). */
55
+ totalSeen: number;
56
+ }
57
+
58
+ const DEFAULT_EXCLUDES = [
59
+ "node_modules",
60
+ ".git",
61
+ "dist",
62
+ "build",
63
+ ".next",
64
+ "coverage",
65
+ ".venv",
66
+ "__pycache__",
67
+ ".DS_Store",
68
+ ];
69
+
70
+ const DEFAULT_EXTENSIONS = [
71
+ ".md",
72
+ ".mdx",
73
+ ".txt",
74
+ ".json",
75
+ ".yaml",
76
+ ".yml",
77
+ ".ts",
78
+ ".tsx",
79
+ ".js",
80
+ ".jsx",
81
+ ".py",
82
+ ".rs",
83
+ ".go",
84
+ ".sh",
85
+ ];
86
+
87
+ const DEFAULT_MAX_DEPTH = 5;
88
+ const DEFAULT_MAX_FILES = 200;
89
+
90
+ /**
91
+ * Convert a simple glob pattern (`*`, `**`, `?`) to a RegExp. Anchors at
92
+ * boundaries so `node_modules` matches `node_modules` AND `node_modules/pkg`.
93
+ * Patterns are matched against POSIX-style relative paths (forward slashes)
94
+ * to give consistent behavior across macOS / Windows.
95
+ */
96
+ function globToRegExp(pattern: string): RegExp {
97
+ // Escape regex metachars except glob ones.
98
+ let re = "";
99
+ for (let i = 0; i < pattern.length; i++) {
100
+ const c = pattern[i]!;
101
+ if (c === "*") {
102
+ if (pattern[i + 1] === "*") {
103
+ re += ".*";
104
+ i++;
105
+ } else {
106
+ re += "[^/]*";
107
+ }
108
+ } else if (c === "?") {
109
+ re += "[^/]";
110
+ } else if ("\\^$.|+()[]{}".includes(c)) {
111
+ re += "\\" + c;
112
+ } else {
113
+ re += c;
114
+ }
115
+ }
116
+ return new RegExp(`^${re}$`);
117
+ }
118
+
119
+ /** Match a posix-style relative path against any of the patterns. */
120
+ function matchesAny(relPosix: string, patterns: string[]): boolean {
121
+ if (patterns.length === 0) return false;
122
+ const basename = relPosix.split("/").pop() ?? relPosix;
123
+ for (const p of patterns) {
124
+ // Bare names match basename OR any path segment.
125
+ if (!p.includes("/") && !p.includes("*")) {
126
+ if (basename === p) return true;
127
+ if (relPosix.split("/").includes(p)) return true;
128
+ continue;
129
+ }
130
+ const re = globToRegExp(p);
131
+ if (re.test(relPosix)) return true;
132
+ if (re.test(basename)) return true;
133
+ }
134
+ return false;
135
+ }
136
+
137
+ /**
138
+ * Parse a .gitignore file into a list of patterns. Comments and blank lines
139
+ * are stripped. Negation (`!`) is not supported — kept conservative.
140
+ */
141
+ function parseGitignore(rootPath: string): string[] {
142
+ const giPath = join(rootPath, ".gitignore");
143
+ if (!existsSync(giPath)) return [];
144
+ try {
145
+ const text = readFileSync(giPath, "utf-8");
146
+ return text
147
+ .split(/\r?\n/)
148
+ .map(l => l.trim())
149
+ .filter(l => l.length > 0 && !l.startsWith("#") && !l.startsWith("!"))
150
+ .map(l => l.replace(/^\//, "").replace(/\/$/, ""));
151
+ } catch {
152
+ return [];
153
+ }
154
+ }
155
+
156
+ /**
157
+ * Convert an absolute path under rootPath to a POSIX-style relative path
158
+ * so glob matching is identical across macOS/Linux/Windows.
159
+ */
160
+ function toPosixRel(rootPath: string, absPath: string): string {
161
+ const rel = relative(rootPath, absPath);
162
+ return rel.split(sep).join("/");
163
+ }
164
+
165
+ /**
166
+ * Walk `rootPath` recursively under the given bounds and return absolute file
167
+ * paths matching the filters. Pure synchronous traversal — no allocations
168
+ * beyond the result array. Symlink cycles are detected via a resolved-path
169
+ * Set; symlink escapes (resolving outside rootPath) are silently skipped.
170
+ */
171
+ export function walkDirectory(rootPath: string, opts: WalkOptions = {}): string[] {
172
+ return walkDirectoryDetailed(rootPath, opts).files;
173
+ }
174
+
175
+ /**
176
+ * Same as walkDirectory but returns capped + totalSeen so callers can surface
177
+ * a "capped at N files" notice in their response.
178
+ */
179
+ export function walkDirectoryDetailed(rootPath: string, opts: WalkOptions = {}): WalkResult {
180
+ const {
181
+ include,
182
+ exclude,
183
+ maxDepth = DEFAULT_MAX_DEPTH,
184
+ maxFiles = DEFAULT_MAX_FILES,
185
+ extensions,
186
+ respectGitignore = true,
187
+ followSymlinks = false,
188
+ } = opts;
189
+
190
+ // Normalize rootPath to its real path so symlink-escape detection is sound.
191
+ let rootReal: string;
192
+ try {
193
+ rootReal = realpathSync(rootPath);
194
+ } catch {
195
+ return { files: [], capped: false, totalSeen: 0 };
196
+ }
197
+
198
+ const exts = (extensions && extensions.length > 0 ? extensions : DEFAULT_EXTENSIONS)
199
+ .map(e => (e.startsWith(".") ? e : "." + e).toLowerCase());
200
+ const excludes = [
201
+ ...DEFAULT_EXCLUDES,
202
+ ...(exclude ?? []),
203
+ ...(respectGitignore ? parseGitignore(rootReal) : []),
204
+ ];
205
+ const includes = include ?? [];
206
+
207
+ const out: string[] = [];
208
+ const visited = new Set<string>([rootReal]);
209
+ let totalSeen = 0;
210
+ let capped = false;
211
+
212
+ function walk(absDir: string, depth: number): void {
213
+ if (capped) return;
214
+ if (depth > maxDepth) return;
215
+ let entries: import("node:fs").Dirent[];
216
+ try {
217
+ entries = readdirSync(absDir, { withFileTypes: true });
218
+ } catch {
219
+ return; // unreadable directory — skip silently
220
+ }
221
+ for (const ent of entries) {
222
+ if (capped) return;
223
+ const absChild = join(absDir, ent.name);
224
+ const relPosix = toPosixRel(rootReal, absChild);
225
+
226
+ // Exclude check applies to both files and dirs — early prune.
227
+ if (matchesAny(relPosix, excludes)) continue;
228
+ // Include filter applies to files only — see below.
229
+
230
+ // Resolve symlinks once; reject escapes; track for cycle detection.
231
+ let isDirChild = ent.isDirectory();
232
+ let isFileChild = ent.isFile();
233
+ let isSymlink = false;
234
+ try {
235
+ const lst = lstatSync(absChild);
236
+ isSymlink = lst.isSymbolicLink();
237
+ } catch {
238
+ continue;
239
+ }
240
+
241
+ if (isSymlink) {
242
+ if (!followSymlinks) continue;
243
+ let resolved: string;
244
+ try {
245
+ resolved = realpathSync(absChild);
246
+ } catch {
247
+ continue; // dangling
248
+ }
249
+ // Symlink-escape: refuse to follow if the resolved target leaves rootReal.
250
+ const escapeRel = relative(rootReal, resolved);
251
+ if (escapeRel.startsWith("..") || resolve(escapeRel) === resolved) {
252
+ // resolve(absolute) === absolute → target is absolute outside root
253
+ if (escapeRel.startsWith("..")) continue;
254
+ }
255
+ if (visited.has(resolved)) continue;
256
+ visited.add(resolved);
257
+ try {
258
+ const st = statSync(resolved);
259
+ isDirChild = st.isDirectory();
260
+ isFileChild = st.isFile();
261
+ } catch {
262
+ continue;
263
+ }
264
+ }
265
+
266
+ if (isDirChild) {
267
+ walk(absChild, depth + 1);
268
+ continue;
269
+ }
270
+ if (!isFileChild) continue;
271
+
272
+ // Extension filter.
273
+ const ext = extname(absChild).toLowerCase();
274
+ if (!exts.includes(ext)) continue;
275
+
276
+ // Include filter (if any): file must match at least one include pattern.
277
+ if (includes.length > 0 && !matchesAny(relPosix, includes)) continue;
278
+
279
+ totalSeen++;
280
+ if (out.length >= maxFiles) {
281
+ capped = true;
282
+ return;
283
+ }
284
+ out.push(absChild);
285
+ }
286
+ }
287
+
288
+ walk(rootReal, 0);
289
+ return { files: out, capped, totalSeen };
290
+ }