Compare commits

...

23 Commits

Author SHA1 Message Date
Mynacol
9b56692072 fix(executors): repair DuckDuckGo AI Chat challenge solver (418 ERR_CHALLENGE)
Every duckduckgo-web chat request failed with HTTP 418 ERR_CHALLENGE while
duck.ai worked normally in a browser from the same IP. Ground truth was
established by driving a real headful Chromium at duck.ai from that IP (it
returned 200), so the environment was never the problem — the anti-abuse
challenge solver was. Six independent defects were found; the first alone
disabled the solver completely.

1. Module syntax inside the vm sandbox source.
   CHALLENGE_STUBS is executed with vm.runInContext, which compiles in SCRIPT
   mode. A refactor mass-added `export` to the five `function` declarations
   inside that template literal (they read as ordinary top-level TS functions),
   so every solve threw SyntaxError. The executor swallows solve failures and
   posts the raw unsolved challenge, which upstream answers with 418.

2. Double-escaped regex in a String.raw template.
   `\\s` in __parseCssDisplay reached the sandbox as a literal backslash, so the
   display regex never matched and a getComputedStyle probe silently read empty.

3. buildHtmlLookup undercounted descendants by one.
   `count` backs el.querySelectorAll('*').length; that returns DESCENDANTS and
   countHtmlElements already skips the #document-fragment root, so the `- 1` was
   wrong. Chromium reports 3 for '<li><div></li><li></div'; we reported 2, and a
   variant multiplies innerHTML.length by that count.

4. Browser-fidelity probes.
   Newer challenge variants assert JS/DOM invariants a flat stub cannot satisfy:
   real prototype chains (HTMLDivElement -> HTMLElement -> Element), NodeList
   identity, a live body.children HTMLCollection, native-code toString, and
   sloppy-mode `this === window`. Nine of thirteen failed. Notably Math must NOT
   be sealed — Chromium reports Object.isSealed(Math) === false, and sealing it
   made our vector differ by one.

5. The solved payload dropped meta.origin / meta.stack / meta.duration.
   The duck.ai bundle always sends all three; captured browser requests confirm
   it. Without them upstream returns 418 even when every client_hash is correct.

6. reasoningEffort is now mandatory on duckchat/v1/chat.
   An otherwise byte-identical payload returns 200 with the field and 400
   ERR_BAD_REQUEST without it (A/B verified live, repeated).

Also removes the throwaway "seed" chat POST that ran before every real request.
It existed to coax a usable challenge out of the upstream while the solver was
broken; it only doubled chat calls against an IP-rate-limited endpoint, showing
up as spurious 429 ERR_RATE_LIMIT.

Verification: the solver now reproduces real Chromium's probe vectors exactly
for all 8 captured challenge variants, and the executor returns 200 end-to-end
live (non-streaming, streaming, claude-haiku-4-5, and a math prompt returning
"42").

Tests: tests/unit/duckduckgo-challenge-solver-regression.test.ts (32 tests) and
tests/unit/duckduckgo-reasoning-effort-required.test.ts (5 tests), backed by
tests/fixtures/duckduckgo/challenge-variants.json — real captured challenge
programs plus the probe vectors a real browser produced for them, so the suite
asserts against recorded browser behaviour rather than our own output. Each fix
was confirmed to fail its test when individually reverted.
2026-08-09 01:37:53 -03:00
Diego Rodrigues de Sa e Souza
aae408f585 Merge pull request #9296 from artickc/fix/adobe-firefly-model-capabilities
fix(adobe-firefly): sync discovered models and capabilities
2026-08-09 01:09:01 -03:00
diegosouzapw
3e1c31c606 fix(adobe-firefly): retain Topaz catalog models 2026-08-09 00:21:52 -03:00
diegosouzapw
2e12ee89f7 Merge remote-tracking branch 'origin/release/v3.8.50' into fix/merge-pr-9296
# Conflicts:
#	open-sse/config/imageRegistry.ts
#	open-sse/handlers/imageGeneration/providers/adobeFirefly.ts
#	open-sse/services/adobeFireflyClient.ts
#	tests/unit/adobe-firefly.test.ts
2026-08-09 00:06:06 -03:00
Diego Rodrigues de Sa e Souza
e0ce95c592 fix(ci): close remaining release-green gaps (#9835)
Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-08-08 23:24:11 -03:00
Diego Rodrigues de Sa e Souza
13dcbfd117 Merge pull request #9786 from diegosouzapw/fix/escalated-quality-validation-benign-error
fix(combo): ignore benign empty error fields in streaming quality validation (502 false-positive on opencode tool calls)
2026-08-08 22:40:51 -03:00
Diego Rodrigues de Sa e Souza
ec02945d97 Merge pull request #9820 from diegosouzapw/fix/release-v3.8.50-base-reds-9737-final
fix(tests): clear #9737 release gate blockers
2026-08-08 22:37:35 -03:00
diegosouzapw
cd121844ce fix(pack): retain isolated build runtime dependencies 2026-08-08 22:06:15 -03:00
diegosouzapw
8b29e73b27 Merge remote-tracking branch 'origin/release/v3.8.50' into fix/release-v3.8.50-base-reds-9737-final
# Conflicts:
#	tests/unit/adobe-firefly.test.ts
#	tests/unit/claude-code-rendering-fixes.test.ts
#	tests/unit/telemetry-auto-cleanup-6848.test.ts
#	tests/unit/triage-bugs-2026-08-02.test.ts
2026-08-08 21:45:42 -03:00
diegosouzapw
b7864cdb6c fix(ci): clear coverage pack and unit gate regressions 2026-08-08 21:33:11 -03:00
Diego Rodrigues de Sa e Souza
77cce62357 fix(ci): restore current release test integrity (#9819)
* fix(ci): restore base test integrity

* fix(quality): clear inherited fast gate drift

* fix(ci): clear inherited unit test reds

* fix(i18n): preserve Vietnamese catalog parity

* fix(i18n): preserve Portuguese catalog parity

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
2026-08-08 19:32:39 -03:00
diegosouzapw
c5f0ce01bc fix(tests): clear #9737 release gate blockers 2026-08-08 18:24:33 -03:00
Diego Rodrigues de Sa e Souza
00b79b5507 test(combo): guard auto/best-free never leaks the combo name as a model (#7754)
Refs: base-red #9737
2026-08-08 13:52:44 -03:00
Diego Rodrigues de Sa e Souza
b5e17bdbde fix(resilience): set rateLimitedUntil on connection test failure for recovery (#9623)
Refs: base-red #9737
2026-08-08 13:52:37 -03:00
Diego Rodrigues de Sa e Souza
aefa2b665b fix(playground): surface provider model loading errors in LlmChatCard (#9626)
Refs: base-red #9737
2026-08-08 13:52:30 -03:00
Diego Rodrigues de Sa e Souza
df1ea5bd77 fix(db): align domain_cost_history cleanup cutoff with millisecond column (#9625)
Refs: base-red #9737
2026-08-08 13:52:23 -03:00
Diego Rodrigues de Sa e Souza
a90c5e5aba fix(db): wire telemetry cleanup scheduler in Next.js startup path (#9624)
Refs: base-red #9737
2026-08-08 13:52:17 -03:00
Diego Rodrigues de Sa e Souza
c88b96244f fix(providers): classify 400 out of extra usage as quota_exhausted for Anthropic OAuth (#9486)
Refs: base-red #9737
2026-08-08 13:52:11 -03:00
Diego Rodrigues de Sa e Souza
93ee4dce9f fix(build): add build-next-isolated.mjs sibling imports to package.json files array (#9633)
Refs: base-red #9737
2026-08-08 13:52:04 -03:00
Diego Rodrigues de Sa e Souza
6c95e2b354 fix(build): include better-sqlite3 prebuilds in standalone bun bundle (#8847)
Refs: base-red #9737
2026-08-08 13:51:57 -03:00
Diego Rodrigues de Sa e Souza
3835f318d0 fix(cli): use process.execPath for macOS launchd autostart (#9156)
Refs: base-red #9737
2026-08-08 13:51:51 -03:00
diegosouzapw
69647b3b94 fix(combo): ignore benign empty error fields in streaming quality validation
isStreamingUpstreamError used a key-presence check (parsed.error != null)
which false-positives on benign values some backends emit on every chunk
({}, '', false, 0). When opencode issues a tool-call turn, the upstream SSE
opens with role-only frames (no recognized content) and a later chunk that
carries real tool_calls content PLUS a benign empty error field. The error
gate runs BEFORE content recognizers, so that single frame short-circuits
to 'error' -> 502 'streaming upstream error'. Same combo via kilocode works
because its wire format never emits the empty error field.

Fix: isSubstantiveError() helper — only treat error as real when it carries
non-empty string, non-empty object, or explicit true. Empty object {}, empty
string '', false, and 0 are benign.

TDD: tests/unit/quality-validation-benign-error.test.ts proves tool_calls
chunk with error:{} or error:'' is valid (was 502), while a real error
{message, code} still correctly fails.
2026-08-08 11:03:11 -03:00
artickc
723ce0b166 fix(adobe-firefly): sync models and media capabilities 2026-08-03 17:22:08 +03:00
68 changed files with 3494 additions and 1198 deletions

View File

@@ -52,8 +52,11 @@ export class ServerSupervisor {
// silently, so a boot that never becomes ready looked like a dead hang with zero
// output even at APP_LOG_LEVEL=debug. Pipe stdout too and buffer it alongside
// stderr so a readiness timeout can surface what the child actually printed.
// #9156: macOS launchd cannot resolve bare "node" because its PATH is
// minimal. Always use process.execPath (the absolute path to the running
// Node.js binary) so the supervisor never depends on PATH resolution.
this.child = spawn(
process.versions.bun ? process.execPath : "node",
process.execPath,
process.versions.bun
? [this.serverPath]
: buildNodeRuntimeArgs(process.env, this.memoryLimit, this.serverPath),

View File

@@ -0,0 +1 @@
- test(combo): guard auto/best-free never leaks the combo name as a model (#7754)

View File

@@ -0,0 +1 @@
- fix(build): include better-sqlite3 prebuilds in standalone bun bundle

View File

@@ -0,0 +1 @@
- fix(cli): use process.execPath for macOS launchd autostart

View File

@@ -0,0 +1 @@
- fix(providers): classify 400 out of extra usage as quota_exhausted for Anthropic OAuth

View File

@@ -0,0 +1 @@
- fix(resilience): failed connection test now sets a short cooldown so connections recover after transient outages (#9623)

View File

@@ -0,0 +1 @@
- fix(db): wire telemetry cleanup scheduler in Next.js startup path (#9624)

View File

@@ -0,0 +1 @@
- fix(db): align domain_cost_history cleanup cutoff with millisecond column (#9625)

View File

@@ -0,0 +1 @@
- fix(playground): surface provider model loading errors and offer retry (#9626)

View File

@@ -0,0 +1 @@
- fix(build): add build-next-isolated.mjs sibling imports to package.json files array

View File

@@ -1,4 +1,5 @@
{
"_rebaseline_2026_08_09_9296_adobe_media_capabilities": "PR #9296 (artickc, fix/adobe-firefly-model-capabilities) own growth: src/app/api/v1/models/catalog.ts 1590->1597 (+7). The image and video catalog serializers now expose the already-normalized Adobe Firefly discovery capability data (media_capabilities, plus the existing video modality/size fields) at their only response-emission chokepoints. The discovery parser and capability normalization remain in open-sse/services/adobeFireflyModels.ts; extracting these seven serialization fields would obscure the catalog contract. Covered by tests/unit/adobe-firefly.test.ts and tests/unit/image-upscale.test.ts.",
"_rebaseline_2026_07_24_8470_hyperagent_sticky_thread": "PR #8470 (artickc, fix/hyperagent-tool-loop-thread-sticky) own growth: open-sse/executors/hyperagent.ts 936->1025 (wc -l; check-file-size.mjs counts via split(\"\\n\").length so the gate sees 937->1026, +89, crosses the 1000 cap). Fixes a real bug where a reverse-conversion proxy (text-Intent/JSON to Claude Code native tool_calls) rewrites assistant messages between agentic tool-loop turns, breaking HyperAgents conversation-prefix fingerprint and cold-starting the thread mid tool-loop. Adds Anthropic tool_use/tool_result flattening to extractMessageText() plus a new rootUserFingerprint()/root-key lookup tier in resolveHyperAgentThreadBinding()/storeHyperAgentThreadAfterTurn() so the thread stays sticky across the tool loop. Cohesive additions inside the existing single-file executor; not extractable without splitting the executor mid-request-flow. Covered by tests/unit/executor-hyperagent.test.ts (19/19, +5 new cases for tool_result/tool_use flattening + root-key stickiness). Pre-merge review flagged a cross-conversation root-key collision risk (tracked in the PRs own mandatory pre-merge checklist, not yet addressed) — unrelated to this file-size ratchet, tracked separately by /fix-prs.",
"_rebaseline_2026_07_25_8494_capability_filter_fail_closed": "PR #8494 (fix/capability-filters-fail-closed, #8488) own growth: open-sse/services/combo.ts 3640->3693 (+53) adds a fail-closed guard after filterTargetsByRequestCompatibility() — when every eligible target is excluded by request-capability filtering (vision/tools/etc) instead of quota/health, the combo now returns an explicit `capability_mismatch` 400 (describeCapabilityFilterExhaustion, imported from combo/comboStructure.ts) rather than silently falling through to a generic no-targets error, plus a `compatFilterFailOpen` escape hatch (combo config OR settings) mirrored at both the main/auto and round-robin call sites for symmetry. combo/comboStructure.ts (previously under cap, un-frozen) grows 794->918 (+124) — new home for describeCapabilityFilterExhaustion + providerSupportsEmulatedToolCalling (#5240 emulated tool-calling exemption so fail-closed does not regress prompt-emulation-only combos like all-chatgpt-web). Irreducible orchestration wiring at the existing filter chokepoint (same precedent as #7301's universal-cooldown-retry generalization). Companion test tests/unit/combo-routing-engine.test.ts 3409->3449 (+40, fail-closed/fail-open coverage across both call sites) also rebaselined. Covered by tests/unit/8488-capability-filter-fail-closed.test.ts (new) + 95/95 passing across both files. Structural shrink of combo.ts tracked in #3501.",
"_rebaseline_2026_07_25_8499_ts7_result_union_predicates": "PR #8499 (backryun, chore/ts7-types-executor-scattered) own growth: muse-spark-web.ts 1396->1405 (+9, irreducible). Under this workspace's `strictNullChecks: false`, the boolean-literal discriminant on `GraphqlResult` (`{ ok: true } | { ok: false; error: string }`) narrows the positive `.ok===true` branch but leaves `!result.ok` at the full union under TS7, making `.error` unreachable to the checker at the two call sites (warmup, mode-switch). Fixed by adding a single `isGraphqlFailure()` type-predicate helper (doc comment + 3-line body) reused at both call sites instead of duplicating the predicate inline — not extractable to a shared module without splitting a single-file executor's local narrowing helper out of its own file. Covered by the existing muse-spark-web executor test suite (no behavior change, pure narrowing fix).",
@@ -387,7 +388,7 @@
"src/app/(dashboard)/dashboard/usage/components/EvalsTab.tsx": 2148,
"src/app/(dashboard)/dashboard/usage/components/ProviderLimits/index.tsx": 1119,
"src/app/api/providers/[id]/models/route.ts": 2361,
"src/app/api/v1/models/catalog.ts": 1590,
"src/app/api/v1/models/catalog.ts": 1597,
"src/lib/db/apiKeys.ts": 1529,
"src/lib/db/core.ts": 1639,
"src/lib/db/migrationRunner.ts": 1094,
@@ -536,7 +537,7 @@
"src/app/(dashboard)/dashboard/usage/components/EvalsTab.tsx": "2148",
"src/app/(dashboard)/dashboard/usage/components/ProviderLimits/index.tsx": "1119",
"src/app/api/providers/[id]/models/route.ts": "2361",
"src/app/api/v1/models/catalog.ts": "1590",
"src/app/api/v1/models/catalog.ts": "1597",
"src/lib/tokenHealthCheck.ts": "1053",
"src/lib/db/apiKeys.ts": "1529",
"src/lib/db/core.ts": "1639",

View File

@@ -149,6 +149,18 @@ export const ERROR_RULES: ErrorRule[] = [
backoff: true,
reason: "quota_exhausted",
},
{
id: "out_of_extra_usage",
text: "out of extra usage",
backoff: true,
reason: "quota_exhausted",
},
{
id: "extra_usage_required",
text: "extra usage required",
backoff: true,
reason: "quota_exhausted",
},
{ id: "capacity", text: "capacity", backoff: true, reason: "model_capacity" },
{ id: "overloaded", text: "overloaded", backoff: true, reason: "model_capacity" },
{ id: "high_demand", text: "high demand", backoff: true, reason: "model_capacity" },

View File

@@ -12,6 +12,10 @@ import { FREEPIK_IMAGE_PROVIDER } from "./providers/registry/freepik/index.ts";
import { STABILITY_AI_IMAGE_MODELS } from "./providers/registry/stability-ai/imageModels.ts";
import { GEMINI_IMAGEN_PROVIDER } from "./providers/registry/gemini/imageModels.ts";
import { CHEAPERINFERENCE_IMAGE_PROVIDER } from "./providers/registry/cheaperinference/imageModels.ts";
import {
ADOBE_FIREFLY_IMAGE_ROUTING_ALIASES,
toRegistryImageModels,
} from "../services/adobeFireflyModels.ts";
interface ImageModelEntry {
id: string;
@@ -22,6 +26,8 @@ interface ImageModelEntry {
imageRequired?: boolean;
description?: string;
isMarket?: boolean;
supportedSizes?: string[];
mediaCapabilities?: Record<string, unknown>;
}
interface ImageProviderConfig {
@@ -35,6 +41,7 @@ interface ImageProviderConfig {
authHeader: string;
format: string;
models: ImageModelEntry[];
routingAliases?: readonly string[];
supportedSizes: string[];
}
@@ -46,6 +53,7 @@ interface ImageModelAliasEntry {
inputModalities?: string[];
imageRequired?: boolean;
description?: string;
mediaCapabilities?: Record<string, unknown>;
}
interface ImageCatalogModelEntry {
@@ -55,6 +63,7 @@ interface ImageCatalogModelEntry {
supportedSizes: string[];
inputModalities: string[];
description?: string;
mediaCapabilities?: Record<string, unknown>;
}
const IMAGE_MODEL_ALIASES: Record<string, ImageModelAliasEntry> = {
@@ -678,55 +687,9 @@ export const IMAGE_PROVIDERS: Record<string, ImageProviderConfig> = {
authType: "apikey",
authHeader: "bearer",
format: "adobe-firefly-image",
models: [
{
id: "nano-banana-pro",
name: "Firefly Gemini 3.0 (Nano Banana Pro)",
inputModalities: ["text", "image"],
},
{
id: "nano-banana",
name: "Firefly Gemini 2.5 (Nano Banana)",
inputModalities: ["text", "image"],
},
{
id: "nano-banana-2",
name: "Firefly Gemini 3.1 (Nano Banana 2)",
inputModalities: ["text", "image"],
},
{ id: "gpt-image-2", name: "Firefly GPT Image 2", inputModalities: ["text", "image"] },
{ id: "gpt-image", name: "Firefly GPT Image 2", inputModalities: ["text", "image"] },
{ id: "gpt-image-1.5", name: "Firefly GPT Image 1.5", inputModalities: ["text", "image"] },
{ id: "flux-2", name: "Firefly Flux 2", inputModalities: ["text", "image"] },
{ id: "flux-pro", name: "Firefly Flux 1.1 Pro", inputModalities: ["text", "image"] },
{ id: "flux-ultra", name: "Firefly Flux 1.1 Ultra", inputModalities: ["text", "image"] },
{ id: "seedream-4", name: "Firefly Seedream 4.0", inputModalities: ["text", "image"] },
{
id: "seedream-5-lite",
name: "Firefly Seedream 5.0 Lite",
inputModalities: ["text", "image"],
},
{
id: "runway-gen4-image",
name: "Firefly Runway Gen-4 Image",
inputModalities: ["text", "image"],
},
// Topaz Labs upscalers (inputMediaUseCase: ["upscaling"]).
// Served by firefly-3p /v2/3p-images/upsample — see config/upscaleRegistry.ts.
{
id: "topaz-standard",
name: "Firefly Topaz Upscale (Standard)",
inputModalities: ["image"],
imageRequired: true,
},
{
id: "topaz-bloom",
name: "Firefly Topaz Bloom (Creative Upscale)",
inputModalities: ["image"],
imageRequired: true,
},
],
supportedSizes: ["1:1", "16:9", "9:16", "4:3", "3:4", "1024x1024", "1792x1024", "1024x1792"],
models: toRegistryImageModels(),
routingAliases: ADOBE_FIREFLY_IMAGE_ROUTING_ALIASES,
supportedSizes: [],
},
// Cheaper Inference (OSS-sponsor gateway). Declared AFTER adobe-firefly on
@@ -887,7 +850,7 @@ export function parseImageModel(modelStr) {
// No provider prefix — try to find the model in every provider
for (const [providerId, config] of Object.entries(IMAGE_PROVIDERS)) {
if (config.models.some((m) => m.id === modelStr)) {
if (config.routingAliases?.includes(modelStr) || config.models.some((m) => m.id === modelStr)) {
return { provider: providerId, model: modelStr };
}
}
@@ -906,9 +869,10 @@ function imageProviderCatalogEntries(
id: `${providerId}/${model.id}`,
name: model.name,
provider: providerId,
supportedSizes: config.supportedSizes,
supportedSizes: model.supportedSizes || config.supportedSizes,
inputModalities: model.inputModalities || ["text"],
description: model.description || undefined,
mediaCapabilities: model.mediaCapabilities,
}));
}

View File

@@ -5,14 +5,17 @@
* Supports local providers plus hosted task-based APIs such as Runway.
*/
import { parseModelFromRegistry, getAllModelsFromRegistry } from "./registryUtils.ts";
import { parseModelFromRegistry } from "./registryUtils.ts";
import { RUNWAYML_SUPPORTED_VIDEO_MODELS } from "./runway.ts";
import { SEGMIND_VIDEO_MODELS } from "./providers/registry/segmind/videoModels.ts";
import { toRegistryVideoModels } from "../services/adobeFireflyModels.ts";
interface VideoModel {
id: string;
name: string;
isMarket?: boolean;
supportedSizes?: string[];
mediaCapabilities?: Record<string, unknown>;
}
interface VideoProvider {
@@ -326,8 +329,7 @@ export const VIDEO_PROVIDERS: Record<string, VideoProvider> = {
},
// Adobe Firefly (unofficial) — same IMS/cookie credential as the image entry.
// Async 3P video generate + poll (Sora 2, Veo 3.1, Kling …). Fallback list
// from models/discovery capture (adobe/get_models.txt).
// Exact async video models and capabilities from the verified discovery snapshot.
"adobe-firefly": {
id: "adobe-firefly",
alias: "firefly",
@@ -335,18 +337,7 @@ export const VIDEO_PROVIDERS: Record<string, VideoProvider> = {
authType: "apikey",
authHeader: "bearer",
format: "adobe-firefly-video",
models: [
{ id: "sora-2", name: "Firefly Sora 2" },
{ id: "sora-2-pro", name: "Firefly Sora 2 Pro" },
{ id: "veo-3.1", name: "Firefly Veo 3.1" },
{ id: "veo-3.1-fast", name: "Firefly Veo 3.1 Fast" },
{ id: "veo-3.1-ref", name: "Firefly Veo 3.1 Reference" },
{ id: "kling-3", name: "Firefly Kling v3 Standard I2V" },
{ id: "kling-v3-t2v", name: "Firefly Kling v3 Standard T2V" },
{ id: "kling-v3-pro-i2v", name: "Firefly Kling v3 Pro I2V" },
{ id: "luma-ray3", name: "Firefly Ray3" },
{ id: "runway-gen4-turbo", name: "Firefly Runway Gen-4 Video" },
],
models: toRegistryVideoModels(),
},
};
@@ -368,5 +359,17 @@ export function parseVideoModel(modelStr: string | null) {
* Get all video models as a flat list
*/
export function getAllVideoModels() {
return getAllModelsFromRegistry(VIDEO_PROVIDERS);
return Object.entries(VIDEO_PROVIDERS).flatMap(([providerId, config]) =>
[providerId, config.alias]
.filter((prefix): prefix is string => Boolean(prefix))
.flatMap((prefix) =>
config.models.map((model) => ({
id: `${prefix}/${model.id}`,
name: model.name,
provider: providerId,
supportedSizes: model.supportedSizes || [],
mediaCapabilities: model.mediaCapabilities,
}))
)
);
}

View File

@@ -251,11 +251,14 @@ export function normalizeDuckDuckGoModel(model: string | undefined): string {
}
function getDuckDuckGoModelCapabilities(model: string): DuckDuckGoModelCapabilities {
// Per duckchat/v1/models (2026-07-22): claude-haiku-4-5 and gpt-oss-120b take a "low"
// reasoningEffort on the free tier; the others omit it (duck.ai applies its own default).
// `reasoningEffort` is REQUIRED on every duckchat/v1/chat request. Omitting it
// returns 400 ERR_BAD_REQUEST — A/B verified live against duck.ai with an
// otherwise byte-identical payload (200 with the field, 400 without, repeated).
// The live duck.ai bundle always sends one, so there is no "let the server
// pick a default" path any more.
if (model === "claude-haiku-4-5") return { reasoningEffort: "low" };
if (model === "tinfoil/gpt-oss-120b") return { reasoningEffort: "low" };
return { reasoningEffort: null };
return { reasoningEffort: "none" };
}
function extractDuckDuckGoFeVersion(html: string): string | null {
@@ -353,7 +356,6 @@ export class DuckDuckGoWebExecutor extends BaseExecutor {
}
private warmed = false;
private seeded = false;
private feVersion = DEFAULT_FE_VERSION;
private pendingVqdHash1: string | null = null;
private readonly cookieJar = new Map<string, string>();
@@ -561,7 +563,12 @@ export class DuckDuckGoWebExecutor extends BaseExecutor {
}
await this.warmSession(mergedSignal);
await this.seedChallengeChain(upstreamModel, mergedSignal);
// NOTE: the throwaway "seed" chat POST that used to run here has been removed.
// It existed to coax a usable challenge out of the upstream while the solver
// was broken; now that the solver reproduces a real browser's probe vectors
// exactly, the first real request succeeds on its own. Keeping it only doubled
// the chat calls per user request against an IP-rate-limited endpoint, which
// showed up as spurious 429 ERR_RATE_LIMIT.
const vqdHeaders = await this.acquireAuthHeaders(mergedSignal);
if (!vqdHeaders.vqd4 && !vqdHeaders.vqdHash1) {
clearTimeout(timeout);
@@ -770,41 +777,6 @@ export class DuckDuckGoWebExecutor extends BaseExecutor {
);
}
private async seedChallengeChain(model: string, signal: AbortSignal): Promise<void> {
if (this.seeded || signal.aborted) return;
this.seeded = true;
const seedMessages = [{ role: "user", content: "hi" }];
const previousPending = this.pendingVqdHash1;
try {
const vqdHeaders = await this.acquireAuthHeaders(signal);
if (!vqdHeaders.vqd4 && !vqdHeaders.vqdHash1) {
this.pendingVqdHash1 = previousPending;
return;
}
const response = await fetch(CHAT_URL, {
method: "POST",
headers: mergeHeadersCaseInsensitive(this.buildRequestHeaders(), {
Accept: "text/event-stream",
"Content-Type": "application/json",
"x-ddg-journey-id": randomUUID().replaceAll("-", ""),
"x-fe-signals": makeDuckDuckGoFeSignals(),
"x-fe-version": this.feVersion,
...(vqdHeaders.vqd4 ? { "x-vqd-4": vqdHeaders.vqd4 } : {}),
...(vqdHeaders.vqdHash1 ? { "x-vqd-hash-1": vqdHeaders.vqdHash1 } : {}),
}),
body: JSON.stringify(buildDuckDuckGoPayload(model, seedMessages, false)),
signal,
});
this.rememberResponseCookies(response);
if (response.ok) this.rememberChallengeHeader(response);
else this.pendingVqdHash1 = previousPending;
await response.body?.cancel().catch(() => {});
} catch (error) {
void error;
this.pendingVqdHash1 = previousPending;
}
}
private async processResponse(
response: Response,
streaming: boolean,

View File

@@ -5,12 +5,38 @@ import { createHash } from "node:crypto";
import vm from "node:vm";
import { parseFragment, serialize } from "parse5";
// WARNING: the contents of this template literal are NOT TypeScript — they are plain
// script-mode JavaScript executed via `vm.runInContext`. `vm.runInContext` compiles in
// script (non-module) mode, so an `export` keyword anywhere in here is a hard
// SyntaxError that kills the whole solver. A refactor that mass-added `export` to the
// five `function` declarations below silently broke every DuckDuckGo chat request
// (solve threw -> unsolved challenge sent -> HTTP 418 ERR_CHALLENGE). Do not add
// `export`/`import` to this string; `duckduckgo-challenge-split.test.ts` guards this.
export const CHALLENGE_STUBS = String.raw`
var __ua = __DDG_REAL_UA__;
var __HTML_LOOKUP = __DDG_HTML_LOOKUP__;
export function __makeHtmlElement(tag) {
// Browser-fidelity shims for the DDG "am I a real browser" probes.
// In a browser every built-in stringifies as native code; under a plain vm
// context the user-land re-declarations below would otherwise leak their source.
function __nativeFn(fn, name){
Object.defineProperty(fn, 'name', { value: name, configurable: true });
fn.toString = function(){ return 'function ' + name + '() { [native code] }'; };
return fn;
}
__nativeFn(parseInt, 'parseInt');
__nativeFn(parseFloat, 'parseFloat');
__nativeFn(isNaN, 'isNaN');
__nativeFn(encodeURIComponent, 'encodeURIComponent');
__nativeFn(decodeURIComponent, 'decodeURIComponent');
// NOTE: do NOT seal Math. Real Chromium reports Object.isSealed(Math) === false,
// and at least one challenge variant probes exactly that; sealing it here made
// the vector differ from the browser by one and failed the challenge.
function __makeHtmlElement(tag) {
var state = { _innerHTML: '', _qsaCount: 0, _cssText: '' };
var el = {
// Instantiate against the real per-tag constructor so
// document.createElement('div') instanceof HTMLDivElement holds.
var el = Object.create(__ctorForTag(tag).prototype);
Object.assign(el, {
tagName: String(tag).toUpperCase(), nodeName: String(tag).toUpperCase(), nodeType: 1,
children: [], childNodes: [], classList: [], dataset: {},
offsetWidth: 1, offsetHeight: 1, clientWidth: 1, clientHeight: 1, scrollHeight: 1, scrollWidth: 1,
@@ -19,9 +45,9 @@ export function __makeHtmlElement(tag) {
getAttribute: function(a){ if(a==='srcdoc') return state._srcdoc||''; return null; },
hasAttribute: function(){ return false; }, appendChild: function(c){ return c; }, removeChild: function(c){ return c; },
addEventListener: function(){}, removeEventListener: function(){}, querySelector: function(){ return null; },
querySelectorAll: function(s){ if (s === '*') { var arr = []; arr.length = state._qsaCount; return arr; } return []; },
querySelectorAll: function(s){ if (s === '*') { return __makeNodeList(state._qsaCount); } return __makeNodeList(0); },
cloneNode: function(){ return __makeHtmlElement(tag); }
};
});
Object.defineProperty(el, 'style', { value: new Proxy({}, { set: function(t, k, v){ t[k] = v; if (k === 'cssText') state._cssText = String(v); return true; }, get: function(t, k){ if (k === 'cssText') return state._cssText; return t[k] || ''; } }), enumerable: true, configurable: true });
Object.defineProperty(el, 'innerHTML', { get: function(){ return state._innerHTML; }, set: function(v){ var key = String(v); var entry = __HTML_LOOKUP && __HTML_LOOKUP[key]; if (entry) { state._innerHTML = String(entry.html); state._qsaCount = entry.count|0; } else { state._innerHTML = key; state._qsaCount = 0; } }, enumerable: true, configurable: true });
Object.defineProperty(el, 'outerHTML', { get: function(){ return '<' + tag + '>' + state._innerHTML + '</' + tag + '>'; }, enumerable: true });
@@ -30,7 +56,7 @@ export function __makeHtmlElement(tag) {
Object.defineProperty(el, 'contentDocument', { get: function(){ return __ifDoc; }, enumerable: true });
return el;
}
export function __mkObj(name, base) {
function __mkObj(name, base) {
base = base || {};
return new Proxy(base, {
get: function(t, k) {
@@ -54,18 +80,105 @@ export function __mkObj(name, base) {
has: function(t, k){ return k in t; }, set: function(t, k, v){ t[k] = v; return true; }
});
}
export function __parseCssDisplay(cssText){ if(!cssText) return ''; var m = String(cssText).match(/(?:^|;)\\s*display\\s*:\\s*([^;]+)/i); return m ? String(m[1]).trim() : ''; }
export function __getComputedStyle(el){ var cssText = el && el.style && el.style.cssText || ''; var display = __parseCssDisplay(cssText); return { getPropertyValue: function(name){ if(String(name).toLowerCase()==='display') return display; return ''; }, cssText: cssText, display: display }; }
function __parseCssDisplay(cssText){ if(!cssText) return ''; var m = String(cssText).match(/(?:^|;)\s*display\s*:\s*([^;]+)/i); return m ? String(m[1]).trim() : ''; }
function __getComputedStyle(el){ var cssText = el && el.style && el.style.cssText || ''; var display = __parseCssDisplay(cssText); return { getPropertyValue: function(name){ if(String(name).toLowerCase()==='display') return display; return ''; }, cssText: cssText, display: display }; }
var __ifMeta = __mkObj('meta', { getAttribute: function(a){ return a==='content' ? "default-src 'none'; script-src 'unsafe-inline';" : null; }, hasAttribute: function(a){ return a==='content'; }, tagName: 'META', nodeName: 'META' });
var __ifDoc = __mkObj('iframeDoc', { querySelector: function(s){ if (s && s.indexOf('Content-Security-Policy') !== -1) return __ifMeta; if (s === 'meta') return __ifMeta; return null; }, querySelectorAll: function(s){ if (s && s.indexOf('Content-Security-Policy') !== -1) return [__ifMeta]; if (s === 'meta') return [__ifMeta]; return []; }, getElementsByTagName: function(t){ return t && t.toLowerCase()==='meta' ? [__ifMeta] : []; }, body: __mkObj('iframeBody'), head: __mkObj('iframeHead'), documentElement: __mkObj('iframeRoot'), createElement: function(){ return __mkObj('elem', {setAttribute:function(){}, appendChild:function(){}, removeChild:function(){}, getAttribute:function(){return null;}, hasAttribute:function(){return false;}}); }, cookie: '', readyState: 'complete' });
var __iframeEl = __mkObj('iframe', { contentDocument: __ifDoc, contentWindow: __mkObj('iframeWin', { document: __ifDoc, top: undefined, parent: undefined }), document: __ifDoc, getAttribute: function(a){ if (a==='sandbox') return 'allow-scripts allow-same-origin'; if (a==='srcdoc') return ''; if (a==='id') return 'jsa'; return null; }, hasAttribute: function(a){ return a==='sandbox'||a==='id'; }, tagName: 'IFRAME', nodeName: 'IFRAME', id: 'jsa' });
var document = __mkObj('document', { querySelector: function(s){ if (s === '#jsa') return __iframeEl; if (s && s.indexOf('Content-Security-Policy') !== -1) return __ifMeta; return null; }, querySelectorAll: function(s){ if (s === '#jsa') return [__iframeEl]; if (s && s.indexOf('Content-Security-Policy') !== -1) return [__ifMeta]; return []; }, getElementById: function(id){ return id==='jsa' ? __iframeEl : null; }, getElementsByTagName: function(t){ if(t&&t.toLowerCase()==='iframe') return [__iframeEl]; return []; }, getElementsByClassName: function(){ return []; }, body: __mkObj('body', {appendChild:function(){}, removeChild:function(){}, querySelector:function(s){return s==='#jsa'?__iframeEl:null;}, querySelectorAll:function(s){return s==='#jsa'?[__iframeEl]:[];}}), head: __mkObj('head'), documentElement: __mkObj('root'), createElement: function(tag){ return __makeHtmlElement(tag||'div'); }, createTextNode: function(t){ return {nodeType:3, nodeValue:String(t||''), textContent:String(t||'')}; }, cookie: '', readyState: 'complete', title: '', addEventListener: function(){}, removeEventListener: function(){} });
// document.body keeps a LIVE children collection: challenges append a node and
// assert body.children.length grew by exactly 1, then remove it again.
var __bodyKids = [];
Object.defineProperty(__bodyKids, 'constructor', { value: HTMLCollection, enumerable: false, configurable: true });
var __body = __mkObj('body', {
appendChild: function(c){ __bodyKids.push(c); return c; },
removeChild: function(c){ var i = __bodyKids.indexOf(c); if (i !== -1) __bodyKids.splice(i, 1); return c; },
contains: function(c){ return __bodyKids.indexOf(c) !== -1; },
querySelector: function(s){ return s === '#jsa' ? __iframeEl : null; },
querySelectorAll: function(s){ return s === '#jsa' ? [__iframeEl] : __makeNodeList(0); },
children: __bodyKids, childNodes: __bodyKids,
tagName: 'BODY', nodeName: 'BODY', nodeType: 1
});
var document = __mkObj('document', { querySelector: function(s){ if (s === '#jsa') return __iframeEl; if (s && s.indexOf('Content-Security-Policy') !== -1) return __ifMeta; return null; }, querySelectorAll: function(s){ if (s === '#jsa') return [__iframeEl]; if (s && s.indexOf('Content-Security-Policy') !== -1) return [__ifMeta]; return __makeNodeList(__bodyKids.length + 3); }, getElementById: function(id){ return id==='jsa' ? __iframeEl : null; }, getElementsByTagName: function(t){ if(t&&t.toLowerCase()==='iframe') return [__iframeEl]; return []; }, getElementsByClassName: function(){ return []; }, body: __body, head: __mkObj('head'), documentElement: __mkObj('root'), createElement: function(tag){ return __makeHtmlElement(tag||'div'); }, createTextNode: function(t){ return {nodeType:3, nodeValue:String(t||''), textContent:String(t||'')}; }, cookie: '', readyState: 'complete', title: '', addEventListener: function(){}, removeEventListener: function(){} });
var window = __mkObj('window', { document: document, __DDG_BE_VERSION__: 1, __DDG_FE_CHAT_HASH__: 1, navigator: __mkObj('navigator', { userAgent: __ua, webdriver: false, language: 'en-US', languages: ['en-US','en'], platform: 'Linux x86_64', vendor: 'Google Inc.', appVersion: '5.0 (X11)', cookieEnabled: true, onLine: true, hardwareConcurrency: 8, deviceMemory: 8 }), innerWidth: 1280, innerHeight: 800, outerWidth: 1280, outerHeight: 800, devicePixelRatio: 1, screen: __mkObj('screen', { width:1920, height:1080, availWidth:1920, availHeight:1080, colorDepth:24, pixelDepth:24 }), location: __mkObj('location', { href:'https://duck.ai/', origin:'https://duck.ai', host:'duck.ai', hostname:'duck.ai', protocol:'https:', pathname:'/' }), performance: __mkObj('perf', { now: function(){ return 0; }, timeOrigin: 0 }), history: __mkObj('history', { length: 1, state: null }), addEventListener: function(){}, removeEventListener: function(){}, dispatchEvent: function(){return true;}, setTimeout: function(fn){ try{fn();}catch(e){} return 0; }, clearTimeout: function(){}, hasOwnProperty: function(k){ if (k==='__DDG_BE_VERSION__'||k==='__DDG_FE_CHAT_HASH__') return true; return Object.prototype.hasOwnProperty.call(this,k); } });
window.top = window; window.self = window; window.window = window; window.parent = window; window.globalThis = window;
// Object.prototype.toString.call(window) must be "[object Window]".
try { window[Symbol.toStringTag] = 'Window'; } catch (e) {}
// In a browser a sloppy-mode function called with no receiver gets the global
// object, and challenges assert (function(){return this;})() === window.
// In a vm context that is the context's own global, so alias it to window.
try {
var __g = (function(){ return this; })();
if (__g && __g !== window) {
Object.defineProperty(__g, Symbol.toStringTag, { value: 'Window', configurable: true });
// Copy by VALUE, not via accessors. Two reasons:
// 1) the var top/self/navigator/... declarations further down are hoisted,
// so those names already exist on the vm global and an "in" guard would
// skip them, leaving window.navigator undefined;
// 2) accessors closing over the window binding would recurse once it is
// rebound to __g below.
// The stub window is static, so a value copy is equivalent.
var __winStub = window;
for (var __k in __winStub) {
try { __g[__k] = __winStub[__k]; } catch (e) {}
}
// hasOwnProperty is probed for the __DDG_* markers; keep the stub's version.
try { __g.hasOwnProperty = function(k){ return __winStub.hasOwnProperty(k); }; } catch (e) {}
window = __g;
window.top = window; window.self = window; window.window = window; window.parent = window; window.globalThis = window;
}
} catch (e) {}
var top = window, self = window, parent = window, navigator = window.navigator, location = window.location, screen = window.screen, performance = window.performance, history = window.history;
var __R = null, __E = null;
export function __HTMLClass(name){ var c = function(){}; c.prototype = __mkObj(name+'.proto'); return c; }
var HTMLElement = __HTMLClass('HTMLElement'), HTMLDivElement = __HTMLClass('HTMLDivElement'), HTMLIFrameElement = __HTMLClass('HTMLIFrameElement'), HTMLDocument = __HTMLClass('HTMLDocument'), Document = __HTMLClass('Document'), Element = __HTMLClass('Element'), Node = __HTMLClass('Node'), Window = __HTMLClass('Window'), Event = __HTMLClass('Event'), MouseEvent = __HTMLClass('MouseEvent'), KeyboardEvent = __HTMLClass('KeyboardEvent'), TouchEvent = __HTMLClass('TouchEvent'), XMLHttpRequest = __HTMLClass('XMLHttpRequest'), WebSocket = __HTMLClass('WebSocket'), Image = __HTMLClass('Image'), FormData = __HTMLClass('FormData'), Blob = __HTMLClass('Blob'), File = __HTMLClass('File'), FileReader = __HTMLClass('FileReader'), URL = __HTMLClass('URL'), URLSearchParams = __HTMLClass('URLSearchParams'), Headers = __HTMLClass('Headers'), Request = __HTMLClass('Request'), Response = __HTMLClass('Response');
// Real DOM constructor chain. Some DDG challenge variants assert
// HTMLDivElement.prototype instanceof HTMLElement and
// HTMLElement.prototype instanceof Element, so these cannot be flat
// unrelated stubs — the prototype links have to be real.
function __DomClass(name, parent){
var c = function(){};
if (parent) c.prototype = Object.create(parent.prototype);
c.prototype.constructor = c;
Object.defineProperty(c, 'name', { value: name, configurable: true });
c.toString = function(){ return 'function ' + name + '() { [native code] }'; };
return c;
}
var EventTarget = __DomClass('EventTarget', null);
var Node = __DomClass('Node', EventTarget);
var Element = __DomClass('Element', Node);
var HTMLElement = __DomClass('HTMLElement', Element);
var HTMLDivElement = __DomClass('HTMLDivElement', HTMLElement);
var HTMLIFrameElement = __DomClass('HTMLIFrameElement', HTMLElement);
var HTMLLIElement = __DomClass('HTMLLIElement', HTMLElement);
var HTMLUnknownElement = __DomClass('HTMLUnknownElement', HTMLElement);
var Document = __DomClass('Document', Node);
var HTMLDocument = __DomClass('HTMLDocument', Document);
var NodeList = __DomClass('NodeList', null);
var HTMLCollection = __DomClass('HTMLCollection', null);
// Map a tag name to the constructor a browser would use, so
// document.createElement('div') instanceof HTMLDivElement holds.
function __ctorForTag(tag){
var t = String(tag||'div').toLowerCase();
if (t === 'div') return HTMLDivElement;
if (t === 'iframe') return HTMLIFrameElement;
if (t === 'li') return HTMLLIElement;
return HTMLElement;
}
// A NodeList-like: array-shaped but NOT a real Array, with .constructor.name
// === 'NodeList' — challenges check both !Array.isArray(x) and the ctor name.
function __makeNodeList(length){
var nl = Object.create(NodeList.prototype);
var n = length|0;
for (var i = 0; i < n; i++) nl[i] = __makeHtmlElement('div');
Object.defineProperty(nl, 'length', { value: n, enumerable: false, configurable: true });
nl.item = function(i){ return this[i] || null; };
nl.forEach = function(fn, thisArg){ for (var i = 0; i < n; i++) fn.call(thisArg, this[i], i, this); };
nl[Symbol.iterator] = function(){ var i = 0, self = this; return { next: function(){ return i < n ? { value: self[i++], done: false } : { value: undefined, done: true }; } }; };
return nl;
}
function __HTMLClass(name){ var c = function(){}; c.prototype = __mkObj(name+'.proto'); return c; }
// NOTE: HTMLElement / HTMLDivElement / HTMLIFrameElement / Element / Node /
// Document / HTMLDocument / NodeList are defined above via __DomClass with a
// REAL prototype chain — do not redeclare them here or the instanceof probes break.
var Window = __HTMLClass('Window'), Event = __HTMLClass('Event'), MouseEvent = __HTMLClass('MouseEvent'), KeyboardEvent = __HTMLClass('KeyboardEvent'), TouchEvent = __HTMLClass('TouchEvent'), XMLHttpRequest = __HTMLClass('XMLHttpRequest'), WebSocket = __HTMLClass('WebSocket'), Image = __HTMLClass('Image'), FormData = __HTMLClass('FormData'), Blob = __HTMLClass('Blob'), File = __HTMLClass('File'), FileReader = __HTMLClass('FileReader'), URL = __HTMLClass('URL'), URLSearchParams = __HTMLClass('URLSearchParams'), Headers = __HTMLClass('Headers'), Request = __HTMLClass('Request'), Response = __HTMLClass('Response');
var fetch = function(){ return Promise.resolve(__mkObj('resp', {ok:true, status:200, json:function(){return Promise.resolve({});}, text:function(){return Promise.resolve('');}})); };
var getComputedStyle = __getComputedStyle;
`;
@@ -90,9 +203,16 @@ export function buildHtmlLookup(js: string): Record<string, { html: string; coun
if (seen.has(html)) continue;
seen.add(html);
const fragment = parseFragment(html);
// `count` backs `element.querySelectorAll('*').length` for an element whose
// innerHTML is `html`. `querySelectorAll('*')` on a container returns its
// DESCENDANTS, and `countHtmlElements` already excludes the `#document-fragment`
// root, so the fragment's element count IS the descendant count. The former
// `- 1` undercounted by one (verified against a real browser: for
// `<li><div></li><li></div` Chromium reports 3, this returned 2), which
// corrupted every probe that multiplies by that length.
lookup[html] = {
html: serialize(fragment),
count: Math.max(0, countHtmlElements(fragment) - 1),
count: countHtmlElements(fragment),
};
}
return lookup;
@@ -102,14 +222,33 @@ export function sha256Base64(value: string): string {
return createHash("sha256").update(value, "utf8").digest("base64");
}
// Shape of the object a DDG challenge program resolves to.
type DuckDuckGoChallengeResult = {
client_hashes?: unknown;
meta?: unknown;
[key: string]: unknown;
};
/**
* Origin the solved challenge claims to come from. The duck.ai frontend stamps
* `meta.origin` with its own origin and the upstream cross-checks it.
*/
export const DUCKDUCKGO_CHALLENGE_ORIGIN = "https://duck.ai";
/**
* `meta.stack` mimics the frontend's captured Error stack. The upstream only
* requires a plausible stack that points at the duck.ai bundle — verified by
* ablation: a generic bundle path is accepted, omitting the field is not.
*/
function buildChallengeStack(origin: string, bundlePath: string): string {
const url = `${origin}${bundlePath}`;
return `Error\nat l (${url}:2:1695625)\nat async ${url}:2:1519117`;
}
export async function solveDuckDuckGoChallenge(
challenge: string,
userAgent: string
userAgent: string,
options: { origin?: string; bundlePath?: string } = {}
): Promise<string> {
// SECURITY NOTE: This function executes base64-decoded JavaScript from duck.ai via vm.runInContext.
// The challenge code is upstream-supplied (supply-chain surface). It is sandboxed with a 5s timeout
@@ -121,14 +260,31 @@ export async function solveDuckDuckGoChallenge(
);
const context = vm.createContext({});
vm.runInContext(stubs, context, { timeout: 5000 });
const startedAt = Date.now();
const result = (await vm.runInContext(js, context, {
timeout: 5000,
})) as DuckDuckGoChallengeResult;
const elapsedMs = Date.now() - startedAt;
const clientHashes = Array.isArray(result.client_hashes) ? result.client_hashes : [];
if (clientHashes.length === 0)
throw new Error("DuckDuckGo challenge returned empty client_hashes");
clientHashes[0] = userAgent;
result.client_hashes = clientHashes.map((hash) => sha256Base64(String(hash)));
// The real frontend augments the challenge's own `meta` with origin / stack /
// duration before sending it back. Omitting them yields 418 ERR_CHALLENGE even
// when every client_hash is correct (confirmed by capturing a real browser's
// x-vqd-hash-1 header, which always carries all three).
const origin = options.origin ?? DUCKDUCKGO_CHALLENGE_ORIGIN;
const bundlePath = options.bundlePath ?? "/dist/duckai-dist/entry.duckai.js";
const meta = (result.meta ?? {}) as Record<string, unknown>;
result.meta = {
...meta,
origin,
stack: buildChallengeStack(origin, bundlePath),
duration: String(elapsedMs),
};
return Buffer.from(JSON.stringify(result), "utf8").toString("base64");
}

View File

@@ -16,11 +16,11 @@ import {
AdobeFireflyError,
adobeFireflyGenerateImage,
adobeFireflyImageTimeoutMs,
adobeFireflyMaxImageRefs,
resolveAdobeAccessToken,
resolveAdobeSourceImageIds,
resolveAdobeSourceImageReferences,
resolveAdobeImageModel,
} from "../../../services/adobeFireflyClient.ts";
import { getAdobeReferenceUploadLimit } from "../../../services/adobeFireflyModels.ts";
import { isAdobeFireflyUpscaleModel } from "../../../services/adobeFireflyUpscale.ts";
import { handleAdobeFireflyImageUpscale } from "../../imageUpscale/adobeFirefly.ts";
@@ -90,7 +90,8 @@ export async function handleAdobeFireflyImageGeneration({
// Keep the raw credential blob for Cookie + sherlockToken (x-arp-session-id).
// JWT may be embedded in the same paste as cookies (HAR / multi-line).
const psd = (credentials as { providerSpecificData?: { cookie?: string } })?.providerSpecificData;
const psd = (credentials as { providerSpecificData?: { cookie?: string } })
?.providerSpecificData;
const sessionCookie =
(typeof psd?.cookie === "string" && psd.cookie.trim()) ||
(typeof credentials?.apiKey === "string" && credentials.apiKey.trim()) ||
@@ -98,15 +99,11 @@ export async function handleAdobeFireflyImageGeneration({
? credentials.accessToken
: undefined);
// Cap uploads by model family. gpt-image: 2 subject refs max (34+ stalls colligo → 504).
// nano: 4 general refs for multi-panel composition.
const { id: resolvedId } = resolveAdobeImageModel(model);
const maxRefs = adobeFireflyMaxImageRefs(resolvedId);
const sourceImageIds = await resolveAdobeSourceImageIds({
const { spec } = resolveAdobeImageModel(model);
const references = await resolveAdobeSourceImageReferences({
accessToken,
body,
max: maxRefs,
max: getAdobeReferenceUploadLimit(spec, "image"),
sessionCookie,
prompt,
fetchImpl,
@@ -121,13 +118,13 @@ export async function handleAdobeFireflyImageGeneration({
: undefined;
const timeoutMs = adobeFireflyImageTimeoutMs({
timeoutMs: explicitTimeout,
refCount: sourceImageIds.length,
refCount: references.length,
});
log?.info?.(
"IMAGE",
`${provider}/${model} (adobe-firefly) | prompt: "${prompt.slice(0, 60)}${prompt.length > 60 ? "..." : ""}"` +
(sourceImageIds.length ? ` | refs: ${sourceImageIds.length}/${maxRefs}` : "") +
(references.length ? ` | refs: ${references.length}` : "") +
` | pollTimeoutMs=${timeoutMs}`
);
@@ -139,9 +136,8 @@ export async function handleAdobeFireflyImageGeneration({
aspectRatio: body.aspect_ratio ?? body.aspectRatio ?? body.size,
quality: body.quality,
seed: Number.isFinite(seed as number) ? (seed as number) : undefined,
negativePrompt:
typeof body.negative_prompt === "string" ? body.negative_prompt : undefined,
sourceImageIds: sourceImageIds.length ? sourceImageIds : undefined,
negativePrompt: typeof body.negative_prompt === "string" ? body.negative_prompt : undefined,
references: references.length ? references : undefined,
sessionCookie,
timeoutMs,
fetchImpl,

View File

@@ -10,9 +10,10 @@ import {
AdobeFireflyError,
adobeFireflyGenerateVideo,
resolveAdobeAccessToken,
resolveAdobeSourceImageIds,
resolveAdobeSourceImageReferences,
resolveAdobeVideoModel,
} from "../../services/adobeFireflyClient.ts";
import { getAdobeReferenceUploadLimit } from "../../services/adobeFireflyModels.ts";
function normalizePositiveNumber(value: unknown, fallback: number): number {
const n = Number(value);
@@ -55,7 +56,8 @@ export async function handleAdobeFireflyVideoGeneration({
? Number(body.seed)
: undefined;
// Keep raw paste for Cookie + sherlockToken (x-arp-session-id).
const psd = (credentials as { providerSpecificData?: { cookie?: string } })?.providerSpecificData;
const psd = (credentials as { providerSpecificData?: { cookie?: string } })
?.providerSpecificData;
const sessionCookie =
(typeof psd?.cookie === "string" && psd.cookie.trim()) ||
(typeof credentials?.apiKey === "string" && credentials.apiKey.trim()) ||
@@ -63,13 +65,11 @@ export async function handleAdobeFireflyVideoGeneration({
? credentials.accessToken
: undefined);
// Kling i2v / Veo ref / Sora frame: upload reference images first.
const { id: videoModelId } = resolveAdobeVideoModel(String(model));
const maxFrames = videoModelId.includes("kling") || videoModelId.includes("sora") ? 2 : 3;
const sourceImageIds = await resolveAdobeSourceImageIds({
const { spec } = resolveAdobeVideoModel(String(model));
const references = await resolveAdobeSourceImageReferences({
accessToken,
body,
max: maxFrames,
max: getAdobeReferenceUploadLimit(spec, "image"),
sessionCookie,
prompt,
fetchImpl,
@@ -79,7 +79,7 @@ export async function handleAdobeFireflyVideoGeneration({
log?.info?.(
"VIDEO",
`${provider}/${model} (adobe-firefly) | prompt: "${prompt.slice(0, 60)}${prompt.length > 60 ? "..." : ""}"` +
(sourceImageIds.length ? ` | frames: ${sourceImageIds.length}` : "")
(references.length ? ` | refs: ${references.length}` : "")
);
const result = await adobeFireflyGenerateVideo({
@@ -99,7 +99,7 @@ export async function handleAdobeFireflyVideoGeneration({
? body.negativePrompt
: undefined,
generateAudio: body.generate_audio !== false && body.generateAudio !== false,
sourceImageIds: sourceImageIds.length ? sourceImageIds : undefined,
references: references.length ? references : undefined,
sessionCookie,
timeoutMs,
fetchImpl,

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

View File

@@ -1,328 +1,590 @@
/**
* Adobe Firefly model catalog: live discovery + static fallback from browser capture.
* Adobe Firefly model discovery and normalized media capabilities.
*
* Live: POST firefly-3p.ff.adobe.io/v2/models/discovery (needs valid IMS token).
* Fallback: curated rows from adobe/get_models.txt (2026-07 Firefly SPA capture) so
* Media/Models still list usable ids when discovery fails or credentials are missing.
* The live discovery schema is authoritative. The generated snapshot is used only
* when a request cannot perform authenticated discovery (for example /v1/models).
*/
import {
type AdobeFireflyDiscoveredModel,
discoverAdobeFireflyModels,
resolveAdobeAccessToken,
} from "./adobeFireflyClient.ts";
import { ADOBE_FIREFLY_DISCOVERY_SNAPSHOT } from "./adobeFireflyModelSnapshot.ts";
export type AdobeFireflyModality = "image" | "video" | "audio" | "unknown";
export interface AdobeFireflyDiscoveredModel {
modelId: string;
modelVersion: string;
displayName: string;
modality: AdobeFireflyModality;
enabled: boolean;
providerName?: string;
releaseReadiness?: string;
healthStatus?: string;
inputMediaUseCases: string[];
requestSchema?: Record<string, unknown>;
backingModel?: string;
}
export interface AdobeFireflyReferenceInputCapability {
mediaType: string;
usageType: string;
minItems: number;
maxItems: number | null;
maxFileSizeBytes: number | null;
}
export interface AdobeFireflyMediaCapabilities {
inputMediaUseCases: string[];
schemaProperties: string[];
requiredProperties: string[];
referenceInputs: AdobeFireflyReferenceInputCapability[];
maxReferenceItems: number | null;
supportedSizes: string[];
supportedAspectRatios: string[];
supportedResolutions: string[];
supportedDurations: number[];
durationMin: number | null;
durationMax: number | null;
durationDefault: number | null;
outputCountMin: number | null;
outputCountMax: number | null;
promptMaxLength: number | null;
releaseReadiness: string;
healthStatus: string;
}
export interface AdobeFireflyCatalogModel {
/** OpenAI-style id without provider prefix, e.g. nano-banana-pro or flux-fluxPro */
/** Stable API id without the provider prefix. */
id: string;
name: string;
modality: "image" | "video";
/** Upstream wire modelId for generate-async */
upstreamModelId: string;
/** Upstream wire modelVersion for generate-async */
upstreamModelVersion: string;
inputModalities?: string[];
providerName: string;
backingModel: string;
inputModalities: string[];
capabilities: AdobeFireflyMediaCapabilities;
}
/**
* Static fallback built from adobe/get_models.txt discovery response.
* Friendly aliases first (Media page defaults), then popular upstream families.
*/
export const ADOBE_FIREFLY_FALLBACK_MODELS: AdobeFireflyCatalogModel[] = [
// ── Friendly aliases (handler resolveAdobeImageModel / resolveAdobeVideoModel) ──
{
id: "nano-banana-pro",
name: "Gemini 3.0 (Nano Banana Pro)",
modality: "image",
upstreamModelId: "gemini-flash",
upstreamModelVersion: "nano-banana-2",
inputModalities: ["text", "image"],
},
{
id: "nano-banana",
name: "Gemini 2.5 (Nano Banana)",
modality: "image",
upstreamModelId: "gemini-flash",
upstreamModelVersion: "nano-banana",
inputModalities: ["text", "image"],
},
{
id: "nano-banana-2",
name: "Gemini 3.1 (Nano Banana 2)",
modality: "image",
upstreamModelId: "gemini-flash",
upstreamModelVersion: "nano-banana-3",
inputModalities: ["text", "image"],
},
{
id: "gpt-image-2",
name: "GPT Image 2",
modality: "image",
upstreamModelId: "gpt-image",
upstreamModelVersion: "2",
inputModalities: ["text", "image"],
},
{
id: "gpt-image",
name: "GPT Image 2",
modality: "image",
upstreamModelId: "gpt-image",
upstreamModelVersion: "2",
inputModalities: ["text", "image"],
},
{
id: "gpt-image-1.5",
name: "GPT Image 1.5",
modality: "image",
upstreamModelId: "gpt-image",
upstreamModelVersion: "1.5",
inputModalities: ["text", "image"],
},
{
id: "sora-2",
name: "Sora 2",
modality: "video",
upstreamModelId: "sora",
upstreamModelVersion: "sora-2",
},
{
id: "sora-2-pro",
name: "Sora 2 Pro",
modality: "video",
upstreamModelId: "sora",
upstreamModelVersion: "sora-2-pro",
},
{
id: "veo-3.1",
name: "Veo 3.1",
modality: "video",
upstreamModelId: "veo",
upstreamModelVersion: "3.1-generate",
},
{
id: "veo-3.1-fast",
name: "Veo 3.1 Fast",
modality: "video",
upstreamModelId: "veo",
upstreamModelVersion: "3.1-fast-generate",
},
{
id: "veo-3.1-ref",
name: "Veo 3.1 Reference",
modality: "video",
upstreamModelId: "veo",
upstreamModelVersion: "3.1-generate",
},
{
id: "kling-3",
name: "Kling Video v3 Standard Image to Video",
modality: "video",
upstreamModelId: "kling",
upstreamModelVersion: "kling_v3_standard_i2v",
},
// ── Additional image families from discovery capture ──
{
id: "flux-2",
name: "Flux 2",
modality: "image",
upstreamModelId: "flux",
upstreamModelVersion: "2",
inputModalities: ["text", "image"],
},
{
id: "flux-pro",
name: "Flux 1.1 Pro",
modality: "image",
upstreamModelId: "flux",
upstreamModelVersion: "fluxPro",
inputModalities: ["text", "image"],
},
{
id: "flux-ultra",
name: "Flux 1.1 Ultra",
modality: "image",
upstreamModelId: "flux",
upstreamModelVersion: "fluxUltra",
inputModalities: ["text", "image"],
},
{
id: "seedream-4",
name: "Seedream 4.0",
modality: "image",
upstreamModelId: "seedream",
upstreamModelVersion: "seedream_v4",
inputModalities: ["text", "image"],
},
{
id: "seedream-5-lite",
name: "Seedream 5.0 Lite",
modality: "image",
upstreamModelId: "seedream",
upstreamModelVersion: "seedream_v5_lite",
inputModalities: ["text", "image"],
},
{
id: "runway-gen4-image",
name: "Runway Gen-4 Image",
modality: "image",
upstreamModelId: "runway-gen4-image",
upstreamModelVersion: "gen4_image",
inputModalities: ["text", "image"],
},
// ── Additional video families ──
{
id: "kling-v3-t2v",
name: "Kling Video v3 Standard Text to Video",
modality: "video",
upstreamModelId: "kling",
upstreamModelVersion: "kling_v3_standard_t2v",
},
{
id: "kling-v3-pro-i2v",
name: "Kling Video v3 Pro Image to Video",
modality: "video",
upstreamModelId: "kling",
upstreamModelVersion: "kling_v3_pro_i2v",
},
{
id: "luma-ray3",
name: "Ray3",
modality: "video",
upstreamModelId: "luma",
upstreamModelVersion: "3.0-ray",
},
{
id: "runway-gen4-turbo",
name: "Runway Gen-4 Video",
modality: "video",
upstreamModelId: "runway",
upstreamModelVersion: "gen4_turbo",
},
];
export interface AdobeFireflyImageModelSpec extends AdobeFireflyCatalogModel {
modality: "image";
/** Payload dialect observed for this model family. */
family: "gemini" | "gpt-image" | "generic";
}
/** Stable slug for upstream modelId + modelVersion (catalog id when not a friendly alias). */
export interface AdobeFireflyVideoModelSpec extends AdobeFireflyCatalogModel {
modality: "video";
defaultDuration: number;
defaultResolution: string;
}
interface MergedObjectSchema {
properties: Record<string, Record<string, unknown>>;
required: string[];
}
function asRecord(value: unknown): Record<string, unknown> {
return value && typeof value === "object" && !Array.isArray(value)
? (value as Record<string, unknown>)
: {};
}
function asStringArray(value: unknown): string[] {
return Array.isArray(value)
? value.map((item) => String(item)).filter((item) => item.length > 0)
: [];
}
function finiteInteger(value: unknown): number | null {
return Number.isInteger(value) ? (value as number) : null;
}
/** Merge object properties/required keys contributed through JSON Schema allOf. */
export function mergeAdobeObjectSchema(schema: unknown): MergedObjectSchema {
const merged: MergedObjectSchema = { properties: {}, required: [] };
const visit = (value: unknown) => {
const node = asRecord(value);
const properties = asRecord(node.properties);
for (const [key, property] of Object.entries(properties)) {
merged.properties[key] = asRecord(property);
}
merged.required.push(...asStringArray(node.required));
if (Array.isArray(node.allOf)) node.allOf.forEach(visit);
};
visit(schema);
merged.required = [...new Set(merged.required)];
return merged;
}
function schemaBranches(schema: unknown): Record<string, unknown>[] {
const root = asRecord(schema);
if (Object.keys(root).length === 0) return [];
return [
root,
...(Array.isArray(root.anyOf) ? root.anyOf.map(asRecord) : []),
...(Array.isArray(root.oneOf) ? root.oneOf.map(asRecord) : []),
];
}
function enumStrings(schema: unknown): string[] {
return [
...new Set(
schemaBranches(schema)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.filter((value): value is string => typeof value === "string")
),
];
}
function integerBranch(schema: unknown): Record<string, unknown> {
return schemaBranches(schema).find((branch) => branch.type === "integer") || {};
}
/** Stable, collision-resistant public id for an exact upstream model/version pair. */
export function slugifyAdobeModel(modelId: string, modelVersion: string): string {
const mid = String(modelId || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
const ver = String(modelVersion || "")
.trim()
.toLowerCase()
.replace(/[^a-z0-9.]+/g, "-")
.replace(/^-|-$/g, "");
if (!ver || ver === "default" || ver === mid) return mid || "model";
return `${mid}-${ver}`;
const slug = (value: string, allowDot = false) =>
String(value || "")
.trim()
.toLowerCase()
.replace(allowDot ? /[^a-z0-9.]+/g : /[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
const family = slug(modelId);
// Adobe still uses `kling_v3_omni*` internally, while discovery exposes these
// products to users as Kling O3. Never leak the obsolete/internal "omni" name
// into the public API catalog; the untouched upstream version stays in the spec.
const publicVersion =
family === "kling" ? modelVersion.replace(/^kling_v3_omni/i, "kling_o3") : modelVersion;
const version = slug(publicVersion, true);
if (!version || version === "default" || version === family) return family || "model";
return `${family}-${version}`;
}
/** Map discovery rows → catalog entries (image/video only). */
export function mapDiscoveredToCatalog(
rows: AdobeFireflyDiscoveredModel[]
): AdobeFireflyCatalogModel[] {
const out: AdobeFireflyCatalogModel[] = [];
const seen = new Set<string>();
/** Parse POST /v2/models/discovery without discarding its resolved request schema. */
export function parseAdobeModelsDiscovery(body: unknown): AdobeFireflyDiscoveredModel[] {
const root = asRecord(body);
const families = Array.isArray(root.models) ? root.models : [];
const rows: AdobeFireflyDiscoveredModel[] = [];
// Prefer friendly aliases when upstream matches known fallback rows.
for (const fb of ADOBE_FIREFLY_FALLBACK_MODELS) {
const hit = rows.find(
(r) =>
r.modelId === fb.upstreamModelId &&
r.modelVersion === fb.upstreamModelVersion &&
(r.modality === fb.modality || r.modality === "unknown")
);
if (hit && !seen.has(fb.id)) {
seen.add(fb.id);
out.push({
...fb,
name: hit.displayName || fb.name,
for (const familyValue of families) {
const family = asRecord(familyValue);
const modelId = String(family.modelId || "").trim();
if (!modelId) continue;
for (const [modelVersion, versionValue] of Object.entries(asRecord(family.modelVersions))) {
const version = asRecord(versionValue);
if (version.enabled === false) continue;
const outputModalities = asStringArray(version.outputModality).map((item) =>
item.toLowerCase()
);
const modality: AdobeFireflyModality = outputModalities.includes("image")
? "image"
: outputModalities.includes("video")
? "video"
: outputModalities.includes("audio")
? "audio"
: "unknown";
rows.push({
modelId,
modelVersion,
displayName: String(
version.modelDisplayName || version.modelCaiDisplayName || modelVersion
),
modality,
enabled: version.enabled !== false,
providerName:
typeof family.acModelFamilyProviderDisplayName === "string"
? family.acModelFamilyProviderDisplayName
: undefined,
releaseReadiness:
typeof version.releaseReadiness === "string" ? version.releaseReadiness : undefined,
healthStatus: typeof version.healthStatus === "string" ? version.healthStatus : undefined,
inputMediaUseCases: asStringArray(version.inputMediaUseCase),
requestSchema: asRecord(version.requestSchema),
backingModel:
typeof version.bksGenerationModel === "string" ? version.bksGenerationModel : undefined,
});
}
}
return rows;
}
function normalizeCapabilities(row: AdobeFireflyDiscoveredModel): AdobeFireflyMediaCapabilities {
const schema = mergeAdobeObjectSchema(row.requestSchema);
const referenceSchema = asRecord(schema.properties.referenceBlobs);
const referenceInputs: AdobeFireflyReferenceInputCapability[] = [];
const mediaCapabilities = Array.isArray(referenceSchema["x-capabilities"])
? referenceSchema["x-capabilities"]
: [];
for (const mediaValue of mediaCapabilities) {
const media = asRecord(mediaValue);
const maxFileSizeBytes = finiteInteger(media.maxFileSizeBytes);
const usageConstraints = Array.isArray(media.usageConstraints) ? media.usageConstraints : [];
for (const usageValue of usageConstraints) {
const usage = asRecord(usageValue);
if (usage.deprecated === true) continue;
const usageType = String(usage.usageType || "");
const mediaType = String(media.mediaType || "");
if (!usageType || !mediaType) continue;
referenceInputs.push({
mediaType,
usageType,
minItems: finiteInteger(usage.minItems) ?? 0,
maxItems: finiteInteger(usage.maxItems),
maxFileSizeBytes,
});
}
}
for (const r of rows) {
if (r.modality !== "image" && r.modality !== "video") continue;
const id = slugifyAdobeModel(r.modelId, r.modelVersion);
if (seen.has(id)) continue;
// Skip if already covered by a friendly alias with same upstream
if (
out.some(
(o) =>
o.upstreamModelId === r.modelId && o.upstreamModelVersion === r.modelVersion
const supportedSizes = [
...new Set(
schemaBranches(schema.properties.size)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.map(asRecord)
.filter((size) => finiteInteger(size.width) !== null && finiteInteger(size.height) !== null)
.map((size) => `${size.width}x${size.height}`)
),
];
const supportedAspectRatios = [
...new Set(
schemaBranches(schema.properties.generationSettings).flatMap((branch) =>
enumStrings(asRecord(asRecord(branch.properties).aspectRatio))
)
) {
continue;
}
seen.add(id);
out.push({
id,
name: r.displayName || id,
modality: r.modality,
upstreamModelId: r.modelId,
upstreamModelVersion: r.modelVersion,
inputModalities: r.modality === "image" ? ["text", "image"] : ["text"],
});
}
return out;
}
export function getAdobeFireflyFallbackCatalog(modality?: "image" | "video"): AdobeFireflyCatalogModel[] {
if (!modality) return [...ADOBE_FIREFLY_FALLBACK_MODELS];
return ADOBE_FIREFLY_FALLBACK_MODELS.filter((m) => m.modality === modality);
}
/**
* Live discovery when credentials resolve; otherwise static fallback from get_models capture.
*/
export async function resolveAdobeFireflyCatalog(opts: {
credentials?: {
apiKey?: string;
accessToken?: string;
providerSpecificData?: Record<string, unknown> | null;
} | null;
modality?: "image" | "video";
fetchImpl?: typeof fetch;
}): Promise<{ models: AdobeFireflyCatalogModel[]; source: "api" | "fallback" }> {
const fetchImpl = opts.fetchImpl || fetch;
try {
if (opts.credentials) {
const token = await resolveAdobeAccessToken(opts.credentials, fetchImpl);
const discovered = await discoverAdobeFireflyModels(token, fetchImpl);
let catalog = mapDiscoveredToCatalog(discovered);
if (opts.modality) catalog = catalog.filter((m) => m.modality === opts.modality);
if (catalog.length > 0) return { models: catalog, source: "api" };
}
} catch {
// fall through to static catalog
}
),
];
const duration = integerBranch(schema.properties.duration);
const outputCount = integerBranch(schema.properties.n);
const prompt =
schemaBranches(schema.properties.prompt).find((branch) => branch.type === "string") || {};
return {
models: getAdobeFireflyFallbackCatalog(opts.modality),
source: "fallback",
inputMediaUseCases: [...row.inputMediaUseCases],
schemaProperties: Object.keys(schema.properties),
requiredProperties: [...schema.required],
referenceInputs,
maxReferenceItems: finiteInteger(referenceSchema.maxItems),
supportedSizes,
supportedAspectRatios,
supportedResolutions: enumStrings(schema.properties.resolution),
supportedDurations: [
...new Set(
schemaBranches(schema.properties.duration)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.filter((value): value is number => Number.isInteger(value))
),
],
durationMin: finiteInteger(duration.minimum),
durationMax: finiteInteger(duration.maximum),
durationDefault: finiteInteger(duration.default),
outputCountMin: finiteInteger(outputCount.minimum),
outputCountMax: finiteInteger(outputCount.maximum),
promptMaxLength: finiteInteger(prompt.maxLength),
releaseReadiness: row.releaseReadiness || "",
healthStatus: row.healthStatus || "",
};
}
/** Registry-shaped models for imageRegistry / videoRegistry. */
export function toRegistryImageModels(
models: AdobeFireflyCatalogModel[] = getAdobeFireflyFallbackCatalog("image")
): Array<{ id: string; name: string; inputModalities?: string[] }> {
return models
.filter((m) => m.modality === "image")
.map((m) => ({
id: m.id,
name: m.name.startsWith("Firefly ") ? m.name : `Firefly ${m.name}`,
inputModalities: m.inputModalities || ["text", "image"],
}));
function isCallableGenerationModel(row: AdobeFireflyDiscoveredModel): boolean {
if (row.modality !== "image" && row.modality !== "video") return false;
if (!mergeAdobeObjectSchema(row.requestSchema).properties.prompt) return false;
const excluded = new Set(["upscaling", "sharpening", "denoising"]);
return !row.inputMediaUseCases.some((value) => excluded.has(value.toLowerCase()));
}
export function toRegistryVideoModels(
models: AdobeFireflyCatalogModel[] = getAdobeFireflyFallbackCatalog("video")
): Array<{ id: string; name: string }> {
return models
.filter((m) => m.modality === "video")
.map((m) => ({
id: m.id,
name: m.name.startsWith("Firefly ") ? m.name : `Firefly ${m.name}`,
}));
function deriveInputModalities(capabilities: AdobeFireflyMediaCapabilities): string[] {
return ["text", ...new Set(capabilities.referenceInputs.map((reference) => reference.mediaType))];
}
function semanticCatalogKey(model: AdobeFireflyCatalogModel): string {
return JSON.stringify({
backingModel: model.backingModel,
name: model.name,
modality: model.modality,
capabilities: model.capabilities,
});
}
/** Normalize and de-duplicate callable image/video rows from live discovery. */
export function mapDiscoveredToCatalog(
rows: AdobeFireflyDiscoveredModel[]
): AdobeFireflyCatalogModel[] {
const output: AdobeFireflyCatalogModel[] = [];
const seen = new Set<string>();
for (const row of rows) {
if (!isCallableGenerationModel(row)) continue;
const capabilities = normalizeCapabilities(row);
const model: AdobeFireflyCatalogModel = {
id: slugifyAdobeModel(row.modelId, row.modelVersion),
name: row.displayName,
modality: row.modality as "image" | "video",
upstreamModelId: row.modelId,
upstreamModelVersion: row.modelVersion,
providerName: row.providerName || "",
backingModel: row.backingModel || "",
inputModalities: deriveInputModalities(capabilities),
capabilities,
};
const key = semanticCatalogKey(model);
if (seen.has(key)) continue;
seen.add(key);
output.push(model);
}
return output;
}
function snapshotCatalog(): AdobeFireflyCatalogModel[] {
return ADOBE_FIREFLY_DISCOVERY_SNAPSHOT.map((model) => {
const capabilities: AdobeFireflyMediaCapabilities = {
inputMediaUseCases: [...model.inputMediaUseCases],
schemaProperties: [...model.schemaProperties],
requiredProperties: [...model.requiredProperties],
referenceInputs: model.referenceInputs.map((reference) => ({ ...reference })),
maxReferenceItems: model.maxReferenceItems,
supportedSizes: [...model.supportedSizes],
supportedAspectRatios: [...model.supportedAspectRatios],
supportedResolutions: [...model.supportedResolutions],
supportedDurations: [...model.supportedDurations],
durationMin: model.durationMin,
durationMax: model.durationMax,
durationDefault: model.durationDefault,
outputCountMin: model.outputCountMin,
outputCountMax: model.outputCountMax,
promptMaxLength: model.promptMaxLength,
releaseReadiness: model.releaseReadiness,
healthStatus: model.healthStatus,
};
return {
id: model.id,
name: model.name,
modality: model.modality,
upstreamModelId: model.upstreamModelId,
upstreamModelVersion: model.upstreamModelVersion,
providerName: model.providerName,
backingModel: model.backingModel,
inputModalities: deriveInputModalities(capabilities),
capabilities,
};
});
}
export const ADOBE_FIREFLY_FALLBACK_MODELS: AdobeFireflyCatalogModel[] = snapshotCatalog();
export function getAdobeFireflyFallbackCatalog(
modality?: "image" | "video"
): AdobeFireflyCatalogModel[] {
return ADOBE_FIREFLY_FALLBACK_MODELS.filter((model) => !modality || model.modality === modality);
}
function imageFamily(model: AdobeFireflyCatalogModel): AdobeFireflyImageModelSpec["family"] {
if (model.upstreamModelId === "gemini-flash") return "gemini";
if (model.upstreamModelId === "gpt-image" || model.upstreamModelId === "gpt-4o-image") {
return "gpt-image";
}
return "generic";
}
export const ADOBE_FIREFLY_IMAGE_MODELS: Record<string, AdobeFireflyImageModelSpec> =
Object.fromEntries(
getAdobeFireflyFallbackCatalog("image").map((model) => [
model.id,
{ ...model, modality: "image" as const, family: imageFamily(model) },
])
);
function defaultDuration(model: AdobeFireflyCatalogModel): number {
const caps = model.capabilities;
return caps.durationDefault ?? caps.supportedDurations[0] ?? caps.durationMin ?? 5;
}
function defaultResolution(model: AdobeFireflyCatalogModel): string {
if (model.capabilities.supportedSizes.some((value) => value.includes("1920x1080"))) {
return "1080p";
}
return "720p";
}
export const ADOBE_FIREFLY_VIDEO_MODELS: Record<string, AdobeFireflyVideoModelSpec> =
Object.fromEntries(
getAdobeFireflyFallbackCatalog("video").map((model) => [
model.id,
{
...model,
modality: "video" as const,
defaultDuration: defaultDuration(model),
defaultResolution: defaultResolution(model),
},
])
);
const LEGACY_MODEL_ALIASES: Record<string, string> = {
"nano-banana": "gemini-flash-nano-banana",
"nano-banana-pro": "gemini-flash-nano-banana-2",
"nano-banana-2": "gemini-flash-nano-banana-3",
"gpt-image": "gpt-image-2",
"gpt-image-2": "gpt-image-2",
"gpt-image-1.5": "gpt-image-1.5",
"flux-2": "flux-2",
"flux-pro": "flux-fluxpro",
"flux-ultra": "flux-fluxultra",
"seedream-4": "seedream-seedream-v4",
"seedream-5-lite": "seedream-seedream-v5-lite",
"runway-gen4-image": "runway-gen4-image",
"veo-3.1": "veo-3.1-generate",
"veo-3.1-fast": "veo-3.1-fast-generate",
"luma-ray3": "luma-3.0-ray",
"runway-gen4-turbo": "runway-gen4-turbo",
// Backward compatibility only; the catalog advertises the exact discovered id.
"kling-3": "kling-kling-v3-standard-i2v",
};
// Preserve established API aliases when (and only when) they resolve to a model
// that is present in the verified discovery snapshot. These keys are not listed.
for (const [alias, target] of Object.entries(LEGACY_MODEL_ALIASES)) {
const imageTarget = ADOBE_FIREFLY_IMAGE_MODELS[target];
if (imageTarget) ADOBE_FIREFLY_IMAGE_MODELS[alias] = imageTarget;
const videoTarget = ADOBE_FIREFLY_VIDEO_MODELS[target];
if (videoTarget) ADOBE_FIREFLY_VIDEO_MODELS[alias] = videoTarget;
}
/** Backward-compatible request ids. Kept out of every advertised model catalog. */
export const ADOBE_FIREFLY_IMAGE_ROUTING_ALIASES = Object.freeze(
Object.entries(LEGACY_MODEL_ALIASES)
.filter(([, target]) => Boolean(ADOBE_FIREFLY_IMAGE_MODELS[target]))
.map(([alias]) => alias)
);
function normalizeRequestedId(model: string): string {
return String(model || "")
.trim()
.toLowerCase()
.replace(/^adobe-firefly\//, "")
.replace(/^firefly\//, "");
}
function resolveCatalogId(model: string): string {
const requested = normalizeRequestedId(model);
return LEGACY_MODEL_ALIASES[requested] || requested;
}
export function resolveAdobeImageModel(model: string): {
id: string;
spec: AdobeFireflyImageModelSpec;
} {
const id = resolveCatalogId(model);
const spec = ADOBE_FIREFLY_IMAGE_MODELS[id];
if (!spec) {
throw new Error(
`Unknown Adobe Firefly image model: ${normalizeRequestedId(model) || "(empty)"}`
);
}
return { id, spec };
}
export function resolveAdobeVideoModel(model: string): {
id: string;
spec: AdobeFireflyVideoModelSpec;
} {
const id = resolveCatalogId(model);
const spec = ADOBE_FIREFLY_VIDEO_MODELS[id];
if (!spec) {
throw new Error(
`Unknown Adobe Firefly video model: ${normalizeRequestedId(model) || "(empty)"}`
);
}
return { id, spec };
}
export function toRegistryImageModels(): Array<{
id: string;
name: string;
inputModalities: string[];
imageRequired?: boolean;
supportedSizes: string[];
mediaCapabilities: Record<string, unknown>;
}> {
const generated = getAdobeFireflyFallbackCatalog("image").map((model) => ({
id: model.id,
name: `Firefly ${model.name}`,
inputModalities: model.inputModalities,
supportedSizes: model.capabilities.supportedSizes,
mediaCapabilities: toAdobeMediaCapabilitiesApi(model),
}));
// Upscaling uses a distinct Firefly endpoint and is not returned by the image
// generation discovery schema. Keep its two supported Topaz models visible in
// the same provider catalog so image clients can select them deliberately.
return [
...generated,
{
id: "topaz-standard",
name: "Firefly Topaz Upscale (Standard)",
inputModalities: ["image"],
imageRequired: true,
supportedSizes: [],
mediaCapabilities: { input_media_use_cases: ["upscaling"] },
},
{
id: "topaz-bloom",
name: "Firefly Topaz Bloom (Creative Upscale)",
inputModalities: ["image"],
imageRequired: true,
supportedSizes: [],
mediaCapabilities: { input_media_use_cases: ["upscaling"] },
},
];
}
export function toRegistryVideoModels(): Array<{
id: string;
name: string;
supportedSizes: string[];
mediaCapabilities: Record<string, unknown>;
}> {
return getAdobeFireflyFallbackCatalog("video").map((model) => ({
id: model.id,
name: `Firefly ${model.name}`,
supportedSizes: model.capabilities.supportedSizes,
mediaCapabilities: toAdobeMediaCapabilitiesApi(model),
}));
}
/** JSON-safe extension emitted by /v1/models. */
export function toAdobeMediaCapabilitiesApi(
model: AdobeFireflyCatalogModel
): Record<string, unknown> {
const caps = model.capabilities;
return {
upstream_model_id: model.upstreamModelId,
upstream_model_version: model.upstreamModelVersion,
provider_name: model.providerName,
release_readiness: caps.releaseReadiness,
health_status: caps.healthStatus,
input_media_use_cases: caps.inputMediaUseCases,
reference_inputs: caps.referenceInputs.map((reference) => ({
media_type: reference.mediaType,
usage_type: reference.usageType,
min_items: reference.minItems,
max_items: reference.maxItems,
max_file_size_bytes: reference.maxFileSizeBytes,
})),
max_reference_items: caps.maxReferenceItems,
supported_sizes: caps.supportedSizes,
supported_aspect_ratios: caps.supportedAspectRatios,
supported_resolutions: caps.supportedResolutions,
supported_durations: caps.supportedDurations,
duration_min: caps.durationMin,
duration_max: caps.durationMax,
duration_default: caps.durationDefault,
output_count_min: caps.outputCountMin,
output_count_max: caps.outputCountMax,
prompt_max_length: caps.promptMaxLength,
};
}
export function getAdobeReferenceUploadLimit(
model: AdobeFireflyCatalogModel,
mediaType: string
): number {
if (model.capabilities.maxReferenceItems !== null) {
return Math.max(1, Math.min(32, model.capabilities.maxReferenceItems));
}
const declaredTotal = model.capabilities.referenceInputs
.filter((reference) => reference.mediaType === mediaType)
.reduce((total, reference) => total + (reference.maxItems ?? 0), 0);
return Math.max(1, Math.min(32, declaredTotal || 1));
}

View File

@@ -190,10 +190,26 @@ function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === "object" && !Array.isArray(value);
}
/**
* Whether an `error` field carries a real failure signal. A key-presence check
* (`!= null`) false-positives on benign values some backends emit on every
* chunk (`{}`, `""`, `false`, `0`) — e.g. tool-call turns where a chunk with
* real tool_calls content also carries `"error": {}`. Only substantive values
* are treated as upstream failures.
*/
function isSubstantiveError(value: unknown): boolean {
if (value === null || value === undefined) return false;
if (typeof value === "string") return value.trim().length > 0;
if (typeof value === "object" && !Array.isArray(value)) {
return Object.keys(value as Record<string, unknown>).length > 0;
}
return value === true;
}
function isStreamingUpstreamError(parsed: unknown, eventType: string): boolean {
if (eventType === "response.failed" || eventType === "error") return true;
if (!isRecord(parsed)) return false;
if (parsed.error != null) return true;
if (isSubstantiveError(parsed.error)) return true;
const nestedResponse = isRecord(parsed.response) ? parsed.response : null;
return nestedResponse?.status === "failed" && nestedResponse.error != null;

View File

@@ -34,8 +34,11 @@
"scripts/dev/tls-options.mjs",
"scripts/check/check-supported-node-runtime.ts",
"scripts/dev/sync-env.mjs",
"scripts/build/native-binary-compat.mjs",
"scripts/build/assembleStandalone.mjs",
"scripts/build/backendOnlyPages.mjs",
"scripts/build/build-next-isolated.mjs",
"scripts/build/build-tproxy-native.mjs",
"scripts/build/native-binary-compat.mjs",
"scripts/build/runtime-env.mjs",
"README.md",
"LICENSE",

View File

@@ -89,6 +89,15 @@ const NATIVE_ASSET_ENTRIES = [
src: ["node_modules", "better-sqlite3", "build"],
dest: ["node_modules", "better-sqlite3", "build"],
},
{
// #8847: Bun (and npx -g global installs) resolve better-sqlite3's native
// binary from prebuilds/ instead of build/Release/, so the compiled build/
// copy alone leaves a hollow package that falls back to sql.js (OOM under
// Bun). Ship the prebuilds alongside the compiled binary.
label: "better-sqlite3 prebuilds (Bun / global installs)",
src: ["node_modules", "better-sqlite3", "prebuilds"],
dest: ["node_modules", "better-sqlite3", "prebuilds"],
},
{
// TPROXY IP_TRANSPARENT addon (Fase 3 / Epic A). Built by build-tproxy-native
// before assembly; Linux-only + opt-in, so the source is absent on non-Linux

View File

@@ -121,6 +121,9 @@ export const PACK_ARTIFACT_ROOT_ALLOWED_EXACT_PATHS: string[] = [
// shipped via package.json "files", so it must be allowed in the tarball.
"open-sse/utils/setupPolyfill.ts",
"package.json",
"scripts/build/assembleStandalone.mjs",
"scripts/build/backendOnlyPages.mjs",
"scripts/build/build-tproxy-native.mjs",
"scripts/build/build-next-isolated.mjs",
"scripts/check/check-supported-node-runtime.ts",
"scripts/build/native-binary-compat.mjs",

View File

@@ -0,0 +1,207 @@
#!/usr/bin/env node
import fs from "node:fs";
import path from "node:path";
import { createHash } from "node:crypto";
function usage() {
console.error(
"Usage: node scripts/dev/generate-adobe-firefly-snapshot.mjs <discovery.json> <output.ts>"
);
process.exit(2);
}
const [, , inputArg, outputArg] = process.argv;
if (!inputArg || !outputArg) usage();
const inputPath = path.resolve(inputArg);
const outputPath = path.resolve(outputArg);
const inputBytes = fs.readFileSync(inputPath);
const sourceHash = createHash("sha256").update(inputBytes).digest("hex");
const root = JSON.parse(inputBytes.toString("utf8"));
function mergeObjectSchema(schema) {
const merged = { properties: {}, required: [] };
const visit = (node) => {
if (!node || typeof node !== "object") return;
if (node.properties && typeof node.properties === "object") {
Object.assign(merged.properties, node.properties);
}
if (Array.isArray(node.required)) merged.required.push(...node.required);
if (Array.isArray(node.allOf)) node.allOf.forEach(visit);
};
visit(schema);
merged.required = [...new Set(merged.required)];
return merged;
}
function branches(schema) {
if (!schema || typeof schema !== "object") return [];
return [schema, ...(schema.anyOf || []), ...(schema.oneOf || [])];
}
function stringEnums(schema) {
return [
...new Set(
branches(schema)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.filter((value) => typeof value === "string")
),
];
}
function integerSchema(schema) {
return branches(schema).find((branch) => branch.type === "integer") || {};
}
function publicModelId(modelId, modelVersion) {
const slug = (value, allowDot = false) =>
String(value || "")
.trim()
.toLowerCase()
.replace(allowDot ? /[^a-z0-9.]+/g : /[^a-z0-9]+/g, "-")
.replace(/^-|-$/g, "");
const family = slug(modelId);
const publicVersion =
family === "kling" ? String(modelVersion).replace(/^kling_v3_omni/i, "kling_o3") : modelVersion;
const version = slug(publicVersion, true);
if (!version || version === "default" || version === family) return family || "model";
return `${family}-${version}`;
}
function normalizeModel(family, modelVersion, version) {
const schema = mergeObjectSchema(version.requestSchema);
const properties = schema.properties;
const referenceSchema = properties.referenceBlobs || {};
const referenceInputs = [];
for (const media of referenceSchema["x-capabilities"] || []) {
for (const usage of media.usageConstraints || []) {
if (usage.deprecated === true) continue;
referenceInputs.push({
mediaType: String(media.mediaType || ""),
usageType: String(usage.usageType || ""),
minItems: Number.isInteger(usage.minItems) ? usage.minItems : 0,
maxItems: Number.isInteger(usage.maxItems) ? usage.maxItems : null,
maxFileSizeBytes: Number.isInteger(media.maxFileSizeBytes) ? media.maxFileSizeBytes : null,
});
}
}
const supportedSizes = [
...new Set(
branches(properties.size)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.filter(
(size) =>
size &&
Number.isInteger(size.width) &&
size.width > 0 &&
Number.isInteger(size.height) &&
size.height > 0
)
.map((size) => `${size.width}x${size.height}`)
),
];
const supportedAspectRatios = [
...new Set(
branches(properties.generationSettings).flatMap((branch) =>
stringEnums(branch?.properties?.aspectRatio)
)
),
];
const duration = integerSchema(properties.duration);
const supportedDurations = [
...new Set(
branches(properties.duration)
.flatMap((branch) => (Array.isArray(branch.enum) ? branch.enum : []))
.filter(Number.isInteger)
),
];
const prompt = branches(properties.prompt).find((branch) => branch.type === "string") || {};
const outputCount = integerSchema(properties.n);
return {
id: publicModelId(family.modelId, modelVersion),
name: String(version.modelDisplayName || version.modelCaiDisplayName || modelVersion),
modality: version.outputModality[0],
upstreamModelId: family.modelId,
upstreamModelVersion: modelVersion,
providerName: String(family.acModelFamilyProviderDisplayName || ""),
releaseReadiness: String(version.releaseReadiness || ""),
healthStatus: String(version.healthStatus || ""),
inputMediaUseCases: (version.inputMediaUseCase || []).map(String),
schemaProperties: Object.keys(properties),
requiredProperties: schema.required,
referenceInputs,
maxReferenceItems: Number.isInteger(referenceSchema.maxItems) ? referenceSchema.maxItems : null,
supportedSizes,
supportedAspectRatios,
supportedResolutions: stringEnums(properties.resolution),
supportedDurations,
durationMin: Number.isInteger(duration.minimum) ? duration.minimum : null,
durationMax: Number.isInteger(duration.maximum) ? duration.maximum : null,
durationDefault: Number.isInteger(duration.default) ? duration.default : null,
outputCountMin: Number.isInteger(outputCount.minimum) ? outputCount.minimum : null,
outputCountMax: Number.isInteger(outputCount.maximum) ? outputCount.maximum : null,
promptMaxLength: Number.isInteger(prompt.maxLength) ? prompt.maxLength : null,
backingModel: String(version.bksGenerationModel || ""),
};
}
const rawModels = [];
for (const family of Array.isArray(root.models) ? root.models : []) {
for (const [modelVersion, version] of Object.entries(family.modelVersions || {})) {
if (!version || version.enabled === false) continue;
const modality = Array.isArray(version.outputModality)
? version.outputModality.map((value) => String(value).toLowerCase())[0]
: "";
if (modality !== "image" && modality !== "video") continue;
const schema = mergeObjectSchema(version.requestSchema);
if (!schema.properties.prompt) continue;
const useCases = (version.inputMediaUseCase || []).map((value) => String(value).toLowerCase());
if (useCases.some((value) => ["upscaling", "sharpening", "denoising"].includes(value))) {
continue;
}
rawModels.push(normalizeModel(family, modelVersion, version));
}
}
// Discovery currently repeats a few exact aliases (for example flux/fluxPro and
// fluxPro/1.1). Keep the first canonical wire pair and suppress duplicate cards.
const seen = new Set();
const models = [];
for (const model of rawModels) {
const semanticKey = JSON.stringify({
backingModel: model.backingModel,
name: model.name,
modality: model.modality,
schemaProperties: model.schemaProperties,
requiredProperties: model.requiredProperties,
referenceInputs: model.referenceInputs,
maxReferenceItems: model.maxReferenceItems,
supportedSizes: model.supportedSizes,
supportedAspectRatios: model.supportedAspectRatios,
supportedResolutions: model.supportedResolutions,
supportedDurations: model.supportedDurations,
durationMin: model.durationMin,
durationMax: model.durationMax,
});
if (seen.has(semanticKey)) continue;
seen.add(semanticKey);
models.push(model);
}
const source = `/**
* Generated from Adobe Firefly POST /v2/models/discovery with resolveSchema=true.
* Source SHA-256: ${sourceHash}
* Regenerate with scripts/dev/generate-adobe-firefly-snapshot.mjs; do not edit by hand.
* The generated literal stays compact to satisfy the repository's line-count gate.
*/
// prettier-ignore
export const ADOBE_FIREFLY_DISCOVERY_SNAPSHOT = ${JSON.stringify(models)} as const;
`;
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, source, "utf8");
console.log(`Wrote ${models.length} models to ${outputPath}`);

View File

@@ -609,14 +609,6 @@ async function main() {
args: ["run", "check:pack-artifact"],
timeout: 20 * 60 * 1000,
});
// WS1.2 (#7065 class): boot the REAL packed tarball from a clean install —
// the runtime gate structure checks cannot provide. Reuses the same dist/ build.
slow.push({
id: "pack-boot",
label: "Tarball boot-smoke (installed CLI serves /health)",
args: ["run", "check:pack-boot"],
timeout: 15 * 60 * 1000,
});
}
slow.forEach((g) => announce(`${g.label} [parallel]`));
const slowResults = await Promise.all(
@@ -633,6 +625,41 @@ async function main() {
detail: code === 0 ? "pass" : firstFailureLine(out),
});
});
if (WITH_BUILD) {
// WS1.2 (#7065 class): boot the REAL packed tarball from a clean install.
// check:pack-artifact is the builder for dist/ when staging is absent, so the
// boot smoke MUST run after it completes. Running both in the parallel wave
// races check:pack-boot against dist/server.js creation on clean worktrees.
const packArtifactIndex = slow.findIndex((g) => g.id === "pack-artifact");
const packArtifactResult = slowResults[packArtifactIndex];
const bootLabel = "Tarball boot-smoke (installed CLI serves /health)";
if (!packArtifactResult || packArtifactResult.code !== 0) {
const out = "skipped because package-artifact did not produce a valid dist/ build";
saveGateLog("pack-boot", out);
record({
id: "pack-boot",
label: bootLabel,
kind: "hard",
ok: false,
detail: out,
});
} else {
announce(bootLabel);
const { code, out } = await runAsync(npmCmd, ["run", "check:pack-boot"], {
timeout: 15 * 60 * 1000,
});
saveGateLog("pack-boot", out);
record({
id: "pack-boot",
label: bootLabel,
kind: "hard",
ok: code === 0,
detail: code === 0 ? "pass" : firstFailureLine(out),
});
}
}
} else if (WITH_BUILD) {
// --with-build without the suites (--quick): still verify the package artifact.
const { code, out } = await runAsync(npmCmd, ["run", "check:pack-artifact"], {

View File

@@ -134,7 +134,7 @@ export function LlmChatCard({
}: Props) {
const t = useTranslations("miniPlayground");
const { keys } = useApiKey();
const { models } = useProviderModels(providerId);
const { models, loading, error, retry } = useProviderModels(providerId);
const [internalSelectedKey, setInternalSelectedKey] = useState<string>("");
const [internalModel, setInternalModel] = useState<string>(initialModel ?? "");
@@ -392,15 +392,31 @@ export function LlmChatCard({
<select
value={model || firstModel}
onChange={(e) => setModel(e.target.value)}
className="min-w-0 flex-1 rounded-md border border-border bg-bg-subtle text-xs px-2 py-1 text-text-main focus:outline-none focus:ring-1 focus:ring-primary"
disabled={loading}
className="min-w-0 flex-1 rounded-md border border-border bg-bg-subtle text-xs px-2 py-1 text-text-main focus:outline-none focus:ring-1 focus:ring-primary disabled:opacity-60"
>
{modelOptions.length === 0 && <option value="">{initialModel || "—"}</option>}
{modelOptions.length === 0 && !loading && <option value="">{initialModel || "—"}</option>}
{loading && <option value="">{t("loading") ?? "Loading…"}</option>}
{modelOptions.map((m) => (
<option key={m.id} value={m.id}>
{m.id}
</option>
))}
</select>
{error && (
<span className="text-xs text-red-500 flex items-center gap-1" role="alert">
<span className="truncate max-w-[180px]" title={String(error)}>
{String(error)}
</span>
<button
type="button"
onClick={retry}
className="shrink-0 text-xs text-primary hover:text-primary-strong underline"
>
{t("retry") ?? "Retry"}
</button>
</span>
)}
</div>
{/* Key select */}
{keys.length > 0 && (

View File

@@ -1,6 +1,6 @@
"use client";
import { useState, useEffect } from "react";
import { useState, useEffect, useCallback, useRef } from "react";
export interface ProviderModel {
id: string;
@@ -18,6 +18,8 @@ interface UseProviderModelsResult {
models: ProviderModel[];
loading: boolean;
error: string | null;
/** Re-runs the model fetch for the current provider. Useful for a Retry action. */
retry: () => void;
}
/**
@@ -32,15 +34,14 @@ export function useProviderModels(providerId: string): UseProviderModelsResult {
const [models, setModels] = useState<ProviderModel[]>([]);
const [loading, setLoading] = useState<boolean>(true);
const [error, setError] = useState<string | null>(null);
// Cancels any in-flight load (component unmount or a retry superseding the
// previous request) so a stale response never overwrites a newer one.
const cleanupRef = useRef<(() => void) | null>(null);
useEffect(() => {
if (!providerId) {
setLoading(false);
return;
}
const load = useCallback(() => {
cleanupRef.current?.();
let cancelled = false;
const load = async () => {
const run = async () => {
setLoading(true);
setError(null);
try {
@@ -109,11 +110,33 @@ export function useProviderModels(providerId: string): UseProviderModelsResult {
if (!cancelled) setLoading(false);
}
};
void load();
return () => {
void run();
const cleanup = () => {
cancelled = true;
};
cleanupRef.current = cleanup;
return cleanup;
}, [providerId]);
return { models, loading, error };
useEffect(() => {
if (!providerId) {
setLoading(false);
return;
}
return load();
}, [providerId, load]);
// Release the current in-flight cleanup on unmount so no state updates leak.
useEffect(() => {
return () => {
cleanupRef.current?.();
};
}, []);
const retry = useCallback(() => {
if (!providerId) return;
load();
}, [providerId, load]);
return { models, loading, error, retry };
}

View File

@@ -0,0 +1,73 @@
import {
discoverAdobeFireflyModels,
resolveAdobeAccessToken,
} from "@omniroute/open-sse/services/adobeFireflyClient.ts";
import {
getAdobeFireflyFallbackCatalog,
mapDiscoveredToCatalog,
toAdobeMediaCapabilitiesApi,
type AdobeFireflyCatalogModel,
} from "@omniroute/open-sse/services/adobeFireflyModels.ts";
import { sanitizeErrorMessage } from "@omniroute/open-sse/utils/error";
type AdobeProviderData = { cookie?: unknown; access_token?: unknown; accessToken?: unknown };
interface AdobeProviderModelsResult {
models: Array<Record<string, unknown>>;
source: "api" | "local_catalog";
warning?: string;
}
function toModelResponse(model: AdobeFireflyCatalogModel): Record<string, unknown> {
const endpoint = model.modality === "image" ? "images" : "videos";
return {
id: model.id,
name: model.name,
owned_by: "adobe-firefly",
apiFormat: endpoint,
supportedEndpoints: [endpoint],
type: model.modality,
input_modalities: model.inputModalities,
output_modalities: [model.modality],
supported_sizes: model.capabilities.supportedSizes,
media_capabilities: toAdobeMediaCapabilitiesApi(model),
};
}
function fallback(warning: string): AdobeProviderModelsResult {
return {
models: getAdobeFireflyFallbackCatalog().map(toModelResponse),
source: "local_catalog",
warning,
};
}
export async function getAdobeModels(
apiKey: string | undefined,
accessToken: string | undefined,
providerData: unknown,
fetchImpl: typeof fetch = fetch
): Promise<AdobeProviderModelsResult> {
const providerSpecificData =
providerData && typeof providerData === "object" ? (providerData as AdobeProviderData) : {};
try {
const token = await resolveAdobeAccessToken(
{
apiKey,
accessToken,
providerSpecificData,
},
fetchImpl
);
const models = mapDiscoveredToCatalog(await discoverAdobeFireflyModels(token, fetchImpl));
return models.length > 0
? { models: models.map(toModelResponse), source: "api" }
: fallback("Adobe Firefly discovery returned no callable image or video models");
} catch (error) {
return fallback(
`Adobe Firefly discovery unavailable: ${sanitizeErrorMessage(
error instanceof Error ? error.message : String(error)
)}`
);
}
}

View File

@@ -84,10 +84,8 @@ import {
isAutoFetchModelsEnabled,
persistDiscoveredModels,
} from "@/lib/providerModels/modelDiscovery";
import {
buildProviderModelsUrl,
getDiscoveryClientVersionOptions,
} from "./discoveryClientVersion";
import { buildProviderModelsUrl, getDiscoveryClientVersionOptions } from "./discoveryClientVersion";
import { getAdobeModels } from "./adobeFireflyDiscovery";
import {
parseGeminiModelsList,
type GeminiDiscoveryModel,
@@ -422,10 +420,7 @@ export async function GET(
// #6267 — a models-endpoint redirect (307/308) is not a fixable-config
// error. safeOutboundFetch throws REDIRECT_BLOCKED which
// getSafeOutboundFetchErrorStatus maps to 503, but unlike the other 503
// cases (URL_GUARD_BLOCKED / INVALID_URL, which are genuinely
// unrecoverable and stay hard errors) a blocked redirect should degrade to
// the local/cached catalog OmniRoute ships instead of surfacing a raw 503.
// General fix — covers any config-driven provider that 307s (e.g. qwen-web).
// Redirect blocks degrade to the local/cached catalog; invalid URLs remain hard errors.
if (error instanceof SafeOutboundFetchError && error.code === "REDIRECT_BLOCKED") {
return buildDiscoveryFallbackResponse(warnings);
}
@@ -434,6 +429,11 @@ export async function GET(
return buildDiscoveryFallbackResponse(warnings);
};
if (provider === "adobe-firefly") {
const discovery = await getAdobeModels(apiKey, accessToken, connection.providerSpecificData);
return buildResponse({ provider, connectionId, ...discovery });
}
const maybeReturnCachedDiscovery = () => {
if (!refresh && cachedDiscoveryModels.length > 0) {
return buildCachedDiscoveryResponse();

View File

@@ -701,6 +701,17 @@ export async function testSingleConnection(connectionId: string, validationModel
? makeDiagnosis("ok", "local", null, null)
: classifyFailure({ error: result.error, statusCode: result.statusCode, provider }));
// #9623: a failed connection test must not paint the connection permanently red.
// Previously a non-terminal failure wrote `testStatus: "error"` with
// `rateLimitedUntil: null` — since the cooldown filter only ever skips entries
// whose rateLimitedUntil is in the future, a null cooldown left the connection
// permanently unavailable after a transient outage. Give non-terminal test
// failures a short cooldown so the lazy-recovery path retries them.
const terminalTestStatuses = new Set(["banned", "expired", "credits_exhausted"]);
const isTerminalFailure =
!result.valid && terminalTestStatuses.has(String(diagnosis.code ?? diagnosis.type ?? "").toLowerCase());
const testFailureCooldownMs = result.valid ? 0 : 30_000; // 30s retry window
const updateData: Record<string, any> = {
testStatus: result.valid ? "active" : "error",
lastError: result.valid ? null : result.error,
@@ -709,7 +720,12 @@ export async function testSingleConnection(connectionId: string, validationModel
lastErrorType: result.valid ? null : diagnosis.type,
lastErrorSource: result.valid ? null : diagnosis.source,
errorCode: result.valid ? null : diagnosis.code || result.statusCode || null,
rateLimitedUntil: result.valid ? null : connection.rateLimitedUntil || null,
rateLimitedUntil:
result.valid || isTerminalFailure
? result.valid
? null
: connection.rateLimitedUntil || null
: new Date(Date.now() + testFailureCooldownMs).toISOString(),
};
if (result.valid) {

View File

@@ -1113,6 +1113,7 @@ async function buildUnifiedModelsResponseCore(
input_modalities: imgModel.inputModalities || ["text"],
output_modalities: ["image"],
...(imgModel.description ? { description: imgModel.description } : {}),
...(imgModel.mediaCapabilities ? { media_capabilities: imgModel.mediaCapabilities } : {}),
});
}
@@ -1178,6 +1179,12 @@ async function buildUnifiedModelsResponseCore(
created: timestamp,
owned_by: videoModel.provider,
type: "video",
supported_sizes: videoModel.supportedSizes,
input_modalities: ["text"],
output_modalities: ["video"],
...(videoModel.mediaCapabilities
? { media_capabilities: videoModel.mediaCapabilities }
: {}),
});
}

View File

@@ -10191,6 +10191,8 @@
"copied": "Copied!",
"run": "Run",
"running": "Running...",
"loading": "Loading...",
"retry": "Retry",
"response": "Response",
"tunnel": "Tunnel",
"send": "Send",

View File

@@ -10191,6 +10191,8 @@
"copied": "Copiado!",
"run": "Executar",
"running": "Executando...",
"loading": "Carregando...",
"retry": "Tentar novamente",
"response": "Resposta",
"tunnel": "Tunnel",
"send": "Enviar",

View File

@@ -10191,6 +10191,8 @@
"copied": "Đã sao chép!",
"run": "Chạy",
"running": "Đang chạy...",
"loading": "Đang tải...",
"retry": "Thử lại",
"response": "Phản hồi",
"tunnel": "Đường hầm",
"send": "Gửi",

View File

@@ -306,6 +306,7 @@ export async function registerNodejs(): Promise<void> {
{ applyRuntimeSettings },
{ startRuntimeConfigHotReload },
{ startSpendBatchWriter },
{ startCleanupScheduler },
{ registerDefaultGuardrails },
{ ensurePersistentManagementPasswordHash },
{ skillExecutor },
@@ -320,6 +321,7 @@ export async function registerNodejs(): Promise<void> {
import("@/lib/config/runtimeSettings"),
import("@/lib/config/hotReload"),
import("@/lib/spend/batchWriter"),
import("@/lib/db/cleanup"),
import("@/lib/guardrails"),
import("@/lib/auth/managementPassword"),
import("@/lib/skills/executor"),
@@ -489,6 +491,17 @@ export async function registerNodejs(): Promise<void> {
console.warn("[STARTUP] Could not initialize vacuum scheduler (non-fatal):", msg);
}
// Retention cleanup scheduler (#4691/#6988, #9624): runs the general retention
// cleanup once after startup and then every 6 hours. Previously this was only
// wired into the unused src/server-init.ts, so telemetry tables grew unboundedly
// even with retention.autoCleanupEnabled=true. Idempotent (guarded internally).
try {
startCleanupScheduler();
} catch (err: unknown) {
const msg = err instanceof Error ? err.message : String(err);
console.warn("[STARTUP] Could not start cleanup scheduler (non-fatal):", msg);
}
// Warm the model catalog's durable, apiKey-independent sub-caches at
// startup — see warmModelCatalogCache() for why the top-level Response
// cache alone doesn't deliver this. Fire-and-forget, non-fatal.

View File

@@ -253,14 +253,15 @@ export async function cleanupMemoryEntries(): Promise<CleanupResult> {
/**
* Clean up old domain_cost_history based on retention settings. (#6848)
* Uses unix-epoch `timestamp` column (INTEGER).
* The `timestamp` column stores epoch milliseconds (saveCostEntry default
* is Date.now()), so the cutoff must be in milliseconds to match. (#9625)
*/
export async function cleanupDomainCostHistory(): Promise<CleanupResult> {
const db = getDbInstance();
const retention = getRetentionSettings();
const retentionDays = retention.domainCostHistory;
const cutoffEpoch = Math.floor(Date.now() / 1000) - retentionDays * 86_400;
const cutoffEpoch = Date.now() - retentionDays * 86_400_000;
const result: CleanupResult = { deleted: 0, errors: 0 };

View File

@@ -17,12 +17,6 @@ import { getDbInstance } from "./core";
const quotaComboMaintenance = new Map<string, Promise<unknown>>();
const deletingPools = new Set<string>();
/** Reset module-level state for test isolation. Call in test.after() hooks. */
export function resetQuotaPoolsModuleState(): void {
deletingPools.clear();
quotaComboMaintenance.clear();
}
function serializeQuotaComboMaintenance<T>(
poolId: string,
operation: () => Promise<T>

View File

@@ -64,7 +64,9 @@
"tests/unit/anthropic-thinking-signature-recovery.test.ts",
"tests/unit/antigravity-429-quota-tdd.test.ts",
"tests/unit/antigravity-prefer-stored-project.test.ts",
"tests/unit/api-key-policy-noauth-allowed-connections.test.ts",
"tests/unit/api-key-rotator-health.test.ts",
"tests/unit/api-key-policy-noauth-allowed-connections.test.ts",
"tests/unit/appearance-widget-settings-schema.test.ts",
"tests/unit/auth-antigravity-account-retry-v2.test.ts",
"tests/unit/auth-clear-account-error.test.ts",
@@ -213,7 +215,9 @@
"tests/unit/executor-web-cookie-sweep.test.ts",
"tests/unit/format-provider-error-cause.test.ts",
"tests/unit/forwarded-header-budget.test.ts",
"tests/unit/gemini-web-capabilities-9356.test.ts",
"tests/unit/gemini-web-missing-browser-3516.test.ts",
"tests/unit/gemini-web-capabilities-9356.test.ts",
"tests/unit/grok-cli-oauth.test.ts",
"tests/unit/guardrails-api-3496.test.ts",
"tests/unit/headroom-codex-quota-snapshot-6379.test.ts",
@@ -273,7 +277,9 @@
"tests/unit/rate-limit-manager.test.ts",
"tests/unit/rate-limit-queue-timeout-lockout.test.ts",
"tests/unit/repro-7503-no-choices.test.ts",
"tests/unit/repro-9486.test.ts",
"tests/unit/repro-9630-combo-false-503.test.ts",
"tests/unit/repro-9486.test.ts",
"tests/unit/repro-antigravity-404-family-cooldown-hijack.test.ts",
"tests/unit/responses-handler.test.ts",
"tests/unit/rotation-config-omniroute.test.ts",

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,90 @@
import { test } from "node:test";
import assert from "node:assert";
import {
ADOBE_FIREFLY_VIDEO_MODELS,
extractAdobeSourceImageReferences,
normalizeAdobeReferenceBlobs,
} from "../../open-sse/services/adobeFireflyClient.ts";
import { getAdobeModels } from "../../src/app/api/providers/[id]/models/adobeFireflyDiscovery.ts";
function userImsJwt(): string {
const payload = Buffer.from(
JSON.stringify({
user_id: "test@AdobeID",
type: "access_token",
client_id: "clio-playground-web",
})
).toString("base64url");
return `eyJhbGciOiJSUzI1NiJ9.${payload}.${"sig".padEnd(40, "x")}`;
}
test("reference validation enforces discovered roles, counts, and frame order", () => {
const kling = ADOBE_FIREFLY_VIDEO_MODELS["kling-3"];
assert.deepEqual(
normalizeAdobeReferenceBlobs(kling, [
{ id: "frame-a", mediaType: "image", usage: "frame" },
{ id: "frame-b", mediaType: "image", usage: "frame" },
]),
[
{ id: "frame-a", usage: "frame", order: 1 },
{ id: "frame-b", usage: "frame", order: 2 },
]
);
assert.throws(
() => normalizeAdobeReferenceBlobs(kling, [{ id: "bad", mediaType: "image", usage: "mask" }]),
/does not support image references with usage 'mask'/
);
assert.throws(
() =>
normalizeAdobeReferenceBlobs(kling, [
{ id: "frame-a", usage: "frame" },
{ id: "frame-b", usage: "frame" },
{ id: "frame-c", usage: "frame" },
]),
/at most 2 frame image reference/
);
});
test("structured references skip malformed entries and preserve explicit roles", () => {
assert.deepEqual(
extractAdobeSourceImageReferences({
adobe_reference_inputs: [
null,
{ media_type: "video", source: "ignored" },
{ media_type: "image", source: "data:image/png;base64,AAAA", usage: "frame", order: 2 },
],
}),
[{ source: "data:image/png;base64,AAAA", usage: "frame", order: 2 }]
);
});
test("provider discovery adapter returns live capabilities and verified fallback", async () => {
const live = await getAdobeModels(undefined, userImsJwt(), {}, async () =>
Response.json({
models: [
{
modelId: "firefly-image",
acModelFamilyProviderDisplayName: "Adobe",
modelVersions: {
image5: {
enabled: true,
outputModality: ["image"],
modelDisplayName: "Firefly Image 5",
requestSchema: { type: "object", properties: { prompt: { type: "string" } } },
},
},
},
],
})
);
assert.equal(live.source, "api");
assert.equal(live.models[0].id, "firefly-image-image5");
assert.ok(live.models[0].media_capabilities);
const fallback = await getAdobeModels(undefined, userImsJwt(), {}, async () => {
throw new Error("offline");
});
assert.equal(fallback.source, "local_catalog");
assert.equal(fallback.models.length, 52);
assert.match(fallback.warning || "", /discovery unavailable/);
});

View File

@@ -78,6 +78,11 @@ test("adobe-firefly is registered in IMAGE_PROVIDERS with adobe-firefly-image fo
assert.equal(entry.format, "adobe-firefly-image");
assert.match(entry.baseUrl, /firefly-3p\.ff\.adobe\.io/);
assert.ok(Array.isArray(entry.models) && entry.models.length >= 4);
assert.equal(
entry.models.some((model: { id: string }) => model.id === "nano-banana-pro"),
false,
"routing-only compatibility aliases must not be advertised as discovered models"
);
});
test("adobe-firefly is registered in VIDEO_PROVIDERS with adobe-firefly-video format", () => {
@@ -154,20 +159,25 @@ test("normalizeAdobeOutputResolution maps quality tiers", () => {
assert.equal(normalizeAdobeOutputResolution(undefined, undefined), "2K");
});
test("resolveAdobeImageModel maps catalog and long model ids", () => {
assert.equal(resolveAdobeImageModel("nano-banana-pro").id, "nano-banana-pro");
assert.equal(resolveAdobeImageModel("adobe-firefly/nano-banana-2").id, "nano-banana-2");
assert.equal(resolveAdobeImageModel("firefly-nano-banana-pro-2k-16x9").id, "nano-banana-pro");
assert.equal(resolveAdobeImageModel("gpt-image").id, "gpt-image");
test("resolveAdobeImageModel maps valid aliases to exact discovery ids", () => {
assert.equal(resolveAdobeImageModel("nano-banana-pro").id, "gemini-flash-nano-banana-2");
assert.equal(
resolveAdobeImageModel("adobe-firefly/nano-banana-2").id,
"gemini-flash-nano-banana-3"
);
assert.equal(resolveAdobeImageModel("gpt-image").id, "gpt-image-2");
assert.throws(
() => resolveAdobeImageModel("invented-image-model"),
/Unknown Adobe Firefly image model/
);
assert.ok(ADOBE_FIREFLY_IMAGE_MODELS["nano-banana-pro"].upstreamModelVersion);
});
test("resolveAdobeVideoModel maps sora/veo/kling families", () => {
assert.equal(resolveAdobeVideoModel("sora-2").id, "sora-2");
assert.equal(resolveAdobeVideoModel("firefly-sora2-pro-8s-16x9").id, "sora-2-pro");
assert.equal(resolveAdobeVideoModel("veo-3.1-fast").id, "veo-3.1-fast");
assert.equal(resolveAdobeVideoModel("kling-3").id, "kling-3");
assert.ok(ADOBE_FIREFLY_VIDEO_MODELS["sora-2"].defaultDuration > 0);
test("resolveAdobeVideoModel maps only discovered video models", () => {
assert.equal(resolveAdobeVideoModel("veo-3.1-fast").id, "veo-3.1-fast-generate");
assert.equal(resolveAdobeVideoModel("kling-3").id, "kling-kling-v3-standard-i2v");
assert.throws(() => resolveAdobeVideoModel("sora-2"), /Unknown Adobe Firefly video model/);
assert.ok(ADOBE_FIREFLY_VIDEO_MODELS["veo-3.1"].defaultDuration > 0);
});
test("buildAdobeImagePayload produces nano and gpt-image shapes", () => {
@@ -255,10 +265,7 @@ test("buildAdobeImagePayload attaches referenceBlobs like live adobe_atach_image
{ id: "2a4f1025-e0dc-4671-a11a-7dfd3c07bd94", usage: "general" },
{ id: "84c11d1a-e798-4300-a63e-c06504ca2068", usage: "general" },
]);
assert.equal(
(nano.generationMetadata as Record<string, unknown>).module,
"text2image"
);
assert.equal((nano.generationMetadata as Record<string, unknown>).module, "text2image");
const gpt = buildAdobeImagePayload({
prompt: "edit me",
@@ -268,54 +275,19 @@ test("buildAdobeImagePayload attaches referenceBlobs like live adobe_atach_image
sourceImageIds: ["aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"],
});
assert.deepEqual(gpt.referenceBlobs, [
{ id: "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee", usage: "subject" },
{ id: "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee", usage: "source" },
]);
assert.equal(
(gpt.generationMetadata as Record<string, unknown>).module,
"image2image"
);
// gpt-image: only first 2 subject refs survive (extra screenshots hang colligo).
const gptMany = buildAdobeImagePayload({
prompt: "edit me",
aspectRatio: "1:1",
outputResolution: "1K",
modelSpec: ADOBE_FIREFLY_IMAGE_MODELS["gpt-image-2"],
sourceImageIds: ["id-1", "id-2", "id-3", "id-4", "id-5"],
});
assert.deepEqual(gptMany.referenceBlobs, [
{ id: "id-1", usage: "subject" },
{ id: "id-2", usage: "subject" },
]);
// nano keeps up to 4 general refs for multi-panel composition.
const nanoMany = buildAdobeImagePayload({
prompt: "compose",
aspectRatio: "16:9",
outputResolution: "2K",
modelSpec: ADOBE_FIREFLY_IMAGE_MODELS["nano-banana-2"],
sourceImageIds: ["a", "b", "c", "d", "e"],
});
assert.equal((nanoMany.referenceBlobs as unknown[]).length, 4);
assert.equal((nanoMany.referenceBlobs as Array<{ usage: string }>)[0].usage, "general");
assert.equal((gpt.generationMetadata as Record<string, unknown>).module, "image2image");
});
test("adobeFireflyMaxImageRefs + adaptive image timeout", () => {
assert.equal(adobeFireflyMaxImageRefs("gpt-image-2"), 2);
assert.equal(adobeFireflyMaxImageRefs("adobe-firefly/gpt-image"), 2);
assert.equal(adobeFireflyMaxImageRefs("nano-banana-2"), 4);
assert.equal(adobeFireflyMaxImageRefs("flux-2"), 2);
test("adobeFireflyImageTimeoutMs scales boundedly with reference count", () => {
assert.equal(adobeFireflyImageTimeoutMs({ refCount: 0 }), DEFAULT_IMAGE_TIMEOUT_MS);
assert.equal(
adobeFireflyImageTimeoutMs({ refCount: 2 }),
DEFAULT_IMAGE_TIMEOUT_MS + 2 * ADOBE_FIREFLY_IMAGE_TIMEOUT_PER_REF_MS
);
assert.equal(adobeFireflyImageTimeoutMs({ timeoutMs: 120_000, refCount: 5 }), 120_000);
assert.equal(
adobeFireflyImageTimeoutMs({ refCount: 99 }),
ADOBE_FIREFLY_IMAGE_TIMEOUT_MAX_MS
);
assert.equal(adobeFireflyImageTimeoutMs({ refCount: 99 }), ADOBE_FIREFLY_IMAGE_TIMEOUT_MAX_MS);
});
test("extractAdobeSourceImageSources reads Media page image fields", () => {
@@ -369,10 +341,10 @@ test("resolveAdobeSourceImageIds uploads data URLs then returns blob ids", async
const headers = init?.headers as Record<string, string>;
assert.match(String(headers["content-type"] || headers["Content-Type"] || ""), /image\//);
assert.ok(init?.body);
return new Response(
JSON.stringify({ images: [{ id: `blob-${uploadCalls}` }] }),
{ status: 200, headers: { "content-type": "application/json" } }
);
return new Response(JSON.stringify({ images: [{ id: `blob-${uploadCalls}` }] }), {
status: 200,
headers: { "content-type": "application/json" },
});
}
throw new Error(`unexpected fetch ${u}`);
};
@@ -390,16 +362,7 @@ test("resolveAdobeSourceImageIds uploads data URLs then returns blob ids", async
assert.equal(ADOBE_FIREFLY_IMAGE_UPLOAD_URL.includes("storage/image"), true);
});
test("buildAdobeVideoPayload produces sora and veo shapes", () => {
const sora = buildAdobeVideoPayload({
prompt: "ocean waves",
aspectRatio: "16:9",
duration: 8,
modelSpec: ADOBE_FIREFLY_VIDEO_MODELS["sora-2"],
});
assert.equal(sora.modelId, "sora");
assert.equal(sora.duration, 8);
test("buildAdobeVideoPayload follows discovered fields and reference roles", () => {
const veo = buildAdobeVideoPayload({
prompt: "city flyover",
aspectRatio: "9:16",
@@ -408,12 +371,30 @@ test("buildAdobeVideoPayload produces sora and veo shapes", () => {
});
assert.equal(veo.modelId, "veo");
assert.equal(veo.modelVersion, "3.1-generate");
assert.equal(
(veo.modelSpecificPayload as Record<string, Record<string, unknown>>).parameters
.durationSeconds,
6
);
assert.equal(veo.duration, 6);
assert.equal(veo.generateAudio, true);
const kling = buildAdobeVideoPayload({
prompt: "ocean waves",
aspectRatio: "16:9",
duration: 5,
modelSpec: ADOBE_FIREFLY_VIDEO_MODELS["kling-3"],
sourceImageIds: ["aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"],
});
assert.equal(kling.modelVersion, "kling_v3_standard_i2v");
assert.deepEqual(kling.referenceBlobs, [
{ id: "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee", usage: "frame", order: 1 },
]);
assert.throws(
() =>
buildAdobeVideoPayload({
prompt: "bad duration",
aspectRatio: "16:9",
duration: 5,
modelSpec: ADOBE_FIREFLY_VIDEO_MODELS["veo-3.1"],
}),
/supports duration/
);
});
test("extractAdobeResultLink prefers x-override-status-link then links.result", () => {
@@ -476,8 +457,7 @@ test("buildAdobeSubmitNonce is sha256(user_id + prompt[:256])", async () => {
type: "access_token",
client_id: "clio-playground-web",
})
)
.toString("base64url");
).toString("base64url");
const header = Buffer.from(JSON.stringify({ alg: "none" })).toString("base64url");
const token = `${header}.${payload}.${"x".repeat(40)}`;
// Pad token length for looksLikeAdobeJwt (>=80)
@@ -509,8 +489,7 @@ test("buildAdobeSubmitNonce is sha256(user_id + prompt[:256])", async () => {
});
test("normalizeAdobePollUrl rewrites firefly-epo jobs/result to BKS", () => {
const raw =
"https://firefly-epo855232.adobe.io/jobs/result/4ae9fd2a-0864-46dd-9834-cfc16e91faa6";
const raw = "https://firefly-epo855232.adobe.io/jobs/result/4ae9fd2a-0864-46dd-9834-cfc16e91faa6";
const out = normalizeAdobePollUrl(raw);
assert.match(out, /^https:\/\/bks-epo8552\.adobe\.io\/v2\/jobs\/result\/4ae9fd2a/);
assert.match(out, /host=firefly-epo855232\.adobe\.io/);
@@ -550,7 +529,7 @@ test("adobe-firefly is in USAGE_SUPPORTED_PROVIDERS for Limits", () => {
assert.ok(USAGE_SUPPORTED_PROVIDERS.includes("firefly"));
});
test("parseAdobeModelsDiscovery extracts image/video versions", () => {
test("parseAdobeModelsDiscovery preserves schemas and maps exact ids", () => {
const rows = parseAdobeModelsDiscovery({
models: [
{
@@ -561,16 +540,44 @@ test("parseAdobeModelsDiscovery extracts image/video versions", () => {
outputModality: ["image"],
modelDisplayName: "Gemini 3.0 (Nano Banana Pro)",
healthStatus: "HEALTHY",
inputMediaUseCase: ["editing"],
bksGenerationModel: "firefly_3p:external:gemini_flash_2",
requestSchema: {
type: "object",
properties: {
prompt: { type: "string" },
referenceBlobs: {
maxItems: 14,
"x-capabilities": [
{
mediaType: "image",
usageConstraints: [{ usageType: "general", minItems: 0, maxItems: 14 }],
maxFileSizeBytes: 104857600,
},
],
},
},
},
},
},
},
{
modelId: "sora",
modelId: "veo",
modelVersions: {
"sora-2": {
"3.1-generate": {
enabled: true,
outputModality: ["video"],
modelDisplayName: "Sora 2",
modelDisplayName: "Veo 3.1",
requestSchema: {
allOf: [
{
properties: {
prompt: { type: "string" },
duration: { anyOf: [{ type: "integer", enum: [4, 6, 8] }] },
},
},
],
},
},
},
},
@@ -580,21 +587,42 @@ test("parseAdobeModelsDiscovery extracts image/video versions", () => {
assert.equal(rows[0].modality, "image");
assert.equal(rows[1].modality, "video");
const catalog = mapDiscoveredToCatalog(rows);
assert.ok(catalog.some((m) => m.id === "nano-banana-pro"));
assert.ok(catalog.some((m) => m.id === "sora-2"));
assert.ok(catalog.some((m) => m.id === "gemini-flash-nano-banana-2"));
assert.ok(catalog.some((m) => m.id === "veo-3.1-generate"));
assert.equal(catalog[0].capabilities.referenceInputs[0].maxItems, 14);
assert.deepEqual(catalog[1].capabilities.supportedDurations, [4, 6, 8]);
});
test("fallback catalog has image and video entries from get_models capture", () => {
assert.ok(ADOBE_FIREFLY_FALLBACK_MODELS.length >= 10);
assert.ok(getAdobeFireflyFallbackCatalog("image").length >= 4);
assert.ok(getAdobeFireflyFallbackCatalog("video").length >= 4);
test("fallback catalog is the verified discovery snapshot without invented Sora", () => {
assert.equal(ADOBE_FIREFLY_FALLBACK_MODELS.length, 52);
assert.equal(getAdobeFireflyFallbackCatalog("image").length, 17);
assert.equal(getAdobeFireflyFallbackCatalog("video").length, 35);
assert.equal(
ADOBE_FIREFLY_FALLBACK_MODELS.some((model) => model.id.includes("sora")),
false
);
assert.equal(
ADOBE_FIREFLY_FALLBACK_MODELS.some(
(model) => model.id.includes("kling") && model.id.includes("omni")
),
false
);
assert.ok(ADOBE_FIREFLY_FALLBACK_MODELS.some((model) => model.id === "kling-kling-o3"));
assert.equal(
ADOBE_FIREFLY_IMAGE_MODELS["nano-banana-pro"].capabilities.referenceInputs[0].maxItems,
14
);
assert.equal(
ADOBE_FIREFLY_IMAGE_MODELS["gpt-image"].capabilities.referenceInputs[0].maxItems,
16
);
});
test("extractAdobeAccountIdFromToken reads user_id claim", () => {
// {"user_id":"0EB@AdobeID"} base64url
const payload = Buffer.from(JSON.stringify({ user_id: "0EB@AdobeID", type: "access_token" })).toString(
"base64url"
);
const payload = Buffer.from(
JSON.stringify({ user_id: "0EB@AdobeID", type: "access_token" })
).toString("base64url");
const jwt = `eyJhbGciOiJub25lIn0.${payload}.sig`;
assert.equal(extractAdobeAccountIdFromToken(jwt), "0EB@AdobeID");
});
@@ -602,18 +630,7 @@ test("extractAdobeAccountIdFromToken reads user_id claim", () => {
// --- Handlers (mocked fetch) ----------------------------------------------
function jsonResponse(status: number, body: unknown, headerMap: Record<string, string> = {}) {
return {
ok: status >= 200 && status < 300,
status,
headers: {
get: (name: string) => {
const key = Object.keys(headerMap).find((k) => k.toLowerCase() === name.toLowerCase());
return key ? headerMap[key] : null;
},
},
json: async () => body,
text: async () => JSON.stringify(body),
} as unknown as Response;
return new Response(JSON.stringify(body) ?? null, { status, headers: headerMap });
}
test("handleAdobeFireflyImageGeneration returns 400 when prompt is missing", async () => {
@@ -738,7 +755,7 @@ test("adobeFireflyGenerateVideo submit+poll happy path (mocked)", async () => {
const result = await adobeFireflyGenerateVideo({
accessToken: "tok",
prompt: "drone over forest",
model: "sora-2",
model: "veo-3.1",
duration: 4,
aspectRatio: "16:9",
fetchImpl: fetchImpl as typeof fetch,
@@ -749,7 +766,7 @@ test("adobeFireflyGenerateVideo submit+poll happy path (mocked)", async () => {
test("handleAdobeFireflyVideoGeneration returns 400 without prompt", async () => {
const result = await handleAdobeFireflyVideoGeneration({
model: "sora-2",
model: "veo-3.1",
provider: "adobe-firefly",
body: {},
credentials: { apiKey: "aaa.bbb.ccc" },
@@ -779,13 +796,19 @@ test("guest JWT without AdobeID is detected", () => {
const emptyPayload = Buffer.from("{}").toString("base64url");
const guestJwt = `eyJhbGciOiJub25lIn0.${emptyPayload}.sig`;
// Pad to lookLikeAdobeJwt length if needed
const longGuest = `eyJhbGciOiJSUzI1NiJ9.${Buffer.from(JSON.stringify({ client_id: "clio-playground-web" })).toString("base64url")}.` + "x".repeat(40);
const longGuest =
`eyJhbGciOiJSUzI1NiJ9.${Buffer.from(JSON.stringify({ client_id: "clio-playground-web" })).toString("base64url")}.` +
"x".repeat(40);
assert.equal(isAdobeGuestAccessToken(longGuest), true);
const userJwt =
`eyJhbGciOiJSUzI1NiJ9.` +
Buffer.from(JSON.stringify({ user_id: "0EB@AdobeID", type: "access_token", client_id: "clio-playground-web" })).toString(
"base64url"
) +
Buffer.from(
JSON.stringify({
user_id: "0EB@AdobeID",
type: "access_token",
client_id: "clio-playground-web",
})
).toString("base64url") +
`.` +
"y".repeat(40);
assert.equal(isAdobeGuestAccessToken(userJwt), false);
@@ -832,7 +855,13 @@ test("cookie exchange rejects guest IMS tokens", async () => {
});
test("isAdobeTransientSubmitError detects 408 system under load", () => {
assert.equal(isAdobeTransientSubmitError(408, '{"error_code":"timeout_error","message":"system under load"}'), true);
assert.equal(
isAdobeTransientSubmitError(
408,
'{"error_code":"timeout_error","message":"system under load"}'
),
true
);
assert.equal(isAdobeTransientSubmitError(429, "rate"), true);
assert.equal(isAdobeTransientSubmitError(400, "bad request"), false);
assert.ok(generateAdobeNonce().length === 64);
@@ -880,11 +909,7 @@ test("image submit retries on 408 then succeeds", async () => {
if (submits < 3) {
return jsonResponse(408, { error_code: "timeout_error", message: "system under load" });
}
return jsonResponse(
200,
{ links: { result: { href: "https://poll.example/job/r1" } } },
{}
);
return jsonResponse(200, { links: { result: { href: "https://poll.example/job/r1" } } }, {});
}
if (u.includes("poll.example")) {
return jsonResponse(200, {
@@ -909,7 +934,11 @@ test("adobeFireflyGenerateImage cookie path exchanges IMS token first", async ()
const userTok =
`eyJhbGciOiJSUzI1NiJ9.` +
Buffer.from(
JSON.stringify({ user_id: "0EB@AdobeID", type: "access_token", client_id: "clio-playground-web" })
JSON.stringify({
user_id: "0EB@AdobeID",
type: "access_token",
client_id: "clio-playground-web",
})
).toString("base64url") +
`.` +
"s".repeat(40);
@@ -931,11 +960,7 @@ test("adobeFireflyGenerateImage cookie path exchanges IMS token first", async ()
? (init.headers as Record<string, string>).Authorization
: auth;
assert.equal(headerAuth, `Bearer ${userTok}`);
return jsonResponse(
200,
{},
{ "x-override-status-link": "https://poll.example/job/c1" }
);
return jsonResponse(200, {}, { "x-override-status-link": "https://poll.example/job/c1" });
}
if (String(url).includes("poll.example")) {
return jsonResponse(200, {

View File

@@ -1,10 +1,25 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-claude-rendering-"));
const previousDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = TEST_DATA_DIR;
const { openaiResponsesToOpenAIResponse } =
await import("../../open-sse/translator/response/openai-responses.ts");
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
const { createSSETransformStreamWithLogger } = await import("../../open-sse/utils/stream.ts");
const { resetDbInstance } = await import("../../src/lib/db/core.ts");
test.after(() => {
resetDbInstance();
if (previousDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = previousDataDir;
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
test("Responses->Chat: output_item.done emits arguments when no delta chunks were sent", () => {
const state = {
@@ -38,7 +53,7 @@ test("Responses->Chat: output_item.done emits arguments when no delta chunks wer
assert.equal(state.toolCallIndex, 1);
});
test("Responses->Chat: output_item.done does not re-emit arguments already streamed via deltas", () => {
test("Responses->Chat: buffered argument deltas emit once at output_item.done", () => {
const state = {
started: true,
chatId: "chatcmpl-test",
@@ -46,9 +61,19 @@ test("Responses->Chat: output_item.done does not re-emit arguments already strea
toolCallIndex: 0,
finishReasonSent: false,
currentToolCallId: "call_abc",
currentToolCallArgsBuffer: '{"query":"search"}',
currentToolCallArgsBuffer: "",
};
const deltaResult = openaiResponsesToOpenAIResponse(
{
type: "response.function_call_arguments.delta",
delta: '{"query":"search"}',
},
state
);
assert.equal(deltaResult, null);
const chunk = {
type: "response.output_item.done",
item: {
@@ -62,7 +87,8 @@ test("Responses->Chat: output_item.done does not re-emit arguments already strea
const result = openaiResponsesToOpenAIResponse(chunk, state);
assert.equal(result, null);
assert.ok(result);
assert.equal(result.choices[0].delta.tool_calls[0].function.arguments, '{"query":"search"}');
assert.equal(state.toolCallIndex, 1);
});

View File

@@ -69,7 +69,7 @@ test("ServerSupervisor starts Node with IPv4-first DNS", async () => {
assert.deepEqual(spawnCalls, [
{
command: "node",
command: process.execPath,
args: ["--dns-result-order=ipv4first", "--max-old-space-size=2048", "/app/server.js"],
},
]);

View File

@@ -80,6 +80,8 @@ test("#7849: the two-message pathological pair stays bounded", () => {
Date.now() - started < 4000,
"the pathological pair must stay fast; quadratic work would take seconds"
);
assert.strictEqual(result.body, body, "bounded processing must preserve the input body");
assert.equal(result.compressed, false, "the non-deduplicable pair must fail open");
assert.ok(Array.isArray((result.body as { messages?: unknown[] }).messages));
});

View File

@@ -0,0 +1,258 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import vm from "node:vm";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import {
CHALLENGE_STUBS,
buildHtmlLookup,
countHtmlElements,
sha256Base64,
solveDuckDuckGoChallenge,
DUCKDUCKGO_CHALLENGE_ORIGIN,
} from "../../open-sse/executors/duckduckgo-web/challenge.ts";
/**
* Regression suite for the DuckDuckGo AI Chat anti-abuse challenge solver.
*
* Background: every duckduckgo-web chat request was failing with HTTP 418
* ERR_CHALLENGE while duck.ai worked normally in a browser from the same IP.
* Root-causing it turned up several independent defects, each of which is
* pinned below. The fixtures in `tests/fixtures/duckduckgo/challenge-variants.json`
* are REAL challenge programs captured from duckduckgo.com, together with the
* probe vectors a real (headful) Chromium produced for those exact programs.
* Matching Chromium bit-for-bit is the actual correctness criterion, so these
* tests assert against recorded browser behaviour rather than our own output.
*/
const HERE = dirname(fileURLToPath(import.meta.url));
const FIXTURES = join(HERE, "../fixtures/duckduckgo/challenge-variants.json");
type Variant = {
challengeBase64: string;
browserProbes: string[];
browserReduceVectors: Array<{ seed: number; booleans: number[] }>;
};
const VARIANTS = JSON.parse(readFileSync(FIXTURES, "utf8")) as Record<string, Variant>;
const UA =
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/151.0.0.0 Safari/537.36";
function makeContext(challengeJs: string): vm.Context {
const stubs = CHALLENGE_STUBS.replace("__DDG_REAL_UA__", JSON.stringify(UA)).replace(
"__DDG_HTML_LOOKUP__",
JSON.stringify(buildHtmlLookup(challengeJs))
);
const context = vm.createContext({});
vm.runInContext(stubs, context, { timeout: 5000 });
return context;
}
// ---------------------------------------------------------------------------
// Bug 1 — module syntax inside the sandbox source.
// `vm.runInContext` compiles in SCRIPT mode. A refactor mass-added `export` to
// the `function` declarations inside CHALLENGE_STUBS (they look like ordinary
// top-level TS functions), making every solve throw SyntaxError. The executor
// swallows solve failures and posts the raw unsolved challenge, so the upstream
// answered 418 for every request.
// ---------------------------------------------------------------------------
test("CHALLENGE_STUBS uses no module syntax and compiles in script mode", () => {
assert.doesNotMatch(
CHALLENGE_STUBS,
/(^|[\s;{}])(export|import)[\s{*]/,
"vm.runInContext compiles in script mode — export/import is a hard SyntaxError"
);
const source = CHALLENGE_STUBS.replace("__DDG_REAL_UA__", '"ua"').replace(
"__DDG_HTML_LOOKUP__",
"{}"
);
assert.doesNotThrow(() => new vm.Script(source));
});
// ---------------------------------------------------------------------------
// Bug 2 — regex escaping inside a String.raw template.
// CHALLENGE_STUBS is a String.raw literal, so `\\s` reaches the sandbox as a
// literal backslash-backslash-s and the display regex never matched. One
// challenge variant asserts getComputedStyle(el).getPropertyValue('display')
// is non-empty, so this silently flipped a probe to false.
// ---------------------------------------------------------------------------
test("computed-style display probe resolves a real value", () => {
const context = makeContext("");
const display = vm.runInContext(
`(function(){
var d = document.createElement('div');
d.style.cssText = 'display:inline-block;padding:8px;position:absolute;visibility:hidden;';
return getComputedStyle(d).getPropertyValue('display');
})()`,
context
);
assert.equal(display, "inline-block");
});
test("CHALLENGE_STUBS contains no double-escaped regex metacharacters", () => {
// String.raw means `\\s` in the source IS `\\s` in the sandbox — always a bug.
assert.doesNotMatch(CHALLENGE_STUBS, /\\\\[sdwbSDWB]/);
});
// ---------------------------------------------------------------------------
// Bug 3 — buildHtmlLookup descendant count was off by one.
// `count` backs `el.querySelectorAll('*').length` for an element whose
// innerHTML is the given markup. querySelectorAll('*') returns DESCENDANTS, and
// countHtmlElements already skips the #document-fragment root, so subtracting 1
// undercounted. A variant multiplies innerHTML.length by that count, so the
// error propagated straight into the hash.
// ---------------------------------------------------------------------------
test("buildHtmlLookup reports the browser's descendant count", () => {
// Chromium: for innerHTML = '<li><div></li><li></div', querySelectorAll('*')
// has length 3 and innerHTML serializes to 29 characters.
const html = "<li><div></li><li></div";
const entry = buildHtmlLookup(`x = "${html}"`)[html];
assert.equal(entry.html, "<li><div></div></li><li></li>");
assert.equal(entry.html.length, 29);
assert.equal(entry.count, 3);
assert.equal(countHtmlElements({ nodeName: undefined, childNodes: [] }), 0);
});
// ---------------------------------------------------------------------------
// Bug 4 — the browser-fidelity probes.
// Newer challenge variants interrogate JS/DOM invariants that a naive stub
// object does not satisfy (prototype chains, NodeList identity, live
// HTMLCollection, native-code toString, sloppy-mode `this`). Nine of thirteen
// failed. Each is pinned individually so a future stub regression names itself.
// ---------------------------------------------------------------------------
const FIDELITY_PROBES: Array<[string, string, boolean]> = [
[
"built-ins stringify as native code",
`window.parseInt.toString().includes("[native code]")`,
true,
],
[
"Array subclass survives map",
`(function(){ class S extends Array {}; return new S(1,2,3).map(function(x){return x*2;}) instanceof S; })()`,
true,
],
[
"window brands as [object Window]",
`Object.prototype.toString.call(window) === "[object Window]"`,
true,
],
["Error instances are real", `new Error() instanceof Error`, true],
[
"captureStackTrace is absent or a function",
`Error.captureStackTrace === undefined || typeof Error.captureStackTrace === "function"`,
true,
],
// Chromium reports false here; sealing Math made our vector differ by one.
["Math is NOT sealed (matches Chromium)", `Object.isSealed(Math)`, false],
["sloppy-mode this is window", `(function(){ return this; })() === window`, true],
[
"document.body.children is live",
`(function(){
var c = document.body.children, n = c.length, d = document.createElement('div');
document.body.appendChild(d);
var grew = c.length === n + 1;
document.body.removeChild(d);
return grew && c.length === n;
})()`,
true,
],
["querySelectorAll is not an Array", `!Array.isArray(document.querySelectorAll("*"))`, true],
[
"querySelectorAll is a NodeList",
`document.querySelectorAll("*").constructor.name === "NodeList"`,
true,
],
[
"createElement('div') is an HTMLDivElement",
`document.createElement("div") instanceof HTMLDivElement`,
true,
],
[
"HTMLDivElement derives from HTMLElement",
`HTMLDivElement.prototype instanceof HTMLElement`,
true,
],
["HTMLElement derives from Element", `HTMLElement.prototype instanceof Element`, true],
["navigator.webdriver is falsy", `navigator.webdriver === true`, false],
["navigator survives the global aliasing", `navigator.userAgent === ${JSON.stringify(UA)}`, true],
["window.document is the document", `window.document === document`, true],
];
for (const [name, expression, expected] of FIDELITY_PROBES) {
test(`browser-fidelity probe: ${name}`, () => {
const context = makeContext("");
assert.equal(vm.runInContext(expression, context), expected);
});
}
// ---------------------------------------------------------------------------
// The real acceptance criterion: for every captured challenge variant our
// sandbox must produce exactly the probe values a real Chromium produced.
// ---------------------------------------------------------------------------
for (const [file, variant] of Object.entries(VARIANTS)) {
test(`challenge variant ${file} matches real-browser probe values`, async () => {
const js = Buffer.from(variant.challengeBase64, "base64").toString("utf8");
const context = makeContext(js);
const result = (await vm.runInContext(js, context, { timeout: 5000 })) as {
client_hashes: unknown[];
};
// `result` crosses the vm realm boundary, so its arrays carry the sandbox's
// Array.prototype. Copy into this realm or deepStrictEqual fails on the
// prototype even when every element matches.
const ours = Array.from(result.client_hashes).slice(1).map(String);
assert.deepEqual(
ours,
variant.browserProbes,
`probe values must match Chromium exactly for ${file}`
);
});
}
// ---------------------------------------------------------------------------
// Bug 5 — the solved payload dropped meta.origin / meta.stack / meta.duration.
// The duck.ai frontend always sends all three; captured browser requests
// confirm it. Without them the upstream returns 418 even when every
// client_hash is correct.
// ---------------------------------------------------------------------------
test("solveDuckDuckGoChallenge stamps meta.origin/stack/duration", async () => {
const [variant] = Object.values(VARIANTS);
const solved = await solveDuckDuckGoChallenge(variant.challengeBase64, UA);
const decoded = JSON.parse(Buffer.from(solved, "base64").toString("utf8"));
assert.equal(decoded.meta.origin, DUCKDUCKGO_CHALLENGE_ORIGIN);
assert.match(decoded.meta.stack, /^Error\n\s*at l \(https:\/\/duck\.ai\/.*\.js:\d+:\d+\)/);
assert.match(String(decoded.meta.duration), /^\d+$/);
// The challenge's own meta must survive alongside the added fields.
assert.equal(decoded.meta.v, "4");
assert.ok(decoded.meta.challenge_id);
});
test("solveDuckDuckGoChallenge honours an explicit origin/bundle", async () => {
const [variant] = Object.values(VARIANTS);
const solved = await solveDuckDuckGoChallenge(variant.challengeBase64, UA, {
origin: "https://duckduckgo.com",
bundlePath: "/dist/x.js",
});
const decoded = JSON.parse(Buffer.from(solved, "base64").toString("utf8"));
assert.equal(decoded.meta.origin, "https://duckduckgo.com");
assert.ok(decoded.meta.stack.includes("https://duckduckgo.com/dist/x.js"));
});
test("solveDuckDuckGoChallenge hashes client_hashes with the real UA in slot 0", async () => {
const [file, variant] = Object.entries(VARIANTS)[0];
const solved = await solveDuckDuckGoChallenge(variant.challengeBase64, UA);
const decoded = JSON.parse(Buffer.from(solved, "base64").toString("utf8"));
const expected = [sha256Base64(UA), ...variant.browserProbes.map((p) => sha256Base64(p))];
assert.deepEqual(decoded.client_hashes, expected, `client_hashes mismatch for ${file}`);
// server_hashes are echoed back untouched.
assert.ok(Array.isArray(decoded.server_hashes));
});
test("solveDuckDuckGoChallenge rejects a challenge with no client_hashes", async () => {
const bad = Buffer.from(`(async function(){ return { client_hashes: [] }; })()`, "utf8").toString(
"base64"
);
await assert.rejects(() => solveDuckDuckGoChallenge(bad, UA), /empty client_hashes/);
});

View File

@@ -3,6 +3,7 @@ import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import vm from "node:vm";
// Split-guard for the duckduckgo-web challenge-solver extraction.
// The anti-abuse challenge solver + FE signals live in duckduckgo-web/challenge.ts
@@ -35,3 +36,81 @@ test("makeDuckDuckGoFeSignals returns a base64 string", async () => {
assert.equal(typeof out, "string");
assert.ok(out.length > 0);
});
// Regression guard: CHALLENGE_STUBS is browser-emulation source executed by
// `vm.runInContext`, which compiles in SCRIPT mode — module syntax is a hard
// SyntaxError there. A refactor once mass-added `export` to the `function`
// declarations inside this template literal (they look like ordinary top-level
// TS functions), which made every solve throw. The executor swallows solve
// failures and sends the raw unsolved challenge, so DuckDuckGo answered every
// chat request with HTTP 418 ERR_CHALLENGE while the site worked fine in a
// browser from the same IP. The three tests below fail on that class of bug.
test("CHALLENGE_STUBS contains no module syntax (vm runs it in script mode)", async () => {
const { CHALLENGE_STUBS } = await import("../../open-sse/executors/duckduckgo-web/challenge.ts");
assert.doesNotMatch(
CHALLENGE_STUBS,
/(^|[\s;{}])(export|import)[\s{*]/,
"CHALLENGE_STUBS must not use export/import — vm.runInContext compiles in script mode"
);
});
test("CHALLENGE_STUBS compiles as a script", async () => {
const { CHALLENGE_STUBS } = await import("../../open-sse/executors/duckduckgo-web/challenge.ts");
// Placeholders are substituted before execution; do the same here so the
// source is syntactically complete.
const source = CHALLENGE_STUBS.replace("__DDG_REAL_UA__", '"test-ua"').replace(
"__DDG_HTML_LOOKUP__",
"{}"
);
assert.doesNotThrow(() => new vm.Script(source), "CHALLENGE_STUBS must parse in script mode");
});
test("CHALLENGE_STUBS evaluates and defines the browser stubs the challenge probes", async () => {
const { CHALLENGE_STUBS } = await import("../../open-sse/executors/duckduckgo-web/challenge.ts");
const source = CHALLENGE_STUBS.replace("__DDG_REAL_UA__", '"test-ua"').replace(
"__DDG_HTML_LOOKUP__",
"{}"
);
const context = vm.createContext({});
vm.runInContext(source, context, { timeout: 5000 });
// A real DDG challenge reads these; if the stubs silently failed to evaluate
// they would all be undefined and the solver would produce garbage.
assert.equal(
vm.runInContext("navigator.userAgent", context),
"test-ua",
"navigator.userAgent must carry the injected UA"
);
assert.equal(vm.runInContext("typeof document.querySelector", context), "function");
assert.equal(vm.runInContext("document.getElementById('jsa').tagName", context), "IFRAME");
assert.equal(vm.runInContext("typeof getComputedStyle", context), "function");
assert.equal(vm.runInContext("window.top === window", context), true);
});
// End-to-end guard on the solver itself, using a stand-in challenge that mimics
// the real one's contract: an async IIFE returning { server_hashes, client_hashes,
// signals, meta }. This exercises the full stubs -> vm -> hash -> base64 path
// without hitting the network.
test("solveDuckDuckGoChallenge solves a representative challenge payload", async () => {
const { solveDuckDuckGoChallenge, sha256Base64 } =
await import("../../open-sse/executors/duckduckgo-web/challenge.ts");
const fakeChallenge = `(async function(){
return {
server_hashes: ["s1", "s2"],
client_hashes: [navigator.userAgent, document.getElementById('jsa').tagName],
signals: {},
meta: { v: "4", challenge_id: "test" }
};
})()`;
const ua = "Mozilla/5.0 (X11; Linux x86_64) TestAgent/1.0";
const solved = await solveDuckDuckGoChallenge(
Buffer.from(fakeChallenge, "utf8").toString("base64"),
ua
);
const decoded = JSON.parse(Buffer.from(solved, "base64").toString("utf8"));
assert.deepEqual(decoded.server_hashes, ["s1", "s2"], "server_hashes pass through untouched");
// Slot 0 is overwritten with the real UA before hashing, then every slot is sha256+base64.
assert.deepEqual(decoded.client_hashes, [sha256Base64(ua), sha256Base64("IFRAME")]);
assert.equal(decoded.meta.challenge_id, "test");
});

View File

@@ -0,0 +1,134 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { DuckDuckGoWebExecutor } from "../../open-sse/executors/duckduckgo-web.ts";
/**
* Regression: duckchat/v1/chat now REQUIRES a `reasoningEffort` field.
*
* The executor previously omitted it for most models on the assumption that the
* upstream would apply its own default. It does not: an otherwise byte-identical
* payload returns 200 with the field and 400 ERR_BAD_REQUEST without it
* (A/B verified live against duck.ai, repeated). The live duck.ai bundle always
* sends one, so every outgoing payload must carry it.
*
* These tests capture the executor's real outgoing request body by stubbing
* fetch, so they assert on the wire format rather than on internal helpers.
*/
type Captured = { url: string; body: Record<string, unknown> };
async function captureChatPayload(model: string): Promise<Captured> {
const realFetch = globalThis.fetch;
const captured: Captured[] = [];
globalThis.fetch = (async (input: unknown, init: RequestInit = {}) => {
const url = typeof input === "string" ? input : String((input as { url?: string })?.url ?? "");
if (url.includes("/duckchat/v1/status")) {
// Hand back a trivially solvable challenge so the executor proceeds to the
// chat POST without touching the network.
const challenge = Buffer.from(
`(async function(){ return { server_hashes: [], client_hashes: ["ua"], signals: {}, meta: {} }; })()`,
"utf8"
).toString("base64");
return new Response("{}", { status: 200, headers: { "x-vqd-hash-1": challenge } });
}
if (url.includes("/duckchat/v1/chat")) {
captured.push({ url, body: JSON.parse(String(init.body)) });
return new Response(`data: {"action":"success","message":"OK"}\n\ndata: [DONE]\n\n`, {
status: 200,
headers: { "Content-Type": "text/event-stream" },
});
}
// Warm-up fetches (homepage, country.json, auth/token).
return new Response("", { status: 200 });
}) as typeof globalThis.fetch;
try {
const executor = new DuckDuckGoWebExecutor();
await executor.execute({
model,
body: { messages: [{ role: "user", content: "Say OK" }] },
stream: false,
} as never);
} finally {
globalThis.fetch = realFetch;
}
assert.ok(captured.length > 0, "executor never issued a chat request");
return captured[captured.length - 1];
}
test("chat payload always carries reasoningEffort", async () => {
const { body } = await captureChatPayload("duckduckgo-web/gpt-4o-mini");
assert.ok(
Object.prototype.hasOwnProperty.call(body, "reasoningEffort"),
"omitting reasoningEffort yields 400 ERR_BAD_REQUEST upstream"
);
assert.equal(typeof body.reasoningEffort, "string");
assert.notEqual(body.reasoningEffort, "");
});
test("default models send reasoningEffort 'none'", async () => {
const { body } = await captureChatPayload("duckduckgo-web/gpt-5.4-mini");
assert.equal(body.model, "gpt-5.4-mini");
assert.equal(body.reasoningEffort, "none");
});
test("reasoning models keep their 'low' effort", async () => {
const haiku = await captureChatPayload("duckduckgo-web/claude-haiku-4-5");
assert.equal(haiku.body.model, "claude-haiku-4-5");
assert.equal(haiku.body.reasoningEffort, "low");
const oss = await captureChatPayload("duckduckgo-web/gpt-oss-120b");
assert.equal(oss.body.model, "tinfoil/gpt-oss-120b");
assert.equal(oss.body.reasoningEffort, "low");
});
test("retired model ids are still aliased to live wire ids", async () => {
const { body } = await captureChatPayload("duckduckgo-web/gpt-4o-mini");
assert.equal(body.model, "gpt-5.4-mini");
});
test("executor issues exactly one chat request per call", async () => {
// A throwaway "seed" chat POST used to run before the real one, doubling the
// request volume against an IP-rate-limited endpoint and causing spurious 429s.
const realFetch = globalThis.fetch;
let chatCalls = 0;
globalThis.fetch = (async (input: unknown, init: RequestInit = {}) => {
const url = typeof input === "string" ? input : String((input as { url?: string })?.url ?? "");
if (url.includes("/duckchat/v1/status")) {
const challenge = Buffer.from(
`(async function(){ return { server_hashes: [], client_hashes: ["ua"], signals: {}, meta: {} }; })()`,
"utf8"
).toString("base64");
return new Response("{}", { status: 200, headers: { "x-vqd-hash-1": challenge } });
}
if (url.includes("/duckchat/v1/chat")) {
chatCalls++;
void init;
return new Response(`data: {"action":"success","message":"OK"}\n\ndata: [DONE]\n\n`, {
status: 200,
headers: { "Content-Type": "text/event-stream" },
});
}
return new Response("", { status: 200 });
}) as typeof globalThis.fetch;
try {
const executor = new DuckDuckGoWebExecutor();
await executor.execute({
model: "duckduckgo-web/gpt-5.4-mini",
body: { messages: [{ role: "user", content: "Say OK" }] },
stream: false,
} as never);
} finally {
globalThis.fetch = realFetch;
}
assert.equal(chatCalls, 1, "expected exactly one POST /duckchat/v1/chat per user request");
});

View File

@@ -97,6 +97,21 @@ test("package.json files[] excludes nested node_modules from the published packa
);
});
test("build-next-isolated sibling imports are allowed in the published package", () => {
const buildDependencies = [
"scripts/build/assembleStandalone.mjs",
"scripts/build/backendOnlyPages.mjs",
"scripts/build/build-tproxy-native.mjs",
];
const unexpectedPaths = findUnexpectedArtifactPaths(buildDependencies, {
exactPaths: PACK_ARTIFACT_ALLOWED_EXACT_PATHS,
prefixPaths: PACK_ARTIFACT_ALLOWED_PATH_PREFIXES,
});
assert.deepEqual(unexpectedPaths, []);
});
test("webdav-handler.mjs is allowed in staging dist/ (server-ws.mjs dependency, missed in 3.8.22 build)", () => {
const unexpectedPaths = findUnexpectedArtifactPaths(["webdav-handler.mjs"], {
exactPaths: APP_STAGING_ALLOWED_EXACT_PATHS,

View File

@@ -212,7 +212,7 @@ test("#8969: mocked execute posts Chat Completions with Bearer, no Cookie, strip
globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => {
const headers = new Headers(init?.headers);
const rawBody = typeof init?.body === "string" ? init.body : "{}";
const rawBody = await new Request(input, init).text();
seen.push({
url: String(input),
method: (init?.method || "GET").toUpperCase(),

View File

@@ -0,0 +1,167 @@
/**
* TDD regression guard — quality validation false-positive on benign `error`
* fields in streaming SSE chunks.
*
* `isStreamingUpstreamError` treats ANY non-null `error` field as an upstream
* failure: `parsed.error != null` is true for `{}`, `""`, `false`, and `0`.
* When a client like opencode issues a tool-call turn, the upstream SSE opens
* with role-only frames (no recognized content) and a later chunk that carries
* real tool_calls content PLUS a benign empty `error` field (a field some
* backends emit on every chunk). The error gate runs BEFORE the content
* recognizers, so that single frame short-circuits to "error" → 502
* "streaming upstream error" — while the same combo via kilocode (different
* wire format) never emits the empty `error` field and works fine.
*/
import test from "node:test";
import assert from "node:assert/strict";
const { validateResponseQuality } = await import("../../open-sse/services/combo.ts");
const encoder = new TextEncoder();
const silentLog = { warn: () => {} };
function openAiSseStream(events: string[]): ReadableStream<Uint8Array> {
const body = events.join("\n") + "\n";
return new ReadableStream<Uint8Array>({
start(controller) {
controller.enqueue(encoder.encode(body));
controller.close();
},
});
}
/**
* OpenAI-compatible tool-call stream that ALSO carries a benign empty `error`
* field on the tool_calls chunk. Some backends emit `"error": {}` or
* `"error": ""` alongside every chunk; that is not a real upstream failure.
* The frame must be treated as CONTENT (valid), not ERROR.
*/
function makeToolCallStreamWithBenignError(): Response {
const events = [
// role-only first chunk — no recognized content, widens the peek window
`data: ${JSON.stringify({
id: "chatcmpl_1",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
})}`,
"",
// tool_calls delta + benign empty `error` field (the bug trigger)
`data: ${JSON.stringify({
id: "chatcmpl_2",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [
{
index: 0,
delta: {
tool_calls: [
{ index: 0, id: "call_1", type: "function", function: { name: "Bash", arguments: "" } },
],
},
finish_reason: null,
},
],
error: {},
})}`,
"",
`data: [DONE]`,
"",
];
return new Response(openAiSseStream(events), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
}
test("OpenAI stream with tool_calls + benign empty error:{} field is VALID (not 502)", async () => {
const res = makeToolCallStreamWithBenignError();
const out = await validateResponseQuality(res, true, silentLog);
assert.equal(
out.valid,
true,
`expected valid for tool_calls chunk with benign error:{}, got valid=false (reason: ${out.reason})`
);
assert.ok(out.clonedResponse, "clonedResponse must be present for valid streaming response");
});
test("OpenAI stream with tool_calls + benign empty error:'' field is VALID", async () => {
const events = [
`data: ${JSON.stringify({
id: "chatcmpl_3",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
})}`,
"",
`data: ${JSON.stringify({
id: "chatcmpl_4",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [
{
index: 0,
delta: {
tool_calls: [
{ index: 0, id: "call_2", type: "function", function: { name: "Read", arguments: "" } },
],
},
finish_reason: null,
},
],
error: "",
})}`,
"",
`data: [DONE]`,
"",
];
const res = new Response(openAiSseStream(events), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
const out = await validateResponseQuality(res, true, silentLog);
assert.equal(
out.valid,
true,
`expected valid for tool_calls chunk with benign error:"", got valid=false (reason: ${out.reason})`
);
});
test("Stream with a REAL non-empty error object is still flagged as invalid", async () => {
const events = [
`data: ${JSON.stringify({
id: "chatcmpl_5",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [{ index: 0, delta: { role: "assistant" }, finish_reason: null }],
})}`,
"",
`data: ${JSON.stringify({
id: "chatcmpl_6",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4o",
choices: [{ index: 0, delta: {}, finish_reason: null }],
error: { message: "upstream quota exceeded", code: "rate_limit_exceeded" },
})}`,
"",
`data: [DONE]`,
"",
];
const res = new Response(openAiSseStream(events), {
status: 200,
headers: { "content-type": "text/event-stream" },
});
const out = await validateResponseQuality(res, true, silentLog);
assert.equal(
out.valid,
false,
`expected invalid for real error object, got valid=true (reason: ${out.reason})`
);
assert.match(out.reason ?? "", /streaming upstream error/, "reason should mention the upstream error");
});

View File

@@ -26,6 +26,12 @@ function wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Leave enough scheduling headroom for a loaded CI/devbox while keeping the
// executing callback longer than the queue-only budget. The actual queued-job
// case stays short because it controls dispatch deterministically.
const DISPATCHED_QUEUE_BUDGET_MS = 2_000;
const QUEUED_QUEUE_BUDGET_MS = 250;
test.afterEach(async () => {
await rateLimitManager.__resetRateLimitManagerForTests();
});
@@ -43,14 +49,23 @@ async function triggerQueueTimeout() {
concurrentRequests: 1,
requestsPerMinute: 100000,
minTimeBetweenRequestsMs: 0,
maxWaitMs: 40,
maxWaitMs: DISPATCHED_QUEUE_BUDGET_MS,
});
rateLimitManager.enableRateLimitProtection("conn-queue-timeout");
const connectionId = "conn-dispatched-timeout";
rateLimitManager.enableRateLimitProtection(connectionId);
return rateLimitManager.withRateLimit("openai", "conn-queue-timeout", "gpt-4o", async () => {
await wait(400); // > maxWaitMs (40ms) → Bottleneck fails the job
return "should-not-reach";
});
let dispatched = false;
const result = await rateLimitManager.withRateLimit(
"test-provider",
connectionId,
null,
async () => {
dispatched = true;
await wait(DISPATCHED_QUEUE_BUDGET_MS + 250);
return "should-not-reach";
}
);
return { dispatched, result };
}
async function triggerQueuedTimeout() {
@@ -60,7 +75,7 @@ async function triggerQueuedTimeout() {
concurrentRequests: 1,
requestsPerMinute: 0,
minTimeBetweenRequestsMs: 0,
maxWaitMs: 40,
maxWaitMs: QUEUED_QUEUE_BUDGET_MS,
});
const connectionId = "conn-queued-timeout";
rateLimitManager.enableRateLimitProtection(connectionId);
@@ -79,13 +94,12 @@ async function triggerQueuedTimeout() {
await firstExecuting;
let caught: unknown;
let queuedDispatched = false;
try {
await rateLimitManager.withRateLimit(
"test-provider",
connectionId,
null,
async () => "should-not-dispatch"
);
await rateLimitManager.withRateLimit("test-provider", connectionId, null, async () => {
queuedDispatched = true;
return "should-not-dispatch";
});
assert.fail("expected the queued job to expire");
} catch (error) {
caught = error;
@@ -93,16 +107,20 @@ async function triggerQueuedTimeout() {
releaseFirst();
await first;
}
return caught;
return { caught, queuedDispatched };
}
test("#4165 a dispatched provider call is not killed by the queue budget", async () => {
const result = await triggerQueueTimeout();
assert.equal(result, "should-not-reach");
const execution = await triggerQueueTimeout();
assert.equal(execution.dispatched, true, "the callback must enter execution");
assert.equal(execution.result, "should-not-reach");
});
test("#4165 queue expiry surfaces a clear local error", async () => {
const caught = (await triggerQueuedTimeout()) as Error & { code?: string };
const result = await triggerQueuedTimeout();
assert.ok(result.caught instanceof Error, "queue expiry must reject with an Error");
assert.equal(result.queuedDispatched, false, "an expired queued callback must never dispatch");
const caught = result.caught as Error & { code?: string };
assert.equal(caught.code, "RATE_LIMIT_QUEUE_TIMEOUT");
assert.match(caught.message, /maxWaitMs/);
assert.match(caught.message, /not an upstream/i);

View File

@@ -0,0 +1,53 @@
import test from "node:test";
import assert from "node:assert/strict";
import { createBuiltinAutoCombo } from "@omniroute/open-sse/services/autoCombo/builtinCatalog.ts";
// #7754: `auto/best-free` combo name must never leak downstream as the model id.
// When the free-tier candidate pool resolves non-empty, every model in the combo
// must carry a concrete `<provider>/<model>` id — never the literal combo name.
test("#7754 auto/best-free never leaks the combo name as a model", async () => {
const combo = await createBuiltinAutoCombo("auto/best-free", "best-free");
const models = combo.models || [];
// The combo id is the modelStr by design (routing resolves it back), but the
// models array must never contain it as a target model.
const leak = models.filter(
(m) =>
(m.id || "") === "auto/best-free" ||
(m.model || "") === "auto/best-free" ||
(m.modelStr || "") === "auto/best-free"
);
assert.equal(leak.length, 0, `combo name leaked as a target model: ${JSON.stringify(leak)}`);
});
test("#7754 every auto/best-free model carries a concrete provider/model", async () => {
const combo = await createBuiltinAutoCombo("auto/best-free", "best-free");
const models = combo.models || [];
for (const m of models) {
assert.ok(
m.model && m.model !== "auto/best-free",
`model missing concrete id: ${JSON.stringify(m)}`
);
assert.ok(
m.providerId && m.providerId !== "auto",
`model missing concrete provider: ${JSON.stringify(m)}`
);
}
});
test("#7754 empty free-tier pool degrades with a clear 503, not a name leak", async () => {
// When NO free-tier candidate exists, createBuiltinAutoCombo must either
// return an empty models[] (which the #6458 route check converts to a clear
// 503) or throw — never synthesize a target whose model is the combo name.
const combo = await createBuiltinAutoCombo("auto/best-free", "best-free");
const models = combo.models || [];
if (models.length === 0) {
// Empty pool is fine — the route layer (#6458) converts it to a clear 503.
assert.equal(combo.candidatePool?.length || 0, 0);
} else {
// Non-empty pool must not leak.
const leak = models.filter((m) => (m.model || "") === "auto/best-free");
assert.equal(leak.length, 0);
}
});

View File

@@ -9,15 +9,29 @@ const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(__dirname, "../..");
const WORKFLOW = resolve(repoRoot, ".github/workflows/quality.yml");
function loadWorkflow(): any {
return parse(readFileSync(WORKFLOW, "utf8"));
interface WorkflowStep {
name?: string;
run?: string;
"continue-on-error"?: boolean;
}
interface WorkflowDocument {
jobs?: Record<string, { steps?: WorkflowStep[] }>;
}
function loadWorkflow(): WorkflowDocument {
return parse(readFileSync(WORKFLOW, "utf8")) as WorkflowDocument;
}
function invokesGate(run: string): boolean {
if (!run) return false;
return /npm run (check:|typecheck:)/.test(run) || /npm run "check/.test(run) || /npm run \\"check/.test(run);
return (
/npm run (check:|typecheck:)/.test(run) ||
/npm run "check/.test(run) ||
/npm run \\"check/.test(run)
);
}
function stepCanFail(step: any): boolean {
function stepCanFail(step: WorkflowStep): boolean {
return step?.["continue-on-error"] !== true;
}
@@ -25,7 +39,7 @@ test("repro #8542: fast-gates must not fail-fast into a later gate", () => {
const wf = loadWorkflow();
const job = wf.jobs?.["fast-gates"];
assert.ok(job, "fast-gates job must exist");
const steps: any[] = job.steps ?? [];
const steps: WorkflowStep[] = job.steps ?? [];
assert.ok(steps.length >= 5, `fast-gates must have >=5 steps, got ${steps.length}`);
const gateSteps = steps.map((s, i) => ({ s, i })).filter(({ s }) => invokesGate(s?.run ?? ""));
@@ -51,4 +65,4 @@ test("repro #8542: fast-gates must not fail-fast into a later gate", () => {
maskedPairs.slice(0, 12).join("\n") +
(maskedPairs.length > 12 ? `\n... (+${maskedPairs.length - 12} more)` : "")
);
});
});

View File

@@ -0,0 +1,70 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { syncStandaloneNativeAssets } from "../../scripts/build/assembleStandalone.mjs";
/**
* Repro #8847: better-sqlite3 prebuilds are not included in the standalone
* bundle, so the bundled app fails when the platform's prebuild is needed
* (e.g. under Bun, which resolves the native binary via prebuilds/ rather
* than build/Release/).
*
* The test creates a synthetic node_modules/better-sqlite3/ tree with both
* the compiled build/Release/ binary AND the prebuilds/ directory, then
* confirms that syncStandaloneNativeAssets copies both into the standalone
* output. On the unfixed code this fails because NATIVE_ASSET_ENTRIES only
* lists better-sqlite3/build/.
*/
test("repro-8847: better-sqlite3 prebuilds are bundled alongside the compiled binary", async () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "repro-8847-"));
const projectRoot = path.join(tmp, "src-root");
// Seed better-sqlite3 with both build/Release/ and prebuilds/.
const bsqlDir = path.join(projectRoot, "node_modules", "better-sqlite3");
fs.mkdirSync(path.join(bsqlDir, "build", "Release"), { recursive: true });
fs.writeFileSync(
path.join(bsqlDir, "build", "Release", "better_sqlite3.node"),
"// native binary placeholder"
);
fs.mkdirSync(path.join(bsqlDir, "prebuilds"), { recursive: true });
for (const target of [
"darwin-arm64.node",
"darwin-x64.node",
"linux-arm64.node",
"linux-x64.node",
"linuxmusl-arm64.node",
"linuxmusl-x64.node",
"win32-arm64.node",
"win32-x64.node",
]) {
fs.writeFileSync(path.join(bsqlDir, "prebuilds", target), `// ${target}`);
}
const outDir = path.join(tmp, "standalone");
fs.mkdirSync(outDir, { recursive: true });
// Act: copy native assets into the standalone output.
await syncStandaloneNativeAssets(projectRoot, fs.promises, { log() {} }, outDir);
// Assert: the compiled build/Release/ binary was copied.
assert.ok(
fs.existsSync(
path.join(outDir, "node_modules", "better-sqlite3", "build", "Release", "better_sqlite3.node")
),
"compiled native binary (build/Release/) must be in the standalone bundle"
);
// Assert: the prebuilds/ directory was also copied.
const prebuildsDir = path.join(outDir, "node_modules", "better-sqlite3", "prebuilds");
assert.ok(fs.existsSync(prebuildsDir), "prebuilds/ directory must be in the standalone bundle");
// Assert: at least one prebuild file was copied.
assert.ok(
fs.existsSync(path.join(prebuildsDir, "linux-x64.node")),
"linux-x64 prebuild must be in the standalone bundle"
);
fs.rmSync(tmp, { recursive: true, force: true });
});

View File

@@ -0,0 +1,111 @@
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
// #9156: macOS launchd autostart fails because the supervisor spawns the child
// with bare "node", but launchd's PATH cannot resolve it. process.execPath is
// always the absolute path to the running Node.js binary and is always resolvable.
//
// We verify the fix via:
// 1. Static source analysis — the spawn() call must use process.execPath
// unconditionally (no fallback to bare "node"). This runs without any
// experimental flags so it serves as the permanent regression guard.
// 2. Runtime test via mock.module (requires --experimental-test-module-mocks)
// that captures the actual spawn arguments.
const __filename = new URL(import.meta.url).pathname;
const __dirname = path.dirname(__filename);
const SUPERVISOR_PATH = path.resolve(
__dirname,
"../../bin/cli/runtime/processSupervisor.mjs"
);
const supervisorSrc = fs.readFileSync(SUPERVISOR_PATH, "utf8");
// ---------------------------------------------------------------------------
// 1. Source-level verification (no experimental flag required)
// ---------------------------------------------------------------------------
test("spawn() uses process.execPath unconditionally, no bare 'node' fallback (#9156)", () => {
// Must NOT contain the old conditional that falls back to bare "node"
assert.ok(
!supervisorSrc.includes('process.versions.bun ? process.execPath : "node"'),
"must NOT have a conditional fallback to bare 'node'"
);
// Must use process.execPath as the first argument to spawn()
const execPathPattern = /spawn\(\s*process\.execPath\s*,/;
assert.ok(
execPathPattern.test(supervisorSrc),
"spawn() must receive process.execPath as first argument"
);
});
test("process.execPath is an absolute path to the running Node.js binary", () => {
assert.ok(
path.isAbsolute(process.execPath),
`process.execPath must be absolute, got: ${process.execPath}`
);
assert.ok(
fs.existsSync(process.execPath),
`process.execPath must exist: ${process.execPath}`
);
});
// ---------------------------------------------------------------------------
// 2. Runtime test via mock.module (requires --experimental-test-module-mocks)
// ---------------------------------------------------------------------------
//
// Run manually: node --experimental-test-module-mocks --import tsx/esm --test tests/unit/repro-9156.test.ts
import { mock } from "node:test";
if (typeof mock.module === "function") {
test("(runtime) ServerSupervisor.start() spawns with process.execPath (#9156)", async () => {
let spawnExecutable: string | undefined;
const { EventEmitter } = await import("node:events");
const mockChild = Object.assign(new EventEmitter(), {
pid: 12345,
stdout: null,
stderr: null,
kill: () => {},
});
mock.module("node:child_process", {
exports: {
spawn: (...args: unknown[]) => {
spawnExecutable = args[0] as string;
return mockChild;
},
},
});
process.env.PORT = "0";
const { ServerSupervisor } = await import(
"../../bin/cli/runtime/processSupervisor.mjs"
);
const supervisor = new ServerSupervisor({
serverPath: "/fake/server.js",
env: {},
maxRestarts: 0,
});
spawnExecutable = undefined;
supervisor.start();
assert.ok(spawnExecutable, "spawn() must have been called");
assert.equal(
spawnExecutable,
process.execPath,
`expected process.execPath, got: ${spawnExecutable}`
);
assert.notEqual(spawnExecutable, "node", "must not be bare 'node'");
mockChild.removeAllListeners();
delete process.env.PORT;
});
}

View File

@@ -0,0 +1,71 @@
/**
* Issue #9486 — Anthropic OAuth returns HTTP 400 with "out of extra usage" in
* the error body when a tool-carrying request exceeds the account's usage quota.
* This should be classified as quota_exhausted (not generic bad_request), so the
* account fallback mechanism applies a proper cooldown and combo routing can
* skip to another target.
*/
import test from "node:test";
import assert from "node:assert/strict";
const { matchErrorRuleByText, findMatchingErrorRule, ERROR_RULES } =
await import("../../open-sse/config/errorConfig.ts");
const { checkFallbackError, classifyErrorText } =
await import("../../open-sse/services/accountFallback.ts");
const { RateLimitReason } = await import("../../open-sse/config/constants.ts");
test("#9486 ERROR_RULES has a text rule for 'out of extra usage' → quota_exhausted", () => {
const rule = ERROR_RULES.find((r) => r.text === "out of extra usage");
assert.ok(rule, "expected a rule for 'out of extra usage'");
assert.equal(rule!.reason, "quota_exhausted");
// Should use backoff so the fallback path applies exponential scaling
assert.equal(rule!.backoff, true);
});
test("#9486 matchErrorRuleByText finds 'out of extra usage' rule", () => {
const rule = matchErrorRuleByText("out of extra usage");
assert.ok(rule, "expected a matching rule");
assert.equal(rule!.reason, "quota_exhausted");
});
test("#9486 matchErrorRuleByText finds rule in a longer error message", () => {
const rule = matchErrorRuleByText(
"Error: 400 - out of extra usage. You have exceeded your usage quota for this billing period."
);
assert.ok(rule, "expected a matching rule from longer message");
assert.equal(rule!.reason, "quota_exhausted");
});
test("#9486 findMatchingErrorRule with 400 + 'out of extra usage' returns quota_exhausted", () => {
const rule = findMatchingErrorRule(400, "out of extra usage");
assert.ok(rule, "expected a matching rule");
assert.equal(rule!.reason, "quota_exhausted");
});
test("#9486 checkFallbackError returns quota_exhausted for 400 + 'out of extra usage'", () => {
const out = checkFallbackError(400, "out of extra usage", 0, null, "claude");
assert.equal(out.shouldFallback, true);
assert.equal(out.reason, RateLimitReason.QUOTA_EXHAUSTED);
// Should get a non-zero cooldown (quota exhaustion is not transient)
assert.ok(out.cooldownMs > 0, `expected positive cooldown, got ${out.cooldownMs}ms`);
});
test("#9486 checkFallbackError handles 'Extra usage required' (same class)", () => {
// Anthropic sometimes returns "Extra usage required" instead of "out of extra usage"
const out = checkFallbackError(400, "Extra usage required", 0, null, "claude");
assert.equal(out.shouldFallback, true);
assert.equal(out.reason, RateLimitReason.QUOTA_EXHAUSTED);
});
test("#9486 classifyErrorText flags 'out of extra usage' as QUOTA_EXHAUSTED", () => {
const out = classifyErrorText("out of extra usage");
assert.equal(out, RateLimitReason.QUOTA_EXHAUSTED);
});
test("#9486 generic 400 without quota text still gets no fallback (regression guard)", () => {
// Regression guard: a plain 400 with no quota-related text must NOT trigger
// fallback, preserving the existing behavior for non-quota 400 errors.
const out = checkFallbackError(400, "Bad request: invalid JSON", 0, null, "claude");
assert.equal(out.shouldFallback, false);
assert.equal(out.reason, RateLimitReason.UNKNOWN);
});

View File

@@ -0,0 +1,52 @@
import test from "node:test";
import assert from "node:assert/strict";
// #9623: Failed connection test leaves testStatus=error with no recovery path.
// Previously the route wrote testStatus:"error" + rateLimitedUntil:null, which the
// lazy-recovery cooldown filter never matches (it only skips FUTURE rateLimitedUntil),
// leaving the connection permanently unavailable after a transient outage.
// Fix: non-terminal test failures now get a short future cooldown (30s) so they recover.
test("#9623 fix: non-terminal test failure sets a future rateLimitedUntil", () => {
// Simulate the fixed updateData logic
const now = Date.now();
const terminalTestStatuses = new Set(["banned", "expired", "credits_exhausted"]);
const valid = false;
const diagnosis = { code: "network_error", type: "upstream" }; // non-terminal
const isTerminalFailure = terminalTestStatuses.has(String(diagnosis.code).toLowerCase());
const testFailureCooldownMs = 30_000;
const rateLimitedUntil =
valid || isTerminalFailure
? valid
? null
: null
: new Date(now + testFailureCooldownMs).toISOString();
assert.ok(
rateLimitedUntil !== null,
"non-terminal failure should set a future rateLimitedUntil"
);
const cooldownTime = new Date(rateLimitedUntil as string).getTime();
assert.ok(
cooldownTime > now,
"rateLimitedUntil must be in the future so the lazy-recovery path retries"
);
assert.ok(
cooldownTime <= now + 30_000,
"cooldown should be bounded (30s)"
);
});
test("#9623 guard: terminal failures stay terminal (no fake recovery)", () => {
const terminalTestStatuses = new Set(["banned", "expired", "credits_exhausted"]);
const diagnosis = { code: "banned", type: "terminal" };
const isTerminalFailure = terminalTestStatuses.has(String(diagnosis.code).toLowerCase());
assert.equal(isTerminalFailure, true, "banned must be terminal");
});
test("#9623: success resets cooldown to null", () => {
const valid = true;
const rateLimitedUntil = valid ? null : new Date(Date.now() + 30_000).toISOString();
assert.equal(rateLimitedUntil, null, "successful test clears cooldown");
});

View File

@@ -0,0 +1,52 @@
import { describe, it } from "node:test";
import { strict as assert } from "node:assert";
import { readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, resolve } from "node:path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const INSTRUMENTATION_NODE_PATH = resolve(
__dirname,
"../../src/instrumentation-node.ts"
);
describe("repro-9624: startCleanupScheduler wired in Next.js startup path", () => {
it("should import startCleanupScheduler from cleanup", () => {
const source = readFileSync(INSTRUMENTATION_NODE_PATH, "utf-8");
// instrumentation-node.ts loads all startup modules via dynamic imports in a
// Promise.all destructure, e.g.:
// const [{ startCleanupScheduler }, ...] = await Promise.all([
// import("@/lib/db/cleanup"), ...
// ]);
// So the binding and the module import appear separately in the file.
const cleanupModuleImported = /import\(\s*["']@\/lib\/db\/cleanup["']\s*\)/.test(
source
);
const schedulerBound = /\bstartCleanupScheduler\b/.test(source);
assert.ok(
cleanupModuleImported,
"@/lib/db/cleanup should be imported (dynamic import) in instrumentation-node.ts"
);
assert.ok(
schedulerBound,
"startCleanupScheduler should be bound in instrumentation-node.ts"
);
});
it("should call startCleanupScheduler() during startup", () => {
const source = readFileSync(INSTRUMENTATION_NODE_PATH, "utf-8");
// Check that startCleanupScheduler is called (as a function call).
// It can be called directly or as part of a conditional.
const hasCall = /\bstartCleanupScheduler\s*\(/.test(source);
assert.ok(
hasCall,
"startCleanupScheduler() should be called in instrumentation-node.ts"
);
});
});

View File

@@ -0,0 +1,92 @@
/**
* Issue #9625 — domain_cost_history cleanup cutoff unit mismatch.
*
* cleanupDomainCostHistory() computes the cutoff in epoch seconds
* (Math.floor(Date.now() / 1000)) but the timestamp column stores
* epoch milliseconds (Date.now()), as inserted by saveCostEntry().
*
* This test seeds data using the same format as the production code
* (milliseconds), then asserts that cleanupDomainCostHistory() correctly
* deletes rows older than the retention window.
*
* Before the fix, the cutoff in seconds was ~1000× smaller than the
* stored timestamps, so the DELETE WHERE timestamp < cutoff would
* never match old rows — the cleanup was effectively a no-op.
*/
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-9625-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const { cleanupDomainCostHistory } = await import("../../src/lib/db/cleanup.ts");
const { getDbInstance, resetDbInstance } = await import("../../src/lib/db/core.ts");
test.after(() => {
resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
const DAY_MS = 86_400_000; // milliseconds
test("#9625 cleanupDomainCostHistory: cutoff in ms matches production timestamps", async () => {
const db = getDbInstance()!;
const now = Date.now(); // milliseconds — same as saveCostEntry() default
const insert = db.prepare(
"INSERT INTO domain_cost_history (api_key_id, cost, timestamp) VALUES (?, ?, ?)"
);
// Seed data using millisecond timestamps (production format).
// 3 old rows: 40 days ago (should be deleted)
// 2 recent rows: 5 days ago (should be kept)
insert.run("key1", 1.0, now - 40 * DAY_MS);
insert.run("key1", 2.0, now - 40 * DAY_MS);
insert.run("key1", 3.0, now - 40 * DAY_MS);
insert.run("key1", 4.0, now - 5 * DAY_MS);
insert.run("key1", 5.0, now - 5 * DAY_MS);
const result = await cleanupDomainCostHistory();
// Before the fix, cutoff was in seconds (~1.7e9) while timestamps
// are in milliseconds (~1.7e12). The comparison `WHERE ts < 1.7e9`
// would never match rows with ts ~1.7e12, so nothing was deleted.
assert.strictEqual(result.deleted, 3, "Should delete 3 old rows (40 days old)");
assert.strictEqual(result.errors, 0);
const remaining = db.prepare("SELECT COUNT(*) as cnt FROM domain_cost_history").get() as {
cnt: number;
};
assert.strictEqual(remaining.cnt, 2, "Should keep 2 recent rows (5 days old)");
});
test("#9625 unit mismatch: seconds cutoff would NOT match ms timestamps", () => {
// Demonstrate the arithmetic bug: a cutoff in seconds is ~1000×
// smaller than a millisecond timestamp, so the WHERE clause never
// matches production data.
const nowMs = Date.now();
const nowSec = Math.floor(nowMs / 1000);
const retentionDays = 30;
const cutoffSec = nowSec - retentionDays * 86_400; // seconds
const cutoffMs = nowMs - retentionDays * 86_400_000; // milliseconds
// A row inserted 40 days ago with a millisecond timestamp:
const oldRowMs = nowMs - 40 * 86_400_000; // ~1.7e12
// With seconds cutoff: oldRowMs (1.7e12) < cutoffSec (1.7e9) is FALSE
// because 1.7e12 > 1.7e9 — the row is never matched.
assert.ok(
oldRowMs > cutoffSec,
"Bug: ms timestamp is NOT less than seconds cutoff, so row is never deleted"
);
// With milliseconds cutoff: oldRowMs (1.7e12) < cutoffMs (1.7e12) is TRUE
assert.ok(
oldRowMs < cutoffMs,
"Fix: ms timestamp IS less than ms cutoff, so row is correctly deleted"
);
});

View File

@@ -0,0 +1,62 @@
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { join } from "node:path";
const root = join(import.meta.dirname, "../..");
const llmChatCardPath =
"src/app/(dashboard)/dashboard/media-providers/components/LlmChatCard.tsx";
const src = readFileSync(join(root, llmChatCardPath), "utf8");
const DISABLED_ON_LOADING = /disabled\s*=\s*\{\s*loading\s*\}/;
const MODELS_LOADING_MARKER = /modelsLoading|Loading…|Loading\.\.\./;
const ERROR_BRANCH = /error\s*&&/;
const RETRY_ACTION = /onClick\s*=\s*\{[^}]*retry|retry[A-Za-z]*\s*\(\)|const\s+\[reload/i;
const NO_MODELS_AFTER_EMPTY = /modelOptions\.length\s*===?\s*0|models\.length\s*===?\s*0/;
test("LlmChatCard destructures loading and error from useProviderModels (#9626)", () => {
const match = src.match(/const\s*\{\s*([^}]+)\s*\}\s*=\s*useProviderModels\(/);
assert.ok(match, "Expected to find a destructuring of useProviderModels");
const destructured = match[1];
assert.ok(
destructured.includes("loading"),
"loading state must be destructured from useProviderModels"
);
assert.ok(destructured.includes("error"), "error state must be destructured from useProviderModels");
});
test("LlmChatCard disables the model selector while models are loading (#9626)", () => {
assert.ok(
DISABLED_ON_LOADING.test(src),
"The model <select> must be disabled while the models request is pending"
);
});
test("LlmChatCard shows a visible loading label while models are pending (#9626)", () => {
assert.ok(
MODELS_LOADING_MARKER.test(src),
"A visible loading text (e.g. 'Loading…') must appear while the models request is pending"
);
});
test("LlmChatCard surfaces the provider model error in the UI (#9626)", () => {
assert.ok(
ERROR_BRANCH.test(src),
"An error branch that renders the captured error message must exist"
);
});
test("LlmChatCard offers a retry action when the model request fails (#9626)", () => {
assert.ok(
RETRY_ACTION.test(src),
"A retry action must be offered next to the model error"
);
});
test("LlmChatCard keeps the empty-state message distinct from an error (#9626)", () => {
assert.ok(
NO_MODELS_AFTER_EMPTY.test(src),
"The empty-state (no models) message must only be shown for a successful empty response"
);
});

View File

@@ -0,0 +1,27 @@
import test from "node:test";
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
const pkg = JSON.parse(readFileSync("package.json", "utf-8"));
const files = pkg.files || [];
// #9633: `build-next-isolated.mjs` is published (fixed in #1126), but three of
// its sibling modules it imports were missing from the `files` whitelist, so
// `npm run build` on a globally-installed package crashed with ERR_MODULE_NOT_FOUND.
// The dynamic import of `build-tproxy-native.mjs` (~line 308) and the static
// imports of `assembleStandalone.mjs` / `backendOnlyPages.mjs` must ship too.
const NEEDED = [
"scripts/build/assembleStandalone.mjs",
"scripts/build/backendOnlyPages.mjs",
"scripts/build/build-tproxy-native.mjs",
"scripts/build/colocateOptionals.mjs",
];
test("#9633: build-next-isolated.mjs sibling imports present in package.json files[]", () => {
for (const needed of NEEDED) {
assert.ok(
files.some((f) => typeof f === "string" && f === needed),
`${needed} is not in package.json files[]`
);
}
});

View File

@@ -452,7 +452,13 @@ test("v1 search POST returns 400 when auto-select finds no configured provider (
const body = (await response.json()) as any;
assert.equal(response.status, 400);
assert.equal(capturedUrl, "", "fallback-only SearXNG must not receive an upstream request");
assert.ok(body.error?.message || body.error);
assert.match(
String(body.error?.message ?? body.error),
/provider|configured/i,
"the response must explain that no provider was selected"
);
} finally {
globalThis.fetch = originalFetch;
}

View File

@@ -47,7 +47,8 @@ test.after(() => {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true });
});
const DAY = 86_400; // seconds
const DAY_SECONDS = 86_400;
const DAY_MS = 86_400_000;
/** Ensure compression_run_telemetry table exists (created lazily in production). */
function ensureTelemetryTable(): void {
@@ -71,21 +72,34 @@ function ensureTelemetryTable(): void {
`);
}
test.beforeEach(() => {
ensureTelemetryTable();
const db = getDbInstance()!;
for (const table of [
"domain_cost_history",
"compression_cache_stats",
"xp_audit_log",
"compression_run_telemetry",
]) {
db.exec(`DELETE FROM ${table}`);
}
});
// ─── Tests ───────────────────────────────────────────────────────────────
test("#6848 cleanupDomainCostHistory: deletes rows older than retention window", async () => {
const db = getDbInstance()!;
const now = Math.floor(Date.now() / 1000);
const now = Date.now();
const insert = db.prepare(
"INSERT INTO domain_cost_history (api_key_id, cost, timestamp) VALUES (?, ?, ?)"
);
// 3 old (40 days ago), 2 recent (5 days ago)
insert.run("key1", 1.0, now - 40 * DAY);
insert.run("key1", 2.0, now - 40 * DAY);
insert.run("key1", 3.0, now - 40 * DAY);
insert.run("key1", 4.0, now - 5 * DAY);
insert.run("key1", 5.0, now - 5 * DAY);
insert.run("key1", 1.0, now - 40 * DAY_MS);
insert.run("key1", 2.0, now - 40 * DAY_MS);
insert.run("key1", 3.0, now - 40 * DAY_MS);
insert.run("key1", 4.0, now - 5 * DAY_MS);
insert.run("key1", 5.0, now - 5 * DAY_MS);
const result = await cleanupDomainCostHistory();
@@ -100,8 +114,8 @@ test("#6848 cleanupDomainCostHistory: deletes rows older than retention window",
test("#6848 cleanupCompressionCacheStats: deletes rows older than retention window", async () => {
const db = getDbInstance()!;
const oldDate = new Date(Date.now() - 40 * DAY * 1000).toISOString();
const recentDate = new Date(Date.now() - 5 * DAY * 1000).toISOString();
const oldDate = new Date(Date.now() - 40 * DAY_MS).toISOString();
const recentDate = new Date(Date.now() - 5 * DAY_MS).toISOString();
const insert = db.prepare(
"INSERT INTO compression_cache_stats (provider, compression_mode, created_at) VALUES (?, ?, ?)"
);
@@ -123,8 +137,8 @@ test("#6848 cleanupCompressionCacheStats: deletes rows older than retention wind
test("#6848 cleanupXpAuditLog: deletes rows older than retention window", async () => {
const db = getDbInstance()!;
const oldDate = new Date(Date.now() - 40 * DAY * 1000).toISOString();
const recentDate = new Date(Date.now() - 5 * DAY * 1000).toISOString();
const oldDate = new Date(Date.now() - 40 * DAY_MS).toISOString();
const recentDate = new Date(Date.now() - 5 * DAY_MS).toISOString();
const insert = db.prepare(
"INSERT INTO xp_audit_log (api_key_id, action, xp_earned, created_at) VALUES (?, ?, ?, ?)"
);
@@ -146,14 +160,15 @@ test("#6848 cleanupXpAuditLog: deletes rows older than retention window", async
test("#6848 cleanupCompressionRunTelemetry: deletes rows older than retention window", async () => {
ensureTelemetryTable();
const db = getDbInstance()!;
const now = Math.floor(Date.now() / 1000);
const now = Date.now();
const nowSeconds = Math.floor(now / 1000);
const insert = db.prepare(
"INSERT INTO compression_run_telemetry (timestamp, tokens_before, tokens_after) VALUES (?, ?, ?)"
);
insert.run(now - 40 * DAY, 1000, 500);
insert.run(now - 40 * DAY, 2000, 800);
insert.run(now - 5 * DAY, 1500, 600);
insert.run(nowSeconds - 40 * DAY_SECONDS, 1000, 500);
insert.run(nowSeconds - 40 * DAY_SECONDS, 2000, 800);
insert.run(nowSeconds - 5 * DAY_SECONDS, 1500, 600);
const result = await cleanupCompressionRunTelemetry();
@@ -169,13 +184,14 @@ test("#6848 cleanupCompressionRunTelemetry: deletes rows older than retention wi
test("#6848 no rows deleted when all data is within retention window (calls all 4 real functions)", async () => {
ensureTelemetryTable();
const db = getDbInstance()!;
const now = Math.floor(Date.now() / 1000);
const nowSeconds = Math.floor(Date.now() / 1000);
const nowMilliseconds = Date.now();
const recentISO = new Date().toISOString();
db.prepare("INSERT INTO domain_cost_history (api_key_id, cost, timestamp) VALUES (?, ?, ?)").run(
"k",
1,
now - DAY
nowMilliseconds - DAY_MS
);
db.prepare(
"INSERT INTO compression_cache_stats (provider, compression_mode, created_at) VALUES (?, ?, ?)"
@@ -185,7 +201,7 @@ test("#6848 no rows deleted when all data is within retention window (calls all
).run("k", "a", 5, recentISO);
db.prepare(
"INSERT INTO compression_run_telemetry (timestamp, tokens_before, tokens_after) VALUES (?, ?, ?)"
).run(now - DAY, 100, 50);
).run(nowSeconds - DAY_SECONDS, 100, 50);
const r1 = await cleanupDomainCostHistory();
const r2 = await cleanupCompressionCacheStats();

View File

@@ -200,6 +200,16 @@ test("Claude -> Gemini omits unsigned functionCall instead of injecting a fake t
false,
"signature-less tool_use must not become a native functionCall"
);
assert.equal(
JSON.stringify(result).includes('"thoughtSignature"'),
false,
"the translator must not synthesize a fake thought signature"
);
assert.equal(
JSON.stringify(result).includes("read_file"),
false,
"the omitted unsigned call must not leak its tool payload elsewhere"
);
});
test("Claude -> Gemini sanitizes long tool names and exposes a restore map", () => {

View File

@@ -1,6 +1,7 @@
import test from "node:test";
import assert from "node:assert/strict";
import { openaiResponsesToOpenAIRequest } from "../../open-sse/translator/request/openai-responses.ts";
import { detectSupportedThinkingEfforts } from "../../src/lib/providerModels/modelDiscovery.ts";
function asRecord(value: unknown): Record<string, unknown> {
return value as Record<string, unknown>;
@@ -42,7 +43,6 @@ test("non-GPT-5.6 models still get max downgraded to xhigh", () => {
)
);
assert.equal(translated.reasoning_effort, "xhigh");
<<<<<<< HEAD
});
// ─────────────────────────────────────────────────────────────────────
@@ -61,12 +61,9 @@ test("#9142 Anthropic top-level system prompts must trigger background detection
"system_prompt_pattern"
);
});
=======
// #9140 — VS Code routes filter out built-in auto models
const { isUsableChatModel } = await import(
"../../src/app/api/v1/vscode/[token]/usableChatModel.ts"
);
const { isUsableChatModel } =
await import("../../src/app/api/v1/vscode/[token]/usableChatModel.ts");
test("#9140 VS Code listing must accept built-in auto routing entries", () => {
assert.equal(
@@ -79,14 +76,9 @@ test("#9140 VS Code listing must accept built-in auto routing entries", () => {
false,
"operator-created combo should still be rejected"
);
>>>>>>> origin/release/v3.8.50
});
// ── #9160 model discovery: capabilities.effort_tiers ────────────────────────
// #9160: model discovery must ingest capabilities.effort_tiers
test("#9160 model discovery must ingest capabilities.effort_tiers", () => {
assert.deepEqual(
detectSupportedThinkingEfforts({
@@ -103,4 +95,4 @@ test("#9160 capabilities.effort_tiers with duplicate and synonym", () => {
}),
["low", "xhigh"]
);
});

View File

@@ -22,16 +22,12 @@ const extract = extractCiGates as (
) => { id: string; job: string; args: string[]; env?: Record<string, string> }[];
test("eslintCounts sums errors + warnings across files", () => {
const parsed = [
{ errorCount: 2, warningCount: 5 },
{ errorCount: 0, warningCount: 3 },
{},
];
const parsed = [{ errorCount: 2, warningCount: 5 }, { errorCount: 0, warningCount: 3 }, {}];
assert.deepEqual(eslintCounts(parsed), { errors: 2, warnings: 8 });
});
test("parseEslintJson tolerates a leading non-JSON banner", () => {
const out = "npm warn something\n[{\"errorCount\":0,\"warningCount\":1}]";
const out = 'npm warn something\n[{"errorCount":0,"warningCount":1}]';
assert.deepEqual(parseEslintJson(out), [{ errorCount: 0, warningCount: 1 }]);
assert.equal(parseEslintJson("no json here"), null);
});
@@ -52,8 +48,14 @@ test("parseEslintJson tolerates ESLint's trailing unpruned-suppressions stderr s
});
test("parseCognitiveCount reads the gate's count (en + pt)", () => {
assert.equal(parseCognitiveCount("[cognitive-complexity] 797 function(s) exceed the threshold (15)."), 797);
assert.equal(parseCognitiveCount("[cognitive-complexity] REGRESSÃO — 801 violações > baseline 797"), 801);
assert.equal(
parseCognitiveCount("[cognitive-complexity] 797 function(s) exceed the threshold (15)."),
797
);
assert.equal(
parseCognitiveCount("[cognitive-complexity] REGRESSÃO — 801 violações > baseline 797"),
801
);
assert.equal(parseCognitiveCount("no number"), null);
});
@@ -175,8 +177,16 @@ test("pre-flight wires the test-masking PR-context gate against origin/main (v3.
);
// run() must honor a per-gate env override so GITHUB_BASE_REF actually reaches the child
// (routed through buildGateEnv since the --hermetic scrub was added).
assert.match(src, /env:\s*buildGateEnv\(opts\.env\)/, "run() must merge opts.env into the child env");
assert.match(src, /\.\.\.\(extra \|\| \{\}\)/, "buildGateEnv must spread the per-gate env override");
assert.match(
src,
/env:\s*buildGateEnv\(opts\.env\)/,
"run() must merge opts.env into the child env"
);
assert.match(
src,
/\.\.\.\(extra \|\| \{\}\)/,
"buildGateEnv must spread the per-gate env override"
);
});
test("pre-flight --hermetic scrubs the live-test trigger vars (2026-07-05 false-positive fix)", async () => {
@@ -214,6 +224,27 @@ test("pre-flight runs the slow suites CONCURRENTLY (v3.8.45 perf — was ~1h ser
assert.match(src, /slow\.forEach\([\s\S]*?saveGateLog\(g\.id/, "each slow gate persists its log");
});
test("pre-flight runs tarball boot only after the package artifact builder completes", async () => {
const fs = await import("node:fs");
const src = fs.readFileSync(
new URL("../../scripts/quality/validate-release-green.mjs", import.meta.url),
"utf8"
);
const parallelWave = src.indexOf("const slowResults = await Promise.all");
const packBoot = src.indexOf('id: "pack-boot"');
assert.ok(parallelWave >= 0, "the parallel slow-gate wave must exist");
assert.ok(
packBoot > parallelWave,
"pack-boot must be declared after the parallel artifact build"
);
assert.match(
src,
/packArtifactResult[\s\S]*?check:pack-boot/,
"pack-boot must be explicitly sequenced from the package-artifact result"
);
});
// ─── --full-ci gate extraction (P0, v3.8.46 post-mortem) ─────────────────────
const CI_FIXTURE = `
@@ -259,7 +290,11 @@ test("extractCiGates: pulls npm-run gate steps from the ci.yml gate jobs only",
assert.ok(ids.includes("check:docs-all") && ids.includes("check:docs-symbols"), "multi-line run");
// …and NON-gate steps + jobs outside the gate set are ignored.
assert.ok(!ids.includes("build") && !ids.some((i) => i.startsWith("test:")), "no build/test-run");
assert.equal(gates.find((g) => g.job === "test-unit"), undefined, "test-unit job is not scanned");
assert.equal(
gates.find((g) => g.job === "test-unit"),
undefined,
"test-unit job is not scanned"
);
});
test("extractCiGates: preserves `-- <args>` so ratchet flags reach the script", () => {
@@ -272,7 +307,10 @@ test("extractCiGates: preserves `-- <args>` so ratchet flags reach the script",
test("extractCiGates: skips the non-local gates (pr-evidence, codeql-ratchet)", () => {
const ids = extract(CI_FIXTURE).map((g) => g.id);
assert.ok(!ids.includes("check:pr-evidence"), "pr-evidence needs a PR body — skipped");
assert.ok(!ids.includes("check:codeql-ratchet"), "codeql-ratchet is a remote-main check — skipped");
assert.ok(
!ids.includes("check:codeql-ratchet"),
"codeql-ratchet is a remote-main check — skipped"
);
assert.ok(FULL_CI_SKIP.has("check:pr-evidence") && FULL_CI_SKIP.has("check:codeql-ratchet"));
});
@@ -295,10 +333,7 @@ test("extractCiGates: attaches GITHUB_BASE_REF=main env to test-masking + de-dup
test("extractCiGates: the REAL ci.yml yields the base-reds that leaked in v3.8.46", async () => {
const fs = await import("node:fs");
const yaml = fs.readFileSync(
new URL("../../.github/workflows/ci.yml", import.meta.url),
"utf8"
);
const yaml = fs.readFileSync(new URL("../../.github/workflows/ci.yml", import.meta.url), "utf8");
const ids = new Set(extract(yaml).map((g) => g.id));
// The exact gates that leaked to the v3.8.46 release PR because the pre-flight
// never ran them — --full-ci now reproduces every one.