angelia-gateway 0.4.2 → 0.4.4
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/brain/brain.js +3 -2
- package/dist/brain/tmux.js +51 -3
- package/dist/brain/tui-permissions.js +78 -8
- package/dist/brain/tui.js +1358 -67
- package/dist/build.json +3 -3
- package/dist/core/cut-turns.js +58 -1
- package/dist/core/deliver/text.js +11 -0
- package/dist/core/orchestrator.js +693 -30
- package/dist/core/screen-asks.js +158 -0
- package/dist/core/session/queue.js +64 -6
- package/dist/daemon/api/routes.js +7 -0
- package/dist/daemon/api/server.js +10 -0
- package/dist/daemon/daemon.js +58 -12
- package/dist/daemon/deferred-restart.js +77 -0
- package/dist/daemon/restart.js +31 -0
- package/dist/instance/config/schema.js +5 -0
- package/dist/instance/guide.js +4 -2
- package/package.json +1 -1
- package/scripts/tui-permission-hook.mjs +75 -9
- package/scripts/tui-pretool-hook.mjs +59 -0
- package/scripts/tui-stop-hook.mjs +1 -1
- package/scripts/turn-origin.mjs +139 -30
package/dist/brain/brain.js
CHANGED
|
@@ -9,8 +9,9 @@ export class PermissionBook {
|
|
|
9
9
|
this.timeoutMs = timeoutMs;
|
|
10
10
|
this.onTimeout = onTimeout;
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
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) {
|
package/dist/brain/tmux.js
CHANGED
|
@@ -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
|
-
|
|
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:
|
|
@@ -155,7 +198,12 @@ export function permissionDialog(pane) {
|
|
|
155
198
|
const body = raw.slice(top >= 0 ? top + 1 : Math.max(0, at - 6), at).map(clean).filter((l) => l && !/^[\s╌─━┄┈]+$/.test(l));
|
|
156
199
|
if (!body.length)
|
|
157
200
|
return { tool: 'tool', preview: 'a permission prompt is open' };
|
|
158
|
-
|
|
201
|
+
// A teammate's request names it after the title: "Bash command · from the restart-relay agent"
|
|
202
|
+
// (claude 2.1.332, seen in a chat's pane 2026-11-25).
|
|
203
|
+
// Only that shape: a title with a "·" of its own says nothing of who asks.
|
|
204
|
+
const asker = /^(.+?)\s*·\s*from\s+((?:the\s+)?[A-Za-z0-9_.-]{1,64}\s+agent)$/.exec(body[0]);
|
|
205
|
+
const tool = (asker ? asker[1] : body[0]).slice(0, 40);
|
|
206
|
+
return { tool, preview: (body.slice(1).join(' · ') || body[0]).slice(0, 300), ...(asker ? { from: asker[2].slice(0, 60) } : {}) };
|
|
159
207
|
}
|
|
160
208
|
/** A directory Claude Code has not been run in before opens a trust dialog before the first
|
|
161
209
|
* prompt, with "No, exit" preselected. Print mode never shows it. */
|
|
@@ -9,12 +9,16 @@ import { permissionPreview } from './brain.js';
|
|
|
9
9
|
* A dialog on the screen is text a command can shape (a line of `─` in a command reads as the dialog's
|
|
10
10
|
* top edge), and a key press answers whichever dialog is open by then; neither is used to approve.
|
|
11
11
|
*
|
|
12
|
-
* The hook relays
|
|
13
|
-
*
|
|
12
|
+
* The hook relays while a turn from the chat is being read, which `beat` says on every poll, and
|
|
13
|
+
* between the chat's turns while the daemon holds the pane (`beat` with `idle`): then the hook adds
|
|
14
|
+
* what started the turn that asks (turnOrigin, as the Stop hook reads it), and the daemon either
|
|
15
|
+
* takes the request (a turn the agent started by itself: the profile's `permissions` decide) or tells
|
|
16
|
+
* the hook to step aside (`pass`: a turn a person typed in the Claude app or the pane). With no
|
|
17
|
+
* heartbeat at all it steps aside at once, and Claude opens its own dialog in the pane and the app.
|
|
14
18
|
*/
|
|
15
19
|
/** 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
|
|
17
|
-
*
|
|
20
|
+
* daemon that died or stopped for a restart leaves one to go stale (every other end of a turn removes
|
|
21
|
+
* it at once); the hook then waits a minute more for the next daemon's (its RESTART_WAIT_MS). */
|
|
18
22
|
export const RELAY_STALE_MS = 30_000;
|
|
19
23
|
/** How often the heartbeat is written while a turn is read from the chat: on a timer of its own, so a
|
|
20
24
|
* reader waiting on a slow chat send does not look like a daemon that stopped. */
|
|
@@ -22,6 +26,9 @@ export const RELAY_BEAT_MS = 1000;
|
|
|
22
26
|
/** How long a dialog must stay on the pane, with no request from the hook, before the chat hears of it. */
|
|
23
27
|
export const SCREEN_ONLY_MS = 1500;
|
|
24
28
|
const REQUEST = /^([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\.request\.json$/;
|
|
29
|
+
/** How long the hook waits for an answer once it wrote its request (scripts/tui-permission-hook.mjs,
|
|
30
|
+
* GIVE_UP_MS). A request offered again after a restart has only what is left of it. */
|
|
31
|
+
export const HOOK_GIVE_UP_MS = 11 * 60_000;
|
|
25
32
|
export class PermissionRelay {
|
|
26
33
|
dir;
|
|
27
34
|
/** Given to the chat and not answered from it. */
|
|
@@ -37,8 +44,9 @@ export class PermissionRelay {
|
|
|
37
44
|
this.open.clear();
|
|
38
45
|
this.seen.clear();
|
|
39
46
|
}
|
|
40
|
-
/** While a turn is read from the chat, every RELAY_BEAT_MS.
|
|
41
|
-
|
|
47
|
+
/** While a turn is read from the chat, every RELAY_BEAT_MS. `idle`: between the chat's turns, while
|
|
48
|
+
* the daemon holds the pane: the hook then says what started the turn that asks. */
|
|
49
|
+
beat(now = Date.now(), idle = false) { this.put('relay.json', idle ? { at: now, idle: true } : { at: now }); }
|
|
42
50
|
/** Nobody reads the chat now: a hook waiting, or asking next, lets Claude open its own dialog. */
|
|
43
51
|
quiet() { rmSync(join(this.dir, 'relay.json'), { force: true }); }
|
|
44
52
|
/** Requests the hook wrote since the last call, oldest first. */
|
|
@@ -68,9 +76,20 @@ export class PermissionRelay {
|
|
|
68
76
|
}
|
|
69
77
|
this.seen.add(id);
|
|
70
78
|
this.open.add(id);
|
|
71
|
-
|
|
79
|
+
const origin = originOf(row.origin);
|
|
80
|
+
out.push({ id, tool: typeof row.tool_name === 'string' && row.tool_name ? row.tool_name : '?', input: row.tool_input, at: Number(row.at) || 0, ...(origin ? { origin } : {}) });
|
|
81
|
+
}
|
|
82
|
+
return out.sort((a, b) => a.at - b.at).map(({ at, ...r }) => ({ ...r, ...(at ? { at } : {}) }));
|
|
83
|
+
}
|
|
84
|
+
/** A hook waits on a request now, taken or not: Claude Code draws its own dialog while the hook
|
|
85
|
+
* runs (measured on claude 2.1.332), so Enter must not be pressed for a message into the turn. */
|
|
86
|
+
waiting() {
|
|
87
|
+
try {
|
|
88
|
+
return readdirSync(this.dir).some((n) => REQUEST.test(n));
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return false;
|
|
72
92
|
}
|
|
73
|
-
return out.sort((a, b) => a.at - b.at).map(({ id, tool, input }) => ({ id, tool, input }));
|
|
74
93
|
}
|
|
75
94
|
/** Requests given to the chat whose hook no longer waits: answered in the pane or the app, or it stepped aside. */
|
|
76
95
|
gone() {
|
|
@@ -94,6 +113,17 @@ export class PermissionRelay {
|
|
|
94
113
|
rmSync(join(this.dir, `${id}.answer.json`), { force: true });
|
|
95
114
|
return false;
|
|
96
115
|
}
|
|
116
|
+
/** Not the chat's to answer (a turn a person typed, between the chat's turns): the hook steps aside
|
|
117
|
+
* and Claude asks in its own dialog, as with no daemon. False when no hook waits for it any more. */
|
|
118
|
+
pass(id) {
|
|
119
|
+
if (!this.open.delete(id))
|
|
120
|
+
return false;
|
|
121
|
+
const request = join(this.dir, `${id}.request.json`);
|
|
122
|
+
if (!existsSync(request))
|
|
123
|
+
return false;
|
|
124
|
+
this.put(`${id}.answer.json`, { pass: true });
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
97
127
|
put(name, value) {
|
|
98
128
|
mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
99
129
|
const tmp = join(this.dir, `${name}.tmp`);
|
|
@@ -101,6 +131,46 @@ export class PermissionRelay {
|
|
|
101
131
|
renameSync(tmp, join(this.dir, name));
|
|
102
132
|
}
|
|
103
133
|
}
|
|
134
|
+
/** The hook's `origin` field, kept only in its own shape: plain words, keys, a time, a count. */
|
|
135
|
+
function originOf(v) {
|
|
136
|
+
if (!v || typeof v !== 'object')
|
|
137
|
+
return undefined;
|
|
138
|
+
const o = v;
|
|
139
|
+
const keys = Array.isArray(o.prompt_keys) && o.prompt_keys.length <= 20 && o.prompt_keys.every((k) => typeof k === 'string' && /^[0-9a-f]{1,128}$/.test(k)) ? o.prompt_keys : undefined;
|
|
140
|
+
if (typeof o.origin !== 'string' || !/^[a-z]{1,20}$/.test(o.origin))
|
|
141
|
+
return undefined;
|
|
142
|
+
return {
|
|
143
|
+
origin: o.origin,
|
|
144
|
+
...(typeof o.origin_why === 'string' ? { why: o.origin_why.replace(/[^\w <>(),-]/g, '').slice(0, 120) } : {}),
|
|
145
|
+
...(typeof o.prompt_key === 'string' && o.prompt_key.length <= 128 ? { promptKey: o.prompt_key } : {}),
|
|
146
|
+
...(typeof o.prompts === 'number' && Number.isInteger(o.prompts) ? { prompts: o.prompts } : {}),
|
|
147
|
+
...(keys ? { promptKeys: keys } : {}),
|
|
148
|
+
...(typeof o.prompt_at === 'number' && Number.isFinite(o.prompt_at) ? { promptAt: o.prompt_at } : {}),
|
|
149
|
+
...(typeof o.opened_by === 'string' && /^[a-z]{1,20}$/.test(o.opened_by) ? { openedBy: o.opened_by } : {}),
|
|
150
|
+
...(words(o.origin_ids) ? { ids: words(o.origin_ids) } : {}),
|
|
151
|
+
...(words(o.origin_teammates) ? { teammates: words(o.origin_teammates) } : {}),
|
|
152
|
+
...(typeof o.joins === 'number' && Number.isInteger(o.joins) && o.joins >= 0 ? { joins: o.joins } : {}),
|
|
153
|
+
...(Array.isArray(o.joined) && o.joined.length <= 20 ? { joined: o.joined.map(joinedOf) } : {}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/** One entry of the hook's `joined`, in its own shape; anything else reads `other` (ties nothing). */
|
|
157
|
+
function joinedOf(v) {
|
|
158
|
+
const j = v && typeof v === 'object' ? v : {};
|
|
159
|
+
const ids = words(j.ids), mates = words(j.teammates);
|
|
160
|
+
if (typeof j.kind !== 'string' || !/^[a-z]{1,20}$/.test(j.kind))
|
|
161
|
+
return { kind: 'other' };
|
|
162
|
+
// A list that is there but not in its shape: the row is not known for anyone's work.
|
|
163
|
+
if ((j.ids !== undefined && !ids) || (j.teammates !== undefined && !mates))
|
|
164
|
+
return { kind: 'other' };
|
|
165
|
+
return { kind: j.kind, ...(ids ? { ids } : {}), ...(mates ? { teammates: mates } : {}) };
|
|
166
|
+
}
|
|
167
|
+
/** A list of ids or names as the hook writes them (turn-origin.mjs: TOOL_USE_ID, TEAMMATE_ID): plain
|
|
168
|
+
* words, at most 20. Anything else is no list, and ties the request to nothing. */
|
|
169
|
+
function words(v) {
|
|
170
|
+
if (!Array.isArray(v) || !v.length || v.length > 20)
|
|
171
|
+
return undefined;
|
|
172
|
+
return v.every((x) => typeof x === 'string' && /^[A-Za-z0-9_.-]{1,100}$/.test(x)) ? v : undefined;
|
|
173
|
+
}
|
|
104
174
|
/** What Claude Code writes as the result of a tool use a person refused in its dialog (No or Esc).
|
|
105
175
|
* Seen on 2.1.287: `toolUseResult` is this string, and the tool_result has `is_error: true`. */
|
|
106
176
|
export const REJECTED = 'User rejected tool use';
|