marmonitor 0.1.0
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/LICENSE +21 -0
- package/README.md +219 -0
- package/bin/marmonitor.js +2 -0
- package/dist/banner/index.d.ts +7 -0
- package/dist/banner/index.js +212 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +855 -0
- package/dist/config/index.d.ts +105 -0
- package/dist/config/index.js +218 -0
- package/dist/guard/index.d.ts +23 -0
- package/dist/guard/index.js +128 -0
- package/dist/output/index.d.ts +28 -0
- package/dist/output/index.js +457 -0
- package/dist/output/utils.d.ts +138 -0
- package/dist/output/utils.js +588 -0
- package/dist/process-safety.d.ts +2 -0
- package/dist/process-safety.js +7 -0
- package/dist/scanner/bounded-map.d.ts +10 -0
- package/dist/scanner/bounded-map.js +25 -0
- package/dist/scanner/cache.d.ts +88 -0
- package/dist/scanner/cache.js +32 -0
- package/dist/scanner/claude.d.ts +25 -0
- package/dist/scanner/claude.js +425 -0
- package/dist/scanner/codex.d.ts +14 -0
- package/dist/scanner/codex.js +195 -0
- package/dist/scanner/gemini.d.ts +21 -0
- package/dist/scanner/gemini.js +131 -0
- package/dist/scanner/group.d.ts +7 -0
- package/dist/scanner/group.js +54 -0
- package/dist/scanner/index.d.ts +16 -0
- package/dist/scanner/index.js +256 -0
- package/dist/scanner/process.d.ts +17 -0
- package/dist/scanner/process.js +91 -0
- package/dist/scanner/status.d.ts +8 -0
- package/dist/scanner/status.js +45 -0
- package/dist/scanner/types.d.ts +6 -0
- package/dist/scanner/types.js +4 -0
- package/dist/tmux/index.d.ts +34 -0
- package/dist/tmux/index.js +166 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/docs/banner-ansi.png +0 -0
- package/docs/marmot-small.png +0 -0
- package/package.json +70 -0
package/dist/cli.js
ADDED
|
@@ -0,0 +1,855 @@
|
|
|
1
|
+
import { mkdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { dirname, join } from "node:path";
|
|
4
|
+
import { Command } from "commander";
|
|
5
|
+
import { renderInstallBanner, renderRuntimeBanner } from "./banner/index.js";
|
|
6
|
+
import { getConfigDir, getConfigSearchPaths, getDefaultConfigPath, loadConfig, resolveConfigPath as resolveLoadedConfigPath, } from "./config/index.js";
|
|
7
|
+
import { evaluateGuard, formatGuardOutput, parseHookEvent, readStdin } from "./guard/index.js";
|
|
8
|
+
import { printAttention, printAttentionJson, printCleanJson, printCleanPlan, printDock, printJumpAttentionChooser, printJumpJson, printJumpResult, printStatus, printStatusJson, renderStatusline, renderUnavailableStatusline, } from "./output/index.js";
|
|
9
|
+
import { selectJumpAttentionItem, selectUnmatchedTargets } from "./output/utils.js";
|
|
10
|
+
import { TERMINAL_RESTORE_SEQUENCE, formatProcessFailure } from "./process-safety.js";
|
|
11
|
+
import { detectClaudePhase } from "./scanner/claude.js";
|
|
12
|
+
import { detectCodexPhase, indexCodexSessions, matchCodexSession } from "./scanner/codex.js";
|
|
13
|
+
import { parseGeminiSession } from "./scanner/gemini.js";
|
|
14
|
+
import { scanAgents } from "./scanner/index.js";
|
|
15
|
+
import { detectCliStdoutPhase } from "./scanner/status.js";
|
|
16
|
+
import { captureTmuxPaneOutput, jumpToAgent, resolveTmuxJumpTarget } from "./tmux/index.js";
|
|
17
|
+
import { VERSION } from "./version.js";
|
|
18
|
+
function sleep(ms) {
|
|
19
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
20
|
+
}
|
|
21
|
+
let stdinRawModeActive = false;
|
|
22
|
+
let processExiting = false;
|
|
23
|
+
function setStdinRawMode(enabled) {
|
|
24
|
+
const stdin = process.stdin;
|
|
25
|
+
if (!("setRawMode" in stdin) || typeof stdin.setRawMode !== "function")
|
|
26
|
+
return;
|
|
27
|
+
stdin.setRawMode(enabled);
|
|
28
|
+
stdinRawModeActive = enabled;
|
|
29
|
+
}
|
|
30
|
+
function restoreTerminalState() {
|
|
31
|
+
if (stdinRawModeActive) {
|
|
32
|
+
try {
|
|
33
|
+
setStdinRawMode(false);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// best effort only
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
process.stdin.pause();
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// best effort only
|
|
44
|
+
}
|
|
45
|
+
if (process.stdout.isTTY) {
|
|
46
|
+
process.stdout.write(TERMINAL_RESTORE_SEQUENCE);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function exitWithCleanup(code, message) {
|
|
50
|
+
if (processExiting) {
|
|
51
|
+
process.exit(code);
|
|
52
|
+
}
|
|
53
|
+
processExiting = true;
|
|
54
|
+
restoreTerminalState();
|
|
55
|
+
if (message) {
|
|
56
|
+
console.error(message);
|
|
57
|
+
}
|
|
58
|
+
process.exit(code);
|
|
59
|
+
}
|
|
60
|
+
function installProcessSafetyHandlers() {
|
|
61
|
+
process.on("uncaughtException", (error) => {
|
|
62
|
+
exitWithCleanup(1, formatProcessFailure(error));
|
|
63
|
+
});
|
|
64
|
+
process.on("unhandledRejection", (reason) => {
|
|
65
|
+
exitWithCleanup(1, formatProcessFailure(reason));
|
|
66
|
+
});
|
|
67
|
+
process.on("SIGINT", () => exitWithCleanup(0));
|
|
68
|
+
process.on("SIGTERM", () => exitWithCleanup(0));
|
|
69
|
+
}
|
|
70
|
+
function buildHelpAppendix() {
|
|
71
|
+
return [
|
|
72
|
+
"Core workflows:",
|
|
73
|
+
" status Full one-shot inventory of local AI sessions",
|
|
74
|
+
" attention Priority list for sessions that need review",
|
|
75
|
+
" watch Live full-screen monitor",
|
|
76
|
+
" dock Persistent tmux-friendly monitor",
|
|
77
|
+
" --statusline Status bar output for tmux",
|
|
78
|
+
" clean Review or kill unmatched leftovers",
|
|
79
|
+
" debug-phase Inspect raw phase signals for one PID",
|
|
80
|
+
" guard Claude hook evaluator; fail-open on malformed input/errors",
|
|
81
|
+
" settings-* Locate, inspect, or initialize settings.json",
|
|
82
|
+
"",
|
|
83
|
+
"Statusline formats:",
|
|
84
|
+
" compact | standard | extended | tmux-badges",
|
|
85
|
+
"",
|
|
86
|
+
"Config helper commands:",
|
|
87
|
+
" settings-path Show config search order and active file",
|
|
88
|
+
" settings-show Print the merged runtime config",
|
|
89
|
+
" settings-init Write a starter settings.json",
|
|
90
|
+
" settings-init --advanced Print or write the full advanced sample",
|
|
91
|
+
"",
|
|
92
|
+
"Settings groups:",
|
|
93
|
+
" display Attention list, statusline density, sorting",
|
|
94
|
+
" integration tmux keys, paused terminal integrations, banner behavior",
|
|
95
|
+
" paths Runtime data path overrides for Claude/Codex",
|
|
96
|
+
" status Thresholds and phase/approval heuristics",
|
|
97
|
+
" performance Snapshot/cache TTL tuning",
|
|
98
|
+
" intervention Guard and policy rules (advanced / optional)",
|
|
99
|
+
"",
|
|
100
|
+
"Default tmux shortcuts:",
|
|
101
|
+
" Prefix+a Attention popup",
|
|
102
|
+
" Prefix+j Jump popup",
|
|
103
|
+
" Prefix+m Toggle dock",
|
|
104
|
+
" Option+1..5 Direct jump to numbered attention sessions",
|
|
105
|
+
"",
|
|
106
|
+
`Config location: ${getConfigDir()}/settings.json`,
|
|
107
|
+
"Common settings:",
|
|
108
|
+
" display.attentionLimit / display.statuslineAttentionLimit",
|
|
109
|
+
" integration.tmux.keys.*",
|
|
110
|
+
" paths.* (runtime data path overrides)",
|
|
111
|
+
"",
|
|
112
|
+
"Shortcuts can be customized in settings.json:",
|
|
113
|
+
" integration.tmux.keys = { attentionPopup, jumpPopup, dockToggle, directJump[] }",
|
|
114
|
+
].join("\n");
|
|
115
|
+
}
|
|
116
|
+
function clearScreen() {
|
|
117
|
+
if (process.stdout.isTTY) {
|
|
118
|
+
process.stdout.write("\x1Bc");
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
async function promptSelectionKey(maxChoice) {
|
|
122
|
+
if (!process.stdin.isTTY || maxChoice < 1)
|
|
123
|
+
return undefined;
|
|
124
|
+
const prompt = maxChoice >= 10
|
|
125
|
+
? "\nPress 1-9 to jump, 0 for item 10, q to cancel: "
|
|
126
|
+
: `\nPress 1-${maxChoice} to jump, q to cancel: `;
|
|
127
|
+
process.stdout.write(prompt);
|
|
128
|
+
return await new Promise((resolve) => {
|
|
129
|
+
const stdin = process.stdin;
|
|
130
|
+
const restoreRawMode = "setRawMode" in stdin && typeof stdin.setRawMode === "function";
|
|
131
|
+
const cleanup = () => {
|
|
132
|
+
if (restoreRawMode)
|
|
133
|
+
setStdinRawMode(false);
|
|
134
|
+
stdin.pause();
|
|
135
|
+
stdin.removeListener("data", onData);
|
|
136
|
+
process.stdout.write("\n");
|
|
137
|
+
};
|
|
138
|
+
const onData = (chunk) => {
|
|
139
|
+
const input = chunk.toString("utf8");
|
|
140
|
+
if (input === "\u0003") {
|
|
141
|
+
cleanup();
|
|
142
|
+
exitWithCleanup(130);
|
|
143
|
+
}
|
|
144
|
+
if (input === "\u001b" || input === "q" || input === "Q") {
|
|
145
|
+
cleanup();
|
|
146
|
+
resolve(undefined);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const trimmed = input.trim();
|
|
150
|
+
if (/^[1-9]$/.test(trimmed)) {
|
|
151
|
+
const selection = Number.parseInt(trimmed, 10);
|
|
152
|
+
if (selection <= maxChoice) {
|
|
153
|
+
cleanup();
|
|
154
|
+
resolve(selection);
|
|
155
|
+
}
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (trimmed === "0" && maxChoice >= 10) {
|
|
159
|
+
cleanup();
|
|
160
|
+
resolve(10);
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
if (restoreRawMode)
|
|
164
|
+
setStdinRawMode(true);
|
|
165
|
+
stdin.resume();
|
|
166
|
+
stdin.on("data", onData);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const program = new Command();
|
|
170
|
+
program
|
|
171
|
+
.name("marmonitor")
|
|
172
|
+
.description("AI agent monitoring tool")
|
|
173
|
+
.version(VERSION, "-v, --version", "output the version number");
|
|
174
|
+
program.helpCommand(false);
|
|
175
|
+
program.showHelpAfterError();
|
|
176
|
+
program.addHelpText("beforeAll", `${renderInstallBanner()}\n`);
|
|
177
|
+
program.addHelpText("afterAll", `\n${buildHelpAppendix()}\n`);
|
|
178
|
+
program
|
|
179
|
+
.command("help [command]")
|
|
180
|
+
.description("Show marmonitor help, commands, and shortcuts")
|
|
181
|
+
.action((commandName) => {
|
|
182
|
+
if (!commandName) {
|
|
183
|
+
program.outputHelp();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
const target = program.commands.find((command) => command.name() === commandName);
|
|
187
|
+
if (!target) {
|
|
188
|
+
console.error(`Unknown command: ${commandName}`);
|
|
189
|
+
process.exit(1);
|
|
190
|
+
}
|
|
191
|
+
target.outputHelp();
|
|
192
|
+
});
|
|
193
|
+
program
|
|
194
|
+
.command("banner")
|
|
195
|
+
.description("Preview install or runtime banner")
|
|
196
|
+
.option("--install", "Render install-style banner")
|
|
197
|
+
.option("--runtime", "Render runtime-style banner")
|
|
198
|
+
.option("--active <n>", "Active session count for runtime banner")
|
|
199
|
+
.action(async (opts) => {
|
|
200
|
+
const active = typeof opts.active === "string" ? Number.parseInt(opts.active, 10) : undefined;
|
|
201
|
+
if (opts.install && opts.runtime) {
|
|
202
|
+
console.error("Use only one of --install or --runtime.");
|
|
203
|
+
process.exit(1);
|
|
204
|
+
}
|
|
205
|
+
if (opts.install) {
|
|
206
|
+
console.log(renderInstallBanner());
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
console.log(renderRuntimeBanner(Number.isNaN(active) ? undefined : active));
|
|
210
|
+
});
|
|
211
|
+
function resolveConfigPath(opts) {
|
|
212
|
+
return opts.config ?? program.opts().config;
|
|
213
|
+
}
|
|
214
|
+
function resolveStatuslineWidth(value) {
|
|
215
|
+
if (value !== undefined) {
|
|
216
|
+
const parsed = Number.parseInt(value, 10);
|
|
217
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
|
|
218
|
+
}
|
|
219
|
+
return process.stdout.isTTY && typeof process.stdout.columns === "number"
|
|
220
|
+
? process.stdout.columns
|
|
221
|
+
: undefined;
|
|
222
|
+
}
|
|
223
|
+
function buildMinimalConfigSample() {
|
|
224
|
+
return JSON.stringify({
|
|
225
|
+
display: {
|
|
226
|
+
attentionLimit: 10,
|
|
227
|
+
statuslineAttentionLimit: 5,
|
|
228
|
+
},
|
|
229
|
+
integration: {
|
|
230
|
+
tmux: {
|
|
231
|
+
keys: {
|
|
232
|
+
attentionPopup: "a",
|
|
233
|
+
jumpPopup: "j",
|
|
234
|
+
dockToggle: "m",
|
|
235
|
+
directJump: ["M-1", "M-2", "M-3", "M-4", "M-5"],
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
}, null, 2);
|
|
240
|
+
}
|
|
241
|
+
function buildAdvancedConfigSample() {
|
|
242
|
+
return JSON.stringify({
|
|
243
|
+
status: {
|
|
244
|
+
stalledAfterMin: 20,
|
|
245
|
+
phaseDecay: {
|
|
246
|
+
thinking: 20,
|
|
247
|
+
tool: 30,
|
|
248
|
+
permission: 0,
|
|
249
|
+
done: 5,
|
|
250
|
+
},
|
|
251
|
+
stdoutHeuristic: {
|
|
252
|
+
approvalPatterns: ["would you like to", "please approve"],
|
|
253
|
+
clearPatterns: ["applying patch", "running tests"],
|
|
254
|
+
},
|
|
255
|
+
},
|
|
256
|
+
display: {
|
|
257
|
+
attentionLimit: 10,
|
|
258
|
+
statuslineAttentionLimit: 5,
|
|
259
|
+
},
|
|
260
|
+
integration: {
|
|
261
|
+
tmux: {
|
|
262
|
+
keys: {
|
|
263
|
+
attentionPopup: "a",
|
|
264
|
+
jumpPopup: "j",
|
|
265
|
+
dockToggle: "m",
|
|
266
|
+
directJump: ["M-1", "M-2", "M-3", "M-4", "M-5"],
|
|
267
|
+
},
|
|
268
|
+
},
|
|
269
|
+
wezterm: {
|
|
270
|
+
enabled: false,
|
|
271
|
+
statusTtlSec: 15,
|
|
272
|
+
},
|
|
273
|
+
banner: {
|
|
274
|
+
install: true,
|
|
275
|
+
runtime: false,
|
|
276
|
+
},
|
|
277
|
+
},
|
|
278
|
+
paths: {
|
|
279
|
+
claudeProjects: [],
|
|
280
|
+
claudeSessions: [],
|
|
281
|
+
codexSessions: [],
|
|
282
|
+
extraRoots: [],
|
|
283
|
+
},
|
|
284
|
+
performance: {
|
|
285
|
+
snapshotTtlMs: 5000,
|
|
286
|
+
statuslineTtlMs: 5000,
|
|
287
|
+
stdoutHeuristicTtlMs: 5000,
|
|
288
|
+
},
|
|
289
|
+
}, null, 2);
|
|
290
|
+
}
|
|
291
|
+
program
|
|
292
|
+
.command("settings-path")
|
|
293
|
+
.description("Show config search paths and the currently resolved config file")
|
|
294
|
+
.option("--json", "Output as JSON")
|
|
295
|
+
.option("--config <path>", "Path to settings.json")
|
|
296
|
+
.action((opts) => {
|
|
297
|
+
const customPath = resolveConfigPath(opts);
|
|
298
|
+
const resolvedPath = resolveLoadedConfigPath(customPath);
|
|
299
|
+
const payload = {
|
|
300
|
+
configDir: getConfigDir(),
|
|
301
|
+
defaultPath: getDefaultConfigPath(),
|
|
302
|
+
searchPaths: customPath ? [customPath] : getConfigSearchPaths(),
|
|
303
|
+
resolvedPath: resolvedPath ?? null,
|
|
304
|
+
source: customPath ? "custom" : resolvedPath ? "discovered" : "default",
|
|
305
|
+
};
|
|
306
|
+
if (opts.json) {
|
|
307
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
console.log(`Config dir: ${payload.configDir}`);
|
|
311
|
+
console.log(`Default path: ${payload.defaultPath}`);
|
|
312
|
+
console.log(`Resolved path: ${payload.resolvedPath ?? "(none, defaults only)"}`);
|
|
313
|
+
console.log("Search paths:");
|
|
314
|
+
for (const path of payload.searchPaths) {
|
|
315
|
+
console.log(` - ${path}`);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
program
|
|
319
|
+
.command("settings-show")
|
|
320
|
+
.description("Show the merged runtime configuration")
|
|
321
|
+
.option("--json", "Output as JSON")
|
|
322
|
+
.option("--config <path>", "Path to settings.json")
|
|
323
|
+
.action(async (opts) => {
|
|
324
|
+
const customPath = resolveConfigPath(opts);
|
|
325
|
+
const resolvedPath = resolveLoadedConfigPath(customPath);
|
|
326
|
+
const config = await loadConfig(customPath);
|
|
327
|
+
const payload = {
|
|
328
|
+
resolvedPath: resolvedPath ?? null,
|
|
329
|
+
config,
|
|
330
|
+
};
|
|
331
|
+
if (opts.json) {
|
|
332
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
console.log(`Resolved path: ${payload.resolvedPath ?? "(none, defaults only)"}`);
|
|
336
|
+
console.log(JSON.stringify(config, null, 2));
|
|
337
|
+
});
|
|
338
|
+
program
|
|
339
|
+
.command("settings-init")
|
|
340
|
+
.description("Write a starter settings.json file")
|
|
341
|
+
.option("--advanced", "Write the advanced sample instead of the minimal sample")
|
|
342
|
+
.option("--stdout", "Print the sample to stdout instead of writing a file")
|
|
343
|
+
.option("--force", "Overwrite an existing settings file")
|
|
344
|
+
.option("--config <path>", "Path to settings.json")
|
|
345
|
+
.action(async (opts) => {
|
|
346
|
+
const targetPath = resolveConfigPath(opts) ?? getDefaultConfigPath();
|
|
347
|
+
const sample = opts.advanced ? buildAdvancedConfigSample() : buildMinimalConfigSample();
|
|
348
|
+
if (opts.stdout) {
|
|
349
|
+
console.log(sample);
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
try {
|
|
353
|
+
await stat(targetPath);
|
|
354
|
+
if (!opts.force) {
|
|
355
|
+
console.error(`Config already exists: ${targetPath}`);
|
|
356
|
+
console.error("Use --force to overwrite or --stdout to preview the sample.");
|
|
357
|
+
process.exit(1);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
catch {
|
|
361
|
+
// target does not exist, continue
|
|
362
|
+
}
|
|
363
|
+
await mkdir(dirname(targetPath), { recursive: true });
|
|
364
|
+
await writeFile(targetPath, `${sample}\n`, "utf-8");
|
|
365
|
+
console.log(`Wrote ${opts.advanced ? "advanced" : "minimal"} config to ${targetPath}`);
|
|
366
|
+
});
|
|
367
|
+
function resolveAttentionLimit(opts, configuredLimit, interactive = false) {
|
|
368
|
+
const raw = Number(opts.limit);
|
|
369
|
+
const fallback = Number.isFinite(raw) && raw > 0 ? raw : configuredLimit;
|
|
370
|
+
if (!interactive)
|
|
371
|
+
return Math.max(Math.floor(fallback), 1);
|
|
372
|
+
return Math.min(Math.max(Math.floor(fallback), 1), 10);
|
|
373
|
+
}
|
|
374
|
+
function statuslineCacheFile(format, attentionLimit, width) {
|
|
375
|
+
const widthKey = width && width > 0 ? String(width) : "auto";
|
|
376
|
+
return join(tmpdir(), "marmonitor", `statusline-${format}-${attentionLimit}-${widthKey}.txt`);
|
|
377
|
+
}
|
|
378
|
+
function snapshotCacheFile(enrichmentMode, showDead) {
|
|
379
|
+
return join(tmpdir(), "marmonitor", `snapshot-${enrichmentMode}-${showDead ? "dead" : "alive"}.json`);
|
|
380
|
+
}
|
|
381
|
+
async function readCachedStatusline(format, attentionLimit, width, ttlMs) {
|
|
382
|
+
const path = statuslineCacheFile(format, attentionLimit, width);
|
|
383
|
+
try {
|
|
384
|
+
const fileStat = await stat(path);
|
|
385
|
+
if (Date.now() - fileStat.mtimeMs > ttlMs)
|
|
386
|
+
return undefined;
|
|
387
|
+
return (await readFile(path, "utf-8")).trimEnd();
|
|
388
|
+
}
|
|
389
|
+
catch {
|
|
390
|
+
return undefined;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async function writeCachedStatusline(format, attentionLimit, width, value) {
|
|
394
|
+
const path = statuslineCacheFile(format, attentionLimit, width);
|
|
395
|
+
try {
|
|
396
|
+
await mkdir(join(tmpdir(), "marmonitor"), { recursive: true });
|
|
397
|
+
await writeFile(path, value, "utf-8");
|
|
398
|
+
}
|
|
399
|
+
catch {
|
|
400
|
+
// cache failures must never break statusline rendering
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
async function readCachedSnapshot(enrichmentMode, showDead, ttlMs) {
|
|
404
|
+
const path = snapshotCacheFile(enrichmentMode, showDead);
|
|
405
|
+
try {
|
|
406
|
+
const fileStat = await stat(path);
|
|
407
|
+
if (Date.now() - fileStat.mtimeMs > ttlMs)
|
|
408
|
+
return undefined;
|
|
409
|
+
return JSON.parse(await readFile(path, "utf-8"));
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
return undefined;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
async function writeCachedSnapshot(enrichmentMode, showDead, agents) {
|
|
416
|
+
const path = snapshotCacheFile(enrichmentMode, showDead);
|
|
417
|
+
try {
|
|
418
|
+
await mkdir(join(tmpdir(), "marmonitor"), { recursive: true });
|
|
419
|
+
await writeFile(path, JSON.stringify(agents), "utf-8");
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
// snapshot cache failures must never break command execution
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
async function getAgentsSnapshot(config, options = {}) {
|
|
426
|
+
const enrichmentMode = options.enrichmentMode ?? "full";
|
|
427
|
+
const ttlMs = options.ttlMs ?? config.performance.snapshotTtlMs;
|
|
428
|
+
const useCache = ttlMs > 0;
|
|
429
|
+
if (useCache) {
|
|
430
|
+
const cached = await readCachedSnapshot(enrichmentMode, config.display.showDead, ttlMs);
|
|
431
|
+
if (cached)
|
|
432
|
+
return cached;
|
|
433
|
+
}
|
|
434
|
+
const agents = await scanAgents(config, { enrichmentMode });
|
|
435
|
+
if (useCache) {
|
|
436
|
+
await writeCachedSnapshot(enrichmentMode, config.display.showDead, agents);
|
|
437
|
+
}
|
|
438
|
+
return agents;
|
|
439
|
+
}
|
|
440
|
+
program
|
|
441
|
+
.option("--statusline", "One-line summary for tmux statusbar")
|
|
442
|
+
.option("--statusline-format <format>", "Statusline format: compact | standard | extended | tmux-badges", "compact")
|
|
443
|
+
.option("--width <n>", "Render width hint for responsive statusline compaction")
|
|
444
|
+
.option("--config <path>", "Path to settings.json")
|
|
445
|
+
.action(async (opts) => {
|
|
446
|
+
const validFormats = ["compact", "standard", "extended", "tmux-badges", "wezterm-pills"];
|
|
447
|
+
if (opts.statuslineFormat && !validFormats.includes(opts.statuslineFormat)) {
|
|
448
|
+
console.error(`Invalid format: ${opts.statuslineFormat}. Valid: ${validFormats.join(", ")}`);
|
|
449
|
+
process.exit(1);
|
|
450
|
+
}
|
|
451
|
+
if (opts.statusline) {
|
|
452
|
+
try {
|
|
453
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
454
|
+
if (opts.statuslineFormat === "wezterm-pills") {
|
|
455
|
+
console.error("wezterm-pills is paused. Use tmux-badges or another tmux format.");
|
|
456
|
+
process.exit(1);
|
|
457
|
+
}
|
|
458
|
+
const attentionLimit = config.display.statuslineAttentionLimit;
|
|
459
|
+
const width = resolveStatuslineWidth(opts.width);
|
|
460
|
+
const cached = await readCachedStatusline(opts.statuslineFormat, attentionLimit, width, config.performance.statuslineTtlMs);
|
|
461
|
+
if (cached) {
|
|
462
|
+
console.log(cached);
|
|
463
|
+
return;
|
|
464
|
+
}
|
|
465
|
+
const agents = await getAgentsSnapshot(config, { enrichmentMode: "full" });
|
|
466
|
+
const rendered = await renderStatusline(agents, opts.statuslineFormat, attentionLimit, width);
|
|
467
|
+
await writeCachedStatusline(opts.statuslineFormat, attentionLimit, width, rendered);
|
|
468
|
+
console.log(rendered);
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
console.log(renderUnavailableStatusline(opts.statuslineFormat));
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
console.log("TUI dashboard not yet implemented. Use 'marmonitor status' for now.");
|
|
477
|
+
});
|
|
478
|
+
program
|
|
479
|
+
.command("status")
|
|
480
|
+
.description("Show current AI agent status")
|
|
481
|
+
.option("--json", "Output as JSON")
|
|
482
|
+
.option("--config <path>", "Path to settings.json")
|
|
483
|
+
.option("--show-dead", "Include dead sessions")
|
|
484
|
+
.action(async (opts) => {
|
|
485
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
486
|
+
if (opts.showDead)
|
|
487
|
+
config.display.showDead = true;
|
|
488
|
+
const agents = await getAgentsSnapshot(config, { enrichmentMode: "full" });
|
|
489
|
+
if (opts.json) {
|
|
490
|
+
await printStatusJson(agents);
|
|
491
|
+
}
|
|
492
|
+
else {
|
|
493
|
+
await printStatus(agents);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
program
|
|
497
|
+
.command("attention")
|
|
498
|
+
.description("Show prioritized sessions that need attention")
|
|
499
|
+
.option("--json", "Output as JSON")
|
|
500
|
+
.option("--interactive", "Choose an attention item and jump to its tmux pane")
|
|
501
|
+
.option("--limit <n>", "Max items to show", "12")
|
|
502
|
+
.option("--config <path>", "Path to settings.json")
|
|
503
|
+
.action(async (opts) => {
|
|
504
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
505
|
+
const agents = await getAgentsSnapshot(config, { enrichmentMode: "full" });
|
|
506
|
+
const limit = resolveAttentionLimit(opts, config.display.attentionLimit);
|
|
507
|
+
const interactiveLimit = resolveAttentionLimit(opts, config.display.attentionLimit, true);
|
|
508
|
+
if (opts.interactive && opts.json) {
|
|
509
|
+
console.error("--interactive cannot be combined with --json.");
|
|
510
|
+
process.exit(1);
|
|
511
|
+
}
|
|
512
|
+
if (opts.interactive) {
|
|
513
|
+
if (!process.stdin.isTTY) {
|
|
514
|
+
console.error("--interactive requires an interactive terminal.");
|
|
515
|
+
process.exit(1);
|
|
516
|
+
}
|
|
517
|
+
printJumpAttentionChooser(agents, interactiveLimit);
|
|
518
|
+
const selection = await promptSelectionKey(interactiveLimit);
|
|
519
|
+
if (selection === undefined)
|
|
520
|
+
return;
|
|
521
|
+
const item = selectJumpAttentionItem(agents, selection);
|
|
522
|
+
if (!item) {
|
|
523
|
+
console.error(`Invalid selection: ${selection}`);
|
|
524
|
+
process.exit(1);
|
|
525
|
+
}
|
|
526
|
+
const agent = agents.find((candidate) => candidate.pid === item.pid);
|
|
527
|
+
if (!agent) {
|
|
528
|
+
console.error(`AI session not found for pid ${item.pid}.`);
|
|
529
|
+
process.exit(1);
|
|
530
|
+
}
|
|
531
|
+
const result = await jumpToAgent(agent);
|
|
532
|
+
printJumpResult(result);
|
|
533
|
+
if (!result.found)
|
|
534
|
+
process.exit(1);
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (opts.json) {
|
|
538
|
+
printAttentionJson(agents, limit);
|
|
539
|
+
}
|
|
540
|
+
else {
|
|
541
|
+
printAttention(agents, limit);
|
|
542
|
+
}
|
|
543
|
+
});
|
|
544
|
+
program
|
|
545
|
+
.command("guard")
|
|
546
|
+
.description("Evaluate Claude hook payload and return allow/block decision")
|
|
547
|
+
.option("--config <path>", "Path to settings.json")
|
|
548
|
+
.action(async (opts) => {
|
|
549
|
+
try {
|
|
550
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
551
|
+
const input = await readStdin();
|
|
552
|
+
const event = parseHookEvent(input);
|
|
553
|
+
if (!event) {
|
|
554
|
+
console.log(JSON.stringify({ decision: "allow" }));
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const result = evaluateGuard(config, event);
|
|
558
|
+
console.log(formatGuardOutput(result));
|
|
559
|
+
}
|
|
560
|
+
catch {
|
|
561
|
+
console.log(JSON.stringify({ decision: "allow" }));
|
|
562
|
+
}
|
|
563
|
+
});
|
|
564
|
+
program
|
|
565
|
+
.command("debug-phase")
|
|
566
|
+
.description("Inspect phase signals, stdout heuristic, and tmux capture for one AI session")
|
|
567
|
+
.requiredOption("--pid <pid>", "AI process PID")
|
|
568
|
+
.option("--lines <n>", "Number of tmux pane lines to capture", "40")
|
|
569
|
+
.option("--json", "Output as JSON")
|
|
570
|
+
.option("--config <path>", "Path to settings.json")
|
|
571
|
+
.action(async (opts) => {
|
|
572
|
+
const pid = Number.parseInt(opts.pid, 10);
|
|
573
|
+
if (Number.isNaN(pid)) {
|
|
574
|
+
console.error(`Invalid pid: ${opts.pid}`);
|
|
575
|
+
process.exit(1);
|
|
576
|
+
}
|
|
577
|
+
const lines = Math.max(Number.parseInt(opts.lines, 10) || 40, 10);
|
|
578
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
579
|
+
let agents;
|
|
580
|
+
try {
|
|
581
|
+
agents = await scanAgents(config, { enrichmentMode: "full" });
|
|
582
|
+
}
|
|
583
|
+
catch (error) {
|
|
584
|
+
const payload = {
|
|
585
|
+
found: false,
|
|
586
|
+
pid,
|
|
587
|
+
error: formatProcessFailure(error),
|
|
588
|
+
};
|
|
589
|
+
if (opts.json) {
|
|
590
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
591
|
+
}
|
|
592
|
+
else {
|
|
593
|
+
console.error(payload.error);
|
|
594
|
+
}
|
|
595
|
+
process.exit(1);
|
|
596
|
+
}
|
|
597
|
+
const agent = agents.find((item) => item.pid === pid);
|
|
598
|
+
if (!agent) {
|
|
599
|
+
const payload = { found: false, pid };
|
|
600
|
+
if (opts.json) {
|
|
601
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
602
|
+
}
|
|
603
|
+
else {
|
|
604
|
+
console.log(`AI session not found for pid ${pid}.`);
|
|
605
|
+
}
|
|
606
|
+
process.exit(1);
|
|
607
|
+
}
|
|
608
|
+
const tmuxTarget = await resolveTmuxJumpTarget(agent);
|
|
609
|
+
const paneOutput = tmuxTarget ? await captureTmuxPaneOutput(tmuxTarget, lines) : undefined;
|
|
610
|
+
const stdoutPhase = agent.runtimeSource === "cli" ? await detectCliStdoutPhase(agent, config) : undefined;
|
|
611
|
+
let sessionPhase;
|
|
612
|
+
let sessionSourceFile;
|
|
613
|
+
let sessionLastActivityAt;
|
|
614
|
+
let sessionLastResponseAt;
|
|
615
|
+
if (agent.agentName === "Gemini") {
|
|
616
|
+
const gemini = await parseGeminiSession(agent.cwd);
|
|
617
|
+
sessionPhase = gemini.phase;
|
|
618
|
+
sessionSourceFile = gemini.sessionFile;
|
|
619
|
+
sessionLastActivityAt = gemini.lastActivityAt;
|
|
620
|
+
sessionLastResponseAt = gemini.lastResponseAt;
|
|
621
|
+
}
|
|
622
|
+
else if (agent.agentName === "Codex") {
|
|
623
|
+
const codexSessions = await indexCodexSessions(config);
|
|
624
|
+
const matched = matchCodexSession(agent.cwd, agent.startedAt, codexSessions);
|
|
625
|
+
sessionPhase = await detectCodexPhase(matched?.filePath, config);
|
|
626
|
+
sessionSourceFile = matched?.filePath;
|
|
627
|
+
}
|
|
628
|
+
else if (agent.agentName === "Claude Code" && agent.sessionId) {
|
|
629
|
+
const phaseResult = await detectClaudePhase(agent.sessionId, agent.cwd, agent.startedAt, config);
|
|
630
|
+
sessionPhase = phaseResult.phase;
|
|
631
|
+
sessionLastActivityAt = phaseResult.lastActivityAt;
|
|
632
|
+
sessionLastResponseAt = phaseResult.lastResponseAt;
|
|
633
|
+
}
|
|
634
|
+
const payload = {
|
|
635
|
+
found: true,
|
|
636
|
+
pid: agent.pid,
|
|
637
|
+
agent: agent.agentName,
|
|
638
|
+
cwd: agent.cwd,
|
|
639
|
+
runtimeSource: agent.runtimeSource,
|
|
640
|
+
status: agent.status,
|
|
641
|
+
finalPhase: agent.phase ?? null,
|
|
642
|
+
finalLastActivityAt: agent.lastActivityAt ?? null,
|
|
643
|
+
finalLastResponseAt: agent.lastResponseAt ?? null,
|
|
644
|
+
sessionPhase: sessionPhase ?? null,
|
|
645
|
+
sessionSourceFile: sessionSourceFile ?? null,
|
|
646
|
+
sessionLastActivityAt: sessionLastActivityAt ?? null,
|
|
647
|
+
sessionLastResponseAt: sessionLastResponseAt ?? null,
|
|
648
|
+
stdoutPhase: stdoutPhase ?? null,
|
|
649
|
+
tmuxTarget: tmuxTarget
|
|
650
|
+
? {
|
|
651
|
+
pane: tmuxTarget.pane.target,
|
|
652
|
+
match: tmuxTarget.match,
|
|
653
|
+
}
|
|
654
|
+
: null,
|
|
655
|
+
paneOutput: paneOutput?.trimEnd() ?? null,
|
|
656
|
+
};
|
|
657
|
+
if (opts.json) {
|
|
658
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
console.log(`PID: ${payload.pid}`);
|
|
662
|
+
console.log(`Agent: ${payload.agent}`);
|
|
663
|
+
console.log(`CWD: ${payload.cwd}`);
|
|
664
|
+
console.log(`Runtime: ${payload.runtimeSource ?? "unknown"}`);
|
|
665
|
+
console.log(`Status: ${payload.status}`);
|
|
666
|
+
console.log(`Final phase: ${payload.finalPhase ?? "(none)"}`);
|
|
667
|
+
console.log(`Session phase: ${payload.sessionPhase ?? "(none)"}`);
|
|
668
|
+
console.log(`Stdout phase: ${payload.stdoutPhase ?? "(none)"}`);
|
|
669
|
+
console.log(`Session file: ${payload.sessionSourceFile ?? "(none)"}`);
|
|
670
|
+
console.log(`tmux target: ${payload.tmuxTarget ? `${payload.tmuxTarget.pane} (${payload.tmuxTarget.match})` : "(none)"}`);
|
|
671
|
+
console.log("Pane capture:");
|
|
672
|
+
console.log(payload.paneOutput ?? "(none)");
|
|
673
|
+
});
|
|
674
|
+
program
|
|
675
|
+
.command("jump")
|
|
676
|
+
.description("Jump to the tmux pane running a specific AI session")
|
|
677
|
+
.option("--pid <pid>", "AI process PID")
|
|
678
|
+
.option("--attention", "Choose from attention items interactively")
|
|
679
|
+
.option("--attention-index <n>", "Jump to Nth jumpable attention item")
|
|
680
|
+
.option("--json", "Output as JSON")
|
|
681
|
+
.option("--config <path>", "Path to settings.json")
|
|
682
|
+
.action(async (opts) => {
|
|
683
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
684
|
+
const agents = await getAgentsSnapshot(config, { enrichmentMode: "full" });
|
|
685
|
+
const useAttention = Boolean(opts.attention);
|
|
686
|
+
const useAttentionIndex = typeof opts.attentionIndex === "string";
|
|
687
|
+
const usePid = typeof opts.pid === "string";
|
|
688
|
+
const selectedModes = [useAttention, useAttentionIndex, usePid].filter(Boolean).length;
|
|
689
|
+
if (selectedModes !== 1) {
|
|
690
|
+
console.error("Use exactly one of --pid <pid>, --attention, or --attention-index <n>.");
|
|
691
|
+
process.exit(1);
|
|
692
|
+
}
|
|
693
|
+
let pid;
|
|
694
|
+
if (useAttention) {
|
|
695
|
+
if (!process.stdin.isTTY) {
|
|
696
|
+
console.error("--attention requires an interactive terminal.");
|
|
697
|
+
process.exit(1);
|
|
698
|
+
}
|
|
699
|
+
const limit = resolveAttentionLimit({ limit: undefined }, config.display.attentionLimit, true);
|
|
700
|
+
printJumpAttentionChooser(agents, limit);
|
|
701
|
+
const selection = await promptSelectionKey(limit);
|
|
702
|
+
if (selection === undefined)
|
|
703
|
+
return;
|
|
704
|
+
const item = selectJumpAttentionItem(agents, selection);
|
|
705
|
+
if (!item) {
|
|
706
|
+
console.error(`Invalid selection: ${selection}`);
|
|
707
|
+
process.exit(1);
|
|
708
|
+
}
|
|
709
|
+
pid = item.pid;
|
|
710
|
+
}
|
|
711
|
+
else if (useAttentionIndex) {
|
|
712
|
+
const selection = Number.parseInt(opts.attentionIndex, 10);
|
|
713
|
+
if (Number.isNaN(selection) || selection < 1) {
|
|
714
|
+
console.error(`Invalid attention index: ${opts.attentionIndex}`);
|
|
715
|
+
process.exit(1);
|
|
716
|
+
}
|
|
717
|
+
const item = selectJumpAttentionItem(agents, selection);
|
|
718
|
+
if (!item) {
|
|
719
|
+
console.error(`No jumpable attention item at index ${selection}.`);
|
|
720
|
+
process.exit(1);
|
|
721
|
+
}
|
|
722
|
+
pid = item.pid;
|
|
723
|
+
}
|
|
724
|
+
else {
|
|
725
|
+
pid = Number.parseInt(opts.pid, 10);
|
|
726
|
+
if (Number.isNaN(pid)) {
|
|
727
|
+
console.error(`Invalid pid: ${opts.pid}`);
|
|
728
|
+
process.exit(1);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
const agent = agents.find((item) => item.pid === pid);
|
|
732
|
+
if (!agent) {
|
|
733
|
+
const result = {
|
|
734
|
+
found: false,
|
|
735
|
+
executed: false,
|
|
736
|
+
insideTmux: Boolean(process.env.TMUX),
|
|
737
|
+
pid,
|
|
738
|
+
message: `AI session not found for pid ${pid}.`,
|
|
739
|
+
};
|
|
740
|
+
if (opts.json) {
|
|
741
|
+
printJumpJson(result);
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
printJumpResult(result);
|
|
745
|
+
}
|
|
746
|
+
process.exit(1);
|
|
747
|
+
}
|
|
748
|
+
const result = await jumpToAgent(agent);
|
|
749
|
+
if (opts.json) {
|
|
750
|
+
printJumpJson(result);
|
|
751
|
+
}
|
|
752
|
+
else {
|
|
753
|
+
printJumpResult(result);
|
|
754
|
+
}
|
|
755
|
+
if (!result.found)
|
|
756
|
+
process.exit(1);
|
|
757
|
+
});
|
|
758
|
+
program
|
|
759
|
+
.command("dock")
|
|
760
|
+
.description("Compact persistent monitor for tmux pane")
|
|
761
|
+
.option("--interval <sec>", "Refresh interval in seconds", "5")
|
|
762
|
+
.option("--detail-interval <sec>", "Heavy enrichment interval", "30")
|
|
763
|
+
.option("--lines <n>", "Max display lines", "12")
|
|
764
|
+
.option("--config <path>", "Path to settings.json")
|
|
765
|
+
.action(async (opts) => {
|
|
766
|
+
const intervalMs = Math.max(Number(opts.interval) || 5, 2) * 1000;
|
|
767
|
+
const detailMs = Math.max(Number(opts.detailInterval) || 30, 5) * 1000;
|
|
768
|
+
const maxLines = Number(opts.lines) || 12;
|
|
769
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
770
|
+
let lastHeavyAt = 0;
|
|
771
|
+
while (true) {
|
|
772
|
+
const now = Date.now();
|
|
773
|
+
const needsHeavy = lastHeavyAt === 0 || now - lastHeavyAt >= detailMs;
|
|
774
|
+
const agents = await scanAgents(config, {
|
|
775
|
+
enrichmentMode: needsHeavy ? "full" : "light",
|
|
776
|
+
});
|
|
777
|
+
if (needsHeavy)
|
|
778
|
+
lastHeavyAt = now;
|
|
779
|
+
clearScreen();
|
|
780
|
+
await printDock(agents, maxLines);
|
|
781
|
+
await sleep(intervalMs);
|
|
782
|
+
}
|
|
783
|
+
});
|
|
784
|
+
program
|
|
785
|
+
.command("watch")
|
|
786
|
+
.description("Refresh agent status in a long-lived loop")
|
|
787
|
+
.option("--interval <sec>", "Refresh interval in seconds", "10")
|
|
788
|
+
.option("--detail-interval <sec>", "Heavy enrichment refresh interval in seconds", "30")
|
|
789
|
+
.option("--json", "Output as JSON")
|
|
790
|
+
.option("--config <path>", "Path to settings.json")
|
|
791
|
+
.option("--show-dead", "Include dead sessions")
|
|
792
|
+
.action(async (opts) => {
|
|
793
|
+
const intervalSec = Number(opts.interval);
|
|
794
|
+
const detailIntervalSec = Number(opts.detailInterval);
|
|
795
|
+
const intervalMs = Number.isFinite(intervalSec) && intervalSec > 0 ? intervalSec * 1000 : 10_000;
|
|
796
|
+
const detailIntervalMs = Number.isFinite(detailIntervalSec) && detailIntervalSec > 0
|
|
797
|
+
? detailIntervalSec * 1000
|
|
798
|
+
: 30_000;
|
|
799
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
800
|
+
if (opts.showDead)
|
|
801
|
+
config.display.showDead = true;
|
|
802
|
+
let lastHeavyAt = 0;
|
|
803
|
+
while (true) {
|
|
804
|
+
const now = Date.now();
|
|
805
|
+
const needsHeavy = lastHeavyAt === 0 || now - lastHeavyAt >= detailIntervalMs;
|
|
806
|
+
const agents = await scanAgents(config, {
|
|
807
|
+
enrichmentMode: needsHeavy ? "full" : "light",
|
|
808
|
+
});
|
|
809
|
+
if (needsHeavy)
|
|
810
|
+
lastHeavyAt = now;
|
|
811
|
+
clearScreen();
|
|
812
|
+
if (opts.json) {
|
|
813
|
+
await printStatusJson(agents);
|
|
814
|
+
}
|
|
815
|
+
else {
|
|
816
|
+
await printStatus(agents);
|
|
817
|
+
}
|
|
818
|
+
await sleep(intervalMs);
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
program
|
|
822
|
+
.command("clean")
|
|
823
|
+
.description("Show or terminate unmatched processes")
|
|
824
|
+
.option("--config <path>", "Path to settings.json")
|
|
825
|
+
.option("--json", "Output as JSON")
|
|
826
|
+
.option("--kill", "Send SIGTERM to selected unmatched processes")
|
|
827
|
+
.option("--pid <pid...>", "Only include specific unmatched PID(s)")
|
|
828
|
+
.action(async (opts) => {
|
|
829
|
+
const config = await loadConfig(resolveConfigPath(opts));
|
|
830
|
+
const agents = await scanAgents(config);
|
|
831
|
+
const selectedPids = Array.isArray(opts.pid)
|
|
832
|
+
? opts.pid
|
|
833
|
+
.map((value) => Number.parseInt(value, 10))
|
|
834
|
+
.filter((value) => !Number.isNaN(value))
|
|
835
|
+
: undefined;
|
|
836
|
+
const targets = selectUnmatchedTargets(agents, selectedPids);
|
|
837
|
+
if (opts.kill) {
|
|
838
|
+
for (const target of targets) {
|
|
839
|
+
try {
|
|
840
|
+
process.kill(target.pid, "SIGTERM");
|
|
841
|
+
}
|
|
842
|
+
catch {
|
|
843
|
+
// keep going; the process may already be gone
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
if (opts.json) {
|
|
848
|
+
printCleanJson(targets, Boolean(opts.kill));
|
|
849
|
+
}
|
|
850
|
+
else {
|
|
851
|
+
printCleanPlan(targets, Boolean(opts.kill));
|
|
852
|
+
}
|
|
853
|
+
});
|
|
854
|
+
installProcessSafetyHandlers();
|
|
855
|
+
program.parse();
|