YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
feat(ai-gateway): allow trusted runner frontier jobs
Louis Beaumont2f878db6f69b
diff --git a/packages/ai-gateway/src/handlers/chat.ts b/packages/ai-gateway/src/handlers/chat.tsindex 332728fe3..7f205db13 100644--- a/packages/ai-gateway/src/handlers/chat.ts+++ b/packages/ai-gateway/src/handlers/chat.ts@@ -459,6 +459,7 @@ export async function handleChatCompletions(env: Env,latency: 'interactive' | 'background' = 'interactive',deviceId: string = '',+ allowFrontierBackground: boolean = false,): Promise<Response> {// A request with no messages at all can never complete: OpenAI would// answer the injected system hint below, and Anthropic 400s outright once@@ -474,7 +475,12 @@ export async function handleChatCompletions(// chain) by default, or hard-reject via PIPE_FRONTIER_POLICY=reject. The client// also hides frontier models from pipe presets; this is the worker backstop that// catches old pipes / custom integrations / the passthrough that slip through.- if (latency === 'background' && body.model !== 'auto' && isFrontierModel(body.model)) {+ if (+ latency === 'background' &&+ !allowFrontierBackground &&+ body.model !== 'auto' &&+ isFrontierModel(body.model)+ ) {if (String((env as any)?.PIPE_FRONTIER_POLICY ?? 'downgrade').toLowerCase() === 'reject') {return errorResponse(body, 403, `"${body.model}" (a frontier model) isn't available for scheduled pipes / background tasks. Use "auto" or a fast model (glm-5, gemini, sonnet, haiku).`);}diff --git a/packages/ai-gateway/src/index.ts b/packages/ai-gateway/src/index.tsindex 2a71e5116..7a2b584a8 100644--- a/packages/ai-gateway/src/index.ts+++ b/packages/ai-gateway/src/index.ts@@ -197,7 +197,13 @@ export async function handleRequest(request: Request, env: Env, ctx: ExecutionCo// (Date.now advances across the upstream fetch I/O) — ≈ TTFB for stream,// total for non-stream. Includes any router/embed overhead.const reqStart = Date.now();- let response = await handleChatCompletions(body, env, latency, authResult.deviceId);+ let response = await handleChatCompletions(+ body,+ env,+ latency,+ authResult.deviceId,+ authResult.service === true,+ );const latencyMs = Date.now() - reqStart;// Difficulty-router decision (null unless the router ran) for A/B measurement.const routerTier = response.headers.get('x-screenpipe-router-tier');diff --git a/packages/ai-gateway/src/types.ts b/packages/ai-gateway/src/types.tsindex 9559c0569..d712fc207 100644--- a/packages/ai-gateway/src/types.ts+++ b/packages/ai-gateway/src/types.ts@@ -140,6 +140,8 @@ export interface GeminiResponse {export interface Env {OPENAI_API_KEY: string;+ /** Runner-only bearer for trusted backend jobs. Stored as a Worker secret. */+ AI_GATEWAY_SERVICE_TOKEN?: string;OPENAI_REALTIME_TRANSCRIPTION_COST_PER_HOUR?: string;ANTHROPIC_API_KEY: string;DEEPGRAM_API_KEY: string;@@ -223,6 +225,8 @@ export interface AuthResult {tier: UserTier;deviceId: string;userId?: string;+ /** True only for the dedicated backend service bearer. */+ service?: boolean;error?: string;}diff --git a/packages/ai-gateway/src/utils/auth.test.ts b/packages/ai-gateway/src/utils/auth.test.tsindex fdfce3a3e..0559762d9 100644--- a/packages/ai-gateway/src/utils/auth.test.ts+++ b/packages/ai-gateway/src/utils/auth.test.ts@@ -82,6 +82,21 @@ describe('validateAuth — verified identities only', () => {});});+ it('authenticates the dedicated backend runner bearer without Clerk', async () => {+ const serviceEnv = {+ ...env,+ AI_GATEWAY_SERVICE_TOKEN: 'runner-service-secret',+ } as Env;++ expect(await validateAuth(requestFor('runner-service-secret'), serviceEnv)).toEqual({+ isValid: true,+ tier: 'subscribed',+ deviceId: 'device-from-header',+ service: true,+ });+ expect(verifyTokenMock).toHaveBeenCalledTimes(0);+ });+it('does not authenticate an arbitrary UUID, even if it names an account', async () => {const fetchMock = mock(async () => new Response(JSON.stringify([{ id: 'active-subscription' }])));globalThis.fetch = fetchMock as typeof fetch;diff --git a/packages/ai-gateway/src/utils/auth.ts b/packages/ai-gateway/src/utils/auth.tsindex fd5272e82..893b528bf 100644--- a/packages/ai-gateway/src/utils/auth.ts+++ b/packages/ai-gateway/src/utils/auth.ts@@ -73,6 +73,21 @@ export async function validateAuth(request: Request, env: Env): Promise<AuthResu};}+ // Dedicated cloud runners need a long-lived machine credential: Clerk+ // session JWTs expire quickly and provider keys must not be copied to+ // employee devices. This bearer lives only in the Worker secret store and+ // the org runner row. It receives subscribed limits and is explicitly+ // marked so the chat handler can allow a requested frontier model on the+ // otherwise-downgraded background lane.+ if (env.AI_GATEWAY_SERVICE_TOKEN && token === env.AI_GATEWAY_SERVICE_TOKEN) {+ return {+ isValid: true,+ tier: 'subscribed',+ deviceId: headerDeviceId,+ service: true,+ };+ }+// Authenticate the caller before trusting any user identifier. A Supabase// UUID or Clerk `user_*` ID names an account, but it is not proof that the// caller owns that account. Treating those public identifiers as bearer