fix(consult): drop traces, budget checkpoints, silent summary inherit
BUG-729: dropped history rounds leave an omission marker in the model-visible summary slot. BUG-730: checkpoint threshold is 0.4 of historyBudgetChars (128k still 16,000). BUG-731: session_full new chat copies owned context_summary on the server; clients send only continued_from_session_id.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Agent } from "@mastra/core/agent";
|
||||
|
||||
import {
|
||||
CONSULTATION_HISTORY_TAIL_MAX_CHARS,
|
||||
consultationHistoryCheckpointChars,
|
||||
lastConsultationPair,
|
||||
parseSessionContextSummary,
|
||||
storedConsultationTurns,
|
||||
@@ -98,7 +98,7 @@ export function sanitizeSessionContextSummary(raw: string): string | null {
|
||||
export function tailCharCount(
|
||||
messages: unknown,
|
||||
summary: SessionContextSummaryV1 | null,
|
||||
options: { excludeRequestId?: string } = {},
|
||||
options: { excludeRequestId?: string; contextWindow?: number | null } = {},
|
||||
): number {
|
||||
const turns = storedConsultationTurns(messages, { excludeRequestId: options.excludeRequestId });
|
||||
const tail = summary
|
||||
@@ -110,9 +110,10 @@ export function tailCharCount(
|
||||
export function shouldCheckpoint(
|
||||
messages: unknown,
|
||||
summary: SessionContextSummaryV1 | null,
|
||||
options: { excludeRequestId?: string } = {},
|
||||
options: { excludeRequestId?: string; contextWindow?: number | null } = {},
|
||||
): boolean {
|
||||
return tailCharCount(messages, summary, options) > CONSULTATION_HISTORY_TAIL_MAX_CHARS;
|
||||
return tailCharCount(messages, summary, options)
|
||||
> consultationHistoryCheckpointChars(options.contextWindow);
|
||||
}
|
||||
|
||||
export function messagesForSummaryInput(
|
||||
@@ -215,6 +216,19 @@ export function nextSessionContextSummary(
|
||||
};
|
||||
}
|
||||
|
||||
export async function resolveInheritedContextSummary(input: {
|
||||
continuedFromSessionId?: string | null;
|
||||
loadOwnedSummary: (sessionId: string) => Promise<unknown>;
|
||||
}): Promise<SessionContextSummaryV1 | null> {
|
||||
const sourceId = input.continuedFromSessionId?.trim();
|
||||
if (!sourceId) return null;
|
||||
try {
|
||||
return parseSessionContextSummary(await input.loadOwnedSummary(sourceId));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeSessionContextSummary(input: {
|
||||
seenUpdatedAt: string | null;
|
||||
summary: SessionContextSummaryV1;
|
||||
@@ -228,13 +242,17 @@ export async function checkpointSessionContextSummary(input: {
|
||||
messages: unknown;
|
||||
summary: unknown;
|
||||
excludeRequestId?: string;
|
||||
contextWindow?: number | null;
|
||||
now?: () => Date;
|
||||
generateText: (prompt: string, signal?: AbortSignal) => Promise<string>;
|
||||
update: (summary: SessionContextSummaryV1, seenUpdatedAt: string | null) => Promise<boolean>;
|
||||
timeoutMs?: number;
|
||||
}): Promise<"written" | "skipped" | "abandoned" | "failed"> {
|
||||
const previous = parseSessionContextSummary(input.summary);
|
||||
if (!shouldCheckpoint(input.messages, previous, { excludeRequestId: input.excludeRequestId })) {
|
||||
if (!shouldCheckpoint(input.messages, previous, {
|
||||
excludeRequestId: input.excludeRequestId,
|
||||
contextWindow: input.contextWindow,
|
||||
})) {
|
||||
return "skipped";
|
||||
}
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user