@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
80 lines • 4.58 kB
JavaScript
/**
* `move_session` op `import` (senpi#3676, senpi#6267): the bundle comes as the folder the transport wrote or inline in
* the request, the session lands in this host's session folder for `cwd` or in the one the caller names, and with
* `open` the landed session is opened in the same answer.
*/
import { constants } from "node:fs";
import { access, stat } from "node:fs/promises";
import { join } from "node:path";
import { getDefaultSessionDir, loadEntriesFromFileAsync } from "../../core/session-manager.js";
import { importMovedSession } from "../../core/session-move/import.js";
import { withStagedInlineBundle } from "../../core/session-move/inline-bundle.js";
import { headerSessionId } from "../../core/session-move/ownership.js";
import { normalizePath, resolvePath } from "../../utils/paths.js";
/**
* Why a `sessionDir` cannot hold the session: a file is in its place or above it, this host could not look at it (no
* permission, a symlink loop), or it is a folder this host may not write into. A request error, asked before the
* import touches the move, so the move stays importable. `undefined` for a folder this host can write into and for a
* path that is not there yet (ENOENT), which the import creates.
*/
export async function sessionDirRefusal(sessionDir) {
if (sessionDir === undefined)
return undefined;
const dir = normalizePath(sessionDir);
const code = (cause) => typeof cause === "object" && cause !== null && "code" in cause ? String(cause.code) : "unknown";
const found = await stat(dir).then((entry) => (entry.isDirectory() ? "folder" : "file"), code);
if (found === "ENOENT")
return undefined;
if (found === "folder") {
const denied = await access(dir, constants.W_OK | constants.X_OK).then(() => undefined, code);
return denied === undefined ? undefined : `sessionDir cannot be used (${denied}): this host cannot write there.`;
}
if (found === "file" || found === "ENOTDIR")
return "sessionDir must be a folder: a file is in its place or above it.";
// Any other answer (no permission, a symlink loop, an I/O error) is not "nothing there": the import would fail on
// the same path after the move began receiving, and settle it failed.
return `sessionDir cannot be used (${found}): this host could not look at it.`;
}
/** An inline bundle is staged in this host's own folder under `root`, then read exactly as a folder is. */
export async function importMoved(parsed, port, ledger, root) {
const cwd = resolvePath(parsed.cwd);
const landing = {
targetCwd: cwd,
ledger,
sessionDir: parsed.sessionDir === undefined ? getDefaultSessionDir(cwd, port.agentDir) : normalizePath(parsed.sessionDir),
from: parsed.from,
};
const { sessionFile } = "dir" in parsed.bundle
? await importMovedSession({ ...landing, bundleDir: normalizePath(parsed.bundle.dir) })
: await withStagedInlineBundle(parsed.bundle.files, join(root, "incoming"), (bundleDir, sent) => importMovedSession({ ...landing, bundleDir, sent }));
const sessionId = headerSessionId(await loadEntriesFromFileAsync(sessionFile));
if (sessionId === undefined)
throw new Error(`The imported session at ${sessionFile} has no id in its header.`);
return { session_id: sessionId, session_path: sessionFile };
}
/**
* Opens the session an import landed, in the folder the import named (`cwd`): an open that names no folder runs in
* the host's own, whatever the landed file's header says (senpi#6427). `undefined` means the open queued the import's
* answer itself; otherwise the open failed, which never undoes the import: the move stands, and the caller opens the
* session with `open_session`.
*/
export async function openLanded(port, id, open, cwd, landed) {
const failure = await port
.open({
...(open === true ? {} : open),
type: "open_session",
sessionPath: landed.session_path,
cwd: resolvePath(cwd),
}, { command: { type: "move_session", id }, data: landed })
.catch((cause) => ({ error: cause instanceof Error ? cause.message : String(cause) }));
if (failure === undefined || !("success" in failure))
return failure;
if (failure.success)
return undefined;
return {
error: failure.error,
...(failure.errorCode === undefined ? {} : { errorCode: failure.errorCode }),
...(failure.errorData === undefined ? {} : { errorData: failure.errorData }),
};
}
//# sourceMappingURL=session-move-import.js.map