Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

test(desktop-e2e): stateful/stateless continuity spec

Alfredo Lopezb2d61ea425f2
diff --git a/Makefile b/Makefile
index 6691f37fe..c9ad10bb4 100644
--- a/Makefile
+++ b/Makefile
@@ -1406,6 +1406,30 @@ test-e2e-hcc-storage-layout-switch: ## Run the #825 storage-layout gate across l
@echo "Running HCC storage-layout switch E2E gate..."
E2E_ALLOW_STATELESS_CADENCE_ACCELERATION=1 KUBECONTEXT=$(E2E_KUBECONTEXT) bash scripts/e2e/e2e-hcc-storage-layout-switch.sh
+.PHONY: test-e2e-825-desktop-continuity
+test-e2e-825-desktop-continuity: ## Run the #825 Desktop journey across stateful/stateless switches (owned branch profile, forwards held, real model turns)
+ @set -eu; \
+ ctx="$${KUBECONTEXT:-$(E2E_KUBECONTEXT)}"; \
+ test -n "$$ctx" || { echo "Refusing #825 Desktop E2E: explicit Kubernetes context is required" >&2; exit 1; }; \
+ test -n "$${E2E_DEV_LOGIN_EMAIL:-}" || { echo "Set E2E_DEV_LOGIN_EMAIL to the seeded Desktop login ([email protected] on a T2 profile)" >&2; exit 1; }; \
+ test -n "$${CONTROL_API_BASE_URL:-}" || { echo "CONTROL_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \
+ test -n "$${EXTERNAL_REST_API_BASE_URL:-}" || { echo "EXTERNAL_REST_API_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \
+ test -n "$${RPC_PROXY_BASE_URL:-}" || { echo "RPC_PROXY_BASE_URL must be set to the branch-owned random port" >&2; exit 1; }; \
+ . scripts/e2e/e2e-lib.sh; \
+ require_branch_profile_urls "$$ctx" "$${CLERUM_PROFILE_PORTS_ENV:-$${E2E_PROFILE_PORTS_ENV:-}}"; \
+ report="$${E2E_825_DESKTOP_REPORT:-$(CURDIR)/desktop-app/test-results/e2e-825-desktop-continuity.json}"; \
+ mkdir -p "$$(dirname "$$report")"; rm -f "$$report"; \
+ cd desktop-app && npm run verify:electron && npm run build; \
+ status=0; \
+ env -u ELECTRON_RUN_AS_NODE KUBECONTEXT="$$ctx" E2E_K8S_CONTEXT="$$ctx" E2E_825_DESKTOP_CONTINUITY=1 \
+ PLAYWRIGHT_JSON_OUTPUT_NAME="$$report" ./node_modules/.bin/playwright test \
+ --config test/e2e-playwright/playwright.config.ts --reporter=list,json --retries=0 \
+ stateful-stateless-switch-continuity.spec.ts || status=$$?; \
+ echo "E2E_825_DESKTOP_PLAYWRIGHT_EXIT=$$status"; \
+ node -e 'const s = JSON.parse(require("fs").readFileSync(process.argv[1], "utf8")).stats; console.log(`E2E_825_DESKTOP_STATS expected=$${s.expected} unexpected=$${s.unexpected} skipped=$${s.skipped} flaky=$${s.flaky}`); process.exit(s.expected > 0 && s.unexpected === 0 && s.skipped === 0 && s.flaky === 0 ? 0 : 1)' "$$report" || status=1; \
+ if [ "$$status" = 0 ]; then echo E2E_825_DESKTOP_RESULT=PASS; else echo E2E_825_DESKTOP_RESULT=FAIL; fi; \
+ exit $$status
+
.PHONY: test-e2e-stateless-wake-recovery
test-e2e-stateless-wake-recovery: ## Run stateless wake-recovery latency gate (R1 warm draining / R2 cold suspended / R3 drained-window p95 budgets; needs port-forwards + seeded chatllm-stateless)
@echo "Running stateless wake-recovery latency gate..."
diff --git a/desktop-app/test/e2e-playwright/stateful-stateless-switch-continuity.spec.ts b/desktop-app/test/e2e-playwright/stateful-stateless-switch-continuity.spec.ts
new file mode 100644
index 000000000..01bfd7de1
--- /dev/null
+++ b/desktop-app/test/e2e-playwright/stateful-stateless-switch-continuity.spec.ts
@@ -0,0 +1,634 @@
+/**
+ * #825 Desktop journey: one conversation survives stateful -> stateless ->
+ * stateful switches of its Host. Every Host keeps its database on the PVC
+ * root, so a lifecycle switch replaces the Pod and leaves the history where
+ * the next Pod reads it.
+ *
+ * The spec creates its own Host (a copy of the seeded source Host's spec)
+ * through the Control API admin API, grants it to the Desktop user, switches
+ * its lifecycle only through that API, and deletes it with its PVC at the end.
+ * It sends three real model turns.
+ *
+ * Environment (this header is the reference):
+ * - `E2E_825_DESKTOP_CONTINUITY=1` opts in. Without exactly `1` the file is
+ * skipped, so the default lane never mutates a cluster.
+ * - `E2E_K8S_CONTEXT` (or `KUBECONTEXT`) names the branch-owned Minikube
+ * profile; the spec refuses any context that is not a Minikube node of that
+ * name.
+ * - `CONTROL_API_BASE_URL` (or `E2E_CONTROL_API_URL`) and the admin password
+ * (`E2E_ADMIN_PASSWORD` / `ADMIN_PASSWORD` / `ADMIN_PASS` /
+ * `TEST_ADMIN_PASSWORD`) are required; loopback targets only.
+ * - `E2E_DEV_LOGIN_EMAIL` is the Desktop user ([email protected] on a T2
+ * profile).
+ * - `E2E_825_SOURCE_HOST` (default `chatllm`) is the Host whose spec is copied.
+ *
+ * Run: `make test-e2e-825-desktop-continuity`.
+ */
+import {
+ type APIRequestContext,
+ type ElectronApplication,
+ type Page,
+ expect,
+ request,
+ test,
+} from '@playwright/test'
+import { execFile } from 'node:child_process'
+import { promisify } from 'node:util'
+import {
+ EXTERNAL_REST_API_BASE_URL,
+ RPC_PROXY_BASE_URL,
+ assertAllowedTarget,
+ desktopCredentials,
+ finalizeRecording,
+ launchDesktopApp,
+ login,
+ openAgentsPage,
+} from './qa-recorder-helpers'
+
+const execFileAsync = promisify(execFile)
+
+const HOST_NAMESPACE = 'mcp-host'
+const WORKSPACE_PATH = '/workspace'
+const POD_READY_TIMEOUT_MS = 300_000
+const ADMIN_SESSION_COOKIE = 'control_ui_admin_session'
+
+// E2E_GUARDIAN_IPC_FLOW: Desktop sends RPC through Electron's main process,
+// which page.waitForResponse cannot observe. The business signals are the
+// Host's own session list and transcript, read through the app's
+// window.clerum.rpc bridge (rpc-proxy -> Host), plus the Pod witness.
+
+function kubeContext(): string {
+ const context = (process.env.E2E_K8S_CONTEXT || process.env.KUBECONTEXT || '').trim()
+ if (!context) {
+ throw new Error('E2E_K8S_CONTEXT (or KUBECONTEXT) must name the branch-owned Minikube profile.')
+ }
+ return context
+}
+
+async function kubectl(args: string[], timeoutMs = 30_000): Promise<string> {
+ const { stdout } = await execFileAsync('kubectl', ['--context', kubeContext(), ...args], {
+ timeout: timeoutMs,
+ maxBuffer: 8 * 1024 * 1024,
+ })
+ return stdout.toString()
+}
+
+async function kubectlJson(args: string[]): Promise<any> {
+ return JSON.parse(await kubectl([...args, '-o', 'json']))
+}
+
+async function assertMinikubeProfile(): Promise<void> {
+ const nodes = await kubectlJson(['get', 'nodes'])
+ const names = (nodes.items ?? []).map(
+ (node: any) => node.metadata?.labels?.['minikube.k8s.io/name'] ?? ''
+ )
+ if (!names.includes(kubeContext())) {
+ throw new Error(
+ `Context ${kubeContext()} is not the Minikube profile of that name (nodes: ${JSON.stringify(names)}).`
+ )
+ }
+}
+
+function requiredEnv(label: string, candidates: Array<string | undefined>): string {
+ const value = candidates.find(candidate => candidate?.trim())?.trim()
+ if (!value) throw new Error(`${label} is required for the #825 Desktop continuity spec.`)
+ return value
+}
+
+/** Control API admin session. The cookie is issued Secure, so it is sent as an explicit header. */
+class AdminApi {
+ private constructor(
+ private readonly api: APIRequestContext,
+ private readonly cookie: string
+ ) {}
+
+ static async login(baseUrl: string): Promise<AdminApi> {
+ const api = await request.newContext({ baseURL: new URL(baseUrl).origin })
+ const username = process.env.TEST_ADMIN_USERNAME?.trim() || 'admin'
+ const password = requiredEnv('The admin password', [
+ process.env.E2E_ADMIN_PASSWORD,
+ process.env.ADMIN_PASSWORD,
+ process.env.ADMIN_PASS,
+ process.env.TEST_ADMIN_PASSWORD,
+ ])
+ const response = await api.post('/api/v1/admin/auth/login', { data: { username, password } })
+ if (!response.ok()) {
+ throw new Error(`admin login returned HTTP ${response.status()}: ${await response.text()}`)
+ }
+ const pair = response
+ .headersArray()
+ .filter(h => h.name.toLowerCase() === 'set-cookie')
+ .map(h => h.value.split(';')[0].trim())
+ .find(p => p.startsWith(`${ADMIN_SESSION_COOKIE}=`))
+ if (!pair || pair === `${ADMIN_SESSION_COOKIE}=`) {
+ throw new Error(`admin login did not set ${ADMIN_SESSION_COOKIE}`)
+ }
+ return new AdminApi(api, pair)
+ }
+
+ private async call(
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
+ path: string,
+ data?: unknown
+ ): Promise<{ status: number; body: any }> {
+ const response = await this.api.fetch(`/api/v1${path}`, {
+ method,
+ headers: { cookie: this.cookie },
+ ...(data === undefined ? {} : { data }),
+ })
+ const text = await response.text()
+ let body: any = text
+ try {
+ body = text ? JSON.parse(text) : null
+ } catch {
+ // Non-JSON bodies are kept as text and reported in the failure message.
+ }
+ return { status: response.status(), body }
+ }
+
+ private async expectStatus(
+ method: 'GET' | 'POST' | 'PUT' | 'DELETE',
+ path: string,
+ expected: number,
+ data?: unknown
+ ): Promise<any> {
+ const { status, body } = await this.call(method, path, data)
+ if (status !== expected) {
+ throw new Error(
+ `${method} ${path} returned ${status} (expected ${expected}): ${JSON.stringify(body)}`
+ )
+ }
+ return body
+ }
+
+ async userIdByEmail(email: string): Promise<string> {
+ const body = await this.expectStatus('GET', `/admin/users?q=${encodeURIComponent(email)}`, 200)
+ const user = (body?.items ?? []).find((item: any) => item?.email === email)
+ if (!user?.id) throw new Error(`Desktop user ${email} not found through the admin API`)
+ return String(user.id)
+ }
+
+ async firstTeamId(userId: string): Promise<string | null> {
+ const body = await this.expectStatus('GET', `/admin/users/${userId}/teams`, 200)
+ return body?.items?.[0]?.id ? String(body.items[0].id) : null
+ }
+
+ async createHost(name: string, sourceSpec: any, stateless: boolean): Promise<void> {
+ const { channels: _channels, desktop: _desktop, ...rest } = sourceSpec
+ const spec = { ...rest, host: name, lifecycle: { stateless } }
+ await this.expectStatus('POST', '/admin/hosts', 201, { metadata: { name }, spec })
+ }
+
+ /** GET then PUT with the read's resourceVersion, echoing the full spec as Control UI does. */
+ async setLifecycle(name: string, stateless: boolean): Promise<void> {
+ const host = await this.expectStatus('GET', `/admin/hosts/${name}`, 200)
+ const resourceVersion = host?.metadata?.resourceVersion
+ if (!resourceVersion) throw new Error(`Host ${name} read has no resourceVersion`)
+ await this.expectStatus('PUT', `/admin/hosts/${name}`, 200, {
+ metadata: { resourceVersion },
+ spec: { ...host.spec, lifecycle: { stateless } },
+ })
+ }
+
+ async deleteHost(name: string): Promise<void> {
+ const { status, body } = await this.call('DELETE', `/admin/hosts/${name}`)
+ if (status !== 200 && status !== 404) {
+ throw new Error(`DELETE Host ${name} returned ${status}: ${JSON.stringify(body)}`)
+ }
+ }
+
+ /**
+ * PUT /admin/{users|teams}/:id/agents filters names against the active
+ * agent set, so a just-created Host can lag; retry until the response
+ * echoes it back (bounded).
+ */
+ async setAgentGrant(path: string, name: string, op: 'add' | 'remove'): Promise<void> {
+ let last = ''
+ for (let attempt = 1; attempt <= 20; attempt++) {
+ const current = await this.expectStatus('GET', path, 200)
+ const agentNames: string[] = current?.agentNames ?? []
+ const expectedCurrentAgentNames = [...agentNames, ...(current?.deletedAgentNames ?? [])]
+ const next =
+ op === 'add'
+ ? Array.from(new Set([...agentNames, name]))
+ : agentNames.filter(agent => agent !== name)
+ const { status, body } = await this.call('PUT', path, {
+ agentNames: next,
+ expectedCurrentAgentNames,
+ })
+ if (status >= 200 && status < 300) {
+ if (op === 'remove' || (body?.agentNames ?? []).includes(name)) return
+ }
+ last = `${status} ${JSON.stringify(body)}`
+ await new Promise(resolve => setTimeout(resolve, 3_000))
+ }
+ throw new Error(`${op} grant '${name}' on ${path} did not converge: ${last}`)
+ }
+
+ async dispose(): Promise<void> {
+ await this.api.dispose()
+ }
+}
+
+type PodStorage = {
+ mounts: Array<{ mountPath: string; name: string }>
+ store: string | undefined
+ dbDir: string | undefined
+ inits: string[]
+}
+
+function mcpHostContainer(pod: any): any {
+ const container = (pod.spec?.containers ?? []).find((c: any) => c.name === 'mcp-host')
+ if (!container) throw new Error(`Pod ${pod.metadata?.name} has no mcp-host container`)
+ return container
+}
+
+function envValue(pod: any, name: string): string | undefined {
+ return (mcpHostContainer(pod).env ?? []).find((e: any) => e.name === name)?.value
+}
+
+function podStorage(pod: any): PodStorage {
+ return {
+ mounts: (mcpHostContainer(pod).volumeMounts ?? [])
+ .filter((m: any) => m.name === 'workspace')
+ .map((m: any) => ({
+ mountPath: m.mountPath,
+ name: m.name,
+ ...(m.subPath ? { subPath: m.subPath } : {}),
+ })),
+ store: envValue(pod, 'CLERUM_SESSION_STORE'),
+ dbDir: envValue(pod, 'CLERUM_SESSION_DB_DIR'),
+ inits: (pod.spec?.initContainers ?? []).map((c: any) => c.name),
+ }
+}
+
+/** Exactly one Pod, Ready, not terminating, with a UID different from `oldUid`. */
+async function waitNewReadyPod(hostName: string, oldUid: string): Promise<any> {
+ let found: any = null
+ // The poll returns 'READY' or the observed Pods, so a timeout prints what it saw.
+ await expect
+ .poll(
+ async () => {
+ const pods = await kubectlJson([
+ 'get',
+ 'pods',
+ '-n',
+ HOST_NAMESPACE,
+ '-l',
+ `app=${hostName}`,
+ ])
+ const items: any[] = pods.items ?? []
+ const observed = JSON.stringify(
+ items.map(p => ({
+ uid: p.metadata?.uid,
+ phase: p.status?.phase,
+ deleting: Boolean(p.metadata?.deletionTimestamp),
+ init: (p.status?.initContainerStatuses ?? []).map((s: any) => s.state),
+ }))
+ )
+ const pod = items.length === 1 ? items[0] : null
+ const ready =
+ pod &&
+ !pod.metadata?.deletionTimestamp &&
+ pod.metadata?.uid !== oldUid &&
+ (pod.status?.conditions ?? []).some((c: any) => c.type === 'Ready' && c.status === 'True')
+ found = ready ? pod : null
+ return found ? 'READY' : observed
+ },
+ {
+ timeout: POD_READY_TIMEOUT_MS,
+ intervals: [3_000],
+ message: `${hostName}: a new Ready Pod replacing ${oldUid || '<none>'}`,
+ }
+ )
+ .toBe('READY')
+ return found
+}
+
+/** The lifecycle witness: the new Pod runs the requested lifecycle on the one root layout. */
+function assertPod(pod: any, stateless: boolean): PodStorage {
+ const name = pod.metadata?.name
+ const flag = envValue(pod, 'CLERUM_STATELESS_LIFECYCLE')
+ const priority = pod.spec?.priorityClassName ?? ''
+ if (stateless) {
+ expect(flag, `${name}: CLERUM_STATELESS_LIFECYCLE`).toBe('true')
+ expect(priority, `${name}: priority class`).toBe('clerum-interactive-host')
+ } else {
+ expect(flag, `${name}: CLERUM_STATELESS_LIFECYCLE`).toBeUndefined()
+ expect(priority, `${name}: priority class`).not.toBe('clerum-interactive-host')
+ }
+ const storage = podStorage(pod)
+ expect(storage, `${name}: storage layout`).toEqual({
+ mounts: [{ mountPath: WORKSPACE_PATH, name: 'workspace' }],
+ store: 'sqlite',
+ dbDir: WORKSPACE_PATH,
+ inits: ['workspace-layout-merge'],
+ })
+ const merge = (pod.status?.initContainerStatuses ?? []).find(
+ (s: any) => s.name === 'workspace-layout-merge'
+ )
+ expect(merge?.state?.terminated?.exitCode, `${name}: workspace-layout-merge exit code`).toBe(0)
+ return storage
+}
+
+async function newChatFromAgentsPage(page: Page, hostName: string): Promise<void> {
+ await openAgentsPage(page)
+ await page.getByRole('button', { name: `More actions for ${hostName}`, exact: true }).click()
+ const menu = page.getByRole('menu')
+ await expect(menu).toBeVisible({ timeout: 15_000 })
+ const newChat = menu
+ .getByRole('button', { name: /^New chat$/ })
+ .or(menu.getByRole('menuitem', { name: /^New chat$/ }))
+ await newChat.click()
+ await expect(page.getByRole('textbox', { name: 'Agent message composer' })).toBeVisible({
+ timeout: 30_000,
+ })
+ await expect(page.getByRole('button', { name: /^Switch chat agent$/ })).toContainText(hostName, {
+ timeout: 30_000,
+ })
+}
+
+async function sendMarker(page: Page, marker: string): Promise<void> {
+ const composer = page.getByRole('textbox', { name: 'Agent message composer' })
+ const sendButton = page.getByTestId('send-button')
+ await composer.fill(`Reply with exactly: ${marker}`)
+ await expect(sendButton).toBeEnabled()
+ await sendButton.click()
+ await expect(page.getByTestId('message-list')).toContainText(marker, { timeout: 30_000 })
+ await expect(page.getByTestId('agent-response').filter({ hasText: marker })).toBeVisible({
+ timeout: 180_000,
+ })
+ await expect(composer).toHaveValue('', { timeout: 30_000 })
+ await expect(sendButton).toHaveAttribute('aria-label', 'Send message', { timeout: 180_000 })
+}
+
+async function renameActiveSession(page: Page, title: string): Promise<void> {
+ const row = page.locator('.nav-latest-session.active')
+ await expect(row).toBeVisible({ timeout: 30_000 })
+ await row.getByRole('button', { name: /^Session options for / }).click()
+ await page.getByRole('menuitem', { name: 'Rename', exact: true }).click()
+ const input = page.getByRole('textbox', { name: 'Rename session', exact: true })
+ await expect(input).toBeVisible()
+ await input.fill(title)
+ await input.press('Enter')
+ await expect(page.getByRole('button', { name: `Open ${title}`, exact: true })).toBeVisible({
+ timeout: 30_000,
+ })
+}
+
+async function localChatId(page: Page, hostName: string, title: string): Promise<string> {
+ let chatId: string | null = null
+ await expect
+ .poll(
+ async () => {
+ const chats = await page.evaluate(
+ async hostRef => (window as any).clerum.chat.list(hostRef),
+ hostName
+ )
+ const chat = (Array.isArray(chats) ? chats : []).find((c: any) => c.title === title)
+ chatId = chat ? (chat.chatId ?? chat.id ?? null) : null
+ return chatId
+ },
+ { timeout: 20_000, message: `local chat titled ${title} for ${hostName}` }
+ )
+ .not.toBeNull()
+ return chatId as unknown as string
+}
+
+/** The Host's own view of the conversation, read through rpc-proxy. */
+async function hostConversation(
+ page: Page,
+ hostName: string,
+ chatId: string
+): Promise<{ sessions: Array<{ chatId: string; turnCount: number }>; transcript: string }> {
+ return page.evaluate(
+ async ({ hostRef, chatId: id }) => {
+ const list = await (window as any).clerum.rpc.listSessions(hostRef)
+ const sessions = (list?.items ?? []).map((item: any) => ({
+ chatId: item.chatId,
+ turnCount: item.turnCount,
+ agent: item.agent,
+ }))
+ const own = sessions.find((s: any) => s.chatId === id)
+ const messages = own
+ ? await (window as any).clerum.rpc.loadSessionMessages(hostRef, own.agent, id)
+ : null
+ return {
+ sessions: sessions.map(({ chatId: c, turnCount }: any) => ({ chatId: c, turnCount })),
+ transcript: messages ? JSON.stringify(messages.turns) : '',
+ }
+ },
+ { hostRef: hostName, chatId }
+ )
+}
+
+/** The Host lists exactly this one conversation with `turns` turns, and its transcript holds every marker. */
+async function expectHostHistory(
+ page: Page,
+ hostName: string,
+ chatId: string,
+ turns: number,
+ markers: string[]
+): Promise<void> {
+ // The poll returns what the Host reports, so a timeout prints the difference.
+ await expect
+ .poll(
+ async () => {
+ const view = await hostConversation(page, hostName, chatId)
+ return {
+ sessions: view.sessions,
+ missingMarkers: markers.filter(marker => !view.transcript.includes(marker)),
+ }
+ },
+ {
+ timeout: 60_000,
+ intervals: [2_000],
+ message: `${hostName}: one session ${chatId} with ${turns} turns holding every marker`,
+ }
+ )
+ .toEqual({ sessions: [{ chatId, turnCount: turns }], missingMarkers: [] })
+}
+
+async function closeDesktop(app: ElectronApplication): Promise<void> {
+ const child = app.process()
+ await app.close()
+ await expect
+ .poll(() => child.exitCode !== null || child.signalCode !== null, {
+ timeout: 30_000,
+ message: `Electron process ${child.pid} must exit`,
+ })
+ .toBe(true)
+}
+
+test.skip(
+ process.env.E2E_825_DESKTOP_CONTINUITY !== '1',
+ 'Set E2E_825_DESKTOP_CONTINUITY=1 for this cluster-mutating #825 journey'
+)
+test.describe.configure({ retries: 0 })
+
+test('#825 — a conversation survives stateful -> stateless -> stateful switches', async ({}, testInfo) => {
+ test.setTimeout(1_800_000)
+
+ await assertMinikubeProfile()
+ const controlApiUrl = requiredEnv('CONTROL_API_BASE_URL', [
+ process.env.E2E_CONTROL_API_URL,
+ process.env.CONTROL_API_BASE_URL,
+ ])
+ await assertAllowedTarget('CONTROL_API_BASE_URL', controlApiUrl)
+ await assertAllowedTarget('EXTERNAL_REST_API_BASE_URL', EXTERNAL_REST_API_BASE_URL)
+ await assertAllowedTarget('RPC_PROXY_BASE_URL', RPC_PROXY_BASE_URL)
+ const credentials = desktopCredentials()
+ requiredEnv('E2E_DEV_LOGIN_EMAIL', [process.env.E2E_DEV_LOGIN_EMAIL])
+ const sourceHost = process.env.E2E_825_SOURCE_HOST?.trim() || 'chatllm'
+
+ const runId = `${Date.now().toString(36)}${Math.random().toString(36).slice(2, 6)}`
+ const hostName = `e2e-825-desk-${runId}`
+ const title = `E2E_825_${runId}`
+ const markers = [`E825_A_${runId}`, `E825_B_${runId}`, `E825_C_${runId}`]
+
+ const admin = await AdminApi.login(controlApiUrl)
+ let hostCreated = false
+ let journeyFailed = false
+ const grantPaths: string[] = []
+ let app: ElectronApplication | undefined
+ let page: Page | undefined
+ const currentPage = (): Page => {
+ if (!page) throw new Error('Desktop page is not open at this step')
+ return page
+ }
+
+ try {
+ let pod: any
+ let storage: PodStorage
+
+ await test.step('precondition: create and grant a stateful Host through the admin API', async () => {
+ const source = await kubectlJson(['get', 'host', sourceHost, '-n', HOST_NAMESPACE])
+ const userId = await admin.userIdByEmail(credentials.email)
+ const teamId = await admin.firstTeamId(userId)
+ await admin.createHost(hostName, source.spec, false)
+ hostCreated = true
+ grantPaths.push(`/admin/users/${userId}/agents`)
+ if (teamId) grantPaths.push(`/admin/teams/${teamId}/agents`)
+ for (const path of grantPaths) await admin.setAgentGrant(path, hostName, 'add')
+ pod = await waitNewReadyPod(hostName, '')
+ storage = assertPod(pod, false)
+ })
+
+ await test.step('1. log in through the UI', async () => {
+ ;({ app, page } = await launchDesktopApp(testInfo))
+ await login(currentPage(), credentials)
+ await expect(currentPage().getByTestId('nav-chat')).toBeVisible({ timeout: 30_000 })
+ })
+
+ let chatId = ''
+ await test.step('2. create a conversation and send the first marker', async () => {
+ const p = currentPage()
+ await newChatFromAgentsPage(p, hostName)
+ await sendMarker(p, markers[0])
+ await renameActiveSession(p, title)
+ chatId = await localChatId(p, hostName, title)
+ await expectHostHistory(p, hostName, chatId, 1, markers.slice(0, 1))
+ })
+
+ const rounds: Array<{ stateless: boolean; marker: string; turns: number }> = [
+ { stateless: true, marker: markers[1], turns: 2 },
+ { stateless: false, marker: markers[2], turns: 3 },
+ ]
+ for (const round of rounds) {
+ const label = round.stateless ? 'stateless' : 'stateful'
+
+ await test.step(`3. switch the Host to ${label} and witness the new Pod`, async () => {
+ const oldUid = pod.metadata.uid
+ await admin.setLifecycle(hostName, round.stateless)
+ pod = await waitNewReadyPod(hostName, oldUid)
+ expect(assertPod(pod, round.stateless), 'storage is unchanged across the switch').toEqual(
+ storage
+ )
+ })
+
+ await test.step(`4. restart Desktop and reopen the conversation from the sidebar (${label})`, async () => {
+ if (!app) throw new Error('Electron app is not running at this step')
+ await closeDesktop(app)
+ app = undefined
+ page = undefined
+ ;({ app, page } = await launchDesktopApp(testInfo))
+ const p = currentPage()
+ await expect(p.locator('.boot-overlay')).toBeHidden({ timeout: 30_000 })
+ await expect(p.getByTestId('nav-chat')).toBeVisible({ timeout: 30_000 })
+ await expect(p.locator('#email-input')).toBeHidden()
+ await p.getByRole('button', { name: `Open ${title}`, exact: true }).click()
+ const composer = p.getByRole('textbox', { name: 'Agent message composer' })
+ await expect(composer).toHaveAttribute(
+ 'placeholder',
+ `Message ${hostName}... (Enter to send, Shift+Enter for newline)`,
+ { timeout: 30_000, ignoreCase: true }
+ )
+ const previous = markers.slice(0, round.turns - 1)
+ for (const marker of previous) {
+ await expect(p.getByTestId('message-list')).toContainText(marker, { timeout: 60_000 })
+ }
+ await expectHostHistory(p, hostName, chatId, round.turns - 1, previous)
+ })
+
+ await test.step(`5. send a follow-up in the same conversation (${label})`, async () => {
+ const p = currentPage()
+ await sendMarker(p, round.marker)
+ expect(await localChatId(p, hostName, title)).toBe(chatId)
+ await expectHostHistory(p, hostName, chatId, round.turns, markers.slice(0, round.turns))
+ })
+ }
+ } catch (error) {
+ journeyFailed = true
+ throw error
+ } finally {
+ await finalizeRecording(app, page)
+ const cleanupErrors: string[] = []
+ if (hostCreated) {
+ for (const step of [
+ () => admin.deleteHost(hostName),
+ ...grantPaths.map(path => () => admin.setAgentGrant(path, hostName, 'remove')),
+ () =>
+ kubectl(
+ [
+ 'wait',
+ '--for=delete',
+ `deployment/${hostName}`,
+ '-n',
+ HOST_NAMESPACE,
+ '--timeout=120s',
+ ],
+ 150_000
+ ).then(() => undefined),
+ () =>
+ kubectl(
+ [
+ 'delete',
+ 'pvc',
+ `${hostName}-workspace`,
+ '-n',
+ HOST_NAMESPACE,
+ '--ignore-not-found',
+ '--wait=true',
+ '--timeout=120s',
+ ],
+ 150_000
+ ).then(() => undefined),
+ ]) {
+ try {
+ await step()
+ } catch (error) {
+ cleanupErrors.push(String(error))
+ }
+ }
+ }
+ await admin.dispose()
+ // Cleanup failures leave cluster state behind. They fail the test; when the
+ // journey already failed they are printed so its own error stays the cause.
+ if (cleanupErrors.length > 0) {
+ // eslint-disable-next-line no-console
+ console.error(`[e2e-825] cleanup of ${hostName} failed:\n${cleanupErrors.join('\n')}`)
+ if (!journeyFailed)
+ throw new Error(`cleanup of ${hostName} failed: ${cleanupErrors.join('; ')}`)
+ }
+ }
+})