@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
140 lines • 5.16 kB
JavaScript
import { CredentialWait, SecretNotLeased, } from "./credentials.js";
const RELOADS_PER_STEP = 3;
const STABLE_HEAD_MS = 5000;
const REDACTED = "<redacted>";
const RETRYABLE = [
"stale_snapshot",
"epoch_mismatch",
"unresolved_field",
"wrap_missing",
"unavailable",
];
class SealedLease {
#values;
#onDispose;
constructor(values, onDispose) {
this.#values = values;
this.#onDispose = onDispose;
}
value(field) {
const bytes = this.#values?.get(field);
if (bytes === undefined)
throw new SecretNotLeased(field);
return new TextDecoder().decode(bytes);
}
[Symbol.dispose]() {
const values = this.#values;
if (values === undefined)
return;
this.#values = undefined;
for (const bytes of values.values())
bytes.fill(0);
this.#onDispose(this);
}
toJSON() {
return REDACTED;
}
toString() {
return REDACTED;
}
[Symbol.for("nodejs.util.inspect.custom")]() {
return REDACTED;
}
}
/** Per egress instance (custody design 3.4): its leases live on the instance, never in module scope. */
export class SealedCredentialSource {
#deps;
#live = new Set();
#generation = 0;
constructor(deps) {
this.#deps = deps;
}
async resolve(providerId, entryDigest, fields, scope) {
if (new Set(fields).size !== fields.length)
throw new TypeError("a sealed release names each field at most once");
const step = { digest: entryDigest, reloads: 0, noticed: false };
for (let attempt = 0; attempt < 2; attempt++) {
const generation = this.#generation;
const values = await this.#resolveValues(providerId, step, fields, scope);
if (generation === this.#generation) {
const lease = new SealedLease(values, (disposed) => this.#live.delete(disposed));
this.#live.add(lease);
return lease;
}
for (const bytes of values.values())
bytes.fill(0);
}
throw new CredentialWait("config-churn", true);
}
/** Disposes every live lease, and voids any lease still being resolved when the bump lands. */
bumpGeneration() {
this.#generation++;
for (const lease of [...this.#live])
lease[Symbol.dispose]();
}
/** One release attempt; `step` carries the reload count, notice latch and digest across bump retries (C.3). */
async #resolveValues(providerId, step, fields, scope) {
const signal = this.#deps.stepSignal(scope);
for (;;) {
signal.throwIfAborted();
const lease = await this.#deps.lease();
const result = await this.#release(providerId, step.digest, fields, scope, lease);
if (result.ok)
return this.#open(providerId, step.digest, fields, result.items, lease);
if (result.code !== "stale_snapshot")
throw new CredentialWait(result.code, RETRYABLE.some((code) => code === result.code));
if (step.reloads < RELOADS_PER_STEP) {
step.reloads++;
}
else {
// config-churn (C.3): every further stale retry re-enters the stable-head wait, never the immediate reloads.
if (!step.noticed) {
step.noticed = true;
this.#deps.onChurnNotice(scope);
}
await this.#waitForStableHead(signal);
}
step.digest = await this.#deps.reloadEntryDigest(providerId);
}
}
async #release(providerId, entryDigest, fields, scope, lease) {
return this.#deps.unsealer.release({
accountId: this.#deps.accountId,
providerId,
entryDigest,
fields,
lease,
recipientPk: this.#deps.recipientPk,
scope,
});
}
/** Returns once the head held still for STABLE_HEAD_MS; rejects when the step aborts first. */
async #waitForStableHead(signal) {
let previous = await this.#deps.readHead();
for (;;) {
await this.#deps.sleep(STABLE_HEAD_MS, signal);
const current = await this.#deps.readHead();
if (current === previous)
return;
previous = current;
}
}
async #open(providerId, entryDigest, fields, items, epochLease) {
if (items.length !== fields.length)
throw new CredentialWait("denied", false);
const values = new Map();
try {
for (const [index, field] of fields.entries()) {
const position = { accountId: this.#deps.accountId, providerId, entryDigest, field, lease: epochLease };
values.set(field, await this.#deps.openItem(items[index], position));
}
}
catch (error) {
for (const bytes of values.values())
bytes.fill(0);
throw error;
}
return values;
}
}
//# sourceMappingURL=sealed-source.js.map