baychat 0.52.0 → 0.54.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.
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  /**
3
- * Pane captures for the launcher's screen-matching tests. The stable phrases are the ones the owner
4
- * quoted from Claude Code (2026-10-05); the surrounding layout is a representative capture.
3
+ * Pane captures for the launcher's screen-matching tests. The dev-channel and folder-trust phrases are
4
+ * the ones the owner quoted from Claude Code (2026-10-05), in a representative layout; the Code Mode
5
+ * screen is a verbatim capture.
5
6
  */
6
7
  Object.defineProperty(exports, "__esModule", { value: true });
7
- exports.CLAUDE_PROMPT = exports.FOLDER_TRUST = exports.DEV_CHANNEL_WARNING_EXIT_SELECTED = exports.DEV_CHANNEL_WARNING = void 0;
8
+ exports.CLAUDE_PROMPT = exports.UNKNOWN_PICKER = exports.CODE_MODE_YES_SELECTED = exports.CODE_MODE_OPT_IN = exports.FOLDER_TRUST = exports.DEV_CHANNEL_WARNING_EXIT_SELECTED = exports.DEV_CHANNEL_WARNING = void 0;
8
9
  exports.DEV_CHANNEL_WARNING = [
9
10
  "╭──────────────────────────────────────────────────────────────────────────╮",
10
11
  "│ WARNING: Loading development channels │",
@@ -37,5 +38,48 @@ exports.FOLDER_TRUST = [
37
38
  " Enter to confirm · Esc to cancel",
38
39
  "",
39
40
  ].join("\n");
41
+ /**
42
+ * Claude Code 2.1.3xx's "Code Mode (experimental)" opt-in, shown before anything else when
43
+ * CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1 is set. Captured VERBATIM with `tmux capture-pane -p` on the PC
44
+ * (120 columns, 19 Nov 2026): no box border, no option numbers, the cursor on "No".
45
+ */
46
+ exports.CODE_MODE_OPT_IN = [
47
+ "────────────────────────────────────────────────────────────────────────────────────────────────────────────────",
48
+ " Code Mode (experimental)",
49
+ "",
50
+ " In Code Mode, Claude works by writing and running JavaScript that calls its tools, instead of calling them",
51
+ " one at a time.",
52
+ "",
53
+ " This is an early experiment. It can behave differently from normal Claude Code, and it may change or be",
54
+ " removed in any release. Remote Control, Claude in Chrome and computer use are off in Code Mode.",
55
+ "",
56
+ " A session that you start in Code Mode can be resumed only with Code Mode on. If you turn Code Mode off, turn",
57
+ " it on again to resume the session. Where Code Mode isn't available, the session can't be resumed: cloud",
58
+ " sessions and the Claude apps can't continue it, and older versions of Claude Code may open it but not work",
59
+ " properly in it.",
60
+ "",
61
+ " Sessions start in the same permission mode as without Code Mode, which can be auto mode where it's",
62
+ " available. In auto mode, the safety check reads each piece of JavaScript once, before it runs. It doesn't",
63
+ " check each tool call that the code then makes, so it can't see values that the code works out as it runs.",
64
+ "",
65
+ " ❯ No, start without it",
66
+ " Yes, turn on Code Mode",
67
+ "",
68
+ " Tell us how it goes with /feedback",
69
+ "",
70
+ " Enter to confirm · Esc to start without it",
71
+ ].join("\n");
72
+ /** The same screen after one Down: the cursor on "Yes, turn on Code Mode". */
73
+ exports.CODE_MODE_YES_SELECTED = exports.CODE_MODE_OPT_IN.replace(" ❯ No, start without it", " No, start without it").replace(" Yes, turn on Code Mode", " ❯ Yes, turn on Code Mode");
74
+ /** A picker nobody here knows: Claude Code's footer and a selection, on a question of its own. */
75
+ exports.UNKNOWN_PICKER = [
76
+ "────────────────────────────────────────────────────────────────────────────────",
77
+ " Something new Claude Code wants to know",
78
+ "",
79
+ " ❯ 1. One way",
80
+ " 2. Another way",
81
+ "",
82
+ " Enter to confirm · Esc to cancel",
83
+ ].join("\n");
40
84
  /** Claude Code's ordinary prompt, past every question. */
41
85
  exports.CLAUDE_PROMPT = ["╭─────────────────────────╮", "│ > /baychat mac-1 │", "╰─────────────────────────╯", " ? for shortcuts", ""].join("\n");
@@ -1,34 +1,49 @@
1
1
  "use strict";
2
2
  /**
3
- * Recognising the two Claude Code screens a detached launch can stop at — pure, so the rule that
3
+ * Recognising the Claude Code screens a detached launch can stop at — pure, so the rule that
4
4
  * decides whether a key is ever pressed is testable on captured text.
5
5
  *
6
6
  * ── STRICT ON PURPOSE ─────────────────────────────────────────────────────────────────
7
- * The only key the launcher ever sends is ONE Enter, and only on the development-channels warning
8
- * after the computer's owner tapped Allow (Karmen, 2026-10-05). So the warning is recognised only
9
- * when ALL of its stable phrases are on screen, and the Enter only when the selected row is
10
- * option 1 ("I am using this for local development"). Anything less is "other", and "other" is
11
- * never answered.
7
+ * The launcher presses keys only after the computer's owner tapped an answer, and only on the
8
+ * screen that answer was for (Karmen, 2026-10-05; Code Mode, vps 20 Nov):
9
+ * • the development-channels warning: ONE Enter, only when the selected row is option 1
10
+ * ("I am using this for local development");
11
+ * • the "Code Mode (experimental)" opt-in: Enter on "No, start without it", or one Down and then
12
+ * Enter on "Yes, turn on Code Mode" — each only with the one `❯` on that very row.
13
+ * Each screen is recognised only when ALL of its stable phrases are on screen. Anything less is
14
+ * "other", and "other" is never answered.
12
15
  *
13
16
  * The folder-trust screen ("Quick safety check") is recognised so it can be REFUSED: trust is the
14
17
  * person at the computer's decision (launch/claude-trust.ts), never the network's.
15
18
  */
16
19
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.FOLDER_TRUST_PHRASES = exports.DEV_CHANNEL_WARNING_PHRASES = void 0;
20
+ exports.FOLDER_TRUST_PHRASES = exports.CODE_MODE_PHRASES = exports.DEV_CHANNEL_WARNING_PHRASES = void 0;
18
21
  exports.classifyPane = classifyPane;
19
22
  exports.firstOptionSelected = firstOptionSelected;
20
23
  exports.safeToConfirm = safeToConfirm;
24
+ exports.codeModeSelection = codeModeSelection;
25
+ exports.safeToAnswerCodeMode = safeToAnswerCodeMode;
26
+ exports.pickerFooterShown = pickerFooterShown;
21
27
  /** Every one of these must be on screen for the development-channels warning. */
22
28
  exports.DEV_CHANNEL_WARNING_PHRASES = ["Loading development channels", "I am using this for local development", "Enter to confirm"];
29
+ /** Every one of these must be on screen for Claude Code's Code Mode opt-in (2.1.3xx). */
30
+ exports.CODE_MODE_PHRASES = ["Code Mode (experimental)", "No, start without it", "Yes, turn on Code Mode"];
23
31
  /** Both of these must be on screen for Claude Code's workspace-trust question. */
24
32
  exports.FOLDER_TRUST_PHRASES = ["Quick safety check", "Yes, I trust this folder"];
25
33
  function classifyPane(text) {
26
34
  if (exports.DEV_CHANNEL_WARNING_PHRASES.every((p) => text.includes(p)))
27
35
  return "dev-channel-warning";
36
+ if (exports.CODE_MODE_PHRASES.every((p) => text.includes(p)))
37
+ return "code-mode-opt-in";
28
38
  if (exports.FOLDER_TRUST_PHRASES.every((p) => text.includes(p)))
29
39
  return "folder-trust";
30
40
  return "other";
31
41
  }
42
+ /** The one line holding a `❯`, or null when there is none or more than one. */
43
+ function selectedLine(text) {
44
+ const selected = text.split("\n").filter((line) => line.includes("❯"));
45
+ return selected.length === 1 ? selected[0] : null;
46
+ }
32
47
  /**
33
48
  * The selection marker on option 1 of the warning, on one line. Only whitespace and a box border
34
49
  * (`│`) may come before the marker; nothing else.
@@ -36,12 +51,43 @@ function classifyPane(text) {
36
51
  const FIRST_OPTION_SELECTED = /^[\s│]*❯\s*1\.\s*I am using this for local development/;
37
52
  /** True only when option 1 ("I am using this for local development") is the selected row. */
38
53
  function firstOptionSelected(text) {
39
- const lines = text.split("\n");
40
- const selected = lines.filter((line) => line.includes("❯"));
54
+ const line = selectedLine(text);
41
55
  // Exactly one selection marker, and it is on option 1.
42
- return selected.length === 1 && FIRST_OPTION_SELECTED.test(selected[0]);
56
+ return line !== null && FIRST_OPTION_SELECTED.test(line);
43
57
  }
44
58
  /** May the one Enter be pressed on this screen? The warning, with option 1 selected. */
45
59
  function safeToConfirm(text) {
46
60
  return classifyPane(text) === "dev-channel-warning" && firstOptionSelected(text);
47
61
  }
62
+ /** The Code Mode rows as captured: no numbers, nothing but whitespace (or a border) before the `❯`. */
63
+ const CODE_MODE_ROWS = {
64
+ no: /^[\s│]*❯\s*No, start without it\s*│?\s*$/,
65
+ yes: /^[\s│]*❯\s*Yes, turn on Code Mode\s*│?\s*$/,
66
+ };
67
+ /** Which Code Mode row the one `❯` is on; null with no marker, two, or one on any other line. */
68
+ function codeModeSelection(text) {
69
+ const line = selectedLine(text);
70
+ if (line === null)
71
+ return null;
72
+ if (CODE_MODE_ROWS.no.test(line))
73
+ return "no";
74
+ if (CODE_MODE_ROWS.yes.test(line))
75
+ return "yes";
76
+ return null;
77
+ }
78
+ /** May Enter be pressed for `choice`? The Code Mode screen, with the one `❯` on that row. */
79
+ function safeToAnswerCodeMode(text, choice) {
80
+ return classifyPane(text) === "code-mode-opt-in" && codeModeSelection(text) === choice;
81
+ }
82
+ /** Claude Code's picker footer, on a line of its own ("Enter to confirm · Esc to …"). */
83
+ const PICKER_FOOTER = /^[\s│]*Enter to confirm\b/;
84
+ /**
85
+ * Is Claude Code waiting at a picker of some kind? Its FOOTER ("Enter to confirm") and a `❯`
86
+ * selection row — matched on the footer, not on what the question says, so a question nobody here
87
+ * knows is still seen (and only reported: nothing is ever pressed on it). Claude Code's own prompt
88
+ * has no such footer.
89
+ */
90
+ function pickerFooterShown(text) {
91
+ const lines = text.split("\n");
92
+ return lines.some((line) => PICKER_FOOTER.test(line)) && lines.some((line) => /^[\s│]*❯/.test(line));
93
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CONFIRM_DEADLINE_MS = exports.WARNING_POLL_MS = exports.WARNING_WATCH_MS = void 0;
3
+ exports.WARNING_WATCH_MS = exports.WARNING_POLL_MS = exports.CONFIRM_DEADLINE_MS = void 0;
4
4
  exports.handleLaunch = handleLaunch;
5
5
  exports.forgetEndedLaunch = forgetEndedLaunch;
6
6
  exports.forgetEndedLaunches = forgetEndedLaunches;
@@ -15,23 +15,16 @@ const approval_settings_1 = require("./approval-settings");
15
15
  const capabilities_1 = require("./capabilities");
16
16
  const codex_launch_1 = require("./codex-launch");
17
17
  const codex_version_1 = require("../codex-version");
18
- const dev_channel_screen_1 = require("./dev-channel-screen");
18
+ const pane_watch_1 = require("./pane-watch");
19
19
  const stop_registered_1 = require("./stop-registered");
20
20
  const multiplexer_1 = require("./multiplexer");
21
21
  const started_1 = require("./started");
22
22
  const refused = (reason) => ({ status: "REFUSED", reason });
23
23
  const NO_MULTIPLEXER_HINT = "Install tmux (macOS: `brew install tmux`; Linux: `sudo apt install tmux`) so BayChat can start sessions here.";
24
- /** How long the pane is watched for the dev-channel warning after a channel launch. */
25
- exports.WARNING_WATCH_MS = 60_000;
26
- /** How often the pane is read while watching. */
27
- exports.WARNING_POLL_MS = 1_000;
28
- /**
29
- * The relay's own limit on waiting for the owner. The server decides "no answer" at 10 min
30
- * (CONFIRM_TIMEOUT); this is only for when the server cannot be reached to say so.
31
- */
32
- exports.CONFIRM_DEADLINE_MS = 10 * 60_000 + 30_000;
33
- const INTERIM_REPORT_ATTEMPTS = 3;
34
- const RETRY_PAUSE_MS = 2_000;
24
+ var pane_watch_2 = require("./pane-watch");
25
+ Object.defineProperty(exports, "CONFIRM_DEADLINE_MS", { enumerable: true, get: function () { return pane_watch_2.CONFIRM_DEADLINE_MS; } });
26
+ Object.defineProperty(exports, "WARNING_POLL_MS", { enumerable: true, get: function () { return pane_watch_2.WARNING_POLL_MS; } });
27
+ Object.defineProperty(exports, "WARNING_WATCH_MS", { enumerable: true, get: function () { return pane_watch_2.WARNING_WATCH_MS; } });
35
28
  async function handleLaunch(launch, deps, io) {
36
29
  if (!launch.commandAuthorized)
37
30
  return refused("NOT_ALLOWED");
@@ -59,7 +52,8 @@ async function handleLaunch(launch, deps, io) {
59
52
  // Monitor sessions have no channel flag, so there is no warning to wait for; neither has Codex.
60
53
  if (launch.listen === "monitor" || launch.runtime !== "claude")
61
54
  return { status: "STARTED" };
62
- return watchForDevChannelWarning(launch, started.entry, deps, io);
55
+ const entry = started.entry;
56
+ return (0, pane_watch_1.watchLaunchPane)({ launch, entry, deps, io, stop: (report, why) => stopOwn(launch, entry, deps, report, why) });
63
57
  }
64
58
  /**
65
59
  * An AGENT's start (Phase 4, docs/features/AUTOMATIONS_AND_PACKAGES.md §4.3–4.4), before anything
@@ -317,109 +311,6 @@ function spawnRecorded(launch, deps, command, spot, multiplexer, started, record
317
311
  deps.log(`launch ${launch.id}: started "${launch.sessionName}" (${launch.runtime}) in "${spot.label}" (${launch.listen}) for ${launch.requestedBy.name}. ${note}.`);
318
312
  return { entry };
319
313
  }
320
- /**
321
- * After a channel launch: Claude Code stops at "WARNING: Loading development channels … Enter to
322
- * confirm" on every start with the flag, and a detached pane would wait there forever. Watch the
323
- * pane; when the warning shows, ask the computer's owner (NEEDS_CONFIRM) and press ONE Enter only
324
- * after their Allow, only on that same screen with option 1 selected. Nothing is ever pressed
325
- * otherwise. The folder-trust screen is never answered.
326
- */
327
- async function watchForDevChannelWarning(launch, entry, deps, io) {
328
- const until = deps.now().getTime() + exports.WARNING_WATCH_MS;
329
- for (;;) {
330
- const text = deps.capture(entry.multiplexer, entry.name);
331
- const screen = text === null ? "other" : (0, dev_channel_screen_1.classifyPane)(text);
332
- if (screen === "folder-trust")
333
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "FOLDER_NOT_TRUSTED" }, "Claude Code asks whether to trust the folder");
334
- if (screen === "dev-channel-warning")
335
- return askOwner(launch, entry, deps, io);
336
- // Never seen (it may already be past it), or the relay is shutting down: the pane runs on.
337
- if (deps.now().getTime() >= until || io?.signal?.aborted)
338
- return { status: "STARTED" };
339
- await deps.sleep(exports.WARNING_POLL_MS);
340
- }
341
- }
342
- async function askOwner(launch, entry, deps, io) {
343
- if (!io || !(await sendInterim(io, deps))) {
344
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "CONFIRM_UNREACHABLE" }, "could not ask the owner");
345
- }
346
- deps.log(`launch ${launch.id}: Claude Code shows the development-channels warning; waiting for the computer owner's OK in BayChat.`);
347
- const answer = await awaitDecision(io, deps);
348
- if (io.signal?.aborted) {
349
- // Shutting down: the report would not be delivered anyway. The server times the question out
350
- // and queues a STOP, which the relay carries out when it is back.
351
- return refused("CONFIRM_TIMEOUT");
352
- }
353
- if (!answer)
354
- return stopOwn(launch, entry, deps, refused("CONFIRM_TIMEOUT"), "no answer from the owner");
355
- if (answer.decision === "deny") {
356
- // Press nothing. The server queued a STOP for this session; the next poll carries it out.
357
- const reason = answer.reason === "CONFIRM_TIMEOUT" ? "CONFIRM_TIMEOUT" : "OWNER_DECLINED";
358
- deps.log(`launch ${launch.id}: the owner did not allow it (${reason}); nothing was pressed.`);
359
- return refused(reason);
360
- }
361
- return confirmWarning(launch, entry, deps);
362
- }
363
- /** The owner allowed it: press ONE Enter, only on the same warning with option 1 selected. */
364
- function confirmWarning(launch, entry, deps) {
365
- // Only ever into the pane this launch created and the record lists.
366
- const recorded = (0, started_1.loadStarted)((0, started_1.startedRecordPath)(deps.launchDir)).find((s) => s.name === entry.name && s.launchId === launch.id && s.multiplexer === entry.multiplexer);
367
- if (!recorded) {
368
- deps.log(`launch ${launch.id}: its pane is no longer in the launcher's record; nothing was pressed.`);
369
- return { status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" };
370
- }
371
- const text = deps.capture(entry.multiplexer, entry.name);
372
- if (text === null)
373
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" }, "its pane cannot be read");
374
- const screen = (0, dev_channel_screen_1.classifyPane)(text);
375
- if (screen === "folder-trust")
376
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "FOLDER_NOT_TRUSTED" }, "Claude Code asks whether to trust the folder");
377
- if (screen === "other") {
378
- // Someone at the computer answered it already (or Claude moved on): nothing to press.
379
- deps.log(`launch ${launch.id}: the warning is gone; nothing was pressed.`);
380
- return { status: "STARTED" };
381
- }
382
- if (!(0, dev_channel_screen_1.safeToConfirm)(text))
383
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" }, "the warning changed (option 1 is not selected)");
384
- if (!deps.pressEnter(entry.multiplexer, entry.name))
385
- return stopOwn(launch, entry, deps, { status: "FAILED", reason: "LAUNCHER_ERROR" }, "Enter could not be sent");
386
- deps.log(`launch ${launch.id}: the owner allowed it; pressed Enter once on "I am using this for local development".`);
387
- return { status: "STARTED" };
388
- }
389
- async function sendInterim(io, deps) {
390
- for (let attempt = 1; attempt <= INTERIM_REPORT_ATTEMPTS; attempt++) {
391
- try {
392
- await io.report({ status: "NEEDS_CONFIRM", reason: "DEV_CHANNEL_WARNING" });
393
- return true;
394
- }
395
- catch (err) {
396
- deps.log(`launcher: NEEDS_CONFIRM report failed (${err instanceof Error ? err.message : String(err)})`);
397
- if (attempt < INTERIM_REPORT_ATTEMPTS)
398
- await deps.sleep(RETRY_PAUSE_MS);
399
- }
400
- }
401
- return false;
402
- }
403
- /** Long-poll until the owner decides, or the relay's own deadline passes (null). */
404
- async function awaitDecision(io, deps) {
405
- const until = deps.now().getTime() + exports.CONFIRM_DEADLINE_MS;
406
- while (deps.now().getTime() < until && !io.signal?.aborted) {
407
- const asked = deps.now().getTime();
408
- try {
409
- const answer = await io.pollDecision();
410
- if (answer.decision === "allow" || answer.decision === "deny")
411
- return answer;
412
- // A poll that came back at once must not turn into a busy loop.
413
- if (deps.now().getTime() - asked < exports.WARNING_POLL_MS)
414
- await deps.sleep(exports.WARNING_POLL_MS);
415
- }
416
- catch (err) {
417
- deps.log(`launcher: confirm poll failed (${err instanceof Error ? err.message : String(err)})`);
418
- await deps.sleep(RETRY_PAUSE_MS);
419
- }
420
- }
421
- return null;
422
- }
423
314
  /** Kill this launch's own pane (never pressing anything), forget it, and return `report`. */
424
315
  function stopOwn(launch, entry, deps, report, why) {
425
316
  deps.log(`launch ${launch.id}: ${why}; nothing was pressed, stopping "${entry.name}".`);
@@ -270,6 +270,7 @@ function realLoopDeps(auth, log, signal, { forgetSession, registeredSession, reg
270
270
  run: multiplexer_1.realRun,
271
271
  capture: (multiplexer, name) => (0, multiplexer_1.capturePane)(multiplexer, name, multiplexer_1.realRun, { dir: (0, approval_settings_1.launchDir)() }),
272
272
  pressEnter: (multiplexer, name) => (0, multiplexer_1.pressEnter)(multiplexer, name, multiplexer_1.realRun),
273
+ pressDown: (multiplexer, name) => (0, multiplexer_1.pressDown)(multiplexer, name, multiplexer_1.realRun),
273
274
  sleep: (ms) => sleepUnlessAborted(ms, signal),
274
275
  cli: (0, approval_settings_1.currentCliLaunch)(),
275
276
  launchDir: (0, approval_settings_1.launchDir)(),
@@ -45,6 +45,7 @@ exports.killLaunchSession = killLaunchSession;
45
45
  exports.tmuxPaneTarget = tmuxPaneTarget;
46
46
  exports.capturePane = capturePane;
47
47
  exports.pressEnter = pressEnter;
48
+ exports.pressDown = pressDown;
48
49
  const child_process_1 = require("child_process");
49
50
  const fs = __importStar(require("fs"));
50
51
  const path = __importStar(require("path"));
@@ -269,6 +270,16 @@ function pressEnter(multiplexer, sessionName, run) {
269
270
  : run("screen", ["-S", (0, argv_1.multiplexerSessionName)(sessionName), "-X", "stuff", "\r"]);
270
271
  return !res.error && res.status === 0;
271
272
  }
273
+ /**
274
+ * Send ONE Down arrow to the launcher's pane — the one other key the launcher has, for Claude Code's
275
+ * Code Mode "Yes" (pane-watch.ts), checked on screen before and after. True when accepted.
276
+ */
277
+ function pressDown(multiplexer, sessionName, run) {
278
+ const res = multiplexer === "tmux"
279
+ ? run("tmux", ["send-keys", "-t", tmuxPaneTarget(sessionName), "Down"])
280
+ : run("screen", ["-S", (0, argv_1.multiplexerSessionName)(sessionName), "-X", "stuff", "\u001b[B"]);
281
+ return !res.error && res.status === 0;
282
+ }
272
283
  function withoutPrefix(names) {
273
284
  return names.filter((n) => n.startsWith(argv_1.LAUNCH_SESSION_PREFIX) && n.length > argv_1.LAUNCH_SESSION_PREFIX.length).map((n) => n.slice(argv_1.LAUNCH_SESSION_PREFIX.length));
274
285
  }
@@ -0,0 +1,283 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KEY_SETTLE_MS = exports.CODE_MODE_ARM_MS = exports.CONFIRM_DEADLINE_MS = exports.WARNING_POLL_MS = exports.WARNING_WATCH_MS = void 0;
4
+ exports.watchLaunchPane = watchLaunchPane;
5
+ const dev_channel_screen_1 = require("./dev-channel-screen");
6
+ const started_1 = require("./started");
7
+ /**
8
+ * After a channel launch: watch the detached pane for the questions Claude Code asks before it
9
+ * starts, and put each one to the computer's owner (NEEDS_CONFIRM with its reason). Out of
10
+ * handle.ts (over its size limit) on 20 Nov, when the second question arrived.
11
+ *
12
+ * ── ONE QUESTION AT A TIME, EACH WITH ITS OWN ANSWER ─────────────────────────────────
13
+ * • DEV_CHANNEL_WARNING — "Loading development channels … Enter to confirm", on every start with
14
+ * the channel flag. Allow: ONE Enter, only on that same screen with option 1 selected. Deny:
15
+ * nothing; the server queued the STOP that closes the pane. The watch ends with it, as before.
16
+ * • CODE_MODE_OPT_IN — "Code Mode (experimental)" (Claude Code 2.1.3xx with
17
+ * CLAUDE_CODE_EXPERIMENTAL_CODE_MODE=1), which comes FIRST. Both answers START the session:
18
+ * Allow = one Down, read again, then ONE Enter only with the `❯` on "Yes, turn on Code Mode";
19
+ * Deny = ONE Enter only with the `❯` on "No, start without it". Never before the screen's arm
20
+ * delay. After Yes, Claude Code restarts itself in the same pane, so the pane is watched again
21
+ * (the channel warning may follow, as its own question).
22
+ * • UNKNOWN_SCREEN — at the end of the watch, Claude Code's picker footer on a screen nobody
23
+ * here recognises: reported as waiting, Stop only. Nothing is ever pressed on it.
24
+ * A NEW question is raised only after the last one was answered; the server resets the decision
25
+ * for it, so an earlier Allow never answers a later question. Each question is asked once per
26
+ * launch. A screen that changed under an answer is never pressed on: the pane is stopped,
27
+ * CONFIRM_SCREEN_CHANGED. The folder-trust screen is never answered.
28
+ */
29
+ /** How long the pane is watched for a question after a channel launch, and again after each answer. */
30
+ exports.WARNING_WATCH_MS = 60_000;
31
+ /** How often the pane is read while watching. */
32
+ exports.WARNING_POLL_MS = 1_000;
33
+ /**
34
+ * The relay's own limit on waiting for the owner. The server decides "no answer" at 10 min
35
+ * (CONFIRM_TIMEOUT); this is only for when the server cannot be reached to say so.
36
+ */
37
+ exports.CONFIRM_DEADLINE_MS = 10 * 60_000 + 30_000;
38
+ /** Claude Code's Code Mode buttons take input only after a short arm delay (`armAfterMs`). */
39
+ exports.CODE_MODE_ARM_MS = 1_000;
40
+ /** After the Down, how long the pane is given to redraw before it is read again. */
41
+ exports.KEY_SETTLE_MS = 300;
42
+ const INTERIM_REPORT_ATTEMPTS = 3;
43
+ const RETRY_PAUSE_MS = 2_000;
44
+ const refused = (reason) => ({ status: "REFUSED", reason });
45
+ const changed = (w, why) => w.stop({ status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" }, why);
46
+ const untrusted = (w) => w.stop({ status: "FAILED", reason: "FOLDER_NOT_TRUSTED" }, "Claude Code asks whether to trust the folder");
47
+ async function watchLaunchPane(w) {
48
+ const { deps, entry, io } = w;
49
+ const now = () => deps.now().getTime();
50
+ let until = now() + exports.WARNING_WATCH_MS;
51
+ let codeModeSince = null;
52
+ let codeModeAnswered = false;
53
+ /** A question this watch may still ask: the warning, Code Mode until it is answered, and the trust screen (refused). */
54
+ const askable = (text) => {
55
+ const screen = (0, dev_channel_screen_1.classifyPane)(text);
56
+ return screen === "dev-channel-warning" || screen === "folder-trust" || (screen === "code-mode-opt-in" && !codeModeAnswered);
57
+ };
58
+ for (;;) {
59
+ const text = deps.capture(entry.multiplexer, entry.name);
60
+ const screen = text === null ? "other" : (0, dev_channel_screen_1.classifyPane)(text);
61
+ if (screen === "folder-trust")
62
+ return untrusted(w);
63
+ if (screen === "dev-channel-warning")
64
+ return answerChannelWarning(w);
65
+ if (screen === "code-mode-opt-in" && !codeModeAnswered) {
66
+ codeModeSince ??= now();
67
+ const outcome = await answerCodeMode(w, codeModeSince);
68
+ if (outcome !== "answered")
69
+ return outcome;
70
+ codeModeAnswered = true;
71
+ // Claude Code restarts itself in this pane after Yes; the channel warning may come next.
72
+ until = now() + exports.WARNING_WATCH_MS;
73
+ continue;
74
+ }
75
+ // The relay is shutting down: the pane runs on (the server's STOP finds it later, if asked).
76
+ if (io?.signal?.aborted)
77
+ return { status: "STARTED" };
78
+ if (now() >= until) {
79
+ // Never seen a question (it may already be past them) — unless Claude Code waits at a picker.
80
+ if (text === null || !(0, dev_channel_screen_1.pickerFooterShown)(text))
81
+ return { status: "STARTED" };
82
+ const outcome = await reportUnknownScreen(w, (t) => (0, dev_channel_screen_1.pickerFooterShown)(t) && !askable(t));
83
+ if (outcome !== "moved-on")
84
+ return outcome;
85
+ until = now() + exports.WARNING_WATCH_MS;
86
+ continue;
87
+ }
88
+ await deps.sleep(exports.WARNING_POLL_MS);
89
+ }
90
+ }
91
+ /** The development-channels warning, as before: ask, and on Allow press ONE Enter on option 1. */
92
+ async function answerChannelWarning(w) {
93
+ const answer = await askOwner(w, "DEV_CHANNEL_WARNING");
94
+ if ("status" in answer)
95
+ return answer;
96
+ if (answer.decision === "deny") {
97
+ // Press nothing. The server queued a STOP for this session; the next poll carries it out.
98
+ const reason = answer.reason === "CONFIRM_TIMEOUT" ? "CONFIRM_TIMEOUT" : "OWNER_DECLINED";
99
+ w.deps.log(`launch ${w.launch.id}: the owner did not allow it (${reason}); nothing was pressed.`);
100
+ return refused(reason);
101
+ }
102
+ if (!ownPane(w))
103
+ return { status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" };
104
+ const text = w.deps.capture(w.entry.multiplexer, w.entry.name);
105
+ if (text === null)
106
+ return changed(w, "its pane cannot be read");
107
+ const screen = (0, dev_channel_screen_1.classifyPane)(text);
108
+ if (screen === "folder-trust")
109
+ return untrusted(w);
110
+ if (screen !== "dev-channel-warning") {
111
+ // Someone at the computer answered it already (or Claude moved on): nothing to press.
112
+ w.deps.log(`launch ${w.launch.id}: the warning is gone; nothing was pressed.`);
113
+ return { status: "STARTED" };
114
+ }
115
+ if (!(0, dev_channel_screen_1.safeToConfirm)(text))
116
+ return changed(w, "the warning changed (option 1 is not selected)");
117
+ if (!w.deps.pressEnter(w.entry.multiplexer, w.entry.name))
118
+ return w.stop({ status: "FAILED", reason: "LAUNCHER_ERROR" }, "Enter could not be sent");
119
+ w.deps.log(`launch ${w.launch.id}: the owner allowed it; pressed Enter once on "I am using this for local development".`);
120
+ return { status: "STARTED" };
121
+ }
122
+ /**
123
+ * The Code Mode opt-in. "answered" when the watch goes on (a key pressed as the owner chose, or
124
+ * the screen already gone); else the launch's report.
125
+ */
126
+ async function answerCodeMode(w, since) {
127
+ const answer = await askOwner(w, "CODE_MODE_OPT_IN");
128
+ if ("status" in answer)
129
+ return answer;
130
+ if (answer.decision === "deny" && answer.reason !== "OWNER_DECLINED") {
131
+ // Not the owner's "Start without it": nobody answered (the server queued a STOP), or the launch
132
+ // ended some other way. Press nothing.
133
+ if (answer.reason === "CONFIRM_TIMEOUT") {
134
+ w.deps.log(`launch ${w.launch.id}: nobody answered the Code Mode question; nothing was pressed.`);
135
+ return refused("CONFIRM_TIMEOUT");
136
+ }
137
+ return w.stop(refused(answer.reason ?? "NOT_AWAITING_CONFIRM"), "the Code Mode question was not answered by its owner");
138
+ }
139
+ const wait = since + exports.CODE_MODE_ARM_MS - w.deps.now().getTime();
140
+ if (wait > 0)
141
+ await w.deps.sleep(wait);
142
+ return pressCodeMode(w, answer.decision === "allow" ? "yes" : "no");
143
+ }
144
+ async function pressCodeMode(w, choice) {
145
+ const { deps, entry, launch } = w;
146
+ if (!ownPane(w))
147
+ return { status: "FAILED", reason: "CONFIRM_SCREEN_CHANGED" };
148
+ const text = deps.capture(entry.multiplexer, entry.name);
149
+ if (text === null)
150
+ return changed(w, "its pane cannot be read");
151
+ const screen = (0, dev_channel_screen_1.classifyPane)(text);
152
+ if (screen === "folder-trust")
153
+ return untrusted(w);
154
+ if (screen !== "code-mode-opt-in") {
155
+ deps.log(`launch ${launch.id}: the Code Mode question is gone; nothing was pressed.`);
156
+ return "answered";
157
+ }
158
+ // The cursor opens on "No"; anything else is a screen this was not asked about.
159
+ if ((0, dev_channel_screen_1.codeModeSelection)(text) !== "no")
160
+ return changed(w, 'the Code Mode question changed (the cursor is not on "No, start without it")');
161
+ if (choice === "yes") {
162
+ if (!deps.pressDown(entry.multiplexer, entry.name))
163
+ return w.stop({ status: "FAILED", reason: "LAUNCHER_ERROR" }, "Down could not be sent");
164
+ await deps.sleep(exports.KEY_SETTLE_MS);
165
+ const moved = deps.capture(entry.multiplexer, entry.name);
166
+ if (moved === null || !(0, dev_channel_screen_1.safeToAnswerCodeMode)(moved, "yes"))
167
+ return changed(w, 'after Down the cursor is not on "Yes, turn on Code Mode"');
168
+ }
169
+ if (!deps.pressEnter(entry.multiplexer, entry.name))
170
+ return w.stop({ status: "FAILED", reason: "LAUNCHER_ERROR" }, "Enter could not be sent");
171
+ deps.log(choice === "yes"
172
+ ? `launch ${launch.id}: the owner turned Code Mode on; pressed Down, then Enter once on "Yes, turn on Code Mode". Claude Code restarts itself in the same pane.`
173
+ : `launch ${launch.id}: the owner started it without Code Mode; pressed Enter once on "No, start without it".`);
174
+ return "answered";
175
+ }
176
+ /**
177
+ * Claude Code waits at a picker nobody here knows: say so (Stop only) and wait for the owner, reading
178
+ * the pane between polls. "moved-on" when the picker went away or became a question this watch
179
+ * can ask; nothing is ever pressed here.
180
+ */
181
+ async function reportUnknownScreen(w, stillThere) {
182
+ const { deps, entry, launch, io } = w;
183
+ if (!io || !(await sendInterim(io, deps, "UNKNOWN_SCREEN"))) {
184
+ // As before this question existed: the pane runs on (an older server does not know it).
185
+ deps.log(`launch ${launch.id}: Claude Code waits at a question BayChat does not know, and it could not be reported; the pane is left running.`);
186
+ return { status: "STARTED" };
187
+ }
188
+ deps.log(`launch ${launch.id}: Claude Code waits at a question BayChat does not know; asked the computer owner (Stop only). Nothing is pressed.`);
189
+ const answer = await awaitDecision(io, deps, () => {
190
+ const text = deps.capture(entry.multiplexer, entry.name);
191
+ return text !== null && stillThere(text);
192
+ });
193
+ if (io.signal?.aborted)
194
+ return refused("CONFIRM_TIMEOUT");
195
+ if (answer === "moved-on") {
196
+ const text = deps.capture(entry.multiplexer, entry.name);
197
+ if (text === null || !(0, dev_channel_screen_1.pickerFooterShown)(text)) {
198
+ // Answered at the computer: running, as far as anyone here can tell. Said now, best-effort.
199
+ try {
200
+ await io.report({ status: "STARTED" });
201
+ }
202
+ catch (err) {
203
+ deps.log(`launcher: STARTED report failed (${err instanceof Error ? err.message : String(err)})`);
204
+ }
205
+ }
206
+ deps.log(`launch ${launch.id}: the unknown question went away; watching on.`);
207
+ return "moved-on";
208
+ }
209
+ if (!answer)
210
+ return w.stop(refused("CONFIRM_TIMEOUT"), "no answer from the owner");
211
+ if (answer.decision === "deny") {
212
+ const reason = answer.reason === "CONFIRM_TIMEOUT" ? "CONFIRM_TIMEOUT" : "OWNER_DECLINED";
213
+ deps.log(`launch ${launch.id}: the owner stopped it (${reason}); nothing was pressed.`);
214
+ return refused(reason);
215
+ }
216
+ // Never offered for this question; if an allow arrives anyway, there is still nothing to press.
217
+ deps.log(`launch ${launch.id}: an allow came for a question BayChat does not know; nothing was pressed.`);
218
+ return { status: "STARTED" };
219
+ }
220
+ /** Raise `question` with the server, then wait for the owner's answer; a report when it ends here. */
221
+ async function askOwner(w, question) {
222
+ const { deps, io, launch } = w;
223
+ if (!io || !(await sendInterim(io, deps, question))) {
224
+ return w.stop({ status: "FAILED", reason: "CONFIRM_UNREACHABLE" }, "could not ask the owner");
225
+ }
226
+ deps.log(`launch ${launch.id}: Claude Code shows ${question === "CODE_MODE_OPT_IN" ? "its Code Mode question" : "the development-channels warning"}; waiting for the computer owner's answer in BayChat.`);
227
+ const answer = await awaitDecision(io, deps);
228
+ if (io.signal?.aborted) {
229
+ // Shutting down: the report would not be delivered anyway. The server times the question out
230
+ // and queues a STOP, which the relay carries out when it is back.
231
+ return refused("CONFIRM_TIMEOUT");
232
+ }
233
+ if (!answer || answer === "moved-on")
234
+ return w.stop(refused("CONFIRM_TIMEOUT"), "no answer from the owner");
235
+ return answer;
236
+ }
237
+ /** Only ever into the pane this launch created and the record lists. */
238
+ function ownPane(w) {
239
+ const { deps, entry, launch } = w;
240
+ const recorded = (0, started_1.loadStarted)((0, started_1.startedRecordPath)(deps.launchDir)).find((s) => s.name === entry.name && s.launchId === launch.id && s.multiplexer === entry.multiplexer);
241
+ if (!recorded)
242
+ deps.log(`launch ${launch.id}: its pane is no longer in the launcher's record; nothing was pressed.`);
243
+ return recorded !== undefined;
244
+ }
245
+ async function sendInterim(io, deps, question) {
246
+ for (let attempt = 1; attempt <= INTERIM_REPORT_ATTEMPTS; attempt++) {
247
+ try {
248
+ await io.report({ status: "NEEDS_CONFIRM", reason: question });
249
+ return true;
250
+ }
251
+ catch (err) {
252
+ deps.log(`launcher: NEEDS_CONFIRM report failed (${err instanceof Error ? err.message : String(err)})`);
253
+ if (attempt < INTERIM_REPORT_ATTEMPTS)
254
+ await deps.sleep(RETRY_PAUSE_MS);
255
+ }
256
+ }
257
+ return false;
258
+ }
259
+ /**
260
+ * Long-poll until the owner decides, or the relay's own deadline passes (null). With `stillWaiting`,
261
+ * it is asked after every poll without a decision; false ends the wait ("moved-on").
262
+ */
263
+ async function awaitDecision(io, deps, stillWaiting) {
264
+ const until = deps.now().getTime() + exports.CONFIRM_DEADLINE_MS;
265
+ while (deps.now().getTime() < until && !io.signal?.aborted) {
266
+ const asked = deps.now().getTime();
267
+ try {
268
+ const answer = await io.pollDecision();
269
+ if (answer.decision === "allow" || answer.decision === "deny")
270
+ return answer;
271
+ if (stillWaiting && !stillWaiting())
272
+ return "moved-on";
273
+ // A poll that came back at once must not turn into a busy loop.
274
+ if (deps.now().getTime() - asked < exports.WARNING_POLL_MS)
275
+ await deps.sleep(exports.WARNING_POLL_MS);
276
+ }
277
+ catch (err) {
278
+ deps.log(`launcher: confirm poll failed (${err instanceof Error ? err.message : String(err)})`);
279
+ await deps.sleep(RETRY_PAUSE_MS);
280
+ }
281
+ }
282
+ return null;
283
+ }