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.
TypeScript adeagent-ideai-agentsautomationbotclaude-codecodexghosttygrok-bothermes-agentmobile-appmuseopenclawopencodeorchestrationpiruntimeterminalworktreesyc-backed
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { encodePairingOffer } from './pairing'
import { listEnvironments } from './runtime-environment-store'
import {
addWslHostEnvironment,
findWslHostDeployment,
reconcileWslHostBridgePorts,
refreshWslHostPairing,
removeWslHostEnvironment
} from './runtime-environment-wsl-host-store'
import { getPreferredPairingOffer } from './runtime-environments'
import { writeWslHostDeployment } from './wsl-host-deployment-store'
const publicKeyB64 = Buffer.alloc(32, 7).toString('base64')
function code(port: number, deviceToken = 'token-1'): string {
return encodePairingOffer({
v: 2,
endpoint: `ws://127.0.0.1:${port}`,
deviceToken,
publicKeyB64
})
}
const record = {
kind: 'wsl' as const,
distro: 'Ubuntu',
distroId: '0e3a7f2c-1b4d-4c5e-9f60-7a8b9c0d1e2f',
localPort: 42000,
remotePort: 6768
}
describe('WSL host environment store', () => {
let userDataPath: string
beforeEach(() => {
userDataPath = mkdtempSync(join(tmpdir(), 'orca-wsl-host-store-'))
vi.spyOn(console, 'warn').mockImplementation(() => {})
})
afterEach(() => {
vi.restoreAllMocks()
rmSync(userDataPath, { recursive: true, force: true })
})
it('registers a paired environment that reads back with its WSL deployment', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
const [environment] = listEnvironments(userDataPath)
expect(environment).toMatchObject({
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
orcadDeployment: { kind: 'wsl', distro: 'Ubuntu', localPort: 42000, remotePort: 6768 }
})
expect(environment.connectionDependency).toBeUndefined()
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')?.createdAt).toBe(100)
})
it('refuses a pairing that does not point at the bridge port', () => {
expect(() =>
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(43000),
record
})
).toThrow(/does not point at its bridge/)
expect(listEnvironments(userDataPath)).toEqual([])
})
it('persists a relaunch’s new device token and a moved bridge, and says so', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
expect(
refreshWslHostPairing(userDataPath, 'wsl:ubuntu', {
pairingCode: code(42000),
localPort: 42000,
remotePort: 6768
}).pairingChanged
).toBe(false)
const refreshed = refreshWslHostPairing(
userDataPath,
'wsl:ubuntu',
{ pairingCode: code(42001, 'token-2'), localPort: 42001, remotePort: 6770 },
500
)
expect(refreshed.pairingChanged).toBe(true)
expect(getPreferredPairingOffer(refreshed.environment).deviceToken).toBe('token-2')
expect(refreshed.environment.pairingRevision).toBe(500)
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')).toMatchObject({
localPort: 42001,
remotePort: 6770
})
})
it('binds the record to the pairing key and follows a relaunch that changes it', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')?.pairingPublicKeyB64).toBe(
publicKeyB64
)
const rotatedKey = Buffer.alloc(32, 9).toString('base64')
const refreshed = refreshWslHostPairing(userDataPath, 'wsl:ubuntu', {
pairingCode: encodePairingOffer({
v: 2,
endpoint: 'ws://127.0.0.1:42000',
deviceToken: 'token-2',
publicKeyB64: rotatedKey
}),
localPort: 42000,
remotePort: 6768
})
expect(refreshed.pairingChanged).toBe(true)
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')?.pairingPublicKeyB64).toBe(rotatedKey)
expect(listEnvironments(userDataPath)[0]?.orcadDeployment).toMatchObject({ kind: 'wsl' })
})
it('writes nothing when a refreshed pairing does not point at the bridge', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
const before = findWslHostDeployment(userDataPath, 'wsl:ubuntu')
expect(() =>
refreshWslHostPairing(userDataPath, 'wsl:ubuntu', {
pairingCode: code(43000, 'token-2'),
localPort: 42001,
remotePort: 6770
})
).toThrow(/does not point at its bridge/)
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')).toEqual(before)
expect(listEnvironments(userDataPath)[0]?.orcadDeployment).toMatchObject({ localPort: 42000 })
})
it('stays a WSL host if a key rotation is interrupted between its two writes', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
const stored = findWslHostDeployment(userDataPath, 'wsl:ubuntu')
if (!stored) {
throw new Error('record missing')
}
// The record write landed; the pairing write that follows it did not.
writeWslHostDeployment(userDataPath, 'wsl:ubuntu', {
...stored,
pairingPublicKeyB64: Buffer.alloc(32, 9).toString('base64'),
previousPairingPublicKeyB64: publicKeyB64
})
expect(listEnvironments(userDataPath)[0]?.orcadDeployment).toMatchObject({ kind: 'wsl' })
})
it('heals a crash between saving a moved bridge and its pairing, keeping the saved pairing', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
const saved = findWslHostDeployment(userDataPath, 'wsl:ubuntu')
if (!saved) {
throw new Error('record missing')
}
// refreshWslHostPairing's record write landed; its pairing write never did.
writeWslHostDeployment(userDataPath, 'wsl:ubuntu', { ...saved, localPort: 42001 })
const [crashed] = listEnvironments(userDataPath)
expect(crashed?.orcadDeployment).toBeUndefined()
expect(reconcileWslHostBridgePorts(userDataPath)).toEqual(['wsl:ubuntu'])
const [healed] = listEnvironments(userDataPath)
expect(healed?.orcadDeployment).toMatchObject({ kind: 'wsl', localPort: 42000 })
expect(healed && getPreferredPairingOffer(healed)).toEqual(
crashed && getPreferredPairingOffer(crashed)
)
expect(reconcileWslHostBridgePorts(userDataPath)).toEqual([])
// The next connect saves both.
refreshWslHostPairing(userDataPath, 'wsl:ubuntu', {
pairingCode: code(42002, 'token-2'),
localPort: 42002,
remotePort: 6768
})
expect(listEnvironments(userDataPath)[0]?.orcadDeployment).toMatchObject({ localPort: 42002 })
})
it('leaves a record written for a different pairing, or with no server, alone', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record,
now: 100
})
const saved = findWslHostDeployment(userDataPath, 'wsl:ubuntu')
if (!saved) {
throw new Error('record missing')
}
const otherKey = Buffer.alloc(32, 3).toString('base64')
writeWslHostDeployment(userDataPath, 'wsl:ubuntu', {
...saved,
localPort: 42001,
pairingPublicKeyB64: otherKey,
previousPairingPublicKeyB64: otherKey
})
writeWslHostDeployment(userDataPath, 'wsl:orphan', { ...saved, environmentId: 'wsl:orphan' })
expect(reconcileWslHostBridgePorts(userDataPath)).toEqual([])
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')?.localPort).toBe(42001)
})
it('removes the environment and its record', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000),
record
})
removeWslHostEnvironment(userDataPath, 'wsl:ubuntu')
expect(listEnvironments(userDataPath)).toEqual([])
expect(findWslHostDeployment(userDataPath, 'wsl:ubuntu')).toBeNull()
})
// A golden image with a baked-in orcad key: each distro is its own host, with its own grant.
it('keeps two distros that carry the same orcad key as two hosts', () => {
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu',
name: 'Ubuntu (WSL)',
pairingCode: code(42000, 'token-ubuntu'),
record
})
addWslHostEnvironment(userDataPath, {
id: 'wsl:ubuntu-2',
name: 'Ubuntu-2 (WSL)',
pairingCode: code(42001, 'token-ubuntu-2'),
record: { ...record, distro: 'Ubuntu-2', localPort: 42001 }
})
const environments = listEnvironments(userDataPath)
expect(environments.map((environment) => environment.id)).toEqual([
'wsl:ubuntu',
'wsl:ubuntu-2'
])
expect(environments.map((environment) => getPreferredPairingOffer(environment))).toEqual([
expect.objectContaining({ publicKeyB64, deviceToken: 'token-ubuntu' }),
expect.objectContaining({ publicKeyB64, deviceToken: 'token-ubuntu-2' })
])
})
})