Files
orca/tests/e2e/local-cli-terminal-retention.spec.ts

323 lines
12 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Use the shipped local CLI socket with one attached desktop and no paired client.
// Create the first host-admitted tab under a proven renderer publication epoch; graph sync must retain its original PTY.
// On relaunch, a genuinely absent pane’s disk-seeded resume record proves the sweep runs while the retained CLI pane must not gain a ghost tab.
import { randomUUID } from 'node:crypto'
import { existsSync, readFileSync, rmSync } from 'node:fs'
import path from 'node:path'
import type { ElectronApplication, Page } from '@stablyai/playwright-test'
import { RuntimeClient } from '../../src/cli/runtime-client'
import { makePaneKey } from '../../src/shared/workspace-layout/stable-pane-id'
import { expect, test } from './helpers/orca-app'
import { TEST_REPO_PATH_FILE } from './global-setup'
import { attachRepoAndOpenTerminal, createRestartSession } from './helpers/orca-restart'
import { mutateStoppedProfileState } from './helpers/persisted-profile-state'
import { waitForSessionReady } from './helpers/store'
import {
createHostCliTerminal,
createHostRendererTerminalTab,
createRetentionFixtureDirectory,
proveSameLivePty,
readHostInventoryWhenTabAppears,
readHostTerminalInventory,
writeRetentionFixture,
type RuntimeRpcCall
} from './helpers/host-created-terminal-retention-oracle'
const scratch = createRetentionFixtureDirectory()
const fixturePath = writeRetentionFixture(scratch)
const CLI_PANE_SESSION_ID = 'local-cli-retention-cli-pane'
const ABSENT_PANE_SESSION_ID = 'local-cli-retention-absent-pane'
test.afterAll(() => {
rmSync(scratch, { recursive: true, force: true })
})
// First paint can precede runtime worktree discovery; wait until selectors resolve the seeded workspace.
async function waitForLocalRuntimeWorktree(
call: RuntimeRpcCall,
worktreeId: string
): Promise<void> {
await expect
.poll(
async () =>
call('worktree.show', { worktree: `id:${worktreeId}` }).then(
() => true,
() => false
),
{
timeout: 120_000,
message: `local runtime never resolved worktree ${worktreeId}`
}
)
.toBe(true)
}
type WorktreeTabSnapshot = {
tabIds: string[]
resumeTabIds: string[]
}
function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
/** The stopped profile's resume records, as the live object a mutation writes through. */
function sleepingRecordsOf(state: Record<string, unknown>): Record<string, unknown> {
const session = state.workspaceSession
if (!isObject(session)) {
throw new Error('The stopped profile has no workspace session')
}
const existing = session.sleepingAgentSessionsByPaneKey
const records = isObject(existing) ? existing : {}
session.sleepingAgentSessionsByPaneKey = records
return records
}
/** The fixture's external worktree is hidden on a new repo; the unrelated-workspace control needs it. */
async function showSeededWorktrees(page: Page): Promise<void> {
await expect
.poll(
() =>
page.evaluate(async () => {
const store = window.__store
const repo = store?.getState().repos[0]
if (!store || !repo) {
return 0
}
await store.getState().updateRepo(repo.id, { externalWorktreeVisibility: 'show' })
await store.getState().fetchWorktrees(repo.id)
return store.getState().allWorktrees().length
}),
{ timeout: 30_000, message: 'seeded e2e worktrees did not load' }
)
.toBeGreaterThan(1)
}
function runtimeCall(userDataDir: string): RuntimeRpcCall {
// The shipped CLI's own transport, against this app's profile: no pairing.
const client = new RuntimeClient(userDataDir, 30_000, null, null)
return async <TResult>(method: string, params: unknown) =>
(await client.call<TResult>(method, params)).result
}
test('keeps a locally created CLI terminal, and never resumes it as a ghost', async (// oxlint-disable-next-line no-empty-pattern -- Playwright's second fixture arg is testInfo; the first must be an object destructure to opt out of the default fixture set.
{}, testInfo) => {
test.setTimeout(600_000)
const repoPath = readFileSync(TEST_REPO_PATH_FILE, 'utf-8').trim()
if (!repoPath || !existsSync(repoPath)) {
test.skip(true, 'Global setup did not produce a seeded test repo')
return
}
const pageErrors: string[] = []
const session = createRestartSession(testInfo)
let app: ElectronApplication | null = null
let call: RuntimeRpcCall | null = null
const createdHandles: string[] = []
try {
const firstLaunch = await session.launch()
app = firstLaunch.app
const firstPage = firstLaunch.page
firstPage.on('pageerror', (error) => pageErrors.push(String(error)))
const worktreeId = await attachRepoAndOpenTerminal(firstPage, repoPath)
await waitForSessionReady(firstPage)
await showSeededWorktrees(firstPage)
const unrelatedWorktreeId = await firstPage.evaluate((active) => {
const unrelated = window.__store
?.getState()
.allWorktrees()
.find((worktree) => worktree.id !== active)
if (!unrelated) {
throw new Error('Host fixture needs a second worktree for the unrelated-workspace control')
}
return unrelated.id
}, worktreeId)
call = runtimeCall(session.userDataDir)
await waitForLocalRuntimeWorktree(call, worktreeId)
await waitForLocalRuntimeWorktree(call, unrelatedWorktreeId)
// Require renderer publication so the CLI tab cannot inherit the already-safe headless epoch.
const rendererTabId = await createHostRendererTerminalTab(firstPage, worktreeId)
const rendererOwned = await readHostInventoryWhenTabAppears(
call,
worktreeId,
rendererTabId,
'Host never published the renderer terminal tab'
)
expect(
rendererOwned.publicationEpoch,
'the attached-window topology requires a renderer-owned publication; a headless epoch takes a different, already-correct path'
).toMatch(/^renderer:/)
const baselineTabIds = [...rendererOwned.tabIds].sort()
const cli = await createHostCliTerminal(
call,
worktreeId,
fixturePath,
path.join(scratch, 'local-target.log')
)
createdHandles.push(cli.handle)
const unrelated = await createHostCliTerminal(
call,
unrelatedWorktreeId,
fixturePath,
path.join(scratch, 'local-unrelated.log')
)
createdHandles.push(unrelated.handle)
const secondRendererTabId = await createHostRendererTerminalTab(firstPage, worktreeId)
// Judge the inventory frame that introduces the renderer tab and could prune the CLI tab.
const afterSync = await readHostInventoryWhenTabAppears(
call,
worktreeId,
secondRendererTabId,
'Host never republished with the second renderer tab'
)
expect(
afterSync.tabIds,
'the renderer graph sync pruned the CLI-created terminal out of the host session inventory'
).toContain(cli.tabId)
expect(
afterSync.ptyIdByTabId[cli.tabId],
'the surviving tab must still name the original PTY, not a replacement'
).toBe(cli.ptyId)
await proveSameLivePty(call, cli, 'after-sync')
// Require exactly the original tabs so replacement/resume tabs cannot hide behind CLI presence.
expect(
[...afterSync.tabIds].sort(),
'the workspace gained or lost a terminal tab across the graph sync'
).toEqual([...baselineTabIds, cli.tabId, secondRendererTabId].sort())
// NEGATIVE SAFETY: no fanout into an unrelated workspace.
const unrelatedInventory = await readHostTerminalInventory(call, unrelatedWorktreeId)
expect(unrelatedInventory.tabIds, 'an unrelated workspace lost its CLI terminal').toContain(
unrelated.tabId
)
expect(unrelatedInventory.ptyIdByTabId[unrelated.tabId]).toBe(unrelated.ptyId)
await proveSameLivePty(call, unrelated, 'unrelated-alive')
const cliPaneKey = makePaneKey(cli.tabId, cli.leafId)
// Seed the same status-store entry a real agent hook reports, keeping this fixture hermetic.
await firstPage.evaluate(
({ paneKey, sessionId, worktreeId }) => {
window.__store?.getState().setAgentStatus(
paneKey,
{
state: 'working',
prompt: 'local cli retention',
agentType: 'claude'
},
'Claude',
undefined,
{ worktreeId },
{ providerSession: { key: 'session_id', id: sessionId } }
)
},
{ paneKey: cliPaneKey, sessionId: CLI_PANE_SESSION_ID, worktreeId }
)
await expect
.poll(
() =>
firstPage.evaluate(
(paneKey) =>
window.__store?.getState().sleepingAgentSessionsByPaneKey[paneKey] !== undefined,
cliPaneKey
),
{ timeout: 30_000, message: 'the seeded provider session never became a sleeping record' }
)
.toBe(true)
// Relaunch reruns the resume sweep with the retained pane; its daemon PTY stays alive.
await session.close(app)
app = null
// Seed an absent pane’s resume record as the positive control for sweep execution.
const absentTabId = randomUUID()
const absentPaneKey = makePaneKey(absentTabId, randomUUID())
mutateStoppedProfileState(session.userDataDir, (state) => {
const records = sleepingRecordsOf(state)
const cliRecord = records[cliPaneKey]
if (!isObject(cliRecord)) {
throw new Error('the CLI pane reported a session, so quit must save its record')
}
records[absentPaneKey] = {
...cliRecord,
paneKey: absentPaneKey,
tabId: absentTabId,
providerSession: { key: 'session_id', id: ABSENT_PANE_SESSION_ID }
}
})
const secondLaunch = await session.launch()
app = secondLaunch.app
const orcaPage = secondLaunch.page
orcaPage.on('pageerror', (error) => pageErrors.push(String(error)))
call = runtimeCall(session.userDataDir)
await waitForSessionReady(orcaPage)
await expect
.poll(() => orcaPage.evaluate(() => window.__store?.getState().activeWorktreeId), {
timeout: 60_000,
message: 'relaunched renderer never reactivated the measured worktree'
})
.toBe(worktreeId)
const readWorktreeTabs = async (): Promise<WorktreeTabSnapshot> =>
orcaPage.evaluate((worktreeId) => {
const tabs = window.__store?.getState().tabsByWorktree[worktreeId] ?? []
return {
tabIds: tabs.map((tab) => tab.id),
resumeTabIds: tabs.filter((tab) => tab.launchAgent === 'claude').map((tab) => tab.id)
}
}, worktreeId)
// Require the absent record’s replay before interpreting CLI silence as no ghost.
await expect
.poll(async () => (await readWorktreeTabs()).resumeTabIds.length > 0, {
timeout: 60_000,
message: 'the activation sweep never replayed the record whose pane is gone'
})
.toBe(true)
const resumed = await readWorktreeTabs()
expect(
resumed.resumeTabIds,
'the sweep replayed more than the one record whose pane is gone'
).toHaveLength(1)
expect(
resumed.tabIds,
'the retained CLI tab did not survive the relaunch the resume sweep runs on'
).toContain(cli.tabId)
// Main's inventory, not the window's tab: a layout-owner window names a hidden tab's PTY on mount.
expect(
(await readHostTerminalInventory(call, worktreeId)).ptyIdByTabId[cli.tabId],
'the relaunched CLI tab must still name the original PTY, not a replacement'
).toBe(cli.ptyId)
await proveSameLivePty(call, cli, 'after-relaunch')
expect(
await orcaPage.evaluate(
(paneKey) =>
window.__store?.getState().sleepingAgentSessionsByPaneKey[paneKey] !== undefined,
cliPaneKey
),
'the CLI pane owns its session, so its record must still be asleep, not consumed by a resume'
).toBe(true)
expect(
resumed.tabIds.filter((tabId) => !baselineTabIds.includes(tabId)).sort(),
'the relaunched workspace carries a tab no step of this journey created'
).toEqual([cli.tabId, secondRendererTabId, resumed.resumeTabIds[0]!].sort())
expect(pageErrors, 'host renderer raised an uncaught error').toEqual([])
} finally {
for (const handle of createdHandles) {
await call?.('terminal.closeTab', { terminal: handle }).catch(() => undefined)
}
if (app) {
await session.close(app)
}
await session.dispose()
}
})