@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
48 lines • 1.99 kB
JavaScript
import { CompactAdmissionError } from "./compact-admission-error.js";
import { OPEN_AFTER_QUEUED_DEADLINE_MS } from "./rpc-request-deadline.js";
/** Join the operation or its epoch abort, releasing late results without publishing them. */
export function runAdmissionProducer(signal, produce, release) {
return new Promise((resolve, reject) => {
const abort = () => reject(signal.reason);
if (signal.aborted)
return abort();
signal.addEventListener("abort", abort, { once: true });
void produce()
.then(async (value) => {
if (signal.aborted)
await release(value);
else
resolve(value);
}, reject)
.finally(() => signal.removeEventListener("abort", abort))
.catch(() => {
// The caller already observed the abort; late cleanup cannot answer it again.
});
});
}
/** Abandoned producers and failed late releases are leak signals: counted and logged, never silent. */
export function recordProducerLoss(losses, kind, count, cause) {
if (count <= 0)
return;
losses[kind] += count;
const detail = cause === undefined ? "" : `: ${cause instanceof Error ? cause.message : String(cause)}`;
process.stderr.write(`senpi rpc admission producer ${kind} ${count}${detail}\n`);
}
/** One deadline per ending epoch, shared by all its bounded producers and duplicate teardown calls. */
export class AdmissionProducerLifetime {
constructor() {
this.controller = new AbortController();
this.signal = this.controller.signal;
}
end(abandoned) {
this.timer ??= setTimeout(() => {
abandoned();
this.controller.abort(new CompactAdmissionError("storage_unavailable"));
}, OPEN_AFTER_QUEUED_DEADLINE_MS);
this.timer.unref();
}
close() {
clearTimeout(this.timer);
}
}
//# sourceMappingURL=compact-admission-producer.js.map