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
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
import chalk from "chalk";
|
|
2
|
+
import * as si from "systeminformation";
|
|
3
|
+
import { buildAttentionFocusText, buildAttentionItems, buildJumpAttentionItems, buildStatuslineSummary, buildTmuxAttentionPills, buildTmuxBadgeBar, compactDirLabel, formatElapsed, formatTokens, serializeWeztermPills, shortenPath, } from "./utils.js";
|
|
4
|
+
/** Collect system resource info */
|
|
5
|
+
export async function getSystemInfo() {
|
|
6
|
+
const [cpu, mem, battery] = await Promise.all([si.currentLoad(), si.mem(), si.battery()]);
|
|
7
|
+
const info = {
|
|
8
|
+
cpuPercent: Math.round(cpu.currentLoad * 10) / 10,
|
|
9
|
+
memoryUsedGb: Math.round((mem.used / 1024 ** 3) * 10) / 10,
|
|
10
|
+
memoryTotalGb: Math.round(mem.total / 1024 ** 3),
|
|
11
|
+
};
|
|
12
|
+
if (battery.hasBattery) {
|
|
13
|
+
info.batteryPercent = battery.percent;
|
|
14
|
+
info.batteryCharging = battery.isCharging;
|
|
15
|
+
}
|
|
16
|
+
return info;
|
|
17
|
+
}
|
|
18
|
+
// shortenPath, formatElapsed, formatTokens imported from ./utils.js
|
|
19
|
+
/** Agent name with distinct color */
|
|
20
|
+
function agentLabel(name) {
|
|
21
|
+
switch (name) {
|
|
22
|
+
case "Claude Code":
|
|
23
|
+
return chalk.hex("#D97706")("Claude"); // amber/orange
|
|
24
|
+
case "Codex":
|
|
25
|
+
return chalk.hex("#10B981")("Codex"); // emerald/green
|
|
26
|
+
case "Gemini":
|
|
27
|
+
return chalk.hex("#3B82F6")("Gemini"); // blue
|
|
28
|
+
default:
|
|
29
|
+
return chalk.white(name);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** Status icon/color */
|
|
33
|
+
function statusLabel(status) {
|
|
34
|
+
switch (status) {
|
|
35
|
+
case "Active":
|
|
36
|
+
return chalk.green("[Active] ");
|
|
37
|
+
case "Idle":
|
|
38
|
+
return chalk.yellow("[Idle] ");
|
|
39
|
+
case "Stalled":
|
|
40
|
+
return chalk.red("[Stalled] ");
|
|
41
|
+
case "Unmatched":
|
|
42
|
+
return chalk.magenta("[Unmatched]");
|
|
43
|
+
case "Dead":
|
|
44
|
+
return chalk.gray("[Dead] ");
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function displayStatusLabel(status, phase) {
|
|
48
|
+
if (phase === "permission")
|
|
49
|
+
return chalk.red("[Allow] ");
|
|
50
|
+
if (phase === "tool")
|
|
51
|
+
return chalk.green("[Tool] ");
|
|
52
|
+
if (phase === "thinking")
|
|
53
|
+
return chalk.magenta("[Think] ");
|
|
54
|
+
return statusLabel(status);
|
|
55
|
+
}
|
|
56
|
+
/** Phase emoji */
|
|
57
|
+
function phaseIcon(phase) {
|
|
58
|
+
switch (phase) {
|
|
59
|
+
case "thinking":
|
|
60
|
+
return " 🤔";
|
|
61
|
+
case "tool":
|
|
62
|
+
return " 🔧";
|
|
63
|
+
case "permission":
|
|
64
|
+
return " ⏳";
|
|
65
|
+
case "done":
|
|
66
|
+
return " ✅";
|
|
67
|
+
default:
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function runtimeLabel(source) {
|
|
72
|
+
switch (source) {
|
|
73
|
+
case "cli":
|
|
74
|
+
return chalk.dim(" [cli]");
|
|
75
|
+
case "vscode":
|
|
76
|
+
return chalk.dim(" [vscode]");
|
|
77
|
+
default:
|
|
78
|
+
return "";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/** Format token usage as a compact string */
|
|
82
|
+
function formatTokenUsage(t, model) {
|
|
83
|
+
if (!t)
|
|
84
|
+
return "";
|
|
85
|
+
const parts = [`in:${formatTokens(t.inputTokens)}`, `out:${formatTokens(t.outputTokens)}`];
|
|
86
|
+
if (t.cacheReadTokens > 0)
|
|
87
|
+
parts.push(`cache:${formatTokens(t.cacheReadTokens)}`);
|
|
88
|
+
const modelStr = model ? chalk.dim(` [${model}]`) : "";
|
|
89
|
+
return chalk.cyan(` tokens(${parts.join(" ")})`) + modelStr;
|
|
90
|
+
}
|
|
91
|
+
/** Print system info line */
|
|
92
|
+
function printSystemLine(sys) {
|
|
93
|
+
let line = `System: CPU ${sys.cpuPercent}% | MEM ${sys.memoryUsedGb}/${sys.memoryTotalGb}GB`;
|
|
94
|
+
if (sys.batteryPercent !== undefined) {
|
|
95
|
+
const plug = sys.batteryCharging ? "+" : "";
|
|
96
|
+
line += ` | Battery ${sys.batteryPercent}%${plug}`;
|
|
97
|
+
}
|
|
98
|
+
console.log(line);
|
|
99
|
+
}
|
|
100
|
+
/** Print agent status as formatted text */
|
|
101
|
+
export async function printStatus(agents) {
|
|
102
|
+
const sys = await getSystemInfo();
|
|
103
|
+
printSystemLine(sys);
|
|
104
|
+
console.log();
|
|
105
|
+
const alive = agents.filter((a) => a.status !== "Dead" && a.status !== "Unmatched");
|
|
106
|
+
const unmatched = agents.filter((a) => a.status === "Unmatched");
|
|
107
|
+
const dead = agents.filter((a) => a.status === "Dead");
|
|
108
|
+
if (alive.length === 0 && unmatched.length === 0 && dead.length === 0) {
|
|
109
|
+
console.log("No AI agent sessions detected.");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (alive.length > 0) {
|
|
113
|
+
console.log(`AI Sessions (${alive.length} active):`);
|
|
114
|
+
for (const a of alive) {
|
|
115
|
+
const icon = displayStatusLabel(a.status, a.phase);
|
|
116
|
+
const path = shortenPath(a.cwd);
|
|
117
|
+
const started = formatElapsed(a.startedAt);
|
|
118
|
+
const tokens = formatTokenUsage(a.tokenUsage, a.model);
|
|
119
|
+
const agent = agentLabel(a.agentName).padEnd(18);
|
|
120
|
+
const phase = phaseIcon(a.phase);
|
|
121
|
+
const runtime = runtimeLabel(a.runtimeSource);
|
|
122
|
+
const lastTime = a.lastResponseAt
|
|
123
|
+
? chalk.dim(` last response ${formatElapsed(a.lastResponseAt)}`)
|
|
124
|
+
: a.lastActivityAt
|
|
125
|
+
? chalk.dim(` last activity ${formatElapsed(a.lastActivityAt)}`)
|
|
126
|
+
: "";
|
|
127
|
+
console.log(` ${icon} ${agent} PID:${String(a.pid).padEnd(6)} ${path}${runtime}${phase}`);
|
|
128
|
+
console.log(` CPU:${a.cpuPercent}% MEM:${a.memoryMb.toFixed(0)}MB started ${started}${lastTime}${tokens}`);
|
|
129
|
+
if (a.workers && a.workers.length > 0) {
|
|
130
|
+
for (let i = 0; i < a.workers.length; i++) {
|
|
131
|
+
const w = a.workers[i];
|
|
132
|
+
const isLast = i === a.workers.length - 1;
|
|
133
|
+
const branch = isLast ? "└──" : "├──";
|
|
134
|
+
const wStatus = statusLabel(w.status).trim();
|
|
135
|
+
console.log(chalk.dim(` ${branch} PID:${String(w.pid).padEnd(6)} CPU:${w.cpuPercent}% MEM:${w.memoryMb.toFixed(0)}MB ${wStatus}`));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (unmatched.length > 0) {
|
|
141
|
+
console.log(chalk.magenta(`\nUnmatched Processes (${unmatched.length}) — no matching session, consider killing:`));
|
|
142
|
+
for (const a of unmatched) {
|
|
143
|
+
const agent = agentLabel(a.agentName);
|
|
144
|
+
console.log(` ${chalk.magenta("[Unmatched]")} ${agent} PID:${a.pid} MEM:${a.memoryMb.toFixed(0)}MB${runtimeLabel(a.runtimeSource)} ${chalk.dim(`kill ${a.pid}`)}`);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (dead.length > 0) {
|
|
148
|
+
console.log(`\nDead Sessions (${dead.length}):`);
|
|
149
|
+
for (const a of dead) {
|
|
150
|
+
const path = shortenPath(a.cwd);
|
|
151
|
+
const started = formatElapsed(a.startedAt);
|
|
152
|
+
const tokens = formatTokenUsage(a.tokenUsage, a.model);
|
|
153
|
+
const agent = agentLabel(a.agentName);
|
|
154
|
+
console.log(` ${chalk.gray("[Dead]")} ${agent} PID:${String(a.pid).padEnd(6)} ${path} started ${started}${tokens}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/** Print agent status as JSON */
|
|
159
|
+
export async function printStatusJson(agents) {
|
|
160
|
+
const sys = await getSystemInfo();
|
|
161
|
+
console.log(JSON.stringify({ system: sys, agents }, null, 2));
|
|
162
|
+
}
|
|
163
|
+
export function printCleanPlan(agents, kill) {
|
|
164
|
+
if (agents.length === 0) {
|
|
165
|
+
console.log("No unmatched processes to clean.");
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
console.log(kill
|
|
169
|
+
? `Cleaning ${agents.length} unmatched process(es) with SIGTERM:`
|
|
170
|
+
: `Cleanup candidates (${agents.length}) — rerun with --kill to terminate:`);
|
|
171
|
+
for (const agent of agents) {
|
|
172
|
+
const label = agentLabel(agent.agentName);
|
|
173
|
+
console.log(` ${label} PID:${agent.pid} MEM:${agent.memoryMb.toFixed(0)}MB${runtimeLabel(agent.runtimeSource)} ${shortenPath(agent.cwd)}`);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
export function printCleanJson(agents, executed, signal = "SIGTERM") {
|
|
177
|
+
console.log(JSON.stringify({
|
|
178
|
+
executed,
|
|
179
|
+
signal,
|
|
180
|
+
count: agents.length,
|
|
181
|
+
targets: agents.map((agent) => ({
|
|
182
|
+
agentName: agent.agentName,
|
|
183
|
+
pid: agent.pid,
|
|
184
|
+
cwd: agent.cwd,
|
|
185
|
+
runtimeSource: agent.runtimeSource,
|
|
186
|
+
memoryMb: agent.memoryMb,
|
|
187
|
+
})),
|
|
188
|
+
}, null, 2));
|
|
189
|
+
}
|
|
190
|
+
function attentionKindLabel(kind) {
|
|
191
|
+
switch (kind) {
|
|
192
|
+
case "unmatched":
|
|
193
|
+
return "⚠ Orphan";
|
|
194
|
+
case "permission":
|
|
195
|
+
return "⏳ Allow Waiting";
|
|
196
|
+
case "stalled":
|
|
197
|
+
return "⚠ Stalled";
|
|
198
|
+
case "thinking":
|
|
199
|
+
return "🤔 Thinking";
|
|
200
|
+
case "tool":
|
|
201
|
+
return "🔧 Tool";
|
|
202
|
+
case "active":
|
|
203
|
+
return "• Recent";
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
function attentionLine(item) {
|
|
207
|
+
const name = item.agentName === "Claude Code" ? "Claude" : item.agentName;
|
|
208
|
+
const path = compactDirLabel(item.cwd);
|
|
209
|
+
const runtime = runtimeLabel(item.runtimeSource);
|
|
210
|
+
const time = item.lastResponseAt
|
|
211
|
+
? ` ${chalk.dim(formatElapsed(item.lastResponseAt))}`
|
|
212
|
+
: item.lastActivityAt
|
|
213
|
+
? ` ${chalk.dim(formatElapsed(item.lastActivityAt))}`
|
|
214
|
+
: "";
|
|
215
|
+
if (item.kind === "unmatched") {
|
|
216
|
+
return `${name} ${chalk.dim(path)}${runtime} PID:${item.pid}${time}`;
|
|
217
|
+
}
|
|
218
|
+
if (item.kind === "permission") {
|
|
219
|
+
return `${name} ${chalk.dim(path)}${runtime} allow pending PID:${item.pid}${time}`;
|
|
220
|
+
}
|
|
221
|
+
if (item.kind === "stalled") {
|
|
222
|
+
return `${name} ${chalk.dim(path)}${runtime} stalled PID:${item.pid}${time}`;
|
|
223
|
+
}
|
|
224
|
+
if (item.kind === "thinking") {
|
|
225
|
+
return `${name} ${chalk.dim(path)}${runtime} thinking PID:${item.pid}${time}`;
|
|
226
|
+
}
|
|
227
|
+
if (item.kind === "active") {
|
|
228
|
+
return `${name} ${chalk.dim(path)}${runtime} recent PID:${item.pid}${time}`;
|
|
229
|
+
}
|
|
230
|
+
return `${name} ${chalk.dim(path)}${runtime} tool PID:${item.pid}${time}`;
|
|
231
|
+
}
|
|
232
|
+
function jumpAttentionStatusLabel(item) {
|
|
233
|
+
if (item.kind === "permission")
|
|
234
|
+
return chalk.red("[Allow] ");
|
|
235
|
+
if (item.kind === "thinking")
|
|
236
|
+
return chalk.green("[Thinking]");
|
|
237
|
+
if (item.kind === "tool")
|
|
238
|
+
return chalk.cyan("[Tool] ");
|
|
239
|
+
return chalk.blue("[Recent] ");
|
|
240
|
+
}
|
|
241
|
+
function jumpAttentionPhaseIcon(item) {
|
|
242
|
+
if (item.kind === "permission")
|
|
243
|
+
return " ⏳";
|
|
244
|
+
if (item.kind === "thinking")
|
|
245
|
+
return " 🤔";
|
|
246
|
+
if (item.kind === "tool")
|
|
247
|
+
return " 🔧";
|
|
248
|
+
return " •";
|
|
249
|
+
}
|
|
250
|
+
function jumpAttentionLine(item) {
|
|
251
|
+
const name = item.agentName === "Claude Code" ? "Claude" : item.agentName;
|
|
252
|
+
const path = compactDirLabel(item.cwd);
|
|
253
|
+
const runtime = runtimeLabel(item.runtimeSource);
|
|
254
|
+
const started = item.startedAt ? chalk.dim(` started ${formatElapsed(item.startedAt)}`) : "";
|
|
255
|
+
const activity = item.lastResponseAt
|
|
256
|
+
? chalk.dim(` last response ${formatElapsed(item.lastResponseAt)}`)
|
|
257
|
+
: item.lastActivityAt
|
|
258
|
+
? chalk.dim(` last activity ${formatElapsed(item.lastActivityAt)}`)
|
|
259
|
+
: "";
|
|
260
|
+
const stats = `CPU:${item.cpuPercent ?? 0}% MEM:${item.memoryMb?.toFixed(0) ?? "0"}MB`;
|
|
261
|
+
const status = jumpAttentionStatusLabel(item);
|
|
262
|
+
const icon = jumpAttentionPhaseIcon(item);
|
|
263
|
+
const agent = agentLabel(item.agentName).padEnd(8);
|
|
264
|
+
if (item.kind === "permission") {
|
|
265
|
+
return ` ${status} ${agent} ${chalk.dim(path)}${runtime}${icon}\n PID:${item.pid} ${stats}${started}${activity}`;
|
|
266
|
+
}
|
|
267
|
+
if (item.kind === "thinking") {
|
|
268
|
+
return ` ${status} ${agent} ${chalk.dim(path)}${runtime}${icon}\n PID:${item.pid} ${stats}${started}${activity}`;
|
|
269
|
+
}
|
|
270
|
+
if (item.kind === "tool") {
|
|
271
|
+
return ` ${status} ${agent} ${chalk.dim(path)}${runtime}${icon}\n PID:${item.pid} ${stats}${started}${activity}`;
|
|
272
|
+
}
|
|
273
|
+
return ` ${status} ${agent} ${chalk.dim(path)}${runtime}${icon}\n PID:${item.pid} ${stats}${started}${activity}`;
|
|
274
|
+
}
|
|
275
|
+
export function printAttention(agents, limit = 12) {
|
|
276
|
+
const items = buildAttentionItems(agents);
|
|
277
|
+
if (items.length === 0) {
|
|
278
|
+
console.log("No attention items.");
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const shown = items.slice(0, limit);
|
|
282
|
+
const groups = new Map();
|
|
283
|
+
for (const item of shown) {
|
|
284
|
+
const existing = groups.get(item.kind) ?? [];
|
|
285
|
+
existing.push(item);
|
|
286
|
+
groups.set(item.kind, existing);
|
|
287
|
+
}
|
|
288
|
+
console.log(`Attention (${items.length}):`);
|
|
289
|
+
for (const [kind, grouped] of groups.entries()) {
|
|
290
|
+
console.log(`\n${attentionKindLabel(kind)} (${grouped.length}):`);
|
|
291
|
+
grouped.forEach((item, index) => {
|
|
292
|
+
console.log(` ${index + 1}) ${attentionLine(item)}`);
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
if (shown.length < items.length) {
|
|
296
|
+
console.log(`\n${chalk.dim(`+${items.length - shown.length} more`)}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
export function printAttentionJson(agents, limit = 12) {
|
|
300
|
+
const items = buildAttentionItems(agents).slice(0, limit);
|
|
301
|
+
console.log(JSON.stringify({ count: items.length, items }, null, 2));
|
|
302
|
+
}
|
|
303
|
+
export function printAttentionChooser(agents, limit = 12) {
|
|
304
|
+
const items = buildAttentionItems(agents).slice(0, limit);
|
|
305
|
+
if (items.length === 0) {
|
|
306
|
+
console.log("No attention items.");
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
console.log("Select attention item:");
|
|
310
|
+
items.forEach((item, index) => {
|
|
311
|
+
console.log(` ${index + 1}) ${attentionLine(item)}`);
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
export function printJumpAttentionChooser(agents, limit = 12) {
|
|
315
|
+
const items = buildJumpAttentionItems(agents).slice(0, limit);
|
|
316
|
+
if (items.length === 0) {
|
|
317
|
+
console.log("No jumpable attention items.");
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
console.log(`Select session to jump to (${items.length}):`);
|
|
321
|
+
items.forEach((item, index) => {
|
|
322
|
+
const [firstLine, ...restLines] = jumpAttentionLine(item).split("\n");
|
|
323
|
+
const indexLabel = `${index + 1})`.padStart(3);
|
|
324
|
+
console.log(`${indexLabel} ${firstLine.trimStart()}`);
|
|
325
|
+
for (const line of restLines) {
|
|
326
|
+
console.log(` ${line.trimStart()}`);
|
|
327
|
+
}
|
|
328
|
+
if (index < items.length - 1) {
|
|
329
|
+
console.log(chalk.dim(" ───────────────────────────────────────────────────────────"));
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
console.log(chalk.dim("\nPress 1-9 to jump, 0 for item 10, q to cancel."));
|
|
333
|
+
}
|
|
334
|
+
export function printJumpResult(result) {
|
|
335
|
+
if (!result.found) {
|
|
336
|
+
console.log(chalk.yellow(result.message ?? "No matching tmux pane found."));
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (!result.executed) {
|
|
340
|
+
console.log(chalk.yellow(result.message ?? "Matched tmux pane but switch failed."));
|
|
341
|
+
return;
|
|
342
|
+
}
|
|
343
|
+
const target = result.target ? chalk.bold(result.target) : chalk.bold("unknown");
|
|
344
|
+
const match = result.match ? chalk.dim(`(${result.match})`) : "";
|
|
345
|
+
console.log(`Jumped to ${target} ${match}`.trim());
|
|
346
|
+
}
|
|
347
|
+
export function printJumpJson(result) {
|
|
348
|
+
console.log(JSON.stringify(result, null, 2));
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Print compact dock view for tmux side/bottom pane.
|
|
352
|
+
* Prioritizes attention signals over completeness.
|
|
353
|
+
*/
|
|
354
|
+
export async function printDock(agents, maxLines = 12) {
|
|
355
|
+
const sys = await getSystemInfo();
|
|
356
|
+
const alive = agents.filter((a) => a.status !== "Dead" && a.status !== "Unmatched");
|
|
357
|
+
const unmatched = agents.filter((a) => a.status === "Unmatched");
|
|
358
|
+
const waitingCount = alive.filter((a) => a.phase === "permission").length;
|
|
359
|
+
const stalledCount = alive.filter((a) => a.status === "Stalled").length;
|
|
360
|
+
const alerts = [];
|
|
361
|
+
if (waitingCount > 0)
|
|
362
|
+
alerts.push(chalk.red(`!${waitingCount} waiting`));
|
|
363
|
+
if (stalledCount > 0)
|
|
364
|
+
alerts.push(chalk.yellow(`${stalledCount} stalled`));
|
|
365
|
+
if (unmatched.length > 0)
|
|
366
|
+
alerts.push(chalk.magenta(`${unmatched.length} orphan`));
|
|
367
|
+
const alertStr = alerts.length > 0 ? ` ${alerts.join(" ")}` : "";
|
|
368
|
+
console.log(`${chalk.bold("AI")} ${alive.length}/${agents.length} CPU:${sys.cpuPercent.toFixed(0)}% MEM:${sys.memoryUsedGb}G${alertStr}`);
|
|
369
|
+
console.log(chalk.dim("─".repeat(36)));
|
|
370
|
+
const prioritized = [...alive].sort((a, b) => {
|
|
371
|
+
const pri = (agent) => {
|
|
372
|
+
if (agent.phase === "permission")
|
|
373
|
+
return 0;
|
|
374
|
+
if (agent.status === "Stalled")
|
|
375
|
+
return 1;
|
|
376
|
+
if (agent.status === "Active")
|
|
377
|
+
return 2;
|
|
378
|
+
return 3;
|
|
379
|
+
};
|
|
380
|
+
return pri(a) - pri(b);
|
|
381
|
+
});
|
|
382
|
+
const unmatchedLine = unmatched.length > 0 ? 1 : 0;
|
|
383
|
+
const slots = Math.max(maxLines - 2 - unmatchedLine, 0);
|
|
384
|
+
const shown = prioritized.slice(0, slots);
|
|
385
|
+
for (const agent of shown) {
|
|
386
|
+
const coloredAgent = agentLabel(agent.agentName);
|
|
387
|
+
const phase = phaseIcon(agent.phase);
|
|
388
|
+
const lastDir = compactDirLabel(agent.cwd);
|
|
389
|
+
const time = agent.lastResponseAt
|
|
390
|
+
? formatElapsed(agent.lastResponseAt)
|
|
391
|
+
: agent.lastActivityAt
|
|
392
|
+
? formatElapsed(agent.lastActivityAt)
|
|
393
|
+
: "";
|
|
394
|
+
const timeStr = time ? chalk.dim(` ${time}`) : "";
|
|
395
|
+
console.log(`${coloredAgent} ${chalk.dim(lastDir)}${phase}${timeStr}`);
|
|
396
|
+
}
|
|
397
|
+
if (shown.length < prioritized.length) {
|
|
398
|
+
console.log(chalk.dim(` +${prioritized.length - shown.length} more`));
|
|
399
|
+
}
|
|
400
|
+
if (unmatched.length > 0) {
|
|
401
|
+
console.log(chalk.magenta(`! ${unmatched.length} unmatched (marmonitor clean)`));
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
/** Print one-line summary for tmux/terminal status bar */
|
|
405
|
+
export function renderUnavailableStatusline(format = "compact") {
|
|
406
|
+
if (format === "wezterm-pills") {
|
|
407
|
+
return "focus\tmarmonitor unavailable\t#bac2de\t#313244";
|
|
408
|
+
}
|
|
409
|
+
return "marmonitor unavailable";
|
|
410
|
+
}
|
|
411
|
+
/** Print one-line summary for tmux/terminal status bar */
|
|
412
|
+
export async function renderStatusline(agents, format = "compact", attentionLimit = 5, width) {
|
|
413
|
+
const alive = agents.filter((a) => a.status !== "Dead" && a.status !== "Unmatched");
|
|
414
|
+
const unmatched = agents.filter((a) => a.status === "Unmatched");
|
|
415
|
+
if (alive.length === 0) {
|
|
416
|
+
return format === "wezterm-pills" ? renderUnavailableStatusline(format) : "AI:0";
|
|
417
|
+
}
|
|
418
|
+
const sys = await getSystemInfo();
|
|
419
|
+
const waitingCount = alive.filter((a) => a.phase === "permission").length;
|
|
420
|
+
const stalledCount = alive.filter((a) => a.status === "Stalled").length;
|
|
421
|
+
const activeCount = alive.filter((a) => a.status === "Active").length;
|
|
422
|
+
const highCpuCount = alive.filter((a) => a.cpuPercent >= 10).length;
|
|
423
|
+
const thinkingCount = alive.filter((a) => a.phase === "thinking").length;
|
|
424
|
+
const toolCount = alive.filter((a) => a.phase === "tool").length;
|
|
425
|
+
const claudeCount = alive.filter((a) => a.agentName === "Claude Code").length;
|
|
426
|
+
const codexCount = alive.filter((a) => a.agentName === "Codex").length;
|
|
427
|
+
const geminiCount = alive.filter((a) => a.agentName === "Gemini").length;
|
|
428
|
+
const snapshot = {
|
|
429
|
+
aliveCount: alive.length,
|
|
430
|
+
waitingCount,
|
|
431
|
+
riskCount: 0,
|
|
432
|
+
stalledCount,
|
|
433
|
+
unmatchedCount: unmatched.length,
|
|
434
|
+
activeCount,
|
|
435
|
+
highCpuCount,
|
|
436
|
+
thinkingCount,
|
|
437
|
+
toolCount,
|
|
438
|
+
claudeCount,
|
|
439
|
+
codexCount,
|
|
440
|
+
geminiCount,
|
|
441
|
+
cpuPercent: sys.cpuPercent,
|
|
442
|
+
memoryUsedGb: sys.memoryUsedGb,
|
|
443
|
+
};
|
|
444
|
+
if (format === "tmux-badges") {
|
|
445
|
+
const focusText = buildTmuxAttentionPills(buildJumpAttentionItems(agents), attentionLimit, width);
|
|
446
|
+
return buildTmuxBadgeBar(snapshot, focusText);
|
|
447
|
+
}
|
|
448
|
+
if (format === "wezterm-pills") {
|
|
449
|
+
const focusText = buildAttentionFocusText(buildAttentionItems(agents), attentionLimit, width);
|
|
450
|
+
return serializeWeztermPills(snapshot, focusText);
|
|
451
|
+
}
|
|
452
|
+
return buildStatuslineSummary(snapshot, format);
|
|
453
|
+
}
|
|
454
|
+
/** Print one-line summary for tmux/terminal status bar */
|
|
455
|
+
export async function printStatusline(agents, format = "compact", attentionLimit = 5, width) {
|
|
456
|
+
console.log(await renderStatusline(agents, format, attentionLimit, width));
|
|
457
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type { AgentSession, SessionPhase } from "../types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Pure utility functions for marmonitor.
|
|
4
|
+
* Extracted for testability — no side effects, no I/O.
|
|
5
|
+
*/
|
|
6
|
+
/** Shorten home directory path with ~ */
|
|
7
|
+
export declare function shortenPath(path: string, home?: string): string;
|
|
8
|
+
/** Format elapsed time from epoch seconds to human-readable */
|
|
9
|
+
export declare function formatElapsed(epochSec?: number, now?: number): string;
|
|
10
|
+
/** Compact elapsed label for status bars/popups.
|
|
11
|
+
* e.g. 26s, 3m, 2h, 1d */
|
|
12
|
+
export declare function formatElapsedCompact(epochSec?: number, now?: number): string | undefined;
|
|
13
|
+
/** Format token count (e.g. 1234 -> "1.2K", 1234567 -> "1.2M") */
|
|
14
|
+
export declare function formatTokens(n: number): string;
|
|
15
|
+
/** Compact directory label for dock/focus display.
|
|
16
|
+
* Shows last 2 path segments to avoid basename collisions.
|
|
17
|
+
* e.g. "/Users/macrent/.ai/projects/mjjo" → "projects/mjjo"
|
|
18
|
+
* "/Users/macrent/Documents/valueofspace/vos-data-service" → "valueofspace/vos-data-service"
|
|
19
|
+
* "/Users/macrent/.ai/projects/vos" → "projects/vos" */
|
|
20
|
+
export declare function compactDirLabel(cwd: string): string;
|
|
21
|
+
/** Compact directory label for narrow statusline surfaces.
|
|
22
|
+
* Preserves the last repo segment most aggressively and shortens parent first. */
|
|
23
|
+
export declare function compactStatuslineDirLabel(cwd: string, maxLength?: number): string;
|
|
24
|
+
export interface StatuslineDetailLayout {
|
|
25
|
+
itemCount: number;
|
|
26
|
+
pathMaxLength: number;
|
|
27
|
+
}
|
|
28
|
+
export declare function resolveStatuslineDetailLayout(width: number | undefined, maxCount: number): StatuslineDetailLayout;
|
|
29
|
+
/** Encode cwd to Claude project directory name.
|
|
30
|
+
* Claude Code replaces both "/" and "." with "-". */
|
|
31
|
+
export declare function cwdToProjectDirName(cwd: string): string;
|
|
32
|
+
/** Determine agent status from CPU, elapsed time, and session match state */
|
|
33
|
+
export declare function determineStatus(cpuPercent: number, elapsedSec: number | undefined, sessionMatched: boolean, activeCpuThreshold: number, stalledAfterMin: number, phase?: SessionPhase, recentActivityActiveSec?: number): "Active" | "Idle" | "Stalled" | "Unmatched" | "Dead";
|
|
34
|
+
export interface CodexSessionCandidate {
|
|
35
|
+
cwd: string;
|
|
36
|
+
timestamp: number;
|
|
37
|
+
}
|
|
38
|
+
/** Select the most plausible Codex session for a process.
|
|
39
|
+
* Prefer exact cwd match, then nearest timestamp; otherwise most recent. */
|
|
40
|
+
export declare function selectCodexSession<T extends CodexSessionCandidate>(processCwd: string, processStartTime: number | undefined, sessions: T[]): T | undefined;
|
|
41
|
+
/** Select unmatched processes that are eligible for cleanup. */
|
|
42
|
+
export declare function selectUnmatchedTargets(agents: AgentSession[], selectedPids?: number[]): AgentSession[];
|
|
43
|
+
export interface StatuslineSnapshot {
|
|
44
|
+
aliveCount: number;
|
|
45
|
+
waitingCount: number;
|
|
46
|
+
riskCount: number;
|
|
47
|
+
stalledCount: number;
|
|
48
|
+
unmatchedCount: number;
|
|
49
|
+
activeCount: number;
|
|
50
|
+
highCpuCount: number;
|
|
51
|
+
thinkingCount?: number;
|
|
52
|
+
toolCount?: number;
|
|
53
|
+
claudeCount?: number;
|
|
54
|
+
codexCount?: number;
|
|
55
|
+
geminiCount?: number;
|
|
56
|
+
cpuPercent?: number;
|
|
57
|
+
memoryUsedGb?: number;
|
|
58
|
+
}
|
|
59
|
+
export type StatuslineFormat = "compact" | "standard" | "extended" | "tmux-badges" | "wezterm-pills";
|
|
60
|
+
export type AttentionKind = "unmatched" | "permission" | "stalled" | "thinking" | "tool" | "active";
|
|
61
|
+
export interface AttentionItem {
|
|
62
|
+
kind: AttentionKind;
|
|
63
|
+
priority: number;
|
|
64
|
+
pid: number;
|
|
65
|
+
agentName: string;
|
|
66
|
+
cwd: string;
|
|
67
|
+
cpuPercent?: number;
|
|
68
|
+
memoryMb?: number;
|
|
69
|
+
startedAt?: number;
|
|
70
|
+
runtimeSource?: AgentSession["runtimeSource"];
|
|
71
|
+
status: AgentSession["status"];
|
|
72
|
+
phase?: AgentSession["phase"];
|
|
73
|
+
lastResponseAt?: number;
|
|
74
|
+
lastActivityAt?: number;
|
|
75
|
+
}
|
|
76
|
+
export interface StatusPill {
|
|
77
|
+
label: string;
|
|
78
|
+
fg: string;
|
|
79
|
+
bg: string;
|
|
80
|
+
}
|
|
81
|
+
export interface PhaseDecayConfig {
|
|
82
|
+
thinking: number;
|
|
83
|
+
tool: number;
|
|
84
|
+
permission: number;
|
|
85
|
+
done: number;
|
|
86
|
+
}
|
|
87
|
+
export interface JsonlCursorState {
|
|
88
|
+
offset: number;
|
|
89
|
+
remainder: string;
|
|
90
|
+
recentLines: string[];
|
|
91
|
+
}
|
|
92
|
+
export interface PhaseHistoryEntry {
|
|
93
|
+
phase: Exclude<SessionPhase, undefined>;
|
|
94
|
+
at: number;
|
|
95
|
+
}
|
|
96
|
+
export interface SessionRegistryEntry {
|
|
97
|
+
filePath: string;
|
|
98
|
+
sessionId: string;
|
|
99
|
+
cwd: string;
|
|
100
|
+
firstSeenOffset: number;
|
|
101
|
+
startedAt?: number;
|
|
102
|
+
model?: string;
|
|
103
|
+
source: "claude" | "codex";
|
|
104
|
+
}
|
|
105
|
+
export interface SessionFileCandidate {
|
|
106
|
+
path: string;
|
|
107
|
+
mtimeMs: number;
|
|
108
|
+
}
|
|
109
|
+
export declare function resolvePhaseWithDecay(currentPhase: SessionPhase, cachedPhase: SessionPhase, cachedDetectedAtMs: number | undefined, decay: PhaseDecayConfig, nowMs?: number): SessionPhase;
|
|
110
|
+
export declare function advanceJsonlCursor(previous: JsonlCursorState, chunk: string, maxLines: number): JsonlCursorState;
|
|
111
|
+
export declare function detectApprovalPromptPhase(output: string, patterns?: string[], clearPatterns?: string[]): SessionPhase;
|
|
112
|
+
export declare function updatePhaseHistory(previousPhase: SessionPhase, previousHistory: PhaseHistoryEntry[], nextPhase: SessionPhase, at: number, maxEntries?: number): {
|
|
113
|
+
previousPhase?: SessionPhase;
|
|
114
|
+
history: PhaseHistoryEntry[];
|
|
115
|
+
};
|
|
116
|
+
export declare function resolvePhaseFromHistory(currentPhase: SessionPhase, history: PhaseHistoryEntry[], nowMs?: number, maxAgeMs?: number): SessionPhase;
|
|
117
|
+
export declare function resolveSessionRegistryPath(registry: Map<string, SessionRegistryEntry>, sessionId: string): string | undefined;
|
|
118
|
+
export declare function upsertSessionRegistryEntry(registry: Map<string, SessionRegistryEntry>, entry: SessionRegistryEntry): void;
|
|
119
|
+
export declare function selectRecentSessionFile(candidates: SessionFileCandidate[], nowMs?: number, maxAgeMs?: number, minLeadMs?: number): string | undefined;
|
|
120
|
+
/** Build prioritized attention list for popup/jump UX.
|
|
121
|
+
* Order: permission -> thinking -> recently active alive sessions. */
|
|
122
|
+
export declare function buildAttentionItems(agents: AgentSession[]): AttentionItem[];
|
|
123
|
+
export declare function selectAttentionItem(agents: AgentSession[], selection: number): AttentionItem | undefined;
|
|
124
|
+
/** Build jumpable attention items for interactive navigation.
|
|
125
|
+
* Statusline/jump share the same top-of-mind ordering. */
|
|
126
|
+
export declare function buildJumpAttentionItems(agents: AgentSession[]): AttentionItem[];
|
|
127
|
+
export declare function selectJumpAttentionItem(agents: AgentSession[], selection: number): AttentionItem | undefined;
|
|
128
|
+
export declare function buildAttentionFocusText(items: AttentionItem[], maxCount?: number, width?: number): string | undefined;
|
|
129
|
+
export declare function buildTmuxBadgeSummary(snapshot: StatuslineSnapshot): string;
|
|
130
|
+
export declare function buildStatusPills(snapshot: StatuslineSnapshot): {
|
|
131
|
+
agents: StatusPill[];
|
|
132
|
+
alerts: StatusPill[];
|
|
133
|
+
};
|
|
134
|
+
/** Build one-line persistent-bar summary string. */
|
|
135
|
+
export declare function buildStatuslineSummary(snapshot: StatuslineSnapshot, format?: StatuslineFormat): string;
|
|
136
|
+
export declare function buildTmuxBadgeBar(snapshot: StatuslineSnapshot, focusText?: string): string;
|
|
137
|
+
export declare function buildTmuxAttentionPills(items: AttentionItem[], maxCount?: number, width?: number): string | undefined;
|
|
138
|
+
export declare function serializeWeztermPills(snapshot: StatuslineSnapshot, focusText?: string): string;
|