-
Notifications
You must be signed in to change notification settings - Fork 14
refactor(session): add unified retry decision metadata #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import type { RunIncident } from "./run-incident" | ||
| import type { RetryClassification } from "./retry-classification" | ||
|
|
||
| export type TechnicalRetryability = | ||
| | { | ||
| retryable: true | ||
| classification?: RetryClassification | ||
| message?: string | ||
| } | ||
| | { | ||
| retryable: false | ||
| classification?: RetryClassification | ||
| reason: "not_retryable" | "terminal_classification" | ||
| } | ||
|
|
||
| export type RetryAttemptKind = "provider_retry" | "safe_recovery_replay" | ||
| export type RecoveryMode = "replay" | "auto_replay_blocked" | "ask_user" | "offer_continue" | "stop" | ||
| export type RetryTimeoutPolicy = | ||
| | "default" | ||
| | "reasoning_global_protected" | ||
| | "reasoning_first_attempt" | ||
| | "reasoning_safe_recovery" | ||
| export type RetryPresentation = "default" | "safe_recovery" | "safe_recovery_failed" | ||
| export type RetryBlockedReason = | ||
| | "technical_not_retryable" | ||
| | "terminal_classification" | ||
| | "safe_recovery_budget_exhausted" | ||
| | RunIncident.Recovery["reason"] | ||
|
|
||
| export type ModelRetryDecision = { | ||
| technicalRetryability: TechnicalRetryability | ||
| safetyGateDecision: RunIncident.Recovery | ||
| canRetry: boolean | ||
| recoveryMode: RecoveryMode | ||
| blockedReason?: RetryBlockedReason | ||
| attemptKind?: RetryAttemptKind | ||
| modelStreamAttempt: number | ||
| safeRecoveryAttempt: number | ||
| timeoutPolicy: RetryTimeoutPolicy | ||
| presentation: RetryPresentation | ||
| } | ||
|
|
||
| export function selectRetryTimeoutPolicy(input: { | ||
| modelSupportsReasoning: boolean | ||
| explicitConnectTimeout: boolean | ||
| beforeProgressAutoRetryAllowed: boolean | ||
| safeRecoveryAttempt: number | ||
| }): RetryTimeoutPolicy { | ||
| if (input.explicitConnectTimeout) return "default" | ||
| if (!input.modelSupportsReasoning) return "default" | ||
| if (input.safeRecoveryAttempt > 0) return "reasoning_safe_recovery" | ||
| return input.beforeProgressAutoRetryAllowed ? "reasoning_first_attempt" : "reasoning_global_protected" | ||
| } | ||
|
|
||
| export function buildModelRetryDecision(input: { | ||
| technicalRetryability: TechnicalRetryability | ||
| safetyGateDecision: RunIncident.Recovery | ||
| modelStreamAttempt: number | ||
| safeRecoveryAttempt: number | ||
| timeoutPolicy: RetryTimeoutPolicy | ||
| }): ModelRetryDecision { | ||
| if (!input.technicalRetryability.retryable) { | ||
| return { | ||
| ...input, | ||
| canRetry: false, | ||
| recoveryMode: "stop", | ||
| blockedReason: | ||
| input.technicalRetryability.reason === "terminal_classification" | ||
| ? "terminal_classification" | ||
| : "technical_not_retryable", | ||
| presentation: "default", | ||
| } | ||
| } | ||
|
|
||
| const safety = input.safetyGateDecision | ||
| if (safety.recommendation === "auto_retry_once") { | ||
| const maxAttempts = safety.auto_retry?.max_attempts ?? 1 | ||
| if (input.safeRecoveryAttempt < maxAttempts) { | ||
| return { | ||
| ...input, | ||
| canRetry: true, | ||
| recoveryMode: "replay", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery", | ||
| } | ||
| } | ||
| return { | ||
| ...input, | ||
| canRetry: false, | ||
| recoveryMode: "auto_replay_blocked", | ||
| blockedReason: "safe_recovery_budget_exhausted", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery_failed", | ||
| } | ||
| } | ||
|
|
||
| if (safety.recommendation === "offer_continue") { | ||
| return { | ||
| ...input, | ||
| canRetry: false, | ||
| recoveryMode: "offer_continue", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| safety.recommendation === "ask_user_before_retry" || | ||
| safety.recommendation === "offer_resume_with_confirmation" | ||
| ) { | ||
| return { | ||
| ...input, | ||
| canRetry: false, | ||
| recoveryMode: "ask_user", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| ...input, | ||
| canRetry: false, | ||
| recoveryMode: "stop", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { buildModelRetryDecision, selectRetryTimeoutPolicy } from "../../src/session/retry-decision" | ||
| import type { RunIncident } from "../../src/session/run-incident" | ||
|
|
||
| const safeReplayGate: RunIncident.Recovery = { | ||
| recommendation: "auto_retry_once", | ||
| confidence: "high", | ||
| reason: "reasoning_only_without_final_text_or_tool_activity", | ||
| auto_retry: { max_attempts: 1, backoff_ms: 1_000 }, | ||
| safety_scope: "visible_output_and_tool_side_effects", | ||
| } | ||
|
|
||
| const visibleOutputGate: RunIncident.Recovery = { | ||
| recommendation: "offer_continue", | ||
| confidence: "high", | ||
| reason: "visible_output_without_tool_execution", | ||
| safety_scope: "visible_output_and_tool_side_effects", | ||
| } | ||
|
|
||
| const ambiguousToolGate: RunIncident.Recovery = { | ||
| recommendation: "ask_user_before_retry", | ||
| confidence: "high", | ||
| reason: "side_effect_facts_incomplete", | ||
| safety_scope: "visible_output_and_tool_side_effects", | ||
| } | ||
|
|
||
| describe("session.retry-decision", () => { | ||
| test("keeps technical retryability separate from safe recovery replay metadata", () => { | ||
| const decision = buildModelRetryDecision({ | ||
| technicalRetryability: { retryable: true, message: "Connection timed out" }, | ||
| safetyGateDecision: safeReplayGate, | ||
| modelStreamAttempt: 3, | ||
| safeRecoveryAttempt: 0, | ||
| timeoutPolicy: "reasoning_first_attempt", | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canRetry: true, | ||
| recoveryMode: "replay", | ||
| attemptKind: "safe_recovery_replay", | ||
| modelStreamAttempt: 3, | ||
| safeRecoveryAttempt: 0, | ||
| timeoutPolicy: "reasoning_first_attempt", | ||
| presentation: "safe_recovery", | ||
| }) | ||
| expect(decision.blockedReason).toBeUndefined() | ||
| expect(decision.technicalRetryability.retryable).toBe(true) | ||
| expect(decision.safetyGateDecision.reason).toBe("reasoning_only_without_final_text_or_tool_activity") | ||
| }) | ||
|
|
||
| test("blocks automatic replay when the safe recovery budget is exhausted", () => { | ||
| const decision = buildModelRetryDecision({ | ||
| technicalRetryability: { retryable: true, message: "Connection timed out" }, | ||
| safetyGateDecision: safeReplayGate, | ||
| modelStreamAttempt: 3, | ||
| safeRecoveryAttempt: 1, | ||
| timeoutPolicy: "reasoning_safe_recovery", | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canRetry: false, | ||
| recoveryMode: "auto_replay_blocked", | ||
| attemptKind: "safe_recovery_replay", | ||
| modelStreamAttempt: 3, | ||
| safeRecoveryAttempt: 1, | ||
| timeoutPolicy: "reasoning_safe_recovery", | ||
| presentation: "safe_recovery_failed", | ||
| blockedReason: "safe_recovery_budget_exhausted", | ||
| }) | ||
| }) | ||
|
|
||
| test("does not ask the safety gate to own terminal technical classification", () => { | ||
| const decision = buildModelRetryDecision({ | ||
| technicalRetryability: { retryable: false, reason: "terminal_classification" }, | ||
| safetyGateDecision: safeReplayGate, | ||
| modelStreamAttempt: 1, | ||
| safeRecoveryAttempt: 0, | ||
| timeoutPolicy: "default", | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canRetry: false, | ||
| recoveryMode: "stop", | ||
| blockedReason: "terminal_classification", | ||
| presentation: "default", | ||
| }) | ||
| expect(decision.safetyGateDecision).toBe(safeReplayGate) | ||
| }) | ||
|
|
||
| test("represents continuation offers without treating them as replay", () => { | ||
| const decision = buildModelRetryDecision({ | ||
| technicalRetryability: { retryable: true, message: "socket closed" }, | ||
| safetyGateDecision: visibleOutputGate, | ||
| modelStreamAttempt: 2, | ||
| safeRecoveryAttempt: 0, | ||
| timeoutPolicy: "default", | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canRetry: false, | ||
| recoveryMode: "offer_continue", | ||
| blockedReason: "visible_output_without_tool_execution", | ||
| presentation: "default", | ||
| }) | ||
| expect(decision.attemptKind).toBeUndefined() | ||
| }) | ||
|
|
||
| test("represents safety-confirmation gates without consuming the replay budget", () => { | ||
| const decision = buildModelRetryDecision({ | ||
| technicalRetryability: { retryable: true, message: "socket closed" }, | ||
| safetyGateDecision: ambiguousToolGate, | ||
| modelStreamAttempt: 2, | ||
| safeRecoveryAttempt: 0, | ||
| timeoutPolicy: "default", | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canRetry: false, | ||
| recoveryMode: "ask_user", | ||
| blockedReason: "side_effect_facts_incomplete", | ||
| safeRecoveryAttempt: 0, | ||
| presentation: "default", | ||
| }) | ||
| expect(decision.attemptKind).toBeUndefined() | ||
| }) | ||
|
|
||
| test("marks blocked-boundary reasoning first attempts as global protected timeout", () => { | ||
| const timeoutPolicy = selectRetryTimeoutPolicy({ | ||
| modelSupportsReasoning: true, | ||
| explicitConnectTimeout: false, | ||
| beforeProgressAutoRetryAllowed: false, | ||
| safeRecoveryAttempt: 0, | ||
| }) | ||
|
|
||
| expect(timeoutPolicy).toBe("reasoning_global_protected") | ||
| }) | ||
| }) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.