context-mode 1.0.111 → 1.0.113
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/.openclaw-plugin/index.ts +3 -2
- package/.openclaw-plugin/openclaw.plugin.json +1 -1
- package/.openclaw-plugin/package.json +1 -1
- package/README.md +152 -34
- package/bin/statusline.mjs +144 -127
- package/build/adapters/base.d.ts +8 -5
- package/build/adapters/base.js +8 -18
- package/build/adapters/claude-code/index.d.ts +24 -3
- package/build/adapters/claude-code/index.js +44 -11
- package/build/adapters/codex/hooks.d.ts +10 -5
- package/build/adapters/codex/hooks.js +10 -5
- package/build/adapters/codex/index.d.ts +17 -5
- package/build/adapters/codex/index.js +337 -37
- package/build/adapters/codex/paths.d.ts +1 -0
- package/build/adapters/codex/paths.js +12 -0
- package/build/adapters/cursor/index.d.ts +6 -0
- package/build/adapters/cursor/index.js +83 -2
- package/build/adapters/detect.d.ts +1 -1
- package/build/adapters/detect.js +29 -6
- package/build/adapters/omp/index.d.ts +65 -0
- package/build/adapters/omp/index.js +182 -0
- package/build/adapters/omp/plugin.d.ts +75 -0
- package/build/adapters/omp/plugin.js +220 -0
- package/build/adapters/openclaw/mcp-tools.d.ts +54 -0
- package/build/adapters/openclaw/mcp-tools.js +198 -0
- package/build/adapters/openclaw/plugin.d.ts +130 -0
- package/build/adapters/openclaw/plugin.js +629 -0
- package/build/adapters/openclaw/workspace-router.d.ts +29 -0
- package/build/adapters/openclaw/workspace-router.js +64 -0
- package/build/adapters/opencode/plugin.d.ts +145 -0
- package/build/adapters/opencode/plugin.js +457 -0
- package/build/adapters/pi/extension.d.ts +26 -0
- package/build/adapters/pi/extension.js +552 -0
- package/build/adapters/pi/index.d.ts +57 -0
- package/build/adapters/pi/index.js +173 -0
- package/build/adapters/pi/mcp-bridge.d.ts +113 -0
- package/build/adapters/pi/mcp-bridge.js +251 -0
- package/build/adapters/types.d.ts +11 -6
- package/build/cli.js +186 -170
- package/build/db-base.d.ts +15 -2
- package/build/db-base.js +50 -5
- package/build/executor.d.ts +2 -0
- package/build/executor.js +15 -2
- package/build/runPool.d.ts +36 -0
- package/build/runPool.js +51 -0
- package/build/runtime.js +64 -5
- package/build/search/auto-memory.js +6 -4
- package/build/security.js +30 -10
- package/build/server.d.ts +23 -1
- package/build/server.js +662 -182
- package/build/session/analytics.d.ts +404 -1
- package/build/session/analytics.js +1347 -42
- package/build/session/db.d.ts +114 -5
- package/build/session/db.js +275 -27
- package/build/session/event-emit.d.ts +48 -0
- package/build/session/event-emit.js +101 -0
- package/build/session/extract.d.ts +1 -0
- package/build/session/extract.js +79 -12
- package/build/session/purge.d.ts +111 -0
- package/build/session/purge.js +138 -0
- package/build/store.d.ts +7 -0
- package/build/store.js +69 -6
- package/build/util/claude-config.d.ts +26 -0
- package/build/util/claude-config.js +91 -0
- package/build/util/hook-config.d.ts +4 -0
- package/build/util/hook-config.js +39 -0
- package/build/util/project-dir.d.ts +49 -0
- package/build/util/project-dir.js +67 -0
- package/cli.bundle.mjs +411 -208
- package/configs/antigravity/GEMINI.md +0 -3
- package/configs/claude-code/CLAUDE.md +1 -4
- package/configs/codex/AGENTS.md +1 -4
- package/configs/codex/config.toml +3 -0
- package/configs/codex/hooks.json +8 -0
- package/configs/cursor/context-mode.mdc +0 -3
- package/configs/gemini-cli/GEMINI.md +0 -3
- package/configs/jetbrains-copilot/copilot-instructions.md +0 -3
- package/configs/kilo/AGENTS.md +0 -3
- package/configs/kiro/KIRO.md +0 -3
- package/configs/omp/SYSTEM.md +85 -0
- package/configs/omp/mcp.json +7 -0
- package/configs/openclaw/AGENTS.md +0 -3
- package/configs/opencode/AGENTS.md +0 -3
- package/configs/pi/AGENTS.md +0 -3
- package/configs/qwen-code/QWEN.md +1 -4
- package/configs/vscode-copilot/copilot-instructions.md +0 -3
- package/configs/zed/AGENTS.md +0 -3
- package/hooks/codex/posttooluse.mjs +9 -2
- package/hooks/codex/precompact.mjs +69 -0
- package/hooks/codex/sessionstart.mjs +13 -9
- package/hooks/codex/stop.mjs +1 -2
- package/hooks/codex/userpromptsubmit.mjs +1 -2
- package/hooks/core/routing.mjs +237 -18
- package/hooks/cursor/afteragentresponse.mjs +1 -1
- package/hooks/cursor/hooks.json +31 -0
- package/hooks/cursor/posttooluse.mjs +1 -1
- package/hooks/cursor/sessionstart.mjs +5 -5
- package/hooks/cursor/stop.mjs +1 -1
- package/hooks/ensure-deps.mjs +12 -13
- package/hooks/gemini-cli/aftertool.mjs +1 -1
- package/hooks/gemini-cli/beforeagent.mjs +1 -1
- package/hooks/gemini-cli/precompress.mjs +3 -2
- package/hooks/gemini-cli/sessionstart.mjs +9 -9
- package/hooks/jetbrains-copilot/posttooluse.mjs +1 -1
- package/hooks/jetbrains-copilot/precompact.mjs +3 -2
- package/hooks/jetbrains-copilot/sessionstart.mjs +9 -9
- package/hooks/kiro/agentspawn.mjs +5 -5
- package/hooks/kiro/posttooluse.mjs +2 -2
- package/hooks/kiro/userpromptsubmit.mjs +1 -1
- package/hooks/posttooluse.mjs +45 -0
- package/hooks/precompact.mjs +17 -0
- package/hooks/pretooluse.mjs +23 -0
- package/hooks/routing-block.mjs +0 -12
- package/hooks/run-hook.mjs +16 -3
- package/hooks/session-db.bundle.mjs +27 -18
- package/hooks/session-extract.bundle.mjs +2 -2
- package/hooks/session-helpers.mjs +101 -64
- package/hooks/sessionstart.mjs +51 -2
- package/hooks/vscode-copilot/posttooluse.mjs +1 -1
- package/hooks/vscode-copilot/precompact.mjs +3 -2
- package/hooks/vscode-copilot/sessionstart.mjs +9 -9
- package/openclaw.plugin.json +1 -1
- package/package.json +14 -8
- package/server.bundle.mjs +349 -147
- package/start.mjs +16 -4
- package/skills/UPSTREAM-CREDITS.md +0 -51
- package/skills/context-mode-ops/SKILL.md +0 -299
- package/skills/context-mode-ops/agent-teams.md +0 -198
- package/skills/context-mode-ops/communication.md +0 -224
- package/skills/context-mode-ops/marketing.md +0 -124
- package/skills/context-mode-ops/release.md +0 -214
- package/skills/context-mode-ops/review-pr.md +0 -269
- package/skills/context-mode-ops/tdd.md +0 -329
- package/skills/context-mode-ops/triage-issue.md +0 -266
- package/skills/context-mode-ops/validation.md +0 -307
- package/skills/diagnose/SKILL.md +0 -122
- package/skills/diagnose/scripts/hitl-loop.template.sh +0 -41
- package/skills/grill-me/SKILL.md +0 -15
- package/skills/grill-with-docs/ADR-FORMAT.md +0 -47
- package/skills/grill-with-docs/CONTEXT-FORMAT.md +0 -77
- package/skills/grill-with-docs/SKILL.md +0 -93
- package/skills/improve-codebase-architecture/DEEPENING.md +0 -37
- package/skills/improve-codebase-architecture/INTERFACE-DESIGN.md +0 -44
- package/skills/improve-codebase-architecture/LANGUAGE.md +0 -53
- package/skills/improve-codebase-architecture/SKILL.md +0 -76
- package/skills/tdd/SKILL.md +0 -114
- package/skills/tdd/deep-modules.md +0 -33
- package/skills/tdd/interface-design.md +0 -31
- package/skills/tdd/mocking.md +0 -59
- package/skills/tdd/refactoring.md +0 -10
- package/skills/tdd/tests.md +0 -61
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenClaw TypeScript plugin entry point for context-mode.
|
|
3
|
+
*
|
|
4
|
+
* Exports an object with { id, name, configSchema, register(api) } for
|
|
5
|
+
* declarative metadata and config validation before code execution.
|
|
6
|
+
*
|
|
7
|
+
* register(api) registers:
|
|
8
|
+
* - before_tool_call hook — Routing enforcement (deny/modify/passthrough)
|
|
9
|
+
* - after_tool_call hook — Session event capture
|
|
10
|
+
* - command:new hook — Session initialization and cleanup
|
|
11
|
+
* - session_start hook — Re-key DB session to OpenClaw's session ID
|
|
12
|
+
* - before_compaction hook — Flush events to resume snapshot
|
|
13
|
+
* - after_compaction hook — Increment compact count
|
|
14
|
+
* - before_prompt_build (p=10) — Resume snapshot injection into system context
|
|
15
|
+
* - before_prompt_build (p=5) — Routing instruction injection into system context
|
|
16
|
+
* - context-mode engine — Context engine with compaction management
|
|
17
|
+
* - /ctx-stats command — Auto-reply command for session statistics
|
|
18
|
+
* - /ctx-doctor command — Auto-reply command for diagnostics
|
|
19
|
+
* - /ctx-upgrade command — Auto-reply command for upgrade
|
|
20
|
+
*
|
|
21
|
+
* Loaded by OpenClaw via: openclaw.extensions entry in package.json
|
|
22
|
+
*
|
|
23
|
+
* OpenClaw plugin paradigm:
|
|
24
|
+
* - Plugins export { id, name, configSchema, register(api) } for metadata
|
|
25
|
+
* - api.registerHook() for event-driven hooks
|
|
26
|
+
* - api.on() for typed lifecycle hooks
|
|
27
|
+
* - api.registerContextEngine() for compaction ownership
|
|
28
|
+
* - api.registerCommand() for auto-reply slash commands
|
|
29
|
+
* - Plugins run in-process with the Gateway (trusted code)
|
|
30
|
+
*/
|
|
31
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
32
|
+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
33
|
+
import { homedir } from "node:os";
|
|
34
|
+
import { dirname, join, resolve } from "node:path";
|
|
35
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
36
|
+
import { OpenClawSessionDB } from "./session-db.js";
|
|
37
|
+
import { extractEvents, extractUserEvents } from "../../session/extract.js";
|
|
38
|
+
import { buildResumeSnapshot } from "../../session/snapshot.js";
|
|
39
|
+
import { WorkspaceRouter } from "./workspace-router.js";
|
|
40
|
+
import { buildNodeCommand } from "../types.js";
|
|
41
|
+
import { OPENCLAW_TOOL_DEFS } from "./mcp-tools.js";
|
|
42
|
+
// ── System-reminder filter (CCv2 — SLICE OClaw-3) ─────────
|
|
43
|
+
// Mirror hooks/userpromptsubmit.mjs:30-33: skip system-generated wrappers
|
|
44
|
+
// so before_model_resolve never inserts spurious user-prompt events.
|
|
45
|
+
const SYSTEM_REMINDER_PREFIXES = [
|
|
46
|
+
"<system-reminder>",
|
|
47
|
+
"<task-notification>",
|
|
48
|
+
"<context_guidance>",
|
|
49
|
+
"<tool-result>",
|
|
50
|
+
];
|
|
51
|
+
function isSystemReminderMessage(msg) {
|
|
52
|
+
const trimmed = msg.trimStart();
|
|
53
|
+
for (const prefix of SYSTEM_REMINDER_PREFIXES) {
|
|
54
|
+
if (trimmed.startsWith(prefix))
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
/** Plugin config schema for OpenClaw validation. */
|
|
60
|
+
const configSchema = {
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: {
|
|
63
|
+
enabled: {
|
|
64
|
+
type: "boolean",
|
|
65
|
+
default: true,
|
|
66
|
+
description: "Enable or disable the context-mode plugin.",
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
additionalProperties: false,
|
|
70
|
+
};
|
|
71
|
+
// ── Helpers ───────────────────────────────────────────────
|
|
72
|
+
function getSessionDir() {
|
|
73
|
+
const dir = join(homedir(), ".openclaw", "context-mode", "sessions");
|
|
74
|
+
mkdirSync(dir, { recursive: true });
|
|
75
|
+
return dir;
|
|
76
|
+
}
|
|
77
|
+
function getDBPath(projectDir) {
|
|
78
|
+
const hash = createHash("sha256")
|
|
79
|
+
.update(projectDir)
|
|
80
|
+
.digest("hex")
|
|
81
|
+
.slice(0, 16);
|
|
82
|
+
return join(getSessionDir(), `${hash}.db`);
|
|
83
|
+
}
|
|
84
|
+
// ── Module-level DB singleton ─────────────────────────────
|
|
85
|
+
// Shared across all register() calls (one per agent session).
|
|
86
|
+
// Lazy-initialized on first register() using the first projectDir seen.
|
|
87
|
+
// Uses OpenClawSessionDB for session_key mapping and rename support.
|
|
88
|
+
let _dbSingleton = null;
|
|
89
|
+
function getOrCreateDB(projectDir) {
|
|
90
|
+
if (!_dbSingleton) {
|
|
91
|
+
const dbPath = getDBPath(projectDir);
|
|
92
|
+
_dbSingleton = new OpenClawSessionDB({ dbPath });
|
|
93
|
+
_dbSingleton.cleanupOldSessions(7);
|
|
94
|
+
}
|
|
95
|
+
return _dbSingleton;
|
|
96
|
+
}
|
|
97
|
+
// ── Module-level state for command handlers ───────────────
|
|
98
|
+
// Commands are re-registered on each register() call (OpenClaw's registerCommand
|
|
99
|
+
// is idempotent). These refs give handlers access to the current session's state.
|
|
100
|
+
let _latestDb = null;
|
|
101
|
+
let _latestSessionId = "";
|
|
102
|
+
let _latestPluginRoot = "";
|
|
103
|
+
// ── Plugin Definition (object export) ─────────────────────
|
|
104
|
+
/**
|
|
105
|
+
* OpenClaw plugin definition. The object form provides declarative metadata
|
|
106
|
+
* (id, name, configSchema) that OpenClaw can read without executing code.
|
|
107
|
+
* register() is called once per agent session with a fresh api object.
|
|
108
|
+
* Each call creates isolated closures (db, sessionId, hooks) — no shared state.
|
|
109
|
+
*/
|
|
110
|
+
export default {
|
|
111
|
+
id: "context-mode",
|
|
112
|
+
name: "Context Mode",
|
|
113
|
+
configSchema,
|
|
114
|
+
// OpenClaw calls register() synchronously — returning a Promise causes hooks
|
|
115
|
+
// to be silently ignored. Async init runs eagerly; hooks await it on first use.
|
|
116
|
+
register(api) {
|
|
117
|
+
// Resolve build dir from compiled JS location
|
|
118
|
+
const buildDir = dirname(fileURLToPath(import.meta.url));
|
|
119
|
+
const projectDir = process.cwd();
|
|
120
|
+
const pluginRoot = resolve(buildDir, "..", "..", "..");
|
|
121
|
+
// Structured logger — wraps api.logger, falls back to no-op.
|
|
122
|
+
// info/error always emit; debug only when api.logger.debug is present
|
|
123
|
+
// (i.e. OpenClaw running with --log-level debug or lower).
|
|
124
|
+
const log = {
|
|
125
|
+
info: (...args) => api.logger?.info("[context-mode]", ...args),
|
|
126
|
+
error: (...args) => api.logger?.error("[context-mode]", ...args),
|
|
127
|
+
debug: (...args) => api.logger?.debug?.("[context-mode]", ...args),
|
|
128
|
+
warn: (...args) => api.logger?.warn?.("[context-mode]", ...args),
|
|
129
|
+
};
|
|
130
|
+
// Get shared DB singleton (lazy-init on first register() call)
|
|
131
|
+
const db = getOrCreateDB(projectDir);
|
|
132
|
+
// Start with temp UUID — session_start will assign the real ID + sessionKey
|
|
133
|
+
let sessionId = randomUUID();
|
|
134
|
+
log.info("register() called, sessionId:", sessionId.slice(0, 8));
|
|
135
|
+
// SLICE OClaw-6 (F6 retraction): `resumeInjected` is correctly scoped
|
|
136
|
+
// per-register() singleton — Phase 7 confirmed F6 fabrication-as-tech-debt.
|
|
137
|
+
// Each OpenClaw agent session calls register() once and gets its own
|
|
138
|
+
// closure; the flag prevents double-injection of the resume snapshot in
|
|
139
|
+
// back-to-back before_prompt_build calls within the same session. Do not
|
|
140
|
+
// promote to module scope.
|
|
141
|
+
let resumeInjected = false;
|
|
142
|
+
let sessionKey;
|
|
143
|
+
// Create temp session so after_tool_call events before session_start have a valid row
|
|
144
|
+
db.ensureSession(sessionId, projectDir);
|
|
145
|
+
const workspaceRouter = new WorkspaceRouter();
|
|
146
|
+
// Async init: load routing module + dynamic routing-block factory.
|
|
147
|
+
// SLICE OClaw-2: replaced static readFileSync(configs/openclaw/AGENTS.md)
|
|
148
|
+
// with createRoutingBlock(createToolNamer("openclaw")) so OpenClaw-specific
|
|
149
|
+
// MCP-prefix substitution stays in lockstep with hooks/routing-block.mjs.
|
|
150
|
+
let routingInstructions = "";
|
|
151
|
+
const initPromise = (async () => {
|
|
152
|
+
const routingPath = resolve(buildDir, "..", "..", "..", "hooks", "core", "routing.mjs");
|
|
153
|
+
const routing = await import(pathToFileURL(routingPath).href);
|
|
154
|
+
// initSecurity() looks for `<dir>/security.js`, which lives at the
|
|
155
|
+
// top of build/ — two levels up from this adapter directory.
|
|
156
|
+
const buildRoot = resolve(buildDir, "..", "..");
|
|
157
|
+
await routing.initSecurity(buildRoot);
|
|
158
|
+
try {
|
|
159
|
+
const blockMod = await import(pathToFileURL(resolve(buildDir, "..", "..", "..", "hooks", "routing-block.mjs")).href);
|
|
160
|
+
const namingMod = await import(pathToFileURL(resolve(buildDir, "..", "..", "..", "hooks", "core", "tool-naming.mjs")).href);
|
|
161
|
+
const toolNamer = namingMod.createToolNamer("openclaw");
|
|
162
|
+
routingInstructions = blockMod.createRoutingBlock(toolNamer);
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
log.warn?.("failed to build dynamic routing block", err);
|
|
166
|
+
// Fallback: legacy disk-read of AGENTS.md (kept for resilience only —
|
|
167
|
+
// primary path is the dynamic factory above).
|
|
168
|
+
try {
|
|
169
|
+
const instructionsPath = resolve(buildDir, "..", "configs", "openclaw", "AGENTS.md");
|
|
170
|
+
if (existsSync(instructionsPath)) {
|
|
171
|
+
routingInstructions = readFileSync(instructionsPath, "utf-8");
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// best effort
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return { routing };
|
|
179
|
+
})();
|
|
180
|
+
// ── 1. tool_call:before — Routing enforcement ──────────
|
|
181
|
+
// NOTE: api.on() was broken in OpenClaw ≤2026.1.29 (fixed in PR #9761, issue #5513).
|
|
182
|
+
// api.on() is the correct API for typed lifecycle hooks (session_start, before_tool_call, etc.).
|
|
183
|
+
// api.registerHook() is for generic/command hooks (command:new, command:reset, command:stop).
|
|
184
|
+
api.on("before_tool_call", async (event) => {
|
|
185
|
+
const { routing } = await initPromise;
|
|
186
|
+
const e = event;
|
|
187
|
+
const toolName = e.toolName ?? "";
|
|
188
|
+
const toolInput = e.params ?? {};
|
|
189
|
+
let decision;
|
|
190
|
+
try {
|
|
191
|
+
decision = routing.routePreToolUse(toolName, toolInput, projectDir, "openclaw");
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return; // Routing failure → allow passthrough
|
|
195
|
+
}
|
|
196
|
+
if (!decision)
|
|
197
|
+
return; // No routing match → passthrough
|
|
198
|
+
log.debug("before_tool_call", { tool: toolName, action: decision.action });
|
|
199
|
+
if (decision.action === "deny" || decision.action === "ask") {
|
|
200
|
+
return {
|
|
201
|
+
block: true,
|
|
202
|
+
blockReason: decision.reason ?? "Blocked by context-mode",
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
if (decision.action === "modify" && decision.updatedInput) {
|
|
206
|
+
// In-place mutation — OpenClaw reads the mutated params object.
|
|
207
|
+
Object.assign(toolInput, decision.updatedInput);
|
|
208
|
+
}
|
|
209
|
+
// "context" action → handled by before_prompt_build, not inline
|
|
210
|
+
});
|
|
211
|
+
// ── 2. after_tool_call — Session event capture ─────────
|
|
212
|
+
// Map OpenClaw tool names → Claude Code equivalents so extractEvents
|
|
213
|
+
// can recognize them. OpenClaw uses lowercase names; CC uses PascalCase.
|
|
214
|
+
const OPENCLAW_TOOL_MAP = {
|
|
215
|
+
exec: "Bash",
|
|
216
|
+
read: "Read",
|
|
217
|
+
write: "Write",
|
|
218
|
+
edit: "Edit",
|
|
219
|
+
apply_patch: "Edit",
|
|
220
|
+
glob: "Glob",
|
|
221
|
+
grep: "Grep",
|
|
222
|
+
search: "Grep",
|
|
223
|
+
};
|
|
224
|
+
api.on("after_tool_call", async (event) => {
|
|
225
|
+
try {
|
|
226
|
+
const e = event;
|
|
227
|
+
const rawToolName = e.toolName ?? "";
|
|
228
|
+
const mappedToolName = OPENCLAW_TOOL_MAP[rawToolName] ?? rawToolName;
|
|
229
|
+
// Accept both result (v2+) and output (older builds)
|
|
230
|
+
const rawResult = e.result ?? e.output;
|
|
231
|
+
const resultStr = typeof rawResult === "string"
|
|
232
|
+
? rawResult
|
|
233
|
+
: rawResult != null
|
|
234
|
+
? JSON.stringify(rawResult)
|
|
235
|
+
: undefined;
|
|
236
|
+
// Accept both error (string, v2+) and isError (boolean, older builds)
|
|
237
|
+
const hasError = Boolean(e.error || e.isError);
|
|
238
|
+
const hookInput = {
|
|
239
|
+
tool_name: mappedToolName,
|
|
240
|
+
tool_input: e.params ?? {},
|
|
241
|
+
tool_response: resultStr,
|
|
242
|
+
tool_output: hasError ? { isError: true } : undefined,
|
|
243
|
+
};
|
|
244
|
+
const events = extractEvents(hookInput);
|
|
245
|
+
// Resolve agent-specific sessionId from workspace paths in params
|
|
246
|
+
const routedSessionId = workspaceRouter.resolveSessionId(e.params ?? {}) ?? sessionId;
|
|
247
|
+
if (events.length > 0) {
|
|
248
|
+
for (const ev of events) {
|
|
249
|
+
db.insertEvent(routedSessionId, ev, "PostToolUse");
|
|
250
|
+
}
|
|
251
|
+
log.debug("after_tool_call", { tool: rawToolName, mapped: mappedToolName, sessionId: routedSessionId.slice(0, 8), events: events.length, durationMs: e.durationMs });
|
|
252
|
+
}
|
|
253
|
+
else if (rawToolName) {
|
|
254
|
+
// Fallback: record any unrecognized tool call as a generic event
|
|
255
|
+
const data = JSON.stringify({
|
|
256
|
+
tool: rawToolName,
|
|
257
|
+
params: e.params,
|
|
258
|
+
durationMs: e.durationMs,
|
|
259
|
+
});
|
|
260
|
+
db.insertEvent(routedSessionId, {
|
|
261
|
+
type: "tool_call",
|
|
262
|
+
category: "openclaw",
|
|
263
|
+
data,
|
|
264
|
+
priority: 1,
|
|
265
|
+
data_hash: createHash("sha256")
|
|
266
|
+
.update(data)
|
|
267
|
+
.digest("hex")
|
|
268
|
+
.slice(0, 16),
|
|
269
|
+
}, "PostToolUse");
|
|
270
|
+
log.debug("after_tool_call", { tool: rawToolName, mapped: rawToolName, sessionId: routedSessionId.slice(0, 8), events: 1, durationMs: e.durationMs });
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
// Silent — session capture must never break the tool call
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
// ── 3. command:new — Session initialization ────────────
|
|
278
|
+
api.registerHook("command:new", async () => {
|
|
279
|
+
try {
|
|
280
|
+
log.debug("command:new", { sessionId: sessionId.slice(0, 8) });
|
|
281
|
+
db.cleanupOldSessions(7);
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
// best effort
|
|
285
|
+
}
|
|
286
|
+
}, {
|
|
287
|
+
name: "context-mode.session-new",
|
|
288
|
+
description: "Session initialization — cleans up old sessions on /new command",
|
|
289
|
+
});
|
|
290
|
+
// ── 3b. command:reset / command:stop — Session cleanup ────
|
|
291
|
+
api.registerHook("command:reset", async () => {
|
|
292
|
+
try {
|
|
293
|
+
log.debug("command:reset", { sessionId: sessionId.slice(0, 8) });
|
|
294
|
+
db.cleanupOldSessions(7);
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
// best effort
|
|
298
|
+
}
|
|
299
|
+
}, {
|
|
300
|
+
name: "context-mode.session-reset",
|
|
301
|
+
description: "Session cleanup on /reset command",
|
|
302
|
+
});
|
|
303
|
+
api.registerHook("command:stop", async () => {
|
|
304
|
+
try {
|
|
305
|
+
log.debug("command:stop", { sessionId: sessionId.slice(0, 8), sessionKey });
|
|
306
|
+
if (sessionKey) {
|
|
307
|
+
workspaceRouter.removeSession(sessionKey);
|
|
308
|
+
}
|
|
309
|
+
db.cleanupOldSessions(7);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
// best effort
|
|
313
|
+
}
|
|
314
|
+
}, {
|
|
315
|
+
name: "context-mode.session-stop",
|
|
316
|
+
description: "Session cleanup on /stop command",
|
|
317
|
+
});
|
|
318
|
+
// ── 4. session_start — Re-key DB session to OpenClaw's session ID ─
|
|
319
|
+
api.on("session_start", async (event) => {
|
|
320
|
+
try {
|
|
321
|
+
const e = event;
|
|
322
|
+
const sid = e?.sessionId;
|
|
323
|
+
if (!sid)
|
|
324
|
+
return;
|
|
325
|
+
const key = e?.sessionKey;
|
|
326
|
+
const resumedFrom = e?.resumedFrom;
|
|
327
|
+
log.debug("session_start", { sessionId: sid.slice(0, 8), sessionKey: key, resumedFrom });
|
|
328
|
+
if (key) {
|
|
329
|
+
// Per-agent session lookup via sessionKey
|
|
330
|
+
const prevId = db.getMostRecentSession(key);
|
|
331
|
+
if (prevId && prevId !== sid) {
|
|
332
|
+
db.renameSession(prevId, sid);
|
|
333
|
+
log.info(`session re-keyed ${prevId.slice(0, 8)}… → ${sid.slice(0, 8)}… (key=${key})`);
|
|
334
|
+
}
|
|
335
|
+
else if (!prevId) {
|
|
336
|
+
db.ensureSessionWithKey(sid, projectDir, key);
|
|
337
|
+
log.info(`new session ${sid.slice(0, 8)}… (key=${key})`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
// Fallback: no sessionKey → fresh session (Option A)
|
|
342
|
+
db.ensureSession(sid, projectDir);
|
|
343
|
+
log.info(`session ${sid.slice(0, 8)}… (no sessionKey — fallback)`);
|
|
344
|
+
}
|
|
345
|
+
sessionId = sid;
|
|
346
|
+
_latestSessionId = sessionId;
|
|
347
|
+
sessionKey = key;
|
|
348
|
+
if (key) {
|
|
349
|
+
workspaceRouter.registerSession(key, sessionId);
|
|
350
|
+
}
|
|
351
|
+
resumeInjected = false;
|
|
352
|
+
// Write routing instructions (AGENTS.md) now that we know the real
|
|
353
|
+
// workspace. Derive the workspace directory from the sessionKey so we
|
|
354
|
+
// only write into recognised /.openclaw/workspace* paths, never into
|
|
355
|
+
// the gateway's cwd or any other arbitrary directory.
|
|
356
|
+
}
|
|
357
|
+
catch {
|
|
358
|
+
// best effort — never break session start
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
// ── 5. before_compaction — Flush events to snapshot before compaction ─
|
|
362
|
+
// NOTE: OpenClaw compaction hooks were broken until #4967/#3728 fix.
|
|
363
|
+
// Adapter gracefully degrades — session recovery falls back to DB snapshot
|
|
364
|
+
// reconstruction when compaction events don't fire.
|
|
365
|
+
api.on("before_compaction", async () => {
|
|
366
|
+
try {
|
|
367
|
+
const sid = sessionId; // snapshot to avoid race with concurrent session_start
|
|
368
|
+
const allEvents = db.getEvents(sid);
|
|
369
|
+
log.debug("before_compaction", { sessionId: sid.slice(0, 8), events: allEvents.length });
|
|
370
|
+
if (allEvents.length === 0)
|
|
371
|
+
return;
|
|
372
|
+
const freshStats = db.getSessionStats(sid);
|
|
373
|
+
const snapshot = buildResumeSnapshot(allEvents, {
|
|
374
|
+
compactCount: (freshStats?.compact_count ?? 0) + 1,
|
|
375
|
+
});
|
|
376
|
+
db.upsertResume(sid, snapshot, allEvents.length);
|
|
377
|
+
}
|
|
378
|
+
catch {
|
|
379
|
+
// best effort — never break compaction
|
|
380
|
+
}
|
|
381
|
+
});
|
|
382
|
+
// ── 6. after_compaction — Increment compact count ─────
|
|
383
|
+
api.on("after_compaction", async () => {
|
|
384
|
+
try {
|
|
385
|
+
const sid = sessionId;
|
|
386
|
+
log.debug("after_compaction", { sessionId: sid.slice(0, 8) });
|
|
387
|
+
db.incrementCompactCount(sid); // sessionId consistent with before_compaction within same sync cycle
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
// best effort
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
// ── 7. before_model_resolve — User message capture ────────
|
|
394
|
+
api.on("before_model_resolve", async (event) => {
|
|
395
|
+
try {
|
|
396
|
+
const sid = sessionId; // snapshot to avoid race with concurrent session_start
|
|
397
|
+
const e = event;
|
|
398
|
+
const messageText = e?.userMessage ?? e?.message ?? e?.content ?? "";
|
|
399
|
+
log.debug("before_model_resolve", { hasMessage: !!messageText });
|
|
400
|
+
if (!messageText)
|
|
401
|
+
return;
|
|
402
|
+
// SLICE OClaw-3: skip system-generated wrappers so we never
|
|
403
|
+
// misclassify them as user prompts. Mirrors hooks/userpromptsubmit.mjs:30-33.
|
|
404
|
+
if (isSystemReminderMessage(messageText)) {
|
|
405
|
+
log.debug("before_model_resolve[skip-system-reminder]");
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const events = extractUserEvents(messageText);
|
|
409
|
+
for (const ev of events) {
|
|
410
|
+
db.insertEvent(sid, ev, "PostToolUse");
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
catch {
|
|
414
|
+
// best effort — never break model resolution
|
|
415
|
+
}
|
|
416
|
+
});
|
|
417
|
+
// ── 8. before_prompt_build — Resume snapshot injection ────
|
|
418
|
+
api.on("before_prompt_build", () => {
|
|
419
|
+
try {
|
|
420
|
+
const sid = sessionId; // snapshot to avoid race with concurrent session_start
|
|
421
|
+
const resume = db.getResume(sid);
|
|
422
|
+
log.debug("before_prompt_build[resume]", { sessionId: sid.slice(0, 8), hasResume: !!resume, injected: !resumeInjected });
|
|
423
|
+
if (resumeInjected)
|
|
424
|
+
return undefined;
|
|
425
|
+
if (!resume)
|
|
426
|
+
return undefined;
|
|
427
|
+
const freshStats = db.getSessionStats(sid);
|
|
428
|
+
if ((freshStats?.compact_count ?? 0) === 0)
|
|
429
|
+
return undefined;
|
|
430
|
+
resumeInjected = true;
|
|
431
|
+
return { prependSystemContext: resume.snapshot };
|
|
432
|
+
}
|
|
433
|
+
catch {
|
|
434
|
+
return undefined;
|
|
435
|
+
}
|
|
436
|
+
}, { priority: 10 });
|
|
437
|
+
// ── 8. before_prompt_build — Routing instruction injection ──
|
|
438
|
+
// SLICE OClaw-2: register unconditionally; routingInstructions is populated
|
|
439
|
+
// asynchronously by initPromise. The closure resolves the latest value at
|
|
440
|
+
// call-time, so the first prompt-build firing after dynamic-import resolution
|
|
441
|
+
// sees the dynamic ROUTING_BLOCK XML (matching hooks/routing-block.mjs).
|
|
442
|
+
api.on("before_prompt_build", () => {
|
|
443
|
+
if (!routingInstructions)
|
|
444
|
+
return undefined;
|
|
445
|
+
log.debug("before_prompt_build[routing]", { hasInstructions: !!routingInstructions });
|
|
446
|
+
// v1.0.107 — visible marker so OpenClaw users can verify the routing
|
|
447
|
+
// block reached the model (Mickey-class verification path; mirrors
|
|
448
|
+
// OpenCode + Pi adapters).
|
|
449
|
+
const marker = `<!-- context-mode: routing block injected (sessionID=${String(sessionId).slice(0, 8)}) -->`;
|
|
450
|
+
return { appendSystemContext: marker + "\n" + routingInstructions };
|
|
451
|
+
}, { priority: 5 });
|
|
452
|
+
// ── 8b. registerTool — Expose 11 ctx_* tools (SLICE OClaw-1) ────
|
|
453
|
+
// Phase 7 audit (v1.0.107-adapter-openclaw.json) flagged severity=CRITICAL:
|
|
454
|
+
// routing block tells agents to call ctx_execute / ctx_search / etc. but
|
|
455
|
+
// nothing called api.registerTool, so the tools didn't exist in the
|
|
456
|
+
// OpenClaw session. This loop fixes that — mirrors swarmvault MCP pattern
|
|
457
|
+
// (refs/plugin-examples/openclaw/swarmvault/packages/engine/src/mcp.ts:46-51).
|
|
458
|
+
if (api.registerTool) {
|
|
459
|
+
for (const def of OPENCLAW_TOOL_DEFS) {
|
|
460
|
+
try {
|
|
461
|
+
api.registerTool(def);
|
|
462
|
+
}
|
|
463
|
+
catch (err) {
|
|
464
|
+
log.warn?.("registerTool failed", { name: def.name }, err);
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
log.debug("registerTool[ctx_*]", { count: OPENCLAW_TOOL_DEFS.length });
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
log.warn?.("api.registerTool unavailable — ctx_* tools not exposed in this OpenClaw build");
|
|
471
|
+
}
|
|
472
|
+
// ── 8c. session_end — Finalize resume snapshot (SLICE OClaw-4) ───
|
|
473
|
+
// OpenClaw fires session_end at session lifecycle boundaries (per
|
|
474
|
+
// refs/platforms/openclaw/docs/plugins/hooks.md:110). We persist a final
|
|
475
|
+
// resume snapshot so a future session_start with resumedFrom can re-attach.
|
|
476
|
+
api.on("session_end", async () => {
|
|
477
|
+
try {
|
|
478
|
+
const sid = sessionId;
|
|
479
|
+
const allEvents = db.getEvents(sid);
|
|
480
|
+
log.debug("session_end", { sessionId: sid.slice(0, 8), events: allEvents.length });
|
|
481
|
+
if (allEvents.length === 0)
|
|
482
|
+
return;
|
|
483
|
+
const freshStats = db.getSessionStats(sid);
|
|
484
|
+
const snapshot = buildResumeSnapshot(allEvents, {
|
|
485
|
+
compactCount: freshStats?.compact_count ?? 0,
|
|
486
|
+
});
|
|
487
|
+
db.upsertResume(sid, snapshot, allEvents.length);
|
|
488
|
+
}
|
|
489
|
+
catch {
|
|
490
|
+
// best effort — never break session shutdown
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
// ── 8d. subagent_spawning — Inject routing block (SLICE OClaw-5) ─
|
|
494
|
+
// OpenClaw's subagent lifecycle (hooks.md:116) gives us a chance to seed
|
|
495
|
+
// every spawned subagent with the same routing block the parent agent
|
|
496
|
+
// sees. Without this, subagents have no MCP-routing guidance and degrade
|
|
497
|
+
// back to flooding the context with raw tool output.
|
|
498
|
+
api.on("subagent_spawning", (event) => {
|
|
499
|
+
try {
|
|
500
|
+
const e = (event ?? {});
|
|
501
|
+
const basePrompt = e?.input?.prompt ?? "";
|
|
502
|
+
if (!routingInstructions)
|
|
503
|
+
return undefined;
|
|
504
|
+
const newPrompt = basePrompt
|
|
505
|
+
? `${basePrompt}\n\n${routingInstructions}`
|
|
506
|
+
: routingInstructions;
|
|
507
|
+
log.debug("subagent_spawning[inject-routing]", {
|
|
508
|
+
basePromptLen: basePrompt.length,
|
|
509
|
+
blockLen: routingInstructions.length,
|
|
510
|
+
});
|
|
511
|
+
return { inputOverride: { ...(e.input ?? {}), prompt: newPrompt } };
|
|
512
|
+
}
|
|
513
|
+
catch {
|
|
514
|
+
return undefined;
|
|
515
|
+
}
|
|
516
|
+
});
|
|
517
|
+
// ── 9. Context engine — Compaction management ──────────
|
|
518
|
+
api.registerContextEngine("context-mode", () => ({
|
|
519
|
+
info: {
|
|
520
|
+
id: "context-mode",
|
|
521
|
+
name: "Context Mode",
|
|
522
|
+
ownsCompaction: false,
|
|
523
|
+
},
|
|
524
|
+
async ingest() {
|
|
525
|
+
return { ingested: true };
|
|
526
|
+
},
|
|
527
|
+
async assemble({ messages }) {
|
|
528
|
+
return { messages, estimatedTokens: 0 };
|
|
529
|
+
},
|
|
530
|
+
async compact() {
|
|
531
|
+
// No-op: session continuity is handled by before_compaction / after_compaction hooks.
|
|
532
|
+
// Returning ownsCompaction: false + compacted: false lets the host platform (OpenClaw)
|
|
533
|
+
// manage conversation truncation, preserving Anthropic thinking/redacted_thinking blocks.
|
|
534
|
+
// See: https://github.com/mksglu/context-mode/issues/191
|
|
535
|
+
return { ok: true, compacted: false };
|
|
536
|
+
},
|
|
537
|
+
}));
|
|
538
|
+
// ── 10. Auto-reply commands — ctx slash commands ──────
|
|
539
|
+
// Update module-level refs so command handlers (registered once) always
|
|
540
|
+
// read the latest session's db/sessionId/pluginRoot.
|
|
541
|
+
_latestDb = db;
|
|
542
|
+
_latestSessionId = sessionId;
|
|
543
|
+
_latestPluginRoot = pluginRoot;
|
|
544
|
+
if (api.registerCommand) {
|
|
545
|
+
api.registerCommand({
|
|
546
|
+
name: "ctx-stats",
|
|
547
|
+
description: "Show context-mode session statistics",
|
|
548
|
+
handler: () => {
|
|
549
|
+
const text = buildStatsText(_latestDb, _latestSessionId);
|
|
550
|
+
return { text };
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
api.registerCommand({
|
|
554
|
+
name: "ctx-doctor",
|
|
555
|
+
description: "Run context-mode diagnostics",
|
|
556
|
+
handler: () => {
|
|
557
|
+
const bundlePath = resolve(_latestPluginRoot, "cli.bundle.mjs");
|
|
558
|
+
const fallbackPath = resolve(_latestPluginRoot, "build", "cli.js");
|
|
559
|
+
const cliPath = existsSync(bundlePath) ? bundlePath : fallbackPath;
|
|
560
|
+
const cmd = `${buildNodeCommand(cliPath)} doctor`;
|
|
561
|
+
return {
|
|
562
|
+
text: [
|
|
563
|
+
"## ctx-doctor",
|
|
564
|
+
"",
|
|
565
|
+
"Run this command to diagnose context-mode:",
|
|
566
|
+
"",
|
|
567
|
+
"```",
|
|
568
|
+
cmd,
|
|
569
|
+
"```",
|
|
570
|
+
].join("\n"),
|
|
571
|
+
};
|
|
572
|
+
},
|
|
573
|
+
});
|
|
574
|
+
api.registerCommand({
|
|
575
|
+
name: "ctx-upgrade",
|
|
576
|
+
description: "Upgrade context-mode to the latest version",
|
|
577
|
+
handler: () => {
|
|
578
|
+
const bundlePath = resolve(_latestPluginRoot, "cli.bundle.mjs");
|
|
579
|
+
const fallbackPath = resolve(_latestPluginRoot, "build", "cli.js");
|
|
580
|
+
const cliPath = existsSync(bundlePath) ? bundlePath : fallbackPath;
|
|
581
|
+
const cmd = `${buildNodeCommand(cliPath)} upgrade`;
|
|
582
|
+
return {
|
|
583
|
+
text: [
|
|
584
|
+
"## ctx-upgrade",
|
|
585
|
+
"",
|
|
586
|
+
"Run this command to upgrade context-mode:",
|
|
587
|
+
"",
|
|
588
|
+
"```",
|
|
589
|
+
cmd,
|
|
590
|
+
"```",
|
|
591
|
+
"",
|
|
592
|
+
"Restart your session after upgrade.",
|
|
593
|
+
].join("\n"),
|
|
594
|
+
};
|
|
595
|
+
},
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
},
|
|
599
|
+
};
|
|
600
|
+
// ── Stats helper ──────────────────────────────────────────
|
|
601
|
+
function buildStatsText(db, sessionId) {
|
|
602
|
+
try {
|
|
603
|
+
const events = db.getEvents(sessionId);
|
|
604
|
+
const stats = db.getSessionStats(sessionId);
|
|
605
|
+
const lines = [
|
|
606
|
+
"## context-mode stats",
|
|
607
|
+
"",
|
|
608
|
+
`- Session: \`${sessionId.slice(0, 8)}…\``,
|
|
609
|
+
`- Events captured: ${events.length}`,
|
|
610
|
+
`- Compactions: ${stats?.compact_count ?? 0}`,
|
|
611
|
+
];
|
|
612
|
+
// Summarize events by type
|
|
613
|
+
const byType = {};
|
|
614
|
+
for (const ev of events) {
|
|
615
|
+
const key = ev.type ?? "unknown";
|
|
616
|
+
byType[key] = (byType[key] ?? 0) + 1;
|
|
617
|
+
}
|
|
618
|
+
if (Object.keys(byType).length > 0) {
|
|
619
|
+
lines.push("- Event breakdown:");
|
|
620
|
+
for (const [type, count] of Object.entries(byType)) {
|
|
621
|
+
lines.push(` - ${type}: ${count}`);
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
return lines.join("\n");
|
|
625
|
+
}
|
|
626
|
+
catch {
|
|
627
|
+
return "context-mode stats unavailable (session DB error)";
|
|
628
|
+
}
|
|
629
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract the agent workspace path from tool call params.
|
|
3
|
+
* Looks for /openclaw/workspace-<name> patterns in cwd, file_path, and command.
|
|
4
|
+
* Returns the workspace root (e.g. "/openclaw/workspace-trainer") or null.
|
|
5
|
+
*/
|
|
6
|
+
export declare function extractWorkspace(params: Record<string, unknown>): string | null;
|
|
7
|
+
/**
|
|
8
|
+
* Maps agent workspaces to sessionIds using sessionKey convention.
|
|
9
|
+
* sessionKey pattern: "agent:<name>:main" → workspace "/openclaw/workspace-<name>"
|
|
10
|
+
*
|
|
11
|
+
* Why this exists alongside per-session closures:
|
|
12
|
+
* Each register() call creates its own closure with its own sessionId, which
|
|
13
|
+
* naturally isolates sessions. The WorkspaceRouter acts as a safety net for
|
|
14
|
+
* after_tool_call events where OpenClaw may deliver the event to the wrong
|
|
15
|
+
* closure (e.g. tool calls interleaving across agents). It resolves the correct
|
|
16
|
+
* sessionId from workspace paths in tool params, falling back to the closure
|
|
17
|
+
* sessionId when no workspace is detected.
|
|
18
|
+
*/
|
|
19
|
+
export declare class WorkspaceRouter {
|
|
20
|
+
private map;
|
|
21
|
+
/** Register a session from session_start event. */
|
|
22
|
+
registerSession(sessionKey: string, sessionId: string): void;
|
|
23
|
+
/** Remove a session (e.g. on command:stop). */
|
|
24
|
+
removeSession(sessionKey: string): void;
|
|
25
|
+
/** Resolve sessionId from tool call params. Returns null if no match. */
|
|
26
|
+
resolveSessionId(params: Record<string, unknown>): string | null;
|
|
27
|
+
/** Derive workspace path from sessionKey. */
|
|
28
|
+
private workspaceFromKey;
|
|
29
|
+
}
|