@howaboua/pi-shepherdr 0.2.28-core.12.1.gda85fa2e8a0f → 1.0.0

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.
Files changed (55) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/README.md +34 -5
  3. package/biome.json +1 -1
  4. package/index.ts +5 -2
  5. package/package.json +19 -14
  6. package/src/agents-contract.ts +80 -61
  7. package/src/agents-discovery.ts +14 -10
  8. package/src/agents-focus.ts +7 -0
  9. package/src/agents-reply.ts +3 -12
  10. package/src/agents-spawn.ts +26 -2
  11. package/src/agents-tool.ts +57 -21
  12. package/src/agents-work.ts +9 -4
  13. package/src/ancestry-record.ts +116 -0
  14. package/src/attachment-notes.ts +3 -2
  15. package/src/attachment-protocol.ts +124 -68
  16. package/src/attachment-target.ts +8 -7
  17. package/src/attachment.ts +3 -3
  18. package/src/board/records.ts +50 -36
  19. package/src/close-idle.ts +2 -2
  20. package/src/controller.ts +5 -4
  21. package/src/core/backend.ts +26 -3
  22. package/src/core/helper-bootstrap.mjs +1 -1
  23. package/src/core/helper-core.mjs +4 -3
  24. package/src/core/helper-lifetime.mjs +199 -0
  25. package/src/core/remote-endpoint.ts +10 -3
  26. package/src/core/remote-helper.ts +3 -0
  27. package/src/core/remote-service.mjs +9 -0
  28. package/src/core/remote-transport.ts +11 -1
  29. package/src/core/spawn.ts +1 -1
  30. package/src/core/terminal-peer.mjs +26 -5
  31. package/src/core/terminal.ts +2 -1
  32. package/src/fleet/connection.ts +4 -0
  33. package/src/fleet/index.ts +24 -3
  34. package/src/launch-contract.ts +1 -0
  35. package/src/launch.ts +40 -3
  36. package/src/messages/attribution.ts +14 -5
  37. package/src/messages/focus.ts +4 -3
  38. package/src/messages/peer-sharing.ts +34 -20
  39. package/src/monitor-events.ts +10 -10
  40. package/src/monitor-extension-messages.ts +1 -0
  41. package/src/monitor.ts +5 -5
  42. package/src/profiles.ts +1 -8
  43. package/src/remote/shepherdr-session.mjs +43 -0
  44. package/src/settlement/delivery.ts +32 -14
  45. package/src/settlement/index.ts +3 -0
  46. package/src/shared-context.ts +4 -100
  47. package/src/spawn-ancestry.ts +103 -0
  48. package/src/spawn-ownership.ts +93 -5
  49. package/src/types.ts +1 -0
  50. package/src/worker-ownership.ts +41 -32
  51. package/src/worker-resume.ts +231 -0
  52. package/src/worker-role.ts +9 -35
  53. package/src/worker-service.ts +95 -59
  54. package/worker-api.ts +38 -0
  55. package/src/profile-prompt.ts +0 -32
@@ -32,6 +32,7 @@ import type { AgentFleet } from "./fleet/index.js";
32
32
  import { attributeAgentPrompt } from "./messages/attribution.js";
33
33
  import type { createPeerCommands } from "./peer-commands.js";
34
34
  import type { SharedAgentContext } from "./shared-context.js";
35
+ import { WorkerResumer } from "./worker-resume.js";
35
36
 
36
37
  export function createAgentsTool(
37
38
  fleet: AgentFleet,
@@ -41,6 +42,7 @@ export function createAgentsTool(
41
42
  commands: ReturnType<typeof createPeerCommands>,
42
43
  gpt: () => boolean = () => false,
43
44
  ) {
45
+ const resumer = new WorkerResumer(pi, fleet);
44
46
  return defineTool({
45
47
  name: "agents",
46
48
  label: "Shepherdr",
@@ -84,6 +86,23 @@ export function createAgentsTool(
84
86
  }
85
87
 
86
88
  let target = required(params.target, "target");
89
+ if (params.action === "send" || params.action === "assign")
90
+ required(params.message, "message");
91
+ const owned =
92
+ (params.action === "send" || params.action === "assign") &&
93
+ target !== "/root"
94
+ ? await resumer.resolve(
95
+ ctx,
96
+ target,
97
+ params.machine,
98
+ executionSignal,
99
+ !/^\/quit(?:\s|$)/.test(params.message?.trim() ?? ""),
100
+ )
101
+ : undefined;
102
+ if (owned) {
103
+ runtime = owned.runtime;
104
+ target = owned.panel.pane_id;
105
+ }
87
106
  if (target === "/root") {
88
107
  const root = await sharedContext.resolveRoot(
89
108
  ctx,
@@ -135,11 +154,13 @@ export function createAgentsTool(
135
154
  });
136
155
  }
137
156
 
138
- const panel = await resolvePiAgent(
139
- runtime.client,
140
- target,
141
- runtime.local ? process.env["HC_TERMINAL_ID"] : "",
142
- );
157
+ const panel =
158
+ owned?.panel ??
159
+ (await resolvePiAgent(
160
+ runtime.client,
161
+ target,
162
+ runtime.local ? process.env["HC_TERMINAL_ID"] : "",
163
+ ));
143
164
  if (params.commands) {
144
165
  return toolResult({
145
166
  machine: runtime.machine,
@@ -191,10 +212,21 @@ export function createAgentsTool(
191
212
  const message = required(params.message, "message");
192
213
  const attributedMessage = await attributeAgentPrompt(
193
214
  fleet.connected().client,
215
+ ctx.sessionManager.getSessionId(),
194
216
  message,
195
217
  params.action === "send" ? "message" : "task",
196
218
  );
197
- attributedMessage.context += `\n${await sharedContext.peerReplyContext(ctx, runtime, panel, executionSignal)}`;
219
+ attributedMessage.context = [
220
+ attributedMessage.context,
221
+ await sharedContext.peerReplyContext(
222
+ ctx,
223
+ runtime,
224
+ panel,
225
+ executionSignal,
226
+ ),
227
+ ]
228
+ .filter(Boolean)
229
+ .join("\n");
198
230
  if (params.action === "send") {
199
231
  executionSignal.throwIfAborted();
200
232
  const receipt = await runtime.client.sendMessage(
@@ -205,6 +237,7 @@ export function createAgentsTool(
205
237
  runtime.monitor.noteMessage(panel.pane_id, message);
206
238
  return toolResult({
207
239
  sent: true,
240
+ ...(owned?.resumed ? { resumed: true } : {}),
208
241
  ...(receipt.command ? { commandSubmitted: true } : {}),
209
242
  machine: runtime.machine,
210
243
  target: panel.pane_id,
@@ -221,24 +254,27 @@ export function createAgentsTool(
221
254
  { expectUserMessage: true },
222
255
  );
223
256
  return toolResult(
224
- dispatch.command
225
- ? {
226
- commandSubmitted: true,
227
- machine: runtime.machine,
228
- target: panel.pane_id,
229
- }
230
- : dispatch.settlement
257
+ {
258
+ ...(owned?.resumed ? { resumed: true } : {}),
259
+ ...(dispatch.command
231
260
  ? {
232
- assigned: true,
233
- ...settlementResult(runtime.machine, dispatch.settlement),
234
- }
235
- : {
236
- assigned: true,
261
+ commandSubmitted: true,
237
262
  machine: runtime.machine,
238
263
  target: panel.pane_id,
239
- status: "working",
240
- next: "Do other work or end this turn; completion/blockage arrives automatically. Do not poll or await timers/extra Promises for reports",
241
- },
264
+ }
265
+ : dispatch.settlement
266
+ ? {
267
+ assigned: true,
268
+ ...settlementResult(runtime.machine, dispatch.settlement),
269
+ }
270
+ : {
271
+ assigned: true,
272
+ machine: runtime.machine,
273
+ target: panel.pane_id,
274
+ status: "working",
275
+ next: "Do other work or end this turn; completion/blockage arrives automatically. Do not poll or await timers/extra Promises for reports",
276
+ }),
277
+ },
242
278
  dispatch.warning,
243
279
  );
244
280
  }
@@ -53,7 +53,7 @@ export async function reserveAgentName(
53
53
  startingNames.set(runtime.client, reserved);
54
54
  }
55
55
  const names = new Set([
56
- ...snapshot.agents.flatMap((agent) => (agent.name ? [agent.name] : [])),
56
+ ...snapshot.panes.flatMap((panel) => (panel.name ? [panel.name] : [])),
57
57
  ...reserved,
58
58
  ]);
59
59
  const name = requested?.trim() || availableAgentName(names, label);
@@ -160,12 +160,16 @@ export async function dispatchAgentWork(
160
160
  settlement = blocking
161
161
  ? runtime.monitor.claimWork(attempt, signal, options.replyingTo)
162
162
  : undefined;
163
+ if (!blocking && options.replyingTo) {
164
+ runtime.monitor.observeReply(attempt, options.replyingTo);
165
+ }
163
166
  void settlement?.catch(() => undefined);
164
167
  signal.throwIfAborted();
165
168
  sendStarted = true;
166
169
  receipt = await send();
167
170
  } catch (error) {
168
- if (settlement) runtime.monitor.releaseWorkClaim(attempt, error);
171
+ if (settlement || options.replyingTo)
172
+ runtime.monitor.releaseWorkClaim(attempt, error);
169
173
  if (sendStarted) await runtime.monitor.handleWorkFailure(attempt, error);
170
174
  else runtime.monitor.rejectWork(attempt);
171
175
  if (addedWatch && (!sendStarted || isDispatchRejected(error))) {
@@ -187,7 +191,7 @@ export async function dispatchAgentWork(
187
191
  }
188
192
  try {
189
193
  if (receipt?.command) {
190
- if (settlement)
194
+ if (settlement || options.replyingTo)
191
195
  runtime.monitor.releaseWorkClaim(
192
196
  attempt,
193
197
  new Error("Command submitted without task waiting"),
@@ -206,7 +210,8 @@ export async function dispatchAgentWork(
206
210
  }
207
211
  await runtime.monitor.reconcileNow();
208
212
  } catch (error) {
209
- if (settlement) runtime.monitor.releaseWorkClaim(attempt, error);
213
+ if (settlement || options.replyingTo)
214
+ runtime.monitor.releaseWorkClaim(attempt, error);
210
215
  return {
211
216
  ...(receipt?.command ? { command: true } : {}),
212
217
  warning: `Submitted, but monitoring refresh failed: ${error instanceof Error ? error.message : String(error)}. Inspect the target before resubmitting`,
@@ -0,0 +1,116 @@
1
+ import { posix, win32 } from "node:path";
2
+ import { isDeepStrictEqual } from "node:util";
3
+ import type {
4
+ ExtensionAPI,
5
+ ExtensionContext,
6
+ } from "@earendil-works/pi-coding-agent";
7
+
8
+ export const ANCESTRY_FLAG = "shepherdr-ancestry";
9
+ const ANCESTRY_ENTRY = "herdr-spawn-ancestry-v1";
10
+
11
+ export interface NativeSessionIdentity {
12
+ machine: string;
13
+ sessionId: string;
14
+ sessionFile: string;
15
+ }
16
+
17
+ export interface SpawnAncestry {
18
+ parent: NativeSessionIdentity;
19
+ root: NativeSessionIdentity;
20
+ }
21
+
22
+ function nativeIdentity(value: unknown): NativeSessionIdentity {
23
+ if (
24
+ !value ||
25
+ typeof value !== "object" ||
26
+ !("machine" in value) ||
27
+ typeof value.machine !== "string" ||
28
+ !value.machine.trim() ||
29
+ value.machine === "local" ||
30
+ !("sessionId" in value) ||
31
+ typeof value.sessionId !== "string" ||
32
+ !value.sessionId ||
33
+ /[/\\\u0000]/.test(value.sessionId) ||
34
+ !("sessionFile" in value) ||
35
+ typeof value.sessionFile !== "string" ||
36
+ !(
37
+ posix.isAbsolute(value.sessionFile) || win32.isAbsolute(value.sessionFile)
38
+ ) ||
39
+ value.sessionFile.includes("\u0000")
40
+ )
41
+ throw new Error("Invalid saved parent identity; respawn the worker");
42
+ return {
43
+ machine: value.machine,
44
+ sessionId: value.sessionId,
45
+ sessionFile: value.sessionFile,
46
+ };
47
+ }
48
+
49
+ export function parseSpawnAncestry(value: unknown): SpawnAncestry {
50
+ if (
51
+ !value ||
52
+ typeof value !== "object" ||
53
+ !("parent" in value) ||
54
+ !("root" in value)
55
+ )
56
+ throw new Error("Invalid saved ancestry; respawn the worker");
57
+ return {
58
+ parent: nativeIdentity(value.parent),
59
+ root: nativeIdentity(value.root),
60
+ };
61
+ }
62
+
63
+ export function savedSpawnAncestry(
64
+ ctx: Pick<ExtensionContext, "sessionManager">,
65
+ ): SpawnAncestry | undefined {
66
+ let ancestry: SpawnAncestry | undefined;
67
+ const sessionId = ctx.sessionManager.getSessionId();
68
+ for (const entry of ctx.sessionManager.getEntries()) {
69
+ if (entry.type !== "custom" || entry.customType !== ANCESTRY_ENTRY)
70
+ continue;
71
+ const value = entry.data;
72
+ if (
73
+ !value ||
74
+ typeof value !== "object" ||
75
+ !("sessionId" in value) ||
76
+ value.sessionId !== sessionId
77
+ )
78
+ continue;
79
+ const candidate = parseSpawnAncestry(value);
80
+ if (ancestry && !isDeepStrictEqual(candidate, ancestry))
81
+ throw new Error("Conflicting saved ancestry; use an exact agent target");
82
+ ancestry = candidate;
83
+ }
84
+ return ancestry;
85
+ }
86
+
87
+ export function registerSpawnAncestry(pi: ExtensionAPI): void {
88
+ pi.registerFlag(ANCESTRY_FLAG, {
89
+ type: "string",
90
+ description: "JSON-encoded native worker ancestry",
91
+ });
92
+ pi.on("before_agent_start", async (_event, ctx) => {
93
+ try {
94
+ // Resume never rebinds an existing native identity, even under a new terminal.
95
+ if (savedSpawnAncestry(ctx)) return;
96
+ const encoded = pi.getFlag(ANCESTRY_FLAG);
97
+ if (encoded === undefined) return;
98
+ const ancestry = parseSpawnAncestry(JSON.parse(String(encoded)));
99
+ if (!ctx.sessionManager.getSessionFile())
100
+ throw new Error("Worker ancestry requires a saved Pi session");
101
+ const sessionId = ctx.sessionManager.getSessionId();
102
+ if (
103
+ ancestry.parent.sessionId === sessionId ||
104
+ ancestry.root.sessionId === sessionId
105
+ )
106
+ throw new Error("A worker cannot be its own ancestor");
107
+ // Fresh Notes/Board binding must finish before the first custom entry.
108
+ pi.appendEntry(ANCESTRY_ENTRY, { sessionId, ...ancestry });
109
+ } catch (cause) {
110
+ await ctx.abort();
111
+ throw new Error("Worker ancestry is unavailable; respawn the worker", {
112
+ cause,
113
+ });
114
+ }
115
+ });
116
+ }
@@ -151,10 +151,11 @@ export class AttachmentNotes {
151
151
  const details = {
152
152
  source,
153
153
  read_only: true,
154
- captured_at: new Date(snapshot.timestamp).toISOString(),
154
+ captured_at:
155
+ new Date(snapshot.timestamp).toISOString().slice(0, 16) + "Z",
155
156
  ...(savedAt === undefined
156
157
  ? {}
157
- : { saved_at: new Date(savedAt).toISOString() }),
158
+ : { saved_at: new Date(savedAt).toISOString().slice(0, 16) + "Z" }),
158
159
  ...(detached
159
160
  ? { detached: true }
160
161
  : {
@@ -1,24 +1,48 @@
1
1
  import { isDeepStrictEqual } from "node:util";
2
2
  import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
3
+ import {
4
+ compile,
5
+ lit,
6
+ obj,
7
+ opt,
8
+ type Static,
9
+ str,
10
+ union,
11
+ unknown,
12
+ } from "@earendil-works/pi-schema";
3
13
  import type { SharedContextResult } from "@howaboua/pi-agent-comms/context-sharing";
4
- import { type Static, type TSchema, Type } from "typebox";
5
- import { Check } from "typebox/value";
6
14
  import { BindingSchema } from "./board/records.js";
7
15
 
8
- const AgentPath = Type.String({
16
+ const AgentPath = str({
9
17
  pattern: "^/(?:[a-zA-Z0-9_.-]+|root(?:/[a-zA-Z0-9_-]+)*)$",
10
18
  });
11
- export const Identity = Type.Object({
12
- protocol: Type.Literal(1),
13
- sessionId: Type.String({ minLength: 1 }),
14
- threadId: Type.String({ minLength: 1 }),
15
- agentName: AgentPath,
16
- storageAgentName: Type.Optional(AgentPath),
17
- storage: Type.Union([Type.Literal("session"), Type.Literal("remote")]),
18
- accountScope: Type.Optional(Type.String({ pattern: "^[a-f0-9]{64}$" })),
19
- backendUrl: Type.Optional(Type.String({ minLength: 1 })),
20
- routing: Type.Optional(Type.Unknown()),
21
- });
19
+ export const Identity = obj(
20
+ {
21
+ protocol: {
22
+ ...lit(1),
23
+ type: "number",
24
+ },
25
+ sessionId: str({ minLength: 1 }),
26
+ threadId: str({ minLength: 1 }),
27
+ agentName: AgentPath,
28
+ storageAgentName: opt(AgentPath),
29
+ storage: union([
30
+ {
31
+ ...lit("session"),
32
+ type: "string",
33
+ },
34
+ {
35
+ ...lit("remote"),
36
+ type: "string",
37
+ },
38
+ ]),
39
+ accountScope: opt(str({ pattern: "^[a-f0-9]{64}$" })),
40
+ backendUrl: opt(str({ minLength: 1 })),
41
+ routing: opt(unknown()),
42
+ },
43
+ { open: true },
44
+ );
45
+ export const checkIdentity = compile(Identity).check;
22
46
  // Persisted attachment records predate native agent references. Keep their storage address.
23
47
  export function canonicalContextIdentity(
24
48
  identity: Static<typeof Identity>,
@@ -32,73 +56,105 @@ export function canonicalContextIdentity(
32
56
  storageAgentName: identity.storageAgentName ?? identity.agentName,
33
57
  };
34
58
  }
35
- const ContextLink = Type.Object({
36
- controller: Identity,
37
- target: Identity,
38
- alias: AgentPath,
39
- controllerAlias: AgentPath,
40
- controllerFile: Type.Optional(Type.String()),
41
- controllerMachine: Type.Optional(Type.String({ minLength: 1 })),
42
- controllerNotes: Type.Unknown(),
43
- targetNotes: Type.Unknown(),
44
- });
45
- export const Plan = Type.Object(
59
+ const ContextLink = obj(
46
60
  {
47
- controllerSessionId: Type.String({ minLength: 1 }),
48
- targetSessionId: Type.String({ minLength: 1 }),
49
- upstream: Type.String({ minLength: 1 }),
50
- context: Type.Optional(ContextLink),
51
- board: Type.Optional(
52
- Type.Object({
61
+ controller: Identity,
62
+ target: Identity,
63
+ alias: AgentPath,
64
+ controllerAlias: AgentPath,
65
+ controllerFile: opt(str()),
66
+ controllerMachine: opt(str({ minLength: 1 })),
67
+ controllerNotes: unknown(),
68
+ targetNotes: unknown(),
69
+ },
70
+ { open: true },
71
+ );
72
+ const Plan = obj({
73
+ controllerSessionId: str({ minLength: 1 }),
74
+ targetSessionId: str({ minLength: 1 }),
75
+ upstream: str({ minLength: 1 }),
76
+ context: opt(ContextLink),
77
+ board: opt(
78
+ obj(
79
+ {
53
80
  previous: BindingSchema,
54
81
  desired: BindingSchema,
55
82
  controller: BindingSchema,
56
- }),
83
+ },
84
+ { open: true },
57
85
  ),
58
- },
59
- { additionalProperties: false },
60
- );
61
- const Route = Type.Object({
62
- plan: Plan,
63
- machine: Type.String(),
64
- sessionFile: Type.String({ minLength: 1 }),
65
- phase: Type.Union([Type.Literal("pending"), Type.Literal("ready")]),
66
- detached: Type.Optional(
67
- Type.Object({
68
- context: Type.Optional(Type.Unknown()),
69
- board: Type.Optional(Type.Literal(true)),
70
- }),
71
86
  ),
72
87
  });
73
- export const Snapshot = Type.Object({
74
- sessionId: Type.String({ minLength: 1 }),
75
- board: BindingSchema,
76
- context: Type.Optional(Identity),
77
- notes: Type.Optional(Type.Unknown()),
78
- contextError: Type.Optional(Type.String()),
79
- boardError: Type.Optional(Type.String()),
80
- });
88
+ export const checkPlan = compile(Plan).check;
89
+ const Route = obj(
90
+ {
91
+ plan: Plan,
92
+ machine: str(),
93
+ sessionFile: str({ minLength: 1 }),
94
+ phase: union([
95
+ {
96
+ ...lit("pending"),
97
+ type: "string",
98
+ },
99
+ {
100
+ ...lit("ready"),
101
+ type: "string",
102
+ },
103
+ ]),
104
+ detached: opt(
105
+ obj(
106
+ {
107
+ context: opt(unknown()),
108
+ board: opt({
109
+ ...lit(true),
110
+ type: "boolean",
111
+ }),
112
+ },
113
+ { open: true },
114
+ ),
115
+ ),
116
+ },
117
+ { open: true },
118
+ );
119
+ const checkRoute = compile(Route).check;
120
+ export const Snapshot = obj(
121
+ {
122
+ sessionId: str({ minLength: 1 }),
123
+ board: BindingSchema,
124
+ context: opt(Identity),
125
+ notes: opt(unknown()),
126
+ contextError: opt(str()),
127
+ boardError: opt(str()),
128
+ },
129
+ { open: true },
130
+ );
131
+ export const checkSnapshot = compile(Snapshot).check;
81
132
  export type AttachmentPlan = Static<typeof Plan>;
82
133
  export type AttachmentRoute = Static<typeof Route>;
83
134
  export const OWNER = "shepherdr-attachment-owner";
84
135
  export const ROUTE = "shepherdr-attachment-route";
85
136
  export const DETACHED = "shepherdr-attachment-detached";
86
- export const Detachment = Type.Object({
87
- plan: Plan,
88
- context: Type.Optional(Type.Unknown()),
89
- board: Type.Optional(Type.Literal(true)),
90
- });
91
-
92
- function saved<T extends TSchema>(
137
+ export const Detachment = obj(
138
+ {
139
+ plan: Plan,
140
+ context: opt(unknown()),
141
+ board: opt({
142
+ ...lit(true),
143
+ type: "boolean",
144
+ }),
145
+ },
146
+ { open: true },
147
+ );
148
+ export const checkDetachment = compile(Detachment).check;
149
+ function saved<T>(
93
150
  ctx: ExtensionContext,
94
151
  key: string,
95
- schema: T,
96
- ): Static<T>[] {
152
+ check: (value: unknown) => value is T,
153
+ ): T[] {
97
154
  return ctx.sessionManager.getEntries().flatMap((entry) => {
98
155
  if (entry.type !== "custom" || entry.customType !== key) return [];
99
- if (!Check(schema, entry.data))
100
- throw new Error("Invalid saved agent attachment");
101
- return [entry.data as Static<T>];
156
+ if (!check(entry.data)) throw new Error("Invalid saved agent attachment");
157
+ return [entry.data];
102
158
  });
103
159
  }
104
160
  export function inside(path: string, root: string) {
@@ -179,18 +235,18 @@ export function remapResult(
179
235
  }
180
236
 
181
237
  export function detachment(ctx: ExtensionContext, plan: AttachmentPlan) {
182
- return saved(ctx, DETACHED, Detachment).findLast((entry) =>
238
+ return saved(ctx, DETACHED, checkDetachment).findLast((entry) =>
183
239
  isDeepStrictEqual(entry.plan, plan),
184
240
  );
185
241
  }
186
242
  export function attachmentOwner(ctx: ExtensionContext) {
187
- return saved(ctx, OWNER, Plan).findLast(
243
+ return saved(ctx, OWNER, checkPlan).findLast(
188
244
  (plan) => plan.targetSessionId === ctx.sessionManager.getSessionId(),
189
245
  );
190
246
  }
191
247
  export function attachmentRoutes(ctx: ExtensionContext) {
192
248
  const found = new Map<string, AttachmentRoute>();
193
- for (const route of saved(ctx, ROUTE, Route))
249
+ for (const route of saved(ctx, ROUTE, checkRoute))
194
250
  if (route.plan.controllerSessionId === ctx.sessionManager.getSessionId())
195
251
  found.set(route.sessionFile + "\0" + route.machine, route);
196
252
  return [...found.values()];
@@ -3,21 +3,22 @@ import type {
3
3
  ExtensionAPI,
4
4
  ExtensionContext,
5
5
  } from "@earendil-works/pi-coding-agent";
6
+ import type { Static } from "@earendil-works/pi-schema";
6
7
  import type { ContextSharingService } from "@howaboua/pi-agent-comms/context-sharing";
7
- import type { Static } from "typebox";
8
- import { Check } from "typebox/value";
9
8
  import { AttachmentNotes } from "./attachment-notes.js";
10
9
  import {
11
10
  type AttachmentPlan,
12
11
  attachmentOwner,
13
12
  attachmentRoutes,
14
13
  canonicalContextIdentity,
14
+ checkDetachment,
15
+ checkIdentity,
16
+ checkPlan,
15
17
  DETACHED,
16
18
  Detachment,
17
19
  detachment,
18
20
  Identity,
19
21
  OWNER,
20
- Plan,
21
22
  } from "./attachment-protocol.js";
22
23
  import type { AgentBoard } from "./board/host.js";
23
24
  import { type BoardBinding, canonicalBoardBinding } from "./board/records.js";
@@ -56,7 +57,7 @@ export class AttachmentTarget {
56
57
  if (!service?.inspectAttachment || !service.retainAttachmentIdentity)
57
58
  throw new Error(NOTES_UNAVAILABLE);
58
59
  const identity = await service.inspectAttachment(ctx, target);
59
- if (!identity || !Check(Identity, identity))
60
+ if (!identity || !checkIdentity(identity))
60
61
  throw new Error(
61
62
  "Context attachment requires notes-based continuity on both agents",
62
63
  );
@@ -118,7 +119,7 @@ export class AttachmentTarget {
118
119
  if (input.operation === "attachment-export-notes") {
119
120
  if (
120
121
  !("plan" in input) ||
121
- !Check(Plan, input.plan) ||
122
+ !checkPlan(input.plan) ||
122
123
  !isDeepStrictEqual(attachmentOwner(ctx), input.plan)
123
124
  )
124
125
  throw new Error("Invalid checkpoint owner");
@@ -129,7 +130,7 @@ export class AttachmentTarget {
129
130
  if (
130
131
  input.operation !== "attach-commit" ||
131
132
  !("plan" in input) ||
132
- !Check(Plan, input.plan)
133
+ !checkPlan(input.plan)
133
134
  )
134
135
  throw new Error("Invalid attachment request");
135
136
  const plan = input.plan as AttachmentPlan;
@@ -285,7 +286,7 @@ export class AttachmentTarget {
285
286
  input: unknown,
286
287
  signal?: AbortSignal,
287
288
  ) {
288
- if (!Check(Detachment, input)) throw new Error("Invalid detach request");
289
+ if (!checkDetachment(input)) throw new Error("Invalid detach request");
289
290
  const requested = input as Static<typeof Detachment>;
290
291
  if (requested.board) this.board.requireAvailable();
291
292
  const { plan } = requested;
package/src/attachment.ts CHANGED
@@ -3,9 +3,8 @@ import type {
3
3
  ExtensionAPI,
4
4
  ExtensionContext,
5
5
  } from "@earendil-works/pi-coding-agent";
6
+ import type { Static } from "@earendil-works/pi-schema";
6
7
  import type { ContextSharingService } from "@howaboua/pi-agent-comms/context-sharing";
7
- import type { Static } from "typebox";
8
- import { Check } from "typebox/value";
9
8
  import type { AgentsParams } from "./agents-contract.js";
10
9
  import { AttachmentNotes } from "./attachment-notes.js";
11
10
  import {
@@ -13,6 +12,7 @@ import {
13
12
  attachmentOwner,
14
13
  attachmentRoutes,
15
14
  canonicalContextIdentity,
15
+ checkSnapshot,
16
16
  ROUTE,
17
17
  Snapshot,
18
18
  } from "./attachment-protocol.js";
@@ -109,7 +109,7 @@ export class AgentAttachment {
109
109
  signal,
110
110
  );
111
111
  await check();
112
- if (!Check(Snapshot, snapshot))
112
+ if (!checkSnapshot(snapshot))
113
113
  throw new Error(
114
114
  "Target does not support attachment; update and reload Shepherdr",
115
115
  );