context-mode 1.0.19 → 1.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,14 +6,14 @@
6
6
  },
7
7
  "metadata": {
8
8
  "description": "Claude Code plugins by Mert Koseoğlu",
9
- "version": "1.0.19"
9
+ "version": "1.0.21"
10
10
  },
11
11
  "plugins": [
12
12
  {
13
13
  "name": "context-mode",
14
14
  "source": "./",
15
15
  "description": "Claude Code MCP plugin that saves 98% of your context window. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and intent-driven search.",
16
- "version": "1.0.19",
16
+ "version": "1.0.21",
17
17
  "author": {
18
18
  "name": "Mert Koseoğlu"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "description": "MCP server that saves 98% of your context window with session continuity. Sandboxed code execution in 11 languages, FTS5 knowledge base with BM25 ranking, and automatic state restore across compactions.",
5
5
  "author": {
6
6
  "name": "Mert Koseoğlu",
package/build/server.js CHANGED
@@ -14,7 +14,7 @@ import { readBashPolicies, evaluateCommandDenyOnly, extractShellCommands, readTo
14
14
  import { detectRuntimes, getRuntimeSummary, getAvailableLanguages, hasBunRuntime, } from "./runtime.js";
15
15
  import { classifyNonZeroExit } from "./exit-classify.js";
16
16
  import { startLifecycleGuard } from "./lifecycle.js";
17
- const VERSION = "1.0.19";
17
+ const VERSION = "1.0.21";
18
18
  // Prevent silent server death from unhandled async errors
19
19
  process.on("unhandledRejection", (err) => {
20
20
  process.stderr.write(`[context-mode] unhandledRejection: ${err}\n`);
package/cli.bundle.mjs CHANGED
@@ -293,7 +293,7 @@ async function main() {
293
293
  main();
294
294
  `}async function sN(){let t=S0();t>0&&console.error(`Cleaned up ${t} stale DB file(s) from previous sessions`);let e=()=>{Ds.cleanupBackgrounded(),mo&&mo.cleanup()},r=async()=>{e(),process.exit(0)};process.on("exit",e),process.on("SIGINT",()=>{r()}),process.on("SIGTERM",()=>{r()}),N0({onShutdown:()=>r()});let n=new lc;await Qt.connect(n);try{let{detectPlatform:o,getAdapter:s}=await Promise.resolve().then(()=>(eu(),jh)),i=o(),a=await s(i.platform);if(!a.capabilities.sessionStart){let c=Bz(Ms(Am(import.meta.url)),".."),u=process.env.CLAUDE_PROJECT_DIR??process.env.CODEX_HOME??process.cwd(),l=a.writeRoutingInstructions(u,c);l&&console.error(`Wrote routing instructions: ${l}`)}}catch{}console.error(`Context Mode MCP server v${L0} running on stdio`),console.error(`Detected runtimes:
295
295
  ${Js(jm)}`),_o()||(console.error(`
296
- Performance tip: Install Bun for 3-5x faster JS/TS execution`),console.error(" curl -fsSL https://bun.sh/install | bash"))}var L0,jm,Kz,Qt,Ds,mo,Ye,Yz,Xz,Qz,eN,mc,$n,Om,tN,j0,M0,Cm,zm,H0=b(()=>{"use strict";o0();c0();$m();wm();k0();C0();Xs();z0();A0();L0="1.0.18";process.on("unhandledRejection",t=>{process.stderr.write(`[context-mode] unhandledRejection: ${t}
296
+ Performance tip: Install Bun for 3-5x faster JS/TS execution`),console.error(" curl -fsSL https://bun.sh/install | bash"))}var L0,jm,Kz,Qt,Ds,mo,Ye,Yz,Xz,Qz,eN,mc,$n,Om,tN,j0,M0,Cm,zm,H0=b(()=>{"use strict";o0();c0();$m();wm();k0();C0();Xs();z0();A0();L0="1.0.20";process.on("unhandledRejection",t=>{process.stderr.write(`[context-mode] unhandledRejection: ${t}
297
297
  `)});process.on("uncaughtException",t=>{process.stderr.write(`[context-mode] uncaughtException: ${t?.message??t}
298
298
  `)});jm=wn(),Kz=Ys(jm),Qt=new cc({name:"context-mode",version:L0}),Ds=new js({runtimes:jm,projectRoot:process.env.CLAUDE_PROJECT_DIR}),mo=null;Ye={calls:{},bytesReturned:{},bytesIndexed:0,bytesSandboxed:0,sessionStart:Date.now()};Yz=Kz.join(", "),Xz=_o()?" (Bun detected \u2014 JS/TS runs 3-5x faster)":"",Qz="",eN="";Qt.registerTool("ctx_execute",{title:"Execute Code",description:`MANDATORY: Use for any command where output exceeds 20 lines. Execute code in a sandboxed subprocess. Only stdout enters context \u2014 raw data stays in the subprocess.${Xz} Available: ${Yz}.
299
299
 
@@ -9,7 +9,7 @@ export const formatters = {
9
9
  hookSpecificOutput: {
10
10
  hookEventName: "PreToolUse",
11
11
  permissionDecision: "deny",
12
- reason,
12
+ permissionDecisionReason: reason,
13
13
  },
14
14
  }),
15
15
  ask: () => ({
@@ -21,6 +21,8 @@ export const formatters = {
21
21
  modify: (updatedInput) => ({
22
22
  hookSpecificOutput: {
23
23
  hookEventName: "PreToolUse",
24
+ permissionDecision: "allow",
25
+ permissionDecisionReason: "Routed to context-mode sandbox",
24
26
  updatedInput,
25
27
  },
26
28
  }),
@@ -46,7 +48,7 @@ export const formatters = {
46
48
  "vscode-copilot": {
47
49
  deny: (reason) => ({
48
50
  permissionDecision: "deny",
49
- reason,
51
+ permissionDecisionReason: reason,
50
52
  }),
51
53
  ask: () => ({
52
54
  permissionDecision: "ask",
@@ -54,6 +56,8 @@ export const formatters = {
54
56
  modify: (updatedInput) => ({
55
57
  hookSpecificOutput: {
56
58
  hookEventName: "PreToolUse",
59
+ permissionDecision: "allow",
60
+ permissionDecisionReason: "Routed to context-mode sandbox",
57
61
  updatedInput,
58
62
  },
59
63
  }),
@@ -11,6 +11,46 @@
11
11
  */
12
12
 
13
13
  import { ROUTING_BLOCK, READ_GUIDANCE, GREP_GUIDANCE, BASH_GUIDANCE } from "../routing-block.mjs";
14
+ import { existsSync, mkdirSync, rmSync, openSync, closeSync, constants as fsConstants } from "node:fs";
15
+ import { tmpdir } from "node:os";
16
+ import { resolve } from "node:path";
17
+
18
+ // Guidance throttle: show each advisory type at most once per session.
19
+ // Hybrid approach:
20
+ // - In-memory Set for same-process (OpenCode ts-plugin, vitest)
21
+ // - File-based markers with O_EXCL for cross-process atomicity
22
+ // (Claude Code, Gemini, Cursor, VS Code Copilot)
23
+ // Session scoped via process.ppid (= host PID, constant for session lifetime).
24
+ const _guidanceShown = new Set();
25
+ const _guidanceDir = resolve(tmpdir(), `context-mode-guidance-${process.ppid}`);
26
+
27
+ function guidanceOnce(type, content) {
28
+ // Fast path: in-memory (same process)
29
+ if (_guidanceShown.has(type)) return null;
30
+
31
+ // Ensure marker directory exists
32
+ try { mkdirSync(_guidanceDir, { recursive: true }); } catch {}
33
+
34
+ // Atomic create-or-fail: O_CREAT | O_EXCL | O_WRONLY
35
+ // First process to create the file wins; others get EEXIST.
36
+ const marker = resolve(_guidanceDir, type);
37
+ try {
38
+ const fd = openSync(marker, fsConstants.O_CREAT | fsConstants.O_EXCL | fsConstants.O_WRONLY);
39
+ closeSync(fd);
40
+ } catch {
41
+ // EEXIST = another process already created it, or we did in-memory
42
+ _guidanceShown.add(type);
43
+ return null;
44
+ }
45
+
46
+ _guidanceShown.add(type);
47
+ return { action: "context", additionalContext: content };
48
+ }
49
+
50
+ export function resetGuidanceThrottle() {
51
+ _guidanceShown.clear();
52
+ try { rmSync(_guidanceDir, { recursive: true, force: true }); } catch {}
53
+ }
14
54
 
15
55
  /**
16
56
  * Strip heredoc content from a shell command.
@@ -157,18 +197,18 @@ export function routePreToolUse(toolName, toolInput, projectDir) {
157
197
  };
158
198
  }
159
199
 
160
- // allow all other Bash commands, but inject routing nudge
161
- return { action: "context", additionalContext: BASH_GUIDANCE };
200
+ // allow all other Bash commands, but inject routing nudge (once per session)
201
+ return guidanceOnce("bash", BASH_GUIDANCE);
162
202
  }
163
203
 
164
- // ─── Read: nudge toward execute_file ───
204
+ // ─── Read: nudge toward execute_file (once per session) ───
165
205
  if (canonical === "Read") {
166
- return { action: "context", additionalContext: READ_GUIDANCE };
206
+ return guidanceOnce("read", READ_GUIDANCE);
167
207
  }
168
208
 
169
- // ─── Grep: nudge toward execute ───
209
+ // ─── Grep: nudge toward execute (once per session) ───
170
210
  if (canonical === "Grep") {
171
- return { action: "context", additionalContext: GREP_GUIDANCE };
211
+ return guidanceOnce("grep", GREP_GUIDANCE);
172
212
  }
173
213
 
174
214
  // ─── WebFetch: deny + redirect to sandbox ───
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "context-mode",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "type": "module",
5
5
  "description": "MCP plugin that saves 98% of your context window. Works with Claude Code, Gemini CLI, VS Code Copilot, OpenCode, and Codex CLI. Sandboxed code execution, FTS5 knowledge base, and intent-driven search.",
6
6
  "author": "Mert Koseoğlu",
package/server.bundle.mjs CHANGED
@@ -238,7 +238,7 @@ stdout:
238
238
  ${n}
239
239
 
240
240
  stderr:
241
- ${o}`}}var rO=process.ppid;function nO(){let t=process.ppid;return!(t!==rO||t===0||t===1)}function Hy(t){let e=t.checkIntervalMs??3e4,r=t.isParentAlive??nO,n=!1,o=()=>{n||(n=!0,t.onShutdown())},s=setInterval(()=>{r()||o()},e);s.unref();let i=()=>o();process.stdin.resume(),process.stdin.on("end",i),process.stdin.on("close",i),process.stdin.on("error",i);let a=["SIGTERM","SIGINT"];process.platform!=="win32"&&a.push("SIGHUP");for(let c of a)process.on(c,o);return()=>{n=!0,clearInterval(s),process.stdin.removeListener("end",i),process.stdin.removeListener("close",i),process.stdin.removeListener("error",i);for(let c of a)process.removeListener(c,o)}}var Cv="1.0.18";process.on("unhandledRejection",t=>{process.stderr.write(`[context-mode] unhandledRejection: ${t}
241
+ ${o}`}}var rO=process.ppid;function nO(){let t=process.ppid;return!(t!==rO||t===0||t===1)}function Hy(t){let e=t.checkIntervalMs??3e4,r=t.isParentAlive??nO,n=!1,o=()=>{n||(n=!0,t.onShutdown())},s=setInterval(()=>{r()||o()},e);s.unref();let i=()=>o();process.stdin.resume(),process.stdin.on("end",i),process.stdin.on("close",i),process.stdin.on("error",i);let a=["SIGTERM","SIGINT"];process.platform!=="win32"&&a.push("SIGHUP");for(let c of a)process.on(c,o);return()=>{n=!0,clearInterval(s),process.stdin.removeListener("end",i),process.stdin.removeListener("close",i),process.stdin.removeListener("error",i);for(let c of a)process.removeListener(c,o)}}var Cv="1.0.20";process.on("unhandledRejection",t=>{process.stderr.write(`[context-mode] unhandledRejection: ${t}
242
242
  `)});process.on("uncaughtException",t=>{process.stderr.write(`[context-mode] uncaughtException: ${t?.message??t}
243
243
  `)});var $p=Yi(),DO=ky($p),Mt=new Gi({name:"context-mode",version:Cv}),ss=new Qi({runtimes:$p,projectRoot:process.env.CLAUDE_PROJECT_DIR}),Hn=null;function ZO(t){try{let e=da(Iv(),".claude","context-mode","sessions");if(!Ov(e))return;let r=zO(e).filter(n=>n.endsWith("-events.md"));for(let n of r){let o=da(e,n);try{t.index({path:o,source:"session-events"}),CO(o)}catch{}}}catch{}}function is(){return Hn||(Hn=new ea),ZO(Hn),Hn}var qe={calls:{},bytesReturned:{},bytesIndexed:0,bytesSandboxed:0,sessionStart:Date.now()};function G(t,e){let r=e.content.reduce((n,o)=>n+Buffer.byteLength(o.text),0);return qe.calls[t]=(qe.calls[t]||0)+1,qe.bytesReturned[t]=(qe.bytesReturned[t]||0)+r,e}function yr(t){qe.bytesIndexed+=t}function Tp(t,e){try{let r=Xd(process.env.CLAUDE_PROJECT_DIR),n=Qd(t,r);if(n.decision==="deny")return G(e,{content:[{type:"text",text:`Command blocked by security policy: matches deny pattern ${n.matchedPattern}`}],isError:!0})}catch{}return null}function zv(t,e,r){try{let n=qy(t,e);if(n.length===0)return null;let o=Xd(process.env.CLAUDE_PROJECT_DIR);for(let s of n){let i=Qd(s,o);if(i.decision==="deny")return G(r,{content:[{type:"text",text:`Command blocked by security policy: embedded shell command "${s}" matches deny pattern ${i.matchedPattern}`}],isError:!0})}}catch{}return null}function LO(t,e){try{let r=Ly("Read",process.env.CLAUDE_PROJECT_DIR),n=Uy(t,r);if(n.denied)return G(e,{content:[{type:"text",text:`File access blocked by security policy: path matches Read deny pattern ${n.matchedPattern}`}],isError:!0})}catch{}return null}var UO=DO.join(", "),qO=Gd()?" (Bun detected \u2014 JS/TS runs 3-5x faster)":"",HO="",FO="";function VO(t){let e=[],r=0,n=0;for(;n<t.length;)if(t[n]===HO){for(e.push(r),n++;n<t.length&&t[n]!==FO;)r++,n++;n<t.length&&n++}else r++,n++;return e}function Nv(t,e,r=1500,n){if(t.length<=r)return t;let o=[];if(n)for(let u of VO(n))o.push(u);if(o.length===0){let u=e.toLowerCase().split(/\s+/).filter(d=>d.length>2),l=t.toLowerCase();for(let d of u){let f=l.indexOf(d);for(;f!==-1;)o.push(f),f=l.indexOf(d,f+1)}}if(o.length===0)return t.slice(0,r)+`
244
244
  \u2026`;o.sort((u,l)=>u-l);let s=300,i=[];for(let u of o){let l=Math.max(0,u-s),d=Math.min(t.length,u+s);i.length>0&&l<=i[i.length-1][1]?i[i.length-1][1]=d:i.push([l,d])}let a=[],c=0;for(let[u,l]of i){if(c>=r)break;let d=t.slice(u,Math.min(l,u+(r-c)));a.push((u>0?"\u2026":"")+d+(l<t.length?"\u2026":"")),c+=d.length}return a.join(`
@@ -1,48 +0,0 @@
1
- /**
2
- * ctx_delegate — Distributed analysis via `claude --print` sub-agents.
3
- *
4
- * Spawns parallel Claude CLI subprocesses with pre-read file contents
5
- * embedded in prompts. Each sub-agent performs single-turn analysis and
6
- * returns a compressed summary. Results are indexed into FTS5 for follow-up search.
7
- *
8
- * Zero dependencies — uses `claude --print` (installed with Claude Code).
9
- */
10
- export interface DelegateTask {
11
- /** Human-readable label for this sub-agent's work */
12
- name: string;
13
- /** What the sub-agent should do — the analysis prompt */
14
- prompt: string;
15
- /** File paths to pre-read and embed. Directories are read recursively (.ts files). */
16
- files?: string[];
17
- }
18
- export interface DelegateOptions {
19
- tasks: DelegateTask[];
20
- /** Model to use for sub-agents. Default: claude-sonnet-4-6 */
21
- model?: string;
22
- /** Per-task timeout in ms. Default: 90_000 (90s) */
23
- timeout?: number;
24
- /** Max concurrent sub-agents. Default: CPU count, max: 10 */
25
- concurrency?: number;
26
- }
27
- export interface DelegateTaskResult {
28
- name: string;
29
- summary: string;
30
- durationMs: number;
31
- inputTokens: number;
32
- outputTokens: number;
33
- cacheReadTokens: number;
34
- promptChars: number;
35
- fileCount: number;
36
- missingPaths: string[];
37
- error?: string;
38
- }
39
- export interface DelegateResult {
40
- results: DelegateTaskResult[];
41
- wallTimeMs: number;
42
- sequentialTimeMs: number;
43
- speedup: number;
44
- totalPromptTokens: number;
45
- totalSummaryTokens: number;
46
- compressionPct: number;
47
- }
48
- export declare function delegate(opts: DelegateOptions, projectRoot: string): Promise<DelegateResult>;
package/build/delegate.js DELETED
@@ -1,265 +0,0 @@
1
- /**
2
- * ctx_delegate — Distributed analysis via `claude --print` sub-agents.
3
- *
4
- * Spawns parallel Claude CLI subprocesses with pre-read file contents
5
- * embedded in prompts. Each sub-agent performs single-turn analysis and
6
- * returns a compressed summary. Results are indexed into FTS5 for follow-up search.
7
- *
8
- * Zero dependencies — uses `claude --print` (installed with Claude Code).
9
- */
10
- import { readFileSync, readdirSync, statSync } from "node:fs";
11
- import { join, relative } from "node:path";
12
- import { cpus } from "node:os";
13
- import { spawn } from "node:child_process";
14
- // ── File Reading ───────────────────────────────────────────────────────
15
- const CODE_EXTENSIONS = new Set([
16
- ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs",
17
- ".py", ".rb", ".go", ".rs", ".java", ".c", ".cpp", ".h",
18
- ".php", ".pl", ".r", ".ex", ".exs", ".sh", ".bash", ".zsh",
19
- ".css", ".scss", ".html", ".json", ".yaml", ".yml", ".toml",
20
- ".md", ".txt", ".sql", ".graphql", ".proto",
21
- ]);
22
- function isCodeFile(path) {
23
- const dot = path.lastIndexOf(".");
24
- if (dot === -1)
25
- return false;
26
- return CODE_EXTENSIONS.has(path.slice(dot).toLowerCase());
27
- }
28
- /**
29
- * Read a file or directory, returning file contents keyed by relative path.
30
- * Directories are read recursively for code files.
31
- */
32
- function readFilesForTask(paths, projectRoot) {
33
- const files = new Map();
34
- const missingPaths = [];
35
- for (const p of paths) {
36
- const abs = p.startsWith("/") ? p : join(projectRoot, p);
37
- try {
38
- const stat = statSync(abs);
39
- if (stat.isDirectory()) {
40
- // Recursive directory read
41
- const entries = readdirSync(abs, { recursive: true });
42
- for (const entry of entries) {
43
- const entryAbs = join(abs, entry);
44
- try {
45
- if (!statSync(entryAbs).isFile())
46
- continue;
47
- if (!isCodeFile(entry))
48
- continue;
49
- const rel = relative(projectRoot, entryAbs);
50
- files.set(rel, readFileSync(entryAbs, "utf-8"));
51
- }
52
- catch { /* skip unreadable files */ }
53
- }
54
- }
55
- else if (stat.isFile()) {
56
- const rel = relative(projectRoot, abs);
57
- files.set(rel, readFileSync(abs, "utf-8"));
58
- }
59
- }
60
- catch {
61
- missingPaths.push(p);
62
- }
63
- }
64
- let content = "";
65
- let totalChars = 0;
66
- for (const [path, text] of files) {
67
- content += `\n--- ${path} ---\n${text}\n`;
68
- totalChars += text.length;
69
- }
70
- return { content, fileCount: files.size, totalChars, missingPaths };
71
- }
72
- // ── Sub-Agent Runner ───────────────────────────────────────────────────
73
- async function runSubAgent(task, projectRoot, model, timeout) {
74
- // Pre-read files and embed in prompt
75
- let fileContent = "";
76
- let fileCount = 0;
77
- let missingPaths = [];
78
- if (task.files && task.files.length > 0) {
79
- const read = readFilesForTask(task.files, projectRoot);
80
- fileContent = read.content;
81
- fileCount = read.fileCount;
82
- missingPaths = read.missingPaths;
83
- // Fail fast: if files were requested but NONE found, don't spawn sub-agent
84
- if (fileCount === 0 && missingPaths.length > 0) {
85
- return {
86
- name: task.name,
87
- summary: `ERROR: No files found. Missing paths: ${missingPaths.join(", ")}`,
88
- durationMs: 0,
89
- inputTokens: 0,
90
- outputTokens: 0,
91
- cacheReadTokens: 0,
92
- promptChars: 0,
93
- fileCount: 0,
94
- missingPaths,
95
- error: "FILES_NOT_FOUND",
96
- };
97
- }
98
- }
99
- const fullPrompt = fileContent
100
- ? `${task.prompt}\n\n${fileContent}`
101
- : task.prompt;
102
- // Strip CLAUDECODE env to prevent recursion detection
103
- const cleanEnv = { ...process.env };
104
- delete cleanEnv.CLAUDECODE;
105
- // Propagate depth guard
106
- const currentDepth = parseInt(process.env.CTX_DELEGATE_DEPTH ?? "0", 10);
107
- cleanEnv.CTX_DELEGATE_DEPTH = String(currentDepth + 1);
108
- const startTime = Date.now();
109
- // Spawn `claude --print` — single-turn, no tools, inherits OAuth session
110
- const args = ["--print", "--model", model, "--max-turns", "1", fullPrompt];
111
- return new Promise((resolve) => {
112
- const child = spawn("claude", args, {
113
- env: cleanEnv,
114
- cwd: projectRoot,
115
- stdio: ["ignore", "pipe", "pipe"],
116
- });
117
- let stdout = "";
118
- let stderr = "";
119
- child.stdout.on("data", (chunk) => { stdout += chunk.toString(); });
120
- child.stderr.on("data", (chunk) => { stderr += chunk.toString(); });
121
- // Timeout guard
122
- const timer = setTimeout(() => {
123
- child.kill("SIGTERM");
124
- setTimeout(() => { if (!child.killed)
125
- child.kill("SIGKILL"); }, 5000);
126
- }, timeout);
127
- child.on("close", (code) => {
128
- clearTimeout(timer);
129
- const durationMs = Date.now() - startTime;
130
- const summary = stdout.trim();
131
- if (code !== 0 && !summary) {
132
- resolve({
133
- name: task.name,
134
- summary: `ERROR: claude --print exited with code ${code}. ${stderr.trim().slice(0, 200)}`,
135
- durationMs,
136
- inputTokens: 0,
137
- outputTokens: 0,
138
- cacheReadTokens: 0,
139
- promptChars: fullPrompt.length,
140
- fileCount,
141
- missingPaths,
142
- error: `EXIT_CODE_${code}`,
143
- });
144
- return;
145
- }
146
- resolve({
147
- name: task.name,
148
- summary: summary || "ERROR: Empty response from claude --print",
149
- durationMs,
150
- inputTokens: 0,
151
- outputTokens: 0,
152
- cacheReadTokens: 0,
153
- promptChars: fullPrompt.length,
154
- fileCount,
155
- missingPaths,
156
- error: summary ? undefined : "EMPTY_RESPONSE",
157
- });
158
- });
159
- child.on("error", (err) => {
160
- clearTimeout(timer);
161
- resolve({
162
- name: task.name,
163
- summary: `ERROR: Failed to spawn claude CLI: ${err.message}. Is Claude Code installed?`,
164
- durationMs: Date.now() - startTime,
165
- inputTokens: 0,
166
- outputTokens: 0,
167
- cacheReadTokens: 0,
168
- promptChars: fullPrompt.length,
169
- fileCount: 0,
170
- missingPaths,
171
- error: "SPAWN_FAILED",
172
- });
173
- });
174
- });
175
- }
176
- // ── Concurrency Limiter ────────────────────────────────────────────────
177
- async function runWithConcurrency(tasks, projectRoot, model, timeout, limit) {
178
- const results = [];
179
- const queue = [...tasks];
180
- const running = new Set();
181
- while (queue.length > 0 || running.size > 0) {
182
- while (running.size < limit && queue.length > 0) {
183
- const task = queue.shift();
184
- const p = runSubAgent(task, projectRoot, model, timeout).then((r) => {
185
- results.push(r);
186
- running.delete(p);
187
- });
188
- running.add(p);
189
- }
190
- if (running.size > 0)
191
- await Promise.race(running);
192
- }
193
- return results;
194
- }
195
- // ── Public API ─────────────────────────────────────────────────────────
196
- const MAX_CONCURRENCY = 10;
197
- const MAX_TASKS = 20;
198
- const MAX_DEPTH = 1;
199
- const DEFAULT_TIMEOUT = 90_000;
200
- const DEFAULT_MODEL = "claude-sonnet-4-6";
201
- export async function delegate(opts, projectRoot) {
202
- // Depth guard — prevent infinite delegation loops
203
- const depth = parseInt(process.env.CTX_DELEGATE_DEPTH ?? "0", 10);
204
- if (depth >= MAX_DEPTH) {
205
- return {
206
- results: [{
207
- name: "depth-guard",
208
- summary: "ERROR: Delegation depth limit reached. Sub-agents cannot delegate further.",
209
- durationMs: 0,
210
- inputTokens: 0,
211
- outputTokens: 0,
212
- cacheReadTokens: 0,
213
- promptChars: 0,
214
- fileCount: 0,
215
- missingPaths: [],
216
- error: "DEPTH_LIMIT",
217
- }],
218
- wallTimeMs: 0,
219
- sequentialTimeMs: 0,
220
- speedup: 0,
221
- totalPromptTokens: 0,
222
- totalSummaryTokens: 0,
223
- compressionPct: 0,
224
- };
225
- }
226
- // Validate tasks
227
- const tasks = opts.tasks.slice(0, MAX_TASKS);
228
- if (tasks.length === 0) {
229
- return {
230
- results: [],
231
- wallTimeMs: 0,
232
- sequentialTimeMs: 0,
233
- speedup: 0,
234
- totalPromptTokens: 0,
235
- totalSummaryTokens: 0,
236
- compressionPct: 0,
237
- };
238
- }
239
- const model = opts.model ?? DEFAULT_MODEL;
240
- const timeout = Math.min(opts.timeout ?? DEFAULT_TIMEOUT, 300_000);
241
- const concurrency = Math.min(opts.concurrency ?? cpus().length, MAX_CONCURRENCY);
242
- const wallStart = Date.now();
243
- const results = await runWithConcurrency(tasks, projectRoot, model, timeout, concurrency);
244
- const wallTimeMs = Date.now() - wallStart;
245
- // Reorder results to match input task order
246
- const ordered = tasks.map((t) => results.find((r) => r.name === t.name) ?? results[0]);
247
- // Compute metrics
248
- const sequentialTimeMs = ordered.reduce((s, r) => s + r.durationMs, 0);
249
- const totalPromptChars = ordered.reduce((s, r) => s + r.promptChars, 0);
250
- const totalSummaryChars = ordered.reduce((s, r) => s + r.summary.length, 0);
251
- const totalPromptTokens = Math.ceil(totalPromptChars / 4);
252
- const totalSummaryTokens = Math.ceil(totalSummaryChars / 4);
253
- const compressionPct = totalPromptTokens > 0
254
- ? (1 - totalSummaryTokens / totalPromptTokens) * 100
255
- : 0;
256
- return {
257
- results: ordered,
258
- wallTimeMs,
259
- sequentialTimeMs,
260
- speedup: wallTimeMs > 0 ? sequentialTimeMs / wallTimeMs : 0,
261
- totalPromptTokens,
262
- totalSummaryTokens,
263
- compressionPct,
264
- };
265
- }
@@ -1,23 +0,0 @@
1
- import type { SanitizedEvent } from "./types.js";
2
- interface BatcherOpts {
3
- batchSize: number;
4
- flushIntervalMs: number;
5
- maxBufferSize: number;
6
- onFlush: (events: SanitizedEvent[]) => Promise<void>;
7
- }
8
- export declare class EventBatcher {
9
- private opts;
10
- private buffer;
11
- private timer;
12
- private inflightPromise;
13
- private consecutiveFailures;
14
- constructor(opts: BatcherOpts);
15
- private startTimer;
16
- push(event: SanitizedEvent): void;
17
- flush(): Promise<void>;
18
- resetCircuitBreaker(): void;
19
- get bufferSize(): number;
20
- get isCircuitOpen(): boolean;
21
- destroy(): void;
22
- }
23
- export {};
@@ -1,74 +0,0 @@
1
- const MAX_CONSECUTIVE_FAILURES = 5;
2
- export class EventBatcher {
3
- opts;
4
- buffer = [];
5
- timer = null;
6
- inflightPromise = null;
7
- consecutiveFailures = 0;
8
- constructor(opts) {
9
- this.opts = opts;
10
- this.startTimer();
11
- }
12
- startTimer() {
13
- this.timer = setInterval(() => {
14
- this.flush();
15
- }, this.opts.flushIntervalMs);
16
- // Don't keep process alive for sync
17
- if (this.timer &&
18
- typeof this.timer === "object" &&
19
- "unref" in this.timer) {
20
- this.timer.unref();
21
- }
22
- }
23
- push(event) {
24
- this.buffer.push(event);
25
- // FIFO eviction
26
- if (this.buffer.length > this.opts.maxBufferSize) {
27
- this.buffer = this.buffer.slice(this.buffer.length - this.opts.maxBufferSize);
28
- }
29
- // Flush if batch size reached
30
- if (this.buffer.length >= this.opts.batchSize) {
31
- this.flush();
32
- }
33
- }
34
- async flush() {
35
- if (this.buffer.length === 0)
36
- return;
37
- if (this.inflightPromise)
38
- return; // Don't overlap
39
- // Circuit breaker
40
- if (this.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES) {
41
- return;
42
- }
43
- const batch = this.buffer.splice(0, this.opts.batchSize);
44
- this.inflightPromise = this.opts
45
- .onFlush(batch)
46
- .then(() => {
47
- this.consecutiveFailures = 0;
48
- })
49
- .catch(() => {
50
- this.consecutiveFailures++;
51
- // Put events back at front
52
- this.buffer.unshift(...batch);
53
- })
54
- .finally(() => {
55
- this.inflightPromise = null;
56
- });
57
- return this.inflightPromise;
58
- }
59
- resetCircuitBreaker() {
60
- this.consecutiveFailures = 0;
61
- }
62
- get bufferSize() {
63
- return this.buffer.length;
64
- }
65
- get isCircuitOpen() {
66
- return this.consecutiveFailures >= MAX_CONSECUTIVE_FAILURES;
67
- }
68
- destroy() {
69
- if (this.timer) {
70
- clearInterval(this.timer);
71
- this.timer = null;
72
- }
73
- }
74
- }
@@ -1,12 +0,0 @@
1
- /**
2
- * Fire-and-forget: sanitize + POST a single event to cloud.
3
- * Never throws — all errors are silently swallowed.
4
- */
5
- export declare function cloudPostEvent(event: {
6
- type: string;
7
- category: string;
8
- priority: number;
9
- data: string;
10
- source_hook: string;
11
- created_at: string;
12
- }, projectDir: string, sessionId: string, gitRemote?: string): void;
@@ -1,38 +0,0 @@
1
- import { loadSyncConfig } from "./config.js";
2
- import { sanitizeEvent, hashSessionId } from "./sanitizer.js";
3
- let _configCache;
4
- function getConfig() {
5
- if (_configCache === undefined) {
6
- _configCache = loadSyncConfig();
7
- }
8
- return _configCache;
9
- }
10
- /**
11
- * Fire-and-forget: sanitize + POST a single event to cloud.
12
- * Never throws — all errors are silently swallowed.
13
- */
14
- export function cloudPostEvent(event, projectDir, sessionId, gitRemote) {
15
- const config = getConfig();
16
- if (!config || !config.enabled || !config.api_token || !config.api_url)
17
- return;
18
- const sanitized = sanitizeEvent(event, projectDir, sessionId);
19
- if (!sanitized)
20
- return; // Blocked event
21
- const hashedSession = hashSessionId(sessionId);
22
- const url = `${config.api_url}/ingest/sessions/${hashedSession}/events`;
23
- // Fire-and-forget — never block the caller
24
- fetch(url, {
25
- method: "POST",
26
- headers: {
27
- "Content-Type": "application/json",
28
- Authorization: `Bearer ${config.api_token}`,
29
- },
30
- body: JSON.stringify({
31
- events: [sanitized],
32
- project_git_remote: gitRemote,
33
- }),
34
- signal: AbortSignal.timeout(5000),
35
- }).catch(() => {
36
- // Silently swallow — cloud sync must never break the plugin
37
- });
38
- }
@@ -1,4 +0,0 @@
1
- import type { SyncConfig } from "./types.js";
2
- export declare function loadSyncConfig(): SyncConfig | null;
3
- export declare function isSyncEnabled(): boolean;
4
- export declare function saveSyncConfig(config: Partial<SyncConfig>): void;
@@ -1,64 +0,0 @@
1
- import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
2
- import { join, dirname } from "node:path";
3
- import { homedir } from "node:os";
4
- const CONFIG_DIR = join(homedir(), ".context-mode");
5
- const CONFIG_PATH = join(CONFIG_DIR, "sync.json");
6
- const DEFAULT_API_URL = "https://api.context-mode.com";
7
- const DEFAULT_CONFIG = {
8
- enabled: false,
9
- api_url: DEFAULT_API_URL,
10
- api_token: "",
11
- organization_id: "",
12
- batch_size: 50,
13
- flush_interval_ms: 30000,
14
- };
15
- export function loadSyncConfig() {
16
- // Env var overrides
17
- const envToken = process.env.CONTEXT_MODE_API_TOKEN;
18
- const envUrl = process.env.CONTEXT_MODE_API_URL;
19
- const envOrgId = process.env.CONTEXT_MODE_ORG_ID;
20
- let fileConfig = {};
21
- let fileLoaded = false;
22
- try {
23
- if (existsSync(CONFIG_PATH)) {
24
- const raw = readFileSync(CONFIG_PATH, "utf-8");
25
- fileConfig = JSON.parse(raw);
26
- fileLoaded = true;
27
- }
28
- }
29
- catch {
30
- // Config file corrupted or missing — fall through to env-only path
31
- }
32
- // Resolve final token: env takes precedence over file
33
- const resolvedToken = envToken || fileConfig.api_token || "";
34
- // If no token available from any source, sync is not possible
35
- if (!resolvedToken) {
36
- return null;
37
- }
38
- // Build the merged config
39
- const config = {
40
- ...DEFAULT_CONFIG,
41
- ...fileConfig,
42
- api_token: resolvedToken,
43
- // Env vars take precedence over file values
44
- ...(envUrl ? { api_url: envUrl } : {}),
45
- ...(envOrgId ? { organization_id: envOrgId } : {}),
46
- };
47
- // Ensure api_url has a sensible default
48
- if (!config.api_url) {
49
- config.api_url = DEFAULT_API_URL;
50
- }
51
- // Resolve enabled flag
52
- config.enabled = fileConfig.enabled ?? true;
53
- return config;
54
- }
55
- export function isSyncEnabled() {
56
- const config = loadSyncConfig();
57
- return config !== null && !!config.api_token;
58
- }
59
- export function saveSyncConfig(config) {
60
- const current = loadSyncConfig() ?? DEFAULT_CONFIG;
61
- const merged = { ...current, ...config };
62
- mkdirSync(dirname(CONFIG_PATH), { recursive: true });
63
- writeFileSync(CONFIG_PATH, JSON.stringify(merged, null, 2), "utf-8");
64
- }
@@ -1,12 +0,0 @@
1
- export declare function initSync(sessionId: string, projectDir: string, gitRemote?: string): boolean;
2
- export declare function enqueueEvent(event: {
3
- type: string;
4
- category: string;
5
- priority: number;
6
- data: string;
7
- source_hook: string;
8
- created_at: string;
9
- }, projectDir: string, sessionId: string): void;
10
- export declare function flushSync(): Promise<void>;
11
- export declare function destroySync(): void;
12
- export declare function isSyncEnabled(): boolean;
@@ -1,55 +0,0 @@
1
- import { EventBatcher } from "./batcher.js";
2
- import { EventSender } from "./sender.js";
3
- import { sanitizeEvent, hashSessionId } from "./sanitizer.js";
4
- import { loadSyncConfig } from "./config.js";
5
- let batcher = null;
6
- let sender = null;
7
- let config = null;
8
- export function initSync(sessionId, projectDir, gitRemote) {
9
- const loadedConfig = loadSyncConfig();
10
- if (!loadedConfig || !loadedConfig.enabled || !loadedConfig.api_token || !loadedConfig.api_url) {
11
- return false;
12
- }
13
- const validConfig = loadedConfig;
14
- config = validConfig;
15
- const hashedSessionId = hashSessionId(sessionId);
16
- sender = new EventSender({
17
- apiUrl: validConfig.api_url,
18
- apiToken: validConfig.api_token,
19
- sessionId: hashedSessionId,
20
- projectGitRemote: gitRemote,
21
- });
22
- batcher = new EventBatcher({
23
- batchSize: validConfig.batch_size,
24
- flushIntervalMs: validConfig.flush_interval_ms,
25
- maxBufferSize: 5000,
26
- onFlush: async (events) => {
27
- await sender.send(events);
28
- },
29
- });
30
- return true;
31
- }
32
- export function enqueueEvent(event, projectDir, sessionId) {
33
- if (!batcher)
34
- return;
35
- const sanitized = sanitizeEvent(event, projectDir, sessionId);
36
- if (!sanitized)
37
- return; // Blocked event
38
- batcher.push(sanitized);
39
- }
40
- export async function flushSync() {
41
- if (batcher) {
42
- await batcher.flush();
43
- }
44
- }
45
- export function destroySync() {
46
- if (batcher) {
47
- batcher.destroy();
48
- batcher = null;
49
- }
50
- sender = null;
51
- config = null;
52
- }
53
- export function isSyncEnabled() {
54
- return batcher !== null;
55
- }
@@ -1,13 +0,0 @@
1
- import type { SanitizedEvent } from "./types.js";
2
- export declare function isBlockedEvent(type: string, category: string): boolean;
3
- export declare function relativizePaths(data: string, projectDir: string): string;
4
- export declare function redactSecrets(data: string): string;
5
- export declare function hashSessionId(sessionId: string): string;
6
- export declare function sanitizeEvent(event: {
7
- type: string;
8
- category: string;
9
- priority: number;
10
- data: string;
11
- source_hook: string;
12
- created_at: string;
13
- }, projectDir: string, _sessionId: string): SanitizedEvent | null;
@@ -1,86 +0,0 @@
1
- import { createHash } from "node:crypto";
2
- // Event types that must NEVER be sent to cloud
3
- const BLOCKED_TYPES = new Set([
4
- "user_prompt",
5
- "ai_response",
6
- "clipboard_content",
7
- "terminal_output",
8
- ]);
9
- // Categories that must never be sent
10
- const BLOCKED_CATEGORIES = new Set(["prompt"]);
11
- // Secret patterns to redact
12
- const SECRET_PATTERNS = [
13
- // AWS
14
- [/AKIA[0-9A-Z]{16}/g, "[AWS_KEY]"],
15
- [
16
- /(aws_secret_access_key|AWS_SECRET_ACCESS_KEY)\s*[=:]\s*\S+/g,
17
- "$1=[REDACTED]",
18
- ],
19
- // GitHub
20
- [/gh[ps]_[A-Za-z0-9_]{36,}/g, "[GITHUB_TOKEN]"],
21
- [/github_pat_[A-Za-z0-9_]{22,}/g, "[GITHUB_TOKEN]"],
22
- // Stripe
23
- [/sk_(test|live)_[A-Za-z0-9]{20,}/g, "[STRIPE_KEY]"],
24
- // Generic KEY=VALUE (for export commands)
25
- [/export\s+(\w+)=\S+/g, "export $1=[REDACTED]"],
26
- // Generic tokens/passwords (negative lookahead avoids clobbering already-redacted values)
27
- [/(password|secret|token|api_key)\s*[=:]\s*(?!\[)\S+/gi, "$1=[REDACTED]"],
28
- ];
29
- export function isBlockedEvent(type, category) {
30
- return BLOCKED_TYPES.has(type) || BLOCKED_CATEGORIES.has(category);
31
- }
32
- export function relativizePaths(data, projectDir) {
33
- const home = process.env.HOME || process.env.USERPROFILE || "";
34
- let result = data;
35
- // Replace project dir first (more specific)
36
- if (projectDir) {
37
- result = result.replaceAll(projectDir, ".");
38
- }
39
- // Replace home dir
40
- if (home) {
41
- result = result.replaceAll(home, "~");
42
- }
43
- return result;
44
- }
45
- export function redactSecrets(data) {
46
- let result = data;
47
- for (const [pattern, replacement] of SECRET_PATTERNS) {
48
- result = result.replace(pattern, replacement);
49
- }
50
- return result;
51
- }
52
- export function hashSessionId(sessionId) {
53
- return createHash("sha256").update(sessionId).digest("hex").slice(0, 16);
54
- }
55
- export function sanitizeEvent(event, projectDir, _sessionId) {
56
- // Block forbidden events
57
- if (isBlockedEvent(event.type, event.category))
58
- return null;
59
- let data = event.data;
60
- // Stage 1: Relativize paths
61
- data = relativizePaths(data, projectDir);
62
- // Stage 2: Redact secrets
63
- data = redactSecrets(data);
64
- // Stage 3: Strip rule_content
65
- if (event.type === "rule_content") {
66
- data = "[rule_content]";
67
- }
68
- // Stage 4: Truncate to 200 chars
69
- if (data.length > 200) {
70
- data = data.slice(0, 197) + "...";
71
- }
72
- // Stage 5: Hash for dedup
73
- const data_hash = createHash("sha256")
74
- .update(`${event.type}:${data}:${event.created_at}`)
75
- .digest("hex")
76
- .slice(0, 16);
77
- return {
78
- type: event.type,
79
- category: event.category,
80
- priority: event.priority,
81
- data,
82
- source_hook: event.source_hook,
83
- created_at: event.created_at,
84
- data_hash,
85
- };
86
- }
@@ -1,15 +0,0 @@
1
- import type { SanitizedEvent, IngestBatchResponse } from "./types.js";
2
- interface SenderOpts {
3
- apiUrl: string;
4
- apiToken: string;
5
- sessionId: string;
6
- projectGitRemote?: string;
7
- machineId?: string;
8
- timeoutMs?: number;
9
- }
10
- export declare class EventSender {
11
- private opts;
12
- constructor(opts: SenderOpts);
13
- send(events: SanitizedEvent[]): Promise<IngestBatchResponse>;
14
- }
15
- export {};
@@ -1,30 +0,0 @@
1
- export class EventSender {
2
- opts;
3
- constructor(opts) {
4
- this.opts = opts;
5
- }
6
- async send(events) {
7
- const url = `${this.opts.apiUrl}/ingest/sessions/${this.opts.sessionId}/events`;
8
- const fetchOpts = {
9
- method: "POST",
10
- headers: {
11
- "Content-Type": "application/json",
12
- Authorization: `Bearer ${this.opts.apiToken}`,
13
- },
14
- body: JSON.stringify({
15
- events,
16
- project_git_remote: this.opts.projectGitRemote,
17
- machine_id: this.opts.machineId,
18
- }),
19
- };
20
- if (this.opts.timeoutMs) {
21
- fetchOpts.signal = AbortSignal.timeout(this.opts.timeoutMs);
22
- }
23
- const response = await fetch(url, fetchOpts);
24
- if (!response.ok) {
25
- const text = await response.text().catch(() => "");
26
- throw new Error(`Sync failed: ${response.status} ${text}`);
27
- }
28
- return response.json();
29
- }
30
- }
@@ -1,31 +0,0 @@
1
- export interface SyncConfig {
2
- enabled: boolean;
3
- api_url: string;
4
- api_token: string;
5
- organization_id: string;
6
- batch_size: number;
7
- flush_interval_ms: number;
8
- }
9
- export interface SanitizedEvent {
10
- type: string;
11
- category: string;
12
- priority: number;
13
- data: string;
14
- source_hook: string;
15
- created_at: string;
16
- data_hash: string;
17
- }
18
- export interface IngestBatchRequest {
19
- events: SanitizedEvent[];
20
- project_git_remote?: string;
21
- machine_id?: string;
22
- }
23
- export interface IngestBatchResponse {
24
- success: boolean;
25
- data?: {
26
- accepted: number;
27
- duplicates: number;
28
- session_id: string;
29
- };
30
- error?: string;
31
- }
@@ -1 +0,0 @@
1
- export {};