@howaboua/pi-shepherdr 0.2.6 → 0.2.8

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/src/launch.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { stat } from "node:fs/promises";
2
2
  import { resolve } from "node:path";
3
+ import { parseArgs } from "@earendil-works/pi-coding-agent";
3
4
  import {
4
5
  getAgent,
5
6
  getPane,
6
7
  getSnapshot,
7
8
  parsePaneInfo,
8
9
  resolveWorkspace,
10
+ sessionPath,
9
11
  } from "./herdr.js";
10
12
  import { type HerdrConnection, isHerdrErrorCode } from "./herdr-client.js";
11
13
  import type { PaneInfo } from "./types.js";
@@ -208,6 +210,12 @@ export async function startAgent(
208
210
  throw new Error("name must match [a-z][a-z0-9_-]{0,31}");
209
211
  }
210
212
  const label = params.label?.trim() || name;
213
+ const args = ["--name", label, ...(options.agentArgs ?? [])];
214
+ if (parseArgs(args).noSession) {
215
+ throw new Error(
216
+ "spawn requires a persistent Pi session; remove --no-session from profile pi_args",
217
+ );
218
+ }
211
219
  if (params.placement === "pane" && params.cwd) {
212
220
  throw new Error(
213
221
  "cwd cannot change an existing pane; prepare the pane through Herdr",
@@ -225,7 +233,7 @@ export async function startAgent(
225
233
  name,
226
234
  kind: "pi",
227
235
  pane_id: created.paneId,
228
- args: ["--name", label, ...(options.agentArgs ?? [])],
236
+ args,
229
237
  timeout_ms: 30_000,
230
238
  });
231
239
  } catch (error) {
@@ -303,6 +311,7 @@ async function startWhenShellReady(
303
311
  );
304
312
  const terminalId = started.agent.terminal_id;
305
313
  const readyDeadline = Date.now() + 30_000;
314
+ let waitingForSession = false;
306
315
  while (Date.now() < readyDeadline) {
307
316
  let current: PaneInfo;
308
317
  try {
@@ -321,14 +330,14 @@ async function startWhenShellReady(
321
330
  if (current.agent_status === "blocked") {
322
331
  throw new Error(`agent ${name} is blocked during startup`);
323
332
  }
324
- if (
333
+ waitingForSession =
325
334
  (current.agent_status === "idle" || current.agent_status === "done") &&
326
335
  current.interactive_ready === true &&
327
- current.agent === "pi"
328
- ) {
329
- return current;
330
- }
331
- if (
336
+ current.agent === "pi";
337
+ if (waitingForSession) {
338
+ if (sessionPath(current)) return current;
339
+ // Herdr can finish launch before Pi reports its persistent session.
340
+ } else if (
332
341
  (current.agent_status === "idle" || current.agent_status === "done") &&
333
342
  current.launch_pending === false
334
343
  ) {
@@ -336,7 +345,9 @@ async function startWhenShellReady(
336
345
  }
337
346
  await new Promise((resolve) => setTimeout(resolve, 100));
338
347
  }
339
- throw new Error(`timed out waiting for agent ${name} to become interactive`);
348
+ throw new Error(
349
+ `timed out waiting for agent ${name} ${waitingForSession ? "to register its Pi session" : "to become interactive"}`,
350
+ );
340
351
  }
341
352
 
342
353
  async function resolveDuringStart(
package/src/messages.ts CHANGED
@@ -22,6 +22,8 @@ const AGENT_EVENT_MESSAGE_TYPE = "herdr-agent-event";
22
22
  const REALTIME_VOICE_PROMPT_CHANNEL =
23
23
  "@howaboua/pi-codex-conversion/realtime-voice-prompt/v1";
24
24
  const MAX_REALTIME_VOICE_PROMPT_BYTES = 8 * 1_024;
25
+ const DELEGATED_WORKER_GUIDANCE =
26
+ "When finished or blocked from progressing, end with an assistant reply. Use send only for useful mid-run updates while work can continue. Do not watch your parent or use messaging to wait for a reply.";
25
27
 
26
28
  function xml(value: string): string {
27
29
  return value
@@ -85,6 +87,7 @@ export async function attributeAgentPrompt(
85
87
  return {
86
88
  sender: `<herdr_sender ${sourceAttributes(source)} />`,
87
89
  text: message,
90
+ ...(kind === "task" ? { context: DELEGATED_WORKER_GUIDANCE } : {}),
88
91
  };
89
92
  }
90
93
 
@@ -213,18 +216,37 @@ function agentVoicePrompt(details: AgentEventDetails): string {
213
216
 
214
217
  function announceAgentEvent(
215
218
  pi: ExtensionAPI,
216
- details: AgentEventDetails,
219
+ candidateId: string,
220
+ prompt: string,
217
221
  ): void {
218
- const candidateId = `pi-shepherdr:${details.machine}:${details.paneId}`;
219
222
  const id =
220
223
  new TextEncoder().encode(candidateId).byteLength <= 160
221
224
  ? candidateId
222
225
  : "pi-shepherdr:worker";
223
- const prompt = agentVoicePrompt(details);
224
226
  pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: true, prompt });
225
227
  pi.events.emit(REALTIME_VOICE_PROMPT_CHANNEL, { id, active: false, prompt });
226
228
  }
227
229
 
230
+ export function announcePeerMessage(
231
+ pi: ExtensionAPI,
232
+ message: PeerMessage,
233
+ ): void {
234
+ const source = Object.fromEntries(
235
+ Array.from(
236
+ message.sender.matchAll(/ ([a-z_]+)="([^"]*)"/g),
237
+ ([, key, value]) => [key, value],
238
+ ),
239
+ );
240
+ if (source["kind"] !== "message" || message.text.startsWith("/")) return;
241
+ announceAgentEvent(
242
+ pi,
243
+ `pi-shepherdr:${source["host"] ?? "local"}:${source["pane"] ?? "peer"}`,
244
+ boundedVoicePrompt(
245
+ `Briefly tell the user the useful update from this peer.\n\nPeer: ${source["host"] ?? "local"} / ${source["name"] || source["pane"] || "unnamed"}\n\nUpdate:\n${message.text.trim()}`,
246
+ ),
247
+ );
248
+ }
249
+
228
250
  function operatorCommand(operatorPrefix: string, command: string): string {
229
251
  return `\`${operatorPrefix} ${command}\``;
230
252
  }
@@ -376,7 +398,11 @@ export function injectAgentEvent(
376
398
  delivery,
377
399
  );
378
400
  if (idle) startPreparedIdleTurn(pi, ctx);
379
- announceAgentEvent(pi, message.details);
401
+ announceAgentEvent(
402
+ pi,
403
+ `pi-shepherdr:${message.details.machine}:${message.details.paneId}`,
404
+ agentVoicePrompt(message.details),
405
+ );
380
406
  }
381
407
 
382
408
  export function registerAgentEventRenderer(pi: ExtensionAPI): void {
package/src/peer-inbox.ts CHANGED
@@ -8,6 +8,7 @@ import type {
8
8
  import { sendPolicyMessage, startPreparedIdleTurn } from "./delivery.js";
9
9
  import { getCurrentPane } from "./herdr.js";
10
10
  import { HerdrClient } from "./herdr-client.js";
11
+ import { announcePeerMessage } from "./messages.js";
11
12
  import {
12
13
  MAX_PEER_FRAME_BYTES,
13
14
  peerInboxPath,
@@ -168,6 +169,15 @@ async function openInbox(
168
169
  // Appending then claiming the shared kickoff also coalesces arrivals
169
170
  // during async preparation, when Pi still reports itself idle.
170
171
  if (idle) startPreparedIdleTurn(pi, ctx);
172
+ // Voice failure must not turn accepted delivery into a retry.
173
+ try {
174
+ announcePeerMessage(pi, { sender: request["sender"], text });
175
+ } catch (error) {
176
+ ctx.ui.notify(
177
+ `Peer voice update failed: ${String(error)}`,
178
+ "warning",
179
+ );
180
+ }
171
181
  reply({ ok: true, command: false });
172
182
  } catch (error) {
173
183
  reply({
package/src/profiles.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { readFileSync } from "node:fs";
1
2
  import {
2
3
  cp,
3
4
  mkdir,
@@ -25,6 +26,7 @@ const BUNDLED_PROFILES_DIRECTORY = fileURLToPath(
25
26
 
26
27
  export interface AgentProfile {
27
28
  accepts: string[];
29
+ blocking?: boolean;
28
30
  description: string;
29
31
  model: string;
30
32
  name: string;
@@ -32,6 +34,7 @@ export interface AgentProfile {
32
34
  prepare?: string;
33
35
  prompt?: string;
34
36
  promptPath?: string;
37
+ shareContext?: boolean;
35
38
  thinking: string;
36
39
  }
37
40
 
@@ -45,11 +48,13 @@ type ProfilePreparationContext = ProfilePreparationInput & { local: boolean };
45
48
 
46
49
  interface ProfileFile {
47
50
  accepts?: unknown;
51
+ blocking?: unknown;
48
52
  description?: unknown;
49
53
  model?: unknown;
50
54
  pi_args?: unknown;
51
55
  prepare?: unknown;
52
56
  prompt?: unknown;
57
+ share_context?: unknown;
53
58
  thinking?: unknown;
54
59
  }
55
60
 
@@ -71,14 +76,17 @@ function optionalString(value: unknown, field: string): string | undefined {
71
76
  return value.trim();
72
77
  }
73
78
 
74
- async function readProfile(
75
- name: string,
76
- directory: string,
77
- ): Promise<AgentProfile> {
79
+ function optionalBoolean(value: unknown, field: string): boolean | undefined {
80
+ if (value === undefined) return undefined;
81
+ if (typeof value !== "boolean") throw new Error(`${field} must be a boolean`);
82
+ return value;
83
+ }
84
+
85
+ function readProfileFile(name: string, directory: string): ProfileFile {
78
86
  const path = join(directory, "profile.json");
79
87
  let parsed: ProfileFile;
80
88
  try {
81
- parsed = JSON.parse(await readFile(path, "utf8")) as ProfileFile;
89
+ parsed = JSON.parse(readFileSync(path, "utf8")) as ProfileFile;
82
90
  } catch (error) {
83
91
  throw new Error(
84
92
  `invalid ${name} profile: ${error instanceof Error ? error.message : String(error)}`,
@@ -89,17 +97,40 @@ async function readProfile(
89
97
  }
90
98
  const allowed = new Set([
91
99
  "accepts",
100
+ "blocking",
92
101
  "description",
93
102
  "model",
94
103
  "pi_args",
95
104
  "prepare",
96
105
  "prompt",
106
+ "share_context",
97
107
  "thinking",
98
108
  ]);
99
109
  const unknown = Object.keys(parsed).filter((key) => !allowed.has(key));
100
110
  if (unknown.length > 0) {
101
111
  throw new Error(`unknown ${name} profile field(s): ${unknown.join(", ")}`);
102
112
  }
113
+ return parsed;
114
+ }
115
+
116
+ // Code/Notebook wait classification is synchronous; read live policy, not a cache.
117
+ export function loadAgentProfileBlocking(name: string): boolean | undefined {
118
+ if (!PROFILE_NAME.test(name))
119
+ throw new Error(`invalid agent profile: ${name}`);
120
+ const parsed = readProfileFile(name, join(PROFILES_DIRECTORY, name));
121
+ return optionalBoolean(parsed.blocking, `${name}.blocking`);
122
+ }
123
+
124
+ async function readProfile(
125
+ name: string,
126
+ directory: string,
127
+ ): Promise<AgentProfile> {
128
+ const parsed = readProfileFile(name, directory);
129
+ const blocking = optionalBoolean(parsed.blocking, `${name}.blocking`);
130
+ const shareContext = optionalBoolean(
131
+ parsed.share_context,
132
+ `${name}.share_context`,
133
+ );
103
134
  const description = optionalString(parsed.description, `${name}.description`);
104
135
  if (!description) throw new Error(`${name}.description is required`);
105
136
  const model = optionalString(parsed.model, `${name}.model`);
@@ -123,6 +154,8 @@ async function readProfile(
123
154
  description,
124
155
  model,
125
156
  thinking,
157
+ ...(blocking === undefined ? {} : { blocking }),
158
+ ...(shareContext === undefined ? {} : { shareContext }),
126
159
  accepts: parsed.accepts
127
160
  ? stringArray(parsed.accepts, `${name}.accepts`)
128
161
  : [],
@@ -0,0 +1,260 @@
1
+ // @howaboua/pi-shepherdr managed bridge
2
+ import { randomBytes, randomUUID } from "node:crypto";
3
+ import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
4
+ import { createConnection, createServer } from "node:net";
5
+ import { homedir } from "node:os";
6
+ import { dirname, join } from "node:path";
7
+ import { readReceiver } from "./shepherdr-peer.mjs";
8
+
9
+ const MAX_BYTES = 8 * 1024 * 1024;
10
+ const TIMEOUT = 35_000;
11
+
12
+ function contextDirectory() {
13
+ return join(
14
+ process.env["PI_CODING_AGENT_DIR"] || join(homedir(), ".pi", "agent"),
15
+ "shepherdr",
16
+ "context",
17
+ );
18
+ }
19
+
20
+ /** @param {string} threadId @param {string} routeId */
21
+ export function relayContextPath(threadId, routeId) {
22
+ if (typeof threadId !== "string" || !/^[a-zA-Z0-9_-]+$/.test(threadId))
23
+ throw new Error("Invalid context thread ID");
24
+ if (typeof routeId !== "string" || !/^[a-f0-9]{32}$/.test(routeId))
25
+ throw new Error("Invalid context route ID");
26
+ return join(contextDirectory(), `relay-${routeId}-${threadId}.json`);
27
+ }
28
+
29
+ /** @param {string} sessionFile */
30
+ export function sessionContextPath(sessionFile) {
31
+ return `${sessionFile}.shepherdr-context.json`;
32
+ }
33
+
34
+ /** @param {unknown} error @param {string} code */
35
+ function hasCode(error, code) {
36
+ return error instanceof Error && "code" in error && error.code === code;
37
+ }
38
+
39
+ /** Local authenticated RPC; SSH helpers relay over their existing connection.
40
+ * @param {string} path @param {unknown} request @param {AbortSignal} [signal]
41
+ * @returns {Promise<unknown>} */
42
+ export async function requestContext(path, request, signal) {
43
+ signal?.throwIfAborted();
44
+ let descriptor;
45
+ try {
46
+ descriptor = JSON.parse(await readReceiver(path));
47
+ } catch (error) {
48
+ throw new Error(
49
+ "Shared context owner is unavailable; resume it and reconnect its machine",
50
+ { cause: error },
51
+ );
52
+ }
53
+ if (
54
+ descriptor.protocol !== 1 ||
55
+ !Number.isInteger(descriptor.port) ||
56
+ descriptor.port < 1 ||
57
+ descriptor.port > 65535 ||
58
+ typeof descriptor.token !== "string" ||
59
+ !/^[a-f0-9]{64}$/.test(descriptor.token)
60
+ )
61
+ throw new Error("Invalid shared context receiver");
62
+ const id = randomUUID();
63
+ const frame = `${JSON.stringify({ protocol: 1, id, token: descriptor.token, request })}\n`;
64
+ if (Buffer.byteLength(frame) > MAX_BYTES)
65
+ throw new Error("Shared context request is too large");
66
+ return new Promise((resolve, reject) => {
67
+ let attempted = false;
68
+ let settled = false;
69
+ let buffer = "";
70
+ const socket = createConnection({
71
+ host: "127.0.0.1",
72
+ port: descriptor.port,
73
+ });
74
+ /** @param {Error} [error] @param {unknown} [result] */
75
+ const finish = (error, result) => {
76
+ if (settled) return;
77
+ settled = true;
78
+ clearTimeout(timer);
79
+ signal?.removeEventListener("abort", failed);
80
+ socket.destroy();
81
+ if (error) reject(error);
82
+ else resolve(result);
83
+ };
84
+ const failed = () =>
85
+ finish(
86
+ new Error(
87
+ attempted
88
+ ? "Shared context response was lost; a write may have completed. Reread the note before retrying"
89
+ : "Shared context owner is unavailable; resume it and reconnect its machine",
90
+ ),
91
+ );
92
+ const timer = setTimeout(failed, TIMEOUT);
93
+ timer.unref();
94
+ signal?.addEventListener("abort", failed, { once: true });
95
+ if (signal?.aborted) {
96
+ failed();
97
+ return;
98
+ }
99
+ socket.setEncoding("utf8");
100
+ socket.on("error", failed);
101
+ socket.on("close", failed);
102
+ socket.on("connect", () => {
103
+ attempted = true;
104
+ socket.write(frame);
105
+ });
106
+ socket.on("data", (chunk) => {
107
+ buffer += chunk;
108
+ if (Buffer.byteLength(buffer) > MAX_BYTES) {
109
+ failed();
110
+ return;
111
+ }
112
+ const newline = buffer.indexOf("\n");
113
+ if (newline < 0) return;
114
+ try {
115
+ const value = JSON.parse(buffer.slice(0, newline));
116
+ if (value.protocol !== 1 || value.id !== id) {
117
+ failed();
118
+ return;
119
+ }
120
+ if (value.ok === true) finish(undefined, value.result);
121
+ else if (value.ok === false && typeof value.error === "string")
122
+ finish(new Error(value.error));
123
+ else failed();
124
+ } catch {
125
+ failed();
126
+ }
127
+ });
128
+ });
129
+ }
130
+
131
+ /** @param {string} path @param {(request: unknown, signal: AbortSignal) => Promise<unknown>} handle */
132
+ export async function listenContext(path, handle) {
133
+ await mkdir(dirname(path), { recursive: true, mode: 0o700 });
134
+ try {
135
+ const previous = JSON.parse(await readFile(path, "utf8"));
136
+ if (Number.isInteger(previous.pid)) {
137
+ let alive = true;
138
+ try {
139
+ process.kill(previous.pid, 0);
140
+ } catch (error) {
141
+ if (hasCode(error, "ESRCH")) alive = false;
142
+ else throw error;
143
+ }
144
+ if (alive)
145
+ throw new Error(
146
+ "This shared context session is already open; close its other live owner first",
147
+ );
148
+ }
149
+ } catch (error) {
150
+ if (!hasCode(error, "ENOENT")) throw error;
151
+ }
152
+ const token = randomBytes(32).toString("hex");
153
+ const sockets = new Set();
154
+ const server = createServer((socket) => {
155
+ sockets.add(socket);
156
+ const controller = new AbortController();
157
+ let buffer = "";
158
+ let received = false;
159
+ socket.setEncoding("utf8");
160
+ socket.setTimeout(TIMEOUT, () => socket.destroy());
161
+ socket.on("error", () => socket.destroy());
162
+ socket.on("close", () => {
163
+ sockets.delete(socket);
164
+ controller.abort();
165
+ });
166
+ socket.on("data", (chunk) => {
167
+ if (received) return;
168
+ buffer += chunk;
169
+ if (Buffer.byteLength(buffer) > MAX_BYTES) {
170
+ socket.destroy();
171
+ return;
172
+ }
173
+ const newline = buffer.indexOf("\n");
174
+ if (newline < 0) return;
175
+ received = true;
176
+ let value;
177
+ try {
178
+ value = JSON.parse(buffer.slice(0, newline));
179
+ } catch {
180
+ socket.destroy();
181
+ return;
182
+ }
183
+ if (
184
+ value?.protocol !== 1 ||
185
+ value.token !== token ||
186
+ typeof value.id !== "string"
187
+ ) {
188
+ socket.destroy();
189
+ return;
190
+ }
191
+ /** @param {object} data */
192
+ const reply = (data) => {
193
+ const frame = `${JSON.stringify({ protocol: 1, id: value.id, ...data })}\n`;
194
+ if (Buffer.byteLength(frame) > MAX_BYTES)
195
+ socket.end(
196
+ `${JSON.stringify({ protocol: 1, id: value.id, ok: false, error: "Shared context response is too large" })}\n`,
197
+ );
198
+ else socket.end(frame);
199
+ };
200
+ void Promise.resolve()
201
+ .then(() => handle(value.request, controller.signal))
202
+ .then(
203
+ (result) => reply({ ok: true, result }),
204
+ (error) =>
205
+ reply({
206
+ ok: false,
207
+ error: error instanceof Error ? error.message : String(error),
208
+ }),
209
+ );
210
+ });
211
+ });
212
+ server.maxConnections = 16;
213
+ const temporary = `${path}.${token}.tmp`;
214
+ let closed = false;
215
+ const close = async () => {
216
+ if (closed) return;
217
+ closed = true;
218
+ for (const socket of sockets) socket.destroy();
219
+ await new Promise((resolve) => server.close(resolve));
220
+ try {
221
+ if (JSON.parse(await readFile(path, "utf8")).token === token)
222
+ await unlink(path);
223
+ } catch (error) {
224
+ if (!hasCode(error, "ENOENT")) throw error;
225
+ }
226
+ };
227
+ try {
228
+ await new Promise((resolve, reject) => {
229
+ server.once("error", reject);
230
+ server.listen(0, "127.0.0.1", () => {
231
+ server.removeListener("error", reject);
232
+ resolve(undefined);
233
+ });
234
+ });
235
+ const address = server.address();
236
+ if (!address || typeof address === "string")
237
+ throw new Error("Shared context receiver has no address");
238
+ await writeFile(
239
+ temporary,
240
+ JSON.stringify({
241
+ protocol: 1,
242
+ port: address.port,
243
+ token,
244
+ pid: process.pid,
245
+ }),
246
+ { flag: "wx", mode: 0o600 },
247
+ );
248
+ await rename(temporary, path);
249
+ server.on("error", () => {
250
+ for (const socket of sockets) socket.destroy();
251
+ });
252
+ return close;
253
+ } catch (error) {
254
+ await unlink(temporary).catch((failure) => {
255
+ if (failure.code !== "ENOENT") throw failure;
256
+ });
257
+ await close();
258
+ throw error;
259
+ }
260
+ }
@@ -31,7 +31,7 @@ function isMissing(error) {
31
31
  }
32
32
 
33
33
  /** @param {string} path @returns {Promise<string>} */
34
- async function readReceiver(path) {
34
+ export async function readReceiver(path) {
35
35
  try {
36
36
  return await readFile(path, "utf8");
37
37
  } catch (error) {
@@ -3,12 +3,57 @@ import { readFile, stat } from "node:fs/promises";
3
3
  import { createConnection } from "node:net";
4
4
  import { homedir } from "node:os";
5
5
  import { join, resolve } from "node:path";
6
+ import {
7
+ listenContext,
8
+ relayContextPath,
9
+ requestContext,
10
+ } from "./shepherdr-context.mjs";
6
11
  import { sendPeerMessage } from "./shepherdr-peer.mjs";
7
12
  import { readSessionView } from "./shepherdr-session.mjs";
8
13
 
9
- const BRIDGE_VERSION = 7;
14
+ const BRIDGE_VERSION = 11;
10
15
  const MAX_FRAME_BYTES = 8 * 1024 * 1024;
11
16
  const subscriptions = new Map();
17
+ const contextRelays = new Map();
18
+ const contextRequests = new Map();
19
+ const contextCalls = new Map();
20
+
21
+ function requestController(request, signal) {
22
+ signal.throwIfAborted();
23
+ const id = crypto.randomUUID();
24
+ return new Promise((resolve, reject) => {
25
+ const finish = (error, result) => {
26
+ if (!contextRequests.delete(id)) return;
27
+ clearTimeout(timer);
28
+ signal.removeEventListener("abort", abort);
29
+ if (error) reject(error);
30
+ else resolve(result);
31
+ };
32
+ const abort = () => {
33
+ send({ type: "context_cancel", id });
34
+ finish(
35
+ new Error(
36
+ "Shared context request cancelled; a write may have completed. Reread before retrying",
37
+ ),
38
+ );
39
+ };
40
+ const timer = setTimeout(() => {
41
+ send({ type: "context_cancel", id });
42
+ finish(
43
+ new Error(
44
+ "Shared context controller did not reply; a write may have completed. Reread before retrying",
45
+ ),
46
+ );
47
+ }, 35_000);
48
+ timer.unref();
49
+ contextRequests.set(id, {
50
+ resolve: (result) => finish(undefined, result),
51
+ reject: (error) => finish(error),
52
+ });
53
+ signal.addEventListener("abort", abort, { once: true });
54
+ send({ type: "context_request", id, request });
55
+ });
56
+ }
12
57
 
13
58
  function argument(name) {
14
59
  const index = process.argv.indexOf(`--${name}`);
@@ -276,6 +321,25 @@ async function handle(message) {
276
321
  if (message.op === "message") {
277
322
  return sendPeerMessage(request, message.agent, message.message);
278
323
  }
324
+ if (message.op === "context") {
325
+ const controller = new AbortController();
326
+ contextCalls.set(message.id, controller);
327
+ try {
328
+ return await requestContext(
329
+ message.path,
330
+ message.request,
331
+ controller.signal,
332
+ );
333
+ } finally {
334
+ contextCalls.delete(message.id);
335
+ }
336
+ }
337
+ if (message.op === "context_relay") {
338
+ const path = relayContextPath(message.threadId, message.routeId);
339
+ if (!contextRelays.has(path))
340
+ contextRelays.set(path, await listenContext(path, requestController));
341
+ return path;
342
+ }
279
343
  if (message.op === "subscribe") {
280
344
  await subscribe(message.id, message.subscriptions ?? []);
281
345
  return { subscribed: true };
@@ -313,6 +377,25 @@ process.stdin.on("data", (chunk) => {
313
377
  send({ id: "invalid", ok: false, error: serializableError(error) });
314
378
  continue;
315
379
  }
380
+ if (message?.op === "context_cancel") {
381
+ contextCalls.get(message.id)?.abort();
382
+ continue;
383
+ }
384
+ if (message?.op === "context_reply") {
385
+ const pending = contextRequests.get(message.id);
386
+ if (pending) {
387
+ if (message.ok === true) pending.resolve(message.result);
388
+ else
389
+ pending.reject(
390
+ new Error(
391
+ typeof message.error === "string"
392
+ ? message.error
393
+ : "Shared context controller failed",
394
+ ),
395
+ );
396
+ }
397
+ continue;
398
+ }
316
399
  void handle(message).then(
317
400
  (result) => send({ id: message.id, ok: true, result }),
318
401
  (error) =>
@@ -321,11 +404,18 @@ process.stdin.on("data", (chunk) => {
321
404
  }
322
405
  });
323
406
 
324
- process.stdin.on("end", () => process.exit());
407
+ async function shutdown() {
408
+ for (const close of subscriptions.values()) close();
409
+ for (const controller of contextCalls.values()) controller.abort();
410
+ await Promise.allSettled([...contextRelays.values()].map((close) => close()));
411
+ process.exit();
412
+ }
413
+ process.stdin.on("end", () => {
414
+ void shutdown();
415
+ });
325
416
  for (const signal of ["SIGINT", "SIGTERM"]) {
326
417
  process.on(signal, () => {
327
- for (const close of subscriptions.values()) close();
328
- process.exit();
418
+ void shutdown();
329
419
  });
330
420
  }
331
421