From b1d66d6eb45d9b9e1141ac756210dd4a0ec11949 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 31 Aug 2026 16:33:47 +0000 Subject: [PATCH] fix: unblock live fixture recording Co-authored-by: Simon Shi --- scripts/live-fixture-recorder.ts | 9 ++++ scripts/record-live-fixtures.ts | 85 +++++++++++++++----------------- 2 files changed, 50 insertions(+), 44 deletions(-) create mode 100644 scripts/live-fixture-recorder.ts diff --git a/scripts/live-fixture-recorder.ts b/scripts/live-fixture-recorder.ts new file mode 100644 index 0000000..dd3e806 --- /dev/null +++ b/scripts/live-fixture-recorder.ts @@ -0,0 +1,9 @@ +export type RecorderStage = "patExchange" | "userinfo" | "modelList" | "chat"; + +export function classify(url: string): RecorderStage { + if (url.includes("/jobToken/exchange")) return "patExchange"; + if (url.includes("/userinfo")) return "userinfo"; + if (url.includes("/model/list")) return "modelList"; + if (url.includes("/chat") || url.includes("/agent_chat_generation")) return "chat"; + throw new Error(`[live] refusing to record unexpected endpoint: ${new URL(url).pathname}`); +} diff --git a/scripts/record-live-fixtures.ts b/scripts/record-live-fixtures.ts index 45766a2..604124f 100644 --- a/scripts/record-live-fixtures.ts +++ b/scripts/record-live-fixtures.ts @@ -9,9 +9,9 @@ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; +import { classify, type RecorderStage as Stage } from "./live-fixture-recorder.js"; type Region = "global" | "cn"; -type Stage = "patExchange" | "userinfo" | "modelList" | "chat"; interface RecordedInteraction { request: { @@ -27,6 +27,41 @@ interface RecordedInteraction { }; } +// These tables are used by fetch interception during recordRegion(). Keep them +// initialized before the top-level recording awaits begin. +const retainedHeaders = new Set([ + "accept", + "content-type", + "user-agent", + "cosy-version", + "cosy-clienttype", + "x-model-key", + "x-model-source", +]); + +const sensitiveKeys = new Set([ + "token", + "access_token", + "refresh_token", + "job_token", + "job_refresh_token", + "security_oauth_token", + "personal_token", + "authorization", + "cookie", + "set-cookie", + "user_id", + "userid", + "uid", + "machine_id", + "machineid", + "request_id", + "requestid", + "request_set_id", + "chat_record_id", + "session_id", +]); + const regions = parseRegions(process.argv.slice(2)); const pats = new Map(); for (const region of regions) { @@ -102,7 +137,7 @@ async function recordRegion(region: Region, pat: string): Promise { }) as typeof fetch; const { credentialsFromPat } = await import("../src/pat.js"); - const { getCachedModels, updateQoderModelsCache } = await import("../src/models.js"); + const { getCachedModelConfig, getCachedModels, updateQoderModelsCache } = await import("../src/models.js"); const { streamQoder } = await import("../src/stream.js"); const credentials = (await credentialsFromPat(pat, region)) as unknown as { @@ -125,7 +160,10 @@ async function recordRegion(region: Region, pat: string): Promise { await updateQoderModelsCache(credentials.access, credentials.userID, credentials.name, credentials.email, region); const models = getCachedModels(region); - const model = models.find((candidate) => (region === "global" ? candidate.id === "lite" : true)); + const model = + region === "global" + ? models.find((candidate) => getCachedModelConfig(candidate.id, region)?.key === "lite") + : models[0]; if (!model) throw new Error(`[live] ${region}: model catalog returned no enabled model`); const events: Array<{ @@ -197,14 +235,6 @@ async function readResponseText(response: Response, stage: Stage): Promise { return Object.fromEntries( [...headers.entries()] @@ -296,29 +316,6 @@ function redactSse(text: string): string { .join("\n"); } -const sensitiveKeys = new Set([ - "token", - "access_token", - "refresh_token", - "job_token", - "job_refresh_token", - "security_oauth_token", - "personal_token", - "authorization", - "cookie", - "set-cookie", - "user_id", - "userid", - "uid", - "machine_id", - "machineid", - "request_id", - "requestid", - "request_set_id", - "chat_record_id", - "session_id", -]); - function redactValue(value: unknown, key = ""): unknown { const normalizedKey = key.toLowerCase(); if (sensitiveKeys.has(normalizedKey) || normalizedKey === "id") {