@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
64 lines • 2.93 kB
JavaScript
import { engineWakeSource } from "./engine-turn-hold.js";
import { isRecord } from "./work-custody-record.js";
import { recoveryCarrier } from "./session-recovery-identity.js";
/** Keep delivery identities, not materialized conversation history, between engine turns. */
export const lifecycleDeliveryEntries = {
initial: () => new Map(),
add: (entries, entry) => {
if (entry.type === "custom_message") {
const ticket = messageTicket(entry);
if (ticket && !entries.has(ticket))
entries.set(ticket, entry.id);
}
return entries;
},
};
export function messageTicket(message) {
return isRecord(message.details) && typeof message.details.ticketId === "string"
? message.details.ticketId
: undefined;
}
/** Synchronous at message_start: transcript first, then the consuming run's custody. */
export function consumeLifecycleDelivery(session, controller, message) {
if (message.role !== "custom")
return undefined;
const ticketId = messageTicket(message);
if (!ticketId)
return undefined;
const custody = controller.obligations.active.find((record) => record.ticketId === ticketId);
if (!custody)
return undefined;
if (custody.state === "consumed")
return undefined;
const runId = controller.ledger.runId;
if (!runId)
return undefined;
const existing = session.sessionManager.reduceHistory(lifecycleDeliveryEntries).get(ticketId);
const transcriptEntryId = existing ??
session.sessionManager.appendCustomMessageEntry(message.customType, message.content, message.display, message.details, { recoveryOwner: recoveryCarrier(message) });
controller.obligations.consumed(ticketId, { runId, transcriptEntryId });
return transcriptEntryId;
}
/**
* #3860 currently returns void from note(). Capture its appended notice by
* ticket identity instead of introducing another held-message store.
*/
export function heldLifecycleNotice(session, ticketId, customType) {
for (const entry of session.sessionManager.getEntries().toReversed()) {
if (entry.type !== "custom" || entry.customType !== "session-notice" || !isRecord(entry.data))
continue;
if (isRecord(entry.data.message) &&
isRecord(entry.data.message.details) &&
entry.data.message.details.ticketId === ticketId &&
typeof entry.data.noticeId === "string")
return entry.data.noticeId;
// Goal notices deliberately store no pre-rendered message (#3860), so only a goal continuation may claim one.
if (engineWakeSource(customType) === "goal" &&
entry.data.producer === "goal" &&
entry.data.message === undefined &&
typeof entry.data.noticeId === "string")
return entry.data.noticeId;
}
return undefined;
}
//# sourceMappingURL=lifecycle-delivery.js.map