Repository navigation
refactor(session): extract safe recovery gate #929
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 1 commit
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
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,63 @@ | ||
| import type { RecoveryDecision } from "./types" | ||
|
|
||
| export type ReplaySafetyDecision = { | ||
| canReplay: boolean | ||
| recoveryMode: "replay" | "auto_replay_blocked" | "ask_user" | "offer_continue" | "stop" | ||
| blockedReason?: "safe_recovery_budget_exhausted" | RecoveryDecision["reason"] | ||
| attemptKind?: "safe_recovery_replay" | ||
| presentation: "default" | "safe_recovery" | "safe_recovery_failed" | ||
| } | ||
|
|
||
| export function evaluateReplaySafety(input: { | ||
| recovery: RecoveryDecision | ||
| safeRecoveryAttempt: number | ||
| }): ReplaySafetyDecision { | ||
| const safety = input.recovery | ||
|
|
||
| if (safety.recommendation === "auto_retry_once") { | ||
| const maxAttempts = safety.auto_retry?.max_attempts ?? 1 | ||
| if (input.safeRecoveryAttempt < maxAttempts) { | ||
| return { | ||
| canReplay: true, | ||
| recoveryMode: "replay", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery", | ||
| } | ||
| } | ||
| return { | ||
| canReplay: false, | ||
| recoveryMode: "auto_replay_blocked", | ||
| blockedReason: "safe_recovery_budget_exhausted", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery_failed", | ||
| } | ||
| } | ||
|
|
||
| if (safety.recommendation === "offer_continue") { | ||
| return { | ||
| canReplay: false, | ||
| recoveryMode: "offer_continue", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } | ||
|
|
||
| if ( | ||
| safety.recommendation === "ask_user_before_retry" || | ||
| safety.recommendation === "offer_resume_with_confirmation" | ||
| ) { | ||
| return { | ||
| canReplay: false, | ||
| recoveryMode: "ask_user", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| canReplay: false, | ||
| recoveryMode: "stop", | ||
| blockedReason: safety.reason, | ||
| presentation: "default", | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
packages/opencode/src/session/run-observability/boundary.ts
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
68 changes: 68 additions & 0 deletions
68
packages/opencode/test/session/run-incident-safety-gate.test.ts
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,68 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { RunIncident } from "../../src/session/run-incident" | ||
|
|
||
| const base = { | ||
| confidence: "high", | ||
| safety_scope: "visible_output_and_tool_side_effects", | ||
| } as const | ||
|
|
||
| describe("run incident safety gate", () => { | ||
| test("allows one automatic replay for safe recovery decisions", () => { | ||
| const decision = RunIncident.evaluateReplaySafety({ | ||
| recovery: { | ||
| ...base, | ||
| recommendation: "auto_retry_once", | ||
| reason: "reasoning_only_without_final_text_or_tool_activity", | ||
| auto_retry: { max_attempts: 1, backoff_ms: 1_000 }, | ||
| }, | ||
| safeRecoveryAttempt: 0, | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canReplay: true, | ||
| recoveryMode: "replay", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery", | ||
| }) | ||
| expect(decision.blockedReason).toBeUndefined() | ||
| }) | ||
|
|
||
| test("blocks automatic replay after the safe recovery budget is exhausted", () => { | ||
| const decision = RunIncident.evaluateReplaySafety({ | ||
| recovery: { | ||
| ...base, | ||
| recommendation: "auto_retry_once", | ||
| reason: "no_visible_output_or_tool_execution", | ||
| auto_retry: { max_attempts: 1, backoff_ms: 1_000 }, | ||
| }, | ||
| safeRecoveryAttempt: 1, | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canReplay: false, | ||
| recoveryMode: "auto_replay_blocked", | ||
| blockedReason: "safe_recovery_budget_exhausted", | ||
| attemptKind: "safe_recovery_replay", | ||
| presentation: "safe_recovery_failed", | ||
| }) | ||
| }) | ||
|
|
||
| test("keeps visible-output recovery as continuation instead of replay", () => { | ||
| const decision = RunIncident.evaluateReplaySafety({ | ||
| recovery: { | ||
| ...base, | ||
| recommendation: "offer_continue", | ||
| reason: "visible_output_without_tool_execution", | ||
| }, | ||
| safeRecoveryAttempt: 0, | ||
| }) | ||
|
|
||
| expect(decision).toMatchObject({ | ||
| canReplay: false, | ||
| recoveryMode: "offer_continue", | ||
| blockedReason: "visible_output_without_tool_execution", | ||
| presentation: "default", | ||
| }) | ||
| expect(decision.attemptKind).toBeUndefined() | ||
| }) | ||
| }) | ||
|
Astro-Han marked this conversation as resolved.
Resolved
|
||
20 changes: 20 additions & 0 deletions
20
packages/opencode/test/session/run-observability-boundary.test.ts
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,20 @@ | ||
| import { describe, expect, test } from "bun:test" | ||
| import { RunObservability } from "../../src/session/run-observability" | ||
|
|
||
| describe("run observability side-effect boundary", () => { | ||
| test("summarizes provider and external tool boundaries as incomplete proof", () => { | ||
| expect( | ||
| RunObservability.sideEffectBoundarySnapshot({ | ||
| providerSearch: { type: "provider" }, | ||
| externalLookup: { externalResult: true }, | ||
| unknownShape: "tool", | ||
| }), | ||
| ).toMatchObject({ | ||
| exposed_tool_count: 3, | ||
| provider_executed_capability_present: true, | ||
| external_boundary_present: true, | ||
| proof_result: "incomplete", | ||
| proof_reason: "provider_executed_capability", | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
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.