fix(runtime): make sibling-workspace terminal-path resolution an explicit client opt-in (#12616)

files.resolveTerminalPath began returning a foreign worktree id + relativePath
for absolute paths owned by a sibling workspace, with no protocol or capability
gate. Mobile 0.0.36 in the field ignores resolved.worktree and reuses its own
worktree id for the follow-up files.open, so a tap on a sibling-worktree path
opened the WRONG worktree's copy of that file (on 1.4.168 the tap was a safe
no-op).

Gate the sibling-workspace lookup behind a new optional crossWorkspace request
field: clients that honor resolved.worktree opt in; everything else keeps the
pre-sibling-resolution contract. Old servers strip the unknown field (zod), so
every version pairing degrades to the safe legacy behavior. Optional-field
addition, so no RUNTIME_PROTOCOL_VERSION bump per protocol-version.ts rules.

The terminal-path RPC tests move to files-terminal-path-resolution.test.ts
because files.test.ts sits at the max-lines cap.

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
This commit is contained in:
Jinwoo Hong
2026-08-04 18:46:36 -07:00
committed by GitHub
co-authored by OrcaWin
parent 39c3c58d55
commit 5ed45739e9
9 changed files with 175 additions and 50 deletions
@@ -64,7 +64,12 @@ describe('openMobileFileTap', () => {
expect(client.sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: '/tmp/result.json', terminal: 'terminal-1' },
{
worktree: 'id:wt-1',
pathText: '/tmp/result.json',
terminal: 'terminal-1',
crossWorkspace: true
},
{ timeoutMs: 10_000 }
)
expect(pushPreviewRoute).toHaveBeenCalledWith({
@@ -322,7 +327,13 @@ describe('openMobileFileTap', () => {
expect(client.sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: 'index.ts', terminal: 'term-1', cwd: '/repo/src' },
{
worktree: 'id:wt-1',
pathText: 'index.ts',
terminal: 'term-1',
cwd: '/repo/src',
crossWorkspace: true
},
{ timeoutMs: 10_000 }
)
})
@@ -75,6 +75,8 @@ async function openMobileFileTapAsync<T extends FileTapSessionTab>(
{
worktree,
pathText: options.pathText,
// Why: opts into sibling-workspace resolutions; this caller honors resolved.worktree.
crossWorkspace: true,
...(options.terminalHandle && options.terminalHandle.trim().length > 0
? { terminal: options.terminalHandle }
: {}),
@@ -60,7 +60,7 @@ describe('openMobileNativeChatFileTap', () => {
expect(sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: 'src/app.ts' },
{ worktree: 'id:wt-1', pathText: 'src/app.ts', crossWorkspace: true },
{ timeoutMs: 10_000 }
)
})
@@ -74,7 +74,7 @@ describe('openMobileNativeChatFileTap', () => {
expect(sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: 'src/app.ts' },
{ worktree: 'id:wt-1', pathText: 'src/app.ts', crossWorkspace: true },
{ timeoutMs: 10_000 }
)
expect(options.triggerOpenFeedback).toHaveBeenCalledTimes(1)
@@ -98,7 +98,13 @@ describe('useMobileFileTapHandlers', () => {
expect(sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: 'index.ts', terminal: 'terminal-1', cwd: '/repo/sub' },
{
worktree: 'id:wt-1',
pathText: 'index.ts',
terminal: 'terminal-1',
cwd: '/repo/sub',
crossWorkspace: true
},
{ timeoutMs: 10_000 }
)
})
@@ -126,7 +132,7 @@ describe('useMobileFileTapHandlers', () => {
expect(sendRequest).toHaveBeenCalledWith(
'files.resolveTerminalPath',
{ worktree: 'id:wt-1', pathText: 'mobile/src/x.ts' },
{ worktree: 'id:wt-1', pathText: 'mobile/src/x.ts', crossWorkspace: true },
{ timeoutMs: 10_000 }
)
expect(options.reportChatTapFailure).toHaveBeenCalledWith("Couldn't open mobile/src/x.ts:12")
+66 -7
View File
@@ -836,9 +836,17 @@ describe('RuntimeFileCommands', () => {
commands: RuntimeFileCommands,
pathText: string,
cwd: string | null = null,
clientId = 'client-a'
clientId = 'client-a',
crossWorkspace = false
) {
return commands.resolveTerminalPath('id:wt-1', pathText, cwd, clientId, 'term-1')
return commands.resolveTerminalPath(
'id:wt-1',
pathText,
cwd,
clientId,
'term-1',
crossWorkspace
)
}
function createRemoteTerminalArtifactGrantFixture(artifactPath = '/tmp/result.json') {
@@ -911,7 +919,14 @@ describe('RuntimeFileCommands', () => {
})
statAsFile()
const result = await commands.resolveTerminalPath('id:wt-1', '/sibling/docs/readme.md')
const result = await commands.resolveTerminalPath(
'id:wt-1',
'/sibling/docs/readme.md',
null,
undefined,
null,
true
)
expect(resolveKnownWorkspaceFileTarget).toHaveBeenCalledWith(
'/sibling/docs/readme.md',
@@ -958,7 +973,7 @@ describe('RuntimeFileCommands', () => {
resolveAuthorizedPathMock.mockImplementation(async (p: string) => p)
statMock.mockResolvedValue({ isDirectory: () => true })
const result = await resolveTerminalArtifactPath(commands, '/sibling')
const result = await resolveTerminalArtifactPath(commands, '/sibling', null, 'client-a', true)
expect(resolveKnownWorkspaceFileTarget).toHaveBeenCalledWith('/sibling', 'local')
expect(result).toEqual({
@@ -999,7 +1014,14 @@ describe('RuntimeFileCommands', () => {
const remoteStat = vi.fn().mockResolvedValue({ type: 'file', size: 12, mtime: 3 })
vi.mocked(getSshFilesystemProvider).mockReturnValue({ stat: remoteStat } as never)
const result = await commands.resolveTerminalPath('id:wt-1', '/sibling/docs/readme.md')
const result = await commands.resolveTerminalPath(
'id:wt-1',
'/sibling/docs/readme.md',
null,
undefined,
null,
true
)
expect(resolveKnownWorkspaceFileTarget).toHaveBeenCalledWith(
'/sibling/docs/readme.md',
@@ -1044,7 +1066,7 @@ describe('RuntimeFileCommands', () => {
const remoteStat = vi.fn().mockResolvedValue({ type: 'directory', size: 0, mtime: 3 })
vi.mocked(getSshFilesystemProvider).mockReturnValue({ stat: remoteStat } as never)
const result = await resolveTerminalArtifactPath(commands, '/sibling')
const result = await resolveTerminalArtifactPath(commands, '/sibling', null, 'client-a', true)
expect(resolveKnownWorkspaceFileTarget).toHaveBeenCalledWith('/sibling', 'ssh:ssh-1')
expect(remoteStat).toHaveBeenCalledWith('/sibling')
@@ -1068,7 +1090,14 @@ describe('RuntimeFileCommands', () => {
resolveKnownWorkspaceFileTarget
})
await commands.resolveTerminalPath('id:wt-1', '/repo-b/docs/readme.md')
await commands.resolveTerminalPath(
'id:wt-1',
'/repo-b/docs/readme.md',
null,
undefined,
null,
true
)
expect(resolveKnownWorkspaceFileTarget).toHaveBeenCalledWith(
'/repo-b/docs/readme.md',
@@ -1076,6 +1105,36 @@ describe('RuntimeFileCommands', () => {
)
})
// Why: clients predating crossWorkspace (mobile <=0.0.36) reuse their own worktree
// id for files.open, so they must keep the pre-sibling-resolution contract.
it('keeps the caller worktree and a null relativePath when crossWorkspace is not requested', async () => {
const resolveKnownWorkspaceFileTarget = vi.fn(async () => ({
worktree: {
id: 'wt-2',
repoId: 'repo-2',
path: '/sibling',
git: { path: '/sibling', head: '', branch: '', isBare: false, isMainWorktree: true }
},
relativePath: 'docs/readme.md'
}))
const { commands } = createRuntimeFileCommands({
path: '/repo',
resolveKnownWorkspaceFileTarget
})
statAsFile()
const result = await commands.resolveTerminalPath('id:wt-1', '/sibling/docs/readme.md')
expect(resolveKnownWorkspaceFileTarget).not.toHaveBeenCalled()
expect(result).toEqual({
worktree: 'wt-1',
relativePath: null,
absolutePath: '/sibling/docs/readme.md',
exists: false,
isDirectory: false
})
})
it('resolves a relative path against the provided cwd', async () => {
const { commands } = createRuntimeFileCommands({ path: '/repo' })
statAsFile()
+5 -2
View File
@@ -719,7 +719,8 @@ export class RuntimeFileCommands {
pathText: string,
cwd?: string | null,
clientId?: string,
terminalHandle?: string | null
terminalHandle?: string | null,
crossWorkspace?: boolean
): Promise<RuntimeTerminalPathResolution> {
const store = this.host.requireStore()
const target = await this.host.resolveRuntimeFileTarget(worktreeSelector)
@@ -757,8 +758,10 @@ export class RuntimeFileCommands {
terminalFileUriHostname
})
const relativePath = relativePathInsideRoot(worktree.path, absolutePath)
// Why: clients that predate crossWorkspace reuse their own worktree id for the
// follow-up files.open, so retargeting to a sibling workspace must be opt-in.
const knownWorkspaceTarget =
relativePath === null
crossWorkspace && relativePath === null
? await this.host.resolveKnownWorkspaceFileTarget?.(
absolutePath,
getRuntimeFileTargetExecutionHostId(target)
@@ -0,0 +1,73 @@
import { describe, expect, it, vi } from 'vitest'
import { RpcDispatcher } from '../dispatcher'
import type { RpcRequest } from '../core'
import type { OrcaRuntimeService } from '../../orca-runtime'
import { FILE_METHODS } from './files'
function makeRequest(method: string, params?: unknown): RpcRequest {
return { id: 'req-1', authToken: 'tok', method, params }
}
describe('files.resolveTerminalPath RPC', () => {
function createDispatcher() {
const runtime = {
getRuntimeId: () => 'test-runtime',
resolveTerminalPath: vi.fn().mockResolvedValue({
worktree: 'wt-1',
relativePath: 'src/index.ts',
exists: true,
isDirectory: false
})
} as unknown as OrcaRuntimeService
return { runtime, dispatcher: new RpcDispatcher({ runtime, methods: FILE_METHODS }) }
}
it('resolves a tapped terminal path for a selected worktree', async () => {
const { runtime, dispatcher } = createDispatcher()
const response = await dispatcher.dispatch(
makeRequest('files.resolveTerminalPath', {
worktree: 'id:wt-1',
pathText: '/repo/src/index.ts',
cwd: '/repo',
terminal: 'term-1'
})
)
expect(runtime.resolveTerminalPath).toHaveBeenCalledWith(
'id:wt-1',
'/repo/src/index.ts',
'/repo',
undefined,
'term-1',
false
)
expect(response).toMatchObject({
ok: true,
result: { relativePath: 'src/index.ts', exists: true, isDirectory: false }
})
})
// Why: clients predating crossWorkspace reuse their own worktree id for the
// follow-up files.open, so sibling-workspace retargeting must be an explicit opt-in.
it('forwards crossWorkspace only when the client opts in', async () => {
const { runtime, dispatcher } = createDispatcher()
await dispatcher.dispatch(
makeRequest('files.resolveTerminalPath', {
worktree: 'id:wt-1',
pathText: '/sibling/docs/readme.md',
crossWorkspace: true
})
)
expect(runtime.resolveTerminalPath).toHaveBeenLastCalledWith(
'id:wt-1',
'/sibling/docs/readme.md',
null,
undefined,
null,
true
)
})
})
@@ -325,40 +325,6 @@ describe('file RPC methods', () => {
})
})
it('resolves a tapped terminal path for a selected worktree', async () => {
const runtime = {
getRuntimeId: () => 'test-runtime',
resolveTerminalPath: vi.fn().mockResolvedValue({
worktree: 'wt-1',
relativePath: 'src/index.ts',
exists: true,
isDirectory: false
})
} as unknown as OrcaRuntimeService
const dispatcher = new RpcDispatcher({ runtime, methods: FILE_METHODS })
const response = await dispatcher.dispatch(
makeRequest('files.resolveTerminalPath', {
worktree: 'id:wt-1',
pathText: '/repo/src/index.ts',
cwd: '/repo',
terminal: 'term-1'
})
)
expect(runtime.resolveTerminalPath).toHaveBeenCalledWith(
'id:wt-1',
'/repo/src/index.ts',
'/repo',
undefined,
'term-1'
)
expect(response).toMatchObject({
ok: true,
result: { relativePath: 'src/index.ts', exists: true, isDirectory: false }
})
})
it('reads a terminal artifact through a grant-scoped method', async () => {
const runtime = {
getRuntimeId: () => 'test-runtime',
+6 -1
View File
@@ -74,6 +74,10 @@ const ResolveTerminalPath = WorktreeSelector.extend({
.unknown()
.transform((v) => (typeof v === 'string' && v.length > 0 ? v : null))
.nullable()
.optional(),
crossWorkspace: z
.unknown()
.transform((v) => v === true)
.optional()
})
@@ -252,7 +256,8 @@ export const FILE_METHODS: RpcAnyMethod[] = [
params.pathText,
params.cwd ?? null,
clientId,
params.terminal ?? null
params.terminal ?? null,
params.crossWorkspace === true
)
}),
defineMethod({