Compare commits

..

1 Commits

Author SHA1 Message Date
diegosouzapw
4b6c652b48 fix(providers): correct opencode-zen muse-spark context length and Responses auth header (#12681, #12633)
- Declare the real ~1M contextLength/maxOutputTokens on the muse-spark-1.2 /
  muse-spark-1.2-contributor-free registry entries (opencode + opencode-zen)
  instead of silently falling back to the 200000 provider default (#12681).
- Send x-api-key instead of Authorization: Bearer for the openai-responses
  format on the main OpenCode Zen host, fixing a 401 on Muse Spark
  Contributor's /v1/responses route; scoped by baseUrl so opencode-go (a
  different upstream) keeps Bearer (#12633).
2026-09-10 14:16:31 -03:00
12 changed files with 134 additions and 247 deletions

View File

@@ -2721,10 +2721,6 @@ PLAYGROUND_COMPARE_MAX_COLUMNS=4
# MEMORY_VEC_TOP_K=20 # default top-K for vector search
# MEMORY_RRF_K=60 # RRF k constant (sqlite-vec hybrid recipe)
# HF_HUB_ENDPOINT=https://huggingface.co # override Hugging Face Hub base URL for static potion downloads
# Test/diagnostic seam (src/lib/memory/vectorStore.ts) — forces getVectorStore() to
# return null (simulates a cloud/WASM environment without sqlite-vec), degrading
# memory retrieval to FTS5 keyword search. Default off; leave unset in production.
# VECTOR_STORE_DISABLE_VEC=false
# TV6 typed memory decay (OPT-IN, default off — the sweep DELETES decayed memories)
# MEMORY_TYPED_DECAY_ENABLED=false # master switch for the destructive sweep (default off)
# MEMORY_TYPED_DECAY_EPISODIC_DAYS=30 # episodic TTL in days; 0 = episodic immune too

View File

@@ -0,0 +1 @@
- fix(providers): send `x-api-key` instead of `Authorization: Bearer` for OpenCode Zen's `/v1/responses` endpoint (Muse Spark Contributor models), fixing a 401 on OmniRoute's auth header (#12633)

View File

@@ -0,0 +1 @@
- fix(models): declare the real ~1M contextLength for OpenCode Zen's Muse Spark 1.2 models instead of falling back to the 200000 provider default (#12681)

View File

@@ -1 +0,0 @@
- fix(memory): authenticate the internal /v1/rerank loopback call so memory reranking no longer silently degrades to unranked order when REQUIRE_API_KEY=true (#12745)

View File

@@ -923,7 +923,6 @@ Embedding layer, vector store and reranking knobs for the persistent memory subs
| `HF_HUB_ENDPOINT` | `https://huggingface.co` | Override Hugging Face Hub base URL used by `staticPotion.ts` (e.g. mirror endpoint for air-gapped setups). |
| `MEMORY_VEC_TOP_K` | `20` | Default top-K used by the `sqlite-vec` brute-force vector search inside `src/lib/memory/vectorStore.ts`. |
| `MEMORY_RRF_K` | `60` | Reciprocal Rank Fusion constant `k` for hybrid FTS5 + vector retrieval (sqlite-vec recipe). |
| `VECTOR_STORE_DISABLE_VEC` | `false` | Test/diagnostic seam in `getVectorStore()` (`src/lib/memory/vectorStore.ts`): when `true`, forces the vector store to `null` (simulates a cloud/WASM environment without `sqlite-vec`), degrading memory retrieval to FTS5 keyword search. Leave unset in production. |
| `NOTION_API_KEY` | _(unset)_ | API key for Notion backend (used by `genericBackend.ts` known backend preset). |
| `NOTION_API_URL` | `https://api.notion.com/v1`| Base URL for Notion API (can override for self-hosted Notion alternatives). |
| `OBSIDIAN_API_KEY` | _(unset)_ | API key for Obsidian Vault backend (used by `genericBackend.ts` known backend preset). |

View File

@@ -30,17 +30,25 @@ export const opencodeProvider: RegistryEntry = {
// content (see issue #10867). The opencode provider is passthrough, so
// declaring them here only sets the wire format / capability flags — the
// live upstream model list already advertises both ids.
// #12681: real window confirmed against the opencode-go registry's own
// muse-spark-1.2-contributor entries (contextLength: 1048576, maxOutputTokens:
// 131072) — without an explicit value here resolution fell back to the
// provider-wide defaultContextLength (200000), understating the real window.
{
id: "muse-spark-1.2",
name: "Muse Spark 1.2",
supportsReasoning: true,
targetFormat: "openai-responses",
contextLength: 1048576,
maxOutputTokens: 131072,
},
{
id: "muse-spark-1.2-contributor-free",
name: "Muse Spark 1.2 Contributor Free",
supportsReasoning: true,
targetFormat: "openai-responses",
contextLength: 1048576,
maxOutputTokens: 131072,
},
{ id: "deepseek-v4-flash-free", name: "DeepSeek V4 Flash Free", supportsReasoning: true },
// #6998: 2026-07-14 refresh — the upstream free tier rotated its lineup;

View File

@@ -63,11 +63,17 @@ export const opencode_zenProvider: RegistryEntry = {
// targetFormat declaration, so requests routed here still hit
// /chat/completions with a mismatched or unanswerable body and the
// upstream returns an empty message.
// #12681: real window confirmed against the opencode-go registry's own
// muse-spark-1.2-contributor entries (contextLength: 1048576, maxOutputTokens:
// 131072) — without an explicit value here resolution fell back to the
// provider-wide defaultContextLength (200000), understating the real window.
{
id: "muse-spark-1.2",
name: "Muse Spark 1.2",
supportsReasoning: true,
targetFormat: "openai-responses",
contextLength: 1048576,
maxOutputTokens: 131072,
},
// Explicit wire-format overlay of the base opencode provider's muse-spark entry
// (targetFormat: openai-responses). Keep in sync with base on catalog syncs.
@@ -76,6 +82,8 @@ export const opencode_zenProvider: RegistryEntry = {
name: "Muse Spark 1.2 Contributor Free",
supportsReasoning: true,
targetFormat: "openai-responses",
contextLength: 1048576,
maxOutputTokens: 131072,
},
// ── DeepSeek ────────────────────────────────────────────────

View File

@@ -31,6 +31,13 @@ import {
import { isOpencodeGeoBlocked, proxyKeyOf } from "./opencodeGeoBlock.ts";
import { isNetworkRotationSharedEgressGuardEnabled } from "@/shared/utils/featureFlags";
/**
* The main OpenCode Zen host, shared by the `opencode` and `opencode-zen`
* registry entries. Used to scope the `x-api-key` auth override (#12633) away
* from `opencode-go`, which serves a different upstream (`.../zen/go/v1`).
*/
const ZEN_BASE_URL = "https://opencode.ai/zen/v1";
/**
* Per-account proxy configuration, persisted by NoAuthAccountCard under
* `providerSpecificData.accountProxies` (keyed by the account id, which the UI
@@ -776,6 +783,20 @@ export class OpencodeExecutor extends BaseExecutor {
}
}
/**
* #12633: OpenCode Zen's `/v1/responses` endpoint (reached when
* `_requestFormat === "openai-responses"`, e.g. Muse Spark Contributor
* models) requires `x-api-key`, not `Authorization: Bearer` — unlike the
* default `/chat/completions` endpoint on the same host, which accepts
* Bearer. Scoped by baseUrl (not provider id/alias) so this only applies to
* the main Zen host (`opencode` / `opencode-zen`, both `https://opencode.ai/zen/v1`)
* and never to opencode-go, which serves Responses-format models from a
* different upstream (`https://opencode.ai/zen/go/v1`) that expects Bearer.
*/
private usesZenApiKeyAuth(): boolean {
return this._requestFormat === "openai-responses" && this.config?.baseUrl === ZEN_BASE_URL;
}
buildHeaders(
credentials: ProviderCredentials | null,
stream = true,
@@ -792,7 +813,7 @@ export class OpencodeExecutor extends BaseExecutor {
: undefined;
if (key) {
if (this._requestFormat === "claude") {
if (this._requestFormat === "claude" || this.usesZenApiKeyAuth()) {
headers["x-api-key"] = key;
} else {
headers["Authorization"] = `Bearer ${key}`;

View File

@@ -1,214 +0,0 @@
/**
* src/lib/memory/__tests__/rerank-loopback-auth-12745.test.ts
*
* Regression guard for #12745 — applyRerank()'s internal loopback call to
* /v1/rerank used to carry no credential, so with REQUIRE_API_KEY=true the
* global authz proxy's clientApiPolicy would 401 it and rerank silently
* degraded to unranked order (fail-open by design, so nothing ever surfaced
* the failure).
*
* This file proves two things:
* 1. The loopback fetch retrieval.ts's applyRerank() issues now carries a
* real Authorization: Bearer <internal key> header (fixed by attaching
* pickApiKeyForInternalUse() — the same internal-probe selector already
* used by combo-health-check / cloud-sync-verify).
* 2. That fix was NOT done by exempting /v1/rerank from auth: an
* unauthenticated *external* request to /api/v1/rerank is still
* rejected by clientApiPolicy when REQUIRE_API_KEY=true.
*/
import { describe, test, expect, vi, beforeEach, afterEach } from "vitest";
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(), "omr-rerank-auth-12745-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.DISABLE_SQLITE_AUTO_BACKUP = "true";
process.env.VECTOR_STORE_DISABLE_VEC = "true";
const INTERNAL_KEY = "sk-internal-test-key-12745";
vi.mock("../settings", () => ({
getMemorySettings: async () => ({
enabled: true,
maxTokens: 2000,
retentionDays: 30,
strategy: "semantic",
skillsEnabled: true,
embeddingSource: "static",
embeddingProviderModel: null,
customBaseUrl: null,
customModelId: null,
transformersEnabled: false,
staticEnabled: true,
rerankEnabled: true,
rerankProviderModel: "test-provider/test-rerank-model",
vectorStore: "sqlite-vec",
primaryBackend: "sqlite",
fallbackBackends: [],
backendConfigs: {},
}),
}));
vi.mock("../embedding", () => ({
resolveEmbeddingSource: () => ({
source: "static",
model: "static-hash-8",
dimensions: 8,
identity: "static",
signature: "static-8",
reason: "test: static embedding, no network",
}),
embed: async () => ({
vector: new Float32Array([1, 0, 0, 0, 0, 0, 0, 0]),
source: "static",
model: "static-hash-8",
dimensions: 8,
latencyMs: 0,
}),
}));
vi.mock("../vectorStore", () => ({
getVectorStore: () => ({
ensureReady: async () => ({ ready: true, reason: "test" }),
upsertVector: async () => undefined,
deleteVector: async () => undefined,
searchVector: async () => [
{ memoryId: "rrk-auth-1", score: 0.91 },
{ memoryId: "rrk-auth-2", score: 0.82 },
],
searchHybrid: async () => [],
stats: async () => ({ rowCount: 2, needsReindex: 0, activeDim: 8 }),
}),
}));
vi.mock("../../db/apiKeys", async (importOriginal) => {
const actual = await importOriginal<typeof import("../../db/apiKeys")>();
return {
...actual,
pickApiKeyForInternalUse: vi.fn(async () => INTERNAL_KEY),
};
});
const core = await import("../../db/core");
const { retrievePreview } = await import("../retrieval");
const { pickApiKeyForInternalUse } = await import("../../db/apiKeys");
function cleanupDb() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
function insertMemory(apiKeyId: string, id: string, content: string) {
const db = core.getDbInstance();
db.prepare(
`INSERT INTO memories (id, api_key_id, session_id, type, key, content, metadata, created_at, updated_at, expires_at)
VALUES (?, ?, ?, 'factual', ?, ?, '{}', datetime('now'), datetime('now'), NULL)`
).run(id, apiKeyId, "", `key-${id}`, content);
}
let originalFetch: typeof globalThis.fetch;
beforeEach(() => {
cleanupDb();
originalFetch = globalThis.fetch;
vi.mocked(pickApiKeyForInternalUse).mockClear();
});
afterEach(() => {
globalThis.fetch = originalFetch;
});
describe("#12745 — memory rerank loopback call authentication", () => {
test("applyRerank()'s loopback fetch to /v1/rerank carries an internal Authorization bearer", async () => {
insertMemory("api-rrk-auth", "rrk-auth-1", "The capital of France is Paris.");
insertMemory("api-rrk-auth", "rrk-auth-2", "TypeScript is a superset of JavaScript.");
const calls: Array<{ url: string; headers: Record<string, string> }> = [];
globalThis.fetch = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : input.toString();
const headers: Record<string, string> = {};
new Headers(init?.headers).forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
calls.push({ url, headers });
// Emulate the REAL clientApiPolicy behavior this loopback call hits in
// production: reject without a bearer/x-api-key, accept a valid one.
const hasCredential = Boolean(headers["authorization"] || headers["x-api-key"]);
if (!hasCredential) {
return new Response(JSON.stringify({ error: { message: "Authentication required" } }), {
status: 401,
});
}
return new Response(
JSON.stringify({
results: [
{ index: 1, relevance_score: 0.95 },
{ index: 0, relevance_score: 0.4 },
],
}),
{ status: 200 }
);
}) as unknown as typeof globalThis.fetch;
const bundle = await retrievePreview("api-rrk-auth", "capital of France", {
strategy: "semantic",
maxTokens: 2000,
limit: 5,
});
expect(calls.length).toBeGreaterThan(0);
const rerankCall = calls.find((c) => c.url.includes("/v1/rerank"));
expect(rerankCall).toBeDefined();
const hasCredential = Boolean(
rerankCall?.headers["authorization"] || rerankCall?.headers["x-api-key"]
);
expect(hasCredential).toBe(true);
expect(rerankCall?.headers["authorization"]).toBe(`Bearer ${INTERNAL_KEY}`);
// Functional consequence: with a valid credential the rerank response is
// actually honored (item order follows relevance_score) instead of
// silently keeping pre-rerank vector-search order.
expect(bundle.items[0]?.memory.id).toBe("rrk-auth-2");
});
test("without a credential the same loopback call would still be 401'd (no auth bypass introduced)", async () => {
insertMemory("api-rrk-noauth", "rrk-auth-1", "The capital of France is Paris.");
insertMemory("api-rrk-noauth", "rrk-auth-2", "TypeScript is a superset of JavaScript.");
// Simulate the pre-fix condition: internal key selector finds nothing.
vi.mocked(pickApiKeyForInternalUse).mockResolvedValueOnce(null);
let sawUnauthenticatedRerankCall = false;
globalThis.fetch = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : input.toString();
const headers: Record<string, string> = {};
new Headers(init?.headers).forEach((value, key) => {
headers[key.toLowerCase()] = value;
});
const hasCredential = Boolean(headers["authorization"] || headers["x-api-key"]);
if (url.includes("/v1/rerank") && !hasCredential) {
sawUnauthenticatedRerankCall = true;
return new Response(JSON.stringify({ error: { message: "Authentication required" } }), {
status: 401,
});
}
return new Response(JSON.stringify({ results: [] }), { status: 200 });
}) as unknown as typeof globalThis.fetch;
const bundle = await retrievePreview("api-rrk-noauth", "capital of France", {
strategy: "semantic",
maxTokens: 2000,
limit: 5,
});
expect(sawUnauthenticatedRerankCall).toBe(true);
// Fail-open by design: retrieval keeps working (unranked) rather than throwing.
expect(bundle.items.length).toBe(2);
});
});

View File

@@ -12,8 +12,6 @@ import { getQdrantConfig, checkQdrantHealth, searchSemanticMemory } from "./qdra
import type { MemoryEngineStatus } from "@/shared/schemas/memory";
import { supportsFts5 } from "../db/migrationRunner";
import type { SqliteAdapter } from "../db/adapters/types";
import { pickApiKeyForInternalUse } from "../db/apiKeys";
import { getRuntimePorts } from "../runtime/ports";
import {
estimateTokens,
parseMetadata,
@@ -145,29 +143,16 @@ function buildFtsRows(apiKeyId: string, config: FtsColConfig): MemoryRow[] {
}
}
// Loopback rerank URL — localhost only, never routed over the network. The port is
// derived from the same runtime source every other internal self-call uses
// (getRuntimePorts()/process.env.PORT — see src/lib/runtime/ports.ts), never hardcoded,
// so this keeps working when an operator overrides PORT/API_PORT (#12745).
function getRerankLoopbackUrl(): string {
const { apiPort } = getRuntimePorts();
// nosemgrep: javascript.lang.security.audit.non-literal-regexp.non-literal-regexp
return `http://127.0.0.1:${apiPort}/v1/rerank`;
}
// Loopback rerank URL — localhost only, never routed over the network.
// nosemgrep: javascript.lang.security.audit.non-literal-regexp.non-literal-regexp
const RERANK_LOOPBACK_URL = "http://127.0.0.1:20128/v1/rerank";
/**
* Apply reranking via /v1/rerank (loopback-only) if rerankEnabled + rerankProviderModel is set.
* Returns reordered array (or original order on any error — rerank failure never fails retrieval).
*
* Auth note (#12745): /v1/rerank is a CLIENT_API route gated by clientApiPolicywith
* REQUIRE_API_KEY=true an unauthenticated loopback call gets 401'd by the same policy
* that protects it from the outside, and this call used to send no credential at all,
* silently degrading retrieval to unranked order. Attach a real, DB-backed API key
* (the same internal-probe selector already used by combo-health-check / cloud-sync-verify,
* see pickApiKeyForInternalUse()) as a Bearer token instead of exempting the route.
*
* Security note: the URL is a loopback address (127.0.0.1) — it never carries sensitive
* data over a network link. HTTP is safe for loopback-only IPC.
* Security note: the URL is a hardcoded loopback address (127.0.0.1:20128) — it never
* carries sensitive data over a network link. HTTP is safe for loopback-only IPC.
* nosemgrep: javascript.lang.security.detect-non-literal-url
*/
async function applyRerank<T extends { memory: Memory; score: number }>(
@@ -186,14 +171,10 @@ async function applyRerank<T extends { memory: Memory; score: number }>(
top_n: items.length,
};
const internalKey = await pickApiKeyForInternalUse("internal-probe");
const headers: Record<string, string> = { "content-type": "application/json" };
if (internalKey) headers.authorization = `Bearer ${internalKey}`;
const res = await fetch(getRerankLoopbackUrl(), {
const res = await fetch(RERANK_LOOPBACK_URL, {
// nosemgrep: typescript.react.security.react-insecure-request.react-insecure-request
method: "POST",
headers,
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
signal: AbortSignal.timeout(5000),
});

View File

@@ -0,0 +1,54 @@
import test from "node:test";
import assert from "node:assert/strict";
import { OpencodeExecutor } from "../../open-sse/executors/opencode.ts";
test("#12633: openai-responses format on opencode-zen sends x-api-key, not Authorization Bearer", () => {
const executor = new OpencodeExecutor("opencode-zen");
executor._requestFormat = "openai-responses";
const headers = executor.buildHeaders(
{ apiKey: "sk-zen-test" },
true,
null,
"muse-spark-1.2-contributor-free"
);
assert.equal(headers["x-api-key"], "sk-zen-test");
assert.equal(headers["Authorization"], undefined);
});
test("#12633: openai-responses format on the base opencode (oc) provider also sends x-api-key", () => {
const executor = new OpencodeExecutor("opencode");
executor._requestFormat = "openai-responses";
const headers = executor.buildHeaders(
{ apiKey: "sk-oc-test" },
true,
null,
"muse-spark-1.2-contributor-free"
);
assert.equal(headers["x-api-key"], "sk-oc-test");
assert.equal(headers["Authorization"], undefined);
});
test("#12633: openai-responses format on opencode-go (different upstream endpoint) keeps Authorization Bearer", () => {
const executor = new OpencodeExecutor("opencode-go");
executor._requestFormat = "openai-responses";
const headers = executor.buildHeaders(
{ apiKey: "sk-go-test" },
true,
null,
"muse-spark-1.2-contributor"
);
assert.equal(headers["Authorization"], "Bearer sk-go-test");
assert.equal(headers["x-api-key"], undefined);
});
test("#12633: claude format keeps sending x-api-key (unchanged behavior)", () => {
const executor = new OpencodeExecutor("opencode-zen");
executor._requestFormat = "claude";
const headers = executor.buildHeaders({ apiKey: "sk-claude-test" }, true, null, "some-model");
assert.equal(headers["x-api-key"], "sk-claude-test");
assert.equal(headers["Authorization"], undefined);
});

View File

@@ -0,0 +1,33 @@
import { test } from "node:test";
import assert from "node:assert/strict";
import { REGISTRY } from "../../open-sse/config/providerRegistry.ts";
import { getTokenLimit } from "../../open-sse/services/contextManager.ts";
test("#12681: opencode registry declares an explicit real contextLength for muse-spark-1.2 models", () => {
const opencode = REGISTRY["opencode"];
const museSpark = opencode.models.find((m) => m.id === "muse-spark-1.2");
const museSparkFree = opencode.models.find((m) => m.id === "muse-spark-1.2-contributor-free");
assert.notEqual(
museSpark?.contextLength,
undefined,
"muse-spark-1.2 should declare its own real contextLength instead of relying on the 200000 provider default"
);
assert.notEqual(
museSparkFree?.contextLength,
undefined,
"muse-spark-1.2-contributor-free should declare its own real contextLength instead of relying on the 200000 provider default"
);
});
test("#12681: opencode-zen registry declares an explicit real contextLength for muse-spark-1.2 models", () => {
const zen = REGISTRY["opencode-zen"];
const museSpark = zen.models.find((m) => m.id === "muse-spark-1.2");
const museSparkFree = zen.models.find((m) => m.id === "muse-spark-1.2-contributor-free");
assert.notEqual(museSpark?.contextLength, undefined);
assert.notEqual(museSparkFree?.contextLength, undefined);
});
test("#12681: contextManager.getTokenLimit resolves muse-spark-1.2-contributor-free to its real 1M+ window, not the 200000 provider default", () => {
assert.equal(getTokenLimit("opencode", "muse-spark-1.2-contributor-free"), 1048576);
assert.equal(getTokenLimit("opencode-zen", "muse-spark-1.2-contributor-free"), 1048576);
});