@howaboua/pi-shepherdr 0.2.16 → 0.2.18
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 +14 -0
- package/README.md +39 -7
- package/index.ts +1 -1
- package/package.json +3 -3
- package/src/agent-identity.ts +17 -0
- package/src/agents-contract.ts +38 -28
- package/src/agents-discovery.ts +9 -6
- package/src/agents-reply.ts +68 -0
- package/src/agents-spawn.ts +1 -10
- package/src/agents-tool.ts +23 -44
- package/src/agents-work.ts +22 -18
- package/src/controller.ts +1 -1
- package/src/extension-reply.ts +35 -0
- package/src/herdr-client.ts +29 -1
- package/src/messages.ts +82 -129
- package/src/{monitor-questions.ts → monitor-extension-messages.ts} +37 -111
- package/src/monitor-record.ts +19 -6
- package/src/monitor-state.ts +71 -25
- package/src/monitor.ts +20 -24
- package/src/peer-commands.ts +29 -15
- package/src/peer-inbox.ts +54 -1
- package/src/remote/shepherdr-channel.mjs +0 -1
- package/src/remote/shepherdr-peer.mjs +101 -18
- package/src/remote/shepherdr-session.mjs +106 -190
- package/src/remote/shepherdr.mjs +12 -2
- package/src/remote-client.ts +22 -1
- package/src/session-reader.ts +3 -364
- package/src/settlement.ts +41 -23
- package/src/types.ts +20 -16
- package/src/agents-answer.ts +0 -130
- package/src/ask-answer.ts +0 -377
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -9,11 +9,16 @@ import {
|
|
|
9
9
|
relayContextPath,
|
|
10
10
|
requestContext,
|
|
11
11
|
} from "./shepherdr-context.mjs";
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
sendPeerCommands,
|
|
14
|
+
sendPeerExtensionReply,
|
|
15
|
+
sendPeerFocus,
|
|
16
|
+
sendPeerMessage,
|
|
17
|
+
} from "./shepherdr-peer.mjs";
|
|
13
18
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
14
19
|
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
15
20
|
|
|
16
|
-
const BRIDGE_VERSION =
|
|
21
|
+
const BRIDGE_VERSION = 20;
|
|
17
22
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
18
23
|
const subscriptions = new Map();
|
|
19
24
|
const contextRelays = new Map();
|
|
@@ -323,12 +328,17 @@ async function handle(message) {
|
|
|
323
328
|
if (message.op === "message") {
|
|
324
329
|
return sendPeerMessage(request, message.agent, message.message);
|
|
325
330
|
}
|
|
331
|
+
if (message.op === "extension-reply")
|
|
332
|
+
return sendPeerExtensionReply(request, message.agent, message.reply);
|
|
326
333
|
if (message.op === "identity") {
|
|
327
334
|
return { host: hostname(), session: argument("session") || "default" };
|
|
328
335
|
}
|
|
329
336
|
if (message.op === "focus") {
|
|
330
337
|
return sendPeerFocus(request, message.agent, message.request);
|
|
331
338
|
}
|
|
339
|
+
if (message.op === "commands") {
|
|
340
|
+
return sendPeerCommands(request, message.agent);
|
|
341
|
+
}
|
|
332
342
|
if (message.op === "channel_open") {
|
|
333
343
|
const channel = await openPeerChannel(
|
|
334
344
|
request,
|
package/src/remote-client.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { fileURLToPath } from "node:url";
|
|
|
5
5
|
import type { HerdrConnection } from "./herdr-client.js";
|
|
6
6
|
import type { SshMachine } from "./machine-catalog.js";
|
|
7
7
|
import { createBufferedChannel } from "./remote/shepherdr-channel.mjs";
|
|
8
|
+
import { readPeerCommands } from "./remote/shepherdr-peer.mjs";
|
|
8
9
|
import type { AssistantReader } from "./session-reader.js";
|
|
9
10
|
import type {
|
|
10
11
|
HerdrEvent,
|
|
@@ -16,7 +17,7 @@ import type {
|
|
|
16
17
|
SessionView,
|
|
17
18
|
} from "./types.js";
|
|
18
19
|
|
|
19
|
-
const BRIDGE_VERSION =
|
|
20
|
+
const BRIDGE_VERSION = 20;
|
|
20
21
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
21
22
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
22
23
|
const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
|
|
@@ -309,6 +310,26 @@ export class RemoteHerdrClient implements HerdrConnection, AssistantReader {
|
|
|
309
310
|
return this.relayPath;
|
|
310
311
|
}
|
|
311
312
|
|
|
313
|
+
async replyExtension(
|
|
314
|
+
agent: PaneInfo,
|
|
315
|
+
reply: import("./types.js").ExtensionReply,
|
|
316
|
+
): Promise<string> {
|
|
317
|
+
const result = await this.call(
|
|
318
|
+
{ op: "extension-reply", agent, reply },
|
|
319
|
+
60_000,
|
|
320
|
+
);
|
|
321
|
+
if (typeof result !== "string")
|
|
322
|
+
throw new Error(
|
|
323
|
+
"Invalid extension reply receipt; inspect the target before retrying",
|
|
324
|
+
);
|
|
325
|
+
return result;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
async commands(agent: PaneInfo): Promise<import("./types.js").PeerCommand[]> {
|
|
329
|
+
const result = await this.call({ op: "commands", agent }, 26_000);
|
|
330
|
+
return readPeerCommands(result);
|
|
331
|
+
}
|
|
332
|
+
|
|
312
333
|
async sendMessage(
|
|
313
334
|
agent: PaneInfo,
|
|
314
335
|
message: PeerMessage,
|
package/src/session-reader.ts
CHANGED
|
@@ -1,342 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { readSessionView } from "./remote/shepherdr-session.mjs";
|
|
2
2
|
import { watchSessions } from "./remote/shepherdr-session-watch.mjs";
|
|
3
|
-
import type {
|
|
4
|
-
AskResult,
|
|
5
|
-
LatestAssistant,
|
|
6
|
-
LatestInput,
|
|
7
|
-
PendingAsk,
|
|
8
|
-
SessionView,
|
|
9
|
-
} from "./types.js";
|
|
10
|
-
|
|
11
|
-
const READ_CHUNK_BYTES = 64 * 1024;
|
|
12
|
-
|
|
13
|
-
function record(value: unknown): Record<string, unknown> | undefined {
|
|
14
|
-
return typeof value === "object" && value !== null && !Array.isArray(value)
|
|
15
|
-
? (value as Record<string, unknown>)
|
|
16
|
-
: undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function assistantFromMessage(
|
|
20
|
-
id: string,
|
|
21
|
-
message: Record<string, unknown>,
|
|
22
|
-
): LatestAssistant | undefined {
|
|
23
|
-
if (message["role"] !== "assistant") return undefined;
|
|
24
|
-
const text: string[] = [];
|
|
25
|
-
if (typeof message["content"] === "string") {
|
|
26
|
-
text.push(message["content"]);
|
|
27
|
-
} else if (Array.isArray(message["content"])) {
|
|
28
|
-
for (const value of message["content"]) {
|
|
29
|
-
const part = record(value);
|
|
30
|
-
if (part?.["type"] === "text" && typeof part["text"] === "string") {
|
|
31
|
-
text.push(part["text"]);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
const joined = text.join("");
|
|
36
|
-
const stopReason =
|
|
37
|
-
typeof message["stopReason"] === "string"
|
|
38
|
-
? message["stopReason"]
|
|
39
|
-
: undefined;
|
|
40
|
-
if (!joined && stopReason !== "error") return undefined;
|
|
41
|
-
return {
|
|
42
|
-
id,
|
|
43
|
-
text: joined,
|
|
44
|
-
...(stopReason ? { stopReason } : {}),
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function inputFromMessage(
|
|
49
|
-
id: string,
|
|
50
|
-
message: Record<string, unknown>,
|
|
51
|
-
): LatestInput | undefined {
|
|
52
|
-
if (message["role"] !== "user") return undefined;
|
|
53
|
-
const text: string[] = [];
|
|
54
|
-
if (typeof message["content"] === "string") {
|
|
55
|
-
text.push(message["content"]);
|
|
56
|
-
} else if (Array.isArray(message["content"])) {
|
|
57
|
-
for (const value of message["content"]) {
|
|
58
|
-
const part = record(value);
|
|
59
|
-
if (part?.["type"] === "text" && typeof part["text"] === "string") {
|
|
60
|
-
text.push(part["text"]);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
const joined = text.join("");
|
|
65
|
-
return joined ? { id, text: joined } : undefined;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function askChoice(
|
|
69
|
-
value: unknown,
|
|
70
|
-
): PendingAsk["prompts"][number]["choices"][number] | undefined {
|
|
71
|
-
const choice = record(value);
|
|
72
|
-
if (!choice || typeof choice["label"] !== "string") return undefined;
|
|
73
|
-
return {
|
|
74
|
-
label: choice["label"],
|
|
75
|
-
...(typeof choice["description"] === "string"
|
|
76
|
-
? { description: choice["description"] }
|
|
77
|
-
: {}),
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function askPrompt(value: unknown): PendingAsk["prompts"][number] | undefined {
|
|
82
|
-
const prompt = record(value);
|
|
83
|
-
if (!prompt || typeof prompt["title"] !== "string") return undefined;
|
|
84
|
-
return {
|
|
85
|
-
title: prompt["title"],
|
|
86
|
-
multiple: prompt["multiple"] === true,
|
|
87
|
-
choices: (Array.isArray(prompt["choices"]) ? prompt["choices"] : [])
|
|
88
|
-
.map(askChoice)
|
|
89
|
-
.filter((choice): choice is NonNullable<typeof choice> =>
|
|
90
|
-
Boolean(choice),
|
|
91
|
-
),
|
|
92
|
-
...(typeof prompt["body"] === "string" ? { body: prompt["body"] } : {}),
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function askCall(value: unknown): PendingAsk | undefined {
|
|
97
|
-
const part = record(value);
|
|
98
|
-
if (
|
|
99
|
-
!part ||
|
|
100
|
-
part["type"] !== "toolCall" ||
|
|
101
|
-
part["name"] !== "ask" ||
|
|
102
|
-
typeof part["id"] !== "string"
|
|
103
|
-
) {
|
|
104
|
-
return undefined;
|
|
105
|
-
}
|
|
106
|
-
return askFromInput(part["id"], part["arguments"]);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function askFromInput(
|
|
110
|
-
toolCallId: string,
|
|
111
|
-
input: unknown,
|
|
112
|
-
): PendingAsk | undefined {
|
|
113
|
-
const args = record(input);
|
|
114
|
-
if (!args) return undefined;
|
|
115
|
-
const prompts = (Array.isArray(args["prompts"]) ? args["prompts"] : [])
|
|
116
|
-
.map(askPrompt)
|
|
117
|
-
.filter((prompt): prompt is NonNullable<typeof prompt> => Boolean(prompt));
|
|
118
|
-
if (prompts.length === 0) return undefined;
|
|
119
|
-
return {
|
|
120
|
-
toolCallId,
|
|
121
|
-
handoff: args["handoff"] === true,
|
|
122
|
-
...(args["delivery"] === "steer" ? { delivery: "steer" as const } : {}),
|
|
123
|
-
prompts,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function resolvedToolCallId(
|
|
128
|
-
message: Record<string, unknown>,
|
|
129
|
-
): string | undefined {
|
|
130
|
-
if (message["role"] !== "toolResult" && message["role"] !== "tool") {
|
|
131
|
-
return undefined;
|
|
132
|
-
}
|
|
133
|
-
const value = message["toolCallId"] ?? message["tool_call_id"];
|
|
134
|
-
return typeof value === "string" ? value : undefined;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function askResult(
|
|
138
|
-
message: Record<string, unknown>,
|
|
139
|
-
): [string, AskResult] | undefined {
|
|
140
|
-
if (message["role"] !== "toolResult" || message["toolName"] !== "ask") {
|
|
141
|
-
return undefined;
|
|
142
|
-
}
|
|
143
|
-
const id = resolvedToolCallId(message);
|
|
144
|
-
if (!id) return undefined;
|
|
145
|
-
if (message["isError"] === true) return [id, { status: "rejected" }];
|
|
146
|
-
if (message["isError"] !== false) return [id, { status: "unknown" }];
|
|
147
|
-
const details = record(message["details"]);
|
|
148
|
-
if (details?.["pending"] === true) return undefined;
|
|
149
|
-
if (details?.["dismissed"] === true) return [id, { status: "rejected" }];
|
|
150
|
-
const rawResponses = details?.["responses"];
|
|
151
|
-
const rawResponseCount = Array.isArray(rawResponses)
|
|
152
|
-
? rawResponses.length
|
|
153
|
-
: -1;
|
|
154
|
-
const responses = Array.isArray(rawResponses)
|
|
155
|
-
? rawResponses
|
|
156
|
-
.map((value) => {
|
|
157
|
-
const response = record(value);
|
|
158
|
-
if (
|
|
159
|
-
!response ||
|
|
160
|
-
typeof response["id"] !== "string" ||
|
|
161
|
-
!Array.isArray(response["selections"]) ||
|
|
162
|
-
!response["selections"].every(
|
|
163
|
-
(selection) => typeof selection === "string",
|
|
164
|
-
)
|
|
165
|
-
) {
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
return {
|
|
169
|
-
id: response["id"],
|
|
170
|
-
selections: response["selections"] as string[],
|
|
171
|
-
...(typeof response["comment"] === "string"
|
|
172
|
-
? { comment: response["comment"] }
|
|
173
|
-
: {}),
|
|
174
|
-
};
|
|
175
|
-
})
|
|
176
|
-
.filter((response) => response !== undefined)
|
|
177
|
-
: undefined;
|
|
178
|
-
return [
|
|
179
|
-
id,
|
|
180
|
-
{
|
|
181
|
-
status:
|
|
182
|
-
responses &&
|
|
183
|
-
responses.length > 0 &&
|
|
184
|
-
responses.length === rawResponseCount
|
|
185
|
-
? "accepted"
|
|
186
|
-
: "unknown",
|
|
187
|
-
...(responses && responses.length === rawResponseCount
|
|
188
|
-
? { responses }
|
|
189
|
-
: {}),
|
|
190
|
-
},
|
|
191
|
-
];
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
async function readSessionView(
|
|
195
|
-
path: string,
|
|
196
|
-
size: number,
|
|
197
|
-
): Promise<SessionView> {
|
|
198
|
-
const file = await open(path, "r");
|
|
199
|
-
let targetId: string | undefined;
|
|
200
|
-
let assistant: LatestAssistant | undefined;
|
|
201
|
-
let assistantDepth: number | undefined;
|
|
202
|
-
let ask: PendingAsk | undefined;
|
|
203
|
-
const askResults = new Map<string, AskResult>();
|
|
204
|
-
let depth = 0;
|
|
205
|
-
let input: LatestInput | undefined;
|
|
206
|
-
let inputDepth: number | undefined;
|
|
207
|
-
const resolved = new Set<string>();
|
|
208
|
-
const result = (): SessionView => ({
|
|
209
|
-
...(assistant ? { assistant } : {}),
|
|
210
|
-
...(ask ? { ask } : {}),
|
|
211
|
-
...(askResults.size > 0
|
|
212
|
-
? { askResults: Object.fromEntries(askResults) }
|
|
213
|
-
: {}),
|
|
214
|
-
...(input ? { input } : {}),
|
|
215
|
-
...(assistantDepth !== undefined && inputDepth !== undefined
|
|
216
|
-
? { assistantAfterInput: assistantDepth < inputDepth }
|
|
217
|
-
: {}),
|
|
218
|
-
});
|
|
219
|
-
const inspect = (line: Buffer): boolean => {
|
|
220
|
-
if (line.length === 0) return false;
|
|
221
|
-
let entry: Record<string, unknown>;
|
|
222
|
-
try {
|
|
223
|
-
entry = JSON.parse(line.toString("utf8")) as Record<string, unknown>;
|
|
224
|
-
} catch {
|
|
225
|
-
return false;
|
|
226
|
-
}
|
|
227
|
-
const id = entry["id"];
|
|
228
|
-
if (typeof id !== "string") return false;
|
|
229
|
-
targetId ??= id;
|
|
230
|
-
if (id !== targetId) return false;
|
|
231
|
-
const currentDepth = depth;
|
|
232
|
-
depth += 1;
|
|
233
|
-
if (entry["type"] === "custom" && entry["customType"] === "pi-ask-active") {
|
|
234
|
-
const update = record(entry["data"]);
|
|
235
|
-
if (update?.["version"] === 1 && typeof update["id"] === "string") {
|
|
236
|
-
const callId = update["id"];
|
|
237
|
-
if (update["state"] === "closed") resolved.add(callId);
|
|
238
|
-
else if (
|
|
239
|
-
update["state"] === "active" &&
|
|
240
|
-
!ask &&
|
|
241
|
-
!resolved.has(callId)
|
|
242
|
-
) {
|
|
243
|
-
ask = askFromInput(callId, update);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
if (
|
|
248
|
-
entry["type"] === "custom" &&
|
|
249
|
-
entry["customType"] === "pi-ask-pending"
|
|
250
|
-
) {
|
|
251
|
-
const update = record(entry["data"]);
|
|
252
|
-
if (
|
|
253
|
-
update?.["version"] === 1 &&
|
|
254
|
-
update["state"] === "closed" &&
|
|
255
|
-
typeof update["id"] === "string"
|
|
256
|
-
) {
|
|
257
|
-
resolved.add(update["id"]);
|
|
258
|
-
const result = askResult({
|
|
259
|
-
role: "toolResult",
|
|
260
|
-
toolName: "ask",
|
|
261
|
-
toolCallId: update["id"],
|
|
262
|
-
isError: false,
|
|
263
|
-
details: update,
|
|
264
|
-
});
|
|
265
|
-
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
if (
|
|
269
|
-
!input &&
|
|
270
|
-
entry["type"] === "custom_message" &&
|
|
271
|
-
entry["customType"] === "herdr-agent-message" &&
|
|
272
|
-
typeof entry["content"] === "string" &&
|
|
273
|
-
entry["content"]
|
|
274
|
-
) {
|
|
275
|
-
input = { id, text: entry["content"] };
|
|
276
|
-
inputDepth = currentDepth;
|
|
277
|
-
}
|
|
278
|
-
const message = record(entry["message"]);
|
|
279
|
-
if (message) {
|
|
280
|
-
const result = askResult(message);
|
|
281
|
-
if (result && !askResults.has(result[0])) askResults.set(...result);
|
|
282
|
-
const resolvedId = resolvedToolCallId(message);
|
|
283
|
-
if (resolvedId && record(message["details"])?.["pending"] !== true)
|
|
284
|
-
resolved.add(resolvedId);
|
|
285
|
-
if (!assistant) {
|
|
286
|
-
assistant = assistantFromMessage(id, message);
|
|
287
|
-
if (assistant) assistantDepth = currentDepth;
|
|
288
|
-
}
|
|
289
|
-
if (!input) {
|
|
290
|
-
input = inputFromMessage(id, message);
|
|
291
|
-
if (input) inputDepth = currentDepth;
|
|
292
|
-
}
|
|
293
|
-
if (
|
|
294
|
-
!ask &&
|
|
295
|
-
message["role"] === "assistant" &&
|
|
296
|
-
Array.isArray(message["content"])
|
|
297
|
-
) {
|
|
298
|
-
ask = [...message["content"]]
|
|
299
|
-
.reverse()
|
|
300
|
-
.map(askCall)
|
|
301
|
-
.find(
|
|
302
|
-
(candidate) =>
|
|
303
|
-
candidate !== undefined &&
|
|
304
|
-
candidate.delivery !== "steer" &&
|
|
305
|
-
!resolved.has(candidate.toolCallId),
|
|
306
|
-
);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
const parentId = entry["parentId"];
|
|
310
|
-
if (typeof parentId !== "string") return true;
|
|
311
|
-
targetId = parentId;
|
|
312
|
-
return false;
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
try {
|
|
316
|
-
let position = size;
|
|
317
|
-
let partial = Buffer.alloc(0);
|
|
318
|
-
while (position > 0) {
|
|
319
|
-
const length = Math.min(READ_CHUNK_BYTES, position);
|
|
320
|
-
position -= length;
|
|
321
|
-
const chunk = Buffer.allocUnsafe(length);
|
|
322
|
-
const { bytesRead } = await file.read(chunk, 0, length, position);
|
|
323
|
-
const data = Buffer.concat([chunk.subarray(0, bytesRead), partial]);
|
|
324
|
-
let lineEnd = data.length;
|
|
325
|
-
for (let index = data.length - 1; index >= 0; index -= 1) {
|
|
326
|
-
if (data[index] !== 0x0a) continue;
|
|
327
|
-
if (inspect(data.subarray(index + 1, lineEnd))) {
|
|
328
|
-
return result();
|
|
329
|
-
}
|
|
330
|
-
lineEnd = index;
|
|
331
|
-
}
|
|
332
|
-
partial = data.subarray(0, lineEnd);
|
|
333
|
-
}
|
|
334
|
-
inspect(partial);
|
|
335
|
-
return result();
|
|
336
|
-
} finally {
|
|
337
|
-
await file.close();
|
|
338
|
-
}
|
|
339
|
-
}
|
|
3
|
+
import type { LatestAssistant, SessionView } from "./types.js";
|
|
340
4
|
|
|
341
5
|
export class SessionReader {
|
|
342
6
|
async watch(
|
|
@@ -347,38 +11,13 @@ export class SessionReader {
|
|
|
347
11
|
): Promise<() => void> {
|
|
348
12
|
return watchSessions(paths, onChange, onError, signal);
|
|
349
13
|
}
|
|
350
|
-
private readonly cache = new Map<
|
|
351
|
-
string,
|
|
352
|
-
{ mtimeMs: number; result: SessionView; size: number }
|
|
353
|
-
>();
|
|
354
|
-
|
|
355
14
|
async view(path?: string): Promise<SessionView> {
|
|
356
|
-
|
|
357
|
-
let metadata;
|
|
358
|
-
try {
|
|
359
|
-
metadata = await stat(path);
|
|
360
|
-
} catch (error) {
|
|
361
|
-
if ((error as NodeJS.ErrnoException).code === "ENOENT") return {};
|
|
362
|
-
throw error;
|
|
363
|
-
}
|
|
364
|
-
const cached = this.cache.get(path);
|
|
365
|
-
if (cached?.size === metadata.size && cached.mtimeMs === metadata.mtimeMs) {
|
|
366
|
-
return cached.result;
|
|
367
|
-
}
|
|
368
|
-
const result = await readSessionView(path, metadata.size);
|
|
369
|
-
this.cache.set(path, {
|
|
370
|
-
mtimeMs: metadata.mtimeMs,
|
|
371
|
-
result,
|
|
372
|
-
size: metadata.size,
|
|
373
|
-
});
|
|
374
|
-
return result;
|
|
15
|
+
return readSessionView(path);
|
|
375
16
|
}
|
|
376
|
-
|
|
377
17
|
async latest(path?: string): Promise<LatestAssistant | undefined> {
|
|
378
18
|
return (await this.view(path)).assistant;
|
|
379
19
|
}
|
|
380
20
|
}
|
|
381
|
-
|
|
382
21
|
export interface AssistantReader {
|
|
383
22
|
watch(
|
|
384
23
|
paths: string[],
|
package/src/settlement.ts
CHANGED
|
@@ -14,10 +14,11 @@ import { injectAgentEvent } from "./messages.js";
|
|
|
14
14
|
import type { MonitorState, WorkAttempt } from "./monitor-state.js";
|
|
15
15
|
import { type AssistantReader, SessionReader } from "./session-reader.js";
|
|
16
16
|
import type {
|
|
17
|
+
ExtensionMessage,
|
|
18
|
+
ExtensionReply,
|
|
17
19
|
LatestAssistant,
|
|
18
20
|
MonitoredAgent,
|
|
19
21
|
PaneInfo,
|
|
20
|
-
PendingAsk,
|
|
21
22
|
SessionView,
|
|
22
23
|
SettledAgentStatus,
|
|
23
24
|
} from "./types.js";
|
|
@@ -29,7 +30,7 @@ interface SettlementLabels {
|
|
|
29
30
|
|
|
30
31
|
interface CollectedSettlement {
|
|
31
32
|
agent: PaneInfo;
|
|
32
|
-
|
|
33
|
+
extensionMessages?: ExtensionMessage[];
|
|
33
34
|
labels: SettlementLabels;
|
|
34
35
|
reply?: LatestAssistant;
|
|
35
36
|
status: SettledAgentStatus;
|
|
@@ -45,7 +46,7 @@ export interface ClaimedSettlement extends CollectedSettlement {
|
|
|
45
46
|
}
|
|
46
47
|
|
|
47
48
|
interface SettlementClaim {
|
|
48
|
-
|
|
49
|
+
replyingTo?: ExtensionReply;
|
|
49
50
|
cleanup(): void;
|
|
50
51
|
reject(error: Error): void;
|
|
51
52
|
resolve(settlement: ClaimedSettlement): void;
|
|
@@ -169,7 +170,9 @@ export class SettlementCollector {
|
|
|
169
170
|
...(tab ? { tab } : {}),
|
|
170
171
|
},
|
|
171
172
|
...(view.assistant ? { reply: view.assistant } : {}),
|
|
172
|
-
...(
|
|
173
|
+
...(view.extensionMessages
|
|
174
|
+
? { extensionMessages: view.extensionMessages }
|
|
175
|
+
: {}),
|
|
173
176
|
session: view,
|
|
174
177
|
status,
|
|
175
178
|
};
|
|
@@ -229,12 +232,11 @@ export class SettlementReporter {
|
|
|
229
232
|
claim(
|
|
230
233
|
attempt: WorkAttempt,
|
|
231
234
|
signal: AbortSignal,
|
|
232
|
-
|
|
233
|
-
): Promise<ClaimedSettlement> {
|
|
235
|
+
replyingTo?: ExtensionReply,
|
|
236
|
+
): Promise<ClaimedSettlement> | undefined {
|
|
234
237
|
signal.throwIfAborted();
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
}
|
|
238
|
+
// Updates share the worker's current settlement, without stealing its waiter.
|
|
239
|
+
if (this.claims.has(attempt.attemptId)) return;
|
|
238
240
|
return new Promise<ClaimedSettlement>((resolve, reject) => {
|
|
239
241
|
const abort = () => {
|
|
240
242
|
const claim = this.claims.get(attempt.attemptId);
|
|
@@ -248,7 +250,7 @@ export class SettlementReporter {
|
|
|
248
250
|
);
|
|
249
251
|
};
|
|
250
252
|
const claim: SettlementClaim = {
|
|
251
|
-
...(
|
|
253
|
+
...(replyingTo ? { replyingTo } : {}),
|
|
252
254
|
cleanup: () => signal.removeEventListener("abort", abort),
|
|
253
255
|
reject,
|
|
254
256
|
resolve,
|
|
@@ -303,9 +305,12 @@ export class SettlementReporter {
|
|
|
303
305
|
}
|
|
304
306
|
this.reporting.set(key, request.status);
|
|
305
307
|
try {
|
|
308
|
+
const replyingTo = retryState.attemptId
|
|
309
|
+
? this.claims.get(retryState.attemptId)?.replyingTo
|
|
310
|
+
: undefined;
|
|
306
311
|
const settlement = await this.collector.collect(
|
|
307
312
|
request.record,
|
|
308
|
-
request.status === "blocked" ? "blocked" : undefined,
|
|
313
|
+
request.status === "blocked" && !replyingTo ? "blocked" : undefined,
|
|
309
314
|
);
|
|
310
315
|
if (!settlement || !lifecycle.isCurrent()) return;
|
|
311
316
|
if (this.pendingBlocked.has(key)) return;
|
|
@@ -330,17 +335,28 @@ export class SettlementReporter {
|
|
|
330
335
|
const retryRequest = task ? { ...request, task } : request;
|
|
331
336
|
const expectedUser = activityExpectedUser(current.activity);
|
|
332
337
|
const blocked = settlement.status === "blocked";
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
338
|
+
// Closing a message and publishing resumed status use separate transports.
|
|
339
|
+
if (blocked && replyingTo) {
|
|
340
|
+
if (
|
|
341
|
+
settlement.extensionMessages?.some(
|
|
342
|
+
(message) =>
|
|
343
|
+
message.extension === replyingTo.extension &&
|
|
344
|
+
message.id === replyingTo.id,
|
|
345
|
+
)
|
|
346
|
+
)
|
|
347
|
+
return;
|
|
348
|
+
if (
|
|
349
|
+
!settlement.extensionMessages?.some((message) => message.blocking)
|
|
350
|
+
) {
|
|
351
|
+
if (!this.retry(lifecycle, retryRequest, retryState)) {
|
|
352
|
+
this.failReconciliation(
|
|
353
|
+
lifecycle,
|
|
354
|
+
current,
|
|
355
|
+
"the worker remained blocked after its extension reply",
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
344
360
|
}
|
|
345
361
|
// A blocked status is authoritative even when Pi has not persisted the turn.
|
|
346
362
|
// Pi persists expanded skills/templates, not the submitted command text.
|
|
@@ -485,7 +501,9 @@ export class SettlementReporter {
|
|
|
485
501
|
injectAgentEvent(this.pi, lifecycle.context, {
|
|
486
502
|
agent: settlement.agent,
|
|
487
503
|
agentToolName: "agents",
|
|
488
|
-
...(settlement.
|
|
504
|
+
...(settlement.extensionMessages
|
|
505
|
+
? { extensionMessages: settlement.extensionMessages }
|
|
506
|
+
: {}),
|
|
489
507
|
machine: this.origin.machine,
|
|
490
508
|
machineLabel: this.origin.label(),
|
|
491
509
|
operatorPrefix: this.origin.operatorPrefix,
|
package/src/types.ts
CHANGED
|
@@ -7,7 +7,14 @@ export interface PeerMessage {
|
|
|
7
7
|
}
|
|
8
8
|
|
|
9
9
|
export interface PeerDelivery {
|
|
10
|
+
result?: string;
|
|
10
11
|
command: boolean;
|
|
12
|
+
commands?: PeerCommand[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface PeerCommand {
|
|
16
|
+
name: string;
|
|
17
|
+
description: string;
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
export interface MachineIdentity {
|
|
@@ -97,11 +104,12 @@ export interface SessionSnapshot {
|
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
export interface MonitoredAgent {
|
|
107
|
+
readonly instructions?: string;
|
|
100
108
|
readonly scope: "task" | "persistent";
|
|
101
109
|
readonly activity: AgentActivity;
|
|
102
110
|
readonly cwd?: string;
|
|
103
111
|
readonly lastAssistantId?: string;
|
|
104
|
-
readonly
|
|
112
|
+
readonly notifiedExtensionMessages?: string[];
|
|
105
113
|
readonly name?: string;
|
|
106
114
|
readonly paneId: string;
|
|
107
115
|
readonly tabId: string;
|
|
@@ -142,26 +150,22 @@ export interface LatestInput {
|
|
|
142
150
|
text: string;
|
|
143
151
|
}
|
|
144
152
|
|
|
145
|
-
export interface
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
multiple: boolean;
|
|
152
|
-
title: string;
|
|
153
|
-
}>;
|
|
154
|
-
toolCallId: string;
|
|
153
|
+
export interface ExtensionMessage {
|
|
154
|
+
extension: string;
|
|
155
|
+
id: string;
|
|
156
|
+
message: string;
|
|
157
|
+
reply?: string;
|
|
158
|
+
blocking: boolean;
|
|
155
159
|
}
|
|
156
160
|
|
|
157
|
-
export interface
|
|
158
|
-
|
|
159
|
-
|
|
161
|
+
export interface ExtensionReply {
|
|
162
|
+
extension: string;
|
|
163
|
+
id: string;
|
|
164
|
+
message: string;
|
|
160
165
|
}
|
|
161
166
|
|
|
162
167
|
export interface SessionView {
|
|
163
|
-
|
|
164
|
-
askResults?: Record<string, AskResult>;
|
|
168
|
+
extensionMessages?: ExtensionMessage[];
|
|
165
169
|
assistant?: LatestAssistant;
|
|
166
170
|
assistantAfterInput?: boolean;
|
|
167
171
|
input?: LatestInput;
|