@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
69 lines • 3.55 kB
JavaScript
/**
* The `move_session` answers that say where an accepted move landed (senpi#6436): `status`, and a `move_fenced`
* refusal of a move already accepted here. Whoever may send the import may ask; nothing is opened, held or rewritten
* for it. A `status` may also name the place the move would have landed, and is then answered from the file there
* first.
*/
import { join } from "node:path";
import { getDefaultSessionDirPath } from "../../core/session-manager.js";
import { answerMoveStatusAndLanding } from "../../core/session-move/import.js";
import { MoveFencedError } from "../../core/session-move/move-ledger.js";
import { normalizePath, resolvePath } from "../../utils/paths.js";
function wireLanded(landed) {
if (landed === undefined)
return undefined;
if (landed === "gone")
return { landed_file: "gone" };
const { ownership } = landed;
const where = { session_path: landed.sessionPath, session_id: landed.sessionId, cwd: landed.cwd };
if (ownership.writable)
return { ...where, writable: true };
// A release that named no device is the empty string in the file. The wire leaves the field out instead, so a
// client cannot show a name that is not there.
const sealed_by = {
move_id: ownership.moveId,
state: ownership.state,
...(ownership.target === "" ? {} : { target: ownership.target }),
};
return { ...where, writable: false, sealed_by };
}
/**
* The session file a `status` asks about. A `landing` is resolved by the rules `import` lands a session by, from the
* same three values, so the two name one path; and without making the session folder, so a question leaves nothing
* behind.
*/
export function statusFile(named, agentDir) {
if (named === undefined)
return undefined;
switch (named.kind) {
case "path":
return { file: named.sessionPath, as: "copy" };
case "landing": {
const sessionDir = named.sessionDir === undefined
? getDefaultSessionDirPath(resolvePath(named.cwd), agentDir)
: normalizePath(named.sessionDir);
return { file: join(sessionDir, named.sessionFileName), as: "landing" };
}
}
}
/** The `status` answer: the receiver's record, and beside `accepted` where the move landed. */
export async function statusAnswer(ledger, moveId, asked) {
const { answer, landed } = await answerMoveStatusAndLanding(ledger, moveId, asked);
return answer.state === "accepted" ? { ...answer, ...wireLanded(landed) } : answer;
}
/**
* What a `move_fenced` refusal's `errorData` gains when the move it refuses is already accepted here, whichever op
* was refused (an `import` into another folder, a `prepare` for another folder): where the move landed, and
* `revoked: true` when that copy was sealed, so a caller learns that binding it would answer `session_moved_away`.
* It only adds to a refusal: if the record or the landed file cannot be read now, the refusal goes out without it.
*/
export async function landedWhenFenced(ledger, cause) {
if (!(cause instanceof MoveFencedError) || cause.record?.state !== "accepted")
return undefined;
const read = await answerMoveStatusAndLanding(ledger, cause.moveId, undefined).catch(() => undefined);
const where = wireLanded(read?.landed);
if (read === undefined || where === undefined)
return undefined;
return "revoked" in read.answer ? { ...where, revoked: true } : where;
}
//# sourceMappingURL=session-move-landed.js.map