import { beforeAll, describe, expect, it } from 'vitest' import { importReleaseCheckoutModule, importWorkingTreeModuleCopy, materializeReleaseCheckout } from './release-checkout' // Pin pre-mainAgent readers: old phones must read new rows by interrupted; new phones must keep that fallback for old hosts. const PRE_CHANGE_REF = 'v1.4.212' const SUITE_TIMEOUT_MS = 180_000 // Both builds load a copy in the checkout cache, out of reach of `mobile/tsconfig.json`. const PHONE_ROW_READER = 'mobile/src/worktree/agent-row-display.ts' const WORKTREE_ID = 'repo::/worktree' const NOW = 1_000_000 type HookSnapshot = Record type AgentRow = Record type DotState = (row: AgentRow, now: number) => string type HostRowModules = { collectRuntimeWorktreeAgentSources: (args: Record) => unknown attachRuntimeWorktreeAgentRows: (args: Record) => void } type Build = { label: string host: HostRowModules agentDotState: DotState } function snapshot( tabId: string, fields: Pick & Partial ): HookSnapshot { return { paneKey: `${tabId}:11111111-1111-4111-8111-111111111111`, tabId, worktreeId: WORKTREE_ID, connectionId: null, prompt: 'ship it', agentType: 'claude', receivedAt: NOW - 1_000, stateStartedAt: NOW - 600_000, ...fields } } // Separate main-agent failure, failure with working children, and an explicit user stop. const SNAPSHOTS: HookSnapshot[] = [ snapshot('failed-done', { state: 'done', mainAgent: { state: 'done', outcome: 'failure', stateStartedAt: NOW - 600_000 } }), snapshot('failed-working', { state: 'working', mainAgent: { state: 'done', outcome: 'failure', stateStartedAt: NOW - 120_000 } }), snapshot('stopped', { state: 'done', interrupted: true, mainAgent: { state: 'done', outcome: 'cancellation', stateStartedAt: NOW - 600_000 } }), // Host-observed crash/unknown endings set no interrupted flag, so old readers still see done. snapshot('crash-cut', { state: 'done', mainAgent: { state: 'done', outcome: 'interruption', stateStartedAt: NOW - 600_000 } }), snapshot('unproven', { state: 'done', mainAgent: { state: 'done', outcome: 'unconfirmed', stateStartedAt: NOW - 600_000 } }), // A turn a newer request replaced: recorded, not a user's stop, so it sets no flag either. snapshot('replaced', { state: 'done', mainAgent: { state: 'done', outcome: 'superseded', stateStartedAt: NOW - 600_000 } }) ] /** A host's `worktree ps` rows, JSON round-tripped as the wire carries them. */ function publishRows(host: HostRowModules): Record { const summary: Record = { worktreeId: WORKTREE_ID, status: 'inactive', hasHostSidebarActivity: false, agents: [] } const summaries = new Map([[WORKTREE_ID, summary]]) host.attachRuntimeWorktreeAgentRows({ summaries, pathIndex: { byPath: new Map(), byRealPath: new Map() }, missingWorktreeIds: new Set(), // Builds read workingTerminalEvidenceByWorktreeId, then terminalTitleEvidenceByWorktreeId, now terminalTitleEvidenceByRow. workingTerminalEvidenceByWorktreeId: new Map(), terminalTitleEvidenceByWorktreeId: new Map(), terminalTitleEvidenceByRow: new Map(), rowSources: host.collectRuntimeWorktreeAgentSources({ hookSnapshots: SNAPSHOTS, mirroredWorktreeIdByTabId: new Map(), connectedPtyEvidence: { tabIds: new Set(SNAPSHOTS.map((row) => row.tabId)), paneKeys: new Set(), ptyIdByTerminalHandle: new Map() } }), orchestrationByPaneKey: null, // Old builds pass (summaries, pathIndex, missingIds, id), then (id), now (id, hostId). getSummary: (...args: unknown[]) => summaries.get(String(args.find((arg) => arg === WORKTREE_ID))) ?? null }) // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: JSON.parse of the rows the host just attached, which are plain objects keyed by paneKey. const rows = JSON.parse(JSON.stringify(summary.agents)) as AgentRow[] return Object.fromEntries(rows.map((row) => [String(row.paneKey).split(':')[0], row])) } function dotStates(build: Build, rows: Record): Record { return Object.fromEntries( Object.entries(rows).map(([tabId, row]) => [tabId, build.agentDotState(row, NOW)]) ) } async function loadBuild(ref: string | null): Promise { if (ref === null) { const [sources, rows, display] = await Promise.all([ import('../../../src/main/runtime/workspaces/process-listing/runtime-worktree-agent-sources'), import('../../../src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows'), importWorkingTreeModuleCopy(PHONE_ROW_READER) ]) return { label: 'current', // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed by its module; this spec drives both builds through one untyped surface. host: { ...sources, ...rows } as unknown as HostRowModules, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import of a copy is typed unknown; this is the current phone's row reader by path. agentDotState: display.agentDotState as unknown as DotState } } const checkout = await materializeReleaseCheckout(ref) const [sources, rows, display] = await Promise.all([ importReleaseCheckoutModule( checkout, 'src/main/runtime/workspaces/process-listing/runtime-worktree-agent-sources.ts' ), importReleaseCheckoutModule( checkout, 'src/main/runtime/workspaces/process-listing/runtime-worktree-agent-rows.ts' ), importReleaseCheckoutModule(checkout, PHONE_ROW_READER) ]) return { label: ref, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; these two modules are the old host's row surface by path. host: { ...sources, ...rows } as unknown as HostRowModules, // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: a dynamic import is typed unknown; this is the old phone's row reader by path. agentDotState: display.agentDotState as unknown as DotState } } let oldBuild: Build let newBuild: Build let agentRowTimeAt: (row: AgentRow) => number beforeAll(async () => { ;[oldBuild, newBuild] = await Promise.all([loadBuild(PRE_CHANGE_REF), loadBuild(null)]) const display = await importWorkingTreeModuleCopy(PHONE_ROW_READER) // oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the rows are JSON the current host published, so the current reader's row type holds. agentRowTimeAt = display.agentRowTimeAt as unknown as (row: AgentRow) => number }, SUITE_TIMEOUT_MS) describe('cross-version worktree ps verdict', () => { it('an OLD phone reads a new host row by the interrupted flag it always read', () => { const rows = publishRows(newBuild.host) expect(rows.stopped).toMatchObject({ interrupted: true }) expect(dotStates(oldBuild, rows)).toEqual({ 'failed-done': 'done', 'failed-working': 'working', stopped: 'interrupted', 'crash-cut': 'done', unproven: 'done', replaced: 'done' }) expect(rows['crash-cut']).toMatchObject({ interrupted: false }) expect(rows.unproven).toMatchObject({ interrupted: false }) expect(rows.replaced).toMatchObject({ interrupted: false }) }) it('a NEW phone reads an old host row, which has no mainAgent, by the same flag', () => { const rows = publishRows(oldBuild.host) expect(dotStates(newBuild, rows)).toEqual({ 'failed-done': 'done', 'failed-working': 'working', // Both phones draw a user's Stop interrupted; only the new one draws it muted. stopped: 'interrupted', 'crash-cut': 'done', unproven: 'done', replaced: 'done' }) // Without the main agent's clock the row dates itself, as it always did. expect(agentRowTimeAt(rows['failed-working'])).toBe(NOW - 600_000) }) it('a NEW phone reads a new host failure as failed, dated by the main agent clock', () => { const rows = publishRows(newBuild.host) expect(dotStates(newBuild, rows)).toEqual({ 'failed-done': 'failed', 'failed-working': 'failed', stopped: 'interrupted', 'crash-cut': 'failed', unproven: 'unconfirmed', replaced: 'interrupted' }) expect(rows['failed-working']).toMatchObject({ state: 'working', mainAgent: { state: 'done', outcome: 'failure', stateStartedAt: NOW - 120_000 } }) expect(agentRowTimeAt(rows['failed-working'])).toBe(NOW - 120_000) }) })