Repository navigation
Expand file tree
/
Copy pathserver.ts
More file actions
4991 lines (4630 loc) · 221 KB
/
Copy pathserver.ts
File metadata and controls
4991 lines (4630 loc) · 221 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { createRequire } from "node:module";
import { existsSync, unlinkSync, readdirSync, readFileSync, writeFileSync, writeSync, renameSync, rmSync, mkdirSync, cpSync, statSync, symlinkSync, lstatSync, realpathSync } from "node:fs";
import { spawnSync, type SpawnSyncOptions, type SpawnSyncReturns } from "node:child_process";
import { join, dirname, resolve, sep, isAbsolute } from "node:path";
import { fileURLToPath } from "node:url";
import { homedir, tmpdir, cpus, platform } from "node:os";
import { request as httpsRequest } from "node:https";
import { AsyncLocalStorage } from "node:async_hooks";
import { z } from "zod";
import { PolyglotExecutor } from "./executor.js";
import { runPool, type PoolJob } from "./runPool.js";
import { ContentStore, cleanupStaleDBs, cleanupStaleContentDBs, type SearchResult, type IndexResult } from "./store.js";
import { composeFetchCacheKey } from "./fetch-cache.js";
import {
readBashPolicies,
evaluateCommandDenyOnly,
extractShellCommands,
readToolDenyPatterns,
readToolPermissionPatterns,
evaluateFilePath,
evaluateProjectContainment,
} from "./security.js";
import {
detectRuntimes,
getRuntimeSummary,
getAvailableLanguages,
hasBunRuntime,
} from "./runtime.js";
import { classifyNonZeroExit } from "./exit-classify.js";
import { startLifecycleGuard, noteMcpActivity, noteRequestStart, noteRequestEnd, attachMcpActivityTap } from "./lifecycle.js";
import { charSafePrefix } from "./truncate.js";
import {
describeStorageDirectorySource,
ensureWritableStorageDir,
formatStorageDirectoryError,
hashProjectDirCanonical,
hashProjectDirLegacy,
resolveContentStorePath,
resolveContentStorageDir,
resolveDefaultSessionDir,
resolveSessionDbPath,
resolveSessionStorageDir,
resolveStatsStorageDir,
SessionDB,
StorageDirectoryError,
} from "./session/db.js";
import { purgeSession } from "./session/purge.js";
import {
emitCacheHitEvent,
emitIndexWriteEvent,
emitSandboxExecuteEvent,
} from "./session/event-emit.js";
import { persistToolCallCounter, restoreSessionStats } from "./session/persist-tool-calls.js";
import { appendRetrievalBytes } from "./session/retrieval-marker.js";
import { searchAllSources } from "./search/unified.js";
import {
buildCtxSearchInputSchema,
CTX_SEARCH_SHARED_MODE,
resolveProjectScope,
} from "./search/ctx-search-schema.js";
import { FloodGuard } from "./search/flood-guard.js";
import { buildNodeCommand, type HookAdapter, type PlatformId, isInProcessPluginPlatform } from "./adapters/types.js";
import { detectPlatform, getSessionDirSegments } from "./adapters/detect.js";
import { parseCodexContextModePluginRoot } from "./adapters/codex/index.js";
import { getHookScriptPaths } from "./util/hook-config.js";
import { stripJsonComments } from "./util/jsonc.js";
import { resolveClaudeConfigDir } from "./util/claude-config.js";
import { resolveProjectDir } from "./util/project-dir.js";
import { loadDatabase } from "./db-base.js";
import { AnalyticsEngine, formatReport, getConversationStats, getContentBytesAllSessions, getConversationWindowStats, getLifetimeStats, getMultiAdapterLifetimeStats, getRealBytesStats, pricePerToken } from "./session/analytics.js";
const __pkg_dir = dirname(fileURLToPath(import.meta.url));
const VERSION: string = (() => {
for (const rel of ["../package.json", "./package.json"]) {
const p = resolve(__pkg_dir, rel);
if (existsSync(p)) {
try { return JSON.parse(readFileSync(p, "utf8")).version; } catch {}
}
}
return "unknown";
})();
function getPackageRoot(): string {
return existsSync(resolve(__pkg_dir, "package.json")) ? __pkg_dir : dirname(__pkg_dir);
}
function resolveCodexRuntimePluginRoot(fallbackRoot: string): string {
try {
const probe = process.platform === "win32"
? spawnSync("cmd.exe", ["/d", "/s", "/c", "codex plugin list"], {
encoding: "utf-8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 5000,
})
: spawnSync("codex", ["plugin", "list"], {
encoding: "utf-8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 5000,
});
if (probe.status !== 0) return fallbackRoot;
const runtimeRoot = parseCodexContextModePluginRoot(String(probe.stdout));
if (runtimeRoot && existsSync(resolve(runtimeRoot, ".codex-plugin", "hooks.json"))) {
return runtimeRoot;
}
} catch {
// Best effort only. Non-Codex hosts and older Codex builds may not expose
// plugin list; keep the package-root fallback for those environments.
}
return fallbackRoot;
}
function getRuntimeAwarePackageRoot(platformId?: PlatformId): string {
const packageRoot = getPackageRoot();
return platformId === "codex"
? resolveCodexRuntimePluginRoot(packageRoot)
: packageRoot;
}
// Prevent silent MCP server death from unhandled async errors.
//
// Guarded for plugin-native OpenCode/Kilo imports (#574): when server.js is
// imported only to reuse the ctx_* tool registry, these handlers would become
// process-wide OpenCode/Kilo host handlers. In Node, adding an
// `uncaughtException` listener changes default crash behavior, so only the
// standalone MCP process may install them.
if (process.env.CONTEXT_MODE_EMBEDDED_PLUGIN_TOOLS !== "1") {
process.on("unhandledRejection", (err) => {
process.stderr.write(`[context-mode] unhandledRejection: ${err}\n`);
});
process.on("uncaughtException", (err) => {
try {
writeSync(2, `[context-mode] uncaughtException: ${err?.message ?? err}\n`);
} finally {
process.exit(1);
}
});
}
const runtimes = detectRuntimes();
const available = getAvailableLanguages(runtimes);
export const server = new McpServer({
name: "context-mode",
version: VERSION,
});
export interface RegisteredCtxTool {
name: string;
config: Record<string, unknown>;
handler: (args: Record<string, unknown>) => Promise<unknown> | unknown;
}
export const REGISTERED_CTX_TOOLS: RegisteredCtxTool[] = [];
export function shouldSuppressMcpToolsForNativePluginHost(
opts: { embedded?: string; platform?: PlatformId; settings?: Record<string, unknown> | null } = {},
): boolean {
const embedded = opts.embedded ?? process.env.CONTEXT_MODE_EMBEDDED_PLUGIN_TOOLS;
if (embedded === "1") return false;
const platform = opts.platform ?? detectPlatform().platform;
if (platform !== "opencode" && platform !== "kilo") return false;
const settings = opts.settings ?? readNativePluginHostSettings(platform);
return settingsHasContextModePlugin(settings) && settingsHasLegacyContextModeMcp(settings);
}
function readNativePluginHostSettings(platform: PlatformId): Record<string, unknown> | null {
const base = platform === "kilo" ? "kilo" : "opencode";
const paths = [
resolve(`${base}.json`),
resolve(`${base}.jsonc`),
resolve(`.${base}`, `${base}.json`),
resolve(`.${base}`, `${base}.jsonc`),
join(homedir(), ".config", base, `${base}.json`),
join(homedir(), ".config", base, `${base}.jsonc`),
];
for (const p of paths) {
try {
if (!existsSync(p)) continue;
return JSON.parse(stripJsonComments(readFileSync(p, "utf8"))) as Record<string, unknown>;
} catch { /* try next config path */ }
}
return null;
}
function settingsHasContextModePlugin(settings: Record<string, unknown> | null | undefined): boolean {
const plugins = settings?.plugin;
return Array.isArray(plugins) && plugins.some((p) => typeof p === "string" && p.includes("context-mode"));
}
function settingsHasLegacyContextModeMcp(settings: Record<string, unknown> | null | undefined): boolean {
const mcp = settings?.mcp;
return !!(
mcp &&
typeof mcp === "object" &&
!Array.isArray(mcp) &&
Object.prototype.hasOwnProperty.call(mcp, "context-mode")
);
}
const suppressMcpToolsForNativePluginHost = shouldSuppressMcpToolsForNativePluginHost();
/**
* Issue #623 — surface why ctx_* tools/list is empty on suppressed legacy MCP
* children. When a user upgrades OpenCode/Kilo from v1.0.136 → v1.0.137+ without
* running `context-mode upgrade`, their opencode.json still has BOTH the legacy
* mcp.context-mode block AND the plugin entry. The plugin path registers the
* tools natively, but the legacy MCP child runs in parallel and used to expose
* duplicate tools — v1.0.137 suppressed those duplicates. The suppression was
* silent, leaving any MCP client that inspected the child via tools/list with
* an empty list and no diagnostic. Emit one stderr line per process so an
* operator running the child directly (or any non-plugin MCP host) sees the
* exact reason and the `context-mode upgrade` fix.
*
* Exported for test (suppression-diagnostic regression guard).
*/
let __suppressionDiagnosticEmitted = false;
export function emitSuppressionDiagnostic(
opts: { platform?: string; write?: (chunk: string) => void } = {},
): void {
if (__suppressionDiagnosticEmitted) return;
__suppressionDiagnosticEmitted = true;
const write = opts.write ?? ((c: string) => { process.stderr.write(c); });
const platform = opts.platform ?? "opencode/kilo";
write(
`[context-mode] ctx_* tools/list intentionally empty on this MCP child: ` +
`legacy mcp.context-mode block coexists with plugin: ["context-mode"] in ` +
`${platform}.json — plugin-native tools are the supported path (#623). ` +
`Run \`context-mode upgrade\` to remove the legacy block (preserves other ` +
`MCP servers).\n`
);
}
/** Test-only: reset the one-shot emission flag so suites can re-exercise. */
export function __resetSuppressionDiagnosticForTests(): void {
__suppressionDiagnosticEmitted = false;
}
/**
* Issue #637 — register an explicit empty `tools/list` handler on the McpServer.
*
* Background: when `suppressMcpToolsForNativePluginHost` is true, every
* `server.registerTool()` call is short-circuited (returns `undefined` above).
* The MCP SDK only installs the SDK-default `tools/list` handler when at least
* one `registerTool()` reaches `setToolRequestHandlers()` internally
* (mcp.js:56-67). Suppressing every registration leaves `tools/list`
* unregistered, and the framework's RPC layer answers it with
* `-32601 "Method not found"`.
*
* The reporter of #637 (SquirrelRat) inspected the suppressed child via
* `tools/list` and read the JSON-RPC error as "the plugin never registers any
* ctx_* tools" — when in fact the plugin DOES register all 11 tools natively
* (verified at `src/adapters/opencode/plugin.ts:469` and
* `tests/opencode-plugin.test.ts:88`). The misleading -32601 is the seed of
* the #637 perception.
*
* This helper installs an explicit handler that returns `{tools: []}` — a
* spec-compliant empty list. Paired with the existing #623 stderr diagnostic,
* an operator now sees:
* - wire response: `{tools: []}` (matches expectation, no JSON-RPC error)
* - stderr: `[context-mode] ctx_* tools/list intentionally empty… (#623)`
*
* Idempotent: throws inside SDK if called twice on the same server because
* `assertCanSetRequestHandler` (mcp.js:60) rejects duplicate registrations;
* we therefore install the SDK's default tool handlers FIRST (via a no-op
* registerTool of a fake tool, immediately removed) only if needed. To keep
* the public surface minimal, we just call `server.server.setRequestHandler`
* directly — that is the same low-level call used for prompts/resources at
* server.ts:259-261 and avoids the SDK guard entirely.
*
* Exported for test (#637 in-memory regression guard).
*/
export function registerEmptyToolsListHandler(target: McpServer = server): void {
target.server.registerCapabilities({ tools: { listChanged: false } });
target.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [] }));
}
const originalRegisterTool = server.registerTool.bind(server);
(server as unknown as { registerTool: (...args: unknown[]) => unknown }).registerTool = (...args: unknown[]) => {
const [name, config, handler] = args as [
string,
Record<string, unknown>,
(toolArgs: Record<string, unknown>) => Promise<unknown> | unknown,
];
if (suppressMcpToolsForNativePluginHost) {
emitSuppressionDiagnostic();
return undefined;
}
const wrappedHandler = wrapToolHandler(name, handler);
REGISTERED_CTX_TOOLS.push({ name, config, handler: wrappedHandler });
args[2] = wrappedHandler;
return (originalRegisterTool as unknown as (...callArgs: unknown[]) => unknown)(...args);
};
function wrapToolHandler(
name: string,
handler: (toolArgs: Record<string, unknown>) => Promise<unknown> | unknown,
): (toolArgs: Record<string, unknown>) => Promise<unknown> {
return async (toolArgs: Record<string, unknown>) => {
// #854: mark a tool call in-flight so the bridge-child idle reaper never
// shuts the server down mid-execution during a long ctx_execute/batch that
// emits no further inbound messages. Symmetric end in finally (success+error).
noteRequestStart();
try {
return await handler(toolArgs);
} catch (err) {
const result = storageErrorResult(err);
if (result) {
try {
return trackResponse(name, result);
} catch (trackErr) {
if (trackErr instanceof StorageDirectoryError) return result;
throw trackErr;
}
}
throw err;
} finally {
noteRequestEnd();
}
};
}
// Issue #637 — when suppression is active, install the empty tools/list handler
// once at module-init time so the suppressed MCP child responds with
// `{tools: []}` instead of JSON-RPC `-32601 Method not found`. Pair with the
// #623 stderr diagnostic that explains WHY the list is empty. Skipped for the
// embedded plugin-import path because the embedded process is not the stdio
// MCP child an operator would inspect — it lives inside the OpenCode/Kilo
// host and never speaks JSON-RPC over stdio.
if (suppressMcpToolsForNativePluginHost && process.env.CONTEXT_MODE_EMBEDDED_PLUGIN_TOOLS !== "1") {
registerEmptyToolsListHandler(server);
}
type ToolContextOverride = { projectDir: string; sessionId?: string };
const projectDirOverride = new AsyncLocalStorage<ToolContextOverride>();
export async function withProjectDirOverride<T>(
projectDir: string | ToolContextOverride,
fn: () => Promise<T>,
): Promise<T> {
const ctx = typeof projectDir === "string" ? { projectDir } : projectDir;
return projectDirOverride.run(ctx, fn);
}
// Register empty prompts/resources handlers so MCP clients don't get -32601 (#168).
// OpenCode calls listPrompts()/listResources() unconditionally — the error can poison
// the SDK transport layer, causing subsequent listTools() calls to fail permanently.
import { ListPromptsRequestSchema, ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
server.server.registerCapabilities({ prompts: { listChanged: false }, resources: { listChanged: false } });
server.server.setRequestHandler(ListPromptsRequestSchema, async () => ({ prompts: [] }));
server.server.setRequestHandler(ListResourcesRequestSchema, async () => ({ resources: [] }));
server.server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => ({ resourceTemplates: [] }));
// ── Strict-client (Gemini function-calling) schema compatibility ──────────────
// Gemini's function-calling API — used by Antigravity CLI (`agy`) and Gemini CLI
// — rejects JSON Schema `const` and `additionalProperties`. A rejected parameter
// schema makes the host SILENTLY DROP that tool from the model's function list,
// so the agent never sees our ctx_* tools and falls back to hand-rolling the MCP
// protocol through its Bash tool. Sanitize the EMITTED tools/list schema:
// • `const: X` → `enum: [X]` — an identical single-value constraint
// • drop `additionalProperties` — advisory only; every ctx_* handler parses
// args with Zod (which strips unknown keys server-side), so removing it
// changes no validation and no call behavior.
// Both transforms are behavior-preserving for every other client (Claude Code,
// Copilot, Cursor, …): `const` and a one-value `enum` are equivalent, and no
// model sends undeclared properties. Only the wire schema changes — never
// validation or how any tool is invoked.
export function sanitizeSchemaForStrictClients(node: unknown): unknown {
if (Array.isArray(node)) return node.map(sanitizeSchemaForStrictClients);
if (node === null || typeof node !== "object") return node;
const out: Record<string, unknown> = {};
for (const [key, value] of Object.entries(node as Record<string, unknown>)) {
if (key === "additionalProperties") continue;
if (key === "const") {
out.enum = [value];
continue;
}
out[key] = sanitizeSchemaForStrictClients(value);
}
return out;
}
// Wrap the SDK-installed tools/list handler so its generated schemas pass through
// the sanitizer above. Best-effort by design: if the MCP SDK's internals shift,
// the original handler is left untouched (no regression — strict clients stay as
// they were, every other client unaffected). Must run AFTER all registerTool()
// calls so the SDK's default tools/list handler already exists.
export function installStrictClientSchemaCompat(target: McpServer = server): void {
try {
const low = target.server as unknown as {
_requestHandlers?: Map<string, (req: unknown, extra: unknown) => Promise<unknown>>;
};
const original = low._requestHandlers?.get("tools/list");
if (typeof original !== "function") return;
target.server.setRequestHandler(ListToolsRequestSchema, async (req, extra) => {
const result = (await original(req as unknown, extra as unknown)) as
| { tools?: Array<{ inputSchema?: unknown }> }
| undefined;
if (result && Array.isArray(result.tools)) {
for (const tool of result.tools) {
if (!tool || tool.inputSchema == null) continue;
try {
tool.inputSchema = sanitizeSchemaForStrictClients(tool.inputSchema);
} catch {
/* leave this tool's schema unchanged */
}
}
}
return result as never;
});
} catch {
/* best-effort — never break tools/list */
}
}
const executor = new PolyglotExecutor({
runtimes,
projectRoot: () => getProjectDir(),
});
// ─────────────────────────────────────────────────────────
// FS read tracking preload for ctx_batch_execute
// ─────────────────────────────────────────────────────────
// NODE_OPTIONS is denied by the executor's #buildSafeEnv (security).
// Instead, we inject it as an inline shell env prefix in each batch command.
// This temp file is loaded via --require when batch commands spawn Node processes.
const CM_FS_PRELOAD = join(tmpdir(), `cm-fs-preload-${process.pid}.js`);
writeFileSync(
CM_FS_PRELOAD,
`(function(){var __cm_fs=0;process.on('exit',function(){if(__cm_fs>0)try{process.stderr.write('__CM_FS__:'+__cm_fs+'\\n')}catch(e){}});try{var f=require('fs');var ors=f.readFileSync;f.readFileSync=function(){var r=ors.apply(this,arguments);if(Buffer.isBuffer(r))__cm_fs+=r.length;else if(typeof r==='string')__cm_fs+=Buffer.byteLength(r);return r;};}catch(e){}})();\n`,
);
// In the stdio MCP path, main() also removes this file during graceful
// shutdown. Plugin-native OpenCode/Kilo imports skip main() (#574), so
// register a top-level best-effort cleanup too to avoid leaking preload
// snippets under /tmp when the host process exits.
process.on("exit", () => { try { unlinkSync(CM_FS_PRELOAD); } catch { /* best effort */ } });
// Lazy singleton — no DB overhead unless index/search is used
let _store: ContentStore | null = null;
/**
* Build the FK-attribution object passed to every ContentStore.index*() call
* in this process. CLAUDE_SESSION_ID is the only MCP-side handle we have on
* the current session — eventId stays undefined because MCP tool invocations
* are not paired with PostToolUse event rows at index time (the hook fires
* AFTER the tool returns). Empty-string fallback inside #insertChunks keeps
* legacy unattributed rows readable.
*/
export function currentAttribution(): { sessionId?: string } | undefined {
const override = projectDirOverride.getStore();
if (override?.sessionId) return { sessionId: override.sessionId };
// CLAUDE_SESSION_ID env var is NOT propagated to MCP servers (only to hooks).
// Cross-adapter resolution: every adapter (15 of them) sets *_PROJECT_DIR env
// and writes session_events via hooks. Read the most-recent session_id from
// THIS project's session DB. Works for claude-code/cursor/gemini-cli/codex/
// kiro/opencode/zed/kilo/openclaw/qwen-code/vscode-copilot/jetbrains-copilot/
// omp/pi/antigravity — no adapter-specific transcript path required.
const sessionId = process.env.CLAUDE_SESSION_ID ?? resolveSessionIdFromSessionDB();
if (!sessionId) return undefined;
return { sessionId };
}
let __cachedSessionId: { sid: string; checkedAt: number } | undefined;
/** v1.0.134 SLICE A: opts injection for testability. Production callers pass nothing. */
export function resolveSessionIdFromSessionDB(opts?: {
projectDir?: string;
sessionsDir?: string;
bypassCache?: boolean;
}): string | undefined {
// 2s cache — ctx_fetch_and_index can fire 5+ chunks/sec; DB open cost adds up.
const now = Date.now();
if (!opts?.bypassCache && __cachedSessionId && now - __cachedSessionId.checkedAt < 2000) {
return __cachedSessionId.sid;
}
try {
const projectDir = opts?.projectDir
?? process.env.CLAUDE_PROJECT_DIR
?? process.env.CONTEXT_MODE_PROJECT_DIR;
if (!projectDir) return undefined;
const sessionsDir = opts?.sessionsDir ?? getSessionDir();
const dbPath = resolveSessionDbPath({ projectDir, sessionsDir });
if (!existsSync(dbPath)) return undefined;
const Database = loadDatabase();
const db = new Database(dbPath, { readonly: true, fileMustExist: true });
try {
const row = db.prepare(
"SELECT session_id FROM session_events ORDER BY created_at DESC LIMIT 1"
).get() as { session_id?: string } | undefined;
const sid = row?.session_id;
if (sid) __cachedSessionId = { sid, checkedAt: now };
return sid;
} finally {
try { db.close(); } catch { /* best-effort */ }
}
} catch {
return undefined;
}
}
/**
* Auto-index session events files written by SessionStart hook.
* Scans ~/.claude/context-mode/sessions/ for *-events.md files.
* CLAUDE_PROJECT_DIR is NOT available to MCP servers — only to hooks —
* so we glob-scan instead of computing a specific hash.
* Files are consumed (deleted) after indexing to prevent double-indexing.
* Called on every getStore() — readdirSync is sub-millisecond when no files match.
*/
function maybeIndexSessionEvents(store: ContentStore): void {
try {
const sessionsDir = getSessionDir();
if (!existsSync(sessionsDir)) return;
const files = readdirSync(sessionsDir).filter(f => f.endsWith("-events.md"));
for (const file of files) {
const filePath = join(sessionsDir, file);
try {
store.index({ path: filePath, source: "session-events", attribution: currentAttribution() });
unlinkSync(filePath);
} catch { /* best-effort per file */ }
}
} catch { /* best-effort — session continuity never blocks tools */ }
}
// ── Platform-aware paths ──────────────────────────────────────────────────
// The adapter (stored after MCP handshake) is the canonical source for
// platform-specific paths. All session DB paths go through it — no
// hardcoded configDir detection in tool handlers.
let _detectedAdapter: HookAdapter | null = null;
/**
* Resolve the Claude Code config root, honoring `CLAUDE_CONFIG_DIR` (incl.
* leading `~`) before falling back to `~/.claude`. Mirrors
* `hooks/session-helpers.mjs::resolveConfigDir` and
* `ClaudeCodeAdapter.getConfigDir` so the pre-detection path agrees with
* hooks/adapter on where Claude Code session data lives. See issue #453.
*
* Issue #460 round-3: delegates to the canonical util so empty/whitespace
* env values fall back instead of poisoning downstream `join()` calls.
*/
async function getDiagnosticAdapter(): Promise<HookAdapter | null> {
if (_detectedAdapter) return _detectedAdapter;
try {
const { getAdapter } = await import("./adapters/detect.js");
const signal = detectPlatform();
return await getAdapter(signal.platform);
} catch {
return null;
}
}
/**
* Get the platform-specific sessions directory from the detected adapter.
* Falls back to the detected platform config root before adapter detection.
*/
function getDefaultSessionDir(): string {
if (_detectedAdapter) return _detectedAdapter.getSessionDir();
// Pre-detection path (race window before MCP `initialize` completes):
// call detectPlatform() (sync, env-var-based) and look up segments via
// getSessionDirSegments() (sync map, no adapter instantiation). This keeps
// non-Claude platforms from spilling sessions into ~/.claude/. For Claude
// Code/Codex (single-segment roots), reroute through their config-dir
// contracts so the pre-detection window does not split-state with hooks.
try {
const signal = detectPlatform();
const segments = getSessionDirSegments(signal.platform);
if (segments) {
return resolveDefaultSessionDir({
configDir: join(...segments),
configDirEnv: configDirEnvForSessionSegments(segments),
});
}
} catch { /* fall through to claude fallback */ }
return resolveDefaultSessionDir({ configDir: ".claude", configDirEnv: "CLAUDE_CONFIG_DIR" });
}
function configDirEnvForSessionSegments(segments: string[]): string | undefined {
if (segments.length === 1 && segments[0] === ".claude") return "CLAUDE_CONFIG_DIR";
if (segments.length === 1 && segments[0] === ".codex") return "CODEX_HOME";
return undefined;
}
function getSessionDir(): string {
return ensureWritableStorageDir(resolveSessionStorageDir(getDefaultSessionDir));
}
/**
* Project directory detection across supported platforms.
*
* Priority:
* 1. Platform-specific env var (set by host IDE before MCP server spawn)
* 2. CONTEXT_MODE_PROJECT_DIR (set by start.mjs for ALL platforms — universal)
* 3. process.cwd() (last resort)
*
* CONTEXT_MODE_PROJECT_DIR guarantees correct projectDir even for platforms
* that don't set their own env var (Cursor, OpenClaw, Codex, Kiro, Zed).
*/
export function getProjectDir(): string {
const override = projectDirOverride.getStore();
if (override) return override.projectDir;
// Delegated to the shared resolver so the env-var chain rejects plugin
// install paths (set by a prior MCP boot's start.mjs after `/ctx-upgrade`)
// and prefers the shell-set PWD before the chdir'd cwd. v1.0.115 adds
// the Claude Code transcript heuristic — read `cwd` from the most-recently-
// modified `~/.claude/projects/<encoded>/<session>.jsonl` to recover the
// real project dir when MCP was launched from a non-project cwd (desktop-
// app launch, /ctx-upgrade respawn). See src/util/project-dir.ts.
//
// Issue #521 (v1.0.119): the transcript heuristic ONLY applies on Claude
// Code. Other platforms (Cursor, OpenCode, Codex, ...) either have no
// transcript at that path or use a different schema without `cwd`. Worse,
// a Cursor user who also runs Claude Code would pick up the most-recently-
// modified Claude Code session's cwd — wrong project entirely. Gate the
// path on detected platform so non-Claude hosts skip the heuristic and
// fall through to PWD/cwd cleanly.
//
// The Claude heuristic must also be fresh. Hosts such as Pi can be
// misdetected as Claude Code solely because ~/.claude exists; without a
// freshness guard an old Claude transcript can globally hijack ctx shell cwd
// after reboot. Active Claude sessions update their transcript as the user
// interacts, so stale transcripts should fall through to PWD/cwd.
//
// Issue #545 (v1.0.124): pass strictPlatform for ALL adapters so the
// env-var cascade is built ALGORITHMICALLY from the platform's own
// workspace vars + universal escape hatch — foreign workspace vars (e.g.
// CLAUDE_PROJECT_DIR leaked into Pi's MCP child env from the user's shell)
// cannot win, regardless of cascade order. start.mjs intentionally does
// NOT pass strictPlatform — host detection is unreliable at the entrypoint
// and the legacy literal cascade is preserved there for semver safety.
let transcriptsRoot: string | undefined;
let strictPlatform: PlatformId | undefined;
let codexHome: string | undefined;
try {
const detected = detectPlatform().platform;
strictPlatform = detected;
if (detected === "claude-code") {
transcriptsRoot = join(homedir(), ".claude", "projects");
}
// Issue #45 — Codex publishes no workspace env var, so the resolver
// reads `meta.cwd` from the most-recently-modified session.jsonl under
// `${codexHome}/sessions/`. Wire codexHome at the call site so the
// resolver can be exercised under test without process-level mutation.
if (detected === "codex") {
codexHome = process.env.CODEX_HOME ?? join(homedir(), ".codex");
}
} catch { /* detection failure — leave undefined, resolver uses legacy cascade */ }
return resolveProjectDir({
env: process.env,
cwd: process.cwd(),
pwd: process.env.PWD,
transcriptsRoot,
transcriptMaxAgeMs: 5 * 60 * 1000,
strictPlatform,
codexHome,
});
}
/**
* Resolve a possibly-relative path against the project directory (full env cascade),
* not the MCP server's process.cwd(). MCP server is spawned by the host and its cwd
* is unrelated to where the user is working.
*/
function resolveProjectPath(filePath: string): string {
return isAbsolute(filePath) ? filePath : resolve(getProjectDir(), filePath);
}
/**
* Resolve the per-project SessionDB path. Delegates to
* {@link resolveSessionDbPath} so casing-only variants of the same
* physical worktree on macOS / Windows hit ONE DB, not two — and any
* pre-existing legacy raw-casing DB gets migrated in place on first
* resolve. Linux is a no-op.
*/
function getSessionDbPath(): string {
return resolveSessionDbPath({
projectDir: getProjectDir(),
sessionsDir: getSessionDir(),
});
}
/**
* Compute a per-project, per-platform persistent path for the ContentStore.
* Derives content dir from the adapter's session dir so each platform
* has its own isolated FTS5 DB — no cross-platform data sharing.
*
* Layout: ~/<configDir>/context-mode/content/<hash>.db
* e.g. ~/.claude/context-mode/content/87c28c41ddb64d38.db
* ~/.cursor/context-mode/content/87c28c41ddb64d38.db
*/
function getStorePath(): string {
const dir = ensureWritableStorageDir(resolveContentStorageDir(getDefaultSessionDir));
// Delegate to resolveContentStorePath: same case-fold + one-shot legacy
// rename behavior as resolveSessionDbPath. On macOS / Windows, an
// existing legacy raw-casing FTS5 db (with -wal/-shm sidecars) is
// migrated in place on first call. On Linux it's a no-op.
return resolveContentStorePath({ projectDir: getProjectDir(), contentDir: dir });
}
function getStore(): ContentStore {
if (!_store) {
// Content DB cleanup on fresh start is handled by SessionStart hook.
// Server just opens whatever DB exists (or creates new if hook deleted it).
const dbPath = getStorePath();
_store = new ContentStore(dbPath);
// Wire deny-policy hook: store re-checks the Read deny list before
// re-reading any file_path during auto-refresh. Catches policy edits
// made after a file was originally indexed. See #442 round-3.
_store.setDenyChecker((filePath: string) => {
try {
const projectDir = getProjectDir();
const denyGlobs = readToolDenyPatterns("Read", projectDir);
const r = evaluateFilePath(
filePath,
denyGlobs,
process.platform === "win32",
projectDir,
);
return r.denied;
} catch {
// Fail-closed for refresh: skip on error rather than re-read.
return true;
}
});
// One-time startup cleanup: remove stale content DBs (>14 days)
try {
const contentDir = dirname(getStorePath());
cleanupStaleContentDBs(contentDir, 14);
_store.cleanupStaleSources(14);
// Also clean legacy shared dir from before platform isolation
const legacyDir = join(homedir(), ".context-mode", "content");
if (existsSync(legacyDir)) cleanupStaleContentDBs(legacyDir, 0);
} catch { /* best-effort */ }
// Also clean old PID-based DBs from migration
cleanupStaleDBs();
}
maybeIndexSessionEvents(_store);
return _store;
}
// ─────────────────────────────────────────────────────────
// Session stats — track context consumption per tool
// ─────────────────────────────────────────────────────────
const sessionStats = {
calls: {} as Record<string, number>,
bytesReturned: {} as Record<string, number>,
bytesIndexed: 0,
bytesSandboxed: 0, // network I/O consumed inside sandbox (never enters context)
cacheHits: 0,
cacheMisses: 0, // ctx_fetch_and_index calls that bypassed the TTL cache
cacheBytesSaved: 0, // bytes avoided by TTL cache hits
sessionStart: Date.now(),
};
type ToolResult = {
content: Array<{ type: "text"; text: string }>;
isError?: boolean;
};
function storageErrorResult(err: unknown): ToolResult | null {
if (!(err instanceof StorageDirectoryError)) return null;
return {
content: [{ type: "text", text: formatStorageDirectoryError(err) }],
isError: true,
};
}
// ── Version outdated warning ──────────────────────────────────────────────
// Non-blocking npm check at startup. trackResponse prepends warning
// using a burst cadence: 3 warnings → 1h silent → 3 warnings → repeat.
let _latestVersion: string | null = null;
let _warningBurstCount = 0;
let _lastBurstStart = 0;
const VERSION_BURST_SIZE = 3;
const VERSION_SILENT_MS = 60 * 60 * 1000; // 1 hour
async function fetchLatestVersion(): Promise<string> {
return new Promise((res) => {
const req = httpsRequest(
"https://registry.npmjs.org/context-mode/latest",
{ headers: { Connection: "close" } },
(resp) => {
let raw = "";
resp.on("data", (chunk: Buffer) => { raw += chunk; });
resp.on("end", () => {
try {
const data = JSON.parse(raw) as { version?: string };
res(data.version ?? "unknown");
} catch { res("unknown"); }
});
},
);
req.on("error", () => res("unknown"));
req.setTimeout(5000, () => { req.destroy(); res("unknown"); });
req.end();
});
}
function getUpgradeHint(): string {
const name = _detectedAdapter?.name;
if (name === "Claude Code") return "/ctx-upgrade";
if (name === "OpenClaw") return "npm run install:openclaw";
if (name === "Pi") return "npm run build";
return "npm update -g context-mode";
}
function semverNewer(a: string, b: string): boolean {
const pa = a.split(".").map(Number);
const pb = b.split(".").map(Number);
for (let i = 0; i < 3; i++) {
if ((pa[i] ?? 0) > (pb[i] ?? 0)) return true;
if ((pa[i] ?? 0) < (pb[i] ?? 0)) return false;
}
return false;
}
function isOutdated(): boolean {
if (!_latestVersion || _latestVersion === "unknown") return false;
return semverNewer(_latestVersion, VERSION);
}
function shouldShowVersionWarning(): boolean {
if (!isOutdated()) return false;
const now = Date.now();
// Start of a new burst?
if (_warningBurstCount >= VERSION_BURST_SIZE) {
if (now - _lastBurstStart < VERSION_SILENT_MS) return false; // still silent
_warningBurstCount = 0; // silence over, reset burst
}
if (_warningBurstCount === 0) _lastBurstStart = now;
_warningBurstCount++;
return true;
}
// ── Self-heal Layer 2: Mid-session registry heal (anthropics/claude-code#46915) ──
// Runs once on first tool call. If Claude Code auto-updated the registry mid-session,
// hooks break because CLAUDE_PLUGIN_ROOT points to a deleted directory. We create a
// symlink from the broken path to our actual directory so hooks recover.
let _cacheHealDone = false;
function healCacheMidSession(): void {
if (_cacheHealDone) return;
_cacheHealDone = true;
try {
// Issue #460 round-3: honor $CLAUDE_CONFIG_DIR so users who relocate
// their CC config root don't have plugin cache healing operate against
// the wrong tree (and silently miss dangling-symlink cleanup).
const claudeRoot = resolveClaudeConfigDir();
const ipPath = resolve(claudeRoot, "plugins", "installed_plugins.json");
if (!existsSync(ipPath)) return;
const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
const cacheRoot = resolve(claudeRoot, "plugins", "cache");
// Issue #795: canonicalize cacheRoot so the traversal guard works when
// ~/.claude is a symlink to another volume. path.resolve() does not
// dereference symlinks, so installPath values stored as physical paths
// (e.g. /Volumes/SSD/.../plugins/cache/...) would fail the startsWith
// check against a symlink-path cacheRoot (/Users/me/.claude/...).
// realpathSync follows the symlink chain to the canonical location.
let cacheRootCanon: string;
try { cacheRootCanon = realpathSync(cacheRoot); }
catch { cacheRootCanon = cacheRoot; }
// Plugin root: build/ for tsc, plugin root for bundle
const pluginRoot = getPackageRoot();
for (const [key, entries] of Object.entries((ip.plugins ?? {}) as Record<string, Array<{ installPath?: string }>>)) {
if (key !== "context-mode@context-mode") continue;
for (const entry of entries) {
const rp = entry.installPath;
if (!rp || existsSync(rp)) continue;
// Path traversal guard (canonical comparison — see #795)
if (!resolve(rp).startsWith(cacheRootCanon + sep)) continue;
// Remove dangling symlink
try { if (lstatSync(rp).isSymbolicLink()) unlinkSync(rp); } catch {}
const parent = dirname(rp);
if (!existsSync(parent)) mkdirSync(parent, { recursive: true });
if (existsSync(pluginRoot)) {
symlinkSync(pluginRoot, rp, process.platform === "win32" ? "junction" : undefined);
}
}
}
} catch { /* best effort */ }
}
function trackResponse(toolName: string, response: ToolResult): ToolResult {
// #854: a response is activity too — refresh the bridge-child idle clock so a
// chatty/streaming call keeps its server alive even between inbound frames.
noteMcpActivity();
// Mid-session cache heal — one-shot, first tool call
healCacheMidSession();
// Prepend version outdated warning if needed
if (shouldShowVersionWarning() && response.content.length > 0) {
const hint = getUpgradeHint();
response.content[0].text =
`⚠️ context-mode v${VERSION} outdated → v${_latestVersion} available. Upgrade: ${hint}\n\n` +
response.content[0].text;
}
const bytes = response.content.reduce(
(sum, c) => sum + Buffer.byteLength(c.text),
0,
);
sessionStats.calls[toolName] = (sessionStats.calls[toolName] || 0) + 1;
sessionStats.bytesReturned[toolName] =
(sessionStats.bytesReturned[toolName] || 0) + bytes;
// Persist a sidecar JSON snapshot for the statusline — read at ~3-5 Hz by
// bin/statusline.mjs (and any external dashboard) so they don't have to
// open the SQLite database. Throttled inside persistStats() (500ms) so
// it's safe to call on every response.
persistStats();
// Persist to SessionDB so counters survive process restart, --continue,
// upgrade. Re-introduces the write path 4742160 added and b392c2f dropped.
// setImmediate keeps this off the response hot path; the helper itself
// is best-effort (never throws).
setImmediate(() => persistToolCallCounter(getSessionDbPath(), toolName, bytes));
// D2 Phase 5/7 — sandbox-execute event emission. Tracks the bytes the
// user actually saw from sandboxed runs so getRealBytesStats() can
// replace the conservative `events × 256` estimate. Best-effort and
// off the hot path, same shape as persistToolCallCounter above.
if (
toolName === "ctx_execute"
|| toolName === "ctx_execute_file"
|| toolName === "ctx_batch_execute"
) {
setImmediate(() =>
emitSandboxExecuteEvent({
sessionDbPath: getSessionDbPath(),
toolName,
bytesReturned: bytes,
})
);
}
// Retrieval ("With context-mode") bridge — ctx_search / ctx_fetch_and_index
// response bytes are the kept-out content the model paid to access. The
// PostToolUse hook never fires for the plugin's OWN MCP tools, so the
// hook-side extractMcpToolCall can never see these calls (bytes_retrieved
// was 0/124454 in prod). Drop the count into a marker keyed by the session
// DB; the next ordinary-tool PostToolUse consumes it and emits a forwardable
// bytes_retrieved event. Off the hot path; never throws.
if (toolName === "ctx_search" || toolName === "ctx_fetch_and_index") {
setImmediate(() => appendRetrievalBytes(getSessionDbPath(), bytes));
}
return response;
}
function trackIndexed(bytes: number, source: string = "unknown"): void {
sessionStats.bytesIndexed += bytes;
persistStats();
// D2 Phase 5/7 — index-write event emission. `bytes_avoided` because
// these are bytes that would have flooded context if the user had
// Read'd the source instead of indexing.
if (bytes > 0) {
setImmediate(() =>
emitIndexWriteEvent({
sessionDbPath: getSessionDbPath(),
source,
bytesAvoided: bytes,
})
);
}
}
// ─────────────────────────────────────────────────────────
// Stats persistence — written after every tool call so
// external readers (status line scripts, dashboards, hooks)
// can see real-time savings without spawning an MCP client.
// ─────────────────────────────────────────────────────────
const STATS_PERSIST_THROTTLE_MS = 500;
// Schema version for the persisted stats payload (~/.claude/context-mode/sessions/stats-*.json).
// Bump when a field is added/renamed/removed. Statusline reads `schemaVersion ?? 0` and warns when
// it sees a future schema, so legacy bundles degrade gracefully on upgrade rather than silently
// rendering missing fields (PR #401 architect review P1.3).
// v2: added tokens_saved_lifetime + dollars_saved_lifetime.
const STATS_SCHEMA_VERSION = 2;
// pricePerToken() intentionally NOT defined here — single source in
// src/session/analytics.ts re-exported above. (P1.1 — pricing constant dedup,
// PR #401 architect + ops 2-vote convergence.)
const LIFETIME_REFRESH_MS = 30_000;
// Matches the conversion factor in src/session/analytics.ts renderBottomLine:
// ~1KB per session event ÷ 4 bytes/token = 256 tokens/event.
const TOKENS_PER_EVENT = 256;
let _lastStatsPersist = 0;
let _lifetimeCache: { tokens: number; computedAt: number } | undefined;
/**
* Resolve the per-session stats file path.
*
* The session id mirrors the Claude Code adapter contract
* (`pid-<parent pid>`), so a status line script can derive
* the same id from `$PPID` without coupling to MCP.
*/
// CLAUDE_SESSION_ID flows from the hosting process (Claude Code, pi, etc.)
// straight into a path.join, and path.join collapses ".." into the result,
// so a host env CLAUDE_SESSION_ID=../../evil writes "stats-evil.json" two