mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 09:47:40 +00:00
Agents: drop stale pre-compaction usage snapshots
This commit is contained in:
@@ -214,6 +214,35 @@ function annotateInterSessionUserMessages(messages: AgentMessage[]): AgentMessag
|
||||
return touched ? out : messages;
|
||||
}
|
||||
|
||||
function stripStaleAssistantUsageBeforeLatestCompaction(messages: AgentMessage[]): AgentMessage[] {
|
||||
let latestCompactionSummaryIndex = -1;
|
||||
for (let i = 0; i < messages.length; i += 1) {
|
||||
if (messages[i]?.role === "compactionSummary") {
|
||||
latestCompactionSummaryIndex = i;
|
||||
}
|
||||
}
|
||||
if (latestCompactionSummaryIndex <= 0) {
|
||||
return messages;
|
||||
}
|
||||
|
||||
const out = [...messages];
|
||||
let touched = false;
|
||||
for (let i = 0; i < latestCompactionSummaryIndex; i += 1) {
|
||||
const candidate = out[i] as (AgentMessage & { usage?: unknown }) | undefined;
|
||||
if (!candidate || candidate.role !== "assistant") {
|
||||
continue;
|
||||
}
|
||||
if (!candidate.usage || typeof candidate.usage !== "object") {
|
||||
continue;
|
||||
}
|
||||
const candidateRecord = candidate as unknown as Record<string, unknown>;
|
||||
const { usage: _droppedUsage, ...rest } = candidateRecord;
|
||||
out[i] = rest as unknown as AgentMessage;
|
||||
touched = true;
|
||||
}
|
||||
return touched ? out : messages;
|
||||
}
|
||||
|
||||
function findUnsupportedSchemaKeywords(schema: unknown, path: string): string[] {
|
||||
if (!schema || typeof schema !== "object") {
|
||||
return [];
|
||||
@@ -466,6 +495,8 @@ export async function sanitizeSessionHistory(params: {
|
||||
? sanitizeToolUseResultPairing(sanitizedToolCalls)
|
||||
: sanitizedToolCalls;
|
||||
const sanitizedToolResults = stripToolResultDetails(repairedTools);
|
||||
const sanitizedCompactionUsage =
|
||||
stripStaleAssistantUsageBeforeLatestCompaction(sanitizedToolResults);
|
||||
|
||||
const isOpenAIResponsesApi =
|
||||
params.modelApi === "openai-responses" || params.modelApi === "openai-codex-responses";
|
||||
@@ -480,8 +511,8 @@ export async function sanitizeSessionHistory(params: {
|
||||
})
|
||||
: false;
|
||||
const sanitizedOpenAI = isOpenAIResponsesApi
|
||||
? downgradeOpenAIReasoningBlocks(sanitizedToolResults)
|
||||
: sanitizedToolResults;
|
||||
? downgradeOpenAIReasoningBlocks(sanitizedCompactionUsage)
|
||||
: sanitizedCompactionUsage;
|
||||
|
||||
if (hasSnapshot && (!priorSnapshot || modelChanged)) {
|
||||
appendModelSnapshot(params.sessionManager, {
|
||||
|
||||
Reference in New Issue
Block a user