Files
orca/src/cli/runtime/runtime-source-fence.ts
T
AnmolandKelvin Amoaba 76d23612ba fix(cli): use the terminal's worktree when cwd is outside every worktree (#38086)
* fix(cli): use the terminal's worktree when cwd is outside every worktree

Browser, terminal and computer commands run from an unmanaged cwd fell back to the worktree focused in the window. They now use the calling terminal's ORCA_WORKTREE_ID or folder ORCA_WORKSPACE_ID before that default. Fixes #37899.

* fix(cli): resolve the terminal worktree against the catalog

Match ORCA_WORKTREE_ID on the caller's host so ids shared across hosts
go out as identity:, accept folder: ids from either terminal env var,
and keep the runtime default when worktree.list cannot be read.

* fix(cli): refuse a stale terminal worktree instead of using the focused one

When ORCA_WORKTREE_ID names a worktree the catalog no longer lists on the
caller's host, fail with a --worktree hint rather than silently targeting the
workspace focused in Orca. Clear the terminal's workspace env in the test setup
so CLI tests run the same inside an Orca terminal.

* fix(cli): keep the floating terminal on the runtime default

The floating terminal's ORCA_WORKTREE_ID is a sentinel with no catalog row, so it must not hit the stale-workspace error. Word that error as what the CLI can tell: the workspace is not listed, which also covers hidden worktrees.

* fix(cli): ignore terminal workspace ids from another Orca profile

A shell from the installed app that targets a dev build or throwaway serve carries worktree ids that runtime never issued. Reuse the runtime-source fence rule to skip them, so those commands keep the target runtime's default instead of failing.

* test(cli): drop tests that pass without the terminal-workspace fallback

Remove the list-failure and cwd-precedence cases, which pass on main, and the SSH stubs the folder case never reads.

---------

Co-authored-by: Kelvin Amoaba <97001695+AmoabaKelvin@users.noreply.github.com>
2026-11-06 05:15:02 +00:00

108 lines
4.2 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 type { RuntimeMetadata } from '../../shared/runtime-bootstrap'
import { isSameUserDataPath } from '../../shared/serve-user-data-path'
import { readRuntimeSourceStamp, type RuntimeSourceStamp } from '../../shared/runtime-source-env'
import type { ExpectedRuntimeSource } from '../../shared/runtime-rpc-envelope'
import type { RuntimeStatus } from '../../shared/runtime-types'
import { getPlatformUserDataPath } from './metadata'
import { sendRequest } from './transport'
import { RuntimeClientError, type RuntimeRpcResponse } from './types'
type StatusProbe = (metadata: RuntimeMetadata) => Promise<RuntimeRpcResponse<RuntimeStatus>>
/** A different nondefault profile opts out of the inherited runtime fence. */
export function resolveFencedRuntimeSource(
stamp: RuntimeSourceStamp | null,
userDataPath: string,
platformDefaultPath: string | null
): RuntimeSourceStamp | null {
if (!stamp?.profilePath || isSameUserDataPath(userDataPath, stamp.profilePath)) {
return stamp
}
// Fence unpinned terminals falling back to the platform default.
return platformDefaultPath !== null && isSameUserDataPath(userDataPath, platformDefaultPath)
? stamp
: null
}
/** Fence requests to their launching source, allowing new incarnations after restarts and updates. */
export class RuntimeSourceFence {
private readonly verifiedRuntimeIds = new Set<string>()
constructor(
private readonly expected: RuntimeSourceStamp | null,
private readonly probe: StatusProbe
) {
if (expected) {
this.verifiedRuntimeIds.add(expected.incarnation)
}
}
async check(
metadata: RuntimeMetadata,
method: string
): Promise<ExpectedRuntimeSource | undefined> {
// status.get is the read-only probe itself.
if (!this.expected || method === 'status.get') {
return undefined
}
const expectation = { sourceId: this.expected.sourceId, runtimeId: metadata.runtimeId }
if (this.verifiedRuntimeIds.has(metadata.runtimeId)) {
return expectation
}
// The transport verifies that this response came from metadata’s runtime.
const response = await this.probe(metadata)
if (response.ok === false) {
throw new RuntimeClientError(
'runtime_source_unverifiable',
`Could not confirm that the running Orca is the one that launched this terminal: ${response.error.message}`
)
}
const sourceId = response.result.hostDescriptor?.installationId
if (!sourceId) {
throw new RuntimeClientError(
'runtime_source_unverifiable',
'The running Orca does not report which profile it serves, so this terminal cannot confirm it is the Orca that launched it. Nothing was sent. Open a new terminal from the Orca you want to control.'
)
}
if (sourceId !== this.expected.sourceId) {
throw new RuntimeClientError(
'runtime_source_mismatch',
'The running Orca is not the one that launched this terminal (another profile or instance took over its runtime record). Nothing was sent. Open a new terminal from the Orca you want to control.'
)
}
this.verifiedRuntimeIds.add(metadata.runtimeId)
return expectation
}
}
function resolveEnvRuntimeSource(
env: Readonly<Record<string, string | undefined>>,
userDataPath: string
): RuntimeSourceStamp | null {
let platformDefaultPath: string | null = null
try {
platformDefaultPath = getPlatformUserDataPath()
} catch {
// Without a platform default, no terminal can fall back to it.
}
return resolveFencedRuntimeSource(readRuntimeSourceStamp(env), userDataPath, platformDefaultPath)
}
/** A terminal launched by another profile carries workspace ids that this runtime never issued. */
export function isLaunchedByAnotherRuntime(
env: Readonly<Record<string, string | undefined>>,
userDataPath: string
): boolean {
return readRuntimeSourceStamp(env) !== null && resolveEnvRuntimeSource(env, userDataPath) === null
}
export function createRuntimeSourceFence(
env: Readonly<Record<string, string | undefined>>,
userDataPath: string,
probeTimeoutMs: number
): RuntimeSourceFence {
return new RuntimeSourceFence(resolveEnvRuntimeSource(env, userDataPath), (metadata) =>
sendRequest<RuntimeStatus>(metadata, 'status.get', undefined, probeTimeoutMs)
)
}