@howaboua/pi-shepherdr 0.2.23 → 0.2.25
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 +20 -18
- package/README.md +80 -69
- package/biome.json +1 -1
- package/index.ts +4 -2
- package/package.json +14 -9
- package/src/agents-contract.ts +9 -6
- package/src/agents-discovery.ts +78 -37
- package/src/agents-focus.ts +17 -11
- package/src/agents-reply.ts +1 -1
- package/src/agents-spawn.ts +24 -8
- package/src/agents-tool.ts +26 -8
- package/src/agents-work.ts +5 -5
- package/src/attachment-notes.ts +14 -2
- package/src/attachment-protocol.ts +21 -4
- package/src/attachment-routing.ts +1 -1
- package/src/attachment-target.ts +5 -2
- package/src/attachment.ts +4 -3
- package/src/board/host.ts +5 -3
- package/src/board/membership.ts +1 -1
- package/src/close-idle.ts +89 -0
- package/src/{control-panel-actions.ts → control-panel/actions.ts} +2 -2
- package/src/{control-panel.ts → control-panel/index.ts} +7 -5
- package/src/{control-panel-items.ts → control-panel/items.ts} +17 -5
- package/src/{control-panel-view.ts → control-panel/view.ts} +9 -5
- package/src/controller.ts +17 -5
- package/src/core/AGENTS.md +5 -0
- package/src/core/backend.ts +266 -0
- package/src/core/client.ts +249 -0
- package/src/core/context-relay.mjs +111 -0
- package/src/core/focus-channel-helper.mjs +165 -0
- package/src/core/focus-channel-stream.ts +140 -0
- package/src/core/focus-channel-wire.mjs +160 -0
- package/src/core/focus-channel.ts +213 -0
- package/src/core/focus-window.ts +192 -0
- package/src/core/pi-executable.mjs +34 -0
- package/src/core/protocol.ts +116 -0
- package/src/core/registration.ts +51 -0
- package/src/core/remote-endpoint.ts +119 -0
- package/src/core/remote-exec.ts +122 -0
- package/src/core/remote-helper.mjs +244 -0
- package/src/core/remote-reader.ts +67 -0
- package/src/core/remote-relay.ts +191 -0
- package/src/core/request.ts +124 -0
- package/src/core/spawn.ts +177 -0
- package/src/core/status.ts +68 -0
- package/src/core/terminal.ts +103 -0
- package/src/fleet/catalog.ts +83 -0
- package/src/fleet/connection.ts +111 -0
- package/src/fleet/index.ts +413 -0
- package/src/fleet/inspection.ts +159 -0
- package/src/fleet/machine.ts +55 -0
- package/src/fleet/monitor.ts +85 -0
- package/src/fleet/persistence.ts +65 -0
- package/src/fleet/reconnect.ts +75 -0
- package/src/fleet/setup.ts +133 -0
- package/src/herdr.ts +25 -5
- package/src/launch-contract.ts +48 -0
- package/src/launch-location.ts +151 -0
- package/src/launch.ts +30 -183
- package/src/messages/attachments.ts +39 -0
- package/src/messages/attribution.ts +84 -0
- package/src/messages/event-details.ts +69 -0
- package/src/messages/events.ts +195 -0
- package/src/messages/focus.ts +47 -0
- package/src/messages/guidance.ts +83 -0
- package/src/messages/render.ts +89 -0
- package/src/messages/voice-announcements.ts +79 -0
- package/src/monitor-record.ts +9 -6
- package/src/monitor.ts +33 -2
- package/src/peer-commands.ts +11 -0
- package/src/peer-inbox-delivery.ts +171 -0
- package/src/peer-inbox-focus.ts +113 -0
- package/src/peer-inbox.ts +127 -209
- package/src/remote/client.ts +127 -0
- package/src/remote/context-relay.ts +84 -0
- package/src/remote/deployment.ts +179 -0
- package/src/remote/focus-channels.ts +89 -0
- package/src/remote/shepherdr-context.mjs +1 -1
- package/src/remote/shepherdr-peer.mjs +18 -5
- package/src/remote/shepherdr-session.mjs +9 -2
- package/src/remote/shepherdr.mjs +1 -1
- package/src/remote/transport.ts +369 -0
- package/src/settlement/collector.ts +122 -0
- package/src/settlement/delivery.ts +144 -0
- package/src/{settlement.ts → settlement/index.ts} +35 -233
- package/src/shared-context-host.ts +40 -14
- package/src/shared-context.ts +44 -6
- package/src/spawn-context.ts +24 -17
- package/src/spawn-ownership.ts +91 -0
- package/src/types.ts +9 -5
- package/src/voice-handoff.ts +1 -1
- package/src/worker-ownership.ts +92 -0
- package/src/worker-service.ts +14 -83
- package/src/fleet.ts +0 -702
- package/src/messages.ts +0 -637
- package/src/remote-client.ts +0 -761
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { Socket } from "node:net";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { sendPolicyMessage } from "./delivery.js";
|
|
7
|
+
import { announcePeerMessage } from "./messages/voice-announcements.js";
|
|
8
|
+
import type { createPeerCommands } from "./peer-commands.js";
|
|
9
|
+
import { PEER_PROTOCOL } from "./remote/shepherdr-peer.mjs";
|
|
10
|
+
|
|
11
|
+
/** Serialises prompt catch-up/acceptance, but never serialises lifecycle commands. */
|
|
12
|
+
export class PeerInboxDelivery {
|
|
13
|
+
private promptDelivery = Promise.resolve();
|
|
14
|
+
pendingPrompts = 0;
|
|
15
|
+
|
|
16
|
+
private readonly sessionFile: string;
|
|
17
|
+
private readonly isRunning: () => boolean;
|
|
18
|
+
private readonly isOpen: () => boolean;
|
|
19
|
+
private readonly quitting: (socket: Socket) => void;
|
|
20
|
+
private readonly commands: ReturnType<typeof createPeerCommands>;
|
|
21
|
+
private readonly catchup: (
|
|
22
|
+
ctx: ExtensionContext,
|
|
23
|
+
) => Promise<{ content: string; accepted: () => void }>;
|
|
24
|
+
constructor(
|
|
25
|
+
sessionFile: string,
|
|
26
|
+
isRunning: () => boolean,
|
|
27
|
+
isOpen: () => boolean,
|
|
28
|
+
quitting: (socket: Socket) => void,
|
|
29
|
+
commands: ReturnType<typeof createPeerCommands>,
|
|
30
|
+
catchup: (
|
|
31
|
+
ctx: ExtensionContext,
|
|
32
|
+
) => Promise<{ content: string; accepted: () => void }>,
|
|
33
|
+
) {
|
|
34
|
+
this.sessionFile = sessionFile;
|
|
35
|
+
this.isRunning = isRunning;
|
|
36
|
+
this.isOpen = isOpen;
|
|
37
|
+
this.quitting = quitting;
|
|
38
|
+
this.commands = commands;
|
|
39
|
+
this.catchup = catchup;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async receive(
|
|
43
|
+
pi: ExtensionAPI,
|
|
44
|
+
ctx: ExtensionContext,
|
|
45
|
+
socket: Socket,
|
|
46
|
+
request: Record<string, unknown>,
|
|
47
|
+
reply: (value: object, flushed?: () => void) => void,
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
let submitted = false;
|
|
50
|
+
try {
|
|
51
|
+
const idle = ctx.isIdle();
|
|
52
|
+
// Manual compaction/tree navigation can be busy without an agent
|
|
53
|
+
// loop to consume steering. Do not acknowledge a stranded message.
|
|
54
|
+
if (!idle && !this.isRunning()) {
|
|
55
|
+
reply({
|
|
56
|
+
ok: false,
|
|
57
|
+
rejected: true,
|
|
58
|
+
error: "Target is changing context; retry after it settles",
|
|
59
|
+
});
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const text = request["text"] as string;
|
|
63
|
+
const sender = request["context"]
|
|
64
|
+
? `${request["sender"]}\n${request["context"]}`
|
|
65
|
+
: (request["sender"] as string);
|
|
66
|
+
if (text.startsWith("/")) {
|
|
67
|
+
const route = this.commands.prepare(text, ctx);
|
|
68
|
+
const { command } = route;
|
|
69
|
+
const submit = () => {
|
|
70
|
+
submitted = true;
|
|
71
|
+
sendPolicyMessage(
|
|
72
|
+
pi,
|
|
73
|
+
{
|
|
74
|
+
customType: "herdr-agent-source",
|
|
75
|
+
content: sender,
|
|
76
|
+
display: true,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
triggerTurn: false,
|
|
80
|
+
deliverAs: idle && !command ? "nextTurn" : "steer",
|
|
81
|
+
},
|
|
82
|
+
);
|
|
83
|
+
route.submit();
|
|
84
|
+
};
|
|
85
|
+
if (command) {
|
|
86
|
+
// Lifecycle commands can tear down this inbox. Flush acceptance first.
|
|
87
|
+
const execute = () => {
|
|
88
|
+
try {
|
|
89
|
+
submit();
|
|
90
|
+
} catch (error) {
|
|
91
|
+
ctx.ui.notify(
|
|
92
|
+
`Command submission failed: ${String(error)}`,
|
|
93
|
+
"error",
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
if (text.trim() === "/quit") {
|
|
98
|
+
// Hold this authenticated connection until process exit, not
|
|
99
|
+
// session_shutdown (which precedes Pi's runtime teardown).
|
|
100
|
+
this.quitting(socket);
|
|
101
|
+
socket.write(
|
|
102
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command, closing: true })}\n`,
|
|
103
|
+
execute,
|
|
104
|
+
);
|
|
105
|
+
} else reply({ ok: true, command }, execute);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
submit();
|
|
109
|
+
reply({ ok: true, command });
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
// Catch-up sampling and acceptance form one prompt boundary, including
|
|
113
|
+
// bursts arriving on separate authenticated sockets.
|
|
114
|
+
const previous = this.promptDelivery;
|
|
115
|
+
this.pendingPrompts += 1;
|
|
116
|
+
let release!: () => void;
|
|
117
|
+
this.promptDelivery = new Promise<void>((resolve) => {
|
|
118
|
+
release = resolve;
|
|
119
|
+
});
|
|
120
|
+
await previous;
|
|
121
|
+
try {
|
|
122
|
+
const notice = await this.catchup(ctx);
|
|
123
|
+
if (
|
|
124
|
+
!this.isOpen() ||
|
|
125
|
+
ctx.sessionManager.getSessionFile() !== this.sessionFile ||
|
|
126
|
+
(!ctx.isIdle() && !this.isRunning())
|
|
127
|
+
) {
|
|
128
|
+
reply({
|
|
129
|
+
ok: false,
|
|
130
|
+
rejected: true,
|
|
131
|
+
error: "Target changed context; retry after it settles",
|
|
132
|
+
});
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
submitted = true;
|
|
136
|
+
sendPolicyMessage(
|
|
137
|
+
pi,
|
|
138
|
+
{
|
|
139
|
+
customType: "herdr-agent-message",
|
|
140
|
+
content: `${sender}\n${text}${notice.content}`,
|
|
141
|
+
display: true,
|
|
142
|
+
},
|
|
143
|
+
{ triggerTurn: true, deliverAs: "steer" },
|
|
144
|
+
);
|
|
145
|
+
notice.accepted();
|
|
146
|
+
// Voice failure must not turn accepted delivery into a retry.
|
|
147
|
+
try {
|
|
148
|
+
announcePeerMessage(pi, {
|
|
149
|
+
sender: request["sender"] as string,
|
|
150
|
+
text,
|
|
151
|
+
});
|
|
152
|
+
} catch (error) {
|
|
153
|
+
ctx.ui.notify(
|
|
154
|
+
`Peer voice update failed: ${String(error)}`,
|
|
155
|
+
"warning",
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
reply({ ok: true, command: false });
|
|
159
|
+
} finally {
|
|
160
|
+
this.pendingPrompts -= 1;
|
|
161
|
+
release();
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
reply({
|
|
165
|
+
ok: false,
|
|
166
|
+
rejected: !submitted,
|
|
167
|
+
error: `${submitted ? "Peer delivery failed; inspect the target before retrying" : "Peer delivery rejected"}: ${error instanceof Error ? error.message : String(error)}`,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import type { Socket } from "node:net";
|
|
2
|
+
import type {
|
|
3
|
+
ExtensionAPI,
|
|
4
|
+
ExtensionContext,
|
|
5
|
+
} from "@earendil-works/pi-coding-agent";
|
|
6
|
+
import { acceptFocus, readFocus } from "./agents-focus.js";
|
|
7
|
+
import type { AgentFleet } from "./fleet/index.js";
|
|
8
|
+
import { acceptFocusChannel } from "./peer-channel.js";
|
|
9
|
+
import {
|
|
10
|
+
MAX_CHANNEL_FRAME_BYTES,
|
|
11
|
+
socketChannel,
|
|
12
|
+
} from "./remote/shepherdr-channel.mjs";
|
|
13
|
+
import { PEER_PROTOCOL } from "./remote/shepherdr-peer.mjs";
|
|
14
|
+
import type { PaneInfo } from "./types.js";
|
|
15
|
+
|
|
16
|
+
/** Focus admission owns its duplex buffering; the inbox retains authenticated socket lifetime. */
|
|
17
|
+
export function receivePeerFocus(
|
|
18
|
+
pi: ExtensionAPI,
|
|
19
|
+
ctx: ExtensionContext,
|
|
20
|
+
fleet: AgentFleet,
|
|
21
|
+
pane: PaneInfo,
|
|
22
|
+
socket: Socket,
|
|
23
|
+
request: Record<string, unknown>,
|
|
24
|
+
trailing: string,
|
|
25
|
+
reply: (value: object, flushed?: () => void) => void,
|
|
26
|
+
): void {
|
|
27
|
+
if (request["kind"] === "channel" && pane) {
|
|
28
|
+
let focus;
|
|
29
|
+
try {
|
|
30
|
+
focus = readFocus(request["focus"]);
|
|
31
|
+
} catch (error) {
|
|
32
|
+
reply({ ok: false, rejected: true, error: String(error) });
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Hold outbound frames until the admission reply precedes them.
|
|
36
|
+
const pending: unknown[] = [];
|
|
37
|
+
let admitted = false;
|
|
38
|
+
const channel = socketChannel(socket, trailing);
|
|
39
|
+
const receiverChannel = {
|
|
40
|
+
...channel,
|
|
41
|
+
send(message: unknown) {
|
|
42
|
+
const json = JSON.stringify(message);
|
|
43
|
+
if (
|
|
44
|
+
json === undefined ||
|
|
45
|
+
Buffer.byteLength(json) > MAX_CHANNEL_FRAME_BYTES
|
|
46
|
+
) {
|
|
47
|
+
channel.close();
|
|
48
|
+
throw new Error("Focus channel frame is too large or not JSON");
|
|
49
|
+
}
|
|
50
|
+
if (admitted) channel.send(message);
|
|
51
|
+
else {
|
|
52
|
+
pending.push(message);
|
|
53
|
+
if (
|
|
54
|
+
pending.length > 64 ||
|
|
55
|
+
Buffer.byteLength(JSON.stringify(pending)) > 1024 * 1024
|
|
56
|
+
) {
|
|
57
|
+
channel.close();
|
|
58
|
+
throw new Error(
|
|
59
|
+
"Focus channel admission output exceeded its buffer",
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
void acceptFocusChannel(pi, receiverChannel, focus, ctx, pane).then(
|
|
66
|
+
() => {
|
|
67
|
+
try {
|
|
68
|
+
socket.write(
|
|
69
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true })}\n`,
|
|
70
|
+
);
|
|
71
|
+
admitted = true;
|
|
72
|
+
for (const message of pending) channel.send(message);
|
|
73
|
+
pending.length = 0;
|
|
74
|
+
} catch {
|
|
75
|
+
channel.close();
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
(error: unknown) => {
|
|
79
|
+
reply(
|
|
80
|
+
{
|
|
81
|
+
ok: false,
|
|
82
|
+
rejected: true,
|
|
83
|
+
error: error instanceof Error ? error.message : String(error),
|
|
84
|
+
},
|
|
85
|
+
() => channel.close(),
|
|
86
|
+
);
|
|
87
|
+
},
|
|
88
|
+
);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
if (request["kind"] === "focus" && pane) {
|
|
92
|
+
try {
|
|
93
|
+
if (readFocus(request["focus"]).voice === true)
|
|
94
|
+
throw new Error(
|
|
95
|
+
"Voice transfer was not admitted; retry using agents focus with voice:true",
|
|
96
|
+
);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
reply({ ok: false, rejected: true, error: String(error) });
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
socket.setTimeout(60_000, () => socket.destroy());
|
|
102
|
+
void acceptFocus(pi, ctx, fleet, pane, request["focus"]).then(
|
|
103
|
+
() => reply({ ok: true, command: false }),
|
|
104
|
+
(error: unknown) =>
|
|
105
|
+
reply({
|
|
106
|
+
ok: false,
|
|
107
|
+
rejected: !(error instanceof Error && "focusChanged" in error),
|
|
108
|
+
error: error instanceof Error ? error.message : String(error),
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
}
|
package/src/peer-inbox.ts
CHANGED
|
@@ -5,19 +5,14 @@ import type {
|
|
|
5
5
|
ExtensionAPI,
|
|
6
6
|
ExtensionContext,
|
|
7
7
|
} from "@earendil-works/pi-coding-agent";
|
|
8
|
-
import {
|
|
9
|
-
import { sendPolicyMessage } from "./delivery.js";
|
|
8
|
+
import { coreIdentity, publishCoreSession } from "./core/registration.js";
|
|
10
9
|
import { dispatchExtensionReply } from "./extension-reply.js";
|
|
11
|
-
import type { AgentFleet } from "./fleet.js";
|
|
12
|
-
import { getCurrentPane } from "./herdr.js";
|
|
10
|
+
import type { AgentFleet } from "./fleet/index.js";
|
|
11
|
+
import { getAgent, getCurrentPane, sessionPath } from "./herdr.js";
|
|
13
12
|
import { HerdrClient } from "./herdr-client.js";
|
|
14
|
-
import { announcePeerMessage } from "./messages.js";
|
|
15
|
-
import { acceptFocusChannel } from "./peer-channel.js";
|
|
16
13
|
import type { createPeerCommands } from "./peer-commands.js";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
socketChannel,
|
|
20
|
-
} from "./remote/shepherdr-channel.mjs";
|
|
14
|
+
import { PeerInboxDelivery } from "./peer-inbox-delivery.js";
|
|
15
|
+
import { receivePeerFocus } from "./peer-inbox-focus.js";
|
|
21
16
|
import {
|
|
22
17
|
MAX_PEER_FRAME_BYTES,
|
|
23
18
|
PEER_PROTOCOL,
|
|
@@ -34,12 +29,30 @@ export function registerPeerInbox(
|
|
|
34
29
|
): void {
|
|
35
30
|
let close: (() => Promise<void>) | undefined;
|
|
36
31
|
let running = false;
|
|
32
|
+
let generation = 0;
|
|
37
33
|
const publish = async (ctx: ExtensionContext) => {
|
|
38
|
-
|
|
34
|
+
const core = coreIdentity(ctx);
|
|
35
|
+
if (
|
|
36
|
+
!core &&
|
|
37
|
+
(process.env["HERDR_ENV"] !== "1" || !process.env["HERDR_SOCKET_PATH"])
|
|
38
|
+
)
|
|
39
39
|
return;
|
|
40
|
-
|
|
40
|
+
if (core) {
|
|
41
|
+
await close?.();
|
|
42
|
+
close = undefined;
|
|
43
|
+
}
|
|
44
|
+
close = await openInbox(
|
|
45
|
+
pi,
|
|
46
|
+
ctx,
|
|
47
|
+
() => running,
|
|
48
|
+
() => generation,
|
|
49
|
+
commands,
|
|
50
|
+
fleet,
|
|
51
|
+
catchup,
|
|
52
|
+
);
|
|
41
53
|
};
|
|
42
54
|
pi.on("before_agent_start", () => {
|
|
55
|
+
generation += 1;
|
|
43
56
|
running = true;
|
|
44
57
|
});
|
|
45
58
|
pi.on("agent_settled", () => {
|
|
@@ -62,6 +75,7 @@ async function openInbox(
|
|
|
62
75
|
pi: ExtensionAPI,
|
|
63
76
|
ctx: ExtensionContext,
|
|
64
77
|
isRunning: () => boolean,
|
|
78
|
+
generation: () => number,
|
|
65
79
|
commands: ReturnType<typeof createPeerCommands>,
|
|
66
80
|
fleet: AgentFleet,
|
|
67
81
|
catchup: (
|
|
@@ -71,13 +85,42 @@ async function openInbox(
|
|
|
71
85
|
const sessionFile = ctx.sessionManager.getSessionFile();
|
|
72
86
|
if (!sessionFile)
|
|
73
87
|
throw new Error("Shepherdr peer delivery requires a saved Pi session");
|
|
74
|
-
const
|
|
88
|
+
const core = coreIdentity(ctx);
|
|
89
|
+
const pane = core
|
|
90
|
+
? {
|
|
91
|
+
backend: "core" as const,
|
|
92
|
+
pane_id: core.terminalId,
|
|
93
|
+
terminal_id: core.terminalId,
|
|
94
|
+
agent_status: "unknown" as const,
|
|
95
|
+
agent_session: {
|
|
96
|
+
agent: "pi",
|
|
97
|
+
kind: "path" as const,
|
|
98
|
+
source: "pi.session_file",
|
|
99
|
+
value: sessionFile,
|
|
100
|
+
},
|
|
101
|
+
}
|
|
102
|
+
: await getCurrentPane(new HerdrClient());
|
|
103
|
+
const terminalId = pane.terminal_id;
|
|
104
|
+
if (!terminalId)
|
|
105
|
+
throw new Error("Shepherdr peer receiver has no terminal identity");
|
|
75
106
|
const token = randomBytes(32).toString("hex");
|
|
76
|
-
const path = peerInboxPath(sessionFile,
|
|
107
|
+
const path = peerInboxPath(sessionFile, terminalId);
|
|
77
108
|
const temporary = `${path}.${token}.tmp`;
|
|
78
109
|
const sockets = new Set<Socket>();
|
|
79
110
|
let state: "open" | "quitting" | "closed" = "open";
|
|
80
|
-
|
|
111
|
+
const delivery = new PeerInboxDelivery(
|
|
112
|
+
sessionFile,
|
|
113
|
+
isRunning,
|
|
114
|
+
() => state === "open",
|
|
115
|
+
(socket) => {
|
|
116
|
+
state = "quitting";
|
|
117
|
+
sockets.delete(socket);
|
|
118
|
+
socket.setTimeout(0);
|
|
119
|
+
},
|
|
120
|
+
commands,
|
|
121
|
+
catchup,
|
|
122
|
+
);
|
|
123
|
+
let clearCoreSession: (() => Promise<void>) | undefined;
|
|
81
124
|
const server = createServer((socket) => {
|
|
82
125
|
sockets.add(socket);
|
|
83
126
|
socket.setEncoding("utf8");
|
|
@@ -116,12 +159,13 @@ async function openInbox(
|
|
|
116
159
|
request["protocol"] !== PEER_PROTOCOL ||
|
|
117
160
|
request["token"] !== token ||
|
|
118
161
|
request["sessionFile"] !== sessionFile ||
|
|
119
|
-
request["terminalId"] !==
|
|
162
|
+
request["terminalId"] !== terminalId ||
|
|
120
163
|
ctx.sessionManager.getSessionFile() !== sessionFile ||
|
|
121
164
|
typeof request["id"] !== "string" ||
|
|
122
165
|
(request["kind"] !== "focus" &&
|
|
123
166
|
request["kind"] !== "commands" &&
|
|
124
167
|
request["kind"] !== "message" &&
|
|
168
|
+
request["kind"] !== "close-idle" &&
|
|
125
169
|
request["kind"] !== "channel" &&
|
|
126
170
|
request["kind"] !== "extension-reply") ||
|
|
127
171
|
(request["kind"] !== "focus" &&
|
|
@@ -190,213 +234,83 @@ async function openInbox(
|
|
|
190
234
|
}
|
|
191
235
|
return;
|
|
192
236
|
}
|
|
193
|
-
if (request["kind"] === "
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const
|
|
205
|
-
const receiverChannel = {
|
|
206
|
-
...channel,
|
|
207
|
-
send(message: unknown) {
|
|
208
|
-
const json = JSON.stringify(message);
|
|
209
|
-
if (
|
|
210
|
-
json === undefined ||
|
|
211
|
-
Buffer.byteLength(json) > MAX_CHANNEL_FRAME_BYTES
|
|
212
|
-
) {
|
|
213
|
-
channel.close();
|
|
214
|
-
throw new Error("Focus channel frame is too large or not JSON");
|
|
215
|
-
}
|
|
216
|
-
if (admitted) channel.send(message);
|
|
217
|
-
else {
|
|
218
|
-
pending.push(message);
|
|
219
|
-
if (
|
|
220
|
-
pending.length > 64 ||
|
|
221
|
-
Buffer.byteLength(JSON.stringify(pending)) > 1024 * 1024
|
|
222
|
-
) {
|
|
223
|
-
channel.close();
|
|
224
|
-
throw new Error(
|
|
225
|
-
"Focus channel admission output exceeded its buffer",
|
|
226
|
-
);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
void acceptFocusChannel(pi, receiverChannel, focus, ctx, pane).then(
|
|
232
|
-
() => {
|
|
233
|
-
try {
|
|
234
|
-
socket.write(
|
|
235
|
-
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true })}\n`,
|
|
236
|
-
);
|
|
237
|
-
admitted = true;
|
|
238
|
-
for (const message of pending) channel.send(message);
|
|
239
|
-
pending.length = 0;
|
|
240
|
-
} catch {
|
|
241
|
-
channel.close();
|
|
242
|
-
}
|
|
243
|
-
},
|
|
244
|
-
(error: unknown) => {
|
|
245
|
-
reply(
|
|
246
|
-
{
|
|
247
|
-
ok: false,
|
|
248
|
-
rejected: true,
|
|
249
|
-
error: error instanceof Error ? error.message : String(error),
|
|
250
|
-
},
|
|
251
|
-
() => channel.close(),
|
|
252
|
-
);
|
|
253
|
-
},
|
|
254
|
-
);
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
if (request["kind"] === "focus") {
|
|
258
|
-
try {
|
|
259
|
-
if (readFocus(request["focus"]).voice === true)
|
|
260
|
-
throw new Error(
|
|
261
|
-
"Voice transfer was not admitted; retry using agents focus with voice:true",
|
|
262
|
-
);
|
|
263
|
-
} catch (error) {
|
|
264
|
-
reply({ ok: false, rejected: true, error: String(error) });
|
|
265
|
-
return;
|
|
266
|
-
}
|
|
267
|
-
socket.setTimeout(60_000, () => socket.destroy());
|
|
268
|
-
void acceptFocus(pi, ctx, fleet, pane, request["focus"]).then(
|
|
269
|
-
() => reply({ ok: true, command: false }),
|
|
270
|
-
(error: unknown) =>
|
|
271
|
-
reply({
|
|
272
|
-
ok: false,
|
|
273
|
-
rejected: !(error instanceof Error && "focusChanged" in error),
|
|
274
|
-
error: error instanceof Error ? error.message : String(error),
|
|
275
|
-
}),
|
|
276
|
-
);
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
let submitted = false;
|
|
280
|
-
try {
|
|
281
|
-
const idle = ctx.isIdle();
|
|
282
|
-
// Manual compaction/tree navigation can be busy without an agent
|
|
283
|
-
// loop to consume steering. Do not acknowledge a stranded message.
|
|
284
|
-
if (!idle && !isRunning()) {
|
|
237
|
+
if (request["kind"] === "close-idle" && pane) {
|
|
238
|
+
const startGeneration = generation();
|
|
239
|
+
const idle = (live: ExtensionContext) =>
|
|
240
|
+
state === "open" &&
|
|
241
|
+
!socket.destroyed &&
|
|
242
|
+
live.sessionManager.getSessionFile() === sessionFile &&
|
|
243
|
+
live.isIdle() &&
|
|
244
|
+
!live.hasPendingMessages() &&
|
|
245
|
+
!isRunning() &&
|
|
246
|
+
delivery.pendingPrompts === 0 &&
|
|
247
|
+
generation() === startGeneration;
|
|
248
|
+
const reject = () =>
|
|
285
249
|
reply({
|
|
286
250
|
ok: false,
|
|
287
251
|
rejected: true,
|
|
288
|
-
error: "Target is
|
|
252
|
+
error: "Target is busy or changed context; idle close skipped",
|
|
289
253
|
});
|
|
254
|
+
if (!idle(ctx)) {
|
|
255
|
+
reject();
|
|
290
256
|
return;
|
|
291
257
|
}
|
|
292
|
-
const text = request["text"] as string;
|
|
293
|
-
const sender = request["context"]
|
|
294
|
-
? `${request["sender"]}\n${request["context"]}`
|
|
295
|
-
: (request["sender"] as string);
|
|
296
|
-
if (text.startsWith("/")) {
|
|
297
|
-
const route = commands.prepare(text, ctx);
|
|
298
|
-
const { command } = route;
|
|
299
|
-
const submit = () => {
|
|
300
|
-
submitted = true;
|
|
301
|
-
sendPolicyMessage(
|
|
302
|
-
pi,
|
|
303
|
-
{
|
|
304
|
-
customType: "herdr-agent-source",
|
|
305
|
-
content: sender,
|
|
306
|
-
display: true,
|
|
307
|
-
},
|
|
308
|
-
{
|
|
309
|
-
triggerTurn: false,
|
|
310
|
-
deliverAs: idle && !command ? "nextTurn" : "steer",
|
|
311
|
-
},
|
|
312
|
-
);
|
|
313
|
-
route.submit();
|
|
314
|
-
};
|
|
315
|
-
if (command) {
|
|
316
|
-
// Lifecycle commands can tear down this inbox. Flush acceptance first.
|
|
317
|
-
const execute = () => {
|
|
318
|
-
try {
|
|
319
|
-
submit();
|
|
320
|
-
} catch (error) {
|
|
321
|
-
ctx.ui.notify(
|
|
322
|
-
`Command submission failed: ${String(error)}`,
|
|
323
|
-
"error",
|
|
324
|
-
);
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
if (text.trim() === "/quit") {
|
|
328
|
-
// Hold this authenticated connection until process exit, not
|
|
329
|
-
// session_shutdown (which precedes Pi's runtime teardown).
|
|
330
|
-
state = "quitting";
|
|
331
|
-
sockets.delete(socket);
|
|
332
|
-
socket.setTimeout(0);
|
|
333
|
-
socket.write(
|
|
334
|
-
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command, closing: true })}\n`,
|
|
335
|
-
execute,
|
|
336
|
-
);
|
|
337
|
-
} else reply({ ok: true, command }, execute);
|
|
338
|
-
return;
|
|
339
|
-
}
|
|
340
|
-
submit();
|
|
341
|
-
reply({ ok: true, command });
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
// Catch-up sampling and acceptance form one prompt boundary, including
|
|
345
|
-
// bursts arriving on separate authenticated sockets.
|
|
346
|
-
const previous = promptDelivery;
|
|
347
|
-
let release!: () => void;
|
|
348
|
-
promptDelivery = new Promise<void>((resolve) => {
|
|
349
|
-
release = resolve;
|
|
350
|
-
});
|
|
351
|
-
await previous;
|
|
352
258
|
try {
|
|
353
|
-
const
|
|
259
|
+
const livePane = await getAgent(
|
|
260
|
+
core ? fleet.connected().client : new HerdrClient(),
|
|
261
|
+
pane.pane_id,
|
|
262
|
+
);
|
|
354
263
|
if (
|
|
355
|
-
|
|
356
|
-
ctx
|
|
357
|
-
|
|
264
|
+
request["text"] !== "/quit" ||
|
|
265
|
+
!idle(ctx) ||
|
|
266
|
+
livePane.pane_id !== pane.pane_id ||
|
|
267
|
+
livePane.terminal_id !== pane.terminal_id ||
|
|
268
|
+
sessionPath(livePane) !== sessionFile ||
|
|
269
|
+
livePane.interactive_ready !== true ||
|
|
270
|
+
livePane.launch_pending === true ||
|
|
271
|
+
(livePane.agent_status !== "idle" &&
|
|
272
|
+
livePane.agent_status !== "done")
|
|
358
273
|
) {
|
|
359
|
-
|
|
360
|
-
ok: false,
|
|
361
|
-
rejected: true,
|
|
362
|
-
error: "Target changed context; retry after it settles",
|
|
363
|
-
});
|
|
274
|
+
reject();
|
|
364
275
|
return;
|
|
365
276
|
}
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
try {
|
|
379
|
-
announcePeerMessage(pi, {
|
|
380
|
-
sender: request["sender"] as string,
|
|
381
|
-
text,
|
|
382
|
-
});
|
|
383
|
-
} catch (error) {
|
|
384
|
-
ctx.ui.notify(
|
|
385
|
-
`Peer voice update failed: ${String(error)}`,
|
|
386
|
-
"warning",
|
|
277
|
+
commands.closeIdle(ctx, async (live) => {
|
|
278
|
+
if (!idle(live)) {
|
|
279
|
+
reject();
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
// No async boundary between the live idle check and shutdown.
|
|
283
|
+
// Retain the socket until process exit, as for ordinary peer quit.
|
|
284
|
+
state = "quitting";
|
|
285
|
+
sockets.delete(socket);
|
|
286
|
+
socket.setTimeout(0);
|
|
287
|
+
socket.write(
|
|
288
|
+
`${JSON.stringify({ protocol: PEER_PROTOCOL, id: request["id"], ok: true, command: true, closing: true })}\n`,
|
|
387
289
|
);
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
}
|
|
391
|
-
|
|
290
|
+
live.shutdown();
|
|
291
|
+
});
|
|
292
|
+
} catch {
|
|
293
|
+
reject();
|
|
392
294
|
}
|
|
393
|
-
|
|
394
|
-
reply({
|
|
395
|
-
ok: false,
|
|
396
|
-
rejected: !submitted,
|
|
397
|
-
error: `${submitted ? "Peer delivery failed; inspect the target before retrying" : "Peer delivery rejected"}: ${error instanceof Error ? error.message : String(error)}`,
|
|
398
|
-
});
|
|
295
|
+
return;
|
|
399
296
|
}
|
|
297
|
+
if (
|
|
298
|
+
(request["kind"] === "channel" || request["kind"] === "focus") &&
|
|
299
|
+
pane
|
|
300
|
+
) {
|
|
301
|
+
receivePeerFocus(
|
|
302
|
+
pi,
|
|
303
|
+
ctx,
|
|
304
|
+
fleet,
|
|
305
|
+
pane,
|
|
306
|
+
socket,
|
|
307
|
+
request,
|
|
308
|
+
buffer.slice(newline + 1),
|
|
309
|
+
reply,
|
|
310
|
+
);
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
await delivery.receive(pi, ctx, socket, request, reply);
|
|
400
314
|
});
|
|
401
315
|
});
|
|
402
316
|
server.maxConnections = 16;
|
|
@@ -420,6 +334,9 @@ async function openInbox(
|
|
|
420
334
|
await unlink(path);
|
|
421
335
|
} catch (error) {
|
|
422
336
|
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error;
|
|
337
|
+
} finally {
|
|
338
|
+
await clearCoreSession?.();
|
|
339
|
+
clearCoreSession = undefined;
|
|
423
340
|
}
|
|
424
341
|
};
|
|
425
342
|
try {
|
|
@@ -448,6 +365,7 @@ async function openInbox(
|
|
|
448
365
|
{ mode: 0o600, flag: "wx" },
|
|
449
366
|
);
|
|
450
367
|
await rename(temporary, path);
|
|
368
|
+
if (core) clearCoreSession = await publishCoreSession(core, sessionFile);
|
|
451
369
|
} catch (error) {
|
|
452
370
|
try {
|
|
453
371
|
await unlink(temporary).catch((failure: NodeJS.ErrnoException) => {
|