@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
51 lines • 2.16 kB
JavaScript
export const ATTACH_TAKEOVER_RECORD = "attach.takeover";
const HOLDER_PREFIX = "attach.holder:";
const holderKey = (sessionId) => `${HOLDER_PREFIX}${JSON.stringify(sessionId)}`;
const isStoreArray = (value) => Array.isArray(value);
function decodeHolder(value) {
if (typeof value !== "object" || value === null || isStoreArray(value))
throw new Error("attach: a holder document is not an object");
const { takeoverId, sessionId, holder, generation } = value;
if (typeof takeoverId !== "string" ||
typeof sessionId !== "string" ||
typeof holder !== "string" ||
typeof generation !== "number" ||
!Number.isSafeInteger(generation) ||
generation < 1)
throw new Error("attach: a holder document is malformed");
return { takeoverId, sessionId, holder, generation };
}
const encodeHolder = (record) => ({ ...record });
/** All writers on one writable store epoch share its single admission line. */
export function createAttachLedger(options) {
const { store, epoch, admission } = options;
const holderOf = async (sessionId) => {
const value = await store.getDocument(holderKey(sessionId));
return value === undefined ? undefined : decodeHolder(value);
};
return {
holderOf,
takeOver: (sessionId, holder) => admission.admit(async () => {
const current = await holderOf(sessionId);
if (current?.holder === holder)
return current;
const next = {
takeoverId: store.mintId(),
sessionId,
holder,
generation: (current?.generation ?? 0) + 1,
};
await store.commit(epoch, [
{
op: "append",
id: `${ATTACH_TAKEOVER_RECORD}:${next.takeoverId}`,
kind: ATTACH_TAKEOVER_RECORD,
data: encodeHolder(next),
},
{ op: "put", key: holderKey(sessionId), data: encodeHolder(next) },
], () => { });
return next;
}),
};
}
//# sourceMappingURL=attach-ledger.js.map