Files
puter/src/backend/core/context.ts
T
404oopsandClaude Fable 5.1 627a5b2d5e fix: stop video generation timeouts and failures from paging (PUT-1620)
A video job that outlived its poll window, an SDK request that timed out,
or a Veo operation that finished with an error all reached the HTTP error
handler as plain Errors. Each became an unhandled 500 with critical
severity and paged on-call for what is the provider's pace or the
provider's fault.

Video providers now share one poll loop that gives up with a 504
`upstream_timeout`, treats a transient poll failure (timeout, dropped
connection, 408/429/5xx) as a missed poll rather than a failed job, and
stops polling with a 400 `client_aborted` when the caller disconnects, so
nothing is metered for a clip nobody will receive. The driver controller
exposes the disconnect as an `abortSignal` on the request context. The
window is ten minutes for every provider; Together and BytePlus move up
from five.

Failed jobs are classified: content-filter refusals become a 400
`bad_request` with `errorCode: moderation_flagged`, rejected parameters a
400 `upstream_bad_request`, and anything else a 502 `upstream_failed`,
each carrying the provider's own code. Veo's filtered output keeps
`disallowed_value` and gains the same `errorCode`. The sanitizer and
content-filter pattern move from the Replicate provider into a shared
util so image and video agree.

Status-less SDK connection timeouts are translated to a 504
`upstream_timeout` at the driver boundary, and the chat driver records
them per attempt so an all-timeout chain is a 504 and a mixed chain is
`upstream_failed` instead of an `internal_error` 500. The Together chat
client gets the same ten-minute request timeout as the other providers.

The OpenAI video provider is left alone beyond an import path: its API is
scheduled to shut down on 2026-09-24.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-03 16:47:16 +02:00

162 lines
5.3 KiB
TypeScript

/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { AsyncLocalStorage } from 'node:async_hooks';
import type { Request } from 'express';
import type { Actor } from './actor';
/**
* Per-request context with both typed well-known fields AND an open-ended
* key-value map for ad-hoc data. Common fields (`actor`, `req`) are typed for
* autocomplete / safety, while the generic `get`/`set` bag lets any code stash
* per-request values without threading them through function arguments.
*
* Backed by Node's `AsyncLocalStorage`, so the context propagates through
* async/await, timers, and microtasks automatically. The middleware
* (`createRequestContextMiddleware`) wraps each incoming request in a fresh
* context after the auth probe has populated `req.actor`.
*
* Usage:
*
* ```ts
* // read typed field
* const actor = Context.get('actor');
*
* // read the express request from anywhere
* const req = Context.get('req');
*
* // stash / read ad-hoc values
* Context.set('myService.txId', txId);
* const txId = Context.get('myService.txId');
* ```
*/
// -- Well-known typed keys -------------------------------------------
export interface KnownContextFields {
/** The authenticated actor, if one was resolved by the auth probe. */
actor: Actor | undefined;
/** The express request object for this request. */
req: Request;
/** A unique id for this request — useful for structured logging / tracing. */
requestId: string;
/**
* The driver name the caller addressed (set by DriverController for
* `/drivers/call` dispatch); drivers read it to pick a provider.
*/
driverName: string;
/**
* Aborts when the client disconnects before the response has finished (set
* by DriverController). Long-running drivers poll it so work nobody will
* receive stops early and is never metered.
*/
abortSignal: AbortSignal;
}
// -- Context store ---------------------------------------------------
interface ContextStore {
known: Partial<KnownContextFields>;
extra: Map<string, unknown>;
}
const als = new AsyncLocalStorage<ContextStore>();
// -- Public API ------------------------------------------------------
/**
* Static-style context accessor.
*
* Well-known keys (`actor`, `req`, `requestId`) return typed values. Any other
* string key hits the generic map and returns `unknown`.
*/
export class Context {
/**
* Get a value from the current request context.
*
* Well-known keys return typed values; arbitrary string keys return
* `unknown`. Returns `undefined` when called outside a request scope or
* when the key hasn't been set.
*/
/** Get the entire context store (no-arg form). */
static get(): ContextStore | undefined;
static get<K extends keyof KnownContextFields>(
key: K,
): KnownContextFields[K] | undefined;
static get(key: string): unknown;
static get(key?: string): unknown {
if (key === undefined) return als.getStore();
const store = als.getStore();
if (!store) return undefined;
if (key in store.known) {
return (store.known as Record<string, unknown>)[key];
}
return store.extra.get(key);
}
/**
* Set a value on the current request context.
*
* Well-known keys are type-checked; arbitrary keys accept `unknown`.
*/
static set<K extends keyof KnownContextFields>(
key: K,
value: KnownContextFields[K],
): void;
static set(key: string, value: unknown): void;
static set(key: string, value: unknown): void {
const store = als.getStore();
if (!store) {
throw new Error(
`Context.set('${key}', ...) called outside a request scope`,
);
}
if (key === 'actor' || key === 'req' || key === 'requestId') {
(store.known as Record<string, unknown>)[key] = value;
} else {
store.extra.set(key, value);
}
}
/**
* Returns the full context store, or `undefined` when called outside a
* request scope. Prefer `.get(key)` for individual lookups.
*/
static current(): ContextStore | undefined {
return als.getStore();
}
}
// -- Internal: used by the request-context middleware -----------------
/**
* Run `fn` inside a new context scope. Used by the request-context middleware
* to wrap the remainder of the middleware/handler chain.
*/
export const runWithContext = <T>(
initial: Partial<KnownContextFields>,
fn: () => T,
): T => {
const store: ContextStore = {
known: { ...initial },
extra: new Map(),
};
return als.run(store, fn);
};