context-mode 1.0.147 → 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.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.openclaw-plugin/openclaw.plugin.json +1 -1
- package/.openclaw-plugin/package.json +1 -1
- package/build/adapters/codex/index.d.ts +7 -0
- package/build/adapters/codex/index.js +33 -2
- package/build/adapters/types.d.ts +4 -1
- package/build/cli.js +5 -1
- package/build/executor.d.ts +9 -0
- package/build/executor.js +6 -2
- package/build/server.d.ts +12 -0
- package/build/server.js +109 -5
- package/build/session/analytics.d.ts +19 -0
- package/build/session/analytics.js +71 -21
- package/build/session/db.d.ts +44 -0
- package/build/session/db.js +85 -18
- package/build/store-directory.d.ts +56 -0
- package/build/store-directory.js +254 -0
- package/build/store.d.ts +29 -0
- package/build/store.js +46 -0
- package/build/util/project-dir.d.ts +43 -0
- package/build/util/project-dir.js +102 -1
- package/cli.bundle.mjs +165 -152
- package/hooks/session-db.bundle.mjs +5 -5
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
- package/server.bundle.mjs +146 -133
package/build/session/db.js
CHANGED
|
@@ -493,6 +493,87 @@ const S = {
|
|
|
493
493
|
getEventBytesSummary: "getEventBytesSummary",
|
|
494
494
|
};
|
|
495
495
|
// ─────────────────────────────────────────────────────────
|
|
496
|
+
// Schema migration helpers (shared with the analytics aggregator)
|
|
497
|
+
// ─────────────────────────────────────────────────────────
|
|
498
|
+
/**
|
|
499
|
+
* Columns that the current `session_events` schema requires but earlier
|
|
500
|
+
* versions of context-mode did not write. Older DBs on disk are missing
|
|
501
|
+
* these — the analytics aggregator opens every DB it finds across all
|
|
502
|
+
* adapters, so without an in-place migration the SUM queries below fail
|
|
503
|
+
* the entire DB (the catch at the top of the read loop swallows the
|
|
504
|
+
* "no such column" error and the DB contributes zero to every column,
|
|
505
|
+
* not just the new ones). v1.0.148 hotfix.
|
|
506
|
+
*/
|
|
507
|
+
const SESSION_EVENTS_REQUIRED_COLUMNS = [
|
|
508
|
+
["project_dir", "TEXT NOT NULL DEFAULT ''"],
|
|
509
|
+
["attribution_source", "TEXT NOT NULL DEFAULT 'unknown'"],
|
|
510
|
+
["attribution_confidence", "REAL NOT NULL DEFAULT 0"],
|
|
511
|
+
["bytes_avoided", "INTEGER NOT NULL DEFAULT 0"],
|
|
512
|
+
["bytes_returned", "INTEGER NOT NULL DEFAULT 0"],
|
|
513
|
+
];
|
|
514
|
+
/**
|
|
515
|
+
* Apply any missing post-v1.0.130 `session_events` columns to an already-
|
|
516
|
+
* open writable database handle. Idempotent — each ALTER is guarded by a
|
|
517
|
+
* PRAGMA table_xinfo check, and the project_dir index is created only
|
|
518
|
+
* when a migration actually ran. Returns true if any column was added.
|
|
519
|
+
*
|
|
520
|
+
* Used by both the SessionDB constructor (for the active DB) and the
|
|
521
|
+
* analytics aggregator (for the 100+ historical DBs that never get
|
|
522
|
+
* opened through SessionDB). ADR-0001 compatible: no EXCLUSIVE pragma,
|
|
523
|
+
* no acquireDbLock — relies on the SQLite busy_timeout + WAL semantics
|
|
524
|
+
* already provided by SQLiteBase.
|
|
525
|
+
*/
|
|
526
|
+
export function applyMissingSessionEventsColumns(db) {
|
|
527
|
+
const colInfo = db.pragma("table_xinfo(session_events)");
|
|
528
|
+
const cols = new Set(colInfo.map((c) => c.name));
|
|
529
|
+
let changed = false;
|
|
530
|
+
for (const [name, spec] of SESSION_EVENTS_REQUIRED_COLUMNS) {
|
|
531
|
+
if (!cols.has(name)) {
|
|
532
|
+
db.exec(`ALTER TABLE session_events ADD COLUMN ${name} ${spec}`);
|
|
533
|
+
changed = true;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
if (changed) {
|
|
537
|
+
db.exec("CREATE INDEX IF NOT EXISTS idx_session_events_project ON session_events(session_id, project_dir)");
|
|
538
|
+
}
|
|
539
|
+
return changed;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* Open a session DB file briefly, run any missing schema migrations,
|
|
543
|
+
* and close. Best-effort: missing tables, file-locks, corrupt files,
|
|
544
|
+
* and any DatabaseCtor error are swallowed silently — the caller
|
|
545
|
+
* (analytics aggregator) handles the readonly query that follows and
|
|
546
|
+
* will skip the DB if it remains unreadable.
|
|
547
|
+
*
|
|
548
|
+
* Lazy migration entry point for the analytics aggregator, which would
|
|
549
|
+
* otherwise read 100+ historical DBs with the old (pre-v1.0.130) schema
|
|
550
|
+
* and lose every signal (not just bytes_avoided) because the SELECT
|
|
551
|
+
* statement references columns that don't exist on legacy schemas.
|
|
552
|
+
*
|
|
553
|
+
* Two open/close cycles in the worst case (one readonly probe to detect
|
|
554
|
+
* legacy schema, one writable to migrate). For already-migrated DBs
|
|
555
|
+
* (the common case after first read), this opens writable once and
|
|
556
|
+
* exits without writing — cheaper than always-writable.
|
|
557
|
+
*/
|
|
558
|
+
export function ensureSessionEventsSchema(dbPath, DatabaseCtor) {
|
|
559
|
+
let db = null;
|
|
560
|
+
try {
|
|
561
|
+
db = new DatabaseCtor(dbPath);
|
|
562
|
+
applyMissingSessionEventsColumns(db);
|
|
563
|
+
}
|
|
564
|
+
catch {
|
|
565
|
+
// best-effort — missing table, file lock, corrupt DB, or DatabaseCtor
|
|
566
|
+
// load failure. The aggregator's existing skip-on-error handles the
|
|
567
|
+
// downstream readonly query.
|
|
568
|
+
}
|
|
569
|
+
finally {
|
|
570
|
+
try {
|
|
571
|
+
db?.close();
|
|
572
|
+
}
|
|
573
|
+
catch { /* ignore */ }
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
// ─────────────────────────────────────────────────────────
|
|
496
577
|
// SessionDB
|
|
497
578
|
// ─────────────────────────────────────────────────────────
|
|
498
579
|
export class SessionDB extends SQLiteBase {
|
|
@@ -569,25 +650,11 @@ export class SessionDB extends SQLiteBase {
|
|
|
569
650
|
CREATE INDEX IF NOT EXISTS idx_tool_calls_session ON tool_calls(session_id);
|
|
570
651
|
`);
|
|
571
652
|
// Migration: add per-event attribution columns for existing DBs.
|
|
653
|
+
// Shared helper — the analytics aggregator (analytics.ts) runs the
|
|
654
|
+
// SAME migration against every historical DB it scans, so the column
|
|
655
|
+
// list lives in one place at the top of this module.
|
|
572
656
|
try {
|
|
573
|
-
|
|
574
|
-
const cols = new Set(colInfo.map((c) => c.name));
|
|
575
|
-
if (!cols.has("project_dir")) {
|
|
576
|
-
this.db.exec("ALTER TABLE session_events ADD COLUMN project_dir TEXT NOT NULL DEFAULT ''");
|
|
577
|
-
}
|
|
578
|
-
if (!cols.has("attribution_source")) {
|
|
579
|
-
this.db.exec("ALTER TABLE session_events ADD COLUMN attribution_source TEXT NOT NULL DEFAULT 'unknown'");
|
|
580
|
-
}
|
|
581
|
-
if (!cols.has("attribution_confidence")) {
|
|
582
|
-
this.db.exec("ALTER TABLE session_events ADD COLUMN attribution_confidence REAL NOT NULL DEFAULT 0");
|
|
583
|
-
}
|
|
584
|
-
if (!cols.has("bytes_avoided")) {
|
|
585
|
-
this.db.exec("ALTER TABLE session_events ADD COLUMN bytes_avoided INTEGER NOT NULL DEFAULT 0");
|
|
586
|
-
}
|
|
587
|
-
if (!cols.has("bytes_returned")) {
|
|
588
|
-
this.db.exec("ALTER TABLE session_events ADD COLUMN bytes_returned INTEGER NOT NULL DEFAULT 0");
|
|
589
|
-
}
|
|
590
|
-
this.db.exec("CREATE INDEX IF NOT EXISTS idx_session_events_project ON session_events(session_id, project_dir)");
|
|
657
|
+
applyMissingSessionEventsColumns(this.db);
|
|
591
658
|
}
|
|
592
659
|
catch {
|
|
593
660
|
// best-effort migration only
|
|
@@ -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
|
|
@@ -60,6 +60,42 @@ export declare function resolveProjectDirFromTranscript(opts: {
|
|
|
60
60
|
/** Test seam for maxAgeMs. Defaults to Date.now(). */
|
|
61
61
|
nowMs?: number;
|
|
62
62
|
}): string | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Issue #45 / c4529042182 — recover the project-cwd from a Codex CLI
|
|
65
|
+
* session log when the spawned MCP child inherits a non-project cwd
|
|
66
|
+
* (e.g. $HOME when Codex was launched from anywhere outside the project).
|
|
67
|
+
*
|
|
68
|
+
* Codex writes its session transcripts to
|
|
69
|
+
* `${CODEX_HOME ?? ~/.codex}/sessions/<uuid>.jsonl`. The first line is a
|
|
70
|
+
* `SessionMeta` JSON struct whose `meta.cwd` field carries the literal
|
|
71
|
+
* project directory the CLI was launched from (see refs/platforms/codex/
|
|
72
|
+
* codex-rs SessionMeta). Codex publishes NO workspace env var to its child
|
|
73
|
+
* MCP processes — so unlike Claude/Pi/Cursor, we have no env signal at all.
|
|
74
|
+
* The session log is the strongest available signal.
|
|
75
|
+
*
|
|
76
|
+
* Mirror of `resolveProjectDirFromTranscript` for Claude Code; differences:
|
|
77
|
+
* • Sessions live flat in `${codexHome}/sessions/*.jsonl` (no per-project
|
|
78
|
+
* encoded subdir like Claude's `~/.claude/projects/<encoded>/`).
|
|
79
|
+
* • The cwd is on `meta.cwd` (nested), not top-level `cwd`.
|
|
80
|
+
*
|
|
81
|
+
* Returns `null` when:
|
|
82
|
+
* • `codexHome` or its `sessions/` subdir does not exist.
|
|
83
|
+
* • No `.jsonl` files exist or none has a parseable `meta.cwd` string.
|
|
84
|
+
* • The newest log is older than `transcriptMaxAgeMs` (multi-window guard).
|
|
85
|
+
* • The resolved `meta.cwd` points at a plugin install path (poisoned).
|
|
86
|
+
*/
|
|
87
|
+
export declare function resolveCodexSessionCwd(opts?: {
|
|
88
|
+
/** Defaults to `process.env.CODEX_HOME ?? path.join(os.homedir(), ".codex")`. */
|
|
89
|
+
codexHome?: string;
|
|
90
|
+
/**
|
|
91
|
+
* Optional freshness guard — Codex appends to the active log while the
|
|
92
|
+
* session is running, so a stale log from days ago must not become a
|
|
93
|
+
* global project-dir signal.
|
|
94
|
+
*/
|
|
95
|
+
transcriptMaxAgeMs?: number;
|
|
96
|
+
/** Test seam for transcriptMaxAgeMs. Defaults to Date.now(). */
|
|
97
|
+
now?: number;
|
|
98
|
+
}): string | null;
|
|
63
99
|
/**
|
|
64
100
|
* Pure project-dir resolver. Mirror of the env-var chain inside
|
|
65
101
|
* `src/server.ts getProjectDir()`, but takes its inputs explicitly so the
|
|
@@ -100,4 +136,11 @@ export declare function resolveProjectDir(opts: {
|
|
|
100
136
|
* for `start.mjs` and any non-strict consumer).
|
|
101
137
|
*/
|
|
102
138
|
strictPlatform?: PlatformId;
|
|
139
|
+
/**
|
|
140
|
+
* Issue #45 — override `${CODEX_HOME ?? ~/.codex}` for tests. When
|
|
141
|
+
* `strictPlatform === "codex"` and the env cascade yields nothing, the
|
|
142
|
+
* resolver reads `meta.cwd` from the newest session.jsonl under
|
|
143
|
+
* `${codexHome}/sessions/`.
|
|
144
|
+
*/
|
|
145
|
+
codexHome?: string;
|
|
103
146
|
}): string;
|