@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
117 lines • 5.87 kB
JavaScript
/**
* Which hand runs a headless session's tools (R4; the DK2a engine half). A hand directory says which hands this engine
* can reach, and the user's choice is recorded per session, so it survives a restart. A chosen hand runs every tool
* call of that session through a remote ToolHost. A chosen hand that cannot run them makes the calls fail as error
* results; nothing falls back to this process. Choosing `null` gives the session back its own ToolHost (the fused
* path, untouched).
*/
import { createReliableTransport, createRemoteToolHost, } from "@earendil-works/pi-agent-core";
import { withHandPageBudget } from "../../extensions/html-render/hand-page-budget.js";
/** The session-file entry that records a session's choice; `device: null` is local. */
export const HAND_SELECTION_ENTRY = "hand_selection";
/** A remote host on a transport that never connects: each call it gets ends as "The hand is unreachable". */
function unreachableHost() {
const transport = createReliableTransport({
clock: { now: () => Date.now(), setTimer: () => () => { } },
onHandlerError: () => { },
});
transport.close();
return createRemoteToolHost({ transport, placement: "cross-machine" });
}
function recordedDevice(session) {
let device = null;
for (const entry of session.sessionManager.getEntries()) {
if (entry.type !== "custom" || entry.customType !== HAND_SELECTION_ENTRY)
continue;
const recorded = Reflect.get(Object(entry.data), "device");
device = typeof recorded === "string" ? recorded : null;
}
return device;
}
export function createHandSelection(directory, options = {}) {
const sessions = new Map();
const gate = options.gate ?? ((_id, host) => host);
const stateOf = (durableSessionId, session) => {
const known = sessions.get(durableSessionId);
if (known !== undefined)
return known;
const fresh = { own: session.agent.toolHost, device: null, remote: undefined, linked: false };
sessions.set(durableSessionId, fresh);
return fresh;
};
const install = (durableSessionId, session, device, remote) => {
const state = stateOf(durableSessionId, session);
state.remote?.close();
// The hand cannot see this session's transcript, so the session's page budget is applied here (senpi#6921). The
// budget is what the session keeps and closes, so it stops listening for the session's runs with the link (senpi#6932).
const host = remote === undefined ? undefined : withHandPageBudget(remote.host, session);
sessions.set(durableSessionId, { own: state.own, device, remote: host, linked: remote?.linked ?? false });
session.agent.toolHost = host === undefined ? state.own : gate(durableSessionId, host);
options.onChange?.(durableSessionId, device);
};
const unreachable = () => ({ host: unreachableHost(), linked: false });
const linkTo = (device, session) => {
try {
return {
host: createRemoteToolHost({ ...directory.link(device), promptSurface: () => session.promptSurface }),
linked: true,
};
}
catch {
return unreachable();
}
};
return {
hands: () => directory.list(),
selected: (durableSessionId) => sessions.get(durableSessionId)?.device ?? null,
select: (durableSessionId, session, device) => {
const current = stateOf(durableSessionId, session);
// Already on it: nothing to do. A recorded hand that was not linked (gone at reopen) is linked again below.
if (current.device === device && (device === null || current.linked))
return { ok: true };
// A running turn keeps the ToolHost it started with: a change now would leave its later batches on the old one.
if (session.isStreaming)
return { ok: false, refusal: "session_busy" };
let remote;
if (device !== null) {
const row = directory.list().find((hand) => hand.device === device);
if (row === undefined)
return { ok: false, refusal: "hand_not_listed" };
if (!row.reachable)
return { ok: false, refusal: "hand_unreachable" };
remote = linkTo(device, session);
if (!remote.linked) {
remote.host.close();
return { ok: false, refusal: "hand_unreachable" };
}
}
// Recorded before it is installed: a failed write leaves the session as it was.
try {
if (current.device !== device)
session.sessionManager.appendCustomEntry(HAND_SELECTION_ENTRY, { device });
}
catch (cause) {
remote?.host.close();
throw cause;
}
install(durableSessionId, session, device, remote);
return { ok: true };
},
restore: (durableSessionId, session) => {
sessions.get(durableSessionId)?.remote?.close();
sessions.delete(durableSessionId);
stateOf(durableSessionId, session);
const device = recordedDevice(session);
if (device === null)
return;
const row = directory.list().find((hand) => hand.device === device);
install(durableSessionId, session, device, row?.reachable === true ? linkTo(device, session) : unreachable());
},
release: (durableSessionId) => {
sessions.get(durableSessionId)?.remote?.close();
sessions.delete(durableSessionId);
options.onRelease?.(durableSessionId);
},
};
}
//# sourceMappingURL=hand-selection.js.map