angelia-gateway 0.4.2 → 0.4.3

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.
@@ -9,8 +9,9 @@ export class PermissionBook {
9
9
  this.timeoutMs = timeoutMs;
10
10
  this.onTimeout = onTimeout;
11
11
  }
12
- add(id) {
13
- this.pending.set(id, setTimeout(() => this.onTimeout(id), this.timeoutMs));
12
+ /** `ms`: a shorter wait for this one request (a tmux hook's own time left, tui.ts). */
13
+ add(id, ms = this.timeoutMs) {
14
+ this.pending.set(id, setTimeout(() => this.onTimeout(id), Math.min(ms, this.timeoutMs)));
14
15
  }
15
16
  /** Clears and returns the full id for a prefix, or null when it matches none or several. */
16
17
  take(prefix) {
@@ -16,6 +16,8 @@ export function tmuxSocketPath(env = process.env) {
16
16
  catch { /* not there: tmux makes it */ }
17
17
  return join(base, `tmux-${process.getuid?.() ?? 0}`, TMUX_SOCKET);
18
18
  }
19
+ /** The longest one tmux client call may take (tmux(), `timeoutMs`). */
20
+ export const TMUX_TIMEOUT_MS = 30_000;
19
21
  /** The call run in a transient scope of its own: under systemd the service's KillMode kills every
20
22
  * process in its cgroup, and the tmux server must outlive the daemon. */
21
23
  export function scopedArgv(bin, args) {
@@ -27,9 +29,12 @@ export function scopedArgv(bin, args) {
27
29
  export function tmux(args, opts = {}) {
28
30
  const full = ['-L', opts.socket ?? TMUX_SOCKET, ...args];
29
31
  const run = (bin, argv) => new Promise((resolve) => {
30
- execFile(bin, argv, { env: opts.env, maxBuffer: 8 << 20 }, (err, stdout, stderr) => {
32
+ // A tmux server that does not answer must not hold a caller for ever (a restart's adoption runs before
33
+ // the daemon listens): every call is a short one, so past TMUX_TIMEOUT_MS the client is killed and the
34
+ // call fails.
35
+ execFile(bin, argv, { env: opts.env, maxBuffer: 8 << 20, timeout: opts.timeoutMs ?? TMUX_TIMEOUT_MS, killSignal: 'SIGKILL' }, (err, stdout, stderr) => {
31
36
  const e = err;
32
- resolve({ code: e ? Number(e.code ?? 1) : 0, out: stdout ?? '', err: stderr ?? '', missing: e?.code === 'ENOENT' });
37
+ resolve({ code: e ? Number(e.code ?? 1) : 0, out: stdout ?? '', err: stderr ?? '', missing: e?.code === 'ENOENT', ...(e?.killed ? { timedOut: true } : {}) });
33
38
  });
34
39
  });
35
40
  const plain = () => run(opts.bin ?? 'tmux', full);
@@ -122,6 +127,44 @@ export function paneIdle(pane) {
122
127
  const box = inputBoxStrict(pane);
123
128
  return box !== null && PROMPT_MARKERS.some((m) => `\n${box}`.includes(m));
124
129
  }
130
+ /** Safe to paste into a running turn: a turn runs, its input box is on screen with the prompt and
131
+ * holds nothing, and no dialog is open. Claude Code then queues what is submitted and gives it to the
132
+ * model at the next tool boundary of the same turn, or as the next turn when none comes (measured on
133
+ * claude 2.1.332, 2026-11-25: fixtures pane-busy-box-332 and pane-busy-queued-332). Never over text
134
+ * someone half typed in the box (pane-busy-halftyped-332): Enter would send theirs with ours. The
135
+ * hint "Press up to edit queued messages" is no text. A permission dialog replaces the box
136
+ * (pane-permission-332), so a capture with one is never safe; the caller still checks the
137
+ * transcript and the relay, since a dialog opened a moment ago may not be drawn yet. */
138
+ /** What sits in the input box: its lines without the prompt marker, or '' for an empty box or a
139
+ * screen with none. Claude Code's own hint in an empty box (`Try "write a test for <filepath>"`,
140
+ * `Press up to edit queued messages`) is not text that Enter would send, so it reads as empty. */
141
+ export function boxText(pane) {
142
+ const box = inputBoxStrict(pane);
143
+ if (box === null)
144
+ return '';
145
+ const t = box.split('\n').map((l) => l.replace(/^\s*(❯|>)\s?/, '').trim()).join('\n').trim();
146
+ if (t.startsWith('Try "') || t.startsWith('Press up to edit queued messages'))
147
+ return '';
148
+ return t;
149
+ }
150
+ export function paneMidTurn(pane, text) {
151
+ if (!pane || !paneBusy(pane) || permissionDialog(pane))
152
+ return false;
153
+ const box = inputBoxStrict(pane);
154
+ if (box === null || !PROMPT_MARKERS.some((m) => `\n${box}`.includes(m)))
155
+ return false;
156
+ // With `text`: the same screen, with that text in the box and nothing else (right before its Enter).
157
+ return text === undefined ? boxText(pane) === '' : boxHoldsOnly(pane, text);
158
+ }
159
+ /** The box holds `text` and nothing else: its collapsed chip (one paste), or the text itself, as the
160
+ * box wraps it. Not text someone typed before or after it. */
161
+ export function boxHoldsOnly(pane, text) {
162
+ const shown = boxText(pane);
163
+ if (/^\[Pasted text #\d+(?: \+\d+ lines?)?\]$/.test(shown))
164
+ return true;
165
+ const flat = (s) => s.replace(/\s+/g, '');
166
+ return !!flat(text) && flat(shown) === flat(text);
167
+ }
125
168
  const clean = (l) => l.replace(/[│╭╮╰╯┃]/g, ' ').replace(/\s+/g, ' ').trim();
126
169
  /** The open permission dialog as a chat line, or null when none is open. The dialog is drawn under
127
170
  * a full-width rule: the title, why the command runs, then the command between dashed rules:
@@ -13,8 +13,8 @@ import { permissionPreview } from './brain.js';
13
13
  * Otherwise it steps aside at once, and Claude opens its own dialog in the pane and the Claude app.
14
14
  */
15
15
  /** A heartbeat older than this means nobody reads the chat. The hook has the same number. Only a
16
- * daemon that died leaves one to go stale (every other end of a turn removes it at once), so this is
17
- * how long a hook waits after a crash, and how long a stalled daemon may stall. */
16
+ * daemon that died or stopped for a restart leaves one to go stale (every other end of a turn removes
17
+ * it at once); the hook then waits a minute more for the next daemon's (its RESTART_WAIT_MS). */
18
18
  export const RELAY_STALE_MS = 30_000;
19
19
  /** How often the heartbeat is written while a turn is read from the chat: on a timer of its own, so a
20
20
  * reader waiting on a slow chat send does not look like a daemon that stopped. */
@@ -22,6 +22,9 @@ export const RELAY_BEAT_MS = 1000;
22
22
  /** How long a dialog must stay on the pane, with no request from the hook, before the chat hears of it. */
23
23
  export const SCREEN_ONLY_MS = 1500;
24
24
  const REQUEST = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.request\.json$/;
25
+ /** How long the hook waits for an answer once it wrote its request (scripts/tui-permission-hook.mjs,
26
+ * GIVE_UP_MS). A request offered again after a restart has only what is left of it. */
27
+ export const HOOK_GIVE_UP_MS = 11 * 60_000;
25
28
  export class PermissionRelay {
26
29
  dir;
27
30
  /** Given to the chat and not answered from it. */
@@ -70,7 +73,17 @@ export class PermissionRelay {
70
73
  this.open.add(id);
71
74
  out.push({ id, tool: typeof row.tool_name === 'string' && row.tool_name ? row.tool_name : '?', input: row.tool_input, at: Number(row.at) || 0 });
72
75
  }
73
- return out.sort((a, b) => a.at - b.at).map(({ id, tool, input }) => ({ id, tool, input }));
76
+ return out.sort((a, b) => a.at - b.at).map(({ id, tool, input, at }) => ({ id, tool, input, ...(at ? { at } : {}) }));
77
+ }
78
+ /** A hook waits on a request now, taken or not: Claude Code draws its own dialog while the hook
79
+ * runs (measured on claude 2.1.332), so Enter must not be pressed for a message into the turn. */
80
+ waiting() {
81
+ try {
82
+ return readdirSync(this.dir).some((n) => REQUEST.test(n));
83
+ }
84
+ catch {
85
+ return false;
86
+ }
74
87
  }
75
88
  /** Requests given to the chat whose hook no longer waits: answered in the pane or the app, or it stepped aside. */
76
89
  gone() {