mirror of
https://github.com/stablyai/orca.git
synced 2026-11-18 11:54:46 +00:00
fix(opencode): resume pre-account sessions in the System store (#43243)
A Session History row from the user's own OpenCode store sent no account request, so the host fell back to the selected managed account, where OpenCode 2's --session silently creates an empty session with the same id. The resume now names the System store, and daemon spawns keep that choice for the private-server check.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { randomUUID, createHash } from 'node:crypto'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { join } from 'node:path'
|
||||
import { mkdtempSync, rmSync } from 'node:fs'
|
||||
import { tmpdir } from 'node:os'
|
||||
import {
|
||||
createDaemonPtyEnvironment,
|
||||
@@ -23,6 +24,10 @@ import {
|
||||
MANAGED_DATA_ACCOUNT_BASELINE_ENV_KEYS,
|
||||
restoreManagedDataAccountEnvironment
|
||||
} from '../../../shared/agent-launch/environment/managed-data-account-environment'
|
||||
import { managedDataAccountResumeEnv } from '../../../shared/agent-launch/environment/managed-data-account-resume'
|
||||
import { getManagedDataAccountService } from '../../managed-data-accounts/service'
|
||||
import { needsOpenCodePrivateServer } from '../../opencode/opencode-private-server'
|
||||
import { writeSecureFile } from '../../../shared/secure-storage/secure-file'
|
||||
|
||||
const selected = vi.hoisted(() => {
|
||||
const value: Record<string, string> = {}
|
||||
@@ -156,6 +161,42 @@ describe('independent pane agent session environments', () => {
|
||||
)
|
||||
})
|
||||
|
||||
describe('OpenCode Session History resume', () => {
|
||||
it('opens a System store row beside a running selected account', () => {
|
||||
const root = mkdtempSync(join(tmpdir(), 'opencode-system-resume-'))
|
||||
try {
|
||||
const accountState = join(root, 'account-state')
|
||||
writeSecureFile(
|
||||
join(accountState, 'opencode', 'service.json'),
|
||||
JSON.stringify({ pid: process.pid })
|
||||
)
|
||||
vi.stubEnv('XDG_DATA_HOME', join(root, 'system-data'))
|
||||
vi.stubEnv('XDG_STATE_HOME', join(root, 'system-state'))
|
||||
selected.value = {
|
||||
XDG_DATA_HOME: join(root, 'account-data'),
|
||||
XDG_STATE_HOME: accountState,
|
||||
OPENCODE_AUTH_CONTENT: '',
|
||||
OPENCODE_DB: 'opencode.db'
|
||||
}
|
||||
vi.spyOn(getManagedDataAccountService(), 'transcriptEnvironments').mockReturnValue([
|
||||
selected.value
|
||||
])
|
||||
const env = managedDataAccountResumeEnv({
|
||||
agent: 'opencode2',
|
||||
filePath: join(root, 'system-data', 'opencode', 'opencode.db#ses_1')
|
||||
})
|
||||
applyManagedDataAccountEnvironment(env, { launchAgent: 'opencode2' })
|
||||
expect(env.XDG_DATA_HOME).toBe(join(root, 'system-data'))
|
||||
expect(env.XDG_STATE_HOME).toBe(join(root, 'system-state'))
|
||||
expect(env.OPENCODE_DB).toBeUndefined()
|
||||
// The selected account's service holds OpenCode's one port.
|
||||
expect(needsOpenCodePrivateServer(env)).toBe(true)
|
||||
} finally {
|
||||
rmSync(root, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
function hash(value: string | undefined): string | undefined {
|
||||
return value === undefined ? undefined : createHash('sha256').update(value).digest('hex')
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ import type { ManagedDataAccountProvider } from '../../shared/agent-accounts/man
|
||||
import { getManagedDataAccountService, type ManagedDataAccountService } from './service'
|
||||
import { MANAGED_DATA_ACCOUNT_BASELINE_ENV_KEYS } from '../../shared/agent-launch/environment/managed-data-account-environment'
|
||||
import { getAgentLaunchExecutableName } from '../../shared/agent-launch/startup/agent-command-line-intent'
|
||||
import { MANAGED_DATA_ACCOUNT_RESUME_ENV } from '../../shared/agent-launch/environment/managed-data-account-resume'
|
||||
import {
|
||||
MANAGED_DATA_ACCOUNT_RESUME_ENV,
|
||||
SYSTEM_DATA_ACCOUNT_RESUME_ID
|
||||
} from '../../shared/agent-launch/environment/managed-data-account-resume'
|
||||
|
||||
export function applyManagedDataAccountEnvironment(
|
||||
environment: Record<string, string>,
|
||||
@@ -32,6 +35,10 @@ export function applyManagedDataAccountEnvironment(
|
||||
}
|
||||
const selected = managedAccountLaunchEnvironment(service, provider, requestedAccountId)
|
||||
if (!selected.XDG_DATA_HOME) {
|
||||
if (provider === 'opencode' && requestedAccountId === SYSTEM_DATA_ACCOUNT_RESUME_ID) {
|
||||
// Later launch checks can't see the consumed request; a bare marker means the System store.
|
||||
environment.ORCA_DATA_ACCOUNT_PROVIDER = provider
|
||||
}
|
||||
return
|
||||
}
|
||||
service.captureOriginalEnvironment(environment, selected)
|
||||
@@ -46,12 +53,15 @@ export function applyManagedDataAccountEnvironment(
|
||||
}
|
||||
}
|
||||
|
||||
/** A resumed session's owning account wins; a removed owner falls back to the active account. */
|
||||
/** A resumed session's owning store wins; a removed owner falls back to the active account. */
|
||||
export function managedAccountLaunchEnvironment(
|
||||
service: Pick<ManagedDataAccountService, 'environmentForAccount' | 'launchEnvironment'>,
|
||||
provider: ManagedDataAccountProvider,
|
||||
requestedAccountId: string | undefined
|
||||
): Record<string, string> {
|
||||
if (requestedAccountId === SYSTEM_DATA_ACCOUNT_RESUME_ID) {
|
||||
return {}
|
||||
}
|
||||
if (requestedAccountId) {
|
||||
try {
|
||||
return service.environmentForAccount(provider, requestedAccountId)
|
||||
|
||||
@@ -53,10 +53,10 @@ export function needsOpenCodePrivateServer(env: Record<string, string>): boolean
|
||||
const userState = stateHome(original)
|
||||
// Daemon spawns already resolved (and consumed) the resume request into the env.
|
||||
const own =
|
||||
(env.ORCA_DATA_ACCOUNT_PROVIDER === 'opencode' && env.ORCA_DATA_ACCOUNT_STATE_HOME) ||
|
||||
managedAccountLaunchEnvironment(service, 'opencode', env[MANAGED_DATA_ACCOUNT_RESUME_ENV])
|
||||
.XDG_STATE_HOME ||
|
||||
userState
|
||||
(env.ORCA_DATA_ACCOUNT_PROVIDER === 'opencode'
|
||||
? env.ORCA_DATA_ACCOUNT_STATE_HOME
|
||||
: managedAccountLaunchEnvironment(service, 'opencode', env[MANAGED_DATA_ACCOUNT_RESUME_ENV])
|
||||
.XDG_STATE_HOME) || userState
|
||||
if (hasLiveService(own)) {
|
||||
return false
|
||||
}
|
||||
@@ -73,10 +73,6 @@ export function needsOpenCodePrivateServer(env: Record<string, string>): boolean
|
||||
return false
|
||||
}
|
||||
|
||||
export function resetOpenCodeSharedServiceClaimForTests(): void {
|
||||
sharedServiceClaim = undefined
|
||||
}
|
||||
|
||||
/** OpenCode 2's one-port background service serves one data dir; a private server picks its own port. */
|
||||
export function withOpenCodeStandaloneServer(command: string, shell: AgentStartupShell): string {
|
||||
const parsed = tokenizeStartupCommand(command, shell)
|
||||
|
||||
@@ -35,7 +35,14 @@ export function restoreManagedDataAccountEnvironment(
|
||||
): void {
|
||||
const provider = environment.ORCA_DATA_ACCOUNT_PROVIDER
|
||||
const dataHome = environment.ORCA_DATA_ACCOUNT_DATA_HOME
|
||||
if (!dataHome || (provider !== undefined && provider !== 'opencode' && provider !== 'devin')) {
|
||||
if (!dataHome) {
|
||||
// A System-store resume marks only the provider.
|
||||
if (provider === 'opencode') {
|
||||
delete environment.ORCA_DATA_ACCOUNT_PROVIDER
|
||||
}
|
||||
return
|
||||
}
|
||||
if (provider !== undefined && provider !== 'opencode' && provider !== 'devin') {
|
||||
return
|
||||
}
|
||||
let original: z.infer<typeof originalEnvironment> | undefined
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
/** Spawn-env request for one managed account; the host validates it and falls back to the active one. */
|
||||
export const MANAGED_DATA_ACCOUNT_RESUME_ENV = 'ORCA_DATA_ACCOUNT_ID'
|
||||
/** Requests the user's own store; older hosts reject it as an unknown account and fall back. */
|
||||
export const SYSTEM_DATA_ACCOUNT_RESUME_ID = 'system'
|
||||
|
||||
const OPENCODE_ACCOUNT_STORE =
|
||||
/[\\/]managed-data-accounts[\\/]opencode[\\/]([0-9a-f-]{36})[\\/]data[\\/]opencode[\\/]/i
|
||||
|
||||
/** OpenCode only resumes from the account store holding the row (`<db>#<session>` or legacy JSON). */
|
||||
/** OpenCode only resumes from the store holding the row (`<db>#<session>` or legacy JSON). */
|
||||
export function managedDataAccountResumeEnv(session: {
|
||||
agent: string
|
||||
filePath?: string
|
||||
}): Record<string, string> {
|
||||
if (session.agent !== 'opencode' && session.agent !== 'opencode2') {
|
||||
if ((session.agent !== 'opencode' && session.agent !== 'opencode2') || !session.filePath) {
|
||||
return {}
|
||||
}
|
||||
const accountId = session.filePath?.match(OPENCODE_ACCOUNT_STORE)?.[1]
|
||||
return accountId ? { [MANAGED_DATA_ACCOUNT_RESUME_ENV]: accountId } : {}
|
||||
const accountId =
|
||||
session.filePath.match(OPENCODE_ACCOUNT_STORE)?.[1] ?? SYSTEM_DATA_ACCOUNT_RESUME_ID
|
||||
return { [MANAGED_DATA_ACCOUNT_RESUME_ENV]: accountId }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user