diff --git a/pm-stack/aggregator/scripts/gather-quota.ts b/pm-stack/aggregator/scripts/gather-quota.ts index b5ed8635b7..71c824311a 100755 --- a/pm-stack/aggregator/scripts/gather-quota.ts +++ b/pm-stack/aggregator/scripts/gather-quota.ts @@ -67,6 +67,23 @@ // own credential machinery, which is what gathering quota by // asking OpenCode means for a route-only pool. // +// 6. The OpenDesign session keeper (src/opendesign-renewal.ts, +// Drew's 2026-11-05 provider browser-auth auto-renewal rule): +// family 1's OpenDesign leg rides on a Better Auth session in +// OpenChamber's managed credential store — the one credential +// in the tick that is a browser session rather than a token. +// Before the provider runs each tick, the keeper reads the +// stored session (authenticated reads slide its expiry once +// the update interval elapses — verified live 2026-11-05: +// expiresAt re-extended to read-time + 7 days), records the +// horizon in opendesign-renewal.json beside quota-current.json, +// re-captures the cookie from the standing laptop Helium over +// CDP when the stored session fails or its horizon runs low +// (validating the candidate before it is stored), and raises +// an alert in that state file while days remain when renewal +// cannot complete. Fail-soft: a keeper error never fails the +// tick. +// // Outputs: // - quota-current.json — the latest-state contract (model.ts // QuotaCurrentFile) in the deployed aggregator dir, beside @@ -145,6 +162,7 @@ import { } from "../src/clinepass-adapter.ts"; import type { CredentialRef } from "../src/token-adapter.ts"; import { safeProviderReason } from "../src/credential-lifecycle.ts"; +import { renewOpenDesignSession, type OpenDesignCredentialLike } from "../src/opendesign-renewal.ts"; const PKG = join(dirname(fileURLToPath(import.meta.url)), ".."); const HOME = homedir(); @@ -430,6 +448,50 @@ async function resolveClinePassCredentialValue(ref: CredentialRef): Promise OpenDesignCredentialLike | null; + writeManagedCredential: (providerId: string, value: OpenDesignCredentialLike) => unknown; +} +interface OpenDesignProviderModule { + fetchOpenDesignQuota: (credential: OpenDesignCredentialLike) => Promise; +} + +/** + * The OpenDesign session keeper run (family 6 above). Runs before + * the provider listing is fetched so a credential renewed this tick + * is the one the tick's OpenDesign read uses. Fail-soft: any error + * is logged and the tick proceeds with the stored credential as-is. + */ +async function runOpenDesignRenewal(tickAt: string): Promise { + if (process.env.OPENDESIGN_RENEWAL_DISABLED === "1") return; + try { + const credentials = (await import(join(libDir, "quota/credentials/providers.js"))) as unknown as OpenDesignCredentialsModule; + const provider = (await import(join(libDir, "quota/providers/opendesign.js"))) as unknown as OpenDesignProviderModule; + const state = await renewOpenDesignSession({ + readCredential: () => credentials.readManagedCredential("opendesign"), + writeCredential: (value) => { + credentials.writeManagedCredential("opendesign", value); + }, + validateCredential: async (credential) => { + await provider.fetchOpenDesignQuota(credential); + }, + cdpUrl: process.env.OPENDESIGN_CDP_URL ?? "http://127.0.0.1:19222", + statePath: process.env.OPENDESIGN_RENEWAL_STATE_PATH ?? join(dirname(currentPath), "opendesign-renewal.json"), + forceCapture: process.env.OPENDESIGN_RENEWAL_FORCE_CAPTURE === "1", + now: () => new Date(tickAt), + log: (line) => console.log(`quota-gatherer: ${line}`), + }); + if (state.status === "attention") { + console.log(`quota-gatherer: opendesign-renewal ATTENTION ${state.alert ?? ""}`); + } + } catch (err) { + console.log( + `quota-gatherer: opendesign-renewal failed-soft: ${safeProviderReason(err instanceof Error ? err.message : String(err))}`, + ); + } +} + /** * The native ClinePass adapter run (#1159). Null when no credential * reference is configured on this host — the adapter is registered @@ -830,6 +892,9 @@ async function main(): Promise { return failTick(tickAt, started, `quota module import failed: ${err instanceof Error ? err.message : String(err)}`); } + // Family 6: renew the OpenDesign session before its read. + await runOpenDesignRenewal(tickAt); + let providerIds: string[]; try { providerIds = await quota.listConfiguredQuotaProviders(); diff --git a/pm-stack/aggregator/src/opendesign-renewal.test.ts b/pm-stack/aggregator/src/opendesign-renewal.test.ts new file mode 100644 index 0000000000..09e23ad632 --- /dev/null +++ b/pm-stack/aggregator/src/opendesign-renewal.test.ts @@ -0,0 +1,129 @@ +import { describe, expect, test } from "bun:test"; +import { mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + horizonHours, + pickOpenDesignSessionCookie, + renewOpenDesignSession, + type OpenDesignCredentialLike, +} from "./opendesign-renewal.ts"; + +const NOW = new Date("2026-11-05T02:00:00.000Z"); +const statePath = () => join(mkdtempSync(join(tmpdir(), "od-renewal-")), "state.json"); +const stored: OpenDesignCredentialLike = { cookie: "better-auth.session_token=stored-token", workspaceId: "ws1" }; + +const baseDeps = (overrides: Record = {}) => ({ + readCredential: () => stored, + writeCredential: () => undefined, + validateCredential: async () => undefined, + statePath: statePath(), + now: () => NOW, + ...overrides, +}); + +describe("pickOpenDesignSessionCookie", () => { + test("prefers the __Secure- cookie on the app domain", () => { + const found = pickOpenDesignSessionCookie([ + { name: "better-auth.session_token", value: "a", domain: ".open-design.ai" }, + { name: "__Secure-better-auth.session_token", value: "b", domain: "app.open-design.ai" }, + { name: "other", value: "c", domain: ".open-design.ai" }, + ]); + expect(found?.value).toBe("b"); + }); + test("ignores other domains and empty values", () => { + expect(pickOpenDesignSessionCookie([{ name: "better-auth.session_token", value: "a", domain: ".example.com" }])).toBeNull(); + expect(pickOpenDesignSessionCookie([{ name: "better-auth.session_token", value: "", domain: ".open-design.ai" }])).toBeNull(); + }); +}); + +describe("horizonHours", () => { + test("computes whole/fractional hours and rejects garbage", () => { + expect(horizonHours("2026-11-12T02:00:00.000Z", NOW)).toBe(168); + expect(horizonHours("2026-11-05T03:30:00.000Z", NOW)).toBe(1.5); + expect(horizonHours(null, NOW)).toBeNull(); + expect(horizonHours("not-a-date", NOW)).toBeNull(); + }); +}); + +describe("renewOpenDesignSession", () => { + test("a healthy sliding session is valid and untouched", async () => { + let writes = 0; + const state = await renewOpenDesignSession( + baseDeps({ + writeCredential: () => writes++, + fetchSession: async () => ({ expiresAt: "2026-11-12T02:00:00.000Z" }), + }) as never, + ); + expect(state.status).toBe("valid"); + expect(state.horizonHours).toBe(168); + expect(state.capture.attempted).toBe(false); + expect(writes).toBe(0); + }); + + test("a dead stored session is renewed from the browser cookie, validated before storing", async () => { + let written: OpenDesignCredentialLike | null = null; + let validated: OpenDesignCredentialLike | null = null; + const state = await renewOpenDesignSession( + baseDeps({ + readCredential: () => written ?? stored, + writeCredential: (value: OpenDesignCredentialLike) => { + written = value; + }, + validateCredential: async (candidate: OpenDesignCredentialLike) => { + validated = candidate; + }, + fetchSession: async (cred: OpenDesignCredentialLike) => + cred.cookie.includes("fresh-token") ? { expiresAt: "2026-11-12T02:00:00.000Z" } : null, + getBrowserCookies: async () => [ + { name: "__Secure-better-auth.session_token", value: "fresh-token", domain: ".open-design.ai" }, + ], + }) as never, + ); + expect(state.status).toBe("renewed"); + expect(validated?.cookie).toContain("fresh-token"); + expect(written?.cookie).toContain("fresh-token"); + expect(written?.workspaceId).toBe("ws1"); + expect(state.horizonHours).toBe(168); + }); + + test("a candidate that fails validation never overwrites the stored credential, and the alert fires", async () => { + let writes = 0; + const state = await renewOpenDesignSession( + baseDeps({ + writeCredential: () => writes++, + validateCredential: async () => { + throw new Error("usage API said no"); + }, + fetchSession: async () => null, + getBrowserCookies: async () => [ + { name: "__Secure-better-auth.session_token", value: "stale-token", domain: ".open-design.ai" }, + ], + }) as never, + ); + expect(state.status).toBe("attention"); + expect(state.capture.result).toBe("validation-failed"); + expect(state.alert).toContain("no longer accepted"); + expect(writes).toBe(0); + }); + + test("no browser session + dead stored session = attention with a sign-in alert, days ahead of zero", async () => { + const state = await renewOpenDesignSession( + baseDeps({ + fetchSession: async () => null, + getBrowserCookies: async () => [], + refreshBrowserTab: async () => undefined, + }) as never, + ); + expect(state.status).toBe("attention"); + expect(state.capture.result).toBe("no-browser-session"); + expect(state.alert).toContain("sign-in"); + expect(state.alertSince).toBe(NOW.toISOString()); + }); + + test("no stored credential is a configuration state, not an alert", async () => { + const state = await renewOpenDesignSession(baseDeps({ readCredential: () => null }) as never); + expect(state.status).toBe("not-configured"); + expect(state.alert).toBeNull(); + }); +}); diff --git a/pm-stack/aggregator/src/opendesign-renewal.ts b/pm-stack/aggregator/src/opendesign-renewal.ts new file mode 100644 index 0000000000..ffd646c301 --- /dev/null +++ b/pm-stack/aggregator/src/opendesign-renewal.ts @@ -0,0 +1,385 @@ +/** + * OpenDesign session renewal — Drew's provider browser-auth + * auto-renewal rule (2026-11-05): no provider integration may depend + * on a browser cookie/session that silently expires. Every + * browser-dependent auth carries an auto-renewing long-lived token, + * or built-in cookie auto-renewal with an alert BEFORE hard expiry + * when renewal fails. + * + * Why OpenDesign needs this: its quota provider reads plan windows + * from the AMR billing API, which accepts ONLY a Better Auth session + * (cookie + workspace id, held in OpenChamber's managed quota + * credential store). The AMR inference API key is refused there + * (verified 403, 2026-11-03; the server-side gap is code#5060). The + * session was imported once by hand on 2026-10-29, its import-time + * expiry (2026-11-06) was treated as a hard death date, and the only + * plan was a one-shot re-import cron. + * + * What actually keeps it alive, verified live 2026-11-05: the Better + * Auth session SLIDES on use — once its update interval (observed + * ~24h) has elapsed, an authenticated get-session read re-extends + * expiresAt to read-time + 7 days (stored session created + * 2026-10-30T01:23Z; a read at 2026-11-05T01:59Z moved expiresAt to + * 2026-11-12T01:59Z, and a read 5h later left it untouched). Against + * a 7-day expiry, the gatherer's 15-minute tick was already renewing + * it by accident, with days of slack. This module makes that + * deliberate and monitored, and adds a browser re-capture for when + * use is not enough: + * + * 1. Every tick: read the stored session (get-session). The read + * is itself the renewal-by-use; expiresAt + horizon land in the + * state file, so the horizon is a watched number, not a + * surprise. + * 2. Stored session failed auth, or horizon below + * LOW_HORIZON_HOURS: re-capture from the standing laptop + * Helium over CDP — read the live better-auth session cookie + * from the browser profile, validate the candidate against the + * usage API BEFORE storing it, and only then replace the + * stored credential. A candidate that fails validation never + * overwrites a working credential. + * 3. Renewal cannot complete (the browser holds no Open Design + * session — sign-in is an agent/Drew step — or CDP is down): + * the state file carries an alert with the remaining horizon. + * The warning is raised while days remain, never as a silent + * lapse at zero. + * + * The state file is rewritten every run (see gather-quota.ts for + * the default path, beside quota-current.json). No credential value + * is logged or written anywhere in this module; capture moves the + * cookie from the browser straight into the managed store. + */ + +import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; +import { dirname } from "node:path"; + +export const OPENDESIGN_API_BASE = "https://amr-api.open-design.ai/api"; +export const OPENDESIGN_APP_URL = "https://open-design.ai"; +export const LOW_HORIZON_HOURS = 72; + +const SESSION_COOKIE_NAMES = ["__Secure-better-auth.session_token", "better-auth.session_token"]; +// The session header's name, kept as a constant: fleet log tooling +// pattern-matches inline cookie assignments and mangles them. +const SESSION_HEADER = "Cookie"; + +export interface OpenDesignCredentialLike { + cookie: string; + workspaceId: string; +} + +export interface BrowserCookie { + name: string; + value: string; + domain: string; +} + +export type CaptureResult = + | "stored" + | "no-browser-session" + | "cdp-unreachable" + | "validation-failed" + | "not-attempted"; + +export interface OpenDesignRenewalState { + provider: "opendesign"; + checkedAt: string; + status: "not-configured" | "valid" | "renewed" | "attention"; + sessionExpiresAt: string | null; + horizonHours: number | null; + capture: { attempted: boolean; result: CaptureResult | string | null }; + alert: string | null; + alertSince: string | null; +} + +export interface OpenDesignRenewalDeps { + readCredential: () => OpenDesignCredentialLike | null; + writeCredential: (value: OpenDesignCredentialLike) => void; + /** Throws when the credential cannot read real quota windows. */ + validateCredential: (credential: OpenDesignCredentialLike) => Promise; + statePath: string; + cdpUrl?: string; + now?: () => Date; + forceCapture?: boolean; + lowHorizonHours?: number; + /** Injectable for tests; default is the real get-session read. */ + fetchSession?: (credential: OpenDesignCredentialLike) => Promise<{ expiresAt: string | null } | null>; + /** Injectable for tests; defaults are the real CDP operations. */ + getBrowserCookies?: (cdpUrl: string) => Promise; + refreshBrowserTab?: (cdpUrl: string) => Promise; + log?: (line: string) => void; +} + +/** Pick the Open Design Better Auth session cookie out of a browser cookie jar. */ +export function pickOpenDesignSessionCookie(cookies: BrowserCookie[]): BrowserCookie | null { + const matches = cookies.filter( + (c) => SESSION_COOKIE_NAMES.includes(c.name) && c.domain.endsWith("open-design.ai") && c.value !== "", + ); + // The __Secure- prefixed name is the one the app sets today; prefer it. + return matches.find((c) => c.name.startsWith("__Secure-")) ?? matches[0] ?? null; +} + +export function horizonHours(expiresAt: string | null, now: Date): number | null { + if (!expiresAt) return null; + const ms = Date.parse(expiresAt) - now.getTime(); + return Number.isFinite(ms) ? Math.round((ms / 3_600_000) * 10) / 10 : null; +} + +const defaultFetchSession = async ( + credential: OpenDesignCredentialLike, +): Promise<{ expiresAt: string | null } | null> => { + const headers: Record = { + Accept: "application/json", + "x-vela-workspace-id": credential.workspaceId, + "User-Agent": "OpenChamber quota gatherer (opendesign renewal)", + }; + headers[SESSION_HEADER] = credential.cookie; + try { + const response = await fetch(`${OPENDESIGN_API_BASE}/auth/get-session`, { + method: "GET", + headers, + signal: AbortSignal.timeout(15_000), + }); + if (!response.ok) return null; + const payload = (await response.json().catch(() => null)) as + | { session?: { expiresAt?: unknown } | null } + | null; + // Better Auth answers 200 with a null/empty body when the cookie + // names no live session — that is an auth failure, not a session + // with an unknown expiry. + const session = payload?.session; + if (!session || typeof session !== "object") return null; + return { expiresAt: typeof session.expiresAt === "string" ? session.expiresAt : null }; + } catch { + return null; + } +}; + +interface CdpSocket { + addEventListener: (type: string, listener: (event: { data?: unknown }) => void) => void; + send: (data: string) => void; + close: () => void; +} + +const withTimeout = async (promise: Promise, ms: number, label: string): Promise => { + let timer: ReturnType | undefined; + try { + return await Promise.race([ + promise, + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms}ms`)), ms); + }), + ]); + } finally { + if (timer) clearTimeout(timer); + } +}; + +const cdpSession = async (cdpUrl: string): Promise<{ + send: (method: string, params?: Record) => Promise>; + close: () => void; +}> => { + const version = (await withTimeout( + fetch(`${cdpUrl}/json/version`).then((r) => r.json() as Promise<{ webSocketDebuggerUrl?: string }>), + 10_000, + "CDP version read", + )) as { webSocketDebuggerUrl?: string }; + if (!version.webSocketDebuggerUrl) throw new Error("CDP version response carried no webSocketDebuggerUrl"); + const SocketCtor = (globalThis as { WebSocket?: new (url: string) => CdpSocket }).WebSocket; + if (!SocketCtor) throw new Error("no WebSocket implementation available for CDP"); + const socket = new SocketCtor(version.webSocketDebuggerUrl); + await withTimeout( + new Promise((resolve, reject) => { + socket.addEventListener("open", () => resolve()); + socket.addEventListener("error", () => reject(new Error("CDP socket error"))); + }), + 10_000, + "CDP socket open", + ); + let nextId = 0; + const pending = new Map) => void>(); + socket.addEventListener("message", (event) => { + try { + const message = JSON.parse(String(event.data)) as { id?: number }; + if (typeof message.id === "number" && pending.has(message.id)) { + pending.get(message.id)?.(message as Record); + pending.delete(message.id); + } + } catch { + // Non-JSON frames are not answers; ignore them. + } + }); + const send = (method: string, params: Record = {}): Promise> => + withTimeout( + new Promise>((resolve) => { + const id = ++nextId; + pending.set(id, resolve); + socket.send(JSON.stringify({ id, method, params })); + }), + 15_000, + `CDP ${method}`, + ); + return { send, close: () => socket.close() }; +}; + +const defaultGetBrowserCookies = async (cdpUrl: string): Promise => { + const { send, close } = await cdpSession(cdpUrl); + try { + const answer = await send("Storage.getCookies", {}); + const result = (answer as { result?: { cookies?: BrowserCookie[] } }).result; + return Array.isArray(result?.cookies) ? result.cookies : []; + } finally { + close(); + } +}; + +const defaultRefreshBrowserTab = async (cdpUrl: string): Promise => { + const { send, close } = await cdpSession(cdpUrl); + try { + const created = await send("Target.createTarget", { url: OPENDESIGN_APP_URL }); + const targetId = (created as { result?: { targetId?: string } }).result?.targetId; + // Give the app a moment to load and slide/set its session cookie. + await new Promise((resolve) => setTimeout(resolve, 4_000)); + if (targetId) await send("Target.closeTarget", { targetId }).catch(() => undefined); + } finally { + close(); + } +}; + +const readPreviousState = async (statePath: string): Promise => { + try { + return JSON.parse(await readFile(statePath, "utf8")) as OpenDesignRenewalState; + } catch { + return null; + } +}; + +const writeState = async (statePath: string, state: OpenDesignRenewalState): Promise => { + await mkdir(dirname(statePath), { recursive: true }); + const tmp = `${statePath}.tmp-${process.pid}`; + await writeFile(tmp, JSON.stringify(state, null, 2) + "\n", "utf8"); + await rename(tmp, statePath); +}; + +/** + * One renewal cycle. Fail-soft by contract: it always resolves with + * a state object (which it also persists); it never throws, so the + * gatherer's tick can call it unconditionally. + */ +export async function renewOpenDesignSession(deps: OpenDesignRenewalDeps): Promise { + const now = (deps.now ?? (() => new Date()))(); + const lowHorizon = deps.lowHorizonHours ?? LOW_HORIZON_HOURS; + const fetchSession = deps.fetchSession ?? defaultFetchSession; + const getBrowserCookies = deps.getBrowserCookies ?? defaultGetBrowserCookies; + const refreshBrowserTab = deps.refreshBrowserTab ?? defaultRefreshBrowserTab; + const cdpUrl = deps.cdpUrl ?? "http://127.0.0.1:19222"; + const log = deps.log ?? (() => undefined); + const previous = await readPreviousState(deps.statePath); + + const finish = async ( + partial: Omit, + ): Promise => { + const state: OpenDesignRenewalState = { + provider: "opendesign", + checkedAt: now.toISOString(), + alertSince: partial.alert ? (previous?.alert ? previous.alertSince : now.toISOString()) : null, + ...partial, + }; + await writeState(deps.statePath, state).catch(() => undefined); + log( + `opendesign-renewal: status=${state.status} horizonHours=${state.horizonHours ?? "unknown"} capture=${state.capture.result ?? "not-attempted"}${state.alert ? ` alert=${state.alert}` : ""}`, + ); + return state; + }; + + const credential = deps.readCredential(); + if (!credential) { + return finish({ + status: "not-configured", + sessionExpiresAt: null, + horizonHours: null, + capture: { attempted: false, result: "not-attempted" }, + alert: null, + }); + } + + let session = await fetchSession(credential); + let horizon = horizonHours(session?.expiresAt ?? null, now); + const needsCapture = deps.forceCapture === true || session === null || horizon === null || horizon < lowHorizon; + + if (!needsCapture) { + return finish({ + status: "valid", + sessionExpiresAt: session?.expiresAt ?? null, + horizonHours: horizon, + capture: { attempted: false, result: "not-attempted" }, + alert: null, + }); + } + + // Re-capture from the standing browser profile. + let captureResult: CaptureResult | string = "no-browser-session"; + try { + let cookies: BrowserCookie[] | null = null; + try { + cookies = await getBrowserCookies(cdpUrl); + } catch { + captureResult = "cdp-unreachable"; + } + if (cookies !== null) { + let found = pickOpenDesignSessionCookie(cookies); + if (!found) { + // Loading the app can slide/set the browser's own session + // cookie; look once more before declaring the browser bare. + await refreshBrowserTab(cdpUrl).catch(() => undefined); + cookies = await getBrowserCookies(cdpUrl).catch(() => []); + found = pickOpenDesignSessionCookie(cookies); + } + if (found) { + const candidate: OpenDesignCredentialLike = { + cookie: `${found.name}=${found.value}`, + workspaceId: credential.workspaceId, + }; + try { + await deps.validateCredential(candidate); + deps.writeCredential(candidate); + captureResult = "stored"; + } catch { + captureResult = "validation-failed"; + } + } + } + } catch (error) { + captureResult = `error:${error instanceof Error ? error.message : String(error)}`; + } + + if (captureResult === "stored") { + const renewed = deps.readCredential(); + session = renewed ? await fetchSession(renewed) : null; + horizon = horizonHours(session?.expiresAt ?? null, now); + return finish({ + status: "renewed", + sessionExpiresAt: session?.expiresAt ?? null, + horizonHours: horizon, + capture: { attempted: true, result: captureResult }, + alert: null, + }); + } + + // Capture did not produce a credential. A still-valid stored + // session keeps the provider reading; the alert fires when the + // session is dead or its horizon is short, while days remain. + const atRisk = session === null || horizon === null || horizon < lowHorizon; + const alert = !atRisk + ? null + : session === null + ? `Open Design stored session is no longer accepted and browser re-capture failed (${captureResult}); an Open Design sign-in in the laptop Helium profile is needed to re-arm renewal.` + : horizon === null + ? `Open Design session read returned no expiry and browser re-capture failed (${captureResult}); if the stored session lapses, an Open Design sign-in in the laptop Helium profile is needed to re-arm renewal.` + : `Open Design session horizon is ${horizon}h (< ${lowHorizon}h) and browser re-capture failed (${captureResult}); if the gatherer stays down or the stored session lapses, an Open Design sign-in in the laptop Helium profile is needed to re-arm renewal.`; + return finish({ + status: atRisk ? "attention" : "valid", + sessionExpiresAt: session?.expiresAt ?? null, + horizonHours: horizon, + capture: { attempted: true, result: captureResult }, + alert, + }); +}