@howaboua/pi-shepherdr 0.2.15 → 0.2.16
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 +21 -0
- package/README.md +40 -33
- package/index.ts +16 -26
- package/package.json +11 -11
- package/src/agents-contract.ts +11 -1
- package/src/agents-discovery.ts +21 -7
- package/src/agents-focus.ts +242 -0
- package/src/agents-spawn.ts +23 -0
- package/src/agents-tool.ts +34 -5
- 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 +39 -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 +173 -0
- package/src/peer-inbox.ts +218 -55
- package/src/remote/shepherdr-channel.mjs +225 -0
- package/src/remote/shepherdr-checkpoints.mjs +5 -3
- package/src/remote/shepherdr-peer.mjs +151 -22
- package/src/remote/shepherdr.mjs +56 -8
- package/src/remote-client.ts +115 -11
- package/src/settlement.ts +1 -0
- package/src/shared-context.ts +104 -1
- package/src/types.ts +23 -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 = 3;
|
|
9
10
|
|
|
10
11
|
/** @param {string} sessionFile @param {string} terminalId */
|
|
11
12
|
export function peerInboxPath(sessionFile, terminalId) {
|
|
@@ -84,9 +85,47 @@ 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]
|
|
87
89
|
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
88
90
|
*/
|
|
89
|
-
export async function sendPeerMessage(request, expected, message) {
|
|
91
|
+
export async function sendPeerMessage(request, expected, message, focus) {
|
|
92
|
+
const { descriptor, sessionFile, agent } = await resolvePeerReceiver(
|
|
93
|
+
request,
|
|
94
|
+
expected,
|
|
95
|
+
Boolean(focus),
|
|
96
|
+
);
|
|
97
|
+
const id = randomUUID();
|
|
98
|
+
const frame =
|
|
99
|
+
JSON.stringify({
|
|
100
|
+
protocol: PEER_PROTOCOL,
|
|
101
|
+
kind: focus ? "focus" : "message",
|
|
102
|
+
focus,
|
|
103
|
+
id,
|
|
104
|
+
token: descriptor.token,
|
|
105
|
+
sessionFile,
|
|
106
|
+
terminalId: agent.terminal_id,
|
|
107
|
+
text: message.text,
|
|
108
|
+
sender: message.sender,
|
|
109
|
+
context: message.context,
|
|
110
|
+
}) + "\n";
|
|
111
|
+
return deliverPeerFrame(
|
|
112
|
+
request,
|
|
113
|
+
expected,
|
|
114
|
+
message,
|
|
115
|
+
focus,
|
|
116
|
+
descriptor,
|
|
117
|
+
sessionFile,
|
|
118
|
+
id,
|
|
119
|
+
frame,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
125
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
126
|
+
* @param {boolean} allowBlocked
|
|
127
|
+
*/
|
|
128
|
+
export async function resolvePeerReceiver(request, expected, allowBlocked) {
|
|
90
129
|
const result = await request("agent.get", { target: expected.pane_id });
|
|
91
130
|
const agent =
|
|
92
131
|
result &&
|
|
@@ -121,7 +160,11 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
121
160
|
true,
|
|
122
161
|
);
|
|
123
162
|
}
|
|
124
|
-
if (
|
|
163
|
+
if (
|
|
164
|
+
!allowBlocked &&
|
|
165
|
+
"agent_status" in agent &&
|
|
166
|
+
agent.agent_status === "blocked"
|
|
167
|
+
) {
|
|
125
168
|
throw deliveryError(
|
|
126
169
|
"Target is blocked; answer its pending question first",
|
|
127
170
|
true,
|
|
@@ -141,7 +184,7 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
141
184
|
);
|
|
142
185
|
}
|
|
143
186
|
if (
|
|
144
|
-
descriptor?.protocol !==
|
|
187
|
+
descriptor?.protocol !== PEER_PROTOCOL ||
|
|
145
188
|
!Number.isInteger(descriptor.port) ||
|
|
146
189
|
descriptor.port < 1 ||
|
|
147
190
|
descriptor.port > 65535 ||
|
|
@@ -153,18 +196,30 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
153
196
|
true,
|
|
154
197
|
);
|
|
155
198
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
199
|
+
return { descriptor, sessionFile, agent };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
204
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
205
|
+
* @param {import("../types.js").PeerMessage} message
|
|
206
|
+
* @param {import("../types.js").FocusRequest | undefined} focus
|
|
207
|
+
* @param {{port:number,token:string}} descriptor
|
|
208
|
+
* @param {string} sessionFile
|
|
209
|
+
* @param {string} id
|
|
210
|
+
* @param {string} frame
|
|
211
|
+
* @returns {Promise<import("../types.js").PeerDelivery>}
|
|
212
|
+
*/
|
|
213
|
+
function deliverPeerFrame(
|
|
214
|
+
request,
|
|
215
|
+
expected,
|
|
216
|
+
message,
|
|
217
|
+
focus,
|
|
218
|
+
descriptor,
|
|
219
|
+
sessionFile,
|
|
220
|
+
id,
|
|
221
|
+
frame,
|
|
222
|
+
) {
|
|
168
223
|
if (Buffer.byteLength(frame) > MAX_PEER_FRAME_BYTES) {
|
|
169
224
|
throw deliveryError("Peer message is too large", true);
|
|
170
225
|
}
|
|
@@ -172,6 +227,7 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
172
227
|
let attempted = false;
|
|
173
228
|
let settled = false;
|
|
174
229
|
let buffer = "";
|
|
230
|
+
let closing = false;
|
|
175
231
|
const socket = createConnection({
|
|
176
232
|
host: "127.0.0.1",
|
|
177
233
|
port: descriptor.port,
|
|
@@ -194,12 +250,28 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
194
250
|
!attempted,
|
|
195
251
|
),
|
|
196
252
|
);
|
|
197
|
-
const timer = setTimeout(failed, 10_000);
|
|
253
|
+
const timer = setTimeout(failed, focus ? 55_000 : 10_000);
|
|
198
254
|
timer.unref();
|
|
199
255
|
socket.setEncoding("utf8");
|
|
200
256
|
socket.on("error", failed);
|
|
201
|
-
socket.on("end",
|
|
202
|
-
|
|
257
|
+
socket.on("end", () => {
|
|
258
|
+
if (settled) return;
|
|
259
|
+
if (!closing) return failed();
|
|
260
|
+
clearTimeout(timer);
|
|
261
|
+
void closeExitedPane(request, expected, sessionFile).then(
|
|
262
|
+
() => finish(undefined, { command: true }),
|
|
263
|
+
(error) =>
|
|
264
|
+
finish(
|
|
265
|
+
deliveryError(
|
|
266
|
+
`Pi shut down, but its pane could not be closed: ${error instanceof Error ? error.message : String(error)}`,
|
|
267
|
+
false,
|
|
268
|
+
),
|
|
269
|
+
),
|
|
270
|
+
);
|
|
271
|
+
});
|
|
272
|
+
socket.on("close", () => {
|
|
273
|
+
if (!closing) failed();
|
|
274
|
+
});
|
|
203
275
|
socket.on("connect", () => {
|
|
204
276
|
attempted = true;
|
|
205
277
|
socket.write(frame);
|
|
@@ -211,10 +283,20 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
211
283
|
if (newline < 0) return;
|
|
212
284
|
try {
|
|
213
285
|
const reply = JSON.parse(buffer.slice(0, newline));
|
|
214
|
-
if (reply.protocol !==
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
286
|
+
if (reply.protocol !== PEER_PROTOCOL || reply.id !== id)
|
|
287
|
+
return failed();
|
|
288
|
+
if (reply.ok === true && typeof reply.command === "boolean") {
|
|
289
|
+
if (
|
|
290
|
+
reply.closing === true &&
|
|
291
|
+
reply.command &&
|
|
292
|
+
message.text.trim() === "/quit"
|
|
293
|
+
) {
|
|
294
|
+
closing = true;
|
|
295
|
+
// The receiver retains this socket through session_shutdown.
|
|
296
|
+
// Only process exit ends it; never close a pane on a timeout.
|
|
297
|
+
clearTimeout(timer);
|
|
298
|
+
} else finish(undefined, { command: reply.command });
|
|
299
|
+
} else if (reply.ok === false && typeof reply.error === "string")
|
|
218
300
|
finish(deliveryError(reply.error, reply.rejected === true));
|
|
219
301
|
else failed();
|
|
220
302
|
} catch {
|
|
@@ -223,3 +305,50 @@ export async function sendPeerMessage(request, expected, message) {
|
|
|
223
305
|
});
|
|
224
306
|
});
|
|
225
307
|
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
311
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
312
|
+
* @param {import("../types.js").FocusRequest} focus
|
|
313
|
+
*/
|
|
314
|
+
export function sendPeerFocus(request, expected, focus) {
|
|
315
|
+
return sendPeerMessage(
|
|
316
|
+
request,
|
|
317
|
+
expected,
|
|
318
|
+
{ text: "focus", sender: "" },
|
|
319
|
+
focus,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* @param {(method: string, params: object) => Promise<unknown>} request
|
|
325
|
+
* @param {import("../types.js").PaneInfo} expected
|
|
326
|
+
* @param {string} sessionFile
|
|
327
|
+
*/
|
|
328
|
+
async function closeExitedPane(request, expected, sessionFile) {
|
|
329
|
+
const result = await request("pane.current", {
|
|
330
|
+
caller_pane_id: expected.pane_id,
|
|
331
|
+
});
|
|
332
|
+
const pane =
|
|
333
|
+
result && typeof result === "object" && "pane" in result
|
|
334
|
+
? result.pane
|
|
335
|
+
: undefined;
|
|
336
|
+
if (
|
|
337
|
+
!pane ||
|
|
338
|
+
typeof pane !== "object" ||
|
|
339
|
+
!("pane_id" in pane) ||
|
|
340
|
+
typeof pane.pane_id !== "string" ||
|
|
341
|
+
!("terminal_id" in pane) ||
|
|
342
|
+
pane.terminal_id !== expected.terminal_id
|
|
343
|
+
)
|
|
344
|
+
throw new Error("Target pane changed; resolve it again");
|
|
345
|
+
if (
|
|
346
|
+
"agent_session" in pane &&
|
|
347
|
+
pane.agent_session &&
|
|
348
|
+
typeof pane.agent_session === "object" &&
|
|
349
|
+
"value" in pane.agent_session &&
|
|
350
|
+
pane.agent_session.value !== sessionFile
|
|
351
|
+
)
|
|
352
|
+
throw new Error("Target session changed; resolve it again");
|
|
353
|
+
await request("pane.close", { pane_id: pane.pane_id });
|
|
354
|
+
}
|
package/src/remote/shepherdr.mjs
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
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 { sendPeerMessage } from "./shepherdr-peer.mjs";
|
|
12
|
+
import { sendPeerFocus, sendPeerMessage } from "./shepherdr-peer.mjs";
|
|
12
13
|
import { readSessionView } from "./shepherdr-session.mjs";
|
|
13
14
|
import { watchSessions } from "./shepherdr-session-watch.mjs";
|
|
14
15
|
|
|
15
|
-
const BRIDGE_VERSION =
|
|
16
|
+
const BRIDGE_VERSION = 18;
|
|
16
17
|
const MAX_FRAME_BYTES = 8 * 1024 * 1024;
|
|
17
18
|
const subscriptions = new Map();
|
|
18
19
|
const contextRelays = new Map();
|
|
19
20
|
const contextRequests = new Map();
|
|
20
21
|
const contextCalls = new Map();
|
|
22
|
+
const channels = new Map();
|
|
21
23
|
|
|
22
24
|
function requestController(request, signal) {
|
|
23
25
|
signal.throwIfAborted();
|
|
@@ -232,12 +234,11 @@ function subscribe(id, requested) {
|
|
|
232
234
|
},
|
|
233
235
|
(value) => {
|
|
234
236
|
if (ended) return;
|
|
237
|
+
if (value.id === requestId && value.error !== undefined) {
|
|
238
|
+
disconnected(errorFromResponse(value.error));
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
235
241
|
if (!acknowledged && value.id === requestId) {
|
|
236
|
-
if (value.error !== undefined) {
|
|
237
|
-
disconnected(errorFromResponse(value.error));
|
|
238
|
-
socket.destroy();
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
242
|
if (value.result?.type !== "subscription_started") {
|
|
242
243
|
disconnected(
|
|
243
244
|
new Error(
|
|
@@ -322,6 +323,35 @@ async function handle(message) {
|
|
|
322
323
|
if (message.op === "message") {
|
|
323
324
|
return sendPeerMessage(request, message.agent, message.message);
|
|
324
325
|
}
|
|
326
|
+
if (message.op === "identity") {
|
|
327
|
+
return { host: hostname(), session: argument("session") || "default" };
|
|
328
|
+
}
|
|
329
|
+
if (message.op === "focus") {
|
|
330
|
+
return sendPeerFocus(request, message.agent, message.request);
|
|
331
|
+
}
|
|
332
|
+
if (message.op === "channel_open") {
|
|
333
|
+
const channel = await openPeerChannel(
|
|
334
|
+
request,
|
|
335
|
+
message.agent,
|
|
336
|
+
message.request,
|
|
337
|
+
);
|
|
338
|
+
channels.set(message.id, channel);
|
|
339
|
+
channel.onClose((error) => {
|
|
340
|
+
channels.delete(message.id);
|
|
341
|
+
send({
|
|
342
|
+
type: "channel",
|
|
343
|
+
channel: message.id,
|
|
344
|
+
closed: true,
|
|
345
|
+
error: error.message,
|
|
346
|
+
});
|
|
347
|
+
});
|
|
348
|
+
channel.onMessage((frame) => {
|
|
349
|
+
if (process.stdout.writableLength > 1024 * 1024)
|
|
350
|
+
throw new Error("Focus channel bridge output exceeded its buffer");
|
|
351
|
+
send({ type: "channel", channel: message.id, message: frame });
|
|
352
|
+
});
|
|
353
|
+
return { admitted: true };
|
|
354
|
+
}
|
|
325
355
|
if (message.op === "context") {
|
|
326
356
|
const controller = new AbortController();
|
|
327
357
|
contextCalls.set(message.id, controller);
|
|
@@ -401,6 +431,23 @@ process.stdin.on("data", (chunk) => {
|
|
|
401
431
|
contextCalls.get(message.id)?.abort();
|
|
402
432
|
continue;
|
|
403
433
|
}
|
|
434
|
+
if (message?.op === "channel_send" || message?.op === "channel_close") {
|
|
435
|
+
const channel = channels.get(message.id);
|
|
436
|
+
try {
|
|
437
|
+
if (message.op === "channel_close") channel?.close();
|
|
438
|
+
else if (channel) channel.send(message.message);
|
|
439
|
+
else
|
|
440
|
+
send({
|
|
441
|
+
type: "channel",
|
|
442
|
+
channel: message.id,
|
|
443
|
+
closed: true,
|
|
444
|
+
error: "Focus channel is unavailable",
|
|
445
|
+
});
|
|
446
|
+
} catch (error) {
|
|
447
|
+
channel?.close();
|
|
448
|
+
}
|
|
449
|
+
continue;
|
|
450
|
+
}
|
|
404
451
|
if (message?.op === "context_reply") {
|
|
405
452
|
const pending = contextRequests.get(message.id);
|
|
406
453
|
if (pending) {
|
|
@@ -425,6 +472,7 @@ process.stdin.on("data", (chunk) => {
|
|
|
425
472
|
});
|
|
426
473
|
|
|
427
474
|
async function shutdown() {
|
|
475
|
+
for (const channel of channels.values()) channel.close();
|
|
428
476
|
for (const close of subscriptions.values()) close();
|
|
429
477
|
for (const controller of contextCalls.values()) controller.abort();
|
|
430
478
|
await Promise.allSettled([...contextRelays.values()].map((close) => close()));
|