Repository navigation
Retire client transports after CLI removes a runtime host #25974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nwparker
wants to merge
6
commits into
main
Choose a base branch
from
nwparker/fix-removed-host-transports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+882
−25
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
75a7aaf
fix: retire removed runtime host transports
nwparker 55055ad
Make CLI removal regression independent of prebuilt artifacts
nwparker 65a5338
test: route new orchestration SQLite fixtures through Node
nwparker b5f8eb3
Use upstream SQLite runtime registrations without duplicates
nwparker d82ac49
Run the upstream agent launch database fixture in Node
nwparker 1371f3e
Keep the CLI removal fixture compatible with managed SSH registration
nwparker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
229 changes: 229 additions & 0 deletions
229
src/main/ipc/runtime-environment-cli-removal.electron.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import { | ||
| spawnProcess, | ||
| runProcess, | ||
| type ChildProcessHandle | ||
| } from '../../shared/child-process/run-process' | ||
| import { | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| rmSync, | ||
| symlinkSync, | ||
| writeFileSync | ||
| } from 'node:fs' | ||
| import { builtinModules, createRequire } from 'node:module' | ||
| import { tmpdir } from 'node:os' | ||
| import { isAbsolute, join } from 'node:path' | ||
| import { afterAll, describe, expect, it } from 'vitest' | ||
| import { build as buildVite } from 'vite' | ||
| import { resolveElectronProbeLaunch } from '../browser/electron-probe-display-launch' | ||
| import { REMOTE_RUNTIME_SOCKET_PING_INTERVAL_MS } from '../../shared/remote-runtime-socket-liveness' | ||
| import { REMOTE_RUNTIME_SHARED_CONTROL_CAPABILITY } from '../../shared/protocol-version' | ||
| import { encodePairingOffer } from '../../shared/pairing' | ||
| import { addEnvironmentFromPairingCode } from '../../shared/runtime-environment-store' | ||
| import { | ||
| closeSharedControlTestServers, | ||
| createSharedControlTestServer | ||
| } from '../../shared/remote-runtime-shared-control-test-server' | ||
|
|
||
| // The real CLI and a hidden Electron process exercise registration and socket retirement together. | ||
|
|
||
| const electronBinary = resolveElectronBinary() | ||
| const roots: string[] = [] | ||
| let child: ChildProcessHandle | null = null | ||
|
|
||
| afterAll(async () => { | ||
| child?.kill('SIGKILL') | ||
| await closeSharedControlTestServers() | ||
| if (process.env.ORCA_KEEP_FIXTURE === '1') { | ||
| return | ||
| } | ||
| for (const root of roots) { | ||
| rmSync(root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 }) | ||
| } | ||
| }) | ||
|
|
||
| function resolveElectronBinary(): string { | ||
| const resolved: unknown = createRequire(import.meta.url)('electron') | ||
| if (typeof resolved !== 'string') { | ||
| throw new Error('The electron package did not resolve to a binary path') | ||
| } | ||
| return resolved | ||
| } | ||
|
|
||
| function buildFixtureMain(bundlePath: string, logPath: string, userDataPath: string): string { | ||
| return [ | ||
| `const { app } = require('electron')`, | ||
| `app.setPath('userData', ${JSON.stringify(userDataPath)})`, | ||
| `const { appendFileSync } = require('node:fs')`, | ||
| `const log = (message) => appendFileSync(${JSON.stringify(logPath)}, message + '\\n')`, | ||
| `process.on('uncaughtException', (error) => log('uncaught ' + (error && error.stack)))`, | ||
| `process.on('unhandledRejection', (error) => log('unhandled ' + (error && error.stack)))`, | ||
| `const runtime = require(${JSON.stringify(bundlePath)})`, | ||
| `runtime.applyBackgroundActivationPolicy()`, | ||
| `app.whenReady().then(() => {`, | ||
| ` log('ready ' + app.getPath('userData'))`, | ||
| ` runtime.registerRuntimeEnvironmentHandlers({ getSettings: () => ({}), getWorkspaceSessionHostIds: () => [] })`, | ||
| ` log('registered')`, | ||
| `}).catch((error) => log('ready-failed ' + (error && error.stack)))` | ||
| ].join('\n') | ||
| } | ||
|
|
||
| async function waitFor( | ||
| predicate: () => boolean, | ||
| timeoutMs: number, | ||
| what: () => string | ||
| ): Promise<void> { | ||
| const startedAt = Date.now() | ||
| while (!predicate()) { | ||
| if (Date.now() - startedAt > timeoutMs) { | ||
| throw new Error(`Timed out waiting for ${what()}`) | ||
| } | ||
| await new Promise((resolve) => setTimeout(resolve, 100)) | ||
| } | ||
| } | ||
|
|
||
| function readFixtureLog(root: string): string { | ||
| const path = join(root, 'fixture.log') | ||
| return existsSync(path) ? readFileSync(path, 'utf8') : '(no fixture log)' | ||
| } | ||
|
|
||
| const delay = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
|
||
| describe('runtime environment removed by the CLI', () => { | ||
| it('drops the live socket the desktop app was holding and never reconnects', async () => { | ||
| const root = mkdtempSync(join(tmpdir(), 'orca-cli-removal-')) | ||
| roots.push(root) | ||
| const userDataPath = join(root, 'profile') | ||
| mkdirSync(userDataPath, { recursive: true }) | ||
| const fixtureDir = join(root, 'fixture') | ||
| mkdirSync(fixtureDir, { recursive: true }) | ||
| symlinkSync( | ||
| join(process.cwd(), 'node_modules'), | ||
| join(fixtureDir, 'node_modules'), | ||
| process.platform === 'win32' ? 'junction' : 'dir' | ||
| ) | ||
|
|
||
| // The app only keeps a standing shared-control socket for a host that advertises the capability. | ||
| const server = await createSharedControlTestServer({ | ||
| resultForRequest: (method) => | ||
| method === 'status.get' | ||
| ? { capabilities: [REMOTE_RUNTIME_SHARED_CONTROL_CAPABILITY], runtimeId: 'runtime-test' } | ||
| : { method } | ||
| }) | ||
| const environment = addEnvironmentFromPairingCode(userDataPath, { | ||
| name: 'removal-host', | ||
| pairingCode: encodePairingOffer(server.pairing) | ||
| }) | ||
|
|
||
| const entryPath = join(fixtureDir, 'entry.ts') | ||
| writeFileSync( | ||
| entryPath, | ||
| `export { registerRuntimeEnvironmentHandlers } from ${JSON.stringify(join(process.cwd(), 'src/main/ipc/runtime-environments.ts'))}\nexport { applyBackgroundActivationPolicy } from ${JSON.stringify(join(process.cwd(), 'src/main/window/foreground-activation-policy.ts'))}` | ||
| ) | ||
| await buildVite({ | ||
| configFile: false, | ||
| logLevel: 'silent', | ||
| // Why `node` conditions and the bare-builtin externals: the default browser resolution stubs | ||
| // `crypto` and the bundle throws on load instead of running the shipped code. | ||
| resolve: { conditions: ['node'] }, | ||
| build: { | ||
| emptyOutDir: false, | ||
| lib: { entry: entryPath, formats: ['cjs'], fileName: () => 'runtime-environments.cjs' }, | ||
| outDir: fixtureDir, | ||
| target: 'node20', | ||
| rollupOptions: { external: ['electron', 'ssh2', /^node:/, ...builtinModules] } | ||
| } | ||
| }) | ||
|
|
||
| await buildVite({ | ||
| configFile: false, | ||
| logLevel: 'silent', | ||
| resolve: { conditions: ['node'] }, | ||
| build: { | ||
| emptyOutDir: false, | ||
| lib: { | ||
| entry: join(process.cwd(), 'src/cli/index.ts'), | ||
| formats: ['cjs'], | ||
| fileName: () => 'orca-cli.cjs' | ||
| }, | ||
| outDir: fixtureDir, | ||
| target: 'node20', | ||
| rollupOptions: { external: (id) => !isAbsolute(id) && !id.startsWith('.') } | ||
| } | ||
| }) | ||
|
|
||
| writeFileSync( | ||
| join(fixtureDir, 'package.json'), | ||
| '{ "name": "orca-cli-removal-fixture", "main": "main.js" }' | ||
| ) | ||
| writeFileSync( | ||
| join(fixtureDir, 'main.js'), | ||
| buildFixtureMain( | ||
| join(fixtureDir, 'runtime-environments.cjs'), | ||
| join(root, 'fixture.log'), | ||
| userDataPath | ||
| ) | ||
| ) | ||
|
|
||
| const { ELECTRON_RUN_AS_NODE: _runAsNode, ...env } = process.env | ||
| const electronArgs = [fixtureDir, `--user-data-dir=${userDataPath}`] | ||
| const { executable, args } = resolveElectronProbeLaunch({ | ||
| electronBinary, | ||
| electronArgs, | ||
| platform: process.platform, | ||
| display: env.DISPLAY | ||
| }) | ||
| child = spawnProcess({ | ||
| program: executable, | ||
| args, | ||
| env: { ...env, ORCA_BACKGROUND_LAUNCH: '1' }, | ||
| stdio: ['ignore', 'pipe', 'pipe'] | ||
| }) | ||
| let stderr = '' | ||
| child.stderr?.on('data', (chunk: Buffer) => { | ||
| stderr += chunk.toString() | ||
| }) | ||
|
|
||
| // The app connects on its own at startup, exactly as it does for a stored environment. | ||
| await waitFor( | ||
| () => | ||
| server.connectionCount() >= 2 && server.openClientCount() === 1 && server.auths.length >= 2, | ||
| 60_000, | ||
| () => | ||
| `the app to connect (connections=${server.connectionCount()} open=${server.openClientCount()} methods=${server.requests.map((request) => request.method).join(',')} stderr=${stderr} fixture=${readFixtureLog(root)})` | ||
| ) | ||
| const connectionsWhileStored = server.connectionCount() | ||
|
|
||
| // What `orca environment rm` does, from a process the app knows nothing about. | ||
| const cli = await runProcess({ | ||
| program: process.execPath, | ||
| args: [ | ||
| join(fixtureDir, 'orca-cli.cjs'), | ||
| 'environment', | ||
| 'rm', | ||
| '--environment', | ||
| environment.id, | ||
| '--json' | ||
| ], | ||
| env: { ...process.env, ORCA_USER_DATA_PATH: userDataPath }, | ||
| timeoutMs: 30_000 | ||
| }) | ||
| expect(cli.code, cli.stderr || cli.stdout).toBe(0) | ||
|
|
||
| await waitFor( | ||
| () => server.openClientCount() === 0, | ||
| REMOTE_RUNTIME_SOCKET_PING_INTERVAL_MS * 3, | ||
| () => `the removed environment to lose its socket (open=${server.openClientCount()})` | ||
| ) | ||
| await delay(REMOTE_RUNTIME_SOCKET_PING_INTERVAL_MS + 2_000) | ||
| expect(server.openClientCount()).toBe(0) | ||
| expect(server.connectionCount()).toBe(connectionsWhileStored) | ||
| console.info('CLI removal socket proof', { | ||
| before: connectionsWhileStored, | ||
| openAfter: server.openClientCount(), | ||
| totalAfter: server.connectionCount() | ||
| }) | ||
| }, 180_000) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: stablyai/orca
Length of output: 1899
🏁 Script executed:
Repository: stablyai/orca
Length of output: 45655
🏁 Script executed:
Repository: stablyai/orca
Length of output: 20306
🏁 Script executed:
Repository: stablyai/orca
Length of output: 20606
🏁 Script executed:
Repository: stablyai/orca
Length of output: 5881
Terminate the process tree before removing the fixture.
child.kill('SIGKILL')targets only the spawned process and does not wait for termination. When Linux usesxvfb-runbecauseDISPLAYis unset, Electron is a child of the process this test kills. On Windows, Chromium descendants can retain profile handles whilermSyncruns. Use the process-tree helper, and spawn detached on POSIX so the helper can terminate the whole group. Skip fixture removal if termination is not verified.Suggested fix