@howaboua/pi-shepherdr 0.2.24 → 0.2.25
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 +13 -0
- package/README.md +17 -9
- package/index.ts +2 -0
- package/package.json +5 -5
- package/src/agents-contract.ts +9 -6
- package/src/agents-discovery.ts +41 -15
- package/src/agents-focus.ts +15 -9
- package/src/agents-spawn.ts +22 -6
- package/src/agents-tool.ts +24 -6
- package/src/agents-work.ts +2 -2
- package/src/attachment.ts +2 -0
- package/src/board/host.ts +3 -1
- package/src/close-idle.ts +89 -0
- package/src/control-panel/index.ts +3 -1
- package/src/control-panel/items.ts +14 -2
- package/src/control-panel/view.ts +6 -2
- package/src/controller.ts +11 -2
- package/src/core/AGENTS.md +5 -0
- package/src/core/backend.ts +266 -0
- package/src/core/client.ts +249 -0
- package/src/core/context-relay.mjs +111 -0
- package/src/core/focus-channel-helper.mjs +165 -0
- package/src/core/focus-channel-stream.ts +140 -0
- package/src/core/focus-channel-wire.mjs +160 -0
- package/src/core/focus-channel.ts +213 -0
- package/src/core/focus-window.ts +192 -0
- package/src/core/pi-executable.mjs +34 -0
- package/src/core/protocol.ts +116 -0
- package/src/core/registration.ts +51 -0
- package/src/core/remote-endpoint.ts +119 -0
- package/src/core/remote-exec.ts +122 -0
- package/src/core/remote-helper.mjs +244 -0
- package/src/core/remote-reader.ts +67 -0
- package/src/core/remote-relay.ts +191 -0
- package/src/core/request.ts +124 -0
- package/src/core/spawn.ts +177 -0
- package/src/core/status.ts +68 -0
- package/src/core/terminal.ts +103 -0
- package/src/fleet/catalog.ts +83 -0
- package/src/fleet/connection.ts +111 -0
- package/src/fleet/index.ts +151 -122
- package/src/fleet/inspection.ts +19 -1
- package/src/fleet/machine.ts +3 -0
- package/src/fleet/monitor.ts +18 -4
- package/src/fleet/persistence.ts +26 -3
- package/src/fleet/setup.ts +54 -13
- package/src/herdr.ts +25 -5
- package/src/launch-contract.ts +48 -0
- package/src/launch-location.ts +151 -0
- package/src/launch.ts +30 -183
- package/src/messages/attribution.ts +15 -6
- package/src/messages/events.ts +10 -4
- package/src/messages/focus.ts +11 -2
- package/src/monitor-record.ts +9 -6
- package/src/monitor.ts +31 -0
- package/src/peer-commands.ts +11 -0
- package/src/peer-inbox-delivery.ts +171 -0
- package/src/peer-inbox-focus.ts +113 -0
- package/src/peer-inbox.ts +126 -208
- package/src/remote/deployment.ts +1 -1
- package/src/remote/shepherdr-context.mjs +1 -1
- package/src/remote/shepherdr-peer.mjs +18 -5
- package/src/remote/shepherdr-session.mjs +9 -2
- package/src/remote/shepherdr.mjs +1 -1
- package/src/settlement/collector.ts +2 -2
- package/src/settlement/index.ts +12 -0
- package/src/shared-context-host.ts +7 -3
- package/src/shared-context.ts +34 -2
- package/src/spawn-context.ts +4 -0
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +9 -5
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +12 -81
package/src/peer-inbox.ts
CHANGED
|
@@ -5,19 +5,14 @@ import type {
|
|
|
5
5
|
ExtensionAPI,
|
|
6
6
|
ExtensionContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
|
-
import {
|
|
9
|
-
import { sendPolicyMessage } from "./delivery.js";
|
|
8
|
+
import { coreIdentity, publishCoreSession } from "./core/registration.js";
|
|
10
9
|
import { dispatchExtensionReply } from "./extension-reply.js";
|
|
11
10
|
import type { AgentFleet } from "./fleet/index.js";
|
|
12
|
-
import { getCurrentPane } from "./herdr.js";
|
|
11
|
+
import { getAgent, getCurrentPane, sessionPath } from "./herdr.js";
|
|
13
12
|
import { HerdrClient } from "./herdr-client.js";
|
|
14
|
-
import { announcePeerMessage } from "./messages/voice-announcements.js";
|
|
15
|
-
import { acceptFocusChannel } from "./peer-channel.js";
|
|
16
13
|
import type { createPeerCommands } from "./peer-commands.js";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
socketChannel,
|
|
20
|
-
} from "./remote/shepherdr-channel.mjs";
|
|
14
|
+
import { PeerInboxDelivery } from "./peer-inbox-delivery.js";
|
|
15
|
+
import { receivePeerFocus } from "./peer-inbox-focus.js";
|
|
21
16
|
import {
|
|
22
17
|
MAX_PEER_FRAME_BYTES,
|
|
23
18
|
PEER_PROTOCOL,
|
|
@@ -34,12 +29,30 @@ export function registerPeerInbox(
|
|
|
34
29
|
): void {
|
|
35
30
|
let close: (() => Promise<void>) | undefined;
|
|
36
31
|
let running = false;
|
|
32
|
+
let generation = 0;
|
|
37
33
|
const publish = async (ctx: ExtensionContext) => {
|
|
38
|
-
|
|
34
|
+
const core = coreIdentity(ctx);
|
|
35
|
+
if (
|
|
36
|
+
!core &&
|
|
37
|
+
(process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"])
|
|
38
|
+
)
|
|
39
39
|
return;
|
|
40
|
-
|
|
40
|
+
if (core) {
|
|
41
|
+
await close?.();
|
|
42
|
+
close = undefined;
|
|
43
|
+
}
|
|
44
|
+
close = await openInbox(
|
|
45
|
+
pi,
|
|
46
|
+
ctx,
|
|
47
|
+
() => running,
|
|
48
|
+
() => generation,
|
|
49
|
+
commands,
|
|
50
|
+
fleet,
|
|
51
|
+
catchup,
|
|
52
|
+
);
|
|
41
53
|
};
|
|
42
54
|
pi.on("before_agent_start", () => {
|
|
55
|
+
generation += 1;
|
|
43
56
|
running = true;
|
|
44
57
|
});
|
|
45
58
|
pi.on("agent_settled", () => {
|
|
@@ -62,6 +75,7 @@ async function openInbox(
|
|
|
62
75
|
pi: ExtensionAPI,
|
|
63
76
|
ctx: ExtensionContext,
|
|
64
77
|
isRunning: () => boolean,
|
|
78
|
+
generation: () => number,
|
|
65
79
|
commands: ReturnType<typeof createPeerCommands>,
|
|
66
80
|
fleet: AgentFleet,
|
|
67
81
|
catchup: (
|
|
@@ -71,13 +85,42 @@ async function openInbox(
|
|
|
71
85
|
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
72
86
|
if (!sessionFile)
|
|
73
87
|
throw new Error("Shepherdr peer delivery requires a saved Pi session");
|
|
74
|
-
const
|
|
88
|
+
const core = coreIdentity(ctx);
|
|
89
|
+
const pane = core
|
|
90
|
+
? {
|
|
91
|
+
backend: "core" as const,
|
|
92
|
+
pane_id: core.terminalId,
|
|
93
|
+
terminal_id: core.terminalId,
|
|
94
|
+
agent_status: "unknown" as const,
|
|
95
|
+
agent_session: {
|
|
96
|
+
agent: "pi",
|
|
97
|
+
kind: "path" as const,
|
|
98
|
+
source: "pi.session_file",
|
|
99
|
+
value: sessionFile,
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
: await getCurrentPane(new HerdrClient());
|
|
103
|
+
const terminalId = pane.terminal_id;
|
|
104
|
+
if (!terminalId)
|
|
105
|
+
throw new Error("Shepherdr peer receiver has no terminal identity");
|
|
75
106
|
const token = randomBytes(32).toString("hex");
|
|
76
|
-
const path = peerInboxPath(sessionFile,
|
|
107
|
+
const path = peerInboxPath(sessionFile, terminalId);
|
|
77
108
|
const temporary = `${path}.${token}.tmp`;
|
|
78
109
|
const sockets = new Set<Socket>();
|
|
79
110
|
let state: "open" | "quitting" | "closed" = "open";
|
|
80
|
-
|
|
111
|
+
const delivery = new PeerInboxDelivery(
|
|
112
|
+
sessionFile,
|
|
113
|
+
isRunning,
|
|
114
|
+
() => state === "open",
|
|
115
|
+
(socket) => {
|
|
116
|
+
state = "quitting";
|
|
117
|
+
sockets.delete(socket);
|
|
118
|
+
socket.setTimeout(0);
|
|
119
|
+
},
|
|
120
|
+
commands,
|
|
121
|
+
catchup,
|
|
122
|
+
);
|
|
123
|
+
let clearCoreSession: (() => Promise<void>) | undefined;
|
|
81
124
|
const server = createServer((socket) => {
|
|
82
125
|
sockets.add(socket);
|
|
83
126
|
socket.setEncoding("utf8");
|
|
@@ -116,12 +159,13 @@ async function openInbox(
|
|
|
116
159
|
request["protocol"] !== PEER_PROTOCOL ||
|
|
117
160
|
request["token"] !== token ||
|
|
118
161
|
request["sessionFile"] !== sessionFile ||
|
|
119
|
-
request["terminalId"] !==
|
|
162
|
+
request["terminalId"] !== terminalId ||
|
|
120
163
|
ctx.sessionManager.getSessionFile() !== sessionFile ||
|
|
121
164
|
typeof request["id"] !== "string" ||
|
|
122
165
|
(request["kind"] !== "focus" &&
|
|
123
166
|
request["kind"] !== "commands" &&
|
|
124
167
|
request["kind"] !== "message" &&
|
|
168
|
+
request["kind"] !== "close-idle" &&
|
|
125
169
|
request["kind"] !== "channel" &&
|
|
126
170
|
request["kind"] !== "extension-reply") ||
|
|
127
171
|
(request["kind"] !== "focus" &&
|
|
@@ -190,213 +234,83 @@ async function openInbox(
|
|
|
190
234
|
}
|
|
191
235
|
return;
|
|
192
236
|
}
|
|
193
|
-
if (request["kind"] === "
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
const receiverChannel = {
|
|
206
|
-
...channel,
|
|
207
|
-
send(message: unknown) {
|
|
208
|
-
const json = JSON.stringify(message);
|
|
209
|
-
if (
|
|
210
|
-
json === undefined ||
|
|
211
|
-
Buffer.byteLength(json) > MAX_CHANNEL_FRAME_BYTES
|
|
212
|
-
) {
|
|
213
|
-
channel.close();
|
|
214
|
-
throw new Error("Focus channel frame is too large or not JSON");
|
|
215
|
-
}
|
|
216
|
-
if (admitted) channel.send(message);
|
|
217
|
-
else {
|
|
218
|
-
pending.push(message);
|
|
219
|
-
if (
|
|
220
|
-
pending.length > 64 ||
|
|
221
|
-
Buffer.byteLength(JSON.stringify(pending)) > 1024 * 1024
|
|
222
|
-
) {
|
|
223
|
-
channel.close();
|
|
224
|
-
throw new Error(
|
|
225
|
-
"Focus channel admission output exceeded its buffer",
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
void acceptFocusChannel(pi, receiverChannel, focus, ctx, pane).then(
|
|
232
|
-
() => {
|
|
233
|
-
try {
|
|
234
|
-
socket.write(
|
|
235
|
-
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true })}\n`,
|
|
236
|
-
);
|
|
237
|
-
admitted = true;
|
|
238
|
-
for (const message of pending) channel.send(message);
|
|
239
|
-
pending.length = 0;
|
|
240
|
-
} catch {
|
|
241
|
-
channel.close();
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
(error: unknown) => {
|
|
245
|
-
reply(
|
|
246
|
-
{
|
|
247
|
-
ok: false,
|
|
248
|
-
rejected: true,
|
|
249
|
-
error: error instanceof Error ? error.message : String(error),
|
|
250
|
-
},
|
|
251
|
-
() => channel.close(),
|
|
252
|
-
);
|
|
253
|
-
},
|
|
254
|
-
);
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
if (request["kind"] === "focus") {
|
|
258
|
-
try {
|
|
259
|
-
if (readFocus(request["focus"]).voice === true)
|
|
260
|
-
throw new Error(
|
|
261
|
-
"Voice transfer was not admitted; retry using agents focus with voice:true",
|
|
262
|
-
);
|
|
263
|
-
} catch (error) {
|
|
264
|
-
reply({ ok: false, rejected: true, error: String(error) });
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
socket.setTimeout(60_000, () => socket.destroy());
|
|
268
|
-
void acceptFocus(pi, ctx, fleet, pane, request["focus"]).then(
|
|
269
|
-
() => reply({ ok: true, command: false }),
|
|
270
|
-
(error: unknown) =>
|
|
271
|
-
reply({
|
|
272
|
-
ok: false,
|
|
273
|
-
rejected: !(error instanceof Error && "focusChanged" in error),
|
|
274
|
-
error: error instanceof Error ? error.message : String(error),
|
|
275
|
-
}),
|
|
276
|
-
);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
let submitted = false;
|
|
280
|
-
try {
|
|
281
|
-
const idle = ctx.isIdle();
|
|
282
|
-
// Manual compaction/tree navigation can be busy without an agent
|
|
283
|
-
// loop to consume steering. Do not acknowledge a stranded message.
|
|
284
|
-
if (!idle && !isRunning()) {
|
|
237
|
+
if (request["kind"] === "close-idle" && pane) {
|
|
238
|
+
const startGeneration = generation();
|
|
239
|
+
const idle = (live: ExtensionContext) =>
|
|
240
|
+
state === "open" &&
|
|
241
|
+
!socket.destroyed &&
|
|
242
|
+
live.sessionManager.getSessionFile() === sessionFile &&
|
|
243
|
+
live.isIdle() &&
|
|
244
|
+
!live.hasPendingMessages() &&
|
|
245
|
+
!isRunning() &&
|
|
246
|
+
delivery.pendingPrompts === 0 &&
|
|
247
|
+
generation() === startGeneration;
|
|
248
|
+
const reject = () =>
|
|
285
249
|
reply({
|
|
286
250
|
ok: false,
|
|
287
251
|
rejected: true,
|
|
288
|
-
error: "Target is
|
|
252
|
+
error: "Target is busy or changed context; idle close skipped",
|
|
289
253
|
});
|
|
254
|
+
if (!idle(ctx)) {
|
|
255
|
+
reject();
|
|
290
256
|
return;
|
|
291
257
|
}
|
|
292
|
-
const text = request["text"] as string;
|
|
293
|
-
const sender = request["context"]
|
|
294
|
-
? `${request["sender"]}\n${request["context"]}`
|
|
295
|
-
: (request["sender"] as string);
|
|
296
|
-
if (text.startsWith("/")) {
|
|
297
|
-
const route = commands.prepare(text, ctx);
|
|
298
|
-
const { command } = route;
|
|
299
|
-
const submit = () => {
|
|
300
|
-
submitted = true;
|
|
301
|
-
sendPolicyMessage(
|
|
302
|
-
pi,
|
|
303
|
-
{
|
|
304
|
-
customType: "herdr-agent-source",
|
|
305
|
-
content: sender,
|
|
306
|
-
display: true,
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
triggerTurn: false,
|
|
310
|
-
deliverAs: idle && !command ? "nextTurn" : "steer",
|
|
311
|
-
},
|
|
312
|
-
);
|
|
313
|
-
route.submit();
|
|
314
|
-
};
|
|
315
|
-
if (command) {
|
|
316
|
-
// Lifecycle commands can tear down this inbox. Flush acceptance first.
|
|
317
|
-
const execute = () => {
|
|
318
|
-
try {
|
|
319
|
-
submit();
|
|
320
|
-
} catch (error) {
|
|
321
|
-
ctx.ui.notify(
|
|
322
|
-
`Command submission failed: ${String(error)}`,
|
|
323
|
-
"error",
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
if (text.trim() === "/quit") {
|
|
328
|
-
// Hold this authenticated connection until process exit, not
|
|
329
|
-
// session_shutdown (which precedes Pi's runtime teardown).
|
|
330
|
-
state = "quitting";
|
|
331
|
-
sockets.delete(socket);
|
|
332
|
-
socket.setTimeout(0);
|
|
333
|
-
socket.write(
|
|
334
|
-
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command, closing: true })}\n`,
|
|
335
|
-
execute,
|
|
336
|
-
);
|
|
337
|
-
} else reply({ ok: true, command }, execute);
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
submit();
|
|
341
|
-
reply({ ok: true, command });
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
// Catch-up sampling and acceptance form one prompt boundary, including
|
|
345
|
-
// bursts arriving on separate authenticated sockets.
|
|
346
|
-
const previous = promptDelivery;
|
|
347
|
-
let release!: () => void;
|
|
348
|
-
promptDelivery = new Promise<void>((resolve) => {
|
|
349
|
-
release = resolve;
|
|
350
|
-
});
|
|
351
|
-
await previous;
|
|
352
258
|
try {
|
|
353
|
-
const
|
|
259
|
+
const livePane = await getAgent(
|
|
260
|
+
core ? fleet.connected().client : new HerdrClient(),
|
|
261
|
+
pane.pane_id,
|
|
262
|
+
);
|
|
354
263
|
if (
|
|
355
|
-
|
|
356
|
-
ctx
|
|
357
|
-
|
|
264
|
+
request["text"] !== "/quit" ||
|
|
265
|
+
!idle(ctx) ||
|
|
266
|
+
livePane.pane_id !== pane.pane_id ||
|
|
267
|
+
livePane.terminal_id !== pane.terminal_id ||
|
|
268
|
+
sessionPath(livePane) !== sessionFile ||
|
|
269
|
+
livePane.interactive_ready !== true ||
|
|
270
|
+
livePane.launch_pending === true ||
|
|
271
|
+
(livePane.agent_status !== "idle" &&
|
|
272
|
+
livePane.agent_status !== "done")
|
|
358
273
|
) {
|
|
359
|
-
|
|
360
|
-
ok: false,
|
|
361
|
-
rejected: true,
|
|
362
|
-
error: "Target changed context; retry after it settles",
|
|
363
|
-
});
|
|
274
|
+
reject();
|
|
364
275
|
return;
|
|
365
276
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
try {
|
|
379
|
-
announcePeerMessage(pi, {
|
|
380
|
-
sender: request["sender"] as string,
|
|
381
|
-
text,
|
|
382
|
-
});
|
|
383
|
-
} catch (error) {
|
|
384
|
-
ctx.ui.notify(
|
|
385
|
-
`Peer voice update failed: ${String(error)}`,
|
|
386
|
-
"warning",
|
|
277
|
+
commands.closeIdle(ctx, async (live) => {
|
|
278
|
+
if (!idle(live)) {
|
|
279
|
+
reject();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// No async boundary between the live idle check and shutdown.
|
|
283
|
+
// Retain the socket until process exit, as for ordinary peer quit.
|
|
284
|
+
state = "quitting";
|
|
285
|
+
sockets.delete(socket);
|
|
286
|
+
socket.setTimeout(0);
|
|
287
|
+
socket.write(
|
|
288
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command: true, closing: true })}\n`,
|
|
387
289
|
);
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
|
|
290
|
+
live.shutdown();
|
|
291
|
+
});
|
|
292
|
+
} catch {
|
|
293
|
+
reject();
|
|
392
294
|
}
|
|
393
|
-
|
|
394
|
-
reply({
|
|
395
|
-
ok: false,
|
|
396
|
-
rejected: !submitted,
|
|
397
|
-
error: `${submitted ? "Peer delivery failed; inspect the target before retrying" : "Peer delivery rejected"}: ${error instanceof Error ? error.message : String(error)}`,
|
|
398
|
-
});
|
|
295
|
+
return;
|
|
399
296
|
}
|
|
297
|
+
if (
|
|
298
|
+
(request["kind"] === "channel" || request["kind"] === "focus") &&
|
|
299
|
+
pane
|
|
300
|
+
) {
|
|
301
|
+
receivePeerFocus(
|
|
302
|
+
pi,
|
|
303
|
+
ctx,
|
|
304
|
+
fleet,
|
|
305
|
+
pane,
|
|
306
|
+
socket,
|
|
307
|
+
request,
|
|
308
|
+
buffer.slice(newline + 1),
|
|
309
|
+
reply,
|
|
310
|
+
);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
await delivery.receive(pi, ctx, socket, request, reply);
|
|
400
314
|
});
|
|
401
315
|
});
|
|
402
316
|
server.maxConnections = 16;
|
|
@@ -420,6 +334,9 @@ async function openInbox(
|
|
|
420
334
|
await unlink(path);
|
|
421
335
|
} catch (error) {
|
|
422
336
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
337
|
+
} finally {
|
|
338
|
+
await clearCoreSession?.();
|
|
339
|
+
clearCoreSession = undefined;
|
|
423
340
|
}
|
|
424
341
|
};
|
|
425
342
|
try {
|
|
@@ -448,6 +365,7 @@ async function openInbox(
|
|
|
448
365
|
{ mode: 0o600, flag: "wx" },
|
|
449
366
|
);
|
|
450
367
|
await rename(temporary, path);
|
|
368
|
+
if (core) clearCoreSession = await publishCoreSession(core, sessionFile);
|
|
451
369
|
} catch (error) {
|
|
452
370
|
try {
|
|
453
371
|
await unlink(temporary).catch((failure: NodeJS.ErrnoException) => {
|
package/src/remote/deployment.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { readFile } from "node:fs/promises";
|
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import type { SshMachine } from "../machine-catalog.js";
|
|
5
5
|
|
|
6
|
-
export const BRIDGE_VERSION =
|
|
6
|
+
export const BRIDGE_VERSION = 24;
|
|
7
7
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
8
8
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
9
9
|
const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
|
|
@@ -88,7 +88,7 @@ export async function requestContext(path, request, signal) {
|
|
|
88
88
|
}
|
|
89
89
|
let descriptor;
|
|
90
90
|
try {
|
|
91
|
-
descriptor = JSON.parse(await readReceiver(path));
|
|
91
|
+
descriptor = JSON.parse(await readReceiver(path, signal));
|
|
92
92
|
} catch (error) {
|
|
93
93
|
throw new Error(
|
|
94
94
|
"Shared context owner is unavailable; resume it and reconnect its machine",
|
|
@@ -31,10 +31,10 @@ function isMissing(error) {
|
|
|
31
31
|
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
/** @param {string} path @returns {Promise<string>} */
|
|
35
|
-
export async function readReceiver(path) {
|
|
34
|
+
/** @param {string} path @param {AbortSignal} [signal] @returns {Promise<string>} */
|
|
35
|
+
export async function readReceiver(path, signal) {
|
|
36
36
|
try {
|
|
37
|
-
return await readFile(path, "utf8");
|
|
37
|
+
return await readFile(path, { encoding: "utf8", signal });
|
|
38
38
|
} catch (error) {
|
|
39
39
|
if (!isMissing(error)) throw error;
|
|
40
40
|
}
|
|
@@ -47,6 +47,7 @@ export async function readReceiver(path) {
|
|
|
47
47
|
if (settled) return false;
|
|
48
48
|
settled = true;
|
|
49
49
|
clearTimeout(timer);
|
|
50
|
+
signal?.removeEventListener("abort", aborted);
|
|
50
51
|
watcher.close();
|
|
51
52
|
return true;
|
|
52
53
|
};
|
|
@@ -54,6 +55,7 @@ export async function readReceiver(path) {
|
|
|
54
55
|
const fail = (error) => {
|
|
55
56
|
if (finish()) reject(error);
|
|
56
57
|
};
|
|
58
|
+
const aborted = () => fail(signal?.reason);
|
|
57
59
|
const timer = setTimeout(
|
|
58
60
|
() =>
|
|
59
61
|
fail(
|
|
@@ -64,7 +66,7 @@ export async function readReceiver(path) {
|
|
|
64
66
|
30_000,
|
|
65
67
|
);
|
|
66
68
|
const read = () => {
|
|
67
|
-
void readFile(path, "utf8").then(
|
|
69
|
+
void readFile(path, { encoding: "utf8", signal }).then(
|
|
68
70
|
(value) => {
|
|
69
71
|
if (finish()) resolve(value);
|
|
70
72
|
},
|
|
@@ -77,6 +79,11 @@ export async function readReceiver(path) {
|
|
|
77
79
|
watcher.on("change", (_event, file) => {
|
|
78
80
|
if (file === null || file === basename(path)) read();
|
|
79
81
|
});
|
|
82
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
83
|
+
if (signal?.aborted) {
|
|
84
|
+
aborted();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
80
87
|
read();
|
|
81
88
|
});
|
|
82
89
|
}
|
|
@@ -104,7 +111,13 @@ export async function sendPeerMessage(
|
|
|
104
111
|
const frame =
|
|
105
112
|
JSON.stringify({
|
|
106
113
|
protocol: PEER_PROTOCOL,
|
|
107
|
-
kind:
|
|
114
|
+
kind: message.closeIfIdle
|
|
115
|
+
? "close-idle"
|
|
116
|
+
: commands
|
|
117
|
+
? "commands"
|
|
118
|
+
: focus
|
|
119
|
+
? "focus"
|
|
120
|
+
: "message",
|
|
108
121
|
focus,
|
|
109
122
|
id,
|
|
110
123
|
token: descriptor.token,
|
|
@@ -103,6 +103,8 @@ async function sessionView(path, size) {
|
|
|
103
103
|
let targetId;
|
|
104
104
|
/** @type {import("../types.js").LatestAssistant | undefined} */
|
|
105
105
|
let assistant;
|
|
106
|
+
let assistantSeen = false;
|
|
107
|
+
let assistantPending = false;
|
|
106
108
|
/** @type {number | undefined} */
|
|
107
109
|
let assistantDepth;
|
|
108
110
|
/** @type {import("../types.js").ExtensionMessage[]} */
|
|
@@ -115,6 +117,7 @@ async function sessionView(path, size) {
|
|
|
115
117
|
const resolved = new Set();
|
|
116
118
|
const result = () => ({
|
|
117
119
|
...(assistant ? { assistant } : {}),
|
|
120
|
+
...(assistantPending ? { assistantPending: true } : {}),
|
|
118
121
|
...(extensionMessages.length ? { extensionMessages } : {}),
|
|
119
122
|
...(input ? { input } : {}),
|
|
120
123
|
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
@@ -177,8 +180,12 @@ async function sessionView(path, size) {
|
|
|
177
180
|
}
|
|
178
181
|
const message = record(entry["message"]);
|
|
179
182
|
if (message && typeof message === "object") {
|
|
180
|
-
|
|
181
|
-
|
|
183
|
+
// Never fall back to an older reply behind a tool call or empty final.
|
|
184
|
+
if (!assistantSeen && message["role"] === "assistant") {
|
|
185
|
+
assistantSeen = true;
|
|
186
|
+
assistantPending = message["stopReason"] === "toolUse";
|
|
187
|
+
if (!assistantPending)
|
|
188
|
+
assistant = assistantFromMessage(entry["id"], message);
|
|
182
189
|
if (assistant) assistantDepth = currentDepth;
|
|
183
190
|
}
|
|
184
191
|
if (!input) {
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
19
19
|
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
20
20
|
|
|
21
|
-
const BRIDGE_VERSION =
|
|
21
|
+
const BRIDGE_VERSION = 23;
|
|
22
22
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
23
23
|
const subscriptions = new Map();
|
|
24
24
|
const contextRelays = new Map();
|
|
@@ -38,9 +38,9 @@ export function blockedSettlement(
|
|
|
38
38
|
...(record.cwd ? { cwd: record.cwd } : {}),
|
|
39
39
|
...(record.name ? { name: record.name } : {}),
|
|
40
40
|
pane_id: record.paneId,
|
|
41
|
-
tab_id: record.tabId,
|
|
41
|
+
...(record.tabId ? { tab_id: record.tabId } : {}),
|
|
42
42
|
terminal_id: record.terminalId,
|
|
43
|
-
workspace_id: record.workspaceId,
|
|
43
|
+
...(record.workspaceId ? { workspace_id: record.workspaceId } : {}),
|
|
44
44
|
},
|
|
45
45
|
labels: {},
|
|
46
46
|
session: {},
|
package/src/settlement/index.ts
CHANGED
|
@@ -167,6 +167,18 @@ export class SettlementReporter {
|
|
|
167
167
|
const retryRequest = task ? { ...request, task } : request;
|
|
168
168
|
const expectedUser = activityExpectedUser(current.activity);
|
|
169
169
|
const blocked = settlement.status === "blocked";
|
|
170
|
+
// Status and session writes arrive separately. A tool call is not a reply,
|
|
171
|
+
// even if done arrives before the final message is visible on disk.
|
|
172
|
+
if (!blocked && settlement.session.assistantPending) {
|
|
173
|
+
if (!this.retry(lifecycle, retryRequest, retryState)) {
|
|
174
|
+
this.failReconciliation(
|
|
175
|
+
lifecycle,
|
|
176
|
+
current,
|
|
177
|
+
"the final assistant reply was not persisted",
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
170
182
|
// Closing a message and publishing resumed status use separate transports.
|
|
171
183
|
if (blocked && replyingTo) {
|
|
172
184
|
if (
|
|
@@ -145,15 +145,19 @@ export async function registerSharedAgentContext(
|
|
|
145
145
|
),
|
|
146
146
|
);
|
|
147
147
|
});
|
|
148
|
-
pi.on("resources_discover", async (_event, ctx) =>
|
|
148
|
+
pi.on("resources_discover", async (_event, ctx) => {
|
|
149
|
+
await publish(ctx);
|
|
150
|
+
await fleet.restoreSharing(ctx);
|
|
151
|
+
});
|
|
149
152
|
pi.on("session_shutdown", async () => {
|
|
150
153
|
unregister?.();
|
|
151
154
|
await close?.();
|
|
152
155
|
connection?.dispose();
|
|
153
156
|
relationships?.dispose();
|
|
154
157
|
});
|
|
155
|
-
fleet.setContextRelay(
|
|
156
|
-
shared.handle(ctx, request, signal),
|
|
158
|
+
fleet.setContextRelay(
|
|
159
|
+
(ctx, request, signal) => shared.handle(ctx, request, signal),
|
|
160
|
+
(ctx, machine) => shared.needsRelay(ctx, machine),
|
|
157
161
|
);
|
|
158
162
|
return shared;
|
|
159
163
|
}
|
package/src/shared-context.ts
CHANGED
|
@@ -3,7 +3,11 @@ import type {
|
|
|
3
3
|
ExtensionAPI,
|
|
4
4
|
ExtensionContext,
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
binding,
|
|
8
|
+
children,
|
|
9
|
+
isBoardEnvelope,
|
|
10
|
+
} from "@howaboua/pi-agent-board/integration";
|
|
7
11
|
import type { CodexAgentRelationshipService } from "@howaboua/pi-codex-conversion/agent-relationship";
|
|
8
12
|
import type {
|
|
9
13
|
ContextSharingService,
|
|
@@ -12,7 +16,7 @@ import type {
|
|
|
12
16
|
} from "@howaboua/pi-notes-compaction/context-sharing";
|
|
13
17
|
import { Check } from "typebox/value";
|
|
14
18
|
import { AgentAttachment } from "./attachment.js";
|
|
15
|
-
import { Identity } from "./attachment-protocol.js";
|
|
19
|
+
import { attachmentRoutes, Identity } from "./attachment-protocol.js";
|
|
16
20
|
import type { AgentBoard } from "./board/host.js";
|
|
17
21
|
import type { AgentFleet } from "./fleet/index.js";
|
|
18
22
|
import { sessionPath } from "./herdr.js";
|
|
@@ -64,6 +68,34 @@ export class SharedAgentContext {
|
|
|
64
68
|
});
|
|
65
69
|
}
|
|
66
70
|
|
|
71
|
+
needsRelay(ctx: ExtensionContext, machine: string): boolean {
|
|
72
|
+
if (children(ctx).some((child) => child.machine === machine)) return true;
|
|
73
|
+
if (
|
|
74
|
+
attachmentRoutes(ctx).some(
|
|
75
|
+
(route) =>
|
|
76
|
+
route.machine === machine &&
|
|
77
|
+
((route.plan.context && !route.detached?.context) ||
|
|
78
|
+
(route.plan.board && !route.detached?.board)),
|
|
79
|
+
)
|
|
80
|
+
)
|
|
81
|
+
return true;
|
|
82
|
+
const identity = this.getService()?.describe(ctx);
|
|
83
|
+
if (!identity) return false;
|
|
84
|
+
return ctx.sessionManager.getEntries().some((entry) => {
|
|
85
|
+
if (entry.type !== "custom" || entry.customType !== MEMBER_ENTRY)
|
|
86
|
+
return false;
|
|
87
|
+
const child = entry.data as Partial<Member> | undefined;
|
|
88
|
+
return (
|
|
89
|
+
child?.machine === machine &&
|
|
90
|
+
child.threadId === identity.threadId &&
|
|
91
|
+
child.sessionId === identity.sessionId &&
|
|
92
|
+
typeof child.sessionFile === "string" &&
|
|
93
|
+
!!child.sessionFile &&
|
|
94
|
+
typeof child.agentName === "string"
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
67
99
|
async handle(
|
|
68
100
|
ctx: ExtensionContext,
|
|
69
101
|
value: unknown,
|
package/src/spawn-context.ts
CHANGED
|
@@ -38,6 +38,9 @@ export class SpawnContext {
|
|
|
38
38
|
this.getRelationships = getRelationships;
|
|
39
39
|
this.getService = getService;
|
|
40
40
|
}
|
|
41
|
+
canShare(ctx: ExtensionContext): boolean {
|
|
42
|
+
return this.getService()?.canCreateChild(ctx) === true;
|
|
43
|
+
}
|
|
41
44
|
prepareRelationship(
|
|
42
45
|
ctx: ExtensionContext,
|
|
43
46
|
runtime: ConnectedMachine,
|
|
@@ -119,6 +122,7 @@ export class SpawnContext {
|
|
|
119
122
|
throw new Error(
|
|
120
123
|
"Shared context spawn requires a saved controller session",
|
|
121
124
|
);
|
|
125
|
+
await runtime.prepareSharing();
|
|
122
126
|
const routing: Routing = {
|
|
123
127
|
transport: "shepherdr",
|
|
124
128
|
parent: runtime.local
|