-
Notifications
You must be signed in to change notification settings - Fork 43
feat(cloud-agent): classify retryable runtime failures #3825
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
|
|
||
| import { ClientErrorSchema, PublicErrorCodeSchema } from './client-error.js'; | ||
|
|
||
| describe('ClientErrorSchema', () => { | ||
| it('accepts the public client error wire contract', () => { | ||
| expect( | ||
| ClientErrorSchema.parse({ | ||
| code: 'PENDING_QUEUE_FULL', | ||
| message: 'Queue is full', | ||
| retryable: true, | ||
| }) | ||
| ).toEqual({ | ||
| code: 'PENDING_QUEUE_FULL', | ||
| message: 'Queue is full', | ||
| retryable: true, | ||
| }); | ||
| }); | ||
|
|
||
| it.each(['', 'lowercase', '_PRIVATE', '9INVALID', 'HAS-DASH'])( | ||
| 'rejects invalid code %j', | ||
| code => { | ||
| expect(PublicErrorCodeSchema.safeParse(code).success).toBe(false); | ||
| } | ||
| ); | ||
| }); |
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,11 @@ | ||
| import { z } from 'zod'; | ||
|
|
||
| export const PublicErrorCodeSchema = z.string().regex(/^[A-Z][A-Z0-9_]*$/); | ||
|
|
||
| export const ClientErrorSchema = z.object({ | ||
| code: PublicErrorCodeSchema, | ||
| message: z.string(), | ||
| retryable: z.boolean(), | ||
| }); | ||
|
|
||
| export type ClientError = z.infer<typeof ClientErrorSchema>; |
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,64 @@ | ||
| import { Hono } from 'hono'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import type { HonoContext } from '../hono-context.js'; | ||
| import type { Env } from '../types.js'; | ||
|
|
||
| vi.mock('../auth.js', () => ({ | ||
| validateKiloToken: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../logger.js', () => { | ||
| const logger = { | ||
| setTags: vi.fn(), | ||
| info: vi.fn(), | ||
| warn: vi.fn(), | ||
| error: vi.fn(), | ||
| withFields: vi.fn(), | ||
| }; | ||
| logger.withFields.mockReturnValue(logger); | ||
| return { | ||
| logger, | ||
| withLogTags: async (_tags: unknown, fn: () => Promise<unknown>) => fn(), | ||
| WithLogTags: | ||
| () => | ||
| ( | ||
| _target: unknown, | ||
| _propertyKey: string, | ||
| descriptor: PropertyDescriptor | ||
| ): PropertyDescriptor => | ||
| descriptor, | ||
| }; | ||
| }); | ||
|
|
||
| const { authMiddleware } = await import('./auth.js'); | ||
| const { validateKiloToken } = await import('../auth.js'); | ||
|
|
||
| describe('authMiddleware', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(validateKiloToken).mockResolvedValue({ success: false, error: 'Invalid token' }); | ||
| }); | ||
|
|
||
| it('returns a non-retryable unauthorized client error without changing message or path', async () => { | ||
| const app = new Hono<HonoContext>(); | ||
| app.use('/trpc/*', authMiddleware); | ||
| app.post('/trpc/:procedure', c => c.json({ ok: true })); | ||
|
|
||
| const response = await app.fetch( | ||
| new Request('https://worker.test/trpc/send', { method: 'POST' }), | ||
| { NEXTAUTH_SECRET: 'secret' } as Env | ||
| ); | ||
| const body: any = await response.json(); | ||
|
|
||
| expect(response.status).toBe(401); | ||
| expect(body.error.message).toBe('Invalid token'); | ||
| expect(body.error.data).toMatchObject({ | ||
| path: 'send', | ||
| clientError: { | ||
| code: 'UNAUTHORIZED', | ||
| message: 'Invalid token', | ||
| retryable: false, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
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,95 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { TRPCError } from '@trpc/server'; | ||
| import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
|
|
||
| import type { TRPCContext } from '../types.js'; | ||
| import { t } from './auth.js'; | ||
|
|
||
| const testRouter = t.router({ | ||
| invalid: t.procedure.query(() => { | ||
| throw new TRPCError({ code: 'BAD_REQUEST', message: 'Invalid request' }); | ||
| }), | ||
| unavailable: t.procedure.query(() => { | ||
| throw new TRPCError({ | ||
| code: 'SERVICE_UNAVAILABLE', | ||
| message: 'Sandbox unavailable', | ||
| cause: { error: 'SANDBOX_CONNECT_FAILED', retryable: true }, | ||
| }); | ||
| }), | ||
| internal: t.procedure.query(() => { | ||
| throw new TRPCError({ code: 'INTERNAL_SERVER_ERROR', message: 'Internal failure' }); | ||
| }), | ||
| }); | ||
|
|
||
| async function requestProcedure(procedure: 'invalid' | 'unavailable' | 'internal') { | ||
| return fetchRequestHandler({ | ||
| endpoint: '/trpc', | ||
| req: new Request(`http://localhost/trpc/${procedure}`), | ||
| router: testRouter, | ||
| createContext: () => ({}) as TRPCContext, | ||
| }); | ||
| } | ||
|
|
||
| describe('tRPC client error formatter', () => { | ||
| it('adds a non-retryable client error to known request failures', async () => { | ||
| const response = await requestProcedure('invalid'); | ||
|
|
||
| await expect(response.json()).resolves.toMatchObject({ | ||
| error: { | ||
| message: 'Invalid request', | ||
| data: { | ||
| code: 'BAD_REQUEST', | ||
| httpStatus: 400, | ||
| path: 'invalid', | ||
| clientError: { | ||
| code: 'BAD_REQUEST', | ||
| message: 'Invalid request', | ||
| retryable: false, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('preserves explicit legacy retry fields beside the client error', async () => { | ||
| const response = await requestProcedure('unavailable'); | ||
|
|
||
| await expect(response.json()).resolves.toMatchObject({ | ||
| error: { | ||
| message: 'Sandbox unavailable', | ||
| data: { | ||
| code: 'SERVICE_UNAVAILABLE', | ||
| httpStatus: 503, | ||
| path: 'unavailable', | ||
| error: 'SANDBOX_CONNECT_FAILED', | ||
| retryable: true, | ||
| clientError: { | ||
| code: 'SANDBOX_CONNECT_FAILED', | ||
| message: 'Sandbox unavailable', | ||
| retryable: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('defaults generic internal failures to retryable', async () => { | ||
| const response = await requestProcedure('internal'); | ||
|
|
||
| await expect(response.json()).resolves.toMatchObject({ | ||
| error: { | ||
| message: 'Internal failure', | ||
| data: { | ||
| code: 'INTERNAL_SERVER_ERROR', | ||
| httpStatus: 500, | ||
| path: 'internal', | ||
| clientError: { | ||
| code: 'INTERNAL_SERVER_ERROR', | ||
| message: 'Internal failure', | ||
| retryable: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kilocode-bot Why was this removed? What replaced this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer:
'payment_required'was added, not removed. The+in the diff hunk means this is a new member being inserted into the union — nothing above or below it was removed.Why it was added
The DB enum is bound to the runtime types via a compile-time contract in
session-message-state.ts:This PR adds
payment_requiredin lockstep to all layers —packages/db/src/schema.ts(here),packages/worker-utils/src/cloud-agent-failure.ts, andsafe-failure-projection.ts. If it were added toCLOUD_AGENT_FAILURE_CODESbut not to the DB type, theAssertTruecontract would fail to compile. The schema addition is required for the Durable Object to carry a typedfailureCode: 'payment_required'at all.What it does
Previously, insufficient-credits failures were flattened into
assistant_error(indistinguishable from any generic error). This PR introduces a typed wrapper signal (WrapperTerminalFailureCodes = ['payment_required', 'model_missing']) so the wrapper can now communicate the precise failure, whichterminal-error-projector.tsclassifies as non-retryable — feeding the newclientError: { code, message, retryable }contract on callbacks andgetMessageResult.The subtlety
payment_requiredis intentionally not persisted to the DB column. Intelemetry/queue-reports.ts,persistedFailureCode()normalizes it away before writing tofailure_code:So if the concern is "this enum value will never actually be written to the DB column" — that's accurate and intentional by design. The value exists in
CloudAgentSessionRunFailureCodesolely to satisfy the shared contract the DO runtime depends on, not because the column is expected to store it.