@howaboua/pi-shepherdr 0.2.5 → 0.2.7
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/CHANGELOG.md +20 -0
- package/README.md +14 -2
- package/index.ts +3 -1
- package/package.json +6 -6
- package/src/agents-answer.ts +108 -0
- package/src/agents-contract.ts +18 -1
- package/src/agents-discovery.ts +211 -0
- package/src/agents-spawn.ts +179 -0
- package/src/agents-tool.ts +24 -202
- package/src/agents-work.ts +194 -0
- package/src/ask-answer.ts +54 -2
- package/src/delivery.ts +14 -3
- package/src/fleet.ts +66 -10
- package/src/herdr-client.ts +17 -0
- package/src/launch.ts +19 -8
- package/src/messages.ts +39 -7
- package/src/monitor.ts +2 -1
- package/src/peer-inbox.ts +10 -0
- package/src/remote/shepherdr-context.mjs +260 -0
- package/src/remote/shepherdr-peer.mjs +1 -1
- package/src/remote/shepherdr-session.mjs +294 -0
- package/src/remote/shepherdr.mjs +98 -236
- package/src/remote-client.ts +126 -17
- package/src/session-reader.ts +58 -0
- package/src/settlement.ts +19 -1
- package/src/shared-context.ts +283 -0
- package/src/types.ts +6 -0
- package/src/agents-operations.ts +0 -413
package/src/remote-client.ts
CHANGED
|
@@ -14,9 +14,11 @@ import type {
|
|
|
14
14
|
SessionView,
|
|
15
15
|
} from "./types.js";
|
|
16
16
|
|
|
17
|
-
const BRIDGE_VERSION =
|
|
17
|
+
const BRIDGE_VERSION = 11;
|
|
18
18
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
19
19
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
20
|
+
const REMOTE_SESSION_HELPER = "~/.pi/agent/shepherdr-session.mjs";
|
|
21
|
+
const REMOTE_CONTEXT_HELPER = "~/.pi/agent/shepherdr-context.mjs";
|
|
20
22
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
21
23
|
const MAX_DIAGNOSTIC_BYTES = 8 * 1024;
|
|
22
24
|
const DEPLOY_TIMEOUT_MS = 20_000;
|
|
@@ -58,6 +60,7 @@ interface PendingCall {
|
|
|
58
60
|
reject: (error: Error) => void;
|
|
59
61
|
resolve: (value: unknown) => void;
|
|
60
62
|
timer: NodeJS.Timeout;
|
|
63
|
+
cleanup?: () => void;
|
|
61
64
|
}
|
|
62
65
|
|
|
63
66
|
interface SubscriptionCallbacks {
|
|
@@ -159,6 +162,11 @@ async function deploy(
|
|
|
159
162
|
}
|
|
160
163
|
|
|
161
164
|
export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
165
|
+
private contextHandler:
|
|
166
|
+
| ((request: unknown, signal: AbortSignal) => Promise<unknown>)
|
|
167
|
+
| undefined;
|
|
168
|
+
private readonly incomingContext = new Map<string, AbortController>();
|
|
169
|
+
private relayPath: string | undefined;
|
|
162
170
|
private readonly child: ChildProcessWithoutNullStreams;
|
|
163
171
|
private closed = false;
|
|
164
172
|
private diagnostics = "";
|
|
@@ -193,15 +201,30 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
193
201
|
config: SshMachine,
|
|
194
202
|
onClose: (error: Error) => void,
|
|
195
203
|
): Promise<RemoteHerdrClient> {
|
|
196
|
-
const [source, peerSource] =
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
+
const [source, peerSource, sessionSource, contextSource] =
|
|
205
|
+
await Promise.all([
|
|
206
|
+
readFile(
|
|
207
|
+
fileURLToPath(new URL("./remote/shepherdr.mjs", import.meta.url)),
|
|
208
|
+
),
|
|
209
|
+
readFile(
|
|
210
|
+
fileURLToPath(
|
|
211
|
+
new URL("./remote/shepherdr-peer.mjs", import.meta.url),
|
|
212
|
+
),
|
|
213
|
+
),
|
|
214
|
+
readFile(
|
|
215
|
+
fileURLToPath(
|
|
216
|
+
new URL("./remote/shepherdr-session.mjs", import.meta.url),
|
|
217
|
+
),
|
|
218
|
+
),
|
|
219
|
+
readFile(
|
|
220
|
+
fileURLToPath(
|
|
221
|
+
new URL("./remote/shepherdr-context.mjs", import.meta.url),
|
|
222
|
+
),
|
|
223
|
+
),
|
|
224
|
+
]);
|
|
204
225
|
await deploy(config, peerSource, REMOTE_PEER_HELPER);
|
|
226
|
+
await deploy(config, sessionSource, REMOTE_SESSION_HELPER);
|
|
227
|
+
await deploy(config, contextSource, REMOTE_CONTEXT_HELPER);
|
|
205
228
|
await deploy(config, source, REMOTE_HELPER);
|
|
206
229
|
const child = spawnConnector(config, remoteCommand(config));
|
|
207
230
|
const client = new RemoteHerdrClient(child, onClose);
|
|
@@ -225,6 +248,35 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
225
248
|
)) as T;
|
|
226
249
|
}
|
|
227
250
|
|
|
251
|
+
async requestContext(
|
|
252
|
+
path: string,
|
|
253
|
+
request: unknown,
|
|
254
|
+
signal?: AbortSignal,
|
|
255
|
+
): Promise<unknown> {
|
|
256
|
+
signal?.throwIfAborted();
|
|
257
|
+
return this.call({ op: "context", path, request }, 36_000, signal);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
async startContextRelay(
|
|
261
|
+
threadId: string,
|
|
262
|
+
routeId: string,
|
|
263
|
+
handler: (request: unknown, signal: AbortSignal) => Promise<unknown>,
|
|
264
|
+
): Promise<void> {
|
|
265
|
+
this.contextHandler = handler;
|
|
266
|
+
const path = await this.call({ op: "context_relay", threadId, routeId });
|
|
267
|
+
if (typeof path !== "string")
|
|
268
|
+
throw new Error("Invalid shared context relay path");
|
|
269
|
+
this.relayPath = path;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
contextRelayPath(): string {
|
|
273
|
+
if (!this.relayPath)
|
|
274
|
+
throw new Error(
|
|
275
|
+
"Shared context relay is unavailable; run /herdr connect to retry",
|
|
276
|
+
);
|
|
277
|
+
return this.relayPath;
|
|
278
|
+
}
|
|
279
|
+
|
|
228
280
|
async sendMessage(
|
|
229
281
|
agent: PaneInfo,
|
|
230
282
|
message: PeerMessage,
|
|
@@ -322,7 +374,9 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
322
374
|
private call(
|
|
323
375
|
message: Record<string, unknown>,
|
|
324
376
|
timeoutMs = 10_000,
|
|
377
|
+
signal?: AbortSignal,
|
|
325
378
|
): Promise<unknown> {
|
|
379
|
+
signal?.throwIfAborted();
|
|
326
380
|
if (this.closed)
|
|
327
381
|
return Promise.reject(
|
|
328
382
|
new Error("remote Shepherdr bridge is unavailable"),
|
|
@@ -330,21 +384,37 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
330
384
|
const id = typeof message["id"] === "string" ? message["id"] : randomUUID();
|
|
331
385
|
return new Promise((resolve, reject) => {
|
|
332
386
|
const timer = setTimeout(() => {
|
|
333
|
-
|
|
334
|
-
|
|
387
|
+
const error = new Error(
|
|
388
|
+
`remote Shepherdr ${String(message["op"])} timed out`,
|
|
335
389
|
);
|
|
390
|
+
if (message["op"] === "context_relay") this.rejectPending(id, error);
|
|
391
|
+
else this.disconnected(error);
|
|
336
392
|
}, timeoutMs);
|
|
337
393
|
timer.unref();
|
|
338
|
-
|
|
394
|
+
const abort = () => {
|
|
395
|
+
if (!this.pending.has(id)) return;
|
|
396
|
+
this.rejectPending(
|
|
397
|
+
id,
|
|
398
|
+
new Error(
|
|
399
|
+
"Shared context request cancelled; a write may have completed. Reread the note before retrying",
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
this.child.stdin.write(
|
|
403
|
+
`${JSON.stringify({ op: "context_cancel", id })}\n`,
|
|
404
|
+
);
|
|
405
|
+
};
|
|
406
|
+
this.pending.set(id, {
|
|
407
|
+
resolve,
|
|
408
|
+
reject,
|
|
409
|
+
timer,
|
|
410
|
+
cleanup: () => signal?.removeEventListener("abort", abort),
|
|
411
|
+
});
|
|
412
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
339
413
|
this.child.stdin.write(
|
|
340
414
|
`${JSON.stringify({ ...message, id })}\n`,
|
|
341
415
|
(error) => {
|
|
342
416
|
if (!error) return;
|
|
343
|
-
|
|
344
|
-
if (!pending) return;
|
|
345
|
-
this.pending.delete(id);
|
|
346
|
-
clearTimeout(pending.timer);
|
|
347
|
-
pending.reject(error);
|
|
417
|
+
this.rejectPending(id, error);
|
|
348
418
|
},
|
|
349
419
|
);
|
|
350
420
|
});
|
|
@@ -400,6 +470,40 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
400
470
|
}
|
|
401
471
|
const id = value["id"];
|
|
402
472
|
if (typeof id !== "string") continue;
|
|
473
|
+
if (value["type"] === "context_cancel") {
|
|
474
|
+
this.incomingContext.get(id)?.abort();
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
if (value["type"] === "context_request") {
|
|
478
|
+
const controller = new AbortController();
|
|
479
|
+
this.incomingContext.set(id, controller);
|
|
480
|
+
const reply = (data: object) => {
|
|
481
|
+
if (this.closed || controller.signal.aborted) return;
|
|
482
|
+
let frame = `${JSON.stringify({ op: "context_reply", id, ...data })}\n`;
|
|
483
|
+
if (Buffer.byteLength(frame) > MAX_FRAME_BYTES)
|
|
484
|
+
frame = `${JSON.stringify({ op: "context_reply", id, ok: false, error: "Shared context response is too large" })}\n`;
|
|
485
|
+
this.child.stdin.write(frame);
|
|
486
|
+
};
|
|
487
|
+
void Promise.resolve()
|
|
488
|
+
.then(() => {
|
|
489
|
+
if (!this.contextHandler)
|
|
490
|
+
throw new Error("Shared context controller is unavailable");
|
|
491
|
+
controller.signal.throwIfAborted();
|
|
492
|
+
return this.contextHandler(value["request"], controller.signal);
|
|
493
|
+
})
|
|
494
|
+
.then(
|
|
495
|
+
(result) => reply({ ok: true, result }),
|
|
496
|
+
(error: unknown) =>
|
|
497
|
+
reply({
|
|
498
|
+
ok: false,
|
|
499
|
+
error: error instanceof Error ? error.message : String(error),
|
|
500
|
+
}),
|
|
501
|
+
)
|
|
502
|
+
.finally(() => {
|
|
503
|
+
this.incomingContext.delete(id);
|
|
504
|
+
});
|
|
505
|
+
continue;
|
|
506
|
+
}
|
|
403
507
|
if (value["ok"] === true) this.resolvePending(id, value["result"]);
|
|
404
508
|
else {
|
|
405
509
|
const detail = value["error"] as BridgeError | undefined;
|
|
@@ -421,6 +525,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
421
525
|
if (!pending) return;
|
|
422
526
|
this.pending.delete(id);
|
|
423
527
|
clearTimeout(pending.timer);
|
|
528
|
+
pending.cleanup?.();
|
|
424
529
|
pending.resolve(value);
|
|
425
530
|
}
|
|
426
531
|
|
|
@@ -429,6 +534,7 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
429
534
|
if (!pending) return;
|
|
430
535
|
this.pending.delete(id);
|
|
431
536
|
clearTimeout(pending.timer);
|
|
537
|
+
pending.cleanup?.();
|
|
432
538
|
pending.reject(error);
|
|
433
539
|
}
|
|
434
540
|
|
|
@@ -440,8 +546,11 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
440
546
|
}
|
|
441
547
|
|
|
442
548
|
private failPending(error: Error, notify: boolean): void {
|
|
549
|
+
for (const controller of this.incomingContext.values()) controller.abort();
|
|
550
|
+
this.incomingContext.clear();
|
|
443
551
|
for (const pending of this.pending.values()) {
|
|
444
552
|
clearTimeout(pending.timer);
|
|
553
|
+
pending.cleanup?.();
|
|
445
554
|
pending.reject(error);
|
|
446
555
|
}
|
|
447
556
|
this.pending.clear();
|
package/src/session-reader.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { open, stat } from "node:fs/promises";
|
|
2
2
|
import type {
|
|
3
|
+
AskResult,
|
|
3
4
|
LatestAssistant,
|
|
4
5
|
LatestInput,
|
|
5
6
|
PendingAsk,
|
|
@@ -131,6 +132,57 @@ function resolvedToolCallId(
|
|
|
131
132
|
return typeof value === "string" ? value : undefined;
|
|
132
133
|
}
|
|
133
134
|
|
|
135
|
+
function askResult(
|
|
136
|
+
message: Record<string, unknown>,
|
|
137
|
+
): [string, AskResult] | undefined {
|
|
138
|
+
if (message["role"] !== "toolResult" || message["toolName"] !== "ask") {
|
|
139
|
+
return undefined;
|
|
140
|
+
}
|
|
141
|
+
const id = resolvedToolCallId(message);
|
|
142
|
+
if (!id) return undefined;
|
|
143
|
+
if (message["isError"] === true) return [id, { status: "rejected" }];
|
|
144
|
+
if (message["isError"] !== false) return [id, { status: "unknown" }];
|
|
145
|
+
const details = record(message["details"]);
|
|
146
|
+
if (details?.["dismissed"] === true) return [id, { status: "rejected" }];
|
|
147
|
+
const rawResponses = details?.["responses"];
|
|
148
|
+
const rawResponseCount = Array.isArray(rawResponses)
|
|
149
|
+
? rawResponses.length
|
|
150
|
+
: -1;
|
|
151
|
+
const responses = Array.isArray(rawResponses)
|
|
152
|
+
? rawResponses
|
|
153
|
+
.map((value) => {
|
|
154
|
+
const response = record(value);
|
|
155
|
+
if (
|
|
156
|
+
!response ||
|
|
157
|
+
typeof response["id"] !== "string" ||
|
|
158
|
+
!Array.isArray(response["selections"]) ||
|
|
159
|
+
!response["selections"].every(
|
|
160
|
+
(selection) => typeof selection === "string",
|
|
161
|
+
)
|
|
162
|
+
) {
|
|
163
|
+
return undefined;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
id: response["id"],
|
|
167
|
+
selections: response["selections"] as string[],
|
|
168
|
+
...(typeof response["comment"] === "string"
|
|
169
|
+
? { comment: response["comment"] }
|
|
170
|
+
: {}),
|
|
171
|
+
};
|
|
172
|
+
})
|
|
173
|
+
.filter((response) => response !== undefined)
|
|
174
|
+
: undefined;
|
|
175
|
+
return [
|
|
176
|
+
id,
|
|
177
|
+
{
|
|
178
|
+
status: "accepted",
|
|
179
|
+
...(responses && responses.length === rawResponseCount
|
|
180
|
+
? { responses }
|
|
181
|
+
: {}),
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
}
|
|
185
|
+
|
|
134
186
|
async function readSessionView(
|
|
135
187
|
path: string,
|
|
136
188
|
size: number,
|
|
@@ -140,6 +192,7 @@ async function readSessionView(
|
|
|
140
192
|
let assistant: LatestAssistant | undefined;
|
|
141
193
|
let assistantDepth: number | undefined;
|
|
142
194
|
let ask: PendingAsk | undefined;
|
|
195
|
+
const askResults = new Map<string, AskResult>();
|
|
143
196
|
let depth = 0;
|
|
144
197
|
let input: LatestInput | undefined;
|
|
145
198
|
let inputDepth: number | undefined;
|
|
@@ -147,6 +200,9 @@ async function readSessionView(
|
|
|
147
200
|
const result = (): SessionView => ({
|
|
148
201
|
...(assistant ? { assistant } : {}),
|
|
149
202
|
...(ask ? { ask } : {}),
|
|
203
|
+
...(askResults.size > 0
|
|
204
|
+
? { askResults: Object.fromEntries(askResults) }
|
|
205
|
+
: {}),
|
|
150
206
|
...(input ? { input } : {}),
|
|
151
207
|
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
152
208
|
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
@@ -192,6 +248,8 @@ async function readSessionView(
|
|
|
192
248
|
}
|
|
193
249
|
const message = record(entry["message"]);
|
|
194
250
|
if (message) {
|
|
251
|
+
const result = askResult(message);
|
|
252
|
+
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
195
253
|
const resolvedId = resolvedToolCallId(message);
|
|
196
254
|
if (resolvedId) resolved.add(resolvedId);
|
|
197
255
|
if (!assistant) {
|
package/src/settlement.ts
CHANGED
|
@@ -45,6 +45,7 @@ export interface ClaimedSettlement extends CollectedSettlement {
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
interface SettlementClaim {
|
|
48
|
+
answeringAskId?: string;
|
|
48
49
|
cleanup(): void;
|
|
49
50
|
reject(error: Error): void;
|
|
50
51
|
resolve(settlement: ClaimedSettlement): void;
|
|
@@ -225,7 +226,11 @@ export class SettlementReporter {
|
|
|
225
226
|
this.retries.clear();
|
|
226
227
|
}
|
|
227
228
|
|
|
228
|
-
claim(
|
|
229
|
+
claim(
|
|
230
|
+
attempt: WorkAttempt,
|
|
231
|
+
signal: AbortSignal,
|
|
232
|
+
answeringAskId?: string,
|
|
233
|
+
): Promise<ClaimedSettlement> {
|
|
229
234
|
signal.throwIfAborted();
|
|
230
235
|
if (this.claims.has(attempt.attemptId)) {
|
|
231
236
|
throw new Error(`work attempt ${attempt.attemptId} is already claimed`);
|
|
@@ -243,6 +248,7 @@ export class SettlementReporter {
|
|
|
243
248
|
);
|
|
244
249
|
};
|
|
245
250
|
const claim: SettlementClaim = {
|
|
251
|
+
...(answeringAskId ? { answeringAskId } : {}),
|
|
246
252
|
cleanup: () => signal.removeEventListener("abort", abort),
|
|
247
253
|
reject,
|
|
248
254
|
resolve,
|
|
@@ -324,6 +330,18 @@ export class SettlementReporter {
|
|
|
324
330
|
const retryRequest = task ? { ...request, task } : request;
|
|
325
331
|
const expectedUser = activityExpectedUser(current.activity);
|
|
326
332
|
const blocked = settlement.status === "blocked";
|
|
333
|
+
const answeringAskId = retryState.attemptId
|
|
334
|
+
? this.claims.get(retryState.attemptId)?.answeringAskId
|
|
335
|
+
: undefined;
|
|
336
|
+
// The original Ask's blocked snapshot is not settlement of its answer.
|
|
337
|
+
if (
|
|
338
|
+
blocked &&
|
|
339
|
+
answeringAskId &&
|
|
340
|
+
settlement.ask?.toolCallId === answeringAskId &&
|
|
341
|
+
!settlement.session.askResults?.[answeringAskId]
|
|
342
|
+
) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
327
345
|
// A blocked status is authoritative even when Pi has not persisted the turn.
|
|
328
346
|
// Pi persists expanded skills/templates, not the submitted command text.
|
|
329
347
|
if (
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type {
|
|
6
|
+
ContextSharingService,
|
|
7
|
+
SharedContextRequest,
|
|
8
|
+
SharedContextResult,
|
|
9
|
+
} from "@howaboua/pi-codex-conversion/context-sharing";
|
|
10
|
+
import type { AgentFleet, ConnectedMachine } from "./fleet.js";
|
|
11
|
+
import { sessionPath } from "./herdr.js";
|
|
12
|
+
import {
|
|
13
|
+
listenContext,
|
|
14
|
+
requestContext,
|
|
15
|
+
sessionContextPath,
|
|
16
|
+
} from "./remote/shepherdr-context.mjs";
|
|
17
|
+
import type { PaneInfo } from "./types.js";
|
|
18
|
+
|
|
19
|
+
const MEMBER_ENTRY = "herdr-context-child";
|
|
20
|
+
interface Member {
|
|
21
|
+
agentName: string;
|
|
22
|
+
sessionId: string;
|
|
23
|
+
threadId: string;
|
|
24
|
+
machine: string;
|
|
25
|
+
sessionFile: string;
|
|
26
|
+
}
|
|
27
|
+
interface Routing {
|
|
28
|
+
transport: "shepherdr";
|
|
29
|
+
parent: string;
|
|
30
|
+
}
|
|
31
|
+
interface ContextEnvelope {
|
|
32
|
+
operation: "context";
|
|
33
|
+
request: SharedContextRequest;
|
|
34
|
+
visited: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export class SharedAgentContext {
|
|
38
|
+
private readonly pi: ExtensionAPI;
|
|
39
|
+
private readonly fleet: AgentFleet;
|
|
40
|
+
private readonly getService: () => ContextSharingService | undefined;
|
|
41
|
+
private running = false;
|
|
42
|
+
|
|
43
|
+
constructor(
|
|
44
|
+
pi: ExtensionAPI,
|
|
45
|
+
fleet: AgentFleet,
|
|
46
|
+
getService: () => ContextSharingService | undefined,
|
|
47
|
+
) {
|
|
48
|
+
this.pi = pi;
|
|
49
|
+
this.fleet = fleet;
|
|
50
|
+
this.getService = getService;
|
|
51
|
+
pi.on("agent_start", () => {
|
|
52
|
+
this.running = true;
|
|
53
|
+
});
|
|
54
|
+
pi.on("agent_settled", () => {
|
|
55
|
+
this.running = false;
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async handle(
|
|
60
|
+
ctx: ExtensionContext,
|
|
61
|
+
value: unknown,
|
|
62
|
+
signal?: AbortSignal,
|
|
63
|
+
): Promise<unknown> {
|
|
64
|
+
if (!value || typeof value !== "object")
|
|
65
|
+
throw new Error("Invalid shared context request");
|
|
66
|
+
const service = this.getService();
|
|
67
|
+
const identity = service?.describe(ctx);
|
|
68
|
+
if ("operation" in value && value.operation === "bind") {
|
|
69
|
+
if (!service || !identity) return null;
|
|
70
|
+
return service.bind(ctx, "binding" in value ? value.binding : undefined);
|
|
71
|
+
}
|
|
72
|
+
if (!identity || !service)
|
|
73
|
+
throw new Error("Context sharing is unavailable in this session");
|
|
74
|
+
const envelope = value as Partial<ContextEnvelope>;
|
|
75
|
+
const request = envelope.request;
|
|
76
|
+
if (
|
|
77
|
+
envelope.operation !== "context" ||
|
|
78
|
+
!request ||
|
|
79
|
+
request.sessionId !== identity.sessionId ||
|
|
80
|
+
typeof request.agentName !== "string" ||
|
|
81
|
+
!Array.isArray(envelope.visited) ||
|
|
82
|
+
envelope.visited.some((item) => typeof item !== "string") ||
|
|
83
|
+
envelope.visited.length >= 64 ||
|
|
84
|
+
envelope.visited.includes(identity.threadId)
|
|
85
|
+
)
|
|
86
|
+
throw new Error("Invalid shared context route or session family");
|
|
87
|
+
if (identity.agentName === request.agentName) {
|
|
88
|
+
if (!ctx.isIdle() && !this.running)
|
|
89
|
+
throw new Error(
|
|
90
|
+
"Context owner is changing branches; retry after it settles",
|
|
91
|
+
);
|
|
92
|
+
return service.execute(ctx, request, signal);
|
|
93
|
+
}
|
|
94
|
+
const forwarded = {
|
|
95
|
+
...envelope,
|
|
96
|
+
visited: [...envelope.visited, identity.threadId],
|
|
97
|
+
};
|
|
98
|
+
const member = ctx.sessionManager.getEntries().findLast((entry) => {
|
|
99
|
+
if (entry.type !== "custom" || entry.customType !== MEMBER_ENTRY)
|
|
100
|
+
return false;
|
|
101
|
+
const candidate = entry.data as Member;
|
|
102
|
+
return (
|
|
103
|
+
candidate?.threadId === identity.threadId &&
|
|
104
|
+
candidate.sessionId === identity.sessionId &&
|
|
105
|
+
typeof candidate.agentName === "string" &&
|
|
106
|
+
(request.agentName === candidate.agentName ||
|
|
107
|
+
request.agentName.startsWith(`${candidate.agentName}/`))
|
|
108
|
+
);
|
|
109
|
+
});
|
|
110
|
+
if (member?.type === "custom") {
|
|
111
|
+
const child = member.data as Member;
|
|
112
|
+
if (
|
|
113
|
+
typeof child.machine !== "string" ||
|
|
114
|
+
typeof child.sessionFile !== "string" ||
|
|
115
|
+
!child.sessionFile
|
|
116
|
+
)
|
|
117
|
+
throw new Error("Invalid saved shared context route");
|
|
118
|
+
const runtime = this.fleet.connected(child.machine);
|
|
119
|
+
return runtime.client.requestContext(
|
|
120
|
+
sessionContextPath(child.sessionFile),
|
|
121
|
+
forwarded,
|
|
122
|
+
signal,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
const routing = identity.routing as Partial<Routing> | undefined;
|
|
126
|
+
if (
|
|
127
|
+
routing?.transport === "shepherdr" &&
|
|
128
|
+
typeof routing.parent === "string"
|
|
129
|
+
)
|
|
130
|
+
return requestContext(routing.parent, forwarded, signal);
|
|
131
|
+
throw new Error(
|
|
132
|
+
`No shared context agent ${request.agentName} in this session family`,
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async prepare(
|
|
137
|
+
ctx: ExtensionContext,
|
|
138
|
+
runtime: ConnectedMachine,
|
|
139
|
+
name: string,
|
|
140
|
+
args: readonly string[] = [],
|
|
141
|
+
) {
|
|
142
|
+
const service = this.getService();
|
|
143
|
+
if (!service?.canCreateChild(ctx)) return undefined;
|
|
144
|
+
const parent = service.describe(ctx);
|
|
145
|
+
if (!parent) return undefined;
|
|
146
|
+
if (
|
|
147
|
+
args.some((arg) =>
|
|
148
|
+
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
149
|
+
arg,
|
|
150
|
+
),
|
|
151
|
+
)
|
|
152
|
+
)
|
|
153
|
+
throw new Error(
|
|
154
|
+
"Shared context spawn cannot use profile session-selection arguments; use assign for existing agents",
|
|
155
|
+
);
|
|
156
|
+
const parentFile = ctx.sessionManager.getSessionFile();
|
|
157
|
+
if (!parentFile)
|
|
158
|
+
throw new Error(
|
|
159
|
+
"Shared context spawn requires a saved controller session",
|
|
160
|
+
);
|
|
161
|
+
const routing: Routing | undefined =
|
|
162
|
+
parent.storage === "session"
|
|
163
|
+
? {
|
|
164
|
+
transport: "shepherdr",
|
|
165
|
+
parent: runtime.local
|
|
166
|
+
? sessionContextPath(parentFile)
|
|
167
|
+
: runtime.client.contextRelayPath(),
|
|
168
|
+
}
|
|
169
|
+
: undefined;
|
|
170
|
+
const { binding, adopt } = await service.createChild(ctx, {
|
|
171
|
+
name,
|
|
172
|
+
...(routing ? { routing } : {}),
|
|
173
|
+
});
|
|
174
|
+
return {
|
|
175
|
+
accept: async (agent: PaneInfo) => {
|
|
176
|
+
const sessionFile = sessionPath(agent);
|
|
177
|
+
if (!sessionFile)
|
|
178
|
+
throw new Error("Spawned agent has no native Pi session path");
|
|
179
|
+
const value = await runtime.client.requestContext(
|
|
180
|
+
sessionContextPath(sessionFile),
|
|
181
|
+
{ operation: "bind", binding },
|
|
182
|
+
);
|
|
183
|
+
if (!value)
|
|
184
|
+
return {
|
|
185
|
+
warning:
|
|
186
|
+
"Spawned without shared context: the target needs Codex conversion with notes-based continuity",
|
|
187
|
+
};
|
|
188
|
+
if (
|
|
189
|
+
typeof value !== "object" ||
|
|
190
|
+
!("sessionId" in value) ||
|
|
191
|
+
value.sessionId !== binding.sessionId ||
|
|
192
|
+
!("agentName" in value) ||
|
|
193
|
+
value.agentName !== binding.agentName ||
|
|
194
|
+
!("storage" in value) ||
|
|
195
|
+
value.storage !== binding.storage ||
|
|
196
|
+
(binding.accountScope !== undefined &&
|
|
197
|
+
(!("accountScope" in value) ||
|
|
198
|
+
value.accountScope !== binding.accountScope))
|
|
199
|
+
)
|
|
200
|
+
throw new Error(
|
|
201
|
+
"Spawned agent did not adopt its shared context identity and storage mode",
|
|
202
|
+
);
|
|
203
|
+
if (ctx.sessionManager.getSessionId() !== parent.threadId)
|
|
204
|
+
throw new Error("Controller session changed during spawn");
|
|
205
|
+
await adopt();
|
|
206
|
+
this.pi.appendEntry<Member>(MEMBER_ENTRY, {
|
|
207
|
+
agentName: binding.agentName,
|
|
208
|
+
sessionId: parent.sessionId,
|
|
209
|
+
threadId: parent.threadId,
|
|
210
|
+
machine: runtime.machine,
|
|
211
|
+
sessionFile,
|
|
212
|
+
});
|
|
213
|
+
return { agentName: binding.agentName };
|
|
214
|
+
},
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export async function registerSharedAgentContext(
|
|
220
|
+
pi: ExtensionAPI,
|
|
221
|
+
fleet: AgentFleet,
|
|
222
|
+
): Promise<SharedAgentContext> {
|
|
223
|
+
let api:
|
|
224
|
+
| typeof import("@howaboua/pi-codex-conversion/context-sharing")
|
|
225
|
+
| undefined;
|
|
226
|
+
try {
|
|
227
|
+
api = await import("@howaboua/pi-codex-conversion/context-sharing");
|
|
228
|
+
} catch (error) {
|
|
229
|
+
const failure = error as { code?: string; message?: string };
|
|
230
|
+
const missing = failure.message?.match(
|
|
231
|
+
/Cannot find (?:package|module) ['"]([^'"]+)['"]/,
|
|
232
|
+
)?.[1];
|
|
233
|
+
const module = "@howaboua/pi-codex-conversion/context-sharing";
|
|
234
|
+
const unavailable =
|
|
235
|
+
((failure.code === "ERR_MODULE_NOT_FOUND" ||
|
|
236
|
+
failure.code === "MODULE_NOT_FOUND") &&
|
|
237
|
+
(missing === "@howaboua/pi-codex-conversion" ||
|
|
238
|
+
missing === module ||
|
|
239
|
+
missing?.endsWith(`/${module}`))) ||
|
|
240
|
+
(failure.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" &&
|
|
241
|
+
failure.message?.includes("@howaboua/pi-codex-conversion") &&
|
|
242
|
+
failure.message.includes("./context-sharing"));
|
|
243
|
+
if (!unavailable)
|
|
244
|
+
throw new Error(
|
|
245
|
+
"Update pi-codex-conversion to use shared agent context",
|
|
246
|
+
{ cause: error },
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
const connection = api?.connectCodexContextSharing(pi);
|
|
250
|
+
const shared = new SharedAgentContext(pi, fleet, () => connection?.service);
|
|
251
|
+
let close: (() => Promise<void>) | undefined;
|
|
252
|
+
let unregister: (() => void) | undefined;
|
|
253
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
254
|
+
await close?.();
|
|
255
|
+
close = undefined;
|
|
256
|
+
unregister?.();
|
|
257
|
+
unregister = connection?.service?.registerRouter(
|
|
258
|
+
async (context, request, signal) =>
|
|
259
|
+
shared.handle(
|
|
260
|
+
context,
|
|
261
|
+
{ operation: "context", request, visited: [] },
|
|
262
|
+
signal,
|
|
263
|
+
) as Promise<SharedContextResult>,
|
|
264
|
+
);
|
|
265
|
+
const file = ctx.sessionManager.getSessionFile();
|
|
266
|
+
if (file)
|
|
267
|
+
close = await listenContext(
|
|
268
|
+
sessionContextPath(file),
|
|
269
|
+
(request: unknown, signal: AbortSignal) =>
|
|
270
|
+
shared.handle(ctx, request, signal),
|
|
271
|
+
);
|
|
272
|
+
});
|
|
273
|
+
pi.on("session_shutdown", async () => {
|
|
274
|
+
unregister?.();
|
|
275
|
+
await close?.();
|
|
276
|
+
connection?.dispose();
|
|
277
|
+
});
|
|
278
|
+
if (connection)
|
|
279
|
+
fleet.setContextRelay((ctx, request, signal) =>
|
|
280
|
+
shared.handle(ctx, request, signal),
|
|
281
|
+
);
|
|
282
|
+
return shared;
|
|
283
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -127,8 +127,14 @@ export interface PendingAsk {
|
|
|
127
127
|
toolCallId: string;
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
export interface AskResult {
|
|
131
|
+
responses?: Array<{ comment?: string; id: string; selections: string[] }>;
|
|
132
|
+
status: "accepted" | "rejected" | "unknown";
|
|
133
|
+
}
|
|
134
|
+
|
|
130
135
|
export interface SessionView {
|
|
131
136
|
ask?: PendingAsk;
|
|
137
|
+
askResults?: Record<string, AskResult>;
|
|
132
138
|
assistant?: LatestAssistant;
|
|
133
139
|
assistantAfterInput?: boolean;
|
|
134
140
|
input?: LatestInput;
|