@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
224 lines • 9.07 kB
JavaScript
import { StringDecoder } from "node:string_decoder";
import { hostStoppingResponse } from "./host-admission.js";
import { MAX_RPC_LINE_CHARACTERS, serializeJsonLine } from "./jsonl.js";
import { launchProofRecordRefusal, RPC_LAUNCH_PROOF_RECORD } from "./rpc-auth-proof.js";
/**
* Requests are size-bounded; each direction awaits downstream socket backpressure.
* Opens remain concurrent with control commands. Stop refuses every pending open,
* while other commands retain their wire order and child-owned semantics.
*/
export function proxyWithAdmission(client, internal, admission, auth,
/** The client proved the launch credential at admission (the Windows public secret): say so to the host first. */
launchProven = false) {
const reply = (chunk) => write(client, auth ? auth.response(chunk.toString()) : chunk);
const pending = [];
const refused = new Set();
let protocols = 0;
let refusals = Promise.resolve();
const unsubscribe = admission.onStop(() => {
refusals = refusePending();
return refusals;
});
async function refusePending() {
const ids = pending.splice(0);
for (const id of ids)
refused.add(id);
try {
for (const id of ids)
await reply(serializeJsonLine(hostStoppingResponse(id, "open_session", admission)));
}
catch {
fail();
}
}
client.once("close", () => {
unsubscribe();
internal?.destroy();
});
const fail = () => {
internal?.destroy();
client.destroy();
};
client.once("error", fail);
internal?.once("error", () => {
if (!admission.stopping)
fail();
});
void requests().catch(() => {
if (!admission.stopping)
fail();
});
if (internal)
void responses(internal).catch(() => {
if (!admission.stopping)
fail();
});
async function requests() {
// Queued, never awaited: the client is resumed as soon as this returns, and its records are read only once the
// loop below is iterating. The write still lands on the hop before any record that loop forwards.
if (internal && launchProven)
internal.write(`${JSON.stringify({ type: RPC_LAUNCH_PROOF_RECORD })}\n`);
for await (const line of lines(auth?.input ?? client, MAX_RPC_LINE_CHARACTERS)) {
if (line === undefined) {
await reply(serializeJsonLine({
type: "response",
command: "parse",
success: false,
error: `RPC input line exceeds ${MAX_RPC_LINE_CHARACTERS} characters.`,
}));
continue;
}
const request = record(line);
// With a filter, every proof record on its input is the filter's own (it refuses the client's).
if (!auth && request?.type === RPC_LAUNCH_PROOF_RECORD) {
await reply(serializeJsonLine(launchProofRecordRefusal(request.id)));
continue;
}
if (admission.stopping && request?.type === "get_protocol_info") {
await reply(serializeJsonLine({
id: request.id,
type: "response",
command: request.type,
success: true,
data: admission.protocol(),
}));
}
else if (admission.stopping && (request?.type === "open_session" || !internal)) {
await reply(serializeJsonLine(hostStoppingResponse(request?.id, String(request?.type ?? "parse"), admission)));
}
else if (internal) {
// Record admission before writing: a child response can arrive immediately.
if (request?.type === "open_session")
pending.push(request.id);
if (request?.type === "get_protocol_info")
protocols++;
await write(internal, `${line}\n`);
}
}
internal?.end();
}
async function responses(source) {
// Keep complete records together so local refusals cannot split a streaming delta.
// After admission, ordinary traffic stays as raw bytes with one write per chunk.
let fragments = [];
for await (const chunk of source) {
const newline = chunk.lastIndexOf(0x0a);
if (newline === -1) {
fragments.push(chunk);
continue;
}
const complete = chunk.subarray(0, newline + 1);
if (!auth && pending.length === 0 && protocols === 0 && refused.size === 0) {
// Queue the entire record batch synchronously, without copying split buffers.
client.cork();
let written;
try {
for (const fragment of fragments)
client.write(fragment);
written = write(client, complete);
}
finally {
client.uncork();
}
await written;
}
else {
const batch = fragments.length > 0 ? Buffer.concat([...fragments, complete]) : complete;
await reply(filterReplies(batch));
}
fragments = newline + 1 < chunk.length ? [chunk.subarray(newline + 1)] : [];
}
if (fragments.length > 0)
await reply(filterReplies(Buffer.concat(fragments)));
await refusals;
client.end(() => client.destroy());
}
function filterReplies(batch) {
if (pending.length === 0 && protocols === 0 && refused.size === 0)
return batch;
const output = [];
let changed = false;
for (let start = 0; start < batch.length;) {
const newline = batch.indexOf(0x0a, start);
const end = newline === -1 ? batch.length : newline + 1;
const line = batch.subarray(start, end);
start = end;
// Only lifecycle responses need decoding; histories and deltas remain byte slices.
const response = line.includes('"open_session"') || line.includes('"get_protocol_info"')
? record(line.toString("utf8"))
: undefined;
if (response?.type === "response" && response.command === "open_session") {
if (refused.has(response.id)) {
changed = true;
continue;
}
const index = pending.indexOf(response.id);
if (index !== -1)
pending.splice(index, 1);
}
if (response?.type === "response" && response.command === "get_protocol_info") {
protocols = Math.max(0, protocols - 1);
if (response.success === true) {
if (isRecord(response.data))
admission.observe(response.data);
output.push(Buffer.from(serializeJsonLine({ ...response, data: admission.protocol() })));
changed = true;
continue;
}
}
output.push(line);
}
return changed ? Buffer.concat(output) : batch;
}
}
async function write(socket, line) {
await new Promise((resolve, reject) => {
socket.write(line, (error) => (error ? reject(error) : resolve()));
});
}
/** Strict LF and incremental UTF-8; an oversized input is discarded through its next LF. */
async function* lines(socket, limit) {
const decoder = new StringDecoder("utf8");
let buffer = "";
let discarding = false;
for await (const chunk of socket) {
const text = typeof chunk === "string" ? chunk : decoder.write(chunk);
let offset = 0;
while (offset < text.length) {
const newline = text.indexOf("\n", offset);
const end = newline === -1 ? text.length : newline;
if (!discarding) {
if (buffer.length + end - offset > limit) {
buffer = "";
discarding = true;
yield undefined;
}
else
buffer += text.slice(offset, end);
}
if (newline === -1)
break;
if (!discarding)
yield buffer;
buffer = "";
discarding = false;
offset = newline + 1;
}
}
buffer += decoder.end();
if (!discarding && buffer.length > 0)
yield buffer;
}
function record(line) {
try {
const value = JSON.parse(line);
return isRecord(value) ? value : undefined;
}
catch {
return undefined;
}
}
function isRecord(value) {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
//# sourceMappingURL=host-admission-proxy.js.map