scaffold.ts 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 llm-deepseek row 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 llm-deepseek 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, 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 Loader from '@deepseek-ai/cordis-plugin-loader'
  35. import Include, { type PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  36. import Group from '@deepseek-ai/cordis-plugin-group'
  37. import {
  38. captureExpectedWorkspaceSnapshot,
  39. captureWorkspaceSnapshot,
  40. formatSystemPromptSnapshot,
  41. formatToolSchemasSnapshot,
  42. normalizedSystemPrompts,
  43. normalizedToolSchemas,
  44. parseSnapshotManifest,
  45. redactSessionSnapshotIds,
  46. normalizeSessionSnapshots,
  47. scrubRequestHeaders,
  48. scrubSessionSnapshot,
  49. stabilizeFixtureMessageIds,
  50. type NormalizeContext,
  51. } from '@deepseek-ai/dsh-session-snapshot'
  52. import {
  53. assertEntriesLoaded,
  54. composeEntries,
  55. healProfilesModuleFallback,
  56. loadOverlayPatches,
  57. } from '@deepseek-ai/dsh-app-boot'
  58. import { dshHomePath } from '@deepseek-ai/dsh-home-paths'
  59. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  60. import { LlmAdapter } from '@deepseek-ai/dsh-llm'
  61. import type {
  62. LlmModelInfo, LlmProviderInfo, LlmResolvedModelInfo, RetryPolicyConfig, StreamChunk,
  63. } from '@deepseek-ai/dsh-llm'
  64. import type { ReplayHandle } from '@deepseek-ai/dsh-llm-replay'
  65. import { installLlmReplay, parseSessionLog } from '@deepseek-ai/dsh-llm-replay'
  66. import SessionStore, {
  67. packChunkRuns,
  68. SESSION_FORMAT_VERSION,
  69. SessionId,
  70. type Session,
  71. type SessionEvent,
  72. type SessionHeader,
  73. } from '@deepseek-ai/dsh-session'
  74. import JsonlSessionPersistence from '@deepseek-ai/dsh-session-persistence-jsonl'
  75. // Empty type imports carry the webServer/agents/sessionPersistence Context merges.
  76. import type {} from '@deepseek-ai/dsh-host-webserver'
  77. import type {} from '@deepseek-ai/dsh-agent'
  78. import { provideCmdline } from '@deepseek-ai/dsh-cmdline'
  79. import { REPO_ROOT, requireDist } from './support.ts'
  80. // Host-side web e2e cannot import a browser package: doing so would pull that
  81. // package's complete TS project into this graph. Mirrored from
  82. // packages/client/ui-settings-models/src/onboarding-copy.ts; drift makes the
  83. // default pre-acknowledgement stop suppressing the notice and fails loudly.
  84. // import {
  85. // WELCOME_NOTICE_ACK_FIELD, WELCOME_NOTICE_SETTINGS_NAMESPACE,
  86. // WELCOME_NOTICE_VERSION, WELCOME_NOTICE_COPY,
  87. // } from '@deepseek-ai/dsh-client-ui-settings-models'
  88. export const WELCOME_NOTICE_SETTINGS_NAMESPACE = 'ui-onboarding'
  89. export const WELCOME_NOTICE_ACK_FIELD = 'welcomeNoticeVersion'
  90. export const WELCOME_NOTICE_VERSION = '2026-08-13.1'
  91. export const WELCOME_NOTICE_COPY = {
  92. zh: {
  93. title: '内测声明',
  94. body: 'DeepSeek Harness 目前的 0.1 版本仍处在面向 Harness 开发者进行测试的阶段,还有许多地方需要持续改进和打磨,希望听取广大开发者的反馈建议。预计 DeepSeek Harness 的核心插件以及基础 API 都会在接下来的一段时间内快速迭代、持续演化。\n\n我们期待与全球开发者一起,在开源、开放、可复用、可组合的基础设施之上,共同探索智能上限。欢迎全球 Harness 开发者加入 DSH 插件生态。',
  95. continueLabel: '继续',
  96. },
  97. } as const
  98. /** Snapshot mode for the lane, from $DSH_SNAPSHOT (same vocabulary as the other snapshot suites). */
  99. export type WebSnapshotMode = 'replay' | 'record' | 'refresh'
  100. /**
  101. * Resolve and validate the lane's snapshot mode.
  102. * @returns the active mode; unset/empty selects replay.
  103. */
  104. export function webSnapshotMode(): WebSnapshotMode {
  105. const value = process.env.DSH_SNAPSHOT
  106. if (value === undefined || value === '' || value === 'replay') return 'replay'
  107. if (value === 'record' || value === 'refresh') return value
  108. throw new Error(`DSH_SNAPSHOT must be replay, record, or refresh; got ${JSON.stringify(value)}`)
  109. }
  110. /**
  111. * Compare a session-driven Web scenario's complete workspace with its committed independent expected state.
  112. * @param scenarioDir - Absolute recorded-session scenario directory.
  113. * @param workspaceRoot - Absolute cwd used by the controlled session.
  114. */
  115. export async function assertFinalWorkspaceSnapshot(scenarioDir: string, workspaceRoot: string): Promise<void> {
  116. const manifestPath = join(scenarioDir, 'snapshot.yml')
  117. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  118. expect(manifest.workspace?.final, `${manifest.scenario ?? scenarioDir}: mutating Web scenario declares workspace.final`)
  119. .toBe(true)
  120. const actual = await captureWorkspaceSnapshot(workspaceRoot)
  121. const expected = await captureExpectedWorkspaceSnapshot(join(scenarioDir, 'workspace.expected'))
  122. expect(actual, `${manifest.scenario ?? scenarioDir}: complete final workspace`).toEqual(expected)
  123. }
  124. async function ownsReplayFixture(replayFixture: string | undefined): Promise<boolean> {
  125. if (replayFixture === undefined || basename(replayFixture) !== 'session.jsonl') return false
  126. const manifestPath = join(dirname(replayFixture), 'snapshot.yml')
  127. if (!existsSync(manifestPath)) return false
  128. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  129. return manifest.session === undefined
  130. }
  131. /** The shipped composition under test: the dsh-base and dsh-web-app bundle patches over the empty profile root. */
  132. const BASE_PATCH_PATH = join(REPO_ROOT, 'packages/bundle/base/cordis.patch.yml')
  133. const WEB_PATCH_PATH = join(REPO_ROOT, 'packages/bundle/web-app/cordis.patch.yml')
  134. /** The installation anchor whose dependency surface the profile module fallback mirrors. */
  135. const INSTALL_ANCHOR = join(REPO_ROOT, 'apps/cli/package.json')
  136. // Replay publishes the provider catalog the gateway routes to (providers
  137. // mode, never catch-all: with llm-deepseek disabled no adapter exists, so a
  138. // catch-all would leave resolveModelInfo unroutable and compaction-basic's
  139. // post-step pressure check would warn every step). The published
  140. // contextWindow keeps that pressure path provably inert for small fixtures.
  141. const REPLAY_PROVIDERS = [{
  142. id: 'deepseek-official',
  143. name: 'DeepSeek',
  144. models: [{ id: 'deepseek-v4-flash', name: 'DeepSeek-V4-Flash', contextWindow: 128_000 }],
  145. }]
  146. /**
  147. * The routes a shipped composition always has, with no ability to stream.
  148. * A fixture-less keyless scenario issues no model calls, but its tree must
  149. * still answer `listProviders()` — surfaces legitimately gate on whether any
  150. * adapter serves a session's route, and an empty registry is a test artifact,
  151. * not a product state.
  152. */
  153. class RouteOnlyAdapter extends LlmAdapter {
  154. constructor(private readonly providers: typeof REPLAY_PROVIDERS) {
  155. super()
  156. }
  157. override providerInfo(provider: string): LlmProviderInfo {
  158. return { id: provider, name: this.providers.find(entry => entry.id === provider)?.name ?? provider }
  159. }
  160. override listModels(provider: string): Promise<readonly LlmModelInfo[]> {
  161. return Promise.resolve((this.providers.find(entry => entry.id === provider)?.models ?? [])
  162. .map(model => ({ provider, id: model.id, name: model.name })))
  163. }
  164. override resolveModel(provider: string, model: string): Promise<LlmResolvedModelInfo> {
  165. const listed = this.providers.find(entry => entry.id === provider)?.models
  166. .find(entry => entry.id === model)
  167. return Promise.resolve({
  168. provider,
  169. id: model,
  170. name: listed?.name ?? model,
  171. ...listed?.contextWindow === undefined ? {} : { contextWindow: listed.contextWindow },
  172. })
  173. }
  174. override async *stream(): AsyncIterable<StreamChunk> {
  175. throw new Error(
  176. 'web e2e scaffold: a model call was issued by a scenario that declared no replay fixture'
  177. + ' — pass replayFixture, or keep the scenario free of model calls',
  178. )
  179. }
  180. }
  181. function replayProviders(contextWindow: number | undefined): typeof REPLAY_PROVIDERS {
  182. if (contextWindow === undefined) return REPLAY_PROVIDERS
  183. return REPLAY_PROVIDERS.map(provider => ({
  184. ...provider,
  185. models: provider.models.map(model => ({ ...model, contextWindow })),
  186. }))
  187. }
  188. /** A booted web scaffold: real composition, mode-selected model backend, temp world. */
  189. export interface WebScaffold {
  190. /** The active snapshot mode this scaffold booted under. */
  191. mode: WebSnapshotMode
  192. /** Browser-facing origin for the bound test server. */
  193. baseUrl: string
  194. /** Settled root context (the in-process readiness barrier; headless event subscription is its sanctioned use). */
  195. ctx: Context
  196. /** Temp project directory sessions run in (shell/fs tool cwd). */
  197. workspaceCwd: string
  198. /** Temp persistence root (seeded sessions land here through the real API). */
  199. persistenceRoot: string
  200. /** Isolated harness home the settings/credentials rows write ($DSH_HOME double). */
  201. harnessHome: string
  202. /** Await a settled turn end: in-process turn/end, then the agent's idle flip (which follows the persistence flush). */
  203. whenTurnSettled(timeoutMs?: number): Promise<SessionId>
  204. /**
  205. * Tear everything down; asserts the replay fixture was fully consumed first
  206. * (replay/refresh), unless booted with replayProvidersOnly (whose fixture
  207. * is validated call-free at boot).
  208. */
  209. close(): Promise<void>
  210. }
  211. /** Options for {@link launchWebScaffold}. */
  212. export interface LaunchOptions {
  213. /** Compare the replayed root session with `replayFixture`; defaults on for a manifest-owned canonical recording. */
  214. compareReplaySession?: boolean
  215. /**
  216. * Optional product overlay applied after the shipped Web surface and before
  217. * the scaffold's hermetic test patches, matching the launcher's `--patch`
  218. * ordering.
  219. */
  220. extraOverlayPath?: string
  221. /**
  222. * Replay fixture (session.jsonl) served by the inserted dsh-llm-replay row
  223. * in replay/refresh modes; ignored in record mode (the real adapter
  224. * answers). Omit for scenarios issuing no model calls — a stray stream then
  225. * fails loud with NO_ADAPTER (llm-deepseek is disabled and no replay row
  226. * mounts). With {@link replayProvidersOnly}, the fixture must record no
  227. * model calls (its header alone mounts the catalog).
  228. */
  229. replayFixture?: string
  230. /**
  231. * Mount the replay provider catalog (the model directory the UI shows)
  232. * without consuming any recorded script: for scenarios that never call a
  233. * model but need the real provider/model labels rendered. Requires
  234. * {@link replayFixture} whose log records no model calls, and rejects
  235. * {@link replayOverride} and {@link replayChildFixtures}; the teardown
  236. * consumption check is skipped for this mode. `replayFixture` without this
  237. * flag keeps the consumption check.
  238. */
  239. replayProvidersOnly?: boolean
  240. /**
  241. * Recorded child logs assigned in child creation order. Each child owns its
  242. * own positional replay cursor across initial and continuation turns.
  243. */
  244. replayChildFixtures?: string[]
  245. /**
  246. * Optional replay.override.json sidecar (whole-script replacement or
  247. * `{ patches }` augmentation) for throw/hang scenarios not expressible as
  248. * recorded chunks; replay/refresh only.
  249. */
  250. replayOverride?: string
  251. /**
  252. * Retry policy registered on every replay provider route, for failure-
  253. * injection scenarios that must exhaust recovery quickly instead of walking
  254. * the shared normal default's five backed-off retries; replay/refresh only.
  255. */
  256. replayRetryPolicy?: RetryPolicyConfig
  257. /** Per-chunk replay pacing (ms) so the browser observes genuinely incremental SSE; replay/refresh only. */
  258. paceMs?: number
  259. /** Synthetic model capacity for UI scenarios whose seeded history must remain uncompacted. */
  260. replayContextWindow?: number
  261. /**
  262. * Tool presentation mode patched onto the shipped `tools` row (`code`
  263. * collapses the wire to run_code + the SDK prompt section). Omit for the
  264. * yml default. The code runtime row is always in the tree, so no extra
  265. * insertion is needed.
  266. */
  267. toolsMode?: 'native' | 'code' | 'both'
  268. /**
  269. * Insert the opt-in model-facing Cordis tool provider into the shipped tree.
  270. * Record and replay use the same tool surface, so captured request headers
  271. * remain reconstructable without making the tools a product default.
  272. */
  273. cordisTools?: boolean
  274. /**
  275. * Keep the shipped DeepSeek adapter mounted while masking the process
  276. * environment's DEEPSEEK_API_KEY for this scaffold lifetime. This is the
  277. * keyless first-run configuration lane; the default disables the adapter.
  278. */
  279. deepSeekMissingCredential?: boolean
  280. /** Leave the current welcome notice pending; ordinary scenarios pre-acknowledge it before browser boot. */
  281. welcomeNoticePending?: boolean
  282. /**
  283. * Patch the shipped DeepSeek search row to a deterministic endpoint and
  284. * credential reference. Browser search scenarios keep the real provider and
  285. * credentials seam while avoiding external search traffic and ambient keys.
  286. */
  287. deepSeekSearch?: {
  288. /** Anthropic-compatible base URL; the provider appends `/messages`. */
  289. baseURL: string
  290. /** Credential reference resolved by the shipped search provider. */
  291. apiKeyEnv: string
  292. }
  293. /**
  294. * Replace the roster row the scaffold pins by default (no configured roots,
  295. * default `standard` — the plugin's own shipped presets). Supply this only
  296. * to change WHICH presets a scenario sees beyond the shipped set — a
  297. * writable user root, a different default. The patch lands after the
  298. * default, so it wins.
  299. */
  300. agentPresets?: {
  301. /** Roots to discover after the plugin's shipped root, in precedence order. */
  302. roots: { path: string; trust: 'system' | 'user' }[]
  303. /** The preset a session that names none is composed from. */
  304. default: string
  305. }
  306. /**
  307. * Mount the shipped telemetry row in FULL mode against this exporter URL
  308. * instead of disabling it. Used to pin a real backend disclosure in
  309. * assembled coverage; point the URL at a local dead endpoint so no record
  310. * leaves the process.
  311. */
  312. telemetryUrl?: string
  313. /**
  314. * Browse through a trusted non-loopback hostname that the browser resolves
  315. * to loopback (for example `*.localhost`). The test server stays bound to
  316. * 127.0.0.1; a non-resolving authority fails before Host trust is exercised.
  317. */
  318. remoteAuthority?: string
  319. /** Reuse an existing harness home so a second Host can verify user settings across origins. */
  320. harnessHome?: string
  321. }
  322. /** Dispose the booted tree and remove both owned temp roots, reporting every independent cleanup failure. */
  323. async function cleanupScaffoldWorld(ctx: Context, workspaceCwd: string, persistenceRoot: string): Promise<unknown[]> {
  324. const failures: unknown[] = []
  325. await Promise.resolve(ctx.fiber.dispose()).catch((error: unknown) => failures.push(error))
  326. await rm(workspaceCwd, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  327. await rm(persistenceRoot, { recursive: true, force: true }).catch((error: unknown) => failures.push(error))
  328. return failures
  329. }
  330. /**
  331. * Boot the real web composition under the current snapshot mode.
  332. * @param options - replay fixture selection and pacing.
  333. * @returns the running scaffold.
  334. */
  335. export async function launchWebScaffold(options: LaunchOptions = {}): Promise<WebScaffold> {
  336. requireDist()
  337. const mode = webSnapshotMode()
  338. const compareReplaySession = options.compareReplaySession ?? await ownsReplayFixture(options.replayFixture)
  339. const browserHost = options.remoteAuthority ?? '127.0.0.1'
  340. if (mode === 'record') {
  341. // Both owning vitest configs (web unconditionally, snapshot in record
  342. // mode) load the repo-root .env before this file runs.
  343. if (process.env.DEEPSEEK_API_KEY === undefined || process.env.DEEPSEEK_API_KEY.length === 0) {
  344. throw new Error('web e2e record mode needs DEEPSEEK_API_KEY (env or repo-root .env)')
  345. }
  346. }
  347. if (mode === 'record' && options.deepSeekMissingCredential === true) {
  348. throw new Error('deepSeekMissingCredential is a keyless replay/refresh option')
  349. }
  350. const maskDeepSeekCredential = mode !== 'record' && options.deepSeekMissingCredential === true
  351. const originalDeepSeekCredential = process.env.DEEPSEEK_API_KEY
  352. let credentialEnvironmentRestored = false
  353. const restoreCredentialEnvironment = (): void => {
  354. if (credentialEnvironmentRestored || !maskDeepSeekCredential) return
  355. credentialEnvironmentRestored = true
  356. if (originalDeepSeekCredential === undefined) {
  357. Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  358. } else {
  359. process.env.DEEPSEEK_API_KEY = originalDeepSeekCredential
  360. }
  361. }
  362. const workspaceCwd = await realpath(await mkdtemp(join(tmpdir(), 'dsh-web-e2e-ws-')))
  363. // Isolated harness home: the settings/credentials rows resolve $DSH_HOME
  364. // paths at load, and an in-process boot must NEVER touch the developer's
  365. // real ~/.dsh document or credential file.
  366. const harnessHome = options.harnessHome ?? join(workspaceCwd, '.dsh-home')
  367. // Skill discovery is model-visible input, and its roots now resolve inside a
  368. // PRESET — a subtree this lane's include patches cannot reach, because the
  369. // roster mounts it directly per session rather than as a row of the booted
  370. // tree. The row's documented fallback is the environment, so pin that: the
  371. // whole scaffold lifetime, not just the boot, since presets mount when a
  372. // session is created. Without this a developer's real ~/.dsh/skills silently
  373. // enters replay requests and goldens while CI sees none. `DSH_HOME` follows
  374. // the resolved harness home so a scaffold sharing another's home — the
  375. // cross-port persistence scenario — pins the same roots the settings and
  376. // credentials rows were configured with.
  377. const skillRootEnvironment = {
  378. DSH_HOME: harnessHome,
  379. DSH_AGENTS_HOME: join(workspaceCwd, '.agents-home'),
  380. DSH_BUNDLED_SKILL_DIR: join(workspaceCwd, '.bundled-skills'),
  381. }
  382. const originalSkillRootEnvironment = Object.fromEntries(
  383. Object.keys(skillRootEnvironment).map(key => [key, process.env[key]]),
  384. )
  385. let skillRootEnvironmentRestored = false
  386. const restoreSkillRootEnvironment = (): void => {
  387. if (skillRootEnvironmentRestored) return
  388. skillRootEnvironmentRestored = true
  389. for (const [key, value] of Object.entries(originalSkillRootEnvironment)) {
  390. if (value === undefined) Reflect.deleteProperty(process.env, key)
  391. else process.env[key] = value
  392. }
  393. }
  394. Object.assign(process.env, skillRootEnvironment)
  395. let persistenceRoot: string
  396. try {
  397. persistenceRoot = await mkdtemp(join(tmpdir(), 'dsh-web-e2e-sessions-'))
  398. } catch (error) {
  399. const failures: unknown[] = [error]
  400. await rm(workspaceCwd, { recursive: true, force: true }).catch((cleanupError: unknown) => failures.push(cleanupError))
  401. restoreSkillRootEnvironment()
  402. if (failures.length > 1) throw new AggregateError(failures, 'web scaffold temp-root setup failed')
  403. throw error
  404. }
  405. if (maskDeepSeekCredential) Reflect.deleteProperty(process.env, 'DEEPSEEK_API_KEY')
  406. // The include patch set — the same layer stack the profile boot composes
  407. // (bundle patches in dsh.profile.bundles order), applied over the SAME empty root (a
  408. // patch id that stops matching a row fails the boot sweep loudly instead of
  409. // drifting).
  410. const basePatches = loadOverlayPatches('web e2e scaffold', BASE_PATCH_PATH)
  411. const surfacePatches = loadOverlayPatches('web e2e scaffold', WEB_PATCH_PATH)
  412. const extraOverlayPatches = options.extraOverlayPath === undefined
  413. ? []
  414. : loadOverlayPatches('web e2e scaffold', options.extraOverlayPath)
  415. const composedRows = composeEntries([basePatches, surfacePatches, extraOverlayPatches])
  416. const webRuntimeConfig = composedRows.find(row => row.id === 'web-runtime')?.config as {
  417. surfaceContext?: boolean
  418. } | undefined
  419. const surfaceContext = webRuntimeConfig?.surfaceContext !== false
  420. const patches: PatchOptions[] = [
  421. ...basePatches,
  422. ...surfacePatches,
  423. ...extraOverlayPatches,
  424. // The roster's shipped presets are the plugin's own, bundled inside
  425. // `dsh-agent-presets` and prepended by it. Pin only the machine-local
  426. // root away: a developer's own `~/.dsh/.agent-presets` must not be able
  427. // to change a golden.
  428. {
  429. id: 'agent-presets',
  430. config: {
  431. default: 'standard',
  432. includeUserRoot: false,
  433. },
  434. },
  435. { id: 'session-persistence-jsonl', config: { root: persistenceRoot } },
  436. // Content search is enabled here although the shipped bundles default it
  437. // off (`openAt: never`, pinned by apps/cli/tests/lazy-search-startup):
  438. // the seeded-session scenarios navigate by content search, and these e2e
  439. // runs are the assembled coverage for the opt-in search path.
  440. { id: 'session-query-sqlite', config: { path: ':memory:', openAt: 'first-search' } },
  441. // storage-json's yml root is anchored to the real $DSH_HOME; pin the row
  442. // to an absolute temp root (removed with the workspace at close) so tests
  443. // never write the user's harness home.
  444. { id: 'storage-json', config: { root: join(workspaceCwd, '.dsh-storages') } },
  445. // Skill discovery is model-visible input. Pin every host-level root inside
  446. // the owned temp world so ~/.dsh, ~/.agents, and a bundled-root env setting
  447. // cannot change replay requests or conversation goldens. Project roots stay
  448. // enabled against the same empty temp workspace, preserving the real seam.
  449. {
  450. id: 'skill-filesystem',
  451. config: {
  452. dshHome: join(workspaceCwd, '.dsh-home'),
  453. agentsHome: join(workspaceCwd, '.agents-home'),
  454. bundledSkillDir: join(workspaceCwd, '.bundled-skills'),
  455. watch: false,
  456. },
  457. },
  458. // fs/bash cwd default to process.cwd(); the gateway injects the same
  459. // value into session.cwd — chdir below anchors all three to the temp
  460. // workspace, keeping the composition untouched.
  461. { id: 'agent-instructions', disabled: true },
  462. { id: 'session-title-llm', disabled: true },
  463. // Fixture sessions must never leave the process: the shipped row defaults
  464. // to the production OTLP endpoint (or whatever DSH_TELEMETRY_OTLP_URL
  465. // names in the ambient environment). A scenario that pins a real backend
  466. // disclosure passes a local dead endpoint instead of disabling the row.
  467. options.telemetryUrl === undefined
  468. ? { id: 'session-telemetry-otel', disabled: true }
  469. : {
  470. id: 'session-telemetry-otel',
  471. config: {
  472. mode: 'FULL',
  473. exporter: { url: options.telemetryUrl },
  474. shutdownTimeoutMillis: 1_000,
  475. },
  476. },
  477. // Use an ephemeral port while preserving the shipped compression policy;
  478. // a patch replaces the row's complete config.
  479. {
  480. id: 'webserver',
  481. config: {
  482. host: '127.0.0.1', port: 0, compression: 'gzip',
  483. compressionLevel: 1, compressionThresholdBytes: 1024,
  484. },
  485. },
  486. // The bundle's web-runtime row resolves the same built dist under test
  487. // (apps/web IS @deepseek-ai/dsh-web-frontend); native browser opening and the
  488. // URL line are disabled because this scaffold owns its Playwright browser.
  489. // Preserve the composed surface-context choice because a patch replaces
  490. // the row's complete config.
  491. { id: 'web-runtime', config: { openBrowser: false, printUrl: false, surfaceContext } },
  492. ...options.remoteAuthority === undefined
  493. ? []
  494. : [{ id: 'connection', config: { trustedHosts: [options.remoteAuthority] } }],
  495. { id: 'settings', config: { dshHome: harnessHome } },
  496. { id: 'credentials', config: { dshHome: harnessHome } },
  497. // The shipped directory-picker row is the -auto chooser, which resolves
  498. // the interaction from the RUNNING host (display, SSH launch, bind). The
  499. // lane's goldens are interaction-specific (workspace-management drives
  500. // the in-app browse dialog), so pin -browse deterministically on every
  501. // host: patch `name` is an assertion, not an override, hence the
  502. // disable+insert pair.
  503. { id: 'directory-picker', disabled: true },
  504. { insert: [
  505. { id: 'directory-picker-browse', name: '@deepseek-ai/dsh-host-directory-picker-browse' },
  506. { id: 'ui-directory-picker-browse', name: '@deepseek-ai/dsh-client-ui-directory-picker-browse' },
  507. ] },
  508. ...options.agentPresets === undefined
  509. ? []
  510. // Never the derived harness-home root: a developer's own presets must not
  511. // be able to change a golden, whatever roots a scenario asks for.
  512. : [{ id: 'agent-presets', config: { ...options.agentPresets, includeUserRoot: false } }],
  513. ...options.toolsMode === undefined ? [] : [{ id: 'tools', config: { mode: options.toolsMode } }],
  514. // The shipped Web bundle already owns both runners and the Cordis UI. This
  515. // scenario adds only the model-facing tools that exercise those services.
  516. ...options.cordisTools === true
  517. ? [{ insert: [
  518. { id: 'tool-cordis', name: '@deepseek-ai/dsh-tool-cordis' },
  519. ] }]
  520. : [],
  521. ...options.deepSeekSearch === undefined
  522. ? []
  523. : [{
  524. id: 'web-search-deepseek',
  525. config: {
  526. apiKeyEnv: options.deepSeekSearch.apiKeyEnv,
  527. baseURL: options.deepSeekSearch.baseURL,
  528. },
  529. }],
  530. ...mode === 'record' || options.deepSeekMissingCredential === true
  531. ? []
  532. : [{ id: 'llm-deepseek', disabled: true }],
  533. ]
  534. // Sessions inherit the gateway's process.cwd() default; run the boot from
  535. // the temp workspace so tool cwd, session cwd, and fixtures agree.
  536. const originalCwd = process.cwd()
  537. const ctx = new Context()
  538. const observedSessions = new Map<SessionId, Session>()
  539. const stopObservingSessions = ctx.on('session/created', (session) => {
  540. observedSessions.set(session.id, session)
  541. })
  542. let port = 0
  543. let replayHandle: ReplayHandle | undefined
  544. try {
  545. process.chdir(workspaceCwd)
  546. // The production module-resolution setup: an empty profile root inside the temp
  547. // harness home, with bare plugin names resolving through the flat module
  548. // fallback the launcher heals under <home>/profiles.
  549. await healProfilesModuleFallback(INSTALL_ANCHOR, harnessHome)
  550. const profileDir = join(harnessHome, 'profiles', 'scaffold')
  551. await mkdir(profileDir, { recursive: true })
  552. const rootConfig = join(profileDir, 'cordis.yml')
  553. await writeFile(rootConfig, '[]\n')
  554. ctx.baseUrl = pathToFileURL(profileDir).href + '/'
  555. // This direct Loader harness supplies the same root-path capability as app-boot.
  556. ctx.provide('dshHomePath', dshHomePath)
  557. // A host with no command line still provides one: the web bundle's startup
  558. // row releases the rows waiting on it, and with no arguments each starts on
  559. // the values this scaffold composed above. An exit request can only come
  560. // from a rejected argument, which a fixed empty list has none of.
  561. provideCmdline(ctx, {
  562. args: [],
  563. exit: (code) => {
  564. throw new Error(`web e2e scaffold: the web app requested exit ${String(code)} with no arguments to reject`)
  565. },
  566. })
  567. await ctx.plugin(Loader)
  568. ctx.loader.builtins.include = Include
  569. // `cordis:group` beside it, exactly as `boot()` registers it: a group row is
  570. // how a preset gives one `isolate` realm to a provider and its consumers,
  571. // and a preset resolving package names from its own directory cannot reach
  572. // `@deepseek-ai/cordis-plugin-group` by name.
  573. ctx.loader.builtins.group = Group
  574. await ctx.loader.create({
  575. name: 'cordis:include',
  576. config: { path: pathToFileURL(rootConfig).href, patches },
  577. })
  578. await ctx.loader.await()
  579. assertEntriesLoaded(ctx, 'web e2e scaffold')
  580. if (options.welcomeNoticePending !== true) {
  581. await ctx.settings.mutate(settingsNamespace(WELCOME_NOTICE_SETTINGS_NAMESPACE), [{
  582. op: 'set', path: [WELCOME_NOTICE_ACK_FIELD], value: WELCOME_NOTICE_VERSION,
  583. }])
  584. }
  585. const boundPort = ctx.get('webServer')?.port
  586. if (boundPort === undefined) {
  587. throw new Error('web e2e scaffold: webServer service missing after settled boot')
  588. }
  589. port = boundPort
  590. // Fill the open llm seam on the settled root ctx. Ordinary keyless modes
  591. // disable llm-deepseek; the first-run lane keeps it mounted but has no
  592. // replay fixture and never streams. The direct install, unlike the plugin
  593. // row, returns the ReplayHandle for the teardown consumption check.
  594. if (options.replayProvidersOnly) {
  595. if (options.replayFixture === undefined) {
  596. throw new Error('replayProvidersOnly requires replayFixture (its file supplies the header)')
  597. }
  598. const fixtureText = readFileSync(options.replayFixture, 'utf8')
  599. // The consumption check is skipped for this mode, so no script source
  600. // may carry callable entries: reject override/child sources outright
  601. // and any call-bearing fixture.
  602. if (options.replayOverride !== undefined || options.replayChildFixtures !== undefined) {
  603. throw new Error('replayProvidersOnly cannot combine with replayOverride or replayChildFixtures')
  604. }
  605. // A fixture without a session header row must not mount the catalog
  606. // silently: the consumption-skip assumes the header-only shape.
  607. let headerType: unknown
  608. try {
  609. headerType = (JSON.parse(fixtureText.trimStart().split('\n', 1)[0] ?? '') as { type?: unknown }).type
  610. } catch {
  611. headerType = undefined
  612. }
  613. if (headerType !== 'session') {
  614. throw new Error('replayProvidersOnly fixture must open with a session header row')
  615. }
  616. const recorded = parseSessionLog(fixtureText)
  617. const hasModelCall = recorded.some(event => (
  618. event.type === 'assistant/chunk' || event.type === 'request/header' || event.type === 'tool/call'
  619. ))
  620. if (hasModelCall) {
  621. throw new Error('replayProvidersOnly fixture must record no model calls')
  622. }
  623. }
  624. if (mode !== 'record' && options.replayFixture !== undefined) {
  625. replayHandle = installLlmReplay(ctx, {
  626. file: options.replayFixture,
  627. providers: replayProviders(options.replayContextWindow).map(provider => ({
  628. ...provider,
  629. ...(options.replayRetryPolicy === undefined ? {} : { retryPolicy: options.replayRetryPolicy }),
  630. })),
  631. ...(options.replayOverride === undefined ? {} : { overrideFile: options.replayOverride }),
  632. ...(options.replayChildFixtures === undefined ? {} : { childFiles: options.replayChildFixtures }),
  633. ...(options.paceMs === undefined ? {} : { paceMs: options.paceMs }),
  634. })
  635. } else if (mode !== 'record' && options.deepSeekMissingCredential !== true) {
  636. // No fixture and no shipped adapter would leave the tree with ZERO
  637. // provider routes — a state no product composition has, and one the
  638. // composer refuses to type into. Register the same routes
  639. // a fixture would, with streaming that still fails loud: the scenario
  640. // issues no model calls, and one that slipped in must not pass quietly.
  641. ctx.effect(() => ctx.llm.registerAdapter(
  642. replayProviders(options.replayContextWindow).map(provider => provider.id),
  643. new RouteOnlyAdapter(replayProviders(options.replayContextWindow)),
  644. ), 'web e2e scaffold: route-only adapter')
  645. }
  646. } catch (error) {
  647. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  648. const cleanupFailures = await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot)
  649. restoreCredentialEnvironment()
  650. restoreSkillRootEnvironment()
  651. if (cleanupFailures.length > 0) {
  652. throw new AggregateError([error, ...cleanupFailures], 'web scaffold setup failed and cleanup was incomplete')
  653. }
  654. throw error
  655. } finally {
  656. if (process.cwd() !== originalCwd) process.chdir(originalCwd)
  657. }
  658. return {
  659. harnessHome,
  660. mode,
  661. baseUrl: `http://${browserHost}:${port}`,
  662. ctx,
  663. workspaceCwd,
  664. persistenceRoot,
  665. // Barrier stack: the in-process turn/end identifies the session, its
  666. // explicit flush makes the transcript durable, and the caller's browser
  667. // settled-poll comes last because host completion strictly precedes render.
  668. whenTurnSettled(timeoutMs = mode === 'record' ? 180_000 : 30_000): Promise<SessionId> {
  669. return new Promise<SessionId>((resolveSettled, reject) => {
  670. const timer = setTimeout(() => {
  671. off()
  672. reject(new Error(`no turn/end within ${timeoutMs}ms`))
  673. }, timeoutMs)
  674. const off = ctx.on('session/event', (session: Session, event: SessionEvent) => {
  675. if (event.type !== 'turn/end') return
  676. clearTimeout(timer)
  677. off()
  678. ctx.sessions.flush(session)
  679. .then(() => { resolveSettled(session.id) }, reject)
  680. })
  681. })
  682. },
  683. async close(): Promise<void> {
  684. const failures: unknown[] = []
  685. if (mode !== 'record'
  686. && options.replayFixture !== undefined
  687. && options.replayProvidersOnly !== true
  688. && compareReplaySession) {
  689. try {
  690. await assertReplaySession(
  691. [...observedSessions.values()],
  692. options.replayFixture,
  693. mode,
  694. `http://${browserHost}:${port}`,
  695. )
  696. } catch (error) {
  697. failures.push(error)
  698. }
  699. }
  700. // Fixture-consumption check first, while the run's binding state is
  701. // still authoritative — a scenario that drove fewer model calls than
  702. // recorded fails here instead of drifting green. Skipped for
  703. // replayProvidersOnly, whose fixture is validated call-free at boot.
  704. if (!options.replayProvidersOnly) {
  705. try {
  706. replayHandle?.assertConsumed()
  707. } catch (error) {
  708. failures.push(error)
  709. }
  710. }
  711. try {
  712. stopObservingSessions()
  713. failures.push(...await cleanupScaffoldWorld(ctx, workspaceCwd, persistenceRoot))
  714. } finally {
  715. restoreCredentialEnvironment()
  716. restoreSkillRootEnvironment()
  717. }
  718. if (failures.length > 0) throw new AggregateError(failures, 'web scaffold teardown failed')
  719. },
  720. }
  721. }
  722. /**
  723. * Serialize a live session to the canonical raw session-JSONL layout — the
  724. * in-memory record-mode harvest, so the on-disk zstd default never matters.
  725. */
  726. function rawSessionLog(session: Session): string {
  727. return [
  728. JSON.stringify({ type: 'session', ...session.header }),
  729. ...packChunkRuns(session.events).map(record => JSON.stringify(record)),
  730. '',
  731. ].join('\n')
  732. }
  733. function normalizeWebSessionVolatiles(log: string): string {
  734. const normalizeValue = (value: unknown): unknown => {
  735. if (typeof value === 'string') {
  736. return value.replace(/Anonymous user: [^.]+(?=\. Session sharing)/g, 'Anonymous user: {{anonymousUserId}}')
  737. }
  738. if (Array.isArray(value)) return value.map(normalizeValue)
  739. if (value !== null && typeof value === 'object') {
  740. return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, normalizeValue(item)]))
  741. }
  742. return value
  743. }
  744. return log.split(/\r?\n/).map((line) => {
  745. if (line.trim() === '') return line
  746. const record = normalizeValue(JSON.parse(line)) as { type?: unknown; data?: { endpoint?: unknown } }
  747. if (record.type === 'web/deepseek-search-llm-request' && typeof record.data?.endpoint === 'string') {
  748. record.data.endpoint = '{{webSearchEndpoint}}'
  749. }
  750. return JSON.stringify(record)
  751. }).join('\n')
  752. }
  753. function stableSessionFixture(session: Session, existing: string, workspaceCwd: string): string {
  754. const fresh = scrubSessionSnapshot(normalizeWebSessionVolatiles(rawSessionLog(session)))
  755. .split(session.id).join('{{sessionId}}')
  756. .split(workspaceCwd).join('{{cwd}}')
  757. const stable = redactSessionSnapshotIds(stabilizeFixtureMessageIds([fresh], [existing]))[0]
  758. if (stable === undefined) throw new Error('session harvest produced no stabilized fixture')
  759. return stable
  760. }
  761. async function assertReplaySession(
  762. sessions: readonly Session[],
  763. fixturePath: string,
  764. mode: WebSnapshotMode,
  765. webUrl: string,
  766. ): Promise<void> {
  767. let expected = await readFile(fixturePath, 'utf8')
  768. const userPrompts = fixtureUserPrompts(expected)
  769. const candidates = sessions.filter((session) => {
  770. if (session.header.parentSession !== undefined) return false
  771. const actual = session.events.flatMap((event) => {
  772. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return []
  773. const text = event.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  774. return text.length === 0 ? [] : [text]
  775. })
  776. return JSON.stringify(actual) === JSON.stringify(userPrompts)
  777. })
  778. expect(candidates, `Web replay fixture ${fixturePath} must match one live root session`).toHaveLength(1)
  779. const session = candidates[0] as Session
  780. const sessionCwd = session.header.cwd
  781. if (sessionCwd === undefined) throw new Error(`${fixturePath}: replayed session has no cwd`)
  782. const actual = rawSessionLog(session)
  783. if (mode === 'refresh') {
  784. expected = stableSessionFixture(session, expected, sessionCwd)
  785. await writeFile(fixturePath, expected)
  786. }
  787. const expectedHeader = JSON.parse(expected.split('\n').find(line => line.trim() !== '') ?? '{}') as {
  788. id?: unknown
  789. cwd?: unknown
  790. }
  791. const actualContext: NormalizeContext = { sessionIds: [String(session.id)], cwd: sessionCwd }
  792. const expectedContext: NormalizeContext = {
  793. sessionIds: typeof expectedHeader.id === 'string' ? [expectedHeader.id] : [],
  794. cwd: typeof expectedHeader.cwd === 'string' ? expectedHeader.cwd : '\0no-cwd\0',
  795. }
  796. expect(normalizeSessionSnapshots([normalizeWebSessionVolatiles(actual)], actualContext)[0], `${fixturePath}: persisted replay`)
  797. .toBe(normalizeSessionSnapshots([normalizeWebSessionVolatiles(expected)], expectedContext)[0])
  798. const fixtureDir = dirname(fixturePath)
  799. const manifestPath = join(fixtureDir, 'snapshot.yml')
  800. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  801. if (manifest.header?.pin !== true) return
  802. const normalizePrompt = (value: string): string => value
  803. .split(REPO_ROOT).join('{{sourceRoot}}')
  804. .split(webUrl).join('{{webUrl}}')
  805. const prompts = normalizedSystemPrompts(actual, actualContext).map(normalizePrompt)
  806. const schemas = normalizedToolSchemas(actual, actualContext)
  807. const promptPath = join(fixtureDir, 'system-prompt.expected.md')
  808. const schemaPath = join(fixtureDir, 'tool-schemas.expected.json')
  809. const promptSnapshot = formatSystemPromptSnapshot(prompts[0] as string, prompts.slice(1))
  810. const schemaSnapshot = formatToolSchemasSnapshot(schemas[0] as unknown[], schemas.slice(1))
  811. if (mode === 'refresh') {
  812. await Promise.all([writeFile(promptPath, promptSnapshot), writeFile(schemaPath, schemaSnapshot)])
  813. }
  814. expect(promptSnapshot, `${fixturePath}: system-prompt pin`).toBe(await readFile(promptPath, 'utf8'))
  815. expect(schemaSnapshot, `${fixturePath}: tool-schema pin`).toBe(await readFile(schemaPath, 'utf8'))
  816. }
  817. /**
  818. * Record-mode fixture write-back: harvest the live session, scrub request
  819. * headers to {{system}}/{{tools}}, tokenize the run-local cwd, redact opaque
  820. * identities with typed relationship-preserving tokens, and write the fixture.
  821. * @param scaffold - the record-mode scaffold.
  822. * @param sessionId - the driven session.
  823. * @param fixturePath - the committed session.jsonl target.
  824. */
  825. export async function recordFixture(scaffold: WebScaffold, sessionId: SessionId, fixturePath: string): Promise<void> {
  826. const agent = scaffold.ctx.agents.get(sessionId)
  827. if (agent === undefined) throw new Error(`record harvest: no live agent for ${sessionId}`)
  828. const existing = existsSync(fixturePath) ? await readFile(fixturePath, 'utf8') : ''
  829. await writeFile(fixturePath, stableSessionFixture(agent.session, existing, scaffold.workspaceCwd))
  830. }
  831. /**
  832. * The user prompts recorded in a fixture, in order — the single source tying
  833. * spec drive steps to recorded reality so script and fixture cannot drift.
  834. * @param fixtureText - raw session.jsonl contents.
  835. * @returns the recorded user prompt texts.
  836. */
  837. export function fixtureUserPrompts(fixtureText: string): string[] {
  838. return parseSessionLog(fixtureText).flatMap((event) => {
  839. if (event.type !== 'user/message' || event.data.source.kind !== 'user') return []
  840. const text = event.data.content.filter(block => block.type === 'text').map(block => block.text).join('')
  841. return text.length > 0 ? [text] : []
  842. })
  843. }
  844. /** Deterministic UUID used when a seed fixture's typed identity token is materialized. */
  845. export function fixtureIdentity(
  846. kind: 'message' | 'approval' | 'workflow' | 'command' | 'rpc' | 'retry' | 'id',
  847. ordinal: number,
  848. ): string {
  849. const hex = createHash('sha256').update(`${kind}:${ordinal}`).digest('hex').slice(0, 32).split('')
  850. hex[12] = '4'
  851. hex[16] = ['8', '9', 'a', 'b'][Number.parseInt(hex[16] as string, 16) % 4] as string
  852. 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('')}`
  853. }
  854. /**
  855. * Seed a recorded session fixture into the scaffold's persistence root
  856. * through the REAL backend API (throwaway Context + SessionStore + JSONL
  857. * plugin — the semantic-checkpoint precedent), never raw file writes: no
  858. * knowledge of bucket hashing, filename encoding, or compression, and
  859. * malformed session events fail loud at seed time. The fixture's tokenized identity
  860. * ({{sessionId}}/{{cwd}}) is realized for this world before parsing. Event
  861. * times are materialized from event order against the fixture header's
  862. * creation time, or the seeded creation time when normalization replaced the
  863. * header value with zero.
  864. * @param scaffold - the target scaffold.
  865. * @param fixtureText - raw recorded session.jsonl contents.
  866. * @param id - the seeded session id (stable for deterministic goldens).
  867. * @param agentPreset - the preset the recorded session was composed from,
  868. * for scenarios asserting what a resumed session reports running.
  869. * @returns the seeded id.
  870. */
  871. /**
  872. * Realize a recorded seed fixture against one scaffold: substitute the
  873. * `{{sessionId}}`/`{{cwd}}` placeholders and rewrite the recorded cwd to the
  874. * scaffold's workspace. Idempotent, so a caller may realize early (e.g. to
  875. * price content exactly as the host will fold it) and still pass the result
  876. * through {@link seedSession}.
  877. * @param scaffold - the booted scaffold whose workspace the seed targets.
  878. * @param fixtureText - the committed seed fixture text.
  879. * @param id - the session id the seed is realized for.
  880. * @returns the realized fixture text.
  881. */
  882. export function realizeSeedFixture(scaffold: WebScaffold, fixtureText: string, id: string): string {
  883. const realized = fixtureText
  884. .split('{{sessionId}}').join(id)
  885. .split('{{session:1}}').join(id)
  886. .replace(/\{\{session:([2-9]\d*)\}\}/g, (_token, ordinal: string) => `${id}-child-${ordinal}`)
  887. .replace(/\{\{(message|approval|workflow|command|rpc|retry|id):([1-9]\d*)\}\}/g, (_token, kind: string, ordinal: string) =>
  888. fixtureIdentity(kind as 'message' | 'approval' | 'workflow' | 'command' | 'rpc' | 'retry' | 'id', Number(ordinal)))
  889. .split('{{cwd}}').join(scaffold.workspaceCwd)
  890. const fixtureCwd = (JSON.parse(realized.split('\n', 1)[0]!) as { cwd?: string }).cwd
  891. return fixtureCwd === undefined
  892. ? realized
  893. : realized.split(fixtureCwd).join(scaffold.workspaceCwd)
  894. }
  895. /**
  896. * Parse a committed web seed fixture through the replay reader.
  897. * @param fixtureText - session JSONL fixture contents.
  898. * @returns the original header line, parsed header, and logical events.
  899. */
  900. export function parseSeedFixture(fixtureText: string): {
  901. headerLine: string
  902. header: Record<string, unknown>
  903. events: SessionEvent[]
  904. } {
  905. const headerLine = fixtureText.split(/\r?\n/).find(line => line.trim().length > 0)
  906. if (headerLine === undefined) throw new Error('seed fixture has no session header')
  907. const header = JSON.parse(headerLine) as Record<string, unknown>
  908. if (header.type !== 'session') throw new Error('seed fixture must start with a session header')
  909. return { headerLine, header, events: parseSessionLog(fixtureText) }
  910. }
  911. /**
  912. * Render logical events as an envelope-free web seed fixture.
  913. * @param headerLine - original session header line.
  914. * @param events - logical session events in order.
  915. * @returns projected session JSONL.
  916. */
  917. export function renderSeedFixture(
  918. headerLine: string,
  919. events: readonly ({ readonly seq: number; readonly time: number } & object)[],
  920. ): string {
  921. return [
  922. headerLine,
  923. ...events.map(({ seq: _seq, time: _time, ...event }) => JSON.stringify(event)),
  924. '',
  925. ].join('\n')
  926. }
  927. export async function seedSession(
  928. scaffold: WebScaffold,
  929. fixtureText: string,
  930. id: string,
  931. agentPreset?: string,
  932. ): Promise<SessionId> {
  933. const decoded = parseSeedFixture(realizeSeedFixture(scaffold, fixtureText, id))
  934. const events = decoded.events
  935. if (events.length === 0) throw new Error('seed fixture has no events')
  936. const last = events[events.length - 1]!
  937. // An open final turn would be mutated by resume's crash repair on first
  938. // open; a committed seed must be a closed recording.
  939. if (last.type !== 'turn/end') throw new Error(`seed fixture must end in turn/end, got ${last.type}`)
  940. const meta: SessionHeader = {
  941. version: SESSION_FORMAT_VERSION,
  942. id: SessionId(id),
  943. createdAt: Date.now() - 60_000,
  944. cwd: scaffold.workspaceCwd,
  945. delegationDepth: 0,
  946. ...agentPreset === undefined ? {} : { agentPreset },
  947. }
  948. const fixtureCreatedAt = decoded.header.createdAt
  949. if (typeof fixtureCreatedAt !== 'number') {
  950. throw new Error('seed fixture requires a numeric createdAt header')
  951. }
  952. const timeAnchor = fixtureCreatedAt === 0 ? meta.createdAt : fixtureCreatedAt
  953. const materializedEvents = events.map((event, index) => ({ ...event, time: timeAnchor + index }))
  954. await persistSeedSession(scaffold, meta, materializedEvents)
  955. return meta.id
  956. }
  957. /** Seed one materialized cold Session whose log has no turn/start event. */
  958. export async function seedBlankSession(
  959. scaffold: WebScaffold,
  960. id: string,
  961. cwd: string,
  962. ): Promise<SessionId> {
  963. const meta: SessionHeader = {
  964. version: SESSION_FORMAT_VERSION,
  965. id: SessionId(id),
  966. createdAt: Date.now() - 60_000,
  967. cwd,
  968. delegationDepth: 0,
  969. }
  970. await persistSeedSession(scaffold, meta, [{
  971. type: 'session/end-seed',
  972. seq: 0,
  973. time: meta.createdAt,
  974. data: {},
  975. }])
  976. return meta.id
  977. }
  978. /** Materialize one detached Session fixture through the shipped JSONL provider. */
  979. async function persistSeedSession(
  980. scaffold: WebScaffold,
  981. meta: SessionHeader,
  982. events: readonly SessionEvent[],
  983. ): Promise<void> {
  984. const seeder = new Context()
  985. try {
  986. await seeder.plugin(SessionStore)
  987. // Same root as the booted tree with the plugin's own default compression,
  988. // so the host's directory-scan list() sees one consistent encoding.
  989. await seeder.plugin(JsonlSessionPersistence, { root: scaffold.persistenceRoot })
  990. await seeder.sessionPersistence.create(meta)
  991. await seeder.sessionPersistence.append(meta.id, events)
  992. } finally {
  993. await seeder.fiber.dispose()
  994. }
  995. }
  996. /**
  997. * Normalize an aria snapshot: uuid, cwd, workspace-basename, duration,
  998. * decode-throughput, and path-sensitive compaction estimates collapse to
  999. * stable tokens.
  1000. *
  1001. * Throughput needs a token for the same reason durations do, and no fixture
  1002. * can supply one: the figure divides a replayed step's output tokens by the
  1003. * wall time the local run took to stream them, so it moves between two runs
  1004. * on one machine (measured 69 → 70 tok/s) and swings wildly on a fast replay
  1005. * (26333 tok/s for a 3 ms stream).
  1006. */
  1007. function normalizeAria(snapshot: string, workspaceCwd: string): string {
  1008. // The session heading renders the workspace's basename, not the full
  1009. // path, so both spellings must collapse to the token.
  1010. const base = workspaceCwd.split('/').pop()!
  1011. return snapshot
  1012. .split(workspaceCwd).join('{{cwd}}')
  1013. .split(base).join('{{workspace}}')
  1014. .replace(/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, '{{uuid}}')
  1015. // The optional space in `\d+m ?\d+s` covers both minute spellings: the
  1016. // stats line's compact `2m42s` and the message-chrome template's `2m 42s`.
  1017. .replace(
  1018. /~\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,
  1019. duration => duration.startsWith('~') ? duration : '{{duration}}',
  1020. )
  1021. .replace(/\b\d[\d,]*(?:\.\d+)? ms\b/g, '{{duration}}')
  1022. .replace(
  1023. /约\d+(?:年(?:\d+个月)?|个月(?:\d+天)?)|\d+(?:天(?:\d+小时(?:\d+分\d+秒)?)?|小时\d+分\d+秒|分\d+秒|(?:\.\d+)?秒)/g,
  1024. duration => duration.startsWith('约') ? duration : '{{duration}}',
  1025. )
  1026. .replace(/\d+(?:\.\d+)?(?= tok\/s(?!\w))/g, '{{throughput}}')
  1027. // Seeded compaction prices realized file paths, whose length differs
  1028. // between local worktrees and CI scratch directories.
  1029. .replace(/(Compacted \d+ history items \(~)\d+( tokens\))/g, '$1{{tokens}}$2')
  1030. // Session summaries and Message IconActions clocks cross calendar
  1031. // boundaries; collapse every shape so goldens stay stable across them.
  1032. .replace(/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z/g, '{{timestamp}}')
  1033. .replace(/\d{4}年\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  1034. .replace(/\d{1,2}月\d{1,2}日 \d{2}:\d{2}/g, '{{clock}}')
  1035. .replace(/(?<!\d)\d{1,2}:\d{2}:\d{2}(?:\.\d+)?(?:\s*[AP]M)?(?!\d)/gi, '{{clock}}')
  1036. .replace(/(?<!\d)\d{2}:\d{2}(?!\d)/g, '{{clock}}')
  1037. }
  1038. /**
  1039. * Capture the region's aria snapshot at a settled milestone: poll until two
  1040. * consecutive normalized captures are equal — a single-shot capture races the
  1041. * last React commits.
  1042. * @param page - the page under test.
  1043. * @param selector - the region locator selector.
  1044. * @param workspaceCwd - normalization input.
  1045. * @returns the stable normalized snapshot.
  1046. */
  1047. export async function captureStableAria(page: Page, selector: string, workspaceCwd: string): Promise<string> {
  1048. const region = page.locator(selector).first()
  1049. let previous = normalizeAria(await region.ariaSnapshot(), workspaceCwd)
  1050. await expect.poll(async () => {
  1051. const current = normalizeAria(await region.ariaSnapshot(), workspaceCwd)
  1052. const stable = current === previous
  1053. previous = current
  1054. return stable
  1055. }, { timeout: 5_000, message: 'aria snapshot did not stabilize' }).toBe(true)
  1056. return previous
  1057. }
  1058. /**
  1059. * Compare a normalized golden, or rewrite it under refresh. Refresh is the
  1060. * ONLY writer: a missing golden in replay mode fails with the healing command
  1061. * instead of silently self-bootstrapping.
  1062. * @param goldenPath - the committed ui.expected.md path.
  1063. * @param actual - the stable normalized snapshot.
  1064. * @param mode - the active snapshot mode.
  1065. */
  1066. export async function compareOrRefreshGolden(goldenPath: string, actual: string, mode: WebSnapshotMode): Promise<void> {
  1067. const payload = `${actual}\n`
  1068. if (mode === 'refresh') {
  1069. await writeFile(goldenPath, payload)
  1070. return
  1071. }
  1072. if (!existsSync(goldenPath)) {
  1073. throw new Error(`missing golden ${goldenPath} — run DSH_SNAPSHOT=refresh pnpm run test:web to generate it`)
  1074. }
  1075. expect(payload).toBe(await readFile(goldenPath, 'utf8'))
  1076. }
  1077. /**
  1078. * Fixture-inventory guard: the scenario directory holds exactly the expected
  1079. * files and every committed JSONL is a header-scrubbed, typed-redaction fixed point.
  1080. * @param dir - the scenario snapshot directory.
  1081. * @param expected - the exact expected file inventory.
  1082. */
  1083. export async function assertFixtureInventory(dir: string, expected: string[]): Promise<void> {
  1084. const entries = (await readdir(dir)).sort()
  1085. const ownsManifest = entries.includes('snapshot.yml')
  1086. const artifacts = entries.filter(name => name !== 'snapshot.yml')
  1087. expect(artifacts).toEqual([...expected].sort())
  1088. if (ownsManifest) {
  1089. const manifestPath = join(dir, 'snapshot.yml')
  1090. const manifest = parseSnapshotManifest(await readFile(manifestPath, 'utf8'), manifestPath)
  1091. expect(manifest.profile).toBe('web')
  1092. if (manifest.session === undefined) {
  1093. expect(
  1094. artifacts.includes('session.jsonl'),
  1095. `${dir}: session owner must carry session.jsonl`,
  1096. ).toBe(true)
  1097. } else {
  1098. expect(existsSync(resolve(dir, manifest.session.source)), `${dir}: session source`).toBe(true)
  1099. }
  1100. }
  1101. for (const entry of artifacts.filter(name => name.endsWith('.jsonl'))) {
  1102. const content = await readFile(join(dir, entry), 'utf8')
  1103. expect(scrubRequestHeaders(content), `${dir}/${entry} carries request-header bulk`).toBe(content)
  1104. expect(redactSessionSnapshotIds([content]), `${dir}/${entry} carries unredacted identities`).toEqual([content])
  1105. }
  1106. }
  1107. /**
  1108. * Console tripwires: reconnect/gap-repair self-healing or a pageerror must
  1109. * fail the scenario, not mask a dead wire behind eventual consistency.
  1110. * @param page - the page under test.
  1111. * @returns live warning/pageerror collectors to assert empty at scenario end.
  1112. */
  1113. export function watchConsole(page: Page): { warnings: string[]; pageErrors: string[] } {
  1114. const warnings: string[] = []
  1115. const pageErrors: string[] = []
  1116. page.on('console', (message) => {
  1117. const text = message.text()
  1118. if (/connection lost|gap repair|discontinuous/i.test(text)) warnings.push(text)
  1119. })
  1120. page.on('pageerror', (error) => { pageErrors.push(String(error)) })
  1121. return { warnings, pageErrors }
  1122. }
  1123. /**
  1124. * Remove only connection-loss warnings emitted after an intentional reload.
  1125. * Earlier warnings and all gap-repair/discontinuity warnings remain fatal.
  1126. * @param tripwire - the live console-warning collector.
  1127. * @param warningStart - warning count captured immediately before reloading.
  1128. */
  1129. export function acknowledgeReloadConnectionLoss(
  1130. tripwire: ReturnType<typeof watchConsole>,
  1131. warningStart: number,
  1132. ): void {
  1133. const reloadWarnings = tripwire.warnings.splice(warningStart)
  1134. tripwire.warnings.push(...reloadWarnings.filter(text => !/connection lost/i.test(text)))
  1135. }