@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
138 lines • 6.94 kB
JavaScript
/**
* Who owns an endpoint's public socket entry, and the fence every rename or remove of that entry runs under
* (senpi#4602, A-amended section 4, carrying the senpi#2729 ordering rule into endpoint adoption).
*
* The owner token names the generation that serves the public name. The fence is a short, cross-process
* exclusive SQLite lock keyed by the canonical endpoint, the same key the ensure lock uses, but a different file:
* a predecessor's teardown must be able to finish its own short mutation while a successor's launcher holds the
* long ensure lock. Inside the fence a writer re-reads the token and the physical entry, mutates, and awaits the
* mutation before releasing, so a check and its rename can never be split by another writer. The kernel releases
* the lock when its holder dies; it is never stolen by age or by a pid guess. No RPC, presenter exchange or
* child-exit wait runs while it is held.
*/
import { randomUUID } from "node:crypto";
import { mkdir, readFile, rename, rm, writeFile } from "node:fs/promises";
import { dirname } from "node:path";
import { hostEnsureLockOptions, hostEnsureLockTarget } from "./host-ensure-lock.js";
import { acquireOwnershipSafeLock, isBusy } from "./ownership-safe-lock.js";
import { adoptPublicSocket, shieldSocketDuringClose, unlinkOwnedSocket, } from "./socket-ownership.js";
/** P6's allowance: a fenced mutation that cannot start within it refuses without mutating anything. */
const ENDPOINT_FENCE_WAIT_MS = 15_000;
export function endpointOwnerToken(endpoint, instanceId, generation) {
return { v: 1, endpoint, instanceId, generation, pid: process.pid, nonce: randomUUID() };
}
const ownerFile = (socket) => `${hostEnsureLockTarget(socket)}.owner.json`;
async function withEndpointFence(socket, run, hooks) {
const fence = `${hostEnsureLockTarget(socket)}.fence.lock`;
await mkdir(dirname(fence), { recursive: true });
let release;
try {
release = await acquireOwnershipSafeLock(fence, { retries: { retries: 0, minTimeout: 1, maxTimeout: 1 } });
}
catch (cause) {
if (!isBusy(cause))
throw cause;
hooks?.onFenceBusy?.();
release = await acquireOwnershipSafeLock(fence, hostEnsureLockOptions(ENDPOINT_FENCE_WAIT_MS));
}
try {
return await run();
}
finally {
await release();
}
}
/** The current owner token; `undefined` when none was claimed or it cannot be read as one. */
export async function readEndpointOwner(socket) {
let parsed;
try {
parsed = JSON.parse(await readFile(ownerFile(socket), "utf8"));
}
catch {
return undefined;
}
if (typeof parsed !== "object" || parsed === null)
return undefined;
const token = parsed;
return token.v === 1 && typeof token.instanceId === "string" && typeof token.nonce === "string"
? parsed
: undefined;
}
async function writeEndpointOwner(socket, token) {
const path = ownerFile(socket);
const staged = `${path}.${process.pid}.tmp`;
await writeFile(staged, `${JSON.stringify(token)}\n`, { mode: 0o600 });
await rename(staged, path);
}
/**
* An initial start: binds the public name (`bind`, with its stale-entry preparation) and claims it. The fence is
* taken only when an owner token already exists. A first start has no predecessor whose late rename or remove it
* could race, and the ensure lock its launcher holds already serialises concurrent first starts of this endpoint, so
* skipping the fence there keeps the SQLite driver out of every supervisor's boot (`rpc-supervisor-footprint`).
*/
export async function claimEndpoint(socket, token, bind) {
const claim = async () => {
await bind?.();
await writeEndpointOwner(socket, token);
};
if (await readEndpointOwner(socket)) {
await withEndpointFence(socket, claim);
return;
}
await mkdir(dirname(ownerFile(socket)), { recursive: true });
await claim();
}
/**
* A successor takes the public name over. Inside the fence: the launcher's grant must still stand, the owner token
* must still name the expected predecessor (when the predecessor participates in the fence at all), the entry must
* still be the one the handoff was decided against; then the rename and the new token, awaited.
*/
export async function adoptEndpoint(options) {
const { publicSocket } = options;
await withEndpointFence(publicSocket, async () => {
if (options.granted && !(await options.granted()))
throw new Error(`${publicSocket}: the launcher revoked this generation's adoption grant`);
const current = await readEndpointOwner(publicSocket);
if (options.expectedOwner !== undefined && current && current.instanceId !== options.expectedOwner)
throw new Error(`${publicSocket}: owner token names ${current.instanceId}, not ${options.expectedOwner}; refusing to replace it`);
await adoptPublicSocket(options.bindSocket, publicSocket, options.expected);
await writeEndpointOwner(publicSocket, options.token);
}, options.hooks);
}
/**
* A generation removes its public entry: only while the owner token does not name another generation, and only
* the entry `identity` describes. Its own token goes with it.
*/
export async function unlinkOwnedEndpoint(socket, identity, instanceId, log, hooks) {
await withEndpointFence(socket, async () => {
const current = await readEndpointOwner(socket);
if (current && current.instanceId !== instanceId) {
log(`socket path ${socket} is owned by generation ${current.instanceId} now; leaving it`);
return;
}
await hooks?.afterValidate?.();
await unlinkOwnedSocket(socket, identity, log);
if (current)
await rm(ownerFile(socket), { force: true });
});
}
/**
* Closes a listener bound to `socket` with the entry shielded from libuv's close-time unlink of the NAME. The
* shield moves whatever entry is there aside and puts that same entry back: inside the fence, so no adoption can
* land in between and be overwritten by the restore.
*/
export async function closeShieldedEndpoint(socket, close) {
await withEndpointFence(socket, () => shieldSocketDuringClose(socket, close));
}
/**
* A successor's launcher, after its attempt failed: revoke the publication grant, then read who owns the public
* name. An adoption that started inside its window but committed after the deadline is a COMPLETED handoff, never
* rolled back; one that did not commit before the revocation can no longer commit at all.
*/
export async function settleCandidateAdoption(socket, candidate, revoke) {
return withEndpointFence(socket, async () => {
await revoke();
return (await readEndpointOwner(socket))?.instanceId === candidate ? "completed" : "not_adopted";
});
}
//# sourceMappingURL=host-endpoint-owner.js.map