angelia-gateway 0.3.29 → 0.4.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/README.md +2 -1
- package/dist/adapters/inbox.js +3 -3
- package/dist/brain/acp.js +371 -0
- package/dist/brain/agy-config.js +56 -0
- package/dist/brain/agy-history.js +31 -0
- package/dist/brain/agy-instructions.js +301 -0
- package/dist/brain/agy-login.js +52 -0
- package/dist/brain/agy.js +554 -0
- package/dist/brain/argv.js +26 -0
- package/dist/brain/brain.js +19 -0
- package/dist/brain/cache.js +13 -1
- package/dist/brain/catalog.js +31 -1
- package/dist/brain/codex-config.js +2 -2
- package/dist/brain/codex.js +2 -2
- package/dist/brain/grok-history.js +9 -4
- package/dist/brain/grok.js +11 -236
- package/dist/brain/index.js +2 -0
- package/dist/brain/locate.js +39 -4
- package/dist/brain/pi-gate.js +99 -35
- package/dist/brain/pi.js +4 -2
- package/dist/brain/tool-request.js +47 -0
- package/dist/brain/tui.js +3 -2
- package/dist/build.json +3 -3
- package/dist/capabilities/compile.js +290 -28
- package/dist/cli/cli.js +6 -4
- package/dist/core/orchestrator.js +91 -14
- package/dist/core/paths.js +29 -1
- package/dist/core/permission-rules.js +3 -0
- package/dist/instance/config/load.js +2 -2
- package/dist/instance/config/schema.js +8 -3
- package/dist/instance/export.js +41 -3
- package/dist/instance/guide.js +10 -2
- package/dist/instance/init-terminal.js +2 -0
- package/dist/instance/init.js +12 -0
- package/dist/instance/instance.js +3 -1
- package/dist/instance/profile-views.js +7 -4
- package/dist/jobs/jobs.js +11 -18
- package/package.json +1 -1
- package/routing.example.yaml +1 -1
package/README.md
CHANGED
|
@@ -189,9 +189,10 @@ Found a hole? Report it privately: [SECURITY.md](SECURITY.md).
|
|
|
189
189
|
| Grok Build | `grok` | supported | 1.0.13 or newer, `grok login` |
|
|
190
190
|
| Codex | `codex` | supported | 0.157 or newer, `codex login` |
|
|
191
191
|
| pi | `pi` | supported | 0.80.4 or newer (tested on 0.86 and 0.87), macOS, a login or key for a provider |
|
|
192
|
+
| Antigravity CLI | `agy` | supported | 1.5.14 or newer (Gemini models), macOS, a Google sign-in (run `agy` once) |
|
|
192
193
|
| OpenCode | | coming soon | |
|
|
193
194
|
|
|
194
|
-
Each chat keeps one warm process, so a message never waits for a CLI to start. Permission prompts reach the chat on all
|
|
195
|
+
Each chat keeps one warm process, so a message never waits for a CLI to start. Permission prompts reach the chat on all five. Codex runs inside its own sandbox, which the operating system enforces: Angelia hands it the profile's rules, so it writes only in its folders and cannot read your credentials or the other profiles, shell commands included. pi has no sandbox of its own, so Angelia puts every pi shell command and every file read and write inside macOS's sandbox, made from the same rules. agy runs whole inside macOS's sandbox the same way, and Angelia's gate answers each of its permission requests from the rules; tmux mode and `chrome: true` stay Claude Code only, and subagents are partial on agy ([what works on each backend](docs/reference.md#what-works-on-each-backend)). agy reads a `GEMINI.md` that Angelia writes from `CLAUDE.md` before each start. Every CLI plugs into the same small interface in `src/brain/`, and nothing else in Angelia knows which one is running.
|
|
195
196
|
|
|
196
197
|
---
|
|
197
198
|
|
package/dist/adapters/inbox.js
CHANGED
|
@@ -126,8 +126,8 @@ export class SenderQuota {
|
|
|
126
126
|
this.perHour = perHour;
|
|
127
127
|
this.now = now;
|
|
128
128
|
}
|
|
129
|
-
/** True, and counted, when `sender` may have one more
|
|
130
|
-
take(sender) {
|
|
129
|
+
/** True, and counted, when `sender` may have one more now; `perHour` replaces the constructor's limit. */
|
|
130
|
+
take(sender, perHour = this.perHour) {
|
|
131
131
|
const cutoff = this.now() - 3600_000;
|
|
132
132
|
for (const [k, v] of this.seen) {
|
|
133
133
|
const kept = v.filter((t) => t > cutoff);
|
|
@@ -137,7 +137,7 @@ export class SenderQuota {
|
|
|
137
137
|
this.seen.delete(k);
|
|
138
138
|
}
|
|
139
139
|
const list = this.seen.get(sender) ?? [];
|
|
140
|
-
if (list.length >=
|
|
140
|
+
if (list.length >= perHour)
|
|
141
141
|
return false;
|
|
142
142
|
list.push(this.now());
|
|
143
143
|
this.seen.set(sender, list);
|
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { EventEmitter } from 'node:events';
|
|
3
|
+
import { childEnv } from './argv.js';
|
|
4
|
+
import { BrainExited, PermissionBook, exitReason, jsonLine, permissionPreview, stopChild, wireChild, CHILD_GROUP, signalGroup } from './brain.js';
|
|
5
|
+
/**
|
|
6
|
+
* A long-lived ACP agent (Agent Client Protocol, JSON-RPC 2.0 on stdio) per session key. Shared by
|
|
7
|
+
* the CLIs that speak it (grok, agy): spawn, `initialize`, a session opened by the subclass
|
|
8
|
+
* (`openSession`), `session/prompt` per turn with its `session/update` notifications read as the
|
|
9
|
+
* turn's text and progress, `session/request_permission` relayed to the chat (or decided by `gate`),
|
|
10
|
+
* stop and kill, and a start timeout. A subclass gives the launch line and the session handshake.
|
|
11
|
+
*/
|
|
12
|
+
export class AcpBrain extends EventEmitter {
|
|
13
|
+
profile;
|
|
14
|
+
session;
|
|
15
|
+
opts;
|
|
16
|
+
/** How long `initialize` and the session handshake may take. Read from the subclass, so a test may set it per backend. */
|
|
17
|
+
static startTimeoutMs = 60_000;
|
|
18
|
+
child;
|
|
19
|
+
/** Lines from the child, by kind: `update`, `permission`, `exit`. */
|
|
20
|
+
lines = new EventEmitter();
|
|
21
|
+
exited = false;
|
|
22
|
+
/** The tail of stderr, for the reason a child that died without answering gives. */
|
|
23
|
+
failure = '';
|
|
24
|
+
nextId = 1;
|
|
25
|
+
replies = new Map();
|
|
26
|
+
permissions;
|
|
27
|
+
/** Pending permission id -> the JSON-RPC id and options the agent offered, and the tool's name. */
|
|
28
|
+
permissionRequests = new Map();
|
|
29
|
+
/** Tools refused in the running turn, by the owner, a timeout or `gate`. */
|
|
30
|
+
refusedThisTurn = [];
|
|
31
|
+
/** Every request `gate` blocked in this session, oldest first. */
|
|
32
|
+
blocked = [];
|
|
33
|
+
ready;
|
|
34
|
+
lastUsedAt = Date.now();
|
|
35
|
+
lastOutputAt;
|
|
36
|
+
version = '';
|
|
37
|
+
backendSessionId;
|
|
38
|
+
/** How many history updates the last session/load replayed (and this class dropped). */
|
|
39
|
+
replayedUpdates = 0;
|
|
40
|
+
constructor(profile, session, opts = {}) {
|
|
41
|
+
super();
|
|
42
|
+
this.profile = profile;
|
|
43
|
+
this.session = session;
|
|
44
|
+
this.opts = opts;
|
|
45
|
+
this.permissions = new PermissionBook(opts.permissionTimeoutMs ?? 10 * 60_000, (id) => { if (this.answerPermission(id, false))
|
|
46
|
+
this.emit('permission-timeout', id); });
|
|
47
|
+
}
|
|
48
|
+
/** The request in Claude Code's naming, for the profile's permission rules; undefined when it cannot be named. */
|
|
49
|
+
toolRequest(_toolCall) { return undefined; }
|
|
50
|
+
/** Work before the child is spawned (folders, files it reads at start). */
|
|
51
|
+
beforeSpawn() { }
|
|
52
|
+
/** Each tool_call update, in a turn and between turns: for a backend that watches what never asks. */
|
|
53
|
+
onToolCall(_u) { }
|
|
54
|
+
/** The child's environment. */
|
|
55
|
+
spawnEnv() { return childEnv(this.opts.env); }
|
|
56
|
+
/**
|
|
57
|
+
* Whether the agent may speak between turns (a background command done, agy wakes itself; ADR 0017 H).
|
|
58
|
+
* When true, after the first turn the stream is read on: its text goes to the chat as an `unasked`
|
|
59
|
+
* answer once it has been quiet for `quietMs`, and its permission requests are gated as in a turn,
|
|
60
|
+
* with 'ask' refused (no turn is open to ask in). False keeps the old reading: nothing between turns.
|
|
61
|
+
*/
|
|
62
|
+
wakesBetweenTurns = false;
|
|
63
|
+
/** How long the stream must be quiet before a between-turns answer is sent. */
|
|
64
|
+
static quietMs = 1500;
|
|
65
|
+
/**
|
|
66
|
+
* The one place a permission request is decided before the chat is asked: 'allow' and 'block' answer
|
|
67
|
+
* the agent at once, 'ask' relays the request to the chat. Every request is asked by default.
|
|
68
|
+
*/
|
|
69
|
+
gate(_toolCall, _request) { return 'ask'; }
|
|
70
|
+
/** The answer of a turn that ended with no text. `refused`: the tools refused in it, in order. */
|
|
71
|
+
emptyAnswer(_refused) { return ''; }
|
|
72
|
+
get alive() { return !!this.child && !this.exited; }
|
|
73
|
+
/** A turn is reading the stream; outside one, after the first, `wakesBetweenTurns` reads it. */
|
|
74
|
+
inTurn = false;
|
|
75
|
+
betweenArmed = false;
|
|
76
|
+
between = { text: '', refused: [], timer: undefined };
|
|
77
|
+
start() {
|
|
78
|
+
this.beforeSpawn();
|
|
79
|
+
const [bin, ...args] = this.argv();
|
|
80
|
+
const child = spawn(bin, args, { cwd: this.profile.cwd, env: this.spawnEnv(), stdio: ['pipe', 'pipe', 'pipe'], ...CHILD_GROUP });
|
|
81
|
+
this.child = child;
|
|
82
|
+
if (this.wakesBetweenTurns) {
|
|
83
|
+
this.lines.on('update', (u) => { if (this.betweenArmed && !this.inTurn)
|
|
84
|
+
this.betweenUpdate(u); });
|
|
85
|
+
this.lines.on('permission', (m) => { if (this.betweenArmed && !this.inTurn)
|
|
86
|
+
this.betweenPermission(m); });
|
|
87
|
+
}
|
|
88
|
+
const done = (code, signal) => {
|
|
89
|
+
if (this.exited)
|
|
90
|
+
return;
|
|
91
|
+
this.exited = true;
|
|
92
|
+
clearTimeout(this.between.timer);
|
|
93
|
+
this.permissions.clear();
|
|
94
|
+
for (const p of this.replies.values())
|
|
95
|
+
p.reject(new BrainExited(`${this.cliName} exited`));
|
|
96
|
+
this.replies.clear();
|
|
97
|
+
this.lines.emit('exit', { code, signal });
|
|
98
|
+
this.emit('exit', { code, signal });
|
|
99
|
+
};
|
|
100
|
+
// A spawn that never happened would otherwise leave the handshake waiting for ever (wireChild).
|
|
101
|
+
wireChild(child, { exit: done, failure: (t) => { this.failure = t; }, line: (m) => { this.lastOutputAt = Date.now(); this.dispatch(m); } });
|
|
102
|
+
// An agent that never answers initialize would hold the chat's first turn for good (Codex has the
|
|
103
|
+
// same guard). Stopped then, so the next message starts a fresh process; the timer is cleared as
|
|
104
|
+
// soon as the handshake ends, or it would kill a healthy brain later.
|
|
105
|
+
const limit = this.constructor.startTimeoutMs;
|
|
106
|
+
let timer;
|
|
107
|
+
this.ready = Promise.race([this.handshake(), new Promise((_, reject) => {
|
|
108
|
+
timer = setTimeout(() => { this.kill(); reject(new Error(`${this.cliName} did not answer within ${Math.round(limit / 1000)} s of starting; check that \`${this.cliName} --version\` runs in a terminal`)); }, limit);
|
|
109
|
+
timer.unref?.();
|
|
110
|
+
})]).finally(() => clearTimeout(timer));
|
|
111
|
+
this.ready.catch(() => { });
|
|
112
|
+
}
|
|
113
|
+
dispatch(m) {
|
|
114
|
+
if (m.id !== undefined && m.method === undefined) {
|
|
115
|
+
const p = this.replies.get(m.id);
|
|
116
|
+
if (!p)
|
|
117
|
+
return;
|
|
118
|
+
this.replies.delete(m.id);
|
|
119
|
+
if (m.error)
|
|
120
|
+
p.reject(new Error(String(m.error.message ?? JSON.stringify(m.error))));
|
|
121
|
+
else
|
|
122
|
+
p.resolve(m.result ?? {});
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
if (m.method === 'session/request_permission') {
|
|
126
|
+
this.lines.emit('permission', m);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
if (m.method === 'session/update') {
|
|
130
|
+
// Another session of the same process (an agy subagent Angelia attached): its text is not this
|
|
131
|
+
// chat's answer; the parent reports what it did.
|
|
132
|
+
const other = m.params?.sessionId !== undefined && this.backendSessionId !== undefined && m.params.sessionId !== this.backendSessionId;
|
|
133
|
+
this.lines.emit(other ? 'other-update' : 'update', m.params?.update ?? {}, m.params?.sessionId);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (m.id !== undefined && m.method) {
|
|
137
|
+
// A client-side request we did not advertise (fs, terminal). Refuse it cleanly.
|
|
138
|
+
this.write({ jsonrpc: '2.0', id: m.id, error: { code: -32601, message: `unsupported: ${m.method}` } });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
write(obj) {
|
|
142
|
+
this.child?.stdin.write(jsonLine(obj));
|
|
143
|
+
}
|
|
144
|
+
call(method, params) {
|
|
145
|
+
if (!this.alive)
|
|
146
|
+
return Promise.reject(new BrainExited('brain not running'));
|
|
147
|
+
const id = this.nextId++;
|
|
148
|
+
return new Promise((resolve, reject) => {
|
|
149
|
+
this.replies.set(id, { resolve, reject });
|
|
150
|
+
this.write({ jsonrpc: '2.0', id, method, params });
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* `session/load` with the history it replays dropped. ACP replays the whole conversation as
|
|
155
|
+
* session/update notifications before it answers. The chat already holds every one of those
|
|
156
|
+
* messages, so they are dropped on purpose, and counted so the decision is visible. They must be
|
|
157
|
+
* drained here: an update still arriving once a turn's listener is attached would be sent to the
|
|
158
|
+
* chat as a fresh answer.
|
|
159
|
+
*/
|
|
160
|
+
async loadDroppingReplay(params) {
|
|
161
|
+
let replayed = 0;
|
|
162
|
+
const drop = () => { replayed++; };
|
|
163
|
+
this.lines.on('update', drop);
|
|
164
|
+
try {
|
|
165
|
+
return await this.call('session/load', params);
|
|
166
|
+
}
|
|
167
|
+
finally {
|
|
168
|
+
this.lines.off('update', drop);
|
|
169
|
+
this.replayedUpdates = replayed;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/** initialize, then open or load the session. Resolves to the ACP session id. */
|
|
173
|
+
async handshake() {
|
|
174
|
+
const init = await this.call('initialize', {
|
|
175
|
+
protocolVersion: 1,
|
|
176
|
+
clientCapabilities: { fs: { readTextFile: false, writeTextFile: false }, terminal: false },
|
|
177
|
+
});
|
|
178
|
+
if (typeof init.agentInfo?.version === 'string')
|
|
179
|
+
this.version = init.agentInfo.version;
|
|
180
|
+
return this.openSession(init);
|
|
181
|
+
}
|
|
182
|
+
async *turn(text) {
|
|
183
|
+
if (!this.alive || !this.ready)
|
|
184
|
+
throw new BrainExited('brain not running');
|
|
185
|
+
this.lastUsedAt = Date.now();
|
|
186
|
+
let sid;
|
|
187
|
+
try {
|
|
188
|
+
sid = await this.ready;
|
|
189
|
+
}
|
|
190
|
+
catch (e) {
|
|
191
|
+
yield { kind: 'result', text: '', isError: true, reason: this.exited ? exitReason(this.failure) : `handshake: ${e.message}` };
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
const queue = [];
|
|
195
|
+
let wake = null;
|
|
196
|
+
const push = (m) => { queue.push(m); wake?.(); };
|
|
197
|
+
const onUpdate = (u) => push({ __kind: 'update', u });
|
|
198
|
+
const onPermission = (m) => push({ __kind: 'permission', m });
|
|
199
|
+
const onExit = () => push({ __kind: 'exit' });
|
|
200
|
+
this.lines.on('update', onUpdate);
|
|
201
|
+
this.lines.on('permission', onPermission);
|
|
202
|
+
this.lines.on('exit', onExit);
|
|
203
|
+
this.refusedThisTurn = [];
|
|
204
|
+
this.inTurn = true;
|
|
205
|
+
// What the agent said between turns and was still waiting for quiet goes first, as its own answer.
|
|
206
|
+
const before = this.takeBetween();
|
|
207
|
+
if (before)
|
|
208
|
+
yield { kind: 'unasked', answer: 'self', text: before };
|
|
209
|
+
let done;
|
|
210
|
+
this.call('session/prompt', { sessionId: sid, prompt: [{ type: 'text', text }] })
|
|
211
|
+
.then((r) => { done = r; push({ __kind: 'done' }); }, (e) => { done = e; push({ __kind: 'done' }); });
|
|
212
|
+
let pendingText = '';
|
|
213
|
+
let lastFlushed = '';
|
|
214
|
+
try {
|
|
215
|
+
while (true) {
|
|
216
|
+
if (!queue.length)
|
|
217
|
+
await new Promise((r) => (wake = r));
|
|
218
|
+
wake = null;
|
|
219
|
+
const item = queue.shift();
|
|
220
|
+
if (item.__kind === 'exit') {
|
|
221
|
+
yield { kind: 'result', text: '', isError: true, reason: exitReason(this.failure) };
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
if (item.__kind === 'update') {
|
|
225
|
+
const u = item.u;
|
|
226
|
+
if (u.sessionUpdate === 'agent_message_chunk' && u.content?.type === 'text')
|
|
227
|
+
pendingText += String(u.content.text ?? '');
|
|
228
|
+
else if (u.sessionUpdate === 'tool_call') {
|
|
229
|
+
this.onToolCall(u);
|
|
230
|
+
const t = pendingText.trim();
|
|
231
|
+
if (t && t !== lastFlushed) {
|
|
232
|
+
lastFlushed = t;
|
|
233
|
+
yield { kind: 'progress', text: t };
|
|
234
|
+
}
|
|
235
|
+
pendingText = '';
|
|
236
|
+
}
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
if (item.__kind === 'permission') {
|
|
240
|
+
const m = item.m;
|
|
241
|
+
const options = Array.isArray(m.params?.options) ? m.params.options : [];
|
|
242
|
+
// Only ever this one call: an allow_always would let the agent skip every later ask of its kind.
|
|
243
|
+
// Without an allow_once, an approval is answered as a refusal (cancelled).
|
|
244
|
+
const allow = options.find((o) => o.kind === 'allow_once');
|
|
245
|
+
const deny = options.find((o) => o.kind === 'reject_once') ?? options.find((o) => o.kind === 'reject_always');
|
|
246
|
+
const tc = m.params?.toolCall ?? {};
|
|
247
|
+
const id = String(tc.toolCallId ?? m.id);
|
|
248
|
+
const tool = this.toolName(tc);
|
|
249
|
+
const request = this.toolRequest(tc);
|
|
250
|
+
const shown = permissionPreview(tc.rawInput ?? tc.title ?? '');
|
|
251
|
+
let verdict = this.gate(tc, request);
|
|
252
|
+
if (verdict === 'allow' && !allow)
|
|
253
|
+
verdict = { action: 'block', reason: `${this.cliName} offered no allow-once answer` };
|
|
254
|
+
if (verdict !== 'ask') {
|
|
255
|
+
const reason = typeof verdict === 'object' ? verdict.reason : undefined;
|
|
256
|
+
if (verdict !== 'allow') {
|
|
257
|
+
this.refusedThisTurn.push(reason ? `${tool}: ${reason}` : tool);
|
|
258
|
+
this.blocked.push({ tool, preview: shown.preview, at: Date.now(), ...(reason ? { reason } : {}) });
|
|
259
|
+
}
|
|
260
|
+
this.reply(m.id, verdict === 'allow' ? allow?.optionId : deny?.optionId);
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
this.permissionRequests.set(id, { rpcId: m.id, allow: allow?.optionId, deny: deny?.optionId, tool });
|
|
264
|
+
this.permissions.add(id);
|
|
265
|
+
yield { kind: 'permission', id, tool, ...shown, ...(request ? { request } : {}) };
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (item.__kind === 'done') {
|
|
269
|
+
this.lastUsedAt = Date.now();
|
|
270
|
+
if (done instanceof Error) {
|
|
271
|
+
yield { kind: 'result', text: '', isError: true, reason: this.exited ? exitReason(this.failure) : done.message };
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const stop = String(done?.stopReason ?? 'end_turn');
|
|
275
|
+
const text = pendingText.trim();
|
|
276
|
+
if (stop === 'refusal' && !text) {
|
|
277
|
+
yield { kind: 'result', text: '', isError: true, reason: 'refusal' };
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
yield { kind: 'result', text: text || this.emptyAnswer([...this.refusedThisTurn]), isError: false };
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
this.lines.off('update', onUpdate);
|
|
287
|
+
this.lines.off('permission', onPermission);
|
|
288
|
+
this.lines.off('exit', onExit);
|
|
289
|
+
this.inTurn = false;
|
|
290
|
+
if (this.wakesBetweenTurns)
|
|
291
|
+
this.betweenArmed = true;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/** Between turns: the agent's text gathers until the stream has been quiet for `quietMs`. */
|
|
295
|
+
betweenUpdate(u) {
|
|
296
|
+
if (u.sessionUpdate === 'tool_call')
|
|
297
|
+
this.onToolCall(u);
|
|
298
|
+
if (u.sessionUpdate === 'agent_message_chunk' && u.content?.type === 'text')
|
|
299
|
+
this.between.text += String(u.content.text ?? '');
|
|
300
|
+
else if (u.sessionUpdate === 'tool_call' && this.between.text && !this.between.text.endsWith('\n\n'))
|
|
301
|
+
this.between.text += '\n\n';
|
|
302
|
+
else if (u.sessionUpdate === 'tool_call')
|
|
303
|
+
this.emit('log', `${this.cliName} between turns: tool ${this.toolName(u)} ${String(u.status ?? '')}`);
|
|
304
|
+
this.armQuiet();
|
|
305
|
+
}
|
|
306
|
+
/** Between turns: gate() answers; what it would ask the chat is refused, as no turn is open to ask in. */
|
|
307
|
+
betweenPermission(m) {
|
|
308
|
+
const options = Array.isArray(m.params?.options) ? m.params.options : [];
|
|
309
|
+
const allow = options.find((o) => o.kind === 'allow_once');
|
|
310
|
+
const deny = options.find((o) => o.kind === 'reject_once') ?? options.find((o) => o.kind === 'reject_always');
|
|
311
|
+
const tc = m.params?.toolCall ?? {};
|
|
312
|
+
const tool = this.toolName(tc);
|
|
313
|
+
const verdict = this.gate(tc, this.toolRequest(tc));
|
|
314
|
+
if (verdict === 'allow') {
|
|
315
|
+
this.reply(m.id, allow?.optionId);
|
|
316
|
+
this.armQuiet();
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const reason = typeof verdict === 'object' ? verdict.reason : verdict === 'ask' ? 'asked between turns, when no one could answer' : undefined;
|
|
320
|
+
this.blocked.push({ tool, preview: permissionPreview(tc.rawInput ?? tc.title ?? '').preview, at: Date.now(), ...(reason ? { reason } : {}), between: true });
|
|
321
|
+
this.between.refused.push(reason ? `${tool}: ${reason}` : tool);
|
|
322
|
+
this.emit('log', `${this.cliName} between turns: refused ${tool}${reason ? ` (${reason})` : ''}`);
|
|
323
|
+
this.reply(m.id, deny?.optionId);
|
|
324
|
+
this.armQuiet();
|
|
325
|
+
}
|
|
326
|
+
armQuiet() {
|
|
327
|
+
clearTimeout(this.between.timer);
|
|
328
|
+
this.between.timer = setTimeout(() => {
|
|
329
|
+
const text = this.takeBetween();
|
|
330
|
+
if (text)
|
|
331
|
+
this.emit('unasked', { answer: 'self', text });
|
|
332
|
+
}, this.constructor.quietMs);
|
|
333
|
+
this.between.timer.unref?.();
|
|
334
|
+
}
|
|
335
|
+
/** The between-turns answer gathered so far, emptied. */
|
|
336
|
+
takeBetween() {
|
|
337
|
+
clearTimeout(this.between.timer);
|
|
338
|
+
const text = this.between.text.trim() || (this.between.refused.length ? this.emptyAnswer(this.between.refused) : '');
|
|
339
|
+
this.between = { text: '', refused: [], timer: undefined };
|
|
340
|
+
return text;
|
|
341
|
+
}
|
|
342
|
+
/** Answer a permission request with an option id; none means the request was cancelled. */
|
|
343
|
+
reply(rpcId, optionId) {
|
|
344
|
+
const outcome = optionId ? { outcome: 'selected', optionId } : { outcome: 'cancelled' };
|
|
345
|
+
this.write({ jsonrpc: '2.0', id: rpcId, result: { outcome } });
|
|
346
|
+
}
|
|
347
|
+
answerPermission(id, allow) {
|
|
348
|
+
const full = this.permissions.take(id);
|
|
349
|
+
if (!full)
|
|
350
|
+
return false;
|
|
351
|
+
const req = this.permissionRequests.get(full);
|
|
352
|
+
this.permissionRequests.delete(full);
|
|
353
|
+
if (!req)
|
|
354
|
+
return false;
|
|
355
|
+
if (!allow)
|
|
356
|
+
this.refusedThisTurn.push(req.tool);
|
|
357
|
+
this.reply(req.rpcId, allow ? req.allow : req.deny);
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
/** The running child's pid, for a backend that ends what the child started (agy). */
|
|
361
|
+
get childPid() { return this.child && !this.exited ? this.child.pid : undefined; }
|
|
362
|
+
get pendingPermissionCount() { return this.permissions.size; }
|
|
363
|
+
hasPendingPermission(id) { return this.permissions.has(id); }
|
|
364
|
+
async stop(graceMs = 5000) {
|
|
365
|
+
if (this.child && !this.exited)
|
|
366
|
+
await stopChild(this.child, this.lines, () => this.exited, graceMs);
|
|
367
|
+
}
|
|
368
|
+
kill() { if (this.child && !this.exited)
|
|
369
|
+
signalGroup(this.child, 'SIGKILL'); }
|
|
370
|
+
}
|
|
371
|
+
//# sourceMappingURL=acp.js.map
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { isInside, readPlainText } from '../core/paths.js';
|
|
4
|
+
import { placedPermissions, readGuard, writePlain } from '../capabilities/compile.js';
|
|
5
|
+
import { AGY_INSTRUCTIONS, agyInstructionsText, agyLaunchProblems } from './agy-instructions.js';
|
|
6
|
+
import { parseRules, real } from './pi-gate.js';
|
|
7
|
+
/** The compiled servers with their variables' values from `env`. A variable that is not set is
|
|
8
|
+
* left out, as Claude leaves an unset ${VAR} empty. */
|
|
9
|
+
export function acpMcpServers(servers, env = process.env) {
|
|
10
|
+
return servers.map((s) => ({
|
|
11
|
+
name: s.name, command: s.command, args: [...s.args],
|
|
12
|
+
env: s.env.filter((k) => env[k] !== undefined).map((k) => ({ name: k, value: String(env[k]) })),
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
/** The profile's compiled servers for session/new and session/resume; none when it was never compiled
|
|
16
|
+
* (the launch guard refuses that one anyway). */
|
|
17
|
+
export function agyMcpServers(stateDir, name, env = process.env) {
|
|
18
|
+
return acpMcpServers(readGuard(stateDir, name)?.agy?.mcpServers ?? [], env);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Before agy starts: refuse what it would load that Angelia did not give it, then write
|
|
22
|
+
* `<cwd>/GEMINI.md` from CLAUDE.md. An import the profile's Read rules deny (compiled, or in its
|
|
23
|
+
* settings now) is left out. Throws with the reasons; the start then fails with them.
|
|
24
|
+
*/
|
|
25
|
+
export function prepareAgyStart(p, o) {
|
|
26
|
+
const home = o.home ?? homedir();
|
|
27
|
+
const guard = readGuard(o.stateDir, o.name);
|
|
28
|
+
const problems = agyLaunchProblems(p.cwd, p.add_dirs, o.geminiDir, guard?.agy?.skills ?? [], home);
|
|
29
|
+
if (problems.length)
|
|
30
|
+
throw new Error(`agy not started: ${problems.join('; ')}`);
|
|
31
|
+
const deny = [...new Set([...(guard?.deny ?? []), ...placedPermissions(p.cwd, home).deny])].filter((r) => r.startsWith('Read('));
|
|
32
|
+
const rules = parseRules(deny, home);
|
|
33
|
+
// The state folder is closed but for what the profile reaches (stateOpenPaths), a file made there
|
|
34
|
+
// after the last compile too: an import from it is left out (red team round 2: secrets/x.env).
|
|
35
|
+
const state = resolve(o.stateDir), open = o.stateOpen;
|
|
36
|
+
const closedState = (x) => !!open && isInside(x, state, home) && x !== state && !open.some((d) => isInside(x, d, home));
|
|
37
|
+
const readable = (abs) => { const r = real(abs); return !rules.some((x) => x.test(r) || x.test(abs)) && !closedState(r) && !closedState(resolve(abs)); };
|
|
38
|
+
const claudeMd = join(p.cwd, 'CLAUDE.md');
|
|
39
|
+
let text = '';
|
|
40
|
+
// CLAUDE.md is the agent's to write: a link there to a file its Read rules deny would put that file's
|
|
41
|
+
// text into GEMINI.md, and a named pipe would hold the daemon (review 3, M1). Both refuse the start.
|
|
42
|
+
if (!readable(claudeMd))
|
|
43
|
+
throw new Error(`agy not started: ${claudeMd} leads to a file this profile may not read`);
|
|
44
|
+
try {
|
|
45
|
+
text = readPlainText(claudeMd);
|
|
46
|
+
}
|
|
47
|
+
catch (e) {
|
|
48
|
+
const code = e.code;
|
|
49
|
+
if (code === 'EFTYPE')
|
|
50
|
+
throw new Error(`agy not started: ${claudeMd} is not a plain file; make it one`);
|
|
51
|
+
if (code !== 'ENOENT')
|
|
52
|
+
throw e;
|
|
53
|
+
}
|
|
54
|
+
writePlain(p.cwd, join(p.cwd, AGY_INSTRUCTIONS), agyInstructionsText(claudeMd, text, { home, readable }));
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=agy-config.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { homedir } from 'node:os';
|
|
2
|
+
import { agyArgv } from './argv.js';
|
|
3
|
+
import { agyGeminiDir } from './cache.js';
|
|
4
|
+
import { acpReplay } from './grok-history.js';
|
|
5
|
+
import { SANDBOX_EXEC } from './pi-gate.js';
|
|
6
|
+
import { AgyBrain, agyPolicy, agySandboxed, agySandboxProfile, agySeedLogin } from './agy.js';
|
|
7
|
+
import { agyLaunchProblems } from './agy-instructions.js';
|
|
8
|
+
import { readGuard } from '../capabilities/compile.js';
|
|
9
|
+
/** A load of an agy session took about 2 s with its start (agy 1.5.14); a cold start up to 20 s. */
|
|
10
|
+
export const AGY_REPLAY_TIMEOUT_MS = 45_000;
|
|
11
|
+
/**
|
|
12
|
+
* An agy session's conversation, for /history and the app: `agy acp` started on the profile's own
|
|
13
|
+
* --gemini-dir, in the same sandbox as the chat's agy, `session/load`, keep what it replays, stop it.
|
|
14
|
+
* No model is called. The conversation store is protobuf in SQLite (antigravity-cli/conversations/
|
|
15
|
+
* <id>.db), which nothing here reads without a new dependency, so the replay is the reader.
|
|
16
|
+
* Refused, like a chat's start, when agy would load what Angelia did not give it (agyLaunchProblems).
|
|
17
|
+
*/
|
|
18
|
+
export function agyReplay(profile, sessionId, o) {
|
|
19
|
+
const gd = o.geminiDir ?? agyGeminiDir(o.stateDir, o.profileName);
|
|
20
|
+
const problems = agyLaunchProblems(profile.cwd, profile.add_dirs, gd, readGuard(o.stateDir, o.profileName)?.agy?.skills ?? []);
|
|
21
|
+
if (problems.length)
|
|
22
|
+
return Promise.reject(new Error(`agy not started: ${problems.join('; ')}`));
|
|
23
|
+
if (agySandboxed(profile))
|
|
24
|
+
agySeedLogin(gd, AgyBrain.readLogin);
|
|
25
|
+
const line = agyArgv(profile, gd, o.bin ?? 'agy');
|
|
26
|
+
const argv = agySandboxed(profile)
|
|
27
|
+
? [SANDBOX_EXEC, '-p', agySandboxProfile(agyPolicy(profile), gd, homedir(), { closeSiblings: !o.geminiDir }), ...line]
|
|
28
|
+
: line;
|
|
29
|
+
return acpReplay(argv, profile.cwd, sessionId, { env: o.env, timeoutMs: o.timeoutMs ?? AGY_REPLAY_TIMEOUT_MS });
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=agy-history.js.map
|