@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
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// @howaboua/pi-shepherdr managed bridge
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { createConnection } from "node:net";
|
|
4
|
+
import { PEER_PROTOCOL, resolvePeerReceiver } from "./shepherdr-peer.mjs";
|
|
5
|
+
|
|
6
|
+
export const MAX_CHANNEL_FRAME_BYTES = 512 * 1024;
|
|
7
|
+
const MAX_CHANNEL_BUFFER_BYTES = 1024 * 1024;
|
|
8
|
+
const MAX_QUEUED_FRAMES = 64;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {(message:unknown)=>void} write
|
|
12
|
+
* @param {()=>void} end
|
|
13
|
+
*/
|
|
14
|
+
export function createBufferedChannel(write, end) {
|
|
15
|
+
/** @type {Set<(message:unknown)=>void>} */
|
|
16
|
+
const messages = new Set();
|
|
17
|
+
/** @type {Set<(error:Error)=>void>} */
|
|
18
|
+
const closes = new Set();
|
|
19
|
+
/** @type {Error | undefined} */
|
|
20
|
+
let error;
|
|
21
|
+
/** @type {unknown[]} */
|
|
22
|
+
let queued = [];
|
|
23
|
+
let bytes = 0;
|
|
24
|
+
/** @param {Error} reason */
|
|
25
|
+
const fail = (reason) => {
|
|
26
|
+
if (error) return;
|
|
27
|
+
error = reason;
|
|
28
|
+
queued = [];
|
|
29
|
+
bytes = 0;
|
|
30
|
+
end();
|
|
31
|
+
for (const listener of closes) listener(reason);
|
|
32
|
+
closes.clear();
|
|
33
|
+
messages.clear();
|
|
34
|
+
};
|
|
35
|
+
/** @param {unknown} message */
|
|
36
|
+
const measure = (message) => {
|
|
37
|
+
const json = JSON.stringify(message);
|
|
38
|
+
if (json === undefined || Buffer.byteLength(json) > MAX_CHANNEL_FRAME_BYTES)
|
|
39
|
+
throw new Error("Focus channel frame is too large or not JSON");
|
|
40
|
+
return Buffer.byteLength(json);
|
|
41
|
+
};
|
|
42
|
+
/** @type {import("../types.js").PeerChannel} */
|
|
43
|
+
const channel = {
|
|
44
|
+
send(message) {
|
|
45
|
+
if (error) throw error;
|
|
46
|
+
try {
|
|
47
|
+
measure(message);
|
|
48
|
+
write(message);
|
|
49
|
+
} catch (cause) {
|
|
50
|
+
const reason =
|
|
51
|
+
cause instanceof Error ? cause : new Error(String(cause));
|
|
52
|
+
fail(reason);
|
|
53
|
+
throw reason;
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
onMessage(listener) {
|
|
57
|
+
if (error) return () => {};
|
|
58
|
+
messages.add(listener);
|
|
59
|
+
const pending = queued;
|
|
60
|
+
queued = [];
|
|
61
|
+
bytes = 0;
|
|
62
|
+
try {
|
|
63
|
+
for (const message of pending) listener(message);
|
|
64
|
+
} catch (cause) {
|
|
65
|
+
fail(cause instanceof Error ? cause : new Error(String(cause)));
|
|
66
|
+
}
|
|
67
|
+
return () => messages.delete(listener);
|
|
68
|
+
},
|
|
69
|
+
onClose(listener) {
|
|
70
|
+
if (error) listener(error);
|
|
71
|
+
else closes.add(listener);
|
|
72
|
+
return () => closes.delete(listener);
|
|
73
|
+
},
|
|
74
|
+
close() {
|
|
75
|
+
fail(new Error("Focus channel closed"));
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
return {
|
|
79
|
+
channel,
|
|
80
|
+
fail,
|
|
81
|
+
/** @param {unknown} message */
|
|
82
|
+
receive(message) {
|
|
83
|
+
if (error) return;
|
|
84
|
+
try {
|
|
85
|
+
const size = measure(message);
|
|
86
|
+
if (!messages.size) {
|
|
87
|
+
bytes += size;
|
|
88
|
+
if (
|
|
89
|
+
bytes > MAX_CHANNEL_BUFFER_BYTES ||
|
|
90
|
+
queued.length >= MAX_QUEUED_FRAMES
|
|
91
|
+
)
|
|
92
|
+
throw new Error("Focus channel initial input exceeded its buffer");
|
|
93
|
+
queued.push(message);
|
|
94
|
+
} else for (const listener of messages) listener(message);
|
|
95
|
+
} catch (cause) {
|
|
96
|
+
fail(cause instanceof Error ? cause : new Error(String(cause)));
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @param {import("node:net").Socket} socket @param {string} [initial] */
|
|
103
|
+
export function socketChannel(socket, initial = "") {
|
|
104
|
+
const state = createBufferedChannel(
|
|
105
|
+
(message) => {
|
|
106
|
+
const frame = `${JSON.stringify({ kind: "data", message })}\n`;
|
|
107
|
+
if (
|
|
108
|
+
socket.writableLength + Buffer.byteLength(frame) >
|
|
109
|
+
MAX_CHANNEL_BUFFER_BYTES
|
|
110
|
+
)
|
|
111
|
+
throw new Error("Focus channel output exceeded its buffer");
|
|
112
|
+
socket.write(frame);
|
|
113
|
+
},
|
|
114
|
+
() => socket.destroy(),
|
|
115
|
+
);
|
|
116
|
+
let buffer = "";
|
|
117
|
+
/** @param {string} chunk */
|
|
118
|
+
const consume = (chunk) => {
|
|
119
|
+
buffer += chunk;
|
|
120
|
+
for (;;) {
|
|
121
|
+
const newline = buffer.indexOf("\n");
|
|
122
|
+
if (
|
|
123
|
+
(newline < 0
|
|
124
|
+
? Buffer.byteLength(buffer)
|
|
125
|
+
: Buffer.byteLength(buffer.slice(0, newline))) >
|
|
126
|
+
MAX_CHANNEL_FRAME_BYTES + 128
|
|
127
|
+
)
|
|
128
|
+
return state.fail(new Error("Focus channel frame is too large"));
|
|
129
|
+
if (newline < 0) return;
|
|
130
|
+
const line = buffer.slice(0, newline);
|
|
131
|
+
buffer = buffer.slice(newline + 1);
|
|
132
|
+
try {
|
|
133
|
+
const frame = JSON.parse(line);
|
|
134
|
+
if (frame?.kind !== "data" || !("message" in frame))
|
|
135
|
+
throw new Error("Invalid focus channel frame");
|
|
136
|
+
state.receive(frame.message);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
return state.fail(
|
|
139
|
+
error instanceof Error ? error : new Error(String(error)),
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
socket.setEncoding("utf8");
|
|
145
|
+
socket.setTimeout(0);
|
|
146
|
+
socket.on("data", consume);
|
|
147
|
+
socket.on("error", (error) => state.fail(error));
|
|
148
|
+
socket.on("close", () => state.fail(new Error("Focus channel disconnected")));
|
|
149
|
+
if (initial) consume(initial);
|
|
150
|
+
return state.channel;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {(method:string, params:object)=>Promise<unknown>} request
|
|
155
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
156
|
+
* @param {import("../types.js").FocusRequest} focus
|
|
157
|
+
* @returns {Promise<import("../types.js").PeerChannel>}
|
|
158
|
+
*/
|
|
159
|
+
export async function openPeerChannel(request, expected, focus) {
|
|
160
|
+
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
161
|
+
request,
|
|
162
|
+
expected,
|
|
163
|
+
true,
|
|
164
|
+
);
|
|
165
|
+
const id = randomUUID();
|
|
166
|
+
const frame = `${JSON.stringify({ protocol: PEER_PROTOCOL, kind: "channel", id, token: descriptor.token, sessionFile, terminalId: agent.terminal_id, focus })}\n`;
|
|
167
|
+
if (Buffer.byteLength(frame) > MAX_CHANNEL_FRAME_BYTES)
|
|
168
|
+
throw new Error("Focus channel request is too large");
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
const socket = createConnection({
|
|
171
|
+
host: "127.0.0.1",
|
|
172
|
+
port: descriptor.port,
|
|
173
|
+
});
|
|
174
|
+
let buffer = "";
|
|
175
|
+
const timer = setTimeout(
|
|
176
|
+
() =>
|
|
177
|
+
fail(
|
|
178
|
+
new Error(
|
|
179
|
+
"Focus channel admission timed out; inspect destination before retrying",
|
|
180
|
+
),
|
|
181
|
+
),
|
|
182
|
+
55_000,
|
|
183
|
+
);
|
|
184
|
+
/** @param {unknown} error */
|
|
185
|
+
const fail = (error) => {
|
|
186
|
+
cleanup();
|
|
187
|
+
socket.destroy();
|
|
188
|
+
reject(error);
|
|
189
|
+
};
|
|
190
|
+
const cleanup = () => {
|
|
191
|
+
clearTimeout(timer);
|
|
192
|
+
socket.off("data", receive);
|
|
193
|
+
socket.off("error", fail);
|
|
194
|
+
socket.off("close", closed);
|
|
195
|
+
};
|
|
196
|
+
const closed = () =>
|
|
197
|
+
fail(new Error("Focus channel closed before admission"));
|
|
198
|
+
/** @param {string} chunk */
|
|
199
|
+
const receive = (chunk) => {
|
|
200
|
+
buffer += chunk;
|
|
201
|
+
if (Buffer.byteLength(buffer) > MAX_CHANNEL_FRAME_BYTES)
|
|
202
|
+
return fail(new Error("Focus channel admission frame is too large"));
|
|
203
|
+
const newline = buffer.indexOf("\n");
|
|
204
|
+
if (newline < 0) return;
|
|
205
|
+
try {
|
|
206
|
+
const reply = JSON.parse(buffer.slice(0, newline));
|
|
207
|
+
if (
|
|
208
|
+
reply.protocol !== PEER_PROTOCOL ||
|
|
209
|
+
reply.id !== id ||
|
|
210
|
+
reply.ok !== true
|
|
211
|
+
)
|
|
212
|
+
throw new Error(reply.error || "Focus channel admission rejected");
|
|
213
|
+
cleanup();
|
|
214
|
+
resolve(socketChannel(socket, buffer.slice(newline + 1)));
|
|
215
|
+
} catch (error) {
|
|
216
|
+
fail(error);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
socket.setEncoding("utf8");
|
|
220
|
+
socket.on("error", fail);
|
|
221
|
+
socket.on("close", closed);
|
|
222
|
+
socket.on("data", receive);
|
|
223
|
+
socket.on("connect", () => socket.write(frame));
|
|
224
|
+
});
|
|
225
|
+
}
|
|
@@ -65,9 +65,11 @@ export async function readPersistedNoteEntries(path, identity) {
|
|
|
65
65
|
if (entry.id !== target) continue;
|
|
66
66
|
if (
|
|
67
67
|
(entry.type === "custom" &&
|
|
68
|
-
[
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
[
|
|
69
|
+
"codex-context-note",
|
|
70
|
+
"codex-context-note-snapshot",
|
|
71
|
+
"notes-compaction:note:v1",
|
|
72
|
+
].includes(entry.customType)) ||
|
|
71
73
|
(entry.type === "branch_summary" &&
|
|
72
74
|
entry.details?.codexContextNoteHandoff)
|
|
73
75
|
) {
|
|
@@ -6,6 +6,7 @@ import { createConnection } from "node:net";
|
|
|
6
6
|
import { basename, dirname } from "node:path";
|
|
7
7
|
|
|
8
8
|
export const MAX_PEER_FRAME_BYTES = 8 * 1024 * 1024;
|
|
9
|
+
export const PEER_PROTOCOL = 4;
|
|
9
10
|
|
|
10
11
|
/** @param {string} sessionFile @param {string} terminalId */
|
|
11
12
|
export function peerInboxPath(sessionFile, terminalId) {
|
|
@@ -84,9 +85,54 @@ export async function readReceiver(path) {
|
|
|
84
85
|
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
85
86
|
* @param {import("../types.js").PaneInfo} expected
|
|
86
87
|
* @param {import("../types.js").PeerMessage} message
|
|
88
|
+
* @param {import("../types.js").FocusRequest} [focus]
|
|
89
|
+
* @param {boolean} [commands]
|
|
87
90
|
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
88
91
|
*/
|
|
89
|
-
export async function sendPeerMessage(
|
|
92
|
+
export async function sendPeerMessage(
|
|
93
|
+
request,
|
|
94
|
+
expected,
|
|
95
|
+
message,
|
|
96
|
+
focus,
|
|
97
|
+
commands = false,
|
|
98
|
+
) {
|
|
99
|
+
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
100
|
+
request,
|
|
101
|
+
expected,
|
|
102
|
+
Boolean(focus) || commands,
|
|
103
|
+
);
|
|
104
|
+
const id = randomUUID();
|
|
105
|
+
const frame =
|
|
106
|
+
JSON.stringify({
|
|
107
|
+
protocol: PEER_PROTOCOL,
|
|
108
|
+
kind: commands ? "commands" : focus ? "focus" : "message",
|
|
109
|
+
focus,
|
|
110
|
+
id,
|
|
111
|
+
token: descriptor.token,
|
|
112
|
+
sessionFile,
|
|
113
|
+
terminalId: agent.terminal_id,
|
|
114
|
+
text: message.text,
|
|
115
|
+
sender: message.sender,
|
|
116
|
+
context: message.context,
|
|
117
|
+
}) + "\n";
|
|
118
|
+
return deliverPeerFrame(
|
|
119
|
+
request,
|
|
120
|
+
expected,
|
|
121
|
+
message,
|
|
122
|
+
focus,
|
|
123
|
+
descriptor,
|
|
124
|
+
sessionFile,
|
|
125
|
+
id,
|
|
126
|
+
frame,
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
132
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
133
|
+
* @param {boolean} allowBlocked
|
|
134
|
+
*/
|
|
135
|
+
export async function resolvePeerReceiver(request, expected, allowBlocked) {
|
|
90
136
|
const result = await request("agent.get", { target: expected.pane_id });
|
|
91
137
|
const agent =
|
|
92
138
|
result &&
|
|
@@ -121,7 +167,11 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
121
167
|
true,
|
|
122
168
|
);
|
|
123
169
|
}
|
|
124
|
-
if (
|
|
170
|
+
if (
|
|
171
|
+
!allowBlocked &&
|
|
172
|
+
"agent_status" in agent &&
|
|
173
|
+
agent.agent_status === "blocked"
|
|
174
|
+
) {
|
|
125
175
|
throw deliveryError(
|
|
126
176
|
"Target is blocked; answer its pending question first",
|
|
127
177
|
true,
|
|
@@ -141,7 +191,7 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
141
191
|
);
|
|
142
192
|
}
|
|
143
193
|
if (
|
|
144
|
-
descriptor?.protocol !==
|
|
194
|
+
descriptor?.protocol !== PEER_PROTOCOL ||
|
|
145
195
|
!Number.isInteger(descriptor.port) ||
|
|
146
196
|
descriptor.port < 1 ||
|
|
147
197
|
descriptor.port > 65535 ||
|
|
@@ -153,18 +203,30 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
153
203
|
true,
|
|
154
204
|
);
|
|
155
205
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
206
|
+
return { descriptor, sessionFile, agent };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
211
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
212
|
+
* @param {import("../types.js").PeerMessage} message
|
|
213
|
+
* @param {import("../types.js").FocusRequest | undefined} focus
|
|
214
|
+
* @param {{port:number,token:string}} descriptor
|
|
215
|
+
* @param {string} sessionFile
|
|
216
|
+
* @param {string} id
|
|
217
|
+
* @param {string} frame
|
|
218
|
+
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
219
|
+
*/
|
|
220
|
+
function deliverPeerFrame(
|
|
221
|
+
request,
|
|
222
|
+
expected,
|
|
223
|
+
message,
|
|
224
|
+
focus,
|
|
225
|
+
descriptor,
|
|
226
|
+
sessionFile,
|
|
227
|
+
id,
|
|
228
|
+
frame,
|
|
229
|
+
) {
|
|
168
230
|
if (Buffer.byteLength(frame) > MAX_PEER_FRAME_BYTES) {
|
|
169
231
|
throw deliveryError("Peer message is too large", true);
|
|
170
232
|
}
|
|
@@ -172,6 +234,7 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
172
234
|
let attempted = false;
|
|
173
235
|
let settled = false;
|
|
174
236
|
let buffer = "";
|
|
237
|
+
let closing = false;
|
|
175
238
|
const socket = createConnection({
|
|
176
239
|
host: "127.0.0.1",
|
|
177
240
|
port: descriptor.port,
|
|
@@ -194,12 +257,28 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
194
257
|
!attempted,
|
|
195
258
|
),
|
|
196
259
|
);
|
|
197
|
-
const timer = setTimeout(failed, 10_000);
|
|
260
|
+
const timer = setTimeout(failed, focus ? 55_000 : 10_000);
|
|
198
261
|
timer.unref();
|
|
199
262
|
socket.setEncoding("utf8");
|
|
200
263
|
socket.on("error", failed);
|
|
201
|
-
socket.on("end",
|
|
202
|
-
|
|
264
|
+
socket.on("end", () => {
|
|
265
|
+
if (settled) return;
|
|
266
|
+
if (!closing) return failed();
|
|
267
|
+
clearTimeout(timer);
|
|
268
|
+
void closeExitedPane(request, expected, sessionFile).then(
|
|
269
|
+
() => finish(undefined, { command: true }),
|
|
270
|
+
(error) =>
|
|
271
|
+
finish(
|
|
272
|
+
deliveryError(
|
|
273
|
+
`Pi shut down, but its pane could not be closed: ${error instanceof Error ? error.message : String(error)}`,
|
|
274
|
+
false,
|
|
275
|
+
),
|
|
276
|
+
),
|
|
277
|
+
);
|
|
278
|
+
});
|
|
279
|
+
socket.on("close", () => {
|
|
280
|
+
if (!closing) failed();
|
|
281
|
+
});
|
|
203
282
|
socket.on("connect", () => {
|
|
204
283
|
attempted = true;
|
|
205
284
|
socket.write(frame);
|
|
@@ -211,10 +290,26 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
211
290
|
if (newline < 0) return;
|
|
212
291
|
try {
|
|
213
292
|
const reply = JSON.parse(buffer.slice(0, newline));
|
|
214
|
-
if (reply.protocol !==
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
293
|
+
if (reply.protocol !== PEER_PROTOCOL || reply.id !== id)
|
|
294
|
+
return failed();
|
|
295
|
+
if (reply.ok === true && typeof reply.command === "boolean") {
|
|
296
|
+
if (
|
|
297
|
+
reply.closing === true &&
|
|
298
|
+
reply.command &&
|
|
299
|
+
message.text.trim() === "/quit"
|
|
300
|
+
) {
|
|
301
|
+
closing = true;
|
|
302
|
+
// The receiver retains this socket through session_shutdown.
|
|
303
|
+
// Only process exit ends it; never close a pane on a timeout.
|
|
304
|
+
clearTimeout(timer);
|
|
305
|
+
} else
|
|
306
|
+
finish(undefined, {
|
|
307
|
+
command: reply.command,
|
|
308
|
+
...(reply.commands === undefined
|
|
309
|
+
? {}
|
|
310
|
+
: { commands: reply.commands }),
|
|
311
|
+
});
|
|
312
|
+
} else if (reply.ok === false && typeof reply.error === "string")
|
|
218
313
|
finish(deliveryError(reply.error, reply.rejected === true));
|
|
219
314
|
else failed();
|
|
220
315
|
} catch {
|
|
@@ -223,3 +318,84 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
223
318
|
});
|
|
224
319
|
});
|
|
225
320
|
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
324
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
325
|
+
* @param {import("../types.js").FocusRequest} focus
|
|
326
|
+
*/
|
|
327
|
+
export function sendPeerFocus(request, expected, focus) {
|
|
328
|
+
return sendPeerMessage(
|
|
329
|
+
request,
|
|
330
|
+
expected,
|
|
331
|
+
{ text: "focus", sender: "" },
|
|
332
|
+
focus,
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/** @param {unknown} value @returns {import("../types.js").PeerCommand[]} */
|
|
337
|
+
export function readPeerCommands(value) {
|
|
338
|
+
if (
|
|
339
|
+
!Array.isArray(value) ||
|
|
340
|
+
!value.every(
|
|
341
|
+
(entry) =>
|
|
342
|
+
entry &&
|
|
343
|
+
typeof entry === "object" &&
|
|
344
|
+
typeof entry.name === "string" &&
|
|
345
|
+
typeof entry.description === "string",
|
|
346
|
+
)
|
|
347
|
+
)
|
|
348
|
+
throw new Error(
|
|
349
|
+
"Command discovery returned an invalid list; ask the user to update and reload Shepherdr in the target session",
|
|
350
|
+
);
|
|
351
|
+
return value.map(({ name, description }) => ({ name, description }));
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
356
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
357
|
+
* @returns {Promise<import("../types.js").PeerCommand[]>}
|
|
358
|
+
*/
|
|
359
|
+
export async function sendPeerCommands(request, expected) {
|
|
360
|
+
const receipt = await sendPeerMessage(
|
|
361
|
+
request,
|
|
362
|
+
expected,
|
|
363
|
+
{ text: "", sender: "" },
|
|
364
|
+
undefined,
|
|
365
|
+
true,
|
|
366
|
+
);
|
|
367
|
+
return readPeerCommands(receipt.commands);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
372
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
373
|
+
* @param {string} sessionFile
|
|
374
|
+
*/
|
|
375
|
+
async function closeExitedPane(request, expected, sessionFile) {
|
|
376
|
+
const result = await request("pane.current", {
|
|
377
|
+
caller_pane_id: expected.pane_id,
|
|
378
|
+
});
|
|
379
|
+
const pane =
|
|
380
|
+
result && typeof result === "object" && "pane" in result
|
|
381
|
+
? result.pane
|
|
382
|
+
: undefined;
|
|
383
|
+
if (
|
|
384
|
+
!pane ||
|
|
385
|
+
typeof pane !== "object" ||
|
|
386
|
+
!("pane_id" in pane) ||
|
|
387
|
+
typeof pane.pane_id !== "string" ||
|
|
388
|
+
!("terminal_id" in pane) ||
|
|
389
|
+
pane.terminal_id !== expected.terminal_id
|
|
390
|
+
)
|
|
391
|
+
throw new Error("Target pane changed; resolve it again");
|
|
392
|
+
if (
|
|
393
|
+
"agent_session" in pane &&
|
|
394
|
+
pane.agent_session &&
|
|
395
|
+
typeof pane.agent_session === "object" &&
|
|
396
|
+
"value" in pane.agent_session &&
|
|
397
|
+
pane.agent_session.value !== sessionFile
|
|
398
|
+
)
|
|
399
|
+
throw new Error("Target session changed; resolve it again");
|
|
400
|
+
await request("pane.close", { pane_id: pane.pane_id });
|
|
401
|
+
}
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -1,23 +1,29 @@
|
|
|
1
1
|
// @howaboua/pi-shepherdr managed bridge
|
|
2
2
|
import { readFile, stat } from "node:fs/promises";
|
|
3
3
|
import { createConnection } from "node:net";
|
|
4
|
-
import { homedir } from "node:os";
|
|
4
|
+
import { homedir, hostname } from "node:os";
|
|
5
5
|
import { join, resolve } from "node:path";
|
|
6
|
+
import { openPeerChannel } from "./shepherdr-channel.mjs";
|
|
6
7
|
import {
|
|
7
8
|
listenContext,
|
|
8
9
|
relayContextPath,
|
|
9
10
|
requestContext,
|
|
10
11
|
} from "./shepherdr-context.mjs";
|
|
11
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
sendPeerCommands,
|
|
14
|
+
sendPeerFocus,
|
|
15
|
+
sendPeerMessage,
|
|
16
|
+
} from "./shepherdr-peer.mjs";
|
|
12
17
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
13
18
|
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
14
19
|
|
|
15
|
-
const BRIDGE_VERSION =
|
|
20
|
+
const BRIDGE_VERSION = 19;
|
|
16
21
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
17
22
|
const subscriptions = new Map();
|
|
18
23
|
const contextRelays = new Map();
|
|
19
24
|
const contextRequests = new Map();
|
|
20
25
|
const contextCalls = new Map();
|
|
26
|
+
const channels = new Map();
|
|
21
27
|
|
|
22
28
|
function requestController(request, signal) {
|
|
23
29
|
signal.throwIfAborted();
|
|
@@ -232,12 +238,11 @@ function subscribe(id, requested) {
|
|
|
232
238
|
},
|
|
233
239
|
(value) => {
|
|
234
240
|
if (ended) return;
|
|
241
|
+
if (value.id === requestId && value.error !== undefined) {
|
|
242
|
+
disconnected(errorFromResponse(value.error));
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
235
245
|
if (!acknowledged && value.id === requestId) {
|
|
236
|
-
if (value.error !== undefined) {
|
|
237
|
-
disconnected(errorFromResponse(value.error));
|
|
238
|
-
socket.destroy();
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
246
|
if (value.result?.type !== "subscription_started") {
|
|
242
247
|
disconnected(
|
|
243
248
|
new Error(
|
|
@@ -322,6 +327,38 @@ async function handle(message) {
|
|
|
322
327
|
if (message.op === "message") {
|
|
323
328
|
return sendPeerMessage(request, message.agent, message.message);
|
|
324
329
|
}
|
|
330
|
+
if (message.op === "identity") {
|
|
331
|
+
return { host: hostname(), session: argument("session") || "default" };
|
|
332
|
+
}
|
|
333
|
+
if (message.op === "focus") {
|
|
334
|
+
return sendPeerFocus(request, message.agent, message.request);
|
|
335
|
+
}
|
|
336
|
+
if (message.op === "commands") {
|
|
337
|
+
return sendPeerCommands(request, message.agent);
|
|
338
|
+
}
|
|
339
|
+
if (message.op === "channel_open") {
|
|
340
|
+
const channel = await openPeerChannel(
|
|
341
|
+
request,
|
|
342
|
+
message.agent,
|
|
343
|
+
message.request,
|
|
344
|
+
);
|
|
345
|
+
channels.set(message.id, channel);
|
|
346
|
+
channel.onClose((error) => {
|
|
347
|
+
channels.delete(message.id);
|
|
348
|
+
send({
|
|
349
|
+
type: "channel",
|
|
350
|
+
channel: message.id,
|
|
351
|
+
closed: true,
|
|
352
|
+
error: error.message,
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
channel.onMessage((frame) => {
|
|
356
|
+
if (process.stdout.writableLength > 1024 * 1024)
|
|
357
|
+
throw new Error("Focus channel bridge output exceeded its buffer");
|
|
358
|
+
send({ type: "channel", channel: message.id, message: frame });
|
|
359
|
+
});
|
|
360
|
+
return { admitted: true };
|
|
361
|
+
}
|
|
325
362
|
if (message.op === "context") {
|
|
326
363
|
const controller = new AbortController();
|
|
327
364
|
contextCalls.set(message.id, controller);
|
|
@@ -401,6 +438,23 @@ process.stdin.on("data", (chunk) => {
|
|
|
401
438
|
contextCalls.get(message.id)?.abort();
|
|
402
439
|
continue;
|
|
403
440
|
}
|
|
441
|
+
if (message?.op === "channel_send" || message?.op === "channel_close") {
|
|
442
|
+
const channel = channels.get(message.id);
|
|
443
|
+
try {
|
|
444
|
+
if (message.op === "channel_close") channel?.close();
|
|
445
|
+
else if (channel) channel.send(message.message);
|
|
446
|
+
else
|
|
447
|
+
send({
|
|
448
|
+
type: "channel",
|
|
449
|
+
channel: message.id,
|
|
450
|
+
closed: true,
|
|
451
|
+
error: "Focus channel is unavailable",
|
|
452
|
+
});
|
|
453
|
+
} catch (error) {
|
|
454
|
+
channel?.close();
|
|
455
|
+
}
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
404
458
|
if (message?.op === "context_reply") {
|
|
405
459
|
const pending = contextRequests.get(message.id);
|
|
406
460
|
if (pending) {
|
|
@@ -425,6 +479,7 @@ process.stdin.on("data", (chunk) => {
|
|
|
425
479
|
});
|
|
426
480
|
|
|
427
481
|
async function shutdown() {
|
|
482
|
+
for (const channel of channels.values()) channel.close();
|
|
428
483
|
for (const close of subscriptions.values()) close();
|
|
429
484
|
for (const controller of contextCalls.values()) controller.abort();
|
|
430
485
|
await Promise.allSettled([...contextRelays.values()].map((close) => close()));
|