@pontia/claude-client 0.8.7 → 0.8.9
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/shared/control-socket.js +292 -0
- package/dist/shared/report-queue.js +33 -0
- package/dist/shared/rpc-error.js +7 -0
- package/dist/src/facts.js +28 -0
- package/dist/src/host.js +9 -6
- package/dist/src/main.js +1 -12
- package/dist/src/permissions.js +63 -37
- package/dist/src/server.js +143 -160
- package/package.json +3 -2
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { RpcError } from "./rpc-error.js";
|
|
2
|
+
export { RpcError } from "./rpc-error.js";
|
|
3
|
+
import { createConnection } from "node:net";
|
|
4
|
+
import { isAbsolute, join } from "node:path";
|
|
5
|
+
import { statSync } from "node:fs";
|
|
6
|
+
export const CONTROL_VERSION = 8;
|
|
7
|
+
export const MAX_CONTROL_FRAME_BYTES = 64 * 1024;
|
|
8
|
+
// Match the former HTTP event body limit, including the RPC envelope.
|
|
9
|
+
export const MAX_RPC_FRAME_BYTES = 2 * 1024 * 1024 + 1024;
|
|
10
|
+
const REQUEST_TIMEOUT_MS = 5_000;
|
|
11
|
+
export class AttachmentRejected extends Error {
|
|
12
|
+
}
|
|
13
|
+
export function controlSocketPath(pontiaHome) {
|
|
14
|
+
if (!isAbsolute(pontiaHome) || pontiaHome.includes("\0"))
|
|
15
|
+
throw new Error("Control home requires an absolute path without NUL");
|
|
16
|
+
const home = statSync(pontiaHome, { bigint: true });
|
|
17
|
+
return join(`/tmp/pontia-control-${home.uid}-${home.dev.toString(16)}-${home.ino.toString(16)}`, "rpc.sock");
|
|
18
|
+
}
|
|
19
|
+
class RpcSocket {
|
|
20
|
+
sequence = 0;
|
|
21
|
+
pending = new Map();
|
|
22
|
+
buffered = Buffer.alloc(0);
|
|
23
|
+
failed = false;
|
|
24
|
+
socket;
|
|
25
|
+
onRequest;
|
|
26
|
+
keepAlive;
|
|
27
|
+
constructor(socket, onRequest, keepAlive) {
|
|
28
|
+
this.socket = socket;
|
|
29
|
+
this.onRequest = onRequest;
|
|
30
|
+
this.keepAlive = keepAlive;
|
|
31
|
+
socket.on("data", (chunk) => this.receive(chunk));
|
|
32
|
+
socket.on("error", (error) => this.fail(error));
|
|
33
|
+
socket.on("close", () => this.fail(new Error("Control RPC connection closed; requests were not replayed")));
|
|
34
|
+
}
|
|
35
|
+
close() {
|
|
36
|
+
this.fail(new Error("Control RPC connection closed"));
|
|
37
|
+
}
|
|
38
|
+
request(method, params) {
|
|
39
|
+
if (this.failed)
|
|
40
|
+
return Promise.reject(new Error("Control RPC connection is closed"));
|
|
41
|
+
const id = `runtime:${this.sequence++}`;
|
|
42
|
+
const encoded = this.encode({ jsonrpc: "2.0", id, method, params });
|
|
43
|
+
return new Promise((resolve, reject) => {
|
|
44
|
+
const timer = setTimeout(() => this.fail(new Error("Control RPC timed out; request was not replayed")), REQUEST_TIMEOUT_MS);
|
|
45
|
+
timer.unref();
|
|
46
|
+
// RPC mode installs its stdin listener only after extension registration.
|
|
47
|
+
this.socket.ref();
|
|
48
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
49
|
+
this.write(encoded);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
encode(value) {
|
|
53
|
+
const encoded = JSON.stringify(value);
|
|
54
|
+
if (Buffer.byteLength(encoded) > MAX_RPC_FRAME_BYTES)
|
|
55
|
+
throw new RpcError(-32602, "Control RPC frame exceeds size limit");
|
|
56
|
+
return `${encoded}\n`;
|
|
57
|
+
}
|
|
58
|
+
write(encoded, afterWrite) {
|
|
59
|
+
if (this.socket.writableLength + Buffer.byteLength(encoded) > 2 * MAX_RPC_FRAME_BYTES) {
|
|
60
|
+
this.fail(new Error("Control RPC backpressure limit reached"));
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this.socket.write(encoded, afterWrite);
|
|
64
|
+
}
|
|
65
|
+
fail(error) {
|
|
66
|
+
if (this.failed)
|
|
67
|
+
return;
|
|
68
|
+
this.failed = true;
|
|
69
|
+
for (const pending of this.pending.values()) {
|
|
70
|
+
clearTimeout(pending.timer);
|
|
71
|
+
pending.reject(error);
|
|
72
|
+
}
|
|
73
|
+
this.pending.clear();
|
|
74
|
+
this.socket.destroy();
|
|
75
|
+
}
|
|
76
|
+
receive(chunk) {
|
|
77
|
+
let offset = 0;
|
|
78
|
+
try {
|
|
79
|
+
while (offset < chunk.length && !this.failed) {
|
|
80
|
+
const newline = chunk.indexOf(10, offset);
|
|
81
|
+
const end = newline === -1 ? chunk.length : newline;
|
|
82
|
+
const part = chunk.subarray(offset, end);
|
|
83
|
+
if (this.buffered.length + part.length > MAX_RPC_FRAME_BYTES)
|
|
84
|
+
throw new Error("Control RPC frame exceeds size limit");
|
|
85
|
+
this.buffered = Buffer.concat([this.buffered, part]);
|
|
86
|
+
if (newline === -1)
|
|
87
|
+
return;
|
|
88
|
+
const frame = this.buffered;
|
|
89
|
+
this.buffered = Buffer.alloc(0);
|
|
90
|
+
this.handle(JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(frame)));
|
|
91
|
+
offset = newline + 1;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
this.fail(error instanceof Error ? error : new Error(String(error)));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
handle(value) {
|
|
99
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
100
|
+
throw new Error("Invalid Control RPC frame");
|
|
101
|
+
const message = value;
|
|
102
|
+
if (message.jsonrpc !== "2.0")
|
|
103
|
+
throw new Error("Invalid Control RPC version");
|
|
104
|
+
if (Object.hasOwn(message, "method")) {
|
|
105
|
+
if (typeof message.method !== "string" ||
|
|
106
|
+
Object.hasOwn(message, "result") ||
|
|
107
|
+
Object.hasOwn(message, "error") ||
|
|
108
|
+
(message.id !== undefined &&
|
|
109
|
+
typeof message.id !== "string" &&
|
|
110
|
+
typeof message.id !== "number")) {
|
|
111
|
+
throw new Error("Invalid Control RPC request");
|
|
112
|
+
}
|
|
113
|
+
const respond = (body, afterWrite) => {
|
|
114
|
+
if (message.id !== undefined)
|
|
115
|
+
this.write(this.encode({ jsonrpc: "2.0", id: message.id, ...body }), afterWrite);
|
|
116
|
+
else
|
|
117
|
+
afterWrite?.();
|
|
118
|
+
};
|
|
119
|
+
const error = (code, message) => respond({ error: { code, message } });
|
|
120
|
+
const params = message.params === undefined ? {} : message.params;
|
|
121
|
+
if (!params || typeof params !== "object" || Array.isArray(params)) {
|
|
122
|
+
error(-32602, "Expected named parameters");
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.onRequest(message.method, params, respond, error, Buffer.byteLength(JSON.stringify(message)));
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
if (Object.hasOwn(message, "result") === Object.hasOwn(message, "error") ||
|
|
129
|
+
(message.error !== undefined &&
|
|
130
|
+
(!message.error ||
|
|
131
|
+
!Number.isInteger(message.error.code) ||
|
|
132
|
+
typeof message.error.message !== "string"))) {
|
|
133
|
+
throw new Error("Invalid Control RPC response");
|
|
134
|
+
}
|
|
135
|
+
const pending = this.pending.get(message.id);
|
|
136
|
+
if (!pending)
|
|
137
|
+
throw new Error("Unexpected Control RPC response ID");
|
|
138
|
+
this.pending.delete(message.id);
|
|
139
|
+
if (!this.pending.size && !this.keepAlive)
|
|
140
|
+
this.socket.unref();
|
|
141
|
+
clearTimeout(pending.timer);
|
|
142
|
+
if (message.error)
|
|
143
|
+
pending.reject(new RpcError(message.error.code, message.error.message));
|
|
144
|
+
else
|
|
145
|
+
pending.resolve(message.result);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export async function connectRuntime(pontiaHome, onError, onRequest, onReconnect, onDisconnect, keepAlive = false) {
|
|
149
|
+
const path = controlSocketPath(pontiaHome);
|
|
150
|
+
let identity;
|
|
151
|
+
let stopped = false;
|
|
152
|
+
let retry;
|
|
153
|
+
let current;
|
|
154
|
+
const connecting = new Set();
|
|
155
|
+
const peers = new Set();
|
|
156
|
+
let reconnectRejected;
|
|
157
|
+
let attached = false;
|
|
158
|
+
let delay = 250;
|
|
159
|
+
const waiting = new Set();
|
|
160
|
+
async function ready() {
|
|
161
|
+
if (!identity || attached || stopped || reconnectRejected)
|
|
162
|
+
return;
|
|
163
|
+
await new Promise((resolve, reject) => {
|
|
164
|
+
const wake = () => {
|
|
165
|
+
clearTimeout(timer);
|
|
166
|
+
waiting.delete(wake);
|
|
167
|
+
resolve();
|
|
168
|
+
};
|
|
169
|
+
const timer = setTimeout(() => {
|
|
170
|
+
waiting.delete(wake);
|
|
171
|
+
reject(new Error("Control reconnect timed out"));
|
|
172
|
+
}, REQUEST_TIMEOUT_MS);
|
|
173
|
+
waiting.add(wake);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
function wakeRequests() {
|
|
177
|
+
for (const wake of waiting)
|
|
178
|
+
wake();
|
|
179
|
+
}
|
|
180
|
+
async function open() {
|
|
181
|
+
const socket = createConnection(path);
|
|
182
|
+
connecting.add(socket);
|
|
183
|
+
if (!keepAlive)
|
|
184
|
+
socket.unref();
|
|
185
|
+
try {
|
|
186
|
+
await new Promise((resolve, reject) => {
|
|
187
|
+
const timer = setTimeout(() => socket.destroy(new Error("Control socket connection timed out")), REQUEST_TIMEOUT_MS);
|
|
188
|
+
socket.once("error", reject);
|
|
189
|
+
socket.once("connect", () => {
|
|
190
|
+
clearTimeout(timer);
|
|
191
|
+
socket.off("error", reject);
|
|
192
|
+
resolve();
|
|
193
|
+
});
|
|
194
|
+
socket.once("close", () => {
|
|
195
|
+
clearTimeout(timer);
|
|
196
|
+
reject(new Error("Control socket closed during connect"));
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
connecting.delete(socket);
|
|
202
|
+
}
|
|
203
|
+
if (stopped) {
|
|
204
|
+
socket.destroy();
|
|
205
|
+
throw new Error("Control connection is closed");
|
|
206
|
+
}
|
|
207
|
+
const peer = new RpcSocket(socket, onRequest, keepAlive);
|
|
208
|
+
peers.add(peer);
|
|
209
|
+
socket.once("close", () => {
|
|
210
|
+
peers.delete(peer);
|
|
211
|
+
if (current === peer) {
|
|
212
|
+
current = undefined;
|
|
213
|
+
attached = false;
|
|
214
|
+
onDisconnect?.();
|
|
215
|
+
reconnect();
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
return peer;
|
|
219
|
+
}
|
|
220
|
+
function reconnect() {
|
|
221
|
+
if (stopped || reconnectRejected || !identity || retry)
|
|
222
|
+
return;
|
|
223
|
+
retry = setTimeout(async () => {
|
|
224
|
+
retry = undefined;
|
|
225
|
+
try {
|
|
226
|
+
const peer = await open();
|
|
227
|
+
current = peer;
|
|
228
|
+
const result = (await peer.request("runtime.attach", {
|
|
229
|
+
version: CONTROL_VERSION,
|
|
230
|
+
session_id: identity.sessionId,
|
|
231
|
+
runtime_id: identity.runtimeId,
|
|
232
|
+
client_session_key: identity.clientSessionKey,
|
|
233
|
+
}));
|
|
234
|
+
if (result?.session_id !== identity.sessionId ||
|
|
235
|
+
result?.runtime_id !== identity.runtimeId) {
|
|
236
|
+
throw new RpcError(-32009, "Control reconnect identity mismatch");
|
|
237
|
+
}
|
|
238
|
+
attached = true;
|
|
239
|
+
delay = 250;
|
|
240
|
+
wakeRequests();
|
|
241
|
+
void onReconnect?.().catch(onError);
|
|
242
|
+
}
|
|
243
|
+
catch (error) {
|
|
244
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
245
|
+
// A rejected identity cannot be repaired by repeating registration.
|
|
246
|
+
if (error instanceof RpcError && error.code !== -32603) {
|
|
247
|
+
reconnectRejected = new AttachmentRejected(error.message);
|
|
248
|
+
wakeRequests();
|
|
249
|
+
}
|
|
250
|
+
current?.close();
|
|
251
|
+
current = undefined;
|
|
252
|
+
delay = Math.min(delay * 2, 5000);
|
|
253
|
+
reconnect();
|
|
254
|
+
}
|
|
255
|
+
}, delay);
|
|
256
|
+
if (!keepAlive)
|
|
257
|
+
retry.unref();
|
|
258
|
+
}
|
|
259
|
+
current = await open();
|
|
260
|
+
return {
|
|
261
|
+
async request(method, params) {
|
|
262
|
+
if (method === "event.report" || method === "host.event")
|
|
263
|
+
await ready();
|
|
264
|
+
else if (identity && !attached)
|
|
265
|
+
throw new Error("Control connection is unavailable");
|
|
266
|
+
if (reconnectRejected)
|
|
267
|
+
throw reconnectRejected;
|
|
268
|
+
if (stopped)
|
|
269
|
+
throw new AttachmentRejected("Control connection closed");
|
|
270
|
+
if (!current)
|
|
271
|
+
return Promise.reject(new Error("Control connection is unavailable"));
|
|
272
|
+
return current.request(method, params);
|
|
273
|
+
},
|
|
274
|
+
registered(value) {
|
|
275
|
+
identity = value;
|
|
276
|
+
attached = !!current;
|
|
277
|
+
if (!current)
|
|
278
|
+
reconnect();
|
|
279
|
+
},
|
|
280
|
+
async close() {
|
|
281
|
+
stopped = true;
|
|
282
|
+
wakeRequests();
|
|
283
|
+
if (retry)
|
|
284
|
+
clearTimeout(retry);
|
|
285
|
+
for (const socket of connecting)
|
|
286
|
+
socket.destroy();
|
|
287
|
+
for (const peer of peers)
|
|
288
|
+
peer.close();
|
|
289
|
+
current = undefined;
|
|
290
|
+
},
|
|
291
|
+
};
|
|
292
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
import { AttachmentRejected, RpcError } from "./control-socket.js";
|
|
3
|
+
// A queue belongs to one runtime attachment. Its owner captures the fact and key
|
|
4
|
+
// before enqueueing so every retry sends the same observation.
|
|
5
|
+
export class ReportQueue {
|
|
6
|
+
tail = Promise.resolve();
|
|
7
|
+
rejected;
|
|
8
|
+
drained() {
|
|
9
|
+
return this.tail;
|
|
10
|
+
}
|
|
11
|
+
enqueue(send) {
|
|
12
|
+
const result = this.tail.then(async () => {
|
|
13
|
+
let delay = 250;
|
|
14
|
+
for (;;) {
|
|
15
|
+
if (this.rejected)
|
|
16
|
+
throw this.rejected;
|
|
17
|
+
try {
|
|
18
|
+
return await send();
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (error instanceof AttachmentRejected)
|
|
22
|
+
this.rejected = error;
|
|
23
|
+
if (this.rejected || (error instanceof RpcError && error.code !== -32603))
|
|
24
|
+
throw error;
|
|
25
|
+
await setTimeout(delay, undefined, { ref: false });
|
|
26
|
+
delay = Math.min(delay * 2, 5000);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
this.tail = result.then(() => { }, () => { });
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
}
|
package/dist/src/facts.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
function reportKey(host, sequence, event) {
|
|
2
|
+
switch (event.kind) {
|
|
3
|
+
case "permission_requested":
|
|
4
|
+
return `claude:${host}:${event.permission_id}`;
|
|
5
|
+
case "permission_settled":
|
|
6
|
+
return `claude:${host}:${event.permission_id}:settled`;
|
|
7
|
+
case "started":
|
|
8
|
+
return `claude:${host}:${event.input_id}:start`;
|
|
9
|
+
case "finished":
|
|
10
|
+
return `claude:${host}:${event.result_id}`;
|
|
11
|
+
case "model":
|
|
12
|
+
case "context_usage":
|
|
13
|
+
case "compaction":
|
|
14
|
+
case "ready":
|
|
15
|
+
return `claude:${host}:${sequence}`;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function hostObservation(host, sequence, event) {
|
|
19
|
+
return {
|
|
20
|
+
host_id: host,
|
|
21
|
+
sequence,
|
|
22
|
+
report_key: reportKey(host, sequence, event),
|
|
23
|
+
...(event.kind === "permission_settled"
|
|
24
|
+
? { registration_key: `claude:${host}:${event.permission_id}` }
|
|
25
|
+
: {}),
|
|
26
|
+
event,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
1
29
|
export class FactError extends Error {
|
|
2
30
|
result;
|
|
3
31
|
constructor(reason, message) {
|
package/dist/src/host.js
CHANGED
|
@@ -101,7 +101,7 @@ export class Host {
|
|
|
101
101
|
...(policy?.disable_mcp ? ["mcp__*"] : []),
|
|
102
102
|
],
|
|
103
103
|
canUseTool: (tool, input, context) => {
|
|
104
|
-
if (
|
|
104
|
+
if (this.closed || this.closing || this.unavailable)
|
|
105
105
|
return Promise.resolve({ behavior: "deny", message: "Tool approval is unavailable." });
|
|
106
106
|
if (policy?.yolo)
|
|
107
107
|
return Promise.resolve({ behavior: "allow", updatedInput: input });
|
|
@@ -176,6 +176,12 @@ export class Host {
|
|
|
176
176
|
}
|
|
177
177
|
// The result, not this acknowledgement, decides whether interruption won.
|
|
178
178
|
}
|
|
179
|
+
hasPermission(id) {
|
|
180
|
+
return this.permissions.has(id);
|
|
181
|
+
}
|
|
182
|
+
cancelPermission(id) {
|
|
183
|
+
this.permissions.cancel(id);
|
|
184
|
+
}
|
|
179
185
|
resolvePermission(id, option) {
|
|
180
186
|
if (!this.acceptsControl() || !this.permissions.resolve(id, option))
|
|
181
187
|
throw new ControlError("rejected", "permission_expired_or_invalid");
|
|
@@ -204,12 +210,9 @@ export class Host {
|
|
|
204
210
|
}
|
|
205
211
|
controlLost() {
|
|
206
212
|
this.controlConnected = false;
|
|
207
|
-
this.permissions.expire();
|
|
208
213
|
}
|
|
209
|
-
controlRestored(
|
|
210
|
-
|
|
211
|
-
// fact can resume input. Before the first input, there cannot be a fact gap.
|
|
212
|
-
this.controlConnected = !this.execution || factsCaughtUp;
|
|
214
|
+
controlRestored() {
|
|
215
|
+
this.controlConnected = true;
|
|
213
216
|
}
|
|
214
217
|
acceptsControl() {
|
|
215
218
|
return !this.closed && !this.closing && !this.unavailable && this.controlConnected;
|
package/dist/src/main.js
CHANGED
|
@@ -29,22 +29,11 @@ async function main() {
|
|
|
29
29
|
}
|
|
30
30
|
const config = bootstrap(data);
|
|
31
31
|
await serve(config);
|
|
32
|
-
process.stdout.write('{"listening":true}\n');
|
|
33
32
|
return;
|
|
34
33
|
}
|
|
35
34
|
throw new Error("bootstrap_missing");
|
|
36
35
|
}
|
|
37
|
-
main().catch((
|
|
38
|
-
if (process.argv[2] !== "preflight" &&
|
|
39
|
-
process.argv[2] !== "history" &&
|
|
40
|
-
process.argv[2] !== "models") {
|
|
41
|
-
const systemCode = error instanceof Error && "code" in error ? error.code : undefined;
|
|
42
|
-
const code = typeof systemCode === "string" &&
|
|
43
|
-
["EACCES", "EADDRINUSE", "EINVAL", "ENOENT", "ENAMETOOLONG", "ENOTDIR", "EPERM"].includes(systemCode)
|
|
44
|
-
? systemCode
|
|
45
|
-
: "HOST_START_FAILED";
|
|
46
|
-
process.stdout.write(JSON.stringify({ listening: false, code }) + "\n");
|
|
47
|
-
}
|
|
36
|
+
main().catch(() => {
|
|
48
37
|
process.stderr.write("Claude Code host could not start.\n");
|
|
49
38
|
process.exitCode = 1;
|
|
50
39
|
});
|
package/dist/src/permissions.js
CHANGED
|
@@ -1,59 +1,85 @@
|
|
|
1
|
-
import {
|
|
2
|
-
//
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
// Object member order is immaterial. Array order, tool arguments and SDK permission
|
|
3
|
+
// context are part of the authorization. The signal and correlation ID are transport state.
|
|
4
|
+
function digest(tool, input, context) {
|
|
5
|
+
const content = JSON.stringify({ tool, input, context }, (_key, value) => {
|
|
6
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
7
|
+
return value;
|
|
8
|
+
return Object.fromEntries(Object.entries(value).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
9
|
+
});
|
|
10
|
+
return createHash("sha256").update(content).digest("hex");
|
|
11
|
+
}
|
|
3
12
|
export class Permissions {
|
|
4
13
|
emit;
|
|
5
14
|
pending = new Map();
|
|
6
15
|
constructor(emit) {
|
|
7
16
|
this.emit = emit;
|
|
8
17
|
}
|
|
9
|
-
request = (tool,
|
|
10
|
-
if (tool === "AskUserQuestion" ||
|
|
18
|
+
request = (tool, input, { signal, requestId: id, ...context }) => {
|
|
19
|
+
if (tool === "AskUserQuestion" || signal.aborted) {
|
|
11
20
|
return Promise.resolve({
|
|
12
21
|
behavior: "deny",
|
|
13
22
|
message: "This interaction is unsupported or expired.",
|
|
14
23
|
});
|
|
15
24
|
}
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
25
|
+
const authorization_digest = digest(tool, input, context);
|
|
26
|
+
const requested = {
|
|
27
|
+
kind: "permission_requested",
|
|
28
|
+
permission_id: id,
|
|
29
|
+
tool: tool.slice(0, 256),
|
|
30
|
+
blocking: !context.agentID,
|
|
31
|
+
authorization_digest,
|
|
32
|
+
};
|
|
33
|
+
const existing = this.pending.get(id);
|
|
34
|
+
if (existing) {
|
|
35
|
+
if (existing.digest !== authorization_digest) {
|
|
36
|
+
this.emit(requested);
|
|
37
|
+
existing.finish();
|
|
38
|
+
}
|
|
39
|
+
return existing.answer;
|
|
40
|
+
}
|
|
41
|
+
let settle;
|
|
42
|
+
const answer = new Promise((resolve) => {
|
|
43
|
+
settle = resolve;
|
|
44
|
+
});
|
|
45
|
+
const abort = () => finish();
|
|
46
|
+
const finish = (option) => {
|
|
47
|
+
if (!this.pending.delete(id))
|
|
48
|
+
return;
|
|
49
|
+
signal.removeEventListener("abort", abort);
|
|
40
50
|
this.emit({
|
|
41
|
-
kind: "
|
|
51
|
+
kind: "permission_settled",
|
|
42
52
|
permission_id: id,
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
authorization_digest,
|
|
54
|
+
outcome: option ? "resolved" : "expired",
|
|
45
55
|
});
|
|
46
|
-
|
|
56
|
+
settle(option === "allow"
|
|
57
|
+
? { behavior: "allow" }
|
|
58
|
+
: {
|
|
59
|
+
behavior: "deny",
|
|
60
|
+
message: option === "deny" ? "User denied this tool use." : "Tool approval expired.",
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
this.pending.set(id, { digest: authorization_digest, answer, finish });
|
|
64
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
65
|
+
this.emit(requested);
|
|
66
|
+
return answer;
|
|
47
67
|
};
|
|
48
68
|
resolve(id, option) {
|
|
49
|
-
const
|
|
50
|
-
if (!
|
|
69
|
+
const pending = this.pending.get(id);
|
|
70
|
+
if (!pending || (option !== "allow" && option !== "deny"))
|
|
51
71
|
return false;
|
|
52
|
-
finish(option);
|
|
72
|
+
pending.finish(option);
|
|
53
73
|
return true;
|
|
54
74
|
}
|
|
75
|
+
has(id) {
|
|
76
|
+
return this.pending.has(id);
|
|
77
|
+
}
|
|
78
|
+
cancel(id) {
|
|
79
|
+
this.pending.get(id)?.finish();
|
|
80
|
+
}
|
|
55
81
|
expire() {
|
|
56
|
-
for (const
|
|
57
|
-
finish();
|
|
82
|
+
for (const pending of this.pending.values())
|
|
83
|
+
pending.finish();
|
|
58
84
|
}
|
|
59
85
|
}
|
package/dist/src/server.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { dirname } from "node:path";
|
|
1
|
+
import { ReportQueue } from "../shared/report-queue.js";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { connectRuntime, RpcError, CONTROL_VERSION, } from "../shared/control-socket.js";
|
|
5
4
|
import { Host, ControlError } from "./host.js";
|
|
6
|
-
|
|
7
|
-
const RECEIPTS = 128;
|
|
5
|
+
import { hostObservation } from "./facts.js";
|
|
8
6
|
function object(value) {
|
|
9
7
|
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
10
8
|
throw new ControlError("rejected", "invalid_request");
|
|
@@ -20,8 +18,7 @@ export function bootstrap(value) {
|
|
|
20
18
|
const result = {
|
|
21
19
|
session_id: string(data.session_id),
|
|
22
20
|
runtime_id: string(data.runtime_id),
|
|
23
|
-
|
|
24
|
-
token: string(data.token),
|
|
21
|
+
pontia_home: string(data.pontia_home),
|
|
25
22
|
cwd: string(data.cwd),
|
|
26
23
|
};
|
|
27
24
|
if (data.system_prompt !== undefined)
|
|
@@ -49,49 +46,86 @@ export function bootstrap(value) {
|
|
|
49
46
|
yolo: policy.yolo,
|
|
50
47
|
};
|
|
51
48
|
}
|
|
52
|
-
if (!/^[0-9a-f]{64}$/.test(result.token))
|
|
53
|
-
throw new ControlError("rejected", "invalid_token");
|
|
54
49
|
return result;
|
|
55
50
|
}
|
|
56
51
|
export async function serve(config, factory) {
|
|
57
|
-
const directory = await lstat(dirname(config.socket_path));
|
|
58
|
-
if (!directory.isDirectory() ||
|
|
59
|
-
directory.isSymbolicLink() ||
|
|
60
|
-
(directory.mode & 0o077) !== 0 ||
|
|
61
|
-
directory.uid !== process.getuid?.())
|
|
62
|
-
throw new Error("socket_directory_not_private");
|
|
63
52
|
const generation = randomUUID();
|
|
64
|
-
let
|
|
53
|
+
let connected = false;
|
|
54
|
+
let generationOfConnection = 0;
|
|
65
55
|
let sequence = 0;
|
|
66
|
-
let
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (Buffer.byteLength(frame) > MAX_FRAME ||
|
|
73
|
-
socket.writableLength + Buffer.byteLength(frame) > MAX_FRAME) {
|
|
74
|
-
socket.destroy();
|
|
75
|
-
if (current === socket) {
|
|
76
|
-
current = undefined;
|
|
77
|
-
host.controlLost();
|
|
78
|
-
}
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
if (!socket.destroyed)
|
|
82
|
-
socket.write(frame);
|
|
56
|
+
let connection;
|
|
57
|
+
const reports = new Set();
|
|
58
|
+
const identity = {
|
|
59
|
+
sessionId: config.session_id,
|
|
60
|
+
runtimeId: config.runtime_id,
|
|
61
|
+
clientSessionKey: undefined,
|
|
83
62
|
};
|
|
63
|
+
const queue = new ReportQueue();
|
|
64
|
+
const acknowledged = new Map();
|
|
65
|
+
let pendingReady;
|
|
66
|
+
let submitted;
|
|
67
|
+
function deliver(params) {
|
|
68
|
+
const event = params.event;
|
|
69
|
+
const report = queue
|
|
70
|
+
.enqueue(async () => {
|
|
71
|
+
const receipt = object(await connection.request("host.event", params));
|
|
72
|
+
if (receipt.accepted !== true)
|
|
73
|
+
throw new RpcError(-32602, "Invalid Claude fact acknowledgement");
|
|
74
|
+
if (event.kind === "ready")
|
|
75
|
+
pendingReady = undefined;
|
|
76
|
+
if (event.kind === "permission_requested" && host.hasPermission(event.permission_id)) {
|
|
77
|
+
if (receipt.ended === true)
|
|
78
|
+
host.cancelPermission(event.permission_id);
|
|
79
|
+
else
|
|
80
|
+
acknowledged.set(event.permission_id, params);
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
.catch((error) => {
|
|
84
|
+
console.error("Claude host fact discarded", params.report_key, String(error));
|
|
85
|
+
if (event.kind === "permission_requested")
|
|
86
|
+
host.cancelPermission(event.permission_id);
|
|
87
|
+
});
|
|
88
|
+
reports.add(report);
|
|
89
|
+
void report.finally(() => reports.delete(report));
|
|
90
|
+
}
|
|
84
91
|
const host = new Host(config, (event) => {
|
|
85
|
-
sequence
|
|
86
|
-
if (
|
|
87
|
-
|
|
92
|
+
const params = hostObservation(generation, ++sequence, event);
|
|
93
|
+
if (event.kind === "started" && submitted?.id === event.input_id) {
|
|
94
|
+
Object.assign(params.event, { input: submitted.metadata });
|
|
95
|
+
submitted = undefined;
|
|
96
|
+
}
|
|
97
|
+
if (event.kind === "ready") {
|
|
98
|
+
identity.clientSessionKey = event.native_session_id;
|
|
99
|
+
pendingReady = {
|
|
100
|
+
report_key: params.report_key,
|
|
101
|
+
native_session_id: event.native_session_id,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (event.kind === "permission_settled")
|
|
105
|
+
acknowledged.delete(event.permission_id);
|
|
106
|
+
if (params.report_key || event.kind === "closed" || event.kind === "unavailable") {
|
|
107
|
+
deliver(params);
|
|
108
|
+
}
|
|
109
|
+
else if (connected) {
|
|
110
|
+
const current = generationOfConnection;
|
|
111
|
+
void queue
|
|
112
|
+
.drained()
|
|
113
|
+
.then(() => {
|
|
114
|
+
if (connected && current === generationOfConnection)
|
|
115
|
+
return connection.request("host.event", params);
|
|
116
|
+
})
|
|
117
|
+
.catch(() => { });
|
|
118
|
+
}
|
|
88
119
|
}, factory);
|
|
89
120
|
const invoke = async (request) => {
|
|
90
121
|
try {
|
|
91
122
|
switch (request.method) {
|
|
92
|
-
case "input":
|
|
93
|
-
|
|
123
|
+
case "input": {
|
|
124
|
+
const input = { id: string(request.input_id), metadata: object(request.input) };
|
|
125
|
+
host.submit(input.id, string(request.text));
|
|
126
|
+
submitted = input;
|
|
94
127
|
break;
|
|
128
|
+
}
|
|
95
129
|
case "steer":
|
|
96
130
|
host.steer(string(request.target_input_id), string(request.input_id), string(request.text));
|
|
97
131
|
break;
|
|
@@ -120,132 +154,81 @@ export async function serve(config, factory) {
|
|
|
120
154
|
: { status: "unknown", reason: "operation_unconfirmed" };
|
|
121
155
|
}
|
|
122
156
|
};
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!authenticated) {
|
|
154
|
-
const token = Buffer.from(typeof request.token === "string" ? request.token : "");
|
|
155
|
-
const expected = Buffer.from(config.token);
|
|
156
|
-
if (request.method !== "attach" ||
|
|
157
|
-
request.session_id !== config.session_id ||
|
|
158
|
-
request.runtime_id !== config.runtime_id ||
|
|
159
|
-
token.length !== expected.length ||
|
|
160
|
-
!timingSafeEqual(token, expected)) {
|
|
161
|
-
socket.destroy();
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
if (current) {
|
|
165
|
-
// A replacement does not inherit pending replies or replay missed facts.
|
|
166
|
-
current.destroy();
|
|
167
|
-
current = undefined;
|
|
168
|
-
host.controlLost();
|
|
169
|
-
}
|
|
170
|
-
current = socket;
|
|
171
|
-
host.controlRestored(request.last_host_id === generation && request.last_sequence === sequence);
|
|
172
|
-
authenticated = true;
|
|
173
|
-
send(socket, {
|
|
174
|
-
type: "attached",
|
|
175
|
-
host_id: generation,
|
|
176
|
-
sequence,
|
|
177
|
-
high_water: highWater,
|
|
178
|
-
...host.status(),
|
|
179
|
-
});
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
if (current !== socket || request.host_id !== generation) {
|
|
183
|
-
socket.destroy();
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
const id = request.id;
|
|
187
|
-
if (typeof id !== "number" || !Number.isSafeInteger(id) || id < 1)
|
|
188
|
-
throw new ControlError("rejected", "invalid_request_id");
|
|
189
|
-
const hash = createHash("sha256").update(line).digest("hex");
|
|
190
|
-
let receipt = receipts.get(id);
|
|
191
|
-
if (receipt) {
|
|
192
|
-
if (receipt.hash !== hash)
|
|
193
|
-
throw new ControlError("rejected", "request_id_reused");
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
if (id <= highWater)
|
|
197
|
-
throw new ControlError("rejected", "request_expired");
|
|
198
|
-
highWater = id;
|
|
199
|
-
receipt = { hash, response: invoke(request) };
|
|
200
|
-
receipts.set(id, receipt);
|
|
201
|
-
if (receipts.size > RECEIPTS)
|
|
202
|
-
receipts.delete(receipts.keys().next().value);
|
|
203
|
-
}
|
|
204
|
-
void receipt.response.then((response) => {
|
|
205
|
-
if (current === socket && !socket.destroyed) {
|
|
206
|
-
send(socket, { type: "response", host_id: generation, id, ...response });
|
|
207
|
-
}
|
|
208
|
-
if (request.method === "close" && response.status === "accepted" && !stopped) {
|
|
209
|
-
stopped = true;
|
|
210
|
-
server.close();
|
|
211
|
-
for (const peer of sockets)
|
|
212
|
-
peer.destroySoon();
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
catch (error) {
|
|
217
|
-
// Malformed or ambiguous requests are never executed, nor echoed into diagnostics.
|
|
218
|
-
send(socket, {
|
|
219
|
-
type: "error",
|
|
220
|
-
status: "rejected",
|
|
221
|
-
reason: error instanceof ControlError ? error.reason : "invalid_request",
|
|
222
|
-
});
|
|
223
|
-
}
|
|
157
|
+
connection = await connectRuntime(config.pontia_home, () => console.error("Claude host control connection unavailable"), (method, params, respond, error) => {
|
|
158
|
+
if (method === "host.status") {
|
|
159
|
+
respond({
|
|
160
|
+
result: {
|
|
161
|
+
host_id: generation,
|
|
162
|
+
...host.status(),
|
|
163
|
+
pending_ready: pendingReady,
|
|
164
|
+
launch: {
|
|
165
|
+
cwd: config.cwd,
|
|
166
|
+
config_dir: process.env.CLAUDE_CONFIG_DIR,
|
|
167
|
+
model: config.model,
|
|
168
|
+
resume: config.resume,
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (method !== "host.command") {
|
|
175
|
+
error(-32601, "Unknown host method");
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
connected = true;
|
|
179
|
+
host.controlRestored();
|
|
180
|
+
void invoke(params)
|
|
181
|
+
.then(async (result) => {
|
|
182
|
+
if (params.method === "close" && result.status === "accepted") {
|
|
183
|
+
await Promise.allSettled(reports);
|
|
184
|
+
respond({ result }, () => {
|
|
185
|
+
void connection.close();
|
|
186
|
+
});
|
|
224
187
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
188
|
+
else
|
|
189
|
+
respond({ result });
|
|
190
|
+
})
|
|
191
|
+
.catch(() => error(-32603, "Host control failed"));
|
|
192
|
+
}, async () => {
|
|
193
|
+
connected = true;
|
|
194
|
+
host.controlRestored();
|
|
195
|
+
for (const params of acknowledged.values())
|
|
196
|
+
deliver(params);
|
|
197
|
+
}, () => {
|
|
198
|
+
connected = false;
|
|
199
|
+
generationOfConnection++;
|
|
200
|
+
host.controlLost();
|
|
201
|
+
}, true);
|
|
235
202
|
try {
|
|
236
|
-
await
|
|
203
|
+
const attached = (await connection.request("runtime.attach", {
|
|
204
|
+
version: CONTROL_VERSION,
|
|
205
|
+
session_id: config.session_id,
|
|
206
|
+
runtime_id: config.runtime_id,
|
|
207
|
+
client_session_key: identity.clientSessionKey,
|
|
208
|
+
}));
|
|
209
|
+
if (attached.session_id !== config.session_id || attached.runtime_id !== config.runtime_id)
|
|
210
|
+
throw new Error("runtime_identity_mismatch");
|
|
211
|
+
connection.registered(identity);
|
|
212
|
+
connected = true;
|
|
213
|
+
host.controlRestored();
|
|
237
214
|
}
|
|
238
215
|
catch (error) {
|
|
239
|
-
|
|
216
|
+
await connection.close();
|
|
240
217
|
throw error;
|
|
241
218
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
stopped = true;
|
|
245
|
-
await host.close();
|
|
246
|
-
for (const socket of sockets)
|
|
247
|
-
socket.destroy();
|
|
248
|
-
await new Promise((resolve) => server.close(() => resolve()));
|
|
249
|
-
},
|
|
219
|
+
const terminate = () => {
|
|
220
|
+
void close();
|
|
250
221
|
};
|
|
222
|
+
process.once("SIGTERM", terminate);
|
|
223
|
+
async function close() {
|
|
224
|
+
process.off("SIGTERM", terminate);
|
|
225
|
+
try {
|
|
226
|
+
await host.close();
|
|
227
|
+
}
|
|
228
|
+
finally {
|
|
229
|
+
await Promise.allSettled(reports);
|
|
230
|
+
await connection.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return { close };
|
|
251
234
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pontia/claude-client",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.9",
|
|
4
4
|
"description": "Claude Code client for Pontia",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -10,7 +10,8 @@
|
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist/src",
|
|
13
|
-
"LICENSE"
|
|
13
|
+
"LICENSE",
|
|
14
|
+
"dist/shared"
|
|
14
15
|
],
|
|
15
16
|
"type": "module",
|
|
16
17
|
"main": "dist/src/main.js",
|