diff --git a/platform/github-app/README.md b/platform/github-app/README.md index ed831043..d9011b2e 100644 --- a/platform/github-app/README.md +++ b/platform/github-app/README.md @@ -153,9 +153,10 @@ owner must opt in again. Reads require repository write or admin access. or if the LLM fails, the plain table is posted. Backfill never comments, and rescores only update an existing comment. -Intent classification uses `TRIAGE_PROVIDER=llm` by default and the optional -`LLM_API_KEY`. Set `TRIAGE_PROVIDER=jev` with `OPENROUTER_API_KEY` to use Jev's -OpenRouter Decisions API instead. AI greetings independently use `LLM_API_KEY`; +Production intent classification is configured with `TRIAGE_PROVIDER=jev` and +the `OPENROUTER_API_KEY` secret for Jev's OpenRouter Decisions API. Local/staging +configuration retains `TRIAGE_PROVIDER=llm` with the optional `LLM_API_KEY`. +AI greetings independently use `LLM_API_KEY`; unavailable greetings fall back to the plain score table. Missing classifier credentials disable intent labeling. Issue and pull request text is never stored; opting in sends it to the configured classification provider. @@ -436,9 +437,9 @@ until then the API answers `auth_unavailable`. `LLM_API_KEY` is an optional secret and is deliberately not in `secrets.required`. Without it, AI greetings are off; intent classification is -also off under the default `llm` provider. Jev uses its separate OpenRouter -secret. The settings page reports these capabilities separately. For greetings -and the default classifier: +also off when using the `llm` provider. Production Jev uses its separate +OpenRouter secret. The settings page reports these capabilities separately. +For AI greetings or the local/staging LLM classifier: ```sh pnpm exec wrangler secret put LLM_API_KEY --env production @@ -450,6 +451,14 @@ the defaults or point them at another OpenAI-compatible provider. For Jev intent classification, set `TRIAGE_PROVIDER=jev`, install the optional `OPENROUTER_API_KEY` secret, and retain `JEV_MODEL=typesafe/jev-1.13`. +Production vars already select Jev; this does not enable any repository's +intent-label opt-in. `JEV_BASE_URL` is an operator-only API root, defaulting to +`https://openrouter.ai/api`; the Worker appends `/alpha/decisions`. A trailing +slash is optional. Custom roots must use HTTPS without userinfo, query or +fragment, and must implement the same typed Decisions API and pinned model. +Configure the URL in Worker vars, not repository settings; the API key stays +in the `OPENROUTER_API_KEY` secret. Invalid URLs disable Jev readiness and fail +before sending any request. Redirects are rejected without forwarding the key. `JEV_THRESHOLD=0.8` is a conservative initial setting, not a calibrated accuracy claim. Each saved existing label is evaluated against its GitHub description; at most three qualifying labels are added. Invalid or unavailable decisions diff --git a/platform/github-app/src/jev.ts b/platform/github-app/src/jev.ts index dc110c41..bf72d2d9 100644 --- a/platform/github-app/src/jev.ts +++ b/platform/github-app/src/jev.ts @@ -7,7 +7,8 @@ declare global { } } -export const JEV_ENDPOINT = "https://openrouter.ai/api/alpha/decisions"; +export const JEV_BASE_URL = "https://openrouter.ai/api"; +export const JEV_ENDPOINT = `${JEV_BASE_URL}/alpha/decisions`; export const JEV_MODEL = "typesafe/jev-1.13"; export const JEV_THRESHOLD = 0.8; export const JEV_REQUEST_VERSION = "description-only-v4-concrete-evidence"; @@ -19,6 +20,7 @@ const supportedModel = (model: string) => export function jevConfigured(env: Env): boolean { try { jevThreshold(env); + jevEndpoint(env); return ( !!env.OPENROUTER_API_KEY?.trim() && supportedModel(env.JEV_MODEL?.trim() || JEV_MODEL) @@ -27,6 +29,28 @@ export function jevConfigured(env: Env): boolean { return false; } } + +// Trusted operator configuration, never repository settings or request input. +export function jevEndpoint(env: Env): string { + const setting = env.JEV_BASE_URL?.trim() || JEV_BASE_URL; + try { + const base = new URL(setting); + if ( + !/^https:\/\//i.test(setting) || + base.protocol !== "https:" || + !base.hostname || + base.username || + base.password || + setting.split("/")[2].includes("@") || + /[?#\\\s]/.test(setting) + ) + throw new Error(); + return `${base.href.replace(/\/+$/, "")}/alpha/decisions`; + } catch { + // Do not echo potentially sensitive operator input into logs. + throw new Error("Invalid Jev base URL"); + } +} export interface IntentLabel { name: string; description: string; @@ -159,8 +183,9 @@ export async function classifyJev( const model = env.JEV_MODEL?.trim() || JEV_MODEL; if (!supportedModel(model)) throw new Error("Unsupported Jev model"); const threshold = jevThreshold(env); + const endpoint = jevEndpoint(env); const response = await jsonRequest( - JEV_ENDPOINT, + endpoint, { method: "POST", headers: { diff --git a/platform/github-app/tests/jev.test.ts b/platform/github-app/tests/jev.test.ts index b6859b9a..9c7a3093 100644 --- a/platform/github-app/tests/jev.test.ts +++ b/platform/github-app/tests/jev.test.ts @@ -4,6 +4,7 @@ import { ApiError } from "../src/github"; import { classifyJev, JEV_ENDPOINT, + JEV_BASE_URL, JEV_MODEL, jevConfigured, jevRequest, @@ -56,6 +57,87 @@ const preview = ( ); describe("Jev intent decisions", () => { + it("targets only the configured HTTPS API root with the Decisions path", async () => { + const fetcher = mock(response()); + for (const [base, endpoint] of [ + ["", JEV_ENDPOINT], + [" ", JEV_ENDPOINT], + [JEV_BASE_URL, JEV_ENDPOINT], + [` ${JEV_BASE_URL}/ `, JEV_ENDPOINT], + [ + "https://decisions.example.test/api///", + "https://decisions.example.test/api/alpha/decisions", + ], + [ + "https://decisions.example.test:8443/custom/v1", + "https://decisions.example.test:8443/custom/v1/alpha/decisions", + ], + ]) { + const settings = { ...jevEnv, JEV_BASE_URL: base }; + expect(jevConfigured(settings)).toBe(true); + expect((await preview(settings)).labels).toEqual(["问题"]); + const [url, init] = fetcher.mock.calls.at(-1) as unknown as [ + string, + RequestInit, + ]; + expect(url).toBe(endpoint); + expect(init.method).toBe("POST"); + expect(init.redirect).toBe("manual"); + expect(new Headers(init.headers).get("authorization")).toBe( + `Bearer ${KEY}`, + ); + expect(JSON.parse(String(init.body)).model).toBe(JEV_MODEL); + } + expect(fetcher).toHaveBeenCalledTimes(6); + }); + + it("rejects unsafe or malformed operator URLs before sending credentials", async () => { + const fetcher = mock(response()); + for (const base of [ + "http://decisions.example.test/api", + "//decisions.example.test/api", + "not-a-url", + "https:decisions.example.test/api", + "https://user:private@decisions.example.test/api", + "https://user@decisions.example.test/api", + "https://@decisions.example.test/api", + "https://decisions.example.test/api?token=private", + "https://decisions.example.test/api?", + "https://decisions.example.test/api#private", + "https://decisions.example.test/api#", + "https://decisions.example.test/a pi", + "https://decisions.example.test/a\npi", + "https:\\decisions.example.test/api", + ]) { + const settings = { ...jevEnv, JEV_BASE_URL: base }; + expect(jevConfigured(settings)).toBe(false); + await expect(preview(settings)).rejects.toThrow("Invalid Jev base URL"); + } + expect(fetcher).not.toHaveBeenCalled(); + }); + + it("fails closed on endpoint redirects without forwarding credentials", async () => { + const fetcher = vi.fn( + async () => + new Response(null, { + status: 307, + headers: { Location: "https://other.example.test/collect" }, + }), + ); + vi.stubGlobal("fetch", fetcher); + await expect( + preview({ + ...jevEnv, + JEV_BASE_URL: "https://decisions.example.test/api", + }), + ).rejects.toMatchObject({ status: 307 }); + expect(fetcher).toHaveBeenCalledTimes(1); + expect(fetcher.mock.calls[0]).toEqual([ + "https://decisions.example.test/api/alpha/decisions", + expect.objectContaining({ redirect: "manual" }), + ]); + }); + it("uses Decisions Noul questions with authoritative definitions and opaque IDs", async () => { const fetcher = mock(response()); const result = await preview(); diff --git a/platform/github-app/worker-configuration.d.ts b/platform/github-app/worker-configuration.d.ts index d385e68f..5e9d03f6 100644 --- a/platform/github-app/worker-configuration.d.ts +++ b/platform/github-app/worker-configuration.d.ts @@ -1,5 +1,5 @@ /* eslint-disable */ -// Generated by Wrangler by running `wrangler types --strict-vars=false` (hash: d73fb54115785c9d48623a47daabd42a) +// Generated by Wrangler by running `wrangler types --strict-vars=false` (hash: 5d3aad997023a38cb2fa613874d76feb) // Runtime types generated with workerd@1.20260911.1 2026-09-10 nodejs_compat interface __BaseEnv_Env { DB: D1Database; @@ -17,6 +17,7 @@ interface __BaseEnv_Env { LLM_BASE_URL: string; LLM_MODEL: string; TRIAGE_PROVIDER: string; + JEV_BASE_URL: string; JEV_MODEL: string; JEV_THRESHOLD: string; APP_PRIVATE_KEY: string; @@ -45,6 +46,7 @@ declare namespace Cloudflare { LLM_BASE_URL: string; LLM_MODEL: string; TRIAGE_PROVIDER: string; + JEV_BASE_URL: string; JEV_MODEL: string; JEV_THRESHOLD: string; APP_PRIVATE_KEY: string; @@ -60,7 +62,7 @@ type StringifyValues> = { [Binding in keyof EnvType]: EnvType[Binding] extends string ? EnvType[Binding] : string; }; declare namespace NodeJS { - interface ProcessEnv extends StringifyValues> {} + interface ProcessEnv extends StringifyValues> {} } // Begin runtime types diff --git a/platform/github-app/wrangler.jsonc b/platform/github-app/wrangler.jsonc index e0e7b72e..482ee5b5 100644 --- a/platform/github-app/wrangler.jsonc +++ b/platform/github-app/wrangler.jsonc @@ -27,10 +27,11 @@ "ENABLED": "false", "ALLOWED_ACCOUNTS": "AsperforMias", // Empty uses the defaults in src/llm.ts. LLM_API_KEY is an optional - // secret: without it, triage and AI comments are unavailable. + // secret: without it, LLM triage and AI greetings are unavailable. "LLM_BASE_URL": "", "LLM_MODEL": "", "TRIAGE_PROVIDER": "llm", + "JEV_BASE_URL": "https://openrouter.ai/api", "JEV_MODEL": "typesafe/jev-1.13", "JEV_THRESHOLD": "0.8", }, @@ -87,7 +88,8 @@ "ALLOWED_ACCOUNTS": "*", "LLM_BASE_URL": "", "LLM_MODEL": "", - "TRIAGE_PROVIDER": "llm", + "TRIAGE_PROVIDER": "jev", + "JEV_BASE_URL": "https://openrouter.ai/api", "JEV_MODEL": "typesafe/jev-1.13", "JEV_THRESHOLD": "0.8", },