@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
237 lines • 11.9 kB
JavaScript
import { createHash, randomUUID } from "node:crypto";
import { SessionPendingInputJournal } from "./pending-input-journal.js";
import { createSessionQueuedInputPort } from "./pending-input-session-port.js";
import { PENDING_INPUT_TRANSITION, PendingInputStore } from "./pending-input-store.js";
export class ClientMessageIdConflictError extends Error {
constructor(clientMessageId) {
super(`Client message id ${clientMessageId} was already used for a different message.`);
this.code = "client_message_id_conflict";
this.name = "ClientMessageIdConflictError";
this.clientMessageId = clientMessageId;
}
}
/** Whether the session history records any pending-input transition; folded without a full-history view (senpi#2537). */
const holdsPendingInputTransition = {
initial: () => false,
add: (held, entry) => held || (entry.type === "custom" && entry.customType === PENDING_INPUT_TRANSITION),
};
/**
* The pending inputs of one AgentSession (senpi#3549): the store, opened on the first identified input, and the
* agent queue port over it. Input without a client id never touches the store and keeps the legacy queue path.
*/
export class SessionPendingInputs {
constructor(options) {
this.lifetimeId = randomUUID();
this.attempts = 0;
/** Bumped synchronously by every clear, so a change can tell a clear it raced from a real divergence. */
this.clears = 0;
this.options = options;
// A session file that already holds pending inputs is recovered now: the agent's first drain must see them.
const journaled = options.sessionManager.reduceHistory(holdsPendingInputTransition);
// Opened eagerly, unawaited: every later use awaits this same opening and surfaces its failure, so the
// eager start only must not report it a second time as an unhandled rejection.
if (journaled)
this.store().catch(() => undefined);
}
/**
* How a restored input (from the RPC admission ledger after a reopen) re-enters the native queue: under the
* store's rebound queue id while it is still queued; not at all once the store retired it (delivered, removed,
* failed); `undefined` when the store never held it, so the caller restores it as before.
*/
async restoreHandle(clientMessageId) {
if (this.storeOpening === undefined)
return undefined;
const input = (await this.storeOpening).current(clientMessageId);
if (input === undefined)
return undefined;
return input.phase === "queued" ? { queuedId: input.handle.queuedId } : "retired";
}
/**
* Admits an identified input and, only when it is newly admitted, puts its native copy in the agent queue
* inside the same gated step, so no drain, edit or removal can run between the two. A repeated submit of the
* same input is answered from the record (`replayed`) and enqueues nothing.
*/
/**
* `refusal` is read before the input is recorded (a refusal there records nothing) and again in the same synchronous
* step as `enqueue`. A turn that ended while the record was written makes the second read refuse: the record is
* cancelled `turn_conflict`, which a later submit under the same id replaces, and the refusal is thrown.
*/
async admit(input, enqueue, refusal) {
const store = await this.store();
return store.transaction(async (tx) => {
const early = refusal?.();
if (early !== undefined)
throw early;
const result = await tx.submit({
clientMessageId: input.clientMessageId,
fingerprint: admissionFingerprint(input),
text: input.text,
attachments: (input.images ?? []).map(attachmentFingerprint),
requestedMode: input.mode,
});
if (result?.outcome === "client_message_id_conflict")
throw new ClientMessageIdConflictError(input.clientMessageId);
const admitted = tx.current(input.clientMessageId);
if (result?.outcome !== "admitted" || admitted === undefined)
return result;
const refused = refusal?.();
if (refused === undefined) {
enqueue(admitted.handle.queuedId);
return result;
}
await tx.apply(input.clientMessageId, { type: "cancel", cause: "turn_conflict", consumerFenced: true });
throw refused;
});
}
/**
* Edits or removes an identified input: the store transition is journaled first, and only when it answers
* `edited`/`removed` does `applyNative` change the native queue, inside the same gated step, so no drain
* can see one without the other. Returns undefined when the store never admitted this input.
*/
async change(request, applyNative) {
if (this.storeOpening === undefined)
return undefined;
const clearsAtStart = this.clears;
const store = await this.storeOpening;
return store.transaction(async (tx) => {
const current = tx.current(request.clientMessageId);
if (current === undefined)
return undefined;
const target = {
effectId: request.effectId ?? randomUUID(),
handle: current.handle,
expectedRevision: request.expectedRevision ?? current.revision,
};
const result = request.text === undefined
? await tx.apply(request.clientMessageId, { type: "remove", ...target })
: await tx.apply(request.clientMessageId, {
type: "edit",
...target,
text: request.text,
attachments: current.attachments,
});
const outcome = legacyOutcome(result?.outcome);
if (outcome !== "edited" && outcome !== "removed")
return outcome;
const native = applyNative();
// A synchronous clear can empty the queue while the change is being journaled; its own record of the
// clear is queued behind this step and retires the input, so the change simply found nothing. Any other
// missing native copy is a real divergence (review L1, L-c).
if (native === "not_found" && this.clears !== clearsAtStart)
return "not_found";
if (native !== outcome)
throw new PendingInputDivergenceError(request.clientMessageId, outcome, native);
return outcome;
});
}
/**
* Records an explicit clear (Stop, `clear_queue`, unload with clearQueue) of identified inputs that just left the
* native queue: each still-queued one is removed by `stop`, so a reopen never queues it again.
*/
async recordCleared(clientMessageIds) {
this.clears += 1;
if (this.storeOpening === undefined || clientMessageIds.length === 0)
return;
const store = await this.storeOpening;
// One record for the whole clear: it lands or not as a unit, never split around a later entry.
await store.transaction(async (tx) => {
const queued = clientMessageIds.filter((clientMessageId) => tx.current(clientMessageId)?.phase === "queued");
if (queued.length === 0)
return;
await tx.applyAll(queued.map((clientMessageId) => ({
clientMessageId,
event: { type: "cancel", cause: "stop", consumerFenced: true },
})));
});
}
/**
* Pending inputs (queued, sending, failed) and the outcome of each known id, consistent through
* `throughCursor`: apply it, then only updates with a later cursor. Absence from `inputs` is never an outcome.
*/
async snapshot(knownClientMessageIds = []) {
const store = this.storeOpening === undefined ? undefined : await this.storeOpening;
const inputs = (store?.snapshot() ?? []).filter((input) => input.state !== "delivered" && input.state !== "removed");
const outcomes = knownClientMessageIds.map((clientMessageId) => {
const read = store?.read(clientMessageId);
return read?.status === "admitted"
? { clientMessageId, status: "admitted", projection: read.projection }
: { clientMessageId, status: "not_admitted" };
});
return { lifetimeId: this.lifetimeId, throughCursor: store?.throughCursor ?? 0, inputs, outcomes };
}
/** The agent's queue port. Before the store opens nothing identified is queued, so it drains natively. */
queuedInputPort() {
return {
claim: async (queue, drain) => (await this.openPort())?.claim(queue, drain) ?? drain(),
commitConsumption: async (queue, messages) => (await this.openPort())?.commitConsumption(queue, messages),
release: async (queue, messages, giveBack) => {
const port = await this.openPort();
if (port === undefined)
giveBack.restore();
else
await port.release(queue, messages, giveBack);
},
};
}
store() {
this.storeOpening ??= PendingInputStore.open({
journal: this.options.journal ?? new SessionPendingInputJournal(this.options.sessionManager),
lifetimeId: this.lifetimeId,
nextQueuedId: this.options.nextQueuedId,
// The agent port commits consumption before any provider sees an input, so an attempt left `sending` by a
// crash was never sent and returns to the queue under its id (T28).
uncommittedAttemptsAreUnsent: true,
}).then((store) => {
const onChange = this.options.onChange;
if (onChange !== undefined)
store.subscribe((projections, cursor) => onChange({ lifetimeId: this.lifetimeId, cursor, changes: projections }));
return store;
});
return this.storeOpening;
}
async openPort() {
if (this.storeOpening === undefined)
return undefined;
const store = await this.storeOpening;
this.port ??= createSessionQueuedInputPort({
store,
ownerId: `${process.pid}:${this.lifetimeId}`,
writerGeneration: 1,
nextAttemptId: () => `${this.lifetimeId}:${++this.attempts}`,
});
return this.port;
}
}
function admissionFingerprint(input) {
const images = (input.images ?? []).map(attachmentFingerprint);
return sha256(JSON.stringify([input.mode, input.text, images]));
}
function attachmentFingerprint(image) {
return `${image.mimeType}:${sha256(image.data)}`;
}
function sha256(value) {
return createHash("sha256").update(value).digest("hex");
}
/** The store changed an input that the native queue no longer held: the two views disagree and must not be hidden. */
export class PendingInputDivergenceError extends Error {
constructor(clientMessageId, recorded, native) {
super(`Queued input ${clientMessageId} was ${recorded} in its record but ${native} in the queue.`);
this.code = "pending_input_divergence";
this.name = "PendingInputDivergenceError";
}
}
/** `sending` keeps the legacy spelling `already_delivered` (the handoff began); the snapshot says `sending`. */
function legacyOutcome(outcome) {
switch (outcome) {
case "edited":
case "removed":
case "conflict":
case "already_delivered":
return outcome;
case "sending":
return "already_delivered";
default:
return "not_found";
}
}
//# sourceMappingURL=pending-input-session.js.map