@qmediat.io/gemini-code-context-mcp 1.7.3 → 1.9.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.
- package/CHANGELOG.md +61 -0
- package/README.md +14 -1
- package/dist/cache/cache-manager.d.ts.map +1 -1
- package/dist/cache/cache-manager.js +8 -8
- package/dist/cache/cache-manager.js.map +1 -1
- package/dist/cache/files-uploader.js +2 -2
- package/dist/cache/files-uploader.js.map +1 -1
- package/dist/cache/ttl-watcher.js +3 -3
- package/dist/cache/ttl-watcher.js.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/indexer/globs.d.ts +14 -0
- package/dist/indexer/globs.d.ts.map +1 -1
- package/dist/indexer/globs.js +17 -0
- package/dist/indexer/globs.js.map +1 -1
- package/dist/server.d.ts +20 -0
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +96 -7
- package/dist/server.js.map +1 -1
- package/dist/tools/agentic/sandbox.d.ts +26 -1
- package/dist/tools/agentic/sandbox.d.ts.map +1 -1
- package/dist/tools/agentic/sandbox.js.map +1 -1
- package/dist/tools/agentic/workspace-tools.d.ts +5 -4
- package/dist/tools/agentic/workspace-tools.d.ts.map +1 -1
- package/dist/tools/agentic/workspace-tools.js +148 -56
- package/dist/tools/agentic/workspace-tools.js.map +1 -1
- package/dist/tools/ask-agentic.tool.d.ts +10 -0
- package/dist/tools/ask-agentic.tool.d.ts.map +1 -1
- package/dist/tools/ask-agentic.tool.js +55 -12
- package/dist/tools/ask-agentic.tool.js.map +1 -1
- package/dist/tools/ask.tool.d.ts +8 -8
- package/dist/tools/ask.tool.js +2 -2
- package/dist/tools/ask.tool.js.map +1 -1
- package/dist/tools/code.tool.d.ts +8 -8
- package/dist/tools/code.tool.js +2 -2
- package/dist/tools/code.tool.js.map +1 -1
- package/dist/utils/logger.d.ts +37 -0
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +0 -0
- package/dist/utils/logger.js.map +1 -1
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP server bootstrap — stdio transport, tool dispatch, graceful lifecycle.
|
|
3
|
+
*
|
|
4
|
+
* **Shutdown drain (T6, v1.8.0):** SIGINT/SIGTERM does not immediately tear
|
|
5
|
+
* down the transport. Each `CallToolRequestSchema` handler invocation is
|
|
6
|
+
* tracked in `inFlightCalls` for the duration of `tool.execute(...)`. On
|
|
7
|
+
* shutdown we `Promise.race` the set against `SHUTDOWN_DRAIN_MS` (default
|
|
8
|
+
* 5000 ms) so a long `ask`/`code`/`ask_agentic` already in flight when
|
|
9
|
+
* Claude Code restarts the server can return its response before the
|
|
10
|
+
* process exits. Hard timeout: a hung call cannot block shutdown — abandoned
|
|
11
|
+
* calls are logged at WARN, not silently dropped. Override the drain budget
|
|
12
|
+
* with `GEMINI_CODE_CONTEXT_SHUTDOWN_DRAIN_MS=<ms>` (clamped to [0, 60000]).
|
|
3
13
|
*/
|
|
4
14
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
15
|
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
|
@@ -11,11 +21,59 @@ import { ManifestDb } from './manifest/db.js';
|
|
|
11
21
|
import { TOOLS } from './tools/index.js';
|
|
12
22
|
import { buildToolInputSchema, errorResult, } from './tools/registry.js';
|
|
13
23
|
import { createTpmThrottle } from './tools/shared/throttle.js';
|
|
14
|
-
import { logger } from './utils/logger.js';
|
|
24
|
+
import { logger, safeForLog } from './utils/logger.js';
|
|
15
25
|
import { readFileSync } from 'node:fs';
|
|
16
26
|
import { dirname as pathDirname, join as pathJoin } from 'node:path';
|
|
17
27
|
import { fileURLToPath } from 'node:url';
|
|
18
28
|
const SERVER_NAME = '@qmediat.io/gemini-code-context-mcp';
|
|
29
|
+
const DEFAULT_SHUTDOWN_DRAIN_MS = 5_000;
|
|
30
|
+
const MAX_SHUTDOWN_DRAIN_MS = 60_000;
|
|
31
|
+
/**
|
|
32
|
+
* Resolve the drain budget from `GEMINI_CODE_CONTEXT_SHUTDOWN_DRAIN_MS`.
|
|
33
|
+
* Non-finite, negative, or out-of-range values fall back to the default —
|
|
34
|
+
* an operator typo (`abc`, `-1`, `999999`) must NOT silently disable the
|
|
35
|
+
* timeout (would block forever) or set it to a pathological value.
|
|
36
|
+
*/
|
|
37
|
+
function resolveDrainBudgetMs() {
|
|
38
|
+
const raw = process.env.GEMINI_CODE_CONTEXT_SHUTDOWN_DRAIN_MS;
|
|
39
|
+
if (raw === undefined || raw === '')
|
|
40
|
+
return DEFAULT_SHUTDOWN_DRAIN_MS;
|
|
41
|
+
const parsed = Number.parseInt(raw, 10);
|
|
42
|
+
if (!Number.isFinite(parsed) || parsed < 0 || parsed > MAX_SHUTDOWN_DRAIN_MS) {
|
|
43
|
+
logger.warn(`GEMINI_CODE_CONTEXT_SHUTDOWN_DRAIN_MS='${raw}' is invalid (must be 0–${MAX_SHUTDOWN_DRAIN_MS}); using default ${DEFAULT_SHUTDOWN_DRAIN_MS}ms`);
|
|
44
|
+
return DEFAULT_SHUTDOWN_DRAIN_MS;
|
|
45
|
+
}
|
|
46
|
+
return parsed;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Wait for every promise in `inFlight` to settle, but no longer than
|
|
50
|
+
* `timeoutMs`. Returns the count of promises that settled in time.
|
|
51
|
+
*
|
|
52
|
+
* Exported for unit testing without booting a real server.
|
|
53
|
+
*/
|
|
54
|
+
export async function drainInFlight(inFlight, timeoutMs) {
|
|
55
|
+
const total = inFlight.size;
|
|
56
|
+
if (total === 0)
|
|
57
|
+
return { settled: 0, abandoned: 0 };
|
|
58
|
+
if (timeoutMs <= 0)
|
|
59
|
+
return { settled: 0, abandoned: total };
|
|
60
|
+
let settled = 0;
|
|
61
|
+
// Wrap each promise to count completions as they settle. We don't care
|
|
62
|
+
// about resolve vs reject — both mean "the handler returned, no longer
|
|
63
|
+
// in-flight". The `settled` counter is read by the caller AFTER the race
|
|
64
|
+
// resolves, so it reflects the count at the moment the timeout (or
|
|
65
|
+
// allSettled) fired.
|
|
66
|
+
const tracked = [...inFlight].map((p) => p.then(() => {
|
|
67
|
+
settled++;
|
|
68
|
+
}, () => {
|
|
69
|
+
settled++;
|
|
70
|
+
}));
|
|
71
|
+
await Promise.race([
|
|
72
|
+
Promise.allSettled(tracked),
|
|
73
|
+
new Promise((resolve) => setTimeout(resolve, timeoutMs).unref?.()),
|
|
74
|
+
]);
|
|
75
|
+
return { settled, abandoned: total - settled };
|
|
76
|
+
}
|
|
19
77
|
/**
|
|
20
78
|
* Read version from package.json at runtime. `server.js` lives in `dist/` after
|
|
21
79
|
* compilation (or `src/` under `tsx`) so package.json is the parent directory.
|
|
@@ -64,6 +122,13 @@ export async function runServer() {
|
|
|
64
122
|
inputSchema: buildToolInputSchema(tool),
|
|
65
123
|
})),
|
|
66
124
|
}));
|
|
125
|
+
// Tracks `tool.execute(...)` calls that are still mid-flight. Used by
|
|
126
|
+
// `shutdown()` to drain gracefully on SIGINT/SIGTERM (T6). Set semantics:
|
|
127
|
+
// each handler invocation `add`s its own promise on entry and `delete`s
|
|
128
|
+
// it in a `finally` block, regardless of resolve/reject path. A hung call
|
|
129
|
+
// remains in the set until shutdown's `drainInFlight` either races it to
|
|
130
|
+
// completion or times out and abandons it.
|
|
131
|
+
const inFlightCalls = new Set();
|
|
67
132
|
server.setRequestHandler(CallToolRequestSchema, async (request) => {
|
|
68
133
|
const { name, arguments: rawArgs } = request.params;
|
|
69
134
|
const progressToken = request.params._meta?.progressToken;
|
|
@@ -86,19 +151,43 @@ export async function runServer() {
|
|
|
86
151
|
progressToken,
|
|
87
152
|
throttle,
|
|
88
153
|
};
|
|
154
|
+
const callPromise = (async () => {
|
|
155
|
+
try {
|
|
156
|
+
const result = await tool.execute(parse.data, ctx);
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
161
|
+
logger.error(`tool '${safeForLog(name)}' threw: ${safeForLog(message)}`);
|
|
162
|
+
return errorResult(`${name} threw: ${message}`);
|
|
163
|
+
}
|
|
164
|
+
})();
|
|
165
|
+
inFlightCalls.add(callPromise);
|
|
89
166
|
try {
|
|
90
|
-
|
|
91
|
-
return result;
|
|
167
|
+
return await callPromise;
|
|
92
168
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
logger.error(`tool '${name}' threw: ${message}`);
|
|
96
|
-
return errorResult(`${name} threw: ${message}`);
|
|
169
|
+
finally {
|
|
170
|
+
inFlightCalls.delete(callPromise);
|
|
97
171
|
}
|
|
98
172
|
});
|
|
99
173
|
const transport = new StdioServerTransport();
|
|
100
174
|
const shutdown = async (signal) => {
|
|
101
175
|
logger.info(`received ${signal}, shutting down…`);
|
|
176
|
+
// Drain in-flight tool calls BEFORE closing the transport / DB. Otherwise
|
|
177
|
+
// an in-progress `await tool.execute(...)` in the request handler would
|
|
178
|
+
// race against `server.close()` / `manifest.close()` and either lose its
|
|
179
|
+
// response (transport torn down) or hit "manifest closed" mid-write.
|
|
180
|
+
if (inFlightCalls.size > 0) {
|
|
181
|
+
const drainBudgetMs = resolveDrainBudgetMs();
|
|
182
|
+
logger.info(`waiting up to ${drainBudgetMs}ms for ${inFlightCalls.size} in-flight tool call(s) to drain`);
|
|
183
|
+
const { settled, abandoned } = await drainInFlight(inFlightCalls, drainBudgetMs);
|
|
184
|
+
if (abandoned > 0) {
|
|
185
|
+
logger.warn(`${abandoned}/${settled + abandoned} in-flight call(s) did not drain in ${drainBudgetMs}ms — abandoning`);
|
|
186
|
+
}
|
|
187
|
+
else if (settled > 0) {
|
|
188
|
+
logger.info(`drained ${settled} in-flight call(s) cleanly`);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
102
191
|
ttlWatcher.stop();
|
|
103
192
|
try {
|
|
104
193
|
manifest.close();
|
package/dist/server.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,2CAA2C,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EACL,qBAAqB,EAErB,sBAAsB,GACvB,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACpD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,OAAO,EAGL,oBAAoB,EACpB,WAAW,GACZ,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAEvD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,IAAI,IAAI,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC,MAAM,WAAW,GAAG,qCAAqC,CAAC;AAC1D,MAAM,yBAAyB,GAAG,KAAK,CAAC;AACxC,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAErC;;;;;GAKG;AACH,SAAS,oBAAoB;IAC3B,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,qCAAqC,CAAC;IAC9D,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,EA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AAC,KAAK,EAAE,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACP,YAAY;QACd,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO,CAAC,IAAI,CAAC,QAAQ,EAAE,GAAG,EAAE;QAC1B,KAAK,QAAQ,CAAC,QAAQ,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;IACH,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE;QAC3B,KAAK,QAAQ,CAAC,SAAS,CAAC,CAAC;IAC3B,CAAC,CAAC,CAAC;IAEH,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAChC,MAAM,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;AAC9C,CAAC"}
|
|
@@ -20,7 +20,32 @@
|
|
|
20
20
|
* Codex PR #20 review #2 flagged that `resolve+startsWith` is symlink-
|
|
21
21
|
* unsafe. This module implements `realpath + root jail` per that feedback.
|
|
22
22
|
*/
|
|
23
|
-
|
|
23
|
+
/**
|
|
24
|
+
* Where each code fires (v1.9.0 — keep this map current as new throw sites
|
|
25
|
+
* are added):
|
|
26
|
+
*
|
|
27
|
+
* PATH_TRAVERSAL — sandbox.ts (jail violation: symlink escape, abs path outside)
|
|
28
|
+
* SECRET_DENYLIST — sandbox.ts (basename / extension / dir on hardcoded secret list)
|
|
29
|
+
* EXCLUDED_DIR — sandbox.ts (DEFAULT_EXCLUDE_DIRS hit, line ~302)
|
|
30
|
+
* AND workspace-tools.ts top-level gates (v1.9.0+):
|
|
31
|
+
* listDirectoryExecutor + grepExecutor's pathPrefix,
|
|
32
|
+
* when the requested dir matches user-supplied excludeGlobs
|
|
33
|
+
* EXCLUDED_FILE — workspace-tools.ts (v1.9.0+): readFileExecutor when
|
|
34
|
+
* the file is excluded by user globs (filename / extension
|
|
35
|
+
* / dir-prefix). Generic message — no path leak.
|
|
36
|
+
* EXCLUDED_FILENAME — sandbox.ts (DEFAULT_EXCLUDE_FILE_NAMES hit, line ~321)
|
|
37
|
+
* NON_SOURCE_FILE — workspace-tools.ts: readFileExecutor when no
|
|
38
|
+
* include-extension matches (path retained — different
|
|
39
|
+
* threat model from EXCLUDED_FILE)
|
|
40
|
+
* NOT_A_DIRECTORY — workspace-tools.ts (listDir / grep when target is a file)
|
|
41
|
+
* NOT_FOUND — sandbox.ts (resolveInsideWorkspace, missing path)
|
|
42
|
+
* AND workspace-tools.ts (executor-side fs errors)
|
|
43
|
+
* NOT_INSIDE_ROOT — sandbox.ts (jail violation, in-workspace check)
|
|
44
|
+
* INVALID_INPUT — workspace-tools.ts (empty pattern, malformed regex)
|
|
45
|
+
*
|
|
46
|
+
* If you add a new throw site for an existing code, update this map.
|
|
47
|
+
*/
|
|
48
|
+
export type SandboxErrorCode = 'PATH_TRAVERSAL' | 'SECRET_DENYLIST' | 'EXCLUDED_DIR' | 'EXCLUDED_FILE' | 'EXCLUDED_FILENAME' | 'NON_SOURCE_FILE' | 'NOT_A_DIRECTORY' | 'NOT_FOUND' | 'NOT_INSIDE_ROOT' | 'INVALID_INPUT';
|
|
24
49
|
export declare class SandboxError extends Error {
|
|
25
50
|
readonly code: SandboxErrorCode;
|
|
26
51
|
readonly requestedPath: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandbox.d.ts","sourceRoot":"","sources":["../../../src/tools/agentic/sandbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAyDH,MAAM,MAAM,gBAAgB,GACxB,gBAAgB,GAChB,iBAAiB,GACjB,cAAc,GACd,mBAAmB,GACnB,iBAAiB,GACjB,iBAAiB,GACjB,WAAW,GACX,iBAAiB,GACjB,eAAe,CAAC;AAmDpB,qBAAa,YAAa,SAAQ,KAAK;IACrC,QAAQ,CAAC,IAAI,EAAE,gBAAgB,CAAC;IAChC,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;gBACnB,IAAI,EA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|
|
1
|
-
{"version":3,"file":"sandbox.js","sourceRoot":"","sources":["../../../src/tools/agentic/sandbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACxE,OAAO,EAAE,oBAAoB,EAAE,gCAAgC,EAAE,MAAM,wBAAwB,CAAC;AAEhG;;;;;;;4DAO4D;AAC5D,MAAM,wBAAwB,GAAsB;IAClD,MAAM;IACN,YAAY;IACZ,kBAAkB;IAClB,iBAAiB;IACjB,WAAW;IACX,cAAc;IACd,QAAQ;IACR,QAAQ;IACR,SAAS;IACT,kBAAkB;IAClB,WAAW;IACX,aAAa;IACb,kBAAkB;IAClB,cAAc;IACd,cAAc;IACd,aAAa;IACb,sBAAsB;CACvB,CAAC;AAEF,2EAA2E;AAC3E,MAAM,8BAA8B,GAAwB,IAAI,GAAG,CACjE,wBAAwB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC,CACrD,CAAC;AAEF;gFACgF;AAChF,MAAM,yBAAyB,GAAsB;IACnD,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,MAAM;IACN,KAAK;IACL,MAAM;IACN,MAAM;IACN,WAAW;IACX,MAAM;IACN,MAAM;IACN,OAAO;CACR,CAAC;
|
|
1
|
+
{"version":3,"file":"sandbox.js","sourceRoot":"","sources":["../../../src/tools/agentic/sandbox.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,GAAG,IAAI,OAAO,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,W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
19
19
|
* minified bundle or a massive listing burning output tokens on one call.
|
|
20
20
|
*/
|
|
21
|
+
import { type MatchConfig } from '../../indexer/globs.js';
|
|
21
22
|
export interface DirEntry {
|
|
22
23
|
relpath: string;
|
|
23
24
|
type: 'file' | 'dir';
|
|
@@ -34,7 +35,7 @@ export interface ListDirectoryResult {
|
|
|
34
35
|
* dirs and denylisted filenames are silently omitted; the model sees only
|
|
35
36
|
* paths it is allowed to ask for via `read_file` / `grep`.
|
|
36
37
|
*/
|
|
37
|
-
export declare function listDirectoryExecutor(workspaceRoot: string, relPath: string): Promise<ListDirectoryResult>;
|
|
38
|
+
export declare function listDirectoryExecutor(workspaceRoot: string, relPath: string, matchConfig?: MatchConfig): Promise<ListDirectoryResult>;
|
|
38
39
|
export interface FindFilesResult {
|
|
39
40
|
pattern: string;
|
|
40
41
|
matches: string[];
|
|
@@ -47,7 +48,7 @@ export interface FindFilesResult {
|
|
|
47
48
|
* sees paths it couldn't `read_file` on anyway — avoids a foot-gun where
|
|
48
49
|
* the model tries to open a secret it saw in a `find_files` response).
|
|
49
50
|
*/
|
|
50
|
-
export declare function findFilesExecutor(workspaceRoot: string, pattern: string): Promise<FindFilesResult>;
|
|
51
|
+
export declare function findFilesExecutor(workspaceRoot: string, pattern: string, matchConfig?: MatchConfig): Promise<FindFilesResult>;
|
|
51
52
|
export interface ReadFileResult {
|
|
52
53
|
relpath: string;
|
|
53
54
|
content: string;
|
|
@@ -68,7 +69,7 @@ export interface ReadFileResult {
|
|
|
68
69
|
* - Omit both → return the first `DEFAULT_READ_LINE_LIMIT` lines, up to
|
|
69
70
|
* `MAX_READ_BYTES`, whichever hits first.
|
|
70
71
|
*/
|
|
71
|
-
export declare function readFileExecutor(workspaceRoot: string, relPath: string, startLine?: number, endLine?: number): Promise<ReadFileResult>;
|
|
72
|
+
export declare function readFileExecutor(workspaceRoot: string, relPath: string, startLine?: number, endLine?: number, matchConfig?: MatchConfig): Promise<ReadFileResult>;
|
|
72
73
|
export interface GrepMatch {
|
|
73
74
|
relpath: string;
|
|
74
75
|
line: number;
|
|
@@ -91,7 +92,7 @@ export interface GrepResult {
|
|
|
91
92
|
* - each match line truncated to 500 chars (guard against minified
|
|
92
93
|
* lines flooding the payload)
|
|
93
94
|
*/
|
|
94
|
-
export declare function grepExecutor(workspaceRoot: string, pattern: string, pathPrefix?: string): Promise<GrepResult>;
|
|
95
|
+
export declare function grepExecutor(workspaceRoot: string, pattern: string, pathPrefix?: string, matchConfig?: MatchConfig): Promise<GrepResult>;
|
|
95
96
|
/** Export knobs for tests to reference without re-declaring numeric values. */
|
|
96
97
|
export declare const AGENTIC_LIMITS: Readonly<{
|
|
97
98
|
readonly MAX_READ_BYTES: 200000;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"workspace-tools.d.ts","sourceRoot":"","sources":["../../../src/tools/agentic/workspace-tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;
|
|
1
|
+
{"version":3,"file":"workspace-tools.d.ts","sourceRoot":"","sources":["../../../src/tools/agentic/workspace-tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAKH,OAAO,EACL,KAAK,WAAW,EAKjB,MAAM,wBAAwB,CAAC;AAqIhC,MAAM,WAAW,QAAQ;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,GAAG,KAAK,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,QAAQ,EAAE,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;;;GAIG;AACH,wBAAsB,qBAAqB,CACzC,aAAa,EAAE,MAAM,EACrB,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,WAAW,GACxB,OAAO,CAAC,mBAAmB,CAAC,CAyF9B;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U,CAAC,EAAE,MAAM,EACnB,WAAW,CAAC,EAAE,WAAW,GACxB,OAAO,CAAC,UAAU,CAAC,CAqJrB;AAED,+EAA+E;AAC/E,eAAO,MAAM,cAAc;;;;;;;EAOhB,CAAC"}
|
|
@@ -20,8 +20,50 @@
|
|
|
20
20
|
*/
|
|
21
21
|
import { readFile, readdir, realpath, stat } from 'node:fs/promises';
|
|
22
22
|
import { join } from 'node:path';
|
|
23
|
-
import {
|
|
23
|
+
import { defaultMatchConfig, isFileIncluded, isPathExcluded, matchesAnyIncludeExtension, } from '../../indexer/globs.js';
|
|
24
24
|
import { SandboxError, resolveInsideWorkspace } from './sandbox.js';
|
|
25
|
+
/**
|
|
26
|
+
* Build a `MatchConfig` honouring the caller's `includeGlobs`/`excludeGlobs`
|
|
27
|
+
* (v1.9.0+). When `matchConfig` is undefined, returns the defaults — the same
|
|
28
|
+
* filter the eager `ask`/`code` scanner uses. The agentic executors below
|
|
29
|
+
* route every dir-recurse and file-include decision through this config so
|
|
30
|
+
* Phase 3's `ask` → `ask_agentic` fallback (v1.9.0) preserves the user's
|
|
31
|
+
* filter semantics — no privacy regression where excluded paths leak into
|
|
32
|
+
* the agentic loop's view.
|
|
33
|
+
*
|
|
34
|
+
* Falls back gracefully: existing test call-sites that don't pass a config
|
|
35
|
+
* see the same behaviour they did pre-v1.9.0.
|
|
36
|
+
*/
|
|
37
|
+
function resolveMatchConfig(matchConfig) {
|
|
38
|
+
return matchConfig ?? defaultMatchConfig({});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Exclude-side check for `list_directory` (v1.9.0+). Returns `true` when the
|
|
42
|
+
* file should be hidden from a navigation listing — covers default filename
|
|
43
|
+
* denylist (lockfiles, `.env*`, etc.), default extension denylist
|
|
44
|
+
* (`.tsbuildinfo`), user `excludeGlobs` filename / extension entries, AND
|
|
45
|
+
* dir-prefix excludes that match the file's parent path. Deliberately does
|
|
46
|
+
* NOT enforce include-extension membership — `list_directory` is a
|
|
47
|
+
* navigation aid; the read-side gate lives in `readFileExecutor`.
|
|
48
|
+
*
|
|
49
|
+
* Implemented as a separate predicate (not `isFileIncluded` from `globs.ts`)
|
|
50
|
+
* because `isFileIncluded` REQUIRES an include-extension match to return
|
|
51
|
+
* true, which is correct for indexing but wrong for listing.
|
|
52
|
+
*/
|
|
53
|
+
function isFileExcludedByConfig(relpath, basename, config) {
|
|
54
|
+
if (isPathExcluded(relpath, config))
|
|
55
|
+
return true;
|
|
56
|
+
const lowerBase = basename.toLowerCase();
|
|
57
|
+
for (const name of config.excludeFileNames) {
|
|
58
|
+
if (lowerBase === name.toLowerCase())
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
for (const ext of config.excludeExtensions) {
|
|
62
|
+
if (lowerBase.endsWith(ext.toLowerCase()))
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
25
67
|
/** Hard upper bound on any single `read_file` response body. Bigger files
|
|
26
68
|
* get head-truncated + flagged — never fully expanded into the iteration.
|
|
27
69
|
*
|
|
@@ -104,8 +146,23 @@ function globToRegExp(glob) {
|
|
|
104
146
|
* dirs and denylisted filenames are silently omitted; the model sees only
|
|
105
147
|
* paths it is allowed to ask for via `read_file` / `grep`.
|
|
106
148
|
*/
|
|
107
|
-
export async function listDirectoryExecutor(workspaceRoot, relPath) {
|
|
149
|
+
export async function listDirectoryExecutor(workspaceRoot, relPath, matchConfig) {
|
|
108
150
|
const target = await resolveInsideWorkspace(workspaceRoot, relPath);
|
|
151
|
+
const config = resolveMatchConfig(matchConfig);
|
|
152
|
+
// Top-level gate (v1.9.0 Phase 1.1, /6step Finding #1): the requested
|
|
153
|
+
// directory itself must be checked against user `excludeGlobs`. Without
|
|
154
|
+
// this, `list_directory("internal-secrets")` (where `internal-secrets`
|
|
155
|
+
// is in user excludes but NOT in `DEFAULT_EXCLUDE_DIRS`) succeeds and
|
|
156
|
+
// returns a filtered child list — the model can probe path existence
|
|
157
|
+
// by comparing success vs `NOT_FOUND` from `resolveInsideWorkspace`.
|
|
158
|
+
// The sandbox layer (`resolveInsideWorkspace`) checks default-excluded
|
|
159
|
+
// dirs only; user-supplied excludes were unenforced at this level.
|
|
160
|
+
// Generic message — no path interpolation — to avoid the same
|
|
161
|
+
// existence-leak via error string (see Finding #2 for the file-level
|
|
162
|
+
// analogue).
|
|
163
|
+
if (target.relpath && isPathExcluded(target.relpath, config)) {
|
|
164
|
+
throw new SandboxError('EXCLUDED_DIR', 'directory is excluded by configured policy', relPath);
|
|
165
|
+
}
|
|
109
166
|
let rawEntries;
|
|
110
167
|
try {
|
|
111
168
|
rawEntries = await readdir(target.absolutePath, { withFileTypes: true, encoding: 'utf8' });
|
|
@@ -130,17 +187,28 @@ export async function listDirectoryExecutor(workspaceRoot, relPath) {
|
|
|
130
187
|
const isDir = entry.isDirectory();
|
|
131
188
|
if (!isFile && !isDir)
|
|
132
189
|
continue; // skip symlinks / sockets / fifos / etc.
|
|
190
|
+
// Build the would-be relpath for this child so dir-prefix excludes
|
|
191
|
+
// (user-supplied `excludeGlobs`, e.g. `src/vendor/`) match against the
|
|
192
|
+
// full nested path, not just the basename.
|
|
193
|
+
const childRel = target.relpath ? `${target.relpath}/${entry.name}` : entry.name;
|
|
133
194
|
// Skip excluded directories (user never gets to recurse into them).
|
|
134
|
-
|
|
195
|
+
// Defaults (`node_modules`, `.git`, …) are merged into `config.excludeDirs`
|
|
196
|
+
// by `defaultMatchConfig`; user `excludeGlobs` (dir-shaped) add to it.
|
|
197
|
+
if (isDir && isPathExcluded(childRel, config))
|
|
135
198
|
continue;
|
|
136
|
-
// Skip
|
|
137
|
-
|
|
199
|
+
// Skip files the user's excludes filter out — exclude-side ONLY.
|
|
200
|
+
// Deliberately NOT requiring include-extension match: `list_directory`
|
|
201
|
+
// is a navigation aid, not a content-access gate, so a `LICENSE` /
|
|
202
|
+
// image / binary should still appear in the listing even though
|
|
203
|
+
// `read_file` would later reject it. The privacy-relevant cases are
|
|
204
|
+
// the exclude-side ones.
|
|
205
|
+
if (isFile && isFileExcludedByConfig(childRel, entry.name, config))
|
|
138
206
|
continue;
|
|
139
207
|
totalEntries += 1;
|
|
140
208
|
if (entries.length >= MAX_LIST_ENTRIES)
|
|
141
209
|
continue; // count but don't push
|
|
142
210
|
const child = {
|
|
143
|
-
relpath:
|
|
211
|
+
relpath: childRel,
|
|
144
212
|
type: isDir ? 'dir' : 'file',
|
|
145
213
|
};
|
|
146
214
|
if (isFile) {
|
|
@@ -167,11 +235,12 @@ export async function listDirectoryExecutor(workspaceRoot, relPath) {
|
|
|
167
235
|
* sees paths it couldn't `read_file` on anyway — avoids a foot-gun where
|
|
168
236
|
* the model tries to open a secret it saw in a `find_files` response).
|
|
169
237
|
*/
|
|
170
|
-
export async function findFilesExecutor(workspaceRoot, pattern) {
|
|
238
|
+
export async function findFilesExecutor(workspaceRoot, pattern, matchConfig) {
|
|
171
239
|
if (typeof pattern !== 'string' || pattern.trim().length === 0) {
|
|
172
240
|
throw new SandboxError('INVALID_INPUT', 'pattern is empty', pattern);
|
|
173
241
|
}
|
|
174
242
|
const regex = globToRegExp(pattern.trim());
|
|
243
|
+
const config = resolveMatchConfig(matchConfig);
|
|
175
244
|
const matches = [];
|
|
176
245
|
let totalMatches = 0;
|
|
177
246
|
// Track real (symlink-resolved) paths we've walked so a directory loop
|
|
@@ -202,38 +271,29 @@ export async function findFilesExecutor(workspaceRoot, pattern) {
|
|
|
202
271
|
return;
|
|
203
272
|
}
|
|
204
273
|
for (const entry of entries) {
|
|
274
|
+
const childRel = currentRel ? `${currentRel}/${entry.name}` : entry.name;
|
|
205
275
|
if (entry.isDirectory()) {
|
|
206
|
-
|
|
276
|
+
// Honour user `excludeGlobs` dir entries in addition to defaults
|
|
277
|
+
// — `isPathExcluded` checks the full nested path, so multi-segment
|
|
278
|
+
// globs like `src/vendor` work even when we hit them mid-walk.
|
|
279
|
+
if (isPathExcluded(childRel, config))
|
|
207
280
|
continue;
|
|
208
|
-
await walk(join(currentAbs, entry.name),
|
|
281
|
+
await walk(join(currentAbs, entry.name), childRel, depth + 1);
|
|
209
282
|
continue;
|
|
210
283
|
}
|
|
211
284
|
if (!entry.isFile())
|
|
212
285
|
continue;
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
//
|
|
217
|
-
|
|
218
|
-
// conversation even though `read_file` would reject them.
|
|
219
|
-
//
|
|
220
|
-
// Case-insensitive to mirror `readFileExecutor` (PR #24 round-4
|
|
221
|
-
// review by Gemini P2): otherwise `App.TS` / `Page.JSX` are readable
|
|
222
|
-
// but hidden from find_files/grep, leaving the model with an
|
|
223
|
-
// inconsistent view of the workspace.
|
|
224
|
-
const lowerName = entry.name.toLowerCase();
|
|
225
|
-
const hasIncludeExt = DEFAULT_INCLUDE_EXTENSIONS.some((ext) => {
|
|
226
|
-
if (ext.startsWith('.'))
|
|
227
|
-
return lowerName.endsWith(ext);
|
|
228
|
-
return lowerName === ext;
|
|
229
|
-
});
|
|
230
|
-
if (!hasIncludeExt)
|
|
286
|
+
// Unified filter: covers default filename / extension excludes,
|
|
287
|
+
// user `excludeGlobs` filename / extension entries, AND requires
|
|
288
|
+
// an include-extension match (default + user). Same predicate the
|
|
289
|
+
// eager scanner uses — no agentic / eager divergence.
|
|
290
|
+
if (!isFileIncluded(childRel, config))
|
|
231
291
|
continue;
|
|
232
|
-
if (!regex.test(
|
|
292
|
+
if (!regex.test(childRel))
|
|
233
293
|
continue;
|
|
234
294
|
totalMatches += 1;
|
|
235
295
|
if (matches.length < MAX_FIND_MATCHES)
|
|
236
|
-
matches.push(
|
|
296
|
+
matches.push(childRel);
|
|
237
297
|
}
|
|
238
298
|
}
|
|
239
299
|
await walk(workspaceRoot, '', 0);
|
|
@@ -254,19 +314,40 @@ export async function findFilesExecutor(workspaceRoot, pattern) {
|
|
|
254
314
|
* - Omit both → return the first `DEFAULT_READ_LINE_LIMIT` lines, up to
|
|
255
315
|
* `MAX_READ_BYTES`, whichever hits first.
|
|
256
316
|
*/
|
|
257
|
-
export async function readFileExecutor(workspaceRoot, relPath, startLine, endLine) {
|
|
317
|
+
export async function readFileExecutor(workspaceRoot, relPath, startLine, endLine, matchConfig) {
|
|
258
318
|
const target = await resolveInsideWorkspace(workspaceRoot, relPath);
|
|
259
|
-
|
|
260
|
-
//
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
319
|
+
const config = resolveMatchConfig(matchConfig);
|
|
320
|
+
// Unified gate (v1.9.0+). `isFileIncluded` enforces:
|
|
321
|
+
// - path not under any excluded dir (default + user `excludeGlobs`)
|
|
322
|
+
// - basename not on filename denylist (default + user)
|
|
323
|
+
// - extension not on exclude-ext list (default + user)
|
|
324
|
+
// - extension IS on include-ext list (default + user `includeGlobs`)
|
|
325
|
+
//
|
|
326
|
+
// Phase 3 (`ask` → `ask_agentic` fallback) depends on this: a user who
|
|
327
|
+
// passed `excludeGlobs: ['*.env*']` to `ask` must see the same files
|
|
328
|
+
// refused by `ask_agentic.read_file` — otherwise silently dropping their
|
|
329
|
+
// filter on fallback would be a privacy regression. The `EXCLUDED_FILE`
|
|
330
|
+
// taxonomy code is new in v1.9.0; the `NON_SOURCE_FILE` code stays for
|
|
331
|
+
// the more specific "extension not allowed at all" sub-case so callers
|
|
332
|
+
// can still distinguish "explicitly excluded by your config" from
|
|
333
|
+
// "default-rejected source-set membership".
|
|
334
|
+
if (!isFileIncluded(target.relpath, config)) {
|
|
335
|
+
// Discriminate the two failure modes via the shared helper (v1.9.0
|
|
336
|
+
// Phase 1.1, /6step Finding #3): if NO include-extension matches, the
|
|
337
|
+
// file is structurally non-source — keep the path in the message
|
|
338
|
+
// because the model needs to know which file it asked for is
|
|
339
|
+
// "wrong-tool" territory (binary, image, etc.) and the path is purely
|
|
340
|
+
// utility, not privacy-bearing. If an include-extension DOES match,
|
|
341
|
+
// the file is excluded by some other rule (filename / extension
|
|
342
|
+
// exclude / dir-prefix exclude) — emit a generic message with NO path
|
|
343
|
+
// (Finding #2) so the error string can't be used as an existence
|
|
344
|
+
// oracle for paths the user explicitly excluded. The third
|
|
345
|
+
// `SandboxError` argument (`relPath`) is preserved either way for
|
|
346
|
+
// internal logging via `requestedPath`.
|
|
347
|
+
if (!matchesAnyIncludeExtension(target.relpath, config)) {
|
|
348
|
+
throw new SandboxError('NON_SOURCE_FILE', `file extension not in allowed source set: ${target.relpath}`, relPath);
|
|
349
|
+
}
|
|
350
|
+
throw new SandboxError('EXCLUDED_FILE', 'file is excluded by configured policy', relPath);
|
|
270
351
|
}
|
|
271
352
|
// Stat first so we never allocate a >200MB Buffer for a minified bundle.
|
|
272
353
|
// Files larger than 5× MAX_READ_BYTES (1MB) get rejected with a
|
|
@@ -371,7 +452,7 @@ export async function readFileExecutor(workspaceRoot, relPath, startLine, endLin
|
|
|
371
452
|
* - each match line truncated to 500 chars (guard against minified
|
|
372
453
|
* lines flooding the payload)
|
|
373
454
|
*/
|
|
374
|
-
export async function grepExecutor(workspaceRoot, pattern, pathPrefix) {
|
|
455
|
+
export async function grepExecutor(workspaceRoot, pattern, pathPrefix, matchConfig) {
|
|
375
456
|
if (typeof pattern !== 'string' || pattern.length === 0) {
|
|
376
457
|
throw new SandboxError('INVALID_INPUT', 'grep pattern is empty', pattern);
|
|
377
458
|
}
|
|
@@ -382,6 +463,7 @@ export async function grepExecutor(workspaceRoot, pattern, pathPrefix) {
|
|
|
382
463
|
catch (err) {
|
|
383
464
|
throw new SandboxError('INVALID_INPUT', `invalid regex: ${String(err)}`, pattern);
|
|
384
465
|
}
|
|
466
|
+
const config = resolveMatchConfig(matchConfig);
|
|
385
467
|
const matches = [];
|
|
386
468
|
let totalMatches = 0;
|
|
387
469
|
let respBytes = 0;
|
|
@@ -394,6 +476,16 @@ export async function grepExecutor(workspaceRoot, pattern, pathPrefix) {
|
|
|
394
476
|
let startRel = '';
|
|
395
477
|
if (typeof pathPrefix === 'string' && pathPrefix.trim().length > 0) {
|
|
396
478
|
const resolved = await resolveInsideWorkspace(workspaceRoot, pathPrefix);
|
|
479
|
+
// Top-level gate (v1.9.0 Phase 1.1, /6step Finding #1 ext): same threat
|
|
480
|
+
// as `listDirectoryExecutor` — the requested pathPrefix must be checked
|
|
481
|
+
// against user `excludeGlobs` BEFORE walking it. Without this,
|
|
482
|
+
// `grep("regex", "internal-secrets")` (where the dir is in user
|
|
483
|
+
// excludes but NOT in DEFAULT_EXCLUDE_DIRS) succeeds-with-zero-results
|
|
484
|
+
// when the dir exists vs throws NOT_FOUND when it doesn't — same
|
|
485
|
+
// existence-probe oracle. Generic message, no path leak.
|
|
486
|
+
if (resolved.relpath && isPathExcluded(resolved.relpath, config)) {
|
|
487
|
+
throw new SandboxError('EXCLUDED_DIR', 'pathPrefix is excluded by configured policy', pathPrefix);
|
|
488
|
+
}
|
|
397
489
|
try {
|
|
398
490
|
const s = await stat(resolved.absolutePath);
|
|
399
491
|
if (!s.isDirectory()) {
|
|
@@ -436,25 +528,25 @@ export async function grepExecutor(workspaceRoot, pattern, pathPrefix) {
|
|
|
436
528
|
for (const entry of entries) {
|
|
437
529
|
if (respBytes >= MAX_RESPONSE_BYTES)
|
|
438
530
|
return;
|
|
531
|
+
const childRel = currentRel ? `${currentRel}/${entry.name}` : entry.name;
|
|
439
532
|
if (entry.isDirectory()) {
|
|
440
|
-
|
|
533
|
+
// Honour user `excludeGlobs` dir entries in addition to defaults
|
|
534
|
+
// (mirror of `findFilesExecutor`). Critical for grep specifically:
|
|
535
|
+
// dropping the user's filter here means the regex would scan files
|
|
536
|
+
// they explicitly excluded — and grep returns matched LINES, so
|
|
537
|
+
// a regex like `password|secret|api_key` could leak content from
|
|
538
|
+
// an excluded `internal-config/` dir straight into the model.
|
|
539
|
+
if (isPathExcluded(childRel, config))
|
|
441
540
|
continue;
|
|
442
|
-
await walk(join(currentAbs, entry.name),
|
|
541
|
+
await walk(join(currentAbs, entry.name), childRel, depth + 1);
|
|
443
542
|
continue;
|
|
444
543
|
}
|
|
445
544
|
if (!entry.isFile())
|
|
446
545
|
continue;
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
const lowerName = entry.name.toLowerCase();
|
|
452
|
-
const hasIncludeExt = DEFAULT_INCLUDE_EXTENSIONS.some((ext) => {
|
|
453
|
-
if (ext.startsWith('.'))
|
|
454
|
-
return lowerName.endsWith(ext);
|
|
455
|
-
return lowerName === ext;
|
|
456
|
-
});
|
|
457
|
-
if (!hasIncludeExt)
|
|
546
|
+
// Unified filter — same predicate as `findFilesExecutor` and the
|
|
547
|
+
// eager scanner. Honours default + user excludes; requires include
|
|
548
|
+
// extension. v1.9.0 closes the agentic / eager divergence.
|
|
549
|
+
if (!isFileIncluded(childRel, config))
|
|
458
550
|
continue;
|
|
459
551
|
// Read full file (bounded by MAX_READ_BYTES via soft stat check).
|
|
460
552
|
const absFile = join(currentAbs, entry.name);
|
|
@@ -487,7 +579,7 @@ export async function grepExecutor(workspaceRoot, pattern, pathPrefix) {
|
|
|
487
579
|
// Cap line length in the payload to 500 chars — minified files
|
|
488
580
|
// can have multi-megabyte single lines.
|
|
489
581
|
const trimmed = line.length > 500 ? `${line.slice(0, 500)}…` : line;
|
|
490
|
-
const match = { relpath:
|
|
582
|
+
const match = { relpath: childRel, line: i + 1, text: trimmed };
|
|
491
583
|
matches.push(match);
|
|
492
584
|
// Use UTF-8 byte length (not .length which is UTF-16 code units)
|
|
493
585
|
// so the 500k cap holds on CJK / emoji-heavy matches. `+32` is
|