scaffold.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 shipped base plus web overlay through
  4. // the vendored Loader (the same include boot AppCLIEntry drives), patched the
  5. // snapshot way — so a real chromium exercises the real HTTP/SSE wire, the
  6. // api-gateway, agent loop, tools, and persistence. Modes ride $DSH_SNAPSHOT:
  7. // replay (default, keyless: normally disables the llm-deepseek row and
  8. // inserts dsh-llm-replay in providers mode), record (real adapter + key,
  9. // harvests fixtures from live session memory), refresh (keyless replay that
  10. // rewrites goldens). A first-run option keeps the real adapter mounted while
  11. // masking its credential, without making a model call.
  12. //
  13. // Composition divergences from `dsh web`, all deliberate, all via include
  14. // patches after the shipped surface overlay: temp persistenceRoot; local skill
  15. // roots confined to the temp workspace; workspace-context disabled (recorded
  16. // fixtures must not embed this repo's AGENTS.md); session-title-llm disabled
  17. // (its fire-and-forget title call would race the loop for the session's replay
  18. // cursor); webserver pinned to port 0 with the built dist; ordinary keyless
  19. // modes disable llm-deepseek and fill the open llm seam post-boot with
  20. // installLlmReplay on the settled root ctx
  21. // (the plugin-row path discards the ReplayHandle; the direct install keeps
  22. // assertConsumed for the teardown fixture-consumption check).
  23. import { existsSync } from 'node:fs'
  24. import { mkdtemp, readFile, readdir, realpath, rm, utimes, writeFile } from 'node:fs/promises'
  25. import { tmpdir } from 'node:os'
  26. import { join, resolve } from 'node:path'
  27. import { pathToFileURL } from 'node:url'
  28. import type { Page } from 'playwright'
  29. import { expect } from 'vitest'
  30. import { Context } from 'cordis'
  31. import Loader from '@cordisjs/plugin-loader'
  32. import Include, { type PatchOptions } from '@cordisjs/plugin-include'
  33. import { scrubRequestHeaders } from '@deepseek-ai/dsh-acp-snapshot'
  34. import { assertEntriesLoaded, loadOverlayPatches } from '@deepseek-ai/dsh-app-boot'
  35. import {
  36. WELCOME_NOTICE_ACK_FIELD, WELCOME_NOTICE_SETTINGS_NAMESPACE, WELCOME_NOTICE_VERSION,
  37. } from '@deepseek-ai/dsh-client-ui-settings-general'
  38. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  39. import type { ReplayHandle } from '@deepseek-ai/dsh-llm-replay'
  40. import { installLlmReplay, parseSessionLog } from '@deepseek-ai/dsh-llm-replay'
  41. import SessionStore, {
  42. packChunkRuns,
  43. SESSION_FORMAT_VERSION,
  44. SessionId,
  45. type Session,
  46. type SessionEvent,
  47. type SessionHeader,
  48. } from '@deepseek-ai/dsh-session'
  49. import SessionPersistenceJsonl from '@deepseek-ai/dsh-session-persistence-jsonl'
  50. import * as ToolCordis from '@deepseek-ai/dsh-tool-cordis'
  51. // Empty type imports carry the httpServer/agents/sessionPersistence Context merges.
  52. import type {} from '@deepseek-ai/dsh-host-webserver'
  53. import type {} from '@deepseek-ai/dsh-agent'
  54. import { DIST_INDEX, REPO_ROOT, requireDist } from './support.ts'
  55. /** Snapshot mode for the lane, from $DSH_SNAPSHOT (same vocabulary as the ACP/TUI suites). */
  56. export type WebSnapshotMode = 'replay' | 'record' | 'refresh'
  57. /**
  58. * Resolve and validate the lane's snapshot mode.
  59. * @returns the active mode; unset/empty selects replay.
  60. */
  61. export function webSnapshotMode(): WebSnapshotMode {
  62. const value = process.env.DSH_SNAPSHOT
  63. if (value === undefined || value === '' || value === 'replay') return 'replay'
  64. if (value === 'record' || value === 'refresh') return value
  65. throw new Error(`DSH_SNAPSHOT must be replay, record, or refresh; got ${JSON.stringify(value)}`)
  66. }
  67. /** The shipped composition under test: apps/cli's shared base and web overlay. */
  68. const CONFIG_PATH = join(REPO_ROOT, 'apps/cli/config/base.cordis.yml')
  69. const WEB_OVERLAY_PATH = join(REPO_ROOT, 'apps/cli/config/web.cordis.yml')
  70. // Replay publishes the provider catalog the gateway routes to (providers
  71. // mode, never catch-all: with llm-deepseek disabled no adapter exists, so a
  72. // catch-all would leave resolveModelInfo unroutable and compact-basic's
  73. // post-step pressure check would warn every step). The published
  74. // contextWindow keeps that pressure path provably inert for small fixtures.
  75. const REPLAY_PROVIDERS = [{
  76. id: 'deepseek-official',
  77. name: 'DeepSeek',
  78. models: [{ id: 'deepseek-v4-flash', name: 'DeepSeek-V4-Flash', contextWindow: 128_000 }],
  79. }]
  80. /** A booted web scaffold: real composition, mode-selected model backend, temp world. */
  81. export interface WebScaffold {
  82. /** The active snapshot mode this scaffold booted under. */
  83. mode: WebSnapshotMode
  84. /** Browser-facing origin (http://127.0.0.1:<bound port>). */
  85. baseUrl: string
  86. /** Settled root context (the in-process barrier seam; headless event subscription is its sanctioned use). */
  87. ctx: Context
  88. /** Temp project directory sessions run in (bash/fs tool cwd). */
  89. workspaceCwd: string
  90. /** Temp persistence root (seeded sessions land here through the real API). */
  91. persistenceRoot: string
  92. /** Isolated harness home the settings/credentials rows write ($DSH_HOME double). */
  93. harnessHome: string
  94. /** Await a settled turn end: in-process turn/end, then the agent's idle flip (which follows the persistence flush). */
  95. whenTurnSettled(timeoutMs?: number): Promise<SessionId>
  96. /** Tear everything down; asserts the replay fixture was fully consumed first (replay/refresh). */
  97. close(): Promise<void>
  98. }
  99. /** Options for {@link launchWebScaffold}. */
  100. export interface LaunchOptions {
  101. /**
  102. * Replay fixture (session.jsonl) served by the inserted dsh-llm-replay row
  103. * in replay/refresh modes; ignored in record mode (the real adapter
  104. * answers). Omit for scenarios issuing no model calls — a stray stream then
  105. * fails loud with NO_ADAPTER (llm-deepseek is disabled and no replay row
  106. * mounts).
  107. */
  108. replayFixture?: string
  109. /**
  110. * Optional replay.override.json sidecar (whole-script replacement or
  111. * `{ patches }` augmentation) for throw/hang scenarios not expressible as
  112. * recorded chunks; replay/refresh only.
  113. */
  114. replayOverride?: string
  115. /** Per-chunk replay pacing (ms) so the browser observes genuinely incremental SSE; replay/refresh only. */
  116. paceMs?: number
  117. /**
  118. * Tool presentation mode patched onto the shipped `tools` row (`code`
  119. * collapses the wire to run_code + the SDK prompt section). Omit for the
  120. * yml default. The code runtime row is always in the tree, so no extra
  121. * insertion is needed.
  122. */
  123. toolsMode?: 'native' | 'code' | 'both'
  124. /**
  125. * Insert the opt-in self-referential Cordis tools into the shipped tree.
  126. * Record and replay use the same tool surface, so captured request headers
  127. * remain reconstructable without making the tools a product default.
  128. */
  129. cordisTools?: boolean
  130. /**
  131. * Keep the shipped DeepSeek adapter mounted while masking the process
  132. * environment's DEEPSEEK_API_KEY for this scaffold lifetime. This is the
  133. * keyless first-run configuration lane; the default disables the adapter.
  134. */
  135. deepSeekMissingCredential?: boolean
  136. /**
  137. * Patch the shipped DeepSeek search row to a deterministic endpoint and
  138. * credential reference. Browser search scenarios keep the real provider and
  139. * credentials seam while avoiding external search traffic and ambient keys.
  140. */
  141. deepSeekSearch?: {
  142. /** Anthropic-compatible base URL; the provider appends `/messages`. */
  143. baseURL: string
  144. /** Credential reference resolved by the shipped search provider. */
  145. apiKeyEnv: string
  146. }
  147. /** Leave the current welcome notice unacknowledged; ordinary scenarios publish it as complete before browser boot. */
  148. welcomeNoticePending?: boolean
  149. }
  150. /** Dispose the booted tree and remove both owned temp roots, reporting every independent cleanup failure. */
  151. async function cleanupScaffoldWorld(ctx: Context, workspaceCwd: string, persistenceRoot: string): Promise<unknown[]> {
  152. const failures: unknown[] = []
  153. await Promise.resolve(ctx.fiber.dispose()).catch((error: unknown) => failures.push(error))
  154. await rm(workspaceCwd, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  155. await rm(persistenceRoot, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  156. return failures
  157. }
  158. /**
  159. * Boot the real web composition under the current snapshot mode.
  160. * @param options - replay fixture selection and pacing.
  161. * @returns the running scaffold.
  162. */
  163. export async function launchWebScaffold(options: LaunchOptions = {}): Promise<WebScaffold> {
  164. requireDist()
  165. const mode = webSnapshotMode()
  166. if (mode === 'record') {
  167. // Both owning vitest configs (web unconditionally, snapshot in record
  168. // mode) load the repo-root .env before this file runs.
  169. if (process.env.DEEPSEEK_API_KEY === undefined || process.env.DEEPSEEK_API_KEY.length === 0) {
  170. throw new Error('web e2e record mode needs DEEPSEEK_API_KEY (env or repo-root .env)')
  171. }
  172. }
  173. if (mode === 'record' && options.deepSeekMissingCredential === true) {
  174. throw new Error('deepSeekMissingCredential is a keyless replay/refresh option')
  175. }
  176. const maskDeepSeekCredential = mode !== 'record' && options.deepSeekMissingCredential === true
  177. const originalDeepSeekCredential = process.env.DEEPSEEK_API_KEY
  178. let credentialEnvironmentRestored = false
  179. const restoreCredentialEnvironment = (): void => {
  180. if (credentialEnvironmentRestored || !maskDeepSeekCredential) return
  181. credentialEnvironmentRestored = true
  182. if (originalDeepSeekCredential === undefined) {
  183. Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  184. } else {
  185. process.env.DEEPSEEK_API_KEY = originalDeepSeekCredential
  186. }
  187. }
  188. const workspaceCwd = await realpath(await mkdtemp(join(tmpdir(), 'dsh-web-e2e-ws-')))
  189. // Isolated harness home: the settings/credentials rows resolve $DSH_HOME
  190. // paths at load, and an in-process boot must NEVER touch the developer's
  191. // real ~/.dsh document or credential file.
  192. const harnessHome = join(workspaceCwd, '.dsh-home')
  193. let persistenceRoot: string
  194. try {
  195. persistenceRoot = await mkdtemp(join(tmpdir(), 'dsh-web-e2e-sessions-'))
  196. } catch (error) {
  197. const failures: unknown[] = [error]
  198. await rm(workspaceCwd, { recursive: true, force: true }).catch((cleanupError: unknown) => failures.push(cleanupError))
  199. if (failures.length > 1) throw new AggregateError(failures, 'web scaffold temp-root setup failed')
  200. throw error
  201. }
  202. if (maskDeepSeekCredential) Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  203. // The include patch set — the same mechanism AppCLIEntry and the ACP
  204. // snapshot overlay use, applied over the SAME shipped tree (a patch id that
  205. // stops matching a row fails the boot sweep loudly instead of drifting).
  206. const surfacePatches = loadOverlayPatches('web e2e scaffold', WEB_OVERLAY_PATH)
  207. const patches: PatchOptions[] = [
  208. ...surfacePatches,
  209. { id: 'session-persistence-jsonl', config: { root: persistenceRoot } },
  210. { id: 'session-query-sqlite', config: { path: ':memory:', openAt: 'first-search' } },
  211. // storage-json's yml root is anchored to the real $DSH_HOME; pin the row
  212. // to an absolute temp root (removed with the workspace at close) so tests
  213. // never write the user's harness home.
  214. { id: 'storage-json', config: { root: join(workspaceCwd, '.dsh-storages') } },
  215. // Skill discovery is model-visible input. Pin every host-level root inside
  216. // the owned temp world so ~/.dsh, ~/.agents, and a bundled-root env setting
  217. // cannot change replay requests or conversation goldens. Project roots stay
  218. // enabled against the same empty temp workspace, preserving the real seam.
  219. {
  220. id: 'skill-local',
  221. config: {
  222. dshHome: join(workspaceCwd, '.dsh-home'),
  223. agentsHome: join(workspaceCwd, '.agents-home'),
  224. bundledSkillDir: join(workspaceCwd, '.bundled-skills'),
  225. watch: false,
  226. },
  227. },
  228. // fs/bash cwd default to process.cwd(); the gateway injects the same
  229. // value into session.cwd — chdir below anchors all three to the temp
  230. // workspace, keeping the composition untouched.
  231. { id: 'workspace-context', disabled: true },
  232. { id: 'session-title-llm', disabled: true },
  233. // Fixture sessions must never leave the process: the shipped row defaults
  234. // to the production OTLP endpoint (or whatever DSH_TELEMETRY_OTLP_URL
  235. // names in the ambient environment).
  236. { id: 'telemetry-otel', disabled: true },
  237. { id: 'webserver', config: { host: '127.0.0.1', port: 0, distIndex: DIST_INDEX } },
  238. { id: 'settings', config: { dshHome: harnessHome } },
  239. { id: 'credentials', config: { dshHome: harnessHome } },
  240. // The shipped directory-picker row is the -auto chooser, which resolves
  241. // the interaction from the RUNNING host (display, SSH launch, bind). The
  242. // lane's goldens are interaction-specific (workspace-management drives
  243. // the in-app browse dialog), so pin -browse deterministically on every
  244. // host: patch `name` is an assertion, not an override, hence the
  245. // disable+insert pair.
  246. { id: 'directory-picker', disabled: true },
  247. { insert: [{ id: 'directory-picker-browse', name: '@deepseek-ai/dsh-host-directory-picker-browse' }] },
  248. ...options.toolsMode === undefined ? [] : [{ id: 'tools', config: { mode: options.toolsMode } }],
  249. ...options.cordisTools === true
  250. ? [{ insert: [{ id: 'tool-cordis', name: 'cordis:tool-cordis' }] }]
  251. : [],
  252. ...options.deepSeekSearch === undefined
  253. ? []
  254. : [{
  255. id: 'web-search-deepseek',
  256. config: {
  257. apiKeyEnv: options.deepSeekSearch.apiKeyEnv,
  258. baseURL: options.deepSeekSearch.baseURL,
  259. },
  260. }],
  261. ...mode === 'record' || options.deepSeekMissingCredential === true
  262. ? []
  263. : [{ id: 'llm-deepseek', disabled: true }],
  264. ]
  265. // Sessions inherit the gateway's process.cwd() default; run the boot from
  266. // the temp workspace so tool cwd, session cwd, and fixtures agree.
  267. const originalCwd = process.cwd()
  268. const ctx = new Context()
  269. let port = 0
  270. let replayHandle: ReplayHandle | undefined
  271. try {
  272. process.chdir(workspaceCwd)
  273. ctx.baseUrl = pathToFileURL(join(resolve(CONFIG_PATH), '..')).href + '/'
  274. await ctx.plugin(Loader)
  275. ctx.loader.builtins.include = Include
  276. // The shipped CLI deliberately has no dependency on this opt-in package.
  277. // Keep the Loader row real without broadening the product installation.
  278. if (options.cordisTools === true) ctx.loader.builtins['tool-cordis'] = ToolCordis
  279. await ctx.loader.create({
  280. name: 'cordis:include',
  281. config: { path: pathToFileURL(resolve(CONFIG_PATH)).href, patches },
  282. })
  283. await ctx.loader.await()
  284. assertEntriesLoaded(ctx, 'web e2e scaffold')
  285. if (options.welcomeNoticePending !== true) {
  286. await ctx.settings.mutate(settingsNamespace(WELCOME_NOTICE_SETTINGS_NAMESPACE), [{
  287. op: 'set', path: [WELCOME_NOTICE_ACK_FIELD], value: WELCOME_NOTICE_VERSION,
  288. }])
  289. }
  290. const boundPort = ctx.get('httpServer')?.port
  291. if (boundPort === undefined) {
  292. throw new Error('web e2e scaffold: httpServer service missing after settled boot')
  293. }
  294. port = boundPort
  295. // Fill the open llm seam on the settled root ctx. Ordinary keyless modes
  296. // disable llm-deepseek; the first-run lane keeps it mounted but has no
  297. // replay fixture and never streams. The direct install, unlike the plugin
  298. // row, returns the ReplayHandle for the teardown consumption check.
  299. if (mode !== 'record' && options.replayFixture !== undefined) {
  300. replayHandle = installLlmReplay(ctx, {
  301. file: options.replayFixture,
  302. providers: REPLAY_PROVIDERS,
  303. ...(options.replayOverride === undefined ? {} : { overrideFile: options.replayOverride }),
  304. ...(options.paceMs === undefined ? {} : { paceMs: options.paceMs }),
  305. })
  306. }
  307. } catch (error) {
  308. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  309. const cleanupFailures = await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot)
  310. restoreCredentialEnvironment()
  311. if (cleanupFailures.length > 0) {
  312. throw new AggregateError([error, ...cleanupFailures], 'web scaffold setup failed and cleanup was incomplete')
  313. }
  314. throw error
  315. } finally {
  316. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  317. }
  318. return {
  319. harnessHome,
  320. mode,
  321. baseUrl: `http://127.0.0.1:${port}`,
  322. ctx,
  323. workspaceCwd,
  324. persistenceRoot,
  325. // Barrier stack: the in-process turn/end identifies the session, then
  326. // agent.whenIdle() covers the persistence flush (the idle flip follows
  327. // the flush), and the caller's browser settled-poll comes last because
  328. // host completion strictly precedes render.
  329. whenTurnSettled(timeoutMs = mode === 'record' ? 180_000 : 30_000): Promise<SessionId> {
  330. return new Promise<SessionId>((resolveSettled, reject) => {
  331. const timer = setTimeout(() => {
  332. off()
  333. reject(new Error(`no turn/end within ${timeoutMs}ms`))
  334. }, timeoutMs)
  335. const off = ctx.on('session/event', (session: { id: SessionId }, event: SessionEvent) => {
  336. if (event.type !== 'turn/end') return
  337. clearTimeout(timer)
  338. off()
  339. const agent = ctx.agents.get(session.id)
  340. if (agent === undefined) {
  341. reject(new Error(`turn/end for ${session.id} but no live agent`))
  342. return
  343. }
  344. agent.whenIdle().then(() => { resolveSettled(session.id) }, reject)
  345. })
  346. })
  347. },
  348. async close(): Promise<void> {
  349. const failures: unknown[] = []
  350. // Fixture-consumption check first, while the run's binding state is
  351. // still authoritative — a scenario that drove fewer model calls than
  352. // recorded fails here instead of drifting green.
  353. try {
  354. replayHandle?.assertConsumed()
  355. } catch (error) {
  356. failures.push(error)
  357. }
  358. try {
  359. failures.push(...await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot))
  360. } finally {
  361. restoreCredentialEnvironment()
  362. }
  363. if (failures.length > 0) throw new AggregateError(failures, 'web scaffold teardown failed')
  364. },
  365. }
  366. }
  367. /**
  368. * Serialize a live session to the canonical raw session-JSONL layout — the
  369. * in-memory record-mode harvest, so the on-disk zstd default never matters.
  370. */
  371. function rawSessionLog(session: Session): string {
  372. return [
  373. JSON.stringify({ type: 'session', ...session.header }),
  374. ...packChunkRuns(session.events).map(record => JSON.stringify(record)),
  375. '',
  376. ].join('\n')
  377. }
  378. /**
  379. * Record-mode fixture write-back: harvest the live session, scrub request
  380. * headers to {{system}}/{{tools}} (TODO(web-header-pin): the web lane pins no
  381. * header class — a deliberate deviation logged in the Agent Note's deferred
  382. * work), tokenize the run-local session id, cwd, and browser RPC id
  383. * ({{sessionId}}/{{cwd}}/{{rpcId}}, the committed fixture convention —
  384. * re-records then diff only on real content), and write the fixture.
  385. * @param scaffold - the record-mode scaffold.
  386. * @param sessionId - the driven session.
  387. * @param fixturePath - the committed session.jsonl / seed.jsonl target.
  388. */
  389. export async function recordFixture(scaffold: WebScaffold, sessionId: SessionId, fixturePath: string): Promise<void> {
  390. const agent = scaffold.ctx.agents.get(sessionId)
  391. if (agent === undefined) throw new Error(`record harvest: no live agent for ${sessionId}`)
  392. const tokenized = scrubRequestHeaders(rawSessionLog(agent.session))
  393. .split(sessionId).join('{{sessionId}}')
  394. .split(scaffold.workspaceCwd).join('{{cwd}}')
  395. .replace(/"rpcId":"[^"]+"/g, '"rpcId":"{{rpcId}}"')
  396. await writeFile(fixturePath, tokenized)
  397. }
  398. /**
  399. * The user prompts recorded in a fixture, in order — the single source tying
  400. * spec drive steps to recorded reality so script and fixture cannot drift.
  401. * @param fixtureText - raw session.jsonl contents.
  402. * @returns the recorded user prompt texts.
  403. */
  404. export function fixtureUserPrompts(fixtureText: string): string[] {
  405. return parseSessionLog(fixtureText).flatMap((event) => {
  406. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return []
  407. const text = event.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  408. return text.length > 0 ? [text] : []
  409. })
  410. }
  411. /**
  412. * Seed a recorded session fixture into the scaffold's persistence root
  413. * through the REAL backend API (throwaway Context + SessionStore + JSONL
  414. * plugin — the semantic-checkpoint precedent), never raw file writes: no
  415. * knowledge of bucket hashing, filename encoding, or compression, and
  416. * malformed shapes fail loud at seed time. The fixture's tokenized identity
  417. * ({{sessionId}}/{{cwd}}) is realized for this world before parsing.
  418. * @param scaffold - the target scaffold.
  419. * @param fixtureText - raw recorded session.jsonl contents.
  420. * @param id - the seeded session id (stable for deterministic goldens).
  421. * @returns the seeded id.
  422. */
  423. export async function seedSession(scaffold: WebScaffold, fixtureText: string, id: string): Promise<SessionId> {
  424. const realized = fixtureText
  425. .split('{{sessionId}}').join(id)
  426. .split('{{cwd}}').join(scaffold.workspaceCwd)
  427. const fixtureCwd = (JSON.parse(realized.split('\n', 1)[0]!) as { cwd?: string }).cwd
  428. const rewritten = fixtureCwd === undefined
  429. ? realized
  430. : realized.split(fixtureCwd).join(scaffold.workspaceCwd)
  431. const events = parseSessionLog(rewritten)
  432. if (events.length === 0) throw new Error('seed fixture has no events')
  433. const last = events[events.length - 1]!
  434. // An open final turn would be mutated by resume's crash repair on first
  435. // open; a committed seed must be a closed recording.
  436. if (last.type !== 'turn/end') throw new Error(`seed fixture must end in turn/end, got ${last.type}`)
  437. const meta: SessionHeader = {
  438. version: SESSION_FORMAT_VERSION,
  439. id: SessionId(id),
  440. createdAt: Date.now() - 60_000,
  441. cwd: scaffold.workspaceCwd,
  442. delegationDepth: 0,
  443. }
  444. const seeder = new Context()
  445. try {
  446. await seeder.plugin(SessionStore)
  447. // Same root as the booted tree with the plugin's own default compression,
  448. // so the host's directory-scan list() sees one consistent encoding.
  449. await seeder.plugin(SessionPersistenceJsonl, { root: scaffold.persistenceRoot })
  450. await seeder.sessionPersistence.create(meta)
  451. await seeder.sessionPersistence.append(meta.id, events)
  452. // Deterministic sidebar order: cold summaries take updatedAt from mtime.
  453. const located = seeder.sessionPersistence.locate(meta)
  454. if (located !== undefined) {
  455. const backdated = new Date(meta.createdAt)
  456. await utimes(located.path, backdated, backdated)
  457. }
  458. } finally {
  459. await seeder.fiber.dispose()
  460. }
  461. return meta.id
  462. }
  463. /**
  464. * Normalize an aria snapshot: uuid, cwd, workspace-basename, and duration
  465. * volatility collapse to stable tokens.
  466. */
  467. function normalizeAria(snapshot: string, workspaceCwd: string): string {
  468. // The header breadcrumb renders the workspace's basename, not the full
  469. // path, so both spellings must collapse to the token.
  470. const base = workspaceCwd.split('/').pop()!
  471. return snapshot
  472. .split(workspaceCwd).join('{{cwd}}')
  473. .split(base).join('{{workspace}}')
  474. .replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '{{uuid}}')
  475. .replace(/\b\d+(?:\.\d+)?(?:ms|s|秒)\b/g, '{{duration}}')
  476. // Message IconActions clocks widen by calendar day/year; collapse every
  477. // shape so goldens stay stable across midnight and year boundaries.
  478. .replace(/\d{4}年\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  479. .replace(/\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  480. .replace(/(?<!\d)\d{2}:\d{2}(?!\d)/g, '{{clock}}')
  481. }
  482. /**
  483. * Capture the region's aria snapshot at a settled milestone: poll until two
  484. * consecutive normalized captures are equal — a single-shot capture races the
  485. * last React commits.
  486. * @param page - the page under test.
  487. * @param selector - the region locator selector.
  488. * @param workspaceCwd - normalization input.
  489. * @returns the stable normalized snapshot.
  490. */
  491. export async function captureStableAria(page: Page, selector: string, workspaceCwd: string): Promise<string> {
  492. const region = page.locator(selector).first()
  493. let previous = normalizeAria(await region.ariaSnapshot(), workspaceCwd)
  494. await expect.poll(async () => {
  495. const current = normalizeAria(await region.ariaSnapshot(), workspaceCwd)
  496. const stable = current === previous
  497. previous = current
  498. return stable
  499. }, { timeout: 5_000, message: 'aria snapshot did not stabilize' }).toBe(true)
  500. return previous
  501. }
  502. /**
  503. * Compare a normalized golden, or rewrite it under refresh. Refresh is the
  504. * ONLY writer: a missing golden in replay mode fails with the healing command
  505. * instead of silently self-bootstrapping.
  506. * @param goldenPath - the committed ui.expected.md path.
  507. * @param actual - the stable normalized snapshot.
  508. * @param mode - the active snapshot mode.
  509. */
  510. export async function compareOrRefreshGolden(goldenPath: string, actual: string, mode: WebSnapshotMode): Promise<void> {
  511. const payload = `${actual}\n`
  512. if (mode === 'refresh') {
  513. await writeFile(goldenPath, payload)
  514. return
  515. }
  516. if (!existsSync(goldenPath)) {
  517. throw new Error(`missing golden ${goldenPath} — run DSH_SNAPSHOT=refresh pnpm run test:web to generate it`)
  518. }
  519. expect(payload).toBe(await readFile(goldenPath, 'utf8'))
  520. }
  521. /**
  522. * Fixture-inventory guard (the TUI afterAll shape): the scenario directory
  523. * holds exactly the expected files and every committed JSONL is a scrub
  524. * fixed-point without a run-local browser RPC id.
  525. * @param dir - the scenario snapshot directory.
  526. * @param expected - the exact expected file inventory.
  527. */
  528. export async function assertFixtureInventory(dir: string, expected: string[]): Promise<void> {
  529. const entries = (await readdir(dir)).sort()
  530. expect(entries).toEqual([...expected].sort())
  531. for (const entry of entries.filter(name => name.endsWith('.jsonl'))) {
  532. const content = await readFile(join(dir, entry), 'utf8')
  533. expect(scrubRequestHeaders(content), `${dir}/${entry} carries request-header bulk`).toBe(content)
  534. expect(content, `${dir}/${entry} carries a run-local rpcId`)
  535. .not.toMatch(/"rpcId":"(?!\{\{rpcId\}\})[^"]+"/)
  536. }
  537. }
  538. /**
  539. * Console tripwires: reconnect/gap-repair self-healing or a pageerror must
  540. * fail the scenario, not mask a dead wire behind eventual consistency.
  541. * @param page - the page under test.
  542. * @returns live warning/pageerror collectors to assert empty at scenario end.
  543. */
  544. export function watchConsole(page: Page): { warnings: string[]; pageErrors: string[] } {
  545. const warnings: string[] = []
  546. const pageErrors: string[] = []
  547. page.on('console', (message) => {
  548. const text = message.text()
  549. if (/connection lost|gap repair|discontinuous/i.test(text)) warnings.push(text)
  550. })
  551. page.on('pageerror', (error) => { pageErrors.push(String(error)) })
  552. return { warnings, pageErrors }
  553. }
  554. /**
  555. * Remove only connection-loss warnings emitted after an intentional reload.
  556. * Earlier warnings and all gap-repair/discontinuity warnings remain fatal.
  557. * @param tripwire - the live console-warning collector.
  558. * @param warningStart - warning count captured immediately before reloading.
  559. */
  560. export function acknowledgeReloadConnectionLoss(
  561. tripwire: ReturnType<typeof watchConsole>,
  562. warningStart: number,
  563. ): void {
  564. const reloadWarnings = tripwire.warnings.splice(warningStart)
  565. tripwire.warnings.push(...reloadWarnings.filter(text => !/connection lost/i.test(text)))
  566. }