Files
archived-orca/src/main/opencode/hook-plugin-opencode2-tui-ownership.test.ts
T
OrcaWinandOrcaWin 7d6603aa17 [comment audit] Complete repository-wide comment cleanup (#33249)
* [comment audit] Align legacy failure-read counts with cached display

* [comment audit] Condense shared provider lifecycle and browser recovery comments [skip ci]

* [comment audit] Condense shared generation, path and preview comments [skip ci]

* [comment audit] Condense host, file and Git contract rationale [skip ci]

* [comment audit] Tighten Git lifecycle and provider contract comments [skip ci]

* [comment audit] Clarify shared host authority and launch isolation [skip ci]

* [comment audit] simplify local access mobile and native chat contracts [skip ci]

* [comment audit] simplify native chat presentation and state contracts [skip ci]

* [comment audit] simplify native runtime and provider contracts [skip ci]

* [comment audit] simplify migration and orchestration contracts [skip ci]

* [comment audit] simplify plugin trust and process capture rationale [skip ci]

* [comment audit] simplify terminal delivery and remote compatibility rationale [skip ci]

* [comment audit] clarify remote recovery and launch wire rationale [skip ci]

* [comment audit] shorten shared RPC compatibility and runtime evidence comments [skip ci]

* [comment audit] simplify secure storage shell startup and sidebar rationale [skip ci]

* [comment audit] clarify sidebar, skill and SSH invariants [skip ci]

* [comment audit] condense structured chat delivery and timing rationale [skip ci]

* [comment audit] condense terminal replay and telemetry invariants [skip ci]

* [comment audit] clarify launch permission and terminal delivery rationale [skip ci]

* [comment audit] clarify workspace ownership and layout contracts [skip ci]

* [comment audit] wait for fitted and painted SSH terminal links

* [comment audit] clarify layout persistence and session ownership [skip ci]

* [comment audit] clarify host routing and workspace lifecycle contracts [skip ci]

* [comment audit] clarify startup persistence and app lifetime comments [skip ci]

* [comment audit] clarify recovery and activity boundaries [skip ci]

* [comment audit] condense activity and artifact invariants [skip ci]

* [comment audit] condense automation authority contracts [skip ci]

* [comment audit] simplify automation scheduling and history contracts [skip ci]

* [comment audit] clarify browser chrome and annotation lifecycles [skip ci]

* [comment audit] clarify browser routing and retained guest ownership [skip ci]

* [comment audit] clarify browser recovery and document ownership comments [skip ci]

* [comment audit] restore hosted runners required by native IME fixtures

* [comment audit] shorten preview, palette and dashboard invariants [skip ci]

* [comment audit] clarify terminal preview, dashboard and retention comments [skip ci]

* [comment audit] clarify diff-note recovery and editor lifecycle comments [skip ci]

* [comment audit] shorten rich-text and combined-diff ownership comments [skip ci]

* [comment audit] shorten autosave, draft recovery and model ownership comments [skip ci]

* [comment audit] shorten save-queue, notebook and Markdown constraints [skip ci]

* [comment audit] shorten Monaco, PDF and rich Markdown safeguards [skip ci]

* [comment audit] clarify rich Markdown source and selection safeguards [skip ci]

* [comment audit] clarify editor loading and emulator stream lifetimes [skip ci]

* [comment audit] trim emulator and feature tour narration [skip ci]

* [comment audit] clarify floating workspace and GitHub cache ownership [skip ci]

* [comment audit] trim provider controls and Linear filter explanations [skip ci]

* [comment audit] clarify pairing and chat resume lifetimes [skip ci]

* [comment audit] clarify chat delivery and transcript lifetimes [skip ci]

* [comment audit] clarify session options and host-owned status [skip ci]

* [comment audit] clarify draft durability and live-status precedence [skip ci]

* [comment audit] Trim chat send and model ownership explanations [skip ci]

* [comment audit] Shorten transcript ownership and send recovery comments [skip ci]

* [comment audit] Simplify native chat composer and transcript hook comments [skip ci]

* [comment audit] Shorten native chat scroll and structured session explanations [skip ci]

* [comment audit] Trim workspace picker and onboarding commentary [skip ci]

* [comment audit] Simplify pet rendering, review cache and quick open comments [skip ci]

* [comment audit] Trim sidebar and session history explanations [skip ci]

* [comment audit] Condense session resume and review checks comments [skip ci]

* [comment audit] Shorten checks state and file explorer refresh comments [skip ci]

* [comment audit] Trim plugin bridge and source-control action rationale [skip ci]

* [comment audit] Trim source-control listing and review rationale [skip ci]

* [comment audit] Shorten sidebar lifecycle and explorer safeguards [skip ci]

* [comment audit] Condense Settings account and appearance rationale [skip ci]

* [comment audit] Trim pairing and repository Settings explanations [skip ci]

* [comment audit] Condense Settings search SSH and terminal safeguards [skip ci]

* [comment audit] Shorten Settings voice and provider ownership comments [skip ci]

* [comment audit] Condense Settings draft persistence and host-state rationale [skip ci]

* [comment audit] Trim Settings hooks and sidebar setup rationale [skip ci]

* [comment audit] Condense sidebar deletion and board-filter safeguards [skip ci]

* [comment audit] Condense worktree cards, host ownership and Markdown contracts [skip ci]

* [comment audit] Condense sidebar drag, folder and filtering contracts [skip ci]

* [comment audit] Condense sidebar parity, sleep and host ownership contracts [skip ci]

* [comment audit] Prune focus-read baseline after comment cleanup

* [comment audit] Clarify sidebar visibility, board drops and agent evidence [skip ci]

* [comment audit] Tighten sidebar listing, drag and reveal contracts [skip ci]

* [comment audit] Clarify viewport geometry and skill update safeguards [skip ci]

* [comment audit] Tighten skill inventory, usage and host status comments [skip ci]

* [comment audit] Clarify usage ownership and resource kill safeguards [skip ci]

* [comment audit] Tighten tab selection, rename and search contracts [skip ci]

* [comment audit] Tighten tab-strip geometry and drag ownership comments [skip ci]

* [comment audit] Tighten Tasks recovery, pagination and mutation comments [skip ci]

* [comment audit] Tighten provider loading and terminal recovery comments [skip ci]

* [comment audit] Tighten completion, cadence and PTY ownership comments [skip ci]

* [comment audit] Clarify terminal restore and parked-pane boundaries [skip ci]

* [comment audit] Tighten PTY startup and replay explanations [skip ci]

* [comment audit] Clarify PTY identity and delivery ownership [skip ci]

* [comment audit] Shorten remote reconnect and input replay rationale [skip ci]

* [comment audit] Condense terminal parking clipboard and file link comments [skip ci]

* [comment audit] Clarify IME ownership and trim trace history [skip ci]

* [comment audit] Shorten pane recovery and parked watcher explanations [skip ci]

* [comment audit] Condense snapshot replay and visibility recovery rationale [skip ci]

* [comment audit] Shorten terminal parking and keyboard ownership rationale [skip ci]

* [comment audit] Trim renderer lifecycle and menu rationale [skip ci]

* [comment audit] Condense workspace cleanup ownership and settlement notes [skip ci]

* [comment audit] Clarify renderer hook evidence and source ownership [skip ci]

* [comment audit] Tighten IPC ownership and reconnect explanations [skip ci]

* [comment audit] Trim renderer focus, paste and attention comments [skip ci]

* [comment audit] Shorten scroll, locale and agent lifecycle comments [skip ci]

* [comment audit] Trim agent launch, resume and ownership commentary [skip ci]

* [comment audit] Shorten account, browser and editor comments [skip ci]

* [comment audit] Clarify host-mirror waits, previews and input ownership [skip ci]

* [comment audit] Condense launch recovery and editor tokenization comments [skip ci]

* [comment audit] Condense workspace selection and terminal pane rationale [skip ci]

* [comment audit] Condense terminal fitting and rendering recovery comments [skip ci]

* [comment audit] Condense terminal input and host routing explanations [skip ci]

* [comment audit] Condense ownership and sleeping-session recovery rationale [skip ci]

* [comment audit] Condense structured launch and terminal link explanations [skip ci]

* [comment audit] Condense terminal ownership and workspace file routing rationale [skip ci]

* [comment audit] Condense session persistence and workspace activation rationale [skip ci]

* [comment audit] Condense workspace routing and browser ownership rationale [skip ci]

* [comment audit] Condense mirror recovery and layout authority rationale [skip ci]

* [comment audit] Condense runtime transport and file safety rationale [skip ci]

* [comment audit] Condense runtime graph and structured session rationale [skip ci]

* [comment audit] Condense browser creation and remote tab recovery rationale [skip ci]

* [comment audit] Condense snapshot authority and publisher recovery rationale [skip ci]

* [comment audit] Condense frame, startup and provider cache rationale [skip ci]

* [comment audit] Condense host twin and agent lifecycle rationale [skip ci]

* [comment audit] Condense browser authority and editor cleanup rationale [skip ci]

* [comment audit] Condense editor buffer identity and review cache rationale [skip ci]

* [comment audit] Condense provider lifetime and repository ownership rationale [skip ci]

* [comment audit] Condense reconnect, hydration and terminal retirement rationale [skip ci]

* [comment audit] Condense recovery, reconciliation and persistence rationale [skip ci]

* [comment audit] Condense cleanup ownership and listing admission rationale [skip ci]

* [comment audit] Condense teardown ordering and terminal ownership rationale [skip ci]

* [comment audit] Condense recovery ledger and web host-boundary rationale [skip ci]

* [comment audit] Condense web reconnect and credential lifecycle rationale [skip ci]

* [comment audit] Condense cloud rollout and assignment safety rationale [skip ci]

* [comment audit] Condense relay session and database recovery rationale [skip ci]

* [comment audit] Condense relay placement and protocol compatibility rationale [skip ci]

* [comment audit] Condense build and packaging constraints [skip ci]

* [comment audit] Condense CI gates and validation isolation rationale [skip ci]

* [comment audit] Condense catalog identity and browser validation rationale [skip ci]

* [comment audit] Condense mobile browser and frame-budget rationale [skip ci]

* [comment audit] Shorten mobile route and browser test explanations [skip ci]

* [comment audit] Condense editor and session seam test rationale [skip ci]

* [comment audit] Trim session and terminal closure history [skip ci]

* [comment audit] Shorten terminal browser and census explanations [skip ci]

* [comment audit] Preserve root-only E2E diagnostic baseline

* [comment audit] Condense mobile seam and HTML preview test rationale [skip ci]

* [comment audit] Condense preview instrumentation and render harness rationale [skip ci]

* [comment audit] Condense bundle contracts and native-build constraints [skip ci]

* [comment audit] Shorten native packaging and CI routing rationale [skip ci]

* [comment audit] Condense release tooling and benchmark constraints [skip ci]

* [comment audit] Condense packaging and platform gate rationale [skip ci]

* [comment audit] Shorten mobile route and history rationale [skip ci]

* [comment audit] Condense mobile browser and component rationale [skip ci]

* [comment audit] Shorten drawer and workspace-form rationale [skip ci]

* [comment audit] Trim mobile review and editor-document explanations [skip ci]

* [comment audit] Condense editor lifecycle and Markdown explanations [skip ci]

* [comment audit] Trim mobile file, diagnostics and dictation commentary [skip ci]

* [comment audit] Simplify mobile host-list and workspace-view explanations [skip ci]

* [comment audit] Clarify mobile shell lifetime and bridge delivery constraints [skip ci]

* [comment audit] Condense bridge stream, native request and audio rationale [skip ci]

* [comment audit] Reduce bridge schema and page-client commentary [skip ci]

* [comment audit] Tighten mobile page bridge contracts [skip ci]

* [comment audit] Clarify mobile cache and session constraints [skip ci]

* [comment audit] Condense mobile route and lifecycle rationale [skip ci]

* [comment audit] Tighten mobile shell hook timing rationale [skip ci]

* [comment audit] Clarify route handoff and notification compatibility [skip ci]

* [comment audit] Clarify push registration and dictation cleanup constraints [skip ci]

* [comment audit] Condense keyboard geometry and native media ownership rationale [skip ci]

* [comment audit] Tighten mobile chat scroll and composer concurrency rationale [skip ci]

* [comment audit] Condense mobile reply compatibility and upload ownership rationale [skip ci]

* [comment audit] Condense optimistic echo and scroll ownership rationale [skip ci]

* [comment audit] Tighten stream replay and queued session ownership rationale [skip ci]

* [comment audit] Condense mobile composer and reply-schema constraints [skip ci]

* [comment audit] Clarify mobile transcript recovery and session ownership [skip ci]

* [comment audit] Clarify mobile queue and source-control compatibility [skip ci]

* [comment audit] Condense storage and task launch constraints [skip ci]

* [comment audit] Clarify task schemas and terminal document ownership [skip ci]

* [comment audit] Tighten terminal input and viewport rationale [skip ci]

* [comment audit] Shorten terminal and RPC recorder contracts [skip ci]

* [comment audit] Condense recorder engine and connection contracts [skip ci]

* [comment audit] Shorten host credential and relay recovery comments [skip ci]

* [comment audit] Tighten pairing recovery and bundle transport rationale [skip ci]

* [comment audit] Clarify RPC delivery and workspace catalog boundaries [skip ci]

* [comment audit] Condense catalog and relay entry contracts [skip ci]

* [comment audit] Shorten shutdown and SSH relay delivery contracts [skip ci]

* [comment audit] Condense WSL relay and E2E regression rationale [skip ci]

* [comment audit] Shorten cross-version harness and renderer fixture explanations [skip ci]

* [comment audit] Condense paired-host and E2E lifecycle rationale [skip ci]

* [comment audit] Shorten layout oracle and regression explanations [skip ci]

* [comment audit] Condense paired recovery and ownership regression contracts [skip ci]

* [comment audit] Shorten restart, sidebar and SSH lifecycle contracts [skip ci]

* [comment audit] Condense terminal rendering and IME regression rationale [skip ci]

* [comment audit] Shorten terminal recovery and benchmark contracts [skip ci]

* [comment audit] Condense tooling and Windows lifecycle proof rationale [skip ci]

* [comment audit] Condense workflow routing and release rationale [skip ci]

* [comment audit] Condense mobile security and installer contracts [skip ci]

* [comment audit] Shorten native rationale and stylesheet workarounds [skip ci]

* [comment audit] Shorten Windows visibility probe rationale [skip ci]

* [comment audit] Condense embedded release and signing rationale [skip ci]

* [comment audit] Retain current main test behavior in final reconciliation [skip ci]

* [comment audit] Complete final comment review across current source inventory

* [comment audit] Handle consolidated audit reports before final CI

* [comment audit] Preserve credential storage census detail [skip ci]

* [comment audit] Scale broad changed E2E selections with verified timing shards

* [comment audit] Align E2E assertions with browser close and settled session metadata

* [comment audit] Match existing E2Es to current resource and windowing contracts

* [comment audit] Observe the shared server outage notice in reconnect coverage

* [comment audit] Make existing renderer probes measure settled tab and PTY work

* [comment audit] Align palette E2E selector with task search

* [comment audit] Refresh remaining palette search selectors

* [comment audit] Keep selector correction within selected audit specs

* [comment audit] Await emoji fixture settings inside renderer evaluation

* [comment audit] Cancel fixture fit retries between terminal tests

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
2026-10-25 04:05:46 -07:00

862 lines
31 KiB
TypeScript
Raw 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.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { pathToFileURL } from 'node:url'
const { getPathMock } = vi.hoisted(() => ({
getPathMock: vi.fn<(name: string) => string>()
}))
vi.mock('electron', () => ({
app: {
getPath: getPathMock
}
}))
import { _internals } from './hook-service'
import { fakeTui, type BusEvent } from './opencode-tui-session-fixture'
type Post = {
paneKey?: string
opencodeMajor?: number
payload?: {
hook_event_name?: string
sessionID?: string
root_state?: string
root_turn_error_name?: string
}
}
type PluginModule = {
default?: { setup?: (ctx: unknown) => Promise<(() => Promise<void>) | undefined> }
}
const { setTimeout: realSetTimeout, clearTimeout: realClearTimeout } = globalThis
const PANE_A = 'tabA:aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa'
const PANE_B = 'tabB:bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb'
const SES_A = 'ses_f161fd85fffeieT0zYt80ZKorS'
const SES_B = 'ses_f161fcd7affeffV0OOrMCrTxtQ'
const ENV_KEYS = [
'ORCA_PANE_KEY',
'ORCA_OPENCODE_AGENT',
'ORCA_AGENT_HOOK_ENDPOINT',
'ORCA_AGENT_HOOK_PORT',
'ORCA_AGENT_HOOK_TOKEN'
] as const
// OpenCode 2.0.14 event shapes, shared by server and TUI plugins.
function turn(sessionID: string, text: string): { start: BusEvent[]; finish: BusEvent[] } {
const assistantMessageID = `msg_${sessionID}_a`
return {
start: [
{
type: 'session.created',
data: { sessionID, projectID: 'global', location: { directory: '/proj' }, subpath: '' }
},
{
type: 'session.inbox.enqueued',
data: {
sessionID,
inboxID: `msg_${sessionID}_u`,
item: { type: 'user', payload: { text, files: [] }, delivery: 'steer' }
}
},
{ type: 'session.execution.started', data: { sessionID } },
{ type: 'session.step.started', data: { sessionID, assistantMessageID, agent: 'build' } },
{ type: 'session.text.started', data: { sessionID, assistantMessageID, ordinal: 0 } }
],
finish: [
{
type: 'session.text.ended',
data: { sessionID, assistantMessageID, ordinal: 0, text: 'tick0 tick1 ' }
},
{ type: 'session.step.ended', data: { sessionID, assistantMessageID, finish: 'stop' } },
{ type: 'session.execution.succeeded', data: { sessionID } }
]
}
}
describe('OpenCode 2 TUI reporter: each pane reports its own sessions', () => {
let tempDir: string
let savedFetch: typeof globalThis.fetch
let savedEnv: Record<string, string | undefined>
let savedArgv: string[]
let posts: Post[]
/** Includes the sessionless start boundary from each fresh TUI. */
let allPosts: Post[]
let failPosts: boolean
let postDelayMs: number
const cleanups = new Set<() => Promise<void>>()
const delayedPosts = new Set<() => void>()
beforeEach(() => {
tempDir = mkdtempSync(join(tmpdir(), 'orca-opencode-tui-adapter-'))
savedFetch = globalThis.fetch
savedArgv = process.argv
savedEnv = {}
for (const key of ENV_KEYS) {
savedEnv[key] = process.env[key]
}
process.env.ORCA_OPENCODE_AGENT = 'opencode'
delete process.env.ORCA_AGENT_HOOK_ENDPOINT
process.env.ORCA_AGENT_HOOK_PORT = '59999'
process.env.ORCA_AGENT_HOOK_TOKEN = 'test-token'
posts = []
allPosts = []
failPosts = false
postDelayMs = 0
globalThis.fetch = vi.fn(async (_input, init) => {
const body = JSON.parse(String(init?.body))
if (postDelayMs > 0) {
await new Promise<void>((resolve) => {
const finish = (): void => {
clearTimeout(timer)
delayedPosts.delete(finish)
resolve()
}
const timer = setTimeout(finish, postDelayMs)
delayedPosts.add(finish)
})
}
if (failPosts) {
return new Response('{}', { status: 500 })
}
allPosts.push(body)
// Per-session assertions exclude the sessionless fresh-TUI boundary.
if (
body.payload?.hook_event_name !== 'SessionStart' ||
body.payload.sessionID !== undefined
) {
posts.push(body)
}
return new Response('{}', { status: 200 })
})
})
afterEach(async () => {
let deadline: ReturnType<typeof setTimeout> | undefined
try {
postDelayMs = 0
for (const finish of delayedPosts) {
finish()
}
const closing = Promise.allSettled([...cleanups].map((cleanup) => cleanup()))
expect(
await Promise.race([
closing.then((results) => results.every((result) => result.status === 'fulfilled')),
new Promise<boolean>((resolve) => {
deadline = realSetTimeout(() => resolve(false), 2000)
})
])
).toBe(true)
} finally {
realClearTimeout(deadline)
try {
if (vi.isFakeTimers()) {
expect(vi.getTimerCount()).toBe(0)
}
} finally {
if (vi.isFakeTimers()) {
vi.clearAllTimers()
}
vi.useRealTimers()
cleanups.clear()
delayedPosts.clear()
globalThis.fetch = savedFetch
process.argv = savedArgv
for (const key of ENV_KEYS) {
if (savedEnv[key] === undefined) {
delete process.env[key]
} else {
process.env[key] = savedEnv[key]
}
}
rmSync(tempDir, { recursive: true, force: true })
}
}
})
async function loadPlugin(dir = tempDir): Promise<PluginModule> {
// Bypass ESM caching to simulate a separate process.
const pluginPath = join(dir, `orca-opencode-status-${Math.random().toString(36).slice(2)}.mjs`)
writeFileSync(pluginPath, _internals.getOpenCodePluginSource())
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: the generated module's default export is exercised below and fails the test if absent.
const plugin = (await import(pathToFileURL(pluginPath).href)) as PluginModule
if (!vi.isFakeTimers()) {
vi.useFakeTimers({
toFake: ['Date', 'setTimeout', 'clearTimeout', 'setInterval', 'clearInterval']
})
}
const setup = plugin.default?.setup
return {
default: {
setup: async (ctx) => {
const cleanup = await setup?.(ctx)
if (!cleanup) {
return
}
const close = async (): Promise<void> => {
try {
await cleanup()
if (vi.isFakeTimers()) {
expect(vi.getTimerCount()).toBe(0)
}
} finally {
cleanups.delete(close)
}
}
cleanups.add(close)
return close
}
}
}
}
const summary = (list: Post[]): string[] =>
list.map((post) => `${post.payload?.hook_event_name}:${post.payload?.sessionID}`)
async function runPane(
paneKey: string,
ownSession: string,
script: (tui: ReturnType<typeof fakeTui>) => Promise<void>
): Promise<Post[]> {
process.env.ORCA_PANE_KEY = paneKey
const start = posts.length
const tui = fakeTui()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
await script(tui)
await waitFor(() => {
expect(summary(posts.slice(start)).at(-1)).toBe(`SessionIdle:${ownSession}`)
})
await cleanup?.()
return posts.slice(start)
}
const tick = async (ms = 20): Promise<void> => {
await (vi.isFakeTimers()
? vi.advanceTimersByTimeAsync(ms)
: new Promise<void>((resolve) => setTimeout(resolve, ms)))
}
const waitFor = async (
check: () => void,
{ timeout = 1000 }: { timeout?: number } = {}
): Promise<void> => {
if (!vi.isFakeTimers()) {
await vi.waitFor(check, { timeout })
return
}
const deadline = Date.now() + timeout
await tick(0)
for (;;) {
try {
check()
return
} catch (error) {
if (Date.now() >= deadline) {
throw error
}
await tick(Math.min(50, deadline - Date.now()))
}
}
}
const pump = async (tui: ReturnType<typeof fakeTui>, events: BusEvent[]): Promise<void> => {
for (const event of events) {
tui.emit(event)
await tick()
}
}
it.each([
['session.execution.failed', { error: { type: 'api' } }, 'api'],
['session.execution.failed', {}, 'UnknownError'],
['session.execution.interrupted', { reason: 'user' }, 'MessageAbortedError'],
['session.execution.interrupted', { reason: 'pause' }, undefined],
['session.execution.succeeded', {}, undefined]
])('carries the root verdict for %s', async (type, fields, errorName) => {
const reported = await runPane(PANE_A, SES_A, async (tui) => {
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'root').start)
await pump(tui, [{ type, data: { sessionID: SES_A, ...fields } }])
})
expect(reported.at(-1)?.payload).toMatchObject({ root_state: 'done' })
expect(reported.at(-1)?.payload?.root_turn_error_name).toBe(errorName)
})
it('clears a failed root verdict on its next turn', async () => {
const reported = await runPane(PANE_A, SES_A, async (tui) => {
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'first').start)
await pump(tui, [{ type: 'session.execution.failed', data: { sessionID: SES_A } }])
await pump(tui, [{ type: 'session.execution.started', data: { sessionID: SES_A } }])
await pump(tui, turn(SES_A, 'next').finish)
})
expect(reported.at(-1)?.payload?.root_turn_error_name).toBeUndefined()
})
it('gives each overlapping pane only its own session and its own Idle', async () => {
const a = turn(SES_A, 'A long SLEEP=12')
const b = turn(SES_B, 'B short SLEEP=3')
const bus = [...a.start, ...b.start, ...b.finish, ...a.finish]
const paneA = await runPane(PANE_A, SES_A, async (tui) => {
tui.navigate(SES_A)
await pump(tui, bus)
})
const paneB = await runPane(PANE_B, SES_B, async (tui) => {
await pump(tui, [...a.start])
tui.navigate(SES_B)
await pump(tui, [...b.start, ...b.finish, ...a.finish])
})
expect(paneA.every((post) => post.paneKey === PANE_A)).toBe(true)
expect(paneB.every((post) => post.paneKey === PANE_B)).toBe(true)
// Prevent the host’s OpenCode 1 session binder from consuming these posts.
expect([...paneA, ...paneB].every((post) => post.opencodeMajor === 2)).toBe(true)
expect(new Set(paneA.map((post) => post.payload?.sessionID))).toEqual(new Set([SES_A]))
expect(new Set(paneB.map((post) => post.payload?.sessionID))).toEqual(new Set([SES_B]))
for (const [list, session] of [
[paneA, SES_A],
[paneB, SES_B]
] as const) {
const names = summary(list)
expect(names[0]).toBe(`SessionStart:${session}`)
expect(names).toContain(`SessionBusy:${session}`)
expect(names.at(-1)).toBe(`SessionIdle:${session}`)
}
})
it('hydrates a session whose route switch lands after execution started', async () => {
const b = turn(SES_B, 'B tui SLEEP=4')
const paneB = await runPane(PANE_B, SES_B, async (tui) => {
await pump(tui, b.start)
expect(posts).toHaveLength(0)
tui.navigate(SES_B)
await waitFor(() => {
expect(summary(posts)).toContain(`SessionBusy:${SES_B}`)
})
await pump(tui, b.finish)
})
const names = summary(paneB)
expect(names.slice(0, 3)).toEqual([
`SessionStart:${SES_B}`,
`MessagePart:${SES_B}`,
`SessionBusy:${SES_B}`
])
expect(names.at(-1)).toBe(`SessionIdle:${SES_B}`)
})
it('keeps a started turn until it settles after the route moves on', async () => {
const a = turn(SES_A, 'A long')
const paneA = await runPane(PANE_A, SES_A, async (tui) => {
tui.navigate(SES_A)
await pump(tui, a.start)
tui.navigate('ses_other_idle_session')
await tick(250)
await pump(tui, a.finish)
})
expect(summary(paneA)).toContain(`SessionBusy:${SES_A}`)
expect(summary(paneA).at(-1)).toBe(`SessionIdle:${SES_A}`)
})
it('reports nothing for sessions the pane never showed', async () => {
process.env.ORCA_PANE_KEY = PANE_A
const tui = fakeTui()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
const b = turn(SES_B, 'another pane')
await pump(tui, [...b.start, ...b.finish])
await tick(250)
await cleanup?.()
expect(posts).toEqual([])
})
it('settles an owned turn once the session data says it ended without an end event', async () => {
process.env.ORCA_PANE_KEY = PANE_A
const tui = fakeTui()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'A').start)
tui.loseEnd(SES_A)
await tick(99)
expect(summary(posts).at(-1)).toBe(`SessionBusy:${SES_A}`)
await tick(1)
expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`)
await cleanup?.()
})
it('does not settle a turn early while its events are still queued', async () => {
process.env.ORCA_PANE_KEY = PANE_A
const tui = fakeTui()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
tui.navigate(SES_A)
await tick()
const a = turn(SES_A, 'A fails fast')
for (const event of [...a.start, ...a.finish]) {
tui.emit(event)
}
await waitFor(() => {
expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`)
})
await tick(250)
await cleanup?.()
const statuses = posts
.map((post) => post.payload?.hook_event_name)
.filter((name) => name === 'SessionBusy' || name === 'SessionIdle')
expect(statuses).toEqual(['SessionBusy', 'SessionIdle'])
})
it('re-derives Working for an owned session whose start it missed', async () => {
process.env.ORCA_PANE_KEY = PANE_A
const tui = fakeTui()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
tui.navigate(SES_A)
const first = turn(SES_A, 'A')
await pump(tui, [...first.start, ...first.finish])
await waitFor(() => {
expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`)
})
tui.loseStart(SES_A)
await waitFor(() => {
expect(summary(posts).at(-1)).toBe(`SessionBusy:${SES_A}`)
})
tui.loseEnd(SES_A)
await waitFor(() => {
expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`)
})
await cleanup?.()
})
describe('derived from the session data', () => {
const SES_CHILD = 'ses_f161fe00affeChildSubagent1'
const statuses = (list: Post[]): string[] =>
summary(list).filter((name) =>
/^(SessionBusy|SessionIdle|PermissionRequest|AskUserQuestion):/.test(name)
)
const start = async (
tui: ReturnType<typeof fakeTui>
): Promise<(() => Promise<void>) | undefined> => {
process.env.ORCA_PANE_KEY = PANE_A
return (await loadPlugin()).default?.setup?.(tui.ctx)
}
const childStart = (parentID: string): BusEvent[] => [
{ type: 'session.created', data: { sessionID: SES_CHILD, parentID } },
{ type: 'session.execution.started', data: { sessionID: SES_CHILD } }
]
const permission = (sessionID: string): BusEvent => ({
type: 'permission.asked',
data: { id: 'per_1', sessionID, action: 'bash', resources: ['rm -rf build'] }
})
it('publishes a root failure while its child keeps the aggregate Working', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
await pump(tui, [...turn(SES_A, 'root').start, ...childStart(SES_A)])
await pump(tui, [{ type: 'session.execution.failed', data: { sessionID: SES_A } }])
await waitFor(() => {
expect(posts.at(-1)?.payload).toMatchObject({
hook_event_name: 'SessionBusy',
root_state: 'done',
root_turn_error_name: 'UnknownError'
})
})
await pump(tui, [{ type: 'session.execution.succeeded', data: { sessionID: SES_CHILD } }])
expect(posts.at(-1)?.payload?.root_turn_error_name).toBe('UnknownError')
await cleanup?.()
})
it('does not turn a child failure into a root failure', async () => {
const reported = await runPane(PANE_A, SES_A, async (tui) => {
tui.navigate(SES_A)
await pump(tui, [...turn(SES_A, 'root').start, ...childStart(SES_A)])
await pump(tui, [{ type: 'session.execution.failed', data: { sessionID: SES_CHILD } }])
await pump(tui, turn(SES_A, 'root').finish)
})
expect(reported.at(-1)?.payload?.root_turn_error_name).toBeUndefined()
})
it('keeps a failed terminal verdict through plugin hot reload', async () => {
const tui = fakeTui()
const first = await start(tui)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'root').start)
await pump(tui, [{ type: 'session.execution.failed', data: { sessionID: SES_A } }])
await waitFor(() => expect(posts.at(-1)?.payload?.root_turn_error_name).toBe('UnknownError'))
await first?.()
const before = posts.length
const second = await start(tui)
await tick(150)
expect(posts).toHaveLength(before)
expect(posts.at(-1)?.payload?.root_turn_error_name).toBe('UnknownError')
await second?.()
})
it.each(['session.execution.succeeded', 'session.execution.interrupted'])(
'repairs a failed verdict when %s follows a missed turn start during unload',
async (finishType) => {
const tui = fakeTui()
const first = await start(tui)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'first').start)
await pump(tui, [{ type: 'session.execution.failed', data: { sessionID: SES_A } }])
await first?.()
await pump(tui, [
{ type: 'session.execution.started', data: { sessionID: SES_A } },
{ type: finishType, data: { sessionID: SES_A, reason: 'user' } }
])
const second = await start(tui)
await waitFor(() => expect(posts.at(-1)?.payload?.root_turn_error_name).toBeUndefined())
expect(posts.at(-1)?.payload).toMatchObject({
hook_event_name: 'SessionIdle',
root_state: 'done'
})
await second?.()
}
)
it('shows Done for a turn that ended while the plugin was reloading', async () => {
const tui = fakeTui()
const firstGeneration = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
await pump(tui, a.start)
await waitFor(() => expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`]))
const beforeReload = posts.length
await firstGeneration?.()
expect(posts).toHaveLength(beforeReload)
await pump(tui, a.finish)
const secondGeneration = await start(tui)
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await secondGeneration?.()
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`, `SessionIdle:${SES_A}`])
})
it('keeps a navigated-away turn across a reload and settles it after', async () => {
const tui = fakeTui()
const firstGeneration = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
await pump(tui, a.start)
tui.navigate(SES_B)
await tick(150)
await firstGeneration?.()
const secondGeneration = await start(tui)
await tick(150)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`])
await pump(tui, a.finish)
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await secondGeneration?.()
})
it('re-posts a level the previous generation could not deliver', async () => {
const tui = fakeTui()
failPosts = true
const firstGeneration = await start(tui)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'A').start)
await tick(50)
await firstGeneration?.()
failPosts = false
const secondGeneration = await start(tui)
await waitFor(() => expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`]))
await secondGeneration?.()
})
it('posts each level once and in order while posts are slow', async () => {
postDelayMs = 120
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
const b = turn(SES_B, 'another pane')
for (const event of [...a.start, ...b.start, ...b.finish, ...a.finish]) {
tui.emit(event)
}
await tick(119)
expect(allPosts).toEqual([])
await tick(1)
expect(summary(allPosts)).toEqual(['SessionStart:undefined'])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`), {
timeout: 3000
})
await tick(300)
await cleanup?.()
expect(summary(posts)).toEqual([
`SessionStart:${SES_A}`,
`MessagePart:${SES_A}`,
`SessionBusy:${SES_A}`,
`MessagePart:${SES_A}`,
`SessionIdle:${SES_A}`
])
expect(posts.at(-2)?.payload).toMatchObject({ role: 'assistant', text: 'tick0 tick1 ' })
expect(summary(allPosts)[0]).toBe('SessionStart:undefined')
})
// Fresh panes may retain status left by another process before an upgrade.
it('lands one start boundary in a freshly started TUI, and none on a reload after Done', async () => {
const tui = fakeTui()
const firstGeneration = await start(tui)
await waitFor(() => expect(summary(allPosts)).toEqual(['SessionStart:undefined']))
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
await pump(tui, [...a.start, ...a.finish])
await waitFor(() => expect(summary(allPosts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await firstGeneration?.()
const secondGeneration = await start(tui)
await tick(250)
await secondGeneration?.()
expect(summary(allPosts).at(-1)).toBe(`SessionIdle:${SES_A}`)
expect(summary(allPosts).filter((name) => name.startsWith('SessionStart:'))).toEqual([
'SessionStart:undefined',
`SessionStart:${SES_A}`
])
})
it('lands the start boundary only once across reloads of an idle pane', async () => {
const tui = fakeTui()
const firstGeneration = await start(tui)
await waitFor(() => expect(summary(allPosts)).toEqual(['SessionStart:undefined']))
await firstGeneration?.()
const secondGeneration = await start(tui)
await tick(150)
await secondGeneration?.()
expect(summary(allPosts)).toEqual(['SessionStart:undefined'])
})
it('names the idle session a freshly started TUI shows', async () => {
const tui = fakeTui()
tui.navigate(SES_B)
const cleanup = await start(tui)
await waitFor(() => expect(summary(allPosts)).toEqual([`SessionStart:${SES_B}`]))
await cleanup?.()
})
it('posts Working instead of the start boundary when the route shows a running turn', async () => {
const tui = fakeTui()
tui.navigate(SES_A)
tui.loseStart(SES_A)
const cleanup = await start(tui)
await waitFor(() => expect(summary(allPosts)).toEqual([`SessionBusy:${SES_A}`]))
await tick(150)
await cleanup?.()
expect(summary(allPosts)).toEqual([`SessionBusy:${SES_A}`])
})
it('lands no start boundary where OpenCode keeps no plugin memory', async () => {
const tui = fakeTui()
Reflect.deleteProperty(tui.ctx, 'storage')
const cleanup = await start(tui)
await tick(150)
await cleanup?.()
expect(allPosts).toEqual([])
})
it('stays Working while a child session holds the turn, even if its end is missed', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A spawns a background task')
await pump(tui, [...a.start, ...childStart(SES_A), ...a.finish])
await tick(250)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`, `SessionBusy:${SES_A}`])
expect(posts.at(-1)?.payload).toMatchObject({ root_state: 'done' })
tui.loseEnd(SES_CHILD)
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await cleanup?.()
expect(posts.every((post) => post.payload?.sessionID === SES_A)).toBe(true)
})
it('keeps automatically answered permissions Working without an attention post', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'auto').start)
for (let index = 0; index < 3; index += 1) {
tui.emit({
type: 'permission.asked',
data: { ...permission(SES_A).data, id: `auto_${index}` }
})
await tick(20)
tui.emit({
type: 'permission.replied',
data: { sessionID: SES_A, requestID: `auto_${index}` }
})
}
await tick(650)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`])
await pump(tui, turn(SES_A, 'auto').finish)
await cleanup?.()
})
it("shows a child's permission as Needs input on the root, then Working after the reply", async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
await pump(tui, [...a.start, ...childStart(SES_A)])
await tick(60)
await pump(tui, [permission(SES_CHILD)])
await tick(479)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`])
await tick(1)
expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`)
await pump(tui, [
{ type: 'permission.replied', data: { sessionID: SES_CHILD, requestID: 'per_1' } },
{ type: 'session.execution.succeeded', data: { sessionID: SES_CHILD } },
...a.finish
])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await cleanup?.()
expect(statuses(posts)).toEqual([
`SessionBusy:${SES_A}`,
`PermissionRequest:${SES_A}`,
`SessionBusy:${SES_A}`,
`SessionIdle:${SES_A}`
])
})
// MessagePart implies Working, so it must not bury an open request.
it('keeps Needs input while the root streams text beside a waiting background child', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A spawns a background task')
await pump(tui, [...a.start, ...childStart(SES_A), permission(SES_CHILD)])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`))
await pump(tui, a.finish)
await tick(300)
expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`)
await cleanup?.()
})
it('clears Needs input answered while disconnected once the TUI reconnects', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
await pump(tui, [...turn(SES_A, 'A').start, permission(SES_A)])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`))
tui.navigate(SES_B)
await pump(tui, [{ type: 'server.connected', data: {} }])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionBusy:${SES_A}`))
await cleanup?.()
})
it('ignores a reply that a reconnect fetch restores', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
await pump(tui, [...turn(SES_A, 'A').start, permission(SES_A)])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`))
const release = tui.holdPermissionFetch()
tui.serverPermissions.set(SES_A, [...(tui.permissions.get(SES_A) ?? [])])
await pump(tui, [
{ type: 'server.connected', data: {} },
{ type: 'permission.replied', data: { sessionID: SES_A, requestID: 'per_1' } }
])
release()
await tick(250)
expect(tui.permissions.get(SES_A)).toHaveLength(1)
expect(summary(posts).at(-1)).toBe(`SessionBusy:${SES_A}`)
await cleanup?.()
})
it('re-posts the current level once the hook endpoint moves', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
await pump(tui, turn(SES_A, 'A').start)
await tick(200)
await waitFor(() => expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`]))
process.env.ORCA_AGENT_HOOK_PORT = '59998'
await tick(4699)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`])
await tick(1)
expect(statuses(posts)).toEqual([`SessionBusy:${SES_A}`, `SessionBusy:${SES_A}`])
await tick(300)
await tick(5000)
await tick(50)
expect(statuses(posts)).toHaveLength(2)
await cleanup?.()
})
it('does not pin Needs input on a request the data kept after its turn ended', async () => {
const tui = fakeTui()
const cleanup = await start(tui)
tui.navigate(SES_A)
const a = turn(SES_A, 'A')
await pump(tui, [...a.start, permission(SES_A)])
await waitFor(() => expect(summary(posts).at(-1)).toBe(`PermissionRequest:${SES_A}`))
await pump(tui, a.finish)
await waitFor(() => expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`))
await tick(250)
expect(summary(posts).at(-1)).toBe(`SessionIdle:${SES_A}`)
await cleanup?.()
})
})
it.each([
['an OpenCode 1 TUI', () => fakeTui('1.18.33')],
['the other pane variant', () => fakeTui()],
['a TUI outside an Orca pane', () => fakeTui()]
])('stays silent in %s', async (label, make) => {
if (label === 'the other pane variant') {
process.env.ORCA_OPENCODE_AGENT = 'opencode2'
}
process.env.ORCA_PANE_KEY = PANE_A
if (label === 'a TUI outside an Orca pane') {
delete process.env.ORCA_PANE_KEY
}
const tui = make()
const cleanup = await (await loadPlugin()).default?.setup?.(tui.ctx)
expect(tui.listen).not.toHaveBeenCalled()
await cleanup?.()
})
describe('server plugin stand-down', () => {
function serverContext() {
const subscribe = vi.fn(async function* () {})
const hook = vi.fn(async () => ({ dispose: vi.fn() }))
return {
subscribe,
hook,
ctx: {
session: { hook, get: async () => undefined },
event: { subscribe }
}
}
}
const installTuiCopy = (): void => {
mkdirSync(join(tempDir, 'orca-opencode-status-tui'))
writeFileSync(join(tempDir, 'orca-opencode-status-tui', 'tui.js'), '')
}
it('reports nothing from a serve process when the TUI copy is installed beside it', async () => {
installTuiCopy()
process.argv = [...savedArgv, 'serve', '--service']
const server = serverContext()
const cleanup = await (await loadPlugin()).default?.setup?.(server.ctx)
expect(server.subscribe).not.toHaveBeenCalled()
expect(server.hook).not.toHaveBeenCalled()
await cleanup?.()
})
it('stands down in a --standalone private server too', async () => {
installTuiCopy()
process.argv = [...savedArgv, 'serve', '--stdio', '--port', '0']
const server = serverContext()
const cleanup = await (await loadPlugin()).default?.setup?.(server.ctx)
expect(server.subscribe).not.toHaveBeenCalled()
await cleanup?.()
})
// Older SSH relays install the server file without a TUI copy.
it('keeps reporting from a serve process whose installer wrote no TUI copy', async () => {
process.argv = [...savedArgv, 'serve', '--service']
const server = serverContext()
const cleanup = await (await loadPlugin()).default?.setup?.(server.ctx)
expect(server.subscribe).toHaveBeenCalled()
await cleanup?.()
})
})
})