baychat 0.14.0 → 0.15.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/dist/args.js +15 -0
- package/dist/doctor.js +21 -2
- package/dist/index.js +7 -0
- package/dist/relay/adapters.js +21 -3
- package/dist/relay/commands.js +284 -26
- package/dist/relay/daemon.js +317 -43
- package/dist/relay/held.js +184 -0
- package/dist/relay/mailbox.js +1 -0
- package/dist/relay/owner-pid.js +129 -0
- package/dist/relay/registry.js +11 -0
- package/dist/relay/socket.js +48 -0
- package/dist/runtime-binary.js +14 -0
- package/dist/runtimes.js +8 -0
- package/package.json +1 -1
package/dist/args.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
28
|
exports.flag = flag;
|
|
29
29
|
exports.positional = positional;
|
|
30
|
+
exports.rejectUnknownFlags = rejectUnknownFlags;
|
|
30
31
|
/**
|
|
31
32
|
* The value of `--name`, or undefined when it was not given one.
|
|
32
33
|
*
|
|
@@ -55,3 +56,17 @@ function flag(args, name) {
|
|
|
55
56
|
function positional(args) {
|
|
56
57
|
return args.find((a) => !a.startsWith("--"));
|
|
57
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Refuse a flag this subcommand does not implement.
|
|
61
|
+
*
|
|
62
|
+
* Silently ignoring one is the worst option available: `relay status --json`
|
|
63
|
+
* printed the human table, exited as if it had honoured the request, and left
|
|
64
|
+
* the caller to discover the absence by parsing prose. A wrong answer that
|
|
65
|
+
* looks right survives far longer than an error.
|
|
66
|
+
*/
|
|
67
|
+
function rejectUnknownFlags(args, allowed, usage) {
|
|
68
|
+
const unknown = args.filter((a) => a.startsWith("--") && !allowed.includes(a));
|
|
69
|
+
if (unknown.length === 0)
|
|
70
|
+
return;
|
|
71
|
+
throw new Error(`unknown option${unknown.length > 1 ? "s" : ""} ${unknown.join(", ")}\nUsage: ${usage}`);
|
|
72
|
+
}
|
package/dist/doctor.js
CHANGED
|
@@ -410,7 +410,8 @@ function skillCheck(runtime, env) {
|
|
|
410
410
|
return { name: "skill", status: "skip", detail: spec.fallback ?? "no command mechanism" };
|
|
411
411
|
}
|
|
412
412
|
const file = `${env.home}/${spec.command.dir}/${spec.command.file}`;
|
|
413
|
-
|
|
413
|
+
const installed = env.readText(file);
|
|
414
|
+
if (installed === null) {
|
|
414
415
|
return {
|
|
415
416
|
name: "skill",
|
|
416
417
|
status: "fail",
|
|
@@ -418,7 +419,25 @@ function skillCheck(runtime, env) {
|
|
|
418
419
|
remedy: setupCommand(runtime),
|
|
419
420
|
};
|
|
420
421
|
}
|
|
421
|
-
|
|
422
|
+
// PRESENT IS NOT CURRENT. Existence was the whole check, so a skill installed
|
|
423
|
+
// months ago passed while telling its agent something this package has since
|
|
424
|
+
// corrected — and `npm publish` does not rewrite an installed file, so the
|
|
425
|
+
// stale copy simply stays. That is not hypothetical: the file on the machine
|
|
426
|
+
// where this was written still said Codex exports no thread id and should
|
|
427
|
+
// background its attach, both of which cost an afternoon of unreachability.
|
|
428
|
+
//
|
|
429
|
+
// A doc that is wrong is worse than one that is missing, because a missing one
|
|
430
|
+
// sends the agent looking.
|
|
431
|
+
const current = (0, runtimes_1.renderCommandFor)(runtime);
|
|
432
|
+
if (current !== null && installed.trim() !== current.trim()) {
|
|
433
|
+
return {
|
|
434
|
+
name: "skill",
|
|
435
|
+
status: "fail",
|
|
436
|
+
detail: `${display(file, env)} is out of date — it does not match the skill this version installs`,
|
|
437
|
+
remedy: setupCommand(runtime),
|
|
438
|
+
};
|
|
439
|
+
}
|
|
440
|
+
return { name: "skill", status: "pass", detail: `${display(file, env)} (current)` };
|
|
422
441
|
}
|
|
423
442
|
function binaryCheck(runtime, env) {
|
|
424
443
|
const command = HAS_BINARY[runtime];
|
package/dist/index.js
CHANGED
|
@@ -254,23 +254,30 @@ async function main() {
|
|
|
254
254
|
const rest = args.filter((a) => a !== sub);
|
|
255
255
|
switch (sub) {
|
|
256
256
|
case "start":
|
|
257
|
+
(0, args_1.rejectUnknownFlags)(rest, ["--foreground"], "baychat relay start [--foreground]");
|
|
257
258
|
await (0, commands_2.cmdRelayStart)({ foreground: args.includes("--foreground") });
|
|
258
259
|
return 0;
|
|
259
260
|
case "status":
|
|
261
|
+
(0, args_1.rejectUnknownFlags)(rest, [], "baychat relay status");
|
|
260
262
|
return await (0, commands_2.cmdRelayStatus)();
|
|
261
263
|
case "stop":
|
|
264
|
+
(0, args_1.rejectUnknownFlags)(rest, [], "baychat relay stop");
|
|
262
265
|
return await (0, commands_2.cmdRelayStop)();
|
|
263
266
|
case "attach": {
|
|
264
267
|
const session = (0, args_1.flag)(rest, "--session");
|
|
265
268
|
if (!session) {
|
|
266
269
|
throw new Error("Usage: baychat relay attach --session <name> [--runtime claude|codex|hermes] [--resume-id <id>] [--timeout <sec>]");
|
|
267
270
|
}
|
|
271
|
+
(0, args_1.rejectUnknownFlags)(rest, ["--session", "--runtime", "--resume-id", "--timeout", "--owner-pid"], "baychat relay attach --session <name> [--runtime claude|codex|hermes] [--resume-id <id>] [--timeout <sec>]");
|
|
268
272
|
const timeoutSec = numberFlag(rest, "--timeout");
|
|
269
273
|
return await (0, commands_2.cmdRelayAttach)({
|
|
270
274
|
session,
|
|
271
275
|
runtime: (0, args_1.flag)(rest, "--runtime") ?? "claude",
|
|
272
276
|
resumeId: (0, args_1.flag)(rest, "--resume-id"),
|
|
273
277
|
timeoutMs: timeoutSec ? timeoutSec * 1000 : undefined,
|
|
278
|
+
// Passed by `relay rearm`, which reads it where the runtime is still
|
|
279
|
+
// an ancestor and hands it to a child that has been detached.
|
|
280
|
+
ownerPid: numberFlag(rest, "--owner-pid"),
|
|
274
281
|
});
|
|
275
282
|
}
|
|
276
283
|
default:
|
package/dist/relay/adapters.js
CHANGED
|
@@ -64,8 +64,24 @@ function buildWakePrompt(session, conversationId, batch, reArm) {
|
|
|
64
64
|
function reArmLines(session, reArm) {
|
|
65
65
|
if (!reArm)
|
|
66
66
|
return [];
|
|
67
|
-
const
|
|
68
|
-
|
|
67
|
+
const attach = `${reArm.node} ${reArm.cli} relay attach --session "${session}" --runtime ${reArm.runtime}`;
|
|
68
|
+
const command = ` ${attach}`;
|
|
69
|
+
// BOUNDED, and it has to be. A foreground attach blocks until a message
|
|
70
|
+
// arrives — `cmdRelayAttach` installs a timer only when `--timeout` is given,
|
|
71
|
+
// and the mailbox path blocks in `readFile(fifo)` with no deadline at all. Told
|
|
72
|
+
// to run that unbounded inside a HEADLESS turn, the session cannot finish, and
|
|
73
|
+
// the daemon sits on it to its own 10-minute limit. Registration takes
|
|
74
|
+
// milliseconds; the wait is not what this is for. Same policy the skill
|
|
75
|
+
// generator states for `arm-once` — see `reachabilityFor` in runtimes.ts.
|
|
76
|
+
const boundedCommand = ` ${attach} --timeout 30`;
|
|
77
|
+
// FOREGROUND for the fifo rung — and for Codex on ANY transport.
|
|
78
|
+
//
|
|
79
|
+
// Branching on transport alone was not enough: a detached Codex whose recorded
|
|
80
|
+
// transport is "socket" or absent fell through to the background text, which
|
|
81
|
+
// is the instruction that started this whole outage. Codex's sandbox kills a
|
|
82
|
+
// backgrounded process when the tool command returns, so the runtime — not the
|
|
83
|
+
// rung — decides this.
|
|
84
|
+
if (reArm.transport === "fifo" || reArm.runtime === "codex") {
|
|
69
85
|
return [
|
|
70
86
|
"",
|
|
71
87
|
"To stay reachable, run this IN THE FOREGROUND before ending your turn. It",
|
|
@@ -73,7 +89,9 @@ function reArmLines(session, reArm) {
|
|
|
73
89
|
"Do NOT put it in the background: your sandbox kills backgrounded processes",
|
|
74
90
|
"when the command returns, so a backgrounded attach listens to nothing while",
|
|
75
91
|
"looking like it worked.",
|
|
76
|
-
command,
|
|
92
|
+
"`--timeout 30` is part of the command, not a suggestion: registering is the",
|
|
93
|
+
"point, and an unbounded wait would hold this turn open for no benefit.",
|
|
94
|
+
boundedCommand,
|
|
77
95
|
];
|
|
78
96
|
}
|
|
79
97
|
return [
|
package/dist/relay/commands.js
CHANGED
|
@@ -43,18 +43,22 @@ exports.attachViaMailbox = attachViaMailbox;
|
|
|
43
43
|
exports.renderSessionLine = renderSessionLine;
|
|
44
44
|
exports.cmdRelayAttach = cmdRelayAttach;
|
|
45
45
|
exports.resolveRuntimeBin = resolveRuntimeBin;
|
|
46
|
+
exports.decideRuntimeBin = decideRuntimeBin;
|
|
46
47
|
const child_process_1 = require("child_process");
|
|
47
48
|
const fs = __importStar(require("fs"));
|
|
48
49
|
const net = __importStar(require("net"));
|
|
50
|
+
const path = __importStar(require("path"));
|
|
49
51
|
const util_1 = require("util");
|
|
50
52
|
const adapters_1 = require("./adapters");
|
|
51
53
|
const autostart_1 = require("./autostart");
|
|
52
54
|
const runtime_binary_1 = require("../runtime-binary");
|
|
55
|
+
const owner_pid_1 = require("./owner-pid");
|
|
56
|
+
const parent_watch_1 = require("./parent-watch");
|
|
53
57
|
const daemon_1 = require("./daemon");
|
|
54
58
|
const resume_1 = require("./resume");
|
|
55
59
|
const mailbox_1 = require("./mailbox");
|
|
56
60
|
const socket_1 = require("./socket");
|
|
57
|
-
const
|
|
61
|
+
const parent_watch_2 = require("./parent-watch");
|
|
58
62
|
const execFileAsync = (0, util_1.promisify)(child_process_1.execFile);
|
|
59
63
|
/** Connect to a running daemon, or explain that there isn't one. */
|
|
60
64
|
async function connectOrFail() {
|
|
@@ -209,24 +213,98 @@ async function cmdRelayStatus() {
|
|
|
209
213
|
console.log(` polls: ${status.polls} delivered: ${status.delivered} cursor: ${status.cursor ?? "—"}`);
|
|
210
214
|
if (status.lastError)
|
|
211
215
|
console.log(` last error: ${status.lastError}`);
|
|
216
|
+
// Computed before the session list so a line can report its own last failure.
|
|
217
|
+
const byName = new Map(status.sessions.map((s) => [s.name, s]));
|
|
218
|
+
/**
|
|
219
|
+
* Why this failure no longer stands, or undefined if it still does.
|
|
220
|
+
*
|
|
221
|
+
* Returns the REASON rather than a boolean so the line can say what cleared
|
|
222
|
+
* it. "(stale)" with no cause is the same species of unexamined claim this
|
|
223
|
+
* command exists to stop printing.
|
|
224
|
+
*/
|
|
225
|
+
const staleReason = (p) => {
|
|
226
|
+
const session = byName.get(p.session);
|
|
227
|
+
// No session at all means nothing has been repaired — that is live, not stale.
|
|
228
|
+
if (!session)
|
|
229
|
+
return undefined;
|
|
230
|
+
// TWO WITNESSES, AND ONLY TWO. Everything weaker was tried and was wrong.
|
|
231
|
+
//
|
|
232
|
+
// A live SOCKET is demonstrably reachable — the attach is holding it now, so
|
|
233
|
+
// whatever this entry records is no longer true. `attached` alone is not
|
|
234
|
+
// that: the daemon sets it for a mailbox REGISTRATION too, and the fifo rung
|
|
235
|
+
// itself refuses to trust one, proving a reader with ENXIO on every write.
|
|
236
|
+
if (session.attached && session.transport !== "fifo")
|
|
237
|
+
return "an attach is holding the socket now";
|
|
238
|
+
// A delivery that actually LANDED after the failure. `registeredAt` was used
|
|
239
|
+
// for this and should not have been: it proves `relay attach` was rerun, and
|
|
240
|
+
// the npm/snap outage was made entirely of attaches that registered
|
|
241
|
+
// perfectly and then failed every single delivery.
|
|
242
|
+
const delivered = session.lastDeliveredAt;
|
|
243
|
+
if (delivered !== undefined && Date.parse(delivered) > Date.parse(p.at)) {
|
|
244
|
+
return `a later delivery landed at ${delivered}`;
|
|
245
|
+
}
|
|
246
|
+
return undefined;
|
|
247
|
+
};
|
|
248
|
+
const lastFailureFor = new Map();
|
|
249
|
+
for (const p of status.pending) {
|
|
250
|
+
if (!staleReason(p))
|
|
251
|
+
lastFailureFor.set(p.session, p.reason);
|
|
252
|
+
}
|
|
212
253
|
console.log(`\nSessions (${status.sessions.length}):`);
|
|
213
254
|
if (status.sessions.length === 0) {
|
|
214
255
|
console.log(" none — a session registers itself by running `baychat relay attach`");
|
|
215
256
|
}
|
|
216
257
|
for (const s of status.sessions) {
|
|
217
|
-
const state = sessionState(s);
|
|
258
|
+
const state = sessionState(s, lastFailureFor.get(s.name));
|
|
218
259
|
console.log(` ${s.name} [${s.runtime}] ${state}`);
|
|
219
260
|
console.log(` ${resumeLabel(s)}`);
|
|
220
261
|
}
|
|
262
|
+
// HELD is a THIRD state and prints as itself.
|
|
263
|
+
//
|
|
264
|
+
// These messages reached this box, were not delivered, and are not a failure:
|
|
265
|
+
// the agent is mid-turn and they go over the moment it re-arms. Folding them
|
|
266
|
+
// into pending would paint a working relay red; leaving them out is what we
|
|
267
|
+
// came from — before this they lived only in the daemon's heap, so a restart
|
|
268
|
+
// lost them silently and `relay status` never mentioned they had existed.
|
|
269
|
+
//
|
|
270
|
+
// Deliberately does NOT affect the exit code. A monitor should page on
|
|
271
|
+
// pending, never on an agent that is simply busy.
|
|
272
|
+
const held = status.held ?? [];
|
|
273
|
+
if (held.length > 0) {
|
|
274
|
+
const total = held.reduce((n, h) => n + h.count, 0);
|
|
275
|
+
console.log(`\nHELD (${total}) — waiting for a busy session to re-arm, not lost:`);
|
|
276
|
+
for (const h of held) {
|
|
277
|
+
console.log(` ${h.heldAt} ${h.session} ${h.count} message(s) in ${h.conversationId}`);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
221
280
|
// Pending is the point of the whole command: these are messages that reached
|
|
222
281
|
// this machine and that nobody answered.
|
|
282
|
+
//
|
|
283
|
+
// But a failure the operator has already FIXED must stop shouting. Pending
|
|
284
|
+
// records are never removed, so before this a single bad afternoon left the
|
|
285
|
+
// command exiting 2 forever — and a status that is permanently red is a status
|
|
286
|
+
// nobody reads. An entry that predates its session's current registration
|
|
287
|
+
// describes a session that has since been repaired: still worth printing as
|
|
288
|
+
// history, no longer worth alerting on.
|
|
223
289
|
if (status.pending.length > 0) {
|
|
224
|
-
|
|
290
|
+
const live = status.pending.filter((p) => !staleReason(p));
|
|
291
|
+
const stale = status.pending.length - live.length;
|
|
292
|
+
const heading = live.length > 0
|
|
293
|
+
? `DELIVERY PENDING (${live.length}) — reached this box, not answered:`
|
|
294
|
+
: `DELIVERY PENDING (0 live, ${stale} historical) — every one has been superseded by a later success:`;
|
|
295
|
+
console.log(`\n${heading}`);
|
|
225
296
|
for (const p of status.pending.slice(-10)) {
|
|
226
|
-
|
|
297
|
+
const why = staleReason(p);
|
|
298
|
+
console.log(` ${p.at} ${p.session} msg ${p.messageId}${why ? ` (stale — ${why})` : ""}`);
|
|
227
299
|
console.log(` ${p.reason}`);
|
|
228
300
|
}
|
|
229
|
-
|
|
301
|
+
if (live.length > 0 && stale > 0) {
|
|
302
|
+
console.log(` (${stale} older entr${stale === 1 ? "y" : "ies"} marked stale — superseded by a later success.)`);
|
|
303
|
+
}
|
|
304
|
+
// Distinct exit code so a monitor can alert on it — but only for failures
|
|
305
|
+
// that still stand.
|
|
306
|
+
if (live.length > 0)
|
|
307
|
+
return 2;
|
|
230
308
|
}
|
|
231
309
|
return 0;
|
|
232
310
|
}
|
|
@@ -340,19 +418,51 @@ async function attachViaMailbox(opts) {
|
|
|
340
418
|
resumeEvidence: opts.resume.ok ? opts.resume.evidence : undefined,
|
|
341
419
|
resumeCwd: opts.resume.ok ? opts.resume.cwd : undefined,
|
|
342
420
|
cwd: process.cwd(),
|
|
343
|
-
// Resolved
|
|
344
|
-
//
|
|
345
|
-
//
|
|
346
|
-
runtimeBin:
|
|
421
|
+
// Resolved inside the session, for the same reason the socket rung does it:
|
|
422
|
+
// the binary a session was launched with is knowable here and nowhere else.
|
|
423
|
+
// Passed in rather than re-probed — see `decideRuntimeBin`.
|
|
424
|
+
runtimeBin: opts.runtimeBin,
|
|
347
425
|
fifo,
|
|
348
426
|
pid: process.pid,
|
|
427
|
+
// The SESSION's pid, not this attach's and not its shell's. `pid` above dies
|
|
428
|
+
// with every wake, and `process.ppid` is a throwaway wrapper that exits in
|
|
429
|
+
// seconds — recording either told the daemon a live session had gone, and it
|
|
430
|
+
// spawned a headless duplicate. See `currentOwnerPid`.
|
|
431
|
+
ownerPid: opts.ownerPid ?? (0, owner_pid_1.currentOwnerPid)(opts.runtime),
|
|
349
432
|
registeredAt: new Date().toISOString(),
|
|
350
433
|
});
|
|
351
|
-
|
|
434
|
+
const timeoutMs = opts.timeoutMs;
|
|
435
|
+
// NON-BLOCKING, because the obvious version cannot be cancelled.
|
|
436
|
+
//
|
|
437
|
+
// `readFile(fifo)` blocks in `open(2)` inside the libuv threadpool. No signal
|
|
438
|
+
// reaches it, and — measured — `process.exit()` does not end the process
|
|
439
|
+
// either: Node waits for that worker, so a "timeout" printed there is a lie the
|
|
440
|
+
// caller then hangs behind. An earlier fix here claimed a bound and shipped
|
|
441
|
+
// exactly that; the test missed it by never reaching the read.
|
|
442
|
+
//
|
|
443
|
+
// `O_RDONLY | O_NONBLOCK` opens a FIFO with no writer IMMEDIATELY, and reads
|
|
444
|
+
// then return nothing until the daemon writes. Polling that is genuinely
|
|
445
|
+
// cancellable: the deadline is a plain loop condition, the process can exit on
|
|
446
|
+
// its own, and there is nothing left blocked behind it.
|
|
447
|
+
const deadline = timeoutMs === undefined ? Number.POSITIVE_INFINITY : Date.now() + timeoutMs;
|
|
448
|
+
try {
|
|
449
|
+
await (0, mailbox_1.awaitWakeFifo)(fifo, Math.min(timeoutMs ?? 30_000, 30_000));
|
|
450
|
+
}
|
|
451
|
+
catch (err) {
|
|
452
|
+
// The relay never made the FIFO. Bounded arm-once treats that as its timeout
|
|
453
|
+
// rather than an error: the registration above still happened, which is the
|
|
454
|
+
// point of arming once.
|
|
455
|
+
if (timeoutMs === undefined)
|
|
456
|
+
throw err;
|
|
457
|
+
console.log(err instanceof Error ? err.message : String(err));
|
|
458
|
+
return 2;
|
|
459
|
+
}
|
|
352
460
|
console.log(`Attached as "${opts.session}" over a mailbox FIFO (${fifo}). Waiting for messages…`);
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
461
|
+
const raw = await readWakeBounded(fifo, deadline);
|
|
462
|
+
if (raw === "timeout") {
|
|
463
|
+
console.log("No new messages before timeout. Registered and reachable — the relay will wake this session.");
|
|
464
|
+
return 2;
|
|
465
|
+
}
|
|
356
466
|
const frame = JSON.parse(raw.trim());
|
|
357
467
|
console.log(`WAKE ${frame.messages.length} message(s) in ${frame.conversationId}:`);
|
|
358
468
|
for (const m of frame.messages) {
|
|
@@ -361,8 +471,49 @@ async function attachViaMailbox(opts) {
|
|
|
361
471
|
}
|
|
362
472
|
return 0;
|
|
363
473
|
}
|
|
474
|
+
/**
|
|
475
|
+
* Wait for one wake frame on a FIFO, without ever blocking uninterruptibly.
|
|
476
|
+
*
|
|
477
|
+
* Returns "timeout" instead of throwing: for `arm-once` a timeout is the normal
|
|
478
|
+
* ending, not a failure — registration already happened, and the relay owns
|
|
479
|
+
* every later wake.
|
|
480
|
+
*/
|
|
481
|
+
async function readWakeBounded(fifo, deadline, pollMs = 25) {
|
|
482
|
+
const handle = await fs.promises.open(fifo, fs.constants.O_RDONLY | fs.constants.O_NONBLOCK);
|
|
483
|
+
try {
|
|
484
|
+
const buf = Buffer.alloc(64 * 1024);
|
|
485
|
+
let acc = "";
|
|
486
|
+
for (;;) {
|
|
487
|
+
let bytes = 0;
|
|
488
|
+
try {
|
|
489
|
+
({ bytesRead: bytes } = await handle.read(buf, 0, buf.length, null));
|
|
490
|
+
}
|
|
491
|
+
catch (err) {
|
|
492
|
+
// EAGAIN is "no writer has written yet" on a non-blocking FIFO, which is
|
|
493
|
+
// the normal state of this wait. Anything else is a real fault.
|
|
494
|
+
if (err.code !== "EAGAIN")
|
|
495
|
+
throw err;
|
|
496
|
+
}
|
|
497
|
+
// A read of 0 is NOT end-of-input here. With no writer attached a
|
|
498
|
+
// non-blocking FIFO reports EOF, and the writer we are waiting for has not
|
|
499
|
+
// arrived yet — treating it as the end would turn every wait into an
|
|
500
|
+
// instant empty answer.
|
|
501
|
+
if (bytes > 0) {
|
|
502
|
+
acc += buf.subarray(0, bytes).toString("utf8");
|
|
503
|
+
if (acc.includes("\n"))
|
|
504
|
+
return acc;
|
|
505
|
+
}
|
|
506
|
+
if (Date.now() >= deadline)
|
|
507
|
+
return "timeout";
|
|
508
|
+
await new Promise((r) => setTimeout(r, pollMs));
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
finally {
|
|
512
|
+
await handle.close();
|
|
513
|
+
}
|
|
514
|
+
}
|
|
364
515
|
/** The state half of one `relay status` session line. */
|
|
365
|
-
function sessionState(s) {
|
|
516
|
+
function sessionState(s, lastWakeFailure) {
|
|
366
517
|
// "registered", not "attached", for the FIFO rung — and the difference is not
|
|
367
518
|
// pedantry. A socket attach IS a held connection, so `attached` is an
|
|
368
519
|
// observation. A mailbox registration is a file on disk; whether the agent is
|
|
@@ -370,8 +521,50 @@ function sessionState(s) {
|
|
|
370
521
|
// a reader blocked in open() holds no descriptor for /proc to see and opening
|
|
371
522
|
// the write end to look would signal EOF. Delivery is the only honest probe,
|
|
372
523
|
// and it makes it: a wake with no reader is recorded pending, never delivered.
|
|
524
|
+
// A live SOCKET outranks a past failure: the attach is holding it right now.
|
|
525
|
+
// A FIFO REGISTRATION does not, and must not short-circuit the check below —
|
|
526
|
+
// that is the same "a registration is not a reader" rule `staleReason` applies
|
|
527
|
+
// to the pending list, and leaving it out here meant a session whose last wake
|
|
528
|
+
// died still printed a clean `registered (fifo)` on the one line a human reads.
|
|
529
|
+
if (s.attached && s.transport !== "fifo")
|
|
530
|
+
return "attached";
|
|
531
|
+
if (s.attached && !lastWakeFailure)
|
|
532
|
+
return "registered (fifo)";
|
|
373
533
|
if (s.attached)
|
|
374
|
-
return
|
|
534
|
+
return `registered (fifo) — LAST WAKE FAILED, not reachable: ${lastWakeFailure}`;
|
|
535
|
+
// "headless resume ready" is a claim about a PRECONDITION — that we hold an id
|
|
536
|
+
// — and for a long time it was printed even while every wake using that id had
|
|
537
|
+
// failed. A non-engineer reads "ready" as "fine", so the one line that answers
|
|
538
|
+
// "is anything listening?" was the line most likely to mislead. If a wake has
|
|
539
|
+
// actually been tried and failed since this session registered, say THAT: an
|
|
540
|
+
// outcome outranks a precondition.
|
|
541
|
+
if (lastWakeFailure)
|
|
542
|
+
return `detached — LAST WAKE FAILED, not reachable: ${lastWakeFailure}`;
|
|
543
|
+
// INTERRUPTED, NOT GONE — and this line used to read "headless resume ready",
|
|
544
|
+
// which sounds fine and is now actively false.
|
|
545
|
+
//
|
|
546
|
+
// Stopping a task in the driving harness kills its background `relay attach`
|
|
547
|
+
// from OUTSIDE the process: no self-detach line is printed, nothing fails, the
|
|
548
|
+
// registration simply stops having a listener. And interrupting your own agent
|
|
549
|
+
// is an ORDINARY thing to do — every other fault in this release needed an
|
|
550
|
+
// unusual machine; this one needs Ctrl+C.
|
|
551
|
+
//
|
|
552
|
+
// "Headless resume ready" is wrong here for a specific reason: the daemon will
|
|
553
|
+
// NOT resume a session whose owning process is still alive — that is the
|
|
554
|
+
// no-clone guard — so a wake is held as pending instead. Reachable is exactly
|
|
555
|
+
// what this session is not.
|
|
556
|
+
//
|
|
557
|
+
// SCOPED TO RUNTIMES WITH NO LIVE-SESSION QUEUE. Codex is deliberately
|
|
558
|
+
// `arm-once`: its bounded attach exits and the session is SUPPOSED to sit
|
|
559
|
+
// detached with a live owner, because rung 2 hands the message to
|
|
560
|
+
// `codex queue` — which reaches the live session, and is tried BEFORE the
|
|
561
|
+
// no-clone guard is ever consulted. Warning there would alarm every healthy
|
|
562
|
+
// Codex on the machine, and a warning that fires when nothing is wrong is how
|
|
563
|
+
// a real one stops being read.
|
|
564
|
+
const hasLiveQueue = (0, adapters_1.adapterFor)(s.runtime).queueMessage !== undefined;
|
|
565
|
+
if (!hasLiveQueue && s.ownerPid !== undefined && (0, parent_watch_1.processIsAlive)(s.ownerPid)) {
|
|
566
|
+
return "detached — NOTHING IS LISTENING, re-arm: the session is alive but its attach is gone (interrupted?), so a wake will be held, not resumed";
|
|
567
|
+
}
|
|
375
568
|
return s.resumeId ? "detached (headless resume ready)" : "detached (no resume id)";
|
|
376
569
|
}
|
|
377
570
|
/**
|
|
@@ -380,8 +573,8 @@ function sessionState(s) {
|
|
|
380
573
|
* Exported so the rung's wording is testable: a fallback nobody can see in
|
|
381
574
|
* `status` is a silent fallback, which is the thing this transport must not be.
|
|
382
575
|
*/
|
|
383
|
-
function renderSessionLine(target) {
|
|
384
|
-
return ` ${target.name} [${target.runtime}] ${sessionState(target)}`;
|
|
576
|
+
function renderSessionLine(target, lastWakeFailure) {
|
|
577
|
+
return ` ${target.name} [${target.runtime}] ${sessionState(target, lastWakeFailure)}`;
|
|
385
578
|
}
|
|
386
579
|
async function cmdRelayAttach(opts) {
|
|
387
580
|
if (!(0, adapters_1.isKnownRuntime)(opts.runtime)) {
|
|
@@ -389,13 +582,35 @@ async function cmdRelayAttach(opts) {
|
|
|
389
582
|
return 1;
|
|
390
583
|
}
|
|
391
584
|
const runtime = opts.runtime;
|
|
585
|
+
// An explicit BAYCHAT_<RUNTIME>_BIN is a user ASSERTION, not a hint: they have
|
|
586
|
+
// told us which binary this session runs under. If it cannot be proven, the
|
|
587
|
+
// only honest move is to stop. Recording `runtimeBin: undefined` and attaching
|
|
588
|
+
// anyway looks like success and hands the daemon back exactly the guess this
|
|
589
|
+
// setting exists to prevent — which is how a confined snap `codex` came to be
|
|
590
|
+
// spawned for an npm session and every wake died "no rollout found".
|
|
591
|
+
// Probed ONCE, here, and carried to whichever transport wins below.
|
|
592
|
+
const runtimeBin = decideRuntimeBin(runtime);
|
|
593
|
+
if (!runtimeBin.ok) {
|
|
594
|
+
console.log(`BAYCHAT_${runtime.toUpperCase()}_BIN is set but unusable: ${runtimeBin.reason}`);
|
|
595
|
+
console.log("Not attaching. Fix it or unset it — nothing was registered, so the relay will not report this session as reachable.");
|
|
596
|
+
return 1;
|
|
597
|
+
}
|
|
392
598
|
const resume = await resolveAttachResumeId(runtime, opts.resumeId, opts.discovery);
|
|
393
599
|
const sockPath = (0, socket_1.socketPath)();
|
|
394
600
|
const probe = await (0, socket_1.probeSocketDetailed)(sockPath);
|
|
395
601
|
if (shouldFallBackToMailbox(probe)) {
|
|
396
602
|
console.log(`Relay socket refused (${probe.alive ? "" : (probe.code ?? "denied")}) — this session is sandboxed.`);
|
|
397
603
|
console.log("Falling back to a mailbox FIFO, which a sandbox permits. `relay status` will show this session as attached (fifo).");
|
|
398
|
-
return attachViaMailbox({
|
|
604
|
+
return attachViaMailbox({
|
|
605
|
+
session: opts.session,
|
|
606
|
+
runtime,
|
|
607
|
+
resume,
|
|
608
|
+
runtimeBin: runtimeBin.bin,
|
|
609
|
+
ownerPid: opts.ownerPid,
|
|
610
|
+
// Honoured on BOTH transports now. It was socket-only, which made
|
|
611
|
+
// `--timeout 30` a no-op precisely where a sandboxed session needs it.
|
|
612
|
+
timeoutMs: opts.timeoutMs,
|
|
613
|
+
});
|
|
399
614
|
}
|
|
400
615
|
if (!probe.alive) {
|
|
401
616
|
console.log((0, socket_1.describeProbeFailure)(sockPath, probe));
|
|
@@ -414,7 +629,7 @@ async function cmdRelayAttach(opts) {
|
|
|
414
629
|
// this process must go with it — otherwise it keeps the socket open, the
|
|
415
630
|
// daemon keeps believing the session is live, and the next wake is written
|
|
416
631
|
// into a corpse and recorded as delivered. See ./parent-watch.ts.
|
|
417
|
-
const parentWatch = (0,
|
|
632
|
+
const parentWatch = (0, parent_watch_2.watchParent)(process.ppid, () => {
|
|
418
633
|
console.log("Session that started this attach has exited — detaching so the relay stops treating it as live.");
|
|
419
634
|
sock.end();
|
|
420
635
|
resolve(3);
|
|
@@ -468,9 +683,12 @@ async function cmdRelayAttach(opts) {
|
|
|
468
683
|
resumeEvidence: resume.ok ? resume.evidence : undefined,
|
|
469
684
|
resumeCwd: resume.ok ? resume.cwd : undefined,
|
|
470
685
|
cwd: process.cwd(),
|
|
471
|
-
//
|
|
472
|
-
|
|
473
|
-
|
|
686
|
+
// Decided once above, not re-probed here — see `decideRuntimeBin`.
|
|
687
|
+
runtimeBin: runtimeBin.bin,
|
|
688
|
+
// See SessionTarget.ownerPid: the runtime process that owns this session —
|
|
689
|
+
// found by walking past the launching shell, which does not outlive the
|
|
690
|
+
// wake and so cannot witness anything.
|
|
691
|
+
ownerPid: opts.ownerPid ?? (0, owner_pid_1.currentOwnerPid)(runtime),
|
|
474
692
|
});
|
|
475
693
|
});
|
|
476
694
|
}
|
|
@@ -510,19 +728,59 @@ function resolveRuntimeBin(runtime) {
|
|
|
510
728
|
// `resolveRuntimeBinary` tries the platform's real extension order and PROVES
|
|
511
729
|
// each candidate by running it, which is the same question this function was
|
|
512
730
|
// always asking — just answered correctly.
|
|
731
|
+
const decided = decideRuntimeBin(runtime);
|
|
732
|
+
return decided.ok ? decided.bin : undefined;
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* What to record for this runtime, or why the attach must not proceed.
|
|
736
|
+
*
|
|
737
|
+
* ONE probe, and the caller carries the answer. `resolveRuntimeBinary` SPAWNS
|
|
738
|
+
* each candidate to prove it, so asking twice is not merely wasteful: the second
|
|
739
|
+
* answer can differ from the first. An attach that passed the override guard and
|
|
740
|
+
* then re-probed could still register `runtimeBin: undefined` — the exact state
|
|
741
|
+
* the guard exists to prevent, reached by way of the guard.
|
|
742
|
+
*/
|
|
743
|
+
function decideRuntimeBin(runtime) {
|
|
513
744
|
const override = process.env[`BAYCHAT_${runtime.toUpperCase()}_BIN`];
|
|
514
745
|
const resolved = (0, runtime_binary_1.resolveRuntimeBinary)(runtime, (0, runtime_binary_1.currentBinaryEnv)(override));
|
|
515
|
-
if (!resolved.ok)
|
|
516
|
-
|
|
746
|
+
if (!resolved.ok) {
|
|
747
|
+
// An explicit override is a user ASSERTION. If it cannot be proven, stop —
|
|
748
|
+
// see the guard in `cmdRelayAttach`. Absence of one is not an assertion, so
|
|
749
|
+
// a plain failed probe keeps the best-effort fallback.
|
|
750
|
+
if (override)
|
|
751
|
+
return { ok: false, reason: (0, runtime_binary_1.summarizeResolutionFailure)(resolved) };
|
|
752
|
+
return { ok: true, bin: managedRuntimeBin(runtime) };
|
|
753
|
+
}
|
|
517
754
|
try {
|
|
518
755
|
// Resolve symlinks: ~/.local/bin/claude is typically a link into a versioned
|
|
519
756
|
// directory, and recording the link means a later version bump silently
|
|
520
757
|
// repoints every wake. The real path is what this session is actually running.
|
|
521
|
-
return fs.realpathSync(resolved.path);
|
|
758
|
+
return { ok: true, bin: fs.realpathSync(resolved.path) };
|
|
522
759
|
}
|
|
523
760
|
catch {
|
|
524
761
|
// A path we just ran but cannot realpath is still the right answer.
|
|
525
|
-
return resolved.path;
|
|
762
|
+
return { ok: true, bin: resolved.path };
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
function managedRuntimeBin(runtime) {
|
|
766
|
+
if (runtime !== "codex")
|
|
767
|
+
return undefined;
|
|
768
|
+
const root = process.env.CODEX_MANAGED_PACKAGE_ROOT?.trim();
|
|
769
|
+
if (!root)
|
|
770
|
+
return undefined;
|
|
771
|
+
const candidate = path.join(root, "bin", "codex.js");
|
|
772
|
+
// PROVEN, not assumed. `statSync().isFile()` was the whole check here, so any
|
|
773
|
+
// file at that path counted — including one that cannot be spawned. Recording
|
|
774
|
+
// an unrunnable path is the failure this fallback exists to prevent, so it is
|
|
775
|
+
// put through the same prover an override gets: spawn it, ask its version.
|
|
776
|
+
const proved = (0, runtime_binary_1.resolveRuntimeBinary)(runtime, (0, runtime_binary_1.currentBinaryEnv)(candidate));
|
|
777
|
+
if (!proved.ok)
|
|
778
|
+
return undefined;
|
|
779
|
+
try {
|
|
780
|
+
return fs.realpathSync(proved.path);
|
|
781
|
+
}
|
|
782
|
+
catch {
|
|
783
|
+
return proved.path;
|
|
526
784
|
}
|
|
527
785
|
}
|
|
528
786
|
/**
|