The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeSqlite from "node:sqlite"; |
| 2 | |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import { assert, describe, it } from "@effect/vitest"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as FileSystem from "effect/FileSystem"; |
| 7 | import * as Path from "effect/Path"; |
| 8 | import type * as EffectAcpSchema from "effect-acp/compat"; |
| 9 | import type { OrchestrationV2ProviderFailure } from "@t3tools/contracts"; |
| 10 | |
| 11 | import { watchAntigravityPromptOutcome } from "./AntigravityTurnOutcome.ts"; |
| 12 | |
| 13 | const USER_INPUT = 14; |
| 14 | const PLANNER_RESPONSE = 15; |
| 15 | const TOOL = 8; |
| 16 | const ERROR_MESSAGE = 17; |
| 17 | |
| 18 | // Error-step payloads trimmed from Antigravity 1.4.5 against a local Gemini endpoint. |
| 19 | const errorPayload = (error: string) => |
| 20 | Buffer.concat([ |
| 21 | Buffer.from([0x0a, 0x24]), |
| 22 | Buffer.from( |
| 23 | `Agent execution terminated due to error.\u0012agent executor error: ${error}: ${error}\n(1) attached stack trace\n -- stack trace:`, |
| 24 | ), |
| 25 | ]); |
| 26 | // An unparsable reset timestamp must not hide the reset delay, nor a retry delay outrank it. |
| 27 | const QUOTA_ERROR = errorPayload( |
| 28 | "Error 429, Message: Quota exhausted for test model., Status: RESOURCE_EXHAUSTED, Details: [map[@type:type.googleapis.com/google.rpc.RetryInfo retryDelay:42s] map[@type:type.googleapis.com/google.rpc.ErrorInfo domain:googleapis.com metadata:map[quotaResetDelay:3600s quotaResetTimeStamp:soon] reason:QUOTA_EXHAUSTED]]", |
| 29 | ); |
| 30 | const RATE_LIMIT_ERROR = errorPayload( |
| 31 | "Error 429, Message: Resource has been exhausted (e.g. check quota)., Status: RESOURCE_EXHAUSTED, Details: [map[@type:type.googleapis.com/google.rpc.ErrorInfo domain:cloudcode-pa.googleapis.com reason:RATE_LIMIT_EXCEEDED] map[@type:type.googleapis.com/google.rpc.RetryInfo retryDelay:42s]]", |
| 32 | ); |
| 33 | const INVALID_ARGUMENT_ERROR = errorPayload( |
| 34 | "Error 400, Message: Request contains an invalid argument., Status: INVALID_ARGUMENT, Details: []", |
| 35 | ); |
| 36 | // Vertex Claude rejects a resumed history whose saved thinking block lost its signature. |
| 37 | const MISSING_SIGNATURE_ERROR = errorPayload( |
| 38 | 'Error 400, Message: {"type":"error","error":{"type":"invalid_request_error","message":"messages.1.content.0.thinking.signature: Field required"},"request_id":"req_vrtx_test"}, Status: INVALID_ARGUMENT, Details: []', |
| 39 | ); |
| 40 | const INVALID_SIGNATURE_ERROR = errorPayload( |
| 41 | 'Error 400, Message: {"type":"error","error":{"type":"invalid_request_error","message":"messages.3.content.0: Invalid `signature` in `thinking` block"}}, Status: INVALID_ARGUMENT, Details: []', |
| 42 | ); |
| 43 | const NOT_FOUND_ERROR = errorPayload( |
| 44 | "Error 404, Message: Requested entity was not found., Status: NOT_FOUND, Details: []", |
| 45 | ); |
| 46 | // Antigravity 1.5 with a Google account writes "STATUS (code NNN): message", then the HTTP body. |
| 47 | const STATUS_NOT_FOUND_ERROR = Buffer.concat([ |
| 48 | Buffer.from([0x12, 0x37]), |
| 49 | Buffer.from( |
| 50 | 'NOT_FOUND (code 404): Requested entity was not found.\u001a\u0080\u0001HTTP 404 Not Found\n\n{"error":{"code":404,"message":"Requested entity was not found.","status":"NOT_FOUND"}}', |
| 51 | ), |
| 52 | ]); |
| 53 | // The model's reply quoting the agent's quota notice is an ordinary planner response. |
| 54 | const QUOTED_NOTICE = Buffer.from( |
| 55 | "Usage Limit Reached\n\nYou have reached your current quota for this period. Your limit will reset in 1 hour.", |
| 56 | ); |
| 57 | const reusable = (failure: OrchestrationV2ProviderFailure) => |
| 58 | ({ failure, threadDisposition: "reusable" }) as const; |
| 59 | |
| 60 | describe("watchAntigravityPromptOutcome", () => { |
| 61 | it.effect("fails only turns whose own steps end in an upstream error", () => |
| 62 | Effect.gen(function* () { |
| 63 | const fs = yield* FileSystem.FileSystem; |
| 64 | const path = yield* Path.Path; |
| 65 | const profileDirectory = yield* fs.makeTempDirectoryScoped(); |
| 66 | const sessionId = "e6aba774-5472-4277-b6ee-27b2e05570a7"; |
| 67 | const conversations = path.join(profileDirectory, "antigravity-acp", "conversations"); |
| 68 | yield* fs.makeDirectory(conversations, { recursive: true }); |
| 69 | let nextIndex = 0; |
| 70 | const appendSteps = (steps: ReadonlyArray<readonly [number, Uint8Array]>) => { |
| 71 | const database = new NodeSqlite.DatabaseSync(path.join(conversations, `${sessionId}.db`)); |
| 72 | try { |
| 73 | database.exec( |
| 74 | "CREATE TABLE IF NOT EXISTS steps (idx INTEGER PRIMARY KEY, step_type INTEGER, step_payload BLOB)", |
| 75 | ); |
| 76 | const insert = database.prepare("INSERT INTO steps VALUES (?, ?, ?)"); |
| 77 | for (const [type, payload] of steps) insert.run(nextIndex++, type, payload); |
| 78 | } finally { |
| 79 | database.close(); |
| 80 | } |
| 81 | }; |
| 82 | const turn = ( |
| 83 | steps: ReadonlyArray<readonly [number, Uint8Array]>, |
| 84 | result: EffectAcpSchema.PromptResponse = { stopReason: "end_turn" }, |
| 85 | ) => |
| 86 | Effect.gen(function* () { |
| 87 | const check = yield* watchAntigravityPromptOutcome(profileDirectory, sessionId); |
| 88 | appendSteps(steps); |
| 89 | return yield* check(result); |
| 90 | }); |
| 91 | const input = Buffer.from("Run the fixture."); |
| 92 | const reply = Buffer.from("FIXTURE_OK"); |
| 93 | |
| 94 | // The first prompt creates the session log. |
| 95 | assert.isUndefined( |
| 96 | yield* turn([ |
| 97 | [USER_INPUT, input], |
| 98 | [PLANNER_RESPONSE, reply], |
| 99 | ]), |
| 100 | ); |
| 101 | assert.deepEqual( |
| 102 | yield* turn([ |
| 103 | [USER_INPUT, input], |
| 104 | [ERROR_MESSAGE, QUOTA_ERROR], |
| 105 | ]), |
| 106 | reusable({ |
| 107 | class: "usage_limit", |
| 108 | message: "You have reached your Antigravity quota for this period.", |
| 109 | code: "RESOURCE_EXHAUSTED", |
| 110 | retryable: true, |
| 111 | resetAt: "1970-01-01T01:00:00.000Z", |
| 112 | }), |
| 113 | ); |
| 114 | // The earlier error step is outside this turn. |
| 115 | assert.isUndefined( |
| 116 | yield* turn( |
| 117 | [ |
| 118 | [USER_INPUT, input], |
| 119 | [PLANNER_RESPONSE, QUOTED_NOTICE], |
| 120 | ], |
| 121 | { |
| 122 | stopReason: "end_turn", |
| 123 | usage: { inputTokens: 42, outputTokens: 3, totalTokens: 54 }, |
| 124 | }, |
| 125 | ), |
| 126 | ); |
| 127 | assert.deepEqual( |
| 128 | yield* turn([ |
| 129 | [USER_INPUT, input], |
| 130 | [ERROR_MESSAGE, RATE_LIMIT_ERROR], |
| 131 | ]), |
| 132 | reusable({ |
| 133 | class: "usage_limit", |
| 134 | message: "Resource has been exhausted (e.g. check quota).", |
| 135 | code: "RESOURCE_EXHAUSTED", |
| 136 | retryable: true, |
| 137 | resetAt: "1970-01-01T00:00:42.000Z", |
| 138 | }), |
| 139 | ); |
| 140 | assert.deepEqual( |
| 141 | yield* turn([ |
| 142 | [USER_INPUT, input], |
| 143 | [ERROR_MESSAGE, INVALID_ARGUMENT_ERROR], |
| 144 | ]), |
| 145 | reusable({ |
| 146 | class: "provider_error", |
| 147 | message: "Request contains an invalid argument.", |
| 148 | code: "INVALID_ARGUMENT", |
| 149 | retryable: false, |
| 150 | }), |
| 151 | ); |
| 152 | // A 404 after the turn already reached the model is a transient edge error, not a bad model. |
| 153 | for (const error of [NOT_FOUND_ERROR, STATUS_NOT_FOUND_ERROR]) { |
| 154 | assert.deepEqual( |
| 155 | yield* turn([ |
| 156 | [USER_INPUT, input], |
| 157 | [PLANNER_RESPONSE, reply], |
| 158 | [TOOL, reply], |
| 159 | [ERROR_MESSAGE, error], |
| 160 | ]), |
| 161 | { |
| 162 | ...reusable({ |
| 163 | class: "provider_error", |
| 164 | message: "Requested entity was not found.", |
| 165 | code: "NOT_FOUND", |
| 166 | retryable: true, |
| 167 | }), |
| 168 | resumeWith: "continue", |
| 169 | }, |
| 170 | ); |
| 171 | } |
| 172 | // A 404 before the model answered is an unknown or unentitled model. |
| 173 | assert.equal( |
| 174 | (yield* turn([ |
| 175 | [USER_INPUT, input], |
| 176 | [ERROR_MESSAGE, STATUS_NOT_FOUND_ERROR], |
| 177 | ]))?.failure.retryable, |
| 178 | false, |
| 179 | ); |
| 180 | // A saved thinking block without a valid signature fails every later prompt. |
| 181 | for (const error of [MISSING_SIGNATURE_ERROR, INVALID_SIGNATURE_ERROR]) { |
| 182 | const outcome = yield* turn([ |
| 183 | [USER_INPUT, input], |
| 184 | [ERROR_MESSAGE, error], |
| 185 | ]); |
| 186 | assert.equal(outcome?.failure.code, "INVALID_THINKING_SIGNATURE"); |
| 187 | assert.equal(outcome?.threadDisposition, "broken"); |
| 188 | } |
| 189 | // A completed tool keeps its usage, and the turn still fails. |
| 190 | assert.equal( |
| 191 | (yield* turn( |
| 192 | [ |
| 193 | [USER_INPUT, input], |
| 194 | [PLANNER_RESPONSE, reply], |
| 195 | [TOOL, reply], |
| 196 | [ERROR_MESSAGE, QUOTA_ERROR], |
| 197 | ], |
| 198 | { |
| 199 | stopReason: "end_turn", |
| 200 | usage: { inputTokens: 40, outputTokens: 5, totalTokens: 45 }, |
| 201 | }, |
| 202 | ))?.failure.class, |
| 203 | "usage_limit", |
| 204 | ); |
| 205 | assert.isUndefined( |
| 206 | yield* turn([ |
| 207 | [USER_INPUT, input], |
| 208 | [PLANNER_RESPONSE, reply], |
| 209 | ]), |
| 210 | ); |
| 211 | // The agent maps the SDK's QUOTA_EXHAUSTED stop reason to `refusal`. |
| 212 | assert.deepEqual( |
| 213 | yield* turn([], { stopReason: "refusal" }), |
| 214 | reusable({ |
| 215 | class: "usage_limit", |
| 216 | message: "You have reached your Antigravity quota for this period.", |
| 217 | code: "QUOTA_EXHAUSTED", |
| 218 | retryable: true, |
| 219 | }), |
| 220 | ); |
| 221 | }).pipe(Effect.provide(NodeServices.layer)), |
| 222 | ); |
| 223 | |
| 224 | it.effect("reads only this session's own unlinked log, and keeps a typed refusal", () => |
| 225 | Effect.gen(function* () { |
| 226 | const fs = yield* FileSystem.FileSystem; |
| 227 | const path = yield* Path.Path; |
| 228 | const profileDirectory = yield* fs.makeTempDirectoryScoped(); |
| 229 | const conversations = path.join(profileDirectory, "antigravity-acp", "conversations"); |
| 230 | yield* fs.makeDirectory(conversations, { recursive: true }); |
| 231 | const own = "e6aba774-5472-4277-b6ee-27b2e05570a7"; |
| 232 | const peer = "0d9c2b4e-1111-4222-8333-944455556666"; |
| 233 | const append = (sessionId: string, type: number, payload: Uint8Array) => { |
| 234 | const database = new NodeSqlite.DatabaseSync(path.join(conversations, `${sessionId}.db`)); |
| 235 | try { |
| 236 | database.exec( |
| 237 | "CREATE TABLE IF NOT EXISTS steps (idx INTEGER PRIMARY KEY, step_type INTEGER, step_payload BLOB)", |
| 238 | ); |
| 239 | database |
| 240 | .prepare("INSERT INTO steps (step_type, step_payload) VALUES (?, ?)") |
| 241 | .run(type, payload); |
| 242 | } finally { |
| 243 | database.close(); |
| 244 | } |
| 245 | }; |
| 246 | |
| 247 | // own.db links to a peer session; the peer's new error step is not this turn's. |
| 248 | append(peer, USER_INPUT, Buffer.from("peer")); |
| 249 | yield* fs.symlink( |
| 250 | path.join(conversations, `${peer}.db`), |
| 251 | path.join(conversations, `${own}.db`), |
| 252 | ); |
| 253 | const linked = yield* watchAntigravityPromptOutcome(profileDirectory, own); |
| 254 | append(peer, ERROR_MESSAGE, QUOTA_ERROR); |
| 255 | assert.isUndefined(yield* linked({ stopReason: "end_turn" })); |
| 256 | assert.equal((yield* linked({ stopReason: "refusal" }))?.failure.class, "usage_limit"); |
| 257 | |
| 258 | // An unreadable log gives no evidence, but the agent's typed refusal still fails the turn. |
| 259 | yield* fs.remove(path.join(conversations, `${own}.db`)); |
| 260 | yield* fs.writeFileString(path.join(conversations, `${own}.db`), "not a database"); |
| 261 | const corrupt = yield* watchAntigravityPromptOutcome(profileDirectory, own); |
| 262 | assert.isUndefined(yield* corrupt({ stopReason: "end_turn" })); |
| 263 | assert.deepEqual( |
| 264 | yield* corrupt({ stopReason: "refusal" }), |
| 265 | reusable({ |
| 266 | class: "usage_limit", |
| 267 | message: "You have reached your Antigravity quota for this period.", |
| 268 | code: "QUOTA_EXHAUSTED", |
| 269 | retryable: true, |
| 270 | }), |
| 271 | ); |
| 272 | }).pipe(Effect.provide(NodeServices.layer)), |
| 273 | ); |
| 274 | }); |