From 769fc85e17d531a80f33ac8f2bf2ef2f5b57c701 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Sun, 25 Oct 2026 19:19:38 +0200 Subject: [PATCH 01/19] fix: honor automatic updates preference and add Settings controls --- components/settings/controls.tsx | 2 +- components/settings/settings-dialog.tsx | 27 +-- components/settings/update-settings.tsx | 113 +++++++++ components/sidebar.tsx | 9 +- components/update-banner.tsx | 23 +- electron/main.cjs | 105 +++++++-- electron/preload.cjs | 30 ++- next.config.ts | 5 + src/core/settings.ts | 2 +- tests/automatic-updates.test.ts | 299 ++++++++++++++++++++++++ tests/desktop-platform.test.ts | 38 ++- tests/render-update-banner.test.tsx | 15 +- tests/render-update-settings.test.tsx | 223 ++++++++++++++++++ tests/tooltip-mechanism.test.tsx | 4 +- 14 files changed, 820 insertions(+), 75 deletions(-) create mode 100644 components/settings/update-settings.tsx create mode 100644 tests/automatic-updates.test.ts create mode 100644 tests/render-update-settings.test.tsx diff --git a/components/settings/controls.tsx b/components/settings/controls.tsx index c7ee2f6..4aaa385 100644 --- a/components/settings/controls.tsx +++ b/components/settings/controls.tsx @@ -63,7 +63,7 @@ export function SettingsRow({ dimmed, }: { label: string; - hint?: string; + hint?: ReactNode; children: React.ReactNode; testid?: string; /** Dependency graying, graph-view style: the whole row dims when the diff --git a/components/settings/settings-dialog.tsx b/components/settings/settings-dialog.tsx index 1142bdf..9145e0c 100644 --- a/components/settings/settings-dialog.tsx +++ b/components/settings/settings-dialog.tsx @@ -40,7 +40,8 @@ import { type UserSettings, } from "@/src/core"; import { useAppearance } from "./appearance.tsx"; -import { SettingsSection, SettingsRow, Toggle } from "./controls.tsx"; +import { SettingsSection } from "./controls.tsx"; +import { UpdateSettings } from "./update-settings.tsx"; import { AgentSection } from "./agent-section.tsx"; import type { ModelsSettings } from "@/src/core"; import { MENU_CLOSE_EVENT, MENU_OPEN_EVENT } from "@/components/menu-shell"; @@ -75,10 +76,6 @@ const DialogContext = createContext<{ open: (section?: string, row?: string) => /** AppShell-level mount: the sidebar row and the mod+, chord call this. * `open(section)` lands the dialog on a specific section (the first-run * setup flow deep-links to Keys when no provider key exists yet). */ -/** The auto-update row's hint (tooltip wave): honest user language — no - * stream vocabulary. Exported for the copy pin (tooltip-mechanism test). */ -export const AUTO_UPDATE_HINT = "Automatic updates arrive in an upcoming release"; - export function SettingsDialogProvider({ children, }: { @@ -92,6 +89,7 @@ export function SettingsDialogProvider({ setLandOnRow(row ?? null); setShow(true); }, []); + useEffect(() => window.lh?.onOpenSettings?.((section) => open(section)), [open]); return ( {children} @@ -362,19 +360,12 @@ export function SettingsDialogBody({ title="General" testid="section-general" > - - - void write("general", { autoUpdate: on }) - } - /> - + + void write("general", { ...record.general, autoUpdate: on }) + } + /> )} {active === "keys" && ( diff --git a/components/settings/update-settings.tsx b/components/settings/update-settings.tsx new file mode 100644 index 0000000..71b948d --- /dev/null +++ b/components/settings/update-settings.tsx @@ -0,0 +1,113 @@ +"use client"; + +import { useEffect, useState } from "react"; +import { Button } from "@/components/ui/button"; +import { BrailleSpinner } from "@/components/braille-spinner"; +import { SettingsRow, Toggle } from "./controls.tsx"; + +/** Observational snapshot from electron/main.cjs; never updater permission. */ +export type UpdateState = { + status: "idle" | "checking" | "downloading" | "up-to-date" | "downloaded" | "error" | "disabled"; + version: string | null; + revision: number; +}; + +export const APP_VERSION = process.env.LH_VERSION ?? ""; +const HOMEPAGE = process.env.LH_HOMEPAGE ?? ""; +export const MANUAL_UPDATE_HINT = "Checking downloads available updates. Downloaded updates install when you quit the app."; +export const AUTO_UPDATE_HINT = "Check at startup and every 24 hours. Turning this off does not stop updates already in progress or ready to install."; +const UPDATE_ERROR = "Couldn’t check for updates. Try again later."; + +/** Subscribe before requesting the snapshot. IPC replies and pushes can + * cross; a revision prevents an older reply from hiding a newer event. + * The banner shares this read-only observer so late loads recover too. */ +export function useUpdateState() { + const [state, setState] = useState(null); + const [failed, setFailed] = useState(false); + useEffect(() => { + const lh = window.lh; + if (!lh) return; + let live = true; + let revision = -1; + const accept = (next: UpdateState) => { + if (!live || next.revision < revision) return; + revision = next.revision; + setState(next); + setFailed(false); + }; + const off = lh.onUpdateStatus?.(accept); + void lh.getUpdateState?.().then(accept).catch(() => { + if (live && revision === -1) setFailed(true); + }); + return () => { live = false; off?.(); }; + }, []); + return { state, failed }; +} + +export function updateStatusText(state: UpdateState | null): string | null { + switch (state?.status) { + case "checking": return "Checking for updates…"; + case "downloading": return state.version ? `Downloading version ${state.version}…` : "Downloading an update…"; + case "up-to-date": return "No newer version was found at the last check."; + case "downloaded": return `${state.version ? `Version ${state.version}` : "An update"} is ready. It will install when you quit the app.`; + case "error": return UPDATE_ERROR; + default: return null; + } +} + +/** Version information is useful everywhere; update controls only belong + * to a shell with an available updater. Opening this row never checks. */ +export function UpdateSettings({ + autoUpdate, + onAutoUpdateChange, + version = APP_VERSION, + homepage = HOMEPAGE, +}: { + autoUpdate: boolean; + onAutoUpdateChange: (on: boolean) => void; + version?: string; + homepage?: string; +}) { + const { state, failed } = useUpdateState(); + const [checkFailed, setCheckFailed] = useState(false); + useEffect(() => setCheckFailed(false), [state]); + const active = state !== null && state.status !== "disabled"; + const busy = state?.status === "checking" || state?.status === "downloading"; + const status = state?.status === "downloaded" ? updateStatusText(state) + : failed || checkFailed ? UPDATE_ERROR : updateStatusText(state); + const releaseNotes = version && homepage ? `${homepage}/releases/tag/v${version}` : null; + const check = () => { + setCheckFailed(false); + void window.lh?.checkForUpdates?.().catch(() => setCheckFailed(true)); + }; + + return ( + <> + + {status && {status}} + {active && {MANUAL_UPDATE_HINT}} + {releaseNotes && Release notes} + + } + > + {active && (state.status === "downloaded" ? ( + + ) : ( + + ))} + + {active && ( + + + + )} + + ); +} diff --git a/components/sidebar.tsx b/components/sidebar.tsx index d853f5c..6ca9c21 100644 --- a/components/sidebar.tsx +++ b/components/sidebar.tsx @@ -49,6 +49,7 @@ const STORAGE_KEY = "lh:sidebar-open"; * Mirrors SIDEBAR_AUTOCLOSE in electron/main.cjs — keep in sync. */ import type { ChatGPTControlBridge, NativeSubscriptionControlBridge } from "@/src/core"; +import type { UpdateState } from "@/components/settings/update-settings.tsx"; const SIDEBAR_AUTOCLOSE = 928; // Module scope, guarded for SSR (the server has no navigator). @@ -66,9 +67,13 @@ declare global { /** The zoom truth (owner round 7): subscribes to "lh:zoom" pushes; returns unsubscribe. */ onZoom?: (cb: (factor: number) => void) => () => void; getZoom?: () => Promise; - /** Auto-update channel (Wave C, 2026-10-10): "lh:update-status" pushes once a staged update exists. */ - onUpdateStatus?: (cb: (status: { status: string; version: string | null }) => void) => () => void; + /** Observational update events and a snapshot for late subscribers. */ + onUpdateStatus?: (cb: (status: UpdateState) => void) => () => void; + getUpdateState?: () => Promise; + /** Explicit manual check also downloads and stages available updates. */ + checkForUpdates?: () => Promise; installUpdate?: () => void; + onOpenSettings?: (cb: (section: string) => void) => () => void; }; } } diff --git a/components/update-banner.tsx b/components/update-banner.tsx index cd63030..a769ec2 100644 --- a/components/update-banner.tsx +++ b/components/update-banner.tsx @@ -3,32 +3,19 @@ /** * The auto-update banner (Wave C, owner 2026-10-10: silent download + * restart-banner UX). The shell pushes "lh:update-status" once a staged - * update exists (electron/main.cjs update-downloaded); this is the whole - * renderer surface — a small fixed card, bottom-right, offering Restart. + * update exists (electron/main.cjs update-downloaded). Snapshot retrieval + * also covers a late load. The card offers Restart alongside Settings. * Browser builds have no window.lh and never see a push: renders null. * * No dismiss: a downloaded update installs on quit anyway * (autoInstallOnAppQuit); the banner is a courtesy, not a gate. */ -import { useEffect, useState } from "react"; - -// The window.lh bridge type lives in components/sidebar.tsx (the incumbent -// declare-global) — onUpdateStatus/installUpdate are declared there too. - -type UpdateStatus = { status: string; version: string | null }; +import { useUpdateState } from "@/components/settings/update-settings.tsx"; export default function UpdateBanner() { - const [ready, setReady] = useState(null); - - useEffect(() => { - // Absent bridge (browser / dev) → stays hidden forever. - return window.lh?.onUpdateStatus?.((s) => { - if (s?.status === "downloaded") setReady(s); - }); - }, []); - - if (!ready) return null; + const { state: ready } = useUpdateState(); + if (ready?.status !== "downloaded") return null; return (
{ - // The download itself is silent (autoDownload); the banner fires on - // update-downloaded. Manual checks additionally answer directly. - if (!manual) return; - const version = result?.updateInfo?.version; - if (!version) dialog.showMessageBox({ type: "info", message: "You're up to date.", detail: `LearnHarness ${app.getVersion()} is the latest version.` }); + // A declined check (e.g. an unsupported package) establishes no + // latest-version claim. Real results arrive through updater events. + if (!result) reportUpdateStatus("idle"); }) .catch((err) => { log(`update check failed: ${err?.message ?? err}`); - if (manual) dialog.showMessageBox({ type: "warning", message: "Couldn't check for updates.", detail: String(err?.message ?? err) }); + reportUpdateStatus("error"); }); } @@ -477,12 +504,14 @@ function installUpdateNow() { function setupAutoUpdater() { if (!app.isPackaged) { log("auto-update: dev build — updater disabled"); + reportUpdateStatus("disabled"); return; } try { autoUpdater = require("electron-updater").autoUpdater; } catch (err) { log(`auto-update: electron-updater unavailable (${err?.message ?? err}) — updates disabled`); + reportUpdateStatus("disabled"); return; } const feed = process.env.LH_UPDATE_FEED; @@ -500,11 +529,37 @@ function setupAutoUpdater() { debug: (m) => {}, // too chatty for the persistent log error: (m) => log(`updater error: ${m}`), }; + autoUpdater.on("checking-for-update", () => reportUpdateStatus("checking")); + autoUpdater.on("update-available", (info) => reportUpdateStatus("downloading", info?.version)); + autoUpdater.on("update-not-available", () => reportUpdateStatus("up-to-date")); autoUpdater.on("update-downloaded", (info) => { log(`update downloaded: ${info?.version} — banner shown`); - broadcastUpdateStatus({ status: "downloaded", version: info?.version ?? null }); + reportUpdateStatus("downloaded", info?.version); }); - autoUpdater.on("error", (err) => log(`updater error: ${err?.message ?? err}`)); + autoUpdater.on("error", (err) => { + log(`updater error: ${err?.message ?? err}`); + reportUpdateStatus("error"); + }); +} + +/** One result surface for manual checks. Preload buffers the navigation + * request until React subscribes, including when all macOS windows closed. */ +function checkForUpdatesFromMenu() { + try { + const win = BrowserWindow.getAllWindows()[0] ?? reopenWindow?.(); + if (win) { + if (win.isMinimized()) win.restore(); + win.focus(); + const openSettings = () => { + try { win.webContents.send("lh:open-settings", "general"); } catch { /* window closed */ } + }; + if (win.webContents.isLoadingMainFrame()) win.webContents.once("did-finish-load", openSettings); + else openSettings(); + } + } catch { + /* Navigation failure must not prevent the manual check. */ + } + checkForUpdates({ manual: true }); } function zoomStep(win, dir) { @@ -530,9 +585,8 @@ function installMenu() { label: app.name, submenu: [ { role: "about" }, - // Manual check (Wave C): answers via native dialog — no renderer - // round-trip. The 24h automatic checks stay silent. - { label: "Check for Updates…", click: () => checkForUpdates({ manual: true }) }, + // Manual checks report their result in Settings → General. + { label: "Check for Updates…", click: () => checkForUpdatesFromMenu() }, { type: "separator" }, { role: "hide" }, { role: "hideOthers" }, @@ -654,6 +708,14 @@ async function main() { // are log lines — this can never block boot. setupAutoUpdater(); ipcMain.on("lh:update-install", () => installUpdateNow()); + ipcMain.handle("lh:update:get", (event) => { + if (!trustedAppWindows.has(BrowserWindow.fromWebContents(event.sender)) || !trustedSender(event, new URL(url).origin, BrowserWindow)) throw new Error("Untrusted desktop request."); + return updateState; + }); + ipcMain.handle("lh:update:check", (event) => { + if (!trustedAppWindows.has(BrowserWindow.fromWebContents(event.sender)) || !trustedSender(event, new URL(url).origin, BrowserWindow)) throw new Error("Untrusted desktop request."); + checkForUpdates({ manual: true }); + }); // Sidebar-at-narrow-window (owner, 2026-09-17): the renderer auto-closes // the sidebar below SIDEBAR_AUTOCLOSE (928px); opening it again at that @@ -670,13 +732,14 @@ async function main() { const target = SIDEBAR_AUTOCLOSE; if (width < target) win.setBounds({ x, y, width: target, height }); }); + reopenWindow = () => { chatgpt?.resume(); subscriptions?.resume(); return createWindow(url); }; app.on("activate", () => { - if (BrowserWindow.getAllWindows().length === 0) { chatgpt?.resume(); subscriptions?.resume(); createWindow(url); } + if (BrowserWindow.getAllWindows().length === 0) reopenWindow(); }); createWindow(url); log(`window open: ${url}`); - // First update check AFTER the window exists (the downloaded-banner - // push needs a listener); dev builds no-op (no autoUpdater). + // Startup + 24h checks stay main-owned and read the current preference. + // No renderer subscription is required; late loads retrieve the snapshot. if (autoUpdater) { checkForUpdates(); setInterval(() => checkForUpdates(), UPDATE_CHECK_INTERVAL_MS); diff --git a/electron/preload.cjs b/electron/preload.cjs index 4912005..9294291 100644 --- a/electron/preload.cjs +++ b/electron/preload.cjs @@ -19,14 +19,25 @@ * * window.lh.onUpdateStatus(cb) / window.lh.installUpdate() — the * auto-update channel (Wave C, 2026-10-10): "lh:update-status" pushes - * {status:"downloaded", version} once an update is staged; installUpdate - * asks main to kill the server and quit-and-install. Browser builds: - * both absent, the banner never renders. + * {status, version, revision} on updater events; installUpdate asks main + * to kill the server and quit-and-install. getUpdateState retrieves the + * snapshot for a late subscriber; checkForUpdates requests a manual + * check (including download/install-on-quit, even with automatic checks OFF). + * + * window.lh.onOpenSettings(cb) — menu navigation, buffered until React + * subscribes after a window is reopened. Browser builds have no bridge. * * Channel names are mirrored in electron/main.cjs — keep them in sync. */ const { contextBridge, ipcRenderer } = require("electron"); +let openSettingsListener = null; +let pendingOpenSettings = null; +ipcRenderer.on("lh:open-settings", (_event, section) => { + if (openSettingsListener) openSettingsListener(section); + else pendingOpenSettings = section; +}); + contextBridge.exposeInMainWorld("lh", { platform: process.platform, chatgpt: { @@ -71,4 +82,17 @@ contextBridge.exposeInMainWorld("lh", { return () => ipcRenderer.removeListener("lh:update-status", listener); }, installUpdate: () => ipcRenderer.send("lh:update-install"), + getUpdateState: () => ipcRenderer.invoke("lh:update:get"), + checkForUpdates: () => ipcRenderer.invoke("lh:update:check"), + onOpenSettings: (cb) => { + openSettingsListener = cb; + if (pendingOpenSettings !== null) { + const section = pendingOpenSettings; + pendingOpenSettings = null; + cb(section); + } + return () => { + if (openSettingsListener === cb) openSettingsListener = null; + }; + }, }); diff --git a/next.config.ts b/next.config.ts index db9352e..94193d8 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,6 +1,11 @@ +import { readFileSync } from "node:fs"; import type { NextConfig } from "next"; +const pkg = JSON.parse(readFileSync(new URL("./package.json", import.meta.url), "utf8")) as { version: string; homepage: string }; + const nextConfig: NextConfig = { + // Public build metadata only; no new runtime Electron version API. + env: { LH_VERSION: pkg.version, LH_HOMEPAGE: pkg.homepage }, // Desktop target (Electron wrapper, 2026-09-17): standalone output = a // self-contained .next/standalone/server.js the Electron shell spawns // on 127.0.0.1. Harmless for plain web deploys (extra build output). diff --git a/src/core/settings.ts b/src/core/settings.ts index 4daece8..ce80c65 100644 --- a/src/core/settings.ts +++ b/src/core/settings.ts @@ -231,7 +231,7 @@ export const ROLE_SLOT: Readonly> = { /** General settings — app-level behavior. Version DISPLAY and plans login * live outside the record (package.json / auth.json) by owner decision. */ export interface GeneralSettings { - /** Auto-update the app (default ON). Mechanism ships in a later wave. */ + /** Automatic startup/24h update checks (default ON). Manual checks bypass this. */ autoUpdate?: boolean; } diff --git a/tests/automatic-updates.test.ts b/tests/automatic-updates.test.ts new file mode 100644 index 0000000..5875a7e --- /dev/null +++ b/tests/automatic-updates.test.ts @@ -0,0 +1,299 @@ +/** Actual shell functions with fake Electron/updater/filesystem. No app, + * profile, network, timer or updater download/install operation is run. */ +import assert from "node:assert/strict"; +import { EventEmitter } from "node:events"; +import { readFileSync } from "node:fs"; +import { createRequire } from "node:module"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; +import { createContext, runInContext } from "node:vm"; +import { describe, it } from "node:test"; + +const mainFile = fileURLToPath(new URL("../electron/main.cjs", import.meta.url)); +const mainSource = readFileSync(mainFile, "utf8"); +const mainRequire = createRequire(mainFile); + +function fixture({ packaged = true, updaterMissing = false } = {}) { + let settings: string | Error = JSON.stringify({ version: 1, general: { autoUpdate: true } }); + const settingsReads: string[] = []; + const order: string[] = []; + const intervals: { callback: () => void; delay: number }[] = []; + const handles = new Map unknown>(); + const windows: FakeWindow[] = []; + let enumerationFails = false; + let userData = "/fixture/custom-user-data"; + let menu: { submenu?: { label?: string; click?: () => void }[] }[] = []; + class FakeUpdater extends EventEmitter { + checks = 0; + autoDownload = false; + autoInstallOnAppQuit = false; + result: object | null = { updateInfo: { version: "0.3.7" } }; + failure: Error | null = null; + checkForUpdates() { + this.checks++; + this.emit("checking-for-update"); + if (this.failure) return Promise.reject(this.failure); + if (this.result) this.emit("update-not-available", { version: "0.3.7" }); + return Promise.resolve(this.result); + } + quitAndInstall() { order.push("install"); } + } + const updater = new FakeUpdater(); + class FakeWebContents extends EventEmitter { + mainFrame = { url: "http://127.0.0.1:9000/" }; + messages: { channel: string; value: unknown }[] = []; + loading = false; + fails = false; + send(channel: string, value: unknown) { + if (this.fails) throw new Error("window destroyed"); + this.messages.push({ channel, value: JSON.parse(JSON.stringify(value)) }); + } + isLoadingMainFrame() { return this.loading; } + setWindowOpenHandler() {} + getZoomFactor() { return 1; } + } + class FakeWindow extends EventEmitter { + webContents = new FakeWebContents(); + minimized = false; + focused = false; + constructor() { super(); windows.push(this); } + loadURL() { this.webContents.loading = true; } + isMinimized() { return this.minimized; } + restore() { this.minimized = false; } + focus() { this.focused = true; } + show() {} + static getAllWindows() { + if (enumerationFails) throw new Error("window enumeration failed"); + return windows; + } + static fromWebContents(contents: FakeWebContents) { return windows.find(win => win.webContents === contents); } + } + const app = Object.assign(new EventEmitter(), { + isPackaged: packaged, + name: "LearnHarness", + setName() {}, + getPath: () => userData, + setPath: (_: string, value: string) => { userData = value; }, + requestSingleInstanceLock: () => false, + quit() {}, + whenReady: async () => {}, + }); + const electron = { + app, BrowserWindow: FakeWindow, + Menu: { buildFromTemplate: (value: typeof menu) => value, setApplicationMenu: (value: typeof menu) => { menu = value; } }, + ipcMain: { on() {}, handle: (channel: string, fn: (event: unknown) => unknown) => handles.set(channel, fn) }, + session: { defaultSession: { setPermissionRequestHandler() {}, setPermissionCheckHandler() {} } }, + }; + const context = createContext({ + require: (id: string) => { + if (id === "electron") return electron; + if (id === "electron-updater") { + if (updaterMissing) throw new Error("missing updater"); + return { autoUpdater: updater }; + } + if (id === "node:fs") return { + readFileSync: (file: string) => { + settingsReads.push(file); + if (settings instanceof Error) throw settings; + return settings; + }, mkdirSync() {}, appendFileSync() {}, + }; + return mainRequire(id); + }, + process: { argv: [], env: { LH_DEV_URL: "http://127.0.0.1:9000" }, platform: "darwin", on() {} }, + __dirname: path.dirname(mainFile), URL, + setInterval: (callback: () => void, delay: number) => intervals.push({ callback, delay }), + setTimeout() {}, + fixtureChild: { kill: (signal: string) => order.push(signal) }, + }); + runInContext(mainSource, context, { filename: mainFile }); + const run = (code: string) => runInContext(code, context); + return { + run, updater, windows, intervals, handles, settingsReads, order, app, + setSettings: (value: unknown) => { settings = JSON.stringify(value); }, + setSettingsFailure: (value: string | Error) => { settings = value; }, + setEnumerationFailure: (value: boolean) => { enumerationFails = value; }, + window: () => new FakeWindow(), + state: () => JSON.parse(JSON.stringify(run("updateState"))), + check: async (manual = false) => { run(`checkForUpdates({ manual: ${manual} })`); await Promise.resolve(); await Promise.resolve(); }, + menuCheck: () => menu[0]!.submenu!.find(item => item.label === "Check for Updates…")!.click!(), + }; +} + +describe("automatic-update preference gate", () => { + it("explicit OFF skips automatic checks, but manual checks still run without reading the preference", async () => { + const f = fixture(); + f.setSettings({ general: { autoUpdate: false } }); + f.run("setupAutoUpdater()"); + await f.check(); + assert.equal(f.updater.checks, 0); + assert.deepEqual(f.settingsReads, ["/fixture/custom-user-data-dev/data/settings.json"]); + await f.check(true); + assert.equal(f.updater.checks, 1); + assert.equal(f.settingsReads.length, 1); + assert.equal(f.updater.autoDownload, true); + assert.equal(f.updater.autoInstallOnAppQuit, true); + }); + + for (const [label, setting] of [ + ["ON", { general: { autoUpdate: true } }], ["absent field", { general: {} }], + ["absent section", { version: 1 }], ["null", null], ["non-boolean", { general: { autoUpdate: "false" } }], + ] as const) { + it(`${label} keeps automatic checks enabled`, async () => { + const f = fixture(); f.setSettings(setting); f.run("setupAutoUpdater()"); await f.check(); + assert.equal(f.updater.checks, 1); + }); + } + for (const [label, failure] of [["missing", new Error("ENOENT")], ["unreadable", new Error("EACCES")], ["corrupt", "{nope"]] as const) { + it(`${label} settings keep automatic checks enabled`, async () => { + const f = fixture(); f.setSettingsFailure(failure); f.run("setupAutoUpdater()"); await f.check(); + assert.equal(f.updater.checks, 1); + }); + } + + it("re-reads the setting on each check, without triggering a check merely when saved", async () => { + const f = fixture(); f.run("setupAutoUpdater()"); + f.setSettings({ general: { autoUpdate: false } }); await f.check(); + f.setSettings({ general: { autoUpdate: true } }); + assert.equal(f.updater.checks, 0); + await f.check(); + f.setSettings({ general: { autoUpdate: false } }); await f.check(); + assert.equal(f.updater.checks, 1); + assert.equal(f.settingsReads.length, 3); + }); + + for (const label of ["unpackaged", "missing updater"] as const) { + it(`${label} stays unavailable, not a saved OFF preference`, async () => { + const f = fixture({ packaged: label !== "unpackaged", updaterMissing: label === "missing updater" }); + f.run("setupAutoUpdater()"); await f.check(true); await f.check(); + assert.equal(f.updater.checks, 0); + assert.equal(f.settingsReads.length, 0); + assert.equal(f.state().status, "disabled"); + }); + } +}); + +describe("status observation never controls the updater", () => { + it("reports events, but a staged update stays visible without suppressing future checks", async () => { + const f = fixture(); const win = f.window(); f.run("setupAutoUpdater()"); + f.updater.emit("checking-for-update"); assert.equal(f.state().status, "checking"); + f.updater.emit("update-available", { version: "0.3.8" }); + assert.equal(f.state().status, "downloading"); assert.equal(f.state().version, "0.3.8"); + f.updater.emit("update-downloaded", { version: "0.3.8" }); + const ready = f.state(); + await f.check(); + f.updater.emit("error", new Error("failure with sensitive diagnostic details")); + assert.equal(f.updater.checks, 1); + assert.deepEqual(f.state(), ready); + f.updater.emit("update-downloaded", { version: "0.3.9" }); + assert.equal(f.state().version, "0.3.9"); + assert.ok(f.state().revision > ready.revision); + assert.equal(win.webContents.messages.at(-1)!.channel, "lh:update-status"); + }); + + for (const status of ["checking", "downloading", "error", "disabled", "downloaded"]) { + it(`a ${status} display state does not block a future automatic check`, async () => { + const f = fixture(); f.run(`setupAutoUpdater(); reportUpdateStatus("${status}")`); await f.check(); + assert.equal(f.updater.checks, 1); + }); + } + + it("check failures settle to a generic error and later checks still run", async () => { + const f = fixture(); f.run("setupAutoUpdater()"); + f.updater.failure = new Error("private endpoint/diagnostic detail"); await f.check(); + assert.equal(f.state().status, "error"); + assert.ok(!JSON.stringify(f.state()).includes("private")); + f.updater.failure = null; await f.check(); + assert.equal(f.updater.checks, 2); assert.equal(f.state().status, "up-to-date"); + }); + + it("a declined check makes no up-to-date claim", async () => { + const f = fixture(); f.updater.result = null; f.run("setupAutoUpdater()"); await f.check(true); + assert.equal(f.state().status, "idle"); + }); + + it("window enumeration/send failures do not break checks or the next window's notification", async () => { + const f = fixture(); const bad = f.window(); bad.webContents.fails = true; const good = f.window(); + f.run("setupAutoUpdater()"); await f.check(); + assert.equal(good.webContents.messages.at(-1)!.channel, "lh:update-status"); + f.setEnumerationFailure(true); await f.check(); + f.setEnumerationFailure(false); await f.check(); + assert.equal(f.updater.checks, 3); + }); + + it("OFF does not cancel in-flight events or alter staging/install-on-quit; Restart kills server first", async () => { + const f = fixture(); f.run("setupAutoUpdater()"); + f.updater.emit("update-available", { version: "0.3.8" }); + f.setSettings({ general: { autoUpdate: false } }); + f.updater.emit("update-downloaded", { version: "0.3.8" }); await f.check(); + assert.equal(f.state().status, "downloaded"); assert.equal(f.updater.checks, 0); + assert.equal(f.updater.autoDownload, true); assert.equal(f.updater.autoInstallOnAppQuit, true); + f.run("serverProcess = fixtureChild; installUpdateNow()"); + assert.deepEqual(f.order, ["SIGTERM", "install"]); + }); +}); + +describe("startup, timer, trusted IPC and menu wiring", () => { + it("starts and retains the 24h timer without any Settings subscription, checking current preference each time", async () => { + const f = fixture(); f.setSettings({ general: { autoUpdate: false } }); + await f.run("main()"); + assert.equal(f.updater.checks, 0); + assert.equal(f.intervals.length, 1); assert.equal(f.intervals[0]!.delay, 24 * 60 * 60 * 1000); + f.setSettings({ general: { autoUpdate: true } }); f.intervals[0]!.callback(); await Promise.resolve(); + assert.equal(f.updater.checks, 1); + f.windows.length = 0; f.intervals[0]!.callback(); await Promise.resolve(); + assert.equal(f.updater.checks, 2, "checks also work with every window closed"); + }); + + it("ON/default boot checks immediately and status never disables the scheduled check", async () => { + const f = fixture(); f.setSettings({ version: 1 }); await f.run("main()"); + assert.equal(f.updater.checks, 1); + f.run('reportUpdateStatus("downloading", "0.3.8")'); f.intervals[0]!.callback(); await Promise.resolve(); + assert.equal(f.updater.checks, 2); + }); + + it("snapshot/manual IPC accepts only an owned main frame and snapshot reads never check", async () => { + const f = fixture(); f.setSettings({ general: { autoUpdate: false } }); await f.run("main()"); + const sender = f.windows[0]!.webContents; + const event = { sender, senderFrame: sender.mainFrame }; + const get = f.handles.get("lh:update:get")!; const check = f.handles.get("lh:update:check")!; + assert.equal((get(event) as { status: string }).status, "idle"); assert.equal(f.updater.checks, 0); + check(event); assert.equal(f.updater.checks, 1); + for (const bad of [ + { sender: {}, senderFrame: sender.mainFrame }, + { sender, senderFrame: { url: sender.mainFrame.url } }, + ]) { + assert.throws(() => get(bad), /Untrusted/); assert.throws(() => check(bad), /Untrusted/); + } + sender.mainFrame.url = "https://untrusted.invalid/"; + assert.throws(() => check(event), /Untrusted/); + assert.equal(f.updater.checks, 1); + }); + + it("menu restores/focuses a minimized window, opens General, and manually checks while OFF", async () => { + const f = fixture(); f.setSettings({ general: { autoUpdate: false } }); await f.run("main()"); + const win = f.windows[0]!; win.webContents.loading = false; win.minimized = true; + f.menuCheck(); + assert.equal(win.minimized, false); assert.equal(win.focused, true); + assert.ok(win.webContents.messages.some(message => message.channel === "lh:open-settings" && message.value === "general")); + assert.equal(f.updater.checks, 1); + }); + + it("menu reopens a window and delivers General after load; loading existing windows are covered too", async () => { + const f = fixture(); f.setSettings({ general: { autoUpdate: false } }); await f.run("main()"); + f.windows.length = 0; f.menuCheck(); + assert.equal(f.windows.length, 1); assert.equal(f.updater.checks, 1); + const win = f.windows[0]!; + assert.ok(!win.webContents.messages.some(message => message.channel === "lh:open-settings")); + win.webContents.emit("did-finish-load"); + assert.ok(win.webContents.messages.some(message => message.channel === "lh:open-settings")); + f.menuCheck(); win.webContents.emit("did-finish-load"); + assert.equal(win.webContents.messages.filter(message => message.channel === "lh:open-settings").length, 2); + }); + + it("menu navigation failure does not prevent its manual check", async () => { + const f = fixture(); f.setSettings({ general: { autoUpdate: false } }); await f.run("main()"); + f.windows[0]!.webContents.fails = true; f.windows[0]!.webContents.loading = false; + f.menuCheck(); assert.equal(f.updater.checks, 1); + }); +}); diff --git a/tests/desktop-platform.test.ts b/tests/desktop-platform.test.ts index c72e633..040a47a 100644 --- a/tests/desktop-platform.test.ts +++ b/tests/desktop-platform.test.ts @@ -72,18 +72,52 @@ describe("desktop window chrome", () => { bridge = api; }, }, - ipcRenderer: {}, + ipcRenderer: { on() {} }, }), }; runInNewContext(readFileSync(new URL("../electron/preload.cjs", import.meta.url), "utf8"), context); assert.equal(bridge.platform, "win32"); assert.deepEqual(Object.keys(bridge).sort(), [ - "chatgpt", "expandWindow", "getZoom", "installUpdate", "onUpdateStatus", "onZoom", "platform", "subscriptions", + "chatgpt", "checkForUpdates", "expandWindow", "getUpdateState", "getZoom", "installUpdate", "onOpenSettings", "onUpdateStatus", "onZoom", "platform", "subscriptions", ]); assert.deepEqual(Object.keys(bridge.subscriptions as object).sort(), ["cancel", "consent", "disconnect", "login", "onChange", "open", "respond", "retry", "status"]); }); }); +describe("update preload bridge", () => { + it("buffers the menu request, unsubscribes safely, and uses the matching snapshot/check channels", async () => { + let bridge!: { + onOpenSettings: (cb: (section: string) => void) => () => void; + getUpdateState: () => Promise; + checkForUpdates: () => Promise; + }; + const handlers = new Map void>(); + const invoked: string[] = []; + runInNewContext(readFileSync(new URL("../electron/preload.cjs", import.meta.url), "utf8"), { + process: { platform: "darwin" }, + require: () => ({ + contextBridge: { exposeInMainWorld: (_: string, value: typeof bridge) => { bridge = value; } }, + ipcRenderer: { + on: (channel: string, listener: (event: unknown, value: string) => void) => handlers.set(channel, listener), + invoke: async (channel: string) => { invoked.push(channel); }, + }, + }), + }); + const send = () => handlers.get("lh:open-settings")!({}, "general"); + const seen: string[] = []; + send(); const off = bridge.onOpenSettings(section => seen.push(section)); + assert.deepEqual(seen, ["general"], "pre-subscription request is delivered once"); + send(); assert.deepEqual(seen, ["general", "general"]); + off(); send(); assert.equal(seen.length, 2); + const next: string[] = []; const offNext = bridge.onOpenSettings(section => next.push(section)); + assert.deepEqual(next, ["general"]); + off(); send(); assert.equal(next.length, 2, "old cleanup cannot remove a newer subscriber"); + offNext(); + await bridge.getUpdateState(); await bridge.checkForUpdates(); + assert.deepEqual(invoked, ["lh:update:get", "lh:update:check"]); + }); +}); + describe("desktop packaging policy", () => { const yaml = require("js-yaml") as { load(source: string): Record }; const config = yaml.load(readFileSync(new URL("../electron-builder.yml", import.meta.url), "utf8")); diff --git a/tests/render-update-banner.test.tsx b/tests/render-update-banner.test.tsx index 0a9c307..a7207df 100644 --- a/tests/render-update-banner.test.tsx +++ b/tests/render-update-banner.test.tsx @@ -1,7 +1,7 @@ /** * Update-banner render tests (Wave C, 2026-10-10): the banner is a pure - * function of the last lh:update-status push — hidden without one (the - * browser/dev world: no window.lh at all), visible with the downloaded + * function of the shell's observational status — hidden without a bridge + * or snapshot/event, visible with the downloaded * status, Restart wired to the bridge. The subscribe effect only runs * client-side, so the SSR pass pins the hidden state; the pushed state is * exercised by mounting with the bridge stubbed (classic-JSX shim as in @@ -17,6 +17,7 @@ import * as ReactDOMClient from "react-dom/client"; import { flushSync } from "react-dom"; import { parseHTML } from "linkedom"; import UpdateBanner from "../components/update-banner.tsx"; +import type { UpdateState } from "../components/settings/update-settings.tsx"; // linkedom as the DOM for the interactive pass (first createRoot test in // the repo — the banner's push→render→click flow needs real effects). @@ -36,9 +37,9 @@ describe("update banner (Wave C)", () => { }); it("renders nothing when the bridge exists but no status has arrived", () => { - const listeners: Array<(s: { status: string; version: string | null }) => void> = []; + const listeners: Array<(s: UpdateState) => void> = []; (window as { lh?: unknown }).lh = { - onUpdateStatus: (cb: (s: { status: string; version: string | null }) => void) => { + onUpdateStatus: (cb: (s: UpdateState) => void) => { listeners.push(cb); return () => {}; }, @@ -50,10 +51,10 @@ describe("update banner (Wave C)", () => { }); it("shows the version + Restart once the downloaded push arrives, and Restart installs", async () => { - let pushed: ((s: { status: string; version: string | null }) => void) | null = null; + let pushed: ((s: UpdateState) => void) | null = null; let installed = 0; (window as { lh?: unknown }).lh = { - onUpdateStatus: (cb: (s: { status: string; version: string | null }) => void) => { + onUpdateStatus: (cb: (s: UpdateState) => void) => { pushed = cb; return () => {}; }, @@ -68,7 +69,7 @@ describe("update banner (Wave C)", () => { assert.equal(host.innerHTML, "", "hidden before the push"); await new Promise((r) => setTimeout(r, 0)); - flushSync(() => pushed?.({ status: "downloaded", version: "0.3.1" })); + flushSync(() => pushed?.({ status: "downloaded", version: "0.3.1", revision: 1 })); assert.ok(host.innerHTML.includes("0.3.1"), "the version renders"); assert.ok(host.innerHTML.includes("Restart"), "the restart affordance renders"); diff --git a/tests/render-update-settings.test.tsx b/tests/render-update-settings.test.tsx new file mode 100644 index 0000000..7c01467 --- /dev/null +++ b/tests/render-update-settings.test.tsx @@ -0,0 +1,223 @@ +/** Update UI with a fake preload bridge: no Electron, network or updates. */ +import React, { act } from "react"; +(globalThis as Record).React = React; +(globalThis as Record).IS_REACT_ACT_ENVIRONMENT = true; + +import assert from "node:assert/strict"; +import { describe, it, afterEach } from "node:test"; +import { readFileSync } from "node:fs"; +import { renderToStaticMarkup } from "react-dom/server"; +import { createRoot, type Root } from "react-dom/client"; +import { parseHTML } from "linkedom"; +import { UpdateSettings, updateStatusText, MANUAL_UPDATE_HINT, AUTO_UPDATE_HINT, type UpdateState } from "../components/settings/update-settings.tsx"; +import UpdateBanner from "../components/update-banner.tsx"; +import { SettingsDialogBody, SettingsDialogProvider } from "../components/settings/settings-dialog.tsx"; +import nextConfig from "../next.config.ts"; + +const dom = parseHTML(""); +Object.assign(globalThis, { window: dom.window, document: dom.document, CustomEvent: dom.window.CustomEvent }); +const originalFetch = globalThis.fetch; +const roots: { root: Root; host: HTMLElement }[] = []; +const state = (over: Partial = {}): UpdateState => ({ status: "idle", version: null, revision: 0, ...over }); +const props = { autoUpdate: true, onAutoUpdateChange: () => {}, version: "0.3.7", homepage: "https://github.com/amosblomqvist/learnharness" }; +const q = (host: ParentNode, id: string) => host.querySelector(`[data-testid='${id}']`); + +function deferred() { + let resolve!: (value: T) => void; + let reject!: (error: Error) => void; + const promise = new Promise((yes, no) => { resolve = yes; reject = no; }); + return { promise, resolve, reject }; +} + +function bridge(initial = state(), snapshot = Promise.resolve(initial)) { + const listeners = new Set<(value: UpdateState) => void>(); + const calls = { check: 0, install: 0, get: 0 }; + let onOpen: ((section: string) => void) | null = null; + let checkFailure: Error | null = null; + window.lh = { + expandWindow() {}, + getUpdateState: () => { calls.get++; return snapshot; }, + onUpdateStatus: cb => { listeners.add(cb); return () => { listeners.delete(cb); }; }, + checkForUpdates: async () => { calls.check++; if (checkFailure) throw checkFailure; }, + installUpdate: () => { calls.install++; }, + onOpenSettings: cb => { onOpen = cb; return () => { onOpen = null; }; }, + }; + return { + calls, listeners, + failCheck: () => { checkFailure = new Error("sensitive raw diagnostics"); }, + push: async (value: UpdateState) => { await act(async () => { for (const listener of listeners) listener(value); }); }, + open: async () => { await act(async () => onOpen?.("general")); }, + }; +} + +async function mount(element: React.ReactElement) { + const host = document.createElement("div"); document.body.append(host); + const root = createRoot(host); roots.push({ root, host }); + await act(async () => { root.render(element); }); + return host; +} + +async function click(element: HTMLElement | null) { + assert.ok(element, "missing click target"); + await act(async () => { element.click(); }); +} + +afterEach(async () => { + for (const { root, host } of roots.splice(0)) { + await act(async () => { root.unmount(); }); host.remove(); + } + delete window.lh; + globalThis.fetch = originalFetch; +}); + +describe("version and contextual copy", () => { + it("build metadata comes from package.json and exposes only public version/homepage values", () => { + const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); + assert.deepEqual(nextConfig.env, { LH_VERSION: pkg.version, LH_HOMEPAGE: pkg.homepage }); + }); + + it("never claims live/latest information and keeps updater errors generic", () => { + assert.equal(updateStatusText(null), null); + assert.equal(updateStatusText(state()), null); + assert.equal(updateStatusText(state({ status: "disabled" })), null); + assert.equal(updateStatusText(state({ status: "checking" })), "Checking for updates…"); + assert.equal(updateStatusText(state({ status: "downloading", version: "0.3.8" })), "Downloading version 0.3.8…"); + assert.equal(updateStatusText(state({ status: "up-to-date" })), "No newer version was found at the last check."); + assert.equal(updateStatusText(state({ status: "downloaded", version: "0.3.8" })), "Version 0.3.8 is ready. It will install when you quit the app."); + assert.equal(updateStatusText(state({ status: "error" })), "Couldn’t check for updates. Try again later."); + assert.equal(updateStatusText(state({ status: "downloading" })), "Downloading an update…"); + assert.equal(updateStatusText(state({ status: "downloaded" })), "An update is ready. It will install when you quit the app."); + }); +}); + +describe("General update rows", () => { + it("browser/SSR shows version and installed-version release notes, without inactive controls or claims", () => { + const html = renderToStaticMarkup(); + assert.ok(html.includes("Version 0.3.7")); + assert.ok(html.includes(`${props.homepage}/releases/tag/v0.3.7`)); + assert.ok(!html.includes("update-check")); assert.ok(!html.includes("row-auto-update")); + assert.ok(!html.includes(MANUAL_UPDATE_HINT)); assert.ok(!html.includes("update-status")); + }); + + it("unpackaged/unavailable updater shows the version-only surface", async () => { + const b = bridge(state({ status: "disabled" })); const host = await mount(); + assert.ok(q(host, "row-version")); assert.equal(q(host, "update-check"), null); + assert.equal(q(host, "row-auto-update"), null); assert.equal(b.calls.check, 0); + }); + + it("idle shows two rows and clear consequences; opening Settings never checks", async () => { + const b = bridge(); const seen: boolean[] = []; + const host = await mount( seen.push(value)} />); + assert.deepEqual([...host.children].map(el => el.getAttribute("data-testid")), ["row-version", "row-auto-update"]); + assert.ok(q(host, "row-version")!.contains(q(host, "update-check"))); + assert.ok(host.textContent!.includes(MANUAL_UPDATE_HINT)); assert.ok(host.textContent!.includes(AUTO_UPDATE_HINT)); + assert.equal(q(host, "update-status"), null); assert.equal(b.calls.check, 0); + await click(q(host, "toggle-auto-update")); assert.deepEqual(seen, [false]); + await click(q(host, "update-check")); assert.equal(b.calls.check, 1); + }); + + it("manual check remains available while automatic checks are OFF", async () => { + const b = bridge(); const host = await mount(); + assert.equal(q(host, "toggle-auto-update")!.getAttribute("aria-checked"), "false"); + await click(q(host, "update-check")); assert.equal(b.calls.check, 1); + }); + + it("checking/downloading shows context and disables only the UI button; ready offers Restart", async () => { + const b = bridge(); const host = await mount(); + await b.push(state({ status: "checking", revision: 1 })); + assert.equal((q(host, "update-check") as HTMLButtonElement).disabled, true); + assert.equal(q(host, "update-check")!.getAttribute("aria-busy"), "true"); + assert.equal(q(host, "update-status")!.textContent, "Checking for updates…"); + await b.push(state({ status: "downloading", version: "0.3.8", revision: 2 })); + assert.equal(q(host, "update-status")!.textContent, "Downloading version 0.3.8…"); + assert.equal((q(host, "update-check") as HTMLButtonElement).disabled, true); + await b.push(state({ status: "downloaded", version: "0.3.8", revision: 3 })); + assert.equal(q(host, "update-check"), null); assert.ok(q(host, "update-status")!.textContent!.includes("quit the app")); + await click(q(host, "update-restart")); assert.equal(b.calls.install, 1); + }); + + it("up-to-date and failure statuses allow another manual check", async () => { + const b = bridge(); const host = await mount(); + await b.push(state({ status: "up-to-date", revision: 1 })); + assert.equal(q(host, "update-status")!.textContent, "No newer version was found at the last check."); + await b.push(state({ status: "error", revision: 2 })); + assert.equal((q(host, "update-check") as HTMLButtonElement).disabled, false); + assert.equal(q(host, "update-status")!.textContent, "Couldn’t check for updates. Try again later."); + await click(q(host, "update-check")); assert.equal(b.calls.check, 1); + }); + + it("bridge command rejection is handled without exposing raw diagnostics", async () => { + const b = bridge(); b.failCheck(); const host = await mount(); + await click(q(host, "update-check")); + assert.equal(q(host, "update-status")!.textContent, "Couldn’t check for updates. Try again later."); + assert.ok(!host.textContent!.includes("sensitive")); + }); + + it("late Settings and banner subscribers recover the staged update without an event", async () => { + const b = bridge(state({ status: "downloaded", version: "0.3.8", revision: 5 })); + const host = await mount(<>); + assert.ok(q(host, "update-restart")); assert.ok(q(host, "update-banner")); + assert.ok(q(host, "update-banner")!.textContent!.includes("0.3.8")); + await click(q(host, "update-banner")!.querySelector("button")); assert.equal(b.calls.install, 1); + assert.equal(b.calls.check, 0); + }); + + it("a delayed older snapshot cannot overwrite a newer push in Settings or the banner", async () => { + const snapshot = deferred(); const b = bridge(state(), snapshot.promise); + const host = await mount(<>); + await b.push(state({ status: "downloaded", version: "0.3.8", revision: 4 })); + await act(async () => { snapshot.resolve(state({ status: "checking", revision: 3 })); }); + assert.ok(q(host, "update-restart")); assert.ok(q(host, "update-banner")); + }); + + it("snapshot rejection cannot erase an already received event and late callbacks are unsubscribed", async () => { + const snapshot = deferred(); const b = bridge(state(), snapshot.promise); + const host = await mount(); + await b.push(state({ status: "downloaded", version: "0.3.8", revision: 1 })); + await act(async () => { snapshot.reject(new Error("IPC disconnected")); }); + assert.ok(q(host, "update-restart")); + const { root } = roots.pop()!; await act(async () => { root.unmount(); }); host.remove(); + assert.equal(b.listeners.size, 0); + }); + + it("snapshot rejection without an event is handled with a generic message", async () => { + const snapshot = deferred(); bridge(state(), snapshot.promise); + const host = await mount(); + await act(async () => { snapshot.reject(new Error("sensitive error")); }); + assert.equal(q(host, "update-status")!.textContent, "Couldn’t check for updates. Try again later."); + assert.ok(!host.textContent!.includes("sensitive")); + }); +}); + +describe("Settings integration", () => { + it("the toggle sends the complete General section and follows stored server truth", async () => { + bridge(); + // A synthetic future sibling exercises the caller's section-preserving + // contract without introducing a new product setting or changing its API. + const record = { version: 1, general: { autoUpdate: true, futureSibling: "keep" } }; + const puts: unknown[] = []; + globalThis.fetch = async (input, init) => { + assert.equal(String(input), init?.method === "PUT" ? "/api/settings/general" : "/api/settings"); + if (init?.method === "PUT") { + const body = JSON.parse(String(init.body)); puts.push(body); + return Response.json({ settings: { ...record, general: body } }); + } + return Response.json({ settings: record }); + }; + const host = await mount( {}} />); + await click(q(host, "toggle-auto-update")); + assert.deepEqual(puts, [{ autoUpdate: false, futureSibling: "keep" }]); + assert.equal(q(host, "toggle-auto-update")!.getAttribute("aria-checked"), "false"); + }); + + it("the shell's navigation request opens General, but opening alone never checks", async () => { + const b = bridge(); + globalThis.fetch = async () => Response.json({ settings: { version: 1 } }); + const host = await mount(app); + assert.equal(q(document, "settings-dialog"), null); + await b.open(); + assert.equal(q(document, "settings-nav-general")!.getAttribute("aria-current"), "true"); + assert.ok(q(document, "row-version")); assert.equal(b.calls.check, 0); + assert.ok(host.textContent!.includes("app")); + }); +}); diff --git a/tests/tooltip-mechanism.test.tsx b/tests/tooltip-mechanism.test.tsx index 653cfe6..b83d834 100644 --- a/tests/tooltip-mechanism.test.tsx +++ b/tests/tooltip-mechanism.test.tsx @@ -17,7 +17,7 @@ import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { Hint, hintText } from "../components/ui/hint.tsx"; import { TOOLTIP_DELAY_MS } from "../components/ui/tooltip.tsx"; -import { AUTO_UPDATE_HINT } from "../components/settings/settings-dialog.tsx"; +import { AUTO_UPDATE_HINT } from "../components/settings/update-settings.tsx"; import { IS_MAC } from "../src/core/keyboard.ts"; describe("hintText — the parenthesized composition", () => { @@ -46,7 +46,7 @@ describe("subtlety round pins (owner 2026-09-25)", () => { describe("spot-pins: the dev-speak cull (owner copy review at merge)", () => { it("the auto-update hint is honest user language — no stream jargon", () => { - assert.equal(AUTO_UPDATE_HINT, "Automatic updates arrive in an upcoming release"); + assert.equal(AUTO_UPDATE_HINT, "Check at startup and every 24 hours. Turning this off does not stop updates already in progress or ready to install."); assert.ok(!AUTO_UPDATE_HINT.includes("wave"), "no stream vocabulary"); }); }); From 7d65fa87544a47228ae46edc324327cf7c09d7bc Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Sun, 25 Oct 2026 19:45:17 +0200 Subject: [PATCH 02/19] fix: recover session submissions and failed turn settlement --- app/api/chats/[id]/route.ts | 4 +- app/api/chats/[id]/stream/route.ts | 7 +- app/chat/[id]/page.tsx | 3 +- components/chat.tsx | 52 +++-- components/question-dock.tsx | 33 ++- components/session-core.ts | 201 +++++++++++++++--- components/session.ts | 14 +- docs/architecture.md | 4 +- src/core/question-core.ts | 15 ++ src/server/chat-handler.ts | 42 +++- src/server/chats.ts | 3 +- src/server/pi/ui-stream.ts | 35 ++- src/server/turn-liveness.ts | 9 +- src/server/turn-runs.ts | 35 ++- src/server/turn-service.ts | 17 +- tests/chat-flow.test.ts | 104 ++++++++- tests/graph.test.ts | 6 +- tests/render-cards.test.tsx | 25 +++ tests/session-core.test.ts | 328 +++++++++++++++++++++++++++++ tests/turn-liveness.test.ts | 11 + tests/turn-recovery.test.ts | 90 ++++++++ 21 files changed, 944 insertions(+), 94 deletions(-) create mode 100644 tests/turn-recovery.test.ts diff --git a/app/api/chats/[id]/route.ts b/app/api/chats/[id]/route.ts index 1b19ce5..33f7e04 100644 --- a/app/api/chats/[id]/route.ts +++ b/app/api/chats/[id]/route.ts @@ -1,4 +1,4 @@ -import { compactionInFlight, deleteChatSession, getChat, isTurnRunning, sessionCoverage, sessionCost, stores, type ChatDeleteMode } from "@/src/server"; +import { compactionInFlight, deleteChatSession, getChat, isTurnRunning, sessionCoverage, sessionCost, stores, turnFailure, type ChatDeleteMode } from "@/src/server"; export async function GET(_req: Request, { params }: { params: Promise<{ id: string }> }) { const { id } = await params; @@ -19,6 +19,8 @@ export async function GET(_req: Request, { params }: { params: Promise<{ id: str messages: chat.messages, pricing: sessionCost(chat), turnInFlight: isTurnRunning(id), + // Also reaches idle views that missed the failing turn's stream. + turnError: turnFailure(id), compacting: compactionInFlight(id), coverage: sessionCoverage(id), }); diff --git a/app/api/chats/[id]/stream/route.ts b/app/api/chats/[id]/stream/route.ts index 6dcc08f..7c56152 100644 --- a/app/api/chats/[id]/stream/route.ts +++ b/app/api/chats/[id]/stream/route.ts @@ -5,8 +5,9 @@ * submitter too attaches here. One SSE stream per chat: a running turn → * subscribe to the live turn object: FULL replay of the event log, then * live chunks until settle ([DONE]). No running turn → the cheap terminal - * ({"type":"turn-settled"} + [DONE]): the record is current, the client - * refetches it. + * ({"type":"turn-settled"} + [DONE]): the client refetches the record. + * A retained settlement-failure notice precedes that terminal when a + * reply could not be saved before the submitter attached. * * 404 mirrors the POST handler's unknown-chat. Framework route is a thin * wrapper over the server barrel (the import law); tests drive the seams @@ -18,6 +19,6 @@ export async function GET(_req: Request, ctx: { params: Promise<{ id: string }> const { id } = await ctx.params; if (!getChat(id)) return Response.json({ error: "Unknown chat" }, { status: 404 }); const handle = runningTurn(id); - if (!handle) return settledTurnResponse(); + if (!handle) return settledTurnResponse(id); return piTurnResponse(handle); } diff --git a/app/chat/[id]/page.tsx b/app/chat/[id]/page.tsx index 9f35d80..83413b3 100644 --- a/app/chat/[id]/page.tsx +++ b/app/chat/[id]/page.tsx @@ -1,7 +1,7 @@ import { notFound } from "next/navigation"; import { Chat } from "@/components/chat"; import { slotsConfigured } from "@/src/core"; -import { compactionInFlight, getChat, isTurnRunning, readSettingsRecord } from "@/src/server"; +import { compactionInFlight, getChat, isTurnRunning, readSettingsRecord, turnFailure } from "@/src/server"; export const dynamic = "force-dynamic"; @@ -29,6 +29,7 @@ export default async function ChatPage({ // derived app-side (core/view.ts projectView; GET serves the same // native messages). initialMessages={chat.messages} + initialError={turnFailure(chat.id)} initialConfigured={slotsConfigured(record?.models)} initialPrompt={prompt} // A teacher turn that outlived its client: the remounted view must diff --git a/components/chat.tsx b/components/chat.tsx index 4986f3f..f8866f1 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -38,7 +38,6 @@ import { messageAnchorId, textPartMessageId, kbdEventForAction as kbdEventForChat, - needsContinuation, type ChatInput, type ChatSettings, type LhMessage, @@ -57,7 +56,7 @@ import { PracticeDock } from "@/components/practice-dock"; import { ThinkingCard, type PracticeThinkingData } from "@/components/practice-cards"; import { GraphContextCard, type GraphContextData } from "@/components/graph-context-card"; import { TopBar } from "@/components/top-bar"; -import type { ArtifactPartProps, JobRecord, Phase3, QuestionAnswerData, TurnUsage, SessionPricing } from "@/src/core"; +import type { ArtifactPartProps, JobRecord, Phase3, QuestionAnswerData, QuestionFormDraft, TurnUsage, SessionPricing } from "@/src/core"; import type { LhViewMessage } from "@/src/core"; import { PhaseChip, PhaseDivider } from "@/components/phase-divider"; import { @@ -198,6 +197,7 @@ export function Chat({ chatId, title, initialMessages, + initialError, initialPrompt, initiallyInFlight, initiallyCompacting = false, @@ -207,6 +207,8 @@ export function Chat({ chatId: string; title: string; initialMessages: LhMessage[]; + /** Memory-only settlement notice supplied on reopening. */ + initialError?: string; /** The session kind (practice sessions, owner 2026-10-04): practice * renders no phase chip (no probe→teach arc applies) and a Practice * tag rides the header instead. */ @@ -236,8 +238,8 @@ export function Chat({ // (core/view.ts projectView); sends are ONE explicit input event each // ({chatId, input} — the stash indirection died with the SDK), and the // turn stream is the VERBATIM AgentEvent passthrough the server emits. - const { messages, nativeMessages, contextTokens, send, steer, steerQueue, submitAnswer, dismissQuestion, dismissProblem, setMessages, adoptRecord, status, error, stop, clearError, resumeStream, setReattachSettled } = - useChatSession({ chatId, initialMessages, initiallyInFlight }); + const { messages, nativeMessages, contextTokens, send, continueIfOwed, steer, steerQueue, submitAnswer, dismissQuestion, dismissProblem, questionDraft, saveQuestionDraft, setMessages, adoptRecord, status, error, stop, clearError, resumeStream, setReattachSettled } = + useChatSession({ chatId, initialMessages, initialError, initiallyInFlight }); const sessionFindScopeRef = useRef(null); const scrollContextRef = useRef(null); @@ -434,9 +436,7 @@ export function Chat({ useEffect(() => { if (mountContinuation.current) return; mountContinuation.current = true; - if (!turnInFlight && status === "ready" && needsContinuation(initialMessages)) { - void send({ type: "continue" }); - } + if (!turnInFlight) continueIfOwed(initialMessages); // mount-only by design (turnInFlight read at initial value) }, []); @@ -532,10 +532,12 @@ export function Chat({ }, []); const pendingQuestionId = pending?.toolCallId ?? null; useEffect(() => { - // A new pending question never inherits the previous one's draft. - readingThinkingRef.current = ""; - setReadingThinking(""); - }, [pendingQuestionId]); + // Different calls never share a draft; a failed submission of THIS + // call restores the reading channel alongside the form's snapshot. + const reading = pendingQuestionId ? questionDraft(pendingQuestionId)?.readingThinking ?? "" : ""; + readingThinkingRef.current = reading; + setReadingThinking(reading); + }, [pendingQuestionId, questionDraft]); // The phase at every message (one linear fold): the transcript draws a // color-coded dotted divider at each boundary; the header chip reads the @@ -717,20 +719,20 @@ export function Chat({ const syncFromServer = useCallback(async () => { const res = await fetch(`/api/chats/${chatId}`); if (!res.ok) return; - const record = (await res.json()) as { messages?: LhMessage[]; turns?: TurnUsage[]; pricing?: SessionPricing }; + const record = (await res.json()) as { messages?: LhMessage[]; turns?: TurnUsage[]; pricing?: SessionPricing; turnError?: string }; if (Array.isArray(record.turns)) setTurns(record.turns); setPricing(record.pricing); if (!Array.isArray(record.messages)) return; - setMessages(record.messages); + setMessages(record.messages, record.turnError); // The continuation seam (contracts.ts protocol §4): a job result // landing in the record leaves the agent owing a continuation — the // record ENDS on un-consumed results, the dock owns no question, and // nothing runs. The native fold (needsContinuation) is the old SDK // lastAssistantMessageIsCompleteWithToolCalls check. - if (status === "ready" && needsContinuation(record.messages)) { - void send({ type: "continue" }); - } - }, [chatId, setMessages, send, status]); + // Current model truth also guards retained/live failures: canonical + // sync must never automatically replace a reply that failed to save. + continueIfOwed(record.messages); + }, [chatId, setMessages, continueIfOwed]); // The settled terminal's other half (stream-reattach wave): the // transport resolved turn-settled — no turn running, the record is @@ -905,8 +907,8 @@ export function Chat({ // (endTurn emits from onFinish before the response terminates, so the // SSE frame reliably beats [DONE]) — the handler below cannot sync // there, it flags instead, and THIS flip consumes the flag: the record - // is guaranteed durable (endTurn fires only after persistTurn, the S2 - // ordering law), so replacing the view onto it absorbs the tail's + // reflects durable truth (a failed settlement retains its notice, + // not its unsaved reply), so replacing the view absorbs the tail's // streamed c_-id copies into base under their server ids. Without // this the tail kept those copies forever and the next send's // steer-adoption window rendered them a second time. The wake handoff @@ -1596,6 +1598,8 @@ export function Chat({ fold) and NEVER overlaps the composer. */} { if (pending) saveQuestionDraft(pending.toolCallId, { form }); }} practice={activeProblem} compactionRunning={compacting} onSubmitThinking={submitThinking} @@ -1663,8 +1667,10 @@ export function Chat({ // ride the same callback (QuestionAnswerData.dismissed). The // reading-thinking draft rides along (owner 2026-10-03) — the // check answer is its only carrier. + saveQuestionDraft(pending.toolCallId, { readingThinking: readingThinkingRef.current }); const reading = readingThinkingRef.current.trim() || undefined; - clearReadingThinking(); + // The pending-id effect clears this only when the question + // actually leaves. An offline preflight keeps the draft visible. submitAnswer(pending.tool, pending.toolCallId, reading ? { ...output, readingThinking: reading } : output); }} /> @@ -1821,6 +1827,8 @@ export function shouldStampGeometryMute(nextH: number, prevH: number, pending: b function DockOverlay({ pending, + initialQuestionDraft, + onSaveQuestionDraft, practice, compactionRunning, onSubmitThinking, @@ -1843,6 +1851,8 @@ function DockOverlay({ queuedSteers, }: { pending: ReturnType; + initialQuestionDraft?: QuestionFormDraft; + onSaveQuestionDraft: (draft: QuestionFormDraft) => void; /** Compaction running for this chat (round 14) — the docks' submit * affordance gate (the server's registry is the real one). */ compactionRunning: boolean; @@ -2309,6 +2319,8 @@ function DockOverlay({ phase={phase} onExpand={expandAndReveal} onSubmit={onSubmitAnswer} + initialDraft={initialQuestionDraft} + onSaveDraft={onSaveQuestionDraft} onEscape={dictationCancel} dictationClaim={!revealPending} compactionRunning={compactionRunning} diff --git a/components/question-dock.tsx b/components/question-dock.tsx index 3b6f848..b5cd734 100644 --- a/components/question-dock.tsx +++ b/components/question-dock.tsx @@ -28,6 +28,7 @@ import { type DockSelection, type ExplainToolInput, type QuestionAnswerData, + type QuestionFormDraft, type QuestionData, type QuestionMode, type QuizToolInput, @@ -71,6 +72,9 @@ export interface QuestionDockProps { reveal?: boolean; onExpand: () => void; onSubmit: (output: QuestionAnswerData) => void; + /** Restore only this call's unconfirmed submission after recovery. */ + initialDraft?: QuestionFormDraft; + onSaveDraft?: (draft: QuestionFormDraft) => void; /** Escape in a question field stops dictation (STT wave) — threaded * from the page that owns the dictation hook. */ onEscape?: () => void; @@ -100,6 +104,8 @@ export function QuestionDock({ reveal = false, onExpand, onSubmit, + initialDraft, + onSaveDraft, onEscape, phase = "probe", dictationClaim = true, @@ -155,6 +161,8 @@ export function QuestionDock({ phase={phase} active={expanded} onSubmit={onSubmit} + initialDraft={initialDraft} + onSaveDraft={onSaveDraft} onEscape={onEscape} dictationClaim={dictationClaim} compactionRunning={compactionRunning} @@ -250,6 +258,8 @@ function QuestionDockForm({ phase = "probe", active, onSubmit, + initialDraft, + onSaveDraft, onEscape, dictationClaim = true, compactionRunning = false, @@ -311,22 +321,22 @@ function QuestionDockForm({ // single-select displaces, multi-select coexists — picking the ask // "Other…" row in a multi-select must never clear already-picked // options, and picking an option must never clear a typed custom answer. - const [sel, setSel] = useState(emptyDockSelection()); + const [sel, setSel] = useState(() => initialDraft?.selection ?? emptyDockSelection()); // The learner's transfer channel — thinking, never "a note". - const [thinking, setThinking] = useState(""); - const [other, setOther] = useState(""); - const [text, setText] = useState(""); + const [thinking, setThinking] = useState(initialDraft?.thinking ?? ""); + const [other, setOther] = useState(initialDraft?.other ?? ""); + const [text, setText] = useState(initialDraft?.text ?? ""); /* Refs MIRROR the two dictation-target fields ("nothing gets lost" * round, owner 2026-10-01): the run's flush lands via setState, which * a closure waiting through `await finishDictation()` cannot observe * in time — submit reads these, kept current at every write site. */ - const thinkingTextRef = useRef(""); - const answerTextRef = useRef(""); + const thinkingTextRef = useRef(initialDraft?.thinking ?? ""); + const answerTextRef = useRef(initialDraft?.text ?? ""); // Confidence (owner, 2026-10-01): an index into CONFIDENCE_STOPS, // resting at 50 — a real coin flip by definition, untouched or // deliberate (the same number, the same meaning). The dock unmounts // between questions, so each question starts fresh at 50. - const [confIdx, setConfIdx] = useState(DEFAULT_CONFIDENCE_INDEX); + const [confIdx, setConfIdx] = useState(initialDraft?.confidenceIndex ?? DEFAULT_CONFIDENCE_INDEX); const selectMode: SelectMode = qd.mode === "multi-select" ? "multi-select" : "single-select"; // The PRIMARY dictation input (owner, STT round 1): while a question @@ -390,6 +400,15 @@ function QuestionDockForm({ // "untouched"); buildAnswer drops it on "I don't know". confidence: kind === "quiz" ? CONFIDENCE_STOPS[confIdx] : undefined, }; + // Snapshot before optimistic submission unmounts the form. Ref values + // include the dictation finals that just landed, unlike stale closures. + onSaveDraft?.({ + selection: { ...sel, selected: new Set(sel.selected) }, + thinking: thinkingTextRef.current, + other, + text: answerTextRef.current, + confidenceIndex: confIdx, + }); onSubmit(buildAnswer(qd, req, toolName)); }; diff --git a/components/session-core.ts b/components/session-core.ts index 657d0da..0ac5d3f 100644 --- a/components/session-core.ts +++ b/components/session-core.ts @@ -17,6 +17,8 @@ import type { AgentEvent } from "@earendil-works/pi-agent-core"; import type { ToolResultMessage } from "@earendil-works/pi-ai"; import { needsContinuation, + danglingQuestionOf, + answeredCallIds, measuredContextTokens, newAnswerResult, newNextMessage, @@ -30,6 +32,7 @@ import { type LhViewMessage, type LiveExecution, type QuestionAnswerData, + type QuestionDraft, } from "../src/core"; export type SessionStatus = "ready" | "submitted" | "streaming" | "error"; @@ -78,13 +81,25 @@ async function turnInFlight(model: SessionModel): Promise { } } -/** One wire event off the SSE body: parsed JSON, or the [DONE] terminal. */ -async function* sseEvents(reader: ReadableStreamDefaultReader): AsyncGenerator> { +const STREAM_DROPPED = "The connection dropped before the turn completed."; + +/** Mark transport failures without confusing JSON/application bugs with drops. */ +class StreamReadError extends Error { + constructor(cause: unknown) { + super(cause instanceof Error ? cause.message : String(cause), { cause }); + } +} + +/** Wire events; return true ONLY for the explicit completion marker. */ +async function* sseEvents(reader: ReadableStreamDefaultReader): AsyncGenerator, boolean> { const decoder = new TextDecoder(); let buf = ""; for (;;) { - const { done, value } = await reader.read(); - if (done) return; + let chunk: ReadableStreamReadResult; + try { chunk = await reader.read(); } + catch (err) { throw new StreamReadError(err); } + const { done, value } = chunk; + if (done) return false; buf += decoder.decode(value, { stream: true }); for (;;) { const end = buf.indexOf("\n\n"); @@ -94,7 +109,7 @@ async function* sseEvents(reader: ReadableStreamDefaultReader): Asyn const dataLine = event.split("\n").find((l) => l.startsWith("data: ")); if (!dataLine) continue; const data = dataLine.slice("data: ".length); - if (data === "[DONE]") return; + if (data === "[DONE]") return true; yield JSON.parse(data) as Record; } } @@ -196,6 +211,8 @@ export function visibleQueuedSteers( export interface SessionModelOptions extends SessionModelDeps { chatId: string; initialMessages: LhMessage[]; + /** Retained, memory-only server failure when reopening a session. */ + initialError?: string; /** Fires on every state change (the hook re-renders). */ onChange?: () => void; /** The reattach settled terminal (turn-settled): no turn running, the @@ -220,6 +237,9 @@ export class SessionModel { // at every full sync; the write-ahead absorbs the optimistic entries. private base: LhMessage[]; private tail: LhMessage[] = []; + private questionDrafts = new Map(); + /** Only learner answers submitted through send(), never silent dismissals. */ + private unconfirmedAnswers = new Map(); // The live turn's execution layer (view model mid-turn states): private executions = new Map(); private streamingIds = new Set(); @@ -229,6 +249,8 @@ export class SessionModel { private statusValue: SessionStatus = "ready"; /** Request/loop failures are transient; provider failures derive from messages. */ private errorValue: { message: string } | null = null; + /** Dismissing a banner must not authorize an automatic replacement turn. */ + private automaticContinuationBlocked = false; /** A completed failure from THIS consume, not the previous attempt's record. */ private liveFailureId: string | null = null; // The single live consume (POST response or reattach stream): a newer @@ -240,6 +262,8 @@ export class SessionModel { constructor(opts: SessionModelOptions) { this.chatId = opts.chatId; this.base = opts.initialMessages; + this.errorValue = opts.initialError === undefined ? null : { message: opts.initialError }; + this.automaticContinuationBlocked = opts.initialError !== undefined; // BOUND: the browser's native fetch is a Window method — storing it // unbound makes every call throw "Illegal invocation" in the browser // (Node's undici doesn't care, so the tests never saw it). Always go @@ -300,6 +324,7 @@ export class SessionModel { private setError(message: string | null): void { this.errorValue = message === null ? null : { message }; + if (message !== null) this.automaticContinuationBlocked = true; this.onChange?.(); } @@ -346,6 +371,12 @@ export class SessionModel { } const agent = event as unknown as AgentEvent; switch (agent.type) { + case "agent_start": + // Positive evidence of a new/replayed server-owned turn. Any + // later failure in that replay blocks continuation again. + this.automaticContinuationBlocked = false; + if (this.errorValue !== null) this.setError(null); + return false; case "message_start": case "message_update": case "message_end": { @@ -445,18 +476,28 @@ export class SessionModel { /** Consume one SSE body to its [DONE] terminal. The turn-settled * terminal (stream-route only) fires the settle hook and ends. */ - async consumeStream(res: Response, controller = this.consumeAbort): Promise { + async consumeStream(res: Response, controller = this.consumeAbort, allowRecovery = true): Promise { if (!res.body) throw new Error("The turn stream body is empty."); const reader = res.body.getReader(); let sawEvent = false; + let sawDone = false; try { - for await (const event of sseEvents(reader)) { + const events = sseEvents(reader); + for (;;) { + let next: IteratorResult, boolean>; + try { next = await events.next(); } + catch (err) { + if (err instanceof StreamReadError) break; + throw err; + } if (this.consumeAbort !== controller) return; + if (next.done) { sawDone = next.value; break; } + const event = next.value; if (event.type === "turn-settled") { // The raced-settle terminal: no turn to attach to — the // record is current; the settle hook refetches. - await reader.cancel().catch(() => {}); - if (this.consumeAbort === controller) this.onReattachSettled?.(); + sawDone = true; + this.onReattachSettled?.(); return; } const acted = this.applyWireEvent(event); @@ -466,14 +507,32 @@ export class SessionModel { } } } finally { + await reader.cancel().catch(() => {}); + reader.releaseLock(); // A cancelled consume may finish after its replacement has entered // Thinking. Only the current owner can settle shared state. if (this.consumeAbort === controller) { this.streamingIds = new Set(); this.executions = new Map(); - if (this.statusValue !== "error") this.setStatus("ready"); + if (sawDone && this.statusValue !== "error") this.setStatus("ready"); } } + if (this.consumeAbort !== controller || sawDone) return; + if (!allowRecovery) throw new StreamReadError(new Error(STREAM_DROPPED)); + // One read-only recovery: refresh the canonical base, then replay the + // SAME server-owned turn. Never resend the learner's input. + await this.resumeStream(true, false); + } + + private async fetchView(url: string, signal: AbortSignal, recoveringDrop: boolean): Promise { + try { + const res = await this.fetch(url, { signal }); + if (recoveringDrop && !res.ok) throw new Error((await res.text()) || STREAM_DROPPED); + return res; + } catch (err) { + if (recoveringDrop && !signal.aborted) throw new StreamReadError(err); + throw err; + } } /** @@ -489,7 +548,7 @@ export class SessionModel { * lands on top. The stream then replays the turn from its first event * through the SAME consume path. */ - async resumeStream(refetchRecordFirst = true): Promise { + async resumeStream(refetchRecordFirst = true, allowRecovery = true): Promise { this.consumeAbort?.abort(); const controller = new AbortController(); this.consumeAbort = controller; @@ -519,16 +578,27 @@ export class SessionModel { this.setStatus("submitted"); try { if (refetchRecordFirst) { - const recordRes = await this.fetch(`/api/chats/${encodeURIComponent(this.chatId)}`, { - signal: controller.signal, - }); + const recordRes = await this.fetchView(`/api/chats/${encodeURIComponent(this.chatId)}`, controller.signal, !allowRecovery); if (this.consumeAbort !== controller) return; if (recordRes.ok) { - const record = (await recordRes.json()) as { messages?: LhMessage[] }; + let text: string; + try { text = await recordRes.text(); } + catch (err) { + if (!allowRecovery && !controller.signal.aborted) throw new StreamReadError(err); + throw err; + } + // Parse outside the transport catch: malformed JSON is a + // contract bug, not a connection-drop diagnostic. + const record = JSON.parse(text) as { messages?: LhMessage[]; turnError?: string }; if (this.consumeAbort !== controller) return; if (Array.isArray(record.messages)) { this.mutate(() => { this.base = record.messages!; + if (record.turnError !== undefined) { + this.errorValue = { message: record.turnError }; + this.automaticContinuationBlocked = true; + } + this.forgetResolvedDrafts(record.messages!); this.incompleteUsageIds.clear(); this.tail = []; }); @@ -543,15 +613,19 @@ export class SessionModel { }); } } - const res = await this.fetch(`/api/chats/${encodeURIComponent(this.chatId)}/stream`, { - signal: controller.signal, - }); + const res = await this.fetchView(`/api/chats/${encodeURIComponent(this.chatId)}/stream`, controller.signal, !allowRecovery); if (this.consumeAbort !== controller) return; if (!res.ok) throw new Error((await res.text()) || "Failed to attach to the running turn."); - await this.consumeStream(res, controller); + await this.consumeStream(res, controller, allowRecovery); } catch (err) { - if (this.consumeAbort !== controller || (err as Error).name === "AbortError") return; // a newer consume won - this.setError((err as Error).message); + if (this.consumeAbort !== controller || controller.signal.aborted) return; // intentionally cancelled + // A failed record read can leave the optimistic answer in the tail. + // Restore its Submit affordance; the next attempt reconciles first. + this.mutate(() => { + this.tail = this.tail.filter(m => !this.unconfirmedAnswers.has(m.id)); + this.unconfirmedAnswers.clear(); + }); + this.setError(!allowRecovery && err instanceof StreamReadError ? STREAM_DROPPED : (err as Error).message); this.setStatus("error"); } } @@ -563,12 +637,42 @@ export class SessionModel { * server's 409 + identity retry covers the wake-race window). */ async send(input: ChatInput): Promise { - if (!this.readyRef) return; + const recovering = this.statusValue === "error"; + if (!this.readyRef && !recovering) return; this.consumeAbort?.abort(); const controller = new AbortController(); this.consumeAbort = controller; this.setError(null); this.setStatus("submitted"); + if (recovering && input.type === "answer") { + try { + const res = await this.fetch(`/api/chats/${encodeURIComponent(this.chatId)}`, { signal: controller.signal }); + if (this.consumeAbort !== controller) return; + if (!res.ok) throw new Error((await res.text()) || "An error occurred."); + const record = await res.json() as { messages?: LhMessage[]; turnInFlight?: boolean; turnError?: string }; + if (this.consumeAbort !== controller) return; + // No verified record/liveness: retain the draft and never guess + // whether a lost acknowledgement meant the input was accepted. + if (!Array.isArray(record.messages) || typeof record.turnInFlight !== "boolean") throw new Error("An error occurred."); + this.setMessages(record.messages, record.turnError); + if (record.turnInFlight) { + await this.resumeStream(false); + return; + } + if (input.type === "answer" && (answeredCallIds(record.messages).has(input.toolCallId) || danglingQuestionOf(record.messages)?.toolCallId !== input.toolCallId)) { + // The call already resolved, or no longer owns the dock. Adopt + // truth, never POST that answer a second time. + this.setStatus("ready"); + return; + } + } catch (err) { + if (this.consumeAbort !== controller || controller.signal.aborted) return; + this.setError((err as Error).message); + this.setStatus("error"); + return; + } + } + if (input.type !== "continue") this.automaticContinuationBlocked = false; // The optimistic appends — exactly the messages the server's // write-ahead will persist (inputAppends), with LOCAL ids so // adoption can tell them from the record's absorbed copies. @@ -585,7 +689,9 @@ export class SessionModel { optimistic.push({ ...newNextMessage(), id: localId() }); } else if (input.type === "answer") { const tool = pendingCallTool([...this.base, ...this.tail], input.toolCallId) ?? "quiz"; - optimistic.push({ ...newAnswerResult(input.toolCallId, tool, input.answer), id: localId() }); + const result = { ...newAnswerResult(input.toolCallId, tool, input.answer), id: localId() }; + optimistic.push(result); + this.unconfirmedAnswers.set(result.id, input.toolCallId); } if (optimistic.length > 0) this.mutate(() => { this.tail = [...this.tail, ...optimistic]; @@ -605,9 +711,7 @@ export class SessionModel { // connection is Diet A's whole job. const res = await this.postInputEvent(input); if (res.status === 409) { - this.setError("A turn is already running — try again in a moment."); - this.setStatus("error"); - return; + throw new Error("A turn is already running — try again in a moment."); } if (!res.ok) { // Refusal envelopes (503 BYOK/key gates) and 400s: the RAW body @@ -629,7 +733,17 @@ export class SessionModel { if (this.consumeAbort !== controller) return; await this.resumeStream(true); } catch (err) { - if ((err as Error).name === "AbortError") return; // stop() or a newer consume won + if (this.consumeAbort !== controller || controller.signal.aborted) return; + // Restore the question when acceptance failed or is unknown. Remove + // only THIS optimistic answer, never saved copies/newer live content. + // Other input kinds retain their existing optimistic presentation. + if (input.type === "answer") { + const ids = new Set(optimistic.map(m => m.id)); + this.mutate(() => { + this.tail = this.tail.filter(m => !ids.has(m.id)); + for (const id of ids) this.unconfirmedAnswers.delete(id); + }); + } this.setError((err as Error).message); this.setStatus("error"); } @@ -740,10 +854,32 @@ export class SessionModel { this.setStatus("ready"); } + /** Submission snapshots outlive the dock, not the session mount. */ + questionDraft(toolCallId: string): QuestionDraft | undefined { + return this.questionDrafts.get(toolCallId); + } + + saveQuestionDraft(toolCallId: string, draft: Partial): void { + this.questionDrafts.set(toolCallId, { ...this.questionDrafts.get(toolCallId), ...draft }); + } + + private forgetResolvedDrafts(record: readonly LhMessage[]): void { + const answered = answeredCallIds(record); + for (const id of answered) this.questionDrafts.delete(id); + for (const [id, callId] of this.unconfirmedAnswers) { + if (answered.has(callId)) this.unconfirmedAnswers.delete(id); + } + } + /** Full snapshot swap (the idle settle sync — server canon). */ - setMessages(record: LhMessage[]): void { + setMessages(record: LhMessage[], turnError?: string): void { this.mutate(() => { this.base = record; + if (turnError !== undefined) { + this.errorValue = { message: turnError }; + this.automaticContinuationBlocked = true; + } + this.forgetResolvedDrafts(record); this.incompleteUsageIds.clear(); this.tail = []; this.streamingMessageId = null; // the tail (and its slot) is gone @@ -763,10 +899,19 @@ export class SessionModel { if (baseAbsorbed) return; // not a pure append — the idle full sync is the net this.mutate(() => { this.base = record; + this.forgetResolvedDrafts(record); this.tail = this.tail.filter((m) => !absorbed(m, record)); }); } + /** Automatic mount/sync continuation is never permission to retry a + * failed turn. Read current model truth, not a stale React closure. */ + async continueIfOwed(record: readonly LhMessage[]): Promise { + if (this.statusValue === "ready" && !this.automaticContinuationBlocked && this.getState().error === null && needsContinuation(record)) { + await this.send({ type: "continue" }); + } + } + /** The mount/sync continuation fold (native-messages.ts). */ static needsContinuation = needsContinuation; diff --git a/components/session.ts b/components/session.ts index e0652cf..7c6d8dc 100644 --- a/components/session.ts +++ b/components/session.ts @@ -22,16 +22,18 @@ export interface UseChatSessionOptions { chatId: string; /** The native record seed (server-rendered page or a full sync). */ initialMessages: LhMessage[]; + /** Transient settlement failure retained by the server, not the transcript. */ + initialError?: string; /** Reattach on mount: a turn is running server-side with no live view. */ initiallyInFlight?: boolean; } -export function useChatSession({ chatId, initialMessages, initiallyInFlight }: UseChatSessionOptions) { +export function useChatSession({ chatId, initialMessages, initialError, initiallyInFlight }: UseChatSessionOptions) { // The model is per-mount (chatId-keyed component); onChange forces the // re-render. The ref pattern keeps it stable across renders. const modelRef = useRef(null); if (modelRef.current === null) { - modelRef.current = new SessionModel({ chatId, initialMessages }); + modelRef.current = new SessionModel({ chatId, initialMessages, initialError }); } const model = modelRef.current; // The render VERSION — bumped by every model change, so the state memo @@ -101,6 +103,7 @@ export function useChatSession({ chatId, initialMessages, initiallyInFlight }: U ); const send = useCallback((input: ChatInput) => void model.send(input), [model]); + const continueIfOwed = useCallback((record: readonly LhMessage[]) => void model.continueIfOwed(record), [model]); const steer = useCallback((text: string) => void model.steer(text), [model]); const submitAnswer = useCallback( (tool: string, toolCallId: string, output: QuestionAnswerData) => @@ -115,7 +118,9 @@ export function useChatSession({ chatId, initialMessages, initiallyInFlight }: U (problemCallId: string) => void model.dismissProblem(problemCallId), [model], ); - const setMessages = useCallback((record: LhMessage[]) => model.setMessages(record), [model]); + const questionDraft = useCallback((toolCallId: string) => model.questionDraft(toolCallId), [model]); + const saveQuestionDraft = useCallback((toolCallId: string, draft: Parameters[1]) => model.saveQuestionDraft(toolCallId, draft), [model]); + const setMessages = useCallback((record: LhMessage[], turnError?: string) => model.setMessages(record, turnError), [model]); const adoptRecord = useCallback((record: LhMessage[]) => model.adoptRecord(record), [model]); const stop = useCallback(() => model.stop(), [model]); const clearError = useCallback(() => model.clearError(), [model]); @@ -129,10 +134,13 @@ export function useChatSession({ chatId, initialMessages, initiallyInFlight }: U error: state.error, steerQueue: state.steerQueue, send, + continueIfOwed, steer, submitAnswer, dismissQuestion, dismissProblem, + questionDraft, + saveQuestionDraft, setMessages, adoptRecord, stop, diff --git a/docs/architecture.md b/docs/architecture.md index 1fcce49..ed7d869 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -307,7 +307,9 @@ sequenceDiagram - **The prober→teacher handoff**: the first turn(s) run the prober (floor/ceiling via quiz, goal via ask); `probe_done` (server-executed) files the assessment and queues a handoff steer; from the first `tool-probe_done` part in the transcript, the handler builds the teacher agent for every later turn. Phases (`core/phases.ts`) fold strictly: probe → plan → teach. - **The formal plan tree is author-once**: the teacher authors the session's dependency map with a single schema-validated `plan_init` tool (a mermaid-subset graph, parsed deterministically with line-numbered errors, validated as a DAG). Progress is never mutated — statuses (`current`/`visited`/upcoming) are **derived** from tagged quiz calls in the transcript; the tree panel and the plan blocks render that derivation. - **Job tools are server-executed, ack-and-steer**: `research`/`demo`/`visual` resolve immediately mid-turn with a fire-and-forget ack (the teacher keeps teaching); `spawn(job, input)` fires the background runner. -- Each turn service is a named, error-contained seam taking explicit deps; failures log one `[chat ]` line and never touch the response. Swapping any of them touches nothing else. +- Turn services are named seams taking explicit dependencies. Extraction contains its errors, but **canonical persistence must not fail silently**: a failed input write refuses acceptance without starting an agent; `persistTurn` throws on a failed reply write. The runner reports settlement failure before `[DONE]` and releases liveness even when normal settlement fails. Steer reconciliation uses the durable record, so a later bookkeeping failure cannot replay already-saved learner input. +- **Late attachment must still see settlement failure.** A small, memory-only notice per session precedes the settled stream terminal when the failing turn has already left the live registry. The next accepted turn, successful session deletion, or server exit clears it. Reopening seeds the existing banner from that notice and waits for ordinary learner input; dismissing the banner is not permission to automatically retry the model. No synthetic failure entry is added to the saved transcript. +- **Recovery never blindly resends an answer.** Submitted question drafts survive optimistic unmount in memory; the existing Submit action reconciles uncertain acceptance against the canonical record and running-turn state before sending again. EOF or a rejected stream read without a terminal triggers one read-only reattachment; another transport failure shows the existing error banner rather than claiming completion. Explicit completion, cancellation, and the literal-final-entry saved-error rule remain unchanged. ### 5.1 The wake pipeline (Workbench → Classroom) diff --git a/src/core/question-core.ts b/src/core/question-core.ts index f0b3214..8ba6c87 100644 --- a/src/core/question-core.ts +++ b/src/core/question-core.ts @@ -511,6 +511,21 @@ export interface DockSelection { export const emptyDockSelection = (): DockSelection => ({ selected: new Set(), otherSelected: false, dontKnow: false }); +/** In-memory submission snapshot: survives the question form's optimistic + * unmount, never written to storage. Indices use the call's stable shuffle. */ +export interface QuestionFormDraft { + selection: DockSelection; + thinking: string; + other: string; + text: string; + confidenceIndex: number; +} + +export interface QuestionDraft { + form?: QuestionFormDraft; + readingThinking?: string; +} + /** * Toggling a listed option. Single-select: the option becomes THE answer and * displaces "Other". Multi-select: the option toggles alongside its siblings diff --git a/src/server/chat-handler.ts b/src/server/chat-handler.ts index af92de7..56132e9 100644 --- a/src/server/chat-handler.ts +++ b/src/server/chat-handler.ts @@ -42,8 +42,8 @@ import { import { modelsNotConfigured, providerKeyMissing as providerKeyMissingEnvelope, type ProviderKeyMissing } from "../core/settings.ts"; import { beginTurn, endTurn } from "./turn-liveness.ts"; import { applyParkedDismissal } from "./dismissal-park.ts"; -import { registerTurn, unregisterTurn } from "./turn-runs.ts"; -import { persistTurn, triggerExtraction } from "./turn-service.ts"; +import { registerTurn, rememberTurnFailure, unregisterTurn } from "./turn-runs.ts"; +import { persistTurn, triggerExtraction, TurnPersistenceError } from "./turn-service.ts"; import { hasProbeDone, danglingQuestionOf } from "../core/native-messages.ts"; import { composeInstructions } from "../agent/teacher-agent.ts"; import { ensureCompacted, kickCompaction, effectiveThresholdTokens } from "./compaction.ts"; @@ -79,6 +79,10 @@ import type {CallUsage, SteerMessage} from "../core/contracts.ts"; * for write-ahead persistence, turn liveness, and the turn services. */ +function settlementErrorText(error: unknown): string { + return error instanceof TurnPersistenceError ? error.message : "An error occurred."; +} + const jobRunner = createJobRunner({ chatStore: stores.chatStore, jobStore: stores.jobStore, @@ -289,7 +293,11 @@ export async function handleChatInput( // the composer gates), so adjacency holds by construction. if (input.type === "answer" && danglingQuestionOf([...record.messages, ...appends]) !== null) { const merged = [...record.messages, ...appends]; - stores.chatStore.saveMessages(chatId, merged); + try { stores.chatStore.saveMessages(chatId, merged); } + catch (err) { + console.error(`[chat ${chatId}] write-ahead persist failed:`, err); + return new Response("Your input could not be saved. Please try again.", { status: 500 }); + } emit("record-changed", { chatId }); return settledTurnResponse(); // the cheap terminal — no beginTurn, no turn } @@ -313,14 +321,19 @@ export async function handleChatInput( let persistBaseCount = stagedAutomatic ? record.messages.length : merged.length; try { if (!stagedAutomatic) stores.chatStore.saveMessages(chatId, merged); - // Dock fix (owner ruling 2026-09-26): persisted markers are visible - // in the record — flip their jobs now (the wake's steers ride this - // write-ahead; the delivering turn may run for minutes). + } catch (err) { + console.error(`[chat ${chatId}] write-ahead persist failed:`, err); + endTurn(chatId); + return new Response("Your input could not be saved. Please try again.", { status: 500 }); + } + // Visibility bookkeeping is not the canonical save. Its failure must + // neither misreport a saved input as lost nor prevent the turn running. + try { for (const appended of stagedAutomatic ? [] : appends) { if ((appended as { role?: string }).role === "custom") noteSteerVisible(appended as SteerMessage); } } catch (err) { - console.error(`[chat ${chatId}] write-ahead persist failed:`, err); + console.error(`[chat ${chatId}] input visibility bookkeeping failed:`, err); } // MONEY CHANNEL (wave 4): one collector per request, fed by the agent's @@ -496,6 +509,21 @@ export async function handleChatInput( console.error(`[chat ${chatId}] turn error:`, error); return "An error occurred."; }, + onSettleError: settlementErrorText, + onSettleFailure: (error, reportedErrorText) => { + // POST only acknowledges acceptance; the submitter may attach + // after this turn has already failed and left the registry. + rememberTurnFailure(chatId, reportedErrorText ?? settlementErrorText(error)); + endTurn(chatId); + unregisterTurn(chatId); + unregisterLiveAgent(chatId); + // The canonical save may have succeeded before later bookkeeping + // threw. Reconcile against actual durability, never replay input + // merely because usage/extraction/other settlement work failed. + const persisted = stores.chatStore.getChat(chatId)?.messages ?? []; + reconcileUserSteers(chatId, persisted); + settleSteers(chatId, persisted); + }, onFinish: (turnMessages, isAborted) => { // Wave B: the record write is the agent's own final state — // the new messages are the slice past the merged base. diff --git a/src/server/chats.ts b/src/server/chats.ts index f90d950..b1976ca 100644 --- a/src/server/chats.ts +++ b/src/server/chats.ts @@ -5,6 +5,7 @@ import { titleFromPrompt } from "../core/contracts.ts"; import { graphRevision } from "./graph.ts"; import { stores } from "./stores.ts"; import { isTurnRunning } from "./turn-liveness.ts"; +import { clearTurnFailure } from "./turn-runs.ts"; /** * Server-side chat entries for the transport layer (app/api/chats*, @@ -108,7 +109,7 @@ export async function deleteChatSession(chatId: string, mode: ChatDeleteMode): P // ledgers → jobs → the chat record. const stats = deleteChatTraces(stores.graphStore, chatId, mode); const jobsRemoved = stores.jobStore.deleteJobsForChat(chatId); - stores.chatStore.deleteChat(chatId); + if (stores.chatStore.deleteChat(chatId)) clearTurnFailure(chatId); return { ok: true, mode, stats, jobsRemoved, graphRevision: graphRevision() }; } diff --git a/src/server/pi/ui-stream.ts b/src/server/pi/ui-stream.ts index b75a570..2619ac4 100644 --- a/src/server/pi/ui-stream.ts +++ b/src/server/pi/ui-stream.ts @@ -79,6 +79,10 @@ export interface PiTurnSpec { * event stream itself is verbatim — provider errors ride their * message's stopReason/errorMessage through untouched). */ onError?: (error: unknown) => string; + /** Report settlement failure while the views are still open. */ + onSettleError?: (error: unknown) => string; + /** Release liveness even if the normal finish seam throws. */ + onSettleFailure?: (error: unknown, reportedErrorText?: string) => void; /** The canonical end-of-turn seam: persist + usage + extraction + endTurn. * Receives the agent's FULL final state (native messages) — the caller * persists the slice past its own base count. */ @@ -119,11 +123,12 @@ export interface PiTurnHandle { } export function runPiTurn(spec: PiTurnSpec): PiTurnHandle { - const { agent, onError, onFinish, usedToolCallIds, updateFlushMs = 100, requestCycle } = spec; + const { agent, onError, onFinish, onSettleError, onSettleFailure, usedToolCallIds, updateFlushMs = 100, requestCycle } = spec; const uniquifier = usedToolCallIds ? new ToolCallIdUniquifier(usedToolCallIds) : undefined; let settled = false; let sawAbort = false; + let reportedErrorText: string | undefined; // THE PER-TURN STATE LOG (memory-only — the settle-only disk writes // are unchanged). COMPACTION LAW (owner, 2026-10-09, supersedes the @@ -221,11 +226,22 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle { // messages of its own (the prompt is composed per turn), so dropping // the role wholesale is exact; revisit if prompt-updates ever land. const settleWith = () => { - const settled_ = [...agent.state.messages].filter((m) => (m as { role?: string }).role !== "system"); - onFinish((uniquifier?.remapSettled(settled_) as AgentMessage[]) ?? settled_, sawAbort); + try { + const settled_ = [...agent.state.messages].filter((m) => (m as { role?: string }).role !== "system"); + onFinish((uniquifier?.remapSettled(settled_) as AgentMessage[]) ?? settled_, sawAbort); + } catch (err) { + console.error("[turn] settlement failed:", err); + try { onSettleFailure?.(err, reportedErrorText); } + catch (cleanupError) { console.error("[turn] settlement cleanup failed:", cleanupError); } + if (reportedErrorText === undefined) { + reportedErrorText = onSettleError?.(err) ?? "An error occurred."; + send({ type: "error", errorText: reportedErrorText }); + } + } }; const handle = (event: AgentEvent) => { + if (settled) return; // THE LACAN FIX: ids uniquify HERE, before any view — the one seam // every consumer downstream shares. No uniquifier → verbatim. const uniqued = uniquifier ? uniquifier.event(event) : event; @@ -246,8 +262,9 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle { if (m?.role === "custom") noteSteerVisible(event.message as SteerMessage); } if (event.type === "agent_end") { - closeAll(); + // Persist/report before the terminal closes every view to new frames. settleWith(); + closeAll(); } }; requestCycle?.bindRelease(handle); @@ -273,11 +290,15 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle { // stream as stopReason "error"): surface, close, persist. If the // agent already settled (agent_end reached before the throw), // the seam has run — never settle twice. - if (settled) return; + if (settled) { + console.error("[turn] error after settlement:", err); + return; + } console.error("[turn] agent loop failed:", err); - send({ type: "error", errorText: onError?.(err) ?? "An error occurred." }); - closeAll(); + reportedErrorText = onError?.(err) ?? "An error occurred."; + send({ type: "error", errorText: reportedErrorText }); settleWith(); + closeAll(); } })(); diff --git a/src/server/turn-liveness.ts b/src/server/turn-liveness.ts index 27c8cc1..b5821fc 100644 --- a/src/server/turn-liveness.ts +++ b/src/server/turn-liveness.ts @@ -21,6 +21,7 @@ */ import { emit } from "../core/events.ts"; +import { clearTurnFailure } from "./turn-runs.ts"; interface LhGlobal { __lhTurnRegistry?: Map; @@ -43,15 +44,17 @@ const MAX_TURN_MS = 30 * 60_000; */ export function beginTurn(chatId: string): boolean { if (isTurnRunning(chatId)) return false; + clearTurnFailure(chatId); inFlight.set(chatId, Date.now()); emit("turn-started", { chatId }); return true; } /** - * Mark the chat's turn as finished (runs in onFinish, aborted or not — - * AFTER persistTurn, so "turn-settled" guarantees a subsequent fetch reads - * durable truth). Emits only when an entry was actually present: a + * Mark the chat's turn as finished after normal settlement, or after the + * failure notice has been retained by settlement cleanup. A subsequent + * fetch reads durable truth, never a promise that an unsaved reply landed. + * Emits only when an entry was actually present: a * defensive no-op endTurn must not fire a spurious settle. */ export function endTurn(chatId: string): void { diff --git a/src/server/turn-runs.ts b/src/server/turn-runs.ts index 29bf49a..3fbf9e7 100644 --- a/src/server/turn-runs.ts +++ b/src/server/turn-runs.ts @@ -13,7 +13,9 @@ * Registration lifecycle: chat-handler registers the handle the moment * the turn starts (after the 409 guard) and its onFinish unregisters LAST * — after persist, usage, extraction, and endTurn — so anything that sees - * "no running turn" also sees an already-persisted record. The per-chat + * "no running turn" sees durable record truth. Failed settlement also + * retains its small notice before unregistering, so late attachments do + * not mistake an unsaved reply for success. The per-chat * 409 guard means a chatId can hold at most one handle, and beginTurn's * 30-minute TTL bounds the pathological never-settling case the same way * it does for the liveness map: if onFinish never fires, the stale entry @@ -30,6 +32,7 @@ import type { PiTurnHandle } from "./pi/ui-stream.ts"; interface LhTurnRunsGlobal { __lhTurnRuns?: Map; + __lhTurnFailures?: Map; } function runs(): Map { @@ -37,6 +40,25 @@ function runs(): Map { return (g.__lhTurnRuns ??= new Map()); } +/** One small, transient notice per failed session. Never stores messages. + * Cleared on the next accepted begin, session deletion, or process exit. */ +function failures(): Map { + const g = globalThis as typeof globalThis & LhTurnRunsGlobal; + return (g.__lhTurnFailures ??= new Map()); +} + +export function rememberTurnFailure(chatId: string, errorText: string): void { + failures().set(chatId, errorText); +} + +export function turnFailure(chatId: string): string | undefined { + return failures().get(chatId); +} + +export function clearTurnFailure(chatId: string): void { + failures().delete(chatId); +} + /** Register the chat's running turn (chat-handler, right after the turn * starts). One per chat by construction — the 409 guard. */ export function registerTurn(chatId: string, handle: PiTurnHandle): void { @@ -67,11 +89,14 @@ export function abortTurn(chatId: string): { ok: true } | { ok: false; reason: " /** The cheap terminal when GET /api/chats/[id]/stream finds no running * turn (raced settle, or the client checked turnInFlight a beat too * late): a same-dialect SSE response whose single chunk tells the client - * "nothing to attach to — the record is current, refetch it". Chosen as - * an EVENT (not a status code) so the reattach path consumes ONE wire + * "nothing to attach to — refetch durable truth". A retained settlement + * failure is delivered first, including when no view attached in time. + * Chosen as an EVENT (not a status code), consuming ONE wire * protocol end to end. */ -export function settledTurnResponse(): Response { - return new Response('data: {"type":"turn-settled"}\n\ndata: [DONE]\n\n', { +export function settledTurnResponse(chatId?: string): Response { + const errorText = chatId ? turnFailure(chatId) : undefined; + const notice = errorText === undefined ? "" : `data: ${JSON.stringify({ type: "error", errorText })}\n\n`; + return new Response(`${notice}data: {"type":"turn-settled"}\n\ndata: [DONE]\n\n`, { headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache, no-transform", diff --git a/src/server/turn-service.ts b/src/server/turn-service.ts index b369697..ef7728a 100644 --- a/src/server/turn-service.ts +++ b/src/server/turn-service.ts @@ -1,5 +1,5 @@ /** - * The chat-turn services — one named, error-contained seam per turn + * The chat-turn services — one named seam per turn * responsibility, extracted from the chat-handler so it composes instead of * implementing. Each takes its dependencies explicitly (storage ports, the * job runner) — no module here imports an adapter. @@ -26,6 +26,14 @@ import type { ChatStore, GraphStore } from "../core/ports.ts"; import type { ExtractionPipeline } from "../agent/subagents/extraction.ts"; import { ModelsNotConfiguredError, ProviderKeyMissingError } from "../core/settings.ts"; +/** Distinguish a failed canonical save from later bookkeeping failures. */ +export class TurnPersistenceError extends Error { + constructor(cause: unknown) { + super("The reply could not be saved.", { cause }); + this.name = "TurnPersistenceError"; + } +} + /** * The canonical state write (Wave B, owner-approved 2026-09-26): append the * turn's own messages — the agent loop's native history, provider/model/ @@ -45,7 +53,8 @@ import { ModelsNotConfiguredError, ProviderKeyMissingError } from "../core/setti * - Plan blocks: one frozen snapshot per successful plan_init call, * idempotent (persist replays are safe). * - * Returns the stamped new messages (usage provenance reads them). + * Returns the stamped new messages (usage provenance reads them). Throws + * TurnPersistenceError on failure so the runner can report it before DONE. */ export function persistTurn( chatStore: ChatStore, @@ -56,7 +65,7 @@ export function persistTurn( ): LhMessage[] { try { const record = chatStore.getChat(chatId); - if (!record) return []; + if (!record) throw new Error(`Chat not found: ${chatId}`); const stamped: LhMessage[] = []; for (const m of turnMessages.slice(baseCount)) { const message = m as LhMessage; @@ -79,7 +88,7 @@ export function persistTurn( return stamped; } catch (err) { console.error(`[chat ${chatId}] persist failed:`, err, { isAborted }); - return []; + throw new TurnPersistenceError(err); } } diff --git a/tests/chat-flow.test.ts b/tests/chat-flow.test.ts index ed25fcc..82ed82f 100644 --- a/tests/chat-flow.test.ts +++ b/tests/chat-flow.test.ts @@ -30,13 +30,14 @@ import { createTeacherTools } from "../src/agent/tools.ts"; import { handleChatInput, handleChatRequest } from "../src/server/chat-handler.ts"; import { effectiveSettings } from "../src/server/stores.ts"; import { isTurnRunning } from "../src/server/turn-liveness.ts"; -import { deliverJobResult, setTurnRunner } from "../src/server/jobs.ts"; +import { deliverJobResult, registerLiveAgent, setTurnRunner, steerLiveChat, unregisterLiveAgent } from "../src/server/jobs.ts"; +import { SessionModel } from "../components/session-core.ts"; import { buildSubagentSteer } from "../src/core/contracts.ts"; import { abortTurn, runningTurn, settledTurnResponse } from "../src/server/turn-runs.ts"; import { chatStore } from "../src/store/chats.ts"; import { jobStore } from "../src/store/jobs.ts"; import { buildAnswer, dismissedAnswer, normalizeOptions, resolveCorrect, type QuestionData } from "../src/core/question-core.ts"; -import { danglingQuestionOf, endsInDismissal, needsContinuation, newAnswerResult } from "../src/core/native-messages.ts"; +import { danglingQuestionOf, endsInDismissal, needsContinuation, newAnswerResult, newUserMessage } from "../src/core/native-messages.ts"; import { projectView } from "../src/core/view.ts"; import type { PiEngine } from "../src/agent/models.ts"; import type { LhMessage } from "../src/core/native-messages.ts"; @@ -46,6 +47,8 @@ import type { LhMessage } from "../src/core/native-messages.ts"; // store imports already resolved under). const dismissRoute = await import("../app/api/chats/[id]/dismiss/route.ts"); const steerRoute = await import("../app/api/chats/[id]/steer/route.ts"); +const streamRoute = await import("../app/api/chats/[id]/stream/route.ts"); +const recordRoute = await import("../app/api/chats/[id]/route.ts"); import { PATHS } from "../config.ts"; // The key-presence turn gate (provider-key interlude, 2026-09-25): scripted @@ -845,6 +848,103 @@ describe("chat store", () => { }); }); +describe("canonical persistence failures", () => { + it("refuses a failed input save without building an agent or leaving a live turn", async () => { + const { engine } = scriptedTeacher(defaultLesson); + const chat = createConfiguredChat("Test: failed answer write-ahead"); + createdChats.push(chat.id); + await drainTurn(post(chat.id, { type: "text", text: "quiz me" }), agentFactory(engine)); + const before = chatStore.getChat(chat.id)!.messages; + const save = chatStore.saveMessages; + let builds = 0; + chatStore.saveMessages = () => { throw new Error("synthetic write-ahead failure"); }; + try { + const res = await handleChatRequest(post(chat.id, { type: "answer", toolCallId: "call-1", answer: gradedAnswer(QUIZ_INPUT, ["Mercury"]) }), (opts) => { + builds++; + return agentFactory(engine)!(opts); + }); + const running = runningTurn(chat.id); + if (running) await new Response(running.subscribe()).text(); + assert.equal(res.status, 500); + const client = new SessionModel({ chatId: chat.id, initialMessages: before, fetch: async () => res.clone() }); + await client.submitAnswer("quiz", "call-1", gradedAnswer(QUIZ_INPUT, ["Mercury"])); + assert.equal(client.getState().error?.message, "Your input could not be saved. Please try again."); + assert.equal(await res.text(), "Your input could not be saved. Please try again."); + assert.equal(builds, 0); + assert.equal(isTurnRunning(chat.id), false); + assert.equal(runningTurn(chat.id), undefined); + assert.deepEqual(chatStore.getChat(chat.id)!.messages, before); + } finally { chatStore.saveMessages = save; } + }); + + it("reports a failed reply save before DONE while keeping the accepted answer durable", async () => { + const { engine } = scriptedTeacher(defaultLesson); + const chat = createConfiguredChat("Test: failed reply persistence"); + createdChats.push(chat.id); + await drainTurn(post(chat.id, { type: "text", text: "quiz me" }), agentFactory(engine)); + const save = chatStore.saveMessages; + let writes = 0; + chatStore.saveMessages = (id, messages) => { + if (++writes === 2) throw new Error("synthetic reply save failure"); + return save(id, messages); + }; + try { + const body = await drainTurn(post(chat.id, { type: "answer", toolCallId: "call-1", answer: gradedAnswer(QUIZ_INPUT, ["Mercury"]) }), agentFactory(engine)); + const errors = eventsOf(body).filter(e => e.type === "error"); + assert.equal(errors.length, 1); + assert.equal(errors[0]!.errorText, "The reply could not be saved."); + assert.ok(body.indexOf("The reply could not be saved.") < body.indexOf("[DONE]")); + assert.equal(isTurnRunning(chat.id), false); + assert.equal(runningTurn(chat.id), undefined); + assert.equal(chatStore.getChat(chat.id)!.messages.at(-1)?.role, "toolResult"); + // The normal submitter may not reach GET /stream until AFTER the + // failing turn has left the live registry. It must still see it. + const late = await streamRoute.GET(new Request("http://test/stream"), { params: Promise.resolve({ id: chat.id }) }); + assert.equal(eventsOf(await late.text()).find(e => e.type === "error")?.errorText, "The reply could not be saved."); + // An already-open idle view can miss both the start and the wire + // error, then learn of settlement only via a record synchronization. + const snapshot = await (await recordRoute.GET(new Request("http://test/record"), { params: Promise.resolve({ id: chat.id }) })).json(); + assert.equal(needsContinuation(snapshot.messages), true, "the durable answer alone looks like an owed continuation"); + let automaticPosts = 0; + const missedView = new SessionModel({ chatId: chat.id, initialMessages: [], fetch: async () => { + automaticPosts++; + return new Response("unexpected automatic retry", { status: 500 }); + } }); + missedView.setMessages(snapshot.messages, snapshot.turnError); + await missedView.continueIfOwed(snapshot.messages); + assert.equal(automaticPosts, 0, "the record notice must block replacement even without a stream attachment"); + assert.equal(missedView.getState().error?.message, "The reply could not be saved."); + } finally { chatStore.saveMessages = save; } + }); + + it("does not redeliver a persisted learner steer after usage bookkeeping fails", async () => { + const { engine } = scriptedTeacher(() => [() => fauxAssistantMessage([fauxText("Synthetic response")])]); + const chat = createConfiguredChat("Test: post-save bookkeeping failure"); + createdChats.push(chat.id); + const steer = newUserMessage("Synthetic learner steer"); + const appendUsage = chatStore.appendTurnUsage; + chatStore.appendTurnUsage = () => { throw new Error("synthetic usage write failure"); }; + try { + const body = await drainTurn(post(chat.id, { type: "text", text: "go" }), opts => { + const agent = createTeacherAgent(undefined, { engine, messages: opts.messages, onUsage: opts.onUsage }); + agent.subscribe(event => { + if (event.type === "agent_start") assert.equal(steerLiveChat(chat.id, steer), true); + }); + return agent; + }); + assert.equal(eventsOf(body).find(e => e.type === "error")?.errorText, "An error occurred."); + assert.ok(chatStore.getChat(chat.id)!.messages.some(m => m.id === steer.id), "the steer was saved before bookkeeping failed"); + const delivered: LhMessage[] = []; + const next = { steer: (message: LhMessage) => delivered.push(message), state: { model: { provider: "faux" } } } as unknown as Parameters[1]; + registerLiveAgent(chat.id, next); + assert.deepEqual(delivered, [], "an already-saved learner steer must not reach another agent"); + } finally { + chatStore.appendTurnUsage = appendUsage; + unregisterLiveAgent(chat.id); + } + }); +}); + describe("mid-turn navigation (turn liveness + write-ahead)", () => { it("one turn per chat: a concurrent request is 409'd; the next one after finish is accepted", async () => { const { engine } = scriptedTeacher(defaultLesson); diff --git a/tests/graph.test.ts b/tests/graph.test.ts index e21203c..5c454dc 100644 --- a/tests/graph.test.ts +++ b/tests/graph.test.ts @@ -657,6 +657,7 @@ describe("graph ledger store", () => { import { deleteChatTraces, type ChatDeletionStats } from "../src/core/graph-deletion.ts"; import { deleteChatSession, sessionCoverage } from "../src/server/chats.ts"; +import { rememberTurnFailure, settledTurnResponse } from "../src/server/turn-runs.ts"; import { beginTurn, endTurn } from "../src/server/turn-liveness.ts"; import { jobStore } from "../src/store/jobs.ts"; import { @@ -811,11 +812,13 @@ describe("session deletion (deleteChatTraces + deleteChatSession, wave 6)", () = const turnedAway = await deleteChatSession(dA.id, "delete-nodes"); assert.deepEqual(turnedAway, { ok: false, status: 409, code: "turn_in_flight", error: "A turn is running in this session — try again when it finishes", jobIds: [] }); endTurn(dA.id); - // 409 — running background jobs. + // 409 — running background jobs; a refused delete keeps its notice. + rememberTurnFailure(dA.id, "The reply could not be saved."); const job = jobStore.createJob({ kind: "research", chatId: dA.id, toolCallId: "del-call-1" }); const jobsBlocked = await deleteChatSession(dA.id, "delete-nodes"); assert.ok(!jobsBlocked.ok && jobsBlocked.status === 409, "refused with 409"); if (jobsBlocked.code === "jobs_running") assert.deepEqual(jobsBlocked.jobIds, [job.id]); + assert.match(await settledTurnResponse(dA.id).text(), /The reply could not be saved\./); assert.equal(jobStore.deleteJobsForChat(dA.id), 1, "test cleanup — the job file is gone"); // Success — a terminal job rides along; the chat record dies last. @@ -830,6 +833,7 @@ describe("session deletion (deleteChatTraces + deleteChatSession, wave 6)", () = assert.ok(outcome.graphRevision > 0, "the version stamp moved"); } assert.equal(chatStore.getChat(dA.id), null, "the chat record is gone"); + assert.doesNotMatch(await settledTurnResponse(dA.id).text(), /"type":"error"/, "deletion clears its transient notice"); assert.equal(jobStore.getJob(doneJob.id), null, "the chat's job records are gone"); // And a second call is an honest 404. assert.deepEqual(await deleteChatSession(dA.id, "delete-nodes"), { ok: false, status: 404, code: "not_found", error: "No such session" }); diff --git a/tests/render-cards.test.tsx b/tests/render-cards.test.tsx index f473f42..d5ae8d3 100644 --- a/tests/render-cards.test.tsx +++ b/tests/render-cards.test.tsx @@ -78,6 +78,31 @@ function renderDock(props: { ); } +describe("question submission draft restoration", () => { + const draft = { + selection: { selected: new Set([0]), otherSelected: true, dontKnow: false }, + thinking: "preserved reasoning", + other: "preserved custom answer", + text: "preserved free-text answer", + confidenceIndex: 4, + }; + + it("restores a quiz selection, thinking, and confidence when its form remounts", () => { + const html = renderToStaticMarkup( {}} onSubmit={() => {}} initialDraft={draft} />); + assert.match(html, /role="radio" aria-checked="true" data-kbdrow="0"/); + assert.ok(html.includes("preserved reasoning")); + assert.ok(html.includes("90%")); + }); + + it("restores an ask's custom answer and a free-text answer", () => { + const custom = renderToStaticMarkup( {}} onSubmit={() => {}} initialDraft={draft} />); + assert.ok(custom.includes('value="preserved custom answer"')); + assert.match(custom, /aria-checked="true" data-testid="other-option"/); + const text = renderToStaticMarkup( {}} onSubmit={() => {}} initialDraft={draft} />); + assert.ok(text.includes("preserved free-text answer")); + }); +}); + describe("DispatchCard — the BYOK refusal tone (owner, 2026-09-22)", () => { const job = (over: Partial): JobRecord => ({ id: "j1", diff --git a/tests/session-core.test.ts b/tests/session-core.test.ts index 7e5ecd7..27c3110 100644 --- a/tests/session-core.test.ts +++ b/tests/session-core.test.ts @@ -684,6 +684,334 @@ describe("provider-error banner (live and saved)", () => { }); }); +describe("truncated stream recovery", () => { + const truncated = () => sseResponse({ type: "message_start", message: { role: "assistant", content: [{ type: "text", text: "partial" }] } }); + const completed = () => sseResponse({ type: "message_start", message: { role: "assistant", content: [{ type: "text", text: "complete" }] } }, { type: "message_end", message: { role: "assistant", content: [{ type: "text", text: "complete" }], stopReason: "stop" } }, "[DONE]"); + + it("reattaches once after EOF without DONE and replaces the incomplete replay", async () => { + let streams = 0; + const m = model({ fetch: dietFetch({ userText: "go", stream: () => ++streams === 1 ? truncated() : completed() }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 2); + assert.equal(m.status, "ready"); + assert.equal(m.getState().error, null); + assert.equal(m.getState().nativeMessages.filter(m => m.role === "assistant").length, 1); + assert.equal(m.getState().messages.at(-1)!.parts.at(-1)!.text, "complete"); + }); + + it("surfaces an error after a second truncation without retrying the input", async () => { + let streams = 0; + let posts = 0; + const m = model({ fetch: dietFetch({ onPost: () => posts++, stream: () => { streams++; return truncated(); } }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(posts, 1); + assert.equal(streams, 2); + assert.equal(m.status, "error"); + assert.equal(m.getState().error?.message, "The connection dropped before the turn completed."); + }); + + it("reports the approved connection error when the recovery record read fails", async () => { + let reads = 0; + const m = model({ fetch: async (url) => { + if (String(url) === "/api/chat") return Response.json({ ok: true }); + if (String(url).endsWith("/stream")) return truncated(); + if (++reads === 1) return Response.json({ messages: [] }); + throw new TypeError("synthetic offline"); + } }); + await m.send({ type: "text", text: "go" }); + assert.equal(m.status, "error"); + assert.equal(m.getState().error?.message, "The connection dropped before the turn completed."); + }); + + it("accepts turn-settled when the original turn finished during the disconnection", async () => { + let streams = 0; + let settled = 0; + const m = model({ onReattachSettled: () => { settled++; }, fetch: dietFetch({ stream: () => ++streams === 1 ? truncated() : sseResponse({ type: "turn-settled" }, "[DONE]") }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 2); + assert.equal(settled, 1); + assert.equal(m.status, "ready"); + assert.equal(m.getState().error, null); + }); + + it("also recovers a rejected body read but does not relabel event-application bugs", async () => { + let streams = 0; + const m = model({ fetch: dietFetch({ stream: () => ++streams === 1 ? new Response(new ReadableStream({ start(controller) { controller.error(new TypeError("synthetic read failure")); } })) : completed() }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 2); + assert.equal(m.status, "ready"); + streams = 0; + const broken = model({ fetch: dietFetch({ stream: () => { streams++; return completed(); } }) }); + broken.applyWireEvent = () => { throw new Error("synthetic event application bug"); }; + await broken.send({ type: "text", text: "go" }); + assert.equal(streams, 1); + assert.equal(broken.getState().error?.message, "synthetic event application bug"); + }); + + it("recovers an AbortError body rejection unless the view was actually cancelled", async () => { + let streams = 0; + const m = model({ fetch: dietFetch({ stream: () => ++streams === 1 ? new Response(new ReadableStream({ + start(controller) { controller.error(new DOMException("synthetic unexpected read abort", "AbortError")); }, + })) : completed() }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 2); + assert.equal(m.status, "ready"); + assert.equal(m.getState().error, null); + }); + + it("uses the approved drop message when recovery record headers arrive but its body read fails", async () => { + let reads = 0; + const m = model({ fetch: async (url) => { + if (String(url) === "/api/chat") return Response.json({ ok: true }); + if (String(url).endsWith("/stream")) return truncated(); + if (++reads === 1) return Response.json({ messages: [] }); + return new Response(new ReadableStream({ start(controller) { controller.error(new TypeError("synthetic record-body transport failure")); } })); + } }); + await m.send({ type: "text", text: "go" }); + assert.equal(m.status, "error"); + assert.equal(m.getState().error?.message, "The connection dropped before the turn completed."); + }); + + it("preserves malformed-event diagnostics rather than calling them network drops", async () => { + let streams = 0; + const m = model({ fetch: dietFetch({ stream: () => { streams++; return new Response("data: {broken}\n\n"); } }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 1); + assert.equal(m.status, "error"); + assert.notEqual(m.getState().error?.message, "The connection dropped before the turn completed."); + assert.match(m.getState().error!.message, /JSON|property name/); + }); + + it("reports a failed recovery stream request without attempting a third attachment", async () => { + let streams = 0; + const m = model({ fetch: dietFetch({ stream: () => ++streams === 1 ? truncated() : new Response("synthetic unavailable", { status: 503 }) }) }); + await m.send({ type: "text", text: "go" }); + assert.equal(streams, 2); + assert.equal(m.status, "error"); + assert.equal(m.getState().error?.message, "The connection dropped before the turn completed."); + }); + + it("a superseded consume cannot recover or overwrite its replacement's state", async () => { + let streams = 0; + let closeFirst!: () => void; + let beganReading!: () => void; + const reading = new Promise(resolve => { beganReading = resolve; }); + const m = model({ fetch: dietFetch({ stream: () => ++streams === 1 ? new Response(new ReadableStream({ + start(controller) { closeFirst = () => controller.close(); }, + pull() { beganReading(); }, + }, { highWaterMark: 0 })) : completed() }) }); + const first = m.resumeStream(false); + await reading; + await m.resumeStream(false); + closeFirst(); + await first; + assert.equal(streams, 2); + assert.equal(m.status, "ready"); + assert.equal(m.getState().error, null); + assert.equal(m.getState().messages.at(-1)!.parts.at(-1)!.text, "complete"); + }); +}); + +describe("automatic continuation after recovery failure", () => { + const question = { ...assistantMsg("question", ""), content: [{ type: "toolCall", id: "q1", name: "quiz", arguments: { question: "Q?" } }] } as LhMessage; + const answer = buildAnswer({ id: "q1", kind: "quiz", question: "Q?", mode: "single-select", options: [], createdAt: "fixture" }, { dontKnow: true }); + const answered = [question, newAnswerResult("q1", "quiz", answer)]; + + it("a retained failure seeds the banner and blocks continuation even after banner dismissal", async () => { + let posts = 0; + const m = model({ initialMessages: answered, initialError: "The reply could not be saved.", fetch: dietFetch({ onPost: () => posts++, stream: () => sseResponse("[DONE]") }) }); + assert.equal(SessionModel.needsContinuation(answered), true); + assert.equal(m.getState().error?.message, "The reply could not be saved."); + await m.continueIfOwed(answered); + assert.equal(posts, 0); + m.clearError(); + await m.continueIfOwed(answered); + assert.equal(posts, 0, "closing the banner is not permission to retry the model"); + await m.send({ type: "text", text: "explicit learner input" }); + assert.equal(posts, 1); + assert.equal(m.getState().error, null); + }); + + it("a new server-owned turn clears the retained banner without releasing Thinking", () => { + const m = model({ initialMessages: answered, initialError: "The reply could not be saved." }); + assert.equal(m.getState().error?.message, "The reply could not be saved."); + assert.equal(m.applyWireEvent({ type: "agent_start" }), false); + assert.equal(m.getState().error, null, "positive new-turn evidence replaces the old transient notice"); + }); + + it("a live failed save cannot auto-continue the accepted answer during canonical sync", async () => { + let posts = 0; + const m = model({ initialMessages: answered, fetch: dietFetch({ onPost: () => posts++, stream: () => sseResponse("[DONE]") }) }); + m.applyWireEvent({ type: "error", errorText: "The reply could not be saved." }); + m.setMessages(answered); + await m.continueIfOwed(answered); + assert.equal(posts, 0); + }); + + it("a healthy owed continuation still starts once through the normal send gate", async () => { + let posts = 0; + const m = model({ initialMessages: answered, fetch: dietFetch({ onPost: () => posts++, stream: () => sseResponse("[DONE]") }) }); + await m.continueIfOwed(answered); + assert.equal(posts, 1); + }); +}); + +// ─── failed answer submissions (no replay without canonical reconciliation) ── + +describe("answer submission recovery", () => { + const question = { + ...assistantMsg("question", "Q?"), + content: [{ type: "toolCall", id: "q1", name: "quiz", arguments: { question: "Q?" } }], + } as LhMessage; + const answer = buildAnswer({ id: "q1", kind: "quiz", question: "Q?", mode: "single-select", options: [], createdAt: "fixture" }, { dontKnow: true, thinking: "keep my reasoning" }); + + it("restores the question after a failed POST and checks the record before another POST", async () => { + const calls: string[] = []; + let posts = 0; + const m = model({ initialMessages: [question], fetch: async (url) => { + calls.push(String(url)); + if (String(url) === "/api/chat") { + posts++; + return posts === 1 ? new Response("request refused", { status: 500 }) : Response.json({ ok: true }); + } + if (String(url).endsWith("/stream")) return sseResponse("[DONE]"); + return Response.json({ messages: [question], turnInFlight: false }); + } }); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(m.getState().messages[0]!.parts.find(p => p.toolCallId === "q1")!.state, "input-available"); + assert.equal(m.status, "error"); + m.clearError(); // dismissing the banner must not disable recovery + await m.submitAnswer("quiz", "q1", answer); + assert.equal(posts, 2, "error must not permanently close the send gate"); + assert.deepEqual(calls.slice(0, 3), ["/api/chat", "/api/chats/chat_x", "/api/chat"]); + }); + + it("retains all submitted draft channels until the canonical answer resolves the call", async () => { + const form = { selection: { selected: new Set([1]), otherSelected: true, dontKnow: false }, thinking: "reasoning", other: "custom answer", text: "free text", confidenceIndex: 4 }; + const m = model({ initialMessages: [question], fetch: async () => { throw new TypeError("offline"); } }); + m.saveQuestionDraft("q1", { form }); + m.saveQuestionDraft("q1", { readingThinking: "reading reasoning" }); + await m.submitAnswer("quiz", "q1", answer); + assert.deepEqual(m.questionDraft("q1"), { form, readingThinking: "reading reasoning" }); + m.setMessages([question]); + assert.deepEqual(m.questionDraft("q1"), { form, readingThinking: "reading reasoning" }); + assert.equal(m.questionDraft("q2"), undefined, "a different question never inherits the draft"); + m.adoptRecord([question, newAnswerResult("q1", "quiz", answer)]); + assert.equal(m.questionDraft("q1"), undefined, "only confirmed native resolution clears it"); + }); + + it("a final 409 restores the question instead of leaving an optimistic answer", async () => { + const m = model({ initialMessages: [question], waitMaxMs: 0, fetch: async () => new Response("busy", { status: 409 }) }); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(m.getState().nativeMessages.length, 1); + assert.equal(m.status, "error"); + }); + + it("does not repost an answer whose acceptance acknowledgement was lost", async () => { + let posts = 0; + const saved = JSON.parse(JSON.stringify([question, newAnswerResult("q1", "quiz", answer)])) as LhMessage[]; + const m = model({ initialMessages: [question], fetch: async (url) => { + if (String(url) === "/api/chat") { posts++; throw new TypeError("failed fetch"); } + return Response.json({ messages: saved, turnInFlight: false }); + } }); + await m.submitAnswer("quiz", "q1", answer); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(posts, 1); + assert.equal(m.status, "ready"); + assert.deepEqual(m.getState().nativeMessages, saved); + }); + + it("reattaches to an accepted running turn instead of posting another answer", async () => { + const calls: string[] = []; + const m = model({ initialMessages: [question], fetch: async (url) => { + calls.push(String(url)); + if (String(url) === "/api/chat") throw new TypeError("failed fetch"); + if (String(url).endsWith("/stream")) return sseResponse("[DONE]"); + return Response.json({ messages: [question, newAnswerResult("q1", "quiz", answer)], turnInFlight: true }); + } }); + await m.submitAnswer("quiz", "q1", answer); + await m.submitAnswer("quiz", "q1", answer); + assert.deepEqual(calls, ["/api/chat", "/api/chats/chat_x", "/api/chats/chat_x/stream"]); + assert.equal(m.status, "ready"); + }); + + it("keeps the question and makes no further POST while reconciliation is unreachable", async () => { + const calls: string[] = []; + const m = model({ initialMessages: [question], fetch: async (url) => { calls.push(String(url)); throw new TypeError("offline"); } }); + await m.submitAnswer("quiz", "q1", answer); + await m.submitAnswer("quiz", "q1", answer); + assert.deepEqual(calls, ["/api/chat", "/api/chats/chat_x"]); + assert.equal(m.status, "error"); + assert.equal(m.getState().nativeMessages.some(m => m.role === "toolResult"), false); + }); + + it("does not undo an acknowledged dismissal when an unrelated send's record read fails", async () => { + const m = model({ initialMessages: [question], fetch: async (url) => { + if (String(url).endsWith("/dismiss") || String(url) === "/api/chat") return Response.json({ ok: true }); + throw new TypeError("record read failed"); + } }); + await m.dismissQuestion("q1", dismissedAnswer("quiz", "Q?")); + await m.send({ type: "text", text: "move on" }); + assert.equal(m.status, "error"); + const part = m.getState().messages.flatMap(m => m.parts).find(p => p.toolCallId === "q1")!; + assert.equal(part.state, "output-available"); + assert.equal((part.output as { dismissed: boolean }).dismissed, true); + }); + + it("restores an unconfirmed optimistic answer when the post-ack record read fails", async () => { + let posts = 0; + let reads = 0; + const saved = JSON.parse(JSON.stringify([question, newAnswerResult("q1", "quiz", answer)])) as LhMessage[]; + const m = model({ initialMessages: [question], fetch: async (url) => { + if (String(url) === "/api/chat") { posts++; return Response.json({ ok: true }); } + if (++reads === 1) throw new TypeError("record read failed"); + return Response.json({ messages: saved, turnInFlight: false }); + } }); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(m.status, "error"); + assert.equal(m.getState().nativeMessages.some(m => m.role === "toolResult"), false); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(posts, 1, "the acknowledged answer must not be reposted"); + assert.equal(m.status, "ready"); + assert.deepEqual(m.getState().nativeMessages, saved); + }); + + for (const next of [{ type: "text", text: "a distinct new message" }, { type: "thinking", problemCallId: "problem1", text: "new practice thinking" }] as const) { + it(`preserves the existing POST/409-wait path for a new ${next.type} submission after an error`, async () => { + const posted: ChatInput[] = []; + const m = model({ pollMs: 0, fetch: async (url, init) => { + if (String(url) === "/api/chat") { + posted.push(JSON.parse(String(init?.body)).input); + if (posted.length === 1) throw new TypeError("failed fetch"); + if (posted.length === 2) return new Response("busy", { status: 409 }); + return Response.json({ ok: true }); + } + if (String(url).startsWith("/api/jobs")) return Response.json({ turnInFlight: false }); + if (String(url).endsWith("/stream")) return sseResponse("[DONE]"); + return Response.json({ messages: [], turnInFlight: true }); + } }); + await m.send({ type: "text", text: "original input" }); + await m.send(next); + assert.deepEqual(posted, [{ type: "text", text: "original input" }, next, next]); + assert.equal(m.status, "ready"); + }); + } + + it("adopts a changed pending question instead of sending the stale answer", async () => { + let posts = 0; + const newer = { ...question, id: "new-question", content: [{ type: "toolCall", id: "q2", name: "quiz", arguments: {} }] } as LhMessage; + const m = model({ initialMessages: [question], fetch: async (url) => { + if (String(url) === "/api/chat") { posts++; throw new TypeError("failed fetch"); } + return Response.json({ messages: [question, newer], turnInFlight: false }); + } }); + await m.submitAnswer("quiz", "q1", answer); + await m.submitAnswer("quiz", "q1", answer); + assert.equal(posts, 1); + assert.deepEqual(m.getState().nativeMessages, [question, newer]); + assert.equal(m.status, "ready"); + }); +}); + // ─── reattach (resumeStream) ──────────────────────────────────────────────── describe("resumeStream (the reattach path)", () => { diff --git a/tests/turn-liveness.test.ts b/tests/turn-liveness.test.ts index 91f1a13..e9bbc03 100644 --- a/tests/turn-liveness.test.ts +++ b/tests/turn-liveness.test.ts @@ -11,8 +11,19 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { beginTurn, endTurn, isTurnRunning } from "../src/server/turn-liveness.ts"; import { subscribe, type ServerEvent } from "../src/core/events.ts"; +import { rememberTurnFailure, settledTurnResponse } from "../src/server/turn-runs.ts"; describe("turn liveness registry", () => { + it("retains a late-attach failure notice until the next accepted begin", async () => { + const id = "chat_failure_notice_unit"; + rememberTurnFailure(id, "The reply could not be saved."); + assert.match(await settledTurnResponse(id).text(), /The reply could not be saved\./); + assert.match(await settledTurnResponse(id).text(), /The reply could not be saved\./, "reading does not consume the notice"); + assert.equal(beginTurn(id), true); + assert.doesNotMatch(await settledTurnResponse(id).text(), /"type":"error"/); + endTurn(id); + }); + it("begin/end round trip; a second begin is rejected until the turn ends", () => { const id = "chat_liveness_unit"; assert.equal(isTurnRunning(id), false, "initially idle"); diff --git a/tests/turn-recovery.test.ts b/tests/turn-recovery.test.ts new file mode 100644 index 0000000..2ef7157 --- /dev/null +++ b/tests/turn-recovery.test.ts @@ -0,0 +1,90 @@ +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import type { Agent, AgentMessage } from "@earendil-works/pi-agent-core"; +import type { ChatStore } from "../src/core/ports.ts"; +import { runPiTurn } from "../src/server/pi/ui-stream.ts"; +import { persistTurn } from "../src/server/turn-service.ts"; + +/** No engine or network: exercise the runner's awaited listener seam. */ +function agentEndingWith(loopFailure?: Error): Agent { + const listeners: Array<(event: never) => unknown> = []; + return { + state: { messages: [] }, + subscribe(listener: (event: never) => unknown) { listeners.push(listener); return () => {}; }, + async continue() { + await Promise.resolve(); // let the view subscribe before the terminal + if (loopFailure) throw loopFailure; + for (const listener of listeners) await listener({ type: "agent_end", messages: [] } as never); + }, + abort() {}, + } as unknown as Agent; +} + +function wireFrames(wire: string): string[] { + return wire.split("\n\n").filter(Boolean).map(frame => frame.slice("data: ".length)); +} + +describe("turn settlement recovery", () => { + it("reports a failed canonical save before DONE and cleans up before reporting", async () => { + const order: string[] = []; + const handle = runPiTurn({ + agent: agentEndingWith(), + onFinish() { order.push("save"); throw new Error("synthetic disk failure"); }, + onSettleFailure() { order.push("cleanup"); }, + onSettleError() { order.push("report"); return "The reply could not be saved."; }, + }); + const frames = wireFrames(await new Response(handle.subscribe()).text()); + assert.deepEqual(order, ["save", "cleanup", "report"]); + assert.equal(frames.at(-1), "[DONE]"); + assert.equal(frames.filter(f => f.includes('"type":"error"')).length, 1); + assert.ok(frames.at(-2)?.includes("The reply could not be saved.")); + }); + + it("still reports settlement failure when cleanup itself throws", async () => { + const handle = runPiTurn({ + agent: agentEndingWith(), + onFinish() { throw new Error("synthetic save failure"); }, + onSettleFailure() { throw new Error("synthetic cleanup failure"); }, + onSettleError: () => "The reply could not be saved.", + }); + const frames = wireFrames(await new Response(handle.subscribe()).text()); + assert.equal(frames.at(-1), "[DONE]"); + assert.equal(frames.filter(f => f.includes('"type":"error"')).length, 1); + }); + + it("does not replace an already reported loop failure with a second settlement error", async () => { + let cleanups = 0; + const handle = runPiTurn({ + agent: agentEndingWith(new Error("synthetic loop failure")), + onError: () => "An error occurred.", + onFinish() { throw new Error("synthetic save failure"); }, + onSettleFailure(error, reportedErrorText) { + cleanups++; + assert.match(String(error), /synthetic save failure/); + assert.equal(reportedErrorText, "An error occurred."); + }, + }); + const frames = wireFrames(await new Response(handle.subscribe()).text()); + assert.equal(cleanups, 1); + assert.equal(frames.filter(f => f.includes('"type":"error"')).length, 1); + assert.equal(frames.at(-1), "[DONE]"); + }); + + it("a healthy terminal follows persistence and emits no error", async () => { + let saves = 0; + const handle = runPiTurn({ agent: agentEndingWith(), onFinish() { saves++; } }); + const frames = wireFrames(await new Response(handle.subscribe()).text()); + assert.equal(saves, 1); + assert.equal(frames.at(-1), "[DONE]"); + assert.equal(frames.some(f => f.includes('"type":"error"')), false); + }); + + it("does not contain a canonical write failure as an empty successful persist", () => { + const store = { + getChat: () => ({ messages: [] }), + saveMessages() { throw new Error("synthetic disk failure"); }, + } as unknown as ChatStore; + const message = { role: "assistant", content: [{ type: "text", text: "Synthetic reply" }], stopReason: "stop", timestamp: 1 } as AgentMessage; + assert.throws(() => persistTurn(store, "fixture", 0, [message], false), /The reply could not be saved\./); + }); +}); From 6701503801b761b2e6f5f92821d5b9fa9afdaa5d Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Sun, 25 Oct 2026 23:29:00 +0200 Subject: [PATCH 03/19] fix: isolate generated SVG figures as images --- components/artifacts/svg-image.tsx | 37 +++++ components/artifacts/visual-card.tsx | 30 ++-- components/session-overlay.tsx | 35 ++--- tests/demos.test.tsx | 23 +-- tests/svg-image.test.tsx | 207 +++++++++++++++++++++++++++ 5 files changed, 282 insertions(+), 50 deletions(-) create mode 100644 components/artifacts/svg-image.tsx create mode 100644 tests/svg-image.test.tsx diff --git a/components/artifacts/svg-image.tsx b/components/artifacts/svg-image.tsx new file mode 100644 index 0000000..6c74cdd --- /dev/null +++ b/components/artifacts/svg-image.tsx @@ -0,0 +1,37 @@ +/** Two display-only repairs for inputs the old HTML embedding accepted. + * Do not rewrite stored artifacts/downloads or attempt general XML repair. + * Comments, CDATA and processing instructions are copied verbatim. */ +export function svgImageSource(svg: string): string { + let foundRoot = false; + return svg.replace( + /|$)||$)|<\?[\s\S]*?(?:\?>|$)|<\/?[a-zA-Z](?:[^<>"']|"[^"]*"|'[^']*')*>| /g, + (token) => { + if (token.startsWith("]]>'); + assert.equal(svgImageSource(source), source); + }); + + it("adds only a missing root namespace, ignoring comments and attribute values", () => { + const source = '\' xmlns:xlink="http://www.w3.org/1999/xlink">'; + const expected = source.replace('Example`; + assert.equal(svgImageSource(source), source); + } + }); + + it("converts nbsp references in text and attributes, but not comments, CDATA or processing instructions", () => { + const source = 'A B &nbsp;  &nbsp;'; + const expected = source.replace('aria-label="A B"', 'aria-label="A B"').replace('A B', 'A B'); + assert.equal(svgImageSource(source), expected); + assert.equal(svgImageSource(expected), expected); + }); +}); + +describe("generated SVG image boundary", () => { + for (const mode of ["inline", "fullscreen"] as const) { + for (const [name, source] of [["reported style rule and fragment IDs", FIGURE], ["active markup", ACTIVE_MARKUP]] as const) { + it(`${mode}: isolates ${name} inside the image URL`, () => { + const html = renderToStaticMarkup(mode === "inline" + ? + : {}} />); + const { document } = parseHTML(html); + const holder = document.querySelector(mode === "inline" ? '[data-testid="visual-svg"]' : "[data-lh-overlay-chart]")!; + assertImageBoundary(holder, source); + assert.ok(holder.classList.contains("bg-white"), "white backing retained"); + }); + } + + for (const [name, original, display] of [ + ["missing namespace", 'Example', 'Example'], + ["HTML entity", 'A B', 'A B'], + ] as const) { + it(`${mode}: repairs ${name} only in the image source`, () => { + const output = { svg: original }; + const html = renderToStaticMarkup(mode === "inline" + ? + : {}} />); + const { document } = parseHTML(html); + const holder = document.querySelector(mode === "inline" ? '[data-testid="visual-svg"]' : "[data-lh-overlay-chart]")!; + assertImageBoundary(holder, display); + assert.equal(output.svg, original, "artifact source was not mutated"); + }); + } + + it(`${mode}: retains inversion on the white backing`, () => { + const html = renderToStaticMarkup(mode === "inline" + ? + : {}} />); + const { document } = parseHTML(html); + const holder = document.querySelector(mode === "inline" ? '[data-testid="visual-svg"]' : "[data-lh-overlay-chart]")!; + assert.match(holder.getAttribute("style")!, /invert\(1\) hue-rotate\(180deg\)/); + assert.ok(holder.classList.contains("bg-white")); + assertImageBoundary(holder, FIGURE); + }); + } + + it("inline: retains natural width and only shrinks oversized figures", () => { + const { document } = parseHTML(renderToStaticMarkup()); + const image = assertImageBoundary(document.querySelector('[data-testid="visual-svg"]')!, FIGURE); + assert.equal(image.getAttribute("width"), "320"); + assert.equal(image.className, "block h-auto max-w-full"); + assert.ok(document.querySelector('[data-testid="visual-card"]')!.classList.contains("w-fit")); + }); + + for (const width of ["", ' width="100%"']) { + it(`inline: keeps the full-width fallback for ${width || "viewBox-only"} SVG`, () => { + const source = `Example`; + const { document } = parseHTML(renderToStaticMarkup()); + const image = assertImageBoundary(document.querySelector('[data-testid="visual-svg"]')!, source); + assert.equal(image.getAttribute("width"), null); + assert.equal(image.className, "block h-auto w-full"); + assert.ok(document.querySelector('[data-testid="visual-card"]')!.classList.contains("w-full")); + }); + } + + it("fullscreen: keeps the natural-size box and zoom controls", () => { + const { document } = parseHTML(renderToStaticMarkup( {}} />)); + const holder = document.querySelector("[data-lh-overlay-chart]")!; + assert.equal(assertImageBoundary(holder, FIGURE).className, "block h-auto w-full"); + assert.match(holder.parentElement!.getAttribute("style")!, /width:320px/); + for (const label of ["Zoom in", "Zoom out", "Fit diagram to view", "Close"]) { + assert.ok(document.querySelector(`button[aria-label="${label}"]`)); + } + }); +}); + +it("the fullscreen button opens the same SVG image and inversion; download keeps original bytes", async () => { + const originalSvg = FIGURE.replace(' xmlns="http://www.w3.org/2000/svg"', '').replace("θ & φ", "θ & φ"); + const displaySvg = FIGURE.replace("θ & φ", "θ & φ"); + const dom = parseHTML(""); + const names = ["window", "document", "Element", "HTMLElement", "IS_REACT_ACT_ENVIRONMENT"] as const; + const originals = names.map((name) => Object.getOwnPropertyDescriptor(globalThis, name)); + Object.assign(globalThis, { + window: dom.window, + document: dom.document, + Element: dom.window.Element, + HTMLElement: dom.window.HTMLElement, + IS_REACT_ACT_ENVIRONMENT: true, + }); + const host = dom.document.createElement("div"); + dom.document.body.append(host); + const root = createRoot(host); + const originalCreateUrl = URL.createObjectURL; + const originalRevokeUrl = URL.revokeObjectURL; + const originalCreateElement = dom.document.createElement; + let downloaded: Blob | undefined; + let revoked: string | undefined; + let clicked: { name: string; href: string } | undefined; + URL.createObjectURL = (blob) => { + assert.ok(blob instanceof Blob); + downloaded = blob; + return "blob:figure-test"; + }; + URL.revokeObjectURL = (url) => { revoked = url; }; + function Harness() { + const [focus, setFocus] = useState<{ svg: string; inverted: boolean } | null>(null); + return ( + {}, openFigure: (svg, inverted) => setFocus({ svg, inverted }) }}> + + {focus && setFocus(null)} />} + + ); + } + try { + await act(async () => root.render()); + await act(async () => { host.querySelector('[data-testid="visual-open"]')!.dispatchEvent(new dom.window.Event("click", { bubbles: true })); }); + const inline = assertImageBoundary(host.querySelector('[data-testid="visual-svg"]')!, displaySvg); + const overlayHolder = host.querySelector("[data-lh-overlay-chart]")!; + const fullscreen = assertImageBoundary(overlayHolder, displaySvg); + assert.equal(fullscreen.getAttribute("src"), inline.getAttribute("src")); + assert.match(overlayHolder.getAttribute("style")!, /invert\(1\) hue-rotate\(180deg\)/); + + // Intercept only the download anchor; never navigate or write a real file. + dom.document.createElement = ((...args: Parameters) => { + const element = originalCreateElement.apply(dom.document, args); + if (args[0] === "a") { + element.click = () => { clicked = { name: element.getAttribute("download")!, href: element.getAttribute("href")! }; }; + } + return element; + }) as typeof originalCreateElement; + await act(async () => { host.querySelector('[data-testid="visual-download"]')!.dispatchEvent(new dom.window.Event("click", { bubbles: true })); }); + assert.ok(downloaded); + assert.equal(await downloaded.text(), originalSvg); + assert.equal(downloaded.type, "image/svg+xml"); + assert.deepEqual(clicked, { name: "figure.svg", href: "blob:figure-test" }); + assert.equal(revoked, "blob:figure-test"); + } finally { + try { + await act(async () => root.unmount()); + } finally { + URL.createObjectURL = originalCreateUrl; + URL.revokeObjectURL = originalRevokeUrl; + dom.document.createElement = originalCreateElement; + host.remove(); + for (const [i, name] of names.entries()) { + if (originals[i]) Object.defineProperty(globalThis, name, originals[i]!); + else Reflect.deleteProperty(globalThis, name); + } + } + } +}); From 46c95c7b5978c441339d4ab4d7953958ed9c7eb8 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 00:15:59 +0200 Subject: [PATCH 04/19] fix: update Electron to 44.9.3 --- package-lock.json | 8 ++++---- package.json | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7a91ffc..0c7ac4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -65,7 +65,7 @@ "@types/turndown": "^5.0.6", "@typescript-eslint/parser": "^8.68.0", "concurrently": "^10.0.5", - "electron": "^44.4.1", + "electron": "^44.9.3", "electron-builder": "26.16.1", "eslint": "^9.39.5", "eslint-import-resolver-typescript": "^4.4.5", @@ -8687,9 +8687,9 @@ } }, "node_modules/electron": { - "version": "44.4.1", - "resolved": "https://registry.npmjs.org/electron/-/electron-44.4.1.tgz", - "integrity": "sha512-gXJarQ2Utfn8G6bp/IqLXvckOSCtrxPWjFqQaqm02Gf4zYn1aP+RN9PKimoLH/DMqjKLfBrNC4C4CviPv0cXnQ==", + "version": "44.9.3", + "resolved": "https://registry.npmjs.org/electron/-/electron-44.9.3.tgz", + "integrity": "sha512-Cv9/yMnZxNWsMlIe8T4v//NKHdUvbeyoTJoXE3BIcRxus2meeFaO9h/P3zB5d4cBXum51IgAYrT2QjpIDWKpJA==", "dev": true, "license": "MIT", "dependencies": { diff --git a/package.json b/package.json index 1b0c649..bd2e627 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "@types/turndown": "^5.0.6", "@typescript-eslint/parser": "^8.68.0", "concurrently": "^10.0.5", - "electron": "^44.4.1", + "electron": "^44.9.3", "electron-builder": "26.16.1", "eslint": "^9.39.5", "eslint-import-resolver-typescript": "^4.4.5", From d9fa314c20baec522a518076b28e4fbe9f6c0499 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 01:18:53 +0200 Subject: [PATCH 05/19] fix(pdf): resolve learner paths relative to home --- src/agent/subagents/pdf-reader.ts | 3 +- tests/jobs.test.ts | 55 +++++++++++++++++++- tests/pdf-reader.test.ts | 84 +++++++++++++++++++++++++++++-- 3 files changed, 137 insertions(+), 5 deletions(-) diff --git a/src/agent/subagents/pdf-reader.ts b/src/agent/subagents/pdf-reader.ts index 462a3e3..c3feae2 100644 --- a/src/agent/subagents/pdf-reader.ts +++ b/src/agent/subagents/pdf-reader.ts @@ -2,6 +2,7 @@ import { randomUUID } from "node:crypto"; import type { AssistantMessage, Message, ToolResultMessage, Usage, Tool } from "@earendil-works/pi-ai"; import { normalizeContext } from "@earendil-works/pi-ai"; import { Type } from "typebox"; +import { resolveSourcePath } from "../tools/fs-shared.ts"; import { piEngineFor, type PiEngine, type ThinkingLevel } from "../models.ts"; import { providerEventObserver, type ProviderEventObserver } from "../provider-events.ts"; import type { PdfResult, PdfToolInput } from "../../core/contracts.ts"; @@ -166,7 +167,7 @@ export async function runPdfReaderAgent( ): Promise { const headless = opts.headless ?? getHeadless(); const maxIterations = opts.maxIterations ?? MAX_ITERATIONS; - const pdfPath = input.path; + const pdfPath = resolveSourcePath(input.path); // Deterministic upfront failure: unreadable / unparseable // for renders — the brief never starts on a broken source. diff --git a/tests/jobs.test.ts b/tests/jobs.test.ts index af9387a..73e1957 100644 --- a/tests/jobs.test.ts +++ b/tests/jobs.test.ts @@ -25,8 +25,9 @@ import assert from "node:assert/strict"; import * as fs from "node:fs"; +import * as os from "node:os"; import * as path from "node:path"; -import { after, afterEach, describe, it } from "node:test"; +import { after, afterEach, before, describe, it } from "node:test"; import { createFauxCore, fauxAssistantMessage, fauxText, fauxToolCall, type AssistantMessage, type FauxResponseStep } from "@earendil-works/pi-ai"; import { accumulateJobUsage, @@ -717,6 +718,27 @@ describe("research runner (async protocol §3)", () => { describe("pdf runner (sources v0)", () => { const PDF_FIXTURE = path.join(import.meta.dirname, "fixtures", "sample-3page.pdf"); const PDF_INPUT = { path: PDF_FIXTURE, question: "What is the integral of x dx?" }; + const relativePath = path.join("Course notes", "sample document.pdf"); + let home: string; + const originalHome = process.env.HOME; + const originalUserProfile = process.env.USERPROFILE; + + before(() => { + home = fs.mkdtempSync(path.join(os.tmpdir(), "lh-pdf-job-home-")); + const target = path.join(home, relativePath); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.copyFileSync(PDF_FIXTURE, target); + process.env.HOME = home; + process.env.USERPROFILE = home; + }); + + after(() => { + if (originalHome === undefined) delete process.env.HOME; + else process.env.HOME = originalHome; + if (originalUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = originalUserProfile; + fs.rmSync(home, { recursive: true, force: true }); + }); /** A pdf-reader that triages, extracts pages 1-2, then briefs. */ function pdfReaderModel(): PiEngine { @@ -759,6 +781,37 @@ describe("pdf runner (sources v0)", () => { assert.ok(steer.content.includes("integral rule"), "steer text carries the brief"); }); + for (const mode of ["dispatch", "recovery"] as const) { + it(`resolves a home-relative PDF from ${mode} input without rewriting history`, async () => { + const chat = chatStore.createChat(`Test: relative PDF ${mode}`); + trackChat(chat.id); + const toolCallId = `call-pdf-relative-${mode}`; + const input = { ...PDF_INPUT, path: relativePath }; + const messages = nativeToolMessages("read this pdf", "pdf", toolCallId, input); + chatStore.saveMessages(chat.id, messages); + const job = jobStore.createJob({ kind: "pdf", chatId: chat.id, toolCallId }); + trackJob(job.id); + const delivery = fakeDeliver(); + const runner = createJobRunner({ chatStore, jobStore, deliver: delivery.deliver }); + pdfModelOverride.current = pdfReaderModel(); + try { + await runner.runJob(job.id, mode === "dispatch" ? input : undefined); + } finally { + pdfModelOverride.current = undefined; + } + + const done = jobStore.getJob(job.id)!; + assert.equal(done.status, "done"); + const result = done.result as PdfResult; + assert.equal(result.path, path.join(home, relativePath)); + assert.match(result.brief, /integral rule/); + assert.deepEqual(result.pagesRead, [1, 2]); + assert.deepEqual(result.pagesConsulted, [1, 2]); + assert.equal(delivery.steers.length, 1); + assert.deepEqual(chatStore.getChat(chat.id)!.messages, messages, "tool-call arguments remain as supplied"); + }); + } + it("an unreadable source: the job lands error with the PDF failed: brief and the steer flags the gap", async () => { const chat = chatStore.createChat("Test: pdf runner failure"); trackChat(chat.id); diff --git a/tests/pdf-reader.test.ts b/tests/pdf-reader.test.ts index 2f4c142..166c412 100644 --- a/tests/pdf-reader.test.ts +++ b/tests/pdf-reader.test.ts @@ -5,8 +5,10 @@ * The pdfjs layer itself is covered by pdf-ops.test.ts against the fixture. */ import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; import * as path from "node:path"; -import { describe, it } from "node:test"; +import { after, before, describe, it } from "node:test"; import { createFauxCore, fauxAssistantMessage, fauxText, fauxToolCall, type FauxResponseStep } from "@earendil-works/pi-ai"; import type { PiEngine } from "../src/agent/models.ts"; import { runPdfReaderAgent } from "../src/agent/subagents/pdf-reader.ts"; @@ -28,10 +30,12 @@ const PNG_1PX = Buffer.from([ 0x42, 0x60, 0x82, ]); -function fakeHeadless(rendered: { pages: number[] } = { pages: [] }): HeadlessApi & { calls: string[] } { +function fakeHeadless(rendered: { pages: number[] } = { pages: [] }): HeadlessApi & { calls: string[]; paths: string[] } { const calls: string[] = []; + const paths: string[] = []; return { calls, + paths, available: () => true, unavailableReason: () => null, runDemo: async () => { @@ -40,7 +44,8 @@ function fakeHeadless(rendered: { pages: number[] } = { pages: [] }): HeadlessAp renderSvg: async () => { throw new Error("unused"); }, - renderPdfPages: async (_pdfPath, pages) => { + renderPdfPages: async (pdfPath, pages) => { + paths.push(pdfPath); calls.push(`render:${pages.join(",")}`); rendered.pages.push(...pages); return pages.map((page) => ({ page, png: PNG_1PX })); @@ -48,6 +53,79 @@ function fakeHeadless(rendered: { pages: number[] } = { pages: [] }): HeadlessAp }; } +describe("pdf-reader source paths", () => { + let home: string; + const relativePath = path.join("Course notes", "sample document.pdf"); + const originalHome = process.env.HOME; + const originalUserProfile = process.env.USERPROFILE; + + before(() => { + home = fs.mkdtempSync(path.join(os.tmpdir(), "lh-pdf-home-")); + const target = path.join(home, relativePath); + fs.mkdirSync(path.dirname(target), { recursive: true }); + fs.copyFileSync(FIXTURE, target); + process.env.HOME = home; + process.env.USERPROFILE = home; + }); + + after(() => { + if (originalHome === undefined) delete process.env.HOME; + else process.env.HOME = originalHome; + if (originalUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = originalUserProfile; + fs.rmSync(home, { recursive: true, force: true }); + }); + + for (const kind of ["absolute", "home-relative"] as const) { + it(`${kind} path reaches text tools and rendering as the same absolute path`, async () => { + const absolutePath = path.join(home, relativePath); + const inputPath = kind === "absolute" ? absolutePath : relativePath; + assert.equal(os.homedir(), home); + assert.notEqual(path.resolve(relativePath), absolutePath, "cwd is not the learner's home"); + const headless = fakeHeadless(); + const engine = fauxEngine([ + fauxAssistantMessage([fauxToolCall("pdf_info", {}, { id: "i1" })], { stopReason: "toolUse" }), + fauxAssistantMessage([fauxToolCall("pdf_search", { query: "integral", literal: true }, { id: "s1" })], { stopReason: "toolUse" }), + fauxAssistantMessage([fauxToolCall("pdf_extract", { pages: "2" }, { id: "e1" })], { stopReason: "toolUse" }), + fauxAssistantMessage([fauxToolCall("pdf_render", { pages: "3" }, { id: "r1" })], { stopReason: "toolUse" }), + fauxAssistantMessage([fauxText("Read the requested document (p. 1, p. 2, p. 3).")]), + ]); + let lastContext = ""; + const stream = engine.streamFn; + engine.streamFn = (...args) => { + lastContext = JSON.stringify(args[1]); + return stream(...args); + }; + const result = await runPdfReaderAgent( + { path: inputPath, question: "Read the document" }, + { engine, headless }, + ); + assert.equal(result.path, absolutePath); + assert.match(result.brief, /^Read the requested document/); + assert.match(lastContext, /Pages: 3/, "metadata opened the home-anchored PDF"); + assert.deepEqual(result.pagesRead, [2, 3]); + assert.deepEqual(result.pagesConsulted, [1, 2, 3], "search and extraction opened the home-anchored PDF"); + assert.deepEqual(headless.paths, [absolutePath]); + }); + } + + it("a missing home-relative source fails before any model call", async () => { + let modelCalls = 0; + const engine = fauxEngine([]); + engine.streamFn = () => { + modelCalls++; + throw new Error("the model must not be called"); + }; + const missing = path.join("Course notes", "missing.pdf"); + const result = await runPdfReaderAgent({ path: missing, question: "anything" }, { engine }); + assert.equal(result.path, path.join(home, missing)); + assert.match(result.brief, /^PDF failed:.*ENOENT/); + assert.equal(modelCalls, 0); + assert.deepEqual(result.pagesRead, []); + assert.deepEqual(result.pagesConsulted, []); + }); +}); + describe("pdf-reader loop (faux engine)", () => { it("triage → extract → final brief: pagesConsulted tracks the extract", async () => { const engine = fauxEngine([ From e851432f4c786d3ee57ee7aaf53439903ab043b7 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 01:55:52 +0200 Subject: [PATCH 06/19] fix: separate worker completion from durable session delivery --- components/artifacts/dispatch-card.tsx | 4 +- docs/architecture.md | 28 +- src/agent/subagents/aborts.ts | 12 +- src/agent/subagents/runner.ts | 62 +++-- src/agent/tools/jobs.ts | 2 +- src/core/contracts/jobs.ts | 60 ++--- src/core/ports.ts | 2 + src/server/chat-handler.ts | 22 +- src/server/chats.ts | 25 +- src/server/jobs.ts | 337 +++++++++++++------------ src/server/pi/ui-stream.ts | 7 - src/store/jobs.ts | 1 + tests/chat-flow.test.ts | 4 +- tests/chatgpt-background.test.ts | 5 +- tests/demos.test.tsx | 10 +- tests/job-delivery-recovery.test.ts | 265 +++++++++++++++++++ tests/jobs.test.ts | 118 +++++---- tests/render-cards.test.tsx | 4 +- tests/subscription-background.test.ts | 4 +- 19 files changed, 646 insertions(+), 326 deletions(-) create mode 100644 tests/job-delivery-recovery.test.ts diff --git a/components/artifacts/dispatch-card.tsx b/components/artifacts/dispatch-card.tsx index eaa6db7..982f284 100644 --- a/components/artifacts/dispatch-card.tsx +++ b/components/artifacts/dispatch-card.tsx @@ -15,7 +15,7 @@ import { BrailleSpinner } from "@/components/braille-spinner"; * * job unknown → "dispatched" pulse (the jobs feed hasn't seen it yet) * running → live activity card (activity line + elapsed) - * done/error → quiet one-liner ("delivered" / "failed") — the artifact + * done/error → quiet one-liner ("finished" / "failed") — the artifact * itself arrives in the steer message right below, so the * box must not duplicate it. */ @@ -54,7 +54,7 @@ export function DispatchCard({ toolCallId, kind }: { toolCallId: string; kind: J > {failed ? : } {label} - {failed ? "failed" : "delivered"} + {failed ? "failed" : "finished"}
); } diff --git a/docs/architecture.md b/docs/architecture.md index ed7d869..70594b1 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -313,22 +313,28 @@ sequenceDiagram ### 5.1 The wake pipeline (Workbench → Classroom) -On sub-agent completion the runner builds a **steer message** — a user-role `UIMessage` whose text part is the model-facing result and whose `data-subagent-artifact` part carries the full result for the client (provider conversion drops data parts) — and hands it to the wake pipeline: +**Worker completion and session delivery are separate lifecycles.** Dispatch persists the tool input on the job before spawning. On completion, the runner saves its result, terminal worker status (`done`/`error`), and `delivery: {kind: "result", status: "pending"}` in one job-record update. It then queues the native custom **steer message** without awaiting delivery. Its content is model-facing text; details carry the UI artifact. Worker resources are released and the Background agents row disappears even if an unanswered question holds the result indefinitely. Dispatch cards say “finished,” not “delivered.” Only computing workers can be aborted; abort saves an independent pending abort notice. ```mermaid flowchart TB - R["job runner finishes sub-agent"] -->|"deliver(steer) — injectable seam"| ENQ["enqueue steer in per-chat queue (globalThis)"] - ENQ --> PUMP["pump(chatId) — one loop per chat (pumping set serializes)"] - PUMP --> GATE{"turn-free AND no pending question? (400ms poll)"} - GATE -->|no| PUMP - GATE -->|yes| REFRESH["re-fetch fresh record, re-check steerDelivered (claim race)"] - REFRESH --> RUN["turnRunner(chatId, record + steer) — the SAME handleChatRequest path"] - RUN -->|"200"| FIN["finalizeSteer → finalizeJob (done/error)
— status flips only after the wake turn persists"] - RUN -->|"409"| BACKOFF["backoff 500→1000→2000→5000ms → retry"] - CRASH["crash recovery: listJobs → recoverStaleJobs (stale >15min)
re-deliver results / respawn via setJobRespawn"] --> PUMP + R["save worker outcome + pending delivery; release worker"] --> ENQ["one registry entry per steer ID, including wake cargo"] + ENQ --> LIVE{"live agent and background consent?"} + LIVE -->|yes| STEER["pi steering at native boundaries; answer run takes parked results"] + LIVE -->|no| GATE["wait for turn-free, question-free, unpaused, consented session"] + GATE --> RUN["idle wake through the normal turn composition"] + RUN -->|"409"| BACKOFF["existing backoff 500→1000→2000→5000ms"] + BACKOFF --> GATE + RUN -->|"403"| HOLD["retain ownership until consent permits delivery"] + RUN -->|"other failed start"| QUIET["quiet pending; next learner run or restart recovery"] + RUN --> SAVE["canonical marker persistence, not merely HTTP 200"] + STEER --> SAVE + SAVE --> FIN["acknowledge delivery; worker outcome unchanged"] + CRASH["lazy recovery + live-agent registration restore known pending jobs"] --> ENQ ``` -Job status flips done/error only after the wake turn persists. Delivery is **exactly-once**: deterministic steer ids (`steer_`, `steer_probe_`), `steerDelivered` checks at every claim point, and `claimSteers` finalizing in-transcript steers instead of re-appending. The client adopts the wake turn through the same `turnInFlight` machinery as returning mid-stream (busy shimmer, composer gate, append-only `mergeAdopted` adoption) and renders steers as artifacts — never a bubble. +The per-chat registry retains ownership across live handoff, idle-wake flight, and 409/403 return; repeated submissions share one entry/promise. A deterministic marker ID in the **saved conversation** completes delivery. Streaming visibility alone does not. A failed bookkeeping acknowledgement cannot replay a saved marker, and later teaching/model failure does not reopen delivery or retry the accepted turn. Failed starts leave results durably pending without a new error UI or retry timer; the in-memory entry stays parked until a learner run or process restart. Normal question/dismissal waits and consent deferrals are not failures. + +Recovery reads saved job input before legacy transcript lookup. Missing research questions fail honestly without invoking the researcher. Legacy running jobs with saved results become finished workers with pending delivery; legacy terminal jobs without delivery metadata remain untouched, rather than guessing that old outcomes should be replayed. Pending result and abort notices are restored before a newly registered agent's first steering poll, even without a preceding jobs-feed read. Deleting a session discards its pending delivery obligations; only live turns and computing workers block deletion. These guarantees use the existing JSON storage and single-process delivery registry, not a transactional queue or a promise of exactly-once model execution. Sub-agents **verify, never one-shot**: the demo-builder headlessly executes its HTML in system Chrome before delivery; the visual-maker renders each SVG and looks at it (needs a vision-capable model — the `vision` slot in settings). diff --git a/src/agent/subagents/aborts.ts b/src/agent/subagents/aborts.ts index ed5b587..b533b8a 100644 --- a/src/agent/subagents/aborts.ts +++ b/src/agent/subagents/aborts.ts @@ -41,18 +41,16 @@ export function unregisterJobAbort(jobId: string): void { * the liveness gate for the stale-job sweep: a "running" record with a * live registration belongs to a loop this process owns (hung or slow, it * is the one truth — never re-spawn beside it); without one the record is - * an orphan of a process death and recovers immediately. The span is - * loop-start to delivery-completion: the runner's unregistering finally - * runs only after deliver() resolves, by which point finalizeJob has - * flipped the record — so absence of registration is unambiguous. */ + * an orphan of a process death and recovers immediately. The registration + * spans computation and cleanup only. Finished outcomes have independent, + * persisted delivery obligations; no worker waits for their consumption. */ export function isJobLive(jobId: string): boolean { return registry().has(jobId); } /** Abort a running job's loop. Returns false when no controller is live — - * the job's loop is not executing (crashed, or in the delivering-result - * phase where the runner has already returned); the caller then owns the - * whole abort on the record side. */ + * the job's loop is not executing. The endpoint separately refuses abort + * for a finished worker, even while its session delivery remains pending. */ export function fireJobAbort(jobId: string): boolean { const controller = registry().get(jobId); if (!controller) return false; diff --git a/src/agent/subagents/runner.ts b/src/agent/subagents/runner.ts index 41a3d40..55651dc 100644 --- a/src/agent/subagents/runner.ts +++ b/src/agent/subagents/runner.ts @@ -3,6 +3,7 @@ import type { SearchProvider } from "../../core/search.ts"; import { accumulateJobUsage, buildSubagentSteer, + isSubagentFailure, piUsageToStep, type DemoResult, type DemoToolInput, @@ -40,13 +41,13 @@ import { runVisualMaker } from "./visual-maker.ts"; * (the job tools' execute fires this via the spawn seam, fire-and-forget), * updating the job's activity line on every iteration. On completion the * runner builds the STEER MESSAGE (buildSubagentSteer) and hands it to - * `deliver`; the wake pipeline fires the server-side teacher turn and - * flips the job to done/error when the lesson has actually resumed. + * `deliver` without waiting. The worker's final outcome and pending + * delivery are saved together; the session owns delivery from then on. * * On failure the result is the failure shape (empty html/svg + "FAILED: …" * notes; research: failure brief) — and it STILL steers, because the - * teacher must learn the outcome. If delivery itself fails, the job is - * marked error (the learner's activity card shows it). + * teacher must learn the outcome. Failed delivery starts stay quietly + * pending and never rewrite the worker's computation outcome. */ /** @@ -67,9 +68,9 @@ export interface JobRunnerDeps { chatStore: ChatStore; jobStore: JobStore; /** - * Hand a finished job's steer to the wake pipeline — resolves when the - * steer is delivered (the wake turn completed and the job status - * flipped); rejects when delivery fails unrecoverably. + * Hand a finished job's steer to the session delivery registry. The + * promise tracks durable marker acceptance, but the worker never waits + * on it. Its persisted pending obligation survives process restart. */ deliver: (steer: SteerMessage) => Promise; /** Settings-resolved engine per role (model slots wave, 2026-09-20): @@ -96,8 +97,8 @@ export interface JobRunnerDeps { export interface JobRunner { /** Run one job's sub-agent loop to completion (background; never throws). * The tool input rides along from dispatch (the spawn fires mid-stream, - * before the tool part is persisted — only crash-recovery re-spawns - * resolve the input from the transcript). */ + * before the tool part is persisted). Recovery reads saved job input, + * with transcript fallback for legacy jobs. Delivery is not awaited. */ runJob(jobId: string, input?: JobToolInput): Promise; } @@ -270,14 +271,15 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner { } } - /** Resolve the job's tool input: dispatch passes it directly; only a - * crash-recovery re-spawn (no input) falls back to scanning the - * transcript (by then onFinish has long persisted the part). */ + /** Dispatch input wins, then the job's saved arguments. Legacy jobs + * without saved input fall back to the durable transcript, which may + * lack a call if the dispatching turn died before settlement. */ function resolveInput( job: JobRecord, input: JobToolInput | undefined, ): JobToolInput | undefined { if (input) return input; + if (job.input !== undefined) return job.input as JobToolInput; const chat = chatStore.getChat(job.chatId); if (!chat) return undefined; // Wave D: the record is native history — the scan reads the native @@ -295,27 +297,28 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner { } /** - * The tail of every job: store the result, build the steer, hand it to - * the wake pipeline. The pipeline flips the status when the lesson has - * actually resumed — until then the job reads "running" so the client's - * activity card keeps polling and re-syncs once, at the end. + * Save the computation outcome and session delivery obligation together. + * Worker liveness ends after cleanup, not after a lesson consumes the result. */ async function finishJob(jobId: string, result: ResearchResult | DemoResult | VisualResult | PdfResult): Promise { const job = jobStore.getJob(jobId); if (!job) return; - // Aborted mid-run or mid-delivery (abortJob flips the record before + // Aborted mid-run (abortJob flips the record before // the loop unwinds): no result write, no steer — the abort path owns // the record and queues the abort steer itself. if (job.status !== "running") return; - jobStore.updateJob(jobId, { result, activity: "delivering result" }); - const steer = buildSubagentSteer(job, result); - try { - await deliver(steer); - } catch (err) { - const reason = err instanceof Error ? err.message : String(err); - console.error(`[job ${jobId}] steer delivery failed:`, err); - jobStore.updateJob(jobId, { status: "error", error: reason, activity: "delivery failed" }); - } + const failed = isSubagentFailure(job.kind, result); + jobStore.updateJob(jobId, { + result, + status: failed ? "error" : "done", + activity: failed ? "failed" : "finished", + delivery: { kind: "result", status: "pending" }, + }); + // The saved result now belongs to the session. Do not keep the worker, + // abort registration, or temporary resources alive behind a question. + void deliver(buildSubagentSteer(job, result)).catch((err: unknown) => { + console.error(`[job ${jobId}] pending delivery:`, err); + }); } /** Chat-record + input resolution shared by all three runners. */ @@ -343,7 +346,12 @@ export function createJobRunner(deps: JobRunnerDeps): JobRunner { const { job } = ctx; const input = (passedInput ?? resolveInput(job, passedInput)) as ResearchToolInput | undefined; - const question = typeof input?.question === "string" && input.question.trim() ? input.question : "(no question)"; + if (!(typeof input?.question === "string" && input.question.trim())) { + jobStore.updateJob(jobId, { error: "research question unavailable" }); + await finishJob(jobId, { brief: "Research failed: the research question is unavailable.", sources: [] }); + return; + } + const question = input.question; const context = typeof input?.context === "string" && input.context.trim() ? input.context : undefined; const prompt = [ diff --git a/src/agent/tools/jobs.ts b/src/agent/tools/jobs.ts index 00a78ca..e3ed6a5 100644 --- a/src/agent/tools/jobs.ts +++ b/src/agent/tools/jobs.ts @@ -78,7 +78,7 @@ function dispatch( input: ResearchInput | DemoInput | VisualInput | PdfInput, ): string { const existing = ctx.jobStore.findJobByToolCallId(ctx.chatId, toolCallId); - const job = existing ?? ctx.jobStore.createJob({ kind, chatId: ctx.chatId, toolCallId }); + const job = existing ?? ctx.jobStore.createJob({ kind, chatId: ctx.chatId, toolCallId, input }); if (!existing) ctx.spawn(job, input); return `Dispatched (job ${job.id}). ${ack}`; } diff --git a/src/core/contracts/jobs.ts b/src/core/contracts/jobs.ts index 1b14519..144e974 100644 --- a/src/core/contracts/jobs.ts +++ b/src/core/contracts/jobs.ts @@ -21,6 +21,12 @@ export interface JobRecord { chatId: string; /** The tool part this job fulfills. */ toolCallId: string; + /** Dispatch-time arguments, saved before spawn. Older jobs resolve from history. */ + input?: unknown; + /** Session-owned delivery, independent of the worker's terminal outcome. + * Absent on legacy records: old terminal jobs must never be replayed. */ + delivery?: { kind: "result" | "abort"; status: "pending" | "delivered" }; + /** Worker computation only; a pending delivery does not keep it running. */ status: JobStatus; /** Latest human-readable activity line, e.g. `web_search: "sigma.js v3"`. */ activity: string; @@ -148,42 +154,32 @@ export function accumulateJobUsage( * sourced briefs) — tracking a live activity line in the job record. * ONE central client poll of GET /api/jobs?chatId=… (in Chat) drives every * job surface: the transcript dispatch cards (live activity by - * toolCallId, quiet one-liner after delivery), the sub-agents sidebar + * toolCallId, quiet one-liner after worker completion), the sub-agents sidebar * panel, and the single re-sync when a job settles. * - * 3. On completion the runner builds a STEER MESSAGE (buildSubagentSteer) - * and hands it to the wake pipeline (src/server/jobs.ts): a user-role - * result-carrying native marker — the content is what the MODEL needs - * (the research brief, the builder's notes), plus a data-subagent-artifact - * part with what the UI renders (the full result: brief + sources / - * SVG / demo HTML). Provider conversion drops data parts, so artifact - * payloads never enter the teacher's context. metadata.subagentResult - * identifies the job; the UI renders these as sub-agent result cards, - * NEVER as learner interjections. + * 3. Dispatch saves input on the job before spawn. On completion the + * runner saves result + terminal worker status + pending delivery in + * one job update, then queues buildSubagentSteer WITHOUT waiting for + * delivery. The native custom marker's content is model-facing text; + * details carry the full artifact payload for the UI. Worker resources + * are released even if a question keeps the result pending indefinitely. + * Only computing workers appear in Background agents or accept abort. * - * 4. The wake pipeline delivers the steer by firing a SERVER-SIDE teacher - * turn through the same turn composition as a client request - * (handleChatRequest) — write-ahead persist, turn liveness, extraction - * trigger: one code path. A per-chat serialized queue: a steer waits - * while a turn is in flight OR a question tool part is pending (an - * unanswered client-side tool call breaks provider conversion — the - * steer fires right after the learner's continuation), then fires; a 409 - * (a client turn raced in) retries. Delivery is idempotent per jobId, so - * a crashed wake can be re-entered by the stale-job recovery sweep. The - * job's status flips to done/error only AFTER the wake turn completes — - * a polling client that sees the flip re-syncs once and gets the - * finished record. + * 4. Session delivery owns one registry entry per deterministic steer ID, + * including while a wake is in flight or a 409/403 returns its cargo. + * A live agent takes it via pi steering; registration restores saved + * pending jobs before the answer run's first poll. Otherwise the idle + * wake uses the normal turn composition and respects pending questions, + * dismissal, and background consent. A saved transcript marker completes + * delivery; HTTP 200 or streamed visibility alone does not. * - * CONTINUATION COALESCING: the idle window above can starve — a - * quiz-chaining teacher ends every turn with a new pending check, so - * the gates never stay open long enough for a wake to fire. The fix: - * when a client turn wins the turn registry, the handler CLAIMS every - * queued, undelivered steer (jobs.ts claimSteers) and appends it after - * the learner's answer in that very turn — conversion-safe, because the - * answered quiz carries its output. Every answer flushes pending - * steers immediately; the wake pump remains for turns that end with no - * pending question. The pump re-checks steerDelivered right before - * firing, so a claim racing the pump converges to exactly one delivery. + * Failed starts remain quietly pending until a learner run or restart + * recovery, without new timers, visible delivery errors, or worker-status + * changes. Existing 409 backoff and consent deferral remain. Accepted + * turns are never automatically replayed. Legacy running records with a + * result become finished/pending; legacy terminal records with no + * delivery metadata remain untouched. Session deletion discards pending + * deliveries rather than treating them as running work. * * 5. No pending job parts ever exist, so the composer never gates on jobs * (the learner can interject mid-job; the steer queues behind their diff --git a/src/core/ports.ts b/src/core/ports.ts index ea67d93..b05d946 100644 --- a/src/core/ports.ts +++ b/src/core/ports.ts @@ -136,6 +136,8 @@ export interface CreateJobInit { toolCallId: string; /** First activity line (defaults to "starting"). */ activity?: string; + /** Saved on the job before its worker is spawned. */ + input?: unknown; } /** Mergeable fields — identity (id, kind, chatId, toolCallId, createdAt) is immutable. */ diff --git a/src/server/chat-handler.ts b/src/server/chat-handler.ts index 56132e9..74ed3c8 100644 --- a/src/server/chat-handler.ts +++ b/src/server/chat-handler.ts @@ -17,7 +17,7 @@ import { createExtractionPipeline } from "../agent/subagents/extraction.ts"; import type { ModelRole } from "../../config.ts"; import { deliverJobResult, - noteSteerVisible, + noteSteersDurable, reconcileUserSteers, registerLiveAgent, restoreUncommittedAutomatic, @@ -58,7 +58,7 @@ import { type TurnInput, } from "../core/input-events.ts"; import { steerDelivered } from "../core/contracts.ts"; -import type {CallUsage, SteerMessage} from "../core/contracts.ts"; +import type {CallUsage} from "../core/contracts.ts"; /** * One chat turn over the server-owned native record. Framework-free (plain @@ -259,7 +259,8 @@ export async function handleChatInput( chatId, ); const compactedRecord = stores.chatStore.getChat(chatId); - if (compactedRecord) record = compactedRecord; + if (!compactedRecord) return Response.json({ error: "Unknown chat" }, { status: 404 }); + record = compactedRecord; // The input's native appends (pure; the honest 400 for an answer with no // pending question call). Computed BEFORE beginTurn so a refused input @@ -329,9 +330,7 @@ export async function handleChatInput( // Visibility bookkeeping is not the canonical save. Its failure must // neither misreport a saved input as lost nor prevent the turn running. try { - for (const appended of stagedAutomatic ? [] : appends) { - if ((appended as { role?: string }).role === "custom") noteSteerVisible(appended as SteerMessage); - } + if (!stagedAutomatic) noteSteersDurable(chatId, merged); } catch (err) { console.error(`[chat ${chatId}] input visibility bookkeeping failed:`, err); } @@ -383,7 +382,11 @@ export async function handleChatInput( foregroundOwed: !callPurpose.background, purpose: callPurpose, scopeCurrent: (scope) => connectionScopeCurrent(conversation.provider, scope), - commitStaged: () => { stores.chatStore.saveMessages(chatId, merged); persistBaseCount = merged.length; }, + commitStaged: () => { + stores.chatStore.saveMessages(chatId, merged); + persistBaseCount = merged.length; + noteSteersDurable(chatId, merged); + }, restoreAutomatic: (automaticIds, retainedIds) => restoreUncommittedAutomatic(chatId, constructed, automaticIds, retainedIds), }); const agentOpts: AgentBuildOptions = { @@ -481,7 +484,7 @@ export async function handleChatInput( // run's first LLM call (the native replacement for continuation // coalescing). Settlement still cannot beat the flow below: it needs // a streamed response, which never happens inside the constructor. - registerLiveAgent(chatId, constructed); + registerLiveAgent(chatId, constructed, input.type !== "steers" && input.type !== "continue"); // Debug recorder run marker (owner, 2026-10-04): correlates the // JSONL event stream with the record's turns; no-op unless // LH_PROVIDER_EVENTS=1. @@ -522,12 +525,13 @@ export async function handleChatInput( // merely because usage/extraction/other settlement work failed. const persisted = stores.chatStore.getChat(chatId)?.messages ?? []; reconcileUserSteers(chatId, persisted); - settleSteers(chatId, persisted); + settleSteers(chatId, persisted, true); }, onFinish: (turnMessages, isAborted) => { // Wave B: the record write is the agent's own final state — // the new messages are the slice past the merged base. const stamped = persistTurn(stores.chatStore, chatId, persistBaseCount, turnMessages, isAborted); + noteSteersDurable(chatId, stores.chatStore.getChat(chatId)?.messages ?? stamped); // usage log append (idempotent per turn id) — the stats bar // refolds this on the client after the turn settles. The // provenance message is the turn's FIRST new assistant message. diff --git a/src/server/chats.ts b/src/server/chats.ts index b1976ca..2e6f2c9 100644 --- a/src/server/chats.ts +++ b/src/server/chats.ts @@ -6,6 +6,7 @@ import { graphRevision } from "./graph.ts"; import { stores } from "./stores.ts"; import { isTurnRunning } from "./turn-liveness.ts"; import { clearTurnFailure } from "./turn-runs.ts"; +import { discardChatDeliveries } from "./jobs.ts"; /** * Server-side chat entries for the transport layer (app/api/chats*, @@ -66,10 +67,7 @@ export type ChatDeleteOutcome = * the wind-down finds the chat gone at its merge and aborts with * nothing merged. */ -export async function deleteChatSession(chatId: string, mode: ChatDeleteMode): Promise { - if (!stores.chatStore.getChat(chatId)) { - return { ok: false, status: 404, code: "not_found", error: "No such session" }; - } +function runningWorkBlocker(chatId: string): Extract | null { if (isTurnRunning(chatId)) { return { ok: false, @@ -89,6 +87,15 @@ export async function deleteChatSession(chatId: string, mode: ChatDeleteMode): P jobIds: runningJobs.map((job) => job.id), }; } + return null; +} + +export async function deleteChatSession(chatId: string, mode: ChatDeleteMode): Promise { + if (!stores.chatStore.getChat(chatId)) { + return { ok: false, status: 404, code: "not_found", error: "No such session" }; + } + const blocker = runningWorkBlocker(chatId); + if (blocker) return blocker; if (extractionStatuses().some((run) => run.chatId === chatId)) { abortExtraction(chatId); // true cancellation — spend stops, nothing merges const deadline = Date.now() + WIND_DOWN_TIMEOUT_MS; @@ -105,11 +112,19 @@ export async function deleteChatSession(chatId: string, mode: ChatDeleteMode): P } } + // A result wake or a worker may have started during extraction wind-down. + // Recheck immediately before the synchronous purge, with no await gap. + const lateBlocker = runningWorkBlocker(chatId); + if (lateBlocker) return lateBlocker; + // THE SYNC BLOCK — no awaits below (see the doc comment). Order: // ledgers → jobs → the chat record. const stats = deleteChatTraces(stores.graphStore, chatId, mode); const jobsRemoved = stores.jobStore.deleteJobsForChat(chatId); - if (stores.chatStore.deleteChat(chatId)) clearTurnFailure(chatId); + if (stores.chatStore.deleteChat(chatId)) { + discardChatDeliveries(chatId); + clearTurnFailure(chatId); + } return { ok: true, mode, stats, jobsRemoved, graphRevision: graphRevision() }; } diff --git a/src/server/jobs.ts b/src/server/jobs.ts index 7424d41..98becef 100644 --- a/src/server/jobs.ts +++ b/src/server/jobs.ts @@ -1,7 +1,7 @@ /** * THE WAKE PIPELINE (contracts.ts "THE ASYNC PROTOCOL" §4) — server-side * steer delivery for finished sub-agent jobs. Wave C (2026-09-26): - * delivery rides pi's OWN follow-up queue when a run is live, and the + * delivery rides pi's OWN steering queue when a run is live, and the * idle wake otherwise. One registry, two vehicles. * * deliverJobResult(steer) — the runner calls this on job completion. @@ -40,15 +40,17 @@ * * settleSteers(chatId, persisted) — the handler calls this at turn * settlement (after unregisterLiveAgent): entries the run DELIVERED - * resolve + flip their jobs; entries that were handed to the dying + * resolve + acknowledge delivery; entries handed to the dying * run but never injected (stopper exit, abort, a missed final poll) * return to pending and the pump restarts — no stranding, no loss. * - * Crash recovery (recoverStaleJobs): a process restart can orphan jobs — - * "running" with a result (the wake never fired) or without one (the - * sub-agent loop itself died). The sweep re-delivers finished results - * (idempotently) and re-spawns unfinished loops, both via injected - * seams. It runs lazily off the /api/jobs poll, throttled. + * Worker status ends at computation completion; JobRecord.delivery owns + * the pending result or abort notice independently. Registry entries + * survive in-flight wakes, so duplicate submissions share one promise. + * Failed starts stay pending without timers or worker errors. Recovery + * restores known pending deliveries and re-spawns unfinished workers; + * registration also restores pending delivery before the first poll. + * Legacy terminal jobs without delivery metadata are never replayed. * * All mutable state lives on globalThis (the turn-liveness lesson: Next dev * gives each route entry its own module instances — a module-level Map would @@ -83,6 +85,11 @@ export type JobRespawn = (jobId: string) => void; interface QueuedSteer { steer: SteerMessage; + promise: Promise; + /** Wake cargo remains owned here, but must not steer into its own run. */ + waking?: boolean; + /** A failed start waits for a learner run or process restart, not a timer. */ + deferred?: boolean; resolve: () => void; reject: (reason: Error) => void; /** The live agent this marker was steered into (agent.steer) — set by @@ -166,18 +173,37 @@ function jobIdOf(steer: SteerMessage): string | null { return typeof details?.jobId === "string" ? details.jobId : null; } -/** Dock bookkeeping: the marker reached the learner's view — flip its job - * NOW instead of waiting for the delivering turn to settle. Called from - * BOTH visibility seams: the marker's message_end wire event (live steer - * injection — the card is on screen while the teacher keeps teaching) and - * the write-ahead persist in handleChatInput (the wake's input appends — - * the card is durable in the record). The registry's delivery promise - * still settles via settleSteers / the runner-200 path; finalizeJob is - * idempotent, so the later flip is a no-op. Owner ruling (2026-09-26): a - * delivered subagent leaves the background dock the moment its result is - * visible, not when the teacher's turn ends. */ -export function noteSteerVisible(steer: SteerMessage): void { - finalizeSteer(steer); +/** Call only after canonical conversation persistence. Streaming visibility + * is not durable acceptance. The saved marker wins over delivery-bookkeeping + * failures; acknowledging delivery never changes the worker's outcome. */ +export function noteSteersDurable(chatId: string, persisted: readonly LhMessage[]): void { + for (const message of persisted) { + if (message.role === "custom") finalizeSteer(message); + } + for (const entry of [...(state().queues.get(chatId) ?? [])]) { + if (persisted.some((message) => message.id === entry.steer.id)) completeEntry(chatId, entry); + } +} + +function removeEntry(chatId: string, entry: QueuedSteer): void { + const remaining = (state().queues.get(chatId) ?? []).filter((candidate) => candidate !== entry); + if (remaining.length > 0) state().queues.set(chatId, remaining); + else state().queues.delete(chatId); +} + +function completeEntry(chatId: string, entry: QueuedSteer): void { + finalizeSteer(entry.steer); + removeEntry(chatId, entry); + entry.resolve(); +} + +/** Deletion discards the session's obligations, never wakes a deleted chat. */ +export function discardChatDeliveries(chatId: string): void { + const s = state(); + const queue = s.queues.get(chatId) ?? []; + s.queues.delete(chatId); + s.userSteers.delete(chatId); + for (const entry of queue) entry.resolve(); } /** Finalize a delivered steer's job bookkeeping — job steers only. */ @@ -186,24 +212,30 @@ function finalizeSteer(steer: SteerMessage): void { if (jobId) finalizeJob(jobId); } -/** Fail a steer's job on unrecoverable delivery failure — job steers only. */ -function failSteer(steer: SteerMessage, err: Error): void { - const jobId = jobIdOf(steer); - if (jobId) failJob(jobId, err); +/** Queue one logical delivery per ID, including in-flight wake cargo. + * Job promises resolve on durable marker acceptance (or session deletion); + * failed starts stay pending. Non-job probe handoffs keep their existing + * wake-acceptance/error semantics. Workers never await this promise. */ +export function deliverJobResult(steer: SteerMessage): Promise { + const chatId = chatIdOf(steer); + const entry = queueSteer(chatId, steer); + void pump(chatId); + return entry.promise; } -/** Queue ANY steer (result, abort, probe handoff) through the pipeline. - * Resolves when the wake turn has completed; rejects when delivery fails - * unrecoverably. Never rejects for queueing itself. */ -export function deliverJobResult(steer: SteerMessage): Promise { - return new Promise((resolve, reject) => { - const s = state(); - const chatId = chatIdOf(steer); - const queue = s.queues.get(chatId) ?? []; - queue.push({ steer, resolve, reject }); - s.queues.set(chatId, queue); - void pump(chatId); - }); +function queueSteer(chatId: string, steer: SteerMessage): QueuedSteer { + const queue = state().queues.get(chatId) ?? []; + const existing = queue.find((entry) => entry.steer.id === steer.id); + if (existing) return existing; + let resolve!: () => void; + let reject!: (error: Error) => void; + const promise = new Promise((res, rej) => { resolve = res; reject = rej; }); + // Recovery/registration may own an entry with no waiting caller. + void promise.catch(() => undefined); + const entry: QueuedSteer = { steer, promise, resolve, reject }; + queue.push(entry); + state().queues.set(chatId, queue); + return entry; } /** Hand every eligible pending marker to a live agent's steering queue @@ -227,7 +259,7 @@ function handOffEligible(chatId: string, agent: Agent): void { // remain deliverable, including the first poll of an answer run. if (automaticFollowUpAllowed(chatId, agent) && queue && queue.length > 0) { for (const entry of queue) { - if (entry.handedTo !== agent && entry.steer.customType !== "probe-handoff") { + if (!entry.waking && !entry.deferred && entry.handedTo !== agent && entry.steer.customType !== "probe-handoff") { agent.steer(entry.steer); entry.handedTo = agent; } @@ -254,8 +286,10 @@ function handOffEligible(chatId: string, agent: Agent): void { * markers are steered into the run right here. The settle seam cannot * beat the registration: settlement needs a streamed response, which * never happens inside the constructor call. */ -export function registerLiveAgent(chatId: string, agent: Agent): void { +export function registerLiveAgent(chatId: string, agent: Agent, allowDeferred = true): void { state().liveAgents.set(chatId, agent); + restorePendingDeliveries(chatId); + if (allowDeferred) for (const entry of state().queues.get(chatId) ?? []) entry.deferred = false; handOffEligible(chatId, agent); } @@ -326,7 +360,7 @@ export function restoreUncommittedAutomatic(chatId: string, agent: Agent, automa * unregisterLiveAgent, with the run's persisted messages. Two jobs: * * - entries whose marker the run DELIVERED (in-run follow-up injection — - * their ids are in the persisted slice) resolve and flip their jobs; + * their ids are in the persisted slice) resolve and acknowledge delivery; * - entries handed to the dying run but never injected (the question * stopper exited before the follow-up poll; an abort; a delivery that * raced the final poll) return to PENDING (handedTo cleared) and the @@ -335,7 +369,7 @@ export function restoreUncommittedAutomatic(chatId: string, agent: Agent, automa * This is what makes the live hand-off safe: pi owns no retry for a marker * stranded in a settled agent's queue, so the registry stays the truth and * the dead agent's copy is simply abandoned (GC'd with the agent). */ -export function settleSteers(chatId: string, persisted: { id?: string }[]): void { +export function settleSteers(chatId: string, persisted: { id?: string }[], failed = false): void { const queue = state().queues.get(chatId); if (!queue || queue.length === 0) return; const deliveredIds = new Set(persisted.map((m) => m.id).filter(Boolean)); @@ -346,6 +380,8 @@ export function settleSteers(chatId: string, persisted: { id?: string }[]): void entry.resolve(); } else { entry.handedTo = undefined; + entry.waking = false; + if (failed && jobIdOf(entry.steer)) entry.deferred = true; remaining.push(entry); } } @@ -373,16 +409,10 @@ export interface AbortOutcome { * runner registered it at loop start) — the in-flight model call * aborts, not just the next loop check. The runner unwinds silently * (its catch checks abortSignal.aborted; finishJob checks status). - * 2. DROP any queued result steer for this job (the delivering-result - * phase): resolve its entry without firing the wake. If the wake - * turn is ALREADY in flight (the pump shifted the entry), it's too - * late — the result steer lands, and the cancellation steer dedupes - * against it via steerDelivered (same jobId metadata). - * 3. FLIP the record to error "Aborted by learner" immediately — the - * poll reflects the user's action at once (the user chose - * error-with-reason over a distinct cancelled status). finalizeJob - * never overwrites a non-running status, so the flip is terminal no - * matter what the wake does. + * 2. REFUSE if computation already finished. Queued results belong to + * the session, not to an abortable background worker. + * 3. SAVE error "Aborted by learner" and a pending abort delivery together. + * Delivery acknowledgement never overwrites the worker's outcome. * 4. STEER the teacher: a one-line abort note (buildAbortSteer) through * the same wake pipeline, so the teacher's promise of a result * doesn't hang silently in the lesson. @@ -394,28 +424,18 @@ export function abortJob(jobId: string): AbortOutcome { const job = stores.jobStore.getJob(jobId); if (!job) return { ok: false, reason: "not_found" }; if (job.status !== "running") return { ok: false, reason: "not_running" }; - - fireJobAbort(jobId); // no-op when the loop already returned (delivering) - - // Drop any queued steers for this job (their deliver() resolves unfired). - // HANDED entries (already in a live run's follow-up queue) are past - // recall — same ruling as a fired wake: the result lands, and the abort - // note below dedupes against it via steerDelivered (same jobId). - const queue = state().queues.get(job.chatId); - if (queue) { - const remaining = queue.filter( - (e) => jobIdOf(e.steer) !== jobId || e.handedTo !== undefined, - ); - if (remaining.length === 0) state().queues.delete(job.chatId); - else state().queues.set(job.chatId, remaining); - for (const entry of queue) { - if (jobIdOf(entry.steer) === jobId && entry.handedTo === undefined) entry.resolve(); - } + if (job.result) { + normalizeLegacyResult(job); + return { ok: false, reason: "not_running" }; } - stores.jobStore.updateJob(jobId, { status: "error", error: "Aborted by learner", activity: "aborted" }); + fireJobAbort(jobId); + stores.jobStore.updateJob(jobId, { + status: "error", error: "Aborted by learner", activity: "aborted", + delivery: { kind: "abort", status: "pending" }, + }); void deliverJobResult(buildAbortSteer(job)).catch(() => { - /* deliverJobResult marks the job error on unrecoverable failure */ + /* The saved pending abort notice remains owned by the session. */ }); return { ok: true }; } @@ -470,18 +490,9 @@ async function pump(chatId: string): Promise { // Idempotent delivery: the transcript already carries these steers // (an in-run injection persisted, a wake completed, or a duplicate // deliver raced in). Resolve and flip without re-firing anything. - const stillPending = queue.filter((e) => !steerDelivered(record.messages, e.steer.id)); - for (const entry of queue) { - if (!stillPending.includes(entry)) { - finalizeSteer(entry.steer); - entry.resolve(); - } - } - if (stillPending.length === 0) { - s.queues.delete(chatId); - return; - } - s.queues.set(chatId, stillPending); + noteSteersDurable(chatId, record.messages); + const stillPending = (s.queues.get(chatId) ?? []).filter((entry) => !entry.waking && !entry.deferred); + if (stillPending.length === 0) { parked = true; return; } if (!automaticFollowUpAllowed(chatId, s.liveAgents.get(chatId))) { parked = true; return; } // VEHICLE 1 — THE LIVE RUN: pi's steering queue. The agent of the @@ -526,28 +537,17 @@ async function pump(chatId: string): Promise { // The runner is required — chat-handler sets it at init. If some // module instance missed the wiring, fail loudly rather than hang. if (!s.turnRunner) { - s.queues.delete(chatId); const err = new Error("wake pipeline: no turn runner wired (setTurnRunner)"); - for (const entry of stillPending) { - failSteer(entry.steer, err); - entry.reject(err); - } + for (const entry of stillPending) deferOrReject(chatId, entry, err); + parked = true; return; } // A live run may have delivered some markers while the gates were // closed — re-check: only still-missing markers ride the wake. - const fire = stillPending.filter((e) => !steerDelivered(fresh.messages, e.steer.id)); - for (const entry of stillPending) { - if (!fire.includes(entry)) { - finalizeSteer(entry.steer); - entry.resolve(); - } - } - if (fire.length === 0) { - s.queues.delete(chatId); - return; - } + noteSteersDurable(chatId, fresh.messages); + const fire = (s.queues.get(chatId) ?? []).filter((entry) => !entry.waking && !entry.deferred && !entry.handedTo); + if (fire.length === 0) return; // THE DISMISSAL PAUSE (owner design 2026-10-03, "A2"): the record // ends in a user dismissal — the session waits for the USER. Hold @@ -561,50 +561,37 @@ async function pump(chatId: string): Promise { return; } - // DEQUEUE-AT-FIRE (the Wave C lesson from the Wave B regression): - // the carried markers leave the registry BEFORE the turn runs — - // the wake run's OWN follow-up poll must never see its own cargo, - // and a 409 (client raced in) must be able to push them back. - s.queues.delete(chatId); + // Keep ownership through the wake. The flag excludes cargo from + // its own live-agent handoff while duplicate callers share it. + for (const entry of fire) entry.waking = true; const steers = fire.map((e) => e.steer); - // Fire the wake (every marker as the turn's input appends). A 409 = - // a client turn raced in between idle and firing — push the cargo - // back and retry after backoff (the racing turn's own settlement - // re-pumps if it beats us to the re-queue). + // Fire the wake with cargo still registered. A 409 releases its + // vehicle flag and retries after the existing backoff, without + // appending another registry entry. const status = await s.turnRunner(chatId, { type: "steers", steers }).catch((err: unknown) => { console.error(`[steer ${steers.map((x) => x.id).join(",")}] wake turn crashed:`, err); return -1; }); - if (status === 403) { - // Consent may have changed after the last gate. Preserve completed - // job/result ownership and wait for an explicit connection change. - const back = s.queues.get(chatId) ?? []; - back.push(...fire); - s.queues.set(chatId, back); - parked = true; - return; - } - if (status === 409) { - const back = s.queues.get(chatId) ?? []; - back.push(...fire); - s.queues.set(chatId, back); + const saved = stores.chatStore.getChat(chatId); + if (saved) noteSteersDurable(chatId, saved.messages); + const owned = fire.filter((entry) => s.queues.get(chatId)?.includes(entry)); + if (status === 403 || status === 409) { + for (const entry of owned) entry.waking = false; + if (status === 403) { parked = true; return; } await delay(RETRY_BACKOFF_MS[Math.min(attempt++, RETRY_BACKOFF_MS.length - 1)]!); continue; } attempt = 0; - - if (status === 200) { - for (const entry of fire) { - finalizeSteer(entry.steer); - entry.resolve(); - } - } else { - const err = new Error(`wake turn failed with status ${status}`); - for (const entry of fire) { - failSteer(entry.steer, err); - entry.reject(err); + for (const entry of owned) { + if (status === 200 && !jobIdOf(entry.steer)) { + // Preserve the non-job probe handoff's acceptance semantics. + completeEntry(chatId, entry); + } else if (status !== 200 || !isTurnRunning(chatId)) { + deferOrReject(chatId, entry, new Error(`wake turn did not persist steer (status ${status})`)); } + // An accepted live turn still owns its cargo until canonical save + // or settlement. HTTP 200 alone is not a durable marker receipt. } } } finally { @@ -614,7 +601,7 @@ async function pump(chatId: string): Promise { // a live run are NOT stragglers (settleSteers owns their resurrection) // — restarting for them would just park in waitForIdle forever. const q = state().queues.get(chatId) ?? []; - if (!parked && automaticFollowUpAllowed(chatId, state().liveAgents.get(chatId)) && q.some((e) => e.handedTo === undefined)) void pump(chatId); + if (!parked && automaticFollowUpAllowed(chatId, state().liveAgents.get(chatId)) && q.some((e) => e.handedTo === undefined && !e.waking && !e.deferred)) void pump(chatId); } } @@ -638,25 +625,55 @@ state().stopConnectionWatch = onSubscriptionChange(() => { } }); -/** Flip the job to its final status now that the lesson has resumed. */ -function finalizeJob(jobId: string): void { - const job = stores.jobStore.getJob(jobId); - if (!job || job.status !== "running") return; - const result = job.result; - const failed = result ? isSubagentFailure(job.kind, result as never) : true; - stores.jobStore.updateJob(jobId, { - status: failed ? "error" : "done", - activity: failed ? "failed" : "delivered", - }); +function deferOrReject(chatId: string, entry: QueuedSteer, error: Error): void { + entry.waking = false; + if (jobIdOf(entry.steer)) { + entry.deferred = true; + console.error(`[steer ${entry.steer.id}] delivery remains pending:`, error.message); + } else { + removeEntry(chatId, entry); + entry.reject(error); + } } -function failJob(jobId: string, err: Error): void { - // An aborted job stays aborted even when its (abort) steer - // delivery fails — the user's action is the terminal fact, not the - // delivery error. Non-running records are never overwritten here. - const job = stores.jobStore.getJob(jobId); - if (!job || job.status !== "running") return; - stores.jobStore.updateJob(jobId, { status: "error", error: err.message, activity: "delivery failed" }); +/** Acknowledge session delivery without changing the worker outcome. */ +function finalizeJob(jobId: string): void { + try { + let job = stores.jobStore.getJob(jobId); + if (!job) return; + if (!job.delivery) job = normalizeLegacyResult(job); + if (job.delivery?.status !== "pending") return; + stores.jobStore.updateJob(jobId, { delivery: { ...job.delivery, status: "delivered" } }); + } catch (error) { + // The conversation is already durable. A bookkeeping failure must + // never replay that marker or turn a successful worker into a failure. + console.error(`[job ${jobId}] delivery acknowledgement failed:`, error); + } +} + +function normalizeLegacyResult(job: JobRecord): JobRecord { + if (job.delivery || job.status !== "running" || !job.result) return job; + const failed = isSubagentFailure(job.kind, job.result as never); + return stores.jobStore.updateJob(job.id, { + status: failed ? "error" : "done", activity: failed ? "failed" : "finished", + delivery: { kind: "result", status: "pending" }, + }) ?? job; +} + +/** Restore known obligations only; old terminal records are never replayed. */ +function restorePendingDeliveries(chatId: string): void { + const record = stores.chatStore.getChat(chatId); + if (!record) return; + noteSteersDurable(chatId, record.messages); + for (const stored of stores.jobStore.listJobs(chatId)) { + const job = isJobLive(stored.id) ? stored : normalizeLegacyResult(stored); + if (job.delivery?.status !== "pending") continue; + const steer = job.delivery.kind === "abort" ? buildAbortSteer(job) + : job.result ? buildSubagentSteer(job, job.result as never) : undefined; + if (!steer) continue; + if (steerDelivered(record.messages, steer.id)) finalizeSteer(steer); + else queueSteer(chatId, steer); + } } /** @@ -669,7 +686,7 @@ function failJob(jobId: string, err: Error): void { async function waitForIdle(chatId: string): Promise { for (;;) { const record = stores.chatStore.getChat(chatId); - if (!record) return; + if (!record || !(state().queues.get(chatId) ?? []).some((entry) => !entry.waking && !entry.deferred && !entry.handedTo)) return; const blocked = isTurnRunning(chatId) || danglingQuestionOf(record.messages) !== null; if (!blocked) { await delay(GRACE_MS); @@ -699,9 +716,9 @@ const SWEEP_THROTTLE_MS = 30_000; * hung-but-alive loops beside themselves — the figure-maker incident, * 2026-09-18). A record WITH a live registration is provably executing * in THIS process — never touched, however old: a hung first model call - * (zero usage ever billed) stays honestly "running" in the dock until - * END kills it. The abort registration spans loop start to delivery - * completion, so a live registration can never belong to a dead run. + * stays honestly running until END kills it. Terminal workers with known + * pending delivery are restored independently; old terminal records without + * delivery metadata are never inferred to owe a delivery. */ /** Re-run a job's sub-agent loop — exposed for the sweep's tests (the * /api/jobs poll is its only production caller, disabled under NODE_ENV=test). */ @@ -713,18 +730,14 @@ export function recoverStaleJobs(): void { try { for (const chat of stores.chatStore.listChats()) { for (const job of stores.jobStore.listJobs(chat.id)) { - if (job.status !== "running") continue; - if (isJobLive(job.id)) continue; // provably executing here — never re-run beside it + if (isJobLive(job.id)) continue; // never re-run beside live computation if (!stores.chatStore.getChat(job.chatId)) continue; - if (isTurnRunning(job.chatId)) continue; // a wake turn may be delivering it right now - if (job.result) { - // The wake never fired (or crashed mid-flight): re-deliver. - const steer = buildSubagentSteer(job, job.result as never); - stores.jobStore.updateJob(job.id, { activity: "recovering: re-delivering" }); - void deliverJobResult(steer).catch(() => { - /* deliverJobResult marks the job error on failure */ - }); - } else { + if (isTurnRunning(job.chatId)) continue; + const recovered = normalizeLegacyResult(job); + if (recovered.delivery?.status === "pending") { + restorePendingDeliveries(job.chatId); + void pump(job.chatId); + } else if (job.status === "running" && !job.result) { // The sub-agent loop itself died with the process: re-spawn. if (!s.respawn) continue; stores.jobStore.updateJob(job.id, { activity: "recovering: re-running" }); diff --git a/src/server/pi/ui-stream.ts b/src/server/pi/ui-stream.ts index 2619ac4..40a831d 100644 --- a/src/server/pi/ui-stream.ts +++ b/src/server/pi/ui-stream.ts @@ -1,6 +1,4 @@ import type { Agent, AgentEvent, AgentMessage } from "@earendil-works/pi-agent-core"; -import type { SteerMessage } from "../../core/contracts.ts"; -import { noteSteerVisible } from "../jobs.ts"; import { ToolCallIdUniquifier } from "./call-ids.ts"; import type { ChatGPTRequestCycle } from "../../agent/chatgpt-request-cycle.ts"; @@ -255,11 +253,6 @@ export function runPiTurn(spec: PiTurnSpec): PiTurnHandle { if (event.type === "message_end") { const m = event.message as { role?: string; stopReason?: string } | undefined; if (m?.role === "assistant" && m.stopReason === "aborted") sawAbort = true; - // Dock fix (owner ruling 2026-09-26): an injected steer marker's - // message_end = the card is visible to the learner — flip the - // job's bookkeeping now (the turn may keep teaching for minutes; - // settleSteers re-runs the flip as a no-op idempotently). - if (m?.role === "custom") noteSteerVisible(event.message as SteerMessage); } if (event.type === "agent_end") { // Persist/report before the terminal closes every view to new frames. diff --git a/src/store/jobs.ts b/src/store/jobs.ts index e8df50c..cea2da7 100644 --- a/src/store/jobs.ts +++ b/src/store/jobs.ts @@ -35,6 +35,7 @@ function createJob(init: CreateJobInit): JobRecord { kind: init.kind, chatId: init.chatId, toolCallId: init.toolCallId, + ...(init.input !== undefined ? { input: init.input } : {}), status: "running", activity: init.activity ?? "starting", createdAt: now, diff --git a/tests/chat-flow.test.ts b/tests/chat-flow.test.ts index 82ed82f..a1f3bca 100644 --- a/tests/chat-flow.test.ts +++ b/tests/chat-flow.test.ts @@ -522,7 +522,7 @@ describe("teacher chat flow (scripted model)", () => { const job = jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "call-mid" }); jobIdMid = job.id; // The runner stores the result BEFORE delivery (finalizeJob reads it). - jobStore.updateJob(job.id, { result: { brief: "Mid-run brief.", sources: [] } }); + jobStore.updateJob(job.id, { status: "done", result: { brief: "Mid-run brief.", sources: [] }, delivery: { kind: "result", status: "pending" } }); const steer = buildSubagentSteer(job, { brief: "Mid-run brief.", sources: [] }); const delivery = deliverJobResult(steer); await new Promise((r) => setTimeout(r, 300)); // vehicle 1 hands off @@ -534,7 +534,7 @@ describe("teacher chat flow (scripted model)", () => { release2(); await drain(await res); await delivery; // resolves at settleSteers (the run persisted the marker) - assert.equal(jobFlippedMidTurn, "done", "the job flipped at the marker's message_end — WHILE the turn still streamed"); + assert.equal(jobFlippedMidTurn, "done", "worker completion is independent of the streaming teacher turn"); // Record shape: the marker sits BETWEEN completed turn units — after // the toolResult it must never split, adjacent to it. diff --git a/tests/chatgpt-background.test.ts b/tests/chatgpt-background.test.ts index 7bdf602..02bf717 100644 --- a/tests/chatgpt-background.test.ts +++ b/tests/chatgpt-background.test.ts @@ -43,7 +43,7 @@ for (const revokedAfterQueue of [false, true]) { stores.chatStore.saveSettings(chat.id, { model: { provider: "chatgpt", id: "gpt-5" } }); const job = stores.jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "scratch-job-call" }); const result = { brief: "Scratch completed job result", sources: [] }; - stores.jobStore.updateJob(job.id, { status: "running", result }); + stores.jobStore.updateJob(job.id, { status: "done", result, delivery: { kind: "result", status: "pending" } }); const marker = buildSubagentSteer(job, result); const contexts: string[] = []; const native = piModels().getModel("openai", "gpt-5")!; @@ -83,7 +83,8 @@ for (const revokedAfterQueue of [false, true]) { unregisterLiveAgent(chat.id); endTurn(chat.id); settleSteers(chat.id, persisted); await new Promise((resolve) => setImmediate(resolve)); assert.equal(wakes, 0, "no background spin or spend while consent is absent"); - assert.equal(stores.jobStore.getJob(job.id)!.status, "running", "delivery ownership must not be finalized or failed by deferral"); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending", "delivery ownership must not be finalized or failed by deferral"); + assert.equal(stores.jobStore.getJob(job.id)!.status, "done", "worker completion is independent of consent deferral"); assert.deepEqual(stores.jobStore.getJob(job.id)!.result, result); connection(true); await delivered; diff --git a/tests/demos.test.tsx b/tests/demos.test.tsx index 8ce0b15..494f66b 100644 --- a/tests/demos.test.tsx +++ b/tests/demos.test.tsx @@ -274,14 +274,14 @@ describe("dispatch card rendering (the tool-call ack box)", () => { assert.match(html, /Dispatched…/); }); - it("settles to a quiet delivered one-liner when done (artifact lives in the steer)", () => { + it("settles to a quiet finished one-liner when the worker is done", () => { const html = renderToStaticMarkup( , ); assert.match(html, /Research agent/); - assert.match(html, /delivered/); + assert.match(html, /finished/); assert.ok(!html.includes("web_search"), "no stale activity line once settled"); }); @@ -325,7 +325,7 @@ describe("dispatch card rendering (the tool-call ack box)", () => { assert.match(html, /pdf_render/); }); - it("pdf dispatch settles to the delivered one-liner", () => { + it("pdf dispatch settles to the finished one-liner", () => { const pdfJob: JobRecord = ({ id: "jp1", kind: "pdf", @@ -342,7 +342,7 @@ describe("dispatch card rendering (the tool-call ack box)", () => { , ); assert.match(html, /PDF reader/); - assert.match(html, /delivered/); + assert.match(html, /finished/); }); }); @@ -848,7 +848,7 @@ describe("demo/visual job runners", () => { const done = jobStore.getJob(job.id)!; assert.equal(done.status, "done"); - assert.equal(done.activity, "delivered", "the fake deliver flips status like the wake pipeline"); + assert.equal(done.activity, "finished", "the worker finishes independently of delivery"); const result = done.result as { title: string; html: string; tested: boolean; notes?: string }; assert.equal(result.title, "Compound interest"); assert.equal(result.html, GOOD_HTML); diff --git a/tests/job-delivery-recovery.test.ts b/tests/job-delivery-recovery.test.ts new file mode 100644 index 0000000..25618c7 --- /dev/null +++ b/tests/job-delivery-recovery.test.ts @@ -0,0 +1,265 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; +import { after, afterEach, test } from "node:test"; +import { createFauxCore, fauxAssistantMessage } from "@earendil-works/pi-ai"; +import type { LhMessage } from "../src/core/native-messages.ts"; +import { buildSubagentSteer, type JobKind, type SteerMessage } from "../src/core/contracts.ts"; + +// Install isolation before importing any storage/configuration composition. +const directory = fs.mkdtempSync(path.join(os.tmpdir(), "lh-job-delivery-")); +process.env.LH_DATA_DIR = path.join(directory, "data"); +process.env.LH_AUTH_FILE = path.join(directory, "auth.json"); +fs.writeFileSync(process.env.LH_AUTH_FILE, "{}"); +const { stores } = await import("../src/server/stores.ts"); +const { jobTools } = await import("../src/agent/tools/jobs.ts"); +const { createJobRunner, researchModelOverride } = await import("../src/agent/subagents/runner.ts"); +const { isJobLive } = await import("../src/agent/subagents/aborts.ts"); +const { beginTurn, endTurn } = await import("../src/server/turn-liveness.ts"); +const { + deliverJobResult, discardChatDeliveries, noteSteersDurable, recoverStaleJobs, + registerLiveAgent, unregisterLiveAgent, settleSteers, setTurnRunner, +} = await import("../src/server/jobs.ts"); +const { activityFeed } = await import("../src/server/observability.ts"); +const { deleteChatSession } = await import("../src/server/chats.ts"); +const chats: string[] = []; +const result = { brief: "Fixture research result", sources: [] }; +const question = { + id: "question-message", role: "assistant", content: [{ type: "toolCall", id: "question-call", name: "quiz", arguments: {} }], + api: "openai-completions", provider: "faux", model: "faux", timestamp: 1, +} as unknown as LhMessage; +const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); +async function until(predicate: () => boolean): Promise { + const end = Date.now() + 8000; + while (!predicate()) { assert(Date.now() < end, "fixture condition timed out"); await delay(10); } +} +function sweep(): void { + const global = globalThis as { __lhWakePipeline?: { lastSweepAt: number } }; + if (global.__lhWakePipeline) global.__lhWakePipeline.lastSweepAt = 0; + recoverStaleJobs(); +} +function chat(blocked = false) { + const value = stores.chatStore.createChat("Isolated delivery fixture"); + chats.push(value.id); + if (blocked) stores.chatStore.saveMessages(value.id, [question]); + return value; +} +function pending(chatId: string) { + const value = stores.jobStore.createJob({ kind: "research", chatId, toolCallId: "fixture-call" }); + const job = stores.jobStore.updateJob(value.id, { + status: "done", result, activity: "finished", delivery: { kind: "result", status: "pending" }, + })!; + return { job, steer: buildSubagentSteer(job, result) }; +} +function agent() { + const handed: SteerMessage[] = []; + return { handed, value: { steer: (marker: SteerMessage) => handed.push(marker) } as unknown as Parameters[1] }; +} +function persist(chatId: string, steers: readonly LhMessage[]): void { + stores.chatStore.saveMessages(chatId, [...stores.chatStore.getChat(chatId)!.messages, ...steers]); + noteSteersDurable(chatId, steers); +} +afterEach(() => { + researchModelOverride.current = undefined; + for (const id of chats.splice(0)) { + unregisterLiveAgent(id); endTurn(id); discardChatDeliveries(id); + stores.jobStore.deleteJobsForChat(id); stores.chatStore.deleteChat(id); + } + setTurnRunner(async () => 500); +}); +after(() => fs.rmSync(directory, { recursive: true, force: true })); + +for (const [kind, input] of Object.entries({ + research: { question: "Saved research question", context: "Saved context" }, + demo: { title: "Fixture", brief: "Saved demo brief" }, + visual: { brief: "Saved figure brief" }, + pdf: { path: "fixture.pdf", question: "Saved PDF question" }, +})) test(`${kind}: dispatch saves input before spawn and does not overwrite it on repeated dispatch`, async () => { + const owner = chat(); + let spawns = 0; + const tools = jobTools({ chatId: owner.id, jobStore: stores.jobStore, spawn: (job) => { + spawns++; + assert.deepEqual(stores.jobStore.getJob(job.id)!.input, input); + } }); + await tools[kind]!.execute!("fixture-dispatch", input); + await tools[kind]!.execute!("fixture-dispatch", input); + assert.equal(spawns, 1); + assert.equal(stores.jobStore.listJobs(owner.id)[0]!.kind, kind as JobKind); +}); + +test("saved question survives absent transcript; runner ends while delivery is still pending", async () => { + const owner = chat(true); + const input = { question: "The dispatch turn was never saved", context: "Fixture context" }; + const job = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "lost-turn", input }); + const faux = createFauxCore({}); + let seen = ""; + faux.setResponses([(context) => { seen = JSON.stringify(context.messages); return fauxAssistantMessage("Fixture brief"); }]); + researchModelOverride.current = { model: faux.getModel(), streamFn: faux.streamSimple }; + let delivery: Promise | undefined; + const runner = createJobRunner({ ...stores, deliver: (steer) => { delivery = deliverJobResult(steer); return delivery; } }); + await runner.runJob(job.id); + assert(seen.includes(input.question)); assert(seen.includes(input.context)); + assert.equal(isJobLive(job.id), false, "worker registration no longer spans delivery"); + assert.equal(stores.jobStore.getJob(job.id)!.status, "done"); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); + assert(!activityFeed().jobs.some((item) => item.id === job.id), "no running background-agent row"); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.equal(next.handed.length, 1); + persist(owner.id, next.handed); unregisterLiveAgent(owner.id); settleSteers(owner.id, next.handed); endTurn(owner.id); + await delivery; +}); + +test("missing or blank research question records an honest failure without invoking a model", async () => { + for (const input of [undefined, { question: " " }]) { + const owner = chat(); + const job = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "missing-question", input }); + const faux = createFauxCore({}); let calls = 0; + faux.setResponses([() => { calls++; return fauxAssistantMessage("must not run"); }]); + researchModelOverride.current = { model: faux.getModel(), streamFn: faux.streamSimple }; + const steers: SteerMessage[] = []; + await createJobRunner({ ...stores, deliver: async (steer) => { steers.push(steer); } }).runJob(job.id); + assert.equal(calls, 0); + assert.equal(stores.jobStore.getJob(job.id)!.error, "research question unavailable"); + assert.equal(stores.jobStore.getJob(job.id)!.status, "error"); + assert.match(steers[0]!.content, /^Research failed: the research question is unavailable\./); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); + } +}); + +test("repeated recovery behind a question yields one handed and one durable marker", async () => { + const owner = chat(true); const { job, steer } = pending(owner.id); + let wakes = 0; setTurnRunner(async () => { wakes++; return 500; }); + sweep(); sweep(); sweep(); + const first = deliverJobResult(steer); + assert.strictEqual(deliverJobResult(steer), first, "duplicate callers share one logical delivery"); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.deepEqual(next.handed.map((message) => message.id), [steer.id]); + persist(owner.id, next.handed); unregisterLiveAgent(owner.id); settleSteers(owner.id, next.handed); endTurn(owner.id); + await first; + assert.equal(wakes, 0); + assert.equal(stores.chatStore.getChat(owner.id)!.messages.filter((message) => message.id === steer.id).length, 1); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "delivered"); +}); + +for (const refused of [409, 403]) test(`duplicate during a ${refused} wake retains one logical owner`, async () => { + const owner = chat(); const { steer } = pending(owner.id); + let release!: () => void; const gate = new Promise((resolve) => { release = resolve; }); + let attempts = 0; + setTurnRunner(async (id, input) => { + assert.equal(input.type, "steers"); if (input.type !== "steers") return 500; + assert.equal(input.steers.length, 1); + attempts++; + if (attempts === 1) { await gate; return refused; } + persist(id, input.steers); return 200; + }); + const first = deliverJobResult(steer); + await until(() => attempts === 1); + assert.strictEqual(deliverJobResult(steer), first, "wake cargo is still in the registry"); + release(); + if (refused === 403) { + await delay(30); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.equal(next.handed.length, 1); + persist(owner.id, next.handed); unregisterLiveAgent(owner.id); settleSteers(owner.id, next.handed); endTurn(owner.id); + } + await first; + assert.equal(stores.chatStore.getChat(owner.id)!.messages.filter((message) => message.id === steer.id).length, 1); +}); + +test("failed start stays quiet across sweeps, then joins a learner run", async () => { + const owner = chat(); const { job, steer } = pending(owner.id); + let attempts = 0; setTurnRunner(async () => { attempts++; return 503; }); + const delivery = deliverJobResult(steer); await until(() => attempts === 1); await delay(30); + sweep(); sweep(); await delay(900); + assert.equal(attempts, 1); + assert.equal(stores.jobStore.getJob(job.id)!.status, "done"); + assert.equal(stores.jobStore.getJob(job.id)!.error, undefined); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.equal(next.handed.length, 1); + persist(owner.id, next.handed); unregisterLiveAgent(owner.id); settleSteers(owner.id, next.handed); endTurn(owner.id); + await delivery; +}); + +test("HTTP acceptance without a saved marker is not delivery completion or a retry trigger", async () => { + const owner = chat(); const { job, steer } = pending(owner.id); + let attempts = 0; + setTurnRunner(async () => { attempts++; assert(beginTurn(owner.id)); return 200; }); + const delivery = deliverJobResult(steer); await until(() => attempts === 1); await delay(30); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); + unregisterLiveAgent(owner.id); endTurn(owner.id); settleSteers(owner.id, [], true); + sweep(); await delay(900); assert.equal(attempts, 1); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.equal(next.handed.length, 1); + persist(owner.id, next.handed); unregisterLiveAgent(owner.id); endTurn(owner.id); await delivery; +}); + +test("canonical save wins even if delivery bookkeeping fails; no marker replay", async () => { + const owner = chat(true); const { job, steer } = pending(owner.id); + const delivery = deliverJobResult(steer); + const update = stores.jobStore.updateJob; + stores.jobStore.updateJob = (id, patch) => { + if (id === job.id && patch.delivery?.status === "delivered") throw new Error("fixture acknowledgement write failure"); + return update(id, patch); + }; + try { persist(owner.id, [steer]); await delivery; } + finally { stores.jobStore.updateJob = update; } + let wakes = 0; setTurnRunner(async () => { wakes++; return 500; }); + sweep(); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "delivered"); + assert.equal(wakes, 0); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.equal(next.handed.length, 0); unregisterLiveAgent(owner.id); endTurn(owner.id); +}); + +test("old running results become finished pending work; old terminal records are untouched", () => { + const owner = chat(true); + const old = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "legacy-pending" }); + stores.jobStore.updateJob(old.id, { result }); + const terminal = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "legacy-terminal" }); + stores.jobStore.updateJob(terminal.id, { status: "done", result }); + sweep(); + assert.equal(stores.jobStore.getJob(old.id)!.status, "done"); + assert.equal(stores.jobStore.getJob(old.id)!.delivery?.status, "pending"); + assert.equal(stores.jobStore.getJob(terminal.id)!.delivery, undefined); + const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); + assert.deepEqual(next.handed.map((message) => message.id), [`steer_${old.id}`]); + unregisterLiveAgent(owner.id); endTurn(owner.id); +}); + +test("fresh process restores a pending delivery on registration without a jobs poll", () => { + const owner = chat(true); const { job, steer } = pending(owner.id); + const script = ` + const { stores } = await import('./src/server/stores.ts'); + const { registerLiveAgent, noteSteersDurable, unregisterLiveAgent } = await import('./src/server/jobs.ts'); + const { beginTurn, endTurn } = await import('./src/server/turn-liveness.ts'); + const handed = []; beginTurn(${JSON.stringify(owner.id)}); + registerLiveAgent(${JSON.stringify(owner.id)}, { steer: marker => handed.push(marker) }); + const record = stores.chatStore.getChat(${JSON.stringify(owner.id)}); + stores.chatStore.saveMessages(record.id, [...record.messages, ...handed]); + noteSteersDurable(record.id, handed); unregisterLiveAgent(record.id); endTurn(record.id); + console.log(JSON.stringify(handed.map(marker => marker.id))); + `; + const child = spawnSync(process.execPath, ["--import", "tsx", "--input-type=module", "-e", script], { + cwd: process.cwd(), encoding: "utf8", timeout: 15000, + env: { PATH: process.env.PATH, HOME: path.join(directory, "home"), NODE_ENV: "test", LH_DATA_DIR: process.env.LH_DATA_DIR, LH_AUTH_FILE: process.env.LH_AUTH_FILE }, + }); + assert.equal(child.status, 0, child.stderr); + assert.deepEqual(JSON.parse(child.stdout.trim()), [steer.id]); + assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "delivered"); + assert.equal(stores.chatStore.getChat(owner.id)!.messages.filter((message) => message.id === steer.id).length, 1); +}); + +test("deleting a session discards its pending deliveries without starting a turn", async () => { + const owner = chat(true); const { job, steer } = pending(owner.id); + let wakes = 0; setTurnRunner(async () => { wakes++; return 500; }); + const delivery = deliverJobResult(steer); + const outcome = await deleteChatSession(owner.id, "preserve-graph"); + assert(outcome.ok); + await delivery; await delay(450); + assert.equal(stores.jobStore.getJob(job.id), null); + assert.equal(stores.chatStore.getChat(owner.id), null); + assert.equal(wakes, 0); +}); diff --git a/tests/jobs.test.ts b/tests/jobs.test.ts index af9387a..161d4ce 100644 --- a/tests/jobs.test.ts +++ b/tests/jobs.test.ts @@ -51,18 +51,20 @@ import { runningTurn } from "../src/server/turn-runs.ts"; import { abortJob, deliverJobResult, - noteSteerVisible, + noteSteersDurable, + discardChatDeliveries, reconcileUserSteers, recoverStaleJobs, registerLiveAgent, setJobRespawn, - setTurnRunner, + setTurnRunner as setRawTurnRunner, + type TurnRunner, settleSteers, steerLiveChat, unregisterLiveAgent, } from "../src/server/jobs.ts"; -import { registerJobAbort, unregisterJobAbort } from "../src/agent/subagents/aborts.ts"; -import { beginTurn, endTurn } from "../src/server/turn-liveness.ts"; +import { isJobLive, registerJobAbort, unregisterJobAbort } from "../src/agent/subagents/aborts.ts"; +import { beginTurn, endTurn, isTurnRunning } from "../src/server/turn-liveness.ts"; import { persistTurn } from "../src/server/turn-service.ts"; import { chatStore } from "../src/store/chats.ts"; import { jobStore } from "../src/store/jobs.ts"; @@ -140,11 +142,13 @@ afterEach(() => { if (job && job.status === "running") { jobStore.updateJob(id, { status: "done", activity: "settled (fence)" }); } + if (job?.delivery?.status === "pending") jobStore.updateJob(id, { delivery: { ...job.delivery, status: "delivered" } }); unregisterJobAbort(id); } catch { /* already gone */ } } + for (const chatId of createdChats) discardChatDeliveries(chatId); // Late wakes from a prior test must be inert: a no-op respawn can't // increment anyone's counter, a no-op runner can't spawn real work. setJobRespawn(() => {}); @@ -454,16 +458,25 @@ function fakeDeliver() { steers, deliver: async (steer: SteerMessage) => { steers.push(steer); - const jobId = (steer.details as { jobId: string }).jobId; - const job = jobStore.getJob(jobId); - if (job && job.status === "running") { - const failed = job.result ? isSubagentFailure(job.kind, job.result as never) : true; - jobStore.updateJob(jobId, { status: failed ? "error" : "done", activity: failed ? "failed" : "delivered" }); - } + const job = jobStore.getJob((steer.details as { jobId: string }).jobId)!; + chatStore.saveMessages(job.chatId, [...chatStore.getChat(job.chatId)!.messages, steer]); + noteSteersDurable(job.chatId, [steer]); }, }; } +/** Fake wakes model canonical input acceptance, not just HTTP acknowledgement. */ +function setTurnRunner(run: TurnRunner): void { + setRawTurnRunner(async (chatId, input) => { + const status = await run(chatId, input); + if (status === 200 && input.type === "steers") { + const record = chatStore.getChat(chatId); + if (record) chatStore.saveMessages(chatId, [...record.messages, ...input.steers.filter((s) => !steerDelivered(record.messages, s.id))]); + } + return status; + }); +} + // ─── 3. dispatch: the tool call acks mid-turn and spawns ───────────────────── const RESEARCH_INPUT = { @@ -526,7 +539,7 @@ describe("dispatch (async protocol §2)", () => { const done = jobStore.getJob(job!.id)!; assert.equal(done.status, "done"); - assert.equal(done.activity, "delivered"); + assert.equal(done.activity, "finished"); // The steer carries the brief on its model-facing content; the // artifact payload rides details for the (UI) renderer. @@ -603,7 +616,7 @@ describe("research runner (async protocol §3)", () => { // The job is done, with the activity line from the tool call. const done = jobStore.getJob(job.id)!; assert.equal(done.status, "done"); - assert.equal(done.activity, "delivered", "the fake deliver flips the status like the pipeline"); + assert.equal(done.activity, "finished", "delivery does not own the worker status"); const result = done.result as ResearchResult; assert.equal(result.brief, BRIEF); assert.ok(result.sources.length > 0, "sources parsed from the brief"); @@ -746,7 +759,7 @@ describe("pdf runner (sources v0)", () => { const done = jobStore.getJob(job.id)!; assert.equal(done.status, "done"); - assert.equal(done.activity, "delivered"); + assert.equal(done.activity, "finished"); const result = done.result as PdfResult; assert.equal(result.path, PDF_FIXTURE); assert.match(result.brief, /integral rule/); @@ -827,7 +840,7 @@ describe("wake pipeline (src/server/jobs.ts)", () => { const job = jobStore.createJob({ kind, chatId: chat.id, toolCallId: `call-${jobStore.listJobs(chat.id).length}` }); trackJob(job.id); const result: ResearchResult = { brief: "Done: it works.", sources: [{ title: "s", url: "https://x.test" }] }; - jobStore.updateJob(job.id, { result, activity: "delivering result" }); + jobStore.updateJob(job.id, { result, status: "done", activity: "finished", delivery: { kind: "result", status: "pending" } }); const steer = buildSubagentSteer(job, result); return { chatId: chat.id, jobId: job.id, steer }; } @@ -882,7 +895,7 @@ describe("wake pipeline (src/server/jobs.ts)", () => { await waitFor(() => jobStore.getJob(jobId)!.status !== "running", 8000, "job flip after delivery"); assert.equal(jobStore.getJob(jobId)!.status, "done"); - assert.equal(jobStore.getJob(jobId)!.activity, "delivered"); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "delivered"); assert.ok(seen, "the turn runner fired"); assert.equal(seen.chatId, chatId); assert.equal(seen.steers.length, 1); @@ -920,7 +933,8 @@ describe("wake pipeline (src/server/jobs.ts)", () => { await delay(1200); assert.equal(fired, 0, "no wake while the quiz is pending"); - assert.equal(jobStore.getJob(jobId)!.status, "running", "job stays running while blocked"); + assert.equal(jobStore.getJob(jobId)!.status, "done", "worker is finished while delivery waits"); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "pending"); // The learner answers: the real tool result clears the dangling call. const record = chatStore.getChat(chatId)!; @@ -984,7 +998,7 @@ describe("wake pipeline (src/server/jobs.ts)", () => { await delay(1200); assert.equal(fired, 0, "no wake while the session sits dismissal-paused"); - assert.equal(jobStore.getJob(jobId)!.status, "running", "the delivery holds (queued, not lost)"); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "pending", "the delivery holds (queued, not lost)"); // The user's next message is the wake — its turn settles, and the // settlement re-pump lands the held delivery right after it. @@ -1056,7 +1070,8 @@ describe("Wave C: live steering handoff + settlement reconciliation", () => { await waitFor(() => handed.length === 1, 8000, "marker handed to the live agent"); await delay(1200); assert.equal(fired, 0, "no wake while the run is live"); - assert.equal(jobStore.getJob(jobId)!.status, "running", "job flips only at delivery"); + assert.equal(jobStore.getJob(jobId)!.status, "done", "worker finished before delivery"); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "pending"); // Settlement: the run persisted the injected marker. unregisterLiveAgent(chatId); @@ -1169,20 +1184,15 @@ describe("Wave C: live steering handoff + settlement reconciliation", () => { assert.equal(jobStore.getJob(jobId)!.status, "done"); }); - it("noteSteerVisible flips the job at the visibility seam — before any settle (owner ruling 2026-09-26)", async () => { - // The dock fix: the marker reaching the learner's view (wire - // message_end / write-ahead persist) flips the job NOW — the - // delivering turn may keep teaching for minutes. Idempotent with - // the settle-path finalize. + it("only a durable marker acknowledges delivery, independently of worker completion", () => { const { chatId, jobId, steer } = setup(); - assert.equal(jobStore.getJob(jobId)!.status, "running"); - noteSteerVisible(steer); - assert.equal(jobStore.getJob(jobId)!.status, "done", "flipped at the seam, no turn settled"); - noteSteerVisible(steer); // the settle-path flip re-runs — no-op assert.equal(jobStore.getJob(jobId)!.status, "done"); - // A probe-handoff (no jobId) is a harmless no-op. - const handoff = buildProbeHandoffSteer(chatId); - noteSteerVisible(handoff); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "pending"); + chatStore.saveMessages(chatId, [...chatStore.getChat(chatId)!.messages, steer]); + noteSteersDurable(chatId, [steer]); + noteSteersDurable(chatId, [steer]); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "delivered"); + assert.equal(jobStore.getJob(jobId)!.activity, "finished"); }); it("the idle wake carries EVERY pending marker in ONE turn", async () => { @@ -1208,17 +1218,25 @@ describe("Wave C: live steering handoff + settlement reconciliation", () => { }); - it("fails the job (error status) when the wake turn errors unrecoverably", async () => { - const { jobId, steer } = setup(); - setTurnRunner(async () => 500); - - await deliverJobResult(steer).then( - () => assert.fail("expected delivery to reject"), - () => undefined, - ); - const errored = jobStore.getJob(jobId)!; - assert.equal(errored.status, "error"); - assert.match(errored.error ?? "", /status 500/); + it("a failed start stays pending without changing the worker outcome", async () => { + const { chatId, jobId, steer } = setup(); + let attempts = 0; + setTurnRunner(async () => { attempts++; return 500; }); + const delivery = deliverJobResult(steer); + await waitFor(() => attempts === 1, 8000, "failed start attempted"); + await delay(1000); + assert.equal(attempts, 1, "no automatic retry loop"); + assert.equal(jobStore.getJob(jobId)!.status, "done"); + assert.equal(jobStore.getJob(jobId)!.error, undefined); + assert.equal(jobStore.getJob(jobId)!.delivery?.status, "pending"); + const handed: SteerMessage[] = []; + assert.ok(beginTurn(chatId)); + registerLiveAgent(chatId, { steer: (m: SteerMessage) => handed.push(m) } as never); + assert.deepEqual(handed.map((m) => m.id), [steer.id]); + unregisterLiveAgent(chatId); + settleSteers(chatId, [steer]); + endTurn(chatId); + await delivery; }); }); @@ -1554,14 +1572,14 @@ describe("abort (src/server/jobs.ts abortJob)", () => { } }); - it("aborting a delivering job drops the queued result steer and wakes with the abort note instead", async () => { + it("a finished worker with a queued result is no longer abortable", async () => { const chat = chatStore.createChat("Test: abort delivering job"); trackChat(chat.id); chatStore.saveMessages(chat.id, [{ id: "u1", role: "user", content: "hello", timestamp: 1 } as never]); const job = jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "call-abort3" }); trackJob(job.id); const result: ResearchResult = { brief: "Done: it works.", sources: [{ title: "s", url: "https://x.test" }] }; - jobStore.updateJob(job.id, { result, activity: "delivering result" }); + jobStore.updateJob(job.id, { result, status: "done", activity: "finished", delivery: { kind: "result", status: "pending" } }); // Block the pump's idle gates so the result steer sits QUEUED, not in flight. assert.ok(beginTurn(chat.id)); @@ -1573,18 +1591,18 @@ describe("abort (src/server/jobs.ts abortJob)", () => { void deliverJobResult(buildSubagentSteer(job, result)).catch(() => { /* dropped by the abort below */ }); - await waitFor(() => jobStore.getJob(job.id)!.activity === "delivering result", 8000, "queued"); + assert.equal(jobStore.getJob(job.id)!.delivery?.status, "pending"); const outcome = abortJob(job.id); - assert.ok(outcome.ok); - assert.equal(jobStore.getJob(job.id)!.status, "error"); - assert.equal(jobStore.getJob(job.id)!.error, "Aborted by learner"); + assert.deepEqual(outcome, { ok: false, reason: "not_running" }); + assert.equal(jobStore.getJob(job.id)!.status, "done"); + assert.equal(jobStore.getJob(job.id)!.error, undefined); endTurn(chat.id); // gates open — whatever is left in the queue fires await waitFor(() => fired.length > 0, 8000, "wake fired"); - assert.equal(fired.length, 1, "exactly one delivery — the result steer was dropped"); - assert.equal((fired[0]!.details as { aborted: boolean }).aborted, true, "the abort steer, not the result"); - assert.equal(jobStore.getJob(job.id)!.status, "error", "terminal after the wake"); + assert.equal(fired.length, 1, "exactly one result delivery"); + assert.equal(fired[0]!.customType, "subagent-steer"); + assert.equal(jobStore.getJob(job.id)!.status, "done"); }); it("outcome reasons: not_found for unknown, not_running for settled jobs", () => { diff --git a/tests/render-cards.test.tsx b/tests/render-cards.test.tsx index d5ae8d3..da0b3e1 100644 --- a/tests/render-cards.test.tsx +++ b/tests/render-cards.test.tsx @@ -156,9 +156,9 @@ describe("DispatchCard — the BYOK refusal tone (owner, 2026-09-22)", () => { assert.ok(!html.includes("dispatch-refused")); }); - it("a delivered job reads delivered, untouched by the wave", () => { + it("a finished worker reads finished, independently of delivery", () => { const html = renderToStaticMarkup(inJobs([job({ status: "done", error: undefined })])); - assert.ok(html.includes("delivered")); + assert.ok(html.includes("finished")); }); }); diff --git a/tests/subscription-background.test.ts b/tests/subscription-background.test.ts index e350009..c9cd7d3 100644 --- a/tests/subscription-background.test.ts +++ b/tests/subscription-background.test.ts @@ -42,7 +42,7 @@ for (const p of NATIVE_SUBSCRIPTIONS) for (const revokeAfterQueue of [false, tru const chat = stores.chatStore.createChat("Fixture mixed queue"); const model = { ...piModels().getModels(p.providerId)[0]!, provider: p.logicalId }; stores.chatStore.saveSettings(chat.id, { model: { provider: p.logicalId, id: model.id } }); const job = stores.jobStore.createJob({ kind: "research", chatId: chat.id, toolCallId: "fixture-job-call" }); const result = { brief: "Fixture helper result", sources: [] }; - stores.jobStore.updateJob(job.id, { status: "running", result }); const steer = buildSubagentSteer(job, result); const contexts: string[] = []; + stores.jobStore.updateJob(job.id, { status: "done", result, delivery: { kind: "result", status: "pending" } }); const steer = buildSubagentSteer(job, result); const contexts: string[] = []; const agent = new Agent({ initialState: { model, messages: [{ id: "fixture-initial", role: "user", content: "Initial learner input", timestamp: 1 } as AgentMessage] }, convertToLlm: toLlmMessages, streamFn: () => { contexts.push(JSON.stringify(agent.state.messages)); const stream = new AssistantMessageEventStream(); const output = { role: "assistant" as const, provider: p.logicalId, api: model.api, model: model.id, content: [{ type: "text" as const, text: "Foreground response" }], stopReason: "stop" as const, timestamp: 2, usage: { input: 2, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 3, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } } }; @@ -57,6 +57,6 @@ for (const p of NATIVE_SUBSCRIPTIONS) for (const revokeAfterQueue of [false, tru assert.equal(contexts.length, 1); assert(contexts[0]!.includes("Continue my learning")); assert(!contexts[0]!.includes(steer.id)); const persisted = persistTurn(stores.chatStore, chat.id, 0, agent.state.messages, false); unregisterLiveAgent(chat.id); endTurn(chat.id); settleSteers(chat.id, persisted); await new Promise((resolve) => setImmediate(resolve)); - assert.equal(wakes, 0); assert.equal(stores.jobStore.getJob(job.id)!.status, "running"); + assert.equal(wakes, 0); assert.equal(stores.jobStore.getJob(job.id)!.status, "done"); assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); connection(p.providerId, true); await delivered; assert.equal(wakes, 1); assert.equal(stores.jobStore.getJob(job.id)!.status, "done"); }); From 87b04cc86e60089ca37ff492ec4b0d4dbe9a1101 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 01:55:57 +0200 Subject: [PATCH 07/19] fix: deliver Claude teaching prose through an output tool --- src/agent/subagents/extraction.ts | 21 +- src/agent/teacher-agent.ts | 38 +++- src/agent/tools/send-to-user.ts | 36 +++ src/core/coverage.ts | 52 ++--- src/core/native-messages.ts | 49 ++++ src/core/session-markdown.ts | 22 +- src/core/view.ts | 28 ++- tests/send-to-user.test.ts | 366 ++++++++++++++++++++++++++++++ 8 files changed, 550 insertions(+), 62 deletions(-) create mode 100644 src/agent/tools/send-to-user.ts create mode 100644 tests/send-to-user.test.ts diff --git a/src/agent/subagents/extraction.ts b/src/agent/subagents/extraction.ts index aca547f..24cc975 100644 --- a/src/agent/subagents/extraction.ts +++ b/src/agent/subagents/extraction.ts @@ -28,7 +28,7 @@ import type { GraphStore } from "../../core/ports.ts"; import { emit } from "../../core/events.ts"; import { settleGraphLayout } from "../../core/layout-settle.ts"; import { batchRuns, coverageOf, messageViewLines } from "../../core/coverage.ts"; -import type { LhMessage } from "../../core/native-messages.ts"; +import { failedOutputCallIds, type LhMessage } from "../../core/native-messages.ts"; import { auditorGraphTools } from "../tools/graph.ts"; import { runSubmitAgent, healStringifiedArrays } from "./submit-agent.ts"; @@ -124,16 +124,20 @@ export function transcriptView(record: ChatRecord): string { } /** The extraction view under the teacher-prose ruling (owner, 2026-09-28): - * ONLY the teacher agent's text blocks — no learner messages, no - * thinking, no tool calls or results, no quiz blocks — each labeled with + * ONLY learner-facing teacher prose (text or send_to_user) — no learner + * messages, thinking, other tool calls/results, or quiz blocks — labeled with * its ACTUAL message id (owner, 2026-10-13): the view lines ARE * core/coverage.ts messageViewLines ("TEACHER [id] text"), the exact * lines the coverage fingerprints hash, so the extractor can anchor * every proposition to the passage it came from (the session submit's * required messageId). The shared markdown engine stays reserved for the * future user-facing export. */ -function proseView(messages: ReadonlyArray, title?: string): string { - const lines = messages.flatMap((message) => messageViewLines(message, message.id)); +function proseView( + messages: ReadonlyArray, + title?: string, + failedOutputCalls: ReadonlySet = failedOutputCallIds(messages), +): string { + const lines = messages.flatMap((message) => messageViewLines(message, message.id, failedOutputCalls)); const head = title && title.length > 0 ? `# ${title}\n\n` : ""; return head + lines.join("\n\n"); } @@ -1113,6 +1117,7 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti // stage runs (typed — the trigger skips, routes surface 503). refuseIfStagesUnready(); const covered = coverageOf(record.messages, graphStore.readCoverage(), chatId); + const failedOutputCalls = failedOutputCallIds(record.messages); if (covered.uncovered.length === 0) { console.info(`[extraction ${chatId}] nothing uncovered — no-op`); return { nodes: 0, chains: 0 }; @@ -1148,14 +1153,14 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti // fingerprints' messageViewLines ("TEACHER [id] text"), one // shared seam. const messages = batch.map((slot) => slot.message); - const view = proseView(messages, record.title); + const view = proseView(messages, record.title, failedOutputCalls); // The batch's teacher-prose ids — the ONLY anchors this // job's submit may name (validateSessionAnchors at the // submit seam refuses anything else: ids from other // batches — even real ones of this chat — other chats, // and non-prose messages). const batchIds = new Set( - messages.filter((m) => messageViewLines(m, m.id).length > 0).map((m) => m.id), + messages.filter((m) => messageViewLines(m, m.id, failedOutputCalls).length > 0).map((m) => m.id), ); setPhase(jobKey, "queued", "chat", undefined, part); const findings = await withExtractSlot(jobKey, async () => { @@ -1188,7 +1193,7 @@ export function createExtractionPipeline(deps: ExtractionPipelineDeps): Extracti const batchStats = mergeFindings(chatId, findings, auditResult, { chatStore, graphStore }, { foldedAt, coverage: batch - .filter((slot) => messageViewLines(slot.message, slot.message.id).length > 0) + .filter((slot) => slot.tokens > 0) .map((slot) => ({ chatId, fp: slot.fp, tokens: slot.tokens, foldedAt })), }); // The layout ledger evolves with the merge (sync — the diff --git a/src/agent/teacher-agent.ts b/src/agent/teacher-agent.ts index 08caa42..6fe6ac1 100644 --- a/src/agent/teacher-agent.ts +++ b/src/agent/teacher-agent.ts @@ -15,10 +15,11 @@ import { type TeacherToolContext, } from "./tools.ts"; import { practiceTools, type PracticeToolContext } from "./tools/practice.ts"; -import { healToolRecord } from "./tools/heal.ts"; +import { healToolRecord, withArgumentHeal } from "./tools/heal.ts"; +import { sendToUserTool } from "./tools/send-to-user.ts"; import { quizInputObject, quizInputProblems } from "../core/question-core.ts"; import { QUESTION_TOOLS, type EnabledTools } from "../core/pending.ts"; -import { toLlmMessages, nativeInputCause } from "../core/native-messages.ts"; +import { toLlmMessages, nativeInputCause, PRACTICE_PROBLEM_TOOL } from "../core/native-messages.ts"; import type { ChatGPTRequestCycle } from "./chatgpt-request-cycle.ts"; import { usageWasAdmitted } from "./subscription-request-cycle.ts"; @@ -145,22 +146,27 @@ export interface AgentBuildOptions { * belt-and-suspenders HALF for the mixed batch (a question + other calls * in one message) that terminate-only semantics would let run on — the * learner cannot answer while a turn is in flight (per-chat 409), so a - * cleanly-posed question MUST always end the turn. maxTurns is gone + * cleanly-posed question MUST always end the turn. Practice's problem pose + * has the same wait-for-the-learner contract, including mixed output/problem + * batches; failed poses must still retry. maxTurns is gone * (owner ruling 2026-09-25: no maximum-agent-turns net on the teaching * turn). */ -function makeQuestionStopper() { +function makeQuestionStopper(kind: AgentKind) { return (context: { message: { content: Array<{ type: string; id?: string; name?: string }> }; toolResults: Array<{ toolCallId: string; isError?: boolean }>; }): boolean => { const called = new Set( context.message.content - .filter((c) => c.type === "toolCall" && c.name !== undefined && (QUESTION_TOOLS as readonly string[]).includes(c.name!)) + .filter((c) => c.type === "toolCall" && c.name !== undefined && ( + (QUESTION_TOOLS as readonly string[]).includes(c.name) || + (kind === "practice" && c.name === PRACTICE_PROBLEM_TOOL) + )) .map((c) => c.id!), ); if (called.size === 0) return false; - // Stop only when every question call of this turn resolved cleanly. + // Stop only when every question/problem pose resolved cleanly. for (const result of context.toolResults) { if (called.has(result.toolCallId) && result.isError) return false; } @@ -174,7 +180,7 @@ function baseAgentOptions(kind: AgentKind, opts: AgentBuildOptions, tools: Agent // conversation-spec resolution as teaching); the cast keeps AgentKind // ("practice" is not a ModelRole) honest about that. const engine = opts.engine ?? piEngineFor(kind as "prober" | "teacher"); - const stopper = makeQuestionStopper(); + const stopper = makeQuestionStopper(kind); const systemPrompt = opts.promptSwitches ? composeInstructions(kind, opts.promptSwitches) : composedInstructions[kind]!; @@ -190,7 +196,16 @@ function baseAgentOptions(kind: AgentKind, opts: AgentBuildOptions, tools: Agent systemPrompt, model: engine.model, thinkingLevel: engine.level, - tools, + // Conditional registry inclusion, like the graph-tool gates: when + // closed, neither the tool nor its usage instructions reach the model. + // Match Claude across providers (including OpenRouter), not the API + // transport. Explicit test registries remain caller-owned. + tools: [ + ...tools, + ...(opts.tools === undefined && engine.model.id.toLowerCase().includes("claude") + ? [withArgumentHeal(sendToUserTool)] + : []), + ], messages: opts.messages ?? [], }, streamFn: engine.streamFn, @@ -312,10 +327,9 @@ export function bareQuestionTools(): Record> { * from turn 1 of a kind:"practice" session — no prober→teacher split, * no plan tools, no question tools. Its ENTIRE registry is * problem + conclude (tools/practice.ts); the doctrine prompt - * (prompts/practice/) carries the method. The question-stop rule does - * not apply (problem rides terminate: true natively — - * QUESTION_TOOLS does not include it), so baseAgentOptions's stopper is - * a no-op here; it stays wired for uniformity. + * (prompts/practice/) carries the method. A successful problem pose ends + * the run, natively for a problem-only batch and via the shared stop hook + * for a mixed batch (for example, send_to_user + problem). */ export function createPracticeAgent(ctx: PracticeToolContext, opts: AgentBuildOptions = {}) { const tools = opts.tools ?? practiceTools(ctx); diff --git a/src/agent/tools/send-to-user.ts b/src/agent/tools/send-to-user.ts new file mode 100644 index 0000000..fb0e409 --- /dev/null +++ b/src/agent/tools/send-to-user.ts @@ -0,0 +1,36 @@ +import { Type } from "typebox"; +import type { AgentTool } from "@earendil-works/pi-agent-core"; +import { SEND_TO_USER_TOOL } from "../../core/native-messages.ts"; +import { serverExecute } from "../tool-execute.ts"; + +const sendToUserInput = Type.Object({ + message: Type.String({ + minLength: 1, + pattern: "\\S", + description: "The complete content to display to the learner. Markdown and LaTeX are supported.", + }), +}); + +/** Claude's documented verbatim mid-turn output channel. Availability is + * model-gated at the shared conversation-agent construction boundary; + * the native call remains in history, and the view renders its message. */ +export const sendToUserTool: AgentTool = { + name: SEND_TO_USER_TOOL, + label: SEND_TO_USER_TOOL, + description: + "## REQUIRED OUTPUT CHANNEL — ALWAYS USE `send_to_user`\n\n" + + "**ALL learner-facing messages MUST be sent using `send_to_user`.** This includes explanations, teaching, " + + "feedback, corrections, hints, introductions, progress messages, and final responses. " + + "There is no exception for short messages or the end of a turn.\n\n" + + "Ordinary response text may be converted into summarized thinking instead of reaching the learner. " + + "**Never rely on ordinary response text to communicate with the learner. Thinking is not delivery.**\n\n" + + "Put the complete message in `message`. Markdown and LaTeX are supported. " + + "Do not duplicate it in ordinary response text.\n\n" + + "Question content still belongs in the appropriate question tool; send any accompanying teaching or " + + "commentary through `send_to_user` before calling that tool.\n\n" + + "This tool does not pause or end your turn. Continue with other tools when needed. " + + "After delivering your final message, end the turn without additional ordinary response text.", + parameters: sendToUserInput, + executionMode: "sequential", + execute: serverExecute(SEND_TO_USER_TOOL, sendToUserInput, () => "Message received for display."), +}; diff --git a/src/core/coverage.ts b/src/core/coverage.ts index fe5b523..50c3ce0 100644 --- a/src/core/coverage.ts +++ b/src/core/coverage.ts @@ -1,9 +1,9 @@ /** * Extraction coverage — the pure core. THE TEACHER'S PROSE IS THE UNIT * (owner ruling, 2026-09-28): the extractor sees ONLY the teacher agent's - * text blocks — no learner messages, no thinking, no tool calls or - * results, no quiz blocks. Everything else is INVISIBLE to coverage: it - * neither opens nor joins a run, so answering a quiz or editing a learner + * prose — ordinary text blocks and explicit send_to_user messages. No + * learner messages, thinking, other tool calls/results, or quiz blocks. + * Everything else is INVISIBLE to coverage: it neither opens nor joins a run, so answering a quiz or editing a learner * message triggers nothing. "Covered" is by construction exactly "what * the extractor saw": the fingerprint (sha256 of a message's view lines) * and the extractor's prompt share ONE selection — the teacher prose. @@ -22,33 +22,27 @@ import { createHash } from "node:crypto"; import type { CoverageEntry } from "./contracts/graph.ts"; -import type { LhMessage } from "./native-messages.ts"; +import { failedOutputCallIds, userFacingTextOf, type LhMessage } from "./native-messages.ts"; export type { CoverageEntry }; -/** The user-facing text of a native message (string or text blocks). */ -function textOf(content: unknown): string[] { - if (typeof content === "string") return content.trim() ? [content] : []; - if (!Array.isArray(content)) return []; - return content - .filter((c): c is { type: "text"; text: string } => (c as { type?: string }).type === "text") - .map((c) => c.text) - .filter((t) => t.trim().length > 0); -} - /** * The extraction-relevant lines of ONE native message — the shared seam * between the extractor's prompt and messageFingerprint (coverage). * TEACHER PROSE ONLY (owner, 2026-09-28): an assistant message yields one - * line per text block; everything else (user messages, tool results, - * markers) yields NOTHING and is invisible to coverage. `mid` is the - * message id — part of the hash input, so the same prose in two messages + * line per learner-facing prose block; everything else (user messages, + * tool results, markers) yields NOTHING and is invisible to coverage. + * `mid` is the message id — part of the hash input, so the same prose in two messages * is two slots (per-message coverage). */ -export function messageViewLines(message: LhMessage, mid: string): string[] { +export function messageViewLines( + message: LhMessage, + mid: string, + failedOutputCalls: ReadonlySet = failedOutputCallIds([message]), +): string[] { const lines: string[] = []; if ((message as { role?: string }).role !== "assistant") return lines; - for (const text of textOf((message as { content: unknown }).content)) { + for (const text of userFacingTextOf((message as { content: unknown }).content, failedOutputCalls)) { lines.push(`TEACHER [${mid}] ${text.trim()}`); } return lines; @@ -62,9 +56,10 @@ export function messageViewLines(message: LhMessage, mid: string): string[] { */ export function teacherProseBlocks(messages: ReadonlyArray): string[] { const out: string[] = []; + const failedOutputCalls = failedOutputCallIds(messages); for (const message of messages) { if ((message as { role?: string }).role !== "assistant") continue; - for (const text of textOf((message as { content: unknown }).content)) out.push(text.trim()); + for (const text of userFacingTextOf((message as { content: unknown }).content, failedOutputCalls)) out.push(text.trim()); } return out; } @@ -79,9 +74,9 @@ export function viewTokens(lines: string[]): number { * teacher prose, with the message id as the tag). Changes exactly when * the teacher prose of the message changes — nothing else can re-open it. */ -export function messageFingerprint(message: LhMessage): string { +export function messageFingerprint(message: LhMessage, failedOutputCalls?: ReadonlySet): string { const h = createHash("sha256"); - h.update(JSON.stringify([(message as { role?: string }).role, messageViewLines(message, message.id)])); + h.update(JSON.stringify([(message as { role?: string }).role, messageViewLines(message, message.id, failedOutputCalls)])); return h.digest("hex"); } @@ -95,7 +90,12 @@ export interface CoverageSlot { /** Fingerprint a whole transcript, in order (used by the trigger, the run, * and the coverage view — one derivation everywhere). */ export function coverageSlots(messages: ReadonlyArray): CoverageSlot[] { - return messages.map((message) => ({ message, fp: messageFingerprint(message), tokens: viewTokens(messageViewLines(message, message.id)) })); + const failedOutputCalls = failedOutputCallIds(messages); + return messages.map((message) => ({ + message, + fp: messageFingerprint(message, failedOutputCalls), + tokens: viewTokens(messageViewLines(message, message.id, failedOutputCalls)), + })); } /** The covered-fingerprint set for one chat, from the ledger. */ @@ -131,16 +131,14 @@ export function coverageOf( let uncoveredTokens = 0; for (let i = 0; i < slots.length; i++) { const slot = slots[i]!; - if (messageViewLines(slot.message, slot.message.id).length === 0) continue; // invisible + if (slot.tokens === 0) continue; // invisible (including failed output calls) if (covered.has(slot.fp)) coveredTokens += slot.tokens; else { uncovered.push(i); uncoveredTokens += slot.tokens; } } - const visibleCount = slots.length - slots.filter( - (s) => messageViewLines(s.message, s.message.id).length === 0, - ).length; + const visibleCount = slots.filter((s) => s.tokens > 0).length; return { slots, uncovered, diff --git a/src/core/native-messages.ts b/src/core/native-messages.ts index 2983e4f..63f2720 100644 --- a/src/core/native-messages.ts +++ b/src/core/native-messages.ts @@ -98,6 +98,55 @@ export function asAgentMessages(messages: readonly LhMessage[]): AgentMessage[] return messages as unknown as AgentMessage[]; } +// ─── explicit learner-facing output ───────────────────────────────────────── + +export const SEND_TO_USER_TOOL = "send_to_user"; + +/** Read the output tool's message without changing the native call. Also + * accepts partial arguments while streaming; malformed/empty inputs carry + * no prose. This projection is independent of the currently selected model. */ +export function sendToUserText(block: unknown): string | undefined { + if (!block || typeof block !== "object") return undefined; + const call = block as { type?: unknown; name?: unknown; arguments?: unknown }; + if (call.type !== "toolCall" || call.name !== SEND_TO_USER_TOOL) return undefined; + if (!call.arguments || typeof call.arguments !== "object") return undefined; + const message = (call.arguments as { message?: unknown }).message; + return typeof message === "string" && message.trim() ? message : undefined; +} + +/** Learner-facing prose, in content order. Only the explicit output tool + * joins ordinary text here — thinking and all other tool inputs stay out. + * Shared by coverage/extraction and Markdown; native history stays intact. */ +export function userFacingTextOf(content: unknown, failedOutputCalls?: ReadonlySet): string[] { + if (typeof content === "string") return content.trim() ? [content] : []; + if (!Array.isArray(content)) return []; + const out: string[] = []; + for (const block of content) { + if (!block || typeof block !== "object") continue; + if (block.type === "toolCall" && failedOutputCalls?.has(block.id)) continue; + const text = block.type === "text" ? block.text : sendToUserText(block); + if (typeof text === "string" && text.trim()) out.push(text); + } + return out; +} + +/** Failed output calls remain retry context, not delivered teaching. + * Truncated calls can have valid-looking partial arguments. Failed/aborted + * generations do not execute their calls at all, so may have no result. */ +export function failedOutputCallIds(messages: readonly LhMessage[]): ReadonlySet { + const failed = new Set(); + for (const message of messages) { + if (message.role === "toolResult" && message.toolName === SEND_TO_USER_TOOL && message.isError) { + failed.add(message.toolCallId); + } else if (message.role === "assistant" && (message.stopReason === "error" || message.stopReason === "aborted")) { + for (const block of message.content) { + if (block.type === "toolCall" && block.name === SEND_TO_USER_TOOL) failed.add(block.id); + } + } + } + return failed; +} + // ─── id minting ────────────────────────────────────────────────────────────── /** The app-wide message id shape (same as the UIMessage-era mint). */ diff --git a/src/core/session-markdown.ts b/src/core/session-markdown.ts index 64a37b6..107e276 100644 --- a/src/core/session-markdown.ts +++ b/src/core/session-markdown.ts @@ -22,22 +22,15 @@ import type { LhMessage } from "./native-messages.ts"; import { + failedOutputCallIds, PRACTICE_CONCLUDE_TOOL, PRACTICE_PROBLEM_TOOL, problemInputForConclude, + SEND_TO_USER_TOOL, + userFacingTextOf, } from "./native-messages.ts"; import type { ToolResultMessage } from "@earendil-works/pi-ai"; -/** The user-facing text blocks of a native message (string or text parts). */ -function textOf(content: unknown): string[] { - if (typeof content === "string") return content.trim() ? [content] : []; - if (!Array.isArray(content)) return []; - return content - .filter((c): c is { type: "text"; text: string } => (c as { type?: string }).type === "text") - .map((c) => c.text) - .filter((t) => t.trim().length > 0); -} - /** The practice join context (owner-approved, 2026-10-06 export round): * practice tool RESULTS are acks — the statement, outcome, and verdict * all live in the assistant's call arguments, and the model solution @@ -48,6 +41,7 @@ export interface MarkdownContext { messages: ReadonlyArray; /** toolCallId → the call part's name + arguments. */ calls: Map | undefined }>; + failedOutputCalls?: ReadonlySet; } function markdownContext(messages: ReadonlyArray): MarkdownContext { @@ -66,7 +60,7 @@ function markdownContext(messages: ReadonlyArray): MarkdownContext { } } } - return { messages, calls }; + return { messages, calls, failedOutputCalls: failedOutputCallIds(messages) }; } /** solvedWithHints → "Solved with hints" — the outcome enum reads as prose. */ @@ -172,16 +166,16 @@ export function messageMarkdown( const parts: string[] = []; if (role === "user") { - for (const text of textOf((message as { content: unknown }).content)) { + for (const text of userFacingTextOf((message as { content: unknown }).content)) { parts.push(`### Learner${anchor}\n\n${text.trim()}`); } } else if (role === "assistant") { - for (const text of textOf((message as { content: unknown }).content)) { + for (const text of userFacingTextOf((message as { content: unknown }).content, ctx?.failedOutputCalls ?? failedOutputCallIds([message]))) { parts.push(`### Teacher${anchor}\n\n${text.trim()}`); } } else if (role === "toolResult") { const result = message as ToolResultMessage; - if (result.isError) return null; // retry context, not learning evidence + if (result.isError || result.toolName === SEND_TO_USER_TOOL) return null; // retry context or output ack, not learning evidence const { heading, body } = toolResultBody(result, ctx); parts.push(`### ${heading}${anchor}\n\n${body.join("\n\n")}`); } diff --git a/src/core/view.ts b/src/core/view.ts index 609de06..166d8d5 100644 --- a/src/core/view.ts +++ b/src/core/view.ts @@ -34,7 +34,7 @@ import type { AssistantMessage, ToolResultMessage } from "@earendil-works/pi-ai"; import type { LhMarkerMessage, LhMessage } from "./native-messages.ts"; -import { modelSolutionForConclude, PRACTICE_CONCLUDE_TOOL, problemInputForConclude } from "./native-messages.ts"; +import { modelSolutionForConclude, PRACTICE_CONCLUDE_TOOL, problemInputForConclude, SEND_TO_USER_TOOL, sendToUserText } from "./native-messages.ts"; import { activeCompactionOf, type CompactionDetails } from "./compaction.ts"; import { QUESTION_TOOLS } from "./pending.ts"; import type { SubagentArtifactData, SubagentResultMeta } from "./contracts/jobs.ts"; @@ -124,6 +124,14 @@ function lastSuccessfulPlanInit(parts: LhViewPart[]): number { * view messages. Pure: the same input always projects to the same list. */ export function projectView(messages: readonly LhMessage[], live?: LiveTurnState): LhViewMessage[] { const out: LhViewMessage[] = []; + // Output calls become text rather than tool parts, so read settled + // failures up front: a failed call must retain the normal error path. + const outputErrors = new Map(); + for (const message of messages) { + if (message.role === "toolResult" && message.toolName === SEND_TO_USER_TOOL && message.isError) { + outputErrors.set(message.toolCallId, message); + } + } const lastAssistant = (): LhViewMessage | undefined => { const last = out[out.length - 1]; @@ -218,6 +226,24 @@ export function projectView(messages: readonly LhMessage[], live?: LiveTurnState reasoning.push({ type: "reasoning", id: `${m.id}-r${reasoningN++}`, text: block.thinking, state: "done" }); } else if (block.type === "toolCall") { const part = toolPartOf(block, live, streamingMessage); + if (block.name === SEND_TO_USER_TOOL) { + // Failed/aborted generations never execute their calls; + // the provider error/abort UI already owns that outcome. + if (assistant.stopReason === "error" || assistant.stopReason === "aborted") continue; + const error = outputErrors.get(block.id); + if (error) applyResult(part, error); + if (part.state === "output-error") { + rest.push(part); + } else { + const text = sendToUserText(block); + if (text !== undefined) { + rest.push({ type: "text", id: `${m.id}-t${textN++}`, text, state: "done" }); + } + } + // No live tool card or acknowledgement: this channel is + // ordinary prose, including while its arguments stream. + continue; + } // The conclusion's reveal join (owner round, 2026-10-05): the // hero of a conclude card is the modelSolution committed at POSE // time — a record fold, enriched here so the card renders pure. diff --git a/tests/send-to-user.test.ts b/tests/send-to-user.test.ts new file mode 100644 index 0000000..beaeae3 --- /dev/null +++ b/tests/send-to-user.test.ts @@ -0,0 +1,366 @@ +/** Explicit Claude output: closed-network synthetic fixtures only. Native + * history stays native; UI, extraction, and Markdown read the same prose. */ +import assert from "node:assert/strict"; +import { createHash } from "node:crypto"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { after, describe, it } from "node:test"; +import { Value } from "typebox/value"; +import { + createFauxCore, fauxAssistantMessage, fauxText, fauxThinking, fauxToolCall, + type FauxResponseStep, +} from "@earendil-works/pi-ai"; +import type { PiEngine } from "../src/agent/models.ts"; +import type { AgentBuildOptions } from "../src/agent/teacher-agent.ts"; +import type { LhMessage } from "../src/core/native-messages.ts"; + +// Set the data/auth seams BEFORE importing app modules. Do not read local +// keys, recordings, or learner sessions, even if a dev server is running. +const scratch = fs.mkdtempSync(path.join(os.tmpdir(), "lh-send-to-user-")); +const originalEnv = process.env; +const originalFetch = globalThis.fetch; +process.env = { + ...(originalEnv.NODE_TEST_CONTEXT ? { NODE_TEST_CONTEXT: originalEnv.NODE_TEST_CONTEXT } : {}), + ...(originalEnv.NODE_OPTIONS ? { NODE_OPTIONS: originalEnv.NODE_OPTIONS } : {}), + NODE_ENV: "test", + LH_DATA_DIR: path.join(scratch, "data"), + LH_AUTH_FILE: path.join(scratch, "auth.json"), + HOME: path.join(scratch, "home"), +}; +globalThis.fetch = async () => { throw new Error("CLOSED: unexpected network request"); }; +after(() => { + process.env = originalEnv; + globalThis.fetch = originalFetch; + fs.rmSync(scratch, { recursive: true, force: true }); +}); + +const { sendToUserTool } = await import("../src/agent/tools/send-to-user.ts"); +const { createProberAgent, createTeacherAgent, createPracticeAgent, composeInstructions } = await import("../src/agent/teacher-agent.ts"); +const { SEND_TO_USER_TOOL, activeProblemOf, asAgentMessages, newUserMessage, sendToUserText, toLlmMessages, userFacingTextOf } = await import("../src/core/native-messages.ts"); +const { coverageOf, messageFingerprint, messageViewLines, teacherProseBlocks } = await import("../src/core/coverage.ts"); +const { projectView } = await import("../src/core/view.ts"); +const { sessionMarkdown } = await import("../src/core/session-markdown.ts"); +const { transcriptView } = await import("../src/agent/subagents/extraction.ts"); +const { runSubagent } = await import("../src/agent/subagents/pi-run.ts"); +const { persistTurn } = await import("../src/server/turn-service.ts"); +const { runPiTurn } = await import("../src/server/pi/ui-stream.ts"); +const { SessionModel } = await import("../components/session-core.ts"); +const { chatStore } = await import("../src/store/chats.ts"); +const { jobStore } = await import("../src/store/jobs.ts"); + +function scripted(provider: string, id: string, steps: FauxResponseStep[] = []) { + const faux = createFauxCore({ provider, models: [{ id }] }); + faux.setResponses(steps); + const engine: PiEngine = { model: faux.getModel(), streamFn: faux.streamSimple }; + return { engine, faux }; +} + +const chatId = chatStore.createChat("Synthetic output gate").id; +const factories = { + prober: (opts: AgentBuildOptions) => createProberAgent({ chatId, chatStore, jobStore, deliver: () => {}, spawn: () => {} }, opts), + teacher: (opts: AgentBuildOptions) => createTeacherAgent({ chatId, chatStore, jobStore, spawn: () => {} }, opts), + practice: (opts: AgentBuildOptions) => createPracticeAgent({ chatId, chatStore }, opts), +}; + +function outputCall(message: string | number | null = "An explanation.", id = "send-1") { + return { type: "toolCall" as const, id, name: SEND_TO_USER_TOOL, arguments: { message } }; +} +function assistant(content: unknown[], id = "a1"): LhMessage { + return { ...fauxAssistantMessage(""), content, id } as LhMessage; +} +function ack(callId = "send-1", isError = false): LhMessage { + return { + id: `result-${callId}`, role: "toolResult", toolCallId: callId, toolName: SEND_TO_USER_TOOL, + content: [{ type: "text", text: isError ? "Invalid tool input" : "Message received for display." }], + details: null, isError, timestamp: 1, + }; +} +function prose(messages: ReturnType): string[] { + return messages.filter((m) => m.role === "assistant").flatMap((m) => m.parts.filter((p) => p.type === "text").map((p) => p.text!)); +} + +describe("send_to_user model gate", () => { + for (const [provider, id, enabled] of [ + ["anthropic", "claude-opus-5-5", true], + ["anthropic", "claude-haiku-4-5-20251001", true], + ["openrouter", "anthropic/claude-opus-5.5", true], + ["openrouter", "anthropic/claude-sonnet-5.5", true], + ["amazon-bedrock", "us.anthropic.claude-opus-5-5-v1:0", true], + ["fixture", "CLAUDE-fixture", true], + ["openrouter", "z-ai/glm-5.3-flash", false], + ["openai", "gpt-4o-mini", false], + ["anthropic", "opaque-alias", false], + ] as const) { + it(`${provider}/${id}: ${enabled ? "includes" : "omits"} tool and description in all conversation agents`, () => { + const { engine } = scripted(provider, id); + for (const [kind, factory] of Object.entries(factories)) { + const agent = factory({ engine }); + const output = agent.state.tools.find((tool) => tool.name === SEND_TO_USER_TOOL); + assert.equal(Boolean(output), enabled, kind); + if (enabled) { + assert.equal(output!.description, sendToUserTool.description); + assert.equal(typeof output!.prepareArguments, "function", "standard argument healing is wired"); + } + assert.equal(agent.state.systemPrompt, composeInstructions(kind as keyof typeof factories)); + assert.ok(!agent.state.systemPrompt.includes(SEND_TO_USER_TOOL), "no added prompt instructions"); + } + }); + } + + it("explicitly injected teacher/practice test registries remain unchanged", () => { + const { engine } = scripted("anthropic", "claude-opus-5-5"); + for (const factory of [factories.teacher, factories.practice]) { + assert.deepEqual(factory({ engine, tools: {} }).state.tools, []); + } + }); + + it("background agents do not acquire a learner-output tool", async () => { + let calls = 0; + const { engine } = scripted("anthropic", "claude-opus-5-5", [(context) => { + calls++; + assert.ok(!context.messages.some((m) => m.role === "system" && m.toolsAdded?.some((t) => t.name === SEND_TO_USER_TOOL))); + return fauxAssistantMessage("Synthetic background result."); + }]); + await runSubagent([], { role: "researcher", engine, systemPrompt: "Synthetic fixture.", prompt: "Finish.", maxTurns: 2 }); + assert.equal(calls, 1); + }); +}); + +describe("send_to_user mechanics", () => { + it("requires nonblank text and acknowledges without echoing or terminating", async () => { + assert.equal(sendToUserTool.name, SEND_TO_USER_TOOL); + for (const input of [{}, { message: "" }, { message: " \n\t" }, { message: 3 }, { message: null }]) { + assert.equal(Value.Check(sendToUserTool.parameters, input), false); + } + const message = "**A full explanation**\n\n$$a = b$$"; + assert.equal(Value.Check(sendToUserTool.parameters, { message }), true); + const result = await sendToUserTool.execute("send-1", { message }); + assert.ok(!result.isError); + assert.ok(!result.terminate); + assert.ok(!JSON.stringify(result).includes(message)); + assert.deepEqual(result.content, [{ type: "text", text: "Message received for display." }]); + }); + + it("only extracts the output tool's nonblank message, without modifying it", () => { + const message = " **Keep spacing and math** $x$\n"; + assert.equal(sendToUserText(outputCall(message)), message); + for (const block of [null, {}, { type: "text", text: "ordinary" }, outputCall(2), outputCall(" "), { ...outputCall(), name: "other" }]) { + assert.equal(sendToUserText(block), undefined); + } + assert.deepEqual(userFacingTextOf([null, fauxThinking("Not lesson prose."), fauxText("First"), outputCall(message), fauxToolCall("quiz", { question: "Q?" })]), ["First", message]); + }); +}); + +describe("send_to_user transcript projection", () => { + it("renders in place as prose, with thinking unchanged and no tool/ack duplication", () => { + const messages = [assistant([ + fauxText("Before."), outputCall("**The lesson.** $x = 1$"), + fauxThinking("Private reasoning."), fauxToolCall("quiz", { question: "Q?" }, { id: "quiz-1" }), + fauxText("After."), + ]), ack()]; + const before = structuredClone(messages); + const view = projectView(messages); + assert.deepEqual(view[0]!.parts.map((p) => p.type), ["step-start", "reasoning", "text", "text", "tool-quiz", "text"]); + assert.deepEqual(prose(view), ["Before.", "**The lesson.** $x = 1$", "After."]); + assert.equal(view[0]!.parts.find((p) => p.type === "reasoning")!.text, "Private reasoning."); + assert.deepEqual(view[0]!.parts.filter((p) => p.type === "text").map((p) => p.id), ["a1-t0", "a1-t1", "a1-t2"]); + assert.deepEqual(messages, before, "projection never rewrites native content"); + assert.deepEqual(projectView(JSON.parse(JSON.stringify(messages))), view, "reload is identical, without consulting a provider gate"); + }); + + it("streams available arguments as replacing prose, never as a live tool card", () => { + const live = { inFlight: true, streamingIds: new Set(["a1"]), executions: new Map() }; + for (const text of ["A", "A growing", "A growing explanation."]) { + const view = projectView([assistant([outputCall(text)])], live); + assert.deepEqual(prose(view), [text]); + assert.ok(!view[0]!.parts.some((p) => p.type.startsWith("tool-"))); + } + const empty = projectView([assistant([{ ...outputCall(), arguments: {} }])], live); + assert.deepEqual(prose(empty), []); + assert.ok(!empty[0]!.parts.some((p) => p.type.startsWith("tool-"))); + }); + + it("malformed messages contain no prose; failures keep the existing error path live and after reload", () => { + assert.deepEqual(prose(projectView([assistant([outputCall(3)])])), []); + const messages = [assistant([outputCall("Incomplete teaching.")]), ack("send-1", true)]; + const settled = projectView(messages); + assert.deepEqual(prose(settled), []); + assert.equal(settled[0]!.parts[1]!.type, "tool-send_to_user"); + assert.equal(settled[0]!.parts[1]!.state, "output-error"); + const live = projectView(messages.slice(0, 1), { + inFlight: true, streamingIds: new Set(["a1"]), + executions: new Map([["send-1", { isError: true, errorText: "Invalid tool input" }]]), + }); + assert.deepEqual(prose(live), []); + assert.equal(live[0]!.parts[1]!.state, "output-error"); + }); +}); + +describe("send_to_user extraction and Markdown", () => { + it("selects output prose once and in order, preserving old fingerprints", () => { + const message = assistant([fauxText("First."), outputCall("Second."), fauxThinking("Hidden."), outputCall("Third.", "send-2")]); + const messages = [message, ack(), ack("send-2")]; + assert.deepEqual(messageViewLines(message, "a1"), ["TEACHER [a1] First.", "TEACHER [a1] Second.", "TEACHER [a1] Third."]); + assert.deepEqual(teacherProseBlocks(messages), ["First.", "Second.", "Third."]); + assert.equal(coverageOf(messages, [], "fixture").uncoveredCount, 1); + const md = sessionMarkdown(messages, { anchors: false }); + assert.equal(md, "### Teacher\n\nFirst.\n\n### Teacher\n\nSecond.\n\n### Teacher\n\nThird."); + const record = chatStore.createChat("Synthetic extraction"); + assert.equal(transcriptView({ ...record, messages }), "# Synthetic extraction\n\nTEACHER [a1] First.\n\nTEACHER [a1] Second.\n\nTEACHER [a1] Third."); + const legacy = assistant([fauxText("Old text."), fauxThinking("Old thinking.")], "legacy"); + const oldFingerprint = createHash("sha256").update(JSON.stringify(["assistant", ["TEACHER [legacy] Old text."]])).digest("hex"); + assert.equal(messageFingerprint(legacy), oldFingerprint, "no re-extraction/migration of legacy prose"); + }); + + it("does not extract/export failed output calls, including partial calls from truncated responses", () => { + const message = assistant([fauxText("Keep this."), outputCall("Do not treat this as delivered.")]); + const messages = [message, ack("send-1", true)]; + assert.deepEqual(teacherProseBlocks(messages), ["Keep this."]); + assert.equal(sessionMarkdown(messages, { anchors: false }), "### Teacher\n\nKeep this."); + const record = chatStore.createChat("Failed output fixture"); + assert.ok(!transcriptView({ ...record, messages }).includes("Do not treat")); + const coverage = coverageOf([assistant([outputCall()]), ack("send-1", true)], [], "fixture"); + assert.equal(coverage.uncoveredCount, 0); + assert.equal(coverage.uncoveredTokens, 0); + }); +}); + +describe("send_to_user interrupted generations", () => { + for (const stopReason of ["error", "aborted"] as const) { + it(`${stopReason} without a tool result never becomes delivered teaching; ordinary text stays unchanged`, () => { + const message = { ...assistant([fauxText("Keep ordinary text."), outputCall("Unfinished output.")]), stopReason }; + const messages = [message]; + assert.deepEqual(prose(projectView(messages)), ["Keep ordinary text."]); + assert.deepEqual(prose(projectView(JSON.parse(JSON.stringify(messages)))), ["Keep ordinary text."]); + assert.deepEqual(messageViewLines(message, message.id), ["TEACHER [a1] Keep ordinary text."]); + assert.deepEqual(teacherProseBlocks(messages), ["Keep ordinary text."]); + assert.equal(sessionMarkdown(messages, { anchors: false }), "### Teacher\n\nKeep ordinary text."); + const onlyOutput = { ...assistant([outputCall("Unfinished output.")]), stopReason }; + assert.equal(coverageOf([onlyOutput], [], "fixture").uncoveredCount, 0); + }); + } +}); + +describe("send_to_user native flow (offline)", () => { + it("output alone continues the loop, so the model receives its native acknowledgement", async () => { + const { engine, faux } = scripted("anthropic", "claude-opus-5-5", [ + fauxAssistantMessage([outputCall("Read this first.")], { stopReason: "toolUse" }), + (context) => { + assert.ok(context.messages.some((m) => m.role === "toolResult" && m.toolName === SEND_TO_USER_TOOL)); + return fauxAssistantMessage([fauxText("Final plain reply.")]); + }, + ]); + const agent = createTeacherAgent(undefined, { engine }); + await agent.prompt("Synthetic request."); + assert.equal(faux.state.callCount, 2); + }); + + for (const outputFirst of [true, false]) { + it(`output + practice problem stops after the pose (${outputFirst ? "output first" : "problem first"})`, async () => { + const output = outputCall("Try this practice problem."); + const problem = fauxToolCall("problem", { + statement: "What is two plus two?", mode: "recall", modelSolution: "Four.", + }, { id: "problem-1" }); + const { engine, faux } = scripted("anthropic", "claude-opus-5-5", [ + fauxAssistantMessage(outputFirst ? [output, problem] : [problem, output], { stopReason: "toolUse" }), + fauxAssistantMessage("Premature continuation must not run."), + ]); + const agent = createPracticeAgent({ chatId, chatStore }, { engine }); + await agent.prompt("Pose a synthetic problem."); + assert.equal(faux.state.callCount, 1, "wait for the learner after a successful mixed-batch pose"); + const results = agent.state.messages.filter((m) => m.role === "toolResult"); + assert.equal(results.length, 2); + assert.ok(results.every((m) => !m.isError)); + }); + } + + for (const stopReason of ["error", "aborted"] as const) { + it(`${stopReason} mixed output/problem generation never executes or activates the pose`, async () => { + const { engine, faux } = scripted("anthropic", "claude-opus-5-5", [ + fauxAssistantMessage([ + outputCall("Undelivered introduction."), + fauxToolCall("problem", { + statement: "What is two plus two?", mode: "recall", modelSolution: "Four.", + }, { id: "unfinished-problem" }), + ], { stopReason }), + ]); + const agent = createPracticeAgent({ chatId, chatStore }, { engine }); + await agent.prompt("Pose a synthetic problem."); + assert.equal(faux.state.callCount, 1); + assert.ok(!agent.state.messages.some((m) => m.role === "toolResult")); + const messages = agent.state.messages.map((m, i) => ({ ...m, id: `interrupted-${i}` })) as LhMessage[]; + assert.equal(activeProblemOf(messages), null); + assert.deepEqual(prose(projectView(messages)), []); + }); + } + + it("a rejected mixed-batch practice pose retries, then stops after the successful pose", async () => { + const { engine, faux } = scripted("anthropic", "claude-opus-5-5", [ + fauxAssistantMessage([ + outputCall("Try this practice problem."), + fauxToolCall("problem", { statement: "Incomplete problem.", mode: "recall" }, { id: "bad-problem" }), + ], { stopReason: "toolUse" }), + fauxAssistantMessage([ + outputCall("Here is the corrected problem.", "send-2"), + fauxToolCall("problem", { + statement: "What is two plus two?", mode: "recall", modelSolution: "Four.", + }, { id: "good-problem" }), + ], { stopReason: "toolUse" }), + fauxAssistantMessage("Premature continuation must not run."), + ]); + const agent = createPracticeAgent({ chatId, chatStore }, { engine }); + await agent.prompt("Pose a synthetic problem."); + assert.equal(faux.state.callCount, 2, "failed authoring retries; successful pose waits for the learner"); + const results = agent.state.messages.filter((m) => m.role === "toolResult" && m.toolName === "problem"); + assert.equal(results.length, 2); + assert.ok(results[0]?.role === "toolResult" && results[0].isError); + assert.ok(results[1]?.role === "toolResult" && !results[1].isError); + }); + + it("output + question streams, persists natively, replays, and reloads as prose before the question", async () => { + const lesson = "**Synthetic lesson:** four is twice two. $2 + 2 = 4$"; + const quiz = { + type: "check", nodeId: "synthetic", question: "What is two plus two?", + options: [{ label: "Four", value: "4" }, { label: "Five", value: "5" }], + correctAnswer: "4", explanation: "Add two pairs.", + }; + const { engine, faux } = scripted("openrouter", "anthropic/claude-opus-5.5", [ + fauxAssistantMessage([ + fauxThinking("Synthetic private thought."), outputCall(lesson), + fauxToolCall("quiz", quiz, { id: "quiz-1" }), + ], { stopReason: "toolUse" }), + ]); + const chat = chatStore.createChat("Synthetic native output flow"); + const user = newUserMessage("Explain synthetic arithmetic."); + chatStore.saveMessages(chat.id, [user]); + const agent = createTeacherAgent(undefined, { engine, messages: asAgentMessages([user]) }); + const handle = runPiTurn({ + agent, + updateFlushMs: 0, + onFinish: (messages, aborted) => { persistTurn(chatStore, chat.id, 1, messages, aborted); }, + }); + const wire = await new Response(handle.subscribe()).text(); + assert.ok(wire.endsWith("data: [DONE]\n\n")); + assert.equal(faux.state.callCount, 1, "question stops the mixed batch as before"); + const saved = chatStore.getChat(chat.id)!.messages; + const response = saved.find((m) => m.role === "assistant")!; + assert.equal(response.role, "assistant"); + if (response.role !== "assistant") assert.fail("missing assistant"); + assert.deepEqual(response.content.map((c) => c.type), ["thinking", "toolCall", "toolCall"]); + assert.deepEqual(response.content[1], outputCall(lesson)); + assert.ok(saved.some((m) => m.role === "toolResult" && m.toolName === SEND_TO_USER_TOOL), "native output ack persists"); + assert.ok(!saved.some((m) => m.role === "toolResult" && m.toolName === "quiz"), "question placeholder remains transient"); + const providerHistory = toLlmMessages(asAgentMessages(saved)); + assert.deepEqual(providerHistory.find((m) => m.role === "assistant")!.content, response.content, "next request keeps native call/thinking structure"); + assert.deepEqual(prose(projectView(saved)), [lesson]); + const parts = projectView(saved).flatMap((m) => m.parts); + assert.ok(parts.findIndex((p) => p.type === "text") < parts.findIndex((p) => p.type === "tool-quiz")); + const client = new SessionModel({ chatId: chat.id, initialMessages: [user], fetch: globalThis.fetch }); + await client.consumeStream(new Response(wire)); + assert.deepEqual(prose(client.getState().messages), [lesson], "native event replay shows the lesson exactly once"); + assert.deepEqual(prose(projectView(JSON.parse(JSON.stringify(saved)))), [lesson], "reload preserves the lesson"); + assert.deepEqual(teacherProseBlocks(saved), [lesson]); + assert.equal(sessionMarkdown(saved).split(lesson).length - 1, 1); + }); +}); From 90828cef49539d25d4ccac107e1f0a904ac79540 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 02:04:18 +0200 Subject: [PATCH 08/19] fix: scope wake settlement ownership and allow legacy result deletion --- src/server/chat-handler.ts | 7 +-- src/server/chats.ts | 7 ++- src/server/jobs.ts | 23 ++++++--- tests/job-delivery-recovery.test.ts | 72 +++++++++++++++++++++++++++-- 4 files changed, 95 insertions(+), 14 deletions(-) diff --git a/src/server/chat-handler.ts b/src/server/chat-handler.ts index 74ed3c8..8415688 100644 --- a/src/server/chat-handler.ts +++ b/src/server/chat-handler.ts @@ -484,7 +484,8 @@ export async function handleChatInput( // run's first LLM call (the native replacement for continuation // coalescing). Settlement still cannot beat the flow below: it needs // a streamed response, which never happens inside the constructor. - registerLiveAgent(chatId, constructed, input.type !== "steers" && input.type !== "continue"); + registerLiveAgent(chatId, constructed, input.type !== "steers" && input.type !== "continue", + input.type === "steers" ? new Set(appends.map((message) => message.id)) : undefined); // Debug recorder run marker (owner, 2026-10-04): correlates the // JSONL event stream with the record's turns; no-op unless // LH_PROVIDER_EVENTS=1. @@ -525,7 +526,7 @@ export async function handleChatInput( // merely because usage/extraction/other settlement work failed. const persisted = stores.chatStore.getChat(chatId)?.messages ?? []; reconcileUserSteers(chatId, persisted); - settleSteers(chatId, persisted, true); + settleSteers(chatId, persisted, true, constructed); }, onFinish: (turnMessages, isAborted) => { // Wave B: the record write is the agent's own final state — @@ -577,7 +578,7 @@ export async function handleChatInput( // resolve; stranded markers return to pending), then let // the pump take any stragglers down the idle wake. unregisterLiveAgent(chatId); - settleSteers(chatId, stamped); + settleSteers(chatId, stamped, false, constructed); // Composer steers (2026-10-03): delivered ids dropped, stranded // ones stay parked for the chat's next run (the registry is the // stranding safety net — the agent's undrained copy died above). diff --git a/src/server/chats.ts b/src/server/chats.ts index 2e6f2c9..857d3d1 100644 --- a/src/server/chats.ts +++ b/src/server/chats.ts @@ -1,4 +1,5 @@ import { abortExtraction, extractionStatuses } from "../agent/subagents/extraction.ts"; +import { isJobLive } from "../agent/subagents/aborts.ts"; import { coverageOf } from "../core/coverage.ts"; import { deleteChatTraces, type ChatDeleteMode, type ChatDeletionStats } from "../core/graph-deletion.ts"; import { titleFromPrompt } from "../core/contracts.ts"; @@ -77,7 +78,11 @@ function runningWorkBlocker(chatId: string): Extract job.status === "running"); + // Legacy records kept completed workers running until delivery. A saved + // result without a live loop is pending session data, not computing work; + // deletion must not depend on a prior recovery poll to recognize it. + const runningJobs = stores.jobStore.listJobs(chatId).filter((job) => + job.status === "running" && (!job.result || isJobLive(job.id))); if (runningJobs.length > 0) { return { ok: false, diff --git a/src/server/jobs.ts b/src/server/jobs.ts index 98becef..f4b2cd2 100644 --- a/src/server/jobs.ts +++ b/src/server/jobs.ts @@ -88,6 +88,9 @@ interface QueuedSteer { promise: Promise; /** Wake cargo remains owned here, but must not steer into its own run. */ waking?: boolean; + /** The constructed vehicle, once startup claims a turn. Until then only + * the pump owns waking: an unrelated foreground settlement cannot free it. */ + wakeAgent?: Agent; /** A failed start waits for a learner run or process restart, not a timer. */ deferred?: boolean; resolve: () => void; @@ -286,10 +289,13 @@ function handOffEligible(chatId: string, agent: Agent): void { * markers are steered into the run right here. The settle seam cannot * beat the registration: settlement needs a streamed response, which * never happens inside the constructor call. */ -export function registerLiveAgent(chatId: string, agent: Agent, allowDeferred = true): void { +export function registerLiveAgent(chatId: string, agent: Agent, allowDeferred = true, wakeSteerIds?: ReadonlySet): void { state().liveAgents.set(chatId, agent); restorePendingDeliveries(chatId); - if (allowDeferred) for (const entry of state().queues.get(chatId) ?? []) entry.deferred = false; + for (const entry of state().queues.get(chatId) ?? []) { + if (allowDeferred) entry.deferred = false; + if (entry.waking && wakeSteerIds?.has(entry.steer.id)) entry.wakeAgent = agent; + } handOffEligible(chatId, agent); } @@ -369,7 +375,7 @@ export function restoreUncommittedAutomatic(chatId: string, agent: Agent, automa * This is what makes the live hand-off safe: pi owns no retry for a marker * stranded in a settled agent's queue, so the registry stays the truth and * the dead agent's copy is simply abandoned (GC'd with the agent). */ -export function settleSteers(chatId: string, persisted: { id?: string }[], failed = false): void { +export function settleSteers(chatId: string, persisted: { id?: string }[], failed = false, settledAgent?: Agent): void { const queue = state().queues.get(chatId); if (!queue || queue.length === 0) return; const deliveredIds = new Set(persisted.map((m) => m.id).filter(Boolean)); @@ -379,9 +385,11 @@ export function settleSteers(chatId: string, persisted: { id?: string }[], faile finalizeSteer(entry.steer); entry.resolve(); } else { - entry.handedTo = undefined; - entry.waking = false; - if (failed && jobIdOf(entry.steer)) entry.deferred = true; + const handedHere = entry.handedTo !== undefined && (!settledAgent || entry.handedTo === settledAgent); + const wakeHere = settledAgent !== undefined && entry.wakeAgent === settledAgent; + if (handedHere) entry.handedTo = undefined; + if (wakeHere) { entry.waking = false; entry.wakeAgent = undefined; } + if (failed && (handedHere || wakeHere) && jobIdOf(entry.steer)) entry.deferred = true; remaining.push(entry); } } @@ -577,7 +585,7 @@ async function pump(chatId: string): Promise { if (saved) noteSteersDurable(chatId, saved.messages); const owned = fire.filter((entry) => s.queues.get(chatId)?.includes(entry)); if (status === 403 || status === 409) { - for (const entry of owned) entry.waking = false; + for (const entry of owned) { entry.waking = false; entry.wakeAgent = undefined; } if (status === 403) { parked = true; return; } await delay(RETRY_BACKOFF_MS[Math.min(attempt++, RETRY_BACKOFF_MS.length - 1)]!); continue; @@ -627,6 +635,7 @@ state().stopConnectionWatch = onSubscriptionChange(() => { function deferOrReject(chatId: string, entry: QueuedSteer, error: Error): void { entry.waking = false; + entry.wakeAgent = undefined; if (jobIdOf(entry.steer)) { entry.deferred = true; console.error(`[steer ${entry.steer.id}] delivery remains pending:`, error.message); diff --git a/tests/job-delivery-recovery.test.ts b/tests/job-delivery-recovery.test.ts index 25618c7..8e34e6e 100644 --- a/tests/job-delivery-recovery.test.ts +++ b/tests/job-delivery-recovery.test.ts @@ -3,9 +3,10 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { spawnSync } from "node:child_process"; +import { Agent } from "@earendil-works/pi-agent-core"; import { after, afterEach, test } from "node:test"; import { createFauxCore, fauxAssistantMessage } from "@earendil-works/pi-ai"; -import type { LhMessage } from "../src/core/native-messages.ts"; +import { toLlmMessages, type LhMessage } from "../src/core/native-messages.ts"; import { buildSubagentSteer, type JobKind, type SteerMessage } from "../src/core/contracts.ts"; // Install isolation before importing any storage/configuration composition. @@ -186,16 +187,59 @@ test("failed start stays quiet across sweeps, then joins a learner run", async ( test("HTTP acceptance without a saved marker is not delivery completion or a retry trigger", async () => { const owner = chat(); const { job, steer } = pending(owner.id); let attempts = 0; - setTurnRunner(async () => { attempts++; assert(beginTurn(owner.id)); return 200; }); + const accepted = agent(); + setTurnRunner(async () => { + attempts++; assert(beginTurn(owner.id)); + registerLiveAgent(owner.id, accepted.value, false, new Set([steer.id])); + return 200; + }); const delivery = deliverJobResult(steer); await until(() => attempts === 1); await delay(30); assert.equal(stores.jobStore.getJob(job.id)!.delivery?.status, "pending"); - unregisterLiveAgent(owner.id); endTurn(owner.id); settleSteers(owner.id, [], true); + unregisterLiveAgent(owner.id); endTurn(owner.id); settleSteers(owner.id, [], true, accepted.value); sweep(); await delay(900); assert.equal(attempts, 1); const next = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, next.value); assert.equal(next.handed.length, 1); persist(owner.id, next.handed); unregisterLiveAgent(owner.id); endTurn(owner.id); await delivery; }); +test("a racing foreground settlement cannot release an outstanding staged wake", async () => { + const owner = chat(); const { steer } = pending(owner.id); + let started = false; + let release!: () => void; + const startup = new Promise((resolve) => { release = resolve; }); + let modelCopies = 0; + let nativeCopies = 0; + setTurnRunner(async (id, input) => { + assert.equal(input.type, "steers"); if (input.type !== "steers") return 500; + started = true; + // Subscription wake startup can await catalog refresh before claiming + // the session. Its cargo is not yet a durable input prefix. + await startup; + const faux = createFauxCore({}); + faux.setResponses([(context) => { + modelCopies = JSON.stringify(context.messages).split(result.brief).length - 1; + return fauxAssistantMessage("Fixture wake response"); + }]); + const wake = new Agent({ initialState: { model: faux.getModel(), messages: input.steers }, convertToLlm: toLlmMessages, streamFn: faux.streamSimple }); + assert(beginTurn(id)); + registerLiveAgent(id, wake, false, new Set(input.steers.map((message) => message.id))); + await wake.continue(); + nativeCopies = wake.state.messages.filter((message) => (message as LhMessage).id === steer.id).length; + persist(id, wake.state.messages.filter((message) => message.role !== "system") as LhMessage[]); + unregisterLiveAgent(id); endTurn(id); settleSteers(id, stores.chatStore.getChat(id)!.messages, false, wake); + return 200; + }); + const delivery = deliverJobResult(steer); + await until(() => started); + const foreground = agent(); assert(beginTurn(owner.id)); registerLiveAgent(owner.id, foreground.value); + assert.equal(foreground.handed.length, 0, "outstanding wake owns the marker"); + unregisterLiveAgent(owner.id); endTurn(owner.id); settleSteers(owner.id, [], false, foreground.value); + release(); await delivery; + assert.equal(modelCopies, 1, "one result in the model's initial context"); + assert.equal(nativeCopies, 1, "no self-steering of staged wake cargo"); + assert.equal(stores.chatStore.getChat(owner.id)!.messages.filter((message) => message.id === steer.id).length, 1); +}); + test("canonical save wins even if delivery bookkeeping fails; no marker replay", async () => { const owner = chat(true); const { job, steer } = pending(owner.id); const delivery = deliverJobResult(steer); @@ -252,6 +296,28 @@ test("fresh process restores a pending delivery on registration without a jobs p assert.equal(stores.chatStore.getChat(owner.id)!.messages.filter((message) => message.id === steer.id).length, 1); }); +test("deleting legacy completed work needs no preceding recovery poll", async () => { + const owner = chat(true); + const old = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "legacy-delete" }); + stores.jobStore.updateJob(old.id, { result }); + const outcome = await deleteChatSession(owner.id, "preserve-graph"); + assert(outcome.ok); + assert.equal(stores.jobStore.getJob(old.id), null); + assert.equal(stores.chatStore.getChat(owner.id), null); +}); + +test("deletion still refuses a computing worker or a live turn", async () => { + const owner = chat(); + const computing = stores.jobStore.createJob({ kind: "research", chatId: owner.id, toolCallId: "computing-delete" }); + const blocked = await deleteChatSession(owner.id, "preserve-graph"); + assert(!blocked.ok && blocked.code === "jobs_running"); + stores.jobStore.updateJob(computing.id, { status: "done" }); + assert(beginTurn(owner.id)); + const live = await deleteChatSession(owner.id, "preserve-graph"); + assert(!live.ok && live.code === "turn_in_flight"); + endTurn(owner.id); +}); + test("deleting a session discards its pending deliveries without starting a turn", async () => { const owner = chat(true); const { job, steer } = pending(owner.id); let wakes = 0; setTurnRunner(async () => { wakes++; return 500; }); From a19f53b2b45027f0b1a531d11527f8a45d38cc53 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 02:57:33 +0200 Subject: [PATCH 09/19] test: preserve PDF history guards alongside canonical result delivery --- tests/jobs.test.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/jobs.test.ts b/tests/jobs.test.ts index d8b877a..be5478b 100644 --- a/tests/jobs.test.ts +++ b/tests/jobs.test.ts @@ -801,6 +801,7 @@ describe("pdf runner (sources v0)", () => { const toolCallId = `call-pdf-relative-${mode}`; const input = { ...PDF_INPUT, path: relativePath }; const messages = nativeToolMessages("read this pdf", "pdf", toolCallId, input); + const before = structuredClone(messages); chatStore.saveMessages(chat.id, messages); const job = jobStore.createJob({ kind: "pdf", chatId: chat.id, toolCallId }); trackJob(job.id); @@ -813,15 +814,31 @@ describe("pdf runner (sources v0)", () => { pdfModelOverride.current = undefined; } + // Worker completion is not the delivery barrier: wait for canonical + // acceptance before asserting the history prefix and appended result. + await waitFor(() => jobStore.getJob(job.id)?.delivery?.status === "delivered", 8000, "canonical PDF delivery acknowledgement"); const done = jobStore.getJob(job.id)!; assert.equal(done.status, "done"); + assert.deepEqual(done.delivery, { kind: "result", status: "delivered" }); const result = done.result as PdfResult; assert.equal(result.path, path.join(home, relativePath)); assert.match(result.brief, /integral rule/); assert.deepEqual(result.pagesRead, [1, 2]); assert.deepEqual(result.pagesConsulted, [1, 2]); assert.equal(delivery.steers.length, 1); - assert.deepEqual(chatStore.getChat(chat.id)!.messages, messages, "tool-call arguments remain as supplied"); + assert.equal(input.path, relativePath, "dispatch input remains relative"); + const persisted = chatStore.getChat(chat.id)!.messages; + assert.deepEqual(persisted.slice(0, before.length), before, "original history and tool-call arguments remain as supplied"); + const steer = delivery.steers[0]!; + assert.equal(steer.id, `steer_${job.id}`); + assert.equal(steer.role, "custom"); + assert.equal(steer.customType, "subagent-steer"); + const details = steer.details as { jobId: string; kind: string; toolCallId: string; result: PdfResult }; + assert.equal(details.jobId, job.id); + assert.equal(details.kind, "pdf"); + assert.equal(details.toolCallId, toolCallId); + assert.deepEqual(details.result, result, "the canonical steer carries the resolved PDF result"); + assert.deepEqual(persisted.slice(before.length), [steer], "exactly one captured result is appended after the unchanged history"); }); } From c0c670b810509a0cd265648d16a5e36141291c70 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 03:00:40 +0200 Subject: [PATCH 10/19] feat: enable subscription connections in web and dev --- .gitignore | 3 + app/api/subscriptions/browser/route.ts | 6 + app/api/subscriptions/route.ts | 6 + components/server-events.tsx | 3 + components/settings/chatgpt-connection.tsx | 23 +-- components/settings/subscription-client.ts | 77 ++++++++++ .../settings/subscription-connections.tsx | 20 +-- components/sidebar.tsx | 3 + docs/architecture.md | 6 +- docs/development.md | 11 ++ electron/main.cjs | 9 ++ electron/preload.cjs | 4 + electron/web-subscription-browser.cjs | 19 +++ next.config.ts | 3 + scripts/build-server.mjs | 3 +- scripts/smoke-web-subscriptions.mjs | 125 ++++++++++++++++ src/agent/chatgpt-bridge.ts | 20 ++- src/agent/subscription-bridge.ts | 13 +- src/agent/subscription-runtime.ts | 30 ++++ src/core/events.ts | 3 + src/server/index.ts | 1 + src/server/web-subscription-http.ts | 68 +++++++++ src/server/web-subscriptions.ts | 127 ++++++++++++++++ tests/chatgpt-dev-ipc.test.ts | 8 +- tests/desktop-platform.test.ts | 24 ++- tests/provider-tracing.test.ts | 4 +- tests/web-subscription-client.test.tsx | 99 +++++++++++++ tests/web-subscription-payload.test.ts | 21 +++ tests/web-subscriptions.test.ts | 140 ++++++++++++++++++ 29 files changed, 838 insertions(+), 41 deletions(-) create mode 100644 app/api/subscriptions/browser/route.ts create mode 100644 app/api/subscriptions/route.ts create mode 100644 components/settings/subscription-client.ts create mode 100644 electron/web-subscription-browser.cjs create mode 100644 scripts/smoke-web-subscriptions.mjs create mode 100644 src/agent/subscription-runtime.ts create mode 100644 src/server/web-subscription-http.ts create mode 100644 src/server/web-subscriptions.ts create mode 100644 tests/web-subscription-client.test.tsx create mode 100644 tests/web-subscription-payload.test.ts create mode 100644 tests/web-subscriptions.test.ts diff --git a/.gitignore b/.gitignore index 320a543..5f72ce7 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ dist/ # dev auth/keys store (auth-keys wave): repo-root auth.json — secrets never committed auth.json +chatgpt-credentials.json* +github-copilot-credentials.json* +xai-credentials.json* # dev-fresh launcher scratch (fresh-user sim): disposable per launch .fresh-data/ diff --git a/app/api/subscriptions/browser/route.ts b/app/api/subscriptions/browser/route.ts new file mode 100644 index 0000000..760380f --- /dev/null +++ b/app/api/subscriptions/browser/route.ts @@ -0,0 +1,6 @@ +import { subscriptionBrowserResponse } from "@/src/server"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; +export const GET = subscriptionBrowserResponse; +export const POST = subscriptionBrowserResponse; diff --git a/app/api/subscriptions/route.ts b/app/api/subscriptions/route.ts new file mode 100644 index 0000000..3674db5 --- /dev/null +++ b/app/api/subscriptions/route.ts @@ -0,0 +1,6 @@ +import { subscriptionControlsResponse } from "@/src/server"; + +export const runtime = "nodejs"; +export const dynamic = "force-dynamic"; +export const GET = subscriptionControlsResponse; +export const POST = subscriptionControlsResponse; diff --git a/components/server-events.tsx b/components/server-events.tsx index bb0331b..0c8cd16 100644 --- a/components/server-events.tsx +++ b/components/server-events.tsx @@ -116,6 +116,9 @@ export function wireSource(source: NotifySource, hub: ReturnType | null>(null); +/** Transport adapters may also be mounted in isolation with an injected bridge. */ +export function useNotifyHub() { return useContext(EventsContext); } + export function ServerEventsProvider({ children, connect }: { children: ReactNode; connect?: () => NotifySource }) { const hub = useMemo(() => createNotifyHub(), []); useEffect(() => wireSource((connect ?? openEventsChannel)(), hub), [hub, connect]); diff --git a/components/settings/chatgpt-connection.tsx b/components/settings/chatgpt-connection.tsx index 3e368dd..f9d2d69 100644 --- a/components/settings/chatgpt-connection.tsx +++ b/components/settings/chatgpt-connection.tsx @@ -5,8 +5,10 @@ import type { ChatGPTStatus } from "@/src/core"; import { ConnectionDialog, ConnectionFooter, ConnectionRow } from "./connection-controls"; import { Button } from "@/components/ui/button"; import { Toggle } from "./controls"; +import { useChatGPTBridge } from "./subscription-client"; export const CHATGPT_NOTICE = "LearnHarness stores your ChatGPT account details and credentials in an unencrypted local file on this device. When this connection is used, relevant conversation context, learning material, and tool results are sent directly to OpenAI. ChatGPT plan limits apply; any additional credits are authorized and managed in ChatGPT, not LearnHarness."; +export function chatGPTNotice(web = false): string { return web ? `${CHATGPT_NOTICE.replace("on this device", "on the machine running LearnHarness")} Complete ChatGPT sign-in in a browser on that same machine.` : CHATGPT_NOTICE; } export const CHATGPT_BACKGROUND_HINT = "Background work counts toward your ChatGPT plan usage."; const BACKGROUND_LABEL = "Allow background use for all ChatGPT accounts"; const REVOCATION_WARNING = "Account disconnected locally. Remote revocation could not be confirmed."; @@ -41,6 +43,8 @@ const Context = createContext({ /** UI lifetime spans setup's keys → models transition. Only existing safe controls * are used; account IDs remain control values, never displayed account labels. */ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { + const bridge = useChatGPTBridge(); + const web = typeof window !== "undefined" && bridge !== window.lh?.chatgpt; const [status, setStatus] = useState(empty); const [dialog, setDialog] = useState(null); const [busy, setBusy] = useState(false); @@ -79,14 +83,14 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { if (attempt.backgroundDirty && next.allowBackgroundUse !== attempt.background) { operation.current = "change"; const request = ++statusSequence.current; - const result: unknown = await window.lh!.chatgpt!.consent(attempt.background); + const result: unknown = await bridge!.consent(attempt.background); if (!owns(attempt)) return; if (!isStatus(result)) { setError(replyError(result)); finish(attempt); return; } replyFailure = result.error; let saved: ChatGPTStatus | null; if (request !== statusSequence.current) { const refresh = ++statusSequence.current; - const value: unknown = await window.lh!.chatgpt!.status(); + const value: unknown = await bridge!.status(); if (!owns(attempt)) return; saved = refresh === statusSequence.current ? publishStatus(value) : statusRef.current; } else saved = publishStatus(result); @@ -120,7 +124,6 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { }; useEffect(() => { mounted.current = true; - const bridge = window.lh?.chatgpt; const refresh = async () => { const request = ++statusSequence.current; try { @@ -131,7 +134,7 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { void refresh(); const off = bridge?.onChange(() => void refresh()); return () => { mounted.current = false; generation.current++; loginAttempt.current = null; off?.(); }; - }, []); + }, [bridge]); const managed = dialog?.kind === "manage" ? status.accounts.find((a) => a.id === dialog.accountId) ?? dialog.account : undefined; const managedPresent = !!managed && status.accounts.some((a) => a.id === managed.id); @@ -161,13 +164,13 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { // Keep cancellation serialized even after login's pending IPC reply. operation.current = "cancel"; setBusy(true); const request = ++statusSequence.current; - void window.lh?.chatgpt?.cancel().then(async (value: unknown) => { + void bridge?.cancel().then(async (value: unknown) => { if (!mounted.current || generation.current !== intent) return; if (!isStatus(value)) { setError(replyError(value)); return; } let next: ChatGPTStatus | null; if (request !== statusSequence.current) { const refresh = ++statusSequence.current; - const current: unknown = await window.lh!.chatgpt!.status(); + const current: unknown = await bridge!.status(); if (!mounted.current || generation.current !== intent) return; next = refresh === statusSequence.current ? publishStatus(current) : statusRef.current; } else next = publishStatus(value); @@ -195,7 +198,7 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { let next: ChatGPTStatus | null; if (request !== statusSequence.current) { const refresh = ++statusSequence.current; - const current: unknown = await window.lh!.chatgpt!.status(); + const current: unknown = await bridge!.status(); if (!mounted.current || intent !== generation.current) return; next = refresh === statusSequence.current ? publishStatus(current) : statusRef.current; } else next = publishStatus(value); @@ -214,7 +217,6 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { finally { if (intent === generation.current) { operation.current = null; if (mounted.current) setBusy(false); } } } async function login() { - const bridge = window.lh?.chatgpt; const owner = dialogRef.current; if (!bridge || operation.current || !status.available || status.pending || owner?.kind !== "connect") return; const attempt: LoginAttempt = { generation: ++generation.current, dialog: owner, registration, background, backgroundDirty, pending: false, phase: "starting" }; @@ -241,7 +243,6 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { // No finally: pending OAuth outlives this IPC promise. Only its status // completion, failure, or explicit cancellation releases the attempt. } - const bridge = typeof window !== "undefined" ? window.lh?.chatgpt : undefined; const context = { status, busy, error: warning ? null : visibleError, inlineError: dialog || warning ? null : visibleError, warning: dialog?.kind === "warning" ? null : warning, connect: openConnect, @@ -262,7 +263,7 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { : dialog.kind === "manage" && dialog.confirm ?

{CHATGPT_DISCONNECT_COPY.title}

{managed?.email || "ChatGPT account"}

-

{CHATGPT_DISCONNECT_COPY.body}

+

{web ? CHATGPT_DISCONNECT_COPY.body.replace("from this device", "from the machine running LearnHarness") : CHATGPT_DISCONNECT_COPY.body}

{context.error &&

{context.error}

}
@@ -277,7 +278,7 @@ export function ChatGPTUIProvider({ children }: { children: React.ReactNode }) { {registration && }
} -

{CHATGPT_NOTICE}

+

{chatGPTNotice(web)}

: null}
button:disabled]:cursor-auto" : ""}`} data-testid="chatgpt-global-background"> { diff --git a/components/settings/subscription-client.ts b/components/settings/subscription-client.ts new file mode 100644 index 0000000..a580a08 --- /dev/null +++ b/components/settings/subscription-client.ts @@ -0,0 +1,77 @@ +"use client"; + +import { useMemo } from "react"; +import type { ChatGPTControlBridge, ChatGPTStatus, NativeSubscriptionControlBridge, NativeSubscriptionProviderId, NativeSubscriptionStatus } from "@/src/core"; +import { useNotifyHub, type createNotifyHub } from "@/components/server-events"; + +type Hub = ReturnType | null; +type Reply = { value: T; browserAction?: string; error?: string }; +const PATH = "/api/subscriptions"; +const headers = { "Content-Type": "application/json", "x-learnharness-subscriptions": "1" }; + +async function status() { + const response = await fetch(PATH, { headers, cache: "no-store" }); + if (!response.ok) throw new Error("Subscription status is unavailable."); + return response.json() as Promise<{ chatgpt: ChatGPTStatus; native: NativeSubscriptionStatus[] }>; +} +async function control(providerId: string, op: string, fields: Record = {}): Promise> { + const response = await fetch(PATH, { method: "POST", headers, body: JSON.stringify({ providerId, op, ...fields }) }); + if (!response.ok) throw new Error("The subscription connection could not be changed."); + return response.json() as Promise>; +} +function onChange(hub: Hub, listener: () => void): () => void { + const offEvent = hub?.subscribe("subscriptions-changed", listener); + const offResync = hub?.onResync(listener); + return () => { offEvent?.(); offResync?.(); }; +} +/** Reserve the new tab synchronously in the click gesture. Only a one-use + * same-origin action URL enters app JS; provider URLs remain in the owner. + * Electron dev has no tabs: its narrow main-process opener performs this step. */ +async function withBrowser(action: () => Promise>): Promise { + const external = window.lh?.subscriptionTransport === "web" ? window.lh.openSubscriptionBrowser : undefined; + const tab = external ? null : window.open("about:blank", "_blank"); + if (!external && !tab) throw new Error("Allow a sign-in tab to open, then try again."); + if (tab) tab.opener = null; + try { + const reply = await action(); + if (reply.browserAction) { + if (!/^\/api\/subscriptions\/browser\?id=[0-9a-f-]{36}$/.test(reply.browserAction)) throw new Error("Invalid sign-in action."); + if (external) await external(reply.browserAction); + else if (tab && !tab.closed) tab.location.href = reply.browserAction; + else throw new Error("The sign-in tab was closed. Cancel and try again."); + } else tab?.close(); + return reply.value; + } catch (error) { tab?.close(); throw error; } +} +export function createWebChatGPTBridge(hub: Hub): ChatGPTControlBridge { + const call = async (op: string, fields?: Record) => (await control("chatgpt", op, fields)).value; + return { + status: async () => (await status()).chatgpt, + login: (id) => withBrowser(() => control("chatgpt", "login", { id })), + cancel: () => call("cancel"), select: (id) => call("select", { id }), + disconnect: (id) => call("disconnect", { id }), logout: (id) => call("logout", { id }), + consent: (enabled) => call("consent", { enabled }), + onChange: (listener) => onChange(hub, listener), + }; +} +export function createWebNativeBridge(hub: Hub): NativeSubscriptionControlBridge { + const call = async (providerId: NativeSubscriptionProviderId, op: string, fields?: Record) => { await control(providerId, op, fields); }; + return { + status: async () => (await status()).native, + login: (providerId, enabled) => call(providerId, "login", { enabled }), + cancel: (providerId) => call(providerId, "cancel"), + respond: (providerId, attemptId, promptId, value) => call(providerId, "respond", { attemptId, promptId, value }), + open: async (providerId, attemptId, actionId) => { await withBrowser(() => control(providerId, "open", { attemptId, actionId })); }, + retry: (providerId) => call(providerId, "retry"), disconnect: (providerId) => call(providerId, "disconnect"), + consent: (providerId, enabled) => call(providerId, "consent", { enabled }), + onChange: (listener) => onChange(hub, listener), + }; +} +export function useChatGPTBridge(): ChatGPTControlBridge | undefined { + const hub = useNotifyHub(); + return useMemo(() => typeof window === "undefined" ? undefined : window.lh?.subscriptionTransport !== "web" && window.lh?.chatgpt ? window.lh.chatgpt : createWebChatGPTBridge(hub), [hub]); +} +export function useNativeSubscriptionBridge(): NativeSubscriptionControlBridge | undefined { + const hub = useNotifyHub(); + return useMemo(() => typeof window === "undefined" ? undefined : window.lh?.subscriptionTransport !== "web" && window.lh?.subscriptions ? window.lh.subscriptions : createWebNativeBridge(hub), [hub]); +} diff --git a/components/settings/subscription-connections.tsx b/components/settings/subscription-connections.tsx index 0120200..6c095d1 100644 --- a/components/settings/subscription-connections.tsx +++ b/components/settings/subscription-connections.tsx @@ -6,16 +6,19 @@ import { notifySettingsSaved } from "@/components/settings-signal"; import { ConnectionDialog, ConnectionFooter, ConnectionRow } from "./connection-controls"; import { ChatGPTConnection } from "./chatgpt-connection"; import { Toggle } from "./controls"; +import { useNativeSubscriptionBridge } from "./subscription-client"; export const COPILOT_POLICY_NOTICE = "Pi's native Copilot sign-in may enable additional models whose policy is not yet configured. Models explicitly disabled by policy are not enabled. Organization restrictions still apply."; export const SUBSCRIPTION_BACKGROUND_HINT = "Allow LearnHarness helper agents and automatic follow-ups to use this connection."; -export function subscriptionNotice(label: string): string { return `Your prompts, tool results, and relevant session context are sent to ${label} to run the work you select. Credentials are stored locally in an owner-only plaintext file. Provider terms, data-use policies, and plan limits apply. This connection never falls back to an API key.`; } -export function subscriptionDisconnectCopy(label: string): string { return `Disconnect ${label} from LearnHarness? This stops work using this connection and removes its saved credentials from this device. It does not cancel your subscription or revoke access at the provider. Saved model choices will remain unavailable until you reconnect or change them.`; } +export function subscriptionNotice(label: string, web = false): string { return `Your prompts, tool results, and relevant session context are sent to ${label} to run the work you select. Credentials are stored ${web ? "on the machine running LearnHarness" : "locally"} in an owner-only plaintext file. Provider terms, data-use policies, and plan limits apply. This connection never falls back to an API key.`; } +export function subscriptionDisconnectCopy(label: string, web = false): string { return `Disconnect ${label} from LearnHarness? This stops work using this connection and removes its saved credentials from ${web ? "the machine running LearnHarness" : "this device"}. It does not cancel your subscription or revoke access at the provider. Saved model choices will remain unavailable until you reconnect or change them.`; } export function emptySubscriptionStatus(providerId: NativeSubscriptionProviderId): NativeSubscriptionStatus { return { providerId, available: false, connected: false, pending: false, allowBackgroundUse: true, connectionId: null, epoch: 0, authorityRevision: 0, revision: 0 }; } const Context = createContext({ statuses: [] as NativeSubscriptionStatus[], busy: false, open: (_providerId: NativeSubscriptionProviderId) => {} }); const CONTROL_ERROR = "The connection could not be changed. Try again."; export function NativeSubscriptionUIProvider({ children }: { children: React.ReactNode }) { + const bridge = useNativeSubscriptionBridge(); + const web = typeof window !== "undefined" && bridge !== window.lh?.subscriptions; const [statuses, setStatuses] = useState([]); const [dialog, setDialog] = useState(null); const [view, setView] = useState<"notice" | "connection" | "manage">("notice"); @@ -30,7 +33,7 @@ export function NativeSubscriptionUIProvider({ children }: { children: React.Rea async function refresh() { const request = ++sequence.current; try { - const values = await window.lh?.subscriptions?.status(); + const values = await bridge?.status(); if (!mounted.current || request !== sequence.current || !Array.isArray(values)) return; const accepted = NATIVE_SUBSCRIPTIONS.map(({ providerId }) => { const next = values.find((s) => s.providerId === providerId), old = statusesRef.current.find((s) => s.providerId === providerId); @@ -41,10 +44,10 @@ export function NativeSubscriptionUIProvider({ children }: { children: React.Rea } useEffect(() => { mounted.current = true; void refresh(); - const off = window.lh?.subscriptions?.onChange(() => { void refresh(); notifySettingsSaved("keys"); }); + const off = bridge?.onChange(() => { void refresh(); notifySettingsSaved("keys"); }); return () => { mounted.current = false; generation.current++; off?.(); }; // The root-lifetime subscriber reads current state through refs. - }, []); + }, [bridge]); useEffect(() => { setPromptValue(""); }, [prompt?.id]); function open(providerId: NativeSubscriptionProviderId) { if (operation || statusesRef.current.some((s) => s.pending)) return; @@ -62,7 +65,6 @@ export function NativeSubscriptionUIProvider({ children }: { children: React.Rea function close() { if (!dialog || busy) return; if (confirm) { setConfirm(false); setError(null); return; } - const bridge = window.lh?.subscriptions; if ((status?.pending || operation === "login") && bridge) { void control(() => bridge.cancel(dialog), true); return; } generation.current++; setDialog(null); setError(null); } @@ -71,23 +73,21 @@ export function NativeSubscriptionUIProvider({ children }: { children: React.Rea generation.current++; setError(null); setConfirm(false); setBackground(status.allowBackgroundUse); setBackgroundDirty(false); setView("notice"); } async function login() { - const bridge = window.lh?.subscriptions; if (!dialog || !bridge || operation || !status?.available) return; const owner = ++generation.current; setOperation("login"); setError(null); try { await bridge.login(dialog, backgroundDirty ? background : undefined); await refresh(); if (mounted.current && generation.current === owner) setDialog(null); } catch { if (mounted.current && generation.current === owner) setError("Sign-in did not complete. Try connecting again."); } finally { if (mounted.current && generation.current === owner) setOperation(null); } } - const bridge = typeof window === "undefined" ? undefined : window.lh?.subscriptions; return {children} {dialog && descriptor && status &&

{descriptor.label}

{confirm ? <>

Disconnect {descriptor.label}?

-

{subscriptionDisconnectCopy(descriptor.label)}

+

{subscriptionDisconnectCopy(descriptor.label, web)}

: view === "notice" ? <> -

{subscriptionNotice(descriptor.label)}

+

{subscriptionNotice(descriptor.label, web)}

{dialog === "github-copilot" &&

{COPILOT_POLICY_NOTICE}

} : <>
diff --git a/components/sidebar.tsx b/components/sidebar.tsx index 6ca9c21..508494a 100644 --- a/components/sidebar.tsx +++ b/components/sidebar.tsx @@ -61,6 +61,9 @@ declare global { lh?: { /** Read-only host OS; determines the native-control gutter. */ platform?: string; + /** External Next desktop-dev uses the server's web connections. */ + subscriptionTransport?: "desktop" | "web"; + openSubscriptionBrowser?: (action: string) => Promise; chatgpt?: ChatGPTControlBridge; subscriptions?: NativeSubscriptionControlBridge; expandWindow: () => void; diff --git a/docs/architecture.md b/docs/architecture.md index ed7d869..f982887 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -425,9 +425,11 @@ The Electron main is a launcher + credential/security guard, never a host for te ### 8.1 Subscription connection boundary -`electron/chatgpt.cjs` retains the existing verified-account ChatGPT authentication, storage and revocation behavior. `electron/subscriptions.cjs` delegates GitHub Copilot/Grok login and refresh to the installed Pi SDK through its public provider/Models interfaces. Their single saved connections live independently in owner-only, atomically written `github-copilot-credentials.json` / `xai-credentials.json` beside the existing app credential files. Electron main alone owns durable OAuth credentials and serialized refresh; no global Pi files or API-key credential precedence are involved. +`electron/chatgpt.cjs` retains the existing verified-account ChatGPT authentication, storage and revocation behavior. `electron/subscriptions.cjs` delegates GitHub Copilot/Grok login and refresh to the installed Pi SDK through its public provider/Models interfaces. Their single saved connections live independently in owner-only, atomically written `github-copilot-credentials.json` / `xai-credentials.json` beside the existing app credential files. In packaged desktop mode Electron main alone owns durable OAuth credentials and serialized refresh; no global Pi files or API-key credential precedence are involved. -The renderer gets projected status, public device user codes, prompts and controls over origin-pinned IPC. The privately launched server child receives credential-free catalogs and scoped request auth (including native headers/account endpoints), never refresh tokens. `src/agent/subscription-bridge.ts` rejects framework-only IPC and invalidates stale connection/catalog scopes. `subscription-stream.ts` dispatches directly through native providers, retaining native protocol identity on the wire and restoring logical app identity in assistant history. Persisted routes `chatgpt`, `github-copilot-subscription` and `xai-subscription` are distinct from ordinary API routes and never fall back to them. +The packaged desktop renderer gets projected status, public device user codes, prompts and controls over origin-pinned IPC. The privately launched server child receives credential-free catalogs and scoped request auth (including native headers/account endpoints), never refresh tokens. `src/agent/subscription-bridge.ts` rejects framework-only IPC and invalidates stale connection/catalog scopes. `subscription-stream.ts` dispatches directly through native providers, retaining native protocol identity on the wire and restoring logical app identity in assistant history. Persisted routes `chatgpt`, `github-copilot-subscription` and `xai-subscription` are distinct from ordinary API routes and never fall back to them. + +For ordinary Next web/dev, `src/server/web-subscriptions.ts` composes these same plain-Node controllers into one lazy process-global credential owner beside configured `AUTH_FILE`. The server injects the private transport through `src/agent/subscription-runtime.ts`; the agent layer never imports the server layer. Existing scoped leases, authority revisions and foreground/background abort listeners work through that transport, without exposing an HTTP token/lease operation. `src/server/web-subscription-http.ts` exposes only same-origin status/control operations. Generic `subscriptions-changed` notifications use the existing SSE notify→fetch channel; opaque one-use browser actions redirect to provider pages without putting authorization URLs in app JSON. Browser adapters preserve the UI bridge contracts; Electron dev uses the web owner with a narrow system-browser opener. Desktop markers fail closed rather than falling back to web credentials. This is the existing shared developer-server trust model, not user authentication or DNS-rebinding protection; LAN behavior is unchanged. Only one server process may own an auth directory. See [development](development.md#subscription-connections-in-webdev) for storage, browser locality and access limitations. `subscription-request-cycle.ts` is the shared admission/automatic-input recovery state machine; the old ChatGPT module is its compatibility export. Background consent is independent per provider, default ON, with foreground-preserving cancellation and queue ownership retained on no-dispatch. Native usage rejected before dispatch is transiently excluded from helper usage collectors, without adding persisted provenance fields. Catalogs and read-time pricing preserve the selected connection: subscription use displays compact provider labels, API dollars remain dollars, and mixed usage joins both without estimating subscription dollars or claiming detected tiers/allowances. diff --git a/docs/development.md b/docs/development.md index 062a72e..1859053 100644 --- a/docs/development.md +++ b/docs/development.md @@ -22,6 +22,7 @@ npm start # serve the production build npm test # the full suite (node:test via tsx; deterministic, no LLM, no network) npm run lint # eslint — includes the import law (architecture §2) npm run typecheck # tsc --noEmit +node scripts/smoke-web-subscriptions.mjs # after build: offline Next start/dev + reload gate npm run desktop:dev # next dev + Electron shell pointed at it npm run desktop:dist # local signed/notarized macOS arm64 build (LH_NOTARY keychain) @@ -47,6 +48,16 @@ Secrets and machine-specific paths only. Model choice and thinking budgets are t The packaged desktop app takes keys from `userData/auth.json` (BYOK, mode 0600, read/written directly by the server via `LH_AUTH_FILE`). PDFs render natively with pdfjs/Skia; SVGs use resvg. +### Subscription connections in web/dev + +Ordinary `npm run dev` and `npm start` support the existing **ChatGPT, GitHub Copilot and Grok** connections in Settings → Providers. No separate launcher or change to Next's listening/LAN behavior is required. `npm run desktop:dev` uses these same dev-server connections; the packaged desktop app keeps its separate main-process credentials and private IPC transport. + +Web/dev saves `chatgpt-credentials.json`, `github-copilot-credentials.json` and `xai-credentials.json` **beside the configured API-key `auth.json`**: the project directory by default, or the directory containing `LH_AUTH_FILE`. These are owner-only plaintext files, not files under `data/`, desktop Application Support, or a global Pi profile. Git and standalone-build exclusions cover both the files and atomic-write temporary files. Run only one server process per credential directory: refresh serialization is process-local, not a cross-process lock. + +Sign-in opens a new tab in the browser that initiated it; permit that tab if your browser blocks popups. **ChatGPT sign-in must be completed in a browser on the server's machine**, because its callback listener remains `127.0.0.1` on that machine. Copilot/Grok use device codes. In the Electron dev shell, provider pages open in the system browser instead. Once connected, credentials and refresh remain server-owned; other browsers using that server share its connections and background-use consent, just as they share its API keys. Disconnect, storage-failure handling, and provider-specific background consent retain the existing behavior; subscription routes never fall back to API-key billing. + +This remains a **trusted developer/shared-server mode**, not multi-user hosting. Subscription controls require same-origin JSON/custom-header requests; browser-opening actions are short-lived, origin-bound and one-use. These checks prevent ordinary cross-origin browser controls, **not user authentication or DNS-rebinding protection**. Do not expose an unprotected dev/server instance to untrusted users or the public Internet; a public deployment needs an independent access/host gate. Remote ChatGPT callback routing, per-user accounts and credential import are not implemented. + ## Architecture Five layers, dependency arrows pointing inward only, machine-enforced as four ESLint zones (shell → barrels only; core and agent never import below themselves). Two composition roots, one writer per store. The full spec — layers, systems, runtime flows, invariants, seams: [`docs/architecture.md`](docs/architecture.md). diff --git a/electron/main.cjs b/electron/main.cjs index f2c2d7d..a853736 100644 --- a/electron/main.cjs +++ b/electron/main.cjs @@ -30,6 +30,7 @@ const { attachChatGPTChild, trustedSender, chatGPTControl } = require("./chatgpt const { createSubscriptions, loadSubscriptionSDK } = require("./subscriptions.cjs"); const { attachSubscriptionChild, subscriptionControl } = require("./subscription-ipc.cjs"); const { desktopAPIHeaders } = require("./desktop-api.cjs"); +const { openWebSubscriptionBrowser } = require("./web-subscription-browser.cjs"); const { randomBytes } = require("node:crypto"); const { spawn } = require("node:child_process"); const fs = require("node:fs"); @@ -315,6 +316,7 @@ function createWindow(url) { contextIsolation: true, sandbox: true, nodeIntegration: false, + ...(process.env.LH_DEV_URL ? { additionalArguments: ["--lh-web-subscriptions"] } : {}), preload: path.join(__dirname, "preload.cjs"), }, // Live-resize + first-paint flash (owner, 2026-09-17): during a resize @@ -700,6 +702,13 @@ async function main() { return subscriptionControl(command, subscriptions); }); + // The dev shell has no browser tabs. Only a one-use action on its own + // Next server may be resolved here; never accept an arbitrary external URL. + ipcMain.handle("lh:subscriptions:web-browser", async (event, action) => { + if (!devUrl || !trustedAppWindows.has(BrowserWindow.fromWebContents(event.sender)) || !trustedSender(event, new URL(url).origin, BrowserWindow)) throw new Error("Untrusted development request."); + await openWebSubscriptionBrowser(action, new URL(url).origin, { openExternal: (target) => shell.openExternal(target) }); + }); + installMenu(); // Auto-update (Wave C): wire the updater before the window so the diff --git a/electron/preload.cjs b/electron/preload.cjs index 9294291..a07fe74 100644 --- a/electron/preload.cjs +++ b/electron/preload.cjs @@ -40,6 +40,10 @@ ipcRenderer.on("lh:open-settings", (_event, section) => { contextBridge.exposeInMainWorld("lh", { platform: process.platform, + ...(process.argv?.includes("--lh-web-subscriptions") ? { + subscriptionTransport: "web", + openSubscriptionBrowser: (action) => ipcRenderer.invoke("lh:subscriptions:web-browser", action), + } : {}), chatgpt: { status: () => ipcRenderer.invoke("lh:chatgpt:control", { op: "status" }), login: (id) => ipcRenderer.invoke("lh:chatgpt:control", { op: "login", id }), diff --git a/electron/web-subscription-browser.cjs b/electron/web-subscription-browser.cjs new file mode 100644 index 0000000..b61c73b --- /dev/null +++ b/electron/web-subscription-browser.cjs @@ -0,0 +1,19 @@ +"use strict"; + +/** Desktop-dev has no tabs. Resolve only an opaque action on its own Next + * server, then open the owner-validated provider page outside the renderer. + * The caller must authenticate its trusted app main frame first. */ +async function openWebSubscriptionBrowser(action, origin, { fetch: request = globalThis.fetch, openExternal }) { + if (typeof action !== "string" || !/^\/api\/subscriptions\/browser\?id=[0-9a-f-]{36}$/.test(action)) throw new Error("Invalid sign-in action."); + const response = await request(new URL(action, origin), { + method: "POST", redirect: "manual", credentials: "omit", + headers: { origin, "x-learnharness-subscriptions": "1" }, + signal: AbortSignal.timeout(15000), + }); + const target = response.headers.get("location"); + if (response.status !== 303 || !target) throw new Error("Sign-in action expired."); + const parsed = new URL(target); + if (parsed.protocol !== "https:" || parsed.username || parsed.password) throw new Error("Invalid provider page."); + await openExternal(target); +} +module.exports = { openWebSubscriptionBrowser }; diff --git a/next.config.ts b/next.config.ts index 94193d8..a4236ad 100644 --- a/next.config.ts +++ b/next.config.ts @@ -48,6 +48,9 @@ const nextConfig: NextConfig = { "*": [ "**/data/**/*", "auth.json", + "chatgpt-credentials.json*", + "github-copilot-credentials.json*", + "xai-credentials.json*", ".env*", ], }, diff --git a/scripts/build-server.mjs b/scripts/build-server.mjs index bbb1480..266ff75 100644 --- a/scripts/build-server.mjs +++ b/scripts/build-server.mjs @@ -52,7 +52,8 @@ if (exists(leakedData)) { // (the DEV BYOK key store, 0600) byte-identical into the DMG (caught in // the 2026-10-08 payload audit; excluded in next.config.ts — this guard // makes a regression loud instead of silent). -for (const secret of ["auth.json", ".env", ".env.local"]) { +const subscriptionSecrets = fs.readdirSync(STANDALONE).filter((name) => /^(chatgpt|github-copilot|xai)-credentials\.json/.test(name)); +for (const secret of ["auth.json", ...subscriptionSecrets, ".env", ".env.local"]) { if (exists(path.join(STANDALONE, secret))) { console.error(`[build-server] REFUSING: standalone contains ${secret} — dev secrets would ship in the app.`); console.error(" Check outputFileTracingExcludes in next.config.ts."); diff --git a/scripts/smoke-web-subscriptions.mjs b/scripts/smoke-web-subscriptions.mjs new file mode 100644 index 0000000..9918b65 --- /dev/null +++ b/scripts/smoke-web-subscriptions.mjs @@ -0,0 +1,125 @@ +/** Offline Next runtime gate (run after next build, with no dev server running). + * Proves cold-start registration, cross-route owner/events and dev reloads using + * synthetic credentials in a fresh profile. No OAuth, provider traffic or real + * auth/data. The dev reload changes only a source mtime, restoring it afterward. + * This is a test, not an alternative app launcher. */ +import assert from "node:assert/strict"; +import fs from "node:fs/promises"; +import path from "node:path"; +import os from "node:os"; +import { pathToFileURL } from "node:url"; +import { createRequire } from "node:module"; +import { spawn } from "node:child_process"; +import { createServer } from "node:net"; + +const ROOT = path.resolve(import.meta.dirname, ".."); +const require = createRequire(import.meta.url); +const { SCOPES } = require("../electron/chatgpt.cjs"); +const sdk = (file) => import(pathToFileURL(path.join(ROOT, "node_modules/@earendil-works/pi-ai/dist", file))); +const { createModels } = await sdk("index.js"); +const { builtinProviders } = await sdk("providers/all.js"); +const { githubCopilotProvider } = await sdk("providers/github-copilot.js"); +const models = createModels(); +for (const provider of builtinProviders()) models.setProvider(provider); +const model = models.getModels("openai").find((entry) => entry.api === "openai-responses"); +const copilot = githubCopilotProvider().getModels()[0]; +assert(model && copilot, "Installed SDK catalogs required."); + +const profile = await fs.mkdtemp(path.join(os.tmpdir(), "lh-web-runtime-")); +await fs.chmod(profile, 0o700); +for (const name of ["home", "config", "cache", "xdg-data", "data", "pi", "claude", "codex", "tmp"]) await fs.mkdir(path.join(profile, name), { mode: 0o700 }); +await fs.writeFile(path.join(profile, "auth.json"), "{}\n", { mode: 0o600 }); +const preload = path.join(profile, "offline-fetch.mjs"); +await fs.writeFile(preload, `const original = globalThis.fetch; +globalThis.fetch = async (input, init) => { + const url = new URL(typeof input === 'string' || input instanceof URL ? input : input.url); + if (url.href === 'https://api.openai.com/v1/models') return Response.json({ models: [{ slug: ${JSON.stringify(model.id)}, display_name: 'Offline fixture ChatGPT', visibility: 'list' }] }); + if (['localhost', '127.0.0.1', '[::1]'].includes(url.hostname)) return original(input, init); + throw new Error('Offline subscription gate forbids external network: ' + url.origin + url.pathname); +};\n`, { mode: 0o600 }); +// Intentionally an allowlist, not a copy of the calling shell's environment. +const env = { + PATH: process.env.PATH ?? "/usr/bin:/bin", HOME: path.join(profile, "home"), + XDG_CONFIG_HOME: path.join(profile, "config"), XDG_CACHE_HOME: path.join(profile, "cache"), XDG_DATA_HOME: path.join(profile, "xdg-data"), + PI_CODING_AGENT_DIR: path.join(profile, "pi"), PI_AGENT_DIR: path.join(profile, "pi"), CLAUDE_CONFIG_DIR: path.join(profile, "claude"), CODEX_HOME: path.join(profile, "codex"), + TMPDIR: path.join(profile, "tmp"), LH_AUTH_FILE: path.join(profile, "auth.json"), LH_DATA_DIR: path.join(profile, "data"), + NODE_OPTIONS: `--import=${pathToFileURL(preload).href}`, NEXT_TELEMETRY_DISABLED: "1", +}; +const saved = { + version: 1, hostId: "offline-host", allowBackgroundUse: true, activeId: "offline-account", + registrations: [{ id: "offline-account", clientId: "oaiapp_fixture", issuer: "https://auth.openai.com", subject: "offline-subject", email: "fixture@example.invalid", tokens: { accessToken: "fixture-chatgpt-access", refreshToken: "fixture-chatgpt-refresh", idToken: "fixture-id-token", scopes: SCOPES, expiresAt: Date.now() + 3600000 } }], +}; +function timeout(promise, milliseconds, message) { + let timer; + return Promise.race([promise, new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(message)), milliseconds); })]).finally(() => clearTimeout(timer)); +} +async function freePort() { + const socket = createServer(); + await new Promise((resolve, reject) => { socket.once("error", reject); socket.listen(0, "127.0.0.1", resolve); }); + const { port } = socket.address(); await new Promise((resolve) => socket.close(resolve)); return port; +} +async function run(mode) { + await fs.writeFile(path.join(profile, "chatgpt-credentials.json"), JSON.stringify(saved), { mode: 0o600 }); + for (const id of ["github-copilot", "xai"]) await fs.writeFile(path.join(profile, `${id}-credentials.json`), JSON.stringify({ + version: 1, allowBackgroundUse: true, connectionId: "00000000-0000-4000-8000-000000000001", + credential: { type: "oauth", access: id === "xai" ? "fixture-xai-access" : "tid=fixture;proxy-ep=proxy.individual.githubcopilot.com;", refresh: "fixture-refresh", expires: Date.now() + 3600000, ...(id === "github-copilot" ? { availableModelIds: [copilot.id] } : {}) }, + }), { mode: 0o600 }); + const port = await freePort(), origin = `http://localhost:${port}`; + let logs = "", exited = false; + const child = spawn(process.execPath, [path.join(ROOT, "node_modules/next/dist/bin/next"), mode, "-p", String(port)], { cwd: ROOT, env, stdio: ["ignore", "pipe", "pipe"] }); + const exit = new Promise((resolve) => child.once("exit", () => { exited = true; resolve(); })); + try { + await timeout(new Promise((resolve, reject) => { + const receive = (data) => { logs = (logs + data).slice(-16000); if (logs.includes("Ready in")) resolve(); }; + child.stdout.on("data", receive); child.stderr.on("data", receive); + child.once("error", reject); child.once("exit", (code) => reject(new Error(`Next ${mode} exited ${code}.`))); + }), 60000, `Next ${mode} readiness timeout.`); + const request = (url, command) => fetch(origin + url, { + ...(command ? { method: "POST", body: JSON.stringify(command) } : {}), + headers: { origin, "sec-fetch-site": "same-origin", "x-learnharness-subscriptions": "1", "content-type": "application/json" }, + signal: AbortSignal.timeout(60000), + }); + // Controls have not been imported through an HTTP request yet. + const catalog = (await (await request("/api/models")).json()).providers; + for (const id of ["chatgpt", "github-copilot-subscription", "xai-subscription"]) assert(catalog.find((provider) => provider.id === id)?.keyPresent, `${mode}: cold ${id} unavailable.`); + let state = await (await request("/api/subscriptions")).json(); + assert.equal(state.chatgpt.accounts[0].email, "fixture@example.invalid"); assert.equal(state.native.length, 2); + for (const secret of ["fixture-chatgpt-access", "fixture-chatgpt-refresh", "fixture-id-token", "fixture-xai-access", "fixture-refresh"]) assert(!JSON.stringify(state).includes(secret)); + const stream = await fetch(origin + "/api/events", { signal: AbortSignal.timeout(60000) }); + const reader = stream.body.getReader(); + try { + const first = await reader.read(); assert(first.value); + assert.equal((await request("/api/subscriptions", { providerId: "chatgpt", op: "consent", enabled: false })).status, 200); + let events = new TextDecoder().decode(first.value); + while (!events.includes("subscriptions-changed")) { + const frame = await timeout(reader.read(), 5000, "Cross-route subscription SSE timeout."); + assert(!frame.done, "Subscription event stream ended early."); events += new TextDecoder().decode(frame.value); + } + } finally { await reader.cancel(); } + state = await (await request("/api/subscriptions")).json(); assert.equal(state.chatgpt.allowBackgroundUse, false); + const login = await (await request("/api/subscriptions", { providerId: "chatgpt", op: "login" })).json(); + assert(login.browserAction); assert(!JSON.stringify(login).includes("auth.openai.com")); + if (mode === "dev") { + const file = path.join(ROOT, "src/server/web-subscriptions.ts"), times = await fs.stat(file); + await fs.utimes(file, new Date(), new Date()); + try { assert.equal((await request("/api/models")).status, 200); assert.equal((await request("/api/subscriptions")).status, 200); } + finally { await fs.utimes(file, times.atime, times.mtime); } + } + const navigate = () => fetch(origin + login.browserAction, { redirect: "manual", headers: { "sec-fetch-site": "same-origin" }, signal: AbortSignal.timeout(30000) }); + const browser = await navigate(); assert.equal(browser.status, 303, `${mode}: owner/action lost across routes or reload.`); + const location = new URL(browser.headers.get("location")); assert.equal(location.origin, "https://auth.openai.com"); assert.equal(new URL(location.searchParams.get("redirect_uri")).hostname, "127.0.0.1"); + assert.equal((await navigate()).status, 410); + assert.equal((await request("/api/subscriptions", { providerId: "chatgpt", op: "cancel" })).status, 200); + assert.equal((await fetch(origin + "/api/subscriptions", { method: "POST", headers: { origin: "https://unrelated.invalid", "content-type": "application/json", "x-learnharness-subscriptions": "1" }, body: JSON.stringify({ providerId: "chatgpt", op: "disconnect", id: "offline-account" }) })).status, 403); + assert.equal((await request("/api/subscriptions", { providerId: "chatgpt", op: "disconnect", id: "offline-account" })).status, 200); + assert.equal((await (await request("/api/models")).json()).providers.find((provider) => provider.id === "chatgpt").keyPresent, false); + console.log(`[smoke-web-subscriptions] ${mode}: cold catalogs, safe status, cross-route SSE/consent/disconnect, opaque redirect/replay, CSRF${mode === "dev" ? ", owner retained across source reload" : ""} passed`); + } catch (error) { console.error(logs); throw error; } + finally { + if (!exited) child.kill("SIGTERM"); + try { await timeout(exit, 5000, "Next shutdown timeout."); } + catch { child.kill("SIGKILL"); await exit; } + } +} +try { await run("start"); await run("dev"); } +finally { await fs.rm(profile, { recursive: true, force: true }); } diff --git a/src/agent/chatgpt-bridge.ts b/src/agent/chatgpt-bridge.ts index 5412549..53d748d 100644 --- a/src/agent/chatgpt-bridge.ts +++ b/src/agent/chatgpt-bridge.ts @@ -1,4 +1,5 @@ import type { Model } from "@earendil-works/pi-ai"; +import { onWebSubscriptionPacket, webSubscriptionRequest, webSubscriptionsEnabled } from "./subscription-runtime.ts"; import type { ChatGPTStatus } from "../core/contracts/chatgpt.ts"; import type { AdmissionScope, NoDispatchKind } from "./chatgpt-request-cycle.ts"; export type { ChatGPTStatus } from "../core/contracts/chatgpt.ts"; @@ -20,14 +21,15 @@ const validStamp = (s: Stamp) => s && [s.epoch, s.authorityRevision, s.revision] function desktopIPCConnected(): boolean { return process.env.LH_DESKTOP_SERVER === "1" && (process.env.LH_DESKTOP_API_SECRET?.length ?? 0) >= 64 && typeof process.send === "function" && process.connected === true; } +function runtimeConnected(): boolean { return desktopIPCConnected() || webSubscriptionsEnabled(); } function bridge(): BridgeState { const global = globalThis as typeof globalThis & { [key]?: BridgeState }; if (!global[key]) { const state: BridgeState = { status: unavailable(), stamp: { epoch: 0, authorityRevision: 0, revision: 0 }, models: [], catalogAccount: null, pending: new Map(), controllers: new Map(), listeners: new Set(), counter: 0, catalogRequest: 0 }; global[key] = state; - process.on("message", (raw: unknown) => { + const receive = (raw: unknown) => { const m = raw as { channel?: string; kind?: string; id?: string; op?: string; ok?: boolean; value?: unknown; stamp: Stamp; status: ChatGPTStatus; invalidate?: string; error?: string }; - if (!desktopIPCConnected() || m?.channel !== "lh:chatgpt") return; + if (m?.channel !== "lh:chatgpt") return; if (m.kind === "state" && validStamp(m.stamp) && m.stamp.revision >= state.stamp.revision) { acceptStatus(state, { ...m.stamp, status: m.status }); if (m.invalidate === "all") { state.catalogRequest++; clearCatalog(state); } @@ -43,8 +45,11 @@ function bridge(): BridgeState { if (m.ok === true) call.resolve(m.value); else call.reject(new Error(m.error ?? "ChatGPT connection failed.")); } - }); + }; + process.on("message", (raw: unknown) => { if (desktopIPCConnected()) receive(raw); }); + onWebSubscriptionPacket("lh:chatgpt", (packet) => { if (webSubscriptionsEnabled()) receive(packet); }); process.on("disconnect", () => { + if (webSubscriptionsEnabled()) return; state.status = unavailable(); clearCatalog(state); state.catalogRequest++; for (const controller of state.controllers.keys()) controller.abort("all"); for (const call of state.pending.values()) { clearTimeout(call.timer); call.reject(new Error("The desktop connection closed.")); } @@ -60,9 +65,10 @@ function acceptStatus(state: BridgeState, value: StatusValue) { if (value.epoch !== state.stamp.epoch || value.authorityRevision !== state.stamp.authorityRevision || value.status.activeId !== state.status.activeId) clearCatalog(state); state.status = value.status; state.stamp = { epoch: value.epoch, authorityRevision: value.authorityRevision, revision: value.revision }; } -/** Runtime-only inherited IPC. No HTTP credential route or renderer method. */ +/** Runtime-only: inherited desktop IPC or direct web owner. Never HTTP leases. */ export function chatGPTRequest(op: "status" | "models" | "lease", fields: Record = {}): Promise { const state = bridge(); + if (webSubscriptionsEnabled()) return webSubscriptionRequest("lh:chatgpt", op, fields); if (!desktopIPCConnected()) return Promise.reject(new Error("ChatGPT sign-in is available only in the desktop app with a private server connection.")); return new Promise((resolve, reject) => { const id = `${process.pid}:${++state.counter}`; @@ -74,7 +80,7 @@ export function chatGPTRequest(op: "status" | "models" | "lease", fields: Rec }); } export function chatGPTStatus(): ChatGPTStatus { return bridge().status; } -export function chatGPTConnected(): boolean { const s = chatGPTStatus(); return desktopIPCConnected() && s.available && s.accounts.some((a) => a.id === s.activeId && a.connected && a.planEnabled === true); } +export function chatGPTConnected(): boolean { const s = chatGPTStatus(); return runtimeConnected() && s.available && s.accounts.some((a) => a.id === s.activeId && a.connected && a.planEnabled === true); } export function chatGPTBackgroundAllowed(): boolean { const s = chatGPTStatus(); return chatGPTConnected() && s.allowBackgroundUse === true; } export function onChatGPTChange(listener: () => void): () => void { bridge().listeners.add(listener); return () => { bridge().listeners.delete(listener); }; } export function chatGPTModels(): Model<"openai-responses">[] { return bridge().models; } @@ -86,7 +92,7 @@ export function chatGPTScope(selectedModelId: string): AdmissionScope { } export function chatGPTScopeCurrent(scope: AdmissionScope): boolean { const s = bridge(); - return desktopIPCConnected() && chatGPTConnected() && scope.accountId === s.catalogAccount && scope.accountId === s.status.activeId && scope.epoch === s.stamp.epoch && scope.authorityRevision === s.stamp.authorityRevision && !!chatGPTModel(scope.selectedModelId); + return runtimeConnected() && chatGPTConnected() && scope.accountId === s.catalogAccount && scope.accountId === s.status.activeId && scope.epoch === s.stamp.epoch && scope.authorityRevision === s.stamp.authorityRevision && !!chatGPTModel(scope.selectedModelId); } export async function chatGPTLease(scope: AdmissionScope, background: boolean): Promise { if (!chatGPTScopeCurrent(scope)) throw new Error("ChatGPT catalog authority changed"); @@ -108,7 +114,7 @@ export async function chatGPTDenial(scope: AdmissionScope | undefined, controlle /** Preserve account order; unknown capabilities/windows are never fabricated. */ export async function refreshChatGPTModels(supported: Model<"openai-responses">[]): Promise { const state = bridge(); - if (!desktopIPCConnected()) { clearCatalog(state); return; } + if (!runtimeConnected()) { clearCatalog(state); return; } const sequence = ++state.catalogRequest; try { const value = await chatGPTRequest("models"); diff --git a/src/agent/subscription-bridge.ts b/src/agent/subscription-bridge.ts index b1a558e..97ef6f8 100644 --- a/src/agent/subscription-bridge.ts +++ b/src/agent/subscription-bridge.ts @@ -1,4 +1,5 @@ import type { Api, Model, ModelAuth } from "@earendil-works/pi-ai"; +import { onWebSubscriptionPacket, webSubscriptionRequest, webSubscriptionsEnabled } from "./subscription-runtime.ts"; import { NATIVE_SUBSCRIPTIONS, type NativeSubscriptionProviderId, type NativeSubscriptionScope, type NativeSubscriptionSnapshot, type NativeSubscriptionStatus } from "../core/contracts/subscriptions.ts"; import type { AdmissionScope, NoDispatchKind } from "./chatgpt-request-cycle.ts"; @@ -18,6 +19,7 @@ export function nativeSubscriptionId(logicalId: string): NativeSubscriptionProvi function desktopConnected(): boolean { return process.env.LH_DESKTOP_SERVER === "1" && (process.env.LH_DESKTOP_API_SECRET?.length ?? 0) >= 64 && typeof process.send === "function" && process.connected === true; } +function runtimeConnected(): boolean { return desktopConnected() || webSubscriptionsEnabled(); } function notify(state: State) { for (const listener of state.listeners) { try { listener(); } catch { /* observer isolation */ } } } function invalidate(state: State, providerId?: NativeSubscriptionProviderId) { for (const [controller, owner] of state.controllers) if (!providerId || owner.providerId === providerId) controller.abort("all"); @@ -60,7 +62,11 @@ function bridge(): State { if (message.ok) pending.resolve(message.value); else pending.reject(new Error(message.error ?? "Subscription connection failed.")); }); + onWebSubscriptionPacket("lh:subscriptions", (packet) => { + if (webSubscriptionsEnabled() && packet.snapshot) accept(state, packet.snapshot as NativeSubscriptionSnapshot); + }); process.on("disconnect", () => { + if (webSubscriptionsEnabled()) return; invalidate(state); for (const pending of state.pending.values()) { clearTimeout(pending.timer); pending.reject(new Error("The desktop connection closed.")); } state.pending.clear(); notify(state); @@ -70,6 +76,7 @@ function bridge(): State { } export function subscriptionRequest(op: "snapshot" | "lease", fields: Record = {}): Promise { const state = bridge(); + if (webSubscriptionsEnabled()) return webSubscriptionRequest("lh:subscriptions", op, fields); if (!desktopConnected()) return Promise.reject(new Error("Subscription sign-in is available only in the desktop app with a private server connection.")); return new Promise((resolve, reject) => { const id = `${process.pid}:${++state.counter}`; @@ -82,17 +89,17 @@ export function subscriptionRequest(op: "snapshot" | "lease", fields: Record< } export async function refreshSubscriptionModels(): Promise { const state = bridge(); - if (!desktopConnected()) { invalidate(state); return; } + if (!runtimeConnected()) { invalidate(state); return; } try { accept(state, await subscriptionRequest("snapshot")); } catch { invalidate(state); notify(state); } } export function nativeSubscriptionStatus(providerId: NativeSubscriptionProviderId): NativeSubscriptionStatus | undefined { return bridge().statuses.get(providerId); } export function nativeSubscriptionConnected(providerId: NativeSubscriptionProviderId): boolean { const status = nativeSubscriptionStatus(providerId); - return desktopConnected() && status?.available === true && status.connected && !!status.connectionId; + return runtimeConnected() && status?.available === true && status.connected && !!status.connectionId; } export function nativeSubscriptionBackgroundAllowed(providerId: NativeSubscriptionProviderId): boolean { return nativeSubscriptionConnected(providerId) && nativeSubscriptionStatus(providerId)?.allowBackgroundUse === true; } -export function nativeSubscriptionModels(): Model[] { return desktopConnected() ? [...bridge().models.values()].flat() : []; } +export function nativeSubscriptionModels(): Model[] { return runtimeConnected() ? [...bridge().models.values()].flat() : []; } export function nativeSubscriptionModel(logicalId: string, modelId: string): Model | undefined { return nativeSubscriptionModels().find((m) => m.provider === logicalId && m.id === modelId); } export function onNativeSubscriptionChange(listener: () => void): () => void { const state = bridge(); state.listeners.add(listener); return () => { state.listeners.delete(listener); }; } export function subscriptionScope(providerId: NativeSubscriptionProviderId, selectedModelId: string): NativeSubscriptionScope { diff --git a/src/agent/subscription-runtime.ts b/src/agent/subscription-runtime.ts new file mode 100644 index 0000000..56e1f07 --- /dev/null +++ b/src/agent/subscription-runtime.ts @@ -0,0 +1,30 @@ +/** Server-injected private web transport. The agent layer never imports a + * filesystem owner or server composition root. Desktop IPC remains separate. */ +export type SubscriptionRuntimeChannel = "lh:chatgpt" | "lh:subscriptions"; +export type SubscriptionRuntimePacket = Record; +interface Runtime { + request?: (channel: SubscriptionRuntimeChannel, op: string, fields: Record) => Promise; + listeners: Map void>>; +} +const KEY = Symbol.for("lh.webSubscriptionRuntime"); +function runtime(): Runtime { + const global = globalThis as typeof globalThis & { [KEY]?: Runtime }; + return global[KEY] ??= { listeners: new Map() }; +} +export function configureWebSubscriptionRuntime(request: NonNullable): void { runtime().request = request; } +export function webSubscriptionsEnabled(): boolean { + return process.env.LH_DESKTOP_SERVER !== "1" && !process.env.LH_DESKTOP_API_SECRET && !!runtime().request; +} +export function onWebSubscriptionPacket(channel: SubscriptionRuntimeChannel, listener: (packet: SubscriptionRuntimePacket) => void): void { + const state = runtime(); const listeners = state.listeners.get(channel) ?? new Set(); + listeners.add(listener); state.listeners.set(channel, listeners); +} +export function publishWebSubscriptionPacket(channel: SubscriptionRuntimeChannel, packet: SubscriptionRuntimePacket): void { + for (const listener of runtime().listeners.get(channel) ?? []) { + try { listener(packet); } catch { /* observer isolation */ } + } +} +export function webSubscriptionRequest(channel: SubscriptionRuntimeChannel, op: string, fields: Record): Promise { + if (!webSubscriptionsEnabled()) return Promise.reject(new Error("The web subscription owner is unavailable.")); + return runtime().request!(channel, op, fields); +} diff --git a/src/core/events.ts b/src/core/events.ts index e639006..dba4236 100644 --- a/src/core/events.ts +++ b/src/core/events.ts @@ -38,6 +38,8 @@ export interface ServerEvents { "activity-changed": Record; /** The session list changed (create, delete, title). */ "chats-changed": Record; + /** Web subscription state changed. No account or credential data on the bus. */ + "subscriptions-changed": Record; /** A chat's record changed WITHOUT a turn (the silent dismissal write). * Same notify-then-fetch contract as the turn events: open views of * the chat refetch and adopt the durable record. */ @@ -61,6 +63,7 @@ export const SERVER_EVENT_TYPES = [ "jobs-changed", "activity-changed", "chats-changed", + "subscriptions-changed", "record-changed", "compaction-changed", ] as const satisfies readonly ServerEventType[]; diff --git a/src/server/index.ts b/src/server/index.ts index 49968ca..a04dfb5 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -43,4 +43,5 @@ export * from "./compaction.ts"; export { runPiTurn, piTurnResponse, type PiTurnHandle, type PiTurnSpec } from "./pi/ui-stream.ts"; export * from "./observability.ts"; export * from "./model-catalog.ts"; +export { subscriptionControlsResponse, subscriptionBrowserResponse } from "./web-subscription-http.ts"; export { sessionCost } from "./pricing.ts"; diff --git a/src/server/web-subscription-http.ts b/src/server/web-subscription-http.ts new file mode 100644 index 0000000..3d5d7a9 --- /dev/null +++ b/src/server/web-subscription-http.ts @@ -0,0 +1,68 @@ +import { NATIVE_SUBSCRIPTIONS } from "../core/contracts/subscriptions.ts"; +import { webSubscriptionOwner, webSubscriptionsEnabled, webSubscriptionStatus } from "./web-subscriptions.ts"; + +const HEADERS = { "Cache-Control": "no-store", "Referrer-Policy": "no-referrer", "X-Content-Type-Options": "nosniff" }; +/** Same-origin browser controls, not user authentication. Web/dev deliberately + * retains the existing shared-server/API-key trust model. Never trust forwarded + * origins or add CORS grants here. A public deployment needs its own access gate. */ +export function subscriptionRequestOrigin(request: Request): string | undefined { + try { + const url = new URL(request.url); + const host = request.headers.get("host") ?? url.host; + const addressed = new URL(`${url.protocol}//${host}`); + if (!["http:", "https:"].includes(addressed.protocol) || addressed.username || addressed.password || addressed.pathname !== "/" || addressed.search || addressed.hash) return; + return addressed.origin; + } catch { return; } +} +function authorized(request: Request): boolean { + const origin = subscriptionRequestOrigin(request); + if (!origin || request.headers.get("x-learnharness-subscriptions") !== "1") return false; + if (request.headers.get("sec-fetch-site") === "cross-site") return false; + const supplied = request.headers.get("origin"); + return request.method === "GET" ? !supplied || supplied === origin : supplied === origin; +} +function error(message: string, status: number) { return Response.json({ error: message }, { status, headers: HEADERS }); } +export async function subscriptionControlsResponse(request: Request): Promise { + if (!webSubscriptionsEnabled()) return error("Web subscription controls are unavailable in desktop-owned mode.", 404); + if (!authorized(request)) return error("Subscription controls require a same-origin LearnHarness request.", 403); + try { + if (request.method === "GET") return Response.json(await webSubscriptionStatus(), { headers: HEADERS }); + if (request.method !== "POST") return error("Unsupported method.", 405); + if (request.headers.get("content-type")?.split(";")[0]?.trim() !== "application/json") return error("Expected JSON.", 415); + // Bound both advertised and streamed input before parsing commands. + if (Number(request.headers.get("content-length")) > 16384) return error("Request too large.", 413); + const reader = request.body?.getReader(); + if (!reader) return error("Expected a command.", 400); + const chunks: Uint8Array[] = []; let size = 0; + try { + for (;;) { + const next = await reader.read(); if (next.done) break; + size += next.value.byteLength; + if (size > 16384) { await reader.cancel(); return error("Request too large.", 413); } + chunks.push(next.value); + } + } finally { reader.releaseLock(); } + const command: unknown = JSON.parse(Buffer.concat(chunks).toString("utf8")); + if (!command || typeof command !== "object" || Array.isArray(command)) return error("Invalid command.", 400); + const fields = command as Record; + const providerId = fields.providerId; + if (providerId !== "chatgpt" && !NATIVE_SUBSCRIPTIONS.some((provider) => provider.providerId === providerId)) return error("Unsupported connection.", 400); + const allowed = providerId === "chatgpt" ? ["login", "cancel", "select", "disconnect", "logout", "consent"] : ["login", "cancel", "respond", "open", "retry", "disconnect", "consent"]; + if (typeof fields.op !== "string" || !allowed.includes(fields.op)) return error("Unsupported control.", 400); + return Response.json(await webSubscriptionOwner().control(providerId as string, fields, subscriptionRequestOrigin(request)!), { headers: HEADERS }); + } catch { + return error("The subscription connection could not be changed. Retry or reconnect in Providers.", 400); + } +} +/** One-use same-origin navigation. Full provider authorization URLs go only to + * a top-level browser redirect, never status JSON, the SSE bus or application JS. */ +export async function subscriptionBrowserResponse(request: Request): Promise { + if (!webSubscriptionsEnabled()) return error("Unavailable.", 404); + const origin = subscriptionRequestOrigin(request); + if (!origin || (request.method === "POST" ? !authorized(request) : request.headers.get("sec-fetch-site") !== "same-origin")) return error("Open sign-in from LearnHarness.", 403); + const id = new URL(request.url).searchParams.get("id"); + if (!id || !/^[0-9a-f-]{36}$/.test(id)) return error("This sign-in action is no longer available.", 410); + const target = await webSubscriptionOwner().takeBrowserAction(id, origin); + if (!target) return error("This sign-in action is no longer available.", 410); + return new Response(null, { status: 303, headers: { ...HEADERS, Location: target } }); +} diff --git a/src/server/web-subscriptions.ts b/src/server/web-subscriptions.ts new file mode 100644 index 0000000..4e40538 --- /dev/null +++ b/src/server/web-subscriptions.ts @@ -0,0 +1,127 @@ +/** Persistent Node web/dev owner. Never imported by client code. Desktop keeps + * its existing main-process owner; a broken desktop IPC channel cannot fall + * back to these files. One server process must own each configured auth dir. */ +import { AsyncLocalStorage } from "node:async_hooks"; +import { randomUUID } from "node:crypto"; +import path from "node:path"; +import { AUTH_FILE } from "../../config.ts"; +import { createChatGPT, fileStore } from "../../electron/chatgpt.cjs"; +import { createSubscriptions } from "../../electron/subscriptions.cjs"; +import { chatGPTControl } from "../../electron/chatgpt-ipc.cjs"; +import { subscriptionControl } from "../../electron/subscription-ipc.cjs"; +import { emit } from "../core/events.ts"; +import type { ChatGPTStatus } from "../core/contracts/chatgpt.ts"; +import type { NativeSubscriptionSnapshot } from "../core/contracts/subscriptions.ts"; +import { configureWebSubscriptionRuntime, publishWebSubscriptionPacket, webSubscriptionsEnabled, type SubscriptionRuntimeChannel as Channel, type SubscriptionRuntimePacket as Packet } from "../agent/subscription-runtime.ts"; +export { webSubscriptionsEnabled } from "../agent/subscription-runtime.ts"; +interface BrowserAction { target: string; origin: string; scope: string; expires: number; current(): Promise } +const KEY = Symbol.for("lh.webSubscriptions"); +interface Registry { + owner?: ReturnType; +} +function registry(): Registry { + const global = globalThis as typeof globalThis & { [KEY]?: Registry }; + return global[KEY] ??= {}; +} +function publish(channel: Channel, packet: Packet) { + publishWebSubscriptionPacket(channel, packet); + emit("subscriptions-changed", {}); +} +async function loadSDK() { + const [sdk, copilot, xai] = await Promise.all([ + import("@earendil-works/pi-ai"), + import("@earendil-works/pi-ai/providers/github-copilot"), + import("@earendil-works/pi-ai/providers/xai"), + ]); + return { ...sdk, ...copilot, ...xai }; +} + +/** Injectable factories/stores keep all test authentication offline. */ +export function createWebSubscriptionOwner(options: { + directory: string; + chatgptStore?: Parameters[0]["store"]; + sdk?: unknown; + onPacket?: (channel: Channel, packet: Packet) => void; +} ) { + const notify = options.onPacket ?? (() => {}); + const launches = new AsyncLocalStorage(); + const actions = new Map(); + let chatGPTLaunch = 0; + const openExternal = async (target: string) => { + const captured = launches.getStore(); + if (!captured || new URL(target).protocol !== "https:") throw new Error("No browser action is pending."); + captured.push(target); + }; + const chatgpt = createChatGPT({ + store: options.chatgptStore ?? fileStore(path.join(options.directory, "chatgpt-credentials.json")), + openExternal, + onChange: (packet?: Packet) => { if (packet) notify("lh:chatgpt", packet); }, + }); + const native = createSubscriptions({ + directory: options.directory, sdk: options.sdk, loadSDK, openExternal, + onChange: (snapshot: NativeSubscriptionSnapshot) => notify("lh:subscriptions", { channel: "lh:subscriptions", op: "changed", snapshot }), + }); + return { + chatgpt, native, + async control(providerId: string, command: Record, origin: string) { + const targets: string[] = []; + const value = await launches.run(targets, () => providerId === "chatgpt" + ? chatGPTControl(command, chatgpt) + : subscriptionControl({ ...command, providerId }, native)); + let browserAction: string | undefined; + const target = targets[0]; + if (target) { + const scope = providerId === "chatgpt" ? "chatgpt" : `${providerId}:${command.attemptId}:${command.actionId}`; + for (const [id, action] of actions) if (action.expires <= Date.now() || action.scope === scope) actions.delete(id); + const id = randomUUID(); + const generation = providerId === "chatgpt" ? ++chatGPTLaunch : chatGPTLaunch; + const current = providerId === "chatgpt" + ? async () => generation === chatGPTLaunch && (await chatgpt.status()).pending === true + : async () => (await native.status()).some((status: { providerId: string; interaction?: { attemptId: string; browser?: { id: string } } }) => status.providerId === providerId && status.interaction?.attemptId === command.attemptId && status.interaction?.browser?.id === command.actionId); + actions.set(id, { target, origin, scope, expires: Date.now() + 180000, current }); + browserAction = `/api/subscriptions/browser?id=${id}`; + } + return { value, ...(browserAction ? { browserAction } : {}) }; + }, + async takeBrowserAction(id: string, origin: string): Promise { + const action = actions.get(id); + if (!action || action.origin !== origin) return; + actions.delete(id); + if (action.expires <= Date.now() || !await action.current()) return; + return action.target; + }, + close() { actions.clear(); chatgpt.close(); native.close(); }, + }; +} +export function webSubscriptionOwner() { + if (!webSubscriptionsEnabled()) throw new Error("Web subscription controls are unavailable in desktop-owned mode."); + const state = registry(); + if (!state.owner) { + state.owner = createWebSubscriptionOwner({ directory: path.dirname(AUTH_FILE), onPacket: publish }); + process.once("exit", () => state.owner?.close()); + } + return state.owner; +} +/** Private runtime calls only: no HTTP lease/token operation exists. */ +export async function webSubscriptionRequest(channel: Channel, op: string, fields: Record): Promise { + const owner = webSubscriptionOwner(); + let value: unknown; + if (channel === "lh:chatgpt") { + if (op === "status") value = { status: await owner.chatgpt.status(), ...owner.chatgpt.stamp() }; + else if (op === "models") value = await owner.chatgpt.models(); + else if (op === "lease" && typeof fields.background === "boolean") value = await owner.chatgpt.lease(fields.background, fields); + else throw new Error("Invalid private operation."); + } else { + if (op === "snapshot") value = await owner.native.snapshot(); + else if (op === "lease" && typeof fields.background === "boolean") value = await owner.native.lease(fields.scope, fields.background); + else throw new Error("Invalid private operation."); + } + return value as T; +} +configureWebSubscriptionRuntime(webSubscriptionRequest); + +export async function webSubscriptionStatus(): Promise<{ chatgpt: ChatGPTStatus; native: NativeSubscriptionSnapshot["statuses"] }> { + const owner = webSubscriptionOwner(); + const [chatgpt, native] = await Promise.all([owner.chatgpt.status(), owner.native.status()]); + return { chatgpt, native }; +} diff --git a/tests/chatgpt-dev-ipc.test.ts b/tests/chatgpt-dev-ipc.test.ts index fcb0e44..e940b01 100644 --- a/tests/chatgpt-dev-ipc.test.ts +++ b/tests/chatgpt-dev-ipc.test.ts @@ -12,7 +12,7 @@ const { GET } = await import("../app/api/models/route.ts"); const { chatGPTRequest, chatGPTConnected, chatGPTModels } = await import("../src/agent/chatgpt-bridge.ts"); after(() => fs.rmSync(scratch, { recursive: true, force: true })); -test("Next-style connected IPC is not desktop transport and never delays keyed API catalogs", async () => { +test("Next-style IPC is not desktop transport; web ownership works without delaying keyed catalogs", async () => { const send = Object.getOwnPropertyDescriptor(process, "send"); const connected = Object.getOwnPropertyDescriptor(process, "connected"); const marker = process.env.LH_DESKTOP_SERVER, secret = process.env.LH_DESKTOP_API_SECRET; @@ -35,7 +35,11 @@ test("Next-style connected IPC is not desktop transport and never delays keyed A assert.equal(catalog.providers.find((p: { id: string }) => p.id === "chatgpt").keyPresent, false); assert.equal(chatGPTConnected(), false); assert.deepEqual(chatGPTModels(), []); - await assert.rejects(chatGPTRequest("status"), /available only in the desktop app/); + if (!mode && !capability) { + const value = await chatGPTRequest<{ status: { available: boolean; accounts: unknown[] } }>("status"); + assert.equal(value.status.available, true); + assert.deepEqual(value.status.accounts, []); + } else await assert.rejects(chatGPTRequest("status"), /available only in the desktop app/); assert.equal(sends, 0, "no desktop request/timer may be started over framework IPC"); } process.env.LH_DESKTOP_SERVER = "1"; process.env.LH_DESKTOP_API_SECRET = "d".repeat(64); diff --git a/tests/desktop-platform.test.ts b/tests/desktop-platform.test.ts index 040a47a..03276c0 100644 --- a/tests/desktop-platform.test.ts +++ b/tests/desktop-platform.test.ts @@ -15,7 +15,7 @@ const mainRequire = createRequire(mainFile); /** Decline the single-instance lock so normal boot never spawns a server. * Then call the REAL createWindow with a stub Electron API and inspect its * constructor options. No rewritten copy of the production logic. */ -function windowOptions(platform: string) { +function windowOptions(platform: string, env: Record = {}) { class FakeWindow { constructor(readonly options: unknown) {} webContents = { on() {}, setWindowOpenHandler() {} }; @@ -23,13 +23,13 @@ function windowOptions(platform: string) { once() {} } const electron = { - app: { setName() {}, requestSingleInstanceLock: () => false, quit() {} }, + app: { setName() {}, getPath: () => "/fixture/LearnHarness", setPath() {}, requestSingleInstanceLock: () => false, quit() {} }, BrowserWindow: FakeWindow, session: { defaultSession: { setPermissionRequestHandler() {}, setPermissionCheckHandler() {} } }, }; const context = { require: (id: string) => id === "electron" ? electron : mainRequire(id), - process: { platform, argv: [], env: {}, on() {} }, + process: { platform, argv: [], env, on() {} }, __dirname: dirname(mainFile), }; runInNewContext(mainSource, context, { filename: mainFile }); @@ -61,6 +61,24 @@ describe("desktop window chrome", () => { }); } + it("only the external dev shell receives the web subscription transport marker", () => { + assert.equal(windowOptions("darwin").webPreferences.additionalArguments, undefined); + assert.deepEqual(windowOptions("darwin", { LH_DEV_URL: "http://localhost:3000" }).webPreferences.additionalArguments, ["--lh-web-subscriptions"]); + }); + + it("dev preload routes opaque sign-in actions through its narrow external-browser channel", async () => { + let bridge!: { subscriptionTransport: string; openSubscriptionBrowser(action: string): Promise }; + const invoked: unknown[][] = []; + runInNewContext(readFileSync(new URL("../electron/preload.cjs", import.meta.url), "utf8"), { + process: { platform: "darwin", argv: ["--lh-web-subscriptions"] }, + require: () => ({ contextBridge: { exposeInMainWorld: (_: string, value: typeof bridge) => { bridge = value; } }, ipcRenderer: { on() {}, invoke: async (...args: unknown[]) => { invoked.push(args); } } }), + }); + assert.equal(bridge.subscriptionTransport, "web"); + const action = "/api/subscriptions/browser?id=00000000-0000-4000-8000-000000000001"; + await bridge.openSubscriptionBrowser(action); + assert.deepEqual(invoked, [["lh:subscriptions:web-browser", action]]); + }); + it("preload exposes OS identity without exposing Node/Electron to the renderer", () => { let bridge: Record = {}; const context = { diff --git a/tests/provider-tracing.test.ts b/tests/provider-tracing.test.ts index e251c8d..0c255e4 100644 --- a/tests/provider-tracing.test.ts +++ b/tests/provider-tracing.test.ts @@ -16,7 +16,7 @@ describe("standalone JavaScript tracing exclusions and native workaround policy" const file = (relative: string) => root + (windows ? "\\" : "/") + (windows ? relative.replaceAll("/", "\\") : relative); const ignored = picomatch(patterns, { contains: true, dot: true, windows }); it(`excludes genuine data directories and secrets (${windows ? "Windows" : "POSIX"})`, () => { - for (const relative of ["data/canary.json", "nested/data/private.json", "auth.json", ".env", ".env.local"]) { + for (const relative of ["data/canary.json", "nested/data/private.json", "auth.json", ".env", ".env.local", "chatgpt-credentials.json", "github-copilot-credentials.json", "xai-credentials.json", "chatgpt-credentials.json.fixture.tmp", "github-copilot-credentials.json.fixture.tmp", "xai-credentials.json.fixture.tmp"]) { assert.equal(ignored(file(relative)), true, relative); } // The explicit includes remain necessary: this raw ignore still matches SDK data. @@ -37,7 +37,7 @@ describe("standalone JavaScript tracing exclusions and native workaround policy" assert.equal(ignore(`${root}/node_modules/gcp-metadata/build/src/index.js`), false); }); it("preserves all secret exclusions exactly", () => { - assert.deepEqual(patterns, ["**/data/**/*", "auth.json", ".env*"]); + assert.deepEqual(patterns, ["**/data/**/*", "auth.json", "chatgpt-credentials.json*", "github-copilot-credentials.json*", "xai-credentials.json*", ".env*"]); }); it("retains exactly the narrow native-Turbopack closure workaround and existing runtime includes", () => { assert.deepEqual(nextConfig.outputFileTracingIncludes!["*"], [ diff --git a/tests/web-subscription-client.test.tsx b/tests/web-subscription-client.test.tsx new file mode 100644 index 0000000..df5e2a8 --- /dev/null +++ b/tests/web-subscription-client.test.tsx @@ -0,0 +1,99 @@ +import assert from "node:assert/strict"; +import test, { afterEach } from "node:test"; +import { parseHTML } from "linkedom"; +import { createNotifyHub } from "../components/server-events.tsx"; +import { createWebChatGPTBridge, createWebNativeBridge } from "../components/settings/subscription-client.ts"; +import { createRequire } from "node:module"; + +const dom = parseHTML(""); +Object.assign(globalThis, { window: dom.window, document: dom.document }); +const originalFetch = globalThis.fetch; +const action = "/api/subscriptions/browser?id=00000000-0000-4000-8000-000000000001"; +afterEach(() => { globalThis.fetch = originalFetch; delete window.lh; }); + +test("web ChatGPT reserves a new tab before async login, navigates only to an opaque action and receives push/resync", async () => { + const calls: string[] = []; + const tab = { opener: {}, closed: false, location: { href: "about:blank" }, close() { this.closed = true; } }; + window.open = (() => { calls.push("tab"); return tab; }) as unknown as typeof window.open; + const safe = { available: true, pending: true, allowBackgroundUse: true, activeId: null, accounts: [], error: null }; + globalThis.fetch = async (url, options) => { + assert.equal(url, "/api/subscriptions"); assert.equal(new Headers(options?.headers).get("x-learnharness-subscriptions"), "1"); + if (!options?.method) return Response.json({ chatgpt: safe, native: [] }); + calls.push("login"); assert.equal(tab.opener, null); + assert.deepEqual(JSON.parse(String(options.body)), { providerId: "chatgpt", op: "login" }); + return Response.json({ value: safe, browserAction: action }); + }; + const hub = createNotifyHub(), bridge = createWebChatGPTBridge(hub); + assert.deepEqual(await bridge.login(), safe); assert.deepEqual(calls, ["tab", "login"]); + assert.equal(tab.location.href, action); assert(!tab.closed); + let updates = 0; const off = bridge.onChange(() => updates++); + hub.dispatch("subscriptions-changed", "{}"); hub.fireOpen(); assert.equal(updates, 2); + off(); hub.fireOpen(); assert.equal(updates, 2); + assert.deepEqual(await bridge.status(), safe); +}); + +test("blocked popups do not start OAuth; failed login closes the reserved tab", async () => { + const bridge = createWebChatGPTBridge(null); + window.open = () => null; + globalThis.fetch = async () => assert.fail("blocked popup must not start sign-in"); + await assert.rejects(bridge.login(), /Allow a sign-in tab/); + const tab = { opener: {}, closed: false, close() { this.closed = true; } }; + window.open = (() => tab) as unknown as typeof window.open; + globalThis.fetch = async () => new Response(null, { status: 400 }); + await assert.rejects(bridge.login()); assert(tab.closed); +}); + +test("native web login retains device prompts; only the explicit Open browser action opens a tab", async () => { + const requests: Record[] = []; let tabs = 0; + const tab = { opener: {}, closed: false, location: { href: "" }, close() {} }; + window.open = (() => { tabs++; return tab; }) as unknown as typeof window.open; + globalThis.fetch = async (_url, options) => { + const command = JSON.parse(String(options?.body)); requests.push(command); + return Response.json(command.op === "open" ? { browserAction: action } : {}); + }; + const bridge = createWebNativeBridge(null); + await bridge.login("github-copilot", false); assert.equal(tabs, 0); + await bridge.respond("github-copilot", "attempt", "prompt", ""); + await bridge.open("github-copilot", "attempt", "action"); assert.equal(tabs, 1); assert.equal(tab.location.href, action); + assert.deepEqual(requests, [{ providerId: "github-copilot", op: "login", enabled: false }, { providerId: "github-copilot", op: "respond", attemptId: "attempt", promptId: "prompt", value: "" }, { providerId: "github-copilot", op: "open", attemptId: "attempt", actionId: "action" }]); +}); + +test("Electron dev uses its narrow external-browser bridge, without attempting a renderer popup", async () => { + const opened: string[] = []; + window.lh = { expandWindow() {}, subscriptionTransport: "web", openSubscriptionBrowser: async (target) => { opened.push(target); } }; + window.open = () => assert.fail("Electron denies renderer popups"); + globalThis.fetch = async () => Response.json({ value: { pending: true }, browserAction: action }); + await createWebChatGPTBridge(null).login(); assert.deepEqual(opened, [action]); +}); + +test("web notices identify the server machine and same-machine ChatGPT sign-in; desktop copy stays unchanged", async () => { + const { CHATGPT_NOTICE, chatGPTNotice } = await import("../components/settings/chatgpt-connection.tsx"); + const { subscriptionNotice, subscriptionDisconnectCopy } = await import("../components/settings/subscription-connections.tsx"); + assert.equal(chatGPTNotice(), CHATGPT_NOTICE); assert(CHATGPT_NOTICE.includes("on this device")); + assert(chatGPTNotice(true).includes("on the machine running LearnHarness")); + assert(chatGPTNotice(true).endsWith("Complete ChatGPT sign-in in a browser on that same machine.")); + for (const label of ["GitHub Copilot", "Grok"]) { + assert(subscriptionNotice(label).includes("stored locally")); assert(subscriptionDisconnectCopy(label).includes("from this device")); + assert(subscriptionNotice(label, true).includes("on the machine running LearnHarness")); + assert(subscriptionDisconnectCopy(label, true).includes("from the machine running LearnHarness")); + } +}); + +const require = createRequire(import.meta.url); +const { openWebSubscriptionBrowser } = require("../electron/web-subscription-browser.cjs"); +test("desktop-dev opener accepts only local action IDs, does not follow redirects or use browser cookies", async () => { + const opened: string[] = []; + const options = { + openExternal: async (url: string) => { opened.push(url); }, + fetch: async (url: URL, init: RequestInit) => { + assert.equal(url.origin, "http://localhost:3000"); assert.equal(url.pathname, "/api/subscriptions/browser"); + assert.equal(init.method, "POST"); assert.equal(init.redirect, "manual"); assert.equal(init.credentials, "omit"); + assert.equal(new Headers(init.headers).get("origin"), "http://localhost:3000"); + return new Response(null, { status: 303, headers: { Location: "https://auth.openai.com/authorize?state=fixture" } }); + }, + }; + await openWebSubscriptionBrowser(action, "http://localhost:3000", options); assert.equal(opened.length, 1); + for (const target of ["https://unrelated.invalid", "//unrelated.invalid", "/api/keys", `${action}&target=evil`]) await assert.rejects(openWebSubscriptionBrowser(target, "http://localhost:3000", options), /Invalid sign-in action/); + for (const target of ["file:///private/fixture", "http://unrelated.invalid", "https://user:pass@unrelated.invalid"]) await assert.rejects(openWebSubscriptionBrowser(action, "http://localhost:3000", { ...options, fetch: async () => new Response(null, { status: 303, headers: { Location: target } }) })); + assert.equal(opened.length, 1); +}); diff --git a/tests/web-subscription-payload.test.ts b/tests/web-subscription-payload.test.ts new file mode 100644 index 0000000..d860660 --- /dev/null +++ b/tests/web-subscription-payload.test.ts @@ -0,0 +1,21 @@ +/** Exercise the real curator against synthetic payloads, never the repo payload. */ +import assert from "node:assert/strict"; +import { mkdtempSync, mkdirSync, writeFileSync, copyFileSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { spawnSync } from "node:child_process"; +import test from "node:test"; + +for (const name of ["chatgpt-credentials.json", "github-copilot-credentials.json", "xai-credentials.json"]) { + for (const secret of [name, `${name}.fixture.tmp`]) test(`payload curator refuses web credentials: ${secret}`, () => { + const root = mkdtempSync(path.join(tmpdir(), "lh-sub-payload-")); + try { + mkdirSync(path.join(root, "scripts")); mkdirSync(path.join(root, ".next", "standalone"), { recursive: true }); mkdirSync(path.join(root, ".next", "static")); + copyFileSync(new URL("../scripts/build-server.mjs", import.meta.url), path.join(root, "scripts", "build-server.mjs")); + writeFileSync(path.join(root, ".next", "standalone", "server.js"), "// fixture"); + writeFileSync(path.join(root, ".next", "standalone", secret), "fixture-not-real-credentials", { mode: 0o600 }); + const result = spawnSync(process.execPath, [path.join(root, "scripts", "build-server.mjs")], { encoding: "utf8", env: process.env }); + assert.equal(result.status, 1); assert(result.stderr.includes(`REFUSING: standalone contains ${secret}`)); + } finally { rmSync(root, { recursive: true, force: true }); } + }); +} diff --git a/tests/web-subscriptions.test.ts b/tests/web-subscriptions.test.ts new file mode 100644 index 0000000..f34141f --- /dev/null +++ b/tests/web-subscriptions.test.ts @@ -0,0 +1,140 @@ +import assert from "node:assert/strict"; +import test, { after } from "node:test"; +import fs from "node:fs/promises"; +import path from "node:path"; +import os from "node:os"; +import { createRequire } from "node:module"; +import { createModels, type OAuthCredential, type ProviderAuthInteraction } from "@earendil-works/pi-ai"; +import { githubCopilotProvider } from "@earendil-works/pi-ai/providers/github-copilot"; +import { xaiProvider } from "@earendil-works/pi-ai/providers/xai"; + +const directory = await fs.mkdtemp(path.join(os.tmpdir(), "lh-web-sub-")); +process.env.LH_AUTH_FILE = path.join(directory, "auth.json"); +process.env.LH_DATA_DIR = path.join(directory, "data"); +delete process.env.LH_DESKTOP_SERVER; delete process.env.LH_DESKTOP_API_SECRET; +await fs.writeFile(process.env.LH_AUTH_FILE, "{}", { mode: 0o600 }); +const require = createRequire(import.meta.url); +const { SCOPES } = require("../electron/chatgpt.cjs"); +const { createWebSubscriptionOwner, webSubscriptionOwner, webSubscriptionsEnabled } = await import("../src/server/web-subscriptions.ts"); +const { subscriptionControlsResponse, subscriptionBrowserResponse } = await import("../src/server/web-subscription-http.ts"); +const chat = await import("../src/agent/chatgpt-bridge.ts"); +const native = await import("../src/agent/subscription-bridge.ts"); +const { piModels } = await import("../src/agent/models.ts"); +const { refreshConnectionCatalog } = await import("../src/server/model-catalog.ts"); +const model = piModels().getModels("openai").find((entry) => entry.api === "openai-responses")!; +const originalFetch = globalThis.fetch; +globalThis.fetch = async (input) => { + assert.equal(String(input), "https://api.openai.com/v1/models", "unexpected network request forbidden"); + return Response.json({ models: [{ slug: model.id, display_name: "Fixture ChatGPT", visibility: "list" }] }); +}; +const copilot = githubCopilotProvider().getModels()[0]!; +const credentials = (id: string): OAuthCredential => ({ type: "oauth", access: id === "xai" ? "fixture-xai-access" : "tid=fixture;proxy-ep=proxy.individual.githubcopilot.com;", refresh: "fixture-refresh", expires: Date.now() + 3600000, ...(id === "github-copilot" ? { availableModelIds: [copilot.id] } : {}) }); +await fs.writeFile(path.join(directory, "chatgpt-credentials.json"), JSON.stringify({ version: 1, hostId: "fixture-host", allowBackgroundUse: true, activeId: "fixture-account", registrations: [{ id: "fixture-account", clientId: "oaiapp_fixture", issuer: "https://auth.openai.com", subject: "fixture-subject", email: "fixture@example.invalid", tokens: { accessToken: "fixture-chatgpt-access", refreshToken: "fixture-chatgpt-refresh", idToken: "fixture-id-token", scopes: SCOPES, expiresAt: Date.now() + 3600000 } }] }), { mode: 0o600 }); +for (const id of ["github-copilot", "xai"]) await fs.writeFile(path.join(directory, `${id}-credentials.json`), JSON.stringify({ version: 1, allowBackgroundUse: true, connectionId: "00000000-0000-4000-8000-000000000001", credential: credentials(id) }), { mode: 0o600 }); +function request(command?: Record, origin = "http://192.168.1.10:3000") { + return new Request(`${origin}/api/subscriptions`, { method: command ? "POST" : "GET", headers: { host: new URL(origin).host, origin, "sec-fetch-site": "same-origin", "x-learnharness-subscriptions": "1", "content-type": "application/json" }, ...(command ? { body: JSON.stringify(command) } : {}) }); +} +after(async () => { webSubscriptionOwner().close(); globalThis.fetch = originalFetch; await fs.rm(directory, { recursive: true, force: true }); }); + +test("ordinary web restores configured sibling credentials, private runtime leases work for LAN-owned requests", async () => { + await refreshConnectionCatalog(); + assert(chat.chatGPTConnected()); assert(native.nativeSubscriptionConnected("github-copilot")); assert(native.nativeSubscriptionConnected("xai")); + assert.equal((await chat.chatGPTLease(chat.chatGPTScope(model.id), false)).accessToken, "fixture-chatgpt-access"); + const xaiModel = native.nativeSubscriptionModels().find((entry) => entry.provider === "xai-subscription")!; + assert.equal((await native.subscriptionLease(native.subscriptionScope("xai", xaiModel.id), false)).apiKey, "fixture-xai-access"); + const response = await subscriptionControlsResponse(request()); + assert.equal(response.status, 200); assert.equal(response.headers.get("cache-control"), "no-store"); + const publicState = await response.text(); + for (const secret of ["fixture-chatgpt-access", "fixture-chatgpt-refresh", "fixture-id-token", "fixture-xai-access", "fixture-refresh"]) assert(!publicState.includes(secret)); + assert(publicState.includes("fixture@example.invalid")); +}); + +test("web background OFF immediately aborts background streams, retains foreground and rejects private background leases", async () => { + const foreground = chat.chatGPTAbortController(false), background = chat.chatGPTAbortController(true); + const nativeForeground = native.subscriptionAbortController("xai", false), nativeBackground = native.subscriptionAbortController("xai", true); + try { + assert.equal((await subscriptionControlsResponse(request({ providerId: "chatgpt", op: "consent", enabled: false }))).status, 200); + assert(background.controller.signal.aborted); assert(!foreground.controller.signal.aborted); + const scope = chat.chatGPTScope(model.id); await chat.chatGPTLease(scope, false); await assert.rejects(chat.chatGPTLease(scope, true)); + assert.equal((await subscriptionControlsResponse(request({ providerId: "xai", op: "consent", enabled: false }))).status, 200); + assert(nativeBackground.controller.signal.aborted); assert(!nativeForeground.controller.signal.aborted); + const xaiModel = native.nativeSubscriptionModels().find((entry) => entry.provider === "xai-subscription")!; + const nativeScope = native.subscriptionScope("xai", xaiModel.id); await native.subscriptionLease(nativeScope, false); await assert.rejects(native.subscriptionLease(nativeScope, true)); + await subscriptionControlsResponse(request({ providerId: "xai", op: "disconnect" })); + assert(nativeForeground.controller.signal.aborted); assert(!native.nativeSubscriptionConnected("xai")); + assert.deepEqual(JSON.parse(await fs.readFile(path.join(directory, "xai-credentials.json"), "utf8")), { version: 1, allowBackgroundUse: false }); + } finally { foreground.release(); background.release(); nativeForeground.release(); nativeBackground.release(); } +}); + +test("HTTP controls reject cross-origin, simple-form and private credential operations; desktop never falls back", async () => { + const forged = request({ providerId: "chatgpt", op: "consent", enabled: true }); forged.headers.set("origin", "https://unrelated.invalid"); + assert.equal((await subscriptionControlsResponse(forged)).status, 403); + const noHeader = request(); noHeader.headers.delete("x-learnharness-subscriptions"); + assert.equal((await subscriptionControlsResponse(noHeader)).status, 403); + const noOrigin = request({ providerId: "chatgpt", op: "cancel" }); noOrigin.headers.delete("origin"); + assert.equal((await subscriptionControlsResponse(noOrigin)).status, 403); + for (const op of ["lease", "models", "snapshot", "token", "openExternal"]) assert.equal((await subscriptionControlsResponse(request({ providerId: "chatgpt", op }))).status, 400); + const huge = request({ providerId: "chatgpt", op: "cancel", padding: "x".repeat(16384) }); + assert.equal((await subscriptionControlsResponse(huge)).status, 413); + const browser = new Request("http://localhost:3000/api/subscriptions/browser?id=00000000-0000-4000-8000-000000000001"); + assert.equal((await subscriptionBrowserResponse(browser)).status, 403); + for (const [marker, secret] of [["1", undefined], [undefined, "fixture-capability"]]) { + if (marker) process.env.LH_DESKTOP_SERVER = marker; else delete process.env.LH_DESKTOP_SERVER; + if (secret) process.env.LH_DESKTOP_API_SECRET = secret; else delete process.env.LH_DESKTOP_API_SECRET; + assert(!webSubscriptionsEnabled()); + assert.equal((await subscriptionControlsResponse(request())).status, 404); + await assert.rejects(chat.chatGPTRequest("status"), /desktop app/); + } + delete process.env.LH_DESKTOP_SERVER; delete process.env.LH_DESKTOP_API_SECRET; +}); + +function fakeSDK(login: (id: string, interaction: ProviderAuthInteraction) => Promise) { + const factory = (id: string) => { + const provider = id === "xai" ? xaiProvider() : githubCopilotProvider(); + return { ...provider, auth: { oauth: { ...provider.auth.oauth!, login: (interaction: ProviderAuthInteraction) => login(id, interaction), refresh: async () => credentials(id) } } }; + }; + return { createModels, githubCopilotProvider: () => factory("github-copilot"), xaiProvider: () => factory("xai") }; +} +test("device browser actions preserve complete URLs privately, are origin-bound, one-use and cancellation-bound", async () => { + const scratch = await fs.mkdtemp(path.join(os.tmpdir(), "lh-web-action-")); + let complete!: (credential: OAuthCredential) => void; + const pending = new Promise((resolve) => { complete = resolve; }); + let notified!: () => void; + const notification = new Promise((resolve) => { notified = resolve; }); + const owner = createWebSubscriptionOwner({ directory: scratch, sdk: fakeSDK(async (_id, interaction) => { interaction.notify({ type: "device_code", userCode: "PUBLIC", verificationUri: "https://auth.x.ai/device?user_code=PUBLIC" }); notified(); return pending; }) }); + try { + const login = owner.control("xai", { op: "login" }, "http://localhost:3000"); await notification; + const interaction = (await owner.native.status()).find((entry: { providerId: string }) => entry.providerId === "xai")!.interaction; + const command = { op: "open", attemptId: interaction.attemptId, actionId: interaction.browser.id }; + const opened = await owner.control("xai", command, "http://localhost:3000"); + assert(opened.browserAction); assert(!JSON.stringify(opened).includes("auth.x.ai")); + const id = new URL(opened.browserAction, "http://localhost:3000").searchParams.get("id")!; + assert.equal(await owner.takeBrowserAction(id, "http://other.invalid"), undefined); + assert.equal(await owner.takeBrowserAction(id, "http://localhost:3000"), "https://auth.x.ai/device?user_code=PUBLIC"); + assert.equal(await owner.takeBrowserAction(id, "http://localhost:3000"), undefined); + const stale = await owner.control("xai", command, "http://localhost:3000"); + await owner.control("xai", { op: "cancel" }, "http://localhost:3000"); + complete(credentials("xai")); await login; + assert.equal(await owner.takeBrowserAction(new URL(stale.browserAction!, "http://localhost:3000").searchParams.get("id")!, "http://localhost:3000"), undefined); + } finally { owner.close(); await fs.rm(scratch, { recursive: true, force: true }); } +}); + +test("web ChatGPT login supplies only an opaque browser action; cancel/replacement invalidates earlier launch", async () => { + const scratch = await fs.mkdtemp(path.join(os.tmpdir(), "lh-web-chat-action-")); + const owner = createWebSubscriptionOwner({ directory: scratch, sdk: fakeSDK(async (id) => credentials(id)) }); + try { + const first = await owner.control("chatgpt", { op: "login" }, "http://localhost:3000"); + assert(first.browserAction); assert(!JSON.stringify(first).includes("auth.openai.com")); + await owner.control("chatgpt", { op: "cancel" }, "http://localhost:3000"); + const second = await owner.control("chatgpt", { op: "login" }, "http://localhost:3000"); + assert(second.browserAction); + const firstId = new URL(first.browserAction, "http://localhost:3000").searchParams.get("id")!; + assert.equal(await owner.takeBrowserAction(firstId, "http://localhost:3000"), undefined); + const secondId = new URL(second.browserAction, "http://localhost:3000").searchParams.get("id")!; + const target = new URL((await owner.takeBrowserAction(secondId, "http://localhost:3000"))!); + assert.equal(target.origin, "https://auth.openai.com"); + const callback = new URL(target.searchParams.get("redirect_uri")!); + assert.equal(callback.hostname, "127.0.0.1"); assert.equal(callback.pathname, "/auth/callback"); + assert(target.searchParams.has("state")); assert(target.searchParams.has("code_challenge")); + } finally { owner.close(); await fs.rm(scratch, { recursive: true, force: true }); } +}); From d3c8709bd2ca6c0dde7019d7377e0ddb7cbca7c2 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 04:49:47 +0200 Subject: [PATCH 11/19] fix: preserve source data and recover tool math at presentation --- components/ai-elements/message.tsx | 12 +- components/answered-card.tsx | 25 +- components/practice-cards.tsx | 10 +- components/practice-dock.tsx | 5 +- components/question-dock.tsx | 15 +- docs/tool-content-recovery.md | 63 ++ package-lock.json | 3 + package.json | 3 + src/core/answer-presentation.ts | 57 ++ src/core/content-reader.ts | 215 ++++++ src/core/index.ts | 1 + src/core/math-delims.ts | 665 +++--------------- src/core/tool-parts.ts | 32 +- tests/content-recovery.test.ts | 116 +++ .../document-percent-regressions.json | 90 +++ .../tool-rendering/matrix-regressions.json | 135 ++++ .../mixed-escaping-regressions.json | 19 + tests/repair-math-display.test.tsx | 16 +- tests/tool-content-interaction.test.tsx | 78 ++ tests/tool-content-presentation.test.tsx | 164 +++++ tests/tool-parts.test.ts | 62 +- 21 files changed, 1134 insertions(+), 652 deletions(-) create mode 100644 docs/tool-content-recovery.md create mode 100644 src/core/answer-presentation.ts create mode 100644 src/core/content-reader.ts create mode 100644 tests/content-recovery.test.ts create mode 100644 tests/fixtures/tool-rendering/document-percent-regressions.json create mode 100644 tests/fixtures/tool-rendering/matrix-regressions.json create mode 100644 tests/fixtures/tool-rendering/mixed-escaping-regressions.json create mode 100644 tests/tool-content-interaction.test.tsx create mode 100644 tests/tool-content-presentation.test.tsx diff --git a/components/ai-elements/message.tsx b/components/ai-elements/message.tsx index e4b533b..9e22382 100644 --- a/components/ai-elements/message.tsx +++ b/components/ai-elements/message.tsx @@ -34,7 +34,7 @@ import { type PluginConfig, } from "streamdown"; import { mdComponents } from "@/components/md-blocks"; -import { transformMathDelimiters } from "@/src/core"; +import { repairMathForDisplay, transformMathDelimiters } from "@/src/core"; export type MessageProps = HTMLAttributes & { from: LhViewMessage["role"]; @@ -388,6 +388,16 @@ export const MessageResponse = memo( MessageResponse.displayName = "MessageResponse"; +/** Recovery is a tool-text presentation concern, not a shape/answer-data + * transform. Ordinary assistant/user prose keeps MessageResponse unchanged. */ +export function ToolContentResponse({ children, ...props }: MessageResponseProps) { + return ( + + {typeof children === "string" ? repairMathForDisplay(children) : children} + + ); +} + export type MessageToolbarProps = ComponentProps<"div">; export const MessageToolbar = ({ diff --git a/components/answered-card.tsx b/components/answered-card.tsx index f281e33..ce76b80 100644 --- a/components/answered-card.tsx +++ b/components/answered-card.tsx @@ -5,9 +5,9 @@ import { ConfidenceRail } from "@/components/confidence-rail"; import { GlyphMark } from "@/components/ui/glyph-mark"; import { ThinkingBlock } from "@/components/thinking-block"; import { cn } from "@/lib/utils"; -import { MessageResponse } from "@/components/ai-elements/message"; +import { ToolContentResponse } from "@/components/ai-elements/message"; import type { QuestionAnswerData } from "@/src/core"; -import { parseAskInput, parseQuizInput } from "@/src/core"; +import { answerForPresentation, parseAskInput, parseQuizInput } from "@/src/core"; /** * Static rendering of an answered question (tool part in `output-available`). @@ -19,7 +19,8 @@ import { parseAskInput, parseQuizInput } from "@/src/core"; * client — the card reveals his answer, the committed correct answer, and * the explanation, with NO verdict chips (owner ruling 2026-09-27). */ -export function AnsweredCard({ answer, input }: { answer: QuestionAnswerData; input?: unknown }) { +export function AnsweredCard({ answer: savedAnswer, input }: { answer: QuestionAnswerData; input?: unknown }) { + const answer = answerForPresentation(savedAnswer, input); const [whyOpen, setWhyOpen] = useState(false); // the tucked explanation (predictions, owner 2026-10-03) if (answer.dismissed) { return ( @@ -86,13 +87,13 @@ export function AnsweredCard({ answer, input }: { answer: QuestionAnswerData; in ))}
- {answer.questionText} + {answer.questionText}
{parsedDetails && ( // The details line, same paint as the pending dock (owner, // 2026-10-12): full markdown, LaTeX included.
- {parsedDetails} + {parsedDetails}
)} @@ -131,14 +132,14 @@ export function AnsweredCard({ answer, input }: { answer: QuestionAnswerData; in )} - {opt.label} + {opt.label} {descByIndex.get(opt.index) && ( // Ask-option subtext (owner, 2026-10-12): same markdown path // as the pending dock — LaTeX included. Quiz options carry // none (owner ruling 2026-09-26); the map stays empty for them. - + {descByIndex.get(opt.index)} - + )}
@@ -211,9 +212,9 @@ export function AnsweredCard({ answer, input }: { answer: QuestionAnswerData; in {whyOpen && (
Why: - {/* The markdown wrapper must go truly inline — a block

after the - "Why:" label created the oversized leading gap (owner's fix list). */} - {answer.explanation} + {/* Keep the first paragraph beside the label, not every paragraph: + later paragraphs must retain their recovered document structure. */} + {answer.explanation}

)}
@@ -225,7 +226,7 @@ export function AnsweredCard({ answer, input }: { answer: QuestionAnswerData; in {isExplain && answer.committed && (
The answer: - {answer.committed} + {answer.committed}
)} diff --git a/components/practice-cards.tsx b/components/practice-cards.tsx index 08cd053..eb4e507 100644 --- a/components/practice-cards.tsx +++ b/components/practice-cards.tsx @@ -1,8 +1,8 @@ "use client"; -import { MessageResponse } from "@/components/ai-elements/message"; +import { ToolContentResponse } from "@/components/ai-elements/message"; import { cn } from "@/lib/utils"; -import { repairMathForDisplay, type ArtifactPartProps } from "@/src/core"; +import type { ArtifactPartProps } from "@/src/core"; /** * The practice transcript cards (practice sessions, owner design @@ -66,7 +66,7 @@ export function ProblemCard({ part }: ArtifactPartProps) {
Problem
- {repairMathForDisplay(statement)} + {statement}
); @@ -133,7 +133,7 @@ export function ConcludeCard({ part }: ArtifactPartProps) { (owner round 3): the same quiet 50% register as quiz explanations. */} {verdict.length > 0 && (
- {repairMathForDisplay(verdict)} + {verdict}
)} {/* Then the divider, then the labeled model solution (Slice G: the @@ -144,7 +144,7 @@ export function ConcludeCard({ part }: ArtifactPartProps) {
Model solution
- {repairMathForDisplay(reveal)} + {reveal}
diff --git a/components/practice-dock.tsx b/components/practice-dock.tsx index e24f4f4..4c53b59 100644 --- a/components/practice-dock.tsx +++ b/components/practice-dock.tsx @@ -2,12 +2,11 @@ import { useCallback, useRef, useState } from "react"; import { ChevronRightIcon } from "lucide-react"; -import { MessageResponse } from "@/components/ai-elements/message"; +import { ToolContentResponse } from "@/components/ai-elements/message"; import { Button } from "@/components/ui/button"; import { joinDictation, useDictationTarget } from "@/components/dictation"; import { useEnterSubmits } from "@/components/enter-submits"; import { cn } from "@/lib/utils"; -import { repairMathForDisplay } from "@/src/core"; /** * The practice dock (practice sessions, owner design 2026-10-04): an @@ -109,7 +108,7 @@ export function PracticeDock({ problemCallId, input, expanded, onExpand, onSubmi
Problem
- {typeof input.statement === "string" ? repairMathForDisplay(input.statement) : input.statement} + {input.statement}
diff --git a/components/question-dock.tsx b/components/question-dock.tsx index 3b6f848..e0a94d7 100644 --- a/components/question-dock.tsx +++ b/components/question-dock.tsx @@ -5,10 +5,11 @@ import { ArrowDownIcon, ChevronRightIcon, PlayIcon } from "lucide-react"; import { Button } from "@/components/ui/button"; import { finishDictation, isDictationActive, joinDictation, useDictationTarget } from "@/components/dictation"; import { useEnterSubmits } from "@/components/enter-submits"; -import { MessageResponse } from "@/components/ai-elements/message"; +import { ToolContentResponse } from "@/components/ai-elements/message"; import { cn } from "@/lib/utils"; import { buildAnswer, + repairMathForDisplay, normalizeQuizType, CONFIDENCE_STOPS, DEFAULT_CONFIDENCE_INDEX, @@ -166,7 +167,7 @@ export function QuestionDock({ /** A plain-text teaser of a question for the collapsed bar: unwrap math, flatten common LaTeX. */ function teaserText(q: string): string { - return q + return repairMathForDisplay(q) .replace(/\\\(([\s\S]*?)\\\)/g, "$1") // \(...\) inline math .replace(/\\\[([\s\S]*?)\\\]/g, "$1") // \[...\] display math .replace(/\$\$?([^$]*)\$\$?/g, "$1") // standard $...$/$$...$$ math @@ -560,11 +561,11 @@ function QuestionDockForm({ {qd.mode === "multi-select" && " · select all that apply"}
- {qd.question} + {qd.question}
{qd.context && (
- {qd.context} + {qd.context}
)} @@ -600,7 +601,7 @@ function QuestionDockForm({ )} - {opt.label} + {opt.label} {kind !== "quiz" && opt.description && ( // Option subtext renders in Source Sans 3 (owner's mockup); // the "I don't know" copy stays JetBrains Mono. QUIZ options render @@ -609,9 +610,9 @@ function QuestionDockForm({ // records must not leak either. Same MessageResponse path as the // label (owner, 2026-10-12): descriptions are full markdown — // LaTeX included — not plain text. - + {opt.description} - + )} diff --git a/docs/tool-content-recovery.md b/docs/tool-content-recovery.md new file mode 100644 index 0000000..8151b1a --- /dev/null +++ b/docs/tool-content-recovery.md @@ -0,0 +1,63 @@ +# Tool-content presentation recovery + +## Contract + +Model-authored tool content is parsed against its schema before presentation. Math or document recovery must not change persisted tool arguments, option identities, learner answers, shuffling, grading, or future answer snapshots. Those use the source-derived, schema-parsed content. + +`ToolContentResponse` projects text through `repairMathForDisplay`, then uses the ordinary Markdown renderer. Teaching/probing questions, asks, recalls/explanations, and practice statements/solutions share this path. Ordinary assistant prose does not receive tool-content recovery. + +There are three separate responsibilities: + +1. **Argument/shape parsing:** decode encoded argument shapes, then schema conversion, validation and cleaning. No LaTeX recovery here. +2. **Content projection:** distinguish literal code, prose/document structure and math; recover only supported escaping defects. +3. **Presentation:** Markdown/KaTeX rendering, including paragraph layout. In labeled Why/answer reveals, only the first direct paragraph is inline with its label; subsequent paragraphs retain their normal layout. + +## Recovery rules + +### Literal code + +Code and inline-code source ranges come from the rendering stack's CommonMark parser with its math extensions. This covers fences, indentation, quotes and lists without maintaining separate container heuristics. The projection copies those source ranges verbatim; it does not serialize an AST back into Markdown. + +### Document escapes + +A literal two-character `\n` is different from a physical newline. Document-layout recovery requires evidence such as repeated document-escape candidates or a candidate alongside over-escaped known TeX commands. Complete known n-prefixed TeX words, including `\nu`, `\nabla`, `\neq` and declared n-prefixed macros, take precedence over interpreting their prefix as document layout. Code, genuine TeX comments and recognized path tokens are protected. + +Recovered double-dollar display framing puts the formula and closing fence on their own lines. Decoding the opening line alone can otherwise turn closing dollars into erroneous TeX content. + +### JSON-swallowed command prefixes + +Inside math, a control character can be the result of JSON consuming the start of a command: TAB + `ext` instead of `\text`, for example. Recovery reconstructs a command only if the control character plus the **complete following alphabetic word** names a known KaTeX command. All supported occurrences recover in one projection. + +The owner-approved typical-model-authoring policy is: + +- **Inline math:** recover recognized prefixes, including physical LF + `u` to `\nu`, and TAB + `o` to `\to`. There is no word-length cutoff. +- **Display math:** keep physical LF and CRLF as source formatting; recover recognized non-newline prefixes. +- **Correctly written commands:** remain commands in both modes. `\nu` is never decoded into LF + `u`. +- **Unrecognized whitespace:** remains unchanged. `x+y` does not become invented `\t`/`\ty` commands. + +The control-character case is inherently ambiguous: whitespace followed by a variable may happen to spell a command. This policy targets typical model-authored LaTeX, not every syntactically possible human whitespace arrangement. TeX parse success alone cannot distinguish the two meanings. + +### Extra command escaping, percentages and rows + +Recovery examines the original TeX context before changing backslash runs: + +- Complete native environment pairs and native `\substack` arguments preserve genuine row separators. +- Consistently extra-escaped environment pairs establish an escaping factor for commands **and rows**. For example, an encoded four-backslash row becomes two backslashes, not one. +- Outside protected native row/comment contexts, extra prefixes on known commands can recover. Unknown command words are not invented. +- Doubled percent escapes in ordinary numeric expressions can recover to `\%`, including numeric percentages at the end of a display formula. Genuine following comment text or equation rows remain literal. This is not a blanket `%` addition to an even-backslash rule. +- As an additional guard, partial command recovery does not turn parseable source into a TeX parse failure. Parse success is not the primary correctness test: both a corrupted matrix and a lost percentage can parse successfully. + +Unbalanced, unsupported or ambiguous content is not generally corrected into new mathematics. This is an escaping recovery projection, not a general TeX authoring repair system. There are no language-specific decoders. + +## Historical answers + +`answerForPresentation` joins model-authored display fields to the original tool call without changing recorded answer facts. Option labels bind only through unambiguous selected values, exact/source-recovered labels, or one uniquely remaining pair in a complete bijection. Ambiguous rows keep their saved snapshots. Saved order, indices, selected values, verdicts, confidence and learner text are retained. Dismissed answers are unchanged. No migration or record rewrite is performed. + +## Verification + +- `tests/content-recovery.test.ts`: recovery/preservation rules and adversarial row/comment/code examples. +- `tests/tool-content-presentation.test.tsx`: provenance-labeled synthetic corpus regressions, historical joins and ambiguity. +- `tests/tool-content-interaction.test.tsx`: select/submit/persist/reveal flow and paragraph structure. +- Existing Markdown, card, practice, tool-shape and grading suites remain applicable. + +The local frozen corpus can be replayed offline with `scripts/replay-tool-rendering-corpus.ts`. Replay verifies source hashes and preserves baseline findings; it reports glyph loss, matrix rows, rendering errors, code bytes, schema regressions, answer facts and projection convergence. Corpus collection and larger diagnostic batches are separate from product recovery and are not prerequisites for running these unit tests. diff --git a/package-lock.json b/package-lock.json index 7a91ffc..21e2398 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,7 +35,10 @@ "katex": "0.18.2", "linkedom": "^0.18.13", "lucide-react": "^1.33.0", + "mdast-util-from-markdown": "2.0.3", + "mdast-util-math": "3.0.0", "media-chrome": "^4.19.2", + "micromark-extension-math": "3.1.0", "mermaid": "^11.17.0", "motion": "^13.1.1", "nanoid": "^6.0.1", diff --git a/package.json b/package.json index 1b0c649..fefaa8c 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,10 @@ "katex": "0.18.2", "linkedom": "^0.18.13", "lucide-react": "^1.33.0", + "mdast-util-from-markdown": "2.0.3", + "mdast-util-math": "3.0.0", "media-chrome": "^4.19.2", + "micromark-extension-math": "3.1.0", "mermaid": "^11.17.0", "motion": "^13.1.1", "nanoid": "^6.0.1", diff --git a/src/core/answer-presentation.ts b/src/core/answer-presentation.ts new file mode 100644 index 0000000..ffc61ef --- /dev/null +++ b/src/core/answer-presentation.ts @@ -0,0 +1,57 @@ +/** Read-only provenance for historical question cards. Never persist this + * projection: answer identities, ordering, grading, and learner text remain + * the recorded facts; only model-authored display text comes from the call. */ +import { normalizeOptions, type QuestionAnswerData } from "./question-core.ts"; +import { parseAskInput, parseExplainInput, parseQuizInput } from "./tool-parts.ts"; +import { repairMathForDisplay } from "./math-delims.ts"; + +export function answerForPresentation(answer: QuestionAnswerData, input: unknown): QuestionAnswerData { + if (answer.dismissed) return answer; + const quiz = answer.kind === "quiz" ? parseQuizInput(input) : undefined; + const explain = answer.kind === "explain" ? parseExplainInput(input) : undefined; + const source = quiz ?? explain ?? (answer.kind === "ask" ? parseAskInput(input) : undefined); + if (!source) return answer; + const projected = { ...answer, questionText: source.question }; + if (quiz) projected.explanation = quiz.explanation; + if (explain) projected.committed = explain.correctAnswer; + if (!answer.options?.length || !("options" in source) || !source.options?.length) return projected; + + const original = normalizeOptions(source.options); + const saved = answer.options; + const bindings = new Map(); // saved position -> original position + const used = new Set(); + const bindUnique = (candidates: number[][]) => { + for (let si = 0; si < saved.length; si++) { + if (bindings.has(si)) continue; + const possible = candidates[si]!.filter((oi) => !used.has(oi)); + if (possible.length !== 1) continue; + const oi = possible[0]!; + // Two saved rows claiming the same source are ambiguous, even if + // each row independently has just one candidate. + if (candidates.some((other, sj) => sj !== si && !bindings.has(sj) && other.includes(oi))) continue; + bindings.set(si, oi); + used.add(oi); + } + }; + // Selected-answer values are stable identities when the record retained + // them. Do not normalize those values, recompute a shuffle, or regrade. + bindUnique(saved.map((option) => { + const values = answer.answers.filter((a) => a.index === option.index).map((a) => a.value); + return original.flatMap((o, oi) => values.includes(o.value) ? [oi] : []); + })); + const recoveredLabels = original.map((o) => repairMathForDisplay(o.label)); + bindUnique(saved.map((option) => original.flatMap((o, oi) => + o.label === option.label || recoveredLabels[oi] === option.label ? [oi] : []))); + // A bijection with one remaining pair has no ordering ambiguity. With + // multiple unmatched rows, keep their snapshots rather than guess. + if (saved.length === original.length && bindings.size === saved.length - 1) { + const si = saved.findIndex((_, i) => !bindings.has(i)); + const oi = original.findIndex((_, i) => !used.has(i)); + bindings.set(si, oi); + } + projected.options = saved.map((option, si) => { + const oi = bindings.get(si); + return oi === undefined ? option : { ...option, label: original[oi]!.label }; + }); + return projected; +} diff --git a/src/core/content-reader.ts b/src/core/content-reader.ts new file mode 100644 index 0000000..a774094 --- /dev/null +++ b/src/core/content-reader.ts @@ -0,0 +1,215 @@ +/** Source-preserving boundaries for tool-authored Markdown. Recovery never + * mutates input, reserializes Markdown, or enters protected literal content. */ +import katex from "katex"; +import { fromMarkdown } from "mdast-util-from-markdown"; +import { mathFromMarkdown } from "mdast-util-math"; +import { math } from "micromark-extension-math"; + +export type ContentSpan = + | { kind: "prose" | "code"; raw: string } + | { kind: "math"; raw: string; open: string; body: string; close: string; display: boolean }; +const blankLine = /^\r?\n[ \t]*\r?\n/; +const escaped = (text: string, i: number) => { + let n = 0; while (i > 0 && text[--i] === "\\") n++; + return n % 2 === 1; +}; +const runLength = (text: string, i: number, char: string) => { let n = 0; while (text[i + n] === char) n++; return n; }; +type MarkdownNode = { type: string; position?: { start: { offset?: number }; end: { offset?: number } }; children?: MarkdownNode[] }; +/** Use the rendering stack's actual CommonMark+math grammar for literal + * ranges, including nested quotes/lists and implicit container endings. + * Keep source offsets: never serialize the AST back into Markdown. */ +function literalRanges(text: string): Array<{ start: number; end: number }> { + const pending: MarkdownNode[] = [fromMarkdown(text, { extensions: [math({ singleDollarTextMath: true })], mdastExtensions: [mathFromMarkdown()] })]; + const ranges: Array<{ start: number; end: number }> = []; + while (pending.length) { + const node = pending.pop()!; + if (node.type === "code" || node.type === "inlineCode") { + const start = node.position?.start.offset, end = node.position?.end.offset; + if (start !== undefined && end !== undefined) ranges.push({ start, end }); + } else if (node.children) pending.push(...node.children); + } + return ranges.sort((a, b) => a.start - b.start); +} + +/** Mirrors the app's native dollar-run and legacy-delimiter conventions, + * with code and blank paragraphs as inline boundaries. */ +export function readContentSpans(text: string): ContentSpan[] { + const spans: ContentSpan[] = []; + const literals = literalRanges(text); + let literal = 0, i = 0, plain = 0; + const opaque = (end: number) => { + if (plain < i) spans.push({ kind: "prose", raw: text.slice(plain, i) }); + spans.push({ kind: "code", raw: text.slice(i, end) }); i = plain = end; + }; + while (i < text.length) { + while (literal < literals.length && literals[literal]!.end <= i) literal++; + const barrier = literals[literal]?.start ?? Infinity; + if (i >= barrier) { opaque(literals[literal]!.end); continue; } + let open = "", close = "", display = false, after = -1, at = -1; + if (text[i] === "\\" && !escaped(text, i) && ["(", "["].includes(text[i + 1] ?? "")) { + open = text.slice(i, i + 2); close = open === "\\(" ? "\\)" : "\\]"; display = open === "\\["; + for (let j = i + 2; j < text.length - 1; j++) { + if (j >= barrier || (!display && blankLine.test(text.slice(j)))) break; + if (text.startsWith(close, j) && !escaped(text, j)) { at = j; after = j + 2; break; } + } + } else if (text[i] === "$" && !escaped(text, i)) { + const size = runLength(text, i, "$"); open = "$".repeat(size); + const eol = text.indexOf("\n", i + size); + const before = text.slice(text.lastIndexOf("\n", i - 1) + 1, i); + display = size >= 2 && /^[ \t]{0,3}$/.test(before) && !text.slice(i + size, eol < 0 ? text.length : eol).includes("$"); + if (display) { + let start = eol; + while (start >= 0 && start < text.length) { + start++; + const next = text.indexOf("\n", start); + const match = /^([ \t]{0,3})(\$+)[ \t]*\r?$/.exec(text.slice(start, next < 0 ? text.length : next)); + if (match && match[2]!.length >= size) { at = start + match[1]!.length; close = match[2]!; after = at + close.length; break; } + start = next; + } + } else { + for (let j = i + size; j < text.length;) { + if (j >= barrier || blankLine.test(text.slice(j))) break; + if (text[j] === "$" && !escaped(text, j)) { + const n = runLength(text, j, "$"); + if (n === size) { at = j; close = open; after = j + n; break; } + j += n; + } else j++; + } + } + } + if (at >= 0) { + if (plain < i) spans.push({ kind: "prose", raw: text.slice(plain, i) }); + spans.push({ kind: "math", raw: text.slice(i, after), open, body: text.slice(i + open.length, at), close, display }); + i = plain = after; + } else i += open.length || 1; + } + if (plain < text.length) spans.push({ kind: "prose", raw: text.slice(plain) }); + return spans; +} + +export function mapMathContent(text: string, transform: (body: string, display: boolean) => string): string { + return readContentSpans(text).map(span => span.kind === "math" ? span.open + transform(span.body, span.display) + span.close : span.raw).join(""); +} + +const knownCommands = new Map(); +/** Probe only a command name, never arbitrary content. Missing-argument errors + * still identify a known command. No trust, macros, IO, or persistent TeX state. */ +export function knownTexCommand(name: string): boolean { + if (!/^[A-Za-z]+$/.test(name)) return false; + const cached = knownCommands.get(name); if (cached !== undefined) return cached; + let known = true; + try { katex.renderToString("\\" + name, { throwOnError: true, trust: false, strict: false }); } + catch (error) { known = !String(error).includes("Undefined control sequence: \\" + name); } + knownCommands.set(name, known); + return known; +} + +export function canParseTex(body: string, display: boolean): boolean { + try { katex.renderToString(body, { throwOnError: true, trust: false, strict: false, displayMode: display }); return true; } + catch { return false; } +} + +export interface TexRange { start: number; end: number; kind: "row" | "encoded" | "comment"; factor?: number } +export function numericPercentEscape(inner: string, percent: number): boolean { + let start = percent; while (start > 0 && inner[start - 1] === "\\") start--; + if (percent - start < 2 || (percent - start) % 2 !== 0 || !/\d[ \t]*$/.test(inner.slice(0, start))) return false; + const tail = inner.slice(percent + 1); + // A final source-formatting LF before the closing fence is not a next + // equation row. A meaningful following line or comment text remains literal. + if (/^[\s.)\]}]*$/.test(tail)) return true; + return !/[\r\n]/.test(tail) && !/^[ \t]*[A-Za-z]/.test(tail); +} + +/** TeX boundaries shared by document decoding and command recovery. Native + * rows are literal; consistently extra-escaped environment pairs supply an + * explicit escaping factor for both commands AND row separators. */ +export function texRanges(inner: string): TexRange[] { + const ranges: TexRange[] = []; + const stack: Array<{ name: string; start: number; factor: number }> = []; + let i = 0; + while (i < inner.length) { + if (inner[i] === "%" && !escaped(inner, i)) { + let start = i; while (start > 0 && inner[start - 1] === "\\") start--; + const factor = stack.at(-1)?.factor ?? ranges.filter(r => r.kind === "encoded" && i >= r.start && i < r.end).at(-1)?.factor; + const encodedLiteral = factor && factor > 1 && (i - start) % factor === 0 && ((i - start) / factor) % 2 === 1; + const native = stack.some(frame => frame.factor === 1) || ranges.some(r => r.kind === "row" && i >= r.start && i < r.end); + if (encodedLiteral || (!native && numericPercentEscape(inner, i))) { i++; continue; } + const newline = inner.indexOf("\n", i); + const end = newline < 0 ? inner.length : newline + 1; + ranges.push({ start: i, end, kind: "comment" }); i = end; continue; + } + if (inner[i] !== "\\") { i++; continue; } + let end = i + 1; while (inner[end] === "\\") end++; + const count = end - i; + const marker = /^(begin|end)\s*\{([^{}]+)\}/.exec(inner.slice(end)); + if (marker) { + const factor = count % 2 === 1 ? 1 : count; + if (marker[1] === "begin") stack.push({ name: marker[2]!, start: i, factor }); + else if (stack.at(-1)?.name === marker[2] && stack.at(-1)?.factor === factor) { + const open = stack.pop()!; + ranges.push({ start: open.start, end: end + marker[0].length, kind: factor === 1 ? "row" : "encoded", factor }); + } + i = end + marker[0].length; continue; + } + // KaTeX's substack macro expands its braced argument into subarray: + // these are row separators even without literal begin/end markers. + const substack = /^substack\s*\{/.exec(inner.slice(end)); + if (substack) { + let depth = 1, j = end + substack[0].length; + for (; j < inner.length && depth > 0; j++) { + if (inner[j] === "%" && !escaped(inner, j)) { const lf = inner.indexOf("\n", j); j = lf < 0 ? inner.length : lf; continue; } + if (!escaped(inner, j)) { if (inner[j] === "{") depth++; else if (inner[j] === "}") depth--; } + } + if (depth === 0) ranges.push({ start: i, end: j, kind: count % 2 === 1 ? "row" : "encoded", factor: count % 2 === 1 ? 1 : count }); + } + i = end; + } + return ranges.sort((a, b) => a.start - b.start); +} + +export function outsideTexComments(body: string, transform: (part: string) => string): string { + let from = 0; const out: string[] = []; + for (const range of texRanges(body).filter(r => r.kind === "comment")) { + out.push(transform(body.slice(from, range.start)), body.slice(range.start, range.end)); from = range.end; + } + out.push(transform(body.slice(from))); return out.join(""); +} + +/** Document escapes are not arbitrary string decoding. Complete known TeX + * words win over the prefix `\\n`; code is never inspected or rewritten. */ +export function recoverDocumentLayout(text: string): string { + const spans = readContentSpans(text); + const candidates = spans.filter(span => span.kind !== "code").map(span => span.raw).join("\u0000"); + const declared = new Set([ + ...candidates.matchAll(/\\(?:newcommand|renewcommand|providecommand)\*?\s*(?:\{\s*)?\\(n[A-Za-z]+)/g), + ...candidates.matchAll(/\\(?:def|gdef|edef)\s*\\(n[A-Za-z]+)/g), + ].map(m => m[1]!)); + const knownN = (name: string) => declared.has(name) || knownTexCommand(name); + const tokens = [...candidates.matchAll(/(\\+)(n[A-Za-z]*)/g)]; + const evidence = tokens.filter(m => m[1]!.length % 2 === 1 && !knownN(m[2]!)); + const structural = evidence.length >= 2 || candidates.includes("\\n\\n") || (evidence.length > 0 && + [...candidates.matchAll(/(\\+)([A-Za-z]+)/g)].some(m => m[1]!.length % 2 === 0 && knownTexCommand(m[2]!))); + if (!structural) return text; + const decode = (part: string) => part.replace(/(\\+)(n[A-Za-z]*)/g, (whole: string, slashes: string, name: string, offset: number) => { + if (slashes.length % 2 === 0 || knownN(name)) return whole; + const word = part.slice(0, offset).split(/\s/).at(-1) ?? ""; + if (/^(?:[A-Za-z]:[\\/]|https?:\/\/|\/)/.test(word + whole)) return whole; + return slashes.slice(0, -1) + "\n" + name.slice(1); + }); + const projected = spans.map(span => ({ span, body: span.kind === "math" ? outsideTexComments(span.body, decode) : span.kind === "code" ? span.raw : decode(span.raw) })); + let out = ""; + for (let i = 0; i < projected.length; i++) { + const { span, body } = projected[i]!; + if (span.kind !== "math") { out += body; continue; } + // Decoding document layout can expose a flow opener whose close was + // written on the formula line. Frame BOTH ends; otherwise Markdown + // consumes the closing dollars as TeX and introduces a rendering error. + if (/^\${2,}$/.test(span.open) && body !== span.body && body.includes("\n")) { + if (!/^[ \t]{0,3}$/.test(out.slice(out.lastIndexOf("\n") + 1))) out += "\n\n"; + out += span.open + (body.startsWith("\n") ? "" : "\n") + body + (body.endsWith("\n") ? "" : "\n") + span.close; + const next = projected[i + 1]; + if (next && !/^[\r\n]/.test(next.span.kind === "math" ? next.span.open : next.body)) out += "\n\n"; + } else out += span.open + body + span.close; + } + return out; +} diff --git a/src/core/index.ts b/src/core/index.ts index b4106d7..77de4ed 100644 --- a/src/core/index.ts +++ b/src/core/index.ts @@ -47,6 +47,7 @@ export * from "./input-events.ts"; // the deleted server projection to the client side. Pure, browser-safe. export * from "./view.ts"; export * from "./tool-parts.ts"; +export * from "./answer-presentation.ts"; export * from "./prices.ts"; export * from "./context-usage.ts"; export type { ChatGPTStatus, ChatGPTControlBridge } from "./contracts/chatgpt.ts"; diff --git a/src/core/math-delims.ts b/src/core/math-delims.ts index b939054..21d7bed 100644 --- a/src/core/math-delims.ts +++ b/src/core/math-delims.ts @@ -1,572 +1,121 @@ -/** - * Legacy math delimiter preprocessing — the \(...\)/\[...\] reader. - * - * Current authoring uses `$...$` inline and `$$...$$` on separate lines for - * display (owner, 2026-10-12). This pure transform keeps correctly formed - * old backslash-delimited content readable; dollar math passes through to - * Streamdown's native parser. Escape restoration below recognizes both - * syntaxes while retaining the existing recovery rules. - * - * Rules: - * - `\(...\)` → `$$...$$` (two-dollar inline, still accepted by micromark), - * `\[...\]` → `$$\n...\n$$` (block form: flow math). - * - An escaped delimiter (`\\(` = literal backslash + paren) is left alone. - * - Nothing inside fenced code blocks or inline code spans is touched. - * - Unbalanced delimiters are left untouched: Streamdown streams chunks, and - * an incomplete `\[` completes on a later chunk (the whole text is - * re-transformed each render). - * - * Pure string → string. No markdown parsing — a single linear scan with a - * code-state machine, cheap enough to run in the render path of a memo'd - * component. - */ +/** Shared Markdown/TeX boundaries for legacy delimiter compatibility and + * display-only tool-content recovery. Source arguments are never rewritten. */ +import { + canParseTex, knownTexCommand, mapMathContent, numericPercentEscape, outsideTexComments, + readContentSpans, recoverDocumentLayout, texRanges, +} from "./content-reader.ts"; -const INLINE_OPEN = "\\("; -const INLINE_CLOSE = "\\)"; -const DISPLAY_OPEN = "\\["; -const DISPLAY_CLOSE = "\\]"; - -const FENCE_OPEN_RE = /^ {0,3}(`{3,}|~{3,})/; - -/** - * Is the backslash at `i` itself escaped (i.e. preceded by an odd number of - * backslashes)? If so, the `\(`/`\[` starting there is a literal backslash - * followed by a bracket — not a delimiter. - */ -function isEscaped(text: string, i: number): boolean { - let count = 0; - for (let j = i - 1; j >= 0 && text[j] === "\\"; j--) count += 1; - return count % 2 === 1; -} - -/** Length of the backtick run starting at `i` (0 if `text[i]` is not a tick). */ -function backtickRun(text: string, i: number): number { - let count = 0; - while (text[i + count] === "`") count += 1; - return count; -} - -/** - * Try to find the closing delimiter for the opening one at `i` (2 chars, - * already verified unescaped). The close scan stops at inline code spans and - * fence starts (barriers — math never contains them), and returns -1 when the - * delimiter is unbalanced (mid-stream chunk). - */ -function findClose( - text: string, - from: number, - close: string, -): number { - for (let j = from; j < text.length - 1; j++) { - if (text[j] === "`") return -1; // inline code barrier - if ( - text[j] === "\\" && - text[j + 1] === close[1] && - !isEscaped(text, j) - ) { - return j; - } - } - return -1; -} - -/** True when `i` sits at the start of a line (position 0 or right after \n). */ -function atLineStart(text: string, i: number): boolean { - return i === 0 || text[i - 1] === "\n"; -} - -/** - * Copy a fenced code block starting at `i` (line start, fence regex matched) - * verbatim to the output. Returns the index after the closing fence line — - * or the end of the text when the fence is unclosed (mid-stream: everything - * after is literal code until the chunk completes). - */ -function copyFencedBlock(text: string, i: number, out: string[]): number { - const open = FENCE_OPEN_RE.exec(text.slice(i)); - const marker = open?.[1] ?? "```"; - const closeRe = new RegExp(`^ {0,3}\\${marker[0]}{${marker.length},}[ \\t]*$`); - let j = i; - // Copy lines until the matching closing fence. - while (j < text.length) { - const lineEnd = text.indexOf("\n", j); - const next = lineEnd === -1 ? text.length : lineEnd + 1; - const line = text.slice(j, lineEnd === -1 ? text.length : lineEnd); - out.push(text.slice(j, next)); - if (j > i && closeRe.test(line)) return next; // closing fence line - j = next; - } - return text.length; -} - -/** Is the `\[` at `i` block-positioned (own line, only whitespace before)? */ -function displayIsBlockPositioned(text: string, i: number): boolean { - for (let j = i - 1; j >= 0; j--) { - const ch = text[j]; - if (ch === "\n") return true; - if (ch !== " " && ch !== "\t") return false; - } - return true; -} - -/** Is the `\[` at `i` indented (whitespace before it on the line)? */ -function displayIsIndented(text: string, i: number): boolean { - let sawWhitespace = false; - for (let j = i - 1; j >= 0; j--) { - const ch = text[j]; - if (ch === "\n") return sawWhitespace; - if (ch !== " " && ch !== "\t") return false; // mid-paragraph - sawWhitespace = true; - } - return sawWhitespace; -} - -/** Does the closing delimiter end its line (only \n or EOF after it)? */ -function closeEndsLine(text: string, afterClose: number): boolean { - return afterClose >= text.length || text[afterClose] === "\n"; -} - -/** - * Rewrite balanced `\(...\)` → `$$...$$` and `\[...\]` → `$$\n...\n$$`. - * Everything else passes through byte-for-byte. - */ -/** The escaped-dollar-aware inline closer (owner, 2026-10-12): micromark's - * mathText closer is code-span logic — ANY size-matched `$` run closes, - * `\$` included, so `$x = \$5$` closes at the `\$` and errors. KaTeX's - * own auto-render scanner skips escaped dollars, and KaTeX renders `\$` - * fine — the author's intent is unambiguous: an escaped dollar (odd number - * of backslashes before it) never closes the span. Mirrors findDollarClose's - * inline rules otherwise (exact size match, whole-run skip). */ -function findInlineDollarCloseEscapeAware(text: string, from: number): number { - for (let j = from; j < text.length;) { - if (text[j] !== "$") { - j += 1; - continue; - } - if (isEscaped(text, j)) { - j += 1; - continue; // `\$` never closes - } - const count = dollarRun(text, j); - if (count === 1) return j; - j += count; - } - return -1; -} - -/** Replace `\$` (odd-backslash dollars) inside a math span with the - * dollar-free `\text{\textdollar}` — KaTeX-safe in math mode (bare - * `\textdollar` is text-mode-only there). Even-backslash dollars (`\\$`, - * row break + dollar) are left alone. */ -function replaceEscapedDollars(content: string): string { - let replaced = ""; - for (let j = 0; j < content.length;) { - if (content[j] === "\\" && content[j + 1] === "$" && !isEscaped(content, j)) { - replaced += "\\text{\\textdollar}"; - j += 2; - } else { - replaced += content[j]; - j += 1; - } - } - return replaced; -} - -export function transformMathDelimiters(markdown: string): string { - const out: string[] = []; - let i = 0; - - while (i < markdown.length) { - const ch = markdown[i]!; - - // Fenced code blocks: copied verbatim (only detectable at line start). - if (atLineStart(markdown, i) && FENCE_OPEN_RE.test(markdown.slice(i))) { - i = copyFencedBlock(markdown, i, out); - continue; - } - - // Inline code spans: copied verbatim. - if (ch === "`") { - const run = backtickRun(markdown, i); - let j = i + run; - // Find the matching run (same length, per CommonMark). - while (j < markdown.length) { - const nextRun = backtickRun(markdown, j); - if (nextRun === run) break; - j += Math.max(nextRun, 1); - } - if (j < markdown.length) { - out.push(markdown.slice(i, j + run)); - i = j + run; - } else { - // Unclosed run (mid-stream): literal, keep scanning after it. - out.push(markdown.slice(i, i + run)); - i += run; - } - continue; - } - - // Math delimiters. - if (ch === "\\" && !isEscaped(markdown, i)) { - const next = markdown[i + 1]; - if (next === "(" || next === "[") { - const isDisplay = next === "["; - const open = isDisplay ? DISPLAY_OPEN : INLINE_OPEN; - const close = isDisplay ? DISPLAY_CLOSE : INLINE_CLOSE; - const closeAt = findClose(markdown, i + open.length, close); - if (closeAt !== -1) { - const content = markdown.slice(i + open.length, closeAt).trim(); - if (content.length > 0) { - if (isDisplay && displayIsIndented(markdown, i)) { - // Indented display math (inside a list item, quote, …): - // an indented $$ flow block half-parses inside list - // continuations (empty math, eaten items), so degrade to - // the inline form — renders inline, stays inside the item. - // mathText cannot span line endings, so collapse them. - out.push(`$$${content.replace(/\s*\n\s*/g, " ")}$$`); - } else if (isDisplay) { - // Flow math ($$ alone on its lines) renders as display - // KaTeX. Mid-paragraph \[...\] (or trailing text after the - // closer) would half-parse into literal dollars + KaTeX - // errors — pad with blank lines to force a clean block. - // Block-positioned math passes through in place. - const afterClose = closeAt + close.length; - const padBefore = - displayIsBlockPositioned(markdown, i) ? "" : "\n\n"; - const padAfter = - closeEndsLine(markdown, afterClose) ? "" : "\n\n"; - out.push(`${padBefore}$$\n${content}\n$$${padAfter}`); - } else { - // Preserve the legacy reader's two-dollar inline output - // (renders inline, not display). - out.push(`$$${content}$$`); - } - i = closeAt + close.length; - continue; - } - } - // Unbalanced or empty: leave both characters untouched. - } - } - - // Escaped dollars inside inline math (owner, 2026-10-12): normalize - // BEFORE tokenization — inside a balanced inline span, `\$` becomes the - // dollar-free `\text{\textdollar}` so the scanner's closer is the one - // the author meant (micromark would otherwise close at the `\$`, error - // out, and cascade). Display runs are copied whole and left untouched — - // a lone `$` can't close `$$`, so `\$` already survives there — and - // copying the run stops its second `$` posing as an inline opener. - // Spans without escaped dollars emit byte-identical text; prose `\$`, - // code, and unbalanced spans are untouched. - if (ch === "$" && !isEscaped(markdown, i)) { - const run = dollarRun(markdown, i); - if (run === 1) { - const closeAt = findInlineDollarCloseEscapeAware(markdown, i + 1); - if (closeAt !== -1) { - const content = markdown.slice(i + 1, closeAt); - const replaced = replaceEscapedDollars(content); - if (replaced !== content) { - out.push(`$${replaced}$`); - i = closeAt + 1; - continue; - } - } - } else { - out.push(markdown.slice(i, i + run)); - i += run; - continue; - } - } - - out.push(ch); - i += 1; - } - - return out.join(""); -} - -/** - * LaTeX-escape restoration (owner ruling 2026-09-26) — the JSON-debris fix. - * - * When a model emits LaTeX with SINGLE backslashes inside JSON tool-call - * arguments (`\forall`, not the properly escaped `\\forall`), JSON escape - * resolution eats the command's first letter: `\f` → form feed + "orall", - * `\v` → vertical tab + "arphi", `\t` → tab + "o". The debris survives into - * the persisted RAW args and renders as garbage. Inside a MATH SPAN these - * control chars can never be legitimate content, so restoring the two-char - * escape reconstructs the model's intent byte-perfectly. Outside spans the - * same chars might be real prose (pasted tables, formatted text), so only - * balanced math spans are touched. The legacy `\(…\)` / `\[…\]` pass stays - * unchanged; the dollar reader adds `$…$` and fenced `$$…$$` recognition, - * excluding code and escaped opening dollars. Unbalanced spans pass through. - * - * ASYMMETRIC NEWLINE RULE (owner ruling 2026-09-27, Option A): inline spans - * restore all six debris chars — real newlines/tabs inside INLINE math - * essentially never occur (options and inline math are single-line by - * nature). Display spans restore five and KEEP real newlines — multi-line - * display math (aligned environments, equation systems) is common, and a - * real line break is byte-identical on the wire to sloppy `\neq`/`\nu`/ - * `\neg`/`\nabla` (JSON `\n` resolves to a newline either way). Corrupting - * legitimate content is strictly worse than failing to repair that rarest - * slice, so display-span n-command sloppiness stays broken by design. - */ -const ESCAPE_DEBRIS: Record = { - "\b": "\\b", - "\t": "\\t", - "\n": "\\n", - "\v": "\\v", - "\f": "\\f", - "\r": "\\r", +const escaped = (text: string, i: number) => { + let n = 0; while (i > 0 && text[--i] === "\\") n++; + return n % 2 === 1; }; -const DEBRIS_RE = /[\b\t\n\v\f\r]/; - -/** Display spans keep real newlines (multi-line equations) — debris there is - * everything except \n (a lone real \r never occurs in authored text, and - * `\right`/`\rho`/`\rangle` corruption is common in display math). */ -const DISPLAY_DEBRIS_RE = /[\b\t\v\f\r]/; - -/** Shared display-only repair. Keep raw arguments and model history unchanged. */ -export function repairMathForDisplay(text: string): string { - return healOverescapedCommands(restoreMathEscapes(text)); +/** Preserve the legacy list-continuation policy: indented display math + * degrades to inline rather than disrupting list structure. Literal code + * indentation is already protected by the shared reader. */ +function indented(text: string, i: number): boolean { + const prefix = text.slice(text.lastIndexOf("\n", i - 1) + 1, i); + return prefix.length > 0 && /^[ \t]+$/.test(prefix); +} +function blockPosition(text: string, i: number): boolean { + return /^[ \t]*$/.test(text.slice(text.lastIndexOf("\n", i - 1) + 1, i)); } +/** micromark closes native single-dollar spans at escaped dollars too. + * Replace an intended literal dollar with equivalent dollar-free TeX before + * tokenization. Code, prose, unbalanced spans and display dollars stay raw. */ +function replaceEscapedDollars(content: string): string { + let out = ""; + for (let i = 0; i < content.length; i++) { + if (content[i] === "\\" && content[i + 1] === "$" && !escaped(content, i)) { + out += "\\text{\\textdollar}"; i++; + } else out += content[i]; + } + return out; +} + +/** Read old \(...\)/\[...\] syntax without touching literal content or + * reparsing native math as Markdown. Dollar authoring remains native. */ +export function transformMathDelimiters(markdown: string): string { + let offset = 0; + return readContentSpans(markdown).map(span => { + const start = offset; offset += span.raw.length; + if (span.kind !== "math") return span.raw; + if (span.open === "$") return "$" + replaceEscapedDollars(span.body) + span.close; + if (span.open !== "\\(" && span.open !== "\\[") return span.raw; + const body = span.body.trim(); + if (!body) return span.raw; + if (span.open === "\\(") return `$$${body}$$`; + if (indented(markdown, start)) return `$$${body.replace(/\s*\n\s*/g, " ")}$$`; + const before = blockPosition(markdown, start) ? "" : "\n\n"; + const end = start + span.raw.length; + const after = end === markdown.length || markdown[end] === "\n" ? "" : "\n\n"; + return `${before}$$\n${body}\n$$${after}`; + }).join(""); +} + +const DEBRIS: Record = { + "\b": "\\b", "\t": "\\t", "\v": "\\v", "\f": "\\f", "\r": "\\r", +}; + +/** Recover JSON-swallowed command prefixes only in recognized math, never + * in code or genuine TeX comments. Restore all debris in one pass. Physical + * display newlines and CRLF remain source whitespace. Inline LF recovery is + * limited to complete known n-command names. Under the approved typical- + * model-authoring policy this includes newline+u→nu in inline math only; + * already-correct commands are never decoded into whitespace. */ export function restoreMathEscapes(text: string): string { - if (!DEBRIS_RE.test(text)) return text; - return restoreDollarMathEscapes(restoreLegacyMathEscapes(text)); + if (!/[\b\t\n\v\f\r]/.test(text)) return text; + return mapMathContent(text, (body, display) => outsideTexComments(body, part => { + const restored = part.replace(/[\b\t\v\f\r]/g, (char, offset: number) => { + if (char === "\r" && part[offset + 1] === "\n") return char; + const suffix = /^[A-Za-z]+/.exec(part.slice(offset + 1))?.[0]; + return suffix && knownTexCommand(DEBRIS[char]!.slice(1) + suffix) ? DEBRIS[char]! : char; + }); + if (display) return restored; + return restored.replace(/\n([A-Za-z]+)/g, (whole, suffix: string) => + knownTexCommand("n" + suffix) ? "\\n" + suffix : whole); + })); } -/** Keep legacy recovery byte-for-byte; only the recognized syntax expands. */ -function restoreLegacyMathEscapes(text: string): string { - if (!DEBRIS_RE.test(text)) return text; - const out: string[] = []; - let i = 0; - while (i < text.length) { - const ch = text[i]!; - if (ch === "\\" && !isEscaped(text, i)) { - const next = text[i + 1]; - if (next === "(" || next === "[") { - const isDisplay = next === "["; - const open = isDisplay ? DISPLAY_OPEN : INLINE_OPEN; - const close = isDisplay ? DISPLAY_CLOSE : INLINE_CLOSE; - const closeAt = findClose(text, i + open.length, close); - if (closeAt !== -1) { - const inner = text.slice(i + open.length, closeAt); - const debris = isDisplay ? DISPLAY_DEBRIS_RE : DEBRIS_RE; - out.push(open + inner.replace(debris, (c) => ESCAPE_DEBRIS[c]!) + close); - i = closeAt + close.length; - continue; - } - } - } - out.push(ch); - i += 1; - } - return out.join(""); -} +const commandSymbol = /[,;:!|{}%]/; -function dollarRun(text: string, i: number): number { - let count = 0; - while (text[i + count] === "$") count += 1; - return count; -} - -/** Native math fences start at a line's block position; one-line $$…$$ - * remains inline. A flow opener may have metadata, but never another $. */ -function dollarIsDisplay(text: string, i: number, size: number): boolean { - if (size < 2 || !displayIsBlockPositioned(text, i)) return false; - const end = text.indexOf("\n", i + size); - return !text.slice(i + size, end === -1 ? text.length : end).includes("$"); -} - -/** Match micromark's delimiter runs: exact size inline, at least the - * opener's size on an otherwise-empty line for display. Inside math, - * backslashes are TeX data, not Markdown escapes for closing dollars. */ -function findDollarClose( - text: string, - from: number, - size: number, - isDisplay: boolean, -): { at: number; after: number } | undefined { - if (isDisplay) { - let lineEnd = text.indexOf("\n", from); - while (lineEnd !== -1) { - const start = lineEnd + 1; - lineEnd = text.indexOf("\n", start); - const line = text.slice(start, lineEnd === -1 ? text.length : lineEnd); - const close = /^([ \t]{0,3})(\$+)[ \t]*\r?$/.exec(line); - if (close && close[2]!.length >= size) { - const at = start + close[1]!.length; - return { at, after: at + close[2]!.length }; - } +/** Decode prefixes and row separators according to ORIGINAL TeX context. + * A native pair is literal; an encoded pair supplies a factor, so an encoded + * four-backslash row becomes TWO, never a command prefix. Unknown commands + * without that structural evidence are left alone. */ +function recoverCommandRuns(inner: string, display: boolean): string { + const ranges = texRanges(inner); + const recovered = inner.replace(/\\+/g, (run: string, offset: number) => { + const contexts = ranges.filter(r => offset >= r.start && offset < r.end); + if (contexts.some(r => r.kind === "row" || r.kind === "comment") || run.length % 2 !== 0) return run; + const next = inner[offset + run.length] ?? ""; + const encoded = contexts.filter(r => r.kind === "encoded").at(-1); + const word = /^[A-Za-z]+/.exec(inner.slice(offset + run.length))?.[0]; + if (encoded) { + const factor = encoded.factor!; + const rows = run.length % (2 * factor) === 0; + const command = word ? knownTexCommand(word) : commandSymbol.test(next); + if (run.length % factor === 0 && (rows || command)) return "\\".repeat(run.length / factor); + return run; } - return undefined; - } - for (let j = from; j < text.length;) { - if (text[j] !== "$") { - j += 1; - continue; - } - const count = dollarRun(text, j); - if (count === size) return { at: j, after: j + count }; - j += count; - } - return undefined; -} - -/** Dollar recognition only — the same six/five-character repair maps, - * with no over-escaping normalization or mathematical validation. */ -function restoreDollarMathEscapes(text: string): string { - if (!DEBRIS_RE.test(text) || !text.includes("$")) return text; - const out: string[] = []; - let i = 0; - while (i < text.length) { - if (atLineStart(text, i) && FENCE_OPEN_RE.test(text.slice(i))) { - i = copyFencedBlock(text, i, out); - continue; - } - if (text[i] === "`") { - const run = backtickRun(text, i); - let j = i + run; - while (j < text.length) { - const nextRun = backtickRun(text, j); - if (nextRun === run) break; - j += Math.max(nextRun, 1); - } - const after = j < text.length ? j + run : i + run; - out.push(text.slice(i, after)); - i = after; - continue; - } - // Legacy spans have already received their own inline/display policy. - // Do not reinterpret dollars inside them in this second pass. - if (text[i] === "\\" && !isEscaped(text, i)) { - const next = text[i + 1]; - if (next === "(" || next === "[") { - const closeAt = findClose(text, i + 2, next === "[" ? DISPLAY_CLOSE : INLINE_CLOSE); - if (closeAt !== -1) { - out.push(text.slice(i, closeAt + 2)); - i = closeAt + 2; - continue; - } - } - } - if (text[i] === "$" && !isEscaped(text, i)) { - const size = dollarRun(text, i); - const isDisplay = dollarIsDisplay(text, i, size); - const close = findDollarClose(text, i + size, size, isDisplay); - if (close) { - const debris = isDisplay ? DISPLAY_DEBRIS_RE : DEBRIS_RE; - out.push( - text.slice(i, i + size) + - text.slice(i + size, close.at).replace(debris, (c) => ESCAPE_DEBRIS[c]!) + - text.slice(close.at, close.after), - ); - i = close.after; - continue; - } - out.push(text.slice(i, i + size)); - i += size; - continue; - } - out.push(text[i]!); - i += 1; - } - return out.join(""); -} - -/** - * Over-escaped command heal (owner ruling 2026-10-15) — the doubled- - * backslash fix. - * - * Some models stochastically emit DOUBLED backslashes inside JSON tool-call - * arguments: the saved record holds `\\nabla` (two real backslashes) where - * `\nabla` was meant. KaTeX reads the `\\` as a row break and then renders - * the command name as plain letters — the learner sees "the word nabla, not - * the symbol". Field evidence (2026-10-15, real data): xiaomi/mimo-v2.6-pro - * doubled per-call, per-field (one probe's question doubled while its - * explanation and options in the SAME call stayed clean — authoring, not - * transport), and deepseek-v4.1-flash once; quadrupling appeared when the - * in-session agent "compensated" for its own doubled first attempt. - * - * THE RULE. Inside a balanced math span ONLY (legacy `\(…\)`/`\[…\]` and - * dollar `$…$`/`$$…$$`, code spans and fences excluded), an EVEN-length - * backslash run (2, 4, …) immediately followed by a command start — a letter - * (any command name; bare letters after a row break are never authored - * content) or a punctuation command (`,` `;` `:` `!` `|` — thin spaces, - * norm bars) — collapses to a single backslash. Even runs before anything - * else (space, newline, `&`, `[`, `$`, digits) are legitimate row breaks and - * stay; ODD runs (3, 5, …) are row break(s) plus a real command with its own - * backslash — e.g. matrix cells written `a \\\nabla` — and stay untouched, - * so matrix content cannot be corrupted. Outside spans nothing is touched: - * doubled backslashes in prose or code are not ours to judge. Unbalanced - * spans pass through, exactly like the debris passes. - */ -const COMMAND_START_CLASS = "[A-Za-z,;:!|]"; - -/** Collapse even backslash runs before a command start; keep odd runs. */ -function halveOverescapedCommandRuns(inner: string): string { - return inner.replace( - new RegExp("(\\\\+)(?=" + COMMAND_START_CLASS + ")", "g"), - (run: string) => (run.length % 2 === 0 ? "\\" : run), - ); + if (next === "%") return numericPercentEscape(inner, offset + run.length) ? "\\" : run; + if (word) return word.length > 1 && knownTexCommand(word) ? "\\" : run; + return next && commandSymbol.test(next) ? "\\" : run; + }); + // Structural evidence is primary (parse success cannot prove row + // preservation). As an additional guard, never turn parseable source into + // a parse failure through partial command recovery. + if (recovered !== inner && !canParseTex(recovered, display) && canParseTex(inner, display)) return inner; + return recovered; } export function healOverescapedCommands(text: string): string { - if (!text.includes("\\\\")) return text; - const out: string[] = []; - let i = 0; - while (i < text.length) { - if (atLineStart(text, i) && FENCE_OPEN_RE.test(text.slice(i))) { - i = copyFencedBlock(text, i, out); - continue; - } - if (text[i] === "`") { - const run = backtickRun(text, i); - let j = i + run; - while (j < text.length) { - const nextRun = backtickRun(text, j); - if (nextRun === run) break; - j += Math.max(nextRun, 1); - } - const after = j < text.length ? j + run : i + run; - out.push(text.slice(i, after)); - i = after; - continue; - } - if (text[i] === "\\" && !isEscaped(text, i)) { - const next = text[i + 1]; - if (next === "(" || next === "[") { - const isDisplay = next === "["; - const open = isDisplay ? DISPLAY_OPEN : INLINE_OPEN; - const close = isDisplay ? DISPLAY_CLOSE : INLINE_CLOSE; - const closeAt = findClose(text, i + open.length, close); - if (closeAt !== -1) { - const inner = text.slice(i + open.length, closeAt); - out.push(open + halveOverescapedCommandRuns(inner) + close); - i = closeAt + close.length; - continue; - } - } - } - if (text[i] === "$" && !isEscaped(text, i)) { - const size = dollarRun(text, i); - const isDisplay = dollarIsDisplay(text, i, size); - const close = findDollarClose(text, i + size, size, isDisplay); - if (close) { - out.push( - text.slice(i, i + size) + - halveOverescapedCommandRuns(text.slice(i + size, close.at)) + - text.slice(close.at, close.after), - ); - i = close.after; - continue; - } - out.push(text.slice(i, i + size)); - i += size; - continue; - } - out.push(text[i]!); - i += 1; - } - return out.join(""); + return text.includes("\\\\") ? mapMathContent(text, recoverCommandRuns) : text; +} + +/** Presentation-only, language-independent recovery. Shape parsing, saved + * identities, grading and model history must use the unprojected source. */ +export function repairMathForDisplay(text: string): string { + return healOverescapedCommands(restoreMathEscapes(recoverDocumentLayout(text))); } diff --git a/src/core/tool-parts.ts b/src/core/tool-parts.ts index cb5585d..db4305c 100644 --- a/src/core/tool-parts.ts +++ b/src/core/tool-parts.ts @@ -30,7 +30,6 @@ import { Value } from "typebox/value"; import type { TSchema } from "typebox"; import { quizDisplayInputSchema, askInputSchema, askOptionProblems, quizInputProblems, explainInputProblems, explainDisplayInputSchema, type AskToolInput, type QuizToolInput, type ExplainToolInput } from "./question-core.ts"; -import { repairMathForDisplay } from "./math-delims.ts"; import { healEncodedArguments } from "./argument-heal.ts"; import { probeDoneInputSchema, type ProbeDoneInput } from "./contracts/probe.ts"; @@ -67,28 +66,9 @@ function boundaryParse(schema: TSchema, input: unknown): T | undefined { * never have been posed (e.g. `correctAnswer: undefined`, which Convert * widens to "") renders as a quiet fallback, not a broken quiz. */ -/** LaTeX-escape restoration, deep over every string field (the JSON-debris - * fix, owner ruling 2026-09-26): records persist RAW call args, so - * single-backslash LaTeX eaten by JSON escape resolution (FF ate the 'f' of - * \forall, VT the 'v' of \varphi, TAB the 't' of \to) survives to the - * display path. This is the ONE seam every rendered question passes - * through — new turns and already-poisoned records alike heal here. - * healOverescapedCommands (owner ruling 2026-10-15) rides the same walk: - * the same models also stochastically DOUBLE command backslashes - * (`\\nabla` saved where `\nabla` was meant — KaTeX shows the row break - * plus "nabla" as letters), so even runs before a command start inside - * balanced math spans collapse to one backslash. */ -function restoreMathEscapesDeep(value: unknown): unknown { - if (typeof value === "string") return repairMathForDisplay(value); - if (Array.isArray(value)) return value.map(restoreMathEscapesDeep); - if (typeof value === "object" && value !== null) { - const out: Record = {}; - for (const [k, v] of Object.entries(value)) out[k] = restoreMathEscapesDeep(v); - return out; - } - return value; -} - +/** Shape parsing never repairs authored text. The dock builds its answer + * data from this source copy; recovery belongs exclusively to presentation + * (ToolContentResponse), never option identities, grading, or snapshots. */ export function parseQuizInput(input: unknown): QuizToolInput | undefined { // quizDisplayInputSchema (question-core, owner 2026-10-03): base + the // OPTIONAL `type` — new teach-phase records carry predict|check and the @@ -100,7 +80,7 @@ export function parseQuizInput(input: unknown): QuizToolInput | undefined { // wobble must heal HERE too or a posed quiz renders invisible (the // tool-parts law). healEncodedArguments is the same pure pass the // boundary's withArgumentHeal runs. - const healedInput = healEncodedArguments(restoreMathEscapesDeep(input), quizDisplayInputSchema); + const healedInput = healEncodedArguments(input, quizDisplayInputSchema); const cleaned = boundaryParse(quizDisplayInputSchema, healedInput); if (!cleaned) return undefined; if (quizInputProblems(cleaned).length > 0) return undefined; @@ -114,7 +94,7 @@ export function parseQuizInput(input: unknown): QuizToolInput | undefined { * keying, so they parse to undefined exactly like the zod seam did). */ export function parseAskInput(input: unknown): AskToolInput | undefined { - const cleaned = boundaryParse(askInputSchema, restoreMathEscapesDeep(input)); + const cleaned = boundaryParse(askInputSchema, input); if (!cleaned) return undefined; if (askOptionProblems(cleaned.options ?? []).length > 0) return undefined; return cleaned; @@ -127,7 +107,7 @@ export function parseAskInput(input: unknown): AskToolInput | undefined { * variants share the same fields; the teacher's nodeId is an unknown * key Value.Clean strips, same as the quiz seam. */ export function parseExplainInput(input: unknown): ExplainToolInput | undefined { - const cleaned = boundaryParse(explainDisplayInputSchema, restoreMathEscapesDeep(input)); + const cleaned = boundaryParse(explainDisplayInputSchema, input); if (!cleaned) return undefined; if (explainInputProblems(cleaned).length > 0) return undefined; return cleaned; diff --git a/tests/content-recovery.test.ts b/tests/content-recovery.test.ts new file mode 100644 index 0000000..d8f9196 --- /dev/null +++ b/tests/content-recovery.test.ts @@ -0,0 +1,116 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { repairMathForDisplay, restoreMathEscapes, transformMathDelimiters } from "../src/core/math-delims.ts"; + +const math = String.raw; +test("document paragraphs and display framing recover without interpreting n-commands as newlines", () => { + const source = math`First.\n\nSecond: $\nabla f\neq0$.\n\n$$\n\frac{1}{2}\n$$`; + assert.equal(repairMathForDisplay(source), "First.\n\nSecond: " + math`$\nabla f\neq0$.` + "\n\n$$\n" + math`\frac{1}{2}` + "\n$$"); +}); +test("literal document escapes fused to unknown math commands use document context", () => { + const source = math`Jacobian:\n\n$$\nJ=\begin{pmatrix}1&2\\3&4\end{pmatrix}\n$$`; + assert.equal(repairMathForDisplay(source), "Jacobian:\n\n$$\n" + math`J=\begin{pmatrix}1&2\\3&4\end{pmatrix}` + "\n$$"); +}); +test("encoded environment profiles decode row separators by their factor, not to one slash", () => { + for (const [source, expected] of [ + [math`$\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}$`, math`$\begin{pmatrix}a&b\\c&d\end{pmatrix}$`], + [math`$\\begin{pmatrix}1&2\\\\3&4\\end{pmatrix}$`, math`$\begin{pmatrix}1&2\\3&4\end{pmatrix}$`], + [math`$\\begin{pmatrix}a&b\\\\\\nabla f&d\\end{pmatrix}$`, math`$\begin{pmatrix}a&b\\\nabla f&d\end{pmatrix}$`], + [math`$\\substack{a\\\\beta}$`, math`$\substack{a\\beta}$`], + ] as const) { + assert.equal(repairMathForDisplay(source), expected); + assert.equal(repairMathForDisplay(expected), expected); + } +}); +test("native substack rows and literal document escapes in genuine comments remain literal", () => { + const source = math`$\substack{a\\beta}$`; + assert.equal(repairMathForDisplay(source), source); + const comment = "$x % " + math`literal \n\n` + "\ny$"; + assert.equal(repairMathForDisplay(comment), comment); +}); +test("inline lost prefixes recover only known full commands; display newlines remain whitespace", () => { + assert.equal(repairMathForDisplay("$\nabla f$"), math`$\nabla f$`); + assert.equal(repairMathForDisplay("$x+\ny$"), "$x+\ny$"); + assert.equal(repairMathForDisplay("$x+\nu$"), math`$x+\nu$`); // approved typical-inline-authoring policy + assert.equal(repairMathForDisplay("$$\nabla f\n$$"), "$$\nabla f\n$$"); // display source whitespace +}); +test("physical display newlines and genuine row breaks/comments survive", () => { + const source = "$$\n" + math`\begin{aligned}a&=b\\% row comment` + "\nc&=d\end{aligned}\n$$"; + assert.equal(repairMathForDisplay(source), source); +}); +test("proper nu survives every context, including fields with document escapes", () => { + for (const source of [math`$\nu$`, "$$\n" + math`\nu` + "\n$$", math`$\nabla f+\nu+\neq$`]) { + assert.equal(repairMathForDisplay(source), source); + } + assert.equal(repairMathForDisplay(math`First.\n\n$\nu$`), "First.\n\n" + math`$\nu$`); + assert.equal(repairMathForDisplay("$$\nu\n$$"), "$$\nu\n$$"); // LF + variable u in display +}); +test("tabs recover known commands including to, not arbitrary whitespace", () => { + assert.equal(repairMathForDisplay("$$\nx\t+\ty\n$$"), "$$\nx\t+\ty\n$$"); + assert.equal(repairMathForDisplay("$a\to b$"), math`$a\to b$`); + assert.equal(repairMathForDisplay("$$\na\to b\n$$"), "$$\n" + math`a\to b` + "\n$$"); + assert.equal(repairMathForDisplay("$$\r\nx+1\r\n$$"), "$$\r\nx+1\r\n$$"); +}); +test("Markdown containers protect literal code and do not swallow following content", () => { + for (const source of [ + "> ~~~text\n> " + math`$\\alpha$ \n\n` + "\n> ~~~", + "> > ~~~text\n> > " + math`$\\alpha$` + "\n> > ~~~", + "- ~~~text\n " + math`$\\alpha$` + "\n ~~~", + "> ~~~text\n> " + math`$\\alpha$` + "\n\n~~~text\n" + math`$\\beta$` + "\n~~~", + ]) { + assert.equal(repairMathForDisplay(source), source); + assert.equal(transformMathDelimiters(source), source); + assert.equal(repairMathForDisplay(source + "\n\n" + math`$\\gamma$`), source + "\n\n" + math`$\gamma$`); + } +}); +test("numeric percentages recover, including percentages inside text groups", () => { + for (const [source, expected] of [ + [math`$60\\%$`, math`$60\%$`], + [math`$50\\%+10\\%=60\\%$`, math`$50\%+10\%=60\%$`], + [math`$\text{ (50\\%)}$`, math`$\text{ (50\%)}$`], + ] as const) assert.equal(repairMathForDisplay(source), expected); +}); +test("numeric row breaks followed by genuine comments are not percentage escapes", () => { + const source = "$60\\\\% explanation of this row\nx$"; + assert.equal(repairMathForDisplay(source), source); + const ambiguous = math`$x\\%$`; + assert.equal(repairMathForDisplay(ambiguous), ambiguous); +}); +test("inline and fenced code retain all bytes, including legacy delimiters and actual control characters", () => { + const code = "```text\n\\(x\nabla\\)\n\\n\\n\n```\n" + "`" + math`\n\n $60\\%$ \(x\)` + "`"; + assert.equal(repairMathForDisplay(code), code); + assert.equal(restoreMathEscapes(code), code); +}); +test("math matching cannot consume a blank paragraph or a code span", () => { + const source = "Price $5.\n\nThen $x$ and `code` remains."; + assert.equal(repairMathForDisplay(source), source); + assert.equal(repairMathForDisplay("$not `code` math$"), "$not `code` math$"); +}); +test("Windows paths and correctly written n-commands are not document escapes", () => { + const source = math`Path C:\new\notes; symbols $\nu+\ni+\neq+\nabla f$.\n\nNext paragraph.`; + assert.equal(repairMathForDisplay(source), math`Path C:\new\notes; symbols $\nu+\ni+\neq+\nabla f$.` + "\n\nNext paragraph."); +}); +test("declared n-prefixed macros are commands, not document escapes", () => { + const source = math`$\newcommand{\nThing}{x}\nThing$.\n\nNext.`; + assert.equal(repairMathForDisplay(source), math`$\newcommand{\nThing}{x}\nThing$.` + "\n\nNext."); +}); +test("partial recovery cannot turn otherwise parseable TeX into a missing-argument error", () => { + const source = math`$x\\frac{a}$`; + assert.equal(repairMathForDisplay(source), source); +}); +test("legacy compatibility and recovery share code barriers, including tilde and indentation", () => { + for (const source of [" " + math`\(x\) \n\n`, "\\[\n~~~tex\n" + math`\n\n \(x\)` + "\n\\]\n~~~"]) { + assert.equal(repairMathForDisplay(source), source); + assert.equal(transformMathDelimiters(source), source); + } + const comment = "$60% " + math`\n\n` + "\nx$"; + assert.equal(repairMathForDisplay(comment), comment); +}); +test("recovery converges after one pass, including multiple debris characters", () => { + for (const source of ["$\forall x,\theta+\to$", math`First.\n\n$$\nJ=\\frac{1}{2}+60\\%\n$$`, + math`$\begin{pmatrix}a&b\\c&d\end{pmatrix}$`, "```text\n\\(\nabla\\)\n```", "$\nabla f$", + ]) { + const once = repairMathForDisplay(source); + assert.equal(repairMathForDisplay(once), once); + } +}); diff --git a/tests/fixtures/tool-rendering/document-percent-regressions.json b/tests/fixtures/tool-rendering/document-percent-regressions.json new file mode 100644 index 0000000..fd5ed9b --- /dev/null +++ b/tests/fixtures/tool-rendering/document-percent-regressions.json @@ -0,0 +1,90 @@ +[ + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo321f7_4202fae0", + "source": "messages.1.content.1.arguments.explanation", + "model": "google/gemini-3.7-flash", + "kind": "percent-at-display-end", + "raw": "To find the posterior probability $P(D \\\\mid +)$, we apply Bayes' theorem:\n$$\nP(D \\\\mid +) = \\\\frac{P(D \\\\cap +)}{P(+)} = \\\\frac{P(D)P(+ \\\\mid D)}{P(D)P(+ \\\\mid D) + P(\\\\neg D)P(+ \\\\mid \\\\neg D)}\n$$\n\nGiven:\n- $P(D) = 0.01$ (and thus $P(\\\\neg D) = 0.99$)\n- $P(+ \\\\mid D) = 0.90$\n- $P(+ \\\\mid \\\\neg D) = 0.05$\n\nEvaluating the numerator and denominator:\n$$\nP(D \\\\cap +) = 0.01 \\\\times 0.90 = 0.009\n$$\n$$\nP(\\\\neg D \\\\cap +) = 0.99 \\\\times 0.05 = 0.0495\n$$\n$$\nP(+) = 0.009 + 0.0495 = 0.0585\n$$\n\nTherefore:\n$$\nP(D \\\\mid +) = \\\\frac{0.009}{0.0585} = \\\\frac{9}{58.5} = \\\\frac{2}{13} \\\\approx 15.4\\\\%\n$$" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo321f7_4202fae0", + "source": "messages.5.content.1.arguments.explanation", + "model": "google/gemini-3.7-flash", + "kind": "percent-at-display-end", + "raw": "Because the two tests are conditionally independent given disease status:\n$$\nP(T_1 = +, T_2 = + \\\\mid D) = P(T_1 = + \\\\mid D) \\\\cdot P(T_2 = + \\\\mid D) = 0.90 \\\\times 0.90 = 0.81\n$$\n$$\nP(T_1 = +, T_2 = + \\\\mid \\\\neg D) = P(T_1 = + \\\\mid \\\\neg D) \\\\cdot P(T_2 = + \\\\mid \\\\neg D) = 0.10 \\\\times 0.10 = 0.01\n$$\n\nNow calculate the joint probabilities:\n$$\nP(D \\\\cap T_1 = + \\\\cap T_2 = +) = P(D) \\\\cdot P(T_1 = +, T_2 = + \\\\mid D) = 0.01 \\\\times 0.81 = 0.0081\n$$\n$$\nP(\\\\neg D \\\\cap T_1 = + \\\\cap T_2 = +) = P(\\\\neg D) \\\\cdot P(T_1 = +, T_2 = + \\\\mid \\\\neg D) = 0.99 \\\\times 0.01 = 0.0099\n$$\n\nTotal probability of two positive tests:\n$$\nP(T_1 = +, T_2 = +) = 0.0081 + 0.0099 = 0.0180\n$$\n\nApplying Bayes' theorem:\n$$\nP(D \\\\mid T_1 = +, T_2 = +) = \\\\frac{0.0081}{0.0180} = \\\\frac{81}{180} = \\\\frac{9}{20} = 45\\\\%\n$$" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo4op90_74572548", + "source": "messages.1.content.2.arguments.explanation", + "language": "pt", + "model": "deepseek/deepseek-v4.1-flash", + "kind": "percent-at-display-end", + "raw": "Em $10\\\\,000$ pessoas: $100$ estão doentes e $99$ dessas testam positivo; $9\\\\,900$ estão saudáveis e $5\\\\%$ delas, isto é $495$, testam positivo. Total de positivos: $99 + 495 = 594$. Entre os positivos, a fração de doentes é\\n$$\\n\\\\frac{99}{594} = \\\\frac{1}{6} \\\\approx 16{,}7\\\\%\\n$$\\nA base pequena de doentes faz os falsos positivos ($495$) dominarem os verdadeiros positivos ($99$)." + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo4op90_74572548", + "source": "messages.3.content.2.arguments.explanation", + "language": "pt", + "model": "deepseek/deepseek-v4.1-flash", + "kind": "percent-at-display-end", + "raw": "Continua valendo a mesma estrutura de Bayes, agora com números diferentes. Em $10\\\\,000$ pessoas: $200$ doentes, das quais $90\\\\%$ testam positivo $= 180$; $9\\\\,800$ saudáveis, das quais $5\\\\%$ testam positivo $= 490$ falsos positivos. Total de positivos: $180 + 490 = 670$.\\n$$\\nP(\\\\text{doente} \\\\mid +) = \\\\frac{180}{670} \\\\approx 26{,}9\\\\%\\n$$\\nNote que a especificidade $95\\\\%$ entra na conta pela sua taxa de falso positivo $1 - 0{,}95 = 0{,}05$, pesada por $P(\\\\text{saudável}) = 0{,}98$." + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo8kgtc_65767ffd", + "source": "messages.5.content.1.arguments.question", + "language": "es", + "model": "xiaomi/mimo-v2.6-flash", + "kind": "document-math-framing", + "raw": "Una enfermedad afecta al $1\\%$ de una población. La prueba tiene una sensibilidad del $99\\%$ (probabilidad de dar positivo si la enfermedad está presente) y una tasa de falsos positivos del $5\\%$ (probabilidad de dar positivo si no está presente). Una persona al azar da positivo. ¿Cuál es la probabilidad de que realmente tenga la enfermedad?\\n\\n$$\\nP(\\text{enf} \\mid +) \\;=\\; ?$$" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo91klg_5332fa79", + "source": "messages.3.content.1.arguments.explanation", + "language": "zh-Hans", + "model": "xiaomi/mimo-v2.6-flash", + "kind": "percent-at-display-end", + "raw": "条件概率把样本空间缩小到条件所指定的那一部分。已知\"检测阳性\",人群就从 $1000$ 人缩到 $140$ 人,其中患病的有 $45$ 人,所以\\n$$\\nP(\\\\text{患病}\\\\mid\\\\text{阳性})=\\\\frac{P(\\\\text{患病}\\\\cap\\\\text{阳性})}{P(\\\\text{阳性})}=\\\\frac{45/1000}{140/1000}=\\\\frac{45}{140}\\\\approx 32.1\\\\%.\\n$$\\n另外三个数各有各的含义:$\\\\frac{45}{50}=90\\\\%$ 是 $P(\\\\text{阳性}\\\\mid\\\\text{患病})$(敏感度,条件方向反了);$\\\\frac{45}{1000}=4.5\\\\%$ 是联合概率 $P(\\\\text{患病}\\\\cap\\\\text{阳性})$(没做条件化);$\\\\frac{140}{1000}=14\\\\%$ 是 $P(\\\\text{阳性})$(不分是否患病的总阳性率)。" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo91klg_5332fa79", + "source": "messages.7.content.1.arguments.explanation", + "language": "zh-Hans", + "model": "xiaomi/mimo-v2.6-flash", + "kind": "percent-at-display-end", + "raw": "用 $10000$ 人(或直接用全概率公式)算分母:\\n$$\\n\\\\begin{aligned}\\n\\\\text{患病且阳性}&=10000\\\\times 0.01\\\\times 0.99=99,\\\\\\\\\\n\\\\text{未患病但阳性}&=10000\\\\times 0.99\\\\times 0.05=495.\\n\\\\end{aligned}\\n$$\\n阳性总数 $=99+495=594$,所以\\n$$\\nP(D\\\\mid +)=\\\\frac{99}{594}\\\\approx 16.7\\\\%.\\n$$\\n各错误选项对应的具体失误:$99\\\\%$ 是把敏感度 $P(+\\\\mid D)$ 当成了 $P(D\\\\mid +)$(条件方向颠倒);$95.2\\\\%=\\\\frac{0.99}{0.99+0.05}$ 是把阳性率和假阳性率直接当人数用,漏掉了基础发生率的加权($99\\\\%$ 的人未患病,所以少量假阳性也架不住人多);$1\\\\%$ 是看到了阳性结果却不更新概率。" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo91klg_5332fa79", + "source": "messages.9.content.1.arguments.explanation", + "language": "zh-Hans", + "model": "xiaomi/mimo-v2.6-flash", + "kind": "percent-at-display-end", + "raw": "两次阳性后的后验,用同样的公式但似然要平方:\\n$$\\n\\\\begin{aligned}\\nP(++\\\\mid D)&=0.99^2=0.9801,\\\\\\\\\\nP(++\\\\mid D')&=0.05^2=0.0025.\\n\\\\end{aligned}\\n$$\\n$$\\nP(D\\\\mid ++)\\\\frac{0.99^2\\\\times 0.01}{0.99^2\\\\times 0.01+0.05^2\\\\times 0.99}=\\\\frac{0.009801}{0.009801+0.002475}\\\\approx 79.8\\\\%.\\n$$\\n关键在于**两条分支都要平方**:患病分支的似然变成 $0.99^2$,未患病分支的假阳性率变成 $0.05^2$($5\\%$ 是很小的数,平方后变得更小,所以假阳性这一侧收缩得比患病侧更狠,后验从 $16.7\\\\%$ 跳到约 $80\\\\%$)。其他选项的失误:$98\\\\%$ 是把 $P(++\\\\mid D)$ 当成了 $P(D\\\\mid ++)$;$16.5\\\\%$ 只对患病分支用了两次独立,未患病分支仍用单次假阳性率;$33\\\\%$ 是把单次后验直接乘 $2$——两次阳性提供的证据不是相加的,而要在似然层面相乘。" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo9c830_3866e4a6", + "source": "messages.7.content.1.arguments.explanation", + "language": "zh-Hans", + "model": "anthropic/claude-haiku-5.5", + "kind": "percent-at-display-end", + "raw": "阳性总人数:真阳性 $100\\\\times0.9=90$,假阳性 $9900\\\\times0.05=495$,合计 $585$。\n\n所求的是阳性者中患病者的占比,分母是全部阳性者:\n$$\n\\\\frac{90}{585}\\\\approx15.4\\\\%\n$$\n\n$90\\\\%$ 是患病者中阳性的比例,分母错成了患病者;$84.6\\\\%$ 是阳性者中健康者的占比,回答的是另一个问题;$5\\\\%$ 是健康者中的假阳性率,分母是健康人群。" + }, + { + "group": "multilingual-20261025-01", + "chatId": "chat_mvo9nu8w_dcc7add9", + "source": "messages.3.content.1.arguments.explanation", + "language": "id", + "model": "xiaomi/mimo-v2.6-flash", + "kind": "percent-at-display-end", + "raw": "Pakai aturan Bayes dengan menghitung dulu kejadian gabungan dalam satu kelompok besar, misalnya $10.000$ orang:\\n\\n- Sakit dan positif: $10.000 \\\\times 0{,}01 \\\\times 0{,}99 = 99$ orang.\\n- Sehat dan positif: $10.000 \\\\times 0{,}99 \\\\times 0{,}05 = 495$ orang.\\n\\nSemua yang hasilnya positif: $99 + 495 = 594$ orang. Maka\\n$$\\nP(\\\\text{sakit}\\\\mid +) = \\\\frac{99}{594} = \\\\frac{1}{6} \\\\approx 16{,}7\\\\%.\\n$$\\n\\nYang menipu di sini adalah base rate: kelompok sehat jauh lebih besar, sehingga meski hanya 5% dari mereka yang salah positif, jumlahnya tetap 5 kali lipat jumlah positif yang benar. Nilai $95{,}2\\\\%$ muncul kalau kelompok sakit dan sehat dianggap sama besar ($\\\\frac{99}{99+5}$); nilai $99\\\\%$ muncul kalau arah kondisinya dibalik, membaca $P(+\\\\mid \\\\text{sakit})$ sebagai $P(\\\\text{sakit}\\\\mid +)$." + } +] diff --git a/tests/fixtures/tool-rendering/matrix-regressions.json b/tests/fixtures/tool-rendering/matrix-regressions.json new file mode 100644 index 0000000..48d409d --- /dev/null +++ b/tests/fixtures/tool-rendering/matrix-regressions.json @@ -0,0 +1,135 @@ +{ + "notice": "Unmodified model-authored fields from the frozen multilingual corpus. oldDisplay records the diagnosed application damage, not an expected output.", + "cases": [ + { + "run": "pilot-20261025-01", + "model": "openai/gpt-6-luna", + "language": "en", + "replicate": 1, + "chatId": "chat_mvo1rzjs_056eac45", + "source": "messages.5.content.1.arguments.explanation", + "raw": "For a $2\\times2$ matrix, $\\det\\begin{bmatrix}a&b\\\\c&d\\end{bmatrix}=ad-bc$. Here $3\\cdot2-6\\cdot1=0$. A square matrix is invertible exactly when its determinant is nonzero, so $M$ is singular. Its columns are also proportional, which matches the determinant-zero result.", + "oldDisplay": "For a $2\\times2$ matrix, $\\det\\begin{bmatrix}a&b\\c&d\\end{bmatrix}=ad-bc$. Here $3\\cdot2-6\\cdot1=0$. A square matrix is invertible exactly when its determinant is nonzero, so $M$ is singular. Its columns are also proportional, which matches the determinant-zero result." + }, + { + "run": "pilot-20261025-01", + "model": "openai/gpt-6-luna", + "language": "en", + "replicate": 1, + "chatId": "chat_mvo1sdnj_8f2d8c5e", + "source": "messages.9.content.1.arguments.explanation", + "raw": "The Jacobian has one row per output and one column per input: $J_F=\\begin{pmatrix}2x&1\\\\y&x\\end{pmatrix}$. At $(1,2)$ this is $\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}$.", + "oldDisplay": "The Jacobian has one row per output and one column per input: $J_F=\\begin{pmatrix}2x&1\\y&x\\end{pmatrix}$. At $(1,2)$ this is $\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}$." + }, + { + "run": "pilot-20261025-01", + "model": "anthropic/claude-haiku-5.5", + "language": "en", + "replicate": 1, + "chatId": "chat_mvo30cfe_57114723", + "source": "messages.3.content.1.arguments.explanation", + "raw": "$A-\\lambda I=\\begin{pmatrix}2-\\lambda&1\\\\1&2-\\lambda\\end{pmatrix}$, and for a $2\\times2$ matrix $\\det\\begin{pmatrix}p&q\\\\r&s\\end{pmatrix}=ps-qr$. So $$\\det(A-\\lambda I)=(2-\\lambda)^2-1\\cdot1=\\lambda^2-4\\lambda+3.$$ Its roots $\\lambda=1,3$ are the eigenvalues. Option $b$ drops the minus sign in $ps-qr$; $c$ subtracts the constant incorrectly; $d$ flips the sign of the linear term.", + "oldDisplay": "$A-\\lambda I=\\begin{pmatrix}2-\\lambda&1\\\\1&2-\\lambda\\end{pmatrix}$, and for a $2\\times2$ matrix $\\det\\begin{pmatrix}p&q\\r&s\\end{pmatrix}=ps-qr$. So $$\\det(A-\\lambda I)=(2-\\lambda)^2-1\\cdot1=\\lambda^2-4\\lambda+3.$$ Its roots $\\lambda=1,3$ are the eigenvalues. Option $b$ drops the minus sign in $ps-qr$; $c$ subtracts the constant incorrectly; $d$ flips the sign of the linear term." + }, + { + "run": "multilingual-20261025-01-zh-Hans-r1", + "model": "openai/gpt-6-luna", + "language": "zh-Hans", + "replicate": 1, + "chatId": "chat_mvo60bsn_1cbbe884", + "source": "messages.3.content.1.arguments.question", + "raw": "对 $2\\times2$ 矩阵 $A=\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}$,以下哪组关于行列式的说法全都正确?", + "oldDisplay": "对 $2\\times2$ 矩阵 $A=\\begin{pmatrix}a&b\\c&d\\end{pmatrix}$,以下哪组关于行列式的说法全都正确?" + }, + { + "run": "multilingual-20261025-01-zh-Hans-r1", + "model": "xiaomi/mimo-v2.6-flash", + "language": "zh-Hans", + "replicate": 1, + "chatId": "chat_mvo66u6w_a233fc16", + "source": "messages.9.content.2.arguments.explanation", + "raw": "雅可比矩阵按“第 $i$ 行 = 第 $i$ 个分量对所有自变量的偏导”排布:\n$$\nJ_{\\mathbf F}=\n\\begin{pmatrix}\n\\dfrac{\\partial (xy)}{\\partial x} & \\dfrac{\\partial (xy)}{\\partial y}\\\\[6pt]\n\\dfrac{\\partial (x+y^2)}{\\partial x} & \\dfrac{\\partial (x+y^2)}{\\partial y}\n\\end{pmatrix}\n=\n\\begin{pmatrix}\ny & x\\\\\n1 & 2y\n\\end{pmatrix}.\n$$\n注意 $\\partial(xy)/\\partial x=y$($y$ 当常数),$\\partial(xy)/\\partial y=x$。转置形式 $\\begin{pmatrix}y&1\\\\x&2y\\end{pmatrix}$ 把偏导排成了列;$\\begin{pmatrix}y&x\\\\2y&1\\end{pmatrix}$ 把第二行的两个分量次序调换了;$\\begin{pmatrix}x&y\\\\1&2y\\end{pmatrix}$ 把第一行的 $x,y$ 位置写反了。", + "oldDisplay": "雅可比矩阵按“第 $i$ 行 = 第 $i$ 个分量对所有自变量的偏导”排布:\n$$\nJ_{\\mathbf F}=\n\\begin{pmatrix}\n\\dfrac{\\partial (xy)}{\\partial x} & \\dfrac{\\partial (xy)}{\\partial y}\\\\[6pt]\n\\dfrac{\\partial (x+y^2)}{\\partial x} & \\dfrac{\\partial (x+y^2)}{\\partial y}\n\\end{pmatrix}\n=\n\\begin{pmatrix}\ny & x\\\\\n1 & 2y\n\\end{pmatrix}.\n$$\n注意 $\\partial(xy)/\\partial x=y$($y$ 当常数),$\\partial(xy)/\\partial y=x$。转置形式 $\\begin{pmatrix}y&1\\x&2y\\end{pmatrix}$ 把偏导排成了列;$\\begin{pmatrix}y&x\\\\2y&1\\end{pmatrix}$ 把第二行的两个分量次序调换了;$\\begin{pmatrix}x&y\\\\1&2y\\end{pmatrix}$ 把第一行的 $x,y$ 位置写反了。" + }, + { + "run": "multilingual-20261025-01-id-r1", + "model": "openai/gpt-6-luna", + "language": "id", + "replicate": 1, + "chatId": "chat_mvo6q2lq_6af14437", + "source": "messages.7.content.1.arguments.explanation", + "raw": "Pada $(x,y)=(1,0)$, $H=(1,1)$. Jacobian $G$ di $(1,1)$ adalah $\\begin{pmatrix}2u&1\\\\v&u\\end{pmatrix}=\\begin{pmatrix}2&1\\\\1&1\\end{pmatrix}$, sedangkan $J_H=\\begin{pmatrix}1&1\\\\1&-1\\end{pmatrix}$. Aturan rantai dengan konvensi ini memberi $J_{G\\circ H}=J_G(H(x,y))J_H(x,y)$, yakni $\\begin{pmatrix}3&1\\\\2&0\\end{pmatrix}$.", + "oldDisplay": "Pada $(x,y)=(1,0)$, $H=(1,1)$. Jacobian $G$ di $(1,1)$ adalah $\\begin{pmatrix}2u&1\\v&u\\end{pmatrix}=\\begin{pmatrix}2&1\\\\1&1\\end{pmatrix}$, sedangkan $J_H=\\begin{pmatrix}1&1\\\\1&-1\\end{pmatrix}$. Aturan rantai dengan konvensi ini memberi $J_{G\\circ H}=J_G(H(x,y))J_H(x,y)$, yakni $\\begin{pmatrix}3&1\\\\2&0\\end{pmatrix}$." + }, + { + "run": "multilingual-20261025-01-id-r1", + "model": "z-ai/glm-5.3-flash", + "language": "id", + "replicate": 1, + "chatId": "chat_mvo77f3x_135f8d66", + "source": "messages.7.content.2.arguments.explanation", + "raw": "Vektor eigen memenuhi $(A-\\lambda I)v=0$. Untuk $\\lambda=2$:\n$$(A-2I)v=\\begin{pmatrix}1&1\\\\0&0\\end{pmatrix}\\begin{pmatrix}v_1\\\\v_2\\end{pmatrix}=0\\;\\Rightarrow\\;v_1+v_2=0\\;\\Rightarrow\\;v=\\begin{pmatrix}1\\\\-1\\end{pmatrix}$$\n(bisa dikali skalar berapa pun). Cek: $Av=\\begin{pmatrix}3-1\\\\-2\\end{pmatrix}=\\begin{pmatrix}2\\\\-2\\end{pmatrix}=2v$. Perhatikan $v=(1,0)$ adalah vektor eigen untuk $\\lambda=3$, bukan $\\lambda=2$.", + "oldDisplay": "Vektor eigen memenuhi $(A-\\lambda I)v=0$. Untuk $\\lambda=2$:\n$$(A-2I)v=\\begin{pmatrix}1&1\\\\0&0\\end{pmatrix}\\begin{pmatrix}v_1\\v_2\\end{pmatrix}=0\\;\\Rightarrow\\;v_1+v_2=0\\;\\Rightarrow\\;v=\\begin{pmatrix}1\\\\-1\\end{pmatrix}$$\n(bisa dikali skalar berapa pun). Cek: $Av=\\begin{pmatrix}3-1\\\\-2\\end{pmatrix}=\\begin{pmatrix}2\\\\-2\\end{pmatrix}=2v$. Perhatikan $v=(1,0)$ adalah vektor eigen untuk $\\lambda=3$, bukan $\\lambda=2$." + }, + { + "run": "multilingual-20261025-01-en-r2", + "model": "openai/gpt-6-luna", + "language": "en", + "replicate": 2, + "chatId": "chat_mvo7ei0l_9263330e", + "source": "messages.3.content.1.arguments.question", + "raw": "For a $2\\times2$ matrix $A=\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}$, which statement correctly links its determinant to area scaling and invertibility?", + "oldDisplay": "For a $2\\times2$ matrix $A=\\begin{pmatrix}a&b\\c&d\\end{pmatrix}$, which statement correctly links its determinant to area scaling and invertibility?" + }, + { + "run": "multilingual-20261025-01-en-r2", + "model": "openai/gpt-6-luna", + "language": "en", + "replicate": 2, + "chatId": "chat_mvo7exl3_e1fe93cd", + "source": "messages.1.content.1.arguments.explanation", + "raw": "First evaluate the Jacobian of $F$ at $(1,2)$:\n$$\nJ_F(x,y)=\\begin{pmatrix}2x&1\\\\y&x\\end{pmatrix},\\qquad J_F(1,2)=\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}.\n$$\nThe velocity is $\\gamma'(1)=(1,2)$. The chain rule gives\n$$\n\\frac{d}{dt}F(\\gamma(t))\\bigg|_{t=1}=J_F(1,2)\\begin{pmatrix}1\\\\2\\end{pmatrix}=\\begin{pmatrix}4\\\\4\\end{pmatrix}.\n$$\nWait, recomputing the component derivatives directly: $F(\\gamma(t))=(t^2+(1+t^2),\\ t(1+t^2))=(1+2t^2,t+t^3)$, whose derivative at $1$ is $(4,4)$. The listed options do not include this value, so this question is invalid.", + "oldDisplay": "First evaluate the Jacobian of $F$ at $(1,2)$:\n$$\nJ_F(x,y)=\\begin{pmatrix}2x&1\\y&x\\end{pmatrix},\\qquad J_F(1,2)=\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}.\n$$\nThe velocity is $\\gamma'(1)=(1,2)$. The chain rule gives\n$$\n\\frac{d}{dt}F(\\gamma(t))\\bigg|_{t=1}=J_F(1,2)\\begin{pmatrix}1\\\\2\\end{pmatrix}=\\begin{pmatrix}4\\\\4\\end{pmatrix}.\n$$\nWait, recomputing the component derivatives directly: $F(\\gamma(t))=(t^2+(1+t^2),\\ t(1+t^2))=(1+2t^2,t+t^3)$, whose derivative at $1$ is $(4,4)$. The listed options do not include this value, so this question is invalid." + }, + { + "run": "multilingual-20261025-01-en-r2", + "model": "openai/gpt-6-luna", + "language": "en", + "replicate": 2, + "chatId": "chat_mvo7exl3_e1fe93cd", + "source": "messages.3.content.1.arguments.explanation", + "raw": "For $F=(F_1,F_2)$, each row contains the partial derivatives of one output component: $J_F=\\begin{pmatrix}\\partial F_1/\\partial x&\\partial F_1/\\partial y\\\\\\partial F_2/\\partial x&\\partial F_2/\\partial y\\end{pmatrix}=\\begin{pmatrix}2x&1\\\\y&x\\end{pmatrix}$. Substituting $(x,y)=(1,2)$ gives $\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}$.", + "oldDisplay": "For $F=(F_1,F_2)$, each row contains the partial derivatives of one output component: $J_F=\\begin{pmatrix}\\partial F_1/\\partial x&\\partial F_1/\\partial y\\\\\\partial F_2/\\partial x&\\partial F_2/\\partial y\\end{pmatrix}=\\begin{pmatrix}2x&1\\y&x\\end{pmatrix}$. Substituting $(x,y)=(1,2)$ gives $\\begin{pmatrix}2&1\\\\2&1\\end{pmatrix}$." + }, + { + "run": "multilingual-20261025-01-pt-r2", + "model": "openai/gpt-6-luna", + "language": "pt", + "replicate": 2, + "chatId": "chat_mvo7wn6c_d63ef3f1", + "source": "messages.7.content.1.arguments.explanation", + "raw": "Para uma matriz $2\\times2$, $\\det\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}=ad-bc$. Logo, $\\det(C)=2\\cdot3-1\\cdot5=1$. O determinante é o fator assinado de escala de área: valor absoluto $1$ preserva área, e sinal positivo indica que a orientação não é invertida. Como o determinante é diferente de zero, $C$ é invertível.", + "oldDisplay": "Para uma matriz $2\\times2$, $\\det\\begin{pmatrix}a&b\\c&d\\end{pmatrix}=ad-bc$. Logo, $\\det(C)=2\\cdot3-1\\cdot5=1$. O determinante é o fator assinado de escala de área: valor absoluto $1$ preserva área, e sinal positivo indica que a orientação não é invertida. Como o determinante é diferente de zero, $C$ é invertível." + }, + { + "run": "multilingual-20261025-01-es-r2", + "model": "openai/gpt-6-luna", + "language": "es", + "replicate": 2, + "chatId": "chat_mvo8j89a_f6fc1cc0", + "source": "messages.3.content.1.arguments.explanation", + "raw": "La regla de la cadena es $D(F\\circ g)=DF(g(s,t))Dg(s,t)$. Aquí $g(1,2)=(3,2)$, $DF(u,v)=\\begin{pmatrix}2u&1\\\\v&u\\end{pmatrix}$ y $Dg(s,t)=\\begin{pmatrix}1&1\\\\t&s\\end{pmatrix}$. Así, $\\begin{pmatrix}6&1\\\\2&3\\end{pmatrix}\\begin{pmatrix}1&1\\\\2&1\\end{pmatrix}=\\begin{pmatrix}8&7\\\\8&5\\end{pmatrix}$.", + "oldDisplay": "La regla de la cadena es $D(F\\circ g)=DF(g(s,t))Dg(s,t)$. Aquí $g(1,2)=(3,2)$, $DF(u,v)=\\begin{pmatrix}2u&1\\v&u\\end{pmatrix}$ y $Dg(s,t)=\\begin{pmatrix}1&1\\t&s\\end{pmatrix}$. Así, $\\begin{pmatrix}6&1\\\\2&3\\end{pmatrix}\\begin{pmatrix}1&1\\\\2&1\\end{pmatrix}=\\begin{pmatrix}8&7\\\\8&5\\end{pmatrix}$." + }, + { + "run": "multilingual-20261025-01-id-r2", + "model": "openai/gpt-6-luna", + "language": "id", + "replicate": 2, + "chatId": "chat_mvo9mesu_dbb56f1f", + "source": "messages.9.content.1.arguments.explanation", + "raw": "Untuk $\\begin{pmatrix}a&b\\\\c&d\\end{pmatrix}$, determinannya $ad-bc$. Jadi $\\det(C)=2\\cdot3-5\\cdot1=1$.", + "oldDisplay": "Untuk $\\begin{pmatrix}a&b\\c&d\\end{pmatrix}$, determinannya $ad-bc$. Jadi $\\det(C)=2\\cdot3-5\\cdot1=1$." + } + ] +} diff --git a/tests/fixtures/tool-rendering/mixed-escaping-regressions.json b/tests/fixtures/tool-rendering/mixed-escaping-regressions.json new file mode 100644 index 0000000..5489f6a --- /dev/null +++ b/tests/fixtures/tool-rendering/mixed-escaping-regressions.json @@ -0,0 +1,19 @@ +{ + "provenance": "multilingual-20261025-01; original collection findings, not the failed r1 rendering", + "cases": [ + { + "chatId": "chat_mvo6kxzl_540b570e", + "source": "messages.1.content.2.arguments.explanation", + "model": "z-ai/glm-5.3-flash", + "raw": "乘法规则是\"左行的元素依次乘右列的元素再相加\":\\n$$\\nAB=\\begin{pmatrix}1\\\\cdot3+2\\\\cdot0 & 1\\\\cdot1+2\\\\cdot2\\\\\\\\ 0\\\\cdot3+1\\\\cdot0 & 0\\\\cdot1+1\\\\cdot2\\\\end{pmatrix}=\\begin{pmatrix}3 & 5\\\\\\\\ 0 & 2\\\\end{pmatrix}\\n$$\\n选项 b 其实是 $BA$($\\\\begin{pmatrix}3 & 7\\\\\\\\ 0 & 2\\\\end{pmatrix}$)——顺序交换后结果不同,这正是矩阵乘法不交换的例子。选项 c 是对应位置直接相加,那是加法不是乘法。", + "oldDisplay": "乘法规则是\"左行的元素依次乘右列的元素再相加\":\\n$$\\nAB=\\begin{pmatrix}1\\cdot3+2\\cdot0 & 1\\cdot1+2\\cdot2\\\\\\\\ 0\\cdot3+1\\cdot0 & 0\\cdot1+1\\cdot2\\end{pmatrix}=\\begin{pmatrix}3 & 5\\\\\\\\ 0 & 2\\end{pmatrix}\\n$$\\n选项 b 其实是 $BA$($\\begin{pmatrix}3 & 7\\\\\\\\ 0 & 2\\end{pmatrix}$)——顺序交换后结果不同,这正是矩阵乘法不交换的例子。选项 c 是对应位置直接相加,那是加法不是乘法。" + }, + { + "chatId": "chat_mvo9oquq_74bfd7f6", + "source": "messages.3.content.1.arguments.explanation", + "model": "xiaomi/mimo-v2.6-flash", + "raw": "Perkalian matriks $AB$ = baris-kolom: elemen $(i,j)$ hasil adalah titik-antara baris $i$ dari $A$ dengan kolom $j$ dari $B$.\\n\\n- $(1,1): (1,2)\\cdot(2,0) = 1\\cdot2 + 2\\cdot1 = 4$\\n- $(1,2): (1,2)\\cdot(0,2) = 1\\cdot0 + 2\\cdot2 = 4$\\n- $(2,1): (3,4)\\cdot(2,0) = 3\\cdot2 + 4\\cdot1 = 10$\\n- $(2,2): (3,4)\\cdot(0,2) = 3\\cdot0 + 4\\cdot2 = 8$\\n\\n$$AB = \\\\begin{pmatrix} 4 & 4 \\\\\\\\ 10 & 8 \\\\end{pmatrix}$$\\n\\nPilihan lain adalah tiga kesalahan berbeda: $\\begin{pmatrix} 2 & 4 \\\\ 7 & 10 \\\\end{pmatrix}$ adalah $BA$ (urutan dibalik), $\\begin{pmatrix} 4 & 10 \\\\ 4 & 8 \\\\end{pmatrix}$ adalah transpos hasil (peletakan elemen tertukar), dan $\\begin{pmatrix} 2 & 0 \\\\ 3 & 8 \\\\end{pmatrix}$ adalah perkalian elemen-per-elemen, bukan baris-kolom. Perhatikan juga bahwa $AB \\neq BA$ — perkalian matriks tidak komutatif.", + "oldDisplay": "Perkalian matriks $AB$ = baris-kolom: elemen $(i,j)$ hasil adalah titik-antara baris $i$ dari $A$ dengan kolom $j$ dari $B$.\\n\\n- $(1,1): (1,2)\\cdot(2,0) = 1\\cdot2 + 2\\cdot1 = 4$\\n- $(1,2): (1,2)\\cdot(0,2) = 1\\cdot0 + 2\\cdot2 = 4$\\n- $(2,1): (3,4)\\cdot(2,0) = 3\\cdot2 + 4\\cdot1 = 10$\\n- $(2,2): (3,4)\\cdot(0,2) = 3\\cdot0 + 4\\cdot2 = 8$\\n\\n$$AB = \\begin{pmatrix} 4 & 4 \\\\\\\\ 10 & 8 \\end{pmatrix}$$\\n\\nPilihan lain adalah tiga kesalahan berbeda: $\\begin{pmatrix} 2 & 4 \\\\ 7 & 10 \\end{pmatrix}$ adalah $BA$ (urutan dibalik), $\\begin{pmatrix} 4 & 10 \\\\ 4 & 8 \\end{pmatrix}$ adalah transpos hasil (peletakan elemen tertukar), dan $\\begin{pmatrix} 2 & 0 \\\\ 3 & 8 \\end{pmatrix}$ adalah perkalian elemen-per-elemen, bukan baris-kolom. Perhatikan juga bahwa $AB \\neq BA$ — perkalian matriks tidak komutatif." + } + ] +} diff --git a/tests/repair-math-display.test.tsx b/tests/repair-math-display.test.tsx index 117f123..13673d0 100644 --- a/tests/repair-math-display.test.tsx +++ b/tests/repair-math-display.test.tsx @@ -12,7 +12,7 @@ import { PracticeDock } from "../components/practice-dock.tsx"; import { QuestionDock } from "../components/question-dock.tsx"; import { LatexGlyph } from "../components/latex-glyph.tsx"; import { ProblemCard, ConcludeCard } from "../components/practice-cards.tsx"; -import { MessageResponse } from "../components/ai-elements/message.tsx"; +import { MessageResponse, ToolContentResponse } from "../components/ai-elements/message.tsx"; import { repairMathForDisplay, healOverescapedCommands, restoreMathEscapes, parseQuizInput, parseAskInput, parseExplainInput, activeProblemOf, projectView, @@ -206,17 +206,17 @@ describe("practice display surfaces", () => { assert.ok(!plain.glyphs.includes("∇")); // Same unchanged assistant/user MessageResponse path. }); - it("teaching quiz/ask/explain still repair the same bytes without mutating their inputs", () => { + it("teaching shape parsing retains source bytes; presentation alone recovers them", () => { const quiz = { question: doubled, options: [{ label: "yes" }, { label: "no" }], correctAnswer: "yes", explanation: debris }; const ask = { question: doubled }; const explain = { question: doubled, correctAnswer: debris }; const before = structuredClone({ quiz, ask, explain }); - assert.equal(parseQuizInput(quiz)?.question, clean); - assert.equal(parseQuizInput(quiz)?.explanation, `$${repairedDebris}$`); - assert.equal(parseAskInput(ask)?.question, clean); - assert.equal(parseExplainInput(explain)?.question, clean); - assert.equal(parseExplainInput(explain)?.correctAnswer, `$${repairedDebris}$`); - assertMath(renderToStaticMarkup({parseAskInput(ask)!.question})); + assert.equal(parseQuizInput(quiz)?.question, doubled); + assert.equal(parseQuizInput(quiz)?.explanation, debris); + assert.equal(parseAskInput(ask)?.question, doubled); + assert.equal(parseExplainInput(explain)?.question, doubled); + assert.equal(parseExplainInput(explain)?.correctAnswer, debris); + assertMath(renderToStaticMarkup({parseAskInput(ask)!.question})); assert.deepEqual({ quiz, ask, explain }, before); }); diff --git a/tests/tool-content-interaction.test.tsx b/tests/tool-content-interaction.test.tsx new file mode 100644 index 0000000..b23f8c6 --- /dev/null +++ b/tests/tool-content-interaction.test.tsx @@ -0,0 +1,78 @@ +import React, { act } from "react"; +(globalThis as { React?: typeof React }).React = React; +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { createRoot } from "react-dom/client"; +import { parseHTML } from "linkedom"; +import { QuestionDock } from "../components/question-dock.tsx"; +import { AnsweredCard } from "../components/answered-card.tsx"; +import { newAnswerResult, type QuestionAnswerData } from "../src/core/index.ts"; + +const dom = parseHTML(""); +Object.assign(globalThis, { window: dom.window, document: dom.document, Element: dom.window.Element, + HTMLElement: dom.window.HTMLElement, IS_REACT_ACT_ENVIRONMENT: true }); + +test("actual select → submit → saved result → historical Why uses source data and presentation-only recovery", async () => { + const question = String.raw`Find $\\nabla f$ for $\begin{pmatrix}a&b\\c&d\end{pmatrix}$.`; + const label = String.raw`$\\nabla f$`; + const explanation = String.raw`First paragraph.\n\nThe matrix is $\begin{pmatrix}p&q\\r&s\end{pmatrix}$ and $\\nabla f$ is its gradient.`; + const input = { question, options: [{ label }, { label: "no" }], correctAnswer: label, explanation }; + const before = structuredClone(input); + let answer: QuestionAnswerData | undefined; + const host = document.createElement("div"); document.body.append(host); + const root = createRoot(host); + try { + await act(async () => root.render( {}} onSubmit={a => { answer = a; }} />)); + assert.equal(host.querySelectorAll("mtr").length, 2, "the live matrix retains two rows"); + const choice = [...host.querySelectorAll('[role="radio"]')].find(button => button.querySelector(".katex"))!; + assert.ok(choice); + await act(async () => choice.click()); + assert.equal(choice.getAttribute("aria-checked"), "true"); + const submit = [...host.querySelectorAll("button")].find(button => button.textContent?.includes("Submit answer"))!; + assert.equal(submit.disabled, false); + // linkedom does not implement the browser's default button→form + // action; dispatch its native submit event through React instead. + await act(async () => submit.closest("form")!.dispatchEvent(new window.Event("submit", { bubbles: true, cancelable: true }))); + assert.ok(answer); + assert.equal(answer.questionText, question); + assert.equal(answer.explanation, explanation); + assert.equal(answer.answers[0]!.value, label, "display recovery must not alter the submitted identity"); + assert.equal(answer.answers[0]!.label, label); + assert.equal(answer.correct, true); + const persisted = JSON.parse(JSON.stringify(newAnswerResult("interactive_source", "probe", answer))); + assert.deepEqual(persisted.details, JSON.parse(JSON.stringify(answer))); + assert.equal(persisted.details.explanation, explanation); + const saved = structuredClone(persisted.details); + await act(async () => root.render()); + const why = [...host.querySelectorAll("button")].find(button => button.textContent === "Why")!; + await act(async () => why.click()); + assert.equal(host.querySelectorAll("mtr").length, 4, "question and revealed explanation retain their rows"); + const paragraphs = host.querySelectorAll('[data-testid="tucked-explanation"] p'); + assert.equal(paragraphs.length, 2, "recovered paragraphs remain distinct in Why"); + assert.ok(paragraphs[0]!.parentElement!.classList.contains("[&>p:first-child]:inline")); + assert.ok(!paragraphs[0]!.parentElement!.classList.contains("[&_p]:inline")); + const annotations = [...host.querySelectorAll('annotation[encoding="application/x-tex"]')].map(n => n.textContent); + assert.equal(annotations.filter(t => t === String.raw`\nabla f`).length, 3); + assert.deepEqual(persisted.details, saved); + assert.deepEqual(input, before); + } finally { await act(async () => root.unmount()); host.remove(); } +}); + +test("committed-answer reveals keep the first paragraph inline without flattening later paragraphs", async () => { + const committed = String.raw`First paragraph.\n\nSecond paragraph with $\\nabla f$ and $\nu$.`; + const answer: QuestionAnswerData = { kind: "explain", questionText: "Explain the gradient.", answers: [], text: "Synthetic answer", committed, dontKnow: false, answeredAt: "2026-10-26T00:00:00.000Z" }; + const input = { question: answer.questionText, correctAnswer: committed }; + const before = structuredClone(answer); + const host = document.createElement("div"); document.body.append(host); + const root = createRoot(host); + try { + await act(async () => root.render()); + const paragraphs = host.querySelectorAll("p"); + assert.equal(paragraphs.length, 3, "question plus two committed-answer paragraphs"); + assert.ok(paragraphs[1]!.parentElement!.classList.contains("[&>p:first-child]:inline")); + assert.ok(!paragraphs[1]!.parentElement!.classList.contains("[&_p]:inline")); + assert.equal(host.querySelectorAll(".katex-error").length, 0); + assert.deepEqual(answer, before); + } finally { await act(async () => root.unmount()); host.remove(); } +}); diff --git a/tests/tool-content-presentation.test.tsx b/tests/tool-content-presentation.test.tsx new file mode 100644 index 0000000..35c13d0 --- /dev/null +++ b/tests/tool-content-presentation.test.tsx @@ -0,0 +1,164 @@ +import React from "react"; +(globalThis as { React?: typeof React }).React = React; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import { test } from "node:test"; +import { renderToStaticMarkup } from "react-dom/server"; +import { parseHTML } from "linkedom"; +import { MessageResponse, ToolContentResponse } from "../components/ai-elements/message.tsx"; +import { QuestionDock } from "../components/question-dock.tsx"; +import { AnsweredCard } from "../components/answered-card.tsx"; +import { answerForPresentation, buildAnswer, normalizeOptions, parseAskInput, parseExplainInput, parseQuizInput, + repairMathForDisplay, resolveCorrect, type QuestionAnswerData, type QuestionData } from "../src/core/index.ts"; + +const fixtures = JSON.parse(readFileSync(new URL("./fixtures/tool-rendering/matrix-regressions.json", import.meta.url), "utf8")) as { + cases: Array<{ chatId: string; source: string; raw: string; oldDisplay: string }>; +}; +const a = String.raw`$\begin{pmatrix}a&b\\c&d\end{pmatrix}$`; +const b = String.raw`$\begin{pmatrix}p&q\\r&s\end{pmatrix}$`; +const brokenA = a.replace(String.raw`\\c`, String.raw`\c`); +const brokenB = b.replace(String.raw`\\r`, String.raw`\r`); +const doubled = String.raw`$\\nabla f$`; +const input = { question: a, options: [{ label: a, value: "a" }, { label: b, value: "b" }], correctAnswer: "a", explanation: a }; +const dom = (html: string) => parseHTML(`${html}`).document; +const render = (text: string, recovered = true) => renderToStaticMarkup( + recovered ? {text} : {text}); +const tex = (html: string) => [...dom(html).querySelectorAll('annotation[encoding="application/x-tex"]')].map(n => n.textContent); + +for (const fixture of fixtures.cases) test(`preserves corpus matrix rows: ${fixture.chatId} ${fixture.source}`, () => { + assert.notEqual(fixture.raw, fixture.oldDisplay); + assert.equal(repairMathForDisplay(fixture.raw), fixture.raw); + const expected = dom(render(fixture.raw, false)); + const actual = dom(render(fixture.raw)); + assert.deepEqual(tex(actual.body.innerHTML), tex(expected.body.innerHTML)); + assert.equal(actual.querySelectorAll("mtr").length, expected.querySelectorAll("mtr").length); + assert.ok(actual.querySelectorAll("mtr").length > dom(render(fixture.oldDisplay, false)).querySelectorAll("mtr").length); + assert.equal(actual.querySelectorAll(".katex-error").length, 0); +}); + +test("generic nested environments, comments, and bare row-leading command names are protected", () => { + for (const environment of ["matrix", "pmatrix", "bmatrix", "Bmatrix", "vmatrix", "Vmatrix", "smallmatrix", "array", "aligned", "cases"]) { + const source = `$\\begin{${environment}}a&b\\\\beta&d\\end{${environment}}$`; + assert.equal(repairMathForDisplay(source), source); + } + const nested = "$$\n" + String.raw`\begin{aligned}x&=\begin{pmatrix}a&b\\c&d\end{pmatrix}\\% \end{aligned} is a comment` + "\n" + String.raw`r&=s\end{aligned}` + "\n$$"; + assert.equal(repairMathForDisplay(nested), nested); + const spaced = String.raw`$\begin {pmatrix}a&b\\c&d\end {pmatrix}$`; + assert.equal(repairMathForDisplay(spaced), spaced); + const mixed = String.raw`$\\lambda+\begin{pmatrix}a&b\\c&d\end{pmatrix}$`; + assert.equal(repairMathForDisplay(mixed), mixed.replace(String.raw`\\lambda`, String.raw`\lambda`)); +}); + +test("corpus document framing and final-display percentages recover without new errors", () => { + const cases = JSON.parse(readFileSync(new URL("./fixtures/tool-rendering/document-percent-regressions.json", import.meta.url), "utf8")); + for (const fixture of cases) { + const recovered = repairMathForDisplay(fixture.raw); + const document = dom(render(fixture.raw)); + assert.equal(document.querySelectorAll(".katex-error").length, 0, fixture.chatId); + assert.equal(repairMathForDisplay(recovered), recovered, fixture.chatId); + if (fixture.kind === "percent-at-display-end") { + assert.ok([...document.querySelectorAll(".katex-html")].some(node => node.textContent?.includes("%")), fixture.chatId); + assert.ok(!recovered.includes("\\\\%"), fixture.chatId); + } else { + assert.match(recovered, /\n\$\$\n/); + assert.match(recovered, /\n\$\$$/); + } + } +}); +test("mixed-escaped environment markers retain existing recovery rather than treating an opener as native", () => { + const mixedFixtures = JSON.parse(readFileSync(new URL("./fixtures/tool-rendering/mixed-escaping-regressions.json", import.meta.url), "utf8")) as typeof fixtures; + assert.equal(mixedFixtures.cases.length, 2); + for (const fixture of mixedFixtures.cases) { + const recovered = repairMathForDisplay(fixture.raw); + assert.equal(repairMathForDisplay(recovered), recovered, fixture.chatId); + assert.doesNotMatch(recovered, /\\\\end\{/); + assert.equal(dom(render(fixture.raw)).querySelectorAll(".katex-error").length, 0); + } +}); + +test("presentation-equivalent labels cannot make a source-invalid correctAnswer gradeable", () => { + const source = { question: "Which?", options: [{ label: String.raw`$\nabla f$` }, { label: "no" }], + correctAnswer: doubled, explanation: "A model-authored identity mismatch" }; + const before = structuredClone(source); + assert.equal(repairMathForDisplay(doubled), source.options[0]!.label); + assert.equal(parseQuizInput(source), undefined); + assert.deepEqual(source, before); +}); + +test("shape parsers retain source text and identities; recovery is presentation only", () => { + const quiz = { ...input, question: doubled, explanation: "$\forall x$", options: [{ label: doubled }, { label: "no" }], correctAnswer: doubled }; + const ask = { question: doubled, options: [{ label: doubled, value: doubled, description: doubled }, { label: "no" }] }; + const explain = { question: doubled, correctAnswer: "$\forall x$" }; + const before = structuredClone({ quiz, ask, explain }); + assert.equal(parseQuizInput(quiz)!.question, doubled); + assert.equal(parseQuizInput(quiz)!.explanation, quiz.explanation); + assert.equal(parseQuizInput(quiz)!.correctAnswer, doubled); + assert.deepEqual(parseAskInput(ask), ask); + assert.deepEqual(parseExplainInput(explain), explain); + assert.deepEqual(tex(render(doubled)), [String.raw`\nabla f`]); + assert.deepEqual(tex(render(doubled, false)), [String.raw`\\nabla f`]); + assert.deepEqual({ quiz, ask, explain }, before); +}); + +test("the live dock recovers only its rendering, including question, context and option labels", () => { + const source = { question: doubled, details: doubled, options: [{ label: doubled }, { label: "no" }] }; + const before = structuredClone(source); + const html = renderToStaticMarkup( {}} onSubmit={() => {}} />); + assert.deepEqual(tex(html), Array(3).fill(String.raw`\nabla f`)); + assert.deepEqual(source, before); +}); + +test("answers snapshot source text, not recovered labels or identities", () => { + const source = { ...input, question: doubled, explanation: doubled, options: [{ label: doubled }, { label: "no" }], correctAnswer: doubled }; + const parsed = parseQuizInput(source)!; + const options = normalizeOptions(parsed.options); + const qd: QuestionData = { id: "source", kind: "quiz", question: parsed.question, mode: "single-select", options, + correctIndices: resolveCorrect(parsed.correctAnswer, options).indices, explanation: parsed.explanation, createdAt: "synthetic" }; + const answer = buildAnswer(qd, { values: [doubled], confidence: 70 }, "probe"); + assert.equal(answer.questionText, doubled); + assert.equal(answer.options![0]!.label, doubled); + assert.equal(answer.answers[0]!.value, doubled); + assert.equal(answer.answers[0]!.label, doubled); + assert.equal(answer.explanation, doubled); + assert.equal(answer.correct, true); + const before = structuredClone(answer); + assert.deepEqual(tex(renderToStaticMarkup()), Array(2).fill(String.raw`\nabla f`)); + assert.deepEqual(answer, before); +}); + +function historical(): QuestionAnswerData { + return { kind: "quiz", questionText: brokenA, options: [{ index: 1, label: brokenB }, { index: 2, label: brokenA }], + answers: [{ index: 1, label: brokenB, value: "b" }], correct: false, correctIndices: [2], dontKnow: false, + explanation: brokenA, confidence: 70, answeredAt: "synthetic" }; +} +test("historical source join restores matrices while retaining recorded order, answer and grade", () => { + const answer = historical(); + const before = structuredClone({ answer, input }); + const projection = answerForPresentation(answer, input); + assert.equal(projection.questionText, a); + assert.equal(projection.explanation, a); + assert.deepEqual(projection.options, [{ index: 1, label: b }, { index: 2, label: a }]); + assert.equal(projection.answers, answer.answers); + assert.equal(projection.correctIndices, answer.correctIndices); + assert.equal(projection.correct, false); + assert.equal(projection.confidence, 70); + const html = renderToStaticMarkup(); + assert.deepEqual(tex(html), [a.slice(1,-1), b.slice(1,-1), a.slice(1,-1)]); + assert.match(html, /incorrect/); + assert.deepEqual({ answer, input }, before); +}); + +test("historical ambiguous options and absent/invalid original calls retain their snapshots", () => { + const source = { ...input, options: [...input.options, { label: String.raw`$\begin{matrix}x\\y\end{matrix}$`, value: "c" }] }; + const answer = { ...historical(), answers: [], dontKnow: true, options: [ + { index: 1, label: brokenB }, { index: 2, label: brokenA }, { index: 3, label: String.raw`$\begin{matrix}x\y\end{matrix}$` }, + ] }; + assert.deepEqual(answerForPresentation(answer, source).options, answer.options); + assert.equal(answerForPresentation(answer, undefined), answer); + assert.equal(answerForPresentation(answer, { question: a }), answer); + const dismissed = { kind: "quiz", dismissed: true } as QuestionAnswerData; + assert.equal(answerForPresentation(dismissed, input), dismissed); + const before = structuredClone(answer); + renderToStaticMarkup(); + assert.deepEqual(answer, before); +}); diff --git a/tests/tool-parts.test.ts b/tests/tool-parts.test.ts index baccdfe..a1576f5 100644 --- a/tests/tool-parts.test.ts +++ b/tests/tool-parts.test.ts @@ -12,7 +12,7 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import { quizInputProblems } from "../src/core/question-core.ts"; import { Value } from "typebox/value"; -import { restoreMathEscapes } from "../src/core/math-delims.ts"; +import { repairMathForDisplay, restoreMathEscapes } from "../src/core/math-delims.ts"; import { parseAskInput, parseProbeDoneInput, @@ -170,7 +170,7 @@ describe("LaTeX-escape restoration (the JSON-debris fix, owner ruling 2026-09-26 // The exact live shapes from the modal-logic session: single-backslash // LaTeX inside JSON args — JSON escape resolution ate the command's // first letter (\f → FF ate 'f' of \forall, \v → VT ate 'v' of \varphi). - it("parseQuizInput restores escape debris inside math spans (question + options + explanation)", () => { + it("presentation restores debris from parsed source fields (question + options + explanation)", () => { const parsed = parseQuizInput({ question: "What does \\(\\forall x\\, \\varphi(x)\\) mean?", options: [ @@ -182,18 +182,18 @@ describe("LaTeX-escape restoration (the JSON-debris fix, owner ruling 2026-09-26 }); assert.ok(parsed); assert.equal(parsed.question, "What does \\(\\forall x\\, \\varphi(x)\\) mean?"); - assert.equal(parsed.options[0]!.label, "\\(\\varphi\\) is true when every element satisfies it"); - assert.equal(parsed.options[1]!.label, "\\(\\forall\\) and \\(\\exists\\) are just shorthand"); - assert.equal(parsed.explanation, "\\(\\forall\\) quantifies over the domain."); + assert.equal(repairMathForDisplay(parsed.options[0]!.label), "\\(\\varphi\\) is true when every element satisfies it"); + assert.equal(repairMathForDisplay(parsed.options[1]!.label), "\\(\\forall\\) and \\(\\exists\\) are just shorthand"); + assert.equal(repairMathForDisplay(parsed.explanation), "\\(\\forall\\) quantifies over the domain."); }); - it("parseAskInput restores debris in ask option labels too", () => { + it("presentation restores debris from parsed ask option labels too", () => { const parsed = parseAskInput({ question: "Goal?", options: [{ label: "\\(\x0corall\\) rigor" }, { label: "Intuition first" }], }); assert.ok(parsed); - assert.equal(parsed.options![0]!.label, "\\(\\forall\\) rigor"); + assert.equal(repairMathForDisplay(parsed.options![0]!.label), "\\(\\forall\\) rigor"); }); it("restores TAB debris (the \\to case) inside spans only", () => { @@ -204,7 +204,7 @@ describe("LaTeX-escape restoration (the JSON-debris fix, owner ruling 2026-09-26 explanation: "", }); assert.ok(parsed); - assert.equal(parsed.question, "Is \\(p\\to q\\) valid?"); + assert.equal(repairMathForDisplay(parsed.question), "Is \\(p\\to q\\) valid?"); }); it("control chars OUTSIDE math spans are left alone (real prose/pasted text)", () => { @@ -253,7 +253,7 @@ describe("display-math newline rule (Option A, owner ruling 2026-09-27)", () => explanation: "", }); assert.ok(parsed); - assert.equal(parsed.question, "Evaluate \\[\\left( a \\right)\\] carefully."); + assert.equal(repairMathForDisplay(parsed.question), "Evaluate \\[\\left( a \\right)\\] carefully."); }); it("sloppy \\neq in display math stays broken — the accepted cost (newline kept, byte-identical to a real line break)", () => { @@ -276,7 +276,7 @@ describe("display-math newline rule (Option A, owner ruling 2026-09-27)", () => explanation: "", }); assert.ok(parsed); - assert.equal(parsed.question, "Is \\(p \\neq q\\) valid?"); + assert.equal(repairMathForDisplay(parsed.question), "Is \\(p \\neq q\\) valid?"); }); }); @@ -289,9 +289,9 @@ describe("dollar-delimiter recognition — existing escape restoration rules", ( explanation: "Here $p\to q$ is an implication.", }); assert.ok(parsed); - assert.equal(parsed.question, "What does $\\forall x$ mean?"); - assert.equal(parsed.options[0]!.label, "$\\varphi$ holds"); - assert.equal(parsed.explanation, "Here $p\\to q$ is an implication."); + assert.equal(repairMathForDisplay(parsed.question), "What does $\\forall x$ mean?"); + assert.equal(repairMathForDisplay(parsed.options[0]!.label), "$\\varphi$ holds"); + assert.equal(repairMathForDisplay(parsed.explanation), "Here $p\\to q$ is an implication."); }); it("keeps the same six-character inline recovery map", () => { @@ -320,21 +320,21 @@ describe("dollar-delimiter recognition — existing escape restoration rules", ( } }); - it("preserves legacy replacement frequency when a span has multiple debris characters", () => { + it("restores all debris in one pass consistently across delimiter syntaxes", () => { const body = "p\to q, \rho"; const legacy = restoreMathEscapes(`\\(${body}\\)`).slice(2, -2); assert.equal(restoreMathEscapes(`$${body}$`), `$${legacy}$`); - assert.equal(legacy, "p\\to q, \rho", "the existing first-match-only behavior is unchanged"); + assert.equal(legacy, "p\\to q, \\rho", "recovery converges instead of requiring a second render"); }); it("shares dollar recovery with ask and free-recall fields", () => { const ask = parseAskInput({ question: "Goal?", options: [{ label: "$\x0corall$ rigor" }, { label: "Intuition" }] }); assert.ok(ask); - assert.equal(ask.options![0]!.label, "$\\forall$ rigor"); + assert.equal(repairMathForDisplay(ask.options![0]!.label), "$\\forall$ rigor"); const explain = parseExplainInput({ question: "Explain $p\to q$", correctAnswer: "$p\neq q$ is different." }); assert.ok(explain); - assert.equal(explain.question, "Explain $p\\to q$"); - assert.equal(explain.correctAnswer, "$p\\neq q$ is different."); + assert.equal(repairMathForDisplay(explain.question), "Explain $p\\to q$"); + assert.equal(repairMathForDisplay(explain.correctAnswer), "$p\\neq q$ is different."); }); it("leaves non-math debris, escaped opening dollars, and code examples untouched", () => { @@ -401,13 +401,9 @@ describe("parseExplainInput: the dock's display seam for explain calls", () => { }); }); -describe("over-escaped command heal at the parse seam (owner ruling 2026-10-15)", () => { - // xiaomi/mimo-v2.6-pro and deepseek-v4.1-flash stochastically DOUBLE - // LaTeX command backslashes inside JSON tool arguments — per-call and - // per-field (gradient-descent msg 3: question doubled, explanation and - // options in the SAME call clean). restoreMathEscapesDeep now rides - // healOverescapedCommands after the debris restore, so doubled spans - // heal at every display parse: live dock, settled cards, old records. +describe("over-escaped command recovery after shape parsing", () => { + // Keep these real authoring shapes, but recover only for presentation. + // Parsed source is also used to build durable answers and identities. const CLEAN = "Let $f : \\mathbb{R}^n \\to \\mathbb{R}$ be differentiable at $x$, with $\\nabla f(x) \\neq 0$. Among all unit directions $\\|u\\|_2 = 1$, which makes $D_u f(x)$ most negative?"; const DOUBLE = (s: string): string => s.replace(/\\/g, "\\\\"); @@ -420,19 +416,21 @@ describe("over-escaped command heal at the parse seam (owner ruling 2026-10-15)" explanation: "By Cauchy\u2013Schwarz: $$u^\\star = -\\frac{\\nabla f(x)}{\\|\\nabla f(x)\\|_2}$$", }; - it("parseQuizInput heals a doubled question field (the gradient-descent msg-3 shape)", () => { + it("presentation heals a doubled question field (the gradient-descent msg-3 shape)", () => { const parsed = parseQuizInput({ ...BASE, question: DOUBLE(CLEAN) }); assert.ok(parsed, "parses"); - assert.equal(parsed.question, CLEAN); + assert.equal(parsed.question, DOUBLE(CLEAN), "shape parsing retains source"); + assert.equal(repairMathForDisplay(parsed.question), CLEAN); // The CLEAN sibling fields of the same call stay byte-identical. assert.equal(parsed.options[0]!.label, BASE.options[0]!.label); assert.equal(parsed.explanation, BASE.explanation); }); - it("parseQuizInput heals a quadrupled field (the agent's over-correction, msg 6)", () => { + it("presentation heals a quadrupled field (the agent's over-correction, msg 6)", () => { const parsed = parseQuizInput({ ...BASE, question: DOUBLE(DOUBLE(CLEAN)) }); assert.ok(parsed, "parses"); - assert.equal(parsed.question, CLEAN); + assert.equal(parsed.question, DOUBLE(DOUBLE(CLEAN)), "shape parsing retains source"); + assert.equal(repairMathForDisplay(parsed.question), CLEAN); }); it("doubled option labels and explanations heal too (the inference-runtime msg-7 shape)", () => { @@ -448,10 +446,10 @@ describe("over-escaped command heal at the parse seam (owner ruling 2026-10-15)" assert.ok(parsed, "parses"); assert.equal(parsed.options[0]!.label, "$mkn$"); assert.equal(parsed.options[1]!.label, "$2mkn$"); - assert.equal(parsed.explanation, "$$c_{ij} = \\sum_{\\ell=1}^{k} a_{i\\ell}\\, b_{\\ell j}$$"); + assert.equal(repairMathForDisplay(parsed.explanation), "$$c_{ij} = \\sum_{\\ell=1}^{k} a_{i\\ell}\\, b_{\\ell j}$$"); }); - it("parseAskInput heals doubled ask-option descriptions the same way", () => { + it("presentation heals doubled ask-option descriptions the same way", () => { const parsed = parseAskInput({ question: "Depth?", options: [ @@ -460,7 +458,7 @@ describe("over-escaped command heal at the parse seam (owner ruling 2026-10-15)" ], }); assert.ok(parsed, "parses"); - assert.equal(parsed.options![0]!.description, "why $-\\nabla f$ is steepest descent"); + assert.equal(repairMathForDisplay(parsed.options![0]!.description!), "why $-\\nabla f$ is steepest descent"); }); it("clean records pass through byte-identical (no over-heal)", () => { From 3dc88472eea41eed0d5e5f29c2f16f8d063c415b Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 06:08:04 +0200 Subject: [PATCH 12/19] chore: prepare 0.3.8 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 15d4690..f2c5467 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "learnharness", - "version": "0.3.7", + "version": "0.3.8", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "learnharness", - "version": "0.3.7", + "version": "0.3.8", "license": "AGPL-3.0-only", "dependencies": { "@earendil-works/pi-agent-core": "1.4.0", diff --git a/package.json b/package.json index 5a9f30b..82e0fad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "learnharness", - "version": "0.3.7", + "version": "0.3.8", "license": "AGPL-3.0-only", "author": { "name": "Amos Blomqvist", From 88ba42fd3719ec9d23f5fc97de72653536ed112e Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 18:50:54 +0200 Subject: [PATCH 13/19] fix: allow repairing model slots after subscription disconnect --- app/api/settings/[section]/route.ts | 14 ++- src/server/model-catalog.ts | 14 ++- tests/models-settings-repair.test.ts | 165 +++++++++++++++++++++++++++ 3 files changed, 183 insertions(+), 10 deletions(-) create mode 100644 tests/models-settings-repair.test.ts diff --git a/app/api/settings/[section]/route.ts b/app/api/settings/[section]/route.ts index 616db9f..a51da1c 100644 --- a/app/api/settings/[section]/route.ts +++ b/app/api/settings/[section]/route.ts @@ -10,7 +10,8 @@ * (server/settings-body.ts): a bad shape/type is a 400 carrying the * supported list where one exists. The chat and models sections * additionally pass the catalog gate (validateChatSettings / - * validateModelsSettings — unknown provider/model = 400). Thinking + * validateModelsSettings — unknown provider/model = 400, except unchanged + * model-slot selections may remain saved while unavailable). Thinking * levels never reject a write (owner, interlude 2026-09-22): a pinned * level CLAMPS to the named model's nearest supported level and the * clamped value is stored — model swaps just work, same as the @@ -63,6 +64,8 @@ export async function PUT(request: Request, ctx: { params: Promise<{ section: st if (!parsed.ok) { return Response.json({ error: parsed.error }, { status: 400 }); } + // Compare against the same record that this whole-section write replaces. + const record = readSettingsRecord() ?? { version: 1 }; let agentHealed: AgentSettings | undefined; // Section-specific cross-validation (against the catalog / the prompt @@ -73,11 +76,11 @@ export async function PUT(request: Request, ctx: { params: Promise<{ section: st return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 }); } } - // Model slots wave (2026-09-20): the models section gets the same - // catalog gate the chat section gets — a slot must name a real - // provider+model and a thinking level it supports. + // New/replaced slot models pass the catalog gate. Unchanged saved model + // references may survive disconnect, including thinking-preference edits, + // so each unavailable slot can be repaired independently of the others. if (section === "models") { - const verdict = validateModelsSettings(parsed.settings as ModelsSettings); + const verdict = validateModelsSettings(parsed.settings as ModelsSettings, record.models); if (!verdict.ok) { return Response.json({ error: verdict.error, supported: verdict.supported }, { status: 400 }); } @@ -124,7 +127,6 @@ export async function PUT(request: Request, ctx: { params: Promise<{ section: st // Read-modify-write ONE section of the record; everything else rides // along untouched (other sections, unknown keys — sanitize preserves). - const record = readSettingsRecord() ?? { version: 1 }; const next = { ...record, [section]: sectionOut }; writeSettingsRecord(next); return Response.json({ settings: next }); diff --git a/src/server/model-catalog.ts b/src/server/model-catalog.ts index 0aa0b2a..11fbe0f 100644 --- a/src/server/model-catalog.ts +++ b/src/server/model-catalog.ts @@ -245,10 +245,17 @@ export function validateChatSettings(settings: ChatSettings): SettingsValidation return { ok: true }; } -export function validateModelsSettings(settings: ModelsSettings): SettingsValidation { +/** Validate introduced/replaced model selections, not unchanged saved pins. + * A disconnected subscription loses its available catalog, but its stored + * selections must not prevent repairing another slot or editing thinking. + * Only the SAME slot's provider+id can pass through; runtime admission stays + * separate. Callers without a stored section retain strict catalog checking. */ +export function validateModelsSettings(settings: ModelsSettings, stored?: ModelsSettings): SettingsValidation { for (const name of MODEL_SLOTS) { const slot = settings[name]; if (!slot) continue; + const previous = stored?.[name]?.model; + if (slot.model && previous && slot.model.provider === previous.provider && slot.model.id === previous.id) continue; const verdict = validateSlotSpec(name, slot); if (!verdict.ok) return verdict; } @@ -280,9 +287,8 @@ function validateSlotSpec(name: ModelSlotName, slot: { model?: { provider: strin // runtime — and the clamped value is what lands in the record. The // server is the truth: the record shows the clamped level after write. -/** Clamp one level against one model (identity when the model is unknown - * or the level already supported — the existence gate rejects unknown - * models separately, so this only ever sees real ones in practice). */ +/** Clamp one level against one model. Preserve the preference when metadata + * is unavailable for an unchanged saved pin; runtime clamps after reconnect. */ function clampLevelFor(provider: string, id: string, level: ThinkingLevel): ThinkingLevel { const model = connectionModel(provider, id); return model ? clampThinkingLevel(model, level) : level; diff --git a/tests/models-settings-repair.test.ts b/tests/models-settings-repair.test.ts new file mode 100644 index 0000000..224e08d --- /dev/null +++ b/tests/models-settings-repair.test.ts @@ -0,0 +1,165 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { after, beforeEach, test } from "node:test"; +import type { ModelsSettings, ModelSlotName } from "../src/core/settings.ts"; +import type { NativeSubscriptionSnapshot } from "../src/core/contracts/subscriptions.ts"; + +// Every app import follows isolation. No installed credentials, ambient keys, +// OAuth, or provider requests: subscription state comes from synthetic IPC. +const directory = fs.mkdtempSync(path.join(os.tmpdir(), "lh-models-repair-")); +process.env.LH_DATA_DIR = directory; +process.env.LH_AUTH_FILE = path.join(directory, "auth.json"); +process.env.LH_IGNORE_ENV_KEYS = "1"; +fs.writeFileSync(process.env.LH_AUTH_FILE, "{}"); +const originalSend = Object.getOwnPropertyDescriptor(process, "send"); +const originalConnected = Object.getOwnPropertyDescriptor(process, "connected"); +const marker = process.env.LH_DESKTOP_SERVER, secret = process.env.LH_DESKTOP_API_SECRET; +process.env.LH_DESKTOP_SERVER = "1"; +process.env.LH_DESKTOP_API_SECRET = "d".repeat(64); +Object.defineProperty(process, "connected", { configurable: true, value: true }); + +const { piModels, piEngineForSpec } = await import("../src/agent/models.ts"); +const { refreshSubscriptionModels } = await import("../src/agent/subscription-bridge.ts"); +const { modelCatalog, validateModelsSettings } = await import("../src/server/model-catalog.ts"); +const { readSettings: readSettingsRecord, writeSettings: writeSettingsRecord } = await import("../src/store/settings.ts"); +const { PUT } = await import("../app/api/settings/[section]/route.ts"); +const { clampThinkingLevel, normalizeContext } = await import("@earendil-works/pi-ai"); +const native = piModels().getModels("github-copilot")[0]!; +const copilot = { provider: "github-copilot-subscription", id: native.id }; +const replacement = { provider: "openrouter", id: "z-ai/glm-5.3-flash" }; +const slots: ModelSlotName[] = ["strong", "cheap", "vision"]; +const initial: ModelsSettings = Object.fromEntries(slots.map((slot) => [slot, { model: copilot, thinkingLevel: "high" }])); +let connected = false, revision = 0, leases = 0; +function snapshot(): NativeSubscriptionSnapshot { + return { + statuses: [{ providerId: "github-copilot", available: true, connected, pending: false, allowBackgroundUse: true, connectionId: connected ? "fixture-connection" : null, epoch: revision, authorityRevision: revision, revision }], + models: { "github-copilot": connected ? [native] : [] }, + }; +} +Object.defineProperty(process, "send", { configurable: true, value: (message: { id: string; op: string }, callback?: () => void) => { + if (message.op !== "snapshot") { leases++; throw new Error("No credential lease expected"); } + queueMicrotask(() => (process.emit as (event: string, value: unknown) => boolean)("message", { channel: "lh:subscriptions", id: message.id, op: message.op, ok: true, value: snapshot() })); + callback?.(); return true; +} }); +async function connection(value: boolean) { connected = value; revision++; await refreshSubscriptionModels(); } +async function put(body: unknown) { + return PUT(new Request("http://localhost/api/settings/models", { method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify(body) }), { params: Promise.resolve({ section: "models" }) }); +} +beforeEach(async () => { + await connection(false); + writeSettingsRecord({ version: 1, models: initial, general: { autoUpdate: false }, graph: { rendering: false } }); +}); +after(() => { + if (originalSend) Object.defineProperty(process, "send", originalSend); else delete process.send; + if (originalConnected) Object.defineProperty(process, "connected", originalConnected); else delete (process as { connected?: boolean }).connected; + for (const [key, value] of [["LH_DESKTOP_SERVER", marker], ["LH_DESKTOP_API_SECRET", secret]]) { + if (value === undefined) delete process.env[key!]; else process.env[key!] = value; + } + fs.rmSync(directory, { recursive: true, force: true }); +}); + +for (const slot of slots) test(`${slot} can be replaced while the other disconnected slots remain saved`, async () => { + const response = await put({ ...initial, [slot]: { model: replacement, thinkingLevel: "xhigh" } }); + assert.equal(response.status, 200, JSON.stringify(await response.clone().json())); + const stored = readSettingsRecord()!; + assert.deepEqual(stored.models?.[slot], { model: replacement, thinkingLevel: "max" }); + for (const other of slots.filter((name) => name !== slot)) assert.deepEqual(stored.models?.[other], initial[other]); + assert.deepEqual(stored.general, { autoUpdate: false }); + assert.deepEqual(stored.graph, { rendering: false }); + assert.deepEqual((await response.json()).settings, stored); + assert(!modelCatalog().models.some((model) => model.provider === copilot.provider), "saving stale pins must not make them available choices"); +}); + +test("all three slots can be repaired successively without reconnecting", async () => { + for (const slot of slots) { + const response = await put({ ...readSettingsRecord()!.models, [slot]: { model: replacement } }); + assert.equal(response.status, 200); + } + assert.deepEqual(readSettingsRecord()!.models, Object.fromEntries(slots.map((slot) => [slot, { model: replacement }]))); +}); + +test("thinking preferences on unchanged disconnected models can be changed, removed, and restored", async () => { + for (const thinkingLevel of ["max", undefined, "minimal"] as const) { + const strong = { model: copilot, ...(thinkingLevel ? { thinkingLevel } : {}) }; + const response = await put({ ...initial, strong }); + assert.equal(response.status, 200); + assert.deepEqual(readSettingsRecord()!.models?.strong, strong); + } + await connection(true); + const engine = piEngineForSpec({ ...copilot, thinkingLevel: "minimal" }, false); + assert.equal(engine.level, clampThinkingLevel(native, "minimal"), "reconnected runtime clamps the saved preference"); + assert.equal((await put(readSettingsRecord()!.models)).status, 200); + assert.equal(readSettingsRecord()!.models?.strong?.thinkingLevel, engine.level, "known metadata still clamps on write"); +}); + +test("introduced or replaced unknown models still reject, without writing any section", async () => { + for (const model of [ + { provider: copilot.provider, id: "fixture-not-in-catalog" }, + { provider: "xai-subscription", id: copilot.id }, + { provider: "fixture-unknown-provider", id: copilot.id }, + { provider: replacement.provider, id: "fixture-not-in-catalog" }, + ]) { + const before = readSettingsRecord(); + assert.equal((await put({ ...initial, strong: { model } })).status, 400); + assert.deepEqual(readSettingsRecord(), before); + } + writeSettingsRecord({ version: 1, models: { strong: initial.strong } }); + assert.equal((await put({ strong: initial.strong, cheap: initial.strong })).status, 400, "a pin saved in one slot cannot authorize another slot"); + assert.equal((await put(initial)).status, 400, "missing slots cannot introduce disconnected models"); +}); + +test("a first models write cannot introduce disconnected selections", async () => { + fs.unlinkSync(path.join(directory, "settings.json")); + assert.equal((await put(initial)).status, 400); + assert.equal(readSettingsRecord(), null); +}); + +test("a replaced stale selection cannot be reintroduced without availability", async () => { + assert.equal((await put({ ...initial, strong: { model: replacement } })).status, 200); + assert.equal((await put(initial)).status, 400); + assert.deepEqual(readSettingsRecord()!.models?.strong?.model, replacement); +}); + +test("slot removal still replaces the whole section, including clearing all slots", async () => { + assert.equal((await put({ cheap: initial.cheap })).status, 200); + assert.deepEqual(readSettingsRecord()!.models, { cheap: initial.cheap }); + assert.equal((await put({})).status, 200); + assert.deepEqual(readSettingsRecord()!.models, {}); +}); + +test("unchanged model identity does not bypass body shape validation", async () => { + for (const body of [ + { ...initial, strong: { model: copilot, thinkingLevel: 7 } }, + { ...initial, strong: { thinkingLevel: "high" } }, + { ...initial, strong: { model: { provider: copilot.provider } } }, + ]) assert.equal((await put(body)).status, 400); + assert.deepEqual(readSettingsRecord()!.models, initial); +}); + +test("standalone catalog validation stays strict without a stored section", () => { + assert.equal(validateModelsSettings(initial).ok, false); + assert.equal(validateModelsSettings({ strong: { model: replacement } }).ok, true); +}); + +test("retained selections remain unavailable at runtime, with no API-key fallback or credential lease", async () => { + await connection(true); + const engine = piEngineForSpec(copilot, false); + await connection(false); + assert.equal((await put({ ...initial, cheap: { model: replacement } })).status, 200); + assert.throws(() => piEngineForSpec(copilot, false), /catalog has no model/); + let requests = 0; + const before = leases; + const key = process.env.COPILOT_GITHUB_TOKEN; + process.env.COPILOT_GITHUB_TOKEN = "fixture-must-not-be-used"; + try { + const result = await engine.streamFn(engine.model, normalizeContext({ messages: [] }), { fetch: async () => { requests++; throw new Error("Must not dispatch"); } }).result(); + assert.equal(result.stopReason, "error"); + assert.match(result.errorMessage ?? "", /No API-key fallback was used/); + assert.equal(requests, 0); + assert.equal(leases, before); + } finally { + if (key === undefined) delete process.env.COPILOT_GITHUB_TOKEN; else process.env.COPILOT_GITHUB_TOKEN = key; + } +}); From b81f0a3455dc514fac2a5b6e2e7d7de850fb5b4f Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 19:08:15 +0200 Subject: [PATCH 14/19] fix: avoid replaying superseded compaction summaries --- docs/compaction-accounting.md | 11 +++ src/core/compaction.ts | 6 +- src/core/native-messages.ts | 14 ++-- src/server/compaction.ts | 17 ++++- tests/compaction-service.test.ts | 112 ++++++++++++++++++++++++++++++- tests/compaction.test.ts | 44 ++++++++++++ 6 files changed, 191 insertions(+), 13 deletions(-) diff --git a/docs/compaction-accounting.md b/docs/compaction-accounting.md index f3b20d6..b2abcdc 100644 --- a/docs/compaction-accounting.md +++ b/docs/compaction-accounting.md @@ -24,6 +24,17 @@ prompt, plan continuity, persistence, threshold, scheduling, and authorization lifecycle. It does not adopt Pi's coding summary or separate split-turn summary calls. A cut that drops no active history does not launch a summarizer. +When the active cutoff is applied, conversation requests carry the latest summary +once, followed by the retained transcript without older compaction markers. When +a subsequent compaction supplies that prior summary separately, it includes it +once, plus the newly superseded transcript without summary markers. All markers +and original messages remain stored. The existing full-history fallback for an +unresolved cutoff is unchanged, including its summary text. + +A provider response stopped at its output limit (`length`) is an incomplete +summary, not a successful checkpoint. Compaction fails without changing the +existing history or prior summary; a later attempt can retry normally. + ## Conversation meter The numerator is the latest qualifying completed conversation response's native diff --git a/src/core/compaction.ts b/src/core/compaction.ts index 8e3f768..4ca0fab 100644 --- a/src/core/compaction.ts +++ b/src/core/compaction.ts @@ -272,9 +272,9 @@ export function newCompactionMarker(summary: string, details: CompactionDetails) }; } -/** The ACTIVE compaction marker: the newest one in the record (chained - * compactions — the last one rules; older markers fall inside the - * dropped span and never render). */ +/** The ACTIVE compaction marker: the newest one with a retained-message + * id. Its summary supersedes older markers even when their stored + * positions remain inside its retained tail. */ export function activeCompactionOf(messages: readonly LhMessage[]): (LhMarkerMessage & { details: CompactionDetails }) | null { for (let i = messages.length - 1; i >= 0; i--) { const m = messages[i]!; diff --git a/src/core/native-messages.ts b/src/core/native-messages.ts index 63f2720..80f9872 100644 --- a/src/core/native-messages.ts +++ b/src/core/native-messages.ts @@ -562,8 +562,9 @@ export function toLlmMessages(messages: readonly AgentMessage[]): Message[] { // THE COMPACTION CUT (owner round 14, 2026-10-05): the ACTIVE marker // (the newest — chained compactions rule last) names the first kept // message; everything before it is replaced by the marker's content - // (header + summary) rendered ONCE, up top. The marker itself is then - // skipped in place — it must never render twice. A cut naming a + // (header + summary) rendered ONCE, up top. All compaction markers + // in the retained tail are then skipped, including older summaries + // whose stored positions still lie after the cut. A cut naming a // missing id degrades honestly (everything renders — never throw). const lhMessages = messages as readonly LhMessage[]; const active = activeCompactionOf(lhMessages); @@ -604,11 +605,10 @@ export function toLlmMessages(messages: readonly AgentMessage[]): Message[] { // UI-only markers: plan blocks, and problem dismissals (the // synthesized conclude pair carries the model's face). if (marker.customType === "plan-block" || marker.customType === "problem-dismissed") continue; - // The ACTIVE compaction marker already rendered as the head — - // never twice. (Stale markers before the cut are unreachable; a - // marker whose cut could not resolve has no head and renders as - // its user text — honest degradation.) - if (marker.customType === "compaction" && head !== null && marker.id === active!.id) continue; + // The head supersedes every compaction marker, not only its own + // stored position. Without a resolved head, preserve the existing + // full-history fallback, including the summary text. + if (marker.customType === "compaction" && head !== null) continue; out.push({ role: "user", content: [{ type: "text", text: marker.content }], diff --git a/src/server/compaction.ts b/src/server/compaction.ts index cc62aa6..865f9ca 100644 --- a/src/server/compaction.ts +++ b/src/server/compaction.ts @@ -69,7 +69,7 @@ async function subagentSummarizer(prompt: string, engine: PiEngine): Promise<{ s totalTokens: 0, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }, }; - const { text } = await runSubagent([], { + const { text, newMessages } = await runSubagent([], { // The engine override is always present (the chat's own conversation // engine — owner ruling), so the role label never resolves an // engine; "researcher" is the least-wrong existing ModelRole label @@ -83,6 +83,12 @@ async function subagentSummarizer(prompt: string, engine: PiEngine): Promise<{ s usage = u; }, }); + // A provider-declared incomplete response is not a usable checkpoint, + // even when it contains nonempty text. Match Pi's compaction failure + // semantics: leave the existing history and prior summary untouched. + if (newMessages.some((message) => message.role === "assistant" && message.stopReason === "length")) { + throw new Error("Compaction summary hit the output limit and is incomplete"); + } return { summary: text, usage }; } @@ -201,7 +207,14 @@ async function compact(deps: CompactionDeps, chatId: string): Promise { spanStart = prevKept; priorSummary = previous.content; } - const spanText = serializeForSummary(messages.slice(spanStart, cut)); + const span = messages.slice(spanStart, cut); + // The separately supplied prior summary replaces all summary markers + // in this span. Without a resolved prior boundary, retain the existing + // legacy fallback rather than silently discarding summary-only context. + const summaryMessages = priorSummary + ? span.filter((m) => m.role !== "custom" || m.customType !== "compaction") + : span; + const spanText = serializeForSummary(summaryMessages); const fullSpan = priorSummary ? `[SYSTEM NOTE — prior compaction summary, still authoritative where not superseded]\n${priorSummary}\n\n${spanText}` : spanText; const prompt = buildCompactionPrompt({ diff --git a/tests/compaction-service.test.ts b/tests/compaction-service.test.ts index ee5b4d4..9a27a66 100644 --- a/tests/compaction-service.test.ts +++ b/tests/compaction-service.test.ts @@ -27,12 +27,14 @@ const { ensureCompacted, kickCompaction, awaitCompaction, + compactionInFlight, effectiveThresholdTokens, projectedContextTokens, } = await import("../src/server/compaction.ts"); import type { CompactionDeps } from "../src/server/compaction.ts"; +import { createFauxCore, fauxAssistantMessage } from "@earendil-works/pi-ai"; const { subscribe } = await import("../src/core/events.ts"); -const { activeCompactionOf, newUserMessage, measuredContextTokens, COMPACTED_HISTORY_HEADER } = await import("../src/core/index.ts"); +const { activeCompactionOf, newCompactionMarker, newUserMessage, measuredContextTokens, toLlmMessages, COMPACTED_HISTORY_HEADER } = await import("../src/core/index.ts"); import type { LhMessage } from "../src/core/index.ts"; after(() => { @@ -167,12 +169,120 @@ describe("the compaction service", () => { await ensureCompacted(deps, chatId); assert.ok(seenPrompt.includes("prior compaction summary"), "the prior summary rides the span"); assert.ok(seenPrompt.includes(COMPACTED_HISTORY_HEADER), "as the marker's canonical text"); + assert.equal(seenPrompt.split(COMPACTED_HISTORY_HEADER).length - 1, 1, "the prior summary is supplied exactly once"); const record = chatStore.getChat(chatId)!; const markers = record.messages.filter((m) => (m as { customType?: string }).customType === "compaction"); assert.equal(markers.length, 2, "both markers persist (history is never rewritten)"); assert.ok(activeCompactionOf(record.messages)!.content.includes("SECOND SUMMARY"), "the newest rules"); }); + it("ten successive compactions keep one summary on the wire and preserve the complete record", async () => { + const chat = chatStore.createChat("repeated-cycle fixture"); + chatStore.saveMessages(chat.id, Array.from({ length: 20 }, (_, i) => big(`exchange-${i}`, 8))); + let calls = 0; + const deps: CompactionDeps = { + ...depsOf(chat.id, 150_000), + summarize: async (prompt) => { + assert.equal(prompt.split(COMPACTED_HISTORY_HEADER).length - 1, calls === 0 ? 0 : 1); + if (calls > 0) assert.ok(prompt.includes(`SUMMARY-${calls}.`), "only the immediately prior summary rides forward"); + return { + summary: `SUMMARY-${++calls}.`, + usage: { input: 1, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 2, cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 } }, + }; + }, + }; + for (let cycle = 1; cycle <= 10; cycle++) { + const messages = [...chatStore.getChat(chat.id)!.messages]; + if (cycle > 1) messages.push(big(`growth-${cycle}`, 8)); + // Controlled provider readings trigger compaction while the visible + // retained tail advances slowly enough to contain older markers. + messages.push({ id: `usage-${cycle}`, role: "assistant", content: [{ type: "text", text: "measured response" }], stopReason: "stop", usage: { input: 150_001, output: 1, cacheRead: 0, cacheWrite: 0, totalTokens: 150_002 }, timestamp: cycle } as LhMessage); + chatStore.saveMessages(chat.id, messages); + await ensureCompacted(deps, chat.id); + assert.equal(calls, cycle); + const record = chatStore.getChat(chat.id)!; + assert.deepEqual(record.messages.slice(0, -1), messages, "every original message and marker remains stored"); + assert.equal(record.messages.filter((m) => m.role === "custom" && m.customType === "compaction").length, cycle); + const wire = JSON.stringify(toLlmMessages(record.messages)); + assert.equal(wire.split(COMPACTED_HISTORY_HEADER).length - 1, 1, `cycle ${cycle}: one summary on the wire`); + assert.ok(wire.includes(`SUMMARY-${cycle}.`)); + assert.equal(measuredContextTokens(record.messages), null, "committing a marker invalidates pre-compaction readings"); + } + }); + + it("a resolved prior summary replaces all summary markers in the newly superseded span", async () => { + const chat = chatStore.createChat("summary span fixture"); + const kept = big("previously retained"); + const details = { firstKeptMessageId: kept.id, tokensBefore: 150_000, at: "fixture" }; + const steer: LhMessage = { id: "steer", role: "custom", customType: "subagent-steer", content: "RESEARCH DELIVERY", display: true, details: {}, timestamp: 1 }; + const messages = [big("discarded"), kept, newCompactionMarker("SUPERSEDED SUMMARY", details), steer, newCompactionMarker("LATEST SUMMARY", details), big("growth"), big("recent")]; + chatStore.saveMessages(chat.id, messages); + let seenPrompt = ""; + await ensureCompacted({ ...depsOf(chat.id, 40_000), summarize: async (prompt, engine) => { + seenPrompt = prompt; + return depsOf(chat.id, 40_000).summarize!(prompt, engine); + } }, chat.id); + assert.ok(seenPrompt.includes("previously retained")); + assert.ok(seenPrompt.includes("RESEARCH DELIVERY"), "non-compaction context markers still ride the span"); + assert.ok(!seenPrompt.includes("SUPERSEDED SUMMARY")); + assert.equal(seenPrompt.split("LATEST SUMMARY").length - 1, 1); + assert.equal(seenPrompt.split(COMPACTED_HISTORY_HEADER).length - 1, 1); + assert.deepEqual(chatStore.getChat(chat.id)!.messages.slice(0, -1), messages); + }); + + it("an unresolved cutoff preserves legacy summary text in the next compaction input", async () => { + const chat = chatStore.createChat("legacy summary fixture"); + const legacy = newCompactionMarker("LEGACY-ONLY INFORMATION", { firstKeptMessageId: "missing", tokensBefore: 150_000, at: "fixture" }); + const messages = [big("old"), legacy, big("recent"), newUserMessage("latest")]; + chatStore.saveMessages(chat.id, messages); + let seenPrompt = ""; + await ensureCompacted({ ...depsOf(chat.id, 40_000), summarize: async (prompt, engine) => { + seenPrompt = prompt; + return depsOf(chat.id, 40_000).summarize!(prompt, engine); + } }, chat.id); + assert.equal(seenPrompt.split("LEGACY-ONLY INFORMATION").length - 1, 1); + assert.ok(!seenPrompt.includes("prior compaction summary"), "no separate prior summary exists for an unresolved cutoff"); + assert.deepEqual(chatStore.getChat(chat.id)!.messages.slice(0, -1), messages); + }); + + it("the production summarizer rejects a length-stopped checkpoint and a complete retry can commit", async () => { + const chatId = overThresholdChat("incomplete summary fixture"); + await ensureCompacted(depsOf(chatId, 40_000), chatId); + chatStore.saveMessages(chatId, [...chatStore.getChat(chatId)!.messages, big("growth 1"), big("growth 2")]); + const faux = createFauxCore({}); + const { summarize: _injected, ...base } = depsOf(chatId, 40_000); + const deps: CompactionDeps = { ...base, engine: { model: faux.getModel(), streamFn: faux.streamSimple } }; + const before = chatStore.getChat(chatId)!; + assert.ok(activeCompactionOf(before.messages), "the failed replacement must preserve an existing checkpoint too"); + const events: string[] = []; + const off = subscribe((event) => { + const { chatId: id, running } = event as { chatId?: string; running?: boolean }; + if (event.type === "compaction-changed" && id === chatId) events.push(`${running}`); + }); + // Exercise the actual zero-tool summarizer with an offline provider; + // NODE_ENV=test otherwise deliberately skips non-injected summaries. + const env: Record = process.env; + const originalNodeEnv = env.NODE_ENV; + env.NODE_ENV = "development"; + try { + faux.setResponses([fauxAssistantMessage("INCOMPLETE SUMMARY", { stopReason: "length" })]); + await ensureCompacted(deps, chatId); + assert.deepEqual(chatStore.getChat(chatId), before, "an explicitly unfinished summary must not become a checkpoint"); + assert.equal(compactionInFlight(chatId), false, "the failed run releases its gate"); + assert.deepEqual(events, ["true", "false"]); + faux.setResponses([fauxAssistantMessage("COMPLETE SUMMARY")]); + await ensureCompacted(deps, chatId); + const record = chatStore.getChat(chatId)!; + assert.ok(activeCompactionOf(record.messages)!.content.includes("COMPLETE SUMMARY")); + assert.deepEqual(record.messages.slice(0, -1), before.messages); + assert.ok(!JSON.stringify(record.messages).includes("INCOMPLETE SUMMARY")); + } finally { + if (originalNodeEnv === undefined) delete env.NODE_ENV; + else env.NODE_ENV = originalNodeEnv; + off(); + } + }); + it("effectiveThresholdTokens: the setting rules big windows; the window rule binds small ones", () => { assert.equal(effectiveThresholdTokens(150_000, 200_000), 150_000); assert.equal(effectiveThresholdTokens(150_000, 100_000), 100_000 - 16_384); diff --git a/tests/compaction.test.ts b/tests/compaction.test.ts index 5139558..954ea5f 100644 --- a/tests/compaction.test.ts +++ b/tests/compaction.test.ts @@ -256,6 +256,22 @@ describe("toLlmMessages honors the cut", () => { assert.equal(JSON.stringify(out).indexOf("COMPACTED]"), JSON.stringify(out).lastIndexOf("COMPACTED]"), "header appears exactly once"); }); + it("omits nine superseded summaries inside the retained tail without changing stored history", () => { + const kept = newUserMessage("retained exchange"); + const details = { firstKeptMessageId: kept.id, tokensBefore: 200_000, at: "fixture" }; + const old = Array.from({ length: 9 }, (_, i) => newCompactionMarker(`SUPERSEDED-${i}`, details)); + const fresh = newCompactionMarker("LATEST SUMMARY", details); + const messages = [newUserMessage("discarded"), kept, ...old, newUserMessage("latest reply"), fresh]; + const original = JSON.stringify(messages); + const out = toLlmMessages(messages); + const flat = JSON.stringify(out); + assert.equal(out.length, 3, "latest summary + retained exchange + latest reply"); + assert.ok(flat.includes("LATEST SUMMARY")); + assert.ok(!flat.includes("SUPERSEDED-")); + assert.equal(flat.split(COMPACTED_HISTORY_HEADER).length - 1, 1); + assert.equal(JSON.stringify(messages), original, "projection never rewrites the record"); + }); + it("a pending (dangling) question call in the KEPT tail still gets the interrupted synthesis", () => { const kept = [assistantWithCall("a1", "call_1")]; const marker = newCompactionMarker("S", { firstKeptMessageId: "a1", tokensBefore: 1, at: "t" }); @@ -270,6 +286,22 @@ describe("toLlmMessages honors the cut", () => { assert.equal(out.length, 3, "no head swap — all three render (marker as its user text)"); }); + it("an unresolved newest cutoff preserves the full-history fallback, including summary text", () => { + const kept = newUserMessage("retained exchange"); + const old = newCompactionMarker("OLD SUMMARY", { firstKeptMessageId: kept.id, tokensBefore: 1, at: "fixture" }); + const fresh = newCompactionMarker("LATEST SUMMARY", { firstKeptMessageId: "missing", tokensBefore: 2, at: "fixture" }); + const messages = [newUserMessage("earliest exchange"), kept, old, newUserMessage("latest reply"), fresh]; + const original = JSON.stringify(messages); + const out = toLlmMessages(messages); + assert.equal(out.length, messages.length); + assert.equal(out[0]!.content, "earliest exchange", "do not silently apply the older cutoff"); + const flat = JSON.stringify(out); + assert.ok(flat.includes("OLD SUMMARY")); + assert.ok(flat.includes("LATEST SUMMARY")); + assert.equal(flat.split(COMPACTED_HISTORY_HEADER).length - 1, 2); + assert.equal(JSON.stringify(messages), original); + }); + it("chained compaction: the newest marker's cut wins; the older marker falls in the dropped span", () => { const oldMarker = newCompactionMarker("OLD", { firstKeptMessageId: "m1", tokensBefore: 1, at: "t" }); const m1 = newUserMessage("x"); @@ -303,6 +335,11 @@ describe("serializeForSummary + buildCompactionPrompt", () => { assert.ok(!text.toLowerCase().includes("plan-block"), "UI-only markers never serialize"); }); + it("preserves legacy summary text when the caller has no separately supplied prior summary", () => { + const marker = newCompactionMarker("LEGACY SUMMARY", { firstKeptMessageId: "missing", tokensBefore: 1, at: "fixture" }); + assert.ok(serializeForSummary([marker]).includes("LEGACY SUMMARY")); + }); + it("the prompt demands the four sections in order and carries the plan tree verbatim", () => { const prompt = buildCompactionPrompt({ spanText: "TRANSCRIPT", planTreeText: "- [done] A (a1)\n- A → B", kind: "teach" }); const order = ["The learner's understanding", "The teaching arc", "The plan dependency tree", "The current moment"].map( @@ -388,6 +425,11 @@ describe("toLlmMessages carries the leading system message (request chrome)", () const record = (): LhMessage[] => [ { id: "old", role: "user", content: "dropped history", timestamp: 1 }, { id: "kept", role: "user", content: "kept tail", timestamp: 2 }, + newCompactionMarker("SUPERSEDED SUMMARY", { + firstKeptMessageId: "kept", + tokensBefore: 98, + at: new Date(0).toISOString(), + }), newCompactionMarker("SUMMARY", { firstKeptMessageId: "kept", tokensBefore: 99, @@ -407,6 +449,8 @@ describe("toLlmMessages carries the leading system message (request chrome)", () : ((m.content as { text?: string }[])[0]?.text ?? ""); assert.ok(out.slice(2).some((m) => m.role === "user" && textOf(m) === "kept tail")); assert.ok(!out.some((m) => m.role === "user" && textOf(m) === "dropped history")); + assert.ok(!JSON.stringify(out).includes("SUPERSEDED SUMMARY")); + assert.equal(JSON.stringify(out).split(COMPACTED_HISTORY_HEADER).length - 1, 1); }); it("without a cut the chrome renders EXACTLY ONCE (no duplicate)", () => { From 683eb93bcca28aa1bb35c531fde4a0c1671e3c5b Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Mon, 26 Oct 2026 19:43:09 +0200 Subject: [PATCH 15/19] Fix model labels and catalog-backed setup initialization --- components/model-catalog.ts | 54 +++++++ components/model-picker.tsx | 52 +------ components/settings/agent-section.tsx | 52 ++----- components/settings/keys-section.tsx | 10 +- components/setup-dialog.tsx | 205 ++++++++++++++------------ components/usage-format.ts | 24 +-- tests/byok-refusal.test.ts | 8 +- tests/model-catalog-setup.test.tsx | 176 ++++++++++++++++++++++ tests/render-stats-bar.test.tsx | 16 +- tests/settings-pure.test.ts | 2 +- 10 files changed, 389 insertions(+), 210 deletions(-) create mode 100644 components/model-catalog.ts create mode 100644 tests/model-catalog-setup.test.tsx diff --git a/components/model-catalog.ts b/components/model-catalog.ts new file mode 100644 index 0000000..6a97320 --- /dev/null +++ b/components/model-catalog.ts @@ -0,0 +1,54 @@ +"use client"; + +import { useCallback, useEffect, useState } from "react"; +import type { ModelCatalogResponse } from "@/src/core"; +import { onSettingsSaved } from "@/components/settings-signal"; + +// Shared stale-while-revalidate catalog. Metadata refreshes retain the last +// good snapshot; connection changes clear selectable models immediately. +let catalogCache: ModelCatalogResponse | undefined; +let catalogError = false; +let catalogInflight: Promise | undefined; +let catalogGeneration = 0; +let stopCatalogSignals: (() => void) | undefined; +const catalogReaders = new Set<() => void>(); +const publishCatalog = () => { for (const reader of catalogReaders) reader(); }; + +function fetchCatalog(revalidate = false): void { + if (revalidate) { catalogGeneration++; catalogInflight = undefined; } + if (catalogInflight) return; + const generation = catalogGeneration; + catalogError = false; + publishCatalog(); + catalogInflight = fetch("/api/models") + .then((r) => { if (!r.ok) throw new Error(`catalog request failed: ${r.status}`); return r.json() as Promise; }) + .then((c) => { + if (generation === catalogGeneration) { catalogCache = c; catalogError = false; publishCatalog(); } + }) + .catch(() => { + if (generation === catalogGeneration) { catalogError = true; publishCatalog(); } + }) + .finally(() => { if (generation === catalogGeneration) catalogInflight = undefined; }); +} + +export function useModelCatalog(): { catalog: ModelCatalogResponse | undefined; failed: boolean; refresh: () => void } { + const [snapshot, setSnapshot] = useState(() => ({ catalog: catalogCache, failed: catalogError })); + useEffect(() => { + const first = catalogReaders.size === 0; + const read = () => setSnapshot({ catalog: catalogCache, failed: catalogError }); + catalogReaders.add(read); + if (first) { + stopCatalogSignals = onSettingsSaved((section) => { + if (section === "keys") { + catalogGeneration++; catalogInflight = undefined; catalogCache = undefined; + fetchCatalog(); + } else if (section === "models") fetchCatalog(true); + }); + fetchCatalog(true); + } else fetchCatalog(); + read(); + return () => { catalogReaders.delete(read); if (!catalogReaders.size) { stopCatalogSignals?.(); stopCatalogSignals = undefined; } }; + }, []); + const refresh = useCallback(() => fetchCatalog(true), []); + return { ...snapshot, refresh }; +} diff --git a/components/model-picker.tsx b/components/model-picker.tsx index 1ae092d..b618e73 100644 --- a/components/model-picker.tsx +++ b/components/model-picker.tsx @@ -8,52 +8,11 @@ import { ModelMenuList, ThinkingMenuList } from "@/components/settings/model-men import { composeSlotModel } from "@/components/settings/agent-section"; import { MenuShell } from "@/components/menu-shell"; import { Hint } from "@/components/ui/hint"; -import { notifySettingsSaved, onSettingsSaved, useSettingsSaved } from "@/components/settings-signal"; +import { notifySettingsSaved, useSettingsSaved } from "@/components/settings-signal"; +import { useModelCatalog } from "@/components/model-catalog"; +export { useModelCatalog } from "@/components/model-catalog"; import { defaultOrderLevel, populateLevel, THINKING_LABEL } from "@/components/thinking-default"; -/** GET /api/models is cached per page load for instant menu opens, but - * the federation is LIVE per call — keys saved mid-session widen the - * catalog immediately server-side. ModelControl therefore re-fetches on - * every model-menu open (stale-while-revalidate: the cached list shows - * instantly, the fresh one swaps in while the menu is open). */ -let catalogCache: ModelCatalogResponse | undefined; -let catalogInflight: Promise | undefined; -let catalogGeneration = 0; -let stopCatalogSignals: (() => void) | undefined; -const catalogReaders = new Set<() => void>(); -const publishCatalog = () => { for (const reader of catalogReaders) reader(); }; -function fetchCatalog(revalidate = false): void { - if (revalidate) { catalogGeneration++; catalogInflight = undefined; } - if (catalogInflight) return; - const generation = catalogGeneration; - catalogInflight = fetch("/api/models") - .then((r) => { if (!r.ok) throw new Error(`catalog request failed: ${r.status}`); return r.json() as Promise; }) - .then((c) => { if (generation === catalogGeneration) { catalogCache = c; publishCatalog(); } }) - .catch(() => { /* existing unavailable/usage-only posture, no retry timer */ }) - .finally(() => { if (generation === catalogGeneration) catalogInflight = undefined; }); -} -export function useModelCatalog(): { catalog: ModelCatalogResponse | undefined; refresh: () => void } { - const [catalog, setCatalog] = useState(catalogCache); - useEffect(() => { - const first = catalogReaders.size === 0; - const read = () => setCatalog(catalogCache); - catalogReaders.add(read); - if (first) { - stopCatalogSignals = onSettingsSaved((section) => { - if (section === "keys") { - catalogGeneration++; catalogInflight = undefined; catalogCache = undefined; - publishCatalog(); fetchCatalog(); - } else if (section === "models") fetchCatalog(true); - }); - fetchCatalog(true); - } else fetchCatalog(); - read(); - return () => { catalogReaders.delete(read); if (!catalogReaders.size) { stopCatalogSignals?.(); stopCatalogSignals = undefined; } }; - }, []); - const refresh = useCallback(() => fetchCatalog(true), []); - return { catalog, refresh }; -} - /** provider:id → contextWindow over the cached catalog — the client-side * twin of the server's catalogContextLimits join (context-meter * amendment, owner 2026-09-23: pi's live catalog is the ONLY limit @@ -315,10 +274,7 @@ export function ModelControl({ chatId, onModelChange }: { chatId?: string; onMod return (
diff --git a/components/settings/agent-section.tsx b/components/settings/agent-section.tsx index bd7c810..7c79b64 100644 --- a/components/settings/agent-section.tsx +++ b/components/settings/agent-section.tsx @@ -55,6 +55,8 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { MenuShell } from "@/components/menu-shell"; import { useSettingsSaved } from "@/components/settings-signal"; import { defaultOrderLevel, populateLevel } from "@/components/thinking-default"; +import { useModelCatalog } from "@/components/model-catalog"; +import { Hint } from "@/components/ui/hint"; import type React from "react"; import { COMPACTION_THRESHOLD_FLOOR, @@ -63,7 +65,6 @@ import { type AgentSettings, type CompactionSettings, type GraphSettings, - type ModelCatalogEntry, type ModelSlotName, type ModelsSettings, } from "@/src/core"; @@ -101,15 +102,6 @@ interface SegmentInfo { shared: boolean; } -/** The GET /api/models response fields this section consumes. */ -interface ModelsBody { - models: ModelCatalogEntry[]; - /** The three tiers' RESOLVED pins (BYOK: NULL per unset tier — no - * factory floor) — machinery only (the thinking list fallback); the - * rows' displays read the record override, never a phantom pin. */ - slots: Record; -} - /** Compose: one tool toggled, the rest of the section carried verbatim. * Pure — exported for tests. */ export function composeTool(agent: AgentSettings | undefined, id: string, on: boolean): AgentSettings { @@ -234,8 +226,10 @@ function MenuTrigger({ onClick, widthClass, disabled, + hint, }: { value: string; + hint?: string; /** What the trigger shows when there is no override ("Default"). */ emptyLabel: string; ariaLabel: string; @@ -250,7 +244,7 @@ function MenuTrigger({ /** The no-key search state grays the whole row; the trigger follows. */ disabled?: boolean; }) { - return ( + const button = ( ); + return hint ? {button} : button; } /* The section's dropdowns all mount the shared MenuShell (fix wave chunk @@ -303,10 +298,9 @@ export function AgentSection({ /** Writes the whole compaction section (replace-PUT discipline). */ onCompactionChange: (next: CompactionSettings) => void; }) { - const [catalog, setCatalog] = useState(null); - /** The three tiers' resolved pins — GET /api/models `slots`. The rows' - * Default hints and thinking lists resolve from it. */ - const [slots, setSlots] = useState(null); + const { catalog: body, failed: catalogFailed, refresh: refreshCatalog } = useModelCatalog(); + const catalog = body?.models; + const slots = body?.slots; /** The live search state (/api/search) — the row's reason line. */ const [search, setSearch] = useState<{ available: boolean; engine: string | null; reason: string } | null>(null); /** Which search engines have a key (/api/keys) — the no-keys-anywhere @@ -341,28 +335,9 @@ export function AgentSection({ .catch(() => {}); }, []); - // The catalog (federated: keyed providers only), the resolved slots, - // the prompt segments, and the search engines' key presence — one - // fetch each per section mount. The models read ALSO re-runs on the - // cross-surface signal (fix wave: another surface's write re-reads - // into the slot rows live). - const modelsRequest = useRef(0); - const modelsMounted = useRef(false); - const readModels = useCallback((connectionChanged = false) => { - const request = ++modelsRequest.current; - if (connectionChanged) setCatalog(null); - fetch("/api/models") - .then((r) => { if (!r.ok) throw new Error("Catalog unavailable"); return r.json(); }) - .then((body: ModelsBody) => { - if (!modelsMounted.current || request !== modelsRequest.current) return; - setCatalog(body.models); - setSlots(body.slots); - }) - .catch(() => { if (modelsMounted.current && request === modelsRequest.current) setCatalog(null); }); - }, []); - + // The shared catalog seeds the first frame and refreshes per mount and + // keys/models signal. Settings props still own saved selections/levels. useSettingsSaved((section) => { - if (section === "models" || section === "keys") readModels(section === "keys"); // THE state-echo root cause: the prompt switches write through the // agent section, but this view (the toggles' truth) never re-read — // the settings-saved law (every reader re-reads) was missing this @@ -371,8 +346,6 @@ export function AgentSection({ }); useEffect(() => { - modelsMounted.current = true; - readModels(); fetch("/api/settings/prompts") .then((r) => r.json()) .then((body: { prober: SegmentInfo[]; teacher: SegmentInfo[] }) => setPrompts(body)) @@ -388,7 +361,6 @@ export function AgentSection({ setEngineKeys(present); }) .catch(() => {}); - return () => { modelsMounted.current = false; modelsRequest.current++; }; }, []); // The reason line re-reads /api/search AFTER an engine write lands — @@ -615,6 +587,7 @@ export function AgentSection({ return ( <> + {catalogFailed &&

Model catalog could not be loaded. Check the connection and retry.

} {MODEL_SLOTS.map((slot) => { const override = models?.[slot]; const modelId = override?.model?.id ?? ""; @@ -652,6 +625,7 @@ export function AgentSection({ {slot !== "strong" && <>} p.envVar === id); + if (matches?.some((p) => p.id === "opencode") && matches.some((p) => p.id === "opencode-go")) return "OpenCode (Zen / Go)"; + return matches?.[0]?.label; +} + /** The minimal status line, e.g. "…ab12 · 64 chars" (" · env" marks an * environment-owned value — the row itself is read-only then). Pure. */ export function keyStatusLine(status: AuthKeyStatus): string { @@ -417,7 +425,7 @@ export function KeysSection({ {providerRows.map((status) => ( p.envVar === status.id)?.label ?? status.info?.label ?? status.id} + label={providerKeyLabel(status.id, providers) ?? status.info?.label ?? status.id} docsUrl={status.info?.docsUrl} status={status} expanded={openRow === status.id} diff --git a/components/setup-dialog.tsx b/components/setup-dialog.tsx index 461e507..641898f 100644 --- a/components/setup-dialog.tsx +++ b/components/setup-dialog.tsx @@ -13,7 +13,8 @@ * STAGED BY DEPENDENCY (owner, 2026-09-20: "first keys, then models — * there's no point presenting the model picker before the user has set a * key because there are no models to pick then"): - * Stage 1 — no keyed provider: a SELF-CONTAINED first-key screen + * Unknown catalog — neutral loading (or error/Retry), never a key screen. + * Stage 1 — no keyed provider or saved selection: a SELF-CONTAINED first-key screen * (zero-keys wave, owner 2026-09-25): provider picker + one key * input + save via PUT /api/keys/:id. The dialog STAYS MOUNTED — * saving dispatches the "keys" signal, the fetch effect re-runs on @@ -23,6 +24,8 @@ * Stage 2 — Strong → Cheap → Vision pickers over the FEDERATED catalog * (keyed providers only), the vision slot filtered to image-capable * models, in the app's shared dropdown language (ModelMenuList). + * Saved disconnected selections stay visible; Add provider opens the + * connection body when no provider remains. Refreshes preserve edits. * All three required (owner: no skip, no defaults); Save PUTs the * models section and reports onDone(models). * @@ -41,6 +44,7 @@ import { prettyModel } from "@/components/usage-format"; import { ModelMenuList } from "@/components/settings/model-menu"; import { MenuShell } from "@/components/menu-shell"; import { notifySettingsSaved, useSettingsSaved } from "@/components/settings-signal"; +import { useModelCatalog } from "@/components/model-catalog"; import { Hint } from "@/components/ui/hint"; import { type ProviderKeyInfo } from "@/components/settings/keys-section"; import { ProviderMenuList } from "@/components/settings/model-menu"; @@ -73,27 +77,26 @@ interface ModelsBody { * (owner's dependency ordering). Exported for tests. */ export function setupStage( providers: { id: string; keyPresent: boolean }[] | null, -): "keys" | "pickers" { - if (providers === null) return "keys"; // fetch failed → the honest fallback +): "loading" | "keys" | "pickers" { + if (providers === null) return "loading"; return providers.some((p) => p.keyPresent) ? "pickers" : "keys"; } -/** Prefill (owner, BYOK wave 2026-09-22): each pinned tier opens with its - * pick — matched in the FEDERATED catalog (a lapsed key's pin does not - * prefill); unpinned tiers stay empty and carry the "needed" mark. - * First-run = nothing pinned → all three empty + marked. Exported pure - * for tests. */ +type ModelSelection = Pick; + +/** Saved identity is independent of selectable metadata. A disconnected + * pin still prefills; only the menu is restricted to the live catalog. */ export function prefillFromSlots( models: ModelCatalogEntry[], slots: Record | undefined, -): Partial> { - const pre: Partial> = {}; +): Partial> { + const pre: Partial> = {}; for (const slot of SLOT_ORDER) { const pin = slots?.[slot]; const entry = pin?.model ? models.find((m) => m.provider === pin.model!.provider && m.id === pin.model!.id) : undefined; - if (entry) pre[slot] = entry; + if (pin?.model) pre[slot] = entry ?? pin.model; } return pre; } @@ -217,24 +220,39 @@ export function SetupKeysBody({ ); } -export function SetupDialog({ - open, - onClose, - /** Saved: the models section landed; the caller marks configured (the - * draft stays put — save never sends). */ - onDone, -}: { +function prefillLevels(body: ModelsBody | undefined): Partial> { + const levels: Partial> = {}; + for (const slot of SLOT_ORDER) { + const pin = body?.slots?.[slot]; + const entry = body?.models.find((m) => m.provider === pin?.model.provider && m.id === pin.model.id); + if (pin?.thinkingLevel !== undefined) levels[slot] = pin.thinkingLevel; + else if (entry) levels[slot] = prefillLevelFromPin(entry, pin); + } + return levels; +} + +interface SetupDialogProps { open: boolean; onClose: () => void; + /** Saving never sends the caller's draft. */ onDone: (models: ModelsSettings) => void; -}) { +} + +export function SetupDialog(props: SetupDialogProps) { + // An open owns its draft. Mount-time cache seeding avoids a key-screen + // first frame, and reopening cannot reuse a previous open's edits. + return props.open ? : null; +} + +function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { const backdropDismiss = useBackdropDismiss(onClose); - const [body, setBody] = useState(null); - const [failed, setFailed] = useState(false); - const [picked, setPicked] = useState>>({}); - /** Per-slot thinking level (chunk 2): populated from the ordering on - * every fresh model pick, editable — an explicit pick always wins. */ - const [levels, setLevels] = useState>>({}); + const { catalog, failed, refresh } = useModelCatalog(); + const [lastBody, setLastBody] = useState(catalog); + const body = catalog ?? lastBody; + const [picked, setPicked] = useState(() => prefillFromSlots(catalog?.models ?? [], catalog?.slots)); + const [levels, setLevels] = useState(() => prefillLevels(catalog)); + const editedSlots = useRef(new Set()); + const [showKeys, setShowKeys] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [openMenu, setOpenMenu] = useState<{ slot: ModelSlotName; kind: "model" | "thinking" } | null>(null); @@ -249,9 +267,8 @@ export function SetupDialog({ const [keyError, setKeyError] = useState(null); const [keyMenuOpen, setKeyMenuOpen] = useState(false); // Stage 1's provider-picker trigger wrapper — the shared shell's anchor. - // Declared with the other hooks: the early `if (!open) return null` sits - // below them all. (Stage 2's slot menus anchor by anchorTestid instead — - // see the slot MenuShell below for why a ref cannot work there.) + // Stage 2's slot menus anchor by anchorTestid instead — see the slot + // MenuShell below for why a freshly attached ref cannot work there. const keyTriggerRef = useRef(null); // The keys signal (zero-keys wave): keys-section saves/deletes AND @@ -263,50 +280,44 @@ export function SetupDialog({ if (section === "keys") setKeysTick((t) => t + 1); }); - // Fresh /api/models per open AND per keys change — a key saved a - // moment ago (stage 1's own screen) widens the pickers immediately. useEffect(() => { - if (!open) return; let cancelled = false; - setBody(null); - setPicked({}); - setLevels({}); - setError(null); fetch("/api/keys") .then((r) => r.json()) .then((b: { providers?: ProviderKeyInfo[] }) => { if (!cancelled) setKeyProviders(b.providers ?? []); }) .catch(() => { if (!cancelled) setKeyProviders([]); }); - fetch("/api/models") - .then((r) => { if (!r.ok) throw new Error("Catalog unavailable"); return r.json(); }) - .then((b: ModelsBody) => { - if (cancelled) return; - setBody(b); - setFailed(false); - // Prefill (owner, 2026-09-22): each pinned tier opens with its - // pick (matched in the FEDERATED catalog — a lapsed key's pin - // does not prefill); unpinned tiers stay empty and carry the - // "needed" mark. First-run = nothing pinned → all three marked. - const pre = prefillFromSlots(b.models, b.slots); - setPicked(pre); - // Levels prefill with the pins (chunk 2): stored level if still - // supported, else the ordering's level for the pinned model. - const preLevels: Partial> = {}; - for (const slot of SLOT_ORDER) { - const entry = pre[slot]; - if (entry) preLevels[slot] = prefillLevelFromPin(entry, b.slots?.[slot]); - } - setLevels(preLevels); - }) - .catch(() => { if (!cancelled) setFailed(true); }); return () => { cancelled = true; }; - }, [open, keysTick]); + }, [keysTick]); + + useEffect(() => { + if (!catalog) return; + setLastBody(catalog); + const pre = prefillFromSlots(catalog.models, catalog.slots); + const preLevels = prefillLevels(catalog); + // Fresh metadata is always used, but a response cannot reset an + // edited slot, including a level chosen while the request was pending. + setPicked((prev) => { + const next = { ...prev }; + for (const slot of SLOT_ORDER) if (!editedSlots.current.has(slot)) { + if (pre[slot]) next[slot] = pre[slot]; else delete next[slot]; + } + return next; + }); + setLevels((prev) => { + const next = { ...prev }; + for (const slot of SLOT_ORDER) if (!editedSlots.current.has(slot)) { + if (preLevels[slot] !== undefined) next[slot] = preLevels[slot]; else delete next[slot]; + } + return next; + }); + if (catalog.providers.some((p) => p.keyPresent)) setShowKeys(false); + }, [catalog]); // Esc + outside-click — layered dismissal, closable WITHOUT // configuring (browsing stays possible; the gate is the send // interception, not this dialog). An open slot menu eats Esc first // (and any click outside it); only a bare Esc closes the dialog. useEffect(() => { - if (!open) return; const onKey = (e: KeyboardEvent) => { if (e.key !== "Escape") return; // THE ONE-ESC LAW (owner, keyboard round 2026-10-06): claim at @@ -341,11 +352,10 @@ export function SetupDialog({ document.removeEventListener("keydown", onKey); document.removeEventListener("pointerdown", onPointerDown); }; - }, [open, onClose, openMenu, keyMenuOpen]); + }, [onClose, openMenu, keyMenuOpen]); - if (!open) return null; - - const stage = failed ? "keys" : setupStage(body ? body.providers : null); + const hasSelections = Object.keys(picked).length > 0 || SLOT_ORDER.some((slot) => body?.slots?.[slot]?.model); + const stage = hasSelections ? "pickers" : setupStage(body ? body.providers : null); const allPicked = SLOT_ORDER.every((s) => picked[s]); /** Stage 1's save: PUT the pasted key, dispatch the keys signal, and @@ -398,6 +408,20 @@ export function SetupDialog({ onDone(models); } + const keysBody = setKeyMenuOpen((v) => !v)} + onPick={(p) => { setKeyProvider(p); setKeyMenuOpen(false); setKeyError(null); }} + onValue={setKeyValue} + onSave={() => void saveKey()} + />; + return createPortal(
- {stage === "keys" ? ( -
- {failed ? ( -

- Model catalog could not be loaded. Check the connection and retry. -

- ) : ( - setKeyMenuOpen((v) => !v)} - onPick={(p) => { - setKeyProvider(p); - setKeyMenuOpen(false); - setKeyError(null); - }} - onValue={setKeyValue} - onSave={() => void saveKey()} - /> - )} -
+ {failed && ( +

+ Model catalog could not be loaded. Check the connection and retry. +

+ )} + {stage === "loading" ? ( + !failed &&

Loading models…

+ ) : stage === "keys" ? ( +
{keysBody}
) : (

Pick the model for each tier. All three are required before chats can start.

+ {!body?.providers.some((p) => p.keyPresent) && <> + + {showKeys && keysBody} + } {SLOT_ORDER.map((slot) => { const entry = picked[slot]; const level = levels[slot]; - const levelsForEntry = entry?.supportedLevels ?? []; - const catalog = - slot === "vision" ? body?.models.filter((m) => m.input.includes("image")) : body?.models; + const metadata = catalog?.models.find((m) => m.id === entry?.id && m.provider === entry.provider); + // Unavailable metadata cannot erase or clamp a saved preference. + const levelsForEntry = metadata?.supportedLevels ?? (entry ? Object.keys(THINKING_LABEL) : []); + const available = catalog?.models ?? (failed ? [] : undefined); + const slotCatalog = slot === "vision" ? available?.filter((m) => m.input.includes("image")) : available; return (
@@ -481,6 +494,7 @@ export function SetupDialog({ {slot !== "strong" && <>}
+ + {/* Anchor by TESTID, not a ref (the settings-section pattern): React attaches a host element's ref AFTER its descendants' layout effects run, so a ref freshly attached in this same @@ -512,10 +527,11 @@ export function SetupDialog({ menuClass="flex w-[22rem] flex-col rounded-xl border border-border bg-card p-2 text-xs shadow-xl" > { + editedSlots.current.add(slot); setPicked((prev) => ({ ...prev, [slot]: m })); // Auto-populate (chunk 2, owner: "populate // everywhere"): no explicit level → the @@ -527,7 +543,7 @@ export function SetupDialog({ })); setOpenMenu(null); }} - footer={catalog && catalog.length === 0 ? "No image-capable models. Add a provider key with access to a vision model." : undefined} + footer={slotCatalog && slotCatalog.length === 0 && slot === "vision" ? "No image-capable models. Add a provider key with access to a vision model." : undefined} /> )} @@ -563,6 +579,7 @@ export function SetupDialog({ levels={levelsForEntry.map(String)} effectiveLevel={level} onPick={(lv) => { + editedSlots.current.add(slot); setLevels((prev) => ({ ...prev, [slot]: lv as ThinkingLevel })); setOpenMenu(null); }} diff --git a/components/usage-format.ts b/components/usage-format.ts index 3a400cd..2bd7d60 100644 --- a/components/usage-format.ts +++ b/components/usage-format.ts @@ -29,25 +29,11 @@ export function splitQualified(id: string): { provider: string | null; id: strin return { provider: id.slice(0, colon), id: id.slice(colon + 1) }; } -/** - * "z-ai/glm-5.3-flash" → "glm-5.3-flash" — vendor prefix dropped, the - * name KEPT EXACTLY as its provider writes it (dashes stay, no - * auto-caps — owner rounds 4-5, 2026-10-05); a trailing all-digit - * segment (OpenRouter's date suffix, "qwen3.8-max-0902") is dropped. - * Quiet-chip friendly. - * - * With `qualified` (usage stamps are "provider:id", model-slots wave): - * the provider rides as a quiet suffix — "glm-5.3-flash · openrouter" — - * because with federation the same model via two providers is genuinely - * different bookkeeping. Bare catalog ids are unchanged. - */ +/** Model-only labels omit the vendor path, never versions, dates or variants. + * Only usage callers pass `qualified`: a bare catalog id's colon belongs to + * the model (e.g. :free), not a provider. Usage retains its provider suffix. */ export function prettyModel(id: string, qualified = false): string { - const { provider, id: bare } = splitQualified(id); + const { provider, id: bare } = qualified ? splitQualified(id) : { provider: null, id }; const short = bare.includes("/") ? bare.slice(bare.lastIndexOf("/") + 1) : bare; - const parts = short.split("-"); - const trimmed = /^\d+$/.test(parts[parts.length - 1] ?? "") ? parts.slice(0, -1) : parts; - // No auto-capitalization, dashes KEPT (owner rounds 4-5, 2026-10-05): - // model names render exactly as their providers write them. - const pretty = trimmed.join("-"); - return qualified && provider ? `${pretty} · ${provider}` : pretty; + return provider ? `${short} · ${provider}` : short; } \ No newline at end of file diff --git a/tests/byok-refusal.test.ts b/tests/byok-refusal.test.ts index fab7db2..7627b8f 100644 --- a/tests/byok-refusal.test.ts +++ b/tests/byok-refusal.test.ts @@ -123,9 +123,9 @@ describe("prefillFromSlots — the SetupDialog prefill (BYOK wave)", () => { assert.deepEqual(pre, {}); }); - it("a pin whose model left the federated catalog does not prefill (lapsed key)", () => { + it("a disconnected saved identity still prefills without selectable metadata", () => { const pre = prefillFromSlots(catalog, { strong: { model: { provider: "gone", id: "vanished" } }, cheap: null, vision: null }); - assert.deepEqual(pre, {}); + assert.deepEqual(pre, { strong: { provider: "gone", id: "vanished" } }); }); it("provider collisions never cross-pollute (same id, different provider)", () => { @@ -158,9 +158,9 @@ describe("prefillLevelFromPin — the setup dialog's level prefill (chunk 2)", ( }); }); -describe("setupStage — unchanged by the BYOK wave", () => { +describe("setupStage — unknown is not keyless", () => { it("keys first (owner ordering), pickers when a provider is keyed", () => { - assert.equal(setupStage(null), "keys"); + assert.equal(setupStage(null), "loading"); assert.equal(setupStage([{ id: "x", keyPresent: false }]), "keys"); assert.equal(setupStage([{ id: "x", keyPresent: true }]), "pickers"); }); diff --git a/tests/model-catalog-setup.test.tsx b/tests/model-catalog-setup.test.tsx new file mode 100644 index 0000000..43db670 --- /dev/null +++ b/tests/model-catalog-setup.test.tsx @@ -0,0 +1,176 @@ +import React, { act } from "react"; +(globalThis as Record).React = React; +import assert from "node:assert/strict"; +import { afterEach, test } from "node:test"; +import { createRoot, type Root } from "react-dom/client"; +import { parseHTML } from "linkedom"; +import { SetupDialog } from "../components/setup-dialog.tsx"; +import { AgentSection } from "../components/settings/agent-section.tsx"; +import { providerKeyLabel, classifyKeys } from "../components/settings/keys-section.tsx"; +import { useModelCatalog } from "../components/model-catalog.ts"; +import { notifySettingsSaved } from "../components/settings-signal.ts"; +import type { ModelCatalogEntry, ModelCatalogResponse, ModelsSettings } from "../src/core/index.ts"; + +const dom = parseHTML(""); +Object.assign(globalThis, { + window: dom.window, document: dom.document, Element: dom.window.Element, + HTMLElement: dom.window.HTMLElement, HTMLInputElement: dom.window.HTMLInputElement, + CustomEvent: dom.window.CustomEvent, IS_REACT_ACT_ENVIRONMENT: true, + requestAnimationFrame: (callback: FrameRequestCallback) => setTimeout(() => callback(performance.now()), 0), + cancelAnimationFrame: (id: number) => clearTimeout(id), +}); +Object.assign(dom.window, { innerWidth: 1280, innerHeight: 800 }); +dom.window.HTMLElement.prototype.scrollIntoView = () => {}; +const originalFetch = globalThis.fetch; +let root: Root | undefined; +let host: HTMLDivElement | undefined; +let requests: ReturnType>[] = []; +let writes: ModelsSettings[] = []; +let snapshot: ReturnType; +function deferred() { let resolve!: (value: T) => void; const promise = new Promise((done) => { resolve = done; }); return { resolve, promise }; } +const provider = { id: "fixture", envVar: "FIXTURE_KEY", label: "Fixture" }; +const a: ModelCatalogEntry = { id: "vendor/model:free", name: "Model free", provider: provider.id, input: ["text", "image"], reasoning: true, supportedLevels: ["off", "low", "high"], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 8192, maxTokens: 1024 }; +const b: ModelCatalogEntry = { ...a, id: "claude-opus-5-5", name: "Claude Opus 5.5", supportedLevels: ["off", "medium"] }; +function catalog(connected = true): ModelCatalogResponse { + return { + models: connected ? [a, b] : [], defaults: null, + rolePins: { researcher: null, demoBuilder: null, visualMaker: null, extractor: null, auditor: null }, + slots: { strong: { model: { provider: a.provider, id: a.id }, thinkingLevel: "low" }, cheap: { model: { provider: a.provider, id: a.id }, thinkingLevel: "high" }, vision: { model: { provider: a.provider, id: a.id }, thinkingLevel: "low" } }, + providers: [{ ...provider, keyPresent: connected }], + }; +} +function Reader() { snapshot = useModelCatalog(); return null; } +function setup() { return {}} onDone={() => {}} />; } +function installFetch() { + requests = []; writes = []; + globalThis.fetch = async (input, init) => { + const path = String(input); + if (path === "/api/models") { const request = deferred(); requests.push(request); return request.promise; } + if (path === "/api/settings/models") { writes.push(JSON.parse(String(init?.body))); return Response.json({ settings: { models: writes.at(-1) } }); } + if (path === "/api/keys") return Response.json({ providers: [provider], keys: [] }); + if (path === "/api/settings/prompts") return Response.json({ prober: [], teacher: [] }); + if (path === "/api/search") return Response.json({ available: false, engine: null, reason: "fixture" }); + throw new Error(`Unexpected request ${path}`); + }; +} +async function render(children: React.ReactNode) { + if (!root) { host = document.createElement("div"); document.body.append(host); root = createRoot(host); } + await act(async () => root!.render(children)); +} +async function answer(index: number, body: ModelCatalogResponse | null) { await act(async () => requests[index]!.resolve(body ? Response.json(body) : new Response("unavailable", { status: 503 }))); } +function element(testid: string) { const e = document.querySelector(`[data-testid="${testid}"]`); assert.ok(e, testid); return e; } +async function click(testid: string) { await act(async () => element(testid).click()); } +function levels() { return [...document.querySelectorAll('[data-testid^="thinking-option-"]')].map((e) => e.textContent); } +async function warm(body = catalog()) { await render(); await answer(0, body); } + +afterEach(async () => { + await act(async () => root?.unmount()); host?.remove(); root = undefined; host = undefined; + globalThis.fetch = originalFetch; +}); + +test("cold setup is loading, not keyless; failure/Retry does not invent a key stage", async () => { + installFetch(); await render(setup()); + assert.ok(element("setup-stage-loading")); + assert.equal(document.querySelector('[data-testid="setup-stage-keys"]'), null); + await answer(0, null); + assert.ok(element("setup-catalog-error")); + assert.equal(document.querySelector('[data-testid="setup-stage-keys"]'), null); + await click("setup-retry"); assert.ok(element("setup-stage-loading")); + const empty = catalog(false); empty.slots = { strong: null, cheap: null, vision: null }; + await answer(1, empty); assert.ok(element("setup-stage-keys")); +}); + +test("cached setup is immediately usable; fresh metadata/untouched slots update without wiping model or thinking edits; failed refresh still allows exact-ID Save", async () => { + installFetch(); await warm(); + await render(<>{setup()}); + assert.equal(requests.length, 2); + assert.ok(element("setup-stage-pickers")); + assert.equal(document.querySelector('[data-testid="setup-stage-keys"]'), null); + assert.equal(element("setup-trigger-strong").textContent, "model:free▾"); + await click("setup-trigger-strong"); await click("model-option-claude-opus-5-5"); + await click("setup-thinking-strong"); await click("thinking-option-medium"); + const fresh = catalog(); fresh.slots.cheap = { model: { provider: b.provider, id: b.id }, thinkingLevel: "off" }; + fresh.models[1] = { ...b, supportedLevels: ["off", "medium", "high"] }; + await answer(1, fresh); + assert.equal(element("setup-trigger-strong").textContent, "claude-opus-5-5▾"); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "medium▾"); + assert.equal(element("setup-trigger-cheap").textContent, "claude-opus-5-5▾"); + await click("setup-thinking-strong"); assert.deepEqual(levels(), ["off", "medium✓", "high"]); + await click("setup-thinking-strong"); + await act(async () => snapshot.refresh()); await answer(2, null); + assert.ok(element("setup-catalog-error")); assert.equal(element("setup-save").hasAttribute("disabled"), false); + await click("setup-save"); + assert.deepEqual(writes[0], { + strong: { model: { provider: b.provider, id: b.id }, thinkingLevel: "medium" }, + cheap: { model: { provider: b.provider, id: b.id }, thinkingLevel: "off" }, + vision: { model: { provider: a.provider, id: a.id }, thinkingLevel: "low" }, + }); +}); + +test("disconnect invalidates choices, not saved selections or thinking; all-disconnected setup offers Add provider", async () => { + installFetch(); await warm(); await render(<>{setup()}); await answer(1, catalog()); + await click("setup-trigger-strong"); + assert.ok(document.querySelector('[data-testid="model-option-claude-opus-5-5"]')); + await act(async () => notifySettingsSaved("keys")); + assert.equal(document.querySelector('[data-testid="model-option-claude-opus-5-5"]'), null, "stale choices disappear before the fresh request resolves"); + await answer(2, catalog(false)); + assert.equal(element("setup-trigger-strong").textContent, "model:free▾"); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "low▾"); + assert.ok(element("setup-add-provider")); await click("setup-add-provider"); assert.ok(element("setup-stage-keys")); + await click("setup-thinking-strong"); await click("thinking-option-xhigh"); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "xhigh▾"); + await click("setup-save"); assert.equal(writes[0]?.strong?.thinkingLevel, "xhigh"); assert.equal(writes[0]?.strong?.model?.id, a.id); +}); + +test("cold reconnect checks saved pins before choosing the key stage, and reopening gets a new draft", async () => { + installFetch(); await warm(); + await act(async () => notifySettingsSaved("keys")); + const stages: Array = []; + function Probe() { + const { catalog } = useModelCatalog(); + React.useLayoutEffect(() => { stages.push(document.querySelector('[data-testid^="setup-stage-"]')?.dataset.testid); }, [catalog]); + return null; + } + await render(<>{setup()}); + assert.ok(element("setup-stage-loading")); + await answer(1, catalog(false)); + assert.deepEqual(stages, ["setup-stage-loading", "setup-stage-pickers"]); + await click("setup-thinking-strong"); await click("thinking-option-xhigh"); + await render(); + await render(<>{setup()}); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "low▾", "a reopened draft uses saved state, not the abandoned edit"); + await answer(2, catalog(false)); +}); + +test("Agent opens with cached thinking metadata; refresh and disconnect never clear saved levels", async () => { + installFetch(); await warm(); + const models: ModelsSettings = { strong: { model: { provider: a.provider, id: a.id }, thinkingLevel: "low" } }; + let changed: ModelsSettings | undefined; + await render(<> {}} onModelsChange={(next) => { changed = next; }} onCompactionChange={() => {}} />); + await click("slot-thinking-strong"); assert.deepEqual(levels(), ["off", "low✓", "high"]); + assert.equal(element("slot-model-strong").textContent, "model:free▾"); + await answer(1, catalog()); + await act(async () => notifySettingsSaved("keys")); await answer(2, catalog(false)); + assert.equal(element("slot-thinking-strong").textContent, "low▾"); + await click("thinking-option-xhigh"); assert.equal(changed?.strong?.thinkingLevel, "xhigh"); assert.equal(changed?.strong?.model?.id, a.id); +}); + +test("shared refresh fences late failure as well as late success, and Retry clears a current error", async () => { + installFetch(); await warm(); + await act(async () => snapshot.refresh()); await act(async () => snapshot.refresh()); + const fresh = catalog(); fresh.models = [b]; + await answer(2, fresh); await answer(1, null); + assert.equal(snapshot.failed, false); assert.equal(snapshot.catalog?.models[0]?.id, b.id); + await act(async () => snapshot.refresh()); await answer(3, null); + assert.equal(snapshot.failed, true); assert.equal(snapshot.catalog?.models[0]?.id, b.id); + await act(async () => snapshot.refresh()); assert.equal(snapshot.failed, false); + await answer(4, fresh); +}); + +test("OpenCode's saved shared credential names both Pi providers without changing addable-provider semantics", () => { + const providers = [{ id: "opencode", envVar: "OPENCODE_API_KEY", label: "OpenCode Zen" }, { id: "opencode-go", envVar: "OPENCODE_API_KEY", label: "OpenCode Go" }]; + assert.equal(providerKeyLabel("OPENCODE_API_KEY", providers), "OpenCode (Zen / Go)"); + assert.equal(providerKeyLabel("OPENCODE_API_KEY", [...providers].reverse()), "OpenCode (Zen / Go)"); + assert.equal(providerKeyLabel("FIXTURE_KEY", [provider]), "Fixture"); + assert.equal(classifyKeys([{ id: "OPENCODE_API_KEY", info: null, present: true, masked: "…fake", source: "file", shadowedByEnv: false }], providers).addable.length, 0); +}); diff --git a/tests/render-stats-bar.test.tsx b/tests/render-stats-bar.test.tsx index 329f186..4f80fa0 100644 --- a/tests/render-stats-bar.test.tsx +++ b/tests/render-stats-bar.test.tsx @@ -89,9 +89,15 @@ describe("stats bar", () => { assert.match(empty, /data-testid="stats-context">\? { + it("prettyModel strips only the vendor path, preserving versions, dates and variants", () => { assert.equal(prettyModel("z-ai/glm-5.3-flash"), "glm-5.3-flash"); // dashes kept (owner round 5) - assert.equal(prettyModel("qwen/qwen3.8-max-0902"), "qwen3.8-max"); + assert.equal(prettyModel("qwen/qwen3.8-max-0902"), "qwen3.8-max-0902"); + assert.equal(prettyModel("google/gemma-4-27b-it:free"), "gemma-4-27b-it:free"); + assert.equal(prettyModel("anthropic/claude-opus-5:batch"), "claude-opus-5:batch"); + assert.equal(prettyModel("claude-opus-5"), "claude-opus-5"); + assert.equal(prettyModel("claude-opus-5-5"), "claude-opus-5-5"); + assert.equal(prettyModel("model:free"), "model:free"); + assert.equal(prettyModel("model:variant:free"), "model:variant:free"); }); it("provider-qualified stamps (usage contract, model-slots wave)", () => { @@ -99,8 +105,10 @@ describe("stats bar", () => { // the same model via two providers different bookkeeping assert.equal(prettyModel("openrouter:z-ai/glm-5.3-flash", true), "glm-5.3-flash · openrouter"); assert.equal(prettyModel("openai:gpt-5.2", true), "gpt-5.2 · openai"); - // unqualified on a qualified stamp: provider silently dropped (old shape) - assert.equal(prettyModel("openrouter:z-ai/glm-5.3-flash"), "glm-5.3-flash"); + assert.equal(prettyModel("openrouter:google/gemma-4-27b-it:free", true), "gemma-4-27b-it:free · openrouter"); + assert.equal(prettyModel("opencode:claude-opus-5-5", true), "claude-opus-5-5 · opencode"); + // Bare ids are never parsed as usage stamps, even with a colon. + assert.equal(prettyModel("model:free"), "model:free"); // qualified on a bare id: no suffix invented assert.equal(prettyModel("z-ai/glm-5.3-flash", true), "glm-5.3-flash"); }); diff --git a/tests/settings-pure.test.ts b/tests/settings-pure.test.ts index d629349..3c38f1d 100644 --- a/tests/settings-pure.test.ts +++ b/tests/settings-pure.test.ts @@ -320,7 +320,7 @@ describe("first-run gate — slotsConfigured + setupStage (model-slots wave)", ( }); it("setupStage: keys before models (owner's dependency ordering)", () => { - assert.equal(setupStage(null), "keys", "fetch failed → the honest fallback"); + assert.equal(setupStage(null), "loading", "unknown catalog must not claim keys are missing"); assert.equal(setupStage([]), "keys", "no providers = keys"); assert.equal(setupStage([{ id: "zai", keyPresent: false }]), "keys", "provider present but keyless"); assert.equal(setupStage([{ id: "zai", keyPresent: false }, { id: "openai", keyPresent: true }]), "pickers", "one keyed provider widens the pickers"); From 42814ba6a641c54b1e8ff4ce305734e3cf380f50 Mon Sep 17 00:00:00 2001 From: anikam13 Date: Mon, 26 Oct 2026 23:03:09 -0700 Subject: [PATCH 16/19] fix: budget dock height for answer controls and toolbar --- components/chat.tsx | 16 ++++++++++++++-- components/practice-dock.tsx | 2 +- components/question-dock.tsx | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/components/chat.tsx b/components/chat.tsx index f8866f1..10ef099 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -2026,11 +2026,23 @@ function DockOverlay({ if (!el) return; const measure = () => { setSbInset(Math.round((el.offsetWidth - el.clientWidth) / 2)); - setDockMaxH(Math.max(el.clientHeight - 32, 160)); + const block = blockRef.current; + const question = block?.querySelector("[data-dock-scroll-area]"); + // The height budget belongs to the whole dock, including answer + // controls, stats, and padding. Only the question may consume the + // remaining space; otherwise the overlay grows behind the header. + const chrome = block && question ? block.offsetHeight - question.offsetHeight : 0; + setDockMaxH(Math.max(el.clientHeight - 32 - chrome, 0)); }; measure(); + const observer = new ResizeObserver(measure); + observer.observe(el); + if (blockRef.current) observer.observe(blockRef.current); window.addEventListener("resize", measure); - return () => window.removeEventListener("resize", measure); + return () => { + observer.disconnect(); + window.removeEventListener("resize", measure); + }; }, [scrollRef]); // GROWTH MUTE (owner round, 2026-10-05 — the generation churn): during a diff --git a/components/practice-dock.tsx b/components/practice-dock.tsx index 4c53b59..3ade8f4 100644 --- a/components/practice-dock.tsx +++ b/components/practice-dock.tsx @@ -105,7 +105,7 @@ export function PracticeDock({ problemCallId, input, expanded, onExpand, onSubmi cards' label row — owner round, 2026-10-05) + the statement, markdown + LaTeX rendered, capped by the measured column (--dock-max-h; the old fixed 40vh cut long problems short). */} -
+
Problem
{input.statement} diff --git a/components/question-dock.tsx b/components/question-dock.tsx index 08f5d23..56b3298 100644 --- a/components/question-dock.tsx +++ b/components/question-dock.tsx @@ -564,7 +564,7 @@ function QuestionDockForm({ {/* Question area — scrolls internally if very tall; the bottom row stays put. Capped by the measured column (--dock-max-h; the old fixed 55dvh cut long questions short — owner round, 2026-10-05). */} -
+
{toolName === "probe" ? "Probe" From d30acd4fddc40514b7c2d463f694ae3f745814f1 Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Tue, 27 Oct 2026 15:27:08 +0200 Subject: [PATCH 17/19] fix: finish measured dock sizing and short-window scrolling --- components/chat.tsx | 125 +++++++----------- components/dock-height.ts | 157 ++++++++++++++++++++++ tests/dock-height.test.tsx | 259 +++++++++++++++++++++++++++++++++++++ 3 files changed, 462 insertions(+), 79 deletions(-) create mode 100644 components/dock-height.ts create mode 100644 tests/dock-height.test.tsx diff --git a/components/chat.tsx b/components/chat.tsx index 10ef099..cef946d 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -26,6 +26,7 @@ import { Composer } from "@/components/composer"; import { useEnterSubmits } from "@/components/enter-submits"; import { blurAfterPointerClick } from "@/components/focus-hygiene"; import { dockStateKey } from "@/components/dock-state-key"; +import { forwardDockWheel, useDockHeight } from "@/components/dock-height"; import { QueuedSteersBody, type QueuedSteerLine } from "@/components/queued-steers"; import { DictationBar, finishDictation, joinDictation, useDictation } from "@/components/dictation"; import { useFieldCycle } from "@/components/field-cycle"; @@ -1972,6 +1973,17 @@ function DockOverlay({ const revealedRef = useRef(revealed); revealedRef.current = revealed; + const blockRef = useRef(null); + const viewportRef = useRef(null); + const bodyRef = useRef(null); + const shellRef = useRef(null); + const contentRef = useRef(null); + const mode = pending + ? `question:${pending.toolCallId}` + : practice + ? `practice:${practice.toolCallId}` + : "chat"; + // The plan marginalia's flank width (owner, plan-placement round): from // the DOCK container's right edge to the block's right edge — the bare // diagram's canvas. Tracked LIVE by a ResizeObserver on the block @@ -2010,40 +2022,33 @@ function DockOverlay({ return () => ro.disconnect(); }, []); + // The reserve grows immediately, but never shrinks while a question or + // practice problem owns the dock: shrinking flow space could pin a nearby + // learner back to the bottom. Composer mode may give space back. + const reserveRef = useRef(0); + const offer = useCallback((blockH: number) => { + const next = blockH + GRADIENT_H; + if (next <= reserveRef.current + 0.5 && pendingRef.current) return; + // Stamp growth at the source, including child-only field resizes, so + // stick-to-bottom's catch-up is not mistaken for the learner leaving. + if (shouldStampGeometryMute(next, reserveRef.current, pendingRef.current)) geometryAtRef.current = Date.now(); + reserveRef.current = next; + onReserve(next); + }, [onReserve]); + // The overlay must not paint over the scroller's scrollbar (owner, scroll // round 2): the scroller reserves symmetric gutters (scrollbar-gutter: // stable both-edges), so inset the overlay's right edge by the gutter — // zero on macOS overlay scrollbars, the real width on classic platforms. - // The same measure owns the QUESTION/PROBLEM HEIGHT CAP (owner round, - // 2026-10-05): the old fixed fractions (55dvh / 40vh) cut long problems - // off absurdly short — the cap is now the AVAILABLE COLUMN (the - // scroller already starts below the top bar) minus a 2rem breathing - // gap, carried as --dock-max-h for the docks' scroll areas. + // Budget the whole solid dock, leaving only its question area flexible. + // The hook measures natural, visible content before the morph/reserve + // effect below. Short windows scroll the reading/question/answer body; + // the stats/model toolbar remains outside that overflow viewport. const [sbInset, setSbInset] = useState(0); - const [dockMaxH, setDockMaxH] = useState(null); - useEffect(() => { - const el = scrollRef.current; - if (!el) return; - const measure = () => { - setSbInset(Math.round((el.offsetWidth - el.clientWidth) / 2)); - const block = blockRef.current; - const question = block?.querySelector("[data-dock-scroll-area]"); - // The height budget belongs to the whole dock, including answer - // controls, stats, and padding. Only the question may consume the - // remaining space; otherwise the overlay grows behind the header. - const chrome = block && question ? block.offsetHeight - question.offsetHeight : 0; - setDockMaxH(Math.max(el.clientHeight - 32 - chrome, 0)); - }; - measure(); - const observer = new ResizeObserver(measure); - observer.observe(el); - if (blockRef.current) observer.observe(blockRef.current); - window.addEventListener("resize", measure); - return () => { - observer.disconnect(); - window.removeEventListener("resize", measure); - }; - }, [scrollRef]); + useDockHeight({ + scrollRef, blockRef, viewportRef, bodyRef, shellRef, contentRef, + expanded, enabled: !!pending || !!practice, mode, onScrollbarInset: setSbInset, onHeight: offer, + }); // GROWTH MUTE (owner round, 2026-10-05 — the generation churn): during a // live turn the transcript content grows and stick-to-bottom chases it; @@ -2079,48 +2084,11 @@ function DockOverlay({ const root = blockRef.current; const el = scrollRef.current; if (!root || !el) return; - const canScroll = (n: HTMLElement, dir: number) => { - if (n.scrollHeight - n.clientHeight < 2) return false; // not scrollable - return dir < 0 ? n.scrollTop > 0 : n.scrollTop < n.scrollHeight - n.clientHeight - 0.5; - }; - const onWheel = (e: WheelEvent) => { - if (e.deltaY === 0) return; - for (let n = e.target as HTMLElement | null; n && n !== root; n = n.parentElement) { - if (n instanceof HTMLElement && canScroll(n, e.deltaY)) return; // native - } - e.preventDefault(); - el.scrollTop += e.deltaY * (e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? el.clientHeight : 1); - }; + const onWheel = (e: WheelEvent) => forwardDockWheel(e, root, el); root.addEventListener("wheel", onWheel, { passive: false }); return () => root.removeEventListener("wheel", onWheel); }, [scrollRef]); - // ── The reserve: the spacer's height, reported upward ── - // Grows immediately (typing, new variants). NEVER drops while a question - // is open — a shrinking reserved tail is scroll geometry and the browser - // clamp would pin a nearby learner to the bottom (the old oscillation). - // In chat mode drops apply (the composer's typing shrink is today's - // harmless in-flow behavior; nothing auto-expands there). - const reserveRef = useRef(0); - const blockRef = useRef(null); - const offer = (blockH: number) => { - const next = blockH + GRADIENT_H; - if (next <= reserveRef.current + 0.5 && pendingRef.current) return; // fold immunity - // Reserve GROWTH is the phantom-scroll SOURCE (flicker round, - // 2026-09-26): the spacer's height lands in this commit, the smooth - // catch-up lags it by the growth amount, and the position machine - // used to read that lag as the learner leaving — form → bar → form - // (the dictation flush, reproduced d≈152 > COLLAPSE_DIST). Stamp the - // mute right where the spacer grows: it covers the scroll events of - // THIS growth no matter how long after the field stretched (the - // field grows on its own render; the offer lands on the NEXT - // DockOverlay render — ~1s apart live). Shrinks never stamp (the - // fold's own give-back must not self-mute the re-expand). - if (shouldStampGeometryMute(next, reserveRef.current, pendingRef.current)) geometryAtRef.current = Date.now(); - reserveRef.current = next; - onReserve(next); - }; - // Explicit expand (bar click): the smooth scroll to the bottom takes // hundreds of ms, during which the position is still legitimately inside // the collapse zone — the machine would instantly re-collapse the dock @@ -2222,11 +2190,6 @@ function DockOverlay({ if (b) offer(b.getBoundingClientRect().height); }; - const mode = pending - ? `question:${pending.toolCallId}` - : practice - ? `practice:${practice.toolCallId}` - : "chat"; // The opening variant of a session appears instantly — the fade/rise is // reserved for mid-session transitions. const initialModeRef = useRef(mode); @@ -2234,11 +2197,9 @@ function DockOverlay({ // Height morph: animate the shell from its previous height to its new one // whenever the variant (chat ↔ question ↔ collapsed bar) changes. Typing // growth keeps the same stateKey and is NOT animated. - const shellRef = useRef(null); // The fold's CONTENT fade (owner round, 2026-10-05): the shell height // morphs, but the bar↔form content used to hard-swap. A WAAPI fade/rise // on the toggle — no remount, so the question form's state survives. - const contentRef = useRef(null); const prevExpandedForFadeRef = useRef(expanded); useIsoLayoutEffect(() => { if (prevExpandedForFadeRef.current === expanded) return; @@ -2286,8 +2247,10 @@ function DockOverlay({ return (
-
+
+
+
{setupCta} {/* The queued-steer indicator (owner spec 2026-10-03): quiet one-liners above the shell - right-aligned to where its corner @@ -2316,11 +2279,13 @@ function DockOverlay({ /> )}
+ {/* Include descendant margins (notably practice thinking's + bottom margin) in the natural content measurement. */}
+ ref={contentRef} + key={mode} + className={mode === initialModeRef.current ? "flow-root" : "flow-root dock-variant"} + > {pending ? (
+
+
{/* The stats row: beneath the composer/question shell, not flush to the screen bottom — static height, outside the morphing shell so it never joins a height animation. */} diff --git a/components/dock-height.ts b/components/dock-height.ts new file mode 100644 index 0000000..dd6f2ac --- /dev/null +++ b/components/dock-height.ts @@ -0,0 +1,157 @@ +"use client"; + +import { useCallback, useEffect, useLayoutEffect, type RefObject } from "react"; + +const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect; + +export interface DockHeightMeasurements { + columnHeight: number; + /** Stats/toolbar and block padding, outside the scrolling viewport. */ + outsideHeight: number; + /** Everything in the viewport except the animated shell. */ + beforeShellHeight: number; + shellBorderHeight: number; + /** Natural content height, not the shell's animated/clamped height. */ + contentHeight: number; + /** Null when collapsed, in chat mode, or showing malformed input. */ + questionHeight: number | null; + questionPaddingHeight: number; +} + +export function dockHeightBudget(m: DockHeightMeasurements) { + const available = Math.max(m.columnHeight - 32, 0); + const viewportMaxHeight = Math.max(available - m.outsideHeight, 0); + const shellMaxHeight = Math.max(viewportMaxHeight - m.beforeShellHeight, 0); + const naturalHeight = m.beforeShellHeight + m.shellBorderHeight + m.contentHeight; + const questionMaxHeight = m.questionHeight === null + ? null + : Math.max(viewportMaxHeight - (naturalHeight - m.questionHeight), 0); + // A cap smaller than the question's padding leaves no content viewport. + // Deliberately uncap the question and scroll the whole content instead; + // do not confuse a measured zero with an unmeasured CSS fallback. + const scrollWholeContent = questionMaxHeight === null + ? naturalHeight > viewportMaxHeight + : questionMaxHeight <= m.questionPaddingHeight; + return { viewportMaxHeight, shellMaxHeight, questionMaxHeight, scrollWholeContent }; +} + +interface DockHeightElements { + column: HTMLElement; + block: HTMLElement; + viewport: HTMLElement; + body: HTMLElement; + shell: HTMLElement; + content: HTMLElement; +} + +const pixels = (value: string) => Number.parseFloat(value) || 0; + +/** Read natural geometry before writing any constraints. Cancel the shell's + * animated height out of both ancestor measurements, then substitute its + * natural content and borders. This stays valid during a WAAPI height morph + * and when the outer viewport is already capped. */ +export function syncDockHeight(elements: DockHeightElements, expanded: boolean) { + const { column, block, viewport, body, shell, content } = elements; + // QuestionDock keeps a laid-out form in an invisible h-0 wrapper; + // PracticeDock removes its form. Neither hidden form is a height input. + const question = expanded ? content.querySelector("[data-dock-scroll-area]") : null; + const height = (el: HTMLElement) => el.getBoundingClientRect().height; + const shellStyle = getComputedStyle(shell); + const questionStyle = question ? getComputedStyle(question) : null; + const budget = dockHeightBudget({ + columnHeight: column.clientHeight, + outsideHeight: Math.max(height(block) - height(viewport), 0), + beforeShellHeight: Math.max(height(body) - height(shell), 0), + shellBorderHeight: pixels(shellStyle.borderTopWidth) + pixels(shellStyle.borderBottomWidth), + contentHeight: height(content), + questionHeight: question ? height(question) : null, + questionPaddingHeight: questionStyle + ? pixels(questionStyle.paddingTop) + pixels(questionStyle.paddingBottom) + : 0, + }); + if (budget.questionMaxHeight === null) block.style.removeProperty("--dock-max-h"); + else block.style.setProperty("--dock-max-h", budget.scrollWholeContent ? "none" : `${budget.questionMaxHeight}px`); + viewport.style.maxHeight = `${budget.viewportMaxHeight}px`; + // Overflow is visible in normal layouts: keep the shell's shadow intact. + // The stats/model toolbar is a sibling, never inside this overflow box. + viewport.style.overflowY = budget.scrollWholeContent ? "auto" : "visible"; + // Clamp animated starting heights too, so a previous tall variant cannot + // paint above the column while morphing into a constrained layout. + shell.style.maxHeight = budget.scrollWholeContent ? "none" : `${budget.shellMaxHeight}px`; + return budget; +} + +interface DockHeightRefs { + scrollRef: RefObject; + blockRef: RefObject; + viewportRef: RefObject; + bodyRef: RefObject; + shellRef: RefObject; + contentRef: RefObject; +} + +export function useDockHeight({ + scrollRef, blockRef, viewportRef, bodyRef, shellRef, contentRef, + expanded, enabled, mode, onScrollbarInset, onHeight, +}: DockHeightRefs & { + expanded: boolean; + enabled: boolean; + /** Reconnect observers when the keyed content node/scroll area changes. */ + mode: string; + onScrollbarInset: (inset: number) => void; + /** Settled visible block height, fed through the existing reserve policy. */ + onHeight: (height: number) => void; +}) { + const measure = useCallback(() => { + const column = scrollRef.current; + const block = blockRef.current; + const viewport = viewportRef.current; + const body = bodyRef.current; + const shell = shellRef.current; + const content = contentRef.current; + if (!column || !block || !viewport || !body || !shell || !content) return; + onScrollbarInset(Math.round((column.offsetWidth - column.clientWidth) / 2)); + if (enabled) syncDockHeight({ column, block, viewport, body, shell, content }, expanded); + else { + block.style.removeProperty("--dock-max-h"); + viewport.style.removeProperty("max-height"); + viewport.style.removeProperty("overflow-y"); + shell.style.removeProperty("max-height"); + } + // ResizeObserver runs even when only a child field changed and React + // did not rerender DockOverlay. Keep its transcript reserve current, + // but never report a mid-morph height. + if (shell.getAnimations().length === 0) onHeight(block.getBoundingClientRect().height); + }, [scrollRef, blockRef, viewportRef, bodyRef, shellRef, contentRef, expanded, enabled, onScrollbarInset, onHeight]); + + // Imperative constraints land before the overlay's later layout effect + // measures its morph target/reserve, and before paint (including reveal). + useIsoLayoutEffect(measure); + useIsoLayoutEffect(() => { + const observer = new ResizeObserver(measure); + for (const el of [scrollRef.current, blockRef.current, viewportRef.current, bodyRef.current, contentRef.current]) { + if (el) observer.observe(el); + } + const question = expanded ? contentRef.current?.querySelector("[data-dock-scroll-area]") : null; + if (question) observer.observe(question); + window.addEventListener("resize", measure); + return () => { + observer.disconnect(); + window.removeEventListener("resize", measure); + }; + }, [measure, mode, expanded, scrollRef, blockRef, viewportRef, bodyRef, contentRef]); +} + +/** Overlay and transcript are siblings. Keep native scrolling in an actual + * scroll container (including the short-window viewport), then forward only + * at its edge. overflow:hidden/visible boxes cannot consume the wheel. */ +export function forwardDockWheel(event: WheelEvent, root: HTMLElement, transcript: HTMLElement) { + if (event.deltaY === 0) return; + for (let n = event.target as HTMLElement | null; n && n !== root; n = n.parentElement) { + if (!(n instanceof HTMLElement) || !/^(auto|scroll)$/.test(getComputedStyle(n).overflowY)) continue; + if (n.scrollHeight - n.clientHeight < 2) continue; + if (event.deltaY < 0 ? n.scrollTop > 0 : n.scrollTop < n.scrollHeight - n.clientHeight - 0.5) return; + } + event.preventDefault(); + transcript.scrollTop += event.deltaY * (event.deltaMode === 1 ? 16 : event.deltaMode === 2 ? transcript.clientHeight : 1); +} diff --git a/tests/dock-height.test.tsx b/tests/dock-height.test.tsx new file mode 100644 index 0000000..e6d9d3d --- /dev/null +++ b/tests/dock-height.test.tsx @@ -0,0 +1,259 @@ +import React, { act, useLayoutEffect, useRef, useState } from "react"; +(globalThis as Record).React = React; +import assert from "node:assert/strict"; +import { describe, it } from "node:test"; +import { createRoot } from "react-dom/client"; +import { parseHTML } from "linkedom"; +import { QuestionDock } from "../components/question-dock.tsx"; +import { PracticeDock } from "../components/practice-dock.tsx"; + +const dom = parseHTML(""); +Object.assign(globalThis, { + window: dom.window, document: dom.document, HTMLElement: dom.window.HTMLElement, + IS_REACT_ACT_ENVIRONMENT: true, +}); +const observers: TestObserver[] = []; +class TestObserver { + elements = new Set(); + disconnected = false; + constructor(readonly callback: () => void) { observers.push(this); } + observe(el: Element) { this.elements.add(el); } + disconnect() { this.disconnected = true; this.elements.clear(); } +} +Object.assign(globalThis, { + ResizeObserver: TestObserver, + getComputedStyle: (el: HTMLElement) => ({ + borderTopWidth: el.dataset.part === "shell" ? "1px" : "0px", + borderBottomWidth: el.dataset.part === "shell" ? "1px" : "0px", + paddingTop: el.hasAttribute("data-dock-scroll-area") ? "12px" : "0px", + paddingBottom: "0px", + overflowY: el.style.overflowY || (el.classList.contains("overflow-y-auto") ? "auto" : "visible"), + }), +}); +// Import after installing window so the hook uses its browser layout effect. +const { dockHeightBudget, syncDockHeight, useDockHeight, forwardDockWheel } = await import("../components/dock-height.ts"); + +const normal = { + columnHeight: 700, outsideHeight: 60, beforeShellHeight: 0, + shellBorderHeight: 2, contentHeight: 800, questionHeight: 600, + questionPaddingHeight: 12, +}; + +describe("dockHeightBudget — whole dock, not just its question", () => { + it("subtracts answer controls, toolbar, borders and breathing gap", () => { + assert.deepEqual(dockHeightBudget(normal), { + viewportMaxHeight: 608, shellMaxHeight: 608, questionMaxHeight: 406, scrollWholeContent: false, + }); + }); + it("accounts for growing answer fields and the separate reading draft", () => { + const result = dockHeightBudget({ ...normal, beforeShellHeight: 170, contentHeight: 880 }); + assert.equal(result.questionMaxHeight, 156); + assert.equal(result.shellMaxHeight, 438); + }); + it("preserves a computed zero and deliberately selects whole-content scrolling", () => { + const result = dockHeightBudget({ ...normal, columnHeight: 292 }); + assert.equal(result.questionMaxHeight, 0); + assert.equal(result.scrollWholeContent, true); + }); + it("switches before padding would leave a zero-sized content viewport, without an invented minimum", () => { + assert.equal(dockHeightBudget({ ...normal, columnHeight: 306 }).scrollWholeContent, true); + assert.equal(dockHeightBudget({ ...normal, columnHeight: 307 }).scrollWholeContent, false); + }); + it("never creates negative caps, even if the toolbar exceeds the column", () => { + const result = dockHeightBudget({ ...normal, columnHeight: 20 }); + assert.equal(result.viewportMaxHeight, 0); + assert.equal(result.shellMaxHeight, 0); + assert.equal(result.questionMaxHeight, 0); + assert.equal(result.scrollWholeContent, true); + }); + it("collapsed bars use their visible height, and a tall reading draft can still scroll", () => { + assert.equal(dockHeightBudget({ ...normal, questionHeight: null, contentHeight: 42 }).scrollWholeContent, false); + assert.equal(dockHeightBudget({ ...normal, questionHeight: null, contentHeight: 42, beforeShellHeight: 650 }).scrollWholeContent, true); + }); +}); + +// These mocked dimensions test the DOM/lifecycle seam, not real CSS layout. +// Live-browser checks separately exercise actual field sizing and WAAPI. +const sizes: Record = {}; +let animating = false; +dom.window.HTMLElement.prototype.getAnimations = () => animating ? [{} as Animation] : []; +const resetSizes = () => Object.assign(sizes, { column: 700, block: 862, viewport: 802, body: 802, shell: 802, content: 800, question: 600 }); +const originalRect = dom.window.HTMLElement.prototype.getBoundingClientRect; +dom.window.HTMLElement.prototype.getBoundingClientRect = function () { + const part = this.hasAttribute("data-dock-scroll-area") ? "question" : this.dataset.part; + if (part && part in sizes) return { height: sizes[part]!, width: 700, x: 0, y: 0, top: 0, left: 0, bottom: sizes[part]!, right: 700, toJSON() {} }; + return originalRect.call(this); +}; +Object.defineProperty(dom.window.HTMLElement.prototype, "clientHeight", { configurable: true, get() { return this.dataset.part === "column" ? sizes.column : Number(this.dataset.clientHeight ?? 0); } }); +Object.defineProperty(dom.window.HTMLElement.prototype, "clientWidth", { configurable: true, get() { return this.dataset.part === "column" ? 680 : 700; } }); +Object.defineProperty(dom.window.HTMLElement.prototype, "offsetWidth", { configurable: true, get() { return 700; } }); +Object.defineProperty(dom.window.HTMLElement.prototype, "scrollHeight", { configurable: true, get() { return Number(this.dataset.scrollHeight ?? 0); } }); + +const ignoreHeight = () => {}; +function Fixture({ expanded = true, practice = false, enabled = true, inspected, onHeight = ignoreHeight }: { + expanded?: boolean; practice?: boolean; enabled?: boolean; inspected?: (block: HTMLDivElement) => void; + onHeight?: (height: number) => void; +}) { + const scrollRef = useRef(null); + const blockRef = useRef(null); + const viewportRef = useRef(null); + const bodyRef = useRef(null); + const shellRef = useRef(null); + const contentRef = useRef(null); + const [inset, setInset] = useState(0); + useDockHeight({ scrollRef, blockRef, viewportRef, bodyRef, shellRef, contentRef, + expanded, enabled, mode: practice ? "practice:p1" : "question:q1", onScrollbarInset: setInset, onHeight }); + // Like DockOverlay's later morph/reserve layout effect: constraints must + // already be applied in this same commit, not an after-paint correction. + useLayoutEffect(() => { if (blockRef.current) inspected?.(blockRef.current); }); + return <>
+
+
+ {practice ? {}} onSubmit={() => {}} /> + : {}} onSubmit={() => {}} />} +
+
+
; +} + +async function withFixture(run: (host: HTMLElement, render: (props?: React.ComponentProps) => Promise) => Promise) { + resetSizes(); + const host = document.createElement("div"); document.body.append(host); + const root = createRoot(host); + const render = async (props = {}) => { await act(async () => root.render()); }; + try { await run(host, render); } + finally { await act(async () => root.unmount()); host.remove(); } +} +const part = (host: HTMLElement, name: string) => host.querySelector(`[data-part="${name}"]`)!; + +describe("measured question/practice DOM and observer lifecycle", () => { + it("applies constraints before the morph layout effect and handles classic scrollbar insets", async () => { + await withFixture(async (host, render) => { + await render({ inspected: block => assert.equal(block.style.getPropertyValue("--dock-max-h"), "406px") }); + assert.equal(part(host, "block").dataset.inset, "10"); + assert.equal(part(host, "viewport").style.overflowY, "visible"); + assert.ok(!part(host, "viewport").contains(host.querySelector('[data-testid="toolbar"]'))); + }); + }); + it("excludes the animated/clamped shell height rather than treating it as answer chrome", async () => { + await withFixture(async (host, render) => { + await render(); + Object.assign(sizes, { block: 160, viewport: 100, body: 100, shell: 100 }); + await act(async () => observers.at(-1)!.callback()); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h"), "406px"); + }); + }); + it("ignores the still-mounted hidden teaching form and sizes it synchronously on reveal", async () => { + await withFixture(async (host, render) => { + Object.assign(sizes, { block: 104, viewport: 44, body: 44, shell: 44, content: 42, question: 2000 }); + await render({ expanded: false }); + assert.ok(host.querySelector('[data-dock-scroll-area]'), "teaching form remains mounted"); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h") || "", ""); + assert.equal(part(host, "viewport").style.overflowY, "visible"); + resetSizes(); + await render({ expanded: true, inspected: block => assert.equal(block.style.getPropertyValue("--dock-max-h"), "406px") }); + }); + }); + it("reconnects to practice's newly mounted question after collapsing and revealing", async () => { + await withFixture(async (host, render) => { + Object.assign(sizes, { block: 104, viewport: 44, body: 44, shell: 44, content: 42 }); + await render({ practice: true, expanded: false }); + const collapsedObserver = observers.at(-1)!; + assert.equal(host.querySelector('[data-dock-scroll-area]'), null); + resetSizes(); + await render({ practice: true, expanded: true }); + assert.equal(collapsedObserver.disconnected, true); + assert.ok(observers.at(-1)!.elements.has(host.querySelector('[data-dock-scroll-area]')!)); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h"), "406px"); + }); + }); + it("intentionally uncaps the question for a short window, then restores the measured cap on growth", async () => { + await withFixture(async (host, render) => { + sizes.column = 292; + await render(); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h"), "none"); + assert.equal(part(host, "viewport").style.overflowY, "auto"); + assert.equal(part(host, "viewport").style.maxHeight, "200px"); + assert.equal(part(host, "shell").style.maxHeight, "none"); + sizes.column = 700; + await act(async () => observers.at(-1)!.callback()); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h"), "406px"); + assert.equal(part(host, "viewport").style.overflowY, "visible"); + }); + }); + it("removes constraints in composer mode and disconnects observers on unmount", async () => { + let last: TestObserver; + await withFixture(async (host, render) => { + await render(); + await render({ enabled: false }); + assert.equal(part(host, "block").style.getPropertyValue("--dock-max-h") || "", ""); + assert.equal(part(host, "shell").style.maxHeight || "", ""); + last = observers.at(-1)!; + }); + assert.equal(last!.disconnected, true); + }); + it("reports child-only resize heights to the reserve, but never mid-morph values", async () => { + await withFixture(async (_host, render) => { + const heights: number[] = []; + await render({ onHeight: h => heights.push(h) }); + assert.equal(heights.at(-1), 862); + animating = true; + try { + sizes.block = 900; + await act(async () => observers.at(-1)!.callback()); + assert.equal(heights.at(-1), 862, "animated block height was not reserved"); + } finally { animating = false; } + await act(async () => observers.at(-1)!.callback()); + assert.equal(heights.at(-1), 900, "settled child resize reaches the reserve without a parent render"); + }); + }); + it("reconnects to a new keyed mode even when expansion is unchanged", async () => { + await withFixture(async (host, render) => { + await render(); + const previous = observers.at(-1)!; + const oldContent = part(host, "content"); + await render({ practice: true }); + assert.equal(previous.disconnected, true); + assert.notEqual(part(host, "content"), oldContent); + assert.ok(observers.at(-1)!.elements.has(part(host, "content"))); + }); + }); + it("keeps the DOM synchronizer deterministic when observed again with the same dimensions", async () => { + await withFixture(async (host, render) => { + await render(); + const elements = { column: part(host, "column"), block: part(host, "block"), viewport: part(host, "viewport"), + body: part(host, "body"), shell: part(host, "shell"), content: part(host, "content") }; + assert.deepEqual(syncDockHeight(elements, true), syncDockHeight(elements, true)); + }); + }); +}); + +describe("wheel routing — question, short-window content, then transcript", () => { + it("keeps native wheel input in the question and then the whole-content viewport", () => { + const root = document.createElement("div"); + root.innerHTML = '
text
'; + const viewport = root.firstElementChild as HTMLElement; + const question = viewport.firstElementChild as HTMLElement; + const transcript = document.createElement("div"); transcript.scrollTop = 100; + for (const el of [viewport, question]) { el.dataset.scrollHeight = "400"; el.dataset.clientHeight = "100"; el.scrollTop = 50; } + const wheel = (deltaY: number, deltaMode = 0) => { + let prevented = false; + forwardDockWheel({ target: question.firstElementChild, deltaY, deltaMode, preventDefault() { prevented = true; } } as unknown as WheelEvent, root, transcript); + return prevented; + }; + assert.equal(wheel(-20), false, "question can scroll up"); + question.scrollTop = 0; + assert.equal(wheel(-20), false, "whole content can scroll up when question is at its edge"); + viewport.scrollTop = 0; + assert.equal(wheel(-20), true); + assert.equal(transcript.scrollTop, 80); + question.scrollTop = viewport.scrollTop = 300; + assert.equal(wheel(1, 1), true); + assert.equal(transcript.scrollTop, 96, "line-mode wheel is normalized"); + viewport.style.overflowY = "visible"; viewport.scrollTop = 50; + assert.equal(wheel(-20), false, "question still consumes upward movement"); + question.style.overflowY = "hidden"; + assert.equal(wheel(-20), true, "hidden and visible boxes are not scroll containers"); + assert.equal(wheel(0), false); + }); +}); From 419dcf1ba29749ed3491b627b780f5c94a08713b Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Tue, 27 Oct 2026 16:12:13 +0200 Subject: [PATCH 18/19] Restore exclusive provider-first setup recovery --- components/chat.tsx | 51 ++++++------ components/home.tsx | 27 +++--- components/new-session.tsx | 26 +++--- components/setup-cta.tsx | 32 +++++++- components/setup-dialog.tsx | 45 +++++----- components/stats-bar.tsx | 11 ++- tests/model-catalog-setup.test.tsx | 127 ++++++++++++++++++++++++----- 7 files changed, 222 insertions(+), 97 deletions(-) diff --git a/components/chat.tsx b/components/chat.tsx index cef946d..952abf5 100644 --- a/components/chat.tsx +++ b/components/chat.tsx @@ -5,7 +5,7 @@ import { useChatSession } from "@/components/session"; import { visibleQueuedSteers } from "@/components/session-core"; import { SetupDialog } from "@/components/setup-dialog"; import { Hint } from "@/components/ui/hint"; -import { useZeroKeys, ZeroKeysPill } from "@/components/setup-cta"; +import { useModelSetup, selectedModelUnavailable, openModelPicker, ZeroKeysPill } from "@/components/setup-cta"; import { useStickToBottomContext, type StickToBottomContext } from "use-stick-to-bottom"; import { Conversation, @@ -78,8 +78,6 @@ import { } from "@/src/core"; import { ReadingThinkingField } from "@/components/reading-thinking"; import { currentUserThinkingOpen, setAllUserThinkingOpen } from "@/components/thinking-block"; -import { slotsConfigured, type ModelsSettings } from "@/src/core"; -import { useSettingsSaved } from "@/components/settings-signal"; /** One width for everything: transcript, cards, and the bottom blob share this column. */ /** @@ -555,30 +553,18 @@ export function Chat({ textRef.current = v; setText(v); }, []); - // The BYOK setup flow (owner, 2026-09-22): the persistent CTA and the - // refusal interception share one SetupDialog mount. `configured` is - // seeded server-side (slotsConfigured on the record) and flips on Save. - const [configured, setConfigured] = useState(initialConfigured); - // The zero-keys truth (zero-keys wave): no provider key = the pill - // takes precedence over the models CTA (keys come first in the chain). - // Reads once on mount, re-reads on the "keys" signal. - const zeroKeys = useZeroKeys(); - // Cross-surface freshness (fix wave): another surface's models write — - // setup save, composer pick, a settings write — re-reads the record so - // the setup CTA appears/disappears live (it used to lag until reload). - useSettingsSaved((section) => { - if (section !== "models") return; - fetch("/api/settings") - .then((r) => r.json() as Promise<{ settings?: { models?: ModelsSettings } | null } | null>) - .then((body) => setConfigured(slotsConfigured(body?.settings?.models))) - .catch(() => {}); - }); + // Saved slot completeness is live, but the session's selected route + // comes from its own ordinary model control, never today's Strong pin. + const { catalog, configured, zeroKeys } = useModelSetup(initialConfigured); + const catalogRef = useRef(catalog); + catalogRef.current = catalog; + const [selectedModel, setSelectedModel] = useState(); const [setupOpen, setSetupOpen] = useState(false); /** The last explicitly-sent draft (composer send): the * refusal flow restores it into the composer, where it waits for the * learner to re-send (Save never auto-sends, owner 2026-10-09). * Auto-continuations (probe answers) never set it — their refusal - * opens the dialog with no replay. */ + * opens recovery with no replay. */ const draftRef = useRef(null); // THE BYOK REFUSAL (owner, 2026-09-22): a send that lands the server's @@ -594,8 +580,9 @@ export function Chat({ if (!error) return; const missing = parseModelsRefusal(error.message); // The key-refusal twin (zero-keys wave): a pinned model whose - // provider key is gone gets the SAME treatment — revert-draft and - // open setup (the dialog derives to its first-key stage on its own). + // provider key is gone also restores the draft. With another provider + // connected, repair belongs to the ordinary current-session picker; + // otherwise setup derives its exclusive provider/loading stage. const keyRefused = parseProviderKeyRefusal(error.message); if (!missing && !keyRefused) return; // a real error keeps the honest banner // ORDER MATTERS: clearError() flips this effect's own dep (`error`) @@ -615,7 +602,8 @@ export function Chat({ draftRef.current = null; } clearError(); - setSetupOpen(true); + if (keyRefused && catalogRef.current?.providers.some((p) => p.keyPresent)) openModelPicker(); + else setSetupOpen(true); })(); return () => { cancelled = true; @@ -1146,6 +1134,14 @@ export function Chat({ if (busy || pending || compacting) return; const t = textRef.current.trim(); if (!t) return; + if (zeroKeys) { + setSetupOpen(true); + return; + } + if (selectedModelUnavailable(catalog, selectedModel)) { + openModelPicker(); + return; // No optimistic send and no change to the learner's draft. + } draftRef.current = t; // the refusal flow's preserve-and-replay seam applyText(""); void send({ type: "text", text: t }); @@ -1605,6 +1601,7 @@ export function Chat({ compactionRunning={compacting} onSubmitThinking={submitThinking} chatId={chatId} + onModelChange={setSelectedModel} plan={hasPlan ? : undefined} onOpenPlan={() => setPlanOpen(true)} requiresReveal={requiresReveal} @@ -1708,7 +1705,6 @@ export function Chat({ onClose={() => setSetupOpen(false)} onDone={() => { setSetupOpen(false); - setConfigured(true); // Save only saves (owner ruling 2026-10-09): the draft went // home into the composer at refusal time and STAYS there — // the learner sends it themselves. @@ -1843,6 +1839,7 @@ function DockOverlay({ composer, onSubmitAnswer, chatId, + onModelChange, onReserve, plan, onOpenPlan, @@ -1879,6 +1876,7 @@ function DockOverlay({ onSubmitAnswer: (output: QuestionAnswerData) => void; /** The model control's pass-through (the stats bar hosts it). */ chatId: string; + onModelChange: (model: string | undefined) => void; /** Reports the dock's settled height (block + gradient) — ChatView's * dockReserve state feeds the transcript's spacer from it. */ onReserve: (h: number) => void; @@ -2331,6 +2329,7 @@ function DockOverlay({ pricing={pricing} dictationSlot={dictationSlot} chatId={chatId} + onModelChange={onModelChange} /> {/* The scroll-down disc (moved here from the Conversation, owner refinement round 2026-09-24): anchored to the BLOCK's diff --git a/components/home.tsx b/components/home.tsx index 4b16d62..6644bc6 100644 --- a/components/home.tsx +++ b/components/home.tsx @@ -7,7 +7,7 @@ import { GraphView } from "@/components/graph-view"; import { MinimalHome } from "@/components/minimal-home"; import { SetupDialog } from "@/components/setup-dialog"; import { ComposerActions, NodePickerControl } from "@/components/composer-actions"; -import { useStrongKeyless, useZeroKeys, ZeroKeysPill } from "@/components/setup-cta"; +import { useModelSetup, selectedModelUnavailable, openModelPicker, ZeroKeysPill } from "@/components/setup-cta"; import { DictationBar, joinDictation, useDictation } from "@/components/dictation"; import { useFieldCycle } from "@/components/field-cycle"; import { SidebarToggle } from "@/components/sidebar"; @@ -41,15 +41,8 @@ export function Home({ // start until the user has picked all three slots — no defaults for // 33 providers. The dock shows the CTA while unconfigured; submitting // a topic opens the setup dialog WITH THE DRAFT PRESERVED (Save - // stores the slots and the queued message sends). - const [configured, setConfigured] = useState(initialConfigured); - // The zero-keys truth (zero-keys wave): no provider key = the pill - // takes precedence over the models CTA (keys come first in the chain). - const zeroKeys = useZeroKeys(); - // The strong slot's keyless truth (pre-send key guards, owner interlude - // 2026-09-25): keys may exist generally while the STRONG slot's pinned - // provider has none — the same skip-everything treatment on send. - const strongKeyless = useStrongKeyless(); + // stores slots only; the learner sends the message themselves). + const { catalog, configured, zeroKeys } = useModelSetup(initialConfigured); const [setupOpen, setSetupOpen] = useState(false); // Voice dictation (STT wave, 2026-09-17): finals append at the end of the // topic draft; the strip lives in the stats bar's middle slot. @@ -123,13 +116,18 @@ export function Home({ // models case guards BEFORE creation; the key cases must too — a // refused first send would otherwise leave an orphan error-shell // chat in the sidebar (the 503 lands only after POST /api/chats - // already created the record). Same skip-everything-and-open-setup - // treatment; the server's 503 gate remains the belt for /chat/[id] - // sessions. - if (zeroKeys || strongKeyless) { + // already created the record). Zero providers opens connection setup; + // an unavailable selection with another provider opens its ordinary + // picker. The server's admission gate remains the belt. + if (zeroKeys) { setSetupOpen(true); return; } + const strong = catalog?.slots.strong?.model; + if (selectedModelUnavailable(catalog, strong ? `${strong.provider}:${strong.id}` : undefined)) { + openModelPicker(); + return; // Preserve the draft and never create an orphan session. + } setBusy(true); try { // The map context (owner, 2026-10-04): picks in EITHER mode ride @@ -326,7 +324,6 @@ export function Home({ open={setupOpen} onClose={() => setSetupOpen(false)} onDone={() => { - setConfigured(true); setSetupOpen(false); // Save only saves: the draft waits in the composer. }} diff --git a/components/new-session.tsx b/components/new-session.tsx index c2a0930..2672340 100644 --- a/components/new-session.tsx +++ b/components/new-session.tsx @@ -8,7 +8,7 @@ import { DictationBar, joinDictation, useDictation } from "@/components/dictatio import { useFieldCycle } from "@/components/field-cycle"; import { SidebarToggle } from "@/components/sidebar"; import { SetupDialog } from "@/components/setup-dialog"; -import { useStrongKeyless, useZeroKeys, ZeroKeysPill } from "@/components/setup-cta"; +import { useModelSetup, selectedModelUnavailable, openModelPicker, ZeroKeysPill } from "@/components/setup-cta"; import { StatsBar } from "@/components/stats-bar"; import { ChainTableOverlay } from "@/components/chain-table.tsx"; import { ComposerActions, NodePickerControl } from "@/components/composer-actions"; @@ -275,14 +275,7 @@ export function NewSession({ initialConfigured, mode = "teach" }: { initialConfi // 33 providers, chats cannot start. The draft stays in the textarea; // Save stores the slots and never sends (owner ruling 2026-10-09) — // the learner sends the draft themselves. - const [configured, setConfigured] = useState(initialConfigured); - // The zero-keys truth (zero-keys wave): no provider key = the pill - // takes precedence over the models CTA (keys come first in the chain). - const zeroKeys = useZeroKeys(); - // The strong slot's keyless truth (pre-send key guards, owner interlude - // 2026-09-25): keys may exist generally while the STRONG slot's pinned - // provider has none — the same skip-everything treatment on send. - const strongKeyless = useStrongKeyless(); + const { catalog, configured, zeroKeys } = useModelSetup(initialConfigured); const [setupOpen, setSetupOpen] = useState(false); const send = useCallback(() => { @@ -295,12 +288,18 @@ export function NewSession({ initialConfigured, mode = "teach" }: { initialConfi // The pre-send KEY guards (owner interlude, no-orphan-chats): guard // BEFORE startDraftSession creates the record — a refused first // send would otherwise leave an orphan error-shell chat in the - // sidebar. Same skip-everything-and-open-setup treatment; the - // server's 503 gate remains the belt for /chat/[id] sessions. - if (zeroKeys || strongKeyless) { + // sidebar. Zero providers opens connection setup; an unavailable + // selection with another provider opens the ordinary Strong picker. + // The server's admission gate remains the belt. + if (zeroKeys) { setSetupOpen(true); return; } + const strong = catalog?.slots.strong?.model; + if (selectedModelUnavailable(catalog, strong ? `${strong.provider}:${strong.id}` : undefined)) { + openModelPicker(); + return; // The learner replaces it explicitly, then sends again. + } setBusy(true); void startDraftSession(draft, fetch, mode === "practice" ? "practice" : undefined, mode === "practice" ? picksRef.current : undefined) .then((target) => { @@ -308,7 +307,7 @@ export function NewSession({ initialConfigured, mode = "teach" }: { initialConfi else setBusy(false); }) .catch(() => setBusy(false)); - }, [busy, router, configured, zeroKeys, strongKeyless]); + }, [busy, router, configured, zeroKeys, catalog]); return ( <> @@ -369,7 +368,6 @@ export function NewSession({ initialConfigured, mode = "teach" }: { initialConfi open={setupOpen} onClose={() => setSetupOpen(false)} onDone={() => { - setConfigured(true); setSetupOpen(false); // Save only saves (owner ruling 2026-10-09): the draft // waits in the composer; the learner sends it. diff --git a/components/setup-cta.tsx b/components/setup-cta.tsx index de59b98..299715d 100644 --- a/components/setup-cta.tsx +++ b/components/setup-cta.tsx @@ -15,9 +15,10 @@ * its own save), the same re-read pattern chat.tsx uses for "models". */ -import type { AuthKeyStatus } from "@/src/core"; +import { useEffect, useState } from "react"; +import { MODEL_SLOTS, type AuthKeyStatus } from "@/src/core"; import { classifyKeys, type ProviderKeyInfo } from "@/components/settings/keys-section"; -import { useModelCatalog } from "@/components/model-picker"; +import { useModelCatalog } from "@/components/model-catalog"; import type { ModelCatalogResponse } from "@/src/core"; /** Pure: does a MODEL-PROVIDER key exist among the statuses? Reuses @@ -32,6 +33,33 @@ export function hasProviderKey( return classifyKeys(statuses, providers).providerRows.some((s) => s.present); } +/** Completeness is saved identity, not current provider availability. */ +export function savedSlotsConfigured(slots: ModelCatalogResponse["slots"] | undefined): boolean { + return MODEL_SLOTS.every((slot) => !!slots?.[slot]?.model); +} + +/** Keep the last known completeness during connection invalidation, but + * follow ordinary slot repairs as soon as their shared snapshot arrives. */ +export function useModelSetup(initialConfigured: boolean) { + const { catalog } = useModelCatalog(); + const [lastConfigured, setLastConfigured] = useState(initialConfigured); + const configured = catalog ? savedSlotsConfigured(catalog.slots) : lastConfigured; + useEffect(() => { if (catalog) setLastConfigured(savedSlotsConfigured(catalog.slots)); }, [catalog]); + return { catalog, configured, zeroKeys: catalog ? !catalog.providers.some((p) => p.keyPresent) : false }; +} + +/** A known unavailable route needs an explicit replacement, never an + * automatic provider/model substitution. Unknown metadata defers to the + * server's admission gate instead of declaring a route disconnected. */ +export function selectedModelUnavailable(catalog: ModelCatalogResponse | undefined, selected: string | undefined): boolean { + return !!catalog && !!selected && !catalog.models.some((m) => `${m.provider}:${m.id}` === selected); +} + +/** Reuse the ordinary control (also opened by the existing keyboard action). */ +export function openModelPicker(): void { + window.dispatchEvent(new CustomEvent("lh:kbd-open-model-picker")); +} + /** The zero-keys truth for the calling page: TRUE only after a * successful read says no provider key exists (pending and failed * reads render nothing — the pill must never flash on a gap, and a diff --git a/components/setup-dialog.tsx b/components/setup-dialog.tsx index 641898f..053f3eb 100644 --- a/components/setup-dialog.tsx +++ b/components/setup-dialog.tsx @@ -24,8 +24,9 @@ * Stage 2 — Strong → Cheap → Vision pickers over the FEDERATED catalog * (keyed providers only), the vision slot filtered to image-capable * models, in the app's shared dropdown language (ModelMenuList). - * Saved disconnected selections stay visible; Add provider opens the - * connection body when no provider remains. Refreshes preserve edits. + * Saved disconnected selections and edits are retained internally in + * the exclusive provider stage. Connecting with all slots already saved + * closes setup; otherwise the pickers require only the missing slots. * All three required (owner: no skip, no defaults); Save PUTs the * models section and reports onDone(models). * @@ -45,6 +46,7 @@ import { ModelMenuList } from "@/components/settings/model-menu"; import { MenuShell } from "@/components/menu-shell"; import { notifySettingsSaved, useSettingsSaved } from "@/components/settings-signal"; import { useModelCatalog } from "@/components/model-catalog"; +import { savedSlotsConfigured } from "@/components/setup-cta"; import { Hint } from "@/components/ui/hint"; import { type ProviderKeyInfo } from "@/components/settings/keys-section"; import { ProviderMenuList } from "@/components/settings/model-menu"; @@ -247,12 +249,11 @@ export function SetupDialog(props: SetupDialogProps) { function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { const backdropDismiss = useBackdropDismiss(onClose); const { catalog, failed, refresh } = useModelCatalog(); - const [lastBody, setLastBody] = useState(catalog); - const body = catalog ?? lastBody; + const providerStageSeen = useRef(false); + const connectionCloseRequested = useRef(false); const [picked, setPicked] = useState(() => prefillFromSlots(catalog?.models ?? [], catalog?.slots)); const [levels, setLevels] = useState(() => prefillLevels(catalog)); const editedSlots = useRef(new Set()); - const [showKeys, setShowKeys] = useState(false); const [saving, setSaving] = useState(false); const [error, setError] = useState(null); const [openMenu, setOpenMenu] = useState<{ slot: ModelSlotName; kind: "model" | "thinking" } | null>(null); @@ -273,8 +274,8 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { // The keys signal (zero-keys wave): keys-section saves/deletes AND // THIS dialog's own first-key save dispatch it; the fetch effect below - // re-runs on it, so saving a key flips the stage to the pickers with - // the widened catalog — no restart, no Settings round-trip. + // re-runs on it, so saving a key widens the catalog. Incomplete saved + // slots advance to pickers; complete saved slots just close setup. const [keysTick, setKeysTick] = useState(0); useSettingsSaved((section) => { if (section === "keys") setKeysTick((t) => t + 1); @@ -291,7 +292,6 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { useEffect(() => { if (!catalog) return; - setLastBody(catalog); const pre = prefillFromSlots(catalog.models, catalog.slots); const preLevels = prefillLevels(catalog); // Fresh metadata is always used, but a response cannot reset an @@ -310,9 +310,18 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { } return next; }); - if (catalog.providers.some((p) => p.keyPresent)) setShowKeys(false); }, [catalog]); + const stage = setupStage(catalog?.providers ?? null); + const closeAfterConnection = stage === "pickers" && providerStageSeen.current && savedSlotsConfigured(catalog?.slots); + useEffect(() => { + if (stage === "keys") providerStageSeen.current = true; + if (closeAfterConnection && !connectionCloseRequested.current) { + connectionCloseRequested.current = true; + onClose(); // Connection recovery is not a model-settings save. + } + }, [stage, closeAfterConnection, onClose]); + // Esc + outside-click — layered dismissal, closable WITHOUT // configuring (browsing stays possible; the gate is the send // interception, not this dialog). An open slot menu eats Esc first @@ -354,14 +363,16 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) { }; }, [onClose, openMenu, keyMenuOpen]); - const hasSelections = Object.keys(picked).length > 0 || SLOT_ORDER.some((slot) => body?.slots?.[slot]?.model); - const stage = hasSelections ? "pickers" : setupStage(body ? body.providers : null); + // Do not briefly paint model pickers between successful connection and + // closing an already-configured setup. No saved model/thinking writes. + if (closeAfterConnection) return null; + const allPicked = SLOT_ORDER.every((s) => picked[s]); /** Stage 1's save: PUT the pasted key, dispatch the keys signal, and - * CLEAN the local screen — the signal re-fetch widens the catalog and - * the stage flips to the pickers on its own; the dialog stays mounted - * the whole way. Shape-gate 400s keep their user-facing line inline. */ + * CLEAN the local key field. The fresh catalog decides whether to + * close recovery or advance to the missing model slots. Shape-gate + * 400s keep their user-facing line inline. */ async function saveKey() { if (!keyProvider || !keyValue.trim() || keySaving) return; setKeySaving(true); @@ -465,10 +476,6 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) {

Pick the model for each tier. All three are required before chats can start.

- {!body?.providers.some((p) => p.keyPresent) && <> - - {showKeys && keysBody} - } {SLOT_ORDER.map((slot) => { const entry = picked[slot]; const level = levels[slot]; @@ -481,7 +488,7 @@ function OpenSetupDialog({ onClose, onDone }: SetupDialogProps) {
{SLOT_META[slot].label} - {!body?.slots?.[slot]?.model && !picked[slot] && ( + {!catalog?.slots?.[slot]?.model && !picked[slot] && ( void; /** The dictation strip (STT wave, 2026-09-17): the row's middle is dead * space between the session data and the model control — the mic * button lives there, and while recording the meter unfurls to fill @@ -55,6 +58,10 @@ export function StatsBar({ }) { const [open, setOpen] = useState(false); const [selectedModel, setSelectedModel] = useState(); + const modelChanged = useCallback((model: string | undefined) => { + setSelectedModel(model); + onModelChange?.(model); + }, [onModelChange]); // Same dropdown discipline as the model control (owner, df4433f treatment, // stats round 1): close on any pointerdown outside the bar or on Escape. const barRef = useRef(null); @@ -192,7 +199,7 @@ export function StatsBar({ {/* The model + thinking control — one controller for the conversation agents (prober/teacher); the record's `turns` above still folds the ACTUAL models billed, regardless of what the picker says. */} - +
); } diff --git a/tests/model-catalog-setup.test.tsx b/tests/model-catalog-setup.test.tsx index 43db670..4583863 100644 --- a/tests/model-catalog-setup.test.tsx +++ b/tests/model-catalog-setup.test.tsx @@ -9,6 +9,8 @@ import { AgentSection } from "../components/settings/agent-section.tsx"; import { providerKeyLabel, classifyKeys } from "../components/settings/keys-section.tsx"; import { useModelCatalog } from "../components/model-catalog.ts"; import { notifySettingsSaved } from "../components/settings-signal.ts"; +import { useModelSetup, savedSlotsConfigured, selectedModelUnavailable, openModelPicker } from "../components/setup-cta.tsx"; +import { StatsBar } from "../components/stats-bar.tsx"; import type { ModelCatalogEntry, ModelCatalogResponse, ModelsSettings } from "../src/core/index.ts"; const dom = parseHTML(""); @@ -40,7 +42,7 @@ function catalog(connected = true): ModelCatalogResponse { }; } function Reader() { snapshot = useModelCatalog(); return null; } -function setup() { return {}} onDone={() => {}} />; } +function setup(onClose = () => {}, onDone = (_models: ModelsSettings) => {}) { return ; } function installFetch() { requests = []; writes = []; globalThis.fetch = async (input, init) => { @@ -107,22 +109,27 @@ test("cached setup is immediately usable; fresh metadata/untouched slots update }); }); -test("disconnect invalidates choices, not saved selections or thinking; all-disconnected setup offers Add provider", async () => { - installFetch(); await warm(); await render(<>{setup()}); await answer(1, catalog()); +test("zero providers always means exclusive connection stage; reconnect with all saved slots only closes, even if every selection remains unavailable", async () => { + installFetch(); await warm(); + let closed = 0, completed = 0; + await render(<>{setup(() => { closed++; }, () => { completed++; })}); await answer(1, catalog()); await click("setup-trigger-strong"); - assert.ok(document.querySelector('[data-testid="model-option-claude-opus-5-5"]')); await act(async () => notifySettingsSaved("keys")); - assert.equal(document.querySelector('[data-testid="model-option-claude-opus-5-5"]'), null, "stale choices disappear before the fresh request resolves"); + assert.ok(element("setup-stage-loading")); + assert.equal(document.querySelector('[data-testid="model-option-claude-opus-5-5"]'), null); await answer(2, catalog(false)); - assert.equal(element("setup-trigger-strong").textContent, "model:free▾"); - assert.equal(element("setup-thinking-strong").textContent?.trim(), "low▾"); - assert.ok(element("setup-add-provider")); await click("setup-add-provider"); assert.ok(element("setup-stage-keys")); - await click("setup-thinking-strong"); await click("thinking-option-xhigh"); - assert.equal(element("setup-thinking-strong").textContent?.trim(), "xhigh▾"); - await click("setup-save"); assert.equal(writes[0]?.strong?.thinkingLevel, "xhigh"); assert.equal(writes[0]?.strong?.model?.id, a.id); + assert.ok(element("setup-stage-keys")); + assert.equal(document.querySelector('[data-testid="setup-stage-pickers"]'), null); + assert.equal(document.querySelector('[data-testid="setup-add-provider"]'), null); + assert.equal(document.querySelector('[data-testid="setup-save"]'), null); + await act(async () => notifySettingsSaved("keys")); + const other = catalog(false); other.providers = [{ id: "other", envVar: "OTHER_KEY", keyPresent: true }]; + await answer(3, other); + assert.equal(closed, 1); assert.equal(completed, 0); assert.deepEqual(writes, []); + assert.equal(document.querySelector('[data-testid="setup-dialog"]'), null, "no intermediate picker frame after reconnect"); }); -test("cold reconnect checks saved pins before choosing the key stage, and reopening gets a new draft", async () => { +test("cold saved disconnected pins still enter the provider stage, without any model-selector frame", async () => { installFetch(); await warm(); await act(async () => notifySettingsSaved("keys")); const stages: Array = []; @@ -132,14 +139,47 @@ test("cold reconnect checks saved pins before choosing the key stage, and reopen return null; } await render(<>{setup()}); - assert.ok(element("setup-stage-loading")); await answer(1, catalog(false)); - assert.deepEqual(stages, ["setup-stage-loading", "setup-stage-pickers"]); - await click("setup-thinking-strong"); await click("thinking-option-xhigh"); - await render(); - await render(<>{setup()}); - assert.equal(element("setup-thinking-strong").textContent?.trim(), "low▾", "a reopened draft uses saved state, not the abandoned edit"); - await answer(2, catalog(false)); + assert.deepEqual(stages, ["setup-stage-loading", "setup-stage-keys"]); +}); + +for (const count of [0, 1, 2]) test(`connecting with ${count} saved slots only requires the missing slots; stale identities/thinking survive`, async () => { + installFetch(); const before = catalog(false); + if (count < 1) before.slots.strong = null; + if (count < 2) before.slots.cheap = null; + before.slots.vision = null; + if (before.slots.strong) before.slots.strong.thinkingLevel = "xhigh"; + await warm(before); + let closed = 0; await render(<>{setup(() => { closed++; })}); await answer(1, before); + assert.ok(element("setup-stage-keys")); + const connected = { ...before, models: [{ ...b, provider: "other" }], providers: [{ id: "other", envVar: "OTHER_KEY", keyPresent: true }] }; + await act(async () => notifySettingsSaved("keys")); await answer(2, connected); + assert.ok(element("setup-stage-pickers")); assert.equal(document.querySelector('[data-testid="setup-stage-keys"]'), null); assert.equal(closed, 0); + for (const slot of ["strong", "cheap", "vision"] as const) { + if (before.slots[slot]) continue; + await click(`setup-trigger-${slot}`); await click("model-option-claude-opus-5-5"); + } + if (count > 0) assert.equal(element("setup-thinking-strong").textContent?.trim(), "xhigh▾"); + await click("setup-save"); + for (const slot of ["strong", "cheap", "vision"] as const) { + if (before.slots[slot]) assert.deepEqual(writes[0]?.[slot], before.slots[slot]); + else assert.equal(writes[0]?.[slot]?.model?.provider, "other"); + } +}); + +test("a partial setup's model/thinking edits survive exclusive provider recovery, but reopening discards the abandoned draft", async () => { + installFetch(); const before = catalog(); before.slots.cheap = null; before.slots.vision = null; + await warm(before); await render(<>{setup()}); await answer(1, before); + await click("setup-trigger-strong"); await click("model-option-claude-opus-5-5"); + await click("setup-thinking-strong"); await click("thinking-option-medium"); + await act(async () => notifySettingsSaved("keys")); await answer(2, { ...before, models: [], providers: [] }); + assert.ok(element("setup-stage-keys")); assert.equal(document.querySelector('[data-testid="setup-stage-pickers"]'), null); + await act(async () => notifySettingsSaved("keys")); await answer(3, before); + assert.equal(element("setup-trigger-strong").textContent, "claude-opus-5-5▾"); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "medium▾"); + await render(); await render(<>{setup()}); + assert.equal(element("setup-trigger-strong").textContent, "model:free▾"); + assert.equal(element("setup-thinking-strong").textContent?.trim(), "low▾"); await answer(4, before); }); test("Agent opens with cached thinking metadata; refresh and disconnect never clear saved levels", async () => { @@ -155,6 +195,55 @@ test("Agent opens with cached thinking metadata; refresh and disconnect never cl await click("thinking-option-xhigh"); assert.equal(changed?.strong?.thinkingLevel, "xhigh"); assert.equal(changed?.strong?.model?.id, a.id); }); +test("saved completeness follows ordinary repairs and survives temporary connection invalidation", async () => { + installFetch(); let state: ReturnType; + function StateReader() { state = useModelSetup(false); return null; } + await render(); await answer(0, catalog()); + assert.equal(state!.configured, true); + await act(async () => notifySettingsSaved("keys")); assert.equal(state!.configured, true); + const partial = catalog(false); partial.slots.vision = null; + await answer(1, partial); assert.equal(state!.configured, false); assert.equal(state!.zeroKeys, true); + await act(async () => notifySettingsSaved("models")); await answer(2, catalog(false)); + assert.equal(state!.configured, true); assert.equal(state!.zeroKeys, true, "saved is not connected"); +}); + +test("blocked session recovery opens its ordinary picker, never changes Strong or substitutes a model", async () => { + installFetch(); const available = { ...b, provider: "other" }; const body = catalog(); + body.models = [available]; body.providers = [{ id: "other", envVar: "OTHER_KEY", keyPresent: true }]; + body.slots.strong = { model: { provider: "other", id: b.id }, thinkingLevel: "medium" }; + const globals: ModelsSettings = { strong: body.slots.strong }; + const session = { model: { provider: a.provider, id: a.id }, thinkingLevel: "xhigh" }; + const requestsOnly = globalThis.fetch; const sessionWrites: unknown[] = []; + globalThis.fetch = async (input, init) => { + if (input === "/api/settings") return Response.json({ settings: { models: globals } }); + if (input === "/api/chats/fixture") return Response.json({ settings: session }); + if (input === "/api/chats/fixture/settings") { const next = JSON.parse(String(init?.body)); sessionWrites.push(next); return Response.json({ settings: next }); } + return requestsOnly(input, init); + }; + await warm(body); let selected: string | undefined; + await render(<> { selected = id; }} />); + assert.equal(selected, `${a.provider}:${a.id}`, "current session does not inherit a different Strong pin"); + assert.equal(selectedModelUnavailable(body, selected), true); + assert.deepEqual(sessionWrites, []); + await act(async () => openModelPicker()); + assert.ok(element("model-menu")); assert.equal(document.querySelector('[data-testid="setup-dialog"]'), null); + assert.equal(document.querySelector('[data-testid="model-option-vendor_model_free"]'), null); + await click("model-option-claude-opus-5-5"); + assert.deepEqual(sessionWrites, [{ model: { provider: "other", id: b.id }, thinkingLevel: "xhigh" }]); + assert.equal(selected, `other:${b.id}`); assert.deepEqual(globals, { strong: body.slots.strong }); +}); + +test("availability and setup completeness never conflate missing metadata, disconnected providers or saved identities", () => { + const body = catalog(false); + assert.equal(savedSlotsConfigured(body.slots), true); + assert.equal(selectedModelUnavailable(body, `${a.provider}:${a.id}`), true); + assert.equal(selectedModelUnavailable(undefined, `${a.provider}:${a.id}`), false); + assert.equal(selectedModelUnavailable(body, undefined), false); + body.models = [b]; body.providers = [{ ...provider, keyPresent: true }]; + assert.equal(selectedModelUnavailable(body, `${b.provider}:${b.id}`), false); + body.slots.cheap = null; assert.equal(savedSlotsConfigured(body.slots), false); +}); + test("shared refresh fences late failure as well as late success, and Retry clears a current error", async () => { installFetch(); await warm(); await act(async () => snapshot.refresh()); await act(async () => snapshot.refresh()); From 06714e599859ccc742197224b96a1371916da58f Mon Sep 17 00:00:00 2001 From: amosblomqvist Date: Tue, 27 Oct 2026 16:21:54 +0200 Subject: [PATCH 19/19] Keep unresolved session identity separate from Strong fallback --- components/model-picker.tsx | 26 +++++++++++++---------- tests/model-catalog-setup.test.tsx | 33 ++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/components/model-picker.tsx b/components/model-picker.tsx index b618e73..0b8e46d 100644 --- a/components/model-picker.tsx +++ b/components/model-picker.tsx @@ -43,7 +43,9 @@ export function catalogLimits(catalog: Pick): Ma export function ModelControl({ chatId, onModelChange }: { chatId?: string; onModelChange?: (model: string | undefined) => void }) { const { catalog, refresh } = useModelCatalog(); const [models, setModels] = useState(); - const [session, setSession] = useState(); + // A successful read with no settings is a legacy session; undefined + // means no session record is known yet, not permission to inherit Strong. + const [session, setSession] = useState<{ settings?: ChatSettings }>(); const [open, setOpen] = useState<"model" | "thinking" | null>(null); const [error, setError] = useState(null); const containerRef = useRef(null); @@ -58,8 +60,8 @@ export function ModelControl({ chatId, onModelChange }: { chatId?: string; onMod const readRecord = useCallback(() => { if (chatId) { fetch(`/api/chats/${chatId}`) - .then((r) => r.json() as Promise<{ settings?: ChatSettings }>) - .then((body) => setSession(body.settings)) + .then((r) => { if (!r.ok) throw new Error(`session read failed: ${r.status}`); return r.json() as Promise<{ settings?: ChatSettings }>; }) + .then((body) => setSession({ settings: body.settings })) .catch(() => { /* same read-only posture as a models-section failure */ }); @@ -83,15 +85,17 @@ export function ModelControl({ chatId, onModelChange }: { chatId?: string; onMod }); // Dual-bound truth: chat mode displays the session's own record, - // falling back to the strong slot for legacy unstamped sessions — - // EXACTLY what resolveConversationSpec runs. Draft mode is the strong - // slot itself. + // falling back to Strong only for a successfully read legacy unstamped + // session — EXACTLY what resolveConversationSpec runs. An unresolved or + // failed record read must not report a provisional global preference as + // this session's identity (including to the Send guard). Draft mode is + // the strong slot itself. const strong = models?.strong; const override: ChatSettings | undefined = chatId - ? { - model: session?.model ?? strong?.model, - thinkingLevel: session?.thinkingLevel ?? strong?.thinkingLevel, - } + ? session ? { + model: session.settings?.model ?? strong?.model, + thinkingLevel: session.settings?.thinkingLevel ?? strong?.thinkingLevel, + } : undefined : strong; const effectiveId = override?.model?.id; const effectiveProvider = override?.model?.provider; @@ -195,7 +199,7 @@ export function ModelControl({ chatId, onModelChange }: { chatId?: string; onMod return; } const stored = (await res.json().catch(() => null)) as { settings?: ChatSettings } | null; - setSession(stored?.settings ?? next); + setSession({ settings: stored?.settings ?? next }); setOpen(null); } catch { setError("Could not reach the server — try again."); diff --git a/tests/model-catalog-setup.test.tsx b/tests/model-catalog-setup.test.tsx index 4583863..a426ae2 100644 --- a/tests/model-catalog-setup.test.tsx +++ b/tests/model-catalog-setup.test.tsx @@ -233,6 +233,39 @@ test("blocked session recovery opens its ordinary picker, never changes Strong o assert.equal(selected, `other:${b.id}`); assert.deepEqual(globals, { strong: body.slots.strong }); }); +for (const result of ["override", "legacy", "http-failure", "network-failure"] as const) test(`known catalog never enforces provisional Strong on a pending session; session read ${result}`, async () => { + installFetch(); const available = { ...b, provider: "other" }; const body = catalog(); + body.models = [available]; body.providers = [{ id: "other", envVar: "OTHER_KEY", keyPresent: true }]; + // Global Strong is unavailable, but this existing session owns a valid + // model. Resolve global settings first while keeping its record pending. + const globals: ModelsSettings = { strong: { model: { provider: a.provider, id: a.id }, thinkingLevel: "xhigh" } }; + let resolveSession!: (response: Response) => void; + let rejectSession!: (reason: Error) => void; + const sessionRead = new Promise((resolve, reject) => { resolveSession = resolve; rejectSession = reject; }); + const requestsOnly = globalThis.fetch; + globalThis.fetch = async (input, init) => { + if (input === "/api/settings") return Response.json({ settings: { models: globals } }); + if (input === "/api/chats/fixture") return sessionRead; + return requestsOnly(input, init); + }; + await warm(body); const identities: Array = []; + await render(<> { identities.push(id); }} />); + assert.ok(snapshot.catalog, "catalog is already known, not the deferred unknown-catalog case"); + assert.equal(element("stats-model").textContent, "—▾"); + assert.equal(identities.at(-1), undefined); + assert.equal(selectedModelUnavailable(snapshot.catalog, identities.at(-1)), false, "pending session cannot block Send using global Strong"); + await act(async () => { + if (result === "network-failure") rejectSession(new Error("fixture connection failure")); + else if (result === "http-failure") resolveSession(Response.json({ error: "fixture unavailable" }, { status: 503 })); + else resolveSession(Response.json(result === "legacy" ? {} : { settings: { model: { provider: available.provider, id: available.id }, thinkingLevel: "medium" } })); + }); + const expected = result === "legacy" ? `${a.provider}:${a.id}` : result === "override" ? `other:${b.id}` : undefined; + assert.equal(identities.at(-1), expected); + assert.equal(selectedModelUnavailable(snapshot.catalog, identities.at(-1)), result === "legacy", "only a confirmed legacy record may inherit the unavailable Strong pin"); + assert.equal(document.querySelector('[data-testid="model-menu"]'), null); + assert.deepEqual(writes, []); +}); + test("availability and setup completeness never conflate missing metadata, disconnected providers or saved identities", () => { const body = catalog(false); assert.equal(savedSlotsConfigured(body.slots), true);