@howaboua/pi-shepherdr 0.2.18 → 0.2.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +14 -0
- package/README.md +4 -2
- package/biome.json +1 -1
- package/index.ts +2 -2
- package/package.json +10 -10
- package/src/agents-contract.ts +7 -0
- package/src/agents-discovery.ts +1 -1
- package/src/agents-spawn.ts +11 -2
- package/src/delivery.ts +139 -136
- package/src/messages.ts +4 -8
- package/src/peer-inbox.ts +11 -15
- package/src/remote/shepherdr-peer.mjs +1 -1
- package/src/remote/shepherdr.mjs +1 -1
- package/src/remote-client.ts +1 -1
- package/src/shared-context-host.ts +124 -0
- package/src/shared-context.ts +14 -206
- package/src/spawn-context.ts +159 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @howaboua/pi-shepherdr
|
|
2
2
|
|
|
3
|
+
## 0.2.20
|
|
4
|
+
|
|
5
|
+
- Requires Pi 1.2.0 or newer.
|
|
6
|
+
|
|
7
|
+
- Idle assignments and completion reports now apply Pi's full prompt and tool preparation.
|
|
8
|
+
- Fixed local and remote spawns failing during session startup.
|
|
9
|
+
- Supervision reminders now distinguish other agents' tasks from the current task after compaction.
|
|
10
|
+
|
|
11
|
+
## 0.2.19
|
|
12
|
+
|
|
13
|
+
- Added Codex parent-child metadata for workers when used with PCC, preserved on resume and independent of shared notes.
|
|
14
|
+
|
|
15
|
+
Agent-read help and invalid-source errors now list supported sources.
|
|
16
|
+
|
|
3
17
|
## 0.2.18
|
|
4
18
|
|
|
5
19
|
- Replaced the Ask-specific `answer` action with `reply` for extension replies. Update and reload Ask and Shepherdr together on controllers and workers.
|
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ With Pi Codex's compatible custom developer-message API active, asynchronous wor
|
|
|
14
14
|
pi install npm:@howaboua/pi-shepherdr
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
Requires Pi 1.
|
|
17
|
+
Requires Pi 1.2.0 or newer, Node.js 22.18 or newer, Herdr 0.9 or newer and the Herdr Pi integration:
|
|
18
18
|
|
|
19
19
|
```bash
|
|
20
20
|
herdr integration install pi
|
|
@@ -120,7 +120,7 @@ Keys are command names without `/`; values are nonempty descriptions. Only the g
|
|
|
120
120
|
|
|
121
121
|
Blocked commands return the built-in commands and currently registered, allowed extension commands. Commands return `commandSubmitted: true` without adding a task watch, even through `spawn` or `assign`. Submission does not confirm command success, except `/quit`, which waits for Pi to exit and its pane to close. Built-in commands other than `/quit` require an idle target. Skills and prompt templates keep their existing behavior.
|
|
122
122
|
|
|
123
|
-
Idle messages start a
|
|
123
|
+
Idle messages start a custom-message turn through Pi’s complete preparation chain. Messages arriving during a run use steering, promoted to developer messages when Pi Codex developer delivery is active. Otherwise they remain ordinary Pi custom messages.
|
|
124
124
|
|
|
125
125
|
### Extension messages
|
|
126
126
|
|
|
@@ -194,6 +194,8 @@ Child board calls use the owning Pi sessions and existing SSH connections, not a
|
|
|
194
194
|
|
|
195
195
|
## Shared notes and history
|
|
196
196
|
|
|
197
|
+
With compatible Pi Codex Conversion on both ends, Shepherdr marks fresh spawned workers before their first task with their immediate parent's native Pi session ID and `subagentKind: "collab_spawn"`. Each worker keeps its own native session ID. This relationship works with shared notes disabled and does not grant notes access. Resume preserves it; forks and profiles selecting existing sessions do not acquire an inherited relationship. Older PCC versions without the optional capability retain ordinary delegation. The [extension API guide](../../docs/extension-apis.md#parent-and-worker-relationships) documents PCC's `bindRelationship` service contract for other orchestrators. Shepherdr owns transport, not a separate public relationship RPC API.
|
|
198
|
+
|
|
197
199
|
With Pi Codex Conversion 3.0.40 or newer and notes-based continuity, enable **Share subagent context** under `/codex context` in the controller. It is off by default. Shared `spawn` gives each child a unique context identity before its first turn; `contextAgent` in the result identifies its notes and history. Shared nested spawns stay in the same family. Turning sharing off affects new spawns only; existing identity survives resume. `assign` and independently started agents remain unchanged. Older compatible Conversion versions keep ordinary delegation without sharing.
|
|
198
200
|
|
|
199
201
|
Set `"share_context": false` in a profile to keep its new workers' notes and history independent even when controller sharing is enabled. Omission or `true` follows the controller setting, never turns sharing on itself. This separates context, not filesystem permissions or information included in the task.
|
package/biome.json
CHANGED
package/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { registerAgentEventRenderer } from "./src/messages.js";
|
|
|
11
11
|
import { createPeerCommands } from "./src/peer-commands.js";
|
|
12
12
|
import { registerPeerInbox } from "./src/peer-inbox.js";
|
|
13
13
|
import { installAgentProfiles } from "./src/profiles.js";
|
|
14
|
-
import { registerSharedAgentContext } from "./src/shared-context.js";
|
|
14
|
+
import { registerSharedAgentContext } from "./src/shared-context-host.js";
|
|
15
15
|
import { registerVoiceFocusReceiver } from "./src/voice-handoff.js";
|
|
16
16
|
|
|
17
17
|
const CODE_MODE_PACKAGE = "@howaboua/pi-codex-conversion";
|
|
@@ -23,7 +23,7 @@ export default async function shepherdrExtension(
|
|
|
23
23
|
registerPackageChangelog(pi);
|
|
24
24
|
|
|
25
25
|
await installAgentProfiles();
|
|
26
|
-
|
|
26
|
+
registerDeveloperDelivery(pi);
|
|
27
27
|
const peerCommands = createPeerCommands(pi);
|
|
28
28
|
const fleet = new AgentFleet(pi);
|
|
29
29
|
registerFocusArrivals(pi);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@howaboua/pi-shepherdr",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.20",
|
|
4
4
|
"description": "Persistent blocking and asynchronous Pi agents through Herdr.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
"pack:dry": "npm pack --dry-run"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
|
-
"@howaboua/pi-codex-conversion": ">=
|
|
46
|
+
"@howaboua/pi-codex-conversion": ">=4.0.0",
|
|
47
47
|
"@earendil-works/pi-ai": ">=1.1.0",
|
|
48
|
-
"@earendil-works/pi-coding-agent": ">=1.
|
|
48
|
+
"@earendil-works/pi-coding-agent": ">=1.2.0",
|
|
49
49
|
"@earendil-works/pi-tui": ">=1.1.0",
|
|
50
50
|
"typebox": "*"
|
|
51
51
|
},
|
|
@@ -58,13 +58,13 @@
|
|
|
58
58
|
"@howaboua/pi-agent-board": "0.0.1"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
|
-
"@howaboua/pi-codex-conversion": "
|
|
62
|
-
"@biomejs/biome": "^2.5.
|
|
63
|
-
"@earendil-works/pi-ai": "1.
|
|
64
|
-
"@earendil-works/pi-coding-agent": "1.
|
|
65
|
-
"@earendil-works/pi-tui": "1.
|
|
66
|
-
"@types/node": "^26.
|
|
67
|
-
"typebox": "1.3.
|
|
61
|
+
"@howaboua/pi-codex-conversion": "4.0.0",
|
|
62
|
+
"@biomejs/biome": "^2.5.15",
|
|
63
|
+
"@earendil-works/pi-ai": "1.3.0",
|
|
64
|
+
"@earendil-works/pi-coding-agent": "1.3.0",
|
|
65
|
+
"@earendil-works/pi-tui": "1.3.0",
|
|
66
|
+
"@types/node": "^26.6.5",
|
|
67
|
+
"typebox": "1.3.37",
|
|
68
68
|
"typescript": "^7.0.2"
|
|
69
69
|
},
|
|
70
70
|
"pi": {
|
package/src/agents-contract.ts
CHANGED
|
@@ -182,6 +182,13 @@ export function parseAgentsRequest(input: unknown): AgentsParams {
|
|
|
182
182
|
if (unknown.length > 0) {
|
|
183
183
|
throw new Error(`unknown ${action} field(s): ${unknown.join(", ")}`);
|
|
184
184
|
}
|
|
185
|
+
if (
|
|
186
|
+
action === "read" &&
|
|
187
|
+
"source" in request &&
|
|
188
|
+
!READ_SOURCES.some((source) => source === request["source"])
|
|
189
|
+
) {
|
|
190
|
+
throw new Error(`read source must be one of: ${READ_SOURCES.join(", ")}`);
|
|
191
|
+
}
|
|
185
192
|
if (!Check(AgentsRequest, request)) {
|
|
186
193
|
throw new Error(
|
|
187
194
|
`invalid ${action} request; call agents help for its contract`,
|
package/src/agents-discovery.ts
CHANGED
|
@@ -44,7 +44,7 @@ export async function agentsHelp(
|
|
|
44
44
|
assign: "target message machine? blocking?",
|
|
45
45
|
attach: "target context board machine?",
|
|
46
46
|
detach: "target context board machine?",
|
|
47
|
-
read: "target machine? source? lines?",
|
|
47
|
+
read: "target machine? source=latest|visible|recent? lines?",
|
|
48
48
|
reply: "target extension id message machine?",
|
|
49
49
|
},
|
|
50
50
|
rules: {
|
package/src/agents-spawn.ts
CHANGED
|
@@ -123,10 +123,15 @@ export async function spawnAgent(
|
|
|
123
123
|
status: "starting",
|
|
124
124
|
});
|
|
125
125
|
const agentArgs = profileAgentArgs(profile, { targetLocal: runtime.local });
|
|
126
|
+
const relationship = sharedContext.spawns.prepareRelationship(
|
|
127
|
+
ctx,
|
|
128
|
+
runtime,
|
|
129
|
+
agentArgs,
|
|
130
|
+
);
|
|
126
131
|
const sharing =
|
|
127
132
|
profile.shareContext === false
|
|
128
133
|
? undefined
|
|
129
|
-
: await sharedContext.prepare(ctx, runtime, name, agentArgs);
|
|
134
|
+
: await sharedContext.spawns.prepare(ctx, runtime, name, agentArgs);
|
|
130
135
|
const boardBinding = await board.prepare(ctx, runtime, name, agentArgs);
|
|
131
136
|
const started = await startAgent(
|
|
132
137
|
runtime.client,
|
|
@@ -140,8 +145,10 @@ export async function spawnAgent(
|
|
|
140
145
|
let dispatch;
|
|
141
146
|
let shared: { agentName?: string; warning?: string } | undefined;
|
|
142
147
|
let boardAgent: string | undefined;
|
|
148
|
+
let relationshipWarning: string | undefined;
|
|
143
149
|
const blocking = shouldBlockAgentSpawn(profile.blocking, params.blocking);
|
|
144
150
|
try {
|
|
151
|
+
relationshipWarning = (await relationship?.accept(started.agent))?.warning;
|
|
145
152
|
boardAgent = await boardBinding?.accept(started.agent);
|
|
146
153
|
shared = await sharing?.accept(started.agent);
|
|
147
154
|
dispatch = await dispatchAgentWork(
|
|
@@ -197,6 +204,8 @@ export async function spawnAgent(
|
|
|
197
204
|
next: "Converse, do other work or reply now; completion/blockage arrives even after you reply. No polling or sleep waits",
|
|
198
205
|
}),
|
|
199
206
|
},
|
|
200
|
-
[dispatch.warning, shared?.warning]
|
|
207
|
+
[dispatch.warning, shared?.warning, relationshipWarning]
|
|
208
|
+
.filter(Boolean)
|
|
209
|
+
.join("\n") || undefined,
|
|
201
210
|
);
|
|
202
211
|
}
|
package/src/delivery.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
1
2
|
import type {
|
|
2
3
|
ExtensionAPI,
|
|
3
4
|
ExtensionContext,
|
|
@@ -5,66 +6,68 @@ import type {
|
|
|
5
6
|
import type {
|
|
6
7
|
CodexDeveloperCustomMessage,
|
|
7
8
|
CodexDeveloperMessageOptions,
|
|
8
|
-
|
|
9
|
-
tryStartCodexPreparedIdleKickoff,
|
|
10
|
-
tryStartCodexPreparedIdlePrompt,
|
|
9
|
+
CodexDeveloperMessageRequest,
|
|
11
10
|
} from "@howaboua/pi-codex-conversion/developer-messages";
|
|
12
11
|
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const MODULE = `${PACKAGE}/developer-messages`;
|
|
31
|
-
|
|
32
|
-
export async function registerDeveloperDelivery(
|
|
33
|
-
pi: ExtensionAPI,
|
|
34
|
-
): Promise<void> {
|
|
35
|
-
try {
|
|
36
|
-
const api = await import(
|
|
37
|
-
"@howaboua/pi-codex-conversion/developer-messages"
|
|
12
|
+
const contexts = new WeakMap<ExtensionAPI, ExtensionContext>();
|
|
13
|
+
const PENDING_MESSAGE = "herdr-pending-message";
|
|
14
|
+
interface PendingMessage {
|
|
15
|
+
sessionId: string;
|
|
16
|
+
message: CodexDeveloperCustomMessage & {
|
|
17
|
+
details: { herdrAdmissionId: string };
|
|
18
|
+
};
|
|
19
|
+
queued?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export function registerDeveloperDelivery(pi: ExtensionAPI): void {
|
|
22
|
+
pi.on("session_start", (event, ctx) => {
|
|
23
|
+
contexts.set(pi, ctx);
|
|
24
|
+
const entries = ctx.sessionManager.getEntries();
|
|
25
|
+
const pending = entries.flatMap((entry) =>
|
|
26
|
+
entry.type === "custom" && entry.customType === PENDING_MESSAGE
|
|
27
|
+
? [readPendingMessage(entry.data)]
|
|
28
|
+
: [],
|
|
38
29
|
);
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
30
|
+
const queued = new Set(
|
|
31
|
+
pending
|
|
32
|
+
.filter((data) => data.queued)
|
|
33
|
+
.map((data) => data.message.details.herdrAdmissionId),
|
|
34
|
+
);
|
|
35
|
+
const delivered = new Set(
|
|
36
|
+
entries.flatMap((entry) =>
|
|
37
|
+
entry.type === "custom_message" &&
|
|
38
|
+
entry.details &&
|
|
39
|
+
typeof entry.details === "object" &&
|
|
40
|
+
"herdrAdmissionId" in entry.details
|
|
41
|
+
? [entry.details.herdrAdmissionId]
|
|
42
|
+
: [],
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
for (const data of pending) {
|
|
46
|
+
const id = data.message.details.herdrAdmissionId;
|
|
47
|
+
if (
|
|
48
|
+
data.sessionId !== ctx.sessionManager.getSessionId() ||
|
|
49
|
+
delivered.has(id) ||
|
|
50
|
+
(event.reason === "reload" && queued.has(id))
|
|
51
|
+
)
|
|
52
|
+
continue;
|
|
53
|
+
pi.sendMessage(data.message, {
|
|
54
|
+
deliverAs: "nextTurn",
|
|
55
|
+
triggerTurn: false,
|
|
56
|
+
});
|
|
57
|
+
delivered.add(id);
|
|
58
|
+
if (!data.queued)
|
|
59
|
+
pi.appendEntry(PENDING_MESSAGE, { ...data, queued: true });
|
|
60
|
+
ctx.ui.notify(
|
|
61
|
+
"Pending coordination restored. Send a message to continue.",
|
|
62
|
+
"warning",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
62
65
|
});
|
|
63
66
|
pi.on("session_shutdown", () => {
|
|
64
|
-
|
|
67
|
+
contexts.delete(pi);
|
|
65
68
|
});
|
|
66
69
|
pi.on("input", (event, ctx) => {
|
|
67
|
-
//
|
|
70
|
+
// Input hooks run before Pi reserves a user kickoff; only real steering is promoted.
|
|
68
71
|
if (
|
|
69
72
|
ctx.isIdle() ||
|
|
70
73
|
event.streamingBehavior === undefined ||
|
|
@@ -73,7 +76,7 @@ export async function registerDeveloperDelivery(
|
|
|
73
76
|
)
|
|
74
77
|
return;
|
|
75
78
|
if (
|
|
76
|
-
|
|
79
|
+
trySendDeveloperMessage(
|
|
77
80
|
pi,
|
|
78
81
|
{
|
|
79
82
|
customType: "herdr-agent-message",
|
|
@@ -89,51 +92,34 @@ export async function registerDeveloperDelivery(
|
|
|
89
92
|
});
|
|
90
93
|
}
|
|
91
94
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
if (start)
|
|
117
|
-
throw new Error(
|
|
118
|
-
"Target has a pending turn; retry after it starts or settles",
|
|
119
|
-
);
|
|
120
|
-
// Pi is already idle while earlier settlement handlers are awaiting.
|
|
121
|
-
if (fallbackKickoffs.get(pi) === "running")
|
|
122
|
-
fallbackKickoffs.set(pi, "queued");
|
|
123
|
-
ctx.ui.notify(
|
|
124
|
-
"An automatic turn is pending. If no turn starts, send a user message or reload the session.",
|
|
125
|
-
"warning",
|
|
126
|
-
);
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
fallbackKickoffs.set(pi, "preparing");
|
|
130
|
-
try {
|
|
131
|
-
if (start) start();
|
|
132
|
-
else pi.sendUserMessage(prompt, { deliverAs: "steer" });
|
|
133
|
-
} catch (error) {
|
|
134
|
-
fallbackKickoffs.delete(pi);
|
|
135
|
-
throw error;
|
|
95
|
+
function readPendingMessage(value: unknown): PendingMessage {
|
|
96
|
+
if (
|
|
97
|
+
value &&
|
|
98
|
+
typeof value === "object" &&
|
|
99
|
+
"sessionId" in value &&
|
|
100
|
+
typeof value.sessionId === "string" &&
|
|
101
|
+
"message" in value &&
|
|
102
|
+
value.message &&
|
|
103
|
+
typeof value.message === "object"
|
|
104
|
+
) {
|
|
105
|
+
const message = value.message as PendingMessage["message"];
|
|
106
|
+
if (
|
|
107
|
+
typeof message.customType === "string" &&
|
|
108
|
+
typeof message.content === "string" &&
|
|
109
|
+
typeof message.display === "boolean" &&
|
|
110
|
+
message.details &&
|
|
111
|
+
typeof message.details === "object" &&
|
|
112
|
+
typeof message.details.herdrAdmissionId === "string"
|
|
113
|
+
)
|
|
114
|
+
return {
|
|
115
|
+
sessionId: value.sessionId,
|
|
116
|
+
message,
|
|
117
|
+
queued: "queued" in value && value.queued === true,
|
|
118
|
+
};
|
|
136
119
|
}
|
|
120
|
+
throw new Error(
|
|
121
|
+
"Stored pending coordination is invalid. Reload a valid session before continuing.",
|
|
122
|
+
);
|
|
137
123
|
}
|
|
138
124
|
|
|
139
125
|
export function sendPolicyMessage(
|
|
@@ -141,45 +127,62 @@ export function sendPolicyMessage(
|
|
|
141
127
|
message: CodexDeveloperCustomMessage,
|
|
142
128
|
options: CodexDeveloperMessageOptions,
|
|
143
129
|
): void {
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
130
|
+
if (trySendDeveloperMessage(pi, message, options)) return;
|
|
131
|
+
const ctx = contexts.get(pi);
|
|
132
|
+
if (options.deliverAs !== "nextTurn" && options.triggerTurn && ctx) {
|
|
133
|
+
const request: {
|
|
134
|
+
protocol: 1;
|
|
135
|
+
ctx: ExtensionContext;
|
|
136
|
+
preparation?: Promise<void>;
|
|
137
|
+
} = { protocol: 1, ctx };
|
|
138
|
+
pi.events.emit("pi-notes-compaction:custom-turn-admission:v1", request);
|
|
139
|
+
if (request.preparation) {
|
|
140
|
+
const sessionId = ctx.sessionManager.getSessionId();
|
|
141
|
+
const pending = {
|
|
142
|
+
...message,
|
|
143
|
+
details: { ...message.details, herdrAdmissionId: randomUUID() },
|
|
144
|
+
};
|
|
145
|
+
pi.appendEntry(PENDING_MESSAGE, { sessionId, message: pending });
|
|
146
|
+
void request.preparation
|
|
147
|
+
.then(() => {
|
|
148
|
+
if (
|
|
149
|
+
contexts.get(pi) !== ctx ||
|
|
150
|
+
ctx.sessionManager.getSessionId() !== sessionId
|
|
151
|
+
)
|
|
152
|
+
throw new Error("Session changed before message delivery");
|
|
153
|
+
pi.sendMessage(pending, options);
|
|
154
|
+
})
|
|
155
|
+
.catch((error) => {
|
|
156
|
+
if (contexts.get(pi) !== ctx) return;
|
|
157
|
+
ctx.ui.notify(
|
|
158
|
+
`Coordination remains pending: ${String(error)}. Retry rollover or reload the session.`,
|
|
159
|
+
"error",
|
|
160
|
+
);
|
|
161
|
+
});
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
pi.sendMessage(message, options);
|
|
153
166
|
}
|
|
154
167
|
|
|
155
|
-
function
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
)?.[1];
|
|
171
|
-
const normalized = missing?.replaceAll("\\", "/").replace(/\.js$/, "");
|
|
172
|
-
return (
|
|
173
|
-
missing === PACKAGE ||
|
|
174
|
-
normalized === MODULE ||
|
|
175
|
-
normalized?.endsWith(`/${MODULE}`) === true
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
return (
|
|
179
|
-
(error.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" ||
|
|
180
|
-
error.code === "ERR_UNSUPPORTED_DIR_IMPORT") &&
|
|
181
|
-
(error.message.includes(MODULE) ||
|
|
182
|
-
(error.message.includes("Package subpath './developer-messages'") &&
|
|
183
|
-
error.message.includes(PACKAGE)))
|
|
168
|
+
function trySendDeveloperMessage(
|
|
169
|
+
pi: ExtensionAPI,
|
|
170
|
+
message: CodexDeveloperCustomMessage,
|
|
171
|
+
options: CodexDeveloperMessageOptions,
|
|
172
|
+
): boolean {
|
|
173
|
+
const request: CodexDeveloperMessageRequest = {
|
|
174
|
+
protocol: 1,
|
|
175
|
+
content: message.content,
|
|
176
|
+
message,
|
|
177
|
+
options,
|
|
178
|
+
};
|
|
179
|
+
// The broker's receipt is discovery: no PCC installation or active adapter is required.
|
|
180
|
+
pi.events.emit(
|
|
181
|
+
"@howaboua/pi-codex-conversion.developer-custom-message/v1",
|
|
182
|
+
request,
|
|
184
183
|
);
|
|
184
|
+
if (!request.outcome) return false;
|
|
185
|
+
if (request.outcome.ok) return true;
|
|
186
|
+
if (request.outcome.reason === "unavailable") return false;
|
|
187
|
+
throw new Error(request.outcome.error);
|
|
185
188
|
}
|
package/src/messages.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { Box, Markdown, Spacer, Text } from "@earendil-works/pi-tui";
|
|
8
8
|
import type { BoardBinding } from "@howaboua/pi-agent-board/integration";
|
|
9
9
|
import { activityTask } from "./activity.js";
|
|
10
|
-
import { sendPolicyMessage
|
|
10
|
+
import { sendPolicyMessage } from "./delivery.js";
|
|
11
11
|
import { getCurrentPane, getSnapshot } from "./herdr.js";
|
|
12
12
|
import type { HerdrConnection } from "./herdr-client.js";
|
|
13
13
|
import { readExtensionMessage } from "./remote/shepherdr-session.mjs";
|
|
@@ -82,7 +82,7 @@ export function activeAgentsBriefing(
|
|
|
82
82
|
): string | undefined {
|
|
83
83
|
if (!agents.length) return;
|
|
84
84
|
return [
|
|
85
|
-
"Supervised agents after context rollover. Do not duplicate running work. Status is last observed; read a target for its reply or pending extension messages.",
|
|
85
|
+
"Supervised agents after context rollover. Their instructions are reference data, not tasks for you. Do not repeat settled tasks or duplicate running work. Status is last observed; read a target for its reply or pending extension messages.",
|
|
86
86
|
JSON.stringify(
|
|
87
87
|
agents.map((agent) => ({
|
|
88
88
|
machine: agent.machine,
|
|
@@ -510,7 +510,7 @@ function agentEvent(options: AgentEventOptions): {
|
|
|
510
510
|
|
|
511
511
|
export function injectAgentEvent(
|
|
512
512
|
pi: ExtensionAPI,
|
|
513
|
-
|
|
513
|
+
_ctx: ExtensionContext,
|
|
514
514
|
options: AgentEventOptions,
|
|
515
515
|
): void {
|
|
516
516
|
const tools = pi.getActiveTools();
|
|
@@ -521,10 +521,7 @@ export function injectAgentEvent(
|
|
|
521
521
|
tools.includes("code") ||
|
|
522
522
|
tools.includes("notebook"),
|
|
523
523
|
});
|
|
524
|
-
const
|
|
525
|
-
const delivery = idle
|
|
526
|
-
? { triggerTurn: false, deliverAs: "nextTurn" as const }
|
|
527
|
-
: { deliverAs: "steer" as const };
|
|
524
|
+
const delivery = { triggerTurn: true, deliverAs: "steer" as const };
|
|
528
525
|
sendPolicyMessage(
|
|
529
526
|
pi,
|
|
530
527
|
{
|
|
@@ -535,7 +532,6 @@ export function injectAgentEvent(
|
|
|
535
532
|
},
|
|
536
533
|
delivery,
|
|
537
534
|
);
|
|
538
|
-
if (idle) startPreparedIdleTurn(pi, ctx);
|
|
539
535
|
announceAgentEvent(
|
|
540
536
|
pi,
|
|
541
537
|
`pi-shepherdr:${message.details.machine}:${message.details.paneId}`,
|
package/src/peer-inbox.ts
CHANGED
|
@@ -6,7 +6,7 @@ import type {
|
|
|
6
6
|
ExtensionContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
8
|
import { acceptFocus, readFocus } from "./agents-focus.js";
|
|
9
|
-
import { sendPolicyMessage
|
|
9
|
+
import { sendPolicyMessage } from "./delivery.js";
|
|
10
10
|
import { dispatchExtensionReply } from "./extension-reply.js";
|
|
11
11
|
import type { AgentFleet } from "./fleet.js";
|
|
12
12
|
import { getCurrentPane } from "./herdr.js";
|
|
@@ -34,20 +34,23 @@ export function registerPeerInbox(
|
|
|
34
34
|
): void {
|
|
35
35
|
let close: (() => Promise<void>) | undefined;
|
|
36
36
|
let running = false;
|
|
37
|
-
|
|
37
|
+
const publish = async (ctx: ExtensionContext) => {
|
|
38
|
+
if (process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"])
|
|
39
|
+
return;
|
|
40
|
+
close = await openInbox(pi, ctx, () => running, commands, fleet, catchup);
|
|
41
|
+
};
|
|
42
|
+
pi.on("before_agent_start", () => {
|
|
38
43
|
running = true;
|
|
39
44
|
});
|
|
40
45
|
pi.on("agent_settled", () => {
|
|
41
46
|
running = false;
|
|
42
47
|
});
|
|
43
|
-
pi.on("session_start", async (
|
|
48
|
+
pi.on("session_start", async () => {
|
|
44
49
|
await close?.();
|
|
45
50
|
close = undefined;
|
|
46
51
|
running = false;
|
|
47
|
-
if (process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"])
|
|
48
|
-
return;
|
|
49
|
-
close = await openInbox(pi, ctx, () => running, commands, fleet, catchup);
|
|
50
52
|
});
|
|
53
|
+
pi.on("resources_discover", async (_event, ctx) => publish(ctx));
|
|
51
54
|
pi.on("session_shutdown", async () => {
|
|
52
55
|
await close?.();
|
|
53
56
|
close = undefined;
|
|
@@ -334,8 +337,7 @@ async function openInbox(
|
|
|
334
337
|
} else reply({ ok: true, command }, execute);
|
|
335
338
|
return;
|
|
336
339
|
}
|
|
337
|
-
|
|
338
|
-
else submit();
|
|
340
|
+
submit();
|
|
339
341
|
reply({ ok: true, command });
|
|
340
342
|
return;
|
|
341
343
|
}
|
|
@@ -361,7 +363,6 @@ async function openInbox(
|
|
|
361
363
|
});
|
|
362
364
|
return;
|
|
363
365
|
}
|
|
364
|
-
const promptIdle = ctx.isIdle();
|
|
365
366
|
submitted = true;
|
|
366
367
|
sendPolicyMessage(
|
|
367
368
|
pi,
|
|
@@ -370,14 +371,9 @@ async function openInbox(
|
|
|
370
371
|
content: `${sender}\n${text}${notice.content}`,
|
|
371
372
|
display: true,
|
|
372
373
|
},
|
|
373
|
-
|
|
374
|
-
? { triggerTurn: false, deliverAs: "nextTurn" }
|
|
375
|
-
: { deliverAs: "steer" },
|
|
374
|
+
{ triggerTurn: true, deliverAs: "steer" },
|
|
376
375
|
);
|
|
377
376
|
notice.accepted();
|
|
378
|
-
// Keep arrivals out of history until idle rollover and preparation
|
|
379
|
-
// finish. The shared kickoff coalesces arrivals during preparation.
|
|
380
|
-
if (promptIdle) startPreparedIdleTurn(pi, ctx);
|
|
381
377
|
// Voice failure must not turn accepted delivery into a retry.
|
|
382
378
|
try {
|
|
383
379
|
announcePeerMessage(pi, {
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -18,7 +18,7 @@ import {
|
|
|
18
18
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
19
19
|
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
20
20
|
|
|
21
|
-
const BRIDGE_VERSION =
|
|
21
|
+
const BRIDGE_VERSION = 21;
|
|
22
22
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
23
23
|
const subscriptions = new Map();
|
|
24
24
|
const contextRelays = new Map();
|
package/src/remote-client.ts
CHANGED
|
@@ -17,7 +17,7 @@ import type {
|
|
|
17
17
|
SessionView,
|
|
18
18
|
} from "./types.js";
|
|
19
19
|
|
|
20
|
-
const BRIDGE_VERSION =
|
|
20
|
+
const BRIDGE_VERSION = 21;
|
|
21
21
|
const REMOTE_HELPER = "~/.pi/agent/shepherdr.mjs";
|
|
22
22
|
const REMOTE_PEER_HELPER = "~/.pi/agent/shepherdr-peer.mjs";
|
|
23
23
|
const REMOTE_CHANNEL_HELPER = "~/.pi/agent/shepherdr-channel.mjs";
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type {
|
|
6
|
+
SharedContextRequest,
|
|
7
|
+
SharedContextResult,
|
|
8
|
+
} from "@howaboua/pi-codex-conversion/context-sharing";
|
|
9
|
+
import { attachmentOwner, attachmentRoutes } from "./attachment-protocol.js";
|
|
10
|
+
import type { AgentBoard } from "./board/host.js";
|
|
11
|
+
import type { AgentFleet } from "./fleet.js";
|
|
12
|
+
import {
|
|
13
|
+
listenContext,
|
|
14
|
+
sessionContextPath,
|
|
15
|
+
} from "./remote/shepherdr-context.mjs";
|
|
16
|
+
import { SharedAgentContext } from "./shared-context.js";
|
|
17
|
+
|
|
18
|
+
export async function registerSharedAgentContext(
|
|
19
|
+
pi: ExtensionAPI,
|
|
20
|
+
fleet: AgentFleet,
|
|
21
|
+
board: AgentBoard,
|
|
22
|
+
): Promise<SharedAgentContext> {
|
|
23
|
+
let api:
|
|
24
|
+
| typeof import("@howaboua/pi-codex-conversion/context-sharing")
|
|
25
|
+
| undefined;
|
|
26
|
+
try {
|
|
27
|
+
api = await import("@howaboua/pi-codex-conversion/context-sharing");
|
|
28
|
+
} catch (error) {
|
|
29
|
+
const failure = error as { code?: string; message?: string };
|
|
30
|
+
const missing = failure.message?.match(
|
|
31
|
+
/Cannot find (?:package|module) ['"]([^'"]+)['"]/,
|
|
32
|
+
)?.[1];
|
|
33
|
+
const module = "@howaboua/pi-codex-conversion/context-sharing";
|
|
34
|
+
const unavailable =
|
|
35
|
+
((failure.code === "ERR_MODULE_NOT_FOUND" ||
|
|
36
|
+
failure.code === "MODULE_NOT_FOUND") &&
|
|
37
|
+
(missing === "@howaboua/pi-codex-conversion" ||
|
|
38
|
+
missing === module ||
|
|
39
|
+
missing?.endsWith(`/${module}`))) ||
|
|
40
|
+
(failure.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" &&
|
|
41
|
+
failure.message?.includes("@howaboua/pi-codex-conversion") &&
|
|
42
|
+
failure.message.includes("./context-sharing"));
|
|
43
|
+
if (!unavailable)
|
|
44
|
+
throw new Error(
|
|
45
|
+
"Update pi-codex-conversion to use shared agent context",
|
|
46
|
+
{ cause: error },
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
const connection = api?.connectCodexContextSharing(pi);
|
|
50
|
+
const shared = new SharedAgentContext(
|
|
51
|
+
pi,
|
|
52
|
+
fleet,
|
|
53
|
+
() => connection?.service,
|
|
54
|
+
board,
|
|
55
|
+
);
|
|
56
|
+
let close: (() => Promise<void>) | undefined;
|
|
57
|
+
let unregister: (() => void) | undefined;
|
|
58
|
+
const publish = async (ctx: ExtensionContext) => {
|
|
59
|
+
const file = ctx.sessionManager.getSessionFile();
|
|
60
|
+
if (file)
|
|
61
|
+
close = await listenContext(
|
|
62
|
+
sessionContextPath(file),
|
|
63
|
+
(request: unknown, signal: AbortSignal) =>
|
|
64
|
+
shared.handle(ctx, request, signal),
|
|
65
|
+
);
|
|
66
|
+
};
|
|
67
|
+
pi.on("session_start", async () => {
|
|
68
|
+
await close?.();
|
|
69
|
+
close = undefined;
|
|
70
|
+
unregister?.();
|
|
71
|
+
unregister = connection?.service?.registerRouter(
|
|
72
|
+
Object.assign(
|
|
73
|
+
async (
|
|
74
|
+
context: ExtensionContext,
|
|
75
|
+
request: SharedContextRequest,
|
|
76
|
+
signal?: AbortSignal,
|
|
77
|
+
) => {
|
|
78
|
+
const attached = await shared.attachment.routing.route(
|
|
79
|
+
context,
|
|
80
|
+
request,
|
|
81
|
+
[],
|
|
82
|
+
(route, next) =>
|
|
83
|
+
fleet
|
|
84
|
+
.connected(route.machine)
|
|
85
|
+
.client.requestContext(
|
|
86
|
+
sessionContextPath(route.sessionFile),
|
|
87
|
+
next,
|
|
88
|
+
signal,
|
|
89
|
+
),
|
|
90
|
+
signal,
|
|
91
|
+
);
|
|
92
|
+
if (attached) return attached;
|
|
93
|
+
if (connection?.service?.describe(context)?.storage === "remote")
|
|
94
|
+
return undefined;
|
|
95
|
+
return shared.handle(
|
|
96
|
+
context,
|
|
97
|
+
{ operation: "context", request, visited: [] },
|
|
98
|
+
signal,
|
|
99
|
+
) as Promise<SharedContextResult>;
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
requiresRemoteScope: (context: ExtensionContext) =>
|
|
103
|
+
attachmentOwner(context)?.context?.controller.storage ===
|
|
104
|
+
"remote" ||
|
|
105
|
+
attachmentRoutes(context).some(
|
|
106
|
+
(route) =>
|
|
107
|
+
route.phase === "ready" &&
|
|
108
|
+
route.plan.context?.target.storage === "remote",
|
|
109
|
+
),
|
|
110
|
+
},
|
|
111
|
+
),
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
pi.on("resources_discover", async (_event, ctx) => publish(ctx));
|
|
115
|
+
pi.on("session_shutdown", async () => {
|
|
116
|
+
unregister?.();
|
|
117
|
+
await close?.();
|
|
118
|
+
connection?.dispose();
|
|
119
|
+
});
|
|
120
|
+
fleet.setContextRelay((ctx, request, signal) =>
|
|
121
|
+
shared.handle(ctx, request, signal),
|
|
122
|
+
);
|
|
123
|
+
return shared;
|
|
124
|
+
}
|
package/src/shared-context.ts
CHANGED
|
@@ -11,29 +11,16 @@ import type {
|
|
|
11
11
|
} from "@howaboua/pi-codex-conversion/context-sharing";
|
|
12
12
|
import { Check } from "typebox/value";
|
|
13
13
|
import { AgentAttachment } from "./attachment.js";
|
|
14
|
-
import {
|
|
15
|
-
attachmentOwner,
|
|
16
|
-
attachmentRoutes,
|
|
17
|
-
Identity,
|
|
18
|
-
} from "./attachment-protocol.js";
|
|
14
|
+
import { Identity } from "./attachment-protocol.js";
|
|
19
15
|
import type { AgentBoard } from "./board/host.js";
|
|
20
|
-
import type { AgentFleet
|
|
16
|
+
import type { AgentFleet } from "./fleet.js";
|
|
21
17
|
import { sessionPath } from "./herdr.js";
|
|
22
18
|
import {
|
|
23
|
-
listenContext,
|
|
24
19
|
requestContext,
|
|
25
20
|
sessionContextPath,
|
|
26
21
|
} from "./remote/shepherdr-context.mjs";
|
|
27
|
-
import type
|
|
22
|
+
import { MEMBER_ENTRY, type Member, SpawnContext } from "./spawn-context.js";
|
|
28
23
|
|
|
29
|
-
const MEMBER_ENTRY = "herdr-context-child";
|
|
30
|
-
interface Member {
|
|
31
|
-
agentName: string;
|
|
32
|
-
sessionId: string;
|
|
33
|
-
threadId: string;
|
|
34
|
-
machine: string;
|
|
35
|
-
sessionFile: string;
|
|
36
|
-
}
|
|
37
24
|
interface Routing {
|
|
38
25
|
transport: "shepherdr";
|
|
39
26
|
parent: string;
|
|
@@ -45,12 +32,12 @@ interface ContextEnvelope {
|
|
|
45
32
|
}
|
|
46
33
|
|
|
47
34
|
export class SharedAgentContext {
|
|
48
|
-
private readonly pi: ExtensionAPI;
|
|
49
35
|
private readonly fleet: AgentFleet;
|
|
50
36
|
private readonly getService: () => ContextSharingService | undefined;
|
|
51
37
|
private readonly board: AgentBoard;
|
|
52
38
|
private running = false;
|
|
53
39
|
readonly attachment: AgentAttachment;
|
|
40
|
+
readonly spawns: SpawnContext;
|
|
54
41
|
|
|
55
42
|
constructor(
|
|
56
43
|
pi: ExtensionAPI,
|
|
@@ -58,12 +45,12 @@ export class SharedAgentContext {
|
|
|
58
45
|
getService: () => ContextSharingService | undefined,
|
|
59
46
|
board: AgentBoard,
|
|
60
47
|
) {
|
|
61
|
-
this.pi = pi;
|
|
62
48
|
this.fleet = fleet;
|
|
63
49
|
this.getService = getService;
|
|
64
50
|
this.board = board;
|
|
65
51
|
this.attachment = new AgentAttachment(pi, board, getService);
|
|
66
|
-
|
|
52
|
+
this.spawns = new SpawnContext(pi, getService);
|
|
53
|
+
pi.on("before_agent_start", () => {
|
|
67
54
|
this.running = true;
|
|
68
55
|
});
|
|
69
56
|
pi.on("agent_settled", () => {
|
|
@@ -160,6 +147,14 @@ export class SharedAgentContext {
|
|
|
160
147
|
if (!service || !identity) return null;
|
|
161
148
|
return service.bind(ctx, "binding" in value ? value.binding : undefined);
|
|
162
149
|
}
|
|
150
|
+
if ("operation" in value && value.operation === "bind-relationship") {
|
|
151
|
+
return (
|
|
152
|
+
service?.bindRelationship?.(
|
|
153
|
+
ctx,
|
|
154
|
+
"relationship" in value ? value.relationship : undefined,
|
|
155
|
+
) ?? null
|
|
156
|
+
);
|
|
157
|
+
}
|
|
163
158
|
if (!identity || !service)
|
|
164
159
|
throw new Error("Context sharing is unavailable in this session");
|
|
165
160
|
const envelope = value as Partial<ContextEnvelope>;
|
|
@@ -289,191 +284,4 @@ export class SharedAgentContext {
|
|
|
289
284
|
);
|
|
290
285
|
return matches[0]!;
|
|
291
286
|
}
|
|
292
|
-
|
|
293
|
-
async prepare(
|
|
294
|
-
ctx: ExtensionContext,
|
|
295
|
-
runtime: ConnectedMachine,
|
|
296
|
-
name: string,
|
|
297
|
-
args: readonly string[] = [],
|
|
298
|
-
) {
|
|
299
|
-
const service = this.getService();
|
|
300
|
-
if (!service?.canCreateChild(ctx)) return undefined;
|
|
301
|
-
const parent = service.describe(ctx);
|
|
302
|
-
if (!parent) return undefined;
|
|
303
|
-
if (
|
|
304
|
-
args.some((arg) =>
|
|
305
|
-
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
306
|
-
arg,
|
|
307
|
-
),
|
|
308
|
-
)
|
|
309
|
-
)
|
|
310
|
-
throw new Error(
|
|
311
|
-
"Shared context spawn cannot use profile session-selection arguments; use assign for existing agents",
|
|
312
|
-
);
|
|
313
|
-
const parentFile = ctx.sessionManager.getSessionFile();
|
|
314
|
-
if (!parentFile)
|
|
315
|
-
throw new Error(
|
|
316
|
-
"Shared context spawn requires a saved controller session",
|
|
317
|
-
);
|
|
318
|
-
const routing: Routing | undefined =
|
|
319
|
-
parent.storage === "session"
|
|
320
|
-
? {
|
|
321
|
-
transport: "shepherdr",
|
|
322
|
-
parent: runtime.local
|
|
323
|
-
? sessionContextPath(parentFile)
|
|
324
|
-
: runtime.client.contextRelayPath(),
|
|
325
|
-
}
|
|
326
|
-
: undefined;
|
|
327
|
-
const { binding, adopt } = await service.createChild(ctx, {
|
|
328
|
-
name,
|
|
329
|
-
...(routing ? { routing } : {}),
|
|
330
|
-
});
|
|
331
|
-
return {
|
|
332
|
-
accept: async (agent: PaneInfo) => {
|
|
333
|
-
const sessionFile = sessionPath(agent);
|
|
334
|
-
if (!sessionFile)
|
|
335
|
-
throw new Error("Spawned agent has no native Pi session path");
|
|
336
|
-
const value = await runtime.client.requestContext(
|
|
337
|
-
sessionContextPath(sessionFile),
|
|
338
|
-
{ operation: "bind", binding },
|
|
339
|
-
);
|
|
340
|
-
if (!value)
|
|
341
|
-
return {
|
|
342
|
-
warning:
|
|
343
|
-
"Spawned without shared context: the target needs Codex conversion with notes-based continuity",
|
|
344
|
-
};
|
|
345
|
-
if (
|
|
346
|
-
typeof value !== "object" ||
|
|
347
|
-
!("sessionId" in value) ||
|
|
348
|
-
value.sessionId !== binding.sessionId ||
|
|
349
|
-
!("agentName" in value) ||
|
|
350
|
-
value.agentName !== binding.agentName ||
|
|
351
|
-
!("storage" in value) ||
|
|
352
|
-
value.storage !== binding.storage ||
|
|
353
|
-
(binding.accountScope !== undefined &&
|
|
354
|
-
(!("accountScope" in value) ||
|
|
355
|
-
value.accountScope !== binding.accountScope))
|
|
356
|
-
)
|
|
357
|
-
throw new Error(
|
|
358
|
-
"Spawned agent did not adopt its shared context identity and storage mode",
|
|
359
|
-
);
|
|
360
|
-
if (ctx.sessionManager.getSessionId() !== parent.threadId)
|
|
361
|
-
throw new Error("Controller session changed during spawn");
|
|
362
|
-
await adopt();
|
|
363
|
-
this.pi.appendEntry<Member>(MEMBER_ENTRY, {
|
|
364
|
-
agentName: binding.agentName,
|
|
365
|
-
sessionId: parent.sessionId,
|
|
366
|
-
threadId: parent.threadId,
|
|
367
|
-
machine: runtime.machine,
|
|
368
|
-
sessionFile,
|
|
369
|
-
});
|
|
370
|
-
return { agentName: binding.agentName };
|
|
371
|
-
},
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
export async function registerSharedAgentContext(
|
|
377
|
-
pi: ExtensionAPI,
|
|
378
|
-
fleet: AgentFleet,
|
|
379
|
-
board: AgentBoard,
|
|
380
|
-
): Promise<SharedAgentContext> {
|
|
381
|
-
let api:
|
|
382
|
-
| typeof import("@howaboua/pi-codex-conversion/context-sharing")
|
|
383
|
-
| undefined;
|
|
384
|
-
try {
|
|
385
|
-
api = await import("@howaboua/pi-codex-conversion/context-sharing");
|
|
386
|
-
} catch (error) {
|
|
387
|
-
const failure = error as { code?: string; message?: string };
|
|
388
|
-
const missing = failure.message?.match(
|
|
389
|
-
/Cannot find (?:package|module) ['"]([^'"]+)['"]/,
|
|
390
|
-
)?.[1];
|
|
391
|
-
const module = "@howaboua/pi-codex-conversion/context-sharing";
|
|
392
|
-
const unavailable =
|
|
393
|
-
((failure.code === "ERR_MODULE_NOT_FOUND" ||
|
|
394
|
-
failure.code === "MODULE_NOT_FOUND") &&
|
|
395
|
-
(missing === "@howaboua/pi-codex-conversion" ||
|
|
396
|
-
missing === module ||
|
|
397
|
-
missing?.endsWith(`/${module}`))) ||
|
|
398
|
-
(failure.code === "ERR_PACKAGE_PATH_NOT_EXPORTED" &&
|
|
399
|
-
failure.message?.includes("@howaboua/pi-codex-conversion") &&
|
|
400
|
-
failure.message.includes("./context-sharing"));
|
|
401
|
-
if (!unavailable)
|
|
402
|
-
throw new Error(
|
|
403
|
-
"Update pi-codex-conversion to use shared agent context",
|
|
404
|
-
{ cause: error },
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
const connection = api?.connectCodexContextSharing(pi);
|
|
408
|
-
const shared = new SharedAgentContext(
|
|
409
|
-
pi,
|
|
410
|
-
fleet,
|
|
411
|
-
() => connection?.service,
|
|
412
|
-
board,
|
|
413
|
-
);
|
|
414
|
-
let close: (() => Promise<void>) | undefined;
|
|
415
|
-
let unregister: (() => void) | undefined;
|
|
416
|
-
pi.on("session_start", async (_event, ctx) => {
|
|
417
|
-
await close?.();
|
|
418
|
-
close = undefined;
|
|
419
|
-
unregister?.();
|
|
420
|
-
unregister = connection?.service?.registerRouter(
|
|
421
|
-
Object.assign(
|
|
422
|
-
async (
|
|
423
|
-
context: ExtensionContext,
|
|
424
|
-
request: SharedContextRequest,
|
|
425
|
-
signal?: AbortSignal,
|
|
426
|
-
) => {
|
|
427
|
-
const attached = await shared.attachment.routing.route(
|
|
428
|
-
context,
|
|
429
|
-
request,
|
|
430
|
-
[],
|
|
431
|
-
(route, next) =>
|
|
432
|
-
fleet
|
|
433
|
-
.connected(route.machine)
|
|
434
|
-
.client.requestContext(
|
|
435
|
-
sessionContextPath(route.sessionFile),
|
|
436
|
-
next,
|
|
437
|
-
signal,
|
|
438
|
-
),
|
|
439
|
-
signal,
|
|
440
|
-
);
|
|
441
|
-
if (attached) return attached;
|
|
442
|
-
if (connection?.service?.describe(context)?.storage === "remote")
|
|
443
|
-
return undefined;
|
|
444
|
-
return shared.handle(
|
|
445
|
-
context,
|
|
446
|
-
{ operation: "context", request, visited: [] },
|
|
447
|
-
signal,
|
|
448
|
-
) as Promise<SharedContextResult>;
|
|
449
|
-
},
|
|
450
|
-
{
|
|
451
|
-
requiresRemoteScope: (context: ExtensionContext) =>
|
|
452
|
-
attachmentOwner(context)?.context?.controller.storage ===
|
|
453
|
-
"remote" ||
|
|
454
|
-
attachmentRoutes(context).some(
|
|
455
|
-
(route) =>
|
|
456
|
-
route.phase === "ready" &&
|
|
457
|
-
route.plan.context?.target.storage === "remote",
|
|
458
|
-
),
|
|
459
|
-
},
|
|
460
|
-
),
|
|
461
|
-
);
|
|
462
|
-
const file = ctx.sessionManager.getSessionFile();
|
|
463
|
-
if (file)
|
|
464
|
-
close = await listenContext(
|
|
465
|
-
sessionContextPath(file),
|
|
466
|
-
(request: unknown, signal: AbortSignal) =>
|
|
467
|
-
shared.handle(ctx, request, signal),
|
|
468
|
-
);
|
|
469
|
-
});
|
|
470
|
-
pi.on("session_shutdown", async () => {
|
|
471
|
-
unregister?.();
|
|
472
|
-
await close?.();
|
|
473
|
-
connection?.dispose();
|
|
474
|
-
});
|
|
475
|
-
fleet.setContextRelay((ctx, request, signal) =>
|
|
476
|
-
shared.handle(ctx, request, signal),
|
|
477
|
-
);
|
|
478
|
-
return shared;
|
|
479
287
|
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from "node:util";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import type { ContextSharingService } from "@howaboua/pi-codex-conversion/context-sharing";
|
|
7
|
+
import type { ConnectedMachine } from "./fleet.js";
|
|
8
|
+
import { sessionPath } from "./herdr.js";
|
|
9
|
+
import { sessionContextPath } from "./remote/shepherdr-context.mjs";
|
|
10
|
+
import type { PaneInfo } from "./types.js";
|
|
11
|
+
|
|
12
|
+
export const MEMBER_ENTRY = "herdr-context-child";
|
|
13
|
+
export interface Member {
|
|
14
|
+
agentName: string;
|
|
15
|
+
sessionId: string;
|
|
16
|
+
threadId: string;
|
|
17
|
+
machine: string;
|
|
18
|
+
sessionFile: string;
|
|
19
|
+
}
|
|
20
|
+
interface Routing {
|
|
21
|
+
transport: "shepherdr";
|
|
22
|
+
parent: string;
|
|
23
|
+
}
|
|
24
|
+
export class SpawnContext {
|
|
25
|
+
private readonly pi: ExtensionAPI;
|
|
26
|
+
private readonly getService: () => ContextSharingService | undefined;
|
|
27
|
+
constructor(
|
|
28
|
+
pi: ExtensionAPI,
|
|
29
|
+
getService: () => ContextSharingService | undefined,
|
|
30
|
+
) {
|
|
31
|
+
this.pi = pi;
|
|
32
|
+
this.getService = getService;
|
|
33
|
+
}
|
|
34
|
+
prepareRelationship(
|
|
35
|
+
ctx: ExtensionContext,
|
|
36
|
+
runtime: ConnectedMachine,
|
|
37
|
+
args: readonly string[],
|
|
38
|
+
) {
|
|
39
|
+
if (!this.getService()?.bindRelationship) return undefined;
|
|
40
|
+
// Session-selection profiles resume existing identities rather than creating workers.
|
|
41
|
+
if (
|
|
42
|
+
args.some((arg) =>
|
|
43
|
+
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
44
|
+
arg,
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
)
|
|
48
|
+
return undefined;
|
|
49
|
+
const parentThreadId = ctx.sessionManager.getSessionId();
|
|
50
|
+
const relationship = { parentThreadId, subagentKind: "collab_spawn" };
|
|
51
|
+
return {
|
|
52
|
+
accept: async (agent: PaneInfo) => {
|
|
53
|
+
const file = sessionPath(agent);
|
|
54
|
+
if (!file) throw new Error("Spawned agent has no saved Pi session");
|
|
55
|
+
const value = await runtime.client.requestContext(
|
|
56
|
+
sessionContextPath(file),
|
|
57
|
+
{
|
|
58
|
+
operation: "bind-relationship",
|
|
59
|
+
relationship,
|
|
60
|
+
},
|
|
61
|
+
);
|
|
62
|
+
if (ctx.sessionManager.getSessionId() !== parentThreadId)
|
|
63
|
+
throw new Error("Controller session changed during spawn");
|
|
64
|
+
if (value === null)
|
|
65
|
+
return {
|
|
66
|
+
warning:
|
|
67
|
+
"Worker relationship marking requires Codex conversion on the target",
|
|
68
|
+
};
|
|
69
|
+
if (!isDeepStrictEqual(value, relationship))
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Spawned agent did not adopt its parent relationship",
|
|
72
|
+
);
|
|
73
|
+
return undefined;
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
async prepare(
|
|
79
|
+
ctx: ExtensionContext,
|
|
80
|
+
runtime: ConnectedMachine,
|
|
81
|
+
name: string,
|
|
82
|
+
args: readonly string[] = [],
|
|
83
|
+
) {
|
|
84
|
+
const service = this.getService();
|
|
85
|
+
if (!service?.canCreateChild(ctx)) return undefined;
|
|
86
|
+
const parent = service.describe(ctx);
|
|
87
|
+
if (!parent) return undefined;
|
|
88
|
+
if (
|
|
89
|
+
args.some((arg) =>
|
|
90
|
+
/^(?:--(?:session(?:-id)?|continue|resume|no-session|fork)(?:=|$)|-[cr]$)/.test(
|
|
91
|
+
arg,
|
|
92
|
+
),
|
|
93
|
+
)
|
|
94
|
+
)
|
|
95
|
+
throw new Error(
|
|
96
|
+
"Shared context spawn cannot use profile session-selection arguments; use assign for existing agents",
|
|
97
|
+
);
|
|
98
|
+
const parentFile = ctx.sessionManager.getSessionFile();
|
|
99
|
+
if (!parentFile)
|
|
100
|
+
throw new Error(
|
|
101
|
+
"Shared context spawn requires a saved controller session",
|
|
102
|
+
);
|
|
103
|
+
const routing: Routing | undefined =
|
|
104
|
+
parent.storage === "session"
|
|
105
|
+
? {
|
|
106
|
+
transport: "shepherdr",
|
|
107
|
+
parent: runtime.local
|
|
108
|
+
? sessionContextPath(parentFile)
|
|
109
|
+
: runtime.client.contextRelayPath(),
|
|
110
|
+
}
|
|
111
|
+
: undefined;
|
|
112
|
+
const { binding, adopt } = await service.createChild(ctx, {
|
|
113
|
+
name,
|
|
114
|
+
...(routing ? { routing } : {}),
|
|
115
|
+
});
|
|
116
|
+
return {
|
|
117
|
+
accept: async (agent: PaneInfo) => {
|
|
118
|
+
const sessionFile = sessionPath(agent);
|
|
119
|
+
if (!sessionFile)
|
|
120
|
+
throw new Error("Spawned agent has no native Pi session path");
|
|
121
|
+
const value = await runtime.client.requestContext(
|
|
122
|
+
sessionContextPath(sessionFile),
|
|
123
|
+
{ operation: "bind", binding },
|
|
124
|
+
);
|
|
125
|
+
if (!value)
|
|
126
|
+
return {
|
|
127
|
+
warning:
|
|
128
|
+
"Spawned without shared context: the target needs Codex conversion with notes-based continuity",
|
|
129
|
+
};
|
|
130
|
+
if (
|
|
131
|
+
typeof value !== "object" ||
|
|
132
|
+
!("sessionId" in value) ||
|
|
133
|
+
value.sessionId !== binding.sessionId ||
|
|
134
|
+
!("agentName" in value) ||
|
|
135
|
+
value.agentName !== binding.agentName ||
|
|
136
|
+
!("storage" in value) ||
|
|
137
|
+
value.storage !== binding.storage ||
|
|
138
|
+
(binding.accountScope !== undefined &&
|
|
139
|
+
(!("accountScope" in value) ||
|
|
140
|
+
value.accountScope !== binding.accountScope))
|
|
141
|
+
)
|
|
142
|
+
throw new Error(
|
|
143
|
+
"Spawned agent did not adopt its shared context identity and storage mode",
|
|
144
|
+
);
|
|
145
|
+
if (ctx.sessionManager.getSessionId() !== parent.threadId)
|
|
146
|
+
throw new Error("Controller session changed during spawn");
|
|
147
|
+
await adopt();
|
|
148
|
+
this.pi.appendEntry<Member>(MEMBER_ENTRY, {
|
|
149
|
+
agentName: binding.agentName,
|
|
150
|
+
sessionId: parent.sessionId,
|
|
151
|
+
threadId: parent.threadId,
|
|
152
|
+
machine: runtime.machine,
|
|
153
|
+
sessionFile,
|
|
154
|
+
});
|
|
155
|
+
return { agentName: binding.agentName };
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|