@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
59 lines • 2.77 kB
JavaScript
import { loadEntriesFromFileAsync, SessionManager } from "../session-manager.js";
import { moveStep } from "./move-steps.js";
import { headerSessionId, ownershipStep, SESSION_MOVED, SESSION_MOVED_IN, } from "./ownership.js";
import { withSessionMoveLock } from "./session-lock.js";
export class RevokeMismatchError extends Error {
constructor(moveId) {
super(`This session file is not the copy this device imported for move ${moveId}; nothing was changed. Revoke only the copy that move brought in, with that move's id.`);
this.code = "move_mismatch";
this.name = "RevokeMismatchError";
}
}
/** The `session_moved` reason a revoke records. */
export const REVOKED = "revoked";
/**
* Seals the copy a receiving device imported for a move after the sender took the session back (senpi#4003):
* `session_moved` with reason `revoked` makes it read-only for good. Only a file whose latest ownership step is this
* move's `session_moved_in` is revoked; a repeat finds it already revoked and changes nothing. After the seal, under the
* same lock, the move's accepted record is marked revoked; a repeat writes it again if a crash lost it.
*/
export function revokeImportedMove(options) {
return withSessionMoveLock(options.sessionFile, async () => {
await seal(options.sessionFile, options.moveId);
moveStep("revoke:sealed");
await options.ledger.markRevoked(options.moveId);
moveStep("revoke:marked");
}, options.guard);
}
async function seal(sessionFile, moveId) {
const entries = await loadEntriesFromFileAsync(sessionFile);
const sessionId = headerSessionId(entries);
let ownership = { writable: true };
let latest;
for (const entry of entries) {
const step = ownershipStep(ownership, entry, sessionId);
if (step.kind !== "apply")
continue;
ownership = step.next;
latest = entry;
}
const ofThisMove = latest !== undefined && data(latest, "move_id") === moveId;
if (ofThisMove && latest?.customType === SESSION_MOVED && data(latest, "reason") === REVOKED)
return;
if (!(ownership.writable && ofThisMove && latest?.customType === SESSION_MOVED_IN))
throw new RevokeMismatchError(moveId);
(await SessionManager.openAsync(sessionFile)).appendCustomEntry(SESSION_MOVED, {
move_id: moveId,
session_id: sessionId ?? "",
target: data(latest, "from") ?? "",
reason: REVOKED,
moved_at: new Date().toISOString(),
});
}
function data(entry, key) {
if (typeof entry.data !== "object" || entry.data === null)
return undefined;
const value = entry.data[key];
return typeof value === "string" ? value : undefined;
}
//# sourceMappingURL=revoke.js.map