fix(agents): skip compaction API call when session has no real messages (#36451)

Merged via squash.

Prepared head SHA: 52dd631789
Co-authored-by: Sid-Qin <201593046+Sid-Qin@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
This commit is contained in:
Sid
2026-03-06 03:40:25 +08:00
committed by GitHub
parent 60a6d11116
commit 6c0376145f
2 changed files with 16 additions and 0 deletions

View File

@@ -132,6 +132,10 @@ type CompactionMessageMetrics = {
contributors: Array<{ role: string; chars: number; tool?: string }>;
};
function hasRealConversationContent(msg: AgentMessage): boolean {
return msg.role === "user" || msg.role === "assistant" || msg.role === "toolResult";
}
function createCompactionDiagId(): string {
return `cmp-${Date.now().toString(36)}-${generateSecureToken(4)}`;
}
@@ -663,6 +667,17 @@ export async function compactEmbeddedPiSessionDirect(
);
}
if (!session.messages.some(hasRealConversationContent)) {
log.info(
`[compaction] skipping — no real conversation messages (sessionKey=${params.sessionKey ?? params.sessionId})`,
);
return {
ok: true,
compacted: false,
reason: "no real conversation messages",
};
}
const compactStartedAt = Date.now();
const result = await compactWithSafetyTimeout(() =>
session.compact(params.customInstructions),