@myclaw163/clawclaw-cli 0.6.89 → 0.6.90
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/package.json +1 -1
- package/scripts/check_monitor.cjs +294 -106
- package/scripts/check_monitor.test.mjs +131 -53
- package/src/commands/game.ts +8 -5
package/package.json
CHANGED
|
@@ -1,28 +1,53 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* Diagnostic: is the built-in Monitor tool available to Claude Code?
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* mirrors the same gate the client compiles to (see enable_monitor.cjs header):
|
|
5
|
+
* Three tiers, best-to-worst signal. First conclusive tier wins.
|
|
7
6
|
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* Tier A — GROUND TRUTH via subprocess (default, authoritative)
|
|
8
|
+
* Spawn `claude -p "hi" --max-turns 0 --output-format stream-json --verbose`
|
|
9
|
+
* and read the `system/init` event's `tools` array. `Monitor ∈ tools` is the
|
|
10
|
+
* real, resolved answer the client itself computes — it reflects every gate
|
|
11
|
+
* (feature flag, version, provider, platform, Git Bash) AND covers the case
|
|
12
|
+
* where a Claude Code version ships Monitor as a plain built-in tool with no
|
|
13
|
+
* feature flag. `--max-turns 0` means NO model turn runs (0 tokens); we read
|
|
14
|
+
* the init line and kill the child. Measured cost: ~4s cold, 0 tokens.
|
|
10
15
|
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
16
|
+
* Cache — the Tier A verdict is cached under a fingerprint of everything that
|
|
17
|
+
* can change it (claude version + platform + flag + provider/telemetry env +
|
|
18
|
+
* Windows bash). A cache hit returns in <5ms, so the ~4s probe is paid once
|
|
19
|
+
* per (version, config) and amortized to ~0 across repeated queue-joins.
|
|
20
|
+
* Cache also self-heals via a 24h TTL. Cached verdicts are probe-derived only.
|
|
21
|
+
*
|
|
22
|
+
* Tier B — CONFIG PROXY (fallback, only when the probe can't run: claude not on
|
|
23
|
+
* PATH, no auth, timeout, or an old build whose init has no `tools`). It
|
|
24
|
+
* reconstructs the compiled gate from disk:
|
|
25
|
+
* version >= 2.1.98 AND cachedGrowthBookFeatures.tengu_amber_sentinel
|
|
26
|
+
* AND provider is direct Anthropic (not Bedrock/Vertex/Foundry)
|
|
27
|
+
* AND neither DISABLE_TELEMETRY nor CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC
|
|
28
|
+
* AND (Windows) a real bash.exe is discoverable.
|
|
29
|
+
* KNOWN BLIND SPOT: the proxy hard-requires the feature flag, so it CANNOT
|
|
30
|
+
* cover "Monitor as a flag-free built-in" — it will report `off` there
|
|
31
|
+
* (a conservative false negative). Only Tier A covers that case.
|
|
32
|
+
* ENABLE_TOOL_SEARCH is NOT a gate (docs + binary confirm it only decides
|
|
33
|
+
* deferred-vs-built-in presentation); it is reported as advisory only.
|
|
34
|
+
*
|
|
35
|
+
* Version < 2.1.98 short-circuits to `off` with no probe (Monitor cannot exist
|
|
36
|
+
* per the official tools reference).
|
|
19
37
|
*
|
|
20
38
|
* Works on Linux, macOS, and Windows.
|
|
21
39
|
*
|
|
22
40
|
* Usage:
|
|
23
|
-
* node check_monitor.cjs
|
|
24
|
-
* node check_monitor.cjs --json
|
|
25
|
-
* node check_monitor.cjs --config
|
|
41
|
+
* node check_monitor.cjs # probe (+cache), fall back to proxy
|
|
42
|
+
* node check_monitor.cjs --json # machine-readable JSON on stdout
|
|
43
|
+
* node check_monitor.cjs --no-probe # config proxy only (no subprocess)
|
|
44
|
+
* node check_monitor.cjs --no-cache # probe but never read/write the cache
|
|
45
|
+
* node check_monitor.cjs --config <p> --settings <p> # override proxy inputs
|
|
46
|
+
* node check_monitor.cjs --cache-file <p> # override cache path
|
|
47
|
+
*
|
|
48
|
+
* Internal test hooks (not for production use):
|
|
49
|
+
* --assume-version <x.y.z> skip `claude --version`, use this value
|
|
50
|
+
* --fake-probe <on|off|fail> skip spawning claude, use a synthetic probe result
|
|
26
51
|
*
|
|
27
52
|
* Exit code: 0 when Monitor is available, 1 when it is not (or on input error).
|
|
28
53
|
*/
|
|
@@ -30,7 +55,8 @@
|
|
|
30
55
|
const fs = require("fs");
|
|
31
56
|
const path = require("path");
|
|
32
57
|
const os = require("os");
|
|
33
|
-
const
|
|
58
|
+
const crypto = require("crypto");
|
|
59
|
+
const { execSync, spawn } = require("child_process");
|
|
34
60
|
const { parseArgs } = require("node:util");
|
|
35
61
|
|
|
36
62
|
// Constants (kept in sync with enable_monitor.cjs)
|
|
@@ -42,7 +68,10 @@ const MIN_VERSION = [2, 1, 98];
|
|
|
42
68
|
const HOME = os.homedir();
|
|
43
69
|
const GHOST_PATH = path.join(HOME, ".claude.json");
|
|
44
70
|
const DEFAULT_SETTINGS_PATH = path.join(HOME, ".claude", "settings.json");
|
|
71
|
+
const DEFAULT_CACHE_PATH = path.join(HOME, ".clawclaw", "cache", "monitor-status.json");
|
|
45
72
|
const IS_WINDOWS = process.platform === "win32";
|
|
73
|
+
const PROBE_TIMEOUT_MS = 7000;
|
|
74
|
+
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
|
46
75
|
|
|
47
76
|
const DEFAULT_GIT_BASH_PATHS = [
|
|
48
77
|
"C:\\Program Files\\Git\\bin\\bash.exe",
|
|
@@ -56,6 +85,11 @@ const { values: args } = parseArgs({
|
|
|
56
85
|
json: { type: "boolean", default: false },
|
|
57
86
|
config: { type: "string", default: GHOST_PATH },
|
|
58
87
|
settings: { type: "string", default: DEFAULT_SETTINGS_PATH },
|
|
88
|
+
"cache-file": { type: "string", default: DEFAULT_CACHE_PATH },
|
|
89
|
+
"no-probe": { type: "boolean", default: false },
|
|
90
|
+
"no-cache": { type: "boolean", default: false },
|
|
91
|
+
"assume-version": { type: "string" },
|
|
92
|
+
"fake-probe": { type: "string" },
|
|
59
93
|
},
|
|
60
94
|
});
|
|
61
95
|
|
|
@@ -65,6 +99,7 @@ function expandHome(input) {
|
|
|
65
99
|
|
|
66
100
|
const configPath = expandHome(args.config);
|
|
67
101
|
const settingsPath = expandHome(args.settings);
|
|
102
|
+
const cachePath = expandHome(args["cache-file"]);
|
|
68
103
|
|
|
69
104
|
// Helpers
|
|
70
105
|
|
|
@@ -86,7 +121,11 @@ function loadJsonMaybe(filePath) {
|
|
|
86
121
|
}
|
|
87
122
|
|
|
88
123
|
function isToolSearchEnabled(value) {
|
|
89
|
-
|
|
124
|
+
// Accepts "true"/"1" and the auto-sizing forms "auto" / "auto:N". Advisory only
|
|
125
|
+
// — NOT part of the Monitor availability gate.
|
|
126
|
+
if (value === true) return true;
|
|
127
|
+
const s = String(value ?? "").trim().toLowerCase();
|
|
128
|
+
return s === "1" || s === "true" || s === "auto" || /^auto:\d+$/.test(s);
|
|
90
129
|
}
|
|
91
130
|
|
|
92
131
|
function truthyEnv(value) {
|
|
@@ -97,6 +136,10 @@ function truthyEnv(value) {
|
|
|
97
136
|
);
|
|
98
137
|
}
|
|
99
138
|
|
|
139
|
+
function sha256(input) {
|
|
140
|
+
return crypto.createHash("sha256").update(input).digest("hex");
|
|
141
|
+
}
|
|
142
|
+
|
|
100
143
|
function whichGit() {
|
|
101
144
|
try {
|
|
102
145
|
const cmd = IS_WINDOWS ? "where git" : "command -v git";
|
|
@@ -124,6 +167,10 @@ function findGitBash(env) {
|
|
|
124
167
|
|
|
125
168
|
/** Best-effort Claude Code version as [major, minor, patch] or null. */
|
|
126
169
|
function detectVersion() {
|
|
170
|
+
if (args["assume-version"]) {
|
|
171
|
+
const m = String(args["assume-version"]).match(/(\d+)\.(\d+)\.(\d+)/);
|
|
172
|
+
return m ? [Number(m[1]), Number(m[2]), Number(m[3])] : null;
|
|
173
|
+
}
|
|
127
174
|
try {
|
|
128
175
|
const out = execSync("claude --version", {
|
|
129
176
|
stdio: ["ignore", "pipe", "ignore"],
|
|
@@ -143,105 +190,246 @@ function gte(a, b) {
|
|
|
143
190
|
return true;
|
|
144
191
|
}
|
|
145
192
|
|
|
146
|
-
//
|
|
193
|
+
// Tier B: config proxy (reconstruct the compiled gate from disk)
|
|
194
|
+
|
|
195
|
+
function buildProxyChecks() {
|
|
196
|
+
const checks = [];
|
|
197
|
+
const record = (id, label, ok, detail, opts = {}) =>
|
|
198
|
+
checks.push({ id, label, ok, detail, advisory: !!opts.advisory });
|
|
199
|
+
|
|
200
|
+
// 1) Feature flag in ~/.claude.json (a hard gate for the proxy)
|
|
201
|
+
const cfg = loadJsonMaybe(configPath);
|
|
202
|
+
if (cfg.missing) {
|
|
203
|
+
record("flag", `${FLAG_KEY} 特性开关`, false, `未找到配置文件: ${configPath}`);
|
|
204
|
+
} else if (!cfg.ok) {
|
|
205
|
+
record("flag", `${FLAG_KEY} 特性开关`, false, `无法解析 ${configPath}: ${cfg.error}`);
|
|
206
|
+
} else {
|
|
207
|
+
const gb = (cfg.value && cfg.value.cachedGrowthBookFeatures) || {};
|
|
208
|
+
const on = gb[FLAG_KEY] === true;
|
|
209
|
+
record("flag", `${FLAG_KEY} 特性开关`, on,
|
|
210
|
+
on ? `${configPath} 中已启用` : `${configPath} 中缺失或非 true`);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const set = loadJsonMaybe(settingsPath);
|
|
214
|
+
const settingsEnv =
|
|
215
|
+
set.ok && set.value && typeof set.value.env === "object" && !Array.isArray(set.value.env)
|
|
216
|
+
? set.value.env
|
|
217
|
+
: {};
|
|
218
|
+
|
|
219
|
+
// 2) (Advisory) ENABLE_TOOL_SEARCH — presentation only, NOT an availability gate.
|
|
220
|
+
const tsVal = settingsEnv.ENABLE_TOOL_SEARCH ?? process.env.ENABLE_TOOL_SEARCH;
|
|
221
|
+
record("tool_search", "ENABLE_TOOL_SEARCH", true,
|
|
222
|
+
isToolSearchEnabled(tsVal)
|
|
223
|
+
? `= ${JSON.stringify(tsVal)} → deferred 工具 (经 ToolSearch 调用)`
|
|
224
|
+
: `未显式开启 (${JSON.stringify(tsVal ?? null)}) → 内置工具直接加载`,
|
|
225
|
+
{ advisory: true });
|
|
226
|
+
|
|
227
|
+
// 3) Windows bash.exe (hard gate on Windows only)
|
|
228
|
+
if (IS_WINDOWS) {
|
|
229
|
+
const bash = findGitBash(settingsEnv);
|
|
230
|
+
record("bash", "Windows bash.exe (Git Bash)", bash !== null,
|
|
231
|
+
bash ? bash : "未找到 bash.exe → Windows 上 Monitor 与 Bash 均被隐藏");
|
|
232
|
+
} else {
|
|
233
|
+
record("bash", "Windows bash.exe", true, `非 Windows (${process.platform}),无需 bash`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 4) Forbidden telemetry / traffic env vars (hard gate)
|
|
237
|
+
for (const key of FORBIDDEN_ENV) {
|
|
238
|
+
const inSettings = truthyEnv(settingsEnv[key]);
|
|
239
|
+
const inProcess = truthyEnv(process.env[key]);
|
|
240
|
+
const bad = inSettings || inProcess;
|
|
241
|
+
const where = inSettings ? "settings.json" : inProcess ? "环境变量" : null;
|
|
242
|
+
record(`env_${key}`, key, !bad, bad ? `已设 (${where}) → 禁用 Monitor` : "未设");
|
|
243
|
+
}
|
|
147
244
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
245
|
+
// 5) Version >= 2.1.98 and provider (hard gates per the official docs)
|
|
246
|
+
const ver = detectVersion();
|
|
247
|
+
if (ver) {
|
|
248
|
+
record("version", "Claude Code 版本", gte(ver, MIN_VERSION),
|
|
249
|
+
`${ver.join(".")} (需 ≥ ${MIN_VERSION.join(".")})`);
|
|
250
|
+
} else {
|
|
251
|
+
// Version unknown → don't fail on it; other gates decide.
|
|
252
|
+
record("version", "Claude Code 版本", true, "无法探测 (claude 不在 PATH),跳过版本闸", { advisory: true });
|
|
253
|
+
}
|
|
254
|
+
let prov = "";
|
|
255
|
+
if (truthyEnv(process.env.CLAUDE_CODE_USE_BEDROCK)) prov = "Bedrock";
|
|
256
|
+
else if (truthyEnv(process.env.CLAUDE_CODE_USE_VERTEX)) prov = "Vertex";
|
|
257
|
+
else if (truthyEnv(process.env.CLAUDE_CODE_USE_FOUNDRY)) prov = "Foundry";
|
|
258
|
+
record("provider", "模型 Provider", prov === "",
|
|
259
|
+
prov === "" ? "直连 Anthropic API" : `${prov} 不支持 Monitor`);
|
|
260
|
+
|
|
261
|
+
const hardFail = checks.filter((c) => !c.advisory && !c.ok);
|
|
262
|
+
return { checks, available: hardFail.length === 0, hardFail };
|
|
151
263
|
}
|
|
152
264
|
|
|
153
|
-
//
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
265
|
+
// Cache (probe-derived verdicts only)
|
|
266
|
+
|
|
267
|
+
function computeFingerprint(versionStr) {
|
|
268
|
+
const cfg = loadJsonMaybe(configPath);
|
|
269
|
+
const flag = cfg.ok ? (cfg.value?.cachedGrowthBookFeatures?.[FLAG_KEY] === true) : false;
|
|
270
|
+
const set = loadJsonMaybe(settingsPath);
|
|
271
|
+
const env =
|
|
272
|
+
set.ok && set.value && typeof set.value.env === "object" && !Array.isArray(set.value.env)
|
|
273
|
+
? set.value.env
|
|
274
|
+
: {};
|
|
275
|
+
const tel = FORBIDDEN_ENV.map((k) => (truthyEnv(env[k]) || truthyEnv(process.env[k]) ? 1 : 0)).join("");
|
|
276
|
+
const prov = [
|
|
277
|
+
process.env.CLAUDE_CODE_USE_BEDROCK,
|
|
278
|
+
process.env.CLAUDE_CODE_USE_VERTEX,
|
|
279
|
+
process.env.CLAUDE_CODE_USE_FOUNDRY,
|
|
280
|
+
].map((v) => (truthyEnv(v) ? 1 : 0)).join("");
|
|
281
|
+
const bash = IS_WINDOWS ? (findGitBash(env) || "none") : "n/a";
|
|
282
|
+
const ts = env.ENABLE_TOOL_SEARCH ?? process.env.ENABLE_TOOL_SEARCH ?? null;
|
|
283
|
+
const raw = JSON.stringify({ v: versionStr || "unknown", p: process.platform, flag, tel, prov, bash, ts });
|
|
284
|
+
return sha256(raw);
|
|
164
285
|
}
|
|
165
286
|
|
|
166
|
-
|
|
167
|
-
const
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
const tsVal = settingsEnv.ENABLE_TOOL_SEARCH ?? process.env.ENABLE_TOOL_SEARCH;
|
|
175
|
-
record("tool_search", "ENABLE_TOOL_SEARCH", isToolSearchEnabled(tsVal),
|
|
176
|
-
isToolSearchEnabled(tsVal)
|
|
177
|
-
? `= ${JSON.stringify(tsVal)}`
|
|
178
|
-
: `需为 "true"/"1" (当前: ${JSON.stringify(tsVal ?? null)})`);
|
|
179
|
-
|
|
180
|
-
// 3) Windows bash.exe
|
|
181
|
-
if (IS_WINDOWS) {
|
|
182
|
-
const bash = findGitBash(settingsEnv);
|
|
183
|
-
record("bash", "Windows bash.exe (Git Bash)", bash !== null,
|
|
184
|
-
bash ? bash : "未找到 bash.exe → Windows 上 Monitor 与 Bash 均被隐藏");
|
|
185
|
-
} else {
|
|
186
|
-
record("bash", "Windows bash.exe", true, `非 Windows (${process.platform}),无需 bash`);
|
|
287
|
+
function readCache(fingerprint) {
|
|
288
|
+
const c = loadJsonMaybe(cachePath);
|
|
289
|
+
if (!c.ok || !c.value) return null;
|
|
290
|
+
const { fingerprint: fp, status, ts } = c.value;
|
|
291
|
+
if (fp !== fingerprint) return null;
|
|
292
|
+
if (status !== "on" && status !== "off") return null;
|
|
293
|
+
if (typeof ts !== "number" || Date.now() - ts > CACHE_TTL_MS) return null;
|
|
294
|
+
return status;
|
|
187
295
|
}
|
|
188
296
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
297
|
+
function writeCache(fingerprint, status) {
|
|
298
|
+
try {
|
|
299
|
+
fs.mkdirSync(path.dirname(cachePath), { recursive: true });
|
|
300
|
+
const tmp = `${cachePath}.${process.pid}.tmp`;
|
|
301
|
+
fs.writeFileSync(tmp, `${JSON.stringify({ fingerprint, status, ts: Date.now(), method: "probe" }, null, 2)}\n`, "utf-8");
|
|
302
|
+
fs.renameSync(tmp, cachePath);
|
|
303
|
+
} catch (_) {
|
|
304
|
+
// Cache is best-effort; never let a write failure change the verdict.
|
|
305
|
+
}
|
|
197
306
|
}
|
|
198
307
|
|
|
199
|
-
//
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
308
|
+
// Tier A: ground-truth probe via `claude ... --output-format stream-json`
|
|
309
|
+
|
|
310
|
+
function runProbe(timeoutMs) {
|
|
311
|
+
const fake = args["fake-probe"];
|
|
312
|
+
if (fake) {
|
|
313
|
+
if (fake === "on") return Promise.resolve({ ok: true, monitor: true });
|
|
314
|
+
if (fake === "off") return Promise.resolve({ ok: true, monitor: false });
|
|
315
|
+
return Promise.resolve({ ok: false, reason: `fake:${fake}` });
|
|
316
|
+
}
|
|
317
|
+
return new Promise((resolve) => {
|
|
318
|
+
let child;
|
|
319
|
+
let done = false;
|
|
320
|
+
let buf = "";
|
|
321
|
+
const finish = (result) => {
|
|
322
|
+
if (done) return;
|
|
323
|
+
done = true;
|
|
324
|
+
clearTimeout(timer);
|
|
325
|
+
try { if (child) child.kill("SIGKILL"); } catch (_) {}
|
|
326
|
+
resolve(result);
|
|
327
|
+
};
|
|
328
|
+
const timer = setTimeout(() => finish({ ok: false, reason: "timeout" }), timeoutMs);
|
|
329
|
+
try {
|
|
330
|
+
child = spawn(
|
|
331
|
+
"claude",
|
|
332
|
+
["-p", "hi", "--max-turns", "0", "--output-format", "stream-json", "--verbose", "--no-session-persistence"],
|
|
333
|
+
{ stdio: ["ignore", "pipe", "ignore"] }
|
|
334
|
+
);
|
|
335
|
+
} catch (e) {
|
|
336
|
+
return finish({ ok: false, reason: `spawn_error:${e.message}` });
|
|
337
|
+
}
|
|
338
|
+
child.on("error", (e) => finish({ ok: false, reason: `spawn_error:${e.message}` }));
|
|
339
|
+
child.on("exit", () => finish({ ok: false, reason: "exited_no_init" }));
|
|
340
|
+
child.stdout.on("data", (d) => {
|
|
341
|
+
buf += d.toString();
|
|
342
|
+
let idx;
|
|
343
|
+
while ((idx = buf.indexOf("\n")) >= 0) {
|
|
344
|
+
const line = buf.slice(0, idx);
|
|
345
|
+
buf = buf.slice(idx + 1);
|
|
346
|
+
if (line.includes('"subtype":"init"')) {
|
|
347
|
+
try {
|
|
348
|
+
const j = JSON.parse(line);
|
|
349
|
+
const tools = Array.isArray(j.tools) ? j.tools : [];
|
|
350
|
+
return finish({ ok: true, monitor: tools.includes("Monitor") });
|
|
351
|
+
} catch (_) {
|
|
352
|
+
// Not the line we want yet; keep scanning.
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
});
|
|
206
358
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
configPath,
|
|
224
|
-
settingsPath,
|
|
225
|
-
checks: checks.map(({ id, ok, advisory, detail }) => ({ id, ok, advisory, detail })),
|
|
226
|
-
}, null, 2));
|
|
227
|
-
} else {
|
|
228
|
-
console.log("Claude Code Monitor 可用性检查 (只读)");
|
|
229
|
-
console.log("------------------------------------------------");
|
|
230
|
-
for (const c of checks) {
|
|
231
|
-
const mark = c.ok ? "✓" : c.advisory ? "!" : "✗";
|
|
232
|
-
const tag = c.advisory ? " (参考)" : "";
|
|
233
|
-
console.log(`${mark} ${c.label}${tag}: ${c.detail}`);
|
|
234
|
-
}
|
|
235
|
-
console.log("------------------------------------------------");
|
|
236
|
-
if (available && advisoryFail.length === 0) {
|
|
237
|
-
console.log("✅ 结论: Monitor 工具在当前环境【可用】");
|
|
238
|
-
} else if (available) {
|
|
239
|
-
console.log("✅ 结论: Monitor 工具在当前环境【可用】(有参考项未通过,见上方 ! 行)");
|
|
359
|
+
|
|
360
|
+
// Output
|
|
361
|
+
|
|
362
|
+
function emit({ available, method, monitor, fingerprint, proxy, probeReason }) {
|
|
363
|
+
if (args.json) {
|
|
364
|
+
console.log(JSON.stringify({
|
|
365
|
+
available,
|
|
366
|
+
method,
|
|
367
|
+
monitor: monitor ?? available,
|
|
368
|
+
platform: process.platform,
|
|
369
|
+
fingerprint,
|
|
370
|
+
probeReason,
|
|
371
|
+
configPath,
|
|
372
|
+
settingsPath,
|
|
373
|
+
checks: proxy ? proxy.checks.map(({ id, ok, advisory, detail }) => ({ id, ok, advisory, detail })) : undefined,
|
|
374
|
+
}, null, 2));
|
|
240
375
|
} else {
|
|
241
|
-
|
|
242
|
-
console.log(
|
|
243
|
-
console.log(
|
|
376
|
+
console.log("Claude Code Monitor 可用性检查");
|
|
377
|
+
console.log("------------------------------------------------");
|
|
378
|
+
console.log(`判定方式: ${method}${probeReason ? ` (probe: ${probeReason})` : ""}`);
|
|
379
|
+
if (proxy) {
|
|
380
|
+
for (const c of proxy.checks) {
|
|
381
|
+
const mark = c.ok ? "✓" : c.advisory ? "!" : "✗";
|
|
382
|
+
const tag = c.advisory ? " (参考)" : "";
|
|
383
|
+
console.log(`${mark} ${c.label}${tag}: ${c.detail}`);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
console.log("------------------------------------------------");
|
|
387
|
+
if (available) {
|
|
388
|
+
console.log("✅ 结论: Monitor 工具在当前环境【可用】");
|
|
389
|
+
} else {
|
|
390
|
+
const reasons = proxy && proxy.hardFail.length ? ` —— 未通过: ${proxy.hardFail.map((c) => c.label).join(", ")}` : "";
|
|
391
|
+
console.log(`❌ 结论: Monitor 工具【不可用】${reasons}`);
|
|
392
|
+
console.log(" 修复: 运行 `node scripts/enable_monitor.cjs` 自动开启。");
|
|
393
|
+
}
|
|
244
394
|
}
|
|
395
|
+
process.exit(available ? 0 : 1);
|
|
245
396
|
}
|
|
246
397
|
|
|
247
|
-
|
|
398
|
+
// Main
|
|
399
|
+
|
|
400
|
+
(async () => {
|
|
401
|
+
// Proxy-only mode (tests / explicit override): skip the subprocess entirely.
|
|
402
|
+
if (args["no-probe"]) {
|
|
403
|
+
const proxy = buildProxyChecks();
|
|
404
|
+
return emit({ available: proxy.available, method: "proxy", proxy });
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const ver = detectVersion();
|
|
408
|
+
const versionStr = ver ? ver.join(".") : null;
|
|
409
|
+
|
|
410
|
+
// Version floor: below 2.1.98 Monitor cannot exist (official docs). No probe.
|
|
411
|
+
if (ver && !gte(ver, MIN_VERSION)) {
|
|
412
|
+
if (!args["no-cache"]) writeCache(computeFingerprint(versionStr), "off");
|
|
413
|
+
return emit({ available: false, method: "version-floor", monitor: false, probeReason: `${versionStr} < ${MIN_VERSION.join(".")}` });
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Cache: a probe-derived verdict keyed by the full fingerprint.
|
|
417
|
+
const fingerprint = computeFingerprint(versionStr);
|
|
418
|
+
if (!args["no-cache"]) {
|
|
419
|
+
const cached = readCache(fingerprint);
|
|
420
|
+
if (cached) {
|
|
421
|
+
return emit({ available: cached === "on", method: "cache", monitor: cached === "on", fingerprint });
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Tier A: ground-truth probe.
|
|
426
|
+
const probe = await runProbe(PROBE_TIMEOUT_MS);
|
|
427
|
+
if (probe.ok) {
|
|
428
|
+
if (!args["no-cache"]) writeCache(fingerprint, probe.monitor ? "on" : "off");
|
|
429
|
+
return emit({ available: probe.monitor, method: "probe", monitor: probe.monitor, fingerprint });
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Tier B: config proxy fallback (probe could not run).
|
|
433
|
+
const proxy = buildProxyChecks();
|
|
434
|
+
return emit({ available: proxy.available, method: "proxy-fallback", proxy, probeReason: probe.reason });
|
|
435
|
+
})();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
3
|
import { tmpdir } from 'node:os';
|
|
4
4
|
import { dirname, join, resolve } from 'node:path';
|
|
5
5
|
import { afterEach, describe, expect, it } from 'vitest';
|
|
@@ -18,7 +18,7 @@ function writeJson(file, value) {
|
|
|
18
18
|
writeFileSync(file, `${JSON.stringify(value, null, 2)}\n`, 'utf8');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
// A config + settings pair that passes every gate on non-Windows.
|
|
21
|
+
// A config + settings pair that passes every proxy gate on non-Windows.
|
|
22
22
|
function writeGoodPair(root) {
|
|
23
23
|
const configPath = join(root, 'claude.json');
|
|
24
24
|
const settingsPath = join(root, '.claude', 'settings.json');
|
|
@@ -33,7 +33,7 @@ function runScript(args, extraEnv = {}) {
|
|
|
33
33
|
encoding: 'utf8',
|
|
34
34
|
env: {
|
|
35
35
|
...process.env,
|
|
36
|
-
// Neutralize
|
|
36
|
+
// Neutralize inherited state that would skew the checks/fingerprint.
|
|
37
37
|
CLAUDE_CODE_GIT_BASH_PATH: '',
|
|
38
38
|
ENABLE_TOOL_SEARCH: '',
|
|
39
39
|
DISABLE_TELEMETRY: '',
|
|
@@ -52,83 +52,161 @@ afterEach(() => {
|
|
|
52
52
|
}
|
|
53
53
|
});
|
|
54
54
|
|
|
55
|
-
//
|
|
56
|
-
// "available" assertions there so the suite is portable.
|
|
55
|
+
// Windows-only bash gate can't pass under this suite off Windows.
|
|
57
56
|
const describeNonWindows = process.platform === 'win32' ? describe.skip : describe;
|
|
58
57
|
|
|
59
|
-
describeNonWindows('check_monitor.cjs', () => {
|
|
60
|
-
it('reports available
|
|
61
|
-
const root = makeRoot('
|
|
58
|
+
describeNonWindows('check_monitor.cjs — Tier B config proxy (--no-probe)', () => {
|
|
59
|
+
it('reports available when flag + gates pass', () => {
|
|
60
|
+
const root = makeRoot('cm-proxy-ok-');
|
|
62
61
|
const { configPath, settingsPath } = writeGoodPair(root);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
expect(result.status).toBe(0);
|
|
67
|
-
expect(result.stdout).toContain('【可用】');
|
|
62
|
+
const r = runScript(['--no-probe', '--config', configPath, '--settings', settingsPath]);
|
|
63
|
+
expect(r.status).toBe(0);
|
|
64
|
+
expect(r.stdout).toContain('【可用】');
|
|
68
65
|
});
|
|
69
66
|
|
|
70
|
-
it('reports unavailable
|
|
71
|
-
const root = makeRoot('
|
|
67
|
+
it('reports unavailable when the feature flag is missing', () => {
|
|
68
|
+
const root = makeRoot('cm-proxy-noflag-');
|
|
72
69
|
const configPath = join(root, 'claude.json');
|
|
73
70
|
const settingsPath = join(root, '.claude', 'settings.json');
|
|
74
71
|
writeJson(configPath, { cachedGrowthBookFeatures: {} });
|
|
75
72
|
writeJson(settingsPath, { env: { ENABLE_TOOL_SEARCH: 'true' } });
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
expect(
|
|
80
|
-
expect(result.stdout).toContain('【不可用】');
|
|
81
|
-
expect(result.stdout).toContain('tengu_amber_sentinel');
|
|
73
|
+
const r = runScript(['--no-probe', '--config', configPath, '--settings', settingsPath]);
|
|
74
|
+
expect(r.status).toBe(1);
|
|
75
|
+
expect(r.stdout).toContain('【不可用】');
|
|
76
|
+
expect(r.stdout).toContain('tengu_amber_sentinel');
|
|
82
77
|
});
|
|
83
78
|
|
|
84
|
-
it('
|
|
85
|
-
const root = makeRoot('
|
|
79
|
+
it('is available when ENABLE_TOOL_SEARCH is off (tool search is advisory, not a gate)', () => {
|
|
80
|
+
const root = makeRoot('cm-proxy-nots-');
|
|
86
81
|
const configPath = join(root, 'claude.json');
|
|
87
82
|
const settingsPath = join(root, '.claude', 'settings.json');
|
|
88
83
|
writeJson(configPath, { cachedGrowthBookFeatures: { tengu_amber_sentinel: true } });
|
|
89
84
|
writeJson(settingsPath, { env: { ENABLE_TOOL_SEARCH: '0' } });
|
|
85
|
+
const r = runScript(['--no-probe', '--config', configPath, '--settings', settingsPath]);
|
|
86
|
+
expect(r.status).toBe(0);
|
|
87
|
+
expect(r.stdout).toContain('【可用】');
|
|
88
|
+
expect(r.stdout).toContain('内置工具');
|
|
89
|
+
});
|
|
90
90
|
|
|
91
|
-
|
|
91
|
+
it('reports unavailable when a forbidden telemetry env var is set', () => {
|
|
92
|
+
const root = makeRoot('cm-proxy-tel-');
|
|
93
|
+
const { configPath, settingsPath } = writeGoodPair(root);
|
|
94
|
+
const r = runScript(['--no-probe', '--config', configPath, '--settings', settingsPath],
|
|
95
|
+
{ DISABLE_TELEMETRY: '1' });
|
|
96
|
+
expect(r.status).toBe(1);
|
|
97
|
+
expect(r.stdout).toContain('DISABLE_TELEMETRY');
|
|
98
|
+
});
|
|
92
99
|
|
|
93
|
-
|
|
94
|
-
|
|
100
|
+
it('reports unavailable when the assumed version is below 2.1.98', () => {
|
|
101
|
+
const root = makeRoot('cm-proxy-oldver-');
|
|
102
|
+
const { configPath, settingsPath } = writeGoodPair(root);
|
|
103
|
+
const r = runScript(['--no-probe', '--assume-version', '2.1.50', '--config', configPath, '--settings', settingsPath]);
|
|
104
|
+
expect(r.status).toBe(1);
|
|
105
|
+
expect(r.stdout).toContain('版本');
|
|
95
106
|
});
|
|
96
107
|
|
|
97
|
-
it('
|
|
98
|
-
const root = makeRoot('
|
|
99
|
-
const configPath =
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
108
|
+
it('emits valid JSON and never spawns anything in --no-probe mode', () => {
|
|
109
|
+
const root = makeRoot('cm-proxy-json-');
|
|
110
|
+
const { configPath, settingsPath } = writeGoodPair(root);
|
|
111
|
+
const r = runScript(['--no-probe', '--json', '--config', configPath, '--settings', settingsPath]);
|
|
112
|
+
expect(r.status).toBe(0);
|
|
113
|
+
const parsed = JSON.parse(r.stdout);
|
|
114
|
+
expect(parsed.method).toBe('proxy');
|
|
115
|
+
expect(parsed.available).toBe(true);
|
|
116
|
+
expect(parsed.checks.find((c) => c.id === 'flag').ok).toBe(true);
|
|
117
|
+
expect(parsed.checks.find((c) => c.id === 'tool_search').advisory).toBe(true);
|
|
118
|
+
});
|
|
103
119
|
|
|
104
|
-
|
|
120
|
+
it('treats a missing config file as unavailable rather than crashing', () => {
|
|
121
|
+
const root = makeRoot('cm-proxy-missing-');
|
|
122
|
+
const r = runScript(['--no-probe', '--config', join(root, 'nope.json'), '--settings', join(root, 's.json')]);
|
|
123
|
+
expect(r.status).toBe(1);
|
|
124
|
+
expect(r.stderr).toBe('');
|
|
125
|
+
expect(r.stdout).toContain('【不可用】');
|
|
126
|
+
});
|
|
127
|
+
});
|
|
105
128
|
|
|
106
|
-
|
|
107
|
-
|
|
129
|
+
describeNonWindows('check_monitor.cjs — version floor', () => {
|
|
130
|
+
it('short-circuits to off (method version-floor) below 2.1.98 without probing', () => {
|
|
131
|
+
const root = makeRoot('cm-floor-');
|
|
132
|
+
const cacheFile = join(root, 'cache.json');
|
|
133
|
+
const r = runScript(['--json', '--assume-version', '2.1.50', '--cache-file', cacheFile]);
|
|
134
|
+
expect(r.status).toBe(1);
|
|
135
|
+
const parsed = JSON.parse(r.stdout);
|
|
136
|
+
expect(parsed.method).toBe('version-floor');
|
|
137
|
+
expect(parsed.available).toBe(false);
|
|
108
138
|
});
|
|
139
|
+
});
|
|
109
140
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
141
|
+
describeNonWindows('check_monitor.cjs — Tier A probe (--fake-probe)', () => {
|
|
142
|
+
it('reports available when the probe sees Monitor in the tool list', () => {
|
|
143
|
+
const root = makeRoot('cm-probe-on-');
|
|
144
|
+
const cacheFile = join(root, 'cache.json');
|
|
145
|
+
const r = runScript(['--json', '--fake-probe', 'on', '--assume-version', '2.1.193', '--cache-file', cacheFile]);
|
|
146
|
+
expect(r.status).toBe(0);
|
|
147
|
+
const parsed = JSON.parse(r.stdout);
|
|
148
|
+
expect(parsed.method).toBe('probe');
|
|
149
|
+
expect(parsed.available).toBe(true);
|
|
150
|
+
expect(existsSync(cacheFile)).toBe(true);
|
|
151
|
+
});
|
|
113
152
|
|
|
114
|
-
|
|
153
|
+
it('reports unavailable when the probe does NOT see Monitor', () => {
|
|
154
|
+
const root = makeRoot('cm-probe-off-');
|
|
155
|
+
const cacheFile = join(root, 'cache.json');
|
|
156
|
+
const r = runScript(['--json', '--fake-probe', 'off', '--assume-version', '2.1.193', '--cache-file', cacheFile]);
|
|
157
|
+
expect(r.status).toBe(1);
|
|
158
|
+
const parsed = JSON.parse(r.stdout);
|
|
159
|
+
expect(parsed.method).toBe('probe');
|
|
160
|
+
expect(parsed.available).toBe(false);
|
|
161
|
+
});
|
|
115
162
|
|
|
116
|
-
|
|
117
|
-
const
|
|
163
|
+
it('falls back to the config proxy when the probe cannot run', () => {
|
|
164
|
+
const root = makeRoot('cm-probe-fail-');
|
|
165
|
+
const { configPath, settingsPath } = writeGoodPair(root);
|
|
166
|
+
const cacheFile = join(root, 'cache.json');
|
|
167
|
+
const r = runScript(['--json', '--fake-probe', 'fail', '--assume-version', '2.1.193',
|
|
168
|
+
'--cache-file', cacheFile, '--config', configPath, '--settings', settingsPath]);
|
|
169
|
+
expect(r.status).toBe(0);
|
|
170
|
+
const parsed = JSON.parse(r.stdout);
|
|
171
|
+
expect(parsed.method).toBe('proxy-fallback');
|
|
118
172
|
expect(parsed.available).toBe(true);
|
|
119
|
-
|
|
120
|
-
expect(
|
|
173
|
+
// A failed probe must NOT be cached.
|
|
174
|
+
expect(existsSync(cacheFile)).toBe(false);
|
|
121
175
|
});
|
|
176
|
+
});
|
|
122
177
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
178
|
+
describeNonWindows('check_monitor.cjs — fingerprint cache', () => {
|
|
179
|
+
it('writes on first probe and serves a hit on the second run without probing', () => {
|
|
180
|
+
const root = makeRoot('cm-cache-');
|
|
181
|
+
const { configPath, settingsPath } = writeGoodPair(root);
|
|
182
|
+
const cacheFile = join(root, 'cache.json');
|
|
183
|
+
const common = ['--json', '--assume-version', '2.1.193', '--cache-file', cacheFile,
|
|
184
|
+
'--config', configPath, '--settings', settingsPath];
|
|
185
|
+
|
|
186
|
+
// First run: fake probe populates the cache.
|
|
187
|
+
const first = runScript([...common, '--fake-probe', 'on']);
|
|
188
|
+
expect(first.status).toBe(0);
|
|
189
|
+
expect(JSON.parse(first.stdout).method).toBe('probe');
|
|
190
|
+
const cache = JSON.parse(readFileSync(cacheFile, 'utf8'));
|
|
191
|
+
expect(cache.status).toBe('on');
|
|
192
|
+
|
|
193
|
+
// Second run WITHOUT --fake-probe: identical fingerprint → cache hit, no spawn.
|
|
194
|
+
const second = runScript(common);
|
|
195
|
+
expect(second.status).toBe(0);
|
|
196
|
+
const parsed = JSON.parse(second.stdout);
|
|
197
|
+
expect(parsed.method).toBe('cache');
|
|
198
|
+
expect(parsed.available).toBe(true);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('ignores a cache entry whose fingerprint no longer matches', () => {
|
|
202
|
+
const root = makeRoot('cm-cache-stale-');
|
|
203
|
+
const cacheFile = join(root, 'cache.json');
|
|
204
|
+
// Seed a stale entry with a bogus fingerprint; a fresh fake-probe-off must override it.
|
|
205
|
+
writeJson(cacheFile, { fingerprint: 'STALE', status: 'on', ts: Date.now(), method: 'probe' });
|
|
206
|
+
const r = runScript(['--json', '--fake-probe', 'off', '--assume-version', '2.1.193', '--cache-file', cacheFile]);
|
|
207
|
+
expect(r.status).toBe(1);
|
|
208
|
+
const parsed = JSON.parse(r.stdout);
|
|
209
|
+
expect(parsed.method).toBe('probe');
|
|
210
|
+
expect(parsed.available).toBe(false);
|
|
133
211
|
});
|
|
134
212
|
});
|
package/src/commands/game.ts
CHANGED
|
@@ -521,11 +521,14 @@ export const MONITOR_CHECK_SCRIPT = resolve(dirname(fileURLToPath(import.meta.ur
|
|
|
521
521
|
/**
|
|
522
522
|
* Detect whether the built-in Claude Code Monitor tool is available in this
|
|
523
523
|
* environment, for the `X-Monitor-Status` header on queue join. Runs the bundled
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
524
|
+
* diagnostic `scripts/check_monitor.cjs` (exit 0 = available → 'on', anything
|
|
525
|
+
* else = unavailable → 'off'). That script probes the real tool list once via a
|
|
526
|
+
* short `claude --max-turns 0` subprocess and caches the verdict by fingerprint,
|
|
527
|
+
* so the ~4s probe is paid once per (version, config) and later joins hit the
|
|
528
|
+
* cache. stdout is discarded so it never pollutes the NDJSON event stream, and
|
|
529
|
+
* any failure (missing script, crash, timeout) is treated as 'off' — the header
|
|
530
|
+
* must never block matchmaking. `scriptPath` is injectable for testing;
|
|
531
|
+
* production always uses the bundled default.
|
|
529
532
|
*/
|
|
530
533
|
export function detectMonitorStatus(scriptPath: string = MONITOR_CHECK_SCRIPT): 'on' | 'off' {
|
|
531
534
|
try {
|