@howaboua/pi-shepherdr 0.2.24 → 0.2.26
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 +26 -0
- package/README.md +20 -12
- package/index.ts +2 -2
- package/package.json +10 -11
- package/src/agent-identity.ts +75 -0
- package/src/agents-contract.ts +20 -12
- package/src/agents-discovery.ts +79 -34
- package/src/agents-focus.ts +15 -9
- package/src/agents-spawn.ts +25 -7
- package/src/agents-tool.ts +35 -7
- package/src/agents-work.ts +2 -2
- package/src/attachment-notes.ts +25 -24
- package/src/attachment-protocol.ts +35 -15
- package/src/attachment-routing.ts +28 -24
- package/src/attachment-target.ts +58 -19
- package/src/attachment.ts +13 -17
- package/src/board/host.ts +19 -20
- package/src/board/membership.ts +49 -30
- package/src/close-idle.ts +89 -0
- package/src/context-routing.ts +196 -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 +161 -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/messages/guidance.ts +22 -16
- package/src/monitor-extension-messages.ts +9 -2
- package/src/monitor-record.ts +13 -6
- package/src/monitor-state.ts +18 -4
- 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-checkpoints.mjs +20 -2
- package/src/remote/shepherdr-context.mjs +15 -3
- 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 -18
- package/src/shared-context.ts +139 -12
- package/src/spawn-context.ts +10 -5
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +10 -5
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +13 -82
- package/changelog.ts +0 -337
|
@@ -111,21 +111,6 @@ export async function registerSharedAgentContext(
|
|
|
111
111
|
request: SharedContextRequest,
|
|
112
112
|
signal?: AbortSignal,
|
|
113
113
|
) => {
|
|
114
|
-
const attached = await shared.attachment.routing.route(
|
|
115
|
-
context,
|
|
116
|
-
request,
|
|
117
|
-
[],
|
|
118
|
-
(route, next) =>
|
|
119
|
-
fleet
|
|
120
|
-
.connected(route.machine)
|
|
121
|
-
.client.requestContext(
|
|
122
|
-
sessionContextPath(route.sessionFile),
|
|
123
|
-
next,
|
|
124
|
-
signal,
|
|
125
|
-
),
|
|
126
|
-
signal,
|
|
127
|
-
);
|
|
128
|
-
if (attached) return attached;
|
|
129
114
|
return shared.handle(
|
|
130
115
|
context,
|
|
131
116
|
{ operation: "context", request, visited: [] },
|
|
@@ -145,15 +130,19 @@ export async function registerSharedAgentContext(
|
|
|
145
130
|
),
|
|
146
131
|
);
|
|
147
132
|
});
|
|
148
|
-
pi.on("resources_discover", async (_event, ctx) =>
|
|
133
|
+
pi.on("resources_discover", async (_event, ctx) => {
|
|
134
|
+
await publish(ctx);
|
|
135
|
+
await fleet.restoreSharing(ctx);
|
|
136
|
+
});
|
|
149
137
|
pi.on("session_shutdown", async () => {
|
|
150
138
|
unregister?.();
|
|
151
139
|
await close?.();
|
|
152
140
|
connection?.dispose();
|
|
153
141
|
relationships?.dispose();
|
|
154
142
|
});
|
|
155
|
-
fleet.setContextRelay(
|
|
156
|
-
shared.handle(ctx, request, signal),
|
|
143
|
+
fleet.setContextRelay(
|
|
144
|
+
(ctx, request, signal) => shared.handle(ctx, request, signal),
|
|
145
|
+
(ctx, machine) => shared.needsRelay(ctx, machine),
|
|
157
146
|
);
|
|
158
147
|
return shared;
|
|
159
148
|
}
|
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,8 +16,17 @@ 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 {
|
|
19
|
+
import {
|
|
20
|
+
attachmentRoutes,
|
|
21
|
+
canonicalContextIdentity,
|
|
22
|
+
Identity,
|
|
23
|
+
} from "./attachment-protocol.js";
|
|
16
24
|
import type { AgentBoard } from "./board/host.js";
|
|
25
|
+
import {
|
|
26
|
+
contextRouteMatches,
|
|
27
|
+
countContextTargets,
|
|
28
|
+
sendContextRoute,
|
|
29
|
+
} from "./context-routing.js";
|
|
17
30
|
import type { AgentFleet } from "./fleet/index.js";
|
|
18
31
|
import { sessionPath } from "./herdr.js";
|
|
19
32
|
import {
|
|
@@ -30,6 +43,7 @@ interface ContextEnvelope {
|
|
|
30
43
|
operation: "context";
|
|
31
44
|
request: SharedContextRequest;
|
|
32
45
|
visited: string[];
|
|
46
|
+
downstream?: boolean;
|
|
33
47
|
}
|
|
34
48
|
|
|
35
49
|
export class SharedAgentContext {
|
|
@@ -64,11 +78,46 @@ export class SharedAgentContext {
|
|
|
64
78
|
});
|
|
65
79
|
}
|
|
66
80
|
|
|
81
|
+
needsRelay(ctx: ExtensionContext, machine: string): boolean {
|
|
82
|
+
if (children(ctx).some((child) => child.machine === machine)) return true;
|
|
83
|
+
if (
|
|
84
|
+
attachmentRoutes(ctx).some(
|
|
85
|
+
(route) =>
|
|
86
|
+
route.machine === machine &&
|
|
87
|
+
((route.plan.context && !route.detached?.context) ||
|
|
88
|
+
(route.plan.board && !route.detached?.board)),
|
|
89
|
+
)
|
|
90
|
+
)
|
|
91
|
+
return true;
|
|
92
|
+
const identity = this.getService()?.describe(ctx);
|
|
93
|
+
if (!identity) return false;
|
|
94
|
+
return ctx.sessionManager.getEntries().some((entry) => {
|
|
95
|
+
if (entry.type !== "custom" || entry.customType !== MEMBER_ENTRY)
|
|
96
|
+
return false;
|
|
97
|
+
const child = entry.data as Partial<Member> | undefined;
|
|
98
|
+
return (
|
|
99
|
+
child?.machine === machine &&
|
|
100
|
+
child.threadId === identity.threadId &&
|
|
101
|
+
child.sessionId === identity.sessionId &&
|
|
102
|
+
typeof child.sessionFile === "string" &&
|
|
103
|
+
!!child.sessionFile &&
|
|
104
|
+
typeof child.agentName === "string"
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
67
109
|
async handle(
|
|
68
110
|
ctx: ExtensionContext,
|
|
69
111
|
value: unknown,
|
|
70
112
|
signal?: AbortSignal,
|
|
71
113
|
): Promise<unknown> {
|
|
114
|
+
if (
|
|
115
|
+
value &&
|
|
116
|
+
typeof value === "object" &&
|
|
117
|
+
"operation" in value &&
|
|
118
|
+
value.operation === "agent-identity"
|
|
119
|
+
)
|
|
120
|
+
return { sessionId: ctx.sessionManager.getSessionId() };
|
|
72
121
|
if (
|
|
73
122
|
value &&
|
|
74
123
|
typeof value === "object" &&
|
|
@@ -98,7 +147,7 @@ export class SharedAgentContext {
|
|
|
98
147
|
const upstream =
|
|
99
148
|
value.scope === "board"
|
|
100
149
|
? own.upstream
|
|
101
|
-
: identity?.
|
|
150
|
+
: identity?.threadId !== identity?.sessionId &&
|
|
102
151
|
routing?.transport === "shepherdr"
|
|
103
152
|
? routing.parent
|
|
104
153
|
: undefined;
|
|
@@ -112,8 +161,8 @@ export class SharedAgentContext {
|
|
|
112
161
|
signal,
|
|
113
162
|
);
|
|
114
163
|
if (
|
|
115
|
-
(value.scope === "board" ? own.
|
|
116
|
-
|
|
164
|
+
(value.scope === "board" ? own.sessionId : identity?.threadId) !==
|
|
165
|
+
family
|
|
117
166
|
)
|
|
118
167
|
throw new Error("No live /root in this agent family");
|
|
119
168
|
return {
|
|
@@ -143,7 +192,10 @@ export class SharedAgentContext {
|
|
|
143
192
|
!file ||
|
|
144
193
|
!("identity" in value) ||
|
|
145
194
|
!Check(Identity, value.identity) ||
|
|
146
|
-
!isDeepStrictEqual(
|
|
195
|
+
!isDeepStrictEqual(
|
|
196
|
+
identity,
|
|
197
|
+
canonicalContextIdentity(value.identity),
|
|
198
|
+
) ||
|
|
147
199
|
identity?.threadId !== ctx.sessionManager.getSessionId()
|
|
148
200
|
)
|
|
149
201
|
throw new Error("Invalid saved checkpoint owner");
|
|
@@ -163,6 +215,14 @@ export class SharedAgentContext {
|
|
|
163
215
|
}
|
|
164
216
|
if (!identity || !service)
|
|
165
217
|
throw new Error("Context sharing is unavailable in this session");
|
|
218
|
+
if ("operation" in value && value.operation === "context-resolve")
|
|
219
|
+
return countContextTargets(
|
|
220
|
+
ctx,
|
|
221
|
+
identity,
|
|
222
|
+
value as Record<string, unknown>,
|
|
223
|
+
this.fleet,
|
|
224
|
+
signal,
|
|
225
|
+
);
|
|
166
226
|
const envelope = value as Partial<ContextEnvelope>;
|
|
167
227
|
const request = envelope.request;
|
|
168
228
|
if (
|
|
@@ -176,17 +236,49 @@ export class SharedAgentContext {
|
|
|
176
236
|
envelope.visited.includes(identity.threadId)
|
|
177
237
|
)
|
|
178
238
|
throw new Error("Invalid shared context route or session family");
|
|
179
|
-
|
|
239
|
+
const forwarded = {
|
|
240
|
+
...envelope,
|
|
241
|
+
visited: [...envelope.visited, identity.threadId],
|
|
242
|
+
};
|
|
243
|
+
const nativeTarget =
|
|
244
|
+
/^\/[^/]+$/.test(request.agentName) && request.agentName !== "/root";
|
|
245
|
+
const routing = identity.routing as Partial<Routing> | undefined;
|
|
246
|
+
let resolved: Awaited<ReturnType<typeof contextRouteMatches>> | undefined;
|
|
247
|
+
if (nativeTarget && !envelope.downstream) {
|
|
248
|
+
if (
|
|
249
|
+
routing?.transport === "shepherdr" &&
|
|
250
|
+
typeof routing.parent === "string"
|
|
251
|
+
)
|
|
252
|
+
return requestContext(routing.parent, forwarded, signal);
|
|
253
|
+
resolved = await contextRouteMatches(
|
|
254
|
+
ctx,
|
|
255
|
+
identity,
|
|
256
|
+
request.agentName,
|
|
257
|
+
[identity.threadId],
|
|
258
|
+
this.fleet,
|
|
259
|
+
signal,
|
|
260
|
+
);
|
|
261
|
+
const count = resolved.reduce(
|
|
262
|
+
(total, match) => total + match.count,
|
|
263
|
+
identity.agentName === request.agentName ? 1 : 0,
|
|
264
|
+
);
|
|
265
|
+
if (count > 1)
|
|
266
|
+
throw new Error(
|
|
267
|
+
`Multiple shared agents use ${request.agentName}; start sessions with distinct Pi IDs`,
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
if (
|
|
271
|
+
identity.agentName === request.agentName ||
|
|
272
|
+
identity.storageAgentName === request.agentName ||
|
|
273
|
+
(request.agentName === "/root" &&
|
|
274
|
+
identity.threadId === identity.sessionId)
|
|
275
|
+
) {
|
|
180
276
|
if (!ctx.isIdle() && !this.running)
|
|
181
277
|
throw new Error(
|
|
182
278
|
"Context owner is changing branches; retry after it settles",
|
|
183
279
|
);
|
|
184
280
|
return service.execute(ctx, request, signal);
|
|
185
281
|
}
|
|
186
|
-
const forwarded = {
|
|
187
|
-
...envelope,
|
|
188
|
-
visited: [...envelope.visited, identity.threadId],
|
|
189
|
-
};
|
|
190
282
|
const attached = await this.attachment.routing.route(
|
|
191
283
|
ctx,
|
|
192
284
|
request,
|
|
@@ -203,6 +295,7 @@ export class SharedAgentContext {
|
|
|
203
295
|
);
|
|
204
296
|
if (attached) return attached;
|
|
205
297
|
const member = ctx.sessionManager.getEntries().findLast((entry) => {
|
|
298
|
+
if (!request.agentName.startsWith("/root/")) return false;
|
|
206
299
|
if (entry.type !== "custom" || entry.customType !== MEMBER_ENTRY)
|
|
207
300
|
return false;
|
|
208
301
|
const candidate = entry.data as Member;
|
|
@@ -229,12 +322,46 @@ export class SharedAgentContext {
|
|
|
229
322
|
signal,
|
|
230
323
|
);
|
|
231
324
|
}
|
|
232
|
-
const routing = identity.routing as Partial<Routing> | undefined;
|
|
233
325
|
if (
|
|
326
|
+
!envelope.downstream &&
|
|
234
327
|
routing?.transport === "shepherdr" &&
|
|
235
328
|
typeof routing.parent === "string"
|
|
236
329
|
)
|
|
237
330
|
return requestContext(routing.parent, forwarded, signal);
|
|
331
|
+
if (nativeTarget) {
|
|
332
|
+
// Change direction only at the family owner. Descending never relays upstream.
|
|
333
|
+
const visited = envelope.downstream
|
|
334
|
+
? forwarded.visited
|
|
335
|
+
: [identity.threadId];
|
|
336
|
+
const matches =
|
|
337
|
+
resolved ??
|
|
338
|
+
(await contextRouteMatches(
|
|
339
|
+
ctx,
|
|
340
|
+
identity,
|
|
341
|
+
request.agentName,
|
|
342
|
+
visited,
|
|
343
|
+
this.fleet,
|
|
344
|
+
signal,
|
|
345
|
+
));
|
|
346
|
+
const count = matches.reduce((total, match) => total + match.count, 0);
|
|
347
|
+
if (count > 1)
|
|
348
|
+
throw new Error(
|
|
349
|
+
`Multiple shared agents use ${request.agentName}; start sessions with distinct Pi IDs`,
|
|
350
|
+
);
|
|
351
|
+
const match = matches.find((candidate) => candidate.count === 1);
|
|
352
|
+
if (match)
|
|
353
|
+
return sendContextRoute(
|
|
354
|
+
this.fleet,
|
|
355
|
+
match.route,
|
|
356
|
+
{
|
|
357
|
+
operation: "context",
|
|
358
|
+
request: { ...request, sessionId: match.route.sessionId },
|
|
359
|
+
visited,
|
|
360
|
+
downstream: true,
|
|
361
|
+
},
|
|
362
|
+
signal,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
238
365
|
throw new Error(
|
|
239
366
|
`No shared context agent ${request.agentName} in this session family`,
|
|
240
367
|
);
|
package/src/spawn-context.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
} from "@earendil-works/pi-coding-agent";
|
|
6
6
|
import type { CodexAgentRelationshipService } from "@howaboua/pi-codex-conversion/agent-relationship";
|
|
7
7
|
import type { ContextSharingService } from "@howaboua/pi-notes-compaction/context-sharing";
|
|
8
|
+
import { nativeAgentName } from "./agent-identity.js";
|
|
8
9
|
import type { ConnectedMachine } from "./fleet/index.js";
|
|
9
10
|
import { sessionPath } from "./herdr.js";
|
|
10
11
|
import { sessionContextPath } from "./remote/shepherdr-context.mjs";
|
|
@@ -38,6 +39,9 @@ export class SpawnContext {
|
|
|
38
39
|
this.getRelationships = getRelationships;
|
|
39
40
|
this.getService = getService;
|
|
40
41
|
}
|
|
42
|
+
canShare(ctx: ExtensionContext): boolean {
|
|
43
|
+
return this.getService()?.canCreateChild(ctx) === true;
|
|
44
|
+
}
|
|
41
45
|
prepareRelationship(
|
|
42
46
|
ctx: ExtensionContext,
|
|
43
47
|
runtime: ConnectedMachine,
|
|
@@ -97,7 +101,6 @@ export class SpawnContext {
|
|
|
97
101
|
async prepare(
|
|
98
102
|
ctx: ExtensionContext,
|
|
99
103
|
runtime: ConnectedMachine,
|
|
100
|
-
name: string,
|
|
101
104
|
args: readonly string[] = [],
|
|
102
105
|
) {
|
|
103
106
|
const service = this.getService();
|
|
@@ -119,21 +122,23 @@ 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
|
|
125
129
|
? sessionContextPath(parentFile)
|
|
126
130
|
: runtime.client.contextRelayPath(),
|
|
127
131
|
};
|
|
128
|
-
const { binding, adopt } = await service.createChild(ctx, {
|
|
129
|
-
name,
|
|
130
|
-
routing,
|
|
131
|
-
});
|
|
132
132
|
return {
|
|
133
133
|
accept: async (agent: PaneInfo) => {
|
|
134
134
|
const sessionFile = sessionPath(agent);
|
|
135
135
|
if (!sessionFile)
|
|
136
136
|
throw new Error("Spawned agent has no native Pi session path");
|
|
137
|
+
const agentName = await nativeAgentName(runtime.client, agent);
|
|
138
|
+
const { binding, adopt } = await service.createChild(ctx, {
|
|
139
|
+
threadId: agentName.slice(1),
|
|
140
|
+
routing,
|
|
141
|
+
});
|
|
137
142
|
const value = await runtime.client.requestContext(
|
|
138
143
|
sessionContextPath(sessionFile),
|
|
139
144
|
{ operation: "bind", binding },
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { ConnectedMachine } from "./fleet/index.js";
|
|
6
|
+
import { sessionPath } from "./herdr.js";
|
|
7
|
+
import type { PaneInfo } from "./types.js";
|
|
8
|
+
|
|
9
|
+
const ENTRY = "herdr-spawn-ownership-v1";
|
|
10
|
+
|
|
11
|
+
export interface SpawnOwnership {
|
|
12
|
+
parent: string;
|
|
13
|
+
machine: string;
|
|
14
|
+
host: string;
|
|
15
|
+
session: string;
|
|
16
|
+
pane: string;
|
|
17
|
+
terminal: string;
|
|
18
|
+
sessionFile: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function spawnedChildren(ctx: ExtensionContext): SpawnOwnership[] {
|
|
22
|
+
const children = new Map<string, SpawnOwnership>();
|
|
23
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
24
|
+
if (entry.type !== "custom" || entry.customType !== ENTRY) continue;
|
|
25
|
+
const value: unknown = entry.data;
|
|
26
|
+
if (!value || typeof value !== "object") continue;
|
|
27
|
+
if (
|
|
28
|
+
!("parent" in value && typeof value.parent === "string") ||
|
|
29
|
+
!("machine" in value && typeof value.machine === "string") ||
|
|
30
|
+
!("host" in value && typeof value.host === "string") ||
|
|
31
|
+
!("session" in value && typeof value.session === "string") ||
|
|
32
|
+
!("pane" in value && typeof value.pane === "string") ||
|
|
33
|
+
!("terminal" in value && typeof value.terminal === "string") ||
|
|
34
|
+
!("sessionFile" in value && typeof value.sessionFile === "string") ||
|
|
35
|
+
value.parent !== ctx.sessionManager.getSessionFile()
|
|
36
|
+
)
|
|
37
|
+
continue;
|
|
38
|
+
const child: SpawnOwnership = {
|
|
39
|
+
parent: value.parent,
|
|
40
|
+
machine: value.machine,
|
|
41
|
+
host: value.host,
|
|
42
|
+
session: value.session,
|
|
43
|
+
pane: value.pane,
|
|
44
|
+
terminal: value.terminal,
|
|
45
|
+
sessionFile: value.sessionFile,
|
|
46
|
+
};
|
|
47
|
+
children.set(JSON.stringify([child.machine, child.pane]), child);
|
|
48
|
+
}
|
|
49
|
+
return [...children.values()];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Capture before launch. A context switch must never adopt a completed spawn.
|
|
53
|
+
export function prepareSpawnOwnership(
|
|
54
|
+
pi: ExtensionAPI,
|
|
55
|
+
ctx: ExtensionContext,
|
|
56
|
+
runtime: ConnectedMachine,
|
|
57
|
+
): (panel: PaneInfo, args: readonly string[]) => Promise<void> {
|
|
58
|
+
const parent = ctx.sessionManager.getSessionFile();
|
|
59
|
+
const anchor = ctx.sessionManager.getLeafId();
|
|
60
|
+
return async (panel, args) => {
|
|
61
|
+
// Launching a resumed identity does not make that session our child.
|
|
62
|
+
if (
|
|
63
|
+
args.some((arg) =>
|
|
64
|
+
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
65
|
+
arg,
|
|
66
|
+
),
|
|
67
|
+
)
|
|
68
|
+
)
|
|
69
|
+
return;
|
|
70
|
+
const identity = await runtime.client.machineIdentity();
|
|
71
|
+
const file = sessionPath(panel);
|
|
72
|
+
if (
|
|
73
|
+
!parent ||
|
|
74
|
+
!file ||
|
|
75
|
+
parent !== ctx.sessionManager.getSessionFile() ||
|
|
76
|
+
(anchor &&
|
|
77
|
+
!ctx.sessionManager.getBranch().some((entry) => entry.id === anchor))
|
|
78
|
+
)
|
|
79
|
+
throw new Error(
|
|
80
|
+
"Parent context changed during spawn; retry from the intended session",
|
|
81
|
+
);
|
|
82
|
+
pi.appendEntry(ENTRY, {
|
|
83
|
+
parent,
|
|
84
|
+
machine: runtime.machine,
|
|
85
|
+
...identity,
|
|
86
|
+
pane: panel.pane_id,
|
|
87
|
+
terminal: panel.terminal_id,
|
|
88
|
+
sessionFile: file,
|
|
89
|
+
} satisfies SpawnOwnership);
|
|
90
|
+
};
|
|
91
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -4,6 +4,7 @@ export interface PeerMessage {
|
|
|
4
4
|
text: string;
|
|
5
5
|
sender: string;
|
|
6
6
|
context?: string;
|
|
7
|
+
closeIfIdle?: true;
|
|
7
8
|
}
|
|
8
9
|
|
|
9
10
|
export interface PeerDelivery {
|
|
@@ -24,7 +25,8 @@ export interface MachineIdentity {
|
|
|
24
25
|
|
|
25
26
|
export interface FocusRequest {
|
|
26
27
|
source: MachineIdentity & {
|
|
27
|
-
|
|
28
|
+
backend?: "core";
|
|
29
|
+
pane?: string;
|
|
28
30
|
terminal: string;
|
|
29
31
|
sessionFile: string;
|
|
30
32
|
};
|
|
@@ -69,6 +71,7 @@ interface AgentSessionInfo {
|
|
|
69
71
|
}
|
|
70
72
|
|
|
71
73
|
export interface PaneInfo {
|
|
74
|
+
backend?: "core";
|
|
72
75
|
agent?: string | null;
|
|
73
76
|
agent_session?: AgentSessionInfo | null;
|
|
74
77
|
agent_status: AgentStatus;
|
|
@@ -80,9 +83,9 @@ export interface PaneInfo {
|
|
|
80
83
|
name?: string | null;
|
|
81
84
|
pane_id: string;
|
|
82
85
|
title?: string | null;
|
|
83
|
-
tab_id
|
|
86
|
+
tab_id?: string;
|
|
84
87
|
terminal_id: string;
|
|
85
|
-
workspace_id
|
|
88
|
+
workspace_id?: string;
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
interface TabInfo {
|
|
@@ -105,6 +108,7 @@ export interface SessionSnapshot {
|
|
|
105
108
|
|
|
106
109
|
export interface MonitoredAgent {
|
|
107
110
|
readonly instructions?: string;
|
|
111
|
+
readonly latestInstruction?: string;
|
|
108
112
|
readonly scope: "task" | "persistent";
|
|
109
113
|
readonly activity: AgentActivity;
|
|
110
114
|
readonly cwd?: string;
|
|
@@ -112,9 +116,9 @@ export interface MonitoredAgent {
|
|
|
112
116
|
readonly notifiedExtensionMessages?: string[];
|
|
113
117
|
readonly name?: string;
|
|
114
118
|
readonly paneId: string;
|
|
115
|
-
readonly tabId
|
|
119
|
+
readonly tabId?: string;
|
|
116
120
|
readonly terminalId: string;
|
|
117
|
-
readonly workspaceId
|
|
121
|
+
readonly workspaceId?: string;
|
|
118
122
|
}
|
|
119
123
|
|
|
120
124
|
type MachineConnectionStatus = "connected" | "connecting" | "unavailable";
|
|
@@ -167,6 +171,7 @@ export interface ExtensionReply {
|
|
|
167
171
|
export interface SessionView {
|
|
168
172
|
extensionMessages?: ExtensionMessage[];
|
|
169
173
|
assistant?: LatestAssistant;
|
|
174
|
+
assistantPending?: boolean;
|
|
170
175
|
assistantAfterInput?: boolean;
|
|
171
176
|
input?: LatestInput;
|
|
172
177
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import { CoreConnection } from "./core/backend.js";
|
|
3
|
+
import type { AgentFleet } from "./fleet/index.js";
|
|
4
|
+
import { getSnapshot, sessionPath } from "./herdr.js";
|
|
5
|
+
import type { PaneInfo } from "./types.js";
|
|
6
|
+
|
|
7
|
+
export const ENTRY = "herdr-owned-worker-v1";
|
|
8
|
+
|
|
9
|
+
export interface OwnedWorker {
|
|
10
|
+
workerId: string;
|
|
11
|
+
pulseId: string;
|
|
12
|
+
runId: string;
|
|
13
|
+
parent: string;
|
|
14
|
+
host: string;
|
|
15
|
+
session: string;
|
|
16
|
+
target: string;
|
|
17
|
+
terminal: string;
|
|
18
|
+
sessionPath: string;
|
|
19
|
+
cwd: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function records(ctx: ExtensionContext): OwnedWorker[] {
|
|
23
|
+
const workers = new Map<string, OwnedWorker>();
|
|
24
|
+
for (const entry of ctx.sessionManager.getBranch()) {
|
|
25
|
+
if (entry.type !== "custom" || entry.customType !== ENTRY) continue;
|
|
26
|
+
const value = entry.data as Record<string, unknown> | undefined;
|
|
27
|
+
if (
|
|
28
|
+
!value ||
|
|
29
|
+
![
|
|
30
|
+
"workerId",
|
|
31
|
+
"pulseId",
|
|
32
|
+
"runId",
|
|
33
|
+
"parent",
|
|
34
|
+
"host",
|
|
35
|
+
"session",
|
|
36
|
+
"target",
|
|
37
|
+
"terminal",
|
|
38
|
+
"sessionPath",
|
|
39
|
+
"cwd",
|
|
40
|
+
].every((key) => typeof value[key] === "string")
|
|
41
|
+
)
|
|
42
|
+
throw new Error(
|
|
43
|
+
"Saved pulse worker ownership is invalid; inspect the session before retrying",
|
|
44
|
+
);
|
|
45
|
+
const worker = value as unknown as OwnedWorker;
|
|
46
|
+
if (worker.parent === ctx.sessionManager.getSessionFile())
|
|
47
|
+
workers.set(worker.workerId, worker);
|
|
48
|
+
}
|
|
49
|
+
return [...workers.values()];
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function panelFor(
|
|
53
|
+
fleet: AgentFleet,
|
|
54
|
+
worker: OwnedWorker,
|
|
55
|
+
): Promise<PaneInfo | undefined> {
|
|
56
|
+
const snapshot = await getSnapshot(fleet.connected().client);
|
|
57
|
+
if (
|
|
58
|
+
snapshot.agents.some(
|
|
59
|
+
(value) =>
|
|
60
|
+
value.pane_id !== worker.target &&
|
|
61
|
+
sessionPath(value) === worker.sessionPath,
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
throw new Error(
|
|
65
|
+
"Worker session is open in another pane; refusing to control it",
|
|
66
|
+
);
|
|
67
|
+
const panel = snapshot.agents.find(
|
|
68
|
+
(value) => value.pane_id === worker.target,
|
|
69
|
+
);
|
|
70
|
+
if (panel) {
|
|
71
|
+
if (
|
|
72
|
+
panel.pane_id ===
|
|
73
|
+
process.env[
|
|
74
|
+
fleet.connected().client instanceof CoreConnection
|
|
75
|
+
? "HC_TERMINAL_ID"
|
|
76
|
+
: "HERDR_PANE_ID"
|
|
77
|
+
] ||
|
|
78
|
+
panel.terminal_id !== worker.terminal ||
|
|
79
|
+
panel.agent !== "pi" ||
|
|
80
|
+
sessionPath(panel) !== worker.sessionPath
|
|
81
|
+
)
|
|
82
|
+
throw new Error("Worker pane identity changed; refusing to control it");
|
|
83
|
+
return panel;
|
|
84
|
+
}
|
|
85
|
+
if (
|
|
86
|
+
snapshot.agents.some((value) => sessionPath(value) === worker.sessionPath)
|
|
87
|
+
)
|
|
88
|
+
throw new Error(
|
|
89
|
+
"Worker session is open in another pane; refusing to control it",
|
|
90
|
+
);
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|