UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

115 lines • 5.31 kB
import { rm } from "node:fs/promises"; import { loadEntriesFromFileAsync, SessionManager } from "../session-manager.js"; import { pathExists } from "../tools/path-utils.js"; import { readKeptBundle } from "./bundle.js"; import { outgoingStateDir } from "./extension-state.js"; import { moveStep } from "./move-steps.js"; import { headerSessionId, SESSION_MOVED, SESSION_RECLAIMED, sessionOwnership } from "./ownership.js"; import { requeueInput } from "./requeue.js"; import { withSessionMoveLock } from "./session-lock.js"; export class MoveMismatchError extends Error { constructor(expected, found, what) { super(`The answer is about move ${expected}, but ${what} belongs to move ${found}; nothing was changed. Finish each move with its own answer and bundle.`); this.code = "move_mismatch"; this.name = "MoveMismatchError"; } } export class MoveAlreadyMovedError extends Error { constructor(moveId) { super(`The receiver revoked its copy for move ${moveId}, but this session file was already marked moved by an earlier accepted answer; nothing was changed and no copy is writable. Copy the session to a new thread to keep working from it.`); this.code = "move_already_moved"; this.name = "MoveAlreadyMovedError"; } } /** * Closes a move on the sending device from the receiver's answer. `accepted` records `session_moved` (read-only for * good). `declined`/`failed`, and `accepted` with `revoked` (the receiver sealed its copy), take the session back: * `session_reclaimed` makes the file writable, the queued input is re-queued under ids stable per move, then the * bundle goes. While the bundle exists the re-queue runs again on the next call, so a crash between any two steps * loses nothing and doubles nothing. A revoked answer for a file already marked moved is refused. */ export function finishMove(options) { return withSessionMoveLock(options.sessionFile, () => settleMove(options), options.guard); } async function settleMove(options) { const { sessionFile, bundleDir, moveId, answer } = options; if (await pathExists(bundleDir)) { const bundled = (await readKeptBundle(bundleDir)).moveId; if (bundled !== moveId) throw new MoveMismatchError(moveId, bundled, "the kept bundle"); } const entries = await loadEntriesFromFileAsync(sessionFile); const ownership = sessionOwnership(entries); if (ownership.writable) { if (reclaimed(entries, moveId)) await requeueDroppedInput(sessionFile, bundleDir); await discardMoveFiles(sessionFile, bundleDir, moveId); return; } if (ownership.moveId !== moveId) throw new MoveMismatchError(moveId, ownership.moveId, "the session file"); if (ownership.state === "moved") { if ("revoked" in answer) throw new MoveAlreadyMovedError(moveId); await discardMoveFiles(sessionFile, bundleDir, moveId); return; } const sessionId = headerSessionId(entries) ?? ""; switch (answer.state) { case "receiving": return; case "accepted": if ("revoked" in answer) { await takeBack(sessionFile, bundleDir, { move_id: moveId, session_id: sessionId, reason: "revoked" }); break; } (await SessionManager.openAsync(sessionFile)).appendCustomEntry(SESSION_MOVED, { move_id: moveId, session_id: sessionId, target: ownership.target, moved_at: new Date().toISOString(), }); moveStep("finish:recorded"); break; case "declined": case "failed": await takeBack(sessionFile, bundleDir, { move_id: moveId, session_id: sessionId, reason: answer.state, detail: answer.reason, }); break; default: { const unreachable = answer; throw new Error(`Unknown move answer ${JSON.stringify(unreachable)}`); } } moveStep("finish:requeued"); await discardMoveFiles(sessionFile, bundleDir, moveId); } /** `session_reclaimed` makes the file writable again, then the queued input comes back under ids stable per move. */ async function takeBack(sessionFile, bundleDir, data) { (await SessionManager.openAsync(sessionFile)).appendCustomEntry(SESSION_RECLAIMED, { ...data, reclaimed_at: new Date().toISOString(), }); moveStep("finish:recorded"); await requeueDroppedInput(sessionFile, bundleDir); } async function discardMoveFiles(sessionFile, bundleDir, moveId) { await rm(bundleDir, { recursive: true, force: true }); await rm(outgoingStateDir(sessionFile, moveId), { recursive: true, force: true }); } function reclaimed(entries, moveId) { return entries.some((entry) => entry.type === "custom" && entry.customType === SESSION_RECLAIMED && entry.data?.move_id === moveId); } export async function requeueDroppedInput(sessionFile, bundleDir) { if (!(await pathExists(bundleDir))) return; const kept = await readKeptBundle(bundleDir); await requeueInput(sessionFile, kept.moveId, kept.droppedUserMessages); } //# sourceMappingURL=finish.js.map