@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
71 lines • 3.36 kB
JavaScript
/**
* One activity-neutral observer connection to a host or terminal endpoint: the socket handshake every
* RPC socket shares, one `subscribe_sessions` frame, then every record the subscription delivers. It never
* attaches and never sends input, so it counts as no attachment and refreshes no idle window. The
* classic `RpcClient` is session-scoped (it drops records tagged with another session), so the monitor
* reads the stream itself.
*/
import { createConnection } from "node:net";
import { OBSERVE_REQUEST_FIELD } from "../../../../modes/rpc/host-observe-request.js";
import { attachJsonlLineReader, MAX_RPC_LINE_CHARACTERS } from "../../../../modes/rpc/jsonl.js";
import { connectAuthenticatedRpcSocket } from "../../../../modes/rpc/rpc-auth-client.js";
import { readSocketSecret, resolveSocketTransportAddress, sendSocketHandshake, socketSecretPath, } from "../../../../modes/rpc/socket-transport.js";
import { socketNeedsHandshake } from "../../../../modes/rpc/tui-socket.js";
const SUBSCRIBE_ID = "monitor-subscribe";
export async function openRpcObserver(socketPath, subscription, handlers, options = {}) {
const secret = socketNeedsHandshake(socketPath) ? await readSocketSecret(socketSecretPath(socketPath)) : undefined;
const authenticated = !socketNeedsHandshake(socketPath)
? await connectAuthenticatedRpcSocket(socketPath, undefined, options)
: undefined;
const socket = authenticated?.socket ?? createConnection(resolveSocketTransportAddress(socketPath, process.platform, secret));
const answer = Promise.withResolvers();
let ended = false;
const end = (reason) => {
if (ended)
return;
ended = true;
answer.reject(new Error(`rpc monitor: ${socketPath}: ${reason}`));
handlers.closed(reason);
};
const subscribe = () => {
if (secret !== undefined)
sendSocketHandshake(socket, secret);
// Marked observing (senpi#3956): without it the supervisor counts this watch as an attached client, so a
// watched host never idle-exits and a `children`/`all` watch pins every task shard host on the machine.
const frame = { ...subscription, [OBSERVE_REQUEST_FIELD]: true, id: SUBSCRIBE_ID, type: "subscribe_sessions" };
socket.write(`${JSON.stringify(frame)}\n`);
};
const detach = attachJsonlLineReader(socket, (line) => {
let record;
try {
record = JSON.parse(line);
}
catch {
return;
}
if (record.type === "response" && record.id === SUBSCRIBE_ID) {
if (record.success === true)
answer.resolve(record);
else {
answer.reject(new Error(`rpc monitor: ${socketPath} refused the subscription: ${String(record.error)}`));
socket.destroy();
}
return;
}
handlers.record(record);
}, { maxLineLength: MAX_RPC_LINE_CHARACTERS });
socket.on("error", (error) => end(error.message));
socket.once("close", () => {
detach();
end("connection closed");
});
if (authenticated)
subscribe();
else
socket.once("connect", subscribe);
return {
subscribed: answer.promise,
close: () => socket.destroy(),
};
}
//# sourceMappingURL=rpc-monitor-observer.js.map