@howaboua/pi-shepherdr 0.2.21 → 0.2.23
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 +25 -0
- package/README.md +27 -1
- package/index.ts +34 -2
- package/package.json +4 -4
- package/src/agents-discovery.ts +23 -15
- package/src/agents-spawn.ts +141 -135
- package/src/agents-tool.ts +13 -4
- package/src/agents-work.ts +30 -6
- package/src/board/host.ts +3 -2
- package/src/launch.ts +43 -13
- package/src/messages.ts +24 -6
- package/src/monitor-extension-messages.ts +14 -1
- package/src/monitor.ts +2 -0
- package/src/profile-prompt.ts +32 -0
- package/src/profiles.ts +5 -22
- package/src/remote-client.ts +12 -2
- package/src/spawn-context.ts +14 -2
- package/src/worker-role.ts +83 -0
- package/src/worker-service.ts +397 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
import { isAbsolute } from "node:path";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { activityTask } from "./activity.js";
|
|
7
|
+
import { dispatchAgentWork, reserveAgentName } from "./agents-work.js";
|
|
8
|
+
import type { AgentBoard } from "./board/host.js";
|
|
9
|
+
import type { AgentFleet } from "./fleet.js";
|
|
10
|
+
import { getSnapshot, sessionPath } from "./herdr.js";
|
|
11
|
+
import {
|
|
12
|
+
RolledBackAgentLaunchError,
|
|
13
|
+
rollbackStartedAgent,
|
|
14
|
+
startAgent,
|
|
15
|
+
UnstartedAgentLaunchError,
|
|
16
|
+
} from "./launch.js";
|
|
17
|
+
import { attributeAgentPrompt } from "./messages.js";
|
|
18
|
+
import { loadAgentProfiles, profileAgentArgs } from "./profiles.js";
|
|
19
|
+
import type { SharedAgentContext } from "./shared-context.js";
|
|
20
|
+
import type { PaneInfo } from "./types.js";
|
|
21
|
+
import { PULSE_WORKER_FLAG, type registerWorkerRole } from "./worker-role.js";
|
|
22
|
+
|
|
23
|
+
const ENTRY = "herdr-owned-worker-v1";
|
|
24
|
+
const RUNNER_PROTOCOL = "@howaboua/pi-pulses/runner/v1";
|
|
25
|
+
// Structural implementation of Pulses' optional runner ABI, without a private-package dependency.
|
|
26
|
+
interface PulseRun {
|
|
27
|
+
taskId: string;
|
|
28
|
+
title: string;
|
|
29
|
+
prompt: string;
|
|
30
|
+
cwd: string;
|
|
31
|
+
runId: string;
|
|
32
|
+
workerId?: string;
|
|
33
|
+
}
|
|
34
|
+
interface PulseWorker {
|
|
35
|
+
workerId: string;
|
|
36
|
+
state: "working" | "idle" | "closed";
|
|
37
|
+
}
|
|
38
|
+
interface OwnedWorker {
|
|
39
|
+
workerId: string;
|
|
40
|
+
pulseId: string;
|
|
41
|
+
runId: string;
|
|
42
|
+
parent: string;
|
|
43
|
+
host: string;
|
|
44
|
+
session: string;
|
|
45
|
+
target: string;
|
|
46
|
+
terminal: string;
|
|
47
|
+
sessionPath: string;
|
|
48
|
+
cwd: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function records(ctx: ExtensionContext): OwnedWorker[] {
|
|
52
|
+
const workers = new Map<string, OwnedWorker>();
|
|
53
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
54
|
+
if (entry.type !== "custom" || entry.customType !== ENTRY) continue;
|
|
55
|
+
const value = entry.data as Record<string, unknown> | undefined;
|
|
56
|
+
if (
|
|
57
|
+
!value ||
|
|
58
|
+
![
|
|
59
|
+
"workerId",
|
|
60
|
+
"pulseId",
|
|
61
|
+
"runId",
|
|
62
|
+
"parent",
|
|
63
|
+
"host",
|
|
64
|
+
"session",
|
|
65
|
+
"target",
|
|
66
|
+
"terminal",
|
|
67
|
+
"sessionPath",
|
|
68
|
+
"cwd",
|
|
69
|
+
].every((key) => typeof value[key] === "string")
|
|
70
|
+
)
|
|
71
|
+
throw new Error(
|
|
72
|
+
"Saved pulse worker ownership is invalid; inspect the session before retrying",
|
|
73
|
+
);
|
|
74
|
+
const worker = value as unknown as OwnedWorker;
|
|
75
|
+
if (worker.parent === ctx.sessionManager.getSessionFile())
|
|
76
|
+
workers.set(worker.workerId, worker);
|
|
77
|
+
}
|
|
78
|
+
return [...workers.values()];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function registerWorkerService(
|
|
82
|
+
pi: ExtensionAPI,
|
|
83
|
+
fleet: AgentFleet,
|
|
84
|
+
shared: SharedAgentContext,
|
|
85
|
+
board: AgentBoard,
|
|
86
|
+
workerRole: ReturnType<typeof registerWorkerRole>,
|
|
87
|
+
): void {
|
|
88
|
+
let active: ExtensionContext | undefined;
|
|
89
|
+
let busy = false;
|
|
90
|
+
const isController = (ctx: ExtensionContext) =>
|
|
91
|
+
ctx.mode === "tui" && !workerRole.isPulseWorker(ctx.sessionManager);
|
|
92
|
+
function assertContext(ctx: ExtensionContext) {
|
|
93
|
+
if (!isController(ctx))
|
|
94
|
+
throw new Error("Detached workers cannot claim or schedule pulses");
|
|
95
|
+
if (
|
|
96
|
+
!active ||
|
|
97
|
+
!ctx.sessionManager.getSessionFile() ||
|
|
98
|
+
ctx.sessionManager.getSessionFile() !==
|
|
99
|
+
active.sessionManager.getSessionFile()
|
|
100
|
+
)
|
|
101
|
+
throw new Error(
|
|
102
|
+
"Worker service is unavailable for this session; retry after session startup",
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
async function owned(ctx: ExtensionContext, workerId: string) {
|
|
106
|
+
assertContext(ctx);
|
|
107
|
+
const worker = records(ctx).find((value) => value.workerId === workerId);
|
|
108
|
+
if (!worker) throw new Error("Worker is not owned by this session branch");
|
|
109
|
+
const identity = await fleet.connected().client.machineIdentity();
|
|
110
|
+
if (identity.host !== worker.host || identity.session !== worker.session)
|
|
111
|
+
throw new Error("Worker belongs to another host or Herdr session");
|
|
112
|
+
return worker;
|
|
113
|
+
}
|
|
114
|
+
async function panelFor(worker: OwnedWorker): Promise<PaneInfo | undefined> {
|
|
115
|
+
const snapshot = await getSnapshot(fleet.connected().client);
|
|
116
|
+
if (
|
|
117
|
+
snapshot.agents.some(
|
|
118
|
+
(value) =>
|
|
119
|
+
value.pane_id !== worker.target &&
|
|
120
|
+
sessionPath(value) === worker.sessionPath,
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
throw new Error(
|
|
124
|
+
"Worker session is open in another pane; refusing to control it",
|
|
125
|
+
);
|
|
126
|
+
const panel = snapshot.agents.find(
|
|
127
|
+
(value) => value.pane_id === worker.target,
|
|
128
|
+
);
|
|
129
|
+
if (panel) {
|
|
130
|
+
if (
|
|
131
|
+
panel.pane_id === process.env["HERDR_PANE_ID"] ||
|
|
132
|
+
panel.terminal_id !== worker.terminal ||
|
|
133
|
+
panel.agent !== "pi" ||
|
|
134
|
+
sessionPath(panel) !== worker.sessionPath
|
|
135
|
+
)
|
|
136
|
+
throw new Error("Worker pane identity changed; refusing to control it");
|
|
137
|
+
return panel;
|
|
138
|
+
}
|
|
139
|
+
if (
|
|
140
|
+
snapshot.agents.some((value) => sessionPath(value) === worker.sessionPath)
|
|
141
|
+
)
|
|
142
|
+
throw new Error(
|
|
143
|
+
"Worker session is open in another pane; refusing to control it",
|
|
144
|
+
);
|
|
145
|
+
return undefined;
|
|
146
|
+
}
|
|
147
|
+
async function inspect(
|
|
148
|
+
ctx: ExtensionContext,
|
|
149
|
+
workerId: string,
|
|
150
|
+
): Promise<PulseWorker> {
|
|
151
|
+
const worker = await owned(ctx, workerId);
|
|
152
|
+
const panel = await panelFor(worker);
|
|
153
|
+
const record = fleet
|
|
154
|
+
.connected()
|
|
155
|
+
.monitor.list()
|
|
156
|
+
.find((value) => value.terminalId === worker.terminal);
|
|
157
|
+
return {
|
|
158
|
+
workerId,
|
|
159
|
+
state: !panel
|
|
160
|
+
? "closed"
|
|
161
|
+
: (panel.agent_status !== "idle" && panel.agent_status !== "done") ||
|
|
162
|
+
panel.interactive_ready !== true ||
|
|
163
|
+
panel.launch_pending ||
|
|
164
|
+
(record &&
|
|
165
|
+
(record.activity.phase !== "settled" ||
|
|
166
|
+
activityTask(record.activity)))
|
|
167
|
+
? "working"
|
|
168
|
+
: "idle",
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
const service = {
|
|
172
|
+
protocol: RUNNER_PROTOCOL,
|
|
173
|
+
id: "shepherdr",
|
|
174
|
+
isController,
|
|
175
|
+
inspect,
|
|
176
|
+
subscribe: (listener: () => void) => fleet.subscribe(listener),
|
|
177
|
+
async start(ctx: ExtensionContext, input: PulseRun) {
|
|
178
|
+
let stage: "preflight" | "launch" | "dispatch" = "dispatch";
|
|
179
|
+
try {
|
|
180
|
+
stage = records(ctx).some(
|
|
181
|
+
(worker) =>
|
|
182
|
+
worker.pulseId === input.taskId && worker.runId === input.runId,
|
|
183
|
+
)
|
|
184
|
+
? "dispatch"
|
|
185
|
+
: "preflight";
|
|
186
|
+
assertContext(ctx);
|
|
187
|
+
const parentFile = ctx.sessionManager.getSessionFile();
|
|
188
|
+
const branchAnchor = ctx.sessionManager.getLeafId();
|
|
189
|
+
const assertBranch = () => {
|
|
190
|
+
assertContext(ctx);
|
|
191
|
+
if (
|
|
192
|
+
ctx.sessionManager.getSessionFile() !== parentFile ||
|
|
193
|
+
(branchAnchor &&
|
|
194
|
+
!ctx.sessionManager
|
|
195
|
+
.getBranch()
|
|
196
|
+
.some((entry) => entry.id === branchAnchor))
|
|
197
|
+
)
|
|
198
|
+
throw new Error(
|
|
199
|
+
"Parent branch changed during worker startup; inspect before retrying",
|
|
200
|
+
);
|
|
201
|
+
};
|
|
202
|
+
if (busy) throw new Error("A worker operation is already in progress");
|
|
203
|
+
for (const field of [
|
|
204
|
+
"taskId",
|
|
205
|
+
"title",
|
|
206
|
+
"prompt",
|
|
207
|
+
"cwd",
|
|
208
|
+
"runId",
|
|
209
|
+
] as const)
|
|
210
|
+
if (!input[field]?.trim()) throw new Error(`${field} is required`);
|
|
211
|
+
if (!isAbsolute(input.cwd))
|
|
212
|
+
throw new Error("Worker cwd must be absolute");
|
|
213
|
+
if (input.prompt.trimStart().startsWith("/"))
|
|
214
|
+
throw new Error("Scheduled work must be a task, not a slash command");
|
|
215
|
+
busy = true;
|
|
216
|
+
try {
|
|
217
|
+
const runtime = fleet.connected();
|
|
218
|
+
let worker = records(ctx).find(
|
|
219
|
+
(value) => value.pulseId === input.taskId,
|
|
220
|
+
);
|
|
221
|
+
if (input.workerId && input.workerId !== worker?.workerId)
|
|
222
|
+
throw new Error("Worker is not owned by this pulse in this branch");
|
|
223
|
+
if (worker) await owned(ctx, worker.workerId);
|
|
224
|
+
let panel = worker ? await panelFor(worker) : undefined;
|
|
225
|
+
if (worker?.runId === input.runId) {
|
|
226
|
+
stage = "dispatch";
|
|
227
|
+
return await inspect(ctx, worker.workerId);
|
|
228
|
+
}
|
|
229
|
+
if (
|
|
230
|
+
worker &&
|
|
231
|
+
(await inspect(ctx, worker.workerId)).state === "working"
|
|
232
|
+
)
|
|
233
|
+
throw new Error("Worker already has active responsibility");
|
|
234
|
+
if (worker && worker.cwd !== input.cwd)
|
|
235
|
+
throw new Error(
|
|
236
|
+
"Saved worker cwd differs; use its original working directory",
|
|
237
|
+
);
|
|
238
|
+
if (!panel) {
|
|
239
|
+
const profile = (await loadAgentProfiles()).get("general");
|
|
240
|
+
if (!profile)
|
|
241
|
+
throw new Error("General worker profile is unavailable");
|
|
242
|
+
const profileArgs = profileAgentArgs(profile, {
|
|
243
|
+
includePrompt: false,
|
|
244
|
+
});
|
|
245
|
+
if (
|
|
246
|
+
profileArgs.some((arg) =>
|
|
247
|
+
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
248
|
+
arg,
|
|
249
|
+
),
|
|
250
|
+
)
|
|
251
|
+
)
|
|
252
|
+
throw new Error(
|
|
253
|
+
"Worker profile cannot select an existing session",
|
|
254
|
+
);
|
|
255
|
+
const args = [
|
|
256
|
+
...profileArgs,
|
|
257
|
+
`--${PULSE_WORKER_FLAG}`,
|
|
258
|
+
...(worker ? ["--session", worker.sessionPath] : []),
|
|
259
|
+
];
|
|
260
|
+
const reservation = await reserveAgentName(runtime, input.title);
|
|
261
|
+
const { name } = reservation;
|
|
262
|
+
try {
|
|
263
|
+
const relationship = worker
|
|
264
|
+
? undefined
|
|
265
|
+
: shared.spawns.prepareRelationship(ctx, runtime, args);
|
|
266
|
+
const sharing =
|
|
267
|
+
worker || profile.shareContext === false
|
|
268
|
+
? undefined
|
|
269
|
+
: await shared.spawns.prepare(ctx, runtime, name, args);
|
|
270
|
+
const boardBinding = worker
|
|
271
|
+
? undefined
|
|
272
|
+
: await board.prepare(ctx, runtime, name, args);
|
|
273
|
+
const workspace = runtime.local
|
|
274
|
+
? process.env["HERDR_WORKSPACE_ID"]
|
|
275
|
+
: undefined;
|
|
276
|
+
stage = "launch";
|
|
277
|
+
const started = await startAgent(
|
|
278
|
+
runtime.client,
|
|
279
|
+
{
|
|
280
|
+
name,
|
|
281
|
+
label: input.title,
|
|
282
|
+
cwd: input.cwd,
|
|
283
|
+
placement: workspace ? "new_tab" : "new_workspace",
|
|
284
|
+
...(workspace ? { workspace } : {}),
|
|
285
|
+
},
|
|
286
|
+
runtime.fallbackCwd,
|
|
287
|
+
runtime.resolveDirectory,
|
|
288
|
+
{ agentArgs: args },
|
|
289
|
+
);
|
|
290
|
+
try {
|
|
291
|
+
const relationshipResult = await relationship?.accept(
|
|
292
|
+
started.agent,
|
|
293
|
+
);
|
|
294
|
+
await boardBinding?.accept(started.agent);
|
|
295
|
+
const sharingResult = await sharing?.accept(started.agent);
|
|
296
|
+
for (const warning of [
|
|
297
|
+
relationshipResult?.warning,
|
|
298
|
+
sharingResult?.warning,
|
|
299
|
+
])
|
|
300
|
+
if (warning) ctx.ui.notify(warning, "warning");
|
|
301
|
+
assertBranch();
|
|
302
|
+
panel = started.agent;
|
|
303
|
+
const file = sessionPath(panel);
|
|
304
|
+
if (!file || (worker && file !== worker.sessionPath))
|
|
305
|
+
throw new Error("Worker did not restore its saved session");
|
|
306
|
+
const identity = await runtime.client.machineIdentity();
|
|
307
|
+
worker = {
|
|
308
|
+
workerId: worker?.workerId ?? crypto.randomUUID(),
|
|
309
|
+
pulseId: input.taskId,
|
|
310
|
+
runId: "",
|
|
311
|
+
parent: ctx.sessionManager.getSessionFile()!,
|
|
312
|
+
...identity,
|
|
313
|
+
target: panel.pane_id,
|
|
314
|
+
terminal: panel.terminal_id,
|
|
315
|
+
sessionPath: file,
|
|
316
|
+
cwd: input.cwd,
|
|
317
|
+
};
|
|
318
|
+
pi.appendEntry(ENTRY, worker);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
return await rollbackStartedAgent(
|
|
321
|
+
runtime.client,
|
|
322
|
+
started,
|
|
323
|
+
error,
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
stage = "preflight";
|
|
327
|
+
} finally {
|
|
328
|
+
reservation.release();
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (!worker) throw new Error("Worker ownership is unavailable");
|
|
332
|
+
const attributed = await attributeAgentPrompt(
|
|
333
|
+
runtime.client,
|
|
334
|
+
input.prompt,
|
|
335
|
+
"task",
|
|
336
|
+
);
|
|
337
|
+
assertBranch();
|
|
338
|
+
const dispatch = await dispatchAgentWork(
|
|
339
|
+
runtime,
|
|
340
|
+
panel,
|
|
341
|
+
input.prompt,
|
|
342
|
+
false,
|
|
343
|
+
new AbortController().signal,
|
|
344
|
+
() => {},
|
|
345
|
+
() => {
|
|
346
|
+
assertBranch();
|
|
347
|
+
stage = "dispatch";
|
|
348
|
+
return runtime.client.sendMessage(panel!, attributed);
|
|
349
|
+
},
|
|
350
|
+
{ expectUserMessage: true },
|
|
351
|
+
);
|
|
352
|
+
worker = { ...worker, runId: input.runId };
|
|
353
|
+
assertBranch();
|
|
354
|
+
pi.appendEntry(ENTRY, worker);
|
|
355
|
+
if (dispatch.warning) ctx.ui.notify(dispatch.warning, "warning");
|
|
356
|
+
return inspect(ctx, worker.workerId);
|
|
357
|
+
} finally {
|
|
358
|
+
busy = false;
|
|
359
|
+
}
|
|
360
|
+
} catch (error) {
|
|
361
|
+
if (
|
|
362
|
+
stage === "preflight" ||
|
|
363
|
+
(stage === "launch" &&
|
|
364
|
+
(error instanceof RolledBackAgentLaunchError ||
|
|
365
|
+
error instanceof UnstartedAgentLaunchError))
|
|
366
|
+
)
|
|
367
|
+
throw Object.assign(
|
|
368
|
+
new Error(error instanceof Error ? error.message : String(error), {
|
|
369
|
+
cause: error,
|
|
370
|
+
}),
|
|
371
|
+
{ dispatched: false as const },
|
|
372
|
+
);
|
|
373
|
+
throw error;
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
};
|
|
377
|
+
pi.events.on(`${RUNNER_PROTOCOL}/request`, (request: unknown) => {
|
|
378
|
+
const value = request as {
|
|
379
|
+
protocol?: unknown;
|
|
380
|
+
accept?: (value: typeof service) => void;
|
|
381
|
+
};
|
|
382
|
+
if (
|
|
383
|
+
value?.protocol === RUNNER_PROTOCOL &&
|
|
384
|
+
typeof value.accept === "function"
|
|
385
|
+
)
|
|
386
|
+
value.accept(service);
|
|
387
|
+
});
|
|
388
|
+
pi.on("session_start", () => {
|
|
389
|
+
active = undefined;
|
|
390
|
+
});
|
|
391
|
+
pi.on("resources_discover", (_event, ctx) => {
|
|
392
|
+
active = ctx;
|
|
393
|
+
});
|
|
394
|
+
pi.on("session_shutdown", () => {
|
|
395
|
+
active = undefined;
|
|
396
|
+
});
|
|
397
|
+
}
|