@howaboua/pi-shepherdr 0.2.25 → 0.2.27
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 +18 -0
- package/README.md +3 -3
- package/index.ts +0 -2
- package/package.json +10 -11
- package/src/agent-identity.ts +75 -0
- package/src/agents-contract.ts +11 -6
- package/src/agents-discovery.ts +45 -26
- package/src/agents-spawn.ts +7 -5
- package/src/agents-tool.ts +15 -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 +11 -17
- package/src/board/host.ts +16 -19
- package/src/board/membership.ts +49 -30
- package/src/context-routing.ts +196 -0
- package/src/core/AGENTS.md +1 -0
- package/src/core/remote-endpoint.ts +0 -2
- package/src/core/spawn.ts +1 -1
- package/src/fleet/index.ts +12 -2
- package/src/messages/guidance.ts +22 -16
- package/src/monitor-extension-messages.ts +9 -2
- package/src/monitor-record.ts +4 -0
- package/src/monitor-state.ts +18 -4
- package/src/remote/deployment.ts +1 -1
- package/src/remote/shepherdr-checkpoints.mjs +20 -2
- package/src/remote/shepherdr-context.mjs +14 -2
- package/src/shared-context-host.ts +0 -15
- package/src/shared-context.ts +106 -11
- package/src/spawn-context.ts +6 -5
- package/src/types.ts +1 -0
- package/src/worker-service.ts +1 -1
- package/changelog.ts +0 -337
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
type AttachmentRoute,
|
|
11
11
|
attachmentOwner,
|
|
12
12
|
attachmentRoutes,
|
|
13
|
+
canonicalContextIdentity,
|
|
13
14
|
detachment,
|
|
14
15
|
inside,
|
|
15
16
|
remap,
|
|
@@ -17,6 +18,7 @@ import {
|
|
|
17
18
|
remapResult,
|
|
18
19
|
} from "./attachment-protocol.js";
|
|
19
20
|
import {
|
|
21
|
+
isContextUnavailable,
|
|
20
22
|
requestContext,
|
|
21
23
|
sessionContextPath,
|
|
22
24
|
} from "./remote/shepherdr-context.mjs";
|
|
@@ -45,19 +47,24 @@ export class AttachmentRouting {
|
|
|
45
47
|
(route) =>
|
|
46
48
|
route.phase === "ready" &&
|
|
47
49
|
route.plan.context &&
|
|
48
|
-
inside(request.agentName, route.plan.context.alias)
|
|
50
|
+
(inside(request.agentName, route.plan.context.alias) ||
|
|
51
|
+
request.agentName === `/${route.plan.targetSessionId}`),
|
|
49
52
|
);
|
|
50
53
|
if (child?.plan.context) {
|
|
51
54
|
const link = child.plan.context;
|
|
52
|
-
|
|
55
|
+
const alias =
|
|
56
|
+
request.agentName === `/${child.plan.targetSessionId}`
|
|
57
|
+
? request.agentName
|
|
58
|
+
: link.alias;
|
|
59
|
+
if (!isDeepStrictEqual(native, canonicalContextIdentity(link.controller)))
|
|
53
60
|
throw new Error(
|
|
54
61
|
"Shared notes are unavailable. Use messages to exchange the context you need.",
|
|
55
62
|
);
|
|
56
63
|
const next = {
|
|
57
64
|
...request,
|
|
58
65
|
sessionId: link.target.sessionId,
|
|
59
|
-
agentName: remap(request.agentName,
|
|
60
|
-
params: remapParams(request.params,
|
|
66
|
+
agentName: remap(request.agentName, alias, link.target.agentName),
|
|
67
|
+
params: remapParams(request.params, alias, link.target.agentName),
|
|
61
68
|
};
|
|
62
69
|
let result: SharedContextResult;
|
|
63
70
|
if (link.target.storage === "remote") {
|
|
@@ -80,7 +87,7 @@ export class AttachmentRouting {
|
|
|
80
87
|
})) as SharedContextResult;
|
|
81
88
|
} catch (error) {
|
|
82
89
|
signal?.throwIfAborted();
|
|
83
|
-
if (!this.notes.canRead(next) || !
|
|
90
|
+
if (!this.notes.canRead(next) || !isContextUnavailable(error))
|
|
84
91
|
throw error;
|
|
85
92
|
result = await this.notes.read(
|
|
86
93
|
next,
|
|
@@ -103,28 +110,29 @@ export class AttachmentRouting {
|
|
|
103
110
|
true,
|
|
104
111
|
async () => undefined,
|
|
105
112
|
);
|
|
106
|
-
return remapResult(result, link.target.agentName,
|
|
113
|
+
return remapResult(result, link.target.agentName, alias);
|
|
107
114
|
}
|
|
108
115
|
const owner = attachmentOwner(ctx);
|
|
109
116
|
const link = owner?.context;
|
|
110
|
-
if (
|
|
111
|
-
|
|
117
|
+
if (
|
|
118
|
+
owner &&
|
|
119
|
+
link &&
|
|
120
|
+
(inside(request.agentName, link.controllerAlias) ||
|
|
121
|
+
request.agentName === `/${owner.controllerSessionId}`)
|
|
122
|
+
) {
|
|
123
|
+
const alias =
|
|
124
|
+
request.agentName === `/${owner.controllerSessionId}`
|
|
125
|
+
? request.agentName
|
|
126
|
+
: link.controllerAlias;
|
|
127
|
+
if (!isDeepStrictEqual(native, canonicalContextIdentity(link.target)))
|
|
112
128
|
throw new Error(
|
|
113
129
|
"Shared notes are unavailable. Use messages to exchange the context you need.",
|
|
114
130
|
);
|
|
115
131
|
const next = {
|
|
116
132
|
...request,
|
|
117
133
|
sessionId: link.controller.sessionId,
|
|
118
|
-
agentName: remap(
|
|
119
|
-
|
|
120
|
-
link.controllerAlias,
|
|
121
|
-
link.controller.agentName,
|
|
122
|
-
),
|
|
123
|
-
params: remapParams(
|
|
124
|
-
request.params,
|
|
125
|
-
link.controllerAlias,
|
|
126
|
-
link.controller.agentName,
|
|
127
|
-
),
|
|
134
|
+
agentName: remap(request.agentName, alias, link.controller.agentName),
|
|
135
|
+
params: remapParams(request.params, alias, link.controller.agentName),
|
|
128
136
|
};
|
|
129
137
|
const detached = detachment(ctx, owner)?.context;
|
|
130
138
|
let result: SharedContextResult;
|
|
@@ -148,7 +156,7 @@ export class AttachmentRouting {
|
|
|
148
156
|
)) as SharedContextResult;
|
|
149
157
|
} catch (error) {
|
|
150
158
|
signal?.throwIfAborted();
|
|
151
|
-
if (!this.notes.canRead(next) || !
|
|
159
|
+
if (!this.notes.canRead(next) || !isContextUnavailable(error))
|
|
152
160
|
throw error;
|
|
153
161
|
result = await this.notes.read(
|
|
154
162
|
next,
|
|
@@ -177,11 +185,7 @@ export class AttachmentRouting {
|
|
|
177
185
|
true,
|
|
178
186
|
async () => undefined,
|
|
179
187
|
);
|
|
180
|
-
return remapResult(
|
|
181
|
-
result,
|
|
182
|
-
link.controller.agentName,
|
|
183
|
-
link.controllerAlias,
|
|
184
|
-
);
|
|
188
|
+
return remapResult(result, link.controller.agentName, alias);
|
|
185
189
|
}
|
|
186
190
|
return undefined;
|
|
187
191
|
}
|
package/src/attachment-target.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
|
+
type BoardBinding,
|
|
8
|
+
binding,
|
|
9
|
+
canonicalBoardBinding,
|
|
10
|
+
} from "@howaboua/pi-agent-board/integration";
|
|
7
11
|
import type { ContextSharingService } from "@howaboua/pi-notes-compaction/context-sharing";
|
|
8
12
|
import type { Static } from "typebox";
|
|
9
13
|
import { Check } from "typebox/value";
|
|
@@ -12,6 +16,7 @@ import {
|
|
|
12
16
|
type AttachmentPlan,
|
|
13
17
|
attachmentOwner,
|
|
14
18
|
attachmentRoutes,
|
|
19
|
+
canonicalContextIdentity,
|
|
15
20
|
DETACHED,
|
|
16
21
|
Detachment,
|
|
17
22
|
detachment,
|
|
@@ -26,6 +31,12 @@ import {
|
|
|
26
31
|
detachmentMessage,
|
|
27
32
|
} from "./messages/attachments.js";
|
|
28
33
|
|
|
34
|
+
function sameBoard(left: BoardBinding, right: BoardBinding) {
|
|
35
|
+
const { legacyAgentName: _left, ...a } = canonicalBoardBinding(left);
|
|
36
|
+
const { legacyAgentName: _right, ...b } = canonicalBoardBinding(right);
|
|
37
|
+
return isDeepStrictEqual(a, b);
|
|
38
|
+
}
|
|
39
|
+
|
|
29
40
|
/** Validates and commits attachment membership in the settled target session. */
|
|
30
41
|
export class AttachmentTarget {
|
|
31
42
|
private readonly pi: ExtensionAPI;
|
|
@@ -135,22 +146,37 @@ export class AttachmentTarget {
|
|
|
135
146
|
(plan.context &&
|
|
136
147
|
(plan.context.controller.threadId !== plan.controllerSessionId ||
|
|
137
148
|
plan.context.target.threadId !== plan.targetSessionId ||
|
|
138
|
-
!
|
|
139
|
-
|
|
149
|
+
!(
|
|
150
|
+
plan.context.alias === `/${plan.targetSessionId}` ||
|
|
151
|
+
plan.context.alias.startsWith(
|
|
152
|
+
`${plan.context.controller.agentName}/`,
|
|
153
|
+
)
|
|
140
154
|
) ||
|
|
141
|
-
!
|
|
142
|
-
|
|
155
|
+
!(
|
|
156
|
+
plan.context.controllerAlias === `/${plan.controllerSessionId}` ||
|
|
157
|
+
plan.context.controllerAlias.startsWith(
|
|
158
|
+
`${plan.context.target.agentName}/`,
|
|
159
|
+
)
|
|
143
160
|
))) ||
|
|
144
161
|
(plan.board &&
|
|
145
162
|
(plan.board.controller.sessionId !== plan.controllerSessionId ||
|
|
146
163
|
plan.board.previous.sessionId !== plan.targetSessionId ||
|
|
147
|
-
!
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
164
|
+
!(
|
|
165
|
+
sameBoard(plan.board.desired, {
|
|
166
|
+
...plan.board.controller,
|
|
167
|
+
sessionId: plan.targetSessionId,
|
|
168
|
+
agentName: `/${plan.targetSessionId}`,
|
|
169
|
+
parentSessionId: plan.controllerSessionId,
|
|
170
|
+
upstream: plan.upstream,
|
|
171
|
+
}) ||
|
|
172
|
+
(isDeepStrictEqual(plan.board.desired, {
|
|
173
|
+
...plan.board.controller,
|
|
174
|
+
agentName: plan.board.desired.agentName,
|
|
175
|
+
upstream: plan.upstream,
|
|
176
|
+
}) &&
|
|
177
|
+
plan.board.desired.agentName.startsWith(
|
|
178
|
+
`${plan.board.controller.agentName}/`,
|
|
179
|
+
))
|
|
154
180
|
)))
|
|
155
181
|
)
|
|
156
182
|
throw new Error("Invalid attachment identities or routes");
|
|
@@ -173,13 +199,16 @@ export class AttachmentTarget {
|
|
|
173
199
|
if (
|
|
174
200
|
plan.context &&
|
|
175
201
|
(!current.context ||
|
|
176
|
-
!isDeepStrictEqual(
|
|
202
|
+
!isDeepStrictEqual(
|
|
203
|
+
current.context,
|
|
204
|
+
canonicalContextIdentity(plan.context.target),
|
|
205
|
+
))
|
|
177
206
|
)
|
|
178
207
|
throw new Error(
|
|
179
208
|
current.contextError ?? "Target context changed during attachment",
|
|
180
209
|
);
|
|
181
210
|
if (plan.board && current.boardError) throw new Error(current.boardError);
|
|
182
|
-
if (plan.board && !
|
|
211
|
+
if (plan.board && !sameBoard(current.board, plan.board.previous))
|
|
183
212
|
throw new Error("Target board changed during attachment");
|
|
184
213
|
// Authentication completes before either membership mutation.
|
|
185
214
|
if (plan.context) {
|
|
@@ -196,8 +225,11 @@ export class AttachmentTarget {
|
|
|
196
225
|
if (
|
|
197
226
|
!ctx.isIdle() ||
|
|
198
227
|
ctx.sessionManager.getSessionId() !== plan.targetSessionId ||
|
|
199
|
-
!isDeepStrictEqual(
|
|
200
|
-
|
|
228
|
+
!isDeepStrictEqual(
|
|
229
|
+
identity,
|
|
230
|
+
canonicalContextIdentity(plan.context.target),
|
|
231
|
+
) ||
|
|
232
|
+
(plan.board && !sameBoard(binding(ctx), plan.board.previous))
|
|
201
233
|
)
|
|
202
234
|
throw new Error(
|
|
203
235
|
"Target changed during attachment authentication; retry after it settles",
|
|
@@ -230,16 +262,20 @@ export class AttachmentTarget {
|
|
|
230
262
|
} else {
|
|
231
263
|
if (
|
|
232
264
|
plan.context &&
|
|
233
|
-
!isDeepStrictEqual(
|
|
265
|
+
!isDeepStrictEqual(
|
|
266
|
+
await this.context(ctx, false),
|
|
267
|
+
canonicalContextIdentity(plan.context.target),
|
|
268
|
+
)
|
|
234
269
|
)
|
|
235
270
|
throw new Error(
|
|
236
271
|
"Shared notes are unavailable. Use messages to exchange the context you need.",
|
|
237
272
|
);
|
|
238
273
|
if (
|
|
239
274
|
plan.board &&
|
|
240
|
-
!
|
|
275
|
+
!sameBoard(binding(ctx), {
|
|
241
276
|
...plan.board.desired,
|
|
242
277
|
sessionId: plan.targetSessionId,
|
|
278
|
+
parentSessionId: plan.controllerSessionId,
|
|
243
279
|
})
|
|
244
280
|
)
|
|
245
281
|
throw new Error("Attached board changed; resolve the target again");
|
|
@@ -275,7 +311,10 @@ export class AttachmentTarget {
|
|
|
275
311
|
const previous = detachment(ctx, plan);
|
|
276
312
|
if (requested.context && plan.context) {
|
|
277
313
|
if (
|
|
278
|
-
!isDeepStrictEqual(
|
|
314
|
+
!isDeepStrictEqual(
|
|
315
|
+
await this.context(ctx, false),
|
|
316
|
+
canonicalContextIdentity(plan.context.target),
|
|
317
|
+
)
|
|
279
318
|
)
|
|
280
319
|
throw new Error("Target context changed before detach");
|
|
281
320
|
this.notes.validate(requested.context, plan.context.controller);
|
package/src/attachment.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import { isDeepStrictEqual } from "node:util";
|
|
3
2
|
import type {
|
|
4
3
|
ExtensionAPI,
|
|
@@ -14,6 +13,7 @@ import {
|
|
|
14
13
|
type AttachmentPlan,
|
|
15
14
|
attachmentOwner,
|
|
16
15
|
attachmentRoutes,
|
|
16
|
+
canonicalContextIdentity,
|
|
17
17
|
ROUTE,
|
|
18
18
|
Snapshot,
|
|
19
19
|
} from "./attachment-protocol.js";
|
|
@@ -123,7 +123,6 @@ export class AgentAttachment {
|
|
|
123
123
|
const upstream = runtime.local
|
|
124
124
|
? sessionContextPath(controllerFile)
|
|
125
125
|
: runtime.client.contextRelayPath();
|
|
126
|
-
const segment = `attached-${randomUUID()}`;
|
|
127
126
|
const plan: AttachmentPlan = {
|
|
128
127
|
controllerSessionId,
|
|
129
128
|
targetSessionId: current.sessionId,
|
|
@@ -133,8 +132,8 @@ export class AgentAttachment {
|
|
|
133
132
|
context: {
|
|
134
133
|
controller: controllerContext,
|
|
135
134
|
target: current.context,
|
|
136
|
-
alias:
|
|
137
|
-
controllerAlias:
|
|
135
|
+
alias: `/${current.sessionId}`,
|
|
136
|
+
controllerAlias: `/${controllerSessionId}`,
|
|
138
137
|
...(runtime.local ? { controllerFile } : {}),
|
|
139
138
|
controllerNotes: await this.notes.export(ctx),
|
|
140
139
|
targetNotes: current.notes,
|
|
@@ -148,7 +147,7 @@ export class AgentAttachment {
|
|
|
148
147
|
desired: this.board.membership.prepareAttachment(
|
|
149
148
|
ctx,
|
|
150
149
|
upstream,
|
|
151
|
-
|
|
150
|
+
current.sessionId,
|
|
152
151
|
),
|
|
153
152
|
controller: binding(ctx),
|
|
154
153
|
},
|
|
@@ -193,19 +192,11 @@ export class AgentAttachment {
|
|
|
193
192
|
}
|
|
194
193
|
return {
|
|
195
194
|
attached: true,
|
|
195
|
+
agent: `/${route.plan.targetSessionId}`,
|
|
196
196
|
machine: runtime.machine,
|
|
197
197
|
target: panel.pane_id,
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
contextAgent: route.plan.context.alias,
|
|
201
|
-
controllerContextAgent: route.plan.context.controllerAlias,
|
|
202
|
-
}
|
|
203
|
-
: {}),
|
|
204
|
-
...(route.plan.board
|
|
205
|
-
? {
|
|
206
|
-
boardAgent: route.plan.board.desired.agentName,
|
|
207
|
-
}
|
|
208
|
-
: {}),
|
|
198
|
+
context: !!route.plan.context,
|
|
199
|
+
board: !!route.plan.board,
|
|
209
200
|
};
|
|
210
201
|
}
|
|
211
202
|
async detach(
|
|
@@ -248,7 +239,10 @@ export class AgentAttachment {
|
|
|
248
239
|
)) ||
|
|
249
240
|
(params.context &&
|
|
250
241
|
plan.context &&
|
|
251
|
-
!isDeepStrictEqual(
|
|
242
|
+
!isDeepStrictEqual(
|
|
243
|
+
currentContext,
|
|
244
|
+
canonicalContextIdentity(plan.context.controller),
|
|
245
|
+
)) ||
|
|
252
246
|
(controllerBoard && !isDeepStrictEqual(binding(ctx), controllerBoard))
|
|
253
247
|
)
|
|
254
248
|
throw new Error(
|
package/src/board/host.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
1
|
import type {
|
|
3
2
|
ExtensionAPI,
|
|
4
3
|
ExtensionContext,
|
|
@@ -62,20 +61,20 @@ export class AgentBoard {
|
|
|
62
61
|
request: requestContext,
|
|
63
62
|
commitDirectory: (own, directory, caller, request) =>
|
|
64
63
|
this.membership.commitDirectory(own, directory, caller, request),
|
|
65
|
-
routeNotice: (ctx, request, signal) => {
|
|
64
|
+
routeNotice: async (ctx, request, signal) => {
|
|
65
|
+
const next = request.route?.[0] ?? request.sessionId;
|
|
66
66
|
const child = children(ctx).find(
|
|
67
|
-
(entry) =>
|
|
68
|
-
|
|
69
|
-
|
|
67
|
+
(entry) => entry.binding.sessionId === next,
|
|
68
|
+
);
|
|
69
|
+
if (!child) return false;
|
|
70
|
+
return fleet.connected(child.machine).client.requestContext(
|
|
71
|
+
sessionContextPath(child.sessionFile),
|
|
72
|
+
{
|
|
73
|
+
...request,
|
|
74
|
+
...(request.route ? { route: request.route.slice(1) } : {}),
|
|
75
|
+
},
|
|
76
|
+
signal,
|
|
70
77
|
);
|
|
71
|
-
if (!child) return Promise.resolve(false);
|
|
72
|
-
return fleet
|
|
73
|
-
.connected(child.machine)
|
|
74
|
-
.client.requestContext(
|
|
75
|
-
sessionContextPath(child.sessionFile),
|
|
76
|
-
request,
|
|
77
|
-
signal,
|
|
78
|
-
);
|
|
79
78
|
},
|
|
80
79
|
propagateEnabled: async (ctx, enabled) => {
|
|
81
80
|
let boardId: string | undefined;
|
|
@@ -134,7 +133,7 @@ export class AgentBoard {
|
|
|
134
133
|
async prepare(
|
|
135
134
|
ctx: ExtensionContext,
|
|
136
135
|
runtime: ConnectedMachine,
|
|
137
|
-
|
|
136
|
+
_name: string,
|
|
138
137
|
args: readonly string[],
|
|
139
138
|
) {
|
|
140
139
|
if (!this.enabled(ctx)) return undefined;
|
|
@@ -154,9 +153,6 @@ export class AgentBoard {
|
|
|
154
153
|
throw new Error(
|
|
155
154
|
"Board child binding requires a saved controller session",
|
|
156
155
|
);
|
|
157
|
-
const segment =
|
|
158
|
-
name.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64) || "agent";
|
|
159
|
-
const agentName = `${own.agentName}/${segment}-${randomUUID()}`;
|
|
160
156
|
await runtime.prepareSharing();
|
|
161
157
|
const upstream = runtime.local
|
|
162
158
|
? sessionContextPath(file)
|
|
@@ -168,7 +164,7 @@ export class AgentBoard {
|
|
|
168
164
|
const sessionFile = sessionPath(agent);
|
|
169
165
|
if (!sessionFile)
|
|
170
166
|
throw new Error("Spawned agent has no native Pi session path");
|
|
171
|
-
const prepared = { ...own,
|
|
167
|
+
const prepared = { ...own, upstream };
|
|
172
168
|
const result = await runtime.client.requestContext(
|
|
173
169
|
sessionContextPath(sessionFile),
|
|
174
170
|
{ operation: "board-bind", binding: prepared },
|
|
@@ -177,7 +173,8 @@ export class AgentBoard {
|
|
|
177
173
|
if (
|
|
178
174
|
adopted.boardId !== own.boardId ||
|
|
179
175
|
adopted.databasePath !== own.databasePath ||
|
|
180
|
-
adopted.agentName !==
|
|
176
|
+
adopted.agentName !== `/${adopted.sessionId}` ||
|
|
177
|
+
adopted.parentSessionId !== own.sessionId ||
|
|
181
178
|
adopted.rootSessionId !== own.rootSessionId ||
|
|
182
179
|
adopted.upstream !== upstream ||
|
|
183
180
|
adopted.sessionId === own.sessionId
|
package/src/board/membership.ts
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
binding,
|
|
10
10
|
children,
|
|
11
11
|
members,
|
|
12
|
+
parseBinding,
|
|
12
13
|
removeMember,
|
|
13
14
|
saveBinding,
|
|
14
15
|
saveChild,
|
|
@@ -17,10 +18,17 @@ import {
|
|
|
17
18
|
import type { ConnectedMachine } from "../fleet/index.js";
|
|
18
19
|
|
|
19
20
|
function sameMembership(left: BoardBinding, right: BoardBinding) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
const { legacyAgentName: _left, ...a } = parseBinding(left);
|
|
22
|
+
const { legacyAgentName: _right, ...b } = parseBinding(right);
|
|
23
|
+
return isDeepStrictEqual({ ...a, enabled: false }, { ...b, enabled: false });
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function adoptedBinding(desired: BoardBinding, sessionId: string) {
|
|
27
|
+
return parseBinding({
|
|
28
|
+
...desired,
|
|
29
|
+
sessionId,
|
|
30
|
+
parentSessionId: desired.parentSessionId ?? desired.sessionId,
|
|
31
|
+
});
|
|
24
32
|
}
|
|
25
33
|
|
|
26
34
|
/** Owns board attachment adoption, registration, restoration and removal. */
|
|
@@ -50,7 +58,11 @@ export class BoardMembership {
|
|
|
50
58
|
throw new Error("Target already owns or belongs to a board family");
|
|
51
59
|
return own;
|
|
52
60
|
}
|
|
53
|
-
prepareAttachment(
|
|
61
|
+
prepareAttachment(
|
|
62
|
+
ctx: ExtensionContext,
|
|
63
|
+
upstream: string,
|
|
64
|
+
targetSessionId: string,
|
|
65
|
+
) {
|
|
54
66
|
this.getActive();
|
|
55
67
|
const own = binding(ctx);
|
|
56
68
|
if (!own.enabled)
|
|
@@ -59,7 +71,14 @@ export class BoardMembership {
|
|
|
59
71
|
);
|
|
60
72
|
if (!ctx.sessionManager.getSessionFile())
|
|
61
73
|
throw new Error("Board attachment requires a saved controller session");
|
|
62
|
-
|
|
74
|
+
const { legacyAgentName: _legacy, ...board } = own;
|
|
75
|
+
return {
|
|
76
|
+
...board,
|
|
77
|
+
upstream,
|
|
78
|
+
sessionId: targetSessionId,
|
|
79
|
+
agentName: `/${targetSessionId}`,
|
|
80
|
+
parentSessionId: own.sessionId,
|
|
81
|
+
};
|
|
63
82
|
}
|
|
64
83
|
commitAttachment(
|
|
65
84
|
ctx: ExtensionContext,
|
|
@@ -70,9 +89,9 @@ export class BoardMembership {
|
|
|
70
89
|
if (!ctx.isIdle())
|
|
71
90
|
throw new Error("Attach a board only after the target settles");
|
|
72
91
|
const own = binding(ctx);
|
|
73
|
-
const adopted =
|
|
92
|
+
const adopted = adoptedBinding(desired, own.sessionId);
|
|
74
93
|
if (isDeepStrictEqual(own, adopted)) return adopted;
|
|
75
|
-
if (!
|
|
94
|
+
if (!sameMembership(this.inspectAttachment(ctx), expected))
|
|
76
95
|
throw new Error(
|
|
77
96
|
"Target board changed during attachment; resolve it again",
|
|
78
97
|
);
|
|
@@ -87,19 +106,22 @@ export class BoardMembership {
|
|
|
87
106
|
expected: BoardBinding,
|
|
88
107
|
) {
|
|
89
108
|
this.getActive();
|
|
90
|
-
if (!
|
|
109
|
+
if (!sameMembership(binding(ctx), expected))
|
|
91
110
|
throw new Error("Controller board changed during attachment");
|
|
92
|
-
saveBinding(this.pi, expected);
|
|
111
|
+
saveBinding(this.pi, parseBinding(expected));
|
|
93
112
|
await this.toOwner(ctx, {
|
|
94
113
|
operation: "board-register",
|
|
95
|
-
caller: expected,
|
|
96
|
-
member: adopted,
|
|
114
|
+
caller: parseBinding(expected),
|
|
115
|
+
member: parseBinding({ ...adopted, parentSessionId: expected.sessionId }),
|
|
97
116
|
});
|
|
98
|
-
if (!
|
|
117
|
+
if (!sameMembership(binding(ctx), expected))
|
|
99
118
|
throw new Error("Controller board changed during attachment");
|
|
100
119
|
saveChild(this.pi, {
|
|
101
120
|
parentSessionId: expected.sessionId,
|
|
102
|
-
binding:
|
|
121
|
+
binding: parseBinding({
|
|
122
|
+
...adopted,
|
|
123
|
+
parentSessionId: expected.sessionId,
|
|
124
|
+
}),
|
|
103
125
|
machine: runtime.machine,
|
|
104
126
|
sessionFile,
|
|
105
127
|
});
|
|
@@ -115,10 +137,7 @@ export class BoardMembership {
|
|
|
115
137
|
"Detach a board member only when it has no board children",
|
|
116
138
|
);
|
|
117
139
|
if (
|
|
118
|
-
!sameMembership(binding(ctx),
|
|
119
|
-
...desired,
|
|
120
|
-
sessionId: previous.sessionId,
|
|
121
|
-
})
|
|
140
|
+
!sameMembership(binding(ctx), adoptedBinding(desired, previous.sessionId))
|
|
122
141
|
)
|
|
123
142
|
throw new Error("Target board changed before detach");
|
|
124
143
|
saveBinding(this.pi, previous);
|
|
@@ -130,7 +149,7 @@ export class BoardMembership {
|
|
|
130
149
|
await this.toOwner(ctx, {
|
|
131
150
|
operation: "board-unregister",
|
|
132
151
|
caller: own,
|
|
133
|
-
member,
|
|
152
|
+
member: parseBinding({ ...member, parentSessionId: own.sessionId }),
|
|
134
153
|
});
|
|
135
154
|
if (!sameMembership(binding(ctx), own))
|
|
136
155
|
throw new Error("Controller board changed during detach");
|
|
@@ -146,37 +165,37 @@ export class BoardMembership {
|
|
|
146
165
|
>,
|
|
147
166
|
) {
|
|
148
167
|
if (request.operation === "board-unregister") {
|
|
149
|
-
const member = request.member;
|
|
168
|
+
const member = parseBinding(request.member);
|
|
150
169
|
if (
|
|
151
170
|
member.boardId !== own.boardId ||
|
|
152
171
|
member.databasePath !== own.databasePath ||
|
|
153
|
-
|
|
172
|
+
member.parentSessionId !== caller.sessionId
|
|
154
173
|
)
|
|
155
174
|
throw new Error("Invalid board member removal");
|
|
156
|
-
if (
|
|
157
|
-
directory.some((entry) =>
|
|
158
|
-
entry.agentName.startsWith(`${member.agentName}/`),
|
|
159
|
-
)
|
|
160
|
-
)
|
|
175
|
+
if (directory.some((entry) => entry.parentSessionId === member.sessionId))
|
|
161
176
|
throw new Error(
|
|
162
177
|
"Detach a board member only when it has no board children",
|
|
163
178
|
);
|
|
164
179
|
const existing = directory.find(
|
|
165
180
|
(entry) => entry.agentName === member.agentName,
|
|
166
181
|
);
|
|
167
|
-
if (
|
|
182
|
+
if (
|
|
183
|
+
existing &&
|
|
184
|
+
(existing.parentSessionId !== caller.sessionId ||
|
|
185
|
+
!sameMembership(existing, member))
|
|
186
|
+
)
|
|
168
187
|
throw new Error("Board member changed before detach");
|
|
169
188
|
removeMember(this.pi, member);
|
|
170
189
|
this.getActive().delete(member.agentName);
|
|
171
190
|
return true;
|
|
172
191
|
}
|
|
173
192
|
if (request.operation === "board-register") {
|
|
174
|
-
const member = request.member;
|
|
193
|
+
const member = parseBinding(request.member);
|
|
175
194
|
if (
|
|
176
195
|
member.boardId !== own.boardId ||
|
|
177
196
|
member.databasePath !== own.databasePath ||
|
|
178
197
|
member.rootSessionId !== own.rootSessionId ||
|
|
179
|
-
|
|
198
|
+
member.parentSessionId !== caller.sessionId
|
|
180
199
|
)
|
|
181
200
|
throw new Error("Invalid board member registration");
|
|
182
201
|
const existing = directory.find(
|
|
@@ -184,7 +203,7 @@ export class BoardMembership {
|
|
|
184
203
|
entry.agentName === member.agentName ||
|
|
185
204
|
entry.sessionId === member.sessionId,
|
|
186
205
|
);
|
|
187
|
-
if (existing &&
|
|
206
|
+
if (existing && sameMembership(existing, member)) return true;
|
|
188
207
|
if (existing) throw new Error("Board member already bound");
|
|
189
208
|
saveMember(this.pi, member);
|
|
190
209
|
return true;
|