@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
154 lines • 5.67 kB
JavaScript
/**
* One `exec_command` terminal as the relay host's wire sees it (senpi#6752), over a `TerminalRuntimeSession` the engine's
* `TerminalManager` owns. The raw PTY bytes are counted and the last `RETAINED_OUTPUT_BYTES` kept, so `lastSeq` and
* `totalBytes` stay byte offsets. Lifecycle as the relay host runs it: running; waiting_input once the program has been
* quiet for OUTPUT_SETTLE_MS while the terminal's foreground group is the shell's own (unknown counts as yes); running
* again on the next output; then exited, or killed after a kill request. Each settled quiet period is also announced.
*/
import { readFile } from "node:fs/promises";
const OUTPUT_SETTLE_MS = 800;
const RETAINED_OUTPUT_BYTES = 1024 * 1024;
export const MACHINE_LIFECYCLES = [
"starting",
"running",
"waiting_input",
"exited",
"killed",
"lost",
];
export const ENDED = ["exited", "killed", "lost"];
export const ended = (state) => ENDED.includes(state);
const PASTE_ON = "\x1b[?2004h";
const PASTE_OFF = "\x1b[?2004l";
/** Linux: whether the terminal's foreground process group is the leader's own (field 8 of /proc/<pid>/stat). */
async function foregroundOf(pid) {
if (pid === undefined || process.platform !== "linux")
return null;
try {
const stat = await readFile(`/proc/${pid}/stat`, "utf8");
const fields = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
return Number(fields[8 - 3]) === pid;
}
catch {
return null;
}
}
export function createLiveHandle(init) {
const session = init.runtime.session;
const pid = session.pid;
const listeners = new Set();
const done = Promise.withResolvers();
// Output that arrived while the manager registered the session: the session's raw tail and byte count.
const early = new Uint8Array(session.rawTail);
const chunks = early.byteLength === 0 ? [] : [early];
let retained = early.byteLength;
let produced = session.rawOutputBytes;
let state = "running";
let exit = null;
let killed = false;
let quiet = false;
let paste = false;
let pasteCarry = "";
let cancelSettle = () => { };
const emit = (e) => {
for (const listener of [...listeners])
listener(e);
};
const setState = (next) => {
if (next === state)
return;
state = next;
emit({ kind: "state", state: next });
};
const armSettle = () => {
cancelSettle();
if (exit !== null)
return;
const at = produced;
cancelSettle = init.after(OUTPUT_SETTLE_MS, () => {
void foregroundOf(pid).then((foreground) => {
if (exit !== null || produced !== at)
return;
quiet = true;
if (foreground !== false)
setState("waiting_input");
emit({ kind: "idle", foreground });
});
});
};
const keep = (bytes) => {
chunks.push(bytes);
retained += bytes.byteLength;
while (retained > RETAINED_OUTPUT_BYTES) {
const first = chunks[0] ?? new Uint8Array(0);
const cut = retained - RETAINED_OUTPUT_BYTES;
if (first.byteLength <= cut)
chunks.shift();
else
chunks[0] = first.subarray(cut);
retained -= Math.min(cut, first.byteLength);
}
};
// The program's bracketed-paste mode, from the last mode switch in its output (a switch may span two chunks).
const trackPaste = (bytes) => {
const seen = pasteCarry + Buffer.from(bytes).toString("latin1");
const on = seen.lastIndexOf(PASTE_ON);
const off = seen.lastIndexOf(PASTE_OFF);
if (on !== off)
paste = on > off;
pasteCarry = seen.slice(-(PASTE_ON.length - 1));
};
const unsubscribeData = session.onData((chunk) => {
const bytes = new Uint8Array(chunk);
produced += bytes.byteLength;
keep(bytes);
trackPaste(bytes);
quiet = false;
if (state === "waiting_input")
setState("running");
emit({ kind: "output", seq: produced, bytes });
armSettle();
});
session.onExit(() => {
const result = session.exitResult;
exit = { exitCode: result?.exitCode ?? null, signal: result?.signal ?? null };
cancelSettle();
unsubscribeData();
setState(killed ? "killed" : "exited");
done.resolve();
});
armSettle();
return {
id: init.id,
pid,
sender: init.sender,
startedMs: init.runtime.startedAtMs,
state: () => state,
exit: () => exit,
produced: () => produced,
retainedFrom: () => produced - retained,
/** The retained bytes from `seq` (or from the oldest retained byte, when `seq` is older) to the end. */
since: (seq) => {
const from = produced - retained;
const all = Buffer.concat(chunks, retained);
return new Uint8Array(all.subarray(Math.max(0, Math.min(seq, produced) - from)));
},
quiet: () => quiet,
bracketedPaste: () => paste,
on: (listener) => {
listeners.add(listener);
return () => void listeners.delete(listener);
},
write: (bytes) => void session.write(bytes),
markKilled: () => {
killed = true;
},
ended: done.promise,
dispose: () => {
cancelSettle();
unsubscribeData();
listeners.clear();
},
};
}
//# sourceMappingURL=live-handle.js.map