@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
168 lines • 7.25 kB
JavaScript
import { answer, reduceMutation } from "./pending-input-mutations.js";
/** The one transition function of a pending input (senpi#3549); it never performs I/O. */
export function reducePendingInput(current, event) {
switch (event.type) {
case "submit":
return submit(current, event);
case "edit":
case "remove":
case "retry":
return reduceMutation(current, event);
default:
return current === undefined ? unchanged(undefined) : reduceLifecycle(current, event);
}
}
function submit(current, event) {
// A steer refused for its turn was never queued, so its id carries no delivery to replay (senpi#5776).
const neverQueued = current?.phase === "removed" && current.removedBy === "turn_conflict";
if (current !== undefined && !neverQueued) {
if (current.fingerprint !== event.fingerprint) {
return { next: current, result: answer(current, "client_message_id_conflict"), effects: [] };
}
return {
next: current,
result: { ...answer(current, "replayed"), replayed: true, text: current.text },
effects: [],
};
}
const next = {
clientMessageId: event.clientMessageId,
fingerprint: event.fingerprint,
handle: event.handle,
revision: 1,
hasEdits: false,
text: event.text,
attachments: event.attachments,
requestedMode: event.requestedMode,
phase: "queued",
attemptCount: 0,
superseded: [],
operations: {},
};
return { next, result: answer(next, "admitted"), effects: [{ type: "persist" }] };
}
function reduceLifecycle(current, event) {
switch (event.type) {
case "reject_before_dispatch":
return current.phase === "queued" ? persist(fail(current, "not_delivered", event.reason)) : unchanged(current);
case "reserve": {
if (current.phase !== "queued" || !tokenFitsInput(current, event.token))
return unchanged(current);
const next = {
...current,
phase: "sending",
attempt: event.token,
attemptCount: current.attemptCount + 1,
};
return { next, effects: [{ type: "persist" }, { type: "dispatch", token: event.token }] };
}
case "release": {
if (!holdsAttempt(current, event.token))
return unchanged(current);
const { attempt: _attempt, ...rest } = current;
return persist({ ...rest, phase: "queued" });
}
case "consumed":
return holdsAttempt(current, event.token) ? persist(deliver(current, "consumed")) : unchanged(current);
case "handled":
if ("token" in event)
return holdsAttempt(current, event.token) ? persist(deliver(current, "handled")) : unchanged(current);
// Without a token the receipt must name the lifetime of the still-queued input it settles.
return current.phase === "queued" && event.lifetimeId === current.handle.lifetimeId
? persist(deliver(current, "handled"))
: unchanged(current);
case "refused":
return holdsAttempt(current, event.token)
? persist(fail(current, "not_delivered", event.reason))
: unchanged(current);
case "ambiguous":
return holdsAttempt(current, event.token)
? persist(fail(current, "uncertain", event.reason))
: unchanged(current);
case "owner_lost":
return holdsAttempt(current, event.token)
? persist(fail(current, "uncertain", "the delivering owner was lost"))
: unchanged(current);
case "witnessed":
return uncertainAbout(current, event.attemptId) ? persist(deliver(current, "consumed")) : unchanged(current);
case "non_delivery_proven":
return uncertainAbout(current, event.attemptId)
? persist(fail(current, "not_delivered", current.failure?.reason ?? ""))
: unchanged(current);
case "teardown":
return current.phase === "sending"
? persist(fail(current, "uncertain", "torn down during handoff"))
: unchanged(current);
case "cancel":
return cancel(current, event.cause, event.consumerFenced);
case "rebind":
return rebind(current, event.handle, event.attemptsUnsent === true);
}
}
function cancel(current, cause, consumerFenced) {
const removable = current.phase === "queued" || (current.phase === "sending" && consumerFenced);
if (!removable) {
return current.phase === "sending"
? persist(fail(current, "uncertain", "stopped before the handoff was confirmed"))
: unchanged(current);
}
const next = settled({ ...current, phase: "removed", removedBy: cause });
return {
next,
effects: cause === "stop"
? [{ type: "persist" }, { type: "restore_to_draft", text: next.text }]
: [{ type: "persist" }],
};
}
function rebind(current, handle, attemptsUnsent) {
if (current.phase === "delivered" || current.phase === "removed")
return unchanged(current);
const rebound = { ...current, handle, revision: 1 };
if (current.phase !== "sending")
return persist(rebound);
if (!attemptsUnsent)
return persist(fail(rebound, "uncertain", "the session reopened during handoff"));
const { attempt: _attempt, ...unsent } = rebound;
return persist({ ...unsent, phase: "queued" });
}
function tokenFitsInput(input, token) {
return token.lifetimeId === input.handle.lifetimeId && token.clientMessageId === input.clientMessageId;
}
function holdsAttempt(input, token) {
const attempt = input.attempt;
return (input.phase === "sending" &&
attempt !== undefined &&
attempt.attemptId === token.attemptId &&
attempt.ownerId === token.ownerId &&
attempt.writerGeneration === token.writerGeneration &&
tokenFitsInput(input, token));
}
function deliver(input, deliveredBy) {
return settled({ ...input, phase: "delivered", ...(deliveredBy === undefined ? {} : { deliveredBy }) });
}
function fail(input, delivery, reason) {
const { attempt, ...rest } = input;
const failedAttempt = attempt ?? input.failure?.attempt;
return {
...rest,
phase: "failed",
failure: { delivery, reason, ...(failedAttempt === undefined ? {} : { attempt: failedAttempt }) },
};
}
/** Late evidence only settles the uncertain failure of the exact attempt it names. */
function uncertainAbout(input, attemptId) {
return (input.phase === "failed" &&
input.failure?.delivery === "uncertain" &&
input.failure.attempt?.attemptId === attemptId);
}
function settled(input) {
const { attempt: _attempt, failure: _failure, ...rest } = input;
return rest;
}
function persist(next) {
return { next, effects: [{ type: "persist" }] };
}
function unchanged(current) {
return { next: current, effects: [] };
}
//# sourceMappingURL=pending-input.js.map