@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
45 lines • 1.8 kB
JavaScript
export class MaterializerRefusal extends Error {
constructor(code) {
super(`materializer refused: ${code}`);
this.name = "MaterializerRefusal";
this.code = code;
}
}
/** Driven separately from RpcClient's synchronous listener; no unbounded queue or implicit retry. */
export class MaterializerConsumer {
constructor(adapter) {
this.adapter = adapter;
}
reset(identity) {
return this.adapter.reset(identity);
}
async accept(unit) {
// A stray control is a protocol error, including at a sequence already committed.
if (unit.kind === "control")
throw new MaterializerRefusal("stray_control");
if (!Number.isSafeInteger(unit.sequence) || unit.sequence < 1 || !Array.isArray(unit.controls))
throw new MaterializerRefusal("invalid_unit");
return this.deliver(unit, async () => unit);
}
/** Acquire the transaction before loading: duplicates neither begin nor decode/copy the shared payload.
* The loader reads/stages the immutable S13 lease; release follows durable content ownership, not this read.
*/
acceptEnvelope(envelope, load) {
return this.deliver(envelope, async () => ({ ...envelope, kind: "unit", record: await load() }));
}
async deliver(unit, load) {
await this.adapter.reset(unit);
if (unit.sequence <= (await this.adapter.frontier(unit)))
return "duplicate";
const transaction = await this.adapter.begin(unit);
try {
await transaction.apply(await load());
return await transaction.commit(unit.sequence);
}
catch (cause) {
await transaction.abort();
throw cause;
}
}
}
//# sourceMappingURL=materializer.js.map