my-durable 0.0.118 → 0.0.120
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/apps/dpi-backend/src/git.ts +83 -8
- package/apps/dpi-backend/src/vitals.ts +5 -4
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/assets/{index-DDRkB11b.js → index-CG_nQ-Sz.js} +1 -1
- package/apps/dpi-playground/apps/dpi-playground-fe/dist/index.html +1 -1
- package/apps/dpi-sdk/src/api.ts +34 -0
- package/apps/dpi-tui/src/app.ts +113 -9
- package/apps/dpi-tui/src/commands.ts +191 -1
- package/apps/dpi-tui/src/pickers.ts +520 -29
- package/apps/dpi-tui/src/views.ts +175 -7
- package/package.json +1 -1
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
<meta name="referrer" content="no-referrer" />
|
|
8
8
|
<link rel="icon" type="image/svg+xml" href="/icons/durable-pi.svg" />
|
|
9
9
|
<title>Durable Pi | Network</title>
|
|
10
|
-
<script type="module" crossorigin src="/assets/index-
|
|
10
|
+
<script type="module" crossorigin src="/assets/index-CG_nQ-Sz.js"></script>
|
|
11
11
|
<link rel="stylesheet" crossorigin href="/assets/index-DD66rPUN.css">
|
|
12
12
|
</head>
|
|
13
13
|
<body>
|
package/apps/dpi-sdk/src/api.ts
CHANGED
|
@@ -457,6 +457,21 @@ export interface DeviceUsageReport {
|
|
|
457
457
|
projects: Array<{ rootPath: string; name: string; tokens: number; cost: number; sessions: number }>;
|
|
458
458
|
}
|
|
459
459
|
|
|
460
|
+
/**
|
|
461
|
+
* Event-loop lag vitals (run-12): the wire copy of dpi-backend's
|
|
462
|
+
* `EventLoopLagVitals` (R7 perf lane). api.ts duplicates wire types by design
|
|
463
|
+
* — no cross-package imports — so this mirrors the backend shape exactly.
|
|
464
|
+
* `loadavg` is host-wide and cannot see the device's own stalls; these can.
|
|
465
|
+
*/
|
|
466
|
+
export interface EventLoopLagVitals {
|
|
467
|
+
/** Worst event-loop delay in the most recent 1 s window, ms (0 before the first window completes). */
|
|
468
|
+
lagMsLast: number;
|
|
469
|
+
/** Worst delay across the last 60 windows (a rolling minute), ms. */
|
|
470
|
+
lagMsMax1m: number;
|
|
471
|
+
/** Windows sampled so far — grows to 60 as the process ages; <60 right after boot. */
|
|
472
|
+
samples: number;
|
|
473
|
+
}
|
|
474
|
+
|
|
460
475
|
/** One device's live host health — asked for on demand, never polled. */
|
|
461
476
|
export interface DeviceVitals {
|
|
462
477
|
/** Seconds the host has been up. */
|
|
@@ -475,6 +490,25 @@ export interface DeviceVitals {
|
|
|
475
490
|
};
|
|
476
491
|
/** Free/total bytes for the volume holding the device's data dir. */
|
|
477
492
|
disk: { path: string; totalBytes: number; freeBytes: number; known: boolean };
|
|
493
|
+
/**
|
|
494
|
+
* Event-loop lag (run-12): how long ready JS actually waited to run — the
|
|
495
|
+
* number `loadavg` cannot express. Backend-side since the R7 perf lane;
|
|
496
|
+
* typed here now so the run-12 TUI ops view renders it with real SDK types.
|
|
497
|
+
*/
|
|
498
|
+
eventLoop: EventLoopLagVitals;
|
|
499
|
+
/**
|
|
500
|
+
* Git spawn semaphore (run-11 h2-2, typed here in run-12): children live
|
|
501
|
+
* now, callers waiting for a slot, and how many waiters sit in the
|
|
502
|
+
* interactive "high" lane — the number that says "a status-batch fan-out
|
|
503
|
+
* is stalling the panel". Required: the backend always returns it (0.0.117+,
|
|
504
|
+
* pinned by dpi-backend/tests/git-priority-r11.test.ts).
|
|
505
|
+
*/
|
|
506
|
+
/**
|
|
507
|
+
* Git spawn semaphore (run-11 h2-2; queuedAged in run-13): live children,
|
|
508
|
+
* waiting callers, interactive "high"-lane waiters, and cumulative low→high
|
|
509
|
+
* aging rescues (>10s — the f4 starvation gauge, backend 0.0.120+).
|
|
510
|
+
*/
|
|
511
|
+
gitSpawns: { live: number; queued: number; queuedHigh: number; queuedAged: number };
|
|
478
512
|
pid: number;
|
|
479
513
|
at: number;
|
|
480
514
|
/** Sessions this device knows about; `archived` counts the archive directories. */
|
package/apps/dpi-tui/src/app.ts
CHANGED
|
@@ -35,6 +35,7 @@ import {
|
|
|
35
35
|
renderHerd,
|
|
36
36
|
renderHerdPanel,
|
|
37
37
|
renderNetwork,
|
|
38
|
+
renderOps,
|
|
38
39
|
renderStatusbar,
|
|
39
40
|
renderViewbar,
|
|
40
41
|
gitStateSuffix,
|
|
@@ -42,6 +43,9 @@ import {
|
|
|
42
43
|
joinPanel,
|
|
43
44
|
networkRows,
|
|
44
45
|
VIEW_TABS,
|
|
46
|
+
type OpsData,
|
|
47
|
+
type OpsErrors,
|
|
48
|
+
type View,
|
|
45
49
|
} from "./views.ts";
|
|
46
50
|
import { appendFileSync } from "node:fs";
|
|
47
51
|
import { fetchHubNetwork } from "./hub-feed.ts";
|
|
@@ -58,7 +62,10 @@ export interface DeviceRef {
|
|
|
58
62
|
token: string;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
|
|
65
|
+
// VIEW_TABS and the View union live in views.ts — ONE source of truth shared
|
|
66
|
+
// by the viewbar labels, the digit keys and this app's view state (live-
|
|
67
|
+
// caught: a local copy here kept the old order while the viewbar taught the
|
|
68
|
+
// new one, so 1 printed "network" but opened herd).
|
|
62
69
|
type AgentTab = "transcript" | "tasks" | "tools" | "state" | "config";
|
|
63
70
|
type InputKind = "message" | "filter" | "palette" | "new" | "kill";
|
|
64
71
|
|
|
@@ -98,6 +105,7 @@ const PALETTE: { name: string; hint: string; run: (a: App) => void }[] = [
|
|
|
98
105
|
{ name: "network", hint: "devices and sessions", run: (a) => a.setView("network") },
|
|
99
106
|
{ name: "agent", hint: "back to the conversation", run: (a) => a.setView("agent") },
|
|
100
107
|
{ name: "log", hint: "device events", run: (a) => a.setView("log") },
|
|
108
|
+
{ name: "ops", hint: "vitals · usage · schedules · env · notify", run: (a) => a.setView("ops") },
|
|
101
109
|
{ name: "sessions", hint: "switch or create a session", run: (a) => void a.command("sessions") },
|
|
102
110
|
{ name: "agents", hint: "switch agent: roots, forks, subagents", run: (a) => void a.command("agents") },
|
|
103
111
|
{ name: "model", hint: "choose this agent's model", run: (a) => void a.command("model") },
|
|
@@ -266,6 +274,54 @@ export class App {
|
|
|
266
274
|
/** Long-running fetch note (dirs on a cold tunnel, R4 finding 8): message line. */
|
|
267
275
|
private pendingNote: string | null = null;
|
|
268
276
|
|
|
277
|
+
// ops view (run-12) -----------------------------------------------------
|
|
278
|
+
/** The five device-ops reads, each failing ALONE (see refreshOps). */
|
|
279
|
+
private opsData: OpsData = {};
|
|
280
|
+
private opsErrors: OpsErrors = {};
|
|
281
|
+
private opsLoading = false;
|
|
282
|
+
/** ~30 s auto-refresh counter while the ops view is on screen (see tick). */
|
|
283
|
+
private opsTick = 0;
|
|
284
|
+
/** Rendered ops body lines / height — scrollBy needs them at keypress time. */
|
|
285
|
+
private lastOpsLen = 0;
|
|
286
|
+
private lastOpsInner = 0;
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Fetch all five ops sections (run-12): vitals, usage, schedules, env,
|
|
290
|
+
* notify sinks. One flight at a time — enter, R and the 30 s tick all ride
|
|
291
|
+
* this door. Loading follows the per-view pattern (setPending on the first
|
|
292
|
+
* load only); a failing section degrades to its own ✗ line in the view, and
|
|
293
|
+
* a total outage toasts once (notify) instead of five toasts.
|
|
294
|
+
*/
|
|
295
|
+
private refreshOps() {
|
|
296
|
+
if (this.opsLoading) return;
|
|
297
|
+
this.opsLoading = true;
|
|
298
|
+
if (!Object.keys(this.opsData).length) this.setPending("loading device ops…");
|
|
299
|
+
const grab = <K extends keyof OpsData>(key: K, p: Promise<OpsData[K]>) =>
|
|
300
|
+
p
|
|
301
|
+
.then((v) => {
|
|
302
|
+
this.opsData[key] = v;
|
|
303
|
+
delete this.opsErrors[key];
|
|
304
|
+
this.requestRender();
|
|
305
|
+
})
|
|
306
|
+
.catch((e) => {
|
|
307
|
+
this.opsErrors[key] = oneLine((e as Error).message || String(e));
|
|
308
|
+
this.requestRender();
|
|
309
|
+
});
|
|
310
|
+
void Promise.allSettled([
|
|
311
|
+
grab("vitals", this.client.vitals()),
|
|
312
|
+
grab("usage", this.client.deviceUsage()),
|
|
313
|
+
grab("schedules", this.client.schedules().then((r) => r.schedules)),
|
|
314
|
+
grab("env", this.client.envView()),
|
|
315
|
+
grab("sinks", this.client.notifySinks().then((r) => r.sinks)),
|
|
316
|
+
]).then(() => {
|
|
317
|
+
this.opsLoading = false;
|
|
318
|
+
this.setPending(null);
|
|
319
|
+
if (!Object.keys(this.opsData).length && Object.keys(this.opsErrors).length)
|
|
320
|
+
this.notify(`ops: ${Object.values(this.opsErrors)[0] ?? "device unreachable"}`, true);
|
|
321
|
+
this.requestRender();
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
|
|
269
325
|
constructor(
|
|
270
326
|
device: DeviceRef,
|
|
271
327
|
client: DpiClient,
|
|
@@ -481,7 +537,11 @@ export class App {
|
|
|
481
537
|
setView(view: View) {
|
|
482
538
|
this.view = view;
|
|
483
539
|
this.cursor = 0;
|
|
484
|
-
|
|
540
|
+
// r12 review m4: every other view is a feed (tail = newest), but ops is a
|
|
541
|
+
// DOCUMENT with VITALS on top — entering at the bottom (NOTIFY) hid the
|
|
542
|
+
// section an operator opens the view for. Anchor the ops view at the
|
|
543
|
+
// document top (offset>0 pins viewportTop to the recorded `top` = 0).
|
|
544
|
+
this.scrollState = view === "ops" ? { offset: 1, top: 0 } : FOLLOW_TAIL;
|
|
485
545
|
if (view === "herd") void this.refreshSessions();
|
|
486
546
|
if (view === "herd") this.refreshBranches();
|
|
487
547
|
if (view === "network") {
|
|
@@ -490,6 +550,9 @@ export class App {
|
|
|
490
550
|
// lost with a reconnecting hub — poll once instead of waiting out the tick.
|
|
491
551
|
if (!this.devicesCache.length && this.mux?.live) this.refreshHubNetwork();
|
|
492
552
|
}
|
|
553
|
+
// run-12: entering ops loads the device-ops bundle on demand (the backend
|
|
554
|
+
// serves vitals on request, never polled — same contract as the web panel).
|
|
555
|
+
if (view === "ops") this.refreshOps();
|
|
493
556
|
this.requestRender();
|
|
494
557
|
}
|
|
495
558
|
|
|
@@ -587,18 +650,17 @@ export class App {
|
|
|
587
650
|
});
|
|
588
651
|
}
|
|
589
652
|
|
|
590
|
-
/** 1-4 switch views (see jump). */
|
|
591
653
|
/**
|
|
592
|
-
* 1-
|
|
654
|
+
* 1-5 switch views — everywhere, matching the digits printed in the
|
|
593
655
|
* viewbar. Usability pass: digits used to jump the nth session, which only
|
|
594
656
|
* made sense in the herd list and left view switching to three mixed-case
|
|
595
657
|
* mnemonics (g/N/L). Sessions open with j/k + enter, which every list
|
|
596
|
-
* already teaches in its status line.
|
|
658
|
+
* already teaches in its status line. Run-12: view 5 is the ops dashboard.
|
|
597
659
|
*/
|
|
598
660
|
private jump(n: number) {
|
|
599
661
|
const tab = VIEW_TABS[n - 1];
|
|
600
662
|
if (tab) this.setView(tab.id);
|
|
601
|
-
else this.notify("views are 1-
|
|
663
|
+
else this.notify("views are 1-5 (network · herd · agent · log · ops)");
|
|
602
664
|
}
|
|
603
665
|
|
|
604
666
|
private async submit(text: string, mode?: "followUp") {
|
|
@@ -720,6 +782,13 @@ export class App {
|
|
|
720
782
|
else if (data === "g") this.setView("herd");
|
|
721
783
|
else if (data === "N") this.setView("network");
|
|
722
784
|
else if (data === "L") this.setView("log");
|
|
785
|
+
// run-12: R re-fetches the ops bundle. Verified unbound before use (the
|
|
786
|
+
// uppercase aliases were g/N/L only); scoped to the ops view anyway so the
|
|
787
|
+
// four existing views keep their exact keyboard behavior.
|
|
788
|
+
else if (data === "R" && this.view === "ops") {
|
|
789
|
+
this.opsTick = 0;
|
|
790
|
+
this.refreshOps();
|
|
791
|
+
}
|
|
723
792
|
else if (data === "j" || matchesKey(data, "down")) this.move(1);
|
|
724
793
|
else if (data === "k" || matchesKey(data, "up")) this.move(-1);
|
|
725
794
|
else if (matchesKey(data, "enter")) this.enter();
|
|
@@ -886,7 +955,7 @@ export class App {
|
|
|
886
955
|
if (this.filter) {
|
|
887
956
|
this.filter = "";
|
|
888
957
|
this.notify("filter cleared");
|
|
889
|
-
} else if (this.view === "agent" || this.view === "log") {
|
|
958
|
+
} else if (this.view === "agent" || this.view === "log" || this.view === "ops") {
|
|
890
959
|
this.setView("herd");
|
|
891
960
|
}
|
|
892
961
|
}
|
|
@@ -909,6 +978,13 @@ export class App {
|
|
|
909
978
|
this.requestRender();
|
|
910
979
|
return;
|
|
911
980
|
}
|
|
981
|
+
// run-12: the ops body is one stacked document, not a selectable list — j/k
|
|
982
|
+
// scroll it with the same viewport math the transcript uses (scroll.ts).
|
|
983
|
+
if (this.view === "ops") {
|
|
984
|
+
this.scrollState = scrollBy(this.scrollState, delta, this.lastOpsLen, this.lastOpsInner);
|
|
985
|
+
this.requestRender();
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
912
988
|
const rows = this.rows();
|
|
913
989
|
if (!rows.length) return;
|
|
914
990
|
const selectable = (i: number) => !!rows[i] && rows[i].kind !== "group";
|
|
@@ -1103,6 +1179,13 @@ export class App {
|
|
|
1103
1179
|
this.hubTick = 0;
|
|
1104
1180
|
this.refreshHubNetwork();
|
|
1105
1181
|
}
|
|
1182
|
+
// run-12: the ops view re-reads the device every ~30 s (250 × 120 ms) while
|
|
1183
|
+
// it is the active view — same cadence pattern as the hub feed above, and
|
|
1184
|
+
// gated on the view so background tabs never poll a health endpoint.
|
|
1185
|
+
if (this.view === "ops" && ++this.opsTick >= 250) {
|
|
1186
|
+
this.opsTick = 0;
|
|
1187
|
+
this.refreshOps();
|
|
1188
|
+
}
|
|
1106
1189
|
this.requestRender();
|
|
1107
1190
|
}
|
|
1108
1191
|
|
|
@@ -1167,7 +1250,9 @@ export class App {
|
|
|
1167
1250
|
? `${oneLine(this.device.id)}:${oneLine(session.name)}:${this.agentId ?? 1}` // R20 cold-1 M4
|
|
1168
1251
|
: this.view === "agent"
|
|
1169
1252
|
? "no workspace"
|
|
1170
|
-
:
|
|
1253
|
+
: this.view === "ops"
|
|
1254
|
+
? "j/k scroll · R refresh"
|
|
1255
|
+
: "j/k move · ↵ open";
|
|
1171
1256
|
return " " + renderViewbar(w - 2, this.view === "log" ? "log" : this.view, right);
|
|
1172
1257
|
}
|
|
1173
1258
|
|
|
@@ -1176,6 +1261,7 @@ export class App {
|
|
|
1176
1261
|
if (this.view === "agent") return this.agentBody(w, h);
|
|
1177
1262
|
if (this.view === "network") return this.networkBody(w, h);
|
|
1178
1263
|
if (this.view === "log") return this.logBody(w, h);
|
|
1264
|
+
if (this.view === "ops") return this.opsBody(w, h);
|
|
1179
1265
|
return this.herdBody(w, h);
|
|
1180
1266
|
}
|
|
1181
1267
|
|
|
@@ -1210,6 +1296,22 @@ export class App {
|
|
|
1210
1296
|
}).slice(0, h);
|
|
1211
1297
|
}
|
|
1212
1298
|
|
|
1299
|
+
// ------------------------------------------------------------------- ops
|
|
1300
|
+
|
|
1301
|
+
/**
|
|
1302
|
+
* The ops view body (run-12): one stacked document, scrolled j/k like the
|
|
1303
|
+
* transcript (scroll.ts absolute-anchor math — new frames must not slide
|
|
1304
|
+
* the page under the operator who scrolled up).
|
|
1305
|
+
*/
|
|
1306
|
+
private opsBody(w: number, h: number): string[] {
|
|
1307
|
+
const content = renderOps(w, this.opsData, { errors: this.opsErrors, loading: this.opsLoading });
|
|
1308
|
+
const view = viewportTop(this.scrollState, content.length, h);
|
|
1309
|
+
this.scrollState = view.state;
|
|
1310
|
+
this.lastOpsLen = content.length;
|
|
1311
|
+
this.lastOpsInner = h;
|
|
1312
|
+
return content.slice(view.from, view.from + h);
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1213
1315
|
// ----------------------------------------------------------------- agent
|
|
1214
1316
|
|
|
1215
1317
|
private agentState(): RowState {
|
|
@@ -1419,6 +1521,8 @@ export class App {
|
|
|
1419
1521
|
: "esc herd · [ ] tabs · m message";
|
|
1420
1522
|
return " " + c.muted(hint).slice(0, w);
|
|
1421
1523
|
}
|
|
1524
|
+
if (this.view === "ops")
|
|
1525
|
+
return " " + c.muted("j/k scroll · R refresh · : palette · / msg · ? help").slice(0, w);
|
|
1422
1526
|
return " " + c.muted("j/k move · ↵ open · : palette · n new · f filter · / msg · ? help").slice(0, w);
|
|
1423
1527
|
}
|
|
1424
1528
|
|
|
@@ -1462,7 +1566,7 @@ export class App {
|
|
|
1462
1566
|
// rows drifted every time a label changed — "↵"-style keys made raw
|
|
1463
1567
|
// padding lie, so pad on visibleWidth before styling.
|
|
1464
1568
|
const keys: { k1: string; d1: string; k2?: string; d2?: string; muted?: boolean }[] = [
|
|
1465
|
-
{ k1: "j k ↑ ↓", d1: "move/scroll", k2: "1-
|
|
1569
|
+
{ k1: "j k ↑ ↓", d1: "move/scroll", k2: "1-5", d2: "views: net/herd/agent/log/ops" },
|
|
1466
1570
|
{ k1: "↵", d1: "open", k2: "g N L", d2: "same views, aliases" },
|
|
1467
1571
|
{ k1: "[ ] t", d1: "agent tabs", k2: "m", d2: "message (enter sends)" },
|
|
1468
1572
|
// esc is BACK only (R5 finding 7): it never interrupts (the 0.0.52/0.0.58
|
|
@@ -4,11 +4,12 @@ import { THINKING_LEVELS, type ThinkingLevel } from "../../dpi-sdk/src/api.ts";
|
|
|
4
4
|
import type { App } from "./app.ts";
|
|
5
5
|
import { sanitizeTerminal, oneLine } from "./sanitize.ts";
|
|
6
6
|
export { oneLine };
|
|
7
|
-
import { Panel, pickAgent, pickDevice, pickModel, pickSession, pickThinking } from "./pickers.ts";
|
|
7
|
+
import { Panel, pickAgent, pickArchived, pickDevice, pickEnv, pickModel, pickNotify, pickSession, pickSchedules, pickThinking } from "./pickers.ts";
|
|
8
8
|
import { parsePairArgs, runPairing } from "./pairing.ts";
|
|
9
9
|
import { gitStateSuffix } from "./views.ts";
|
|
10
10
|
import { c } from "./theme.ts";
|
|
11
11
|
import { basename, dirname, isAbsolute, join } from "node:path";
|
|
12
|
+
import { access, writeFile } from "node:fs/promises";
|
|
12
13
|
|
|
13
14
|
export const COMMANDS: SlashCommand[] = [
|
|
14
15
|
{ name: "sessions", description: "switch or create a session on this device" },
|
|
@@ -29,6 +30,11 @@ export const COMMANDS: SlashCommand[] = [
|
|
|
29
30
|
{ name: "name", description: "rename this agent", argumentHint: "<name>" },
|
|
30
31
|
{ name: "tasks", description: "live tasks in this session" },
|
|
31
32
|
{ name: "find", description: "search every session's transcript on this device", argumentHint: "<text>" },
|
|
33
|
+
// run-13 wave 2 — session take-out. :export writes the device's zip bundle
|
|
34
|
+
// to a local path; :archive walks the archived sessions (transcript /
|
|
35
|
+
// restore). No delete door: the SDK exposes no endpoint for it.
|
|
36
|
+
{ name: "export", description: "save a session bundle (zip) to a local path", argumentHint: "[sessionId]" },
|
|
37
|
+
{ name: "archive", description: "archived sessions: read a transcript, restore" },
|
|
32
38
|
{ name: "pair", description: "pair THIS device with a hub", argumentHint: "<CODE|PAIR_URL> [--hub ORIGIN] [--force]" },
|
|
33
39
|
{
|
|
34
40
|
name: "git",
|
|
@@ -39,6 +45,14 @@ export const COMMANDS: SlashCommand[] = [
|
|
|
39
45
|
.filter((s) => s.startsWith(prefix))
|
|
40
46
|
.map((s) => ({ value: s, label: s })),
|
|
41
47
|
},
|
|
48
|
+
// run-12 device-ops doors. None takes an argument — each opens a picker
|
|
49
|
+
// (or, for :usage/:vitals, dumps straight to the toast + log view), so there
|
|
50
|
+
// are no subcommands to complete; the row hotkeys live in the pickers.
|
|
51
|
+
{ name: "schedules", description: "scheduled sessions: run now, toggle, delete, create" },
|
|
52
|
+
{ name: "env", description: "the device's env file: view, edit, set, remove" },
|
|
53
|
+
{ name: "notify", description: "notify sinks: list them and fire a test event" },
|
|
54
|
+
{ name: "usage", description: "token/cost rollup — toast + log view" },
|
|
55
|
+
{ name: "vitals", description: "device health: uptime, memory, disk, lag, git spawns" },
|
|
42
56
|
{ name: "help", description: "keys and commands" },
|
|
43
57
|
{ name: "quit", description: "exit (the agent keeps running)" },
|
|
44
58
|
];
|
|
@@ -105,6 +119,147 @@ async function gitCommand(app: App, arg: string) {
|
|
|
105
119
|
}
|
|
106
120
|
}
|
|
107
121
|
|
|
122
|
+
// ---------------------------------------------- run-12: :usage and :vitals
|
|
123
|
+
|
|
124
|
+
/** Bytes → "8.0G" / "512M" / "42B" — one toast/log line, not a spreadsheet. */
|
|
125
|
+
export function fmtBytes(n: number): string {
|
|
126
|
+
if (!Number.isFinite(n) || n <= 0) return "0";
|
|
127
|
+
const units = ["K", "M", "G", "T"];
|
|
128
|
+
let v = n;
|
|
129
|
+
let i = -1;
|
|
130
|
+
while (v >= 1024 && i < units.length - 1) {
|
|
131
|
+
v /= 1024;
|
|
132
|
+
i += 1;
|
|
133
|
+
}
|
|
134
|
+
return i < 0 ? `${n}B` : `${v >= 100 ? Math.round(v) : v.toFixed(1)}${units[i]}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Seconds → "2d3h" / "5h01m" / "12m" / "45s" (run-12 vitals rows). */
|
|
138
|
+
export function fmtDur(sec: number): string {
|
|
139
|
+
const s = Math.max(0, Math.floor(sec));
|
|
140
|
+
const d = Math.floor(s / 86400);
|
|
141
|
+
const h = Math.floor((s % 86400) / 3600);
|
|
142
|
+
const m = Math.floor((s % 3600) / 60);
|
|
143
|
+
if (d) return `${d}d${h}h`;
|
|
144
|
+
if (h) return `${h}h${String(m).padStart(2, "0")}m`;
|
|
145
|
+
if (m) return `${m}m`;
|
|
146
|
+
return `${s}s`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** /vitals (run-12) — the :git status shape: headline toast, full report in
|
|
150
|
+
* the log view. Read-only, so there is nothing to confirm anywhere here. */
|
|
151
|
+
async function vitalsCommand(app: App) {
|
|
152
|
+
try {
|
|
153
|
+
const v = await app.client.vitals();
|
|
154
|
+
// r12 review m2: a device on a pre-0.0.117 build omits eventLoop/gitSpawns
|
|
155
|
+
// — degrade to 0s instead of throwing the whole command away (the ops
|
|
156
|
+
// view's renderer already defends the same way).
|
|
157
|
+
const lag1m = Number(v.eventLoop?.lagMsMax1m) || 0;
|
|
158
|
+
const lagLast = Number(v.eventLoop?.lagMsLast) || 0;
|
|
159
|
+
const samples = Number(v.eventLoop?.samples) || 0;
|
|
160
|
+
const gitLive = Number(v.gitSpawns?.live) || 0;
|
|
161
|
+
const gitQ = Number(v.gitSpawns?.queued) || 0;
|
|
162
|
+
const gitQh = Number(v.gitSpawns?.queuedHigh) || 0;
|
|
163
|
+
app.notify(
|
|
164
|
+
`up ${fmtDur(v.uptime)} · mem ${fmtBytes(v.memory.usedBytes)}/${fmtBytes(v.memory.totalBytes)} · disk ${fmtBytes(v.disk.freeBytes)} free · lag ${lag1m}ms (log view: L)`,
|
|
165
|
+
);
|
|
166
|
+
app.logEvent("vitals", `uptime ${fmtDur(v.uptime)} · process ${fmtDur(v.processUptime)} · pid ${v.pid} · ${v.cpuCount} cpu`);
|
|
167
|
+
app.logEvent("vitals", `load ${v.loadavg.join(" / ")}`);
|
|
168
|
+
app.logEvent(
|
|
169
|
+
"vitals",
|
|
170
|
+
`mem used ${fmtBytes(v.memory.usedBytes)} · free ${fmtBytes(v.memory.freeBytes)} · rss ${fmtBytes(v.memory.processRssBytes)} · heap ${fmtBytes(v.memory.heapUsedBytes)} of ${fmtBytes(v.memory.totalBytes)}`,
|
|
171
|
+
);
|
|
172
|
+
app.logEvent(
|
|
173
|
+
"vitals",
|
|
174
|
+
`disk ${oneLine(v.disk.path)} — ${fmtBytes(v.disk.freeBytes)} free of ${fmtBytes(v.disk.totalBytes)}${v.disk.known ? "" : " (size unknown)"}`,
|
|
175
|
+
);
|
|
176
|
+
app.logEvent("vitals", `event-loop lag ${lagLast}ms last · ${lag1m}ms max/1m · ${samples} samples`);
|
|
177
|
+
app.logEvent("vitals", `git spawns ${gitLive} live · ${gitQ} queued · ${gitQh} queued-high`);
|
|
178
|
+
if (v.counts) app.logEvent("vitals", `sessions ${v.counts.sessions} live · ${v.counts.archived} archived`);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
app.notify(`vitals: ${(e as Error).message.slice(0, 80)}`, true);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** /usage (run-12) — same shape: tokens/cost headline, per-model/-session/
|
|
185
|
+
* -project detail in the log view. Each list is top-sliced so one command
|
|
186
|
+
* cannot flood the 500-entry log ring. */
|
|
187
|
+
async function usageCommand(app: App) {
|
|
188
|
+
try {
|
|
189
|
+
const u = await app.client.deviceUsage();
|
|
190
|
+
const tok = (n: number) => n.toLocaleString("en-US");
|
|
191
|
+
const usd = (n: number) => `$${n.toFixed(2)}`;
|
|
192
|
+
app.notify(
|
|
193
|
+
`usage: ${tok(u.totals.tokens)} tokens · ${usd(u.totals.cost)} · ${u.sessions.length} session${u.sessions.length === 1 ? "" : "s"} (log view: L)`,
|
|
194
|
+
);
|
|
195
|
+
app.logEvent(
|
|
196
|
+
"usage",
|
|
197
|
+
`device rollup · ${u.sessions.length} sessions · ${u.projects.length} projects · ${u.models.length} models`,
|
|
198
|
+
);
|
|
199
|
+
for (const m of u.models.slice(0, 12))
|
|
200
|
+
app.logEvent(
|
|
201
|
+
"usage",
|
|
202
|
+
` ${oneLine(m.key)} — ${tok(m.tokens)} tok · ${usd(m.cost)} (in ${tok(m.input)} · out ${tok(m.output)} · cache ${tok(m.cacheRead)})`,
|
|
203
|
+
);
|
|
204
|
+
for (const s of [...u.sessions].sort((a, b) => b.tokens - a.tokens).slice(0, 8))
|
|
205
|
+
app.logEvent("usage", ` ${oneLine(s.name)} — ${tok(s.tokens)} tok · ${usd(s.cost)} · ${s.agents} agent${s.agents === 1 ? "" : "s"} · ${oneLine(s.cwd)}`);
|
|
206
|
+
for (const p of u.projects.slice(0, 8))
|
|
207
|
+
app.logEvent("usage", ` ${oneLine(p.name)} — ${tok(p.tokens)} tok · ${usd(p.cost)} · ${p.sessions} session${p.sessions === 1 ? "" : "s"} · ${oneLine(p.rootPath)}`);
|
|
208
|
+
} catch (e) {
|
|
209
|
+
app.notify(`usage: ${(e as Error).message.slice(0, 80)}`, true);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ---------------------------------------------- run-13: :export
|
|
214
|
+
|
|
215
|
+
/** run-13 A1: the export download name is DEVICE-AUTHORED (Content-Disposition
|
|
216
|
+
* — SessionExport.filename), so before it seeds the save-path prompt it gets
|
|
217
|
+
* the FE r16 fold: oneLine first (it is chrome AND a path segment — CR/LF
|
|
218
|
+
* must go before the charset fold), then [^A-Za-z0-9._-] → "_", capped at 80.
|
|
219
|
+
* The operator's TYPED path is trusted; only the seeded default passes here. */
|
|
220
|
+
export function sanitizeExportName(filename: string): string {
|
|
221
|
+
return oneLine(String(filename ?? ""))
|
|
222
|
+
.replace(/[^A-Za-z0-9._-]/g, "_")
|
|
223
|
+
.slice(0, 80);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** /export (run-13 A1) — pull a session's zip bundle and write it to a local
|
|
227
|
+
* path. Every failure mode (device refusal, bad path, disk full) lands in one
|
|
228
|
+
* error toast: the command is operator-driven (prompts between awaits), so it
|
|
229
|
+
* must never throw out of runCommand. */
|
|
230
|
+
async function exportCommand(app: App, sessionId: string) {
|
|
231
|
+
let filename = "session.bundle.zip";
|
|
232
|
+
let bytes: Uint8Array = new Uint8Array();
|
|
233
|
+
try {
|
|
234
|
+
const ex = await app.client.exportSession(sessionId);
|
|
235
|
+
filename = sanitizeExportName(ex.filename) || filename;
|
|
236
|
+
bytes = ex.bytes;
|
|
237
|
+
} catch (e) {
|
|
238
|
+
return app.notify(`export: ${(e as Error).message.slice(0, 80)}`, true);
|
|
239
|
+
}
|
|
240
|
+
const typed = await app.prompt("Save export as", `./${filename}`);
|
|
241
|
+
if (typed === undefined || !typed.trim()) return app.flash("export cancelled"), true;
|
|
242
|
+
const target = typed.trim();
|
|
243
|
+
try {
|
|
244
|
+
// The overwrite gate — the same y/n /reset asks (R4 finding 4): only the
|
|
245
|
+
// operator's explicit y may clobber an existing file.
|
|
246
|
+
const exists = await access(target).then(
|
|
247
|
+
() => true,
|
|
248
|
+
() => false,
|
|
249
|
+
);
|
|
250
|
+
if (exists) {
|
|
251
|
+
const ok = await app.prompt(`Overwrite ${target}? y/n`, "");
|
|
252
|
+
if (ok === undefined || !ok.trim().toLowerCase().startsWith("y"))
|
|
253
|
+
return app.flash("export cancelled — kept the existing file"), true;
|
|
254
|
+
}
|
|
255
|
+
await writeFile(target, bytes);
|
|
256
|
+
app.notify(`saved ${target} (${fmtBytes(bytes.length)})`);
|
|
257
|
+
app.logEvent("export", `saved ${oneLine(target)} · ${fmtBytes(bytes.length)} · ${oneLine(filename)}`);
|
|
258
|
+
} catch (e) {
|
|
259
|
+
app.notify(`export: ${(e as Error).message.slice(0, 80)}`, true);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
108
263
|
const KEYS: [string, string][] = [
|
|
109
264
|
["enter", "send; steers the agent while it works"],
|
|
110
265
|
["alt+enter", "queue as a follow-up"],
|
|
@@ -236,6 +391,41 @@ export async function runCommand(app: App, text: string): Promise<boolean> {
|
|
|
236
391
|
case "git":
|
|
237
392
|
await gitCommand(app, arg);
|
|
238
393
|
break;
|
|
394
|
+
// run-13 wave 2: session take-out — :export writes a bundle to disk,
|
|
395
|
+
// :archive opens the archived-session picker (pickers.ts owns its rows).
|
|
396
|
+
case "export": {
|
|
397
|
+
// No argument → the session picker (attach-on-pick is that door's
|
|
398
|
+
// documented behavior), then export the picked id. Esc and +New resolve
|
|
399
|
+
// undefined — /export stays quiet rather than flashing "cancelled" over
|
|
400
|
+
// a flow that tells its own story.
|
|
401
|
+
let id = arg;
|
|
402
|
+
if (!id) {
|
|
403
|
+
const picked = await pickSession(app);
|
|
404
|
+
if (picked === undefined) break;
|
|
405
|
+
id = picked;
|
|
406
|
+
}
|
|
407
|
+
await exportCommand(app, id);
|
|
408
|
+
break;
|
|
409
|
+
}
|
|
410
|
+
case "archive":
|
|
411
|
+
await pickArchived(app);
|
|
412
|
+
break;
|
|
413
|
+
// run-12: the five device-ops doors — the pickers own their row hotkeys.
|
|
414
|
+
case "schedules":
|
|
415
|
+
await pickSchedules(app);
|
|
416
|
+
break;
|
|
417
|
+
case "env":
|
|
418
|
+
await pickEnv(app);
|
|
419
|
+
break;
|
|
420
|
+
case "notify":
|
|
421
|
+
await pickNotify(app);
|
|
422
|
+
break;
|
|
423
|
+
case "usage":
|
|
424
|
+
await usageCommand(app);
|
|
425
|
+
break;
|
|
426
|
+
case "vitals":
|
|
427
|
+
await vitalsCommand(app);
|
|
428
|
+
break;
|
|
239
429
|
case "pair":
|
|
240
430
|
await runPairing(app, parsePairArgs(arg));
|
|
241
431
|
break;
|