@howaboua/pi-shepherdr 0.2.23 → 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 +20 -18
- package/README.md +80 -69
- package/biome.json +1 -1
- package/index.ts +4 -2
- package/package.json +14 -9
- package/src/agents-contract.ts +9 -6
- package/src/agents-discovery.ts +78 -37
- package/src/agents-focus.ts +17 -11
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +24 -8
- package/src/agents-tool.ts +26 -8
- package/src/agents-work.ts +5 -5
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +4 -3
- package/src/board/host.ts +5 -3
- package/src/board/membership.ts +1 -1
- package/src/close-idle.ts +89 -0
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +7 -5
- package/src/{control-panel-items.ts → control-panel/items.ts} +17 -5
- package/src/{control-panel-view.ts → control-panel/view.ts} +9 -5
- package/src/controller.ts +17 -5
- 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 +413 -0
- package/src/fleet/inspection.ts +159 -0
- package/src/fleet/machine.ts +55 -0
- package/src/fleet/monitor.ts +85 -0
- package/src/fleet/persistence.ts +65 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +133 -0
- 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/attachments.ts +39 -0
- package/src/messages/attribution.ts +84 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +195 -0
- package/src/messages/focus.ts +47 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor-record.ts +9 -6
- package/src/monitor.ts +33 -2
- 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 +127 -209
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- 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/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +35 -233
- package/src/shared-context-host.ts +40 -14
- package/src/shared-context.ts +44 -6
- package/src/spawn-context.ts +24 -17
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +9 -5
- package/src/voice-handoff.ts +1 -1
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +14 -83
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -637
- package/src/remote-client.ts +0 -761
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import type { HerdrEvent, PaneInfo, PeerChannel } from "../types.js";
|
|
4
|
+
import { ContextRelay } from "./context-relay.js";
|
|
5
|
+
import { appendBounded, BRIDGE_VERSION } from "./deployment.js";
|
|
6
|
+
import { FocusChannels } from "./focus-channels.js";
|
|
7
|
+
|
|
8
|
+
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
9
|
+
const READY_TIMEOUT_MS = 15_000;
|
|
10
|
+
|
|
11
|
+
interface PendingCall {
|
|
12
|
+
reject: (error: Error) => void;
|
|
13
|
+
resolve: (value: unknown) => void;
|
|
14
|
+
timer: NodeJS.Timeout | undefined;
|
|
15
|
+
cleanup?: () => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface SubscriptionCallbacks {
|
|
19
|
+
onDisconnect: (error?: Error) => void;
|
|
20
|
+
onEvent: (event: HerdrEvent) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface BridgeError {
|
|
24
|
+
code?: string;
|
|
25
|
+
herdrResponse?: boolean;
|
|
26
|
+
message: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Sole authority for framed calls, streams, duplex channels, context relay and teardown. */
|
|
30
|
+
export class RemoteTransport {
|
|
31
|
+
private readonly contextRelay: ContextRelay;
|
|
32
|
+
private readonly child: ChildProcessWithoutNullStreams;
|
|
33
|
+
private closed = false;
|
|
34
|
+
private diagnostics = "";
|
|
35
|
+
private input = "";
|
|
36
|
+
private readonly onClose: (error: Error) => void;
|
|
37
|
+
private readonly pending = new Map<string, PendingCall>();
|
|
38
|
+
private readonly subscriptions = new Map<string, SubscriptionCallbacks>();
|
|
39
|
+
private readonly focusChannels: FocusChannels;
|
|
40
|
+
|
|
41
|
+
protected constructor(
|
|
42
|
+
child: ChildProcessWithoutNullStreams,
|
|
43
|
+
onClose: (error: Error) => void,
|
|
44
|
+
) {
|
|
45
|
+
this.child = child;
|
|
46
|
+
this.contextRelay = new ContextRelay(
|
|
47
|
+
child,
|
|
48
|
+
() => this.closed,
|
|
49
|
+
MAX_FRAME_BYTES,
|
|
50
|
+
);
|
|
51
|
+
this.focusChannels = new FocusChannels(child, () => this.closed);
|
|
52
|
+
this.onClose = onClose;
|
|
53
|
+
child.stdout.setEncoding("utf8");
|
|
54
|
+
child.stdout.on("data", (chunk: string) => this.consume(chunk));
|
|
55
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
56
|
+
this.diagnostics = appendBounded(this.diagnostics, chunk);
|
|
57
|
+
});
|
|
58
|
+
child.stdin.on("error", (error) => this.disconnected(error));
|
|
59
|
+
child.on("error", (error) => this.disconnected(error));
|
|
60
|
+
child.on("exit", (code) => {
|
|
61
|
+
this.disconnected(
|
|
62
|
+
new Error(
|
|
63
|
+
this.diagnostics.trim() ||
|
|
64
|
+
`remote Shepherdr bridge exited ${code ?? "without status"}`,
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async requestContext(
|
|
71
|
+
path: string,
|
|
72
|
+
request: unknown,
|
|
73
|
+
signal?: AbortSignal,
|
|
74
|
+
): Promise<unknown> {
|
|
75
|
+
signal?.throwIfAborted();
|
|
76
|
+
return this.call({ op: "context", path, request }, 36_000, signal);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async startContextRelay(
|
|
80
|
+
threadId: string,
|
|
81
|
+
routeId: string,
|
|
82
|
+
handler: (request: unknown, signal: AbortSignal) => Promise<unknown>,
|
|
83
|
+
): Promise<void> {
|
|
84
|
+
return this.contextRelay.start(threadId, routeId, handler, (message) =>
|
|
85
|
+
this.call(message),
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
contextRelayPath(): string {
|
|
89
|
+
return this.contextRelay.path();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async openFocusChannel(
|
|
93
|
+
agent: PaneInfo,
|
|
94
|
+
request: import("../types.js").FocusRequest,
|
|
95
|
+
): Promise<PeerChannel> {
|
|
96
|
+
return this.focusChannels.open(agent, request, (message, timeoutMs) =>
|
|
97
|
+
this.call(message, timeoutMs),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async subscribe(
|
|
102
|
+
subscriptions: object[],
|
|
103
|
+
onEvent: (event: HerdrEvent) => void,
|
|
104
|
+
onDisconnect: (error?: Error) => void,
|
|
105
|
+
signal?: AbortSignal,
|
|
106
|
+
): Promise<() => void> {
|
|
107
|
+
return this.subscribeTo(
|
|
108
|
+
{ op: "subscribe", subscriptions },
|
|
109
|
+
onEvent,
|
|
110
|
+
onDisconnect,
|
|
111
|
+
signal,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async watch(
|
|
116
|
+
paths: string[],
|
|
117
|
+
onChange: (path: string) => void,
|
|
118
|
+
onError: (error: Error) => void,
|
|
119
|
+
signal: AbortSignal,
|
|
120
|
+
): Promise<() => void> {
|
|
121
|
+
return this.subscribeTo(
|
|
122
|
+
{ op: "session_subscribe", paths },
|
|
123
|
+
(event) => {
|
|
124
|
+
if (typeof event.data["path"] === "string")
|
|
125
|
+
onChange(event.data["path"]);
|
|
126
|
+
},
|
|
127
|
+
(error) => onError(error ?? new Error("Session activity stream closed")),
|
|
128
|
+
signal,
|
|
129
|
+
);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private async subscribeTo(
|
|
133
|
+
request: Record<string, unknown>,
|
|
134
|
+
onEvent: (event: HerdrEvent) => void,
|
|
135
|
+
onDisconnect: (error?: Error) => void,
|
|
136
|
+
signal?: AbortSignal,
|
|
137
|
+
): Promise<() => void> {
|
|
138
|
+
signal?.throwIfAborted();
|
|
139
|
+
const id = randomUUID();
|
|
140
|
+
this.subscriptions.set(id, { onEvent, onDisconnect });
|
|
141
|
+
const unsubscribe = () => {
|
|
142
|
+
signal?.removeEventListener("abort", unsubscribe);
|
|
143
|
+
if (!this.subscriptions.delete(id) || this.closed) return;
|
|
144
|
+
void this.call({ op: "unsubscribe", subscription: id }).catch(
|
|
145
|
+
() => undefined,
|
|
146
|
+
);
|
|
147
|
+
};
|
|
148
|
+
try {
|
|
149
|
+
const ready = this.call({ ...request, id }, 11_000);
|
|
150
|
+
signal?.addEventListener("abort", unsubscribe, { once: true });
|
|
151
|
+
if (signal?.aborted) unsubscribe();
|
|
152
|
+
await ready;
|
|
153
|
+
signal?.throwIfAborted();
|
|
154
|
+
} catch (error) {
|
|
155
|
+
unsubscribe();
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
return unsubscribe;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
close(): void {
|
|
162
|
+
if (this.closed) return;
|
|
163
|
+
this.closed = true;
|
|
164
|
+
this.child.kill();
|
|
165
|
+
this.failPending(new Error("remote Shepherdr bridge closed"), false);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
protected ready(): Promise<void> {
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
const timer = setTimeout(() => {
|
|
171
|
+
this.close();
|
|
172
|
+
reject(new Error("remote Shepherdr bridge did not become ready"));
|
|
173
|
+
}, READY_TIMEOUT_MS);
|
|
174
|
+
timer.unref();
|
|
175
|
+
this.pending.set("ready", {
|
|
176
|
+
timer,
|
|
177
|
+
resolve: (value) => {
|
|
178
|
+
const ready = value as { version?: unknown };
|
|
179
|
+
if (ready.version !== BRIDGE_VERSION) {
|
|
180
|
+
reject(new Error("remote Shepherdr bridge version mismatch"));
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
resolve();
|
|
184
|
+
},
|
|
185
|
+
reject,
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
protected call(
|
|
191
|
+
message: Record<string, unknown>,
|
|
192
|
+
timeoutMs = 10_000,
|
|
193
|
+
signal?: AbortSignal,
|
|
194
|
+
): Promise<unknown> {
|
|
195
|
+
signal?.throwIfAborted();
|
|
196
|
+
if (this.closed)
|
|
197
|
+
return Promise.reject(
|
|
198
|
+
new Error("remote Shepherdr bridge is unavailable"),
|
|
199
|
+
);
|
|
200
|
+
const id = typeof message["id"] === "string" ? message["id"] : randomUUID();
|
|
201
|
+
return new Promise((resolve, reject) => {
|
|
202
|
+
const timer =
|
|
203
|
+
timeoutMs === 0
|
|
204
|
+
? undefined
|
|
205
|
+
: setTimeout(() => {
|
|
206
|
+
const error = new Error(
|
|
207
|
+
`remote Shepherdr ${String(message["op"])} timed out`,
|
|
208
|
+
);
|
|
209
|
+
if (message["op"] === "context_relay")
|
|
210
|
+
this.rejectPending(id, error);
|
|
211
|
+
else this.disconnected(error);
|
|
212
|
+
}, timeoutMs);
|
|
213
|
+
timer?.unref();
|
|
214
|
+
const abort = () => {
|
|
215
|
+
if (!this.pending.has(id)) return;
|
|
216
|
+
this.rejectPending(
|
|
217
|
+
id,
|
|
218
|
+
new Error(
|
|
219
|
+
"Shared context request cancelled; a write may have completed. Reread the note before retrying",
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
this.child.stdin.write(
|
|
223
|
+
`${JSON.stringify({ op: "context_cancel", id })}\n`,
|
|
224
|
+
);
|
|
225
|
+
};
|
|
226
|
+
this.pending.set(id, {
|
|
227
|
+
resolve,
|
|
228
|
+
reject,
|
|
229
|
+
timer,
|
|
230
|
+
cleanup: () => signal?.removeEventListener("abort", abort),
|
|
231
|
+
});
|
|
232
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
233
|
+
this.child.stdin.write(
|
|
234
|
+
`${JSON.stringify({ ...message, id })}\n`,
|
|
235
|
+
(error) => {
|
|
236
|
+
if (!error) return;
|
|
237
|
+
this.disconnected(error);
|
|
238
|
+
},
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
private consume(chunk: string): void {
|
|
244
|
+
this.input += chunk;
|
|
245
|
+
if (Buffer.byteLength(this.input) > MAX_FRAME_BYTES) {
|
|
246
|
+
this.disconnected(
|
|
247
|
+
new Error("remote Shepherdr bridge frame is too large"),
|
|
248
|
+
);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
for (;;) {
|
|
252
|
+
const newline = this.input.indexOf("\n");
|
|
253
|
+
if (newline < 0) break;
|
|
254
|
+
const line = this.input.slice(0, newline);
|
|
255
|
+
this.input = this.input.slice(newline + 1);
|
|
256
|
+
if (!line) continue;
|
|
257
|
+
let value: Record<string, unknown>;
|
|
258
|
+
try {
|
|
259
|
+
value = JSON.parse(line) as Record<string, unknown>;
|
|
260
|
+
} catch (error) {
|
|
261
|
+
this.disconnected(
|
|
262
|
+
new Error(
|
|
263
|
+
`remote Shepherdr bridge returned invalid JSON: ${error instanceof Error ? error.message : String(error)}`,
|
|
264
|
+
),
|
|
265
|
+
);
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (value["type"] === "ready") {
|
|
269
|
+
this.resolvePending("ready", value);
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
if (this.focusChannels.consume(value)) continue;
|
|
273
|
+
const subscription = value["subscription"];
|
|
274
|
+
if (typeof subscription === "string") {
|
|
275
|
+
const callbacks = this.subscriptions.get(subscription);
|
|
276
|
+
const event = value["event"];
|
|
277
|
+
if (callbacks && event && typeof event === "object") {
|
|
278
|
+
try {
|
|
279
|
+
callbacks.onEvent(event as HerdrEvent);
|
|
280
|
+
} catch (error) {
|
|
281
|
+
this.disconnected(
|
|
282
|
+
new Error(
|
|
283
|
+
`Shepherdr event handling failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
284
|
+
{ cause: error },
|
|
285
|
+
),
|
|
286
|
+
);
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
const id = value["id"];
|
|
293
|
+
if (typeof id !== "string") continue;
|
|
294
|
+
if (this.contextRelay.consume(id, value)) continue;
|
|
295
|
+
if (value["ok"] === true) this.resolvePending(id, value["result"]);
|
|
296
|
+
else {
|
|
297
|
+
const detail = value["error"] as BridgeError | undefined;
|
|
298
|
+
const error = new Error(
|
|
299
|
+
detail?.message ?? "remote Shepherdr bridge request failed",
|
|
300
|
+
) as Error & {
|
|
301
|
+
code?: string;
|
|
302
|
+
herdrResponse?: true;
|
|
303
|
+
};
|
|
304
|
+
if (detail?.code) error.code = detail.code;
|
|
305
|
+
if (detail?.herdrResponse) error.herdrResponse = true;
|
|
306
|
+
this.rejectPending(id, error);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
private resolvePending(id: string, value: unknown): void {
|
|
312
|
+
const pending = this.pending.get(id);
|
|
313
|
+
if (!pending) return;
|
|
314
|
+
this.pending.delete(id);
|
|
315
|
+
clearTimeout(pending.timer);
|
|
316
|
+
pending.cleanup?.();
|
|
317
|
+
pending.resolve(value);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private rejectPending(id: string, error: Error): void {
|
|
321
|
+
const pending = this.pending.get(id);
|
|
322
|
+
if (!pending) return;
|
|
323
|
+
this.pending.delete(id);
|
|
324
|
+
clearTimeout(pending.timer);
|
|
325
|
+
pending.cleanup?.();
|
|
326
|
+
pending.reject(error);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
private disconnected(error: Error): void {
|
|
330
|
+
if (this.closed) return;
|
|
331
|
+
if ("code" in error && error.code === "EPIPE")
|
|
332
|
+
error = new Error("Connection to the remote machine closed", {
|
|
333
|
+
cause: error,
|
|
334
|
+
});
|
|
335
|
+
this.closed = true;
|
|
336
|
+
this.child.kill();
|
|
337
|
+
this.failPending(error, true);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
private failPending(error: Error, notify: boolean): void {
|
|
341
|
+
this.focusChannels.failAll(error);
|
|
342
|
+
this.contextRelay.abortAll();
|
|
343
|
+
for (const pending of this.pending.values()) {
|
|
344
|
+
clearTimeout(pending.timer);
|
|
345
|
+
pending.cleanup?.();
|
|
346
|
+
pending.reject(error);
|
|
347
|
+
}
|
|
348
|
+
this.pending.clear();
|
|
349
|
+
let callbackFailure: unknown;
|
|
350
|
+
for (const callbacks of this.subscriptions.values()) {
|
|
351
|
+
try {
|
|
352
|
+
callbacks.onDisconnect(error);
|
|
353
|
+
} catch (failure) {
|
|
354
|
+
callbackFailure ??= failure;
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
this.subscriptions.clear();
|
|
358
|
+
if (notify) {
|
|
359
|
+
this.onClose(
|
|
360
|
+
callbackFailure === undefined
|
|
361
|
+
? error
|
|
362
|
+
: new Error(
|
|
363
|
+
`${error.message}; disconnect handler failed: ${callbackFailure instanceof Error ? callbackFailure.message : String(callbackFailure)}`,
|
|
364
|
+
{ cause: callbackFailure },
|
|
365
|
+
),
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { isSettledStatus } from "../activity.js";
|
|
2
|
+
import { getAgent, getSnapshot, sessionPath } from "../herdr.js";
|
|
3
|
+
import type { HerdrConnection } from "../herdr-client.js";
|
|
4
|
+
import { type AssistantReader, SessionReader } from "../session-reader.js";
|
|
5
|
+
import type {
|
|
6
|
+
ExtensionMessage,
|
|
7
|
+
LatestAssistant,
|
|
8
|
+
MonitoredAgent,
|
|
9
|
+
PaneInfo,
|
|
10
|
+
SessionView,
|
|
11
|
+
SettledAgentStatus,
|
|
12
|
+
} from "../types.js";
|
|
13
|
+
|
|
14
|
+
interface SettlementLabels {
|
|
15
|
+
tab?: string;
|
|
16
|
+
workspace?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface CollectedSettlement {
|
|
20
|
+
agent: PaneInfo;
|
|
21
|
+
extensionMessages?: ExtensionMessage[];
|
|
22
|
+
labels: SettlementLabels;
|
|
23
|
+
reply?: LatestAssistant;
|
|
24
|
+
status: SettledAgentStatus;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CollectedSettlementWithSession extends CollectedSettlement {
|
|
28
|
+
session: SessionView;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function blockedSettlement(
|
|
32
|
+
record: MonitoredAgent,
|
|
33
|
+
): CollectedSettlementWithSession {
|
|
34
|
+
return {
|
|
35
|
+
agent: {
|
|
36
|
+
agent: "pi",
|
|
37
|
+
agent_status: "blocked",
|
|
38
|
+
...(record.cwd ? { cwd: record.cwd } : {}),
|
|
39
|
+
...(record.name ? { name: record.name } : {}),
|
|
40
|
+
pane_id: record.paneId,
|
|
41
|
+
...(record.tabId ? { tab_id: record.tabId } : {}),
|
|
42
|
+
terminal_id: record.terminalId,
|
|
43
|
+
...(record.workspaceId ? { workspace_id: record.workspaceId } : {}),
|
|
44
|
+
},
|
|
45
|
+
labels: {},
|
|
46
|
+
session: {},
|
|
47
|
+
status: "blocked",
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class SettlementCollector {
|
|
52
|
+
private readonly client: HerdrConnection;
|
|
53
|
+
private readonly reader: AssistantReader;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
client: HerdrConnection,
|
|
57
|
+
reader: AssistantReader = new SessionReader(),
|
|
58
|
+
) {
|
|
59
|
+
this.client = client;
|
|
60
|
+
this.reader = reader;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async latest(panel: PaneInfo): Promise<LatestAssistant | undefined> {
|
|
64
|
+
try {
|
|
65
|
+
return await this.reader.latest(sessionPath(panel));
|
|
66
|
+
} catch (error) {
|
|
67
|
+
if (panel.agent_status !== "blocked") throw error;
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
view(panel: PaneInfo): Promise<SessionView> {
|
|
73
|
+
return this.reader.view(sessionPath(panel));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async collect(
|
|
77
|
+
record: MonitoredAgent,
|
|
78
|
+
reportedStatus?: "blocked",
|
|
79
|
+
): Promise<CollectedSettlementWithSession | undefined> {
|
|
80
|
+
const [current, snapshot] = await Promise.all([
|
|
81
|
+
getAgent(this.client, record.paneId),
|
|
82
|
+
getSnapshot(this.client),
|
|
83
|
+
]);
|
|
84
|
+
if (current.terminal_id !== record.terminalId || current.agent !== "pi") {
|
|
85
|
+
return reportedStatus === "blocked"
|
|
86
|
+
? blockedSettlement(record)
|
|
87
|
+
: undefined;
|
|
88
|
+
}
|
|
89
|
+
const status =
|
|
90
|
+
reportedStatus ??
|
|
91
|
+
(isSettledStatus(current.agent_status)
|
|
92
|
+
? current.agent_status
|
|
93
|
+
: undefined);
|
|
94
|
+
if (!status) return undefined;
|
|
95
|
+
let view: SessionView;
|
|
96
|
+
try {
|
|
97
|
+
view = await this.reader.view(sessionPath(current));
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (status !== "blocked") throw error;
|
|
100
|
+
view = {};
|
|
101
|
+
}
|
|
102
|
+
const workspace = snapshot.workspaces.find(
|
|
103
|
+
(candidate) => candidate.workspace_id === current.workspace_id,
|
|
104
|
+
)?.label;
|
|
105
|
+
const tab = snapshot.tabs.find(
|
|
106
|
+
(candidate) => candidate.tab_id === current.tab_id,
|
|
107
|
+
)?.label;
|
|
108
|
+
return {
|
|
109
|
+
agent: current,
|
|
110
|
+
labels: {
|
|
111
|
+
...(workspace ? { workspace } : {}),
|
|
112
|
+
...(tab ? { tab } : {}),
|
|
113
|
+
},
|
|
114
|
+
...(view.assistant ? { reply: view.assistant } : {}),
|
|
115
|
+
...(view.extensionMessages
|
|
116
|
+
? { extensionMessages: view.extensionMessages }
|
|
117
|
+
: {}),
|
|
118
|
+
session: view,
|
|
119
|
+
status,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { activityAttemptId } from "../activity.js";
|
|
3
|
+
import { injectAgentEvent } from "../messages/events.js";
|
|
4
|
+
import type { WorkAttempt } from "../monitor-state.js";
|
|
5
|
+
import type {
|
|
6
|
+
ExtensionReply,
|
|
7
|
+
LatestAssistant,
|
|
8
|
+
MonitoredAgent,
|
|
9
|
+
} from "../types.js";
|
|
10
|
+
import type {
|
|
11
|
+
CollectedSettlement,
|
|
12
|
+
CollectedSettlementWithSession,
|
|
13
|
+
} from "./collector.js";
|
|
14
|
+
import type { SettlementLifecycle } from "./index.js";
|
|
15
|
+
|
|
16
|
+
export interface ClaimedSettlement extends CollectedSettlement {
|
|
17
|
+
blockedMessage?: string;
|
|
18
|
+
record: MonitoredAgent;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface SettlementClaim {
|
|
22
|
+
replyingTo?: ExtensionReply;
|
|
23
|
+
cleanup(): void;
|
|
24
|
+
reject(error: Error): void;
|
|
25
|
+
resolve(settlement: ClaimedSettlement): void;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Claims are per-attempt capabilities; delivery chooses exactly one waiter or controller. */
|
|
29
|
+
export class SettlementDelivery {
|
|
30
|
+
private readonly claims = new Map<string, SettlementClaim>();
|
|
31
|
+
private readonly pi: ExtensionAPI;
|
|
32
|
+
private readonly origin: {
|
|
33
|
+
machine: string;
|
|
34
|
+
label: () => string;
|
|
35
|
+
operatorPrefix: string;
|
|
36
|
+
};
|
|
37
|
+
constructor(
|
|
38
|
+
pi: ExtensionAPI,
|
|
39
|
+
origin: { machine: string; label: () => string; operatorPrefix: string },
|
|
40
|
+
) {
|
|
41
|
+
this.pi = pi;
|
|
42
|
+
this.origin = origin;
|
|
43
|
+
}
|
|
44
|
+
replyingTo(attemptId: string) {
|
|
45
|
+
return this.claims.get(attemptId)?.replyingTo;
|
|
46
|
+
}
|
|
47
|
+
stop() {
|
|
48
|
+
for (const claim of this.claims.values()) {
|
|
49
|
+
claim.cleanup();
|
|
50
|
+
claim.reject(new Error("Shepherdr monitor stopped"));
|
|
51
|
+
}
|
|
52
|
+
this.claims.clear();
|
|
53
|
+
}
|
|
54
|
+
claim(
|
|
55
|
+
attempt: WorkAttempt,
|
|
56
|
+
signal: AbortSignal,
|
|
57
|
+
replyingTo?: ExtensionReply,
|
|
58
|
+
): Promise<ClaimedSettlement> | undefined {
|
|
59
|
+
signal.throwIfAborted();
|
|
60
|
+
// Updates share the worker's current settlement, without stealing its waiter.
|
|
61
|
+
if (this.claims.has(attempt.attemptId)) return;
|
|
62
|
+
return new Promise<ClaimedSettlement>((resolve, reject) => {
|
|
63
|
+
const abort = () => {
|
|
64
|
+
const claim = this.claims.get(attempt.attemptId);
|
|
65
|
+
if (!claim) return;
|
|
66
|
+
this.claims.delete(attempt.attemptId);
|
|
67
|
+
claim.cleanup();
|
|
68
|
+
reject(
|
|
69
|
+
signal.reason instanceof Error
|
|
70
|
+
? signal.reason
|
|
71
|
+
: new Error("Shepherdr wait aborted"),
|
|
72
|
+
);
|
|
73
|
+
};
|
|
74
|
+
const claim: SettlementClaim = {
|
|
75
|
+
...(replyingTo ? { replyingTo } : {}),
|
|
76
|
+
cleanup: () => signal.removeEventListener("abort", abort),
|
|
77
|
+
reject,
|
|
78
|
+
resolve,
|
|
79
|
+
};
|
|
80
|
+
this.claims.set(attempt.attemptId, claim);
|
|
81
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
releaseClaim(attempt: WorkAttempt | undefined, error: unknown): void {
|
|
86
|
+
if (!attempt) return;
|
|
87
|
+
this.releaseAttempt(attempt.attemptId, error);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
releaseAgentClaim(record: MonitoredAgent, error: unknown): void {
|
|
91
|
+
const attemptId = activityAttemptId(record.activity);
|
|
92
|
+
if (attemptId) this.releaseAttempt(attemptId, error);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
releaseAttempt(attemptId: string, error: unknown): void {
|
|
96
|
+
const claim = this.claims.get(attemptId);
|
|
97
|
+
if (!claim) return;
|
|
98
|
+
this.claims.delete(attemptId);
|
|
99
|
+
claim.cleanup();
|
|
100
|
+
claim.reject(error instanceof Error ? error : new Error(String(error)));
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
deliverSettlement(
|
|
104
|
+
lifecycle: SettlementLifecycle,
|
|
105
|
+
record: MonitoredAgent,
|
|
106
|
+
settlement: CollectedSettlementWithSession,
|
|
107
|
+
attemptId: string | undefined,
|
|
108
|
+
blockedMessage: string | undefined,
|
|
109
|
+
reply: LatestAssistant | undefined,
|
|
110
|
+
): void {
|
|
111
|
+
const claim = attemptId ? this.claims.get(attemptId) : undefined;
|
|
112
|
+
if (claim && attemptId) {
|
|
113
|
+
this.claims.delete(attemptId);
|
|
114
|
+
claim.cleanup();
|
|
115
|
+
const {
|
|
116
|
+
reply: _latestReply,
|
|
117
|
+
session: _session,
|
|
118
|
+
...claimedSettlement
|
|
119
|
+
} = settlement;
|
|
120
|
+
claim.resolve({
|
|
121
|
+
...claimedSettlement,
|
|
122
|
+
record,
|
|
123
|
+
...(blockedMessage ? { blockedMessage } : {}),
|
|
124
|
+
...(reply ? { reply } : {}),
|
|
125
|
+
});
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
injectAgentEvent(this.pi, lifecycle.context, {
|
|
129
|
+
agent: settlement.agent,
|
|
130
|
+
agentToolName: "agents",
|
|
131
|
+
...(settlement.extensionMessages
|
|
132
|
+
? { extensionMessages: settlement.extensionMessages }
|
|
133
|
+
: {}),
|
|
134
|
+
machine: this.origin.machine,
|
|
135
|
+
machineLabel: this.origin.label(),
|
|
136
|
+
operatorPrefix: this.origin.operatorPrefix,
|
|
137
|
+
...(blockedMessage ? { blockedMessage } : {}),
|
|
138
|
+
labels: settlement.labels,
|
|
139
|
+
record,
|
|
140
|
+
...(reply ? { reply } : {}),
|
|
141
|
+
status: settlement.status,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
}
|