@yusukeshib/pi-babysit 0.6.30 → 0.6.32
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/README.md +17 -2
- package/gc.d.mts +22 -0
- package/gc.mjs +231 -0
- package/index.ts +267 -258
- package/package.json +3 -1
- package/rpc-stream-proxy.mjs +11 -0
package/README.md
CHANGED
|
@@ -146,6 +146,14 @@ roots with unresolved remote identities; it never deletes/prunes remote logs.
|
|
|
146
146
|
After reconnecting, check or kill tracked workers to establish terminal status
|
|
147
147
|
before local retention can collect their metadata.
|
|
148
148
|
|
|
149
|
+
The background poller only contacts hosts that still have unsettled tracked
|
|
150
|
+
sessions. When a host keeps failing (SSH error, or none of its tracked
|
|
151
|
+
sessions can be found), the poller retries it with backoff (free for two
|
|
152
|
+
consecutive failures, then 5s doubling up to 60s) and sleeps while such sessions
|
|
153
|
+
are the only pending work. Their status stays "unknown"; it is never
|
|
154
|
+
reported as an exit. Explicit `babysit_check`/`wait`/`kill` calls always query
|
|
155
|
+
the host.
|
|
156
|
+
|
|
149
157
|
## Commands (human)
|
|
150
158
|
|
|
151
159
|
| Command | What it does |
|
|
@@ -153,7 +161,7 @@ before local retention can collect their metadata.
|
|
|
153
161
|
| `/babysit` | Arrow-key picker over all sessions. Renders an **inline snapshot** (no tmux): running **process** → current rendered screen + recent output + a copy-paste `babysit attach` take-over hint (detach `Ctrl-\ Ctrl-\`); running **subagent** → read-only progress (RPC stdin stays untouchable); finished → summary. Re-run `/babysit` to refresh |
|
|
154
162
|
| `/babysit gc [days]` | Preview and confirm deletion of old Pi-session roots (default 14 days). Active leases, live supervisor/child PIDs, unknown states, the current root, and recent roots are retained; old empty roots are eligible once their lease is gone. Deletion uses a GC lock and atomic rename |
|
|
155
163
|
|
|
156
|
-
The widget below the editor lists all running sessions, newest first,
|
|
164
|
+
The TUI-only widget below the editor lists all running sessions, newest first,
|
|
157
165
|
including reusable idle agents. Summary counts use `RUNNING` / `IDLE`; rows show
|
|
158
166
|
`PROCESS RUNNING`, `AGENT RUNNING`, or `AGENT IDLE`, total session age, remote host
|
|
159
167
|
and unknown-status indicators when applicable, and a colored one-line description.
|
|
@@ -200,6 +208,10 @@ targeted pattern over a broad tail, and do not read a potentially large log file
|
|
|
200
208
|
in full. Subagent crashes return structured errors plus the full log path, never
|
|
201
209
|
a raw RPC JSON tail.
|
|
202
210
|
|
|
211
|
+
Periodic widget updates are disabled outside TUI mode, so headless RPC workers
|
|
212
|
+
do not fill their logs with status/age UI requests. Background completion
|
|
213
|
+
notifications and actionable warnings/errors remain enabled.
|
|
214
|
+
|
|
203
215
|
Subagents run in RPC mode with normal Pi session persistence enabled, using
|
|
204
216
|
Pi's configured session storage (by default `~/.pi/agent/sessions/`). Each worker
|
|
205
217
|
has its own Pi session, separate from the parent and from babysit's output log.
|
|
@@ -265,6 +277,9 @@ because blindly rerunning an arbitrary command can duplicate side effects.
|
|
|
265
277
|
an explicit `notificationGroup` overrides this. Group members wait for every
|
|
266
278
|
currently running member to stop and then share one notification even when
|
|
267
279
|
their exits span multiple polls.
|
|
280
|
+
Finished sessions with nothing left to deliver are skipped by later polls,
|
|
281
|
+
and metadata reads are cached (revalidated by file identity), so a Pi session
|
|
282
|
+
with hundreds of finished workers stays cheap to poll.
|
|
268
283
|
`babysit_kill` and an exit already reported by `babysit_wait` suppress the
|
|
269
284
|
notification. Kill results are reconciled with persisted terminal state, so
|
|
270
285
|
a backend escalation warning that arrives after the child has already exited
|
|
@@ -316,7 +331,7 @@ session totals.
|
|
|
316
331
|
| `PI_BABYSIT_REAP_AFTER` | `120s` | idle grace before a finished subagent self-exits (`off`/`none`/`0` disables) |
|
|
317
332
|
| `PI_BABYSIT_BUDGET_GRACE` | `90s` | grace after a subagent budget is exceeded before verified termination |
|
|
318
333
|
| `PI_BABYSIT_RPC_LOG_MODE` | `compact` | `compact` removes duplicate RPC lifecycle payloads; `standard` opts into legacy payloads |
|
|
319
|
-
| `PI_BABYSIT_RETENTION_DAYS` | `3` | at most once per day, remove safe terminal roots older than this; set `0` to disable automatic retention |
|
|
334
|
+
| `PI_BABYSIT_RETENTION_DAYS` | `3` | at most once per day, remove safe terminal roots older than this (in a background child process, so session start never waits); set `0` to disable automatic retention |
|
|
320
335
|
| `PI_BABYSIT_TAIL_MAX_BYTES` | `4000` | default cap for explicit log tails/screens returned by `babysit_check`; override per call with `maxBytes` |
|
|
321
336
|
| `PI_BABYSIT_INLINE_OUTPUT_MAX_BYTES` | `8000` | cap for complete process output and aggregate multi-wait results |
|
|
322
337
|
| `PI_BABYSIT_NOTIFY_OUTPUT_MAX_BYTES` | `2000` | per-process output cap for unsolicited completion notifications (`0` omits all output) |
|
package/gc.d.mts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export interface BabysitGcResult {
|
|
2
|
+
candidates: string[];
|
|
3
|
+
deleted: string[];
|
|
4
|
+
bytes: number;
|
|
5
|
+
skippedLive: string[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface BabysitGcOptions {
|
|
9
|
+
rootBase: string;
|
|
10
|
+
currentRoot: string;
|
|
11
|
+
olderThanMs: number;
|
|
12
|
+
dryRun?: boolean;
|
|
13
|
+
now?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare const GC_LOCK_FILE: string;
|
|
17
|
+
export declare const GC_STAMP_FILE: string;
|
|
18
|
+
export declare const AUTOMATIC_GC_INTERVAL_MS: number;
|
|
19
|
+
export declare const ACTIVE_LEASE_PREFIX: string;
|
|
20
|
+
export declare function isConfirmedTerminalState(state: string): boolean;
|
|
21
|
+
export declare function processIsAlive(pid: number): boolean;
|
|
22
|
+
export declare function gcBabysitRoots(options: BabysitGcOptions): BabysitGcResult;
|
package/gc.mjs
ADDED
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Garbage collection for old pi-babysit roots (one root per Pi session).
|
|
3
|
+
//
|
|
4
|
+
// The collector walks every file under every root, which takes hundreds of
|
|
5
|
+
// milliseconds on a large ~/.pi-babysit. index.ts imports this module for the
|
|
6
|
+
// shared lease/lock protocol and runs collections in a child process
|
|
7
|
+
// (`node gc.mjs '<json options>'`) so the Pi event loop never blocks on it.
|
|
8
|
+
// All coordination is cross-process (lease files + lock file + revalidation),
|
|
9
|
+
// so running in a child preserves the safety argument unchanged.
|
|
10
|
+
import * as fs from "node:fs";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
|
|
14
|
+
/** @typedef {{ candidates: string[]; deleted: string[]; bytes: number; skippedLive: string[] }} BabysitGcResult */
|
|
15
|
+
/** @typedef {{ rootBase: string; currentRoot: string; olderThanMs: number; dryRun?: boolean; now?: number }} BabysitGcOptions */
|
|
16
|
+
|
|
17
|
+
export const GC_LOCK_FILE = ".pi-babysit-gc.lock";
|
|
18
|
+
export const GC_STAMP_FILE = ".pi-babysit-gc.last";
|
|
19
|
+
export const AUTOMATIC_GC_INTERVAL_MS = 24 * 60 * 60 * 1_000;
|
|
20
|
+
export const ACTIVE_LEASE_PREFIX = ".pi-babysit-active-";
|
|
21
|
+
|
|
22
|
+
/** @param {string} state */
|
|
23
|
+
export function isConfirmedTerminalState(state) {
|
|
24
|
+
return state === "killed" || state === "exited";
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @param {number} pid */
|
|
28
|
+
export function processIsAlive(pid) {
|
|
29
|
+
try {
|
|
30
|
+
process.kill(pid, 0);
|
|
31
|
+
return true;
|
|
32
|
+
} catch (error) {
|
|
33
|
+
return /** @type {NodeJS.ErrnoException} */ (error).code === "EPERM";
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @param {string} root
|
|
39
|
+
* @param {number} [rootMtimeMs]
|
|
40
|
+
* @returns {{ bytes: number; newestMtimeMs: number }}
|
|
41
|
+
*/
|
|
42
|
+
function scanTreeStats(root, rootMtimeMs) {
|
|
43
|
+
let bytes = 0;
|
|
44
|
+
let newestMtimeMs;
|
|
45
|
+
try {
|
|
46
|
+
// The collector's own lock file updates the root mtime; use the
|
|
47
|
+
// pre-lock age when rescanning, but still include fresh descendants.
|
|
48
|
+
newestMtimeMs = rootMtimeMs ?? fs.lstatSync(root).mtimeMs;
|
|
49
|
+
} catch {
|
|
50
|
+
return { bytes: 0, newestMtimeMs: Infinity };
|
|
51
|
+
}
|
|
52
|
+
const pending = [root];
|
|
53
|
+
while (pending.length > 0) {
|
|
54
|
+
const current = /** @type {string} */ (pending.pop());
|
|
55
|
+
let entries;
|
|
56
|
+
try {
|
|
57
|
+
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
58
|
+
} catch {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
if (entry.name === GC_LOCK_FILE) continue;
|
|
63
|
+
const fullPath = path.join(current, entry.name);
|
|
64
|
+
try {
|
|
65
|
+
const stat = fs.lstatSync(fullPath);
|
|
66
|
+
newestMtimeMs = Math.max(newestMtimeMs, stat.mtimeMs);
|
|
67
|
+
if (entry.isDirectory()) pending.push(fullPath);
|
|
68
|
+
else if (entry.isFile()) bytes += stat.size;
|
|
69
|
+
} catch {
|
|
70
|
+
/* raced with cleanup */
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return { bytes, newestMtimeMs };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** @param {string} root */
|
|
78
|
+
function gcRootIsSafe(root) {
|
|
79
|
+
let rootEntries;
|
|
80
|
+
try {
|
|
81
|
+
rootEntries = fs.readdirSync(root, { withFileTypes: true });
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
for (const entry of rootEntries) {
|
|
86
|
+
if (!entry.isFile() || !entry.name.startsWith(ACTIVE_LEASE_PREFIX)) continue;
|
|
87
|
+
try {
|
|
88
|
+
const lease = JSON.parse(fs.readFileSync(path.join(root, entry.name), "utf8"));
|
|
89
|
+
if (!Number.isSafeInteger(lease.pid) || lease.pid <= 0) return false;
|
|
90
|
+
if (processIsAlive(lease.pid)) return false;
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// Remote PIDs are meaningless locally. Retain unresolved remote references;
|
|
97
|
+
// terminal observations are persisted by listSessions, never prune remote data.
|
|
98
|
+
try {
|
|
99
|
+
for (const file of fs.readdirSync(path.join(root, "meta"))) {
|
|
100
|
+
if (!file.endsWith(".json")) continue;
|
|
101
|
+
const meta = JSON.parse(fs.readFileSync(path.join(root, "meta", file), "utf8"));
|
|
102
|
+
if (meta.host && (!meta.remoteTerminalState || !isConfirmedTerminalState(meta.remoteTerminalState))) return false;
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (/** @type {NodeJS.ErrnoException} */ (error).code !== "ENOENT") return false;
|
|
106
|
+
}
|
|
107
|
+
const sessionsDir = path.join(root, "sessions");
|
|
108
|
+
let sessionDirs;
|
|
109
|
+
try {
|
|
110
|
+
sessionDirs = fs.readdirSync(sessionsDir, { withFileTypes: true });
|
|
111
|
+
} catch (error) {
|
|
112
|
+
// session_start acquires a lease before the first worker exists. Once that
|
|
113
|
+
// lease is gone, an old root with no sessions is safe to collect.
|
|
114
|
+
return /** @type {NodeJS.ErrnoException} */ (error).code === "ENOENT";
|
|
115
|
+
}
|
|
116
|
+
if (!sessionDirs.some((entry) => entry.isDirectory())) return true;
|
|
117
|
+
let sawStatus = false;
|
|
118
|
+
for (const sessionEntry of sessionDirs) {
|
|
119
|
+
if (!sessionEntry.isDirectory()) continue;
|
|
120
|
+
const sessionDir = path.join(sessionsDir, sessionEntry.name);
|
|
121
|
+
try {
|
|
122
|
+
const status = JSON.parse(fs.readFileSync(path.join(sessionDir, "status.json"), "utf8"));
|
|
123
|
+
sawStatus = true;
|
|
124
|
+
if (status.state !== "running") {
|
|
125
|
+
if (!status.state || (!isConfirmedTerminalState(status.state) && status.state !== "dead")) {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
const supervisorPid = Number(
|
|
131
|
+
JSON.parse(fs.readFileSync(path.join(sessionDir, "meta.json"), "utf8")).babysit_pid,
|
|
132
|
+
);
|
|
133
|
+
const childPid = Number(status.child_pid);
|
|
134
|
+
if (
|
|
135
|
+
!Number.isSafeInteger(supervisorPid) ||
|
|
136
|
+
supervisorPid <= 0 ||
|
|
137
|
+
processIsAlive(supervisorPid) ||
|
|
138
|
+
(Number.isSafeInteger(childPid) && childPid > 0 && processIsAlive(childPid))
|
|
139
|
+
) {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return sawStatus;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {BabysitGcOptions} options
|
|
151
|
+
* @returns {BabysitGcResult}
|
|
152
|
+
*/
|
|
153
|
+
export function gcBabysitRoots(options) {
|
|
154
|
+
const now = options.now ?? Date.now();
|
|
155
|
+
const current = path.resolve(options.currentRoot);
|
|
156
|
+
/** @type {BabysitGcResult} */
|
|
157
|
+
const result = { candidates: [], deleted: [], bytes: 0, skippedLive: [] };
|
|
158
|
+
let roots;
|
|
159
|
+
try {
|
|
160
|
+
roots = fs.readdirSync(options.rootBase, { withFileTypes: true });
|
|
161
|
+
} catch {
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (const entry of roots) {
|
|
166
|
+
if (!entry.isDirectory() || entry.name.startsWith(".pi-babysit-gc-")) continue;
|
|
167
|
+
const root = path.join(options.rootBase, entry.name);
|
|
168
|
+
if (path.resolve(root) === current) continue;
|
|
169
|
+
if (!gcRootIsSafe(root)) {
|
|
170
|
+
result.skippedLive.push(entry.name);
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
const stats = scanTreeStats(root);
|
|
174
|
+
if (now - stats.newestMtimeMs < options.olderThanMs) continue;
|
|
175
|
+
result.candidates.push(entry.name);
|
|
176
|
+
if (options.dryRun !== false) {
|
|
177
|
+
result.bytes += stats.bytes;
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const lockPath = path.join(root, GC_LOCK_FILE);
|
|
182
|
+
let lockFd;
|
|
183
|
+
let tombstone;
|
|
184
|
+
try {
|
|
185
|
+
lockFd = fs.openSync(lockPath, "wx");
|
|
186
|
+
// Compatible Pi processes acquire an active lease around this same lock.
|
|
187
|
+
// Revalidate after locking, then atomically rename the root so a resume
|
|
188
|
+
// racing deletion creates a fresh namespace instead of writing into rmSync.
|
|
189
|
+
if (!gcRootIsSafe(root)) {
|
|
190
|
+
result.skippedLive.push(entry.name);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const refreshedStats = scanTreeStats(root, stats.newestMtimeMs);
|
|
194
|
+
if (now - refreshedStats.newestMtimeMs < options.olderThanMs) continue;
|
|
195
|
+
tombstone = path.join(options.rootBase, `.pi-babysit-gc-${entry.name}-${process.pid}-${Date.now()}`);
|
|
196
|
+
fs.closeSync(lockFd);
|
|
197
|
+
lockFd = undefined;
|
|
198
|
+
fs.renameSync(root, tombstone);
|
|
199
|
+
fs.rmSync(tombstone, { recursive: true, force: true });
|
|
200
|
+
result.deleted.push(entry.name);
|
|
201
|
+
result.bytes += refreshedStats.bytes;
|
|
202
|
+
} catch {
|
|
203
|
+
/* report only roots whose atomic removal completed */
|
|
204
|
+
} finally {
|
|
205
|
+
if (lockFd != null) {
|
|
206
|
+
try {
|
|
207
|
+
fs.closeSync(lockFd);
|
|
208
|
+
} catch {
|
|
209
|
+
/* best-effort */
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
try {
|
|
213
|
+
fs.rmSync(lockPath, { force: true });
|
|
214
|
+
} catch {
|
|
215
|
+
/* root may already have been atomically renamed */
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return result;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
// CLI entry: `node gc.mjs '<BabysitGcOptions JSON>'` prints the result as JSON.
|
|
223
|
+
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
224
|
+
try {
|
|
225
|
+
const options = JSON.parse(process.argv[2] ?? "");
|
|
226
|
+
process.stdout.write(JSON.stringify(gcBabysitRoots(options)));
|
|
227
|
+
} catch (error) {
|
|
228
|
+
process.stderr.write(String(error instanceof Error ? error.message : error));
|
|
229
|
+
process.exitCode = 1;
|
|
230
|
+
}
|
|
231
|
+
}
|
package/index.ts
CHANGED
|
@@ -48,6 +48,18 @@ import { Box, Markdown, Text, truncateToWidth, visibleWidth, type TuiMouseEvent
|
|
|
48
48
|
import { Type, type TSchema } from "typebox";
|
|
49
49
|
import { StringEnum } from "@earendil-works/pi-ai";
|
|
50
50
|
import { type AgentConfig, type AgentScope, discoverAgents } from "./agents";
|
|
51
|
+
import {
|
|
52
|
+
ACTIVE_LEASE_PREFIX,
|
|
53
|
+
AUTOMATIC_GC_INTERVAL_MS,
|
|
54
|
+
type BabysitGcOptions,
|
|
55
|
+
type BabysitGcResult,
|
|
56
|
+
GC_LOCK_FILE,
|
|
57
|
+
GC_STAMP_FILE,
|
|
58
|
+
gcBabysitRoots,
|
|
59
|
+
isConfirmedTerminalState,
|
|
60
|
+
} from "./gc.mjs";
|
|
61
|
+
|
|
62
|
+
export { type BabysitGcResult, gcBabysitRoots, isConfirmedTerminalState };
|
|
51
63
|
|
|
52
64
|
const descriptionSchema = Type.String({
|
|
53
65
|
minLength: 1,
|
|
@@ -389,14 +401,58 @@ async function localSessions(host?: string): Promise<{ sessions: BsSession[]; er
|
|
|
389
401
|
}
|
|
390
402
|
}
|
|
391
403
|
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
404
|
+
// Remote status costs an SSH round trip per host. The background poller backs
|
|
405
|
+
// off hosts that keep failing (transport error, or none of the tracked
|
|
406
|
+
// sessions found) so an unreachable host or a lost remote session cannot make
|
|
407
|
+
// it SSH every POLL_MS forever. Explicit tool calls always query.
|
|
408
|
+
export const REMOTE_BACKOFF_FREE_FAILURES = 2;
|
|
409
|
+
export const REMOTE_BACKOFF_MAX_MS = 60_000;
|
|
410
|
+
export function remoteBackoffDelayMs(failures: number): number {
|
|
411
|
+
if (failures <= REMOTE_BACKOFF_FREE_FAILURES) return 0;
|
|
412
|
+
return Math.min(POLL_MS * 2 ** (failures - REMOTE_BACKOFF_FREE_FAILURES), REMOTE_BACKOFF_MAX_MS);
|
|
413
|
+
}
|
|
414
|
+
const remoteHostBackoff = new Map<string, { failures: number; nextAt: number; error: string }>();
|
|
415
|
+
const remoteBackoffUntil = (host: string): number | undefined => remoteHostBackoff.get(host)?.nextAt;
|
|
416
|
+
|
|
417
|
+
const unknownRemoteSession = (id: string, host: string, error: string): BsSession => ({
|
|
418
|
+
id,
|
|
419
|
+
host,
|
|
420
|
+
state: "running",
|
|
421
|
+
transportError: error,
|
|
422
|
+
note: `status unknown on ${host}: ${error}`,
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* List local sessions plus tracked remote sessions. With `poll: true` (the
|
|
427
|
+
* background poller), settled sessions are skipped, hosts with only settled
|
|
428
|
+
* sessions are not contacted, and failing hosts are retried with backoff.
|
|
429
|
+
* `ids` limits which tracked remote sessions (and therefore hosts) are queried;
|
|
430
|
+
* `local: false` skips the local registry.
|
|
431
|
+
*/
|
|
432
|
+
async function listSessions(
|
|
433
|
+
options: { poll?: boolean; ids?: readonly string[]; local?: boolean } = {},
|
|
434
|
+
): Promise<{ sessions: BsSession[]; error?: string }> {
|
|
435
|
+
const local: { sessions: BsSession[]; error?: string } =
|
|
436
|
+
options.local === false ? { sessions: [] } : await localSessions();
|
|
437
|
+
const known = remoteMetadata().filter(
|
|
438
|
+
({ id }) => (!options.poll || !settledSessionIds.has(id)) && (!options.ids || options.ids.includes(id)),
|
|
439
|
+
);
|
|
395
440
|
await Promise.all([...new Set(known.map(({ meta }) => meta.host!))].map(async (host) => {
|
|
441
|
+
const tracked = known.filter((entry) => entry.meta.host === host);
|
|
442
|
+
const backoff = remoteHostBackoff.get(host);
|
|
443
|
+
if (options.poll && backoff && Date.now() < backoff.nextAt) {
|
|
444
|
+
const retry = Math.ceil((backoff.nextAt - Date.now()) / 1000);
|
|
445
|
+
for (const { id } of tracked) {
|
|
446
|
+
local.sessions.push(unknownRemoteSession(id, host, `${backoff.error} (retrying in ${retry}s)`));
|
|
447
|
+
}
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
396
450
|
const remote = await localSessions(host);
|
|
397
|
-
|
|
451
|
+
let foundCount = 0;
|
|
452
|
+
for (const { id, meta } of tracked) {
|
|
398
453
|
const found = remote.sessions.find((session) => session.id === meta.remoteId);
|
|
399
454
|
if (found && !remote.error) {
|
|
455
|
+
foundCount++;
|
|
400
456
|
local.sessions.push({ ...found, id, host });
|
|
401
457
|
const terminalState = isConfirmedTerminalState(found.state) ? found.state : undefined;
|
|
402
458
|
if (meta.remoteTerminalState !== terminalState) {
|
|
@@ -404,13 +460,44 @@ async function listSessions(): Promise<{ sessions: BsSession[]; error?: string }
|
|
|
404
460
|
}
|
|
405
461
|
} else {
|
|
406
462
|
// Keep unresolved references active for polling/group delivery, never fabricate exit.
|
|
407
|
-
local.sessions.push(
|
|
463
|
+
local.sessions.push(unknownRemoteSession(id, host, remote.error ?? "tracked remote session not found"));
|
|
408
464
|
}
|
|
409
465
|
}
|
|
466
|
+
if (foundCount > 0) {
|
|
467
|
+
remoteHostBackoff.delete(host);
|
|
468
|
+
} else if (options.poll) {
|
|
469
|
+
const failures = (backoff?.failures ?? 0) + 1;
|
|
470
|
+
remoteHostBackoff.set(host, {
|
|
471
|
+
failures,
|
|
472
|
+
nextAt: Date.now() + remoteBackoffDelayMs(failures),
|
|
473
|
+
error: remote.error ?? "tracked remote session not found",
|
|
474
|
+
});
|
|
475
|
+
}
|
|
410
476
|
}));
|
|
411
477
|
return local;
|
|
412
478
|
}
|
|
413
479
|
|
|
480
|
+
/**
|
|
481
|
+
* When every reason to keep polling is a remote session on a backed-off host,
|
|
482
|
+
* return the earliest retry time so the poller can sleep until then (0 = poll
|
|
483
|
+
* normally).
|
|
484
|
+
*/
|
|
485
|
+
export function pollDeferredUntil(
|
|
486
|
+
sessions: Array<{ id: string; state: string; host?: string; transportError?: string }>,
|
|
487
|
+
metaFor: (id: string) => Meta | null,
|
|
488
|
+
backoffUntil: (host: string) => number | undefined,
|
|
489
|
+
now = Date.now(),
|
|
490
|
+
): number {
|
|
491
|
+
let earliest = Infinity;
|
|
492
|
+
for (const session of sessions) {
|
|
493
|
+
if (!shouldKeepPolling([session], metaFor)) continue;
|
|
494
|
+
const until = session.host && session.transportError ? backoffUntil(session.host) : undefined;
|
|
495
|
+
if (until === undefined || until <= now) return 0;
|
|
496
|
+
earliest = Math.min(earliest, until);
|
|
497
|
+
}
|
|
498
|
+
return Number.isFinite(earliest) ? earliest : 0;
|
|
499
|
+
}
|
|
500
|
+
|
|
414
501
|
async function lookupStatus(
|
|
415
502
|
id: string,
|
|
416
503
|
): Promise<{ session: BsSession | null; error?: string }> {
|
|
@@ -418,7 +505,10 @@ async function lookupStatus(
|
|
|
418
505
|
// including `alive` and `note`. Using it directly avoids the old
|
|
419
506
|
// status-then-list pair (two CLI subprocesses for every status lookup).
|
|
420
507
|
try {
|
|
421
|
-
|
|
508
|
+
// Only contact the registry that owns this session: a local lookup must not
|
|
509
|
+
// SSH to every remote host, and a remote one needs no local list.
|
|
510
|
+
const remote = Boolean(readMeta(id)?.host);
|
|
511
|
+
const listed = await listSessions({ ids: [id], local: !remote });
|
|
422
512
|
const session = listed.sessions.find((session) => session.id === id) ?? null;
|
|
423
513
|
if (session?.transportError) return { session: null, error: `${session.host}: ${session.transportError}` };
|
|
424
514
|
if (listed.error && !readMeta(id)?.host) return { session: null, error: listed.error };
|
|
@@ -432,10 +522,6 @@ async function statusOf(id: string): Promise<BsSession | null> {
|
|
|
432
522
|
return (await lookupStatus(id)).session;
|
|
433
523
|
}
|
|
434
524
|
|
|
435
|
-
export function isConfirmedTerminalState(state: string): boolean {
|
|
436
|
-
return state === "killed" || state === "exited";
|
|
437
|
-
}
|
|
438
|
-
|
|
439
525
|
export function validateKillResponse(stdout: string): string | null {
|
|
440
526
|
try {
|
|
441
527
|
const response = JSON.parse(stdout);
|
|
@@ -591,19 +677,56 @@ const logPath = (id: string) => {
|
|
|
591
677
|
return meta?.host ? `${shq(BABYSIT_BIN)} --host ${shq(meta.host)} log -s ${shq(meta.remoteId!)}` : localLogPath(id);
|
|
592
678
|
};
|
|
593
679
|
|
|
680
|
+
// Which sessions are remote is fixed when their metadata is created, and every
|
|
681
|
+
// writeMeta creates + renames a file in the meta directory, which changes the
|
|
682
|
+
// directory's mtime. Rescan the directory only when it changed; the metadata
|
|
683
|
+
// itself is still read through the stat-validated readMeta cache.
|
|
684
|
+
let remoteIndex: { dir: string; stat: FileIdentity; ids: string[] } | undefined;
|
|
685
|
+
|
|
594
686
|
function remoteMetadata(): Array<{ id: string; meta: Meta }> {
|
|
687
|
+
const dir = metaDir();
|
|
688
|
+
let stat: fs.Stats;
|
|
595
689
|
try {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
690
|
+
stat = fs.statSync(dir);
|
|
691
|
+
} catch {
|
|
692
|
+
return [];
|
|
693
|
+
}
|
|
694
|
+
if (!remoteIndex || remoteIndex.dir !== dir || !sameFile(remoteIndex.stat, stat)) {
|
|
695
|
+
let files: string[];
|
|
696
|
+
try {
|
|
697
|
+
files = fs.readdirSync(dir);
|
|
698
|
+
} catch {
|
|
699
|
+
return [];
|
|
700
|
+
}
|
|
701
|
+
const ids = files
|
|
702
|
+
.filter((file) => file.endsWith(".json"))
|
|
703
|
+
.map((file) => file.slice(0, -5))
|
|
704
|
+
.filter((id) => {
|
|
705
|
+
const meta = readMeta(id);
|
|
706
|
+
return Boolean(meta?.host && meta.remoteId);
|
|
707
|
+
});
|
|
708
|
+
remoteIndex = { dir, stat: { mtimeMs: stat.mtimeMs, size: stat.size, ino: stat.ino }, ids };
|
|
709
|
+
}
|
|
710
|
+
return remoteIndex.ids.flatMap((id) => {
|
|
711
|
+
const meta = readMeta(id);
|
|
712
|
+
return meta?.host && meta.remoteId ? [{ id, meta }] : [];
|
|
713
|
+
});
|
|
602
714
|
}
|
|
603
715
|
|
|
716
|
+
/**
|
|
717
|
+
* Terminal sessions with nothing left to deliver (completion notified, kill
|
|
718
|
+
* suppressed, or subagent result collected). The poller skips them so a long
|
|
719
|
+
* Pi session with hundreds of finished workers does not re-read every
|
|
720
|
+
* metadata file each tick. A restarted session reports `running` again and is
|
|
721
|
+
* therefore never filtered out; any metadata write re-evaluates the session.
|
|
722
|
+
*/
|
|
723
|
+
const settledSessionIds = new Set<string>();
|
|
724
|
+
|
|
604
725
|
function writeMeta(id: string, m: Meta): boolean {
|
|
605
726
|
const target = path.join(metaDir(), `${id}.json`);
|
|
606
727
|
const temp = `${target}.${process.pid}.${Date.now()}.${Math.random().toString(16).slice(2)}.tmp`;
|
|
728
|
+
metaCache.delete(target);
|
|
729
|
+
settledSessionIds.delete(id);
|
|
607
730
|
try {
|
|
608
731
|
fs.mkdirSync(metaDir(), { recursive: true });
|
|
609
732
|
fs.writeFileSync(temp, JSON.stringify(m));
|
|
@@ -635,35 +758,35 @@ export function claimFileOnce(file: string, payload: string): boolean {
|
|
|
635
758
|
return true;
|
|
636
759
|
}
|
|
637
760
|
|
|
761
|
+
// The poller and tool paths read metadata many times per tick. Cache parsed
|
|
762
|
+
// metadata per file and revalidate with a stat: writeMeta always renames a new
|
|
763
|
+
// temp file into place, so any write (from this or another Pi process sharing
|
|
764
|
+
// the root) changes the inode and mtime. Callers mutate the returned object
|
|
765
|
+
// before writing it back, so every read returns a private copy.
|
|
766
|
+
interface FileIdentity {
|
|
767
|
+
mtimeMs: number;
|
|
768
|
+
size: number;
|
|
769
|
+
ino: number;
|
|
770
|
+
}
|
|
771
|
+
const sameFile = (a: FileIdentity, b: FileIdentity) =>
|
|
772
|
+
a.mtimeMs === b.mtimeMs && a.size === b.size && a.ino === b.ino;
|
|
773
|
+
const metaCache = new Map<string, { stat: FileIdentity; meta: Meta }>();
|
|
774
|
+
|
|
638
775
|
function readMeta(id: string): Meta | null {
|
|
776
|
+
const file = path.join(metaDir(), `${id}.json`);
|
|
639
777
|
try {
|
|
640
|
-
|
|
778
|
+
const stat = fs.statSync(file);
|
|
779
|
+
const cached = metaCache.get(file);
|
|
780
|
+
if (cached && sameFile(cached.stat, stat)) return structuredClone(cached.meta);
|
|
781
|
+
const meta = JSON.parse(fs.readFileSync(file, "utf-8")) as Meta;
|
|
782
|
+
metaCache.set(file, { stat: { mtimeMs: stat.mtimeMs, size: stat.size, ino: stat.ino }, meta });
|
|
783
|
+
return structuredClone(meta);
|
|
641
784
|
} catch {
|
|
785
|
+
metaCache.delete(file);
|
|
642
786
|
return null;
|
|
643
787
|
}
|
|
644
788
|
}
|
|
645
789
|
|
|
646
|
-
export interface BabysitGcResult {
|
|
647
|
-
candidates: string[];
|
|
648
|
-
deleted: string[];
|
|
649
|
-
bytes: number;
|
|
650
|
-
skippedLive: string[];
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
function processIsAlive(pid: number): boolean {
|
|
654
|
-
try {
|
|
655
|
-
process.kill(pid, 0);
|
|
656
|
-
return true;
|
|
657
|
-
} catch (error) {
|
|
658
|
-
return (error as NodeJS.ErrnoException).code === "EPERM";
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
const GC_LOCK_FILE = ".pi-babysit-gc.lock";
|
|
663
|
-
const GC_STAMP_FILE = ".pi-babysit-gc.last";
|
|
664
|
-
const AUTOMATIC_GC_INTERVAL_MS = 24 * 60 * 60 * 1_000;
|
|
665
|
-
const ACTIVE_LEASE_PREFIX = ".pi-babysit-active-";
|
|
666
|
-
|
|
667
790
|
function automaticGcDue(now = Date.now()): boolean {
|
|
668
791
|
try {
|
|
669
792
|
return now - fs.statSync(path.join(ROOT_BASE, GC_STAMP_FILE)).mtimeMs >= AUTOMATIC_GC_INTERVAL_MS;
|
|
@@ -681,118 +804,6 @@ function markAutomaticGc(now = new Date()): void {
|
|
|
681
804
|
}
|
|
682
805
|
}
|
|
683
806
|
|
|
684
|
-
function scanTreeStats(root: string, rootMtimeMs?: number): { bytes: number; newestMtimeMs: number } {
|
|
685
|
-
let bytes = 0;
|
|
686
|
-
let newestMtimeMs: number;
|
|
687
|
-
try {
|
|
688
|
-
// The collector's own lock file updates the root mtime; use the
|
|
689
|
-
// pre-lock age when rescanning, but still include fresh descendants.
|
|
690
|
-
newestMtimeMs = rootMtimeMs ?? fs.lstatSync(root).mtimeMs;
|
|
691
|
-
} catch {
|
|
692
|
-
return { bytes: 0, newestMtimeMs: Infinity };
|
|
693
|
-
}
|
|
694
|
-
const pending = [root];
|
|
695
|
-
while (pending.length > 0) {
|
|
696
|
-
const current = pending.pop() as string;
|
|
697
|
-
let entries: fs.Dirent[];
|
|
698
|
-
try {
|
|
699
|
-
entries = fs.readdirSync(current, { withFileTypes: true });
|
|
700
|
-
} catch {
|
|
701
|
-
continue;
|
|
702
|
-
}
|
|
703
|
-
for (const entry of entries) {
|
|
704
|
-
if (entry.name === GC_LOCK_FILE) continue;
|
|
705
|
-
const fullPath = path.join(current, entry.name);
|
|
706
|
-
try {
|
|
707
|
-
const stat = fs.lstatSync(fullPath);
|
|
708
|
-
newestMtimeMs = Math.max(newestMtimeMs, stat.mtimeMs);
|
|
709
|
-
if (entry.isDirectory()) pending.push(fullPath);
|
|
710
|
-
else if (entry.isFile()) bytes += stat.size;
|
|
711
|
-
} catch {
|
|
712
|
-
/* raced with cleanup */
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
return { bytes, newestMtimeMs };
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
function gcRootIsSafe(root: string): boolean {
|
|
720
|
-
let rootEntries: fs.Dirent[];
|
|
721
|
-
try {
|
|
722
|
-
rootEntries = fs.readdirSync(root, { withFileTypes: true });
|
|
723
|
-
} catch {
|
|
724
|
-
return false;
|
|
725
|
-
}
|
|
726
|
-
for (const entry of rootEntries) {
|
|
727
|
-
if (!entry.isFile() || !entry.name.startsWith(ACTIVE_LEASE_PREFIX)) continue;
|
|
728
|
-
try {
|
|
729
|
-
const lease = JSON.parse(fs.readFileSync(path.join(root, entry.name), "utf8")) as {
|
|
730
|
-
pid?: number;
|
|
731
|
-
};
|
|
732
|
-
if (!Number.isSafeInteger(lease.pid) || (lease.pid as number) <= 0) return false;
|
|
733
|
-
if (processIsAlive(lease.pid as number)) return false;
|
|
734
|
-
} catch {
|
|
735
|
-
return false;
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
// Remote PIDs are meaningless locally. Retain unresolved remote references;
|
|
740
|
-
// terminal observations are persisted by listSessions, never prune remote data.
|
|
741
|
-
try {
|
|
742
|
-
for (const file of fs.readdirSync(path.join(root, "meta"))) {
|
|
743
|
-
if (!file.endsWith(".json")) continue;
|
|
744
|
-
const meta = JSON.parse(fs.readFileSync(path.join(root, "meta", file), "utf8")) as Meta;
|
|
745
|
-
if (meta.host && (!meta.remoteTerminalState || !isConfirmedTerminalState(meta.remoteTerminalState))) return false;
|
|
746
|
-
}
|
|
747
|
-
} catch (error) {
|
|
748
|
-
if ((error as NodeJS.ErrnoException).code !== "ENOENT") return false;
|
|
749
|
-
}
|
|
750
|
-
const sessionsDir = path.join(root, "sessions");
|
|
751
|
-
let sessionDirs: fs.Dirent[];
|
|
752
|
-
try {
|
|
753
|
-
sessionDirs = fs.readdirSync(sessionsDir, { withFileTypes: true });
|
|
754
|
-
} catch (error) {
|
|
755
|
-
// session_start acquires a lease before the first worker exists. Once that
|
|
756
|
-
// lease is gone, an old root with no sessions is safe to collect.
|
|
757
|
-
return (error as NodeJS.ErrnoException).code === "ENOENT";
|
|
758
|
-
}
|
|
759
|
-
if (!sessionDirs.some((entry) => entry.isDirectory())) return true;
|
|
760
|
-
let sawStatus = false;
|
|
761
|
-
for (const sessionEntry of sessionDirs) {
|
|
762
|
-
if (!sessionEntry.isDirectory()) continue;
|
|
763
|
-
const sessionDir = path.join(sessionsDir, sessionEntry.name);
|
|
764
|
-
try {
|
|
765
|
-
const status = JSON.parse(
|
|
766
|
-
fs.readFileSync(path.join(sessionDir, "status.json"), "utf8"),
|
|
767
|
-
) as { state?: string; child_pid?: number | null };
|
|
768
|
-
sawStatus = true;
|
|
769
|
-
if (status.state !== "running") {
|
|
770
|
-
if (!status.state || (!isConfirmedTerminalState(status.state) && status.state !== "dead")) {
|
|
771
|
-
return false;
|
|
772
|
-
}
|
|
773
|
-
continue;
|
|
774
|
-
}
|
|
775
|
-
const supervisorPid = Number(
|
|
776
|
-
(JSON.parse(fs.readFileSync(path.join(sessionDir, "meta.json"), "utf8")) as {
|
|
777
|
-
babysit_pid?: number;
|
|
778
|
-
}).babysit_pid,
|
|
779
|
-
);
|
|
780
|
-
const childPid = Number(status.child_pid);
|
|
781
|
-
if (
|
|
782
|
-
!Number.isSafeInteger(supervisorPid) ||
|
|
783
|
-
supervisorPid <= 0 ||
|
|
784
|
-
processIsAlive(supervisorPid) ||
|
|
785
|
-
(Number.isSafeInteger(childPid) && childPid > 0 && processIsAlive(childPid))
|
|
786
|
-
) {
|
|
787
|
-
return false;
|
|
788
|
-
}
|
|
789
|
-
} catch {
|
|
790
|
-
return false;
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
return sawStatus;
|
|
794
|
-
}
|
|
795
|
-
|
|
796
807
|
function acquireRootLease(root: string): string | null {
|
|
797
808
|
fs.mkdirSync(root, { recursive: true });
|
|
798
809
|
const leasePath = path.join(
|
|
@@ -828,81 +839,37 @@ function releaseRootLease(leasePath: string | undefined): void {
|
|
|
828
839
|
}
|
|
829
840
|
}
|
|
830
841
|
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
continue;
|
|
855
|
-
}
|
|
856
|
-
const stats = scanTreeStats(root);
|
|
857
|
-
if (now - stats.newestMtimeMs < options.olderThanMs) continue;
|
|
858
|
-
result.candidates.push(entry.name);
|
|
859
|
-
if (options.dryRun !== false) {
|
|
860
|
-
result.bytes += stats.bytes;
|
|
861
|
-
continue;
|
|
862
|
-
}
|
|
863
|
-
|
|
864
|
-
const lockPath = path.join(root, GC_LOCK_FILE);
|
|
865
|
-
let lockFd: number | undefined;
|
|
866
|
-
let tombstone: string | undefined;
|
|
867
|
-
try {
|
|
868
|
-
lockFd = fs.openSync(lockPath, "wx");
|
|
869
|
-
// Compatible Pi processes acquire an active lease around this same lock.
|
|
870
|
-
// Revalidate after locking, then atomically rename the root so a resume
|
|
871
|
-
// racing deletion creates a fresh namespace instead of writing into rmSync.
|
|
872
|
-
if (!gcRootIsSafe(root)) {
|
|
873
|
-
result.skippedLive.push(entry.name);
|
|
874
|
-
continue;
|
|
875
|
-
}
|
|
876
|
-
const refreshedStats = scanTreeStats(root, stats.newestMtimeMs);
|
|
877
|
-
if (now - refreshedStats.newestMtimeMs < options.olderThanMs) continue;
|
|
878
|
-
tombstone = path.join(
|
|
879
|
-
options.rootBase,
|
|
880
|
-
`.pi-babysit-gc-${entry.name}-${process.pid}-${Date.now()}`,
|
|
881
|
-
);
|
|
882
|
-
fs.closeSync(lockFd);
|
|
883
|
-
lockFd = undefined;
|
|
884
|
-
fs.renameSync(root, tombstone);
|
|
885
|
-
fs.rmSync(tombstone, { recursive: true, force: true });
|
|
886
|
-
result.deleted.push(entry.name);
|
|
887
|
-
result.bytes += refreshedStats.bytes;
|
|
888
|
-
} catch {
|
|
889
|
-
/* report only roots whose atomic removal completed */
|
|
890
|
-
} finally {
|
|
891
|
-
if (lockFd != null) {
|
|
892
|
-
try {
|
|
893
|
-
fs.closeSync(lockFd);
|
|
894
|
-
} catch {
|
|
895
|
-
/* best-effort */
|
|
896
|
-
}
|
|
842
|
+
/**
|
|
843
|
+
* Run a GC pass in a child process. Collection walks every file under every
|
|
844
|
+
* root (hundreds of milliseconds for a large ~/.pi-babysit), so it must not run
|
|
845
|
+
* on Pi's event loop. The lease/lock protocol in gc.mjs is cross-process.
|
|
846
|
+
*/
|
|
847
|
+
export function runGcWorker(options: BabysitGcOptions): Promise<BabysitGcResult> {
|
|
848
|
+
return new Promise((resolve, reject) => {
|
|
849
|
+
const child = spawn(process.execPath, [path.join(EXT_DIR, "gc.mjs"), JSON.stringify(options)], {
|
|
850
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
851
|
+
});
|
|
852
|
+
let stdout = "";
|
|
853
|
+
let stderr = "";
|
|
854
|
+
child.stdout?.on("data", (data) => {
|
|
855
|
+
stdout += data;
|
|
856
|
+
});
|
|
857
|
+
child.stderr?.on("data", (data) => {
|
|
858
|
+
stderr += data;
|
|
859
|
+
});
|
|
860
|
+
child.on("error", reject);
|
|
861
|
+
child.on("close", (code) => {
|
|
862
|
+
if (code !== 0) {
|
|
863
|
+
reject(new Error(stderr.trim() || `babysit GC worker exited with code ${code}`));
|
|
864
|
+
return;
|
|
897
865
|
}
|
|
898
866
|
try {
|
|
899
|
-
|
|
867
|
+
resolve(JSON.parse(stdout) as BabysitGcResult);
|
|
900
868
|
} catch {
|
|
901
|
-
|
|
869
|
+
reject(new Error(`babysit GC worker returned invalid output: ${stdout.slice(0, 200)}`));
|
|
902
870
|
}
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
return result;
|
|
871
|
+
});
|
|
872
|
+
});
|
|
906
873
|
}
|
|
907
874
|
|
|
908
875
|
function cleanupMessageTempDirs(id: string, meta: Meta, agentEnds: number): void {
|
|
@@ -940,6 +907,11 @@ export function shouldDeliverSubagentCompletion(meta: Meta | null): meta is Meta
|
|
|
940
907
|
);
|
|
941
908
|
}
|
|
942
909
|
|
|
910
|
+
/** A finished session whose metadata leaves nothing to notify or collect. */
|
|
911
|
+
export function isSettledSession(meta: Meta | null): boolean {
|
|
912
|
+
return !shouldDeliverProcessCompletion(meta) && !shouldDeliverSubagentCompletion(meta);
|
|
913
|
+
}
|
|
914
|
+
|
|
943
915
|
export function isNotificationGroupReady(
|
|
944
916
|
meta: Meta,
|
|
945
917
|
sessions: BsSession[],
|
|
@@ -2057,16 +2029,21 @@ interface TaskProgressCache {
|
|
|
2057
2029
|
}
|
|
2058
2030
|
const taskProgressCache = new Map<string, TaskProgressCache>();
|
|
2059
2031
|
|
|
2032
|
+
/**
|
|
2033
|
+
* Drop cached state for sessions that are no longer running, except `keep`
|
|
2034
|
+
* (finished sessions whose result has not been delivered or collected yet).
|
|
2035
|
+
*/
|
|
2060
2036
|
export function pruneTerminalSessionCache<T>(
|
|
2061
2037
|
cache: Map<string, T>,
|
|
2062
2038
|
sessions: Array<{ id: string; state: string }>,
|
|
2039
|
+
keep: ReadonlySet<string> = new Set(),
|
|
2063
2040
|
): number {
|
|
2064
2041
|
const running = new Set(
|
|
2065
2042
|
sessions.filter((session) => session.state === "running").map((session) => session.id),
|
|
2066
2043
|
);
|
|
2067
2044
|
let removed = 0;
|
|
2068
2045
|
for (const id of cache.keys()) {
|
|
2069
|
-
if (running.has(id)) continue;
|
|
2046
|
+
if (running.has(id) || keep.has(id)) continue;
|
|
2070
2047
|
cache.delete(id);
|
|
2071
2048
|
removed++;
|
|
2072
2049
|
}
|
|
@@ -2134,7 +2111,8 @@ const reservedSessionIds = new Set<string>();
|
|
|
2134
2111
|
|
|
2135
2112
|
async function reserveUniqueSessionId(name: string): Promise<string> {
|
|
2136
2113
|
const base = name.replace(/[^\w.-]+/g, "-").replace(/^-+|-+$/g, "") || "proc";
|
|
2137
|
-
|
|
2114
|
+
// Remote ids come from local metadata; querying remote hosts adds nothing here.
|
|
2115
|
+
const taken = new Set((await listSessions({ ids: [] })).sessions.map((s) => s.id));
|
|
2138
2116
|
for (const { id } of remoteMetadata()) taken.add(id);
|
|
2139
2117
|
for (const id of reservedSessionIds) taken.add(id);
|
|
2140
2118
|
let id = base;
|
|
@@ -3287,6 +3265,17 @@ export function resolveSubagentSendMode(
|
|
|
3287
3265
|
export default function (pi: ExtensionAPI) {
|
|
3288
3266
|
let pollTimer: ReturnType<typeof setInterval> | undefined;
|
|
3289
3267
|
let pollNeeded = true;
|
|
3268
|
+
// Earliest time the poller should run again when every pending session is a
|
|
3269
|
+
// remote one whose host is in backoff (0 = poll every POLL_MS).
|
|
3270
|
+
let pollDeferredUntilMs = 0;
|
|
3271
|
+
// Bumped by every re-arm so a tick that started before a new session was
|
|
3272
|
+
// created cannot switch polling off based on its stale snapshot.
|
|
3273
|
+
let pollGeneration = 0;
|
|
3274
|
+
const armPolling = () => {
|
|
3275
|
+
pollNeeded = true;
|
|
3276
|
+
pollDeferredUntilMs = 0;
|
|
3277
|
+
pollGeneration++;
|
|
3278
|
+
};
|
|
3290
3279
|
let automaticGcRan = false;
|
|
3291
3280
|
let rootLeasePath: string | undefined;
|
|
3292
3281
|
const declaredToolErrors = new Set<string>();
|
|
@@ -3585,7 +3574,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
3585
3574
|
|
|
3586
3575
|
let sessionWidget: ReturnType<typeof createWidgetComponent> | undefined;
|
|
3587
3576
|
const refreshWidget = async (ctx: ExtensionContext, snapshot?: BsSession[]) => {
|
|
3588
|
-
|
|
3577
|
+
// RPC hasUI is also true, but every setWidget call becomes a log record.
|
|
3578
|
+
// Keep the age/status ticker local to the TUI, not headless worker logs.
|
|
3579
|
+
if (!ctx.hasUI || ctx.mode !== "tui") return;
|
|
3589
3580
|
const all = snapshot ?? (await listSessions()).sessions;
|
|
3590
3581
|
const { active } = widgetSessionGroups(all, (id) => readMeta(id)?.startedAt ?? 0);
|
|
3591
3582
|
const subs = active.filter((session) => kindOf(session.id) === "subagent");
|
|
@@ -3617,21 +3608,15 @@ export default function (pi: ExtensionAPI) {
|
|
|
3617
3608
|
description: description ? theme.fg("warning", theme.bold(escapeCommandForDisplay(description))) : undefined,
|
|
3618
3609
|
};
|
|
3619
3610
|
});
|
|
3620
|
-
if (
|
|
3621
|
-
|
|
3622
|
-
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
}, { placement: "belowEditor" });
|
|
3628
|
-
} else {
|
|
3629
|
-
sessionWidget.update(summaryLines, displays);
|
|
3630
|
-
}
|
|
3611
|
+
if (!sessionWidget) {
|
|
3612
|
+
ctx.ui.setWidget("pi-babysit", (tui) => {
|
|
3613
|
+
sessionWidget = createWidgetComponent(summaryLines, displays,
|
|
3614
|
+
(id) => copyToClipboard(sessionCliCommand(id, "log")),
|
|
3615
|
+
() => tui.requestRender());
|
|
3616
|
+
return sessionWidget;
|
|
3617
|
+
}, { placement: "belowEditor" });
|
|
3631
3618
|
} else {
|
|
3632
|
-
|
|
3633
|
-
placement: "belowEditor",
|
|
3634
|
-
});
|
|
3619
|
+
sessionWidget.update(summaryLines, displays);
|
|
3635
3620
|
}
|
|
3636
3621
|
};
|
|
3637
3622
|
|
|
@@ -3750,7 +3735,9 @@ export default function (pi: ExtensionAPI) {
|
|
|
3750
3735
|
}
|
|
3751
3736
|
taskProgressCache.clear();
|
|
3752
3737
|
searchLogCache.clear();
|
|
3753
|
-
|
|
3738
|
+
settledSessionIds.clear();
|
|
3739
|
+
remoteHostBackoff.clear();
|
|
3740
|
+
armPolling();
|
|
3754
3741
|
const retentionDays = Number(process.env.PI_BABYSIT_RETENTION_DAYS ?? "3");
|
|
3755
3742
|
if (
|
|
3756
3743
|
!automaticGcRan &&
|
|
@@ -3759,19 +3746,26 @@ export default function (pi: ExtensionAPI) {
|
|
|
3759
3746
|
automaticGcDue()
|
|
3760
3747
|
) {
|
|
3761
3748
|
automaticGcRan = true;
|
|
3762
|
-
|
|
3749
|
+
markAutomaticGc();
|
|
3750
|
+
// Fire and forget: the collector runs in a child process so session
|
|
3751
|
+
// start never waits for a full ~/.pi-babysit walk.
|
|
3752
|
+
void runGcWorker({
|
|
3763
3753
|
rootBase: ROOT_BASE,
|
|
3764
3754
|
currentRoot: ROOT,
|
|
3765
3755
|
olderThanMs: retentionDays * 86_400_000,
|
|
3766
3756
|
dryRun: false,
|
|
3767
|
-
})
|
|
3768
|
-
|
|
3769
|
-
|
|
3770
|
-
|
|
3771
|
-
|
|
3772
|
-
|
|
3773
|
-
|
|
3774
|
-
|
|
3757
|
+
})
|
|
3758
|
+
.then((gc) => {
|
|
3759
|
+
if (gc.deleted.length > 0 && ctx.hasUI) {
|
|
3760
|
+
ctx.ui.notify(
|
|
3761
|
+
`pi-babysit GC removed ${gc.deleted.length} roots (${gc.bytes} bytes).`,
|
|
3762
|
+
"info",
|
|
3763
|
+
);
|
|
3764
|
+
}
|
|
3765
|
+
})
|
|
3766
|
+
.catch(() => {
|
|
3767
|
+
/* best-effort; the next automatic window retries */
|
|
3768
|
+
});
|
|
3775
3769
|
}
|
|
3776
3770
|
// Warn early if the binary is missing so the user isn't surprised only when
|
|
3777
3771
|
// a tool later fails. Tools/commands still enforce it via requireBabysit.
|
|
@@ -3783,27 +3777,42 @@ export default function (pi: ExtensionAPI) {
|
|
|
3783
3777
|
// Once a namespace has no live workers or pending notifications, avoid
|
|
3784
3778
|
// spawning `babysit list` forever while the UI is idle. A successful
|
|
3785
3779
|
// babysit_run re-arms polling below.
|
|
3786
|
-
if (polling || !pollNeeded) return;
|
|
3780
|
+
if (polling || !pollNeeded || Date.now() < pollDeferredUntilMs) return;
|
|
3787
3781
|
polling = true;
|
|
3782
|
+
const generation = pollGeneration;
|
|
3788
3783
|
void (async () => {
|
|
3789
|
-
const listed = await listSessions();
|
|
3784
|
+
const listed = await listSessions({ poll: true });
|
|
3790
3785
|
if (listed.error) {
|
|
3791
|
-
pollNeeded = shouldKeepPollingAfterList(listed, readMeta);
|
|
3786
|
+
if (generation === pollGeneration) pollNeeded = shouldKeepPollingAfterList(listed, readMeta);
|
|
3792
3787
|
throw new Error(listed.error);
|
|
3793
3788
|
}
|
|
3794
|
-
|
|
3789
|
+
// Finished sessions with nothing left to deliver never need another look.
|
|
3790
|
+
const snapshot = listed.sessions.filter(
|
|
3791
|
+
(session) =>
|
|
3792
|
+
session.state === "running" || session.transportError || !settledSessionIds.has(session.id),
|
|
3793
|
+
);
|
|
3795
3794
|
await enforceSubagentBudgets(snapshot);
|
|
3796
3795
|
await Promise.all([
|
|
3797
3796
|
notifyEndedProcesses(ctx, snapshot),
|
|
3798
3797
|
notifySettledSubagents(ctx, snapshot),
|
|
3799
3798
|
refreshWidget(ctx, snapshot),
|
|
3800
3799
|
]);
|
|
3801
|
-
|
|
3800
|
+
const pendingTerminal = new Set<string>();
|
|
3801
|
+
for (const session of snapshot) {
|
|
3802
|
+
if (session.state === "running" || session.transportError) continue;
|
|
3803
|
+
if (isSettledSession(readMeta(session.id))) settledSessionIds.add(session.id);
|
|
3804
|
+
else pendingTerminal.add(session.id);
|
|
3805
|
+
}
|
|
3806
|
+
// Keep parsed progress for finished subagents whose result is still
|
|
3807
|
+
// pending; otherwise each tick would re-parse their whole log.
|
|
3808
|
+
pruneTerminalSessionCache(taskProgressCache, snapshot, pendingTerminal);
|
|
3809
|
+
if (generation !== pollGeneration) return;
|
|
3802
3810
|
pollNeeded = shouldKeepPolling(snapshot, readMeta);
|
|
3811
|
+
pollDeferredUntilMs = pollNeeded ? pollDeferredUntil(snapshot, readMeta, remoteBackoffUntil) : 0;
|
|
3803
3812
|
})()
|
|
3804
3813
|
.catch(() => {
|
|
3805
3814
|
// Keep polling armed after a transient CLI/filesystem error.
|
|
3806
|
-
|
|
3815
|
+
armPolling();
|
|
3807
3816
|
})
|
|
3808
3817
|
.finally(() => {
|
|
3809
3818
|
polling = false;
|
|
@@ -4147,7 +4156,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
4147
4156
|
details: {},
|
|
4148
4157
|
};
|
|
4149
4158
|
}
|
|
4150
|
-
|
|
4159
|
+
armPolling();
|
|
4151
4160
|
await refreshWidget(ctx);
|
|
4152
4161
|
|
|
4153
4162
|
// One-shot / non-interactive mode has no event loop that can deliver an
|
|
@@ -4310,7 +4319,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
4310
4319
|
};
|
|
4311
4320
|
}
|
|
4312
4321
|
|
|
4313
|
-
|
|
4322
|
+
armPolling();
|
|
4314
4323
|
await refreshWidget(ctx);
|
|
4315
4324
|
if (params.foreground || !ctx.hasUI) {
|
|
4316
4325
|
const outcome = await waitForTask(res.id, null, _signal);
|
|
@@ -5177,7 +5186,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
5177
5186
|
currentRoot: ROOT,
|
|
5178
5187
|
olderThanMs: days * 86_400_000,
|
|
5179
5188
|
};
|
|
5180
|
-
const preview =
|
|
5189
|
+
const preview = await runGcWorker({ ...options, dryRun: true });
|
|
5181
5190
|
if (preview.candidates.length === 0) {
|
|
5182
5191
|
ctx.ui.notify(
|
|
5183
5192
|
`No terminal pi-babysit roots older than ${days} days are safe to remove.`,
|
|
@@ -5191,7 +5200,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
5191
5200
|
"Running supervisors and the current Pi session are excluded.",
|
|
5192
5201
|
);
|
|
5193
5202
|
if (!confirmed) return;
|
|
5194
|
-
const removed =
|
|
5203
|
+
const removed = await runGcWorker({ ...options, dryRun: false });
|
|
5195
5204
|
ctx.ui.notify(
|
|
5196
5205
|
`Removed ${removed.deleted.length} pi-babysit roots (${removed.bytes} bytes).`,
|
|
5197
5206
|
"info",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yusukeshib/pi-babysit",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.32",
|
|
4
4
|
"description": "Run any shell command and pi subagents under babysit, with context-safe captured output.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pi-package",
|
|
@@ -25,6 +25,8 @@
|
|
|
25
25
|
"format-stream.mjs",
|
|
26
26
|
"rpc-stream-proxy.mjs",
|
|
27
27
|
"search-log.mjs",
|
|
28
|
+
"gc.mjs",
|
|
29
|
+
"gc.d.mts",
|
|
28
30
|
"README.md",
|
|
29
31
|
"LICENSE"
|
|
30
32
|
],
|
package/rpc-stream-proxy.mjs
CHANGED
|
@@ -80,6 +80,17 @@ export function compactRpcLine(
|
|
|
80
80
|
mode = process.env.PI_BABYSIT_RPC_LOG_MODE ?? "compact",
|
|
81
81
|
) {
|
|
82
82
|
if (!line.trimStart().startsWith("{")) return line;
|
|
83
|
+
// Pi >= 1.4 already writes delta-only message_update lines (no cumulative
|
|
84
|
+
// `message` or `partial`). They are the bulk of a worker's stream, so skip
|
|
85
|
+
// the parse/re-serialize round trip when there is nothing to strip. Inside
|
|
86
|
+
// JSON strings a quote is escaped, so these key patterns cannot match text.
|
|
87
|
+
if (
|
|
88
|
+
line.startsWith('{"type":"message_update"') &&
|
|
89
|
+
!line.includes('"message":') &&
|
|
90
|
+
!line.includes('"partial":')
|
|
91
|
+
) {
|
|
92
|
+
return line;
|
|
93
|
+
}
|
|
83
94
|
|
|
84
95
|
let event;
|
|
85
96
|
try {
|