fix: preserve failed responses when retrying model calls

Keep failed attempts in the saved transcript and omit only trailing failures from Retry's temporary agent history. Use the matching temporary prefix length when persisting genuinely new output.

Preserve the error notice across plan/compaction markers, refuse Retry after later compaction, and avoid synthesizing results for tool calls inside failed generations while retaining Pi's native filtering.

Verified: 173 focused Retry/session/compaction-projection/practice-record tests, typecheck, changed-file lint, and diff --check. No app launch or real provider calls.
This commit is contained in:
amosblomqvist
2026-10-27 16:49:29 +02:00
parent a572675900
commit 82c6999a63
7 changed files with 559 additions and 200 deletions
+42 -25
View File
@@ -16,9 +16,9 @@
* continue — run the next turn on the current record (mount
* continuations, job-settle re-syncs: the record ends at an
* answered question with no teacher reaction yet).
* retry — re-run a turn whose model call failed (issue #110): the
* trailing failed assistant message(s) leave the record and
* the turn re-runs from the input that preceded them.
* retry — retry a failed model response (issue #110), without new
* input. Failed attempts stay in the record; only the retry's
* temporary agent history omits the trailing failures.
*
* (The plan verdict input died 2026-09-26 with the verdict itself — the
* learner's post-plan reply is a natural text message.)
@@ -218,8 +218,8 @@ export function inputAppends(
case "continue":
return dismissalConclusion(record);
case "retry":
// The failed turn's own input already landed write-ahead; the
// handler trims the failure (retryDrops) — nothing new appends.
// The input already landed write-ahead. retryDrops affects only
// the temporary agent history — nothing new appends here.
return [];
case "thinking": {
const active = activeProblemOf(record.messages);
@@ -244,40 +244,57 @@ export function inputAppends(
}
}
/** Does the record end in a failed model call (a retryable turn)? */
export function endsInFailedTurn(messages: readonly LhMessage[]): boolean {
return failedTail(messages).ids.size > 0;
/** The failure owning the ephemeral banner. Plan presentation and
* compaction do not resolve an error; substantive later input/output does.
* This is deliberately broader than Retry eligibility below. */
export function failedResponseAtEnd(messages: readonly LhMessage[]): Extract<LhMessage, { role: "assistant" }> | undefined {
for (let i = messages.length - 1; i >= 0; i--) {
const m = messages[i]!;
if (m.role === "custom" && (m.customType === "plan-block" || m.customType === "compaction")) continue;
return m.role === "assistant" && m.stopReason === "error" ? m : undefined;
}
return undefined;
}
/** The ids a retry drops (issue #110, pure): the failed assistant
* message(s) ending the record. A provider failure persists as an
* assistant message with stopReason "error", and pi refuses to continue
* from an assistant — so neither re-answering (the question is no longer
* dangling) nor `continue` can resume the turn. Ids, not a cut index:
* UI-only markers may follow the failure, and they stay. Honest 400s
* when there is no failure to retry, or when dropping it leaves nothing
* to continue from. */
/** Does the record end in a failure with input that can be retried?
* Aborts are deliberate stops; compaction after a failure requires new
* learner input rather than a retry using a changed context. */
export function endsInFailedTurn(messages: readonly LhMessage[]): boolean {
const { ids, before } = failedTail(messages);
return ids.size > 0 && before !== undefined && before.role !== "assistant";
}
/** The ids omitted ONLY from the retry's temporary agent history.
* Failed attempts (including partial text and their ids) stay on disk.
* Pi's provider conversion already skips error/aborted responses, but
* continue() rejects an assistant tail BEFORE conversion. Omitting the
* trailing failures lets that continuation start from the saved input or
* completed tool results without replaying tools or resubmitting input.
* Plan blocks may follow the failure and stay; later compaction blocks
* Retry without changing compaction itself. */
export function retryDrops(messages: readonly LhMessage[]): Set<string> {
const { ids, before } = failedTail(messages);
if (ids.size === 0) throw new ChatInputError("retry input requires a failed turn to retry");
if (ids.size === 0) {
const failure = failedResponseAtEnd(messages);
if (failure) {
throw new ChatInputError(`${failure.errorMessage ?? "An error occurred."}\n\nThis response cannot be retried after compaction. Send a new message to continue.`);
}
throw new ChatInputError("retry input requires a failed turn to retry");
}
if (!before || before.role === "assistant") throw new ChatInputError("retry input leaves no input to continue from");
return ids;
}
/** Markers that can land AFTER a failed call and are never model input:
* persistTurn's plan block (plan_init succeeded, the next call failed)
* and the settle kick's compaction marker. */
const TRAILING_MARKERS: readonly string[] = ["plan-block", "compaction"];
/** Walk back from the end over failed assistants and trailing markers:
* the failures' ids, and the message the run stops at. */
/** Walk back over failed assistants and UI-only plan blocks. A compaction
* is a boundary, not a marker Retry may cross. A failure AFTER a compaction
* can still retry: that compacted context was already its starting state. */
function failedTail(messages: readonly LhMessage[]): { ids: Set<string>; before: LhMessage | undefined } {
const ids = new Set<string>();
let i = messages.length - 1;
for (; i >= 0; i--) {
const m = messages[i]!;
if (m.role === "assistant" && m.stopReason === "error") ids.add(m.id);
else if (!(m.role === "custom" && TRAILING_MARKERS.includes(m.customType))) break;
else if (!(m.role === "custom" && m.customType === "plan-block")) break;
}
return { ids, before: messages[i] };
}
+8 -6
View File
@@ -551,9 +551,10 @@ function resultIdsFrom(messages: readonly AgentMessage[]): Set<string> {
* - markers flatten to a user message carrying their content (the
* canonical text single-sourced in core/contracts); plan blocks are
* UI-only and filtered.
* - dangling tool calls (an aborted turn's unanswered questions) get the
* synthesized interrupted result — the provider protocol's hard
* invariant: a tool call must be followed by its result.
* - dangling calls from successfully generated assistant messages (for
* example, an unanswered question) get a synthesized interrupted result.
* Error/aborted generations do not: Pi drops those assistant messages,
* so manufacturing their results would leave orphan results on the wire.
*
* Contract (pi-agent-core): never throws — an unknown shape passes
* through untouched rather than killing the loop.
@@ -619,11 +620,12 @@ export function toLlmMessages(messages: readonly AgentMessage[]): Message[] {
// Strip the record id: the wire carries the native shape only.
const { id: _id, ...native } = m as LhMessage;
out.push(native as Message);
// Dangling-call synthesis: an assistant message whose toolCalls have
// no results in the remaining tail (abort mid-question) — the
// provider rejects unanswered calls outright.
// Repair unanswered calls from completed generations, not calls in
// failed generations. Pi filters error/aborted assistants itself;
// adding results for their unexecuted calls would leave orphans.
if (role === "assistant") {
const assistant = m as AssistantMessage;
if (assistant.stopReason === "error" || assistant.stopReason === "aborted") continue;
const calls = assistant.content.filter((b) => b.type === "toolCall");
if (calls.length === 0) continue;
const answered = resultIdsFrom(messages.slice(i + 1));