@myclaw163/clawclaw-cli 0.6.80 → 0.6.82
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/personas//346/227/240/344/272/272/350/256/276.md +3 -0
- package/scripts/enable_monitor.cjs +677 -0
- package/scripts/enable_monitor.test.mjs +201 -0
- package/skills/clawclaw/SKILL.md +9 -8
- package/skills/clawclaw/references/CHATTERBOX.md +3 -3
- package/skills/clawclaw/references/CLAUDE-CODE-MONITOR.md +224 -0
- package/skills/clawclaw/references/COMMANDS.md +10 -17
- package/skills/clawclaw/references/GAME-MECHANICS.md +3 -3
- package/skills/clawclaw/references/TACTICS.md +4 -4
- package/src/cli.ts +3 -2
- package/src/commands/account.ts +12 -3
- package/src/commands/game.ts +3 -2
- package/src/commands/load.test.ts +14 -0
- package/src/commands/load.ts +10 -1
- package/src/lib/account-guidance.test.ts +32 -0
- package/src/lib/account-guidance.ts +62 -0
- package/src/lib/load-context.ts +2 -0
- package/src/lib/persona.test.ts +43 -0
- package/src/lib/persona.ts +17 -0
- package/src/runtime/ws-client.test.ts +44 -0
- package/src/runtime/ws-client.ts +3 -10
- package/src/strategies/game-utils.ts +2 -2
- package/src/strategies/goals/corpse-patrol-top.ts +1 -1
- package/src/strategies/paradise-fish.ts +1 -1
package/package.json
CHANGED
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Enable the built-in Monitor tool for Claude Code.
|
|
4
|
+
*
|
|
5
|
+
* Two independent requirements must BOTH be satisfied:
|
|
6
|
+
* 1. Feature flag `tengu_amber_sentinel` = true in ~/.claude.json
|
|
7
|
+
* (cachedGrowthBookFeatures) and ENABLE_TOOL_SEARCH="true" in settings.json
|
|
8
|
+
* (legacy "1" is also accepted).
|
|
9
|
+
* -> handled by inject() + validateAndFixSettingsStrict(), validated across envs.
|
|
10
|
+
* 2. On Windows, a real `bash.exe` must be discoverable, because the tool's
|
|
11
|
+
* gate compiles to roughly:
|
|
12
|
+
* isEnabled() = flag("tengu_amber_sentinel") && Au()
|
|
13
|
+
* Au() = platform!=="win32" ? true : findGitBash()!==null
|
|
14
|
+
* i.e. Monitor (and the Bash tool) stay hidden on Windows with no bash.
|
|
15
|
+
* -> handled by ensureGitBash(): mirror the binary's lookup, then pin
|
|
16
|
+
* CLAUDE_CODE_GIT_BASH_PATH when a non-default bash.exe is used.
|
|
17
|
+
*
|
|
18
|
+
* Works on Linux, macOS, and Windows. The bash step is a no-op off Windows.
|
|
19
|
+
*
|
|
20
|
+
* Usage:
|
|
21
|
+
* node enable_monitor.js # enable everything
|
|
22
|
+
* node enable_monitor.js --dry-run # preview changes only
|
|
23
|
+
* node enable_monitor.js --revert # restore the latest timestamped backup
|
|
24
|
+
* node enable_monitor.js --revert-backup <dir> # restore a specific backup directory
|
|
25
|
+
* node enable_monitor.js --git-bash-path <p> # use this bash.exe explicitly
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const fs = require("fs");
|
|
29
|
+
const path = require("path");
|
|
30
|
+
const os = require("os");
|
|
31
|
+
const crypto = require("crypto");
|
|
32
|
+
const { execSync } = require("child_process");
|
|
33
|
+
const { parseArgs } = require("node:util");
|
|
34
|
+
|
|
35
|
+
// Constants
|
|
36
|
+
|
|
37
|
+
const FLAG_KEY = "tengu_amber_sentinel";
|
|
38
|
+
const GIT_BASH_ENV = "CLAUDE_CODE_GIT_BASH_PATH";
|
|
39
|
+
const HOME = os.homedir();
|
|
40
|
+
const GHOST_PATH = path.join(HOME, ".claude.json");
|
|
41
|
+
const DEFAULT_SETTINGS_PATH = path.join(HOME, ".claude", "settings.json");
|
|
42
|
+
const IS_WINDOWS = process.platform === "win32";
|
|
43
|
+
const BACKUP_PREFIX = "enable-monitor";
|
|
44
|
+
|
|
45
|
+
// Default Git for Windows install locations probed by Claude Code's own lookup.
|
|
46
|
+
const DEFAULT_GIT_BASH_PATHS = [
|
|
47
|
+
"C:\\Program Files\\Git\\bin\\bash.exe",
|
|
48
|
+
"C:\\Program Files (x86)\\Git\\bin\\bash.exe",
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
// CLI
|
|
52
|
+
|
|
53
|
+
const { values: args } = parseArgs({
|
|
54
|
+
options: {
|
|
55
|
+
"dry-run": { type: "boolean", default: false },
|
|
56
|
+
revert: { type: "boolean", default: false },
|
|
57
|
+
"revert-backup": { type: "string" },
|
|
58
|
+
"git-bash-path": { type: "string" },
|
|
59
|
+
config: { type: "string", default: GHOST_PATH },
|
|
60
|
+
settings: { type: "string", default: DEFAULT_SETTINGS_PATH },
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
function expandHome(input) {
|
|
65
|
+
return path.resolve(String(input).replace(/^~(?=$|[\\/])/, HOME));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const configPath = expandHome(args.config);
|
|
69
|
+
const settingsPath = expandHome(args.settings);
|
|
70
|
+
|
|
71
|
+
// Helpers
|
|
72
|
+
|
|
73
|
+
const backupState = {
|
|
74
|
+
dir: null,
|
|
75
|
+
createdAt: null,
|
|
76
|
+
entries: [],
|
|
77
|
+
copiedKeys: new Set(),
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
function defaultBackupRoot() {
|
|
81
|
+
const appData = process.env.APPDATA?.trim();
|
|
82
|
+
if (appData) return path.resolve(appData, "clawclaw", "claude-config-backups");
|
|
83
|
+
return path.resolve(HOME, ".clawclaw", "claude-config-backups");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function pad2(n) {
|
|
87
|
+
return String(n).padStart(2, "0");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function formatTimestamp(date = new Date()) {
|
|
91
|
+
return (
|
|
92
|
+
`${date.getUTCFullYear()}${pad2(date.getUTCMonth() + 1)}${pad2(date.getUTCDate())}` +
|
|
93
|
+
`-${pad2(date.getUTCHours())}${pad2(date.getUTCMinutes())}${pad2(date.getUTCSeconds())}Z`
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function chooseUniqueDir(baseDir) {
|
|
98
|
+
if (!fs.existsSync(baseDir)) return baseDir;
|
|
99
|
+
for (let i = 1; i < 1000; i += 1) {
|
|
100
|
+
const candidate = `${baseDir}-${i}`;
|
|
101
|
+
if (!fs.existsSync(candidate)) return candidate;
|
|
102
|
+
}
|
|
103
|
+
throw new Error(`Unable to choose a unique backup directory under ${path.dirname(baseDir)}`);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function ensureBackupDir() {
|
|
107
|
+
if (backupState.dir) return backupState.dir;
|
|
108
|
+
const root = defaultBackupRoot();
|
|
109
|
+
const backupDir = chooseUniqueDir(path.join(root, `${BACKUP_PREFIX}-${formatTimestamp()}`));
|
|
110
|
+
fs.mkdirSync(backupDir, { recursive: true });
|
|
111
|
+
backupState.dir = backupDir;
|
|
112
|
+
backupState.createdAt = new Date().toISOString();
|
|
113
|
+
writeBackupManifest();
|
|
114
|
+
return backupDir;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function sha256File(filePath) {
|
|
118
|
+
return crypto.createHash("sha256").update(fs.readFileSync(filePath)).digest("hex");
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function writeBackupManifest() {
|
|
122
|
+
if (!backupState.dir) return;
|
|
123
|
+
const manifest = {
|
|
124
|
+
createdAt: backupState.createdAt,
|
|
125
|
+
purpose: "enable-monitor",
|
|
126
|
+
configPath,
|
|
127
|
+
settingsPath,
|
|
128
|
+
entries: backupState.entries,
|
|
129
|
+
};
|
|
130
|
+
fs.writeFileSync(
|
|
131
|
+
path.join(backupState.dir, "manifest.json"),
|
|
132
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
133
|
+
"utf-8"
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function backupBeforeWrite(filePath, backupName) {
|
|
138
|
+
if (args["dry-run"]) {
|
|
139
|
+
throw new Error("backupBeforeWrite must not be called during dry-run");
|
|
140
|
+
}
|
|
141
|
+
const absPath = path.resolve(filePath);
|
|
142
|
+
const key = absPath.toLowerCase();
|
|
143
|
+
if (backupState.copiedKeys.has(key)) return;
|
|
144
|
+
|
|
145
|
+
const backupDir = ensureBackupDir();
|
|
146
|
+
const backupPath = path.join(backupDir, backupName);
|
|
147
|
+
const missingPath = path.join(backupDir, `${backupName}.missing.txt`);
|
|
148
|
+
|
|
149
|
+
if (fs.existsSync(absPath)) {
|
|
150
|
+
fs.copyFileSync(absPath, backupPath);
|
|
151
|
+
backupState.entries.push({
|
|
152
|
+
sourcePath: absPath,
|
|
153
|
+
backupFile: backupName,
|
|
154
|
+
existed: true,
|
|
155
|
+
sha256: sha256File(absPath),
|
|
156
|
+
});
|
|
157
|
+
} else {
|
|
158
|
+
fs.writeFileSync(missingPath, `MISSING: ${absPath}\n`, "utf-8");
|
|
159
|
+
backupState.entries.push({
|
|
160
|
+
sourcePath: absPath,
|
|
161
|
+
backupFile: `${backupName}.missing.txt`,
|
|
162
|
+
existed: false,
|
|
163
|
+
sha256: null,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
backupState.copiedKeys.add(key);
|
|
168
|
+
writeBackupManifest();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function loadConfig(filePath) {
|
|
172
|
+
let raw;
|
|
173
|
+
try {
|
|
174
|
+
raw = fs.readFileSync(filePath, "utf-8");
|
|
175
|
+
} catch (e) {
|
|
176
|
+
throw new Error(`Failed to read ${filePath}: ${e.message}`);
|
|
177
|
+
}
|
|
178
|
+
if (raw.charCodeAt(0) === 0xfeff) raw = raw.slice(1);
|
|
179
|
+
try {
|
|
180
|
+
return JSON.parse(raw);
|
|
181
|
+
} catch (e) {
|
|
182
|
+
throw new Error(`Invalid JSON in ${filePath}: ${e.message}`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function detectJsonIndent(raw) {
|
|
187
|
+
const match = raw.match(/\n([ \t]+)"/);
|
|
188
|
+
return match ? match[1] : 2;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function detectEol(raw) {
|
|
192
|
+
return raw.includes("\r\n") ? "\r\n" : "\n";
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function formatJsonLikeOriginal(config, originalRaw) {
|
|
196
|
+
const hasBom = originalRaw !== null && originalRaw.charCodeAt(0) === 0xfeff;
|
|
197
|
+
const styleRaw = hasBom ? originalRaw.slice(1) : originalRaw;
|
|
198
|
+
const indent = styleRaw === null ? 2 : detectJsonIndent(styleRaw);
|
|
199
|
+
const eol = styleRaw === null ? "\n" : detectEol(styleRaw);
|
|
200
|
+
let data = JSON.stringify(config, null, indent);
|
|
201
|
+
if (eol !== "\n") data = data.replace(/\n/g, eol);
|
|
202
|
+
if (styleRaw === null || styleRaw.endsWith("\n") || styleRaw.endsWith("\r")) {
|
|
203
|
+
data += eol;
|
|
204
|
+
}
|
|
205
|
+
if (hasBom) data = "\ufeff" + data;
|
|
206
|
+
return data;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Atomically write JSON to a file: write to a temp file, then os.replace().
|
|
211
|
+
* Preserves file permissions on Unix (best-effort).
|
|
212
|
+
*/
|
|
213
|
+
function saveConfig(filePath, config) {
|
|
214
|
+
let originalRaw = null;
|
|
215
|
+
try {
|
|
216
|
+
originalRaw = fs.readFileSync(filePath, "utf-8");
|
|
217
|
+
} catch (e) {
|
|
218
|
+
if (e.code !== "ENOENT") throw e;
|
|
219
|
+
}
|
|
220
|
+
const data = formatJsonLikeOriginal(config, originalRaw);
|
|
221
|
+
|
|
222
|
+
fs.mkdirSync(path.dirname(filePath), { recursive: true });
|
|
223
|
+
|
|
224
|
+
let originalMode = null;
|
|
225
|
+
try {
|
|
226
|
+
originalMode = fs.statSync(filePath).mode & 0o777;
|
|
227
|
+
} catch (_) {
|
|
228
|
+
// File does not exist yet, so there is no mode to preserve.
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const tmpPath = filePath + "." + Date.now() + ".tmp";
|
|
232
|
+
fs.writeFileSync(tmpPath, data, "utf-8");
|
|
233
|
+
|
|
234
|
+
if (originalMode !== null) {
|
|
235
|
+
try {
|
|
236
|
+
fs.chmodSync(tmpPath, originalMode);
|
|
237
|
+
} catch (_) {
|
|
238
|
+
// Windows will not honor chmod fully, but that is fine.
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// On Windows, os.replace() can fail if the target is read-only
|
|
243
|
+
if (process.platform === "win32" && fs.existsSync(filePath)) {
|
|
244
|
+
try {
|
|
245
|
+
fs.chmodSync(filePath, 0o666);
|
|
246
|
+
} catch (_) {}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
fs.renameSync(tmpPath, filePath);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function hasMonitorFlag(config) {
|
|
253
|
+
const gb = config.cachedGrowthBookFeatures || {};
|
|
254
|
+
return gb[FLAG_KEY] === true;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function inject(config) {
|
|
258
|
+
if (!config.cachedGrowthBookFeatures) {
|
|
259
|
+
config.cachedGrowthBookFeatures = {};
|
|
260
|
+
}
|
|
261
|
+
const gb = config.cachedGrowthBookFeatures;
|
|
262
|
+
if (gb[FLAG_KEY] === true) {
|
|
263
|
+
return false; // already present
|
|
264
|
+
}
|
|
265
|
+
gb[FLAG_KEY] = true;
|
|
266
|
+
config.cachedGrowthBookFeaturesAt = Date.now();
|
|
267
|
+
return true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
function revert(config) {
|
|
271
|
+
const gb = config.cachedGrowthBookFeatures || {};
|
|
272
|
+
if (!(FLAG_KEY in gb)) {
|
|
273
|
+
return false; // not present
|
|
274
|
+
}
|
|
275
|
+
delete gb[FLAG_KEY];
|
|
276
|
+
if (Object.keys(gb).length === 0) {
|
|
277
|
+
delete config.cachedGrowthBookFeatures;
|
|
278
|
+
delete config.cachedGrowthBookFeaturesAt;
|
|
279
|
+
}
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Bash discovery (mirrors Claude Code's eye() on Windows)
|
|
284
|
+
|
|
285
|
+
function whichGit() {
|
|
286
|
+
try {
|
|
287
|
+
const cmd = IS_WINDOWS ? "where git" : "command -v git";
|
|
288
|
+
const out = execSync(cmd, { stdio: ["ignore", "pipe", "ignore"] })
|
|
289
|
+
.toString()
|
|
290
|
+
.trim();
|
|
291
|
+
return out.split(/\r?\n/)[0] || null;
|
|
292
|
+
} catch (_) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Locate a usable bash.exe the same way the client does:
|
|
299
|
+
* 1. CLAUDE_CODE_GIT_BASH_PATH env (skipped when ignoreEnv)
|
|
300
|
+
* 2. default Git for Windows locations
|
|
301
|
+
* 3. `git` on PATH -> <gitDir>/../../bin/bash.exe
|
|
302
|
+
* Returns { path, isDefault, source }. `path` is null when nothing was found.
|
|
303
|
+
*/
|
|
304
|
+
function findGitBash(ignoreEnv = false) {
|
|
305
|
+
if (!ignoreEnv) {
|
|
306
|
+
const envPath = process.env[GIT_BASH_ENV];
|
|
307
|
+
if (envPath) {
|
|
308
|
+
if (fs.existsSync(envPath))
|
|
309
|
+
return { path: envPath, isDefault: false, source: "env" };
|
|
310
|
+
// A set-but-missing env var is fatal at Claude startup (process.exit(1)).
|
|
311
|
+
return { path: null, isDefault: false, source: "env-broken", broken: envPath };
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
for (const p of DEFAULT_GIT_BASH_PATHS) {
|
|
315
|
+
if (fs.existsSync(p)) return { path: p, isDefault: true, source: "default" };
|
|
316
|
+
}
|
|
317
|
+
const git = whichGit();
|
|
318
|
+
if (git) {
|
|
319
|
+
const p = path.join(git, "..", "..", "bin", "bash.exe");
|
|
320
|
+
if (fs.existsSync(p)) return { path: p, isDefault: false, source: "git-path" };
|
|
321
|
+
}
|
|
322
|
+
return { path: null, isDefault: false, source: "none" };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Ensure a bash.exe exists and, when needed, pin it via settings.env.
|
|
327
|
+
* Mutates `env` in place. Returns { changed, ok, bashPath }.
|
|
328
|
+
* No-op (ok:true) on non-Windows, where the tool gate does not require bash.
|
|
329
|
+
*/
|
|
330
|
+
function ensureGitBash(env, dryRun) {
|
|
331
|
+
if (!IS_WINDOWS) return { changed: false, ok: true, bashPath: null };
|
|
332
|
+
|
|
333
|
+
let changed = false;
|
|
334
|
+
|
|
335
|
+
if (process.env[GIT_BASH_ENV] && !fs.existsSync(process.env[GIT_BASH_ENV])) {
|
|
336
|
+
console.log(` ERROR: External ${GIT_BASH_ENV} points to a missing file: ${process.env[GIT_BASH_ENV]}`);
|
|
337
|
+
console.log(" This script only edits Claude settings. Clear or fix that external environment variable, then rerun.");
|
|
338
|
+
console.log(` PowerShell user-scope fix: [Environment]::SetEnvironmentVariable('${GIT_BASH_ENV}', $null, 'User')`);
|
|
339
|
+
return { changed: false, ok: false, bashPath: null };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Explicit override wins and skips discovery/install.
|
|
343
|
+
if (args["git-bash-path"]) {
|
|
344
|
+
const p = path.resolve(args["git-bash-path"]);
|
|
345
|
+
if (!fs.existsSync(p)) {
|
|
346
|
+
console.log(` ERROR: --git-bash-path does not exist: ${p}`);
|
|
347
|
+
return { changed: false, ok: false, bashPath: null };
|
|
348
|
+
}
|
|
349
|
+
if (env[GIT_BASH_ENV] !== p) {
|
|
350
|
+
console.log(` WARN: Pinning ${GIT_BASH_ENV}=${p}`);
|
|
351
|
+
env[GIT_BASH_ENV] = p;
|
|
352
|
+
changed = true;
|
|
353
|
+
}
|
|
354
|
+
return { changed, ok: true, bashPath: p };
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// If settings.json already pins a path that no longer exists, it would crash
|
|
358
|
+
// Claude on startup. Drop it and re-discover a good one below.
|
|
359
|
+
if (env[GIT_BASH_ENV] && !fs.existsSync(env[GIT_BASH_ENV])) {
|
|
360
|
+
console.log(` WARN: Removing stale ${GIT_BASH_ENV} (missing file): ${env[GIT_BASH_ENV]}`);
|
|
361
|
+
delete env[GIT_BASH_ENV];
|
|
362
|
+
changed = true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
let found = findGitBash(true); // discovery ignores env; we manage env ourselves
|
|
366
|
+
|
|
367
|
+
if (found.path) {
|
|
368
|
+
console.log(` OK: bash found: ${found.path} (${found.source})`);
|
|
369
|
+
// Only pin when NOT at a default location; Claude auto-detects those.
|
|
370
|
+
if (!found.isDefault && env[GIT_BASH_ENV] !== found.path) {
|
|
371
|
+
console.log(` WARN: Pinning ${GIT_BASH_ENV}=${found.path}`);
|
|
372
|
+
env[GIT_BASH_ENV] = found.path;
|
|
373
|
+
changed = true;
|
|
374
|
+
}
|
|
375
|
+
return { changed, ok: true, bashPath: found.path };
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
console.log(" ERROR: No bash.exe found. Monitor and Bash will stay hidden on Windows.");
|
|
379
|
+
console.log(" Install one of:");
|
|
380
|
+
console.log(" - Git for Windows: https://git-scm.com/downloads/win");
|
|
381
|
+
console.log(` Or re-run with --git-bash-path <path-to-bash.exe>.`);
|
|
382
|
+
return { changed, ok: false, bashPath: null };
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Settings validation
|
|
386
|
+
|
|
387
|
+
function isToolSearchEnabled(value) {
|
|
388
|
+
return value === "1" || value === "true" || value === true;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
function validateAndFixSettingsStrict(dryRun) {
|
|
392
|
+
let settings;
|
|
393
|
+
|
|
394
|
+
if (!fs.existsSync(settingsPath)) {
|
|
395
|
+
console.log(`WARN: Settings file not found: ${settingsPath}`);
|
|
396
|
+
console.log(" Creating a minimal settings.json with required env vars ...");
|
|
397
|
+
settings = { env: {} };
|
|
398
|
+
} else {
|
|
399
|
+
try {
|
|
400
|
+
settings = loadConfig(settingsPath);
|
|
401
|
+
} catch (e) {
|
|
402
|
+
console.log(`ERROR: ${e.message}`);
|
|
403
|
+
return { changed: false, ok: false };
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (!settings || typeof settings !== "object" || Array.isArray(settings)) {
|
|
408
|
+
console.log(`ERROR: ${settingsPath} must contain a JSON object`);
|
|
409
|
+
return { changed: false, ok: false };
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
let changed = false;
|
|
413
|
+
if (!settings.env || typeof settings.env !== "object" || Array.isArray(settings.env)) {
|
|
414
|
+
settings.env = {};
|
|
415
|
+
changed = true;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const env = settings.env;
|
|
419
|
+
|
|
420
|
+
for (const forbidden of ["DISABLE_TELEMETRY", "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC"]) {
|
|
421
|
+
if (forbidden in env) {
|
|
422
|
+
console.log(` WARN: Removing ${forbidden}=${JSON.stringify(env[forbidden])} from env`);
|
|
423
|
+
delete env[forbidden];
|
|
424
|
+
changed = true;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (!isToolSearchEnabled(env["ENABLE_TOOL_SEARCH"])) {
|
|
429
|
+
const old = env["ENABLE_TOOL_SEARCH"];
|
|
430
|
+
console.log(` WARN: Setting ENABLE_TOOL_SEARCH="true" (was: ${JSON.stringify(old)})`);
|
|
431
|
+
env["ENABLE_TOOL_SEARCH"] = "true";
|
|
432
|
+
changed = true;
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
const bash = ensureGitBash(env, dryRun);
|
|
436
|
+
if (bash.changed) changed = true;
|
|
437
|
+
if (!bash.ok) {
|
|
438
|
+
if (changed) {
|
|
439
|
+
if (dryRun) {
|
|
440
|
+
console.log(`[DRY RUN] Would write partial settings cleanup to ${settingsPath}:`);
|
|
441
|
+
console.log(` ${JSON.stringify(settings, null, 2)}`);
|
|
442
|
+
} else {
|
|
443
|
+
backupBeforeWrite(settingsPath, "settings.json");
|
|
444
|
+
saveConfig(settingsPath, settings);
|
|
445
|
+
console.log(`OK: ${settingsPath} updated with partial cleanup`);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
console.log("ERROR: Git Bash is required before enabling Monitor on Windows.");
|
|
449
|
+
return { changed, ok: false };
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
if (!changed) {
|
|
453
|
+
console.log(`OK: ${settingsPath} passes Monitor validation`);
|
|
454
|
+
return { changed: false, ok: true };
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
if (dryRun) {
|
|
458
|
+
console.log(`[DRY RUN] Would write to ${settingsPath}:`);
|
|
459
|
+
console.log(` ${JSON.stringify(settings, null, 2)}`);
|
|
460
|
+
return { changed: true, ok: true };
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
backupBeforeWrite(settingsPath, "settings.json");
|
|
464
|
+
saveConfig(settingsPath, settings);
|
|
465
|
+
console.log(`OK: ${settingsPath} updated for Monitor`);
|
|
466
|
+
return { changed: true, ok: true };
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
function findLatestBackupDir() {
|
|
470
|
+
const root = defaultBackupRoot();
|
|
471
|
+
if (!fs.existsSync(root)) return null;
|
|
472
|
+
const dirs = fs.readdirSync(root, { withFileTypes: true })
|
|
473
|
+
.filter((entry) => entry.isDirectory() && entry.name.startsWith(`${BACKUP_PREFIX}-`))
|
|
474
|
+
.map((entry) => path.join(root, entry.name))
|
|
475
|
+
.sort((a, b) => b.localeCompare(a));
|
|
476
|
+
return dirs[0] || null;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function resolveRevertBackupDir() {
|
|
480
|
+
if (args["revert-backup"]) return path.resolve(args["revert-backup"]);
|
|
481
|
+
return findLatestBackupDir();
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
function restoreBackupEntry(backupDir, targetPath, backupName, dryRun) {
|
|
485
|
+
const sourcePath = path.join(backupDir, backupName);
|
|
486
|
+
const missingPath = path.join(backupDir, `${backupName}.missing.txt`);
|
|
487
|
+
|
|
488
|
+
if (fs.existsSync(sourcePath)) {
|
|
489
|
+
if (dryRun) {
|
|
490
|
+
console.log(`[DRY RUN] Would restore ${targetPath} from ${sourcePath}`);
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
|
494
|
+
fs.copyFileSync(sourcePath, targetPath);
|
|
495
|
+
console.log(`OK: Restored ${targetPath} from ${sourcePath}`);
|
|
496
|
+
return true;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
if (fs.existsSync(missingPath)) {
|
|
500
|
+
if (dryRun) {
|
|
501
|
+
console.log(`[DRY RUN] Would remove ${targetPath} because it was missing before backup`);
|
|
502
|
+
return true;
|
|
503
|
+
}
|
|
504
|
+
if (fs.existsSync(targetPath)) fs.unlinkSync(targetPath);
|
|
505
|
+
console.log(`OK: Removed ${targetPath}; it was missing before backup`);
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
function revertFromTimestampBackup(dryRun) {
|
|
513
|
+
const backupDir = resolveRevertBackupDir();
|
|
514
|
+
if (!backupDir) {
|
|
515
|
+
console.log(`WARN: No ${BACKUP_PREFIX}-* backup directory found under ${defaultBackupRoot()}`);
|
|
516
|
+
return false;
|
|
517
|
+
}
|
|
518
|
+
if (!fs.existsSync(backupDir) || !fs.statSync(backupDir).isDirectory()) {
|
|
519
|
+
console.log(`ERROR: Backup directory not found: ${backupDir}`);
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
console.log(`Using backup directory: ${backupDir}`);
|
|
524
|
+
const restoredConfig = restoreBackupEntry(backupDir, configPath, "claude.json", dryRun);
|
|
525
|
+
const restoredSettings = restoreBackupEntry(backupDir, settingsPath, "settings.json", dryRun);
|
|
526
|
+
if (!restoredConfig && !restoredSettings) {
|
|
527
|
+
console.log("WARN: Backup directory contains neither claude.json nor settings.json backup entries");
|
|
528
|
+
}
|
|
529
|
+
return restoredConfig || restoredSettings;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function revertLegacySettingsBackup(dryRun) {
|
|
533
|
+
const bak = settingsPath + ".bak";
|
|
534
|
+
if (!fs.existsSync(bak)) {
|
|
535
|
+
console.log(`WARN: No legacy settings backup found: ${bak}`);
|
|
536
|
+
return false;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
if (dryRun) {
|
|
540
|
+
console.log(`[DRY RUN] Would restore ${settingsPath} from ${bak}`);
|
|
541
|
+
try {
|
|
542
|
+
const settings = loadConfig(bak);
|
|
543
|
+
console.log(` Preview: ${JSON.stringify(settings, null, 2)}`);
|
|
544
|
+
} catch (e) {
|
|
545
|
+
console.log(`ERROR: ${e.message}`);
|
|
546
|
+
return false;
|
|
547
|
+
}
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
let settings;
|
|
552
|
+
try {
|
|
553
|
+
settings = loadConfig(bak);
|
|
554
|
+
} catch (e) {
|
|
555
|
+
console.log(`ERROR: ${e.message}`);
|
|
556
|
+
return false;
|
|
557
|
+
}
|
|
558
|
+
backupBeforeWrite(settingsPath, "settings.json");
|
|
559
|
+
saveConfig(settingsPath, settings);
|
|
560
|
+
fs.unlinkSync(bak);
|
|
561
|
+
console.log(`OK: Restored ${settingsPath} from legacy backup ${bak}`);
|
|
562
|
+
return true;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
// Main
|
|
566
|
+
|
|
567
|
+
if (args.revert) {
|
|
568
|
+
const restored = revertFromTimestampBackup(args["dry-run"]);
|
|
569
|
+
if (!restored) {
|
|
570
|
+
console.log("WARN: Falling back to legacy flag removal and settings.json.bak restore.");
|
|
571
|
+
|
|
572
|
+
if (fs.existsSync(configPath)) {
|
|
573
|
+
let configForRevert;
|
|
574
|
+
try {
|
|
575
|
+
configForRevert = loadConfig(configPath);
|
|
576
|
+
} catch (e) {
|
|
577
|
+
console.log(`ERROR: ${e.message}`);
|
|
578
|
+
process.exit(1);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
if (!configForRevert || typeof configForRevert !== "object" || Array.isArray(configForRevert)) {
|
|
582
|
+
console.log(`ERROR: ${configPath} must contain a JSON object`);
|
|
583
|
+
process.exit(1);
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
if (!hasMonitorFlag(configForRevert)) {
|
|
587
|
+
console.log(`OK: Nothing to revert; flag not present (${configPath})`);
|
|
588
|
+
} else {
|
|
589
|
+
const changed = revert(configForRevert);
|
|
590
|
+
if (changed && args["dry-run"]) {
|
|
591
|
+
console.log(`[DRY RUN] Would remove ${FLAG_KEY}=true from ${configPath}`);
|
|
592
|
+
} else if (changed) {
|
|
593
|
+
backupBeforeWrite(configPath, "claude.json");
|
|
594
|
+
saveConfig(configPath, configForRevert);
|
|
595
|
+
console.log(`OK: Removed ${FLAG_KEY}=true in ${configPath}`);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
} else {
|
|
599
|
+
console.log(`WARN: Config file not found: ${configPath}`);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
revertLegacySettingsBackup(args["dry-run"]);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
console.log(" Note: Git for Windows is left in place.");
|
|
606
|
+
if (!args["dry-run"]) {
|
|
607
|
+
console.log("\n Restart Claude Code for revert to take effect.");
|
|
608
|
+
}
|
|
609
|
+
process.exit(0);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
if (!fs.existsSync(configPath)) {
|
|
613
|
+
console.log(`ERROR: Config file not found: ${configPath}`);
|
|
614
|
+
process.exit(1);
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
let config;
|
|
618
|
+
try {
|
|
619
|
+
config = loadConfig(configPath);
|
|
620
|
+
} catch (e) {
|
|
621
|
+
console.log(`ERROR: ${e.message}`);
|
|
622
|
+
process.exit(1);
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
626
|
+
console.log(`ERROR: ${configPath} must contain a JSON object`);
|
|
627
|
+
process.exit(1);
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// Enable path
|
|
631
|
+
|
|
632
|
+
const settingsResult = validateAndFixSettingsStrict(args["dry-run"]);
|
|
633
|
+
if (!settingsResult.ok) {
|
|
634
|
+
if (!args["dry-run"] && backupState.dir) {
|
|
635
|
+
console.log(` Backup at: ${backupState.dir}`);
|
|
636
|
+
}
|
|
637
|
+
console.log("\n Monitor was not enabled. Fix the error above and run this script again.");
|
|
638
|
+
process.exit(1);
|
|
639
|
+
}
|
|
640
|
+
const settingsFixed = settingsResult.changed;
|
|
641
|
+
|
|
642
|
+
const flagAlready = hasMonitorFlag(config);
|
|
643
|
+
if (flagAlready) {
|
|
644
|
+
console.log(`OK: Monitor flag already enabled (${configPath})`);
|
|
645
|
+
} else {
|
|
646
|
+
const changed = inject(config);
|
|
647
|
+
if (!changed) {
|
|
648
|
+
console.log("ERROR: No flag change applied");
|
|
649
|
+
} else if (args["dry-run"]) {
|
|
650
|
+
const preview = JSON.stringify(
|
|
651
|
+
{
|
|
652
|
+
cachedGrowthBookFeatures: config.cachedGrowthBookFeatures || {},
|
|
653
|
+
cachedGrowthBookFeaturesAt: config.cachedGrowthBookFeaturesAt,
|
|
654
|
+
},
|
|
655
|
+
null,
|
|
656
|
+
2
|
|
657
|
+
);
|
|
658
|
+
console.log(`[DRY RUN] Would write to ${configPath}:`);
|
|
659
|
+
console.log(` Added ${FLAG_KEY}=true`);
|
|
660
|
+
console.log(` Preview: ${preview}`);
|
|
661
|
+
} else {
|
|
662
|
+
backupBeforeWrite(configPath, "claude.json");
|
|
663
|
+
saveConfig(configPath, config);
|
|
664
|
+
console.log(`OK: Added ${FLAG_KEY}=true in ${configPath}`);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (!args["dry-run"]) {
|
|
669
|
+
if (settingsFixed || !flagAlready) {
|
|
670
|
+
if (backupState.dir) {
|
|
671
|
+
console.log(` Backup at: ${backupState.dir}`);
|
|
672
|
+
}
|
|
673
|
+
console.log("\n Config changed. Restart Claude Code for Monitor to appear.");
|
|
674
|
+
} else {
|
|
675
|
+
console.log("\n Already fully configured. Restart Claude Code if Monitor is not visible yet.");
|
|
676
|
+
}
|
|
677
|
+
}
|