scaffold.ts 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. // Shared scaffold for the keyless browser e2e lane (Agent Note:
  2. // .agents/notes/implemented/testing/2026-07-24-web-gui-browser-e2e-lane.md).
  3. // Boots the REAL web composition — the dsh-base and dsh-web-app bundle
  4. // patches over the empty profile root through the vendored Loader (the same
  5. // layer stack the profile boot composes), patched the
  6. // snapshot way — so a real chromium exercises the real HTTP uplink/WebSocket
  7. // downlink, api-gateway, agent loop, tools, and persistence. Modes ride $DSH_SNAPSHOT:
  8. // replay (default, keyless: normally disables the direct DeepSeek rows and
  9. // inserts dsh-llm-replay in providers mode), record (real adapter + key,
  10. // harvests fixtures from live session memory), refresh (keyless replay that
  11. // rewrites goldens). A first-run option keeps the real adapter mounted while
  12. // masking its credential, without making a model call.
  13. //
  14. // Composition divergences from `dsh web`, all deliberate, all via include
  15. // patches after the shipped bundle layers, over the SAME tree (never a
  16. // second yml): temp persistenceRoot; host-level skill roots confined to the
  17. // temp workspace while project skill discovery remains real; agent-instructions
  18. // disabled (recorded fixtures must not embed this repo's AGENTS.md);
  19. // session-title-llm disabled (its fire-and-forget title call would race the
  20. // loop for the session's replay cursor); webserver pinned to port 0 with the
  21. // built dist; ordinary keyless modes disable both direct adapters and fill the open
  22. // llm seam post-boot with installLlmReplay on the settled root ctx
  23. // (the plugin-row path discards the ReplayHandle; the direct install keeps
  24. // assertConsumed for the teardown fixture-consumption check).
  25. import { existsSync, readFileSync } from 'node:fs'
  26. import { createHash } from 'node:crypto'
  27. import { mkdir, mkdtemp, readFile, readdir, realpath, rm, symlink, writeFile } from 'node:fs/promises'
  28. import { tmpdir } from 'node:os'
  29. import { basename, dirname, join, resolve } from 'node:path'
  30. import { pathToFileURL } from 'node:url'
  31. import type { Page } from 'playwright'
  32. import { expect } from 'vitest'
  33. import { Context } from '@deepseek-ai/cordis'
  34. import { DSH_LAUNCH_ENVIRONMENT_KEY, type LaunchEnvironmentSnapshot } from '@deepseek-ai/dsh-launch-environment'
  35. import Loader from '@deepseek-ai/cordis-plugin-loader'
  36. import Include, { type PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  37. import Group from '@deepseek-ai/cordis-plugin-group'
  38. import {
  39. captureExpectedWorkspaceSnapshot,
  40. captureWorkspaceSnapshot,
  41. assertSessionFixtureVersion,
  42. formatSystemPromptSnapshot,
  43. formatToolSchemasSnapshot,
  44. normalizedSystemPrompts,
  45. normalizedToolSchemas,
  46. parseSnapshotManifest,
  47. redactSessionSnapshotIds,
  48. normalizeSessionSnapshots,
  49. parseSessionFixtureName,
  50. scrubModelRequestBulk,
  51. scrubSessionSnapshot,
  52. sessionFixtureFiles,
  53. sessionFixtureName,
  54. stabilizeFixtureMessageIds,
  55. stabilizeRefreshLog,
  56. writesCurrentSessionFixtures,
  57. type NormalizeContext,
  58. } from '@deepseek-ai/dsh-session-snapshot'
  59. import type { Profile, ProfileContext, ProfileResolutionMode } from '@deepseek-ai/dsh-app-boot'
  60. import { dshHomePath } from '@deepseek-ai/dsh-home-paths'
  61. import { LlmAdapter } from '@deepseek-ai/dsh-llm'
  62. import type {
  63. LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo, RetryPolicyConfig, StreamChunk,
  64. } from '@deepseek-ai/dsh-llm'
  65. import type { ReplayHandle, ReplayProviderConfig } from '@deepseek-ai/dsh-llm-replay'
  66. import {
  67. installLlmReplay,
  68. parseSessionLog,
  69. prepareSessionSnapshotFixtureForComparison,
  70. } from '@deepseek-ai/dsh-llm-replay'
  71. import type { SessionFormatEvent } from '@deepseek-ai/dsh-session-format'
  72. import { sessionFormatCatalog } from '@deepseek-ai/dsh-session-format-catalog'
  73. import {
  74. SESSION_FORMAT_VERSION,
  75. SessionId,
  76. type Session,
  77. type SessionEvent,
  78. type SessionHeader,
  79. } from '@deepseek-ai/dsh-session'
  80. import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
  81. // Empty type imports carry the webServer/agents/sessionPersistence Context merges.
  82. import type {} from '@deepseek-ai/dsh-host-webserver'
  83. import type {} from '@deepseek-ai/dsh-agent'
  84. import { provideCmdline } from '@deepseek-ai/dsh-cmdline'
  85. import { REPO_ROOT, requireBuilt, requireDist } from './support.ts'
  86. type AppBoot = typeof import('@deepseek-ai/dsh-app-boot')
  87. let builtAppBoot: AppBoot | undefined
  88. /**
  89. * The launcher's own module, as built: the manager and HMR plugins the profile
  90. * loads reload the tree through this copy's registry of the root Include, so
  91. * the scaffold mounts through the same copy rather than the source import.
  92. * Resolved on the first launch, which needs the build anyway, so the fixture
  93. * helpers this module also exports load without one.
  94. */
  95. function appBoot(): AppBoot {
  96. builtAppBoot ??= requireBuilt('@deepseek-ai/dsh-app-boot') as AppBoot
  97. return builtAppBoot
  98. }
  99. // Host-side web e2e cannot import a browser package: doing so would pull that
  100. // package's complete TS project into this graph. Mirrored from
  101. // packages/client/ui-settings-models/src/onboarding-copy.ts; drift makes the
  102. // default pre-acknowledgement stop suppressing the notice and fails loudly.
  103. // import {
  104. // WELCOME_NOTICE_ACK_FIELD, WELCOME_NOTICE_SETTINGS_NAMESPACE,
  105. // WELCOME_NOTICE_VERSION, WELCOME_NOTICE_COPY,
  106. // } from '@deepseek-ai/dsh-client-ui-settings-models'
  107. export const WELCOME_NOTICE_SETTINGS_NAMESPACE = 'ui-onboarding'
  108. export const WELCOME_NOTICE_ACK_FIELD = 'welcomeNoticeVersion'
  109. export const WELCOME_NOTICE_VERSION = '2026-08-13.1'
  110. export const WELCOME_NOTICE_COPY = {
  111. zh: {
  112. title: '内测声明',
  113. body: 'DeepSeek Harness 目前的 0.1 版本仍处在面向 Harness 开发者进行测试的阶段,还有许多地方需要持续改进和打磨,希望听取广大开发者的反馈建议。预计 DeepSeek Harness 的核心插件以及基础 API 都会在接下来的一段时间内快速迭代、持续演化。\n\n我们期待与全球开发者一起,在开源、开放、可复用、可组合的基础设施之上,共同探索智能上限。欢迎全球 Harness 开发者加入 DSH 插件生态。',
  114. continueLabel: '继续',
  115. },
  116. } as const
  117. /** Snapshot mode for the lane, from $DSH_SNAPSHOT (same vocabulary as the other snapshot suites). */
  118. export type WebSnapshotMode = 'replay' | 'record' | 'refresh'
  119. /**
  120. * Resolve and validate the lane's snapshot mode.
  121. * @returns the active mode; unset/empty selects replay.
  122. */
  123. export function webSnapshotMode(): WebSnapshotMode {
  124. const value = process.env.DSH_SNAPSHOT
  125. if (value === undefined || value === '' || value === 'replay') return 'replay'
  126. if (value === 'record' || value === 'refresh') return value
  127. throw new Error(`DSH_SNAPSHOT must be replay, record, or refresh; got ${JSON.stringify(value)}`)
  128. }
  129. /**
  130. * Compare a session-driven Web scenario's complete workspace with its committed independent expected state.
  131. * @param scenarioDir - Absolute recorded-session scenario directory.
  132. * @param workspaceRoot - Absolute cwd used by the controlled session.
  133. */
  134. export async function assertFinalWorkspaceSnapshot(scenarioDir: string, workspaceRoot: string): Promise<void> {
  135. const manifestPath = join(scenarioDir, 'snapshot.yml')
  136. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  137. expect(manifest.workspace?.final, `${manifest.scenario ?? scenarioDir}: mutating Web scenario declares workspace.final`)
  138. .toBe(true)
  139. const actual = await captureWorkspaceSnapshot(workspaceRoot)
  140. const expected = await captureExpectedWorkspaceSnapshot(join(scenarioDir, 'workspace.expected'))
  141. expect(actual, `${manifest.scenario ?? scenarioDir}: complete final workspace`).toEqual(expected)
  142. }
  143. async function ownsReplayFixture(replayFixture: string | undefined): Promise<boolean> {
  144. if (replayFixture === undefined) return false
  145. const fixture = parseSessionFixtureName(basename(replayFixture))
  146. if (fixture === undefined || fixture.index !== 0) return false
  147. const manifestPath = join(dirname(replayFixture), 'snapshot.yml')
  148. if (!existsSync(manifestPath)) return false
  149. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  150. return manifest.session === undefined
  151. }
  152. /**
  153. * Resolve one requested fixture role to its highest committed generation.
  154. * @param path - any generation path for the requested parent or child role.
  155. * @param allowAbsent - Keep an absent canonical path only for an override-only replay script.
  156. * @returns the highest canonical sibling generation, or the input for non-Session files.
  157. */
  158. export async function selectedSessionFixture(path: string, allowAbsent = false): Promise<string> {
  159. const requested = parseSessionFixtureName(basename(path))
  160. if (requested === undefined) return path
  161. const entries = await readdir(dirname(path))
  162. if (allowAbsent && !entries.some(name => parseSessionFixtureName(name) !== undefined)) return path
  163. const selected = sessionFixtureFiles(entries)
  164. .find(candidate => candidate.index === requested.index)
  165. if (selected === undefined) throw new Error(`${path}: missing Session fixture role ${requested.index}`)
  166. const resolved = join(dirname(path), selected.name)
  167. assertSessionFixtureVersion(selected.name, await readFile(resolved, 'utf8'))
  168. return resolved
  169. }
  170. /**
  171. * Return the current-writer target without replacing the requested older fixture.
  172. * @param path - any canonical fixture generation for one role.
  173. * @param version - generation emitted by the current writer.
  174. * @returns the canonical sibling path for that role and generation.
  175. */
  176. export function recordedSessionFixturePath(path: string, version: number): string {
  177. const fixture = parseSessionFixtureName(basename(path))
  178. if (fixture === undefined) throw new Error(`record harvest: invalid Session fixture path ${path}`)
  179. return join(dirname(path), sessionFixtureName(fixture.index, version))
  180. }
  181. /** The shipped composition under test: the dsh-base and dsh-web-app bundle patches over the empty profile root. */
  182. const BASE_PATCH_PATH = join(REPO_ROOT, 'packages/bundle/base/cordis.patch.yml')
  183. const WEB_PATCH_PATH = join(REPO_ROOT, 'packages/bundle/web-app/cordis.patch.yml')
  184. /** The installation anchor whose dependency surface the profile module fallback mirrors. */
  185. const INSTALL_ANCHOR = join(REPO_ROOT, 'apps/cli/package.json')
  186. // Replay publishes the provider catalog the gateway routes to (providers
  187. // mode, never catch-all: with both direct adapters disabled no adapter exists, so a
  188. // catch-all would leave resolveModelInfo unroutable and compaction-basic's
  189. // post-step pressure check would warn every step). The published
  190. // contextWindow keeps that pressure path provably inert for small fixtures.
  191. const REPLAY_PROVIDERS = [{
  192. id: 'deepseek-official',
  193. name: 'DeepSeek',
  194. models: [
  195. { id: 'deepseek-v4-flash', name: 'DeepSeek-V4-Flash', contextWindow: 128_000 },
  196. {
  197. id: 'deepseek-v4-flash-vision-exp',
  198. name: 'DeepSeek-V4-Flash-Vision-Exp',
  199. contextWindow: 1_000_000,
  200. inputModalities: ['text', 'image'] as const,
  201. defaultMaxTokens: 256_000,
  202. reasoningEfforts: ['off', 'low', 'high', 'max'],
  203. defaultReasoningEffort: 'high',
  204. },
  205. ],
  206. }]
  207. /**
  208. * The routes a shipped composition always has, with no ability to stream.
  209. * A fixture-less keyless scenario issues no model calls, but its tree must
  210. * still answer `listProviders()` — surfaces legitimately gate on whether any
  211. * adapter serves a session's route, and an empty registry is a test artifact,
  212. * not a product state.
  213. */
  214. class RouteOnlyAdapter extends LlmAdapter {
  215. constructor(private readonly providers: typeof REPLAY_PROVIDERS) {
  216. super()
  217. }
  218. override providerInfo(provider: string): LlmProviderInfo {
  219. return { id: provider, name: this.providers.find(entry => entry.id === provider)?.name ?? provider }
  220. }
  221. override listModels(provider: string): Promise<readonly LlmModelInfo[]> {
  222. return Promise.resolve((this.providers.find(entry => entry.id === provider)?.models ?? [])
  223. .map(model => ({ provider, id: model.id, name: model.name })))
  224. }
  225. override resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
  226. const listed = this.providers.find(entry => entry.id === provider)?.models
  227. .find(entry => entry.id === model)
  228. return Promise.resolve({
  229. provider,
  230. id: model,
  231. name: listed?.name ?? model,
  232. ...listed?.contextWindow === undefined ? {} : { contextWindow: listed.contextWindow },
  233. })
  234. }
  235. override async *stream(): AsyncIterable<StreamChunk> {
  236. throw new Error(
  237. 'web e2e scaffold: a model call was issued by a scenario that declared no replay fixture'
  238. + ' — pass replayFixture, or keep the scenario free of model calls',
  239. )
  240. }
  241. }
  242. function replayProviders(contextWindow: number | undefined, messages: boolean): typeof REPLAY_PROVIDERS {
  243. return REPLAY_PROVIDERS.map(provider => ({
  244. ...provider,
  245. id: messages ? 'deepseek-messages' : provider.id,
  246. models: provider.models.map(model => ({
  247. ...model,
  248. ...contextWindow === undefined ? {} : { contextWindow },
  249. })),
  250. }))
  251. }
  252. /** A booted web scaffold: real composition, mode-selected model backend, temp world. */
  253. export interface WebScaffold {
  254. /** The active snapshot mode this scaffold booted under. */
  255. mode: WebSnapshotMode
  256. /** Browser-facing origin for the bound test server. */
  257. baseUrl: string
  258. /** Process-token URL that establishes this scaffold's browser session. */
  259. authenticatedUrl: string
  260. /** Settled root context (the in-process readiness barrier; headless event subscription is its sanctioned use). */
  261. ctx: Context
  262. /** Temp project directory sessions run in (shell/fs tool cwd). */
  263. workspaceCwd: string
  264. /** Temp persistence root (seeded sessions land here through the real API). */
  265. persistenceRoot: string
  266. /** Isolated harness home the settings/credentials rows write ($DSH_HOME double). */
  267. harnessHome: string
  268. /** Send a browser-equivalent Host request with this scaffold's authenticated cookie. */
  269. hostFetch(path: string, init?: RequestInit): Promise<Response>
  270. /** Await a settled turn end: in-process turn/end, then the agent's idle flip (which follows the persistence flush). */
  271. whenTurnSettled(timeoutMs?: number): Promise<SessionId>
  272. /**
  273. * Tear everything down; asserts the replay fixture was fully consumed first
  274. * (replay/refresh), unless booted with replayProvidersOnly (whose fixture
  275. * is validated call-free at boot).
  276. */
  277. close(): Promise<void>
  278. }
  279. /** Options for {@link launchWebScaffold}. */
  280. export interface LaunchOptions {
  281. /** Profile resolver backend used by this test Host; defaults to runtime coverage. */
  282. profileResolutionMode?: Extract<ProfileResolutionMode, 'dual' | 'runtime'>
  283. /** Enable the real Open In rows with deterministic launch-environment facts. */
  284. openInAppEnvironment?: LaunchEnvironmentSnapshot
  285. /** Compare the replayed root session with `replayFixture`; defaults on for a manifest-owned canonical recording. */
  286. compareReplaySession?: boolean
  287. /**
  288. * Optional product overlay applied after the shipped Web surface and before
  289. * the scaffold's hermetic test patches, matching the launcher's `--patch`
  290. * ordering.
  291. */
  292. extraOverlayPath?: string
  293. /**
  294. * Additional package manifests whose dependency closures supply experimental
  295. * profile layers named by {@link extraOverlayPath}.
  296. */
  297. extraInstallAnchors?: string[]
  298. /**
  299. * Manage the scaffold profile the way the launcher does: a `profileContext`
  300. * over the profile directory, whose manifest lists the shipped web bundles
  301. * and each package directory here as an installed dependency (`file:` in the
  302. * manifest, a symlink under the profile's `node_modules`); `enabled` also
  303. * lists a bundle in `dsh.profile.bundles`. The plugin manager mounts on such
  304. * a profile, and the root Include is mounted from the profile's own layers.
  305. * The base bundle's `hmr` row turns on with the profile context, so
  306. * configuration changes apply live; `hmr: false` disables that row through
  307. * an overlay, leaving changes for the next start.
  308. */
  309. profile?: {
  310. hmr?: boolean
  311. packages: { dir: string; enabled?: boolean }[]
  312. }
  313. /**
  314. * Replay fixture (session.jsonl) served by the inserted dsh-llm-replay row
  315. * in replay/refresh modes; ignored in record mode (the real adapter
  316. * answers). Omit for scenarios issuing no model calls — a stray stream then
  317. * fails loud with NO_ADAPTER (both direct adapters are disabled and no replay row
  318. * mounts). With {@link replayProvidersOnly}, the fixture must record no
  319. * model calls (its header alone mounts the catalog).
  320. */
  321. replayFixture?: string
  322. /** Explicit replay routes for scenarios exercising provider-dependent behavior; replay/refresh only. */
  323. replayProviders?: ReplayProviderConfig[]
  324. /**
  325. * Mount the replay provider catalog (the model directory the UI shows)
  326. * without consuming any recorded script: for scenarios that never call a
  327. * model but need the real provider/model labels rendered. Requires
  328. * {@link replayFixture} whose log records no model calls, and rejects
  329. * {@link replayOverride} and {@link replayChildFixtures}; the teardown
  330. * consumption check is skipped for this mode. `replayFixture` without this
  331. * flag keeps the consumption check.
  332. */
  333. replayProvidersOnly?: boolean
  334. /**
  335. * Recorded child logs assigned in child creation order. Each child owns its
  336. * own positional replay cursor across initial and continuation turns.
  337. */
  338. replayChildFixtures?: string[]
  339. /**
  340. * Optional replay.override.json sidecar (whole-script replacement or
  341. * `{ patches }` augmentation) for throw/hang scenarios not expressible as
  342. * recorded chunks; replay/refresh only.
  343. */
  344. replayOverride?: string
  345. /**
  346. * Retry policy registered on every replay provider route, for failure-
  347. * injection scenarios that must exhaust recovery quickly instead of walking
  348. * the shared normal default's five backed-off retries; replay/refresh only.
  349. */
  350. replayRetryPolicy?: RetryPolicyConfig
  351. /** Per-chunk replay pacing (ms) so the browser observes genuinely incremental SSE; replay/refresh only. */
  352. paceMs?: number
  353. /** Synthetic model capacity for UI scenarios whose seeded history must remain uncompacted. */
  354. replayContextWindow?: number
  355. /**
  356. * Tool presentation mode patched onto the shipped `tools` row (`code`
  357. * collapses the wire to run_code + the SDK prompt section). Omit for the
  358. * yml default. The PTC runtime row is always in the tree, so no extra
  359. * insertion is needed.
  360. */
  361. toolsMode?: 'native' | 'ptc' | 'both'
  362. /**
  363. * Insert the opt-in model-facing Cordis tool provider into the shipped tree.
  364. * Record and replay use the same tool surface, so captured request headers
  365. * remain reconstructable without making the tools a product default.
  366. */
  367. cordisTools?: boolean
  368. /**
  369. * Keep the shipped DeepSeek adapter mounted while masking the process
  370. * environment's DEEPSEEK_API_KEY for this scaffold lifetime. This is the
  371. * keyless first-run configuration lane; the default disables the adapter.
  372. */
  373. deepSeekMissingCredential?: boolean
  374. /** Record or replay a Messages scenario; older scenarios explicitly retain their recorded Chat Completions route. */
  375. deepSeekMessages?: boolean
  376. /** Leave the current welcome notice pending; ordinary scenarios pre-acknowledge it before browser boot. */
  377. welcomeNoticePending?: boolean
  378. /**
  379. * Patch the shipped DeepSeek search row to a deterministic endpoint and
  380. * credential reference. Browser search scenarios keep the real provider and
  381. * credentials seam while avoiding external search traffic and ambient keys.
  382. */
  383. deepSeekSearch?: {
  384. /** Anthropic-compatible base URL; the provider appends `/messages`. */
  385. baseURL: string
  386. /** Credential reference resolved by the shipped search provider. */
  387. apiKeyEnv: string
  388. }
  389. /**
  390. * Replace the roster row the scaffold pins by default (no configured roots,
  391. * default `standard` — the plugin's own shipped presets). Supply this only
  392. * to change WHICH presets a scenario sees beyond the shipped set — a
  393. * writable user root, a different default. The patch lands after the
  394. * default, so it wins.
  395. */
  396. agentPresets?: {
  397. /** Roots to discover after the plugin's shipped root, in precedence order. */
  398. roots: { path: string; trust: 'system' | 'user' }[]
  399. /** The preset a session that names none is composed from. */
  400. default: string
  401. }
  402. /**
  403. * Patch the telemetry exporter URL while preserving the shipped enabled
  404. * setting. A scenario-owned loopback collector contains all fixture uploads.
  405. */
  406. telemetryUrl?: string
  407. /** Mode when telemetryUrl is supplied; defaults to FEEDBACK_ONLY without enabling a disabled row. */
  408. telemetryMode?: 'FEEDBACK_ONLY'
  409. /** SDK batch cadence for a scenario-owned collector; omitted to retain the SDK default. */
  410. telemetryScheduledDelayMillis?: number
  411. /**
  412. * Browse through a trusted non-loopback hostname that the browser resolves
  413. * to loopback (for example `*.localhost`). The test server stays bound to
  414. * 127.0.0.1; a non-resolving authority fails before Host trust is exercised.
  415. */
  416. remoteAuthority?: string
  417. /** Reuse an existing harness home so a second Host can verify user settings across origins. */
  418. harnessHome?: string
  419. }
  420. /** Dispose the booted tree and remove both owned temp roots, reporting every independent cleanup failure. */
  421. async function cleanupScaffoldWorld(ctx: Context, workspaceCwd: string, persistenceRoot: string): Promise<unknown[]> {
  422. const failures: unknown[] = []
  423. await Promise.resolve(ctx.fiber.dispose()).catch((error: unknown) => failures.push(error))
  424. await rm(workspaceCwd, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  425. await rm(persistenceRoot, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  426. return failures
  427. }
  428. /**
  429. * Boot the real web composition under the current snapshot mode.
  430. * @param options - replay fixture selection and pacing.
  431. * @returns the running scaffold.
  432. */
  433. export async function launchWebScaffold(options: LaunchOptions = {}): Promise<WebScaffold> {
  434. requireDist()
  435. const {
  436. auditStartupEntries, composeEntries, createProfileResolutionGeneration, healProfilesModuleFallback, initProfile,
  437. mountRootInclude, readProfileManifest, readProfilePatches, loadOverlayPatches, PluginPackages,
  438. } = appBoot()
  439. const mode = webSnapshotMode()
  440. const replayFixture = options.replayFixture === undefined
  441. ? undefined
  442. : await selectedSessionFixture(options.replayFixture, options.replayOverride !== undefined)
  443. const replayChildFixtures = options.replayChildFixtures === undefined
  444. ? undefined
  445. : await Promise.all(options.replayChildFixtures.map(path => selectedSessionFixture(path)))
  446. const compareReplaySession = options.compareReplaySession ?? await ownsReplayFixture(replayFixture)
  447. const browserHost = options.remoteAuthority ?? '127.0.0.1'
  448. if (mode === 'record') {
  449. // Both owning vitest configs (web unconditionally, snapshot in record
  450. // mode) load the repo-root .env before this file runs.
  451. if (process.env.DEEPSEEK_API_KEY === undefined || process.env.DEEPSEEK_API_KEY.length === 0) {
  452. throw new Error('web e2e record mode needs DEEPSEEK_API_KEY (env or repo-root .env)')
  453. }
  454. }
  455. if (mode === 'record' && options.deepSeekMissingCredential === true) {
  456. throw new Error('deepSeekMissingCredential is a keyless replay/refresh option')
  457. }
  458. const maskDeepSeekCredential = mode !== 'record' && options.deepSeekMissingCredential === true
  459. const messages = options.deepSeekMessages === true
  460. const originalDeepSeekCredential = process.env.DEEPSEEK_API_KEY
  461. let credentialEnvironmentRestored = false
  462. const restoreCredentialEnvironment = (): void => {
  463. if (credentialEnvironmentRestored || !maskDeepSeekCredential) return
  464. credentialEnvironmentRestored = true
  465. if (originalDeepSeekCredential === undefined) {
  466. Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  467. } else {
  468. process.env.DEEPSEEK_API_KEY = originalDeepSeekCredential
  469. }
  470. }
  471. const workspaceCwd = await realpath(await mkdtemp(join(tmpdir(), 'dsh-web-e2e-ws-')))
  472. // Isolated harness home: the settings/credentials rows resolve $DSH_HOME
  473. // paths at load, and an in-process boot must NEVER touch the developer's
  474. // real ~/.dsh document or credential file.
  475. const harnessHome = options.harnessHome ?? join(workspaceCwd, '.dsh-home')
  476. // Skill discovery is model-visible input, and its roots now resolve inside a
  477. // PRESET — a subtree this lane's include patches cannot reach, because the
  478. // roster mounts it directly per session rather than as a row of the booted
  479. // tree. The row's documented fallback is the environment, so pin that: the
  480. // whole scaffold lifetime, not just the boot, since presets mount when a
  481. // session is created. Without this a developer's real ~/.dsh/skills silently
  482. // enters replay requests and goldens while CI sees none. `DSH_HOME` follows
  483. // the resolved harness home so a scaffold sharing another's home — the
  484. // cross-port persistence scenario — pins the same roots the settings and
  485. // credentials rows were configured with.
  486. const skillRootEnvironment = {
  487. DSH_HOME: harnessHome,
  488. DSH_AGENTS_HOME: join(workspaceCwd, '.agents-home'),
  489. DSH_BUNDLED_SKILL_DIR: join(workspaceCwd, '.bundled-skills'),
  490. }
  491. const originalSkillRootEnvironment = Object.fromEntries(
  492. Object.keys(skillRootEnvironment).map(key => [key, process.env[key]]),
  493. )
  494. let skillRootEnvironmentRestored = false
  495. const restoreSkillRootEnvironment = (): void => {
  496. if (skillRootEnvironmentRestored) return
  497. skillRootEnvironmentRestored = true
  498. for (const [key, value] of Object.entries(originalSkillRootEnvironment)) {
  499. if (value === undefined) Reflect.deleteProperty(process.env, key)
  500. else process.env[key] = value
  501. }
  502. }
  503. Object.assign(process.env, skillRootEnvironment)
  504. let persistenceRoot: string
  505. try {
  506. persistenceRoot = await mkdtemp(join(tmpdir(), 'dsh-web-e2e-sessions-'))
  507. } catch (error) {
  508. const failures: unknown[] = [error]
  509. await rm(workspaceCwd, { recursive: true, force: true }).catch((cleanupError: unknown) => failures.push(cleanupError))
  510. restoreSkillRootEnvironment()
  511. if (failures.length > 1) throw new AggregateError(failures, 'web scaffold temp-root setup failed')
  512. throw error
  513. }
  514. if (maskDeepSeekCredential) Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  515. // The include patch set — the same layer stack the profile boot composes
  516. // (bundle patches in dsh.profile.bundles order), applied over the SAME empty root (a
  517. // patch id that stops matching a row fails the boot sweep loudly instead of
  518. // drifting).
  519. const basePatches = loadOverlayPatches('web e2e scaffold', BASE_PATCH_PATH)
  520. const surfacePatches = loadOverlayPatches('web e2e scaffold', WEB_PATCH_PATH)
  521. const extraOverlayPatches = options.extraOverlayPath === undefined
  522. ? []
  523. : loadOverlayPatches('web e2e scaffold', options.extraOverlayPath)
  524. const composedRows = composeEntries([basePatches, surfacePatches, extraOverlayPatches])
  525. const webRuntimeConfig = composedRows.find(row => row.id === 'web-runtime')?.config as {
  526. surfaceContext?: boolean
  527. } | undefined
  528. const surfaceContext = webRuntimeConfig?.surfaceContext !== false
  529. // The scaffold's own overrides, above every bundle layer like `--patch` overlays.
  530. const overlayPatches: PatchOptions[] = [
  531. // Without HMR the profile applies configuration changes at its next start.
  532. ...options.profile?.hmr === false ? [{ id: 'hmr', disabled: true }] : [],
  533. { id: 'session-log-deepseek', config: { enabled: false } },
  534. // The historical Messages fixture retains its recorded route during replay;
  535. // live configuration uses the shared DeepSeek route. Explicit overlays win.
  536. ...messages
  537. ? [{ id: 'agent-default-model', config: { provider: mode === 'record' || maskDeepSeekCredential ? 'deepseek-official' : 'deepseek-messages', model: maskDeepSeekCredential ? 'deepseek-flash' : 'deepseek-v4-flash' } }]
  538. : mode === 'record' || options.deepSeekMissingCredential === true
  539. ? []
  540. : [{ id: 'agent-default-model', config: { provider: 'deepseek-official', model: 'deepseek-v4-flash' } }],
  541. ...extraOverlayPatches,
  542. // The roster's shipped presets are the plugin's own, bundled inside
  543. // `dsh-agent-presets` and prepended by it. Pin only the machine-local
  544. // root away: a developer's own `~/.dsh/.agent-presets` must not be able
  545. // to change a golden.
  546. {
  547. id: 'agent-presets',
  548. config: {
  549. default: 'standard',
  550. includeUserRoot: false,
  551. },
  552. },
  553. { id: 'session-persistence-jsonl', config: { root: persistenceRoot } },
  554. // Content search is enabled here although the shipped bundles default it
  555. // off (`openAt: never`, pinned by apps/cli/tests/lazy-search-startup):
  556. // the seeded-session scenarios navigate by content search, and these e2e
  557. // runs are the assembled coverage for the opt-in search path.
  558. { id: 'session-query-sqlite', config: { path: ':memory:', openAt: 'first-search' } },
  559. // storage-json's yml root is anchored to the real $DSH_HOME; pin the row
  560. // to an absolute temp root (removed with the workspace at close) so tests
  561. // never write the user's harness home.
  562. { id: 'storage-json', config: { root: join(workspaceCwd, '.dsh-storages') } },
  563. // Skill discovery is model-visible input. Pin every host-level root inside
  564. // the owned temp world so ~/.dsh, ~/.agents, and a bundled-root env setting
  565. // cannot change replay requests or conversation goldens. Project roots stay
  566. // enabled against the same empty temp workspace, preserving the real seam.
  567. {
  568. id: 'skill-filesystem',
  569. config: {
  570. dshHome: join(workspaceCwd, '.dsh-home'),
  571. agentsHome: join(workspaceCwd, '.agents-home'),
  572. bundledSkillDir: join(workspaceCwd, '.bundled-skills'),
  573. watch: false,
  574. },
  575. },
  576. // fs/bash cwd default to process.cwd(); the gateway injects the same
  577. // value into session.cwd — chdir below anchors all three to the temp
  578. // workspace, keeping the composition untouched.
  579. { id: 'agent-instructions', disabled: true },
  580. { id: 'session-title-llm', disabled: true },
  581. // Fixture sessions must never leave the process: the shipped row defaults
  582. // to the production OTLP endpoint (or whatever DSH_TELEMETRY_OTLP_URL
  583. // names in the ambient environment). A scenario with a local collector
  584. // preserves the shipped disabled setting instead of overriding it.
  585. options.telemetryUrl === undefined
  586. ? { id: 'session-telemetry-otel', disabled: true }
  587. : {
  588. id: 'session-telemetry-otel',
  589. config: {
  590. mode: options.telemetryMode ?? 'FEEDBACK_ONLY',
  591. exporter: { url: options.telemetryUrl },
  592. ...(options.telemetryScheduledDelayMillis === undefined ? {} : {
  593. processor: { scheduledDelayMillis: options.telemetryScheduledDelayMillis },
  594. }),
  595. shutdownTimeoutMillis: 1_000,
  596. },
  597. },
  598. // Use an ephemeral port while preserving the shipped compression policy;
  599. // a patch replaces the row's complete config.
  600. {
  601. id: 'webserver',
  602. config: {
  603. host: '127.0.0.1', port: 0, compression: 'gzip',
  604. compressionLevel: 1, compressionThresholdBytes: 1024,
  605. },
  606. },
  607. // The bundle's web-runtime row resolves the same built dist under test
  608. // (apps/web IS @deepseek-ai/dsh-web-frontend); native browser opening and the
  609. // URL line are disabled because this scaffold owns its Playwright browser.
  610. // Preserve the composed surface-context choice because a patch replaces
  611. // the row's complete config.
  612. { id: 'web-runtime', config: { openBrowser: false, printUrl: false, surfaceContext } },
  613. ...options.remoteAuthority === undefined
  614. ? []
  615. : [{ id: 'connection', config: { trustedHosts: [options.remoteAuthority] } }],
  616. { id: 'settings', config: { dshHome: harnessHome } },
  617. { id: 'credentials', config: { dshHome: harnessHome } },
  618. // The shipped directory-picker row is the -auto chooser, which resolves
  619. // the interaction from the RUNNING host (display, SSH launch, bind). The
  620. // lane's goldens are interaction-specific (workspace-management drives
  621. // the in-app browse dialog), so pin -browse deterministically on every
  622. // host: patch `name` is an assertion, not an override, hence the
  623. // disable+insert pair.
  624. { id: 'directory-picker', disabled: true },
  625. { insert: [
  626. { id: 'directory-picker-browse', name: '@deepseek-ai/dsh-host-directory-picker-browse' },
  627. { id: 'ui-directory-picker-browse', name: '@deepseek-ai/dsh-client-ui-directory-picker-browse' },
  628. ] },
  629. // Ordinary scenarios exclude host-dependent application discovery. The
  630. // Open In scenario supplies launch facts that suppress every native probe.
  631. { id: 'open-in-app', disabled: options.openInAppEnvironment === undefined },
  632. { id: 'ui-open-in-app', disabled: options.openInAppEnvironment === undefined },
  633. ...options.agentPresets === undefined
  634. ? []
  635. // Never the derived harness-home root: a developer's own presets must not
  636. // be able to change a golden, whatever roots a scenario asks for.
  637. : [{ id: 'agent-presets', config: { ...options.agentPresets, includeUserRoot: false } }],
  638. ...options.toolsMode === undefined ? [] : [{ id: 'tools', config: { mode: options.toolsMode } }],
  639. // The shipped Web bundle already owns both runners and the Cordis UI. This
  640. // scenario adds only the model-facing tools that exercise those services.
  641. ...options.cordisTools === true
  642. ? [{ insert: [
  643. { id: 'tool-cordis', name: '@deepseek-ai/dsh-tool-cordis' },
  644. ] }]
  645. : [],
  646. ...options.deepSeekSearch === undefined
  647. ? []
  648. : [{
  649. id: 'web-search-deepseek',
  650. config: {
  651. apiKeyEnv: options.deepSeekSearch.apiKeyEnv,
  652. baseURL: options.deepSeekSearch.baseURL,
  653. },
  654. }],
  655. ...maskDeepSeekCredential && !messages ? [] : [
  656. { id: 'llm-deepseek', disabled: mode !== 'record' && !maskDeepSeekCredential,
  657. config: messages ? {} : { protocol: 'chat-completions' } },
  658. ],
  659. ]
  660. const patches: PatchOptions[] = [...basePatches, ...surfacePatches, ...overlayPatches]
  661. // Sessions inherit the gateway's process.cwd() default; run the boot from
  662. // the temp workspace so tool cwd, session cwd, and fixtures agree.
  663. const originalCwd = process.cwd()
  664. const ctx = new Context()
  665. if (options.openInAppEnvironment !== undefined) ctx.provide(DSH_LAUNCH_ENVIRONMENT_KEY, options.openInAppEnvironment)
  666. const observedSessions = new Map<SessionId, Session>()
  667. const stopObservingSessions = ctx.on('session/created', (session) => {
  668. observedSessions.set(session.id, session)
  669. })
  670. let port = 0
  671. let baseUrl = ''
  672. let authenticatedUrl = ''
  673. let cookieHeader = ''
  674. let replayHandle: ReplayHandle | undefined
  675. try {
  676. process.chdir(workspaceCwd)
  677. const profileDir = join(harnessHome, 'profiles', 'scaffold')
  678. const extraLayers: Profile['layers'] = await Promise.all((options.extraInstallAnchors ?? []).map(async (anchor) => {
  679. const manifest = JSON.parse(await readFile(anchor, 'utf8')) as { name?: unknown }
  680. if (typeof manifest.name !== 'string' || manifest.name === '') {
  681. throw new Error(`web scaffold extra install anchor has no package name: ${anchor}`)
  682. }
  683. const packageDir = dirname(anchor)
  684. // A real profile already has each bundle installed by `dsh plugin add`.
  685. // Reproduce that link so a private bundle can import its own plugin.
  686. const installedLink = join(profileDir, 'node_modules', manifest.name)
  687. await mkdir(dirname(installedLink), { recursive: true })
  688. await symlink(packageDir, installedLink, 'junction')
  689. return {
  690. packageName: manifest.name,
  691. packageDir,
  692. patchPath: join(packageDir, 'cordis.patch.yml'),
  693. patches: [],
  694. }
  695. }))
  696. const profile: Profile = {
  697. name: 'scaffold',
  698. dir: profileDir,
  699. layers: extraLayers,
  700. patchPath: join(profileDir, 'cordis.patch.yml'),
  701. patches: [],
  702. }
  703. const profileResolutionMode = options.profileResolutionMode ?? 'runtime'
  704. const resolutionOptions = { installAnchor: INSTALL_ANCHOR, home: harnessHome, profile }
  705. const resolution = profileResolutionMode === 'runtime'
  706. ? await createProfileResolutionGeneration(resolutionOptions)
  707. : await healProfilesModuleFallback(resolutionOptions)
  708. await mkdir(profileDir, { recursive: true })
  709. const rootConfig = join(profileDir, 'cordis.yml')
  710. await writeFile(rootConfig, '[]\n')
  711. ctx.baseUrl = pathToFileURL(profileDir).href + '/'
  712. let profileContext: ProfileContext | undefined
  713. if (options.profile !== undefined) {
  714. // A real profile: the shipped web bundles plus each fixture package,
  715. // installed the way `dsh plugin add` leaves them.
  716. const dependencies: Record<string, string> = {}
  717. const bundles = ['@deepseek-ai/dsh-base', '@deepseek-ai/dsh-web-app']
  718. for (const entry of options.profile.packages) {
  719. const manifest = JSON.parse(await readFile(join(entry.dir, 'package.json'), 'utf8')) as { name: string }
  720. dependencies[manifest.name] = `file:${entry.dir}`
  721. if (entry.enabled === true) bundles.push(manifest.name)
  722. const link = join(profileDir, 'node_modules', manifest.name)
  723. await mkdir(dirname(link), { recursive: true })
  724. await symlink(entry.dir, link, 'junction')
  725. }
  726. initProfile(profileDir, bundles)
  727. const manifest = readProfileManifest('dsh', profileDir)
  728. manifest.dependencies = dependencies
  729. await writeFile(join(profileDir, 'package.json'), JSON.stringify(manifest, null, 2) + '\n')
  730. profileContext = {
  731. name: 'scaffold', dir: profileDir, patchPath: profile.patchPath, installAnchor: INSTALL_ANCHOR,
  732. cwd: workspaceCwd, home: harnessHome, startedBundles: bundles,
  733. overlays: overlayPatches, telemetryDisabledEnv: undefined,
  734. }
  735. // HMR gates file-driven reloads on application readiness, which the
  736. // launcher commits after boot; this direct harness is ready at once.
  737. ctx.provide('appReady', { onReady: (listener) => { listener(); return () => {} } })
  738. ctx.provide('profileContext', profileContext)
  739. }
  740. // This direct Loader harness supplies the same root-path capability as app-boot.
  741. ctx.provide('dshHomePath', dshHomePath)
  742. // A host with no command line still provides one: the web bundle's startup
  743. // row releases the rows waiting on it, and with no arguments each starts on
  744. // the values this scaffold composed above. An exit request can only come
  745. // from a rejected argument, which a fixed empty list has none of.
  746. provideCmdline(ctx, {
  747. args: [],
  748. exit: (code) => {
  749. throw new Error(`web e2e scaffold: the web app requested exit ${String(code)} with no arguments to reject`)
  750. },
  751. })
  752. await ctx.plugin(PluginPackages, {
  753. generation: resolution,
  754. behavior: profileResolutionMode === 'dual' ? 'verify' : 'enforce',
  755. })
  756. await ctx.plugin(Loader)
  757. if (profileContext === undefined) {
  758. ctx.loader.builtins.include = Include
  759. // `cordis:group` beside it, exactly as `boot()` registers it: a group row is
  760. // how a preset gives one `isolate` realm to a provider and its consumers,
  761. // and a preset resolving package names from its own directory cannot reach
  762. // `@deepseek-ai/cordis-plugin-group` by name.
  763. ctx.loader.builtins.group = Group
  764. await ctx.loader.create({
  765. name: 'cordis:include',
  766. config: { path: pathToFileURL(rootConfig).href, patches },
  767. })
  768. } else {
  769. // The launcher's own mount, so the manager's reloads find the root Include
  770. // and compose the same layers the profile files name; bare names still
  771. // resolve through the resolution generation above, as in the direct mount.
  772. await mountRootInclude(ctx, rootConfig, readProfilePatches('dsh', profileContext))
  773. }
  774. await ctx.loader.await()
  775. await auditStartupEntries(ctx, 'web e2e scaffold')
  776. if (options.welcomeNoticePending !== true) {
  777. await ctx.settings.mutate(WELCOME_NOTICE_SETTINGS_NAMESPACE, [{
  778. op: 'set', path: [WELCOME_NOTICE_ACK_FIELD], value: WELCOME_NOTICE_VERSION,
  779. }])
  780. }
  781. const boundPort = ctx.get('webServer')?.port
  782. if (boundPort === undefined) {
  783. throw new Error('web e2e scaffold: webServer service missing after settled boot')
  784. }
  785. port = boundPort
  786. // Fill the open llm seam on the settled root ctx. Ordinary keyless modes
  787. // disable the direct adapter; the first-run lane keeps the selected adapter but has no
  788. // replay fixture and never streams. The direct install, unlike the plugin
  789. // row, returns the ReplayHandle for the teardown consumption check.
  790. if (options.replayProvidersOnly) {
  791. if (replayFixture === undefined) {
  792. throw new Error('replayProvidersOnly requires replayFixture (its file supplies the header)')
  793. }
  794. const fixtureText = readFileSync(replayFixture, 'utf8')
  795. // The consumption check is skipped for this mode, so no script source
  796. // may carry callable entries: reject override/child sources outright
  797. // and any call-bearing fixture.
  798. if (options.replayOverride !== undefined || replayChildFixtures !== undefined) {
  799. throw new Error('replayProvidersOnly cannot combine with replayOverride or replayChildFixtures')
  800. }
  801. // A fixture without a session header row must not mount the catalog
  802. // silently: the consumption-skip assumes the header-only shape.
  803. let headerType: unknown
  804. try {
  805. headerType = (JSON.parse(fixtureText.trimStart().split('\n', 1)[0] ?? '') as { type?: unknown }).type
  806. } catch {
  807. headerType = undefined
  808. }
  809. if (headerType !== 'session') {
  810. throw new Error('replayProvidersOnly fixture must open with a session header row')
  811. }
  812. const recorded = parseSessionLog(fixtureText)
  813. const hasModelCall = recorded.some(event => (
  814. event.type === 'assistant/message' || event.type === 'assistant/attempt'
  815. || event.type === 'request/header' || event.type === 'tool/call'
  816. ))
  817. if (hasModelCall) {
  818. throw new Error('replayProvidersOnly fixture must record no model calls')
  819. }
  820. }
  821. if (mode !== 'record' && replayFixture !== undefined) {
  822. replayHandle = installLlmReplay(ctx, {
  823. file: replayFixture,
  824. providers: (options.replayProviders ?? replayProviders(options.replayContextWindow, messages)).map(provider => ({
  825. ...provider,
  826. ...(options.replayRetryPolicy === undefined ? {} : { retryPolicy: options.replayRetryPolicy }),
  827. })),
  828. ...(options.replayOverride === undefined ? {} : { overrideFile: options.replayOverride }),
  829. ...(replayChildFixtures === undefined ? {} : { childFiles: replayChildFixtures }),
  830. ...(options.paceMs === undefined ? {} : { paceMs: options.paceMs }),
  831. })
  832. } else if (mode !== 'record' && options.deepSeekMissingCredential !== true) {
  833. // No fixture and no shipped adapter would leave the tree with ZERO
  834. // provider routes — a state no product composition has, and one the
  835. // composer refuses to type into. Register the same routes
  836. // a fixture would, with streaming that still fails loud: the scenario
  837. // issues no model calls, and one that slipped in must not pass quietly.
  838. ctx.effect(() => ctx.llm.registerAdapter(
  839. replayProviders(options.replayContextWindow, messages).map(provider => provider.id),
  840. new RouteOnlyAdapter(replayProviders(options.replayContextWindow, messages)),
  841. ), 'web e2e scaffold: route-only adapter')
  842. }
  843. baseUrl = `http://${browserHost}:${String(port)}`
  844. authenticatedUrl = ctx.connection.authenticatedUrl(baseUrl)
  845. const login = await fetch(authenticatedUrl, { redirect: 'manual' })
  846. const setCookie = login.headers.get('set-cookie')
  847. if (login.status !== 303 || login.headers.get('location') !== '/' || setCookie === null) {
  848. throw new Error('web e2e scaffold: browser token exchange did not return its session cookie')
  849. }
  850. cookieHeader = setCookie.split(';', 1)[0] ?? ''
  851. if (cookieHeader.length === 0) {
  852. throw new Error('web e2e scaffold: browser token exchange returned an empty session cookie')
  853. }
  854. } catch (error) {
  855. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  856. const cleanupFailures = await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot)
  857. restoreCredentialEnvironment()
  858. restoreSkillRootEnvironment()
  859. if (cleanupFailures.length > 0) {
  860. throw new AggregateError([error, ...cleanupFailures], 'web scaffold setup failed and cleanup was incomplete')
  861. }
  862. throw error
  863. } finally {
  864. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  865. }
  866. return {
  867. harnessHome,
  868. mode,
  869. baseUrl,
  870. authenticatedUrl,
  871. ctx,
  872. workspaceCwd,
  873. persistenceRoot,
  874. hostFetch(path: string, init: RequestInit = {}): Promise<Response> {
  875. const headers = new Headers(init.headers)
  876. headers.set('cookie', cookieHeader)
  877. return fetch(new URL(path, baseUrl), { ...init, headers })
  878. },
  879. // Barrier stack: the in-process turn/end identifies the session, its
  880. // explicit flush makes the transcript durable, and the caller's browser
  881. // settled-poll comes last because host completion strictly precedes render.
  882. whenTurnSettled(timeoutMs = mode === 'record' ? 180_000 : 30_000): Promise<SessionId> {
  883. return new Promise<SessionId>((resolveSettled, reject) => {
  884. const timer = setTimeout(() => {
  885. off()
  886. reject(new Error(`no turn/end within ${timeoutMs}ms`))
  887. }, timeoutMs)
  888. const off = ctx.on('session/event', (session: Session, event: SessionEvent) => {
  889. if (event.type !== 'turn/end') return
  890. clearTimeout(timer)
  891. off()
  892. ctx.sessions.flush(session)
  893. .then(() => { resolveSettled(session.id) }, reject)
  894. })
  895. })
  896. },
  897. async close(): Promise<void> {
  898. const failures: unknown[] = []
  899. if (mode !== 'record'
  900. && replayFixture !== undefined
  901. && options.replayProvidersOnly !== true
  902. && compareReplaySession) {
  903. try {
  904. await assertReplaySession(
  905. [...observedSessions.values()],
  906. replayFixture,
  907. mode,
  908. `http://${browserHost}:${port}`,
  909. harnessHome,
  910. )
  911. } catch (error) {
  912. failures.push(error)
  913. }
  914. }
  915. // Fixture-consumption check first, while the run's binding state is
  916. // still authoritative — a scenario that drove fewer model calls than
  917. // recorded fails here instead of drifting green. Skipped for
  918. // replayProvidersOnly, whose fixture is validated call-free at boot.
  919. if (!options.replayProvidersOnly) {
  920. try {
  921. replayHandle?.assertConsumed()
  922. } catch (error) {
  923. failures.push(error)
  924. }
  925. }
  926. try {
  927. stopObservingSessions()
  928. failures.push(...await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot))
  929. } finally {
  930. restoreCredentialEnvironment()
  931. restoreSkillRootEnvironment()
  932. }
  933. if (failures.length > 0) throw new AggregateError(failures, 'web scaffold teardown failed')
  934. },
  935. }
  936. }
  937. /**
  938. * Serialize a live session to the canonical raw session-JSONL layout — the
  939. * in-memory record-mode harvest, so the on-disk zstd default never matters.
  940. */
  941. function rawSessionLog(session: Session): string {
  942. const encodedEvents = (session.snapshotEvents() as unknown as readonly SessionFormatEvent[])
  943. .map(event => sessionFormatCatalog.encodeCurrentEvent(event))
  944. const header = sessionFormatCatalog.encodeCurrentHeader({
  945. ...session.header,
  946. delegationDepth: session.header.delegationDepth ?? 0,
  947. }, session.inheritedEventCount)
  948. return [
  949. JSON.stringify(header),
  950. ...encodedEvents.map(record => JSON.stringify(record)),
  951. '',
  952. ].join('\n')
  953. }
  954. function mapJsonStringValues(value: unknown, map: (value: string) => string): unknown {
  955. if (typeof value === 'string') return map(value)
  956. if (Array.isArray(value)) return value.map(item => mapJsonStringValues(item, map))
  957. if (value !== null && typeof value === 'object') {
  958. return Object.fromEntries(Object.entries(value).map(([key, item]) => [
  959. key,
  960. mapJsonStringValues(item, map),
  961. ]))
  962. }
  963. return value
  964. }
  965. /** Tokenize the browser timezone carried by user message sources. */
  966. function normalizeClientTimeZones(value: unknown): unknown {
  967. if (Array.isArray(value)) return value.map(item => normalizeClientTimeZones(item))
  968. if (value !== null && typeof value === 'object') {
  969. const next = Object.fromEntries(Object.entries(value).map(([key, item]) => [
  970. key,
  971. normalizeClientTimeZones(item),
  972. ]))
  973. const source = (next as { source?: unknown }).source
  974. if (source !== null && typeof source === 'object'
  975. && (source as { kind?: unknown }).kind === 'user'
  976. && typeof (source as { clientTimeZone?: unknown }).clientTimeZone === 'string') {
  977. return {
  978. ...next,
  979. source: { ...source, clientTimeZone: '{{clientTimeZone}}' },
  980. }
  981. }
  982. return next
  983. }
  984. return value
  985. }
  986. const WEB_PATH_TEXT_BOUNDARY_RE = /[\s<>'"`()\[\]{},;:!?=]/
  987. const WEB_FILE_URI_PATH_PREFIX_RE = /(?:^|[^a-z0-9+.-])file:\/\/\/?$/i
  988. function isWebCwdMatch(value: string, start: number, length: number): boolean {
  989. const before = value[start - 1]
  990. const after = value[start + length]
  991. const afterPunctuation = value[start + length + 1]
  992. const startsAtBoundary = before === undefined
  993. || WEB_PATH_TEXT_BOUNDARY_RE.test(before)
  994. || WEB_FILE_URI_PATH_PREFIX_RE.test(value.slice(0, start))
  995. const endsAtBoundary = after === undefined
  996. || after === '/'
  997. || after === '\\'
  998. || WEB_PATH_TEXT_BOUNDARY_RE.test(after)
  999. || after === '.' && (afterPunctuation === undefined || WEB_PATH_TEXT_BOUNDARY_RE.test(afterPunctuation))
  1000. return startsAtBoundary && endsAtBoundary
  1001. }
  1002. function replaceWebCwd(value: string, cwd: string): string {
  1003. let cursor = 0
  1004. let normalized = ''
  1005. while (cursor < value.length) {
  1006. const match = value.indexOf(cwd, cursor)
  1007. if (match < 0) return normalized + value.slice(cursor)
  1008. const end = match + cwd.length
  1009. if (isWebCwdMatch(value, match, cwd.length)) {
  1010. normalized += value.slice(cursor, match) + '{{cwd}}'
  1011. cursor = end
  1012. } else {
  1013. normalized += value.slice(cursor, end)
  1014. cursor = end
  1015. }
  1016. }
  1017. return normalized
  1018. }
  1019. /**
  1020. * Normalize Web-only volatile strings while preserving JSON structure and row framing.
  1021. * @param log - raw Session JSONL.
  1022. * @param workspaceCwd - optional scaffold parent used before a live Session selects its cwd.
  1023. * @returns compact JSONL with run-local strings tokenized.
  1024. */
  1025. export function normalizeWebSessionVolatiles(log: string, workspaceCwd?: string): string {
  1026. const headerLine = log.split(/\r?\n/).find(line => line.trim().length > 0)
  1027. const header = headerLine === undefined ? undefined : JSON.parse(headerLine) as { cwd?: unknown }
  1028. const sessionCwd = typeof header?.cwd === 'string' && header.cwd.length > 0 ? header.cwd : undefined
  1029. const cwdSpellings = [...new Set([sessionCwd ?? workspaceCwd]
  1030. .filter((value): value is string => typeof value === 'string' && value.length > 0)
  1031. .flatMap((value) => {
  1032. const forward = value.replaceAll('\\', '/')
  1033. const native = /^[A-Za-z]:[\\/]/.test(value) ? forward.replaceAll('/', '\\') : value
  1034. return [value, value.replaceAll('\\', '\\\\'), forward, native]
  1035. }))].sort((left, right) => right.length - left.length)
  1036. return log.split(/\r?\n/).map((line) => {
  1037. if (line.trim() === '') return line
  1038. const record = normalizeClientTimeZones(mapJsonStringValues(JSON.parse(line), (value) => {
  1039. let normalized = value
  1040. .replace(/Anonymous user: [0-9a-f-]{36}(?=\.$)/gi, 'Anonymous user: {{anonymousUserId}}')
  1041. for (const cwd of cwdSpellings) normalized = replaceWebCwd(normalized, cwd)
  1042. return normalized
  1043. })) as { type?: unknown; data?: { endpoint?: unknown } }
  1044. if (record.type === 'web/deepseek-search-llm-request' && typeof record.data?.endpoint === 'string') {
  1045. record.data.endpoint = '{{webSearchEndpoint}}'
  1046. }
  1047. return JSON.stringify(record)
  1048. }).join('\n')
  1049. }
  1050. function stableSessionFixture(
  1051. session: Session,
  1052. existing: string,
  1053. workspaceCwd: string,
  1054. harnessHome: string,
  1055. ): string {
  1056. const prepared = prepareSessionSnapshotFixtureForComparison(
  1057. normalizeWebSessionVolatiles(rawSessionLog(session), workspaceCwd),
  1058. )
  1059. const stabilized = existing === ''
  1060. ? prepared
  1061. : stabilizeRefreshLog(prepared, existing, [], {
  1062. sessionIds: [String(session.id)],
  1063. cwd: workspaceCwd,
  1064. })
  1065. const fresh = scrubSessionSnapshot(stabilized)
  1066. .split(session.id).join('{{session:1}}')
  1067. .split(harnessHome).join('{{harnessHome}}')
  1068. const stable = redactSessionSnapshotIds(stabilizeFixtureMessageIds([fresh], [existing]))[0]
  1069. if (stable === undefined) throw new Error('session harvest produced no stabilized fixture')
  1070. return stable
  1071. }
  1072. async function assertReplaySession(
  1073. sessions: readonly Session[],
  1074. fixturePath: string,
  1075. mode: WebSnapshotMode,
  1076. webUrl: string,
  1077. harnessHome: string,
  1078. ): Promise<void> {
  1079. let expected = await readFile(fixturePath, 'utf8')
  1080. const fixtureDir = dirname(fixturePath)
  1081. const manifestPath = join(fixtureDir, 'snapshot.yml')
  1082. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  1083. let expectedPath = fixturePath
  1084. const userPrompts = fixtureUserPrompts(expected)
  1085. const candidates = sessions.filter((session) => {
  1086. if (session.header.parentSession !== undefined) return false
  1087. const actual = session.snapshotEvents().flatMap((event) => {
  1088. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return []
  1089. const text = event.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  1090. return text.length === 0 ? [] : [text]
  1091. })
  1092. return JSON.stringify(actual) === JSON.stringify(userPrompts)
  1093. })
  1094. expect(candidates, `Web replay fixture ${fixturePath} must match one live root session`).toHaveLength(1)
  1095. const session = candidates[0] as Session
  1096. const sessionCwd = session.header.cwd
  1097. if (sessionCwd === undefined) throw new Error(`${fixturePath}: replayed session has no cwd`)
  1098. const actual = rawSessionLog(session)
  1099. if (mode === 'refresh' && writesCurrentSessionFixtures(manifest, mode)) {
  1100. expected = stableSessionFixture(session, expected, sessionCwd, harnessHome)
  1101. expectedPath = recordedSessionFixturePath(fixturePath, session.header.version)
  1102. await writeFile(expectedPath, expected)
  1103. }
  1104. const expectedHeader = JSON.parse(expected.split('\n').find(line => line.trim() !== '') ?? '{}') as {
  1105. id?: unknown
  1106. cwd?: unknown
  1107. }
  1108. const actualContext: NormalizeContext = { sessionIds: [String(session.id)], cwd: sessionCwd }
  1109. const expectedContext: NormalizeContext = {
  1110. sessionIds: typeof expectedHeader.id === 'string' ? [expectedHeader.id] : [],
  1111. cwd: typeof expectedHeader.cwd === 'string' ? expectedHeader.cwd : '\0no-cwd\0',
  1112. }
  1113. const actualSnapshot = normalizeSessionSnapshots([normalizeWebSessionVolatiles(actual)], actualContext)[0]
  1114. ?.split(harnessHome).join('{{harnessHome}}')
  1115. const expectedSnapshot = normalizeSessionSnapshots([normalizeWebSessionVolatiles(expected)], expectedContext)[0]
  1116. ?.split(harnessHome).join('{{harnessHome}}')
  1117. expect(actualSnapshot, `${fixturePath}: persisted replay`).toBe(expectedSnapshot)
  1118. if (manifest.header?.pin !== true) return
  1119. const normalizePrompt = (value: string): string => value
  1120. .split(REPO_ROOT).join('{{sourceRoot}}')
  1121. .split(webUrl).join('{{webUrl}}')
  1122. const prompts = normalizedSystemPrompts(actual, actualContext).map(normalizePrompt)
  1123. const schemas = normalizedToolSchemas(actual, actualContext)
  1124. const promptPath = join(fixtureDir, 'system-prompt.expected.md')
  1125. const schemaPath = join(fixtureDir, 'tool-schemas.expected.json')
  1126. const promptSnapshot = formatSystemPromptSnapshot(prompts[0] as string, prompts.slice(1))
  1127. const schemaSnapshot = formatToolSchemasSnapshot(schemas[0] as unknown[], schemas.slice(1))
  1128. if (mode === 'refresh') {
  1129. await Promise.all([writeFile(promptPath, promptSnapshot), writeFile(schemaPath, schemaSnapshot)])
  1130. }
  1131. expect(promptSnapshot, `${fixturePath}: system-prompt pin`).toBe(await readFile(promptPath, 'utf8'))
  1132. expect(schemaSnapshot, `${fixturePath}: tool-schema pin`).toBe(await readFile(schemaPath, 'utf8'))
  1133. }
  1134. /**
  1135. * Record-mode fixture write-back: harvest the live session, scrub the
  1136. * system-prompt text to {{system}} and header tool schemas to {{tools}},
  1137. * tokenize the run-local cwd, redact opaque identities with typed
  1138. * relationship-preserving tokens, and write the fixture.
  1139. * A manifest-retained historical generation makes the write-back a no-op.
  1140. * @param scaffold - the record-mode scaffold.
  1141. * @param sessionId - the driven session.
  1142. * @param fixturePath - the committed session.jsonl target.
  1143. */
  1144. export async function recordFixture(scaffold: WebScaffold, sessionId: SessionId, fixturePath: string): Promise<void> {
  1145. const agent = scaffold.ctx.agents.get(sessionId)
  1146. if (agent === undefined) throw new Error(`record harvest: no live agent for ${sessionId}`)
  1147. const manifestPath = join(dirname(fixturePath), 'snapshot.yml')
  1148. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  1149. if (!writesCurrentSessionFixtures(manifest, 'record')) return
  1150. const target = recordedSessionFixturePath(fixturePath, agent.session.header.version)
  1151. const existingPath = existsSync(target) ? target : fixturePath
  1152. const existing = existsSync(existingPath) ? await readFile(existingPath, 'utf8') : ''
  1153. await writeFile(target, stableSessionFixture(
  1154. agent.session,
  1155. existing,
  1156. scaffold.workspaceCwd,
  1157. scaffold.harnessHome,
  1158. ))
  1159. }
  1160. /**
  1161. * The user prompts recorded in a fixture, in order — the single source tying
  1162. * spec drive steps to recorded reality so script and fixture cannot drift.
  1163. * @param fixtureText - raw session.jsonl contents.
  1164. * @returns the recorded user prompt texts.
  1165. */
  1166. export function fixtureUserPrompts(fixtureText: string): string[] {
  1167. return parseSessionLog(fixtureText).flatMap((event) => {
  1168. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return []
  1169. const text = event.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  1170. return text.length > 0 ? [text] : []
  1171. })
  1172. }
  1173. /** Deterministic UUID used when a seed fixture's typed identity token is materialized. */
  1174. export function fixtureIdentity(
  1175. kind: 'message' | 'approval' | 'workflow' | 'command' | 'rpc' | 'retry' | 'id',
  1176. ordinal: number,
  1177. ): string {
  1178. const hex = createHash('sha256').update(`${kind}:${ordinal}`).digest('hex').slice(0, 32).split('')
  1179. hex[12] = '4'
  1180. hex[16] = ['8', '9', 'a', 'b'][Number.parseInt(hex[16] as string, 16) % 4] as string
  1181. return `${hex.slice(0, 8).join('')}-${hex.slice(8, 12).join('')}-${hex.slice(12, 16).join('')}-${hex.slice(16, 20).join('')}-${hex.slice(20).join('')}`
  1182. }
  1183. /**
  1184. * Realize a recorded seed fixture against one scaffold: substitute the
  1185. * `{{sessionId}}`/`{{cwd}}`/`{{harnessHome}}` placeholders and rewrite the
  1186. * recorded cwd to the scaffold's workspace. Idempotent, so a caller may realize early (e.g. to
  1187. * price content exactly as the host will fold it) and still pass the result
  1188. * through {@link seedSession}.
  1189. * @param scaffold - the booted scaffold whose workspace the seed targets.
  1190. * @param fixtureText - the committed seed fixture text.
  1191. * @param id - the session id the seed is realized for.
  1192. * @returns the realized fixture text.
  1193. */
  1194. export function realizeSeedFixture(scaffold: WebScaffold, fixtureText: string, id: string): string {
  1195. const firstLine = fixtureText.split(/\r?\n/).find(line => line.trim().length > 0)
  1196. const fixtureCwd = firstLine === undefined
  1197. ? undefined
  1198. : (JSON.parse(firstLine) as { cwd?: unknown }).cwd
  1199. return fixtureText.split(/\r?\n/).map((line) => {
  1200. if (line.trim() === '') return line
  1201. const realized = mapJsonStringValues(JSON.parse(line), (value) => {
  1202. let result = typeof fixtureCwd === 'string'
  1203. ? value.split(fixtureCwd).join(scaffold.workspaceCwd)
  1204. : value
  1205. result = result
  1206. .split('{{sessionId}}').join(id)
  1207. .split('{{session:1}}').join(id)
  1208. .replace(/\{\{session:([2-9]\d*)\}\}/g, (_token, ordinal: string) => `${id}-child-${ordinal}`)
  1209. .replace(/\{\{(message|approval|workflow|command|rpc|retry|id):([1-9]\d*)\}\}/g, (_token, kind: string, ordinal: string) =>
  1210. fixtureIdentity(kind as 'message' | 'approval' | 'workflow' | 'command' | 'rpc' | 'retry' | 'id', Number(ordinal)))
  1211. .split('{{harnessHome}}').join(scaffold.harnessHome)
  1212. .split('{{cwd}}').join(scaffold.workspaceCwd)
  1213. return result
  1214. })
  1215. return JSON.stringify(realized)
  1216. }).join('\n')
  1217. }
  1218. /**
  1219. * Parse a committed web seed fixture through the replay reader.
  1220. * @param fixtureText - session JSONL fixture contents.
  1221. * @returns the current header line, parsed header, and logical events.
  1222. */
  1223. /** Give a migrated fixture stream positive relative timing before its final wall-clock rebase. */
  1224. function spreadMigratedSeedStream(
  1225. stream: SessionEvent<'assistant/message'>['data']['stream'],
  1226. ): SessionEvent<'assistant/message'>['data']['stream'] {
  1227. let nextTime = 0
  1228. return stream.map((record) => {
  1229. if ('time' in record) {
  1230. const timed = { ...record, time: nextTime }
  1231. nextTime += 1
  1232. return timed
  1233. }
  1234. const timed = { ...record, time0: nextTime }
  1235. nextTime += record.dt.reduce((total, delta) => total + delta, 0) + 1
  1236. return timed
  1237. })
  1238. }
  1239. export function parseSeedFixture(fixtureText: string): {
  1240. headerLine: string
  1241. header: Record<string, unknown>
  1242. events: SessionEvent[]
  1243. } {
  1244. const sourceHeaderLine = fixtureText.split(/\r?\n/).find(line => line.trim().length > 0)
  1245. if (sourceHeaderLine === undefined) throw new Error('seed fixture has no session header')
  1246. const sourceHeader = JSON.parse(sourceHeaderLine) as { version?: unknown }
  1247. const current = prepareSessionSnapshotFixtureForComparison(fixtureText)
  1248. const headerLine = current.split(/\r?\n/).find(line => line.trim().length > 0)
  1249. if (headerLine === undefined) throw new Error('seed fixture has no session header')
  1250. const header = JSON.parse(headerLine) as Record<string, unknown>
  1251. if (header.type !== 'session') throw new Error('seed fixture must start with a session header')
  1252. const events = parseSessionLog(current).map((event) => {
  1253. if (sourceHeader.version === SESSION_FORMAT_VERSION) return event
  1254. if (event.type === 'assistant/message') {
  1255. return { ...event, data: { ...event.data, stream: spreadMigratedSeedStream(event.data.stream) } }
  1256. }
  1257. if (event.type === 'assistant/attempt') {
  1258. return { ...event, data: { ...event.data, stream: spreadMigratedSeedStream(event.data.stream) } }
  1259. }
  1260. return event
  1261. })
  1262. return { headerLine, header, events }
  1263. }
  1264. /**
  1265. * Render logical events as an envelope-free web seed fixture.
  1266. * @param headerLine - original session header line.
  1267. * @param events - logical session events in order.
  1268. * @returns projected session JSONL.
  1269. */
  1270. export function renderSeedFixture(
  1271. headerLine: string,
  1272. events: readonly ({ readonly seq: number; readonly time: number } & object)[],
  1273. ): string {
  1274. return [
  1275. headerLine,
  1276. ...events.map(({ seq: _seq, time: _time, ...event }) => JSON.stringify(event)),
  1277. '',
  1278. ].join('\n')
  1279. }
  1280. /** Re-anchor one projected embedded stream while preserving every intra-stream gap. */
  1281. function rebaseSeedStream(
  1282. stream: SessionEvent<'assistant/message'>['data']['stream'],
  1283. startAt: number,
  1284. ): SessionEvent<'assistant/message'>['data']['stream'] {
  1285. const first = stream[0]
  1286. if (first === undefined) return stream
  1287. const sourceStart = 'time' in first ? first.time : first.time0
  1288. const delta = startAt - sourceStart
  1289. return stream.map(record => 'time' in record
  1290. ? { ...record, time: record.time + delta }
  1291. : { ...record, time0: record.time0 + delta })
  1292. }
  1293. /** Last logical timestamp carried by an embedded Assistant stream. */
  1294. function seedStreamEnd(
  1295. stream: SessionEvent<'assistant/message'>['data']['stream'],
  1296. ): number | undefined {
  1297. let end: number | undefined
  1298. for (const record of stream) {
  1299. const recordEnd = 'time' in record
  1300. ? record.time
  1301. : record.time0 + record.dt.reduce((total, delta) => total + delta, 0)
  1302. end = end === undefined ? recordEnd : Math.max(end, recordEnd)
  1303. }
  1304. return end
  1305. }
  1306. /**
  1307. * Seed a recorded session fixture into the scaffold's persistence root
  1308. * through the real Session and JSONL APIs.
  1309. * @param scaffold - the target scaffold.
  1310. * @param fixtureText - raw recorded session.jsonl contents.
  1311. * @param id - the seeded session id.
  1312. * @param agentPreset - preset recorded by scenarios that assert resumed composition.
  1313. * @param options - deterministic metadata overrides for ordering-sensitive scenarios.
  1314. * @returns the seeded id.
  1315. */
  1316. export async function seedSession(
  1317. scaffold: WebScaffold,
  1318. fixtureText: string,
  1319. id: string,
  1320. agentPreset?: string,
  1321. options: { readonly createdAt?: number } = {},
  1322. ): Promise<SessionId> {
  1323. const decoded = parseSeedFixture(realizeSeedFixture(scaffold, fixtureText, id))
  1324. const events = decoded.events
  1325. if (events.length === 0) throw new Error('seed fixture has no events')
  1326. const last = events[events.length - 1]!
  1327. // An open final turn would be mutated by resume's crash repair on first
  1328. // open; a committed seed must be a closed recording.
  1329. if (last.type !== 'turn/end') throw new Error(`seed fixture must end in turn/end, got ${last.type}`)
  1330. const createdAt = options.createdAt ?? Date.now() - 60_000
  1331. const meta: SessionHeader = {
  1332. version: SESSION_FORMAT_VERSION,
  1333. id: SessionId(id),
  1334. createdAt,
  1335. isSeeded: false,
  1336. cwd: scaffold.workspaceCwd,
  1337. delegationDepth: 0,
  1338. ...agentPreset === undefined ? {} : { agentPreset },
  1339. }
  1340. const fixtureCreatedAt = decoded.header.createdAt
  1341. if (typeof fixtureCreatedAt !== 'number') {
  1342. throw new Error('seed fixture requires a numeric createdAt header')
  1343. }
  1344. const timeAnchor = fixtureCreatedAt === 0 ? createdAt : fixtureCreatedAt
  1345. let nextTime = timeAnchor
  1346. const materializedEvents: SessionEvent[] = events.map((event) => {
  1347. const time = nextTime
  1348. if (event.type === 'assistant/message') {
  1349. const stream = rebaseSeedStream(event.data.stream, time)
  1350. const completedAt = Math.max(time, seedStreamEnd(stream) ?? time)
  1351. nextTime = completedAt + 1
  1352. return {
  1353. ...event,
  1354. time: completedAt,
  1355. data: { ...event.data, stream },
  1356. }
  1357. }
  1358. if (event.type === 'assistant/attempt') {
  1359. const stream = rebaseSeedStream(event.data.stream, time)
  1360. const completedAt = Math.max(time, seedStreamEnd(stream) ?? time)
  1361. nextTime = completedAt + 1
  1362. return {
  1363. ...event,
  1364. time: completedAt,
  1365. data: { ...event.data, stream },
  1366. }
  1367. }
  1368. nextTime = time + 1
  1369. return { ...event, time }
  1370. })
  1371. await persistSeedSession(scaffold, meta, materializedEvents)
  1372. return meta.id
  1373. }
  1374. /** Materialize one detached Session fixture through the shipped JSONL provider. */
  1375. async function persistSeedSession(
  1376. scaffold: WebScaffold,
  1377. meta: SessionHeader,
  1378. events: readonly SessionEvent[],
  1379. ): Promise<void> {
  1380. const seeder = new Context()
  1381. try {
  1382. // Same root as the booted tree with the plugin's own default compression,
  1383. // so the host's directory-scan list() sees one consistent encoding.
  1384. await seeder.plugin(JsonlSessionPersistence, { root: scaffold.persistenceRoot })
  1385. const handle = await seeder.sessionPersistence.create(meta)
  1386. await handle.append(events)
  1387. await handle.close()
  1388. } finally {
  1389. await seeder.fiber.dispose()
  1390. }
  1391. }
  1392. /**
  1393. * Read one stored session's physical event log through a throwaway read
  1394. * handle. The physical log carries no synthetic closers: a resumed session
  1395. * shows the closers the loop appended durably, and a never-resumed
  1396. * interrupted log stays interrupted.
  1397. * @param scaffold - the booted scaffold whose persistence holds the session.
  1398. * @param id - the stored session to read.
  1399. * @returns the stored events.
  1400. */
  1401. export async function readPersistedEvents(scaffold: WebScaffold, id: SessionId): Promise<readonly SessionEvent[]> {
  1402. const handle = await scaffold.ctx.sessionPersistence.open(id, 'read')
  1403. try {
  1404. return (await handle.read()).events
  1405. } finally {
  1406. await handle.close()
  1407. }
  1408. }
  1409. /**
  1410. * Normalize an aria snapshot: uuid, cwd, workspace-basename, duration,
  1411. * decode-throughput, and path-sensitive compaction estimates collapse to
  1412. * stable tokens.
  1413. *
  1414. * Throughput needs a token for the same reason durations do, and no fixture
  1415. * can supply one: the figure divides a replayed step's output tokens by the
  1416. * wall time the local run took to stream them, so it moves between two runs
  1417. * on one machine (measured 69 → 70 tok/s) and swings wildly on a fast replay
  1418. * (26333 tok/s for a 3 ms stream).
  1419. */
  1420. /**
  1421. * Relative-time buckets rendered by a dated row, in both dictionaries.
  1422. *
  1423. * Opt-in per capture: a session-tree golden asserts its own literal age (a
  1424. * fresh row reads `now`, an older one does not), so collapsing the vocabulary
  1425. * everywhere would delete that assertion. A region whose rows are dated from
  1426. * live wall-clock state asks for it instead. Anchored on an aria label's
  1427. * closing quote, where the bucket is always last.
  1428. */
  1429. const ARIA_AGE =
  1430. /(?:now|\d+min|\d+h|\d+d|\d+mo|\d+y|刚刚|\d+分钟|\d+小时|\d+天|\d+个月|\d+年)(?=")/g
  1431. function normalizeAria(snapshot: string, workspaceCwd: string, age: boolean): string {
  1432. // The session heading renders the workspace's basename, not the full
  1433. // path, so both spellings must collapse to the token.
  1434. const base = workspaceCwd.split('/').pop()!
  1435. return (age ? snapshot.replace(ARIA_AGE, '{{age}}') : snapshot)
  1436. .split(workspaceCwd).join('{{cwd}}')
  1437. .split(base).join('{{workspace}}')
  1438. .replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '{{uuid}}')
  1439. // The optional space in `\d+m ?\d+s` covers both minute spellings: the
  1440. // stats line's compact `2m42s` and the message-chrome template's `2m 42s`.
  1441. .replace(
  1442. /~\d+(?:y(?: \d+mo)?|mo(?: \d+d)?)|\b(?:\d+d(?: \d+h(?: \d+m \d+s)?)?|\d+h \d+m \d+s|\d+m ?\d+s|\d+(?:\.\d+)?s|\d+(?:\.\d+)?ms)\b/g,
  1443. duration => duration.startsWith('~') ? duration : '{{duration}}',
  1444. )
  1445. .replace(/\b\d[\d,]*(?:\.\d+)? ms\b/g, '{{duration}}')
  1446. .replace(
  1447. /约\d+(?:年(?:\d+个月)?|个月(?:\d+天)?)|\d+(?:天(?:\d+小时(?:\d+分\d+秒)?)?|小时\d+分\d+秒|分\d+秒|(?:\.\d+)?秒)/g,
  1448. duration => duration.startsWith('约') ? duration : '{{duration}}',
  1449. )
  1450. .replace(/\d+(?:\.\d+)?(?= tok\/s(?!\w))/g, '{{throughput}}')
  1451. // Seeded compaction prices realized file paths, whose length differs
  1452. // between local worktrees and CI scratch directories.
  1453. .replace(/(Compacted \d+ history items \(~)\d+( tokens\))/g, '$1{{tokens}}$2')
  1454. // Session summaries and Message IconActions clocks cross calendar
  1455. // boundaries; collapse every shape so goldens stay stable across them.
  1456. .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g, '{{timestamp}}')
  1457. .replace(/\d{4}年\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  1458. .replace(/\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  1459. .replace(/(?<!\d)\d{1,2}:\d{2}:\d{2}(?:\.\d+)?(?:\s*[AP]M)?(?!\d)/gi, '{{clock}}')
  1460. .replace(/(?<!\d)\d{2}:\d{2}(?!\d)/g, '{{clock}}')
  1461. }
  1462. /**
  1463. * Capture the region's aria snapshot at a settled milestone: poll until two
  1464. * consecutive normalized captures are equal — a single-shot capture races the
  1465. * last React commits.
  1466. * @param page - the page under test.
  1467. * @param selector - the region locator selector.
  1468. * @param workspaceCwd - normalization input.
  1469. * @param options - `normalizeAge` collapses relative-time buckets to `{{age}}`
  1470. * for a region whose rows are dated from live wall-clock state;
  1471. * `replacements` tokenizes scenario-owned values before generic normalization.
  1472. * @returns the stable normalized snapshot.
  1473. */
  1474. export async function captureStableAria(
  1475. page: Page,
  1476. selector: string,
  1477. workspaceCwd: string,
  1478. options: {
  1479. normalizeAge?: boolean
  1480. replacements?: readonly (readonly [value: string, token: string])[]
  1481. } = {},
  1482. ): Promise<string> {
  1483. const region = page.locator(selector).first()
  1484. const age = options.normalizeAge === true
  1485. const normalize = (snapshot: string): string => {
  1486. for (const [value, token] of options.replacements ?? []) {
  1487. snapshot = snapshot.split(value).join(token)
  1488. }
  1489. return normalizeAria(snapshot, workspaceCwd, age)
  1490. }
  1491. let previous = normalize(await region.ariaSnapshot())
  1492. await expect.poll(async () => {
  1493. const current = normalize(await region.ariaSnapshot())
  1494. const stable = current === previous
  1495. previous = current
  1496. return stable
  1497. }, { timeout: 5_000, message: 'aria snapshot did not stabilize' }).toBe(true)
  1498. return previous
  1499. }
  1500. /**
  1501. * Capture a stable aria snapshot with every eligible Turn process expanded,
  1502. * then restore the controls that were closed before the capture.
  1503. * @param page - the page under test.
  1504. * @param selector - the region locator selector.
  1505. * @param workspaceCwd - normalization input.
  1506. * @param options - optional user-visible state to establish before capture.
  1507. * @returns the stable normalized expanded snapshot.
  1508. */
  1509. export async function captureExpandedTurnProcessAria(
  1510. page: Page,
  1511. selector: string,
  1512. workspaceCwd: string,
  1513. options: { scrollToBottom?: boolean } = {},
  1514. ): Promise<string> {
  1515. const controls = page.locator('[data-turn-process]')
  1516. const count = await controls.count()
  1517. expect(count).toBeGreaterThan(0)
  1518. const opened: number[] = []
  1519. for (let index = 0; index < count; index++) {
  1520. const control = controls.nth(index)
  1521. if (!await control.isVisible() || await control.getAttribute('aria-expanded') === 'true') continue
  1522. await control.click()
  1523. opened.push(index)
  1524. }
  1525. try {
  1526. if (options.scrollToBottom === true) {
  1527. const backToBottom = page.getByRole('button', { name: 'Back to bottom', exact: true })
  1528. const scroll = page.locator('[data-conversation-scroll]')
  1529. await expect.poll(async () => {
  1530. const distanceFromBottom = await scroll.evaluate((host) => {
  1531. host.scrollTop = host.scrollHeight
  1532. return host.scrollHeight - host.clientHeight - host.scrollTop
  1533. })
  1534. return Math.abs(distanceFromBottom) <= 1 && await backToBottom.count() === 0
  1535. }, { timeout: 10_000 }).toBe(true)
  1536. }
  1537. return await captureStableAria(page, selector, workspaceCwd)
  1538. } finally {
  1539. for (const index of opened.reverse()) {
  1540. const control = controls.nth(index)
  1541. if (await control.getAttribute('aria-expanded') === 'true') await control.click()
  1542. }
  1543. }
  1544. }
  1545. /**
  1546. * Compare a normalized golden, or rewrite it under refresh. Refresh is the
  1547. * ONLY writer: a missing golden in replay mode fails with the healing command
  1548. * instead of silently self-bootstrapping.
  1549. * @param goldenPath - the committed ui.expected.md path.
  1550. * @param actual - the stable normalized snapshot.
  1551. * @param mode - the active snapshot mode.
  1552. */
  1553. export async function compareOrRefreshGolden(goldenPath: string, actual: string, mode: WebSnapshotMode): Promise<void> {
  1554. const payload = `${actual}\n`
  1555. if (mode === 'refresh') {
  1556. await writeFile(goldenPath, payload)
  1557. return
  1558. }
  1559. if (!existsSync(goldenPath)) {
  1560. throw new Error(`missing golden ${goldenPath} — run DSH_SNAPSHOT=refresh pnpm run test:web to generate it`)
  1561. }
  1562. expect(payload).toBe(await readFile(goldenPath, 'utf8'))
  1563. }
  1564. /**
  1565. * Fixture-inventory guard: the scenario directory holds exactly the expected
  1566. * files and every committed JSONL is a header-scrubbed, typed-redaction fixed point.
  1567. * @param dir - the scenario snapshot directory.
  1568. * @param expected - the exact expected file inventory.
  1569. */
  1570. export async function assertFixtureInventory(dir: string, expected: string[]): Promise<void> {
  1571. const entries = (await readdir(dir)).sort()
  1572. const ownsManifest = entries.includes('snapshot.yml')
  1573. const artifacts = entries.filter(name => name !== 'snapshot.yml')
  1574. const roleInventory = (names: readonly string[]): string[] => [...new Set(names.map((name) => {
  1575. const fixture = parseSessionFixtureName(name)
  1576. return fixture === undefined ? name : sessionFixtureName(fixture.index, 0)
  1577. }))].sort()
  1578. expect(roleInventory(artifacts)).toEqual(roleInventory(expected))
  1579. if (ownsManifest) {
  1580. const manifestPath = join(dir, 'snapshot.yml')
  1581. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  1582. expect(manifest.profile).toBe('web')
  1583. if (manifest.session === undefined) {
  1584. expect(
  1585. artifacts.some(name => parseSessionFixtureName(name)?.index === 0),
  1586. `${dir}: session owner must carry a canonical parent Session fixture`,
  1587. ).toBe(true)
  1588. } else {
  1589. expect(existsSync(resolve(dir, manifest.session.source)), `${dir}: session source`).toBe(true)
  1590. }
  1591. }
  1592. for (const entry of artifacts.filter(name => name.endsWith('.jsonl'))) {
  1593. const content = await readFile(join(dir, entry), 'utf8')
  1594. expect(scrubModelRequestBulk(content), `${dir}/${entry} carries prompt text or tool-schema bulk`).toBe(content)
  1595. expect(redactSessionSnapshotIds([content]), `${dir}/${entry} carries unredacted identities`).toEqual([content])
  1596. }
  1597. }
  1598. /**
  1599. * Console tripwires: reconnect/gap-repair self-healing or a pageerror must
  1600. * fail the scenario, not mask a dead wire behind eventual consistency.
  1601. * @param page - the page under test.
  1602. * @returns live warning/pageerror collectors to assert empty at scenario end.
  1603. */
  1604. export function watchConsole(page: Page): { warnings: string[]; pageErrors: string[] } {
  1605. const warnings: string[] = []
  1606. const pageErrors: string[] = []
  1607. page.on('console', (message) => {
  1608. const text = message.text()
  1609. if (/connection lost|gap repair|discontinuous/i.test(text)) warnings.push(text)
  1610. })
  1611. page.on('pageerror', (error) => { pageErrors.push(String(error)) })
  1612. return { warnings, pageErrors }
  1613. }
  1614. /**
  1615. * Remove only connection-loss warnings emitted after an intentional reload.
  1616. * Earlier warnings and all gap-repair/discontinuity warnings remain fatal.
  1617. * @param tripwire - the live console-warning collector.
  1618. * @param warningStart - warning count captured immediately before reloading.
  1619. */
  1620. export function acknowledgeReloadConnectionLoss(
  1621. tripwire: ReturnType<typeof watchConsole>,
  1622. warningStart: number,
  1623. ): void {
  1624. const reloadWarnings = tripwire.warnings.splice(warningStart)
  1625. tripwire.warnings.push(...reloadWarnings.filter(text => !/connection lost/i.test(text)))
  1626. }