fix(agents): harden replay normalization guards

This commit is contained in:
FullerStackDev
2026-04-21 16:16:34 -06:00
committed by Ayaan Zaidi
parent 3105597f8f
commit f58dd0f9b6
2 changed files with 13 additions and 4 deletions

View File

@@ -40,6 +40,7 @@ import {
type AssistantUsageSnapshot,
type UsageLike,
} from "../usage.js";
import { log } from "./logger.js";
import { dropThinkingBlocks } from "./thinking.js";
const INTER_SESSION_PREFIX_BASE = "[Inter-session message]";
@@ -235,6 +236,12 @@ export function normalizeAssistantReplayContent(messages: AgentMessage[]): Agent
if (!message || message.role !== "assistant" || Array.isArray(message.content)) {
continue;
}
if (typeof message.content !== "string") {
log.warn(
`normalizeAssistantReplayContent: repairing malformed assistant content ` +
`(index=${i}, type=${typeof message.content})`,
);
}
out[i] = {
...(message as unknown as Record<string, unknown>),
content:

View File

@@ -1144,9 +1144,11 @@ export async function runEmbeddedAttempt(
throw new Error("Embedded agent session missing");
}
const activeSession = session;
const baseConvertToLlm = activeSession.agent.convertToLlm.bind(activeSession.agent);
activeSession.agent.convertToLlm = async (messages) =>
await baseConvertToLlm(normalizeAssistantReplayContent(messages));
if (typeof activeSession.agent.convertToLlm === "function") {
const baseConvertToLlm = activeSession.agent.convertToLlm.bind(activeSession.agent);
activeSession.agent.convertToLlm = async (messages) =>
await baseConvertToLlm(normalizeAssistantReplayContent(messages));
}
let prePromptMessageCount = activeSession.messages.length;
abortSessionForYield = () => {
yieldAbortSettled = Promise.resolve(activeSession.abort());
@@ -2193,7 +2195,7 @@ export async function runEmbeddedAttempt(
activeSession.agent.state.messages = normalizedReplayMessages;
}
finalPromptText = effectivePrompt;
const btwSnapshotMessages = activeSession.messages.slice(-MAX_BTW_SNAPSHOT_MESSAGES);
const btwSnapshotMessages = normalizedReplayMessages.slice(-MAX_BTW_SNAPSHOT_MESSAGES);
updateActiveEmbeddedRunSnapshot(params.sessionId, {
transcriptLeafId,
messages: btwSnapshotMessages,