@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
80 lines • 3.94 kB
JavaScript
import { randomUUID } from "node:crypto";
import { correlatedRecord } from "./client-message-events.js";
import { CALL_ONLY_RECORDS, CALL_RECORDS } from "./compact-call-model.js";
import { COMPONENT_RECORDS } from "./compact-current-components.js";
/** Recovery kinds take a sequence; responses and every other record keep FIFO order with none. */
export const SEQUENCED = new Set([
"agent_start",
"agent_end",
"turn_start",
"turn_end",
"message_start",
"message_update",
"message_end",
"tool_execution_start",
"tool_execution_update",
"tool_execution_end",
"entry_appended",
"session_abort",
"transcript_write_failed",
...COMPONENT_RECORDS,
...CALL_RECORDS,
]);
export function field(record, key) {
return key in record ? Reflect.get(record, key) : undefined;
}
function turnScoped(type, record) {
if (type === "turn_end" || type.startsWith("tool_execution_"))
return true;
if (!type.startsWith("message_"))
return false;
const role = field(Object(field(record, "message")), "role");
return role === "assistant" || role === "toolResult";
}
/**
* The calls component folds first, at the sequence and turn this record would take: an ignored call-only record
* changes no component, takes no sequence, mints no turn, commits no correlation and publishes no root
* (RULING-13 Q9), so the lane commits nothing before it decides. Returns the place the record takes, or undefined
* when it is ignored. `folds` false: the record was refused at the protocol depth limit and folds no call.
*/
export function admitRecord(current, record, type, at, folds = true, callKey) {
const mintsTurn = type === "turn_start" || (at.turnId === null && turnScoped(type, record));
const turnId = mintsTurn ? randomUUID() : at.turnId;
const next = at.sequence + 1;
const call = folds && CALL_RECORDS.has(type) ? current.foldCall(record, next, turnId, callKey) : undefined;
if (call && !call.changed && CALL_ONLY_RECORDS.has(type))
return undefined;
return { sequence: next, turnId, call };
}
/** A change of the outer client correlation is its own transition, sequenced just before its record (C1.1). */
export function correlationChange(standing, turnId, record) {
// Within a turn, any identity-less correlated record other than turn_start (user input, an extension steer, a
// mid-turn session_abort, custom-role message_* records) keeps the standing correlation and gains no client identity
// of its own; only a new turn boundary ends it (ruling B).
const identified = "clientMessageId" in record || "clientTurnId" in record;
if (!identified && (standing === null || !correlatedRecord(record)))
return undefined;
if (!identified && turnId !== null && field(record, "type") !== "turn_start")
return undefined;
const next = {
clientMessageId: field(record, "clientMessageId"),
clientTurnId: field(record, "clientTurnId"),
clientMessages: field(record, "clientMessages"),
};
const current = standing;
const currentMessages = current?.clientMessages;
const nextMessages = next.clientMessages;
if (current !== null &&
current.clientMessageId === next.clientMessageId &&
current.clientTurnId === next.clientTurnId &&
(currentMessages === nextMessages ||
(Array.isArray(currentMessages) &&
Array.isArray(nextMessages) &&
currentMessages.length === nextMessages.length &&
currentMessages.every((identity, index) => field(Object(identity), "clientMessageId") ===
field(Object(nextMessages[index]), "clientMessageId") &&
field(Object(identity), "clientTurnId") === field(Object(nextMessages[index]), "clientTurnId")))))
return undefined;
return next;
}
//# sourceMappingURL=session-acceptance-admission.js.map