@howaboua/pi-shepherdr 0.2.15 → 0.2.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +42 -33
- package/index.ts +16 -26
- package/package.json +11 -11
- package/src/agents-contract.ts +36 -8
- package/src/agents-discovery.ts +23 -7
- package/src/agents-focus.ts +242 -0
- package/src/agents-spawn.ts +23 -0
- package/src/agents-tool.ts +46 -6
- package/src/attachment-protocol.ts +7 -3
- package/src/attachment-target.ts +3 -1
- package/src/attachment.ts +3 -1
- package/src/board/host.ts +98 -366
- package/src/board/membership.ts +14 -8
- package/src/command-config.ts +51 -0
- package/src/control-panel-actions.ts +0 -20
- package/src/control-panel-items.ts +8 -36
- package/src/control-panel-view.ts +2 -11
- package/src/control-panel.ts +2 -11
- package/src/controller.ts +5 -5
- package/src/delivery.ts +0 -33
- package/src/herdr-client.ts +52 -6
- package/src/messages.ts +61 -61
- package/src/monitor-events.ts +24 -2
- package/src/monitor-questions.ts +12 -3
- package/src/monitor.ts +2 -2
- package/src/peer-channel.ts +39 -0
- package/src/peer-commands.ts +187 -0
- package/src/peer-inbox.ts +232 -55
- package/src/remote/shepherdr-channel.mjs +225 -0
- package/src/remote/shepherdr-checkpoints.mjs +5 -3
- package/src/remote/shepherdr-peer.mjs +198 -22
- package/src/remote/shepherdr.mjs +63 -8
- package/src/remote-client.ts +121 -11
- package/src/settlement.ts +1 -0
- package/src/shared-context.ts +104 -1
- package/src/types.ts +29 -0
- package/src/voice-handoff.ts +177 -0
- package/src/board/archive.ts +0 -77
- package/src/board/awareness.ts +0 -115
- package/src/board/config.ts +0 -121
- package/src/board/contract.ts +0 -181
- package/src/board/identity.ts +0 -169
- package/src/board/paging.ts +0 -55
- package/src/board/protocol.ts +0 -73
- package/src/board/queries.ts +0 -176
- package/src/board/response.ts +0 -56
- package/src/board/store.ts +0 -327
- package/src/board/tool.ts +0 -23
- package/src/board/turns.ts +0 -100
- package/src/context-briefing.ts +0 -226
package/src/monitor.ts
CHANGED
|
@@ -174,7 +174,7 @@ export class AgentMonitor {
|
|
|
174
174
|
new Error(`${paneId} was unwatched before its work settled`),
|
|
175
175
|
);
|
|
176
176
|
this.persist();
|
|
177
|
-
|
|
177
|
+
await this.questions.refresh();
|
|
178
178
|
await this.events.refresh();
|
|
179
179
|
return true;
|
|
180
180
|
}
|
|
@@ -327,7 +327,7 @@ export class AgentMonitor {
|
|
|
327
327
|
}
|
|
328
328
|
|
|
329
329
|
private refreshAfterEvent(): void {
|
|
330
|
-
|
|
330
|
+
void this.questions.refresh();
|
|
331
331
|
// MonitorEvents reports failures through onWarning before rejecting awaited updates.
|
|
332
332
|
void this.events.refresh().catch(() => undefined);
|
|
333
333
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ExtensionAPI,
|
|
3
|
+
ExtensionContext,
|
|
4
|
+
} from "@earendil-works/pi-coding-agent";
|
|
5
|
+
import type { FocusRequest, PaneInfo, PeerChannel } from "./types.js";
|
|
6
|
+
|
|
7
|
+
type Receiver = (
|
|
8
|
+
channel: PeerChannel,
|
|
9
|
+
request: FocusRequest,
|
|
10
|
+
ctx: ExtensionContext,
|
|
11
|
+
pane: PaneInfo,
|
|
12
|
+
) => Promise<void>;
|
|
13
|
+
const receivers = new WeakMap<ExtensionAPI, Receiver>();
|
|
14
|
+
|
|
15
|
+
/** Resolve after admission; the receiver owns the channel through call shutdown. */
|
|
16
|
+
export function registerFocusChannelReceiver(
|
|
17
|
+
pi: ExtensionAPI,
|
|
18
|
+
receiver: Receiver,
|
|
19
|
+
): () => void {
|
|
20
|
+
if (receivers.has(pi))
|
|
21
|
+
throw new Error("Focus channel receiver already registered");
|
|
22
|
+
receivers.set(pi, receiver);
|
|
23
|
+
return () => {
|
|
24
|
+
if (receivers.get(pi) === receiver) receivers.delete(pi);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function acceptFocusChannel(
|
|
29
|
+
pi: ExtensionAPI,
|
|
30
|
+
channel: PeerChannel,
|
|
31
|
+
request: FocusRequest,
|
|
32
|
+
ctx: ExtensionContext,
|
|
33
|
+
pane: PaneInfo,
|
|
34
|
+
): Promise<void> {
|
|
35
|
+
const receiver = receivers.get(pi);
|
|
36
|
+
if (!receiver)
|
|
37
|
+
throw new Error("Voice transfer is unavailable in the destination session");
|
|
38
|
+
await receiver(channel, request, ctx, pane);
|
|
39
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionCommandContext,
|
|
5
|
+
ExtensionContext,
|
|
6
|
+
} from "@earendil-works/pi-coding-agent";
|
|
7
|
+
import { readExtensionCommands } from "./command-config.js";
|
|
8
|
+
|
|
9
|
+
const builtins = {
|
|
10
|
+
quit: "Shut down Pi and close its pane",
|
|
11
|
+
model: "Set model: /model provider/model-id",
|
|
12
|
+
thinking: "Set thinking: /thinking off|minimal|low|medium|high|xhigh",
|
|
13
|
+
name: "Set session name: /name name",
|
|
14
|
+
new: "Start a new session",
|
|
15
|
+
reload: "Reload extensions and resources",
|
|
16
|
+
resume: "Resume a session: /resume session-path",
|
|
17
|
+
compact: "Compact context: /compact [instructions]",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function createPeerCommands(pi: ExtensionAPI) {
|
|
21
|
+
const pending = new Map<
|
|
22
|
+
string,
|
|
23
|
+
{
|
|
24
|
+
session: string | undefined;
|
|
25
|
+
run: (ctx: ExtensionCommandContext) => Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
>();
|
|
28
|
+
pi.on("session_shutdown", () => pending.clear());
|
|
29
|
+
const available = (configured?: Record<string, string>) => {
|
|
30
|
+
let allowed: Record<string, string>;
|
|
31
|
+
try {
|
|
32
|
+
allowed = configured ?? readExtensionCommands();
|
|
33
|
+
} catch {
|
|
34
|
+
throw new Error(
|
|
35
|
+
"Command permissions could not be read; built-in commands remain available. Ask the user to check Shepherdr's Agent commands settings",
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
return Object.entries(builtins)
|
|
39
|
+
.map(([name, description]) => ({ name, description }))
|
|
40
|
+
.concat(
|
|
41
|
+
pi.getCommands().flatMap((entry) => {
|
|
42
|
+
const description = allowed[entry.name];
|
|
43
|
+
return entry.source === "extension" &&
|
|
44
|
+
Object.hasOwn(allowed, entry.name) &&
|
|
45
|
+
description !== undefined &&
|
|
46
|
+
!Object.hasOwn(builtins, entry.name)
|
|
47
|
+
? [{ name: entry.name, description }]
|
|
48
|
+
: [];
|
|
49
|
+
}),
|
|
50
|
+
);
|
|
51
|
+
};
|
|
52
|
+
return {
|
|
53
|
+
available,
|
|
54
|
+
async handle(args: string, ctx: ExtensionCommandContext): Promise<boolean> {
|
|
55
|
+
const intent = pending.get(args);
|
|
56
|
+
if (!intent) return false;
|
|
57
|
+
pending.delete(args);
|
|
58
|
+
if (ctx.sessionManager.getSessionFile() !== intent.session)
|
|
59
|
+
throw new Error("Target session changed; resolve the target again");
|
|
60
|
+
await intent.run(ctx);
|
|
61
|
+
return true;
|
|
62
|
+
},
|
|
63
|
+
prepare(
|
|
64
|
+
text: string,
|
|
65
|
+
ctx: ExtensionContext,
|
|
66
|
+
): { command: boolean; submit: () => void } {
|
|
67
|
+
const space = text.indexOf(" ");
|
|
68
|
+
const name = text.slice(1, space < 0 ? undefined : space);
|
|
69
|
+
const args = space < 0 ? "" : text.slice(space + 1).trim();
|
|
70
|
+
if (Object.hasOwn(builtins, name)) {
|
|
71
|
+
if (name !== "quit" && !ctx.isIdle())
|
|
72
|
+
throw new Error("Target is busy; retry after it settles");
|
|
73
|
+
const run = prepareBuiltin(pi, name, args, ctx);
|
|
74
|
+
return {
|
|
75
|
+
command: true,
|
|
76
|
+
submit: () => {
|
|
77
|
+
const token = randomUUID();
|
|
78
|
+
pending.set(token, {
|
|
79
|
+
session: ctx.sessionManager.getSessionFile(),
|
|
80
|
+
run,
|
|
81
|
+
});
|
|
82
|
+
pi.sendUserMessage(`/herdr ${token}`, {
|
|
83
|
+
expandPromptTemplates: true,
|
|
84
|
+
});
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
const commands = pi.getCommands();
|
|
89
|
+
const entry = commands.find((entry) => entry.name === name);
|
|
90
|
+
let allowed: Record<string, string> = {};
|
|
91
|
+
try {
|
|
92
|
+
allowed = readExtensionCommands();
|
|
93
|
+
} catch (error) {
|
|
94
|
+
ctx.ui.notify(`Extension commands disabled: ${String(error)}`, "error");
|
|
95
|
+
}
|
|
96
|
+
if (
|
|
97
|
+
!entry ||
|
|
98
|
+
(entry.source === "extension" && !Object.hasOwn(allowed, name))
|
|
99
|
+
) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
`Command /${name} is unavailable. Available commands:\n${available(
|
|
102
|
+
allowed,
|
|
103
|
+
)
|
|
104
|
+
.map(({ name, description }) => `/${name}: ${description}`)
|
|
105
|
+
.join("\n")}`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
command: entry.source === "extension",
|
|
110
|
+
submit: () =>
|
|
111
|
+
pi.sendUserMessage(text, {
|
|
112
|
+
expandPromptTemplates: true,
|
|
113
|
+
deliverAs: "steer",
|
|
114
|
+
}),
|
|
115
|
+
};
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function prepareBuiltin(
|
|
121
|
+
pi: ExtensionAPI,
|
|
122
|
+
name: string,
|
|
123
|
+
args: string,
|
|
124
|
+
ctx: ExtensionContext,
|
|
125
|
+
): (ctx: ExtensionCommandContext) => Promise<void> {
|
|
126
|
+
if (["quit", "new", "reload"].includes(name) && args)
|
|
127
|
+
throw new Error(`/${name} takes no arguments`);
|
|
128
|
+
switch (name) {
|
|
129
|
+
case "quit":
|
|
130
|
+
return async (ctx) => ctx.shutdown();
|
|
131
|
+
case "new":
|
|
132
|
+
return async (ctx) => {
|
|
133
|
+
if ((await ctx.newSession()).cancelled)
|
|
134
|
+
throw new Error("New session cancelled");
|
|
135
|
+
};
|
|
136
|
+
case "reload":
|
|
137
|
+
return async (ctx) => {
|
|
138
|
+
await ctx.reload();
|
|
139
|
+
};
|
|
140
|
+
case "resume":
|
|
141
|
+
if (!args) throw new Error("Use /resume session-path");
|
|
142
|
+
return async (ctx) => {
|
|
143
|
+
if ((await ctx.switchSession(args)).cancelled)
|
|
144
|
+
throw new Error("Session switch cancelled");
|
|
145
|
+
};
|
|
146
|
+
case "name":
|
|
147
|
+
if (!args) throw new Error("Use /name name");
|
|
148
|
+
return async () => pi.setSessionName(args);
|
|
149
|
+
case "thinking": {
|
|
150
|
+
if (
|
|
151
|
+
args !== "off" &&
|
|
152
|
+
args !== "minimal" &&
|
|
153
|
+
args !== "low" &&
|
|
154
|
+
args !== "medium" &&
|
|
155
|
+
args !== "high" &&
|
|
156
|
+
args !== "xhigh"
|
|
157
|
+
)
|
|
158
|
+
throw new Error(builtins.thinking);
|
|
159
|
+
return async () => pi.setThinkingLevel(args);
|
|
160
|
+
}
|
|
161
|
+
case "model": {
|
|
162
|
+
const model = ctx.modelRegistry
|
|
163
|
+
.getAvailable()
|
|
164
|
+
.find((model) => `${model.provider}/${model.id}` === args);
|
|
165
|
+
if (!model)
|
|
166
|
+
throw new Error(
|
|
167
|
+
`Use /model provider/model-id. Available: ${ctx.modelRegistry
|
|
168
|
+
.getAvailable()
|
|
169
|
+
.map((model) => `${model.provider}/${model.id}`)
|
|
170
|
+
.join(", ")}`,
|
|
171
|
+
);
|
|
172
|
+
return async () => {
|
|
173
|
+
if (!(await pi.setModel(model)))
|
|
174
|
+
throw new Error("Model unavailable; choose another model");
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
case "compact":
|
|
178
|
+
return async (ctx) =>
|
|
179
|
+
ctx.compact({
|
|
180
|
+
...(args ? { customInstructions: args } : {}),
|
|
181
|
+
onError: (error) =>
|
|
182
|
+
ctx.ui.notify(`Compaction failed: ${error.message}`, "error"),
|
|
183
|
+
});
|
|
184
|
+
default:
|
|
185
|
+
throw new Error(`Unsupported command /${name}`);
|
|
186
|
+
}
|
|
187
|
+
}
|
package/src/peer-inbox.ts
CHANGED
|
@@ -5,16 +5,32 @@ import type {
|
|
|
5
5
|
ExtensionAPI,
|
|
6
6
|
ExtensionContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
|
+
import { acceptFocus, readFocus } from "./agents-focus.js";
|
|
8
9
|
import { sendPolicyMessage, startPreparedIdleTurn } from "./delivery.js";
|
|
10
|
+
import type { AgentFleet } from "./fleet.js";
|
|
9
11
|
import { getCurrentPane } from "./herdr.js";
|
|
10
12
|
import { HerdrClient } from "./herdr-client.js";
|
|
11
13
|
import { announcePeerMessage } from "./messages.js";
|
|
14
|
+
import { acceptFocusChannel } from "./peer-channel.js";
|
|
15
|
+
import type { createPeerCommands } from "./peer-commands.js";
|
|
16
|
+
import {
|
|
17
|
+
MAX_CHANNEL_FRAME_BYTES,
|
|
18
|
+
socketChannel,
|
|
19
|
+
} from "./remote/shepherdr-channel.mjs";
|
|
12
20
|
import {
|
|
13
21
|
MAX_PEER_FRAME_BYTES,
|
|
22
|
+
PEER_PROTOCOL,
|
|
14
23
|
peerInboxPath,
|
|
15
24
|
} from "./remote/shepherdr-peer.mjs";
|
|
16
25
|
|
|
17
|
-
export function registerPeerInbox(
|
|
26
|
+
export function registerPeerInbox(
|
|
27
|
+
pi: ExtensionAPI,
|
|
28
|
+
commands: ReturnType<typeof createPeerCommands>,
|
|
29
|
+
fleet: AgentFleet,
|
|
30
|
+
catchup: (
|
|
31
|
+
ctx: ExtensionContext,
|
|
32
|
+
) => Promise<{ content: string; accepted: () => void }>,
|
|
33
|
+
): void {
|
|
18
34
|
let close: (() => Promise<void>) | undefined;
|
|
19
35
|
let running = false;
|
|
20
36
|
pi.on("agent_start", () => {
|
|
@@ -29,7 +45,7 @@ export function registerPeerInbox(pi: ExtensionAPI): void {
|
|
|
29
45
|
running = false;
|
|
30
46
|
if (process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"])
|
|
31
47
|
return;
|
|
32
|
-
close = await openInbox(pi, ctx, () => running);
|
|
48
|
+
close = await openInbox(pi, ctx, () => running, commands, fleet, catchup);
|
|
33
49
|
});
|
|
34
50
|
pi.on("session_shutdown", async () => {
|
|
35
51
|
await close?.();
|
|
@@ -42,6 +58,11 @@ async function openInbox(
|
|
|
42
58
|
pi: ExtensionAPI,
|
|
43
59
|
ctx: ExtensionContext,
|
|
44
60
|
isRunning: () => boolean,
|
|
61
|
+
commands: ReturnType<typeof createPeerCommands>,
|
|
62
|
+
fleet: AgentFleet,
|
|
63
|
+
catchup: (
|
|
64
|
+
ctx: ExtensionContext,
|
|
65
|
+
) => Promise<{ content: string; accepted: () => void }>,
|
|
45
66
|
): Promise<() => Promise<void>> {
|
|
46
67
|
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
47
68
|
if (!sessionFile)
|
|
@@ -51,7 +72,8 @@ async function openInbox(
|
|
|
51
72
|
const path = peerInboxPath(sessionFile, pane.terminal_id);
|
|
52
73
|
const temporary = `${path}.${token}.tmp`;
|
|
53
74
|
const sockets = new Set<Socket>();
|
|
54
|
-
let
|
|
75
|
+
let state: "open" | "quitting" | "closed" = "open";
|
|
76
|
+
let promptDelivery = Promise.resolve();
|
|
55
77
|
const server = createServer((socket) => {
|
|
56
78
|
sockets.add(socket);
|
|
57
79
|
socket.setEncoding("utf8");
|
|
@@ -60,7 +82,7 @@ async function openInbox(
|
|
|
60
82
|
socket.on("close", () => sockets.delete(socket));
|
|
61
83
|
let buffer = "";
|
|
62
84
|
let received = false;
|
|
63
|
-
socket.on("data", (chunk: string) => {
|
|
85
|
+
socket.on("data", async (chunk: string) => {
|
|
64
86
|
if (received) return;
|
|
65
87
|
buffer += chunk;
|
|
66
88
|
if (Buffer.byteLength(buffer) > MAX_PEER_FRAME_BYTES) {
|
|
@@ -80,24 +102,32 @@ async function openInbox(
|
|
|
80
102
|
socket.destroy();
|
|
81
103
|
return;
|
|
82
104
|
}
|
|
83
|
-
const reply = (value: object) =>
|
|
105
|
+
const reply = (value: object, flushed?: () => void) =>
|
|
84
106
|
socket.end(
|
|
85
|
-
`${JSON.stringify({ protocol:
|
|
107
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ...value })}\n`,
|
|
108
|
+
flushed,
|
|
86
109
|
);
|
|
87
110
|
if (
|
|
88
|
-
|
|
89
|
-
request["protocol"] !==
|
|
111
|
+
state !== "open" ||
|
|
112
|
+
request["protocol"] !== PEER_PROTOCOL ||
|
|
90
113
|
request["token"] !== token ||
|
|
91
114
|
request["sessionFile"] !== sessionFile ||
|
|
92
115
|
request["terminalId"] !== pane.terminal_id ||
|
|
93
116
|
ctx.sessionManager.getSessionFile() !== sessionFile ||
|
|
94
117
|
typeof request["id"] !== "string" ||
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
(request["
|
|
100
|
-
|
|
118
|
+
(request["kind"] !== "focus" &&
|
|
119
|
+
request["kind"] !== "commands" &&
|
|
120
|
+
request["kind"] !== "message" &&
|
|
121
|
+
request["kind"] !== "channel") ||
|
|
122
|
+
(request["kind"] !== "focus" &&
|
|
123
|
+
request["kind"] !== "commands" &&
|
|
124
|
+
request["kind"] !== "channel" &&
|
|
125
|
+
(typeof request["text"] !== "string" ||
|
|
126
|
+
!request["text"].trim() ||
|
|
127
|
+
typeof request["sender"] !== "string" ||
|
|
128
|
+
!/^<herdr_sender [^\n]+ \/>$/.test(request["sender"]) ||
|
|
129
|
+
(request["context"] !== undefined &&
|
|
130
|
+
typeof request["context"] !== "string")))
|
|
101
131
|
) {
|
|
102
132
|
reply({
|
|
103
133
|
ok: false,
|
|
@@ -106,6 +136,104 @@ async function openInbox(
|
|
|
106
136
|
});
|
|
107
137
|
return;
|
|
108
138
|
}
|
|
139
|
+
if (request["kind"] === "commands") {
|
|
140
|
+
try {
|
|
141
|
+
reply({ ok: true, command: false, commands: commands.available() });
|
|
142
|
+
} catch (error) {
|
|
143
|
+
reply({
|
|
144
|
+
ok: false,
|
|
145
|
+
rejected: true,
|
|
146
|
+
error: error instanceof Error ? error.message : String(error),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
if (request["kind"] === "channel") {
|
|
152
|
+
let focus;
|
|
153
|
+
try {
|
|
154
|
+
focus = readFocus(request["focus"]);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
reply({ ok: false, rejected: true, error: String(error) });
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
// Hold outbound frames until the admission reply precedes them.
|
|
160
|
+
const pending: unknown[] = [];
|
|
161
|
+
let admitted = false;
|
|
162
|
+
const channel = socketChannel(socket, buffer.slice(newline + 1));
|
|
163
|
+
const receiverChannel = {
|
|
164
|
+
...channel,
|
|
165
|
+
send(message: unknown) {
|
|
166
|
+
const json = JSON.stringify(message);
|
|
167
|
+
if (
|
|
168
|
+
json === undefined ||
|
|
169
|
+
Buffer.byteLength(json) > MAX_CHANNEL_FRAME_BYTES
|
|
170
|
+
) {
|
|
171
|
+
channel.close();
|
|
172
|
+
throw new Error("Focus channel frame is too large or not JSON");
|
|
173
|
+
}
|
|
174
|
+
if (admitted) channel.send(message);
|
|
175
|
+
else {
|
|
176
|
+
pending.push(message);
|
|
177
|
+
if (
|
|
178
|
+
pending.length > 64 ||
|
|
179
|
+
Buffer.byteLength(JSON.stringify(pending)) > 1024 * 1024
|
|
180
|
+
) {
|
|
181
|
+
channel.close();
|
|
182
|
+
throw new Error(
|
|
183
|
+
"Focus channel admission output exceeded its buffer",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
void acceptFocusChannel(pi, receiverChannel, focus, ctx, pane).then(
|
|
190
|
+
() => {
|
|
191
|
+
try {
|
|
192
|
+
socket.write(
|
|
193
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true })}\n`,
|
|
194
|
+
);
|
|
195
|
+
admitted = true;
|
|
196
|
+
for (const message of pending) channel.send(message);
|
|
197
|
+
pending.length = 0;
|
|
198
|
+
} catch {
|
|
199
|
+
channel.close();
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
(error: unknown) => {
|
|
203
|
+
reply(
|
|
204
|
+
{
|
|
205
|
+
ok: false,
|
|
206
|
+
rejected: true,
|
|
207
|
+
error: error instanceof Error ? error.message : String(error),
|
|
208
|
+
},
|
|
209
|
+
() => channel.close(),
|
|
210
|
+
);
|
|
211
|
+
},
|
|
212
|
+
);
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (request["kind"] === "focus") {
|
|
216
|
+
try {
|
|
217
|
+
if (readFocus(request["focus"]).voice === true)
|
|
218
|
+
throw new Error(
|
|
219
|
+
"Voice transfer was not admitted; retry using agents focus with voice:true",
|
|
220
|
+
);
|
|
221
|
+
} catch (error) {
|
|
222
|
+
reply({ ok: false, rejected: true, error: String(error) });
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
socket.setTimeout(60_000, () => socket.destroy());
|
|
226
|
+
void acceptFocus(pi, ctx, fleet, pane, request["focus"]).then(
|
|
227
|
+
() => reply({ ok: true, command: false }),
|
|
228
|
+
(error: unknown) =>
|
|
229
|
+
reply({
|
|
230
|
+
ok: false,
|
|
231
|
+
rejected: !(error instanceof Error && "focusChanged" in error),
|
|
232
|
+
error: error instanceof Error ? error.message : String(error),
|
|
233
|
+
}),
|
|
234
|
+
);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
109
237
|
let submitted = false;
|
|
110
238
|
try {
|
|
111
239
|
const idle = ctx.isIdle();
|
|
@@ -119,20 +247,13 @@ async function openInbox(
|
|
|
119
247
|
});
|
|
120
248
|
return;
|
|
121
249
|
}
|
|
122
|
-
const text = request["text"];
|
|
250
|
+
const text = request["text"] as string;
|
|
123
251
|
const sender = request["context"]
|
|
124
252
|
? `${request["sender"]}\n${request["context"]}`
|
|
125
|
-
: request["sender"];
|
|
253
|
+
: (request["sender"] as string);
|
|
126
254
|
if (text.startsWith("/")) {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const space = text.indexOf(" ");
|
|
130
|
-
const name = text.slice(1, space < 0 ? undefined : space);
|
|
131
|
-
const command = pi
|
|
132
|
-
.getCommands()
|
|
133
|
-
.some(
|
|
134
|
-
(entry) => entry.source === "extension" && entry.name === name,
|
|
135
|
-
);
|
|
255
|
+
const route = commands.prepare(text, ctx);
|
|
256
|
+
const { command } = route;
|
|
136
257
|
const submit = () => {
|
|
137
258
|
submitted = true;
|
|
138
259
|
sendPolicyMessage(
|
|
@@ -147,41 +268,93 @@ async function openInbox(
|
|
|
147
268
|
deliverAs: idle && !command ? "nextTurn" : "steer",
|
|
148
269
|
},
|
|
149
270
|
);
|
|
150
|
-
|
|
151
|
-
expandPromptTemplates: true,
|
|
152
|
-
deliverAs: "steer",
|
|
153
|
-
});
|
|
271
|
+
route.submit();
|
|
154
272
|
};
|
|
273
|
+
if (command) {
|
|
274
|
+
// Lifecycle commands can tear down this inbox. Flush acceptance first.
|
|
275
|
+
const execute = () => {
|
|
276
|
+
try {
|
|
277
|
+
submit();
|
|
278
|
+
} catch (error) {
|
|
279
|
+
ctx.ui.notify(
|
|
280
|
+
`Command submission failed: ${String(error)}`,
|
|
281
|
+
"error",
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
if (text.trim() === "/quit") {
|
|
286
|
+
// Hold this authenticated connection until process exit, not
|
|
287
|
+
// session_shutdown (which precedes Pi's runtime teardown).
|
|
288
|
+
state = "quitting";
|
|
289
|
+
sockets.delete(socket);
|
|
290
|
+
socket.setTimeout(0);
|
|
291
|
+
socket.write(
|
|
292
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command, closing: true })}\n`,
|
|
293
|
+
execute,
|
|
294
|
+
);
|
|
295
|
+
} else reply({ ok: true, command }, execute);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
155
298
|
if (idle && !command) startPreparedIdleTurn(pi, ctx, submit);
|
|
156
299
|
else submit();
|
|
157
300
|
reply({ ok: true, command });
|
|
158
301
|
return;
|
|
159
302
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
idle
|
|
169
|
-
? { triggerTurn: false, deliverAs: "nextTurn" }
|
|
170
|
-
: { deliverAs: "steer" },
|
|
171
|
-
);
|
|
172
|
-
// Keep arrivals out of history until idle rollover and preparation
|
|
173
|
-
// finish. The shared kickoff coalesces arrivals during preparation.
|
|
174
|
-
if (idle) startPreparedIdleTurn(pi, ctx);
|
|
175
|
-
// Voice failure must not turn accepted delivery into a retry.
|
|
303
|
+
// Catch-up sampling and acceptance form one prompt boundary, including
|
|
304
|
+
// bursts arriving on separate authenticated sockets.
|
|
305
|
+
const previous = promptDelivery;
|
|
306
|
+
let release!: () => void;
|
|
307
|
+
promptDelivery = new Promise<void>((resolve) => {
|
|
308
|
+
release = resolve;
|
|
309
|
+
});
|
|
310
|
+
await previous;
|
|
176
311
|
try {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
312
|
+
const notice = await catchup(ctx);
|
|
313
|
+
if (
|
|
314
|
+
state !== "open" ||
|
|
315
|
+
ctx.sessionManager.getSessionFile() !== sessionFile ||
|
|
316
|
+
(!ctx.isIdle() && !isRunning())
|
|
317
|
+
) {
|
|
318
|
+
reply({
|
|
319
|
+
ok: false,
|
|
320
|
+
rejected: true,
|
|
321
|
+
error: "Target changed context; retry after it settles",
|
|
322
|
+
});
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
const promptIdle = ctx.isIdle();
|
|
326
|
+
submitted = true;
|
|
327
|
+
sendPolicyMessage(
|
|
328
|
+
pi,
|
|
329
|
+
{
|
|
330
|
+
customType: "herdr-agent-message",
|
|
331
|
+
content: `${sender}\n${text}${notice.content}`,
|
|
332
|
+
display: true,
|
|
333
|
+
},
|
|
334
|
+
promptIdle
|
|
335
|
+
? { triggerTurn: false, deliverAs: "nextTurn" }
|
|
336
|
+
: { deliverAs: "steer" },
|
|
182
337
|
);
|
|
338
|
+
notice.accepted();
|
|
339
|
+
// Keep arrivals out of history until idle rollover and preparation
|
|
340
|
+
// finish. The shared kickoff coalesces arrivals during preparation.
|
|
341
|
+
if (promptIdle) startPreparedIdleTurn(pi, ctx);
|
|
342
|
+
// Voice failure must not turn accepted delivery into a retry.
|
|
343
|
+
try {
|
|
344
|
+
announcePeerMessage(pi, {
|
|
345
|
+
sender: request["sender"] as string,
|
|
346
|
+
text,
|
|
347
|
+
});
|
|
348
|
+
} catch (error) {
|
|
349
|
+
ctx.ui.notify(
|
|
350
|
+
`Peer voice update failed: ${String(error)}`,
|
|
351
|
+
"warning",
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
reply({ ok: true, command: false });
|
|
355
|
+
} finally {
|
|
356
|
+
release();
|
|
183
357
|
}
|
|
184
|
-
reply({ ok: true, command: false });
|
|
185
358
|
} catch (error) {
|
|
186
359
|
reply({
|
|
187
360
|
ok: false,
|
|
@@ -193,10 +366,14 @@ async function openInbox(
|
|
|
193
366
|
});
|
|
194
367
|
server.maxConnections = 16;
|
|
195
368
|
const stop = async () => {
|
|
196
|
-
if (
|
|
197
|
-
|
|
369
|
+
if (state === "closed") return;
|
|
370
|
+
const quitting = state === "quitting";
|
|
371
|
+
state = "closed";
|
|
198
372
|
for (const socket of sockets) socket.destroy();
|
|
199
|
-
|
|
373
|
+
const closed = new Promise<void>((resolve) =>
|
|
374
|
+
server.close(() => resolve()),
|
|
375
|
+
);
|
|
376
|
+
if (!quitting) await closed;
|
|
200
377
|
try {
|
|
201
378
|
const current: unknown = JSON.parse(await readFile(path, "utf8"));
|
|
202
379
|
if (
|
|
@@ -232,7 +409,7 @@ async function openInbox(
|
|
|
232
409
|
throw new Error("Shepherdr peer receiver has no address");
|
|
233
410
|
await writeFile(
|
|
234
411
|
temporary,
|
|
235
|
-
JSON.stringify({ protocol:
|
|
412
|
+
JSON.stringify({ protocol: PEER_PROTOCOL, port: address.port, token }),
|
|
236
413
|
{ mode: 0o600, flag: "wx" },
|
|
237
414
|
);
|
|
238
415
|
await rename(temporary, path);
|