context-mode 1.0.150 → 1.0.152
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.codex-plugin/mcp.json +5 -1
- package/.codex-plugin/plugin.json +1 -1
- package/.openclaw-plugin/openclaw.plugin.json +16 -1
- package/.openclaw-plugin/package.json +1 -1
- package/README.md +89 -3
- package/build/adapters/claude-code/hooks.js +2 -2
- package/build/adapters/claude-code/index.js +14 -13
- package/build/adapters/client-map.js +3 -0
- package/build/adapters/detect.js +13 -1
- package/build/adapters/gemini-cli/hooks.d.ts +10 -0
- package/build/adapters/gemini-cli/hooks.js +12 -2
- package/build/adapters/gemini-cli/index.d.ts +21 -1
- package/build/adapters/gemini-cli/index.js +37 -1
- package/build/adapters/kimi/config.d.ts +8 -0
- package/build/adapters/kimi/config.js +8 -0
- package/build/adapters/kimi/hooks.d.ts +28 -0
- package/build/adapters/kimi/hooks.js +34 -0
- package/build/adapters/kimi/index.d.ts +66 -0
- package/build/adapters/kimi/index.js +537 -0
- package/build/adapters/kimi/paths.d.ts +1 -0
- package/build/adapters/kimi/paths.js +12 -0
- package/build/adapters/kiro/hooks.js +2 -2
- package/build/adapters/openclaw/plugin.d.ts +14 -13
- package/build/adapters/openclaw/plugin.js +140 -40
- package/build/adapters/opencode/plugin.js +4 -3
- package/build/adapters/opencode/zod3tov4.js +8 -8
- package/build/adapters/pi/extension.js +9 -24
- package/build/adapters/pi/mcp-bridge.js +37 -0
- package/build/adapters/qwen-code/index.js +7 -7
- package/build/adapters/types.d.ts +39 -2
- package/build/adapters/types.js +55 -2
- package/build/adapters/vscode-copilot/index.js +13 -1
- package/build/cli.js +433 -25
- package/build/executor.js +6 -3
- package/build/runtime.d.ts +81 -1
- package/build/runtime.js +195 -9
- package/build/search/ctx-search-schema.d.ts +90 -0
- package/build/search/ctx-search-schema.js +135 -0
- package/build/search/unified.d.ts +12 -0
- package/build/search/unified.js +17 -2
- package/build/server.d.ts +2 -1
- package/build/server.js +378 -97
- package/build/session/analytics.d.ts +36 -13
- package/build/session/analytics.js +123 -26
- package/build/session/db.d.ts +24 -0
- package/build/session/db.js +41 -0
- package/build/session/extract.js +30 -0
- package/build/session/snapshot.js +24 -0
- package/build/store.d.ts +12 -1
- package/build/store.js +72 -20
- package/build/types.d.ts +7 -0
- package/build/util/project-dir.d.ts +19 -16
- package/build/util/project-dir.js +80 -45
- package/cli.bundle.mjs +371 -320
- package/configs/kimi/hooks.json +54 -0
- package/configs/pi/AGENTS.md +3 -85
- package/hooks/cache-heal-utils.mjs +148 -0
- package/hooks/core/formatters.mjs +26 -0
- package/hooks/core/routing.mjs +9 -1
- package/hooks/core/stdin.mjs +74 -3
- package/hooks/core/tool-naming.mjs +1 -0
- package/hooks/heal-partial-install.mjs +712 -0
- package/hooks/kimi/platform.mjs +1 -0
- package/hooks/kimi/posttooluse.mjs +72 -0
- package/hooks/kimi/precompact.mjs +80 -0
- package/hooks/kimi/pretooluse.mjs +42 -0
- package/hooks/kimi/sessionend.mjs +61 -0
- package/hooks/kimi/sessionstart.mjs +113 -0
- package/hooks/kimi/stop.mjs +61 -0
- package/hooks/kimi/userpromptsubmit.mjs +90 -0
- package/hooks/normalize-hooks.mjs +66 -12
- package/hooks/routing-block.mjs +8 -2
- package/hooks/security.bundle.mjs +1 -1
- package/hooks/session-db.bundle.mjs +6 -4
- package/hooks/session-extract.bundle.mjs +2 -2
- package/hooks/session-helpers.mjs +93 -3
- package/hooks/session-snapshot.bundle.mjs +20 -19
- package/hooks/sessionstart.mjs +64 -0
- package/insight/server.mjs +15 -3
- package/openclaw.plugin.json +16 -1
- package/package.json +1 -1
- package/scripts/heal-installed-plugins.mjs +31 -10
- package/scripts/postinstall.mjs +10 -0
- package/server.bundle.mjs +206 -157
- package/skills/ctx-index/SKILL.md +46 -0
- package/skills/ctx-search/SKILL.md +35 -0
- package/start.mjs +84 -11
- package/build/cache-heal.d.ts +0 -48
- package/build/cache-heal.js +0 -150
- package/build/concurrency/runPool.d.ts +0 -36
- package/build/concurrency/runPool.js +0 -51
- package/build/openclaw/mcp-tools.d.ts +0 -54
- package/build/openclaw/mcp-tools.js +0 -198
- package/build/openclaw/workspace-router.d.ts +0 -29
- package/build/openclaw/workspace-router.js +0 -64
- package/build/openclaw-plugin.d.ts +0 -130
- package/build/openclaw-plugin.js +0 -626
- package/build/opencode-plugin.d.ts +0 -122
- package/build/opencode-plugin.js +0 -375
- package/build/pi-extension.d.ts +0 -14
- package/build/pi-extension.js +0 -451
- package/build/routing-block.d.ts +0 -8
- package/build/routing-block.js +0 -86
- package/build/tool-naming.d.ts +0 -4
- package/build/tool-naming.js +0 -24
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ctx-index
|
|
3
|
+
description: |
|
|
4
|
+
Index a local file or directory into context-mode's persistent FTS5 knowledge base
|
|
5
|
+
so future ctx_search calls can retrieve focused snippets without rereading raw files.
|
|
6
|
+
Trigger: /context-mode:ctx-index
|
|
7
|
+
user-invocable: true
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Context Mode Index
|
|
11
|
+
|
|
12
|
+
Index local project content for later search.
|
|
13
|
+
|
|
14
|
+
## Instructions
|
|
15
|
+
|
|
16
|
+
1. Prefer the `ctx_index` MCP tool when it is available.
|
|
17
|
+
2. Ask for a path only if the user did not provide one and the current project root is ambiguous.
|
|
18
|
+
3. Use `path`, not large inline `content`, so file bytes do not enter the conversation.
|
|
19
|
+
4. For repository indexing, pass conservative bounds and a clear source label:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
ctx_index({
|
|
23
|
+
path: ".",
|
|
24
|
+
source: "project:<name>",
|
|
25
|
+
maxDepth: 5,
|
|
26
|
+
maxFiles: 200
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
5. If MCP tools are unavailable, fall back to the CLI:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
context-mode index . --source project:<name>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
6. Report the indexed source label, file count or section count, and the matching search command:
|
|
37
|
+
|
|
38
|
+
```javascript
|
|
39
|
+
ctx_search({ source: "project:<name>", queries: ["..."] })
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Safety
|
|
43
|
+
|
|
44
|
+
- Do not index dependency directories, build outputs, secrets, or generated artifacts.
|
|
45
|
+
- Prefer `--exclude` or `exclude` for project-specific noisy paths.
|
|
46
|
+
- For broad repos, ask the user before raising `maxFiles` above 500.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ctx-search
|
|
3
|
+
description: |
|
|
4
|
+
Search context-mode's persistent FTS5 knowledge base for previously indexed
|
|
5
|
+
local project content, documentation, or session memory.
|
|
6
|
+
Trigger: /context-mode:ctx-search
|
|
7
|
+
user-invocable: true
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# Context Mode Search
|
|
11
|
+
|
|
12
|
+
Search indexed content without rereading raw sources into conversation context.
|
|
13
|
+
|
|
14
|
+
## Instructions
|
|
15
|
+
|
|
16
|
+
1. Prefer the `ctx_search` MCP tool when it is available.
|
|
17
|
+
2. Batch all related questions in one `queries` array.
|
|
18
|
+
3. Scope with `source` when the user names a project or indexed label.
|
|
19
|
+
4. Use short, specific queries of two to four technical terms.
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
ctx_search({
|
|
23
|
+
source: "project:<name>",
|
|
24
|
+
queries: ["authentication middleware", "token refresh"],
|
|
25
|
+
limit: 5
|
|
26
|
+
})
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
5. If MCP tools are unavailable, fall back to the CLI:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
context-mode search "authentication middleware" --source project:<name> --limit 5
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
6. If the index is empty, tell the user to run `/context-mode:ctx-index` or `context-mode index <path>` first.
|
package/start.mjs
CHANGED
|
@@ -36,7 +36,7 @@ function resolveClaudeConfigDir() {
|
|
|
36
36
|
// the env auto-set in that case; getProjectDir() defends a second time inside
|
|
37
37
|
// server.ts via resolveProjectDir(). See src/util/project-dir.ts.
|
|
38
38
|
const isPluginInstallPath = (p) =>
|
|
39
|
-
/[/\\]\.claude[/\\]plugins[/\\](cache|marketplaces)[/\\]/.test(p);
|
|
39
|
+
/[/\\]\.(claude|codex)[/\\]plugins[/\\](cache|marketplaces)[/\\]/.test(p);
|
|
40
40
|
const safeOriginalCwd = isPluginInstallPath(originalCwd) ? null : originalCwd;
|
|
41
41
|
|
|
42
42
|
if (!process.env.CLAUDE_PROJECT_DIR && safeOriginalCwd) {
|
|
@@ -121,11 +121,33 @@ if (cacheMatch) {
|
|
|
121
121
|
});
|
|
122
122
|
const newest = dirs[dirs.length - 1];
|
|
123
123
|
if (newest && newest !== myVersion) {
|
|
124
|
+
// Issue #727: normalize hooks.json + plugin.json in the newest version
|
|
125
|
+
// dir BEFORE updating the registry. CC's auto-update carries forward
|
|
126
|
+
// files from the old cache dir, including hooks.json and plugin.json
|
|
127
|
+
// with absolute paths baked to the old version. start.mjs's Self-heal
|
|
128
|
+
// Layer 5 would catch this, but plugin.json's stale mcpServers path
|
|
129
|
+
// prevents the new start.mjs from ever booting — chicken-and-egg.
|
|
130
|
+
// Fix: normalize from HERE (the old start.mjs that CC CAN still launch)
|
|
131
|
+
// so the new dir's files are correct before the next session reads them.
|
|
132
|
+
const newestDir = resolve(cacheParent, newest);
|
|
133
|
+
try {
|
|
134
|
+
// #713: use narrow helper — wide normalizeHooksOnStartup would
|
|
135
|
+
// write plugin.json on the NEW cache dir, the exact #711 poison
|
|
136
|
+
// vector. Only hooks.json needs the placeholder→absolute rewrite
|
|
137
|
+
// pre-bump to close the first-hook-fire window.
|
|
138
|
+
const { normalizeHooksJsonOnly } = await import("./hooks/normalize-hooks.mjs");
|
|
139
|
+
normalizeHooksJsonOnly({
|
|
140
|
+
pluginRoot: newestDir,
|
|
141
|
+
nodePath: process.execPath,
|
|
142
|
+
platform: process.platform,
|
|
143
|
+
});
|
|
144
|
+
} catch { /* best effort — never block startup */ }
|
|
145
|
+
|
|
124
146
|
const ip = JSON.parse(readFileSync(ipPath, "utf-8"));
|
|
125
147
|
for (const [key, entries] of Object.entries(ip.plugins || {})) {
|
|
126
148
|
if (key !== "context-mode@context-mode") continue;
|
|
127
149
|
for (const entry of entries) {
|
|
128
|
-
entry.installPath =
|
|
150
|
+
entry.installPath = newestDir;
|
|
129
151
|
entry.version = newest;
|
|
130
152
|
entry.lastUpdated = new Date().toISOString();
|
|
131
153
|
}
|
|
@@ -253,11 +275,11 @@ try {
|
|
|
253
275
|
if (existsSync(oldBashHook)) {
|
|
254
276
|
try { unlinkSync(oldBashHook); } catch {}
|
|
255
277
|
}
|
|
256
|
-
if (!existsSync(
|
|
257
|
-
|
|
258
|
-
const healScript = `#!/usr/bin/env node
|
|
278
|
+
if (!existsSync(globalHooksDir)) mkdirSync(globalHooksDir, { recursive: true });
|
|
279
|
+
const healScript = `#!/usr/bin/env node
|
|
259
280
|
// context-mode plugin cache self-heal (auto-deployed)
|
|
260
281
|
// Fixes anthropics/claude-code#46915: auto-update breaks CLAUDE_PLUGIN_ROOT
|
|
282
|
+
// Issue #727: also normalizes stale version paths in existing installPaths
|
|
261
283
|
// Honors CLAUDE_CONFIG_DIR (#577) — checked at this script's runtime so users
|
|
262
284
|
// who set CLAUDE_CONFIG_DIR after install still get healed correctly.
|
|
263
285
|
// Pure Node.js — no bash/shell dependency.
|
|
@@ -274,8 +296,25 @@ try{
|
|
|
274
296
|
if(k!=="context-mode@context-mode")continue;
|
|
275
297
|
for(const e of es){
|
|
276
298
|
const p=e.installPath;
|
|
277
|
-
if(!p
|
|
299
|
+
if(!p)continue;
|
|
278
300
|
if(!resolve(p).startsWith(cacheRoot+sep))continue;
|
|
301
|
+
if(existsSync(p)){
|
|
302
|
+
// Issue #727: normalize stale version paths in existing installPaths.
|
|
303
|
+
// CC's auto-update can carry forward hooks.json/plugin.json with paths
|
|
304
|
+
// baked to a previous version dir. Import normalize-hooks from the
|
|
305
|
+
// installPath itself and let it detect + rewrite stale segments.
|
|
306
|
+
try{
|
|
307
|
+
// #713: narrow helper only — installPath belongs to a different
|
|
308
|
+
// version's cache dir; writing plugin.json there is the #711 vector.
|
|
309
|
+
const nhPath=join(p,"hooks","normalize-hooks.mjs");
|
|
310
|
+
if(existsSync(nhPath)){
|
|
311
|
+
const mod=await import(nhPath);
|
|
312
|
+
const fn=mod.normalizeHooksJsonOnly||mod.normalizeHooksOnStartup;
|
|
313
|
+
if(fn)fn({pluginRoot:p,nodePath:process.execPath,platform:process.platform});
|
|
314
|
+
}
|
|
315
|
+
}catch{}
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
279
318
|
const parent=dirname(p);
|
|
280
319
|
if(!existsSync(parent))continue;
|
|
281
320
|
try{if(lstatSync(p).isSymbolicLink())unlinkSync(p)}catch{}
|
|
@@ -287,6 +326,13 @@ try{
|
|
|
287
326
|
}
|
|
288
327
|
}catch{}
|
|
289
328
|
`;
|
|
329
|
+
// Deploy or update the heal hook when content changes (not just when missing).
|
|
330
|
+
// Allows new heal logic (e.g. #727 path normalization) to propagate on next boot.
|
|
331
|
+
let needsWrite = !existsSync(healHookPath);
|
|
332
|
+
if (!needsWrite) {
|
|
333
|
+
try { needsWrite = readFileSync(healHookPath, "utf-8") !== healScript; } catch { needsWrite = true; }
|
|
334
|
+
}
|
|
335
|
+
if (needsWrite) {
|
|
290
336
|
writeFileSync(healHookPath, healScript, { mode: 0o755 });
|
|
291
337
|
}
|
|
292
338
|
|
|
@@ -353,9 +399,20 @@ try{
|
|
|
353
399
|
if (!process.env.VITEST) {
|
|
354
400
|
try {
|
|
355
401
|
const { normalizeHooksOnStartup } = await import("./hooks/normalize-hooks.mjs");
|
|
402
|
+
// #738: probe for Bun ≥1.0 and pass the resolved path so the static
|
|
403
|
+
// hooks/hooks.json rewrite swaps `node` → `bun` (~40-60ms cold-start win
|
|
404
|
+
// per hook). Probe is wrapped in its own try so resolveHookRuntime
|
|
405
|
+
// failures (missing build, missing module) never block boot.
|
|
406
|
+
let jsRuntimePath;
|
|
407
|
+
try {
|
|
408
|
+
const { resolveHookRuntime } = await import("./build/runtime.js");
|
|
409
|
+
const r = resolveHookRuntime();
|
|
410
|
+
if (r.isBun) jsRuntimePath = r.path;
|
|
411
|
+
} catch { /* best effort — fall through to nodePath default */ }
|
|
356
412
|
normalizeHooksOnStartup({
|
|
357
413
|
pluginRoot: __dirname,
|
|
358
414
|
nodePath: process.execPath,
|
|
415
|
+
jsRuntimePath,
|
|
359
416
|
platform: process.platform,
|
|
360
417
|
});
|
|
361
418
|
} catch { /* best effort — never block server startup */ }
|
|
@@ -415,6 +472,21 @@ if (!existsSync(resolve(__dirname, "cli.bundle.mjs")) && existsSync(resolve(__di
|
|
|
415
472
|
if (process.platform !== "win32") chmodSync(shimPath, 0o755);
|
|
416
473
|
}
|
|
417
474
|
|
|
475
|
+
// ── Self-heal partial install from marketplace clone ──
|
|
476
|
+
// Runs BEFORE the Algo-D4 integrity check so a fixable partial install
|
|
477
|
+
// gets repaired rather than just reported. Best-effort and idempotent;
|
|
478
|
+
// the integrity check below remains the authoritative gate that decides
|
|
479
|
+
// whether boot proceeds. See hooks/heal-partial-install.mjs for the
|
|
480
|
+
// failure-mode description and module contract.
|
|
481
|
+
if (!process.env.VITEST) {
|
|
482
|
+
try {
|
|
483
|
+
const { healPartialInstallFromMarketplace } = await import(
|
|
484
|
+
"./hooks/heal-partial-install.mjs"
|
|
485
|
+
);
|
|
486
|
+
healPartialInstallFromMarketplace({ pluginRoot: __dirname });
|
|
487
|
+
} catch { /* best effort, never block boot */ }
|
|
488
|
+
}
|
|
489
|
+
|
|
418
490
|
// ── Algo-D4: plugin cache integrity check ──
|
|
419
491
|
// Verify boot-critical siblings exist BEFORE importing server.bundle.mjs.
|
|
420
492
|
// Without this, a partial install (#550) gives an opaque downstream
|
|
@@ -423,11 +495,12 @@ if (!existsSync(resolve(__dirname, "cli.bundle.mjs")) && existsSync(resolve(__di
|
|
|
423
495
|
// external monitoring grep + the user both see the actionable signal.
|
|
424
496
|
//
|
|
425
497
|
// Runs AFTER the heal layers above so missing files they can fix
|
|
426
|
-
// (cli.bundle.mjs shim, dangling symlinks
|
|
427
|
-
//
|
|
428
|
-
//
|
|
429
|
-
//
|
|
430
|
-
//
|
|
498
|
+
// (cli.bundle.mjs shim, dangling symlinks, partial-install copy from
|
|
499
|
+
// the marketplace clone) get a chance first. Helper is shared with
|
|
500
|
+
// `ctx doctor` (Algo-D5) — single source of truth so boot + diagnostic
|
|
501
|
+
// agree byte-for-byte. Skipped under VITEST so the repo's own test
|
|
502
|
+
// invocations against in-tree start.mjs don't fail when running before
|
|
503
|
+
// `npm run build` produces the bundles.
|
|
431
504
|
if (!process.env.VITEST) {
|
|
432
505
|
try {
|
|
433
506
|
const { assertPluginCacheIntegrity, formatPartialInstallReport } =
|
package/build/cache-heal.d.ts
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
|
|
3
|
-
*
|
|
4
|
-
* Claude Code's plugin auto-update can leave installed_plugins.json pointing
|
|
5
|
-
* to a non-existent directory (anthropics/claude-code#46915). This module
|
|
6
|
-
* detects and repairs the mismatch by creating symlinks.
|
|
7
|
-
*
|
|
8
|
-
* 4-layer defense:
|
|
9
|
-
* 1. start.mjs startup — reverse heal (registry → symlink to us)
|
|
10
|
-
* 2. server.ts first tool call — mid-session heal
|
|
11
|
-
* 3. postinstall.mjs — backward symlink on new install
|
|
12
|
-
* 4. global hook auto-deploy — survives total plugin cache breakage
|
|
13
|
-
*/
|
|
14
|
-
export interface HealResult {
|
|
15
|
-
healed: boolean;
|
|
16
|
-
action?: "symlink" | "global-hook" | "none";
|
|
17
|
-
from?: string;
|
|
18
|
-
to?: string;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Core heal: if installed_plugins.json points to a non-existent directory,
|
|
22
|
-
* create a symlink from that path to our actual directory.
|
|
23
|
-
*
|
|
24
|
-
* @param currentDir - The directory we're actually running from
|
|
25
|
-
* @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
|
|
26
|
-
*/
|
|
27
|
-
export declare function healRegistryMismatch(currentDir: string, installedPluginsPath?: string): HealResult;
|
|
28
|
-
/**
|
|
29
|
-
* Deploy a global SessionStart hook that heals plugin cache mismatches.
|
|
30
|
-
* This hook lives outside the plugin directory, so it survives cache breakage.
|
|
31
|
-
*
|
|
32
|
-
* Written to ~/.claude/hooks/context-mode-cache-heal.sh
|
|
33
|
-
*/
|
|
34
|
-
export declare function deployGlobalHealHook(): HealResult;
|
|
35
|
-
/**
|
|
36
|
-
* Backward symlink: during postinstall, if the registry points to a
|
|
37
|
-
* non-existent OLD path, create a symlink from old → new (our directory).
|
|
38
|
-
* Same as healRegistryMismatch but called from postinstall context.
|
|
39
|
-
*/
|
|
40
|
-
export { healRegistryMismatch as healBackwardCompat };
|
|
41
|
-
/**
|
|
42
|
-
* Mid-session heal — call on first MCP tool invocation.
|
|
43
|
-
* Checks if registry path differs from our running directory.
|
|
44
|
-
* Creates symlink if needed. Runs only once per process.
|
|
45
|
-
*/
|
|
46
|
-
export declare function healMidSession(currentDir: string): HealResult;
|
|
47
|
-
/** Reset mid-session flag (for testing only) */
|
|
48
|
-
export declare function _resetMidSession(): void;
|
package/build/cache-heal.js
DELETED
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Plugin cache self-heal — fixes broken CLAUDE_PLUGIN_ROOT references.
|
|
3
|
-
*
|
|
4
|
-
* Claude Code's plugin auto-update can leave installed_plugins.json pointing
|
|
5
|
-
* to a non-existent directory (anthropics/claude-code#46915). This module
|
|
6
|
-
* detects and repairs the mismatch by creating symlinks.
|
|
7
|
-
*
|
|
8
|
-
* 4-layer defense:
|
|
9
|
-
* 1. start.mjs startup — reverse heal (registry → symlink to us)
|
|
10
|
-
* 2. server.ts first tool call — mid-session heal
|
|
11
|
-
* 3. postinstall.mjs — backward symlink on new install
|
|
12
|
-
* 4. global hook auto-deploy — survives total plugin cache breakage
|
|
13
|
-
*/
|
|
14
|
-
import { existsSync, readFileSync, symlinkSync, mkdirSync, writeFileSync } from "node:fs";
|
|
15
|
-
import { resolve, dirname } from "node:path";
|
|
16
|
-
import { homedir } from "node:os";
|
|
17
|
-
/**
|
|
18
|
-
* Core heal: if installed_plugins.json points to a non-existent directory,
|
|
19
|
-
* create a symlink from that path to our actual directory.
|
|
20
|
-
*
|
|
21
|
-
* @param currentDir - The directory we're actually running from
|
|
22
|
-
* @param installedPluginsPath - Path to installed_plugins.json (injectable for testing)
|
|
23
|
-
*/
|
|
24
|
-
export function healRegistryMismatch(currentDir, installedPluginsPath) {
|
|
25
|
-
const ipPath = installedPluginsPath ?? resolve(homedir(), ".claude", "plugins", "installed_plugins.json");
|
|
26
|
-
if (!existsSync(ipPath))
|
|
27
|
-
return { healed: false, action: "none" };
|
|
28
|
-
if (!existsSync(currentDir))
|
|
29
|
-
return { healed: false, action: "none" };
|
|
30
|
-
let ip;
|
|
31
|
-
try {
|
|
32
|
-
ip = JSON.parse(readFileSync(ipPath, "utf-8"));
|
|
33
|
-
}
|
|
34
|
-
catch {
|
|
35
|
-
return { healed: false, action: "none" };
|
|
36
|
-
}
|
|
37
|
-
for (const [key, entries] of Object.entries(ip.plugins ?? {})) {
|
|
38
|
-
if (!key.toLowerCase().includes("context-mode"))
|
|
39
|
-
continue;
|
|
40
|
-
for (const entry of entries) {
|
|
41
|
-
const registryPath = entry.installPath;
|
|
42
|
-
if (!registryPath)
|
|
43
|
-
continue;
|
|
44
|
-
// Registry path exists — no healing needed
|
|
45
|
-
if (existsSync(registryPath))
|
|
46
|
-
continue;
|
|
47
|
-
// Registry path doesn't exist — create symlink to our directory
|
|
48
|
-
try {
|
|
49
|
-
const parent = dirname(registryPath);
|
|
50
|
-
if (!existsSync(parent))
|
|
51
|
-
mkdirSync(parent, { recursive: true });
|
|
52
|
-
if (process.platform === "win32") {
|
|
53
|
-
// Windows: use junction (no admin required)
|
|
54
|
-
symlinkSync(currentDir, registryPath, "junction");
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
symlinkSync(currentDir, registryPath);
|
|
58
|
-
}
|
|
59
|
-
return { healed: true, action: "symlink", from: registryPath, to: currentDir };
|
|
60
|
-
}
|
|
61
|
-
catch {
|
|
62
|
-
return { healed: false, action: "none" };
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
return { healed: false, action: "none" };
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Deploy a global SessionStart hook that heals plugin cache mismatches.
|
|
70
|
-
* This hook lives outside the plugin directory, so it survives cache breakage.
|
|
71
|
-
*
|
|
72
|
-
* Written to ~/.claude/hooks/context-mode-cache-heal.sh
|
|
73
|
-
*/
|
|
74
|
-
export function deployGlobalHealHook() {
|
|
75
|
-
const hooksDir = resolve(homedir(), ".claude", "hooks");
|
|
76
|
-
const hookPath = resolve(hooksDir, "context-mode-cache-heal.sh");
|
|
77
|
-
// Already deployed
|
|
78
|
-
if (existsSync(hookPath))
|
|
79
|
-
return { healed: false, action: "none" };
|
|
80
|
-
try {
|
|
81
|
-
if (!existsSync(hooksDir))
|
|
82
|
-
mkdirSync(hooksDir, { recursive: true });
|
|
83
|
-
const script = `#!/usr/bin/env bash
|
|
84
|
-
# context-mode plugin cache self-heal — auto-deployed by context-mode MCP server
|
|
85
|
-
# Fixes anthropics/claude-code#46915: auto-update breaks CLAUDE_PLUGIN_ROOT
|
|
86
|
-
# This hook runs at SessionStart (global, not plugin-level) so it works even
|
|
87
|
-
# when the plugin cache is broken.
|
|
88
|
-
|
|
89
|
-
set -euo pipefail
|
|
90
|
-
|
|
91
|
-
PLUGINS_FILE="$HOME/.claude/plugins/installed_plugins.json"
|
|
92
|
-
[[ -f "$PLUGINS_FILE" ]] || exit 0
|
|
93
|
-
|
|
94
|
-
# Find context-mode entries and heal missing directories
|
|
95
|
-
node -e '
|
|
96
|
-
const fs = require("fs");
|
|
97
|
-
const path = require("path");
|
|
98
|
-
try {
|
|
99
|
-
const ip = JSON.parse(fs.readFileSync(process.argv[1], "utf-8"));
|
|
100
|
-
for (const [key, entries] of Object.entries(ip.plugins || {})) {
|
|
101
|
-
if (!key.toLowerCase().includes("context-mode")) continue;
|
|
102
|
-
for (const entry of entries) {
|
|
103
|
-
const p = entry.installPath;
|
|
104
|
-
if (!p || fs.existsSync(p)) continue;
|
|
105
|
-
const parent = path.dirname(p);
|
|
106
|
-
if (!fs.existsSync(parent)) continue;
|
|
107
|
-
const dirs = fs.readdirSync(parent).filter(d => /^\\d+\\.\\d+/.test(d) && fs.statSync(path.join(parent, d)).isDirectory());
|
|
108
|
-
if (dirs.length === 0) continue;
|
|
109
|
-
dirs.sort((a, b) => {
|
|
110
|
-
const pa = a.split(".").map(Number), pb = b.split(".").map(Number);
|
|
111
|
-
for (let i = 0; i < 3; i++) { if ((pa[i]||0) !== (pb[i]||0)) return (pa[i]||0) - (pb[i]||0); }
|
|
112
|
-
return 0;
|
|
113
|
-
});
|
|
114
|
-
const target = path.join(parent, dirs[dirs.length - 1]);
|
|
115
|
-
try { fs.symlinkSync(target, p); } catch {}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
} catch {}
|
|
119
|
-
' "$PLUGINS_FILE" 2>/dev/null || true
|
|
120
|
-
`;
|
|
121
|
-
writeFileSync(hookPath, script, { mode: 0o755 });
|
|
122
|
-
return { healed: true, action: "global-hook", from: hookPath };
|
|
123
|
-
}
|
|
124
|
-
catch {
|
|
125
|
-
return { healed: false, action: "none" };
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Backward symlink: during postinstall, if the registry points to a
|
|
130
|
-
* non-existent OLD path, create a symlink from old → new (our directory).
|
|
131
|
-
* Same as healRegistryMismatch but called from postinstall context.
|
|
132
|
-
*/
|
|
133
|
-
export { healRegistryMismatch as healBackwardCompat };
|
|
134
|
-
/** One-shot flag for mid-session heal in server.ts */
|
|
135
|
-
let _midSessionHealed = false;
|
|
136
|
-
/**
|
|
137
|
-
* Mid-session heal — call on first MCP tool invocation.
|
|
138
|
-
* Checks if registry path differs from our running directory.
|
|
139
|
-
* Creates symlink if needed. Runs only once per process.
|
|
140
|
-
*/
|
|
141
|
-
export function healMidSession(currentDir) {
|
|
142
|
-
if (_midSessionHealed)
|
|
143
|
-
return { healed: false, action: "none" };
|
|
144
|
-
_midSessionHealed = true;
|
|
145
|
-
return healRegistryMismatch(currentDir);
|
|
146
|
-
}
|
|
147
|
-
/** Reset mid-session flag (for testing only) */
|
|
148
|
-
export function _resetMidSession() {
|
|
149
|
-
_midSessionHealed = false;
|
|
150
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic in-flight-capped worker pool.
|
|
3
|
-
*
|
|
4
|
-
* Used by:
|
|
5
|
-
* - runBatchCommands (ctx_batch_execute parallel branch)
|
|
6
|
-
* - runBatchFetch (ctx_fetch_and_index batch path)
|
|
7
|
-
*
|
|
8
|
-
* Returns Promise.allSettled-style results so one job's throw cannot
|
|
9
|
-
* strand siblings. Caller maps fulfilled/rejected per index. Output
|
|
10
|
-
* order is preserved by input index (not completion order).
|
|
11
|
-
*
|
|
12
|
-
* Designed to be the SINGLE concurrency primitive for the project —
|
|
13
|
-
* all "run N independent operations with at most M in flight" needs
|
|
14
|
-
* route here. Avoids the worker-pool copy-paste flagged in the
|
|
15
|
-
* concurrency PRD architectural review (finding G).
|
|
16
|
-
*/
|
|
17
|
-
export interface PoolJob<T> {
|
|
18
|
-
run(): Promise<T>;
|
|
19
|
-
}
|
|
20
|
-
export interface RunPoolOptions {
|
|
21
|
-
/** Hard concurrency cap (1-N). Auto-clamped to job count. */
|
|
22
|
-
concurrency: number;
|
|
23
|
-
/** Optional: also clamp by `os.cpus().length` (memory-pressure safety). Default false. */
|
|
24
|
-
capByCpuCount?: boolean;
|
|
25
|
-
/** Optional: per-settled callback (e.g. for progress reporting / metrics). */
|
|
26
|
-
onSettled?: (idx: number, result: PromiseSettledResult<unknown>) => void;
|
|
27
|
-
}
|
|
28
|
-
export interface RunPoolResult<T> {
|
|
29
|
-
/** Per-index settled result, ordered by input index. */
|
|
30
|
-
settled: PromiseSettledResult<T>[];
|
|
31
|
-
/** Concurrency actually used after all caps applied. */
|
|
32
|
-
effectiveConcurrency: number;
|
|
33
|
-
/** True when effectiveConcurrency < requested concurrency. */
|
|
34
|
-
capped: boolean;
|
|
35
|
-
}
|
|
36
|
-
export declare function runPool<T>(jobs: PoolJob<T>[], opts: RunPoolOptions): Promise<RunPoolResult<T>>;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Generic in-flight-capped worker pool.
|
|
3
|
-
*
|
|
4
|
-
* Used by:
|
|
5
|
-
* - runBatchCommands (ctx_batch_execute parallel branch)
|
|
6
|
-
* - runBatchFetch (ctx_fetch_and_index batch path)
|
|
7
|
-
*
|
|
8
|
-
* Returns Promise.allSettled-style results so one job's throw cannot
|
|
9
|
-
* strand siblings. Caller maps fulfilled/rejected per index. Output
|
|
10
|
-
* order is preserved by input index (not completion order).
|
|
11
|
-
*
|
|
12
|
-
* Designed to be the SINGLE concurrency primitive for the project —
|
|
13
|
-
* all "run N independent operations with at most M in flight" needs
|
|
14
|
-
* route here. Avoids the worker-pool copy-paste flagged in the
|
|
15
|
-
* concurrency PRD architectural review (finding G).
|
|
16
|
-
*/
|
|
17
|
-
import { cpus } from "node:os";
|
|
18
|
-
export async function runPool(jobs, opts) {
|
|
19
|
-
const { concurrency, capByCpuCount = false, onSettled } = opts;
|
|
20
|
-
if (jobs.length === 0) {
|
|
21
|
-
return { settled: [], effectiveConcurrency: 0, capped: false };
|
|
22
|
-
}
|
|
23
|
-
const requested = Math.max(1, concurrency);
|
|
24
|
-
const cpuCap = capByCpuCount ? Math.max(1, cpus().length) : requested;
|
|
25
|
-
const effectiveConcurrency = Math.min(requested, cpuCap, jobs.length);
|
|
26
|
-
const capped = effectiveConcurrency < requested;
|
|
27
|
-
const settled = new Array(jobs.length);
|
|
28
|
-
let nextIdx = 0;
|
|
29
|
-
async function worker() {
|
|
30
|
-
while (true) {
|
|
31
|
-
const idx = nextIdx++;
|
|
32
|
-
if (idx >= jobs.length)
|
|
33
|
-
return;
|
|
34
|
-
try {
|
|
35
|
-
const value = await jobs[idx].run();
|
|
36
|
-
settled[idx] = { status: "fulfilled", value };
|
|
37
|
-
}
|
|
38
|
-
catch (err) {
|
|
39
|
-
settled[idx] = { status: "rejected", reason: err };
|
|
40
|
-
}
|
|
41
|
-
onSettled?.(idx, settled[idx]);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
const workers = [];
|
|
45
|
-
for (let w = 0; w < effectiveConcurrency; w++)
|
|
46
|
-
workers.push(worker());
|
|
47
|
-
// allSettled defends against any promise rejection escaping a worker
|
|
48
|
-
// (the worker already swallows its own errors, but this is belt-and-braces).
|
|
49
|
-
await Promise.allSettled(workers);
|
|
50
|
-
return { settled, effectiveConcurrency, capped };
|
|
51
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenClaw MCP tool registry.
|
|
3
|
-
*
|
|
4
|
-
* Catalogs the 11 ctx_* tools that OpenClaw plugin must register via
|
|
5
|
-
* api.registerTool(...) so the routing block (which nudges agents toward
|
|
6
|
-
* ctx_execute, ctx_search, etc.) actually has tools to call. Without this,
|
|
7
|
-
* Phase 7 audit (v1.0.107-adapter-openclaw.json) flagged severity=CRITICAL —
|
|
8
|
-
* routing-block premise is broken when the named tools don't exist.
|
|
9
|
-
*
|
|
10
|
-
* Pattern mirrors the swarmvault MCP plugin
|
|
11
|
-
* (refs/plugin-examples/openclaw/swarmvault/packages/engine/src/mcp.ts:46-51):
|
|
12
|
-
* server.registerTool(name, { description, inputSchema }, handler)
|
|
13
|
-
*
|
|
14
|
-
* OpenClaw signature is slightly different — see building-plugins.md:116
|
|
15
|
-
* api.registerTool({ name, description, parameters: TypeBox, execute(id, params) })
|
|
16
|
-
*
|
|
17
|
-
* Tool handlers are intentionally thin shims that delegate to the bundled CLI
|
|
18
|
-
* (cli.bundle.mjs) — same fall-through pattern already used by ctx-doctor and
|
|
19
|
-
* ctx-upgrade slash commands. This keeps the plugin's blast radius minimal:
|
|
20
|
-
* we don't re-export the entire MCP server stack inside OpenClaw's process.
|
|
21
|
-
*
|
|
22
|
-
* The 11 tools mirror src/server.ts registerTool calls (lines 897, 1226, 1371,
|
|
23
|
-
* 1497, 2034, 2256, 2440, 2501, 2592, 2712, 2808).
|
|
24
|
-
*/
|
|
25
|
-
/** Minimal JSON-schema-like parameter spec accepted by OpenClaw registerTool. */
|
|
26
|
-
export interface OpenClawToolParameters {
|
|
27
|
-
type: "object";
|
|
28
|
-
properties: Record<string, {
|
|
29
|
-
type: string;
|
|
30
|
-
description?: string;
|
|
31
|
-
}>;
|
|
32
|
-
required?: string[];
|
|
33
|
-
additionalProperties?: boolean;
|
|
34
|
-
}
|
|
35
|
-
/** Tool definition shape returned to OpenClaw via api.registerTool. */
|
|
36
|
-
export interface OpenClawToolDef {
|
|
37
|
-
name: string;
|
|
38
|
-
description: string;
|
|
39
|
-
parameters: OpenClawToolParameters;
|
|
40
|
-
execute: (id: string, params: Record<string, unknown>) => Promise<{
|
|
41
|
-
content: Array<{
|
|
42
|
-
type: "text";
|
|
43
|
-
text: string;
|
|
44
|
-
}>;
|
|
45
|
-
}>;
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* The 11 ctx_* tool definitions registered into OpenClaw via api.registerTool.
|
|
49
|
-
* Names + descriptions mirror src/server.ts registerTool blocks 1:1 so prompts
|
|
50
|
-
* referencing them (routing block, AGENTS.md) resolve to real callable tools.
|
|
51
|
-
*/
|
|
52
|
-
export declare const OPENCLAW_TOOL_DEFS: readonly OpenClawToolDef[];
|
|
53
|
-
/** Stable list of tool names — used by tests and manifest validation. */
|
|
54
|
-
export declare const OPENCLAW_TOOL_NAMES: readonly string[];
|