@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
51 lines • 2.59 kB
JavaScript
import { realpath } from "node:fs/promises";
import { resolve } from "node:path";
import { withSessionMoveLock as withHolderMoveLock } from "../session-holders.js";
import { loadEntriesFromFileAsync } from "../session-manager.js";
import { serializeByKey } from "../session-sidecar-store.js";
import { headerSessionId } from "./ownership.js";
export async function realSessionPath(path) {
const absolute = resolve(path);
try {
return await realpath(absolute);
}
catch {
return absolute;
}
}
const lockKey = (resolvedPath) => `session-move:${resolvedPath}`;
// Resolutions of one path settle one after another, in call order, so two steps asked for in order enter the
// per-file queue in that order even though resolving the real path is asynchronous. Each path has its own chain, so
// one stuck realpath never delays another file's; a chain is dropped once it is idle. `realSessionPath` never rejects.
const pathChains = new Map();
export function orderedRealPath(path) {
const key = resolve(path);
const next = (pathChains.get(key) ?? Promise.resolve("")).then(() => realSessionPath(path));
pathChains.set(key, next);
void next.then(() => {
if (pathChains.get(key) === next)
pathChains.delete(key);
});
return next;
}
export const moveLockKey = async (sessionFile) => lockKey(await orderedRealPath(sessionFile));
/**
* Runs the sender's steps of a move (export, finish) one at a time per session file, across processes too.
*
* - In this process, steps queue per file, so a second `finish` for the same move starts after the first has settled
* and finds the move already closed: the queued input it hands back is re-queued once, never twice.
* - Across processes (two host generations on one agent directory), the step holds the session's move lock, which
* proves no live process holds the session (`SessionHeldError`, code `ESESSIONHELD`) and makes every later
* `holdSessionFile` wait or refuse while the step runs.
* - `guard` runs first inside the queue, so a caller's own check (for example "not open on this host") sees the
* state the step will act on.
*/
export async function withSessionMoveLock(sessionFile, step, guard) {
const path = await orderedRealPath(sessionFile);
return serializeByKey(lockKey(path), async () => {
await guard?.();
const sessionId = headerSessionId(await loadEntriesFromFileAsync(path));
return sessionId === undefined ? step() : withHolderMoveLock(path, sessionId, step);
});
}
//# sourceMappingURL=session-lock.js.map