fix(rectification): read-case keeps conversation memory with many candidates (BUG-1057)
T3 of TASK-rectification-grounding-20260927 (red line 3). The turn-decision read-case is the model's only conversation memory; with about 7+ candidates (9 in the public AA case) it exceeded 6 KB and cleared recent_turns and relevant_evidence_summary first. Candidates in the model-visible inference now carry time / score / status / cluster_range only (the last round names candidates by time), candidate_summary.candidates (a duplicate) is gone, and over budget the order is: drop cluster ranges → keep the best six candidates → shorten turns/evidence → clear them. Stored inference and receipts are untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017eEAG8HD3mm8gsKXgk8uU8
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
84eb23152a
commit
13d93020be
@@ -21,6 +21,15 @@ export const TURN_DECISION_MAX_BYTES = 6 * 1024;
|
||||
export const TURN_DECISION_RECENT_TURNS = 6;
|
||||
export const TURN_DECISION_TURN_TEXT_LIMIT = 400;
|
||||
export const TURN_DECISION_EVIDENCE_LIMIT = 6;
|
||||
/**
|
||||
* The only fields a candidate carries in the model-visible inference
|
||||
* (BUG-1057). `score` is the inference probability; the model must still call
|
||||
* it relative support, never a probability (Skill §9). Candidate ids, window
|
||||
* positions and raw posteriors stay in the stored receipt.
|
||||
*/
|
||||
export const TURN_DECISION_CANDIDATE_FIELDS = ["time", "score", "status", "cluster_range"] as const;
|
||||
/** When the payload is over budget, at most this many candidates survive (best first). */
|
||||
export const TURN_DECISION_BUDGET_CANDIDATES = 6;
|
||||
|
||||
export type ReadCaseProjection = "turn_decision" | "full_diagnostics";
|
||||
|
||||
@@ -59,6 +68,52 @@ function clipText(value: string | null | undefined, max: number): string | null
|
||||
return text.length <= max ? text : `${text.slice(0, max)}…`;
|
||||
}
|
||||
|
||||
function roundScore(value: unknown): number | null {
|
||||
return typeof value === "number" && Number.isFinite(value) ? Math.round(value * 1000) / 1000 : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Model-visible slice of `compactInferenceProjection` (BUG-1057): candidates
|
||||
* keep time / score / status / cluster_range only, and the last round names
|
||||
* candidates by time instead of by id. Everything else is unchanged. The
|
||||
* stored inference state and receipts are not touched.
|
||||
*/
|
||||
export function modelVisibleInference(
|
||||
compact: Record<string, unknown> | null | undefined,
|
||||
): Record<string, unknown> | null {
|
||||
if (!compact) return null;
|
||||
const rows = Array.isArray(compact.candidates) ? compact.candidates as Array<Record<string, unknown>> : [];
|
||||
const timeById = new Map<string, string>();
|
||||
for (const row of rows) {
|
||||
if (typeof row.id === "string" && typeof row.time === "string") timeById.set(row.id, row.time);
|
||||
}
|
||||
const candidates = rows.map((row) => ({
|
||||
time: row.time,
|
||||
score: roundScore(row.probability),
|
||||
status: row.status,
|
||||
cluster_range: row.cluster_range,
|
||||
}));
|
||||
const round = compact.last_inference_round && typeof compact.last_inference_round === "object"
|
||||
? compact.last_inference_round as Record<string, unknown>
|
||||
: null;
|
||||
const lastRound = round
|
||||
? {
|
||||
kind: round.kind,
|
||||
entropy_before: round.entropy_before,
|
||||
entropy_after: round.entropy_after,
|
||||
eliminated_times: (Array.isArray(round.eliminated_ids) ? round.eliminated_ids : [])
|
||||
.flatMap((id) => (typeof id === "string" && timeById.has(id) ? [timeById.get(id)!] : [])),
|
||||
score_deltas: Object.fromEntries(
|
||||
Object.entries(round.score_deltas && typeof round.score_deltas === "object"
|
||||
? round.score_deltas as Record<string, unknown>
|
||||
: {})
|
||||
.flatMap(([id, delta]) => (timeById.has(id) ? [[timeById.get(id)!, roundScore(delta)]] : [])),
|
||||
),
|
||||
}
|
||||
: null;
|
||||
return { ...compact, candidates, last_inference_round: lastRound };
|
||||
}
|
||||
|
||||
function looksLikeChoiceSchema(schema: Readonly<Record<string, unknown>> | null | undefined): boolean {
|
||||
if (!schema) return false;
|
||||
const choice = schema.choice;
|
||||
@@ -192,11 +247,6 @@ export function projectTurnDecision(
|
||||
const decision = decideFromDossier(dossier, {
|
||||
currentEvidenceFingerprint: evidenceLedgerFingerprint(dossier.evidence),
|
||||
});
|
||||
const candidates = (dossier.latestResult?.candidates ?? []).slice(0, 6).map((item) => ({
|
||||
time: item.time,
|
||||
relative_support: item.relativeSupport,
|
||||
rank: item.rank,
|
||||
}));
|
||||
const recentTurns = dossier.turns.slice(-TURN_DECISION_RECENT_TURNS).flatMap((turn) => {
|
||||
const text = clipText(turn.text, TURN_DECISION_TURN_TEXT_LIMIT);
|
||||
if (!text || (turn.status !== "completed" && !(turn.role === "user" && turn.status === "pending"))) {
|
||||
@@ -217,7 +267,7 @@ export function projectTurnDecision(
|
||||
const renderableQuestion = isRenderableChoiceQuestion(currentQuestion)
|
||||
? currentQuestion
|
||||
: null;
|
||||
const inferenceProjection = compactInferenceProjection(inference);
|
||||
const inferenceProjection = modelVisibleInference(compactInferenceProjection(inference));
|
||||
const payload: Record<string, unknown> = {
|
||||
projection: "turn_decision",
|
||||
case_id: dossier.case.caseId,
|
||||
@@ -230,7 +280,7 @@ export function projectTurnDecision(
|
||||
selection_allowed: decision.selectionAllowed,
|
||||
completion_status: decision.completionStatus,
|
||||
validated: decision.validated,
|
||||
candidates,
|
||||
// BUG-1057: the per-candidate list lives once, in inference.candidates.
|
||||
entropy: inference?.entropy ?? null,
|
||||
},
|
||||
inference: renderableQuestion || !inferenceProjection
|
||||
@@ -265,15 +315,57 @@ export function projectTurnDecision(
|
||||
return enforceTurnDecisionBudget(payload);
|
||||
}
|
||||
|
||||
function withInferenceCandidates(
|
||||
payload: Record<string, unknown>,
|
||||
shape: (candidates: Array<Record<string, unknown>>) => { candidates: Array<Record<string, unknown>>; omitted?: number },
|
||||
): Record<string, unknown> {
|
||||
const inference = payload.inference && typeof payload.inference === "object" && !Array.isArray(payload.inference)
|
||||
? payload.inference as Record<string, unknown>
|
||||
: null;
|
||||
if (!inference || !Array.isArray(inference.candidates)) return payload;
|
||||
const shaped = shape(inference.candidates as Array<Record<string, unknown>>);
|
||||
return {
|
||||
...payload,
|
||||
inference: {
|
||||
...inference,
|
||||
candidates: shaped.candidates,
|
||||
...(shaped.omitted ? { candidates_omitted: shaped.omitted } : {}),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Over budget, candidate detail goes first and the conversation memory last
|
||||
* (BUG-1057). The model gets no message history; `recent_turns` and
|
||||
* `relevant_evidence_summary` are its only memory of the conversation.
|
||||
* Order: drop each candidate's cluster_range → keep the best
|
||||
* TURN_DECISION_BUDGET_CANDIDATES candidates (active before eliminated) →
|
||||
* shorten turns/evidence → clear them (last resort, `truncated: true`).
|
||||
*/
|
||||
export function enforceTurnDecisionBudget(payload: Record<string, unknown>): Record<string, unknown> {
|
||||
if (utf8Bytes(payload) <= TURN_DECISION_MAX_BYTES) return payload;
|
||||
const noRanges = withInferenceCandidates(payload, (candidates) => ({
|
||||
candidates: candidates.map(({ cluster_range: _range, ...rest }) => rest),
|
||||
}));
|
||||
if (utf8Bytes(noRanges) <= TURN_DECISION_MAX_BYTES) return noRanges;
|
||||
const topCandidates = withInferenceCandidates(noRanges, (candidates) => {
|
||||
const ranked = [...candidates].sort((left, right) => {
|
||||
const leftActive = left.status === "eliminated" ? 1 : 0;
|
||||
const rightActive = right.status === "eliminated" ? 1 : 0;
|
||||
if (leftActive !== rightActive) return leftActive - rightActive;
|
||||
return (Number(right.score) || 0) - (Number(left.score) || 0);
|
||||
});
|
||||
const kept = ranked.slice(0, TURN_DECISION_BUDGET_CANDIDATES);
|
||||
return { candidates: kept, omitted: candidates.length - kept.length };
|
||||
});
|
||||
if (utf8Bytes(topCandidates) <= TURN_DECISION_MAX_BYTES) return topCandidates;
|
||||
const shrunk = {
|
||||
...payload,
|
||||
recent_turns: Array.isArray(payload.recent_turns)
|
||||
? (payload.recent_turns as unknown[]).slice(-4)
|
||||
...topCandidates,
|
||||
recent_turns: Array.isArray(topCandidates.recent_turns)
|
||||
? (topCandidates.recent_turns as unknown[]).slice(-4)
|
||||
: [],
|
||||
relevant_evidence_summary: Array.isArray(payload.relevant_evidence_summary)
|
||||
? (payload.relevant_evidence_summary as unknown[]).slice(-3)
|
||||
relevant_evidence_summary: Array.isArray(topCandidates.relevant_evidence_summary)
|
||||
? (topCandidates.relevant_evidence_summary as unknown[]).slice(-3)
|
||||
: [],
|
||||
};
|
||||
if (utf8Bytes(shrunk) <= TURN_DECISION_MAX_BYTES) return shrunk;
|
||||
|
||||
Reference in New Issue
Block a user