|
2 | 2 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
3 | 3 | import type { ProviderRuntimeModel } from "../../../plugins/provider-runtime-model.types.js"; |
4 | 4 | import type { AgentRuntimePlan } from "../../runtime-plan/types.js"; |
5 | | -import { resolveAttemptTranscriptPolicy } from "./attempt.transcript-policy.js"; |
| 5 | +import { |
| 6 | + resolveAttemptTranscriptPolicy, |
| 7 | + shouldForceToolCallIdSanitization, |
| 8 | + shouldRetryWithForcedToolCallIdSanitization, |
| 9 | +} from "./attempt.transcript-policy.js"; |
6 | 10 |
|
7 | 11 | const resolveProviderRuntimePluginMock = vi.hoisted(() => vi.fn()); |
8 | 12 |
|
@@ -66,6 +70,106 @@ describe("resolveAttemptTranscriptPolicy", () => { |
66 | 70 | expect(resolvePolicy).toHaveBeenCalledWith(runtimePlanModelContext); |
67 | 71 | }); |
68 | 72 |
|
| 73 | + it("forces strict tool call id sanitization after a provider format retry", () => { |
| 74 | + const plannedPolicy = { |
| 75 | + sanitizeMode: "images-only", |
| 76 | + sanitizeToolCallIds: false, |
| 77 | + toolCallIdMode: "strict9", |
| 78 | + preserveNativeAnthropicToolUseIds: false, |
| 79 | + repairToolUseResultPairing: true, |
| 80 | + preserveSignatures: false, |
| 81 | + dropThinkingBlocks: false, |
| 82 | + applyGoogleTurnOrdering: false, |
| 83 | + validateGeminiTurns: false, |
| 84 | + validateAnthropicTurns: false, |
| 85 | + allowSyntheticToolResults: false, |
| 86 | + } as const; |
| 87 | + const runtimePlan = { |
| 88 | + transcript: { |
| 89 | + resolvePolicy: vi.fn(() => plannedPolicy), |
| 90 | + }, |
| 91 | + } as unknown as AgentRuntimePlan; |
| 92 | + |
| 93 | + const policy = resolveAttemptTranscriptPolicy({ |
| 94 | + runtimePlan, |
| 95 | + runtimePlanModelContext: { |
| 96 | + workspaceDir: ".", |
| 97 | + modelApi: "anthropic-messages", |
| 98 | + }, |
| 99 | + provider: "github-copilot", |
| 100 | + modelId: "claude-sonnet-4", |
| 101 | + forceToolCallIdSanitization: true, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(policy).toEqual({ |
| 105 | + ...plannedPolicy, |
| 106 | + sanitizeToolCallIds: true, |
| 107 | + toolCallIdMode: "strict", |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it("allows exactly one forced sanitization retry for a provider format error", () => { |
| 112 | + expect( |
| 113 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 114 | + cloudCodeAssistFormatError: true, |
| 115 | + errorMessage: "messages.17.content.0.tool_use.id: String should match pattern", |
| 116 | + modelApi: "anthropic-messages", |
| 117 | + }), |
| 118 | + ).toBe(true); |
| 119 | + expect( |
| 120 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 121 | + cloudCodeAssistFormatError: true, |
| 122 | + errorMessage: "tool call id was invalid and must match the required pattern", |
| 123 | + modelApi: "anthropic-messages", |
| 124 | + }), |
| 125 | + ).toBe(true); |
| 126 | + expect( |
| 127 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 128 | + cloudCodeAssistFormatError: true, |
| 129 | + errorMessage: "messages.17.content.0.tool_use.id: String should match pattern", |
| 130 | + forceToolCallIdSanitizationApi: "anthropic-messages", |
| 131 | + modelApi: "anthropic-messages", |
| 132 | + }), |
| 133 | + ).toBe(false); |
| 134 | + expect( |
| 135 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 136 | + cloudCodeAssistFormatError: true, |
| 137 | + errorMessage: "invalid request format", |
| 138 | + modelApi: "anthropic-messages", |
| 139 | + }), |
| 140 | + ).toBe(false); |
| 141 | + expect( |
| 142 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 143 | + cloudCodeAssistFormatError: true, |
| 144 | + errorMessage: "messages.17.content.0.tool_use.id: String should match pattern", |
| 145 | + modelApi: "openai-responses", |
| 146 | + }), |
| 147 | + ).toBe(false); |
| 148 | + expect( |
| 149 | + shouldRetryWithForcedToolCallIdSanitization({ |
| 150 | + cloudCodeAssistFormatError: true, |
| 151 | + errorMessage: "messages.17.content.0.tool_use.id: String should match pattern", |
| 152 | + forceToolCallIdSanitizationApi: "anthropic-messages", |
| 153 | + modelApi: "openai-responses", |
| 154 | + }), |
| 155 | + ).toBe(false); |
| 156 | + }); |
| 157 | + |
| 158 | + it("limits forced sanitization to the API that rejected the transcript", () => { |
| 159 | + expect( |
| 160 | + shouldForceToolCallIdSanitization({ |
| 161 | + forceToolCallIdSanitizationApi: "anthropic-messages", |
| 162 | + modelApi: "anthropic-messages", |
| 163 | + }), |
| 164 | + ).toBe(true); |
| 165 | + expect( |
| 166 | + shouldForceToolCallIdSanitization({ |
| 167 | + forceToolCallIdSanitizationApi: "anthropic-messages", |
| 168 | + modelApi: "openai-responses", |
| 169 | + }), |
| 170 | + ).toBe(false); |
| 171 | + }); |
| 172 | + |
69 | 173 | it("keeps the legacy provider transcript fallback when no RuntimePlan is available", () => { |
70 | 174 | // Legacy fallback remains for older runner paths and tests provider runtime |
71 | 175 | // plugin discovery with the same workspace/env context. |
|
0 commit comments