@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
63 lines • 3.32 kB
JavaScript
import chalk from "chalk";
import { checkSessionHolders, describeSessionHolders, hostAttachSocket, SESSION_HOLDER_LOOKUP_FAILED, sessionIdFromFile, sessionHolderWarning, stopSessionHolders, takeOverRefusal, } from "../core/foreign-session-holders.js";
/** Warn after the runtime publishes its holder; an advisory lookup must not abort startup. */
export async function prepareSessionOpening(manager, mode) {
if (mode === "interactive") {
const warning = await sessionHolderWarning(manager.getSessionFile(), manager.getSessionId());
if (warning !== undefined)
console.error(chalk.yellow(`Warning: ${warning}`));
}
}
/**
* Exit code of a startup refused because another live process holds the session. The schedule runner reads it to
* leave the occurrence pending instead of failing it (senpi#5756).
*/
export const SESSION_HELD_EXIT_CODE = 76;
const RETRY = "then open it again";
/**
* Startup admission for a session another live process holds (senpi#5756, the startup path of #5744): opening it
* would start a second run of the same session. Without a person to ask it is refused with the holder named; on a
* terminal the person chooses to cancel or to take over (the holder is asked to exit first, as /resume does).
*/
export async function admitStartupSession(sessionFile, io) {
if (sessionFile === undefined)
return "open";
const check = await (io.lookup ?? checkSessionHolders)(sessionFile);
if ("lookupFailed" in check) {
io.err(chalk.yellow(`Warning: ${SESSION_HOLDER_LOOKUP_FAILED}`));
return "open";
}
const { holders } = check;
if (holders.length === 0)
return "open";
// #6059: held by this machine's multi-session host (the OmO desktop's engine): a terminal follows that live session
// over the host, so what either side does shows on the other, instead of being refused or a second writer.
const socket = io.canAsk && io.canAttach === true ? hostAttachSocket(holders) : undefined;
const sessionId = socket === undefined ? undefined : await sessionIdFromFile(sessionFile);
if (socket !== undefined && sessionId !== undefined)
return { attach: { socket, sessionId } };
const held = `This session is open in another process (${describeSessionHolders(holders)}); opening it here would start a second run of the same session.`;
const refusal = takeOverRefusal(holders, RETRY);
if (!io.canAsk || refusal !== undefined) {
io.err(chalk.red(`Not opened: ${held} ${refusal ?? `Quit it there, ${RETRY}.`}`));
return "refused";
}
if (!(await io.confirm(`${held}\nTake over (stop it there)?`))) {
io.err(`Not opened: the session is open in another process (${describeSessionHolders(holders)}).`);
return "cancelled";
}
let stopped;
try {
stopped = await stopSessionHolders(sessionFile, holders);
}
catch (error) {
io.err(chalk.red(`Not opened: could not stop the other process (${error instanceof Error ? error.message : String(error)}).`));
return "refused";
}
if (!stopped) {
io.err(chalk.red(`Not opened: the session is still open in another process; quit it there, ${RETRY}.`));
return "refused";
}
return "open";
}
//# sourceMappingURL=session-opening.js.map