Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

7.5 KB
Raw Blame
import {
  readPersistedProfileState,
  mutateStoppedProfileState
} from './helpers/persisted-profile-state'
// Keep finished local agents from respawning after pane retirement and app restart; retained resume identity must not restate done as working.
import { existsSync, readFileSync } from 'node:fs'
import type { ElectronApplication } from '@stablyai/playwright-test'
import { test, expect } from './helpers/orca-app'
import { TEST_REPO_PATH_FILE } from './global-setup'
import {
  execInTerminal,
  waitForActivePaneHookDescriptor,
  waitForActivePanePtyId,
  waitForActiveTerminalManager,
  waitForPaneCount,
  waitForTerminalOutput
} from './helpers/terminal'
import { ensureTerminalVisible, waitForActiveWorktree, waitForSessionReady } from './helpers/store'
import { attachRepoAndOpenTerminal, createRestartSession } from './helpers/orca-restart'
import { createHostRendererTerminalTab } from './helpers/host-created-terminal-retention-oracle'

const PROVIDER_SESSION_ID = 'e2e-finished-agent-session'

type PersistedRecord = {
  state?: unknown
  origin?: unknown
  providerSession?: { id?: unknown }
  launchConfig?: { agentCommand?: string; agentArgs?: string; agentEnv?: Record<string, string> }
}

function readPersistedRecords(userDataDir: string): Record<string, PersistedRecord> {
  // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This test owns the persisted fixture; optional fields are checked at use sites.
  const data = readPersistedProfileState(userDataDir) as {
    workspaceSession?: { sleepingAgentSessionsByPaneKey?: Record<string, PersistedRecord> }
  }
  return data.workspaceSession?.sleepingAgentSessionsByPaneKey ?? {}
}

/** Make the resume hermetic: the respawned tab echoes instead of running codex. */
function stubPersistedResumeCommand(userDataDir: string): PersistedRecord {
  return mutateStoppedProfileState(userDataDir, (state) => {
    // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: This test owns the persisted fixture; optional fields are checked at use sites.
    const data = state as {
      workspaceSession?: { sleepingAgentSessionsByPaneKey?: Record<string, PersistedRecord> }
    }
    const record = Object.values(data.workspaceSession?.sleepingAgentSessionsByPaneKey ?? {}).find(
      (candidate) => candidate.providerSession?.id === PROVIDER_SESSION_ID
    )
    if (!record) {
      throw new Error('Expected the finished agent turn to leave a persisted record')
    }
    record.launchConfig = { agentCommand: 'echo', agentArgs: '', agentEnv: {} }
    return record
  })
}

test.describe.configure({ mode: 'serial' })

test('does not respawn an agent whose turn already finished', 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) => {
  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 session = createRestartSession(testInfo)
  let firstApp: ElectronApplication | null = null
  let secondApp: ElectronApplication | null = null

  try {
    const firstLaunch = await session.launch()
    firstApp = firstLaunch.app
    const page = await firstApp.firstWindow()
    const worktreeId = await attachRepoAndOpenTerminal(page, repoPath)
    await waitForSessionReady(page)
    await waitForActiveWorktree(page)
    await ensureTerminalVisible(page)
    await waitForActiveTerminalManager(page, 30_000)
    await waitForPaneCount(page, 1, 30_000)

    const marker = `FINISHED_AGENT_${Date.now()}`
    const descriptor = await waitForActivePaneHookDescriptor(page)
    const ptyId = await waitForActivePanePtyId(page)
    const transcriptPath = session.seedCodexResumeRollout(PROVIDER_SESSION_ID, repoPath)
    await execInTerminal(page, ptyId, `echo ${marker}`)
    await waitForTerminalOutput(page, marker)

    await page.evaluate(
      ({ paneKey, worktreeId: wtId, providerSessionId, transcriptPath }) => {
        window.__store
          ?.getState()
          .setAgentStatus(
            paneKey,
            { state: 'done', prompt: 'review the diff', agentType: 'codex' },
            'Codex',
            undefined,
            { worktreeId: wtId },
            { providerSession: { key: 'session_id', id: providerSessionId, transcriptPath } }
          )
      },
      {
        paneKey: descriptor.paneKey,
        worktreeId: descriptor.worktreeId,
        providerSessionId: PROVIDER_SESSION_ID,
        transcriptPath
      }
    )

    const liveRecord = await page.evaluate((paneKey) => {
      const record = window.__store?.getState().sleepingAgentSessionsByPaneKey[paneKey]
      return record ? { state: record.state, origin: record.origin } : null
    }, descriptor.paneKey)
    expect(liveRecord, 'a finished turn leaves a resume record').not.toBeNull()
    expect(liveRecord?.state, 'the done turn stays done').toBe('done')
    expect(liveRecord?.origin).toBe('live')

    // Keep another terminal because empty workspaces do not reactivate on relaunch.
    const survivingTabId = await createHostRendererTerminalTab(page, worktreeId)

    // Use PTY-exit close to retire the pane while retaining its resume record.
    const tabId = await page.evaluate(
      (wtId) => (window.__store?.getState().tabsByWorktree[wtId] ?? [])[0]?.id ?? null,
      worktreeId
    )
    expect(tabId, 'the agent pane must have a tab to close').not.toBeNull()
    expect(tabId).not.toBe(survivingTabId)
    await page.evaluate(
      (id) => window.__store?.getState().closeTab(id, { reason: 'pty-exit' }),
      tabId!
    )
    await expect
      .poll(
        async () =>
          page.evaluate(
            (wtId) => (window.__store?.getState().tabsByWorktree[wtId] ?? []).map((tab) => tab.id),
            worktreeId
          ),
        { timeout: 15_000, message: 'the pty-exit close never removed the agent tab' }
      )
      .toEqual([survivingTabId])

    await session.close(firstApp)
    firstApp = null

    const persisted = readPersistedRecords(session.userDataDir)
    const survivor = Object.values(persisted).find(
      (candidate) => candidate.providerSession?.id === PROVIDER_SESSION_ID
    )
    expect(survivor, 'killing the pane left the resume record behind').toBeDefined()
    stubPersistedResumeCommand(session.userDataDir)

    const secondLaunch = await session.launch()
    secondApp = secondLaunch.app
    await waitForSessionReady(secondLaunch.page)
    await expect
      .poll(
        async () => secondLaunch.page.evaluate(() => window.__store?.getState().activeWorktreeId),
        { timeout: 15_000 }
      )
      .toBe(worktreeId)
    await ensureTerminalVisible(secondLaunch.page)

    const respawned = await secondLaunch.page.evaluate((wtId) => {
      const state = window.__store?.getState()
      const tabs = state?.tabsByWorktree[wtId] ?? []
      return tabs.map((tab) => ({
        id: tab.id,
        launchAgent: tab.launchAgent ?? null,
        startup: state?.pendingStartupByTabId[tab.id]?.command ?? null,
        banner: state?.pendingStartupByTabId[tab.id]?.showSessionRestoredBanner ?? false
      }))
    }, worktreeId)
    const resumeTabs = respawned.filter(
      (tab) => tab.launchAgent === 'codex' || tab.startup?.includes(PROVIDER_SESSION_ID)
    )
    expect(resumeTabs, `a finished agent was respawned: ${JSON.stringify(respawned)}`).toEqual([])
  } finally {
    if (secondApp) {
      await session.close(secondApp)
    }
    if (firstApp) {
      await session.close(firstApp)
    }
    await session.dispose()
  }
})