Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/scripts/vitest-sqlite-runtime-files.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export const SQLITE_RUNTIME_INCLUDE = [
'src/main/runtime/rpc/methods/agent-launch-prestart-failure.test.ts',
'src/main/runtime/rpc/methods/agent-launch-replay.test.ts',
'src/main/runtime/rpc/methods/agent-launch-session-reservation.test.ts',
'src/main/runtime/rpc/methods/repo-update-owner-dispatch.test.ts',
'src/main/runtime/rpc/methods/structured-agent-session-adoption-replay.test.ts',
'src/main/runtime/rpc/methods/structured-agent-session-at-rest.test.ts',
'src/main/runtime/rpc/methods/structured-agent-session-create-message.test.ts',
Expand Down
270 changes: 270 additions & 0 deletions src/main/runtime/rpc/methods/repo-update-owner-dispatch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
import { mkdtempSync, rmSync } from 'node:fs'
import { join } from 'node:path'
import { tmpdir } from 'node:os'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { getDefaultPersistedState } from '../../../../shared/constants'
import {
REPO_UPDATE_EXECUTION_HOST_RUNTIME_CAPABILITY,
RUNTIME_CAPABILITIES
} from '../../../../shared/protocol-version'
import type { Repo } from '../../../../shared/repo-types'
import {
closeTestStores,
createStore,
testState,
writeDataFile
} from '../../../persistence-test-harness'
import { OrcaRuntimeService } from '../../orca-runtime'
import { RpcDispatcher } from '../dispatcher'
import { REPO_METHODS } from './repo'

vi.mock('electron', () => ({
app: { getPath: () => testState.dir },
safeStorage: { isEncryptionAvailable: () => false }
}))
vi.mock('../../../telemetry/client', () => ({ track: vi.fn() }))
vi.mock('../../../telemetry/cohort-classifier', () => ({ getCohortAtEmit: vi.fn() }))

beforeEach(() => {
testState.dir = mkdtempSync(join(tmpdir(), 'orca-repo-update-owner-'))
})
afterEach(async () => {
await closeTestStores()
rmSync(testState.dir, { recursive: true, force: true })
})

function repo(suffix: 'a' | 'b'): Repo {
return {
id: 'shared-id',
displayName: 'Shared',
path: `/receiver/${suffix}`,
executionHostId: `ssh:private-${suffix}`,
kind: 'folder',
badgeColor: '#737373',
addedAt: 1
}
}

function fixture(rows: readonly Repo[]) {
writeDataFile({ ...getDefaultPersistedState(testState.dir), repos: rows })
const store = createStore()
const runtime = new OrcaRuntimeService(store)
const update = vi.spyOn(store, 'updateRepo')
const runtimeUpdate = vi.spyOn(runtime, 'updateRepo')
const dispatcher = new RpcDispatcher({ runtime, methods: REPO_METHODS })
return { store, runtime, update, runtimeUpdate, dispatcher }
}

async function dispatchUpdate(dispatcher: RpcDispatcher, params: Record<string, unknown>) {
const replies: unknown[] = []
await dispatcher.dispatchStreaming(
{ id: 'update', authToken: 'owned-test', method: 'repo.update', params },
(frame) => replies.push(JSON.parse(frame)),
{
clientKind: 'runtime',
pairedDeviceId: 'owned-repo-update-device',
clientCapabilities: [...RUNTIME_CAPABILITIES]
}
)
expect(replies).toHaveLength(1)
return replies[0]
}

describe('registered repo update owner resolution', () => {
it('advertises qualified repo updates from the runtime status', () => {
const { runtime } = fixture([repo('b')])
expect(runtime.getStatus().capabilities).toContain(
REPO_UPDATE_EXECUTION_HOST_RUNTIME_CAPABILITY
)
})

it.each([false, true])('updates only qualified B (B first: %s)', async (bFirst) => {
const rows = bFirst ? [repo('b'), repo('a')] : [repo('a'), repo('b')]
const { store, update, dispatcher } = fixture(rows)
const before = structuredClone(store.getRepos())
const updates = { displayName: 'Renamed B' }
const result = await dispatchUpdate(dispatcher, {
repo: 'shared-id',
executionHostId: 'ssh:private-b',
updates
})
expect(result).toMatchObject({
ok: true,
result: { repo: { ...repo('b'), ...updates } }
})
expect(update).toHaveBeenCalledExactlyOnceWith('shared-id', updates, 'ssh:private-b')
expect(store.getRepos()).toEqual(
before.map((row) => (row.executionHostId === 'ssh:private-b' ? { ...row, ...updates } : row))
)
await store.flushPendingOrThrowAsync({ drainToStableGeneration: true })
expect(createStore().getRepos()).toEqual(store.getRepos())
})

it.each(['id:shared-id', 'name:Shared', 'path:/receiver/b'])(
'qualifies the existing %s selector',
async (selector) => {
const { store, update, dispatcher } = fixture([repo('a'), repo('b')])
const before = structuredClone(store.getRepos())
const result = await dispatchUpdate(dispatcher, {
repo: selector,
executionHostId: 'ssh:private-b',
updates: { displayName: 'Selected B' }
})
expect(result).toMatchObject({
ok: true,
result: { repo: { path: '/receiver/b', executionHostId: 'ssh:private-b' } }
})
expect(update).toHaveBeenCalledExactlyOnceWith(
'shared-id',
{ displayName: 'Selected B' },
'ssh:private-b'
)
expect(store.getRepos()[0]).toEqual(before[0])
}
)

it.each([false, true])('keeps an unqualified unique path on B (B first: %s)', async (bFirst) => {
const rows = bFirst ? [repo('b'), repo('a')] : [repo('a'), repo('b')]
const { store, update, runtimeUpdate, dispatcher } = fixture(rows)
const before = structuredClone(store.getRepos())
const updates = { displayName: 'Path-selected B' }
expect(await dispatchUpdate(dispatcher, { repo: 'path:/receiver/b', updates })).toMatchObject({
ok: true,
result: { repo: { path: '/receiver/b', ...updates } }
})
expect(runtimeUpdate).toHaveBeenCalledExactlyOnceWith('path:/receiver/b', updates)
expect(update).toHaveBeenCalledExactlyOnceWith('shared-id', updates, 'ssh:private-b')
expect(store.getRepos()).toEqual(
before.map((row) => (row.executionHostId === 'ssh:private-b' ? { ...row, ...updates } : row))
)
})

it('preserves the unique legacy update call and durable local row', async () => {
const { store, update, runtimeUpdate, dispatcher } = fixture([
{ ...repo('b'), executionHostId: 'local' }
])
const updates = { displayName: 'Legacy rename' }
expect(await dispatchUpdate(dispatcher, { repo: 'shared-id', updates })).toMatchObject({
ok: true,
result: { repo: { displayName: 'Legacy rename', executionHostId: 'local' } }
})
expect(runtimeUpdate).toHaveBeenCalledExactlyOnceWith('shared-id', updates)
expect(update).toHaveBeenCalledExactlyOnceWith('shared-id', updates, 'local')
await store.flushPendingOrThrowAsync({ drainToStableGeneration: true })
expect(createStore().getRepos()).toEqual(store.getRepos())
})

it('keeps sanitized fields, hooks and explicit clears on the qualified row', async () => {
const inherited = {
ghAccount: { host: 'github.com', user: 'Alice' },
sourceControlAi: { enabled: false },
externalWorktreeDiscoverySuppressedAt: 99,
worktreeBasePath: '../old'
}
const { store, runtime, update, dispatcher } = fixture([
{ ...repo('a'), ...inherited },
{ ...repo('b'), ...inherited }
])
const before = structuredClone(store.getRepos())
const hookSettings = {
mode: 'override' as const,
scripts: { setup: 'prepare', archive: 'cleanup' }
}
expect(
await dispatchUpdate(dispatcher, {
repo: 'shared-id',
executionHostId: 'ssh:private-b',
updates: {
ghAccount: { host: ' GitHub.COM ', user: ' Bob ' },
sourceControlAi: null,
externalWorktreeDiscoverySuppressedAt: null,
hookSettings
}
})
).toMatchObject({ ok: true })
expect(update).toHaveBeenCalledExactlyOnceWith(
'shared-id',
{
ghAccount: { host: 'github.com', user: 'Bob' },
sourceControlAi: null,
externalWorktreeDiscoverySuppressedAt: undefined,
hookSettings
},
'ssh:private-b'
)
await runtime.updateRepo(
'shared-id',
{ ghAccount: null, worktreeBasePath: undefined },
'ssh:private-b'
)
expect(store.getRepos()[0]).toEqual(before[0])
const selected = store.getRepos()[1]
expect(selected?.hookSettings).toMatchObject(hookSettings)
for (const field of [
'ghAccount',
'sourceControlAi',
'externalWorktreeDiscoverySuppressedAt',
'worktreeBasePath'
]) {
expect(selected).not.toHaveProperty(field)
}
await store.flushPendingOrThrowAsync({ drainToStableGeneration: true })
expect(createStore().getRepos()).toEqual(store.getRepos())
})

it.each([false, true])('refuses ambiguous legacy IDs (B first: %s)', async (bFirst) => {
const { store, update, dispatcher } = fixture(
bFirst ? [repo('b'), repo('a')] : [repo('a'), repo('b')]
)
const before = structuredClone(store.getRepos())
expect(
await dispatchUpdate(dispatcher, {
repo: 'shared-id',
updates: { displayName: 'Do not save' }
})
).toMatchObject({ ok: false, error: { code: 'selector_ambiguous' } })
expect(update).not.toHaveBeenCalled()
expect(store.getRepos()).toEqual(before)
})

it.each([
{ selector: 'shared-id', host: 'ssh:missing', code: 'repo_not_found' },
{ selector: 'path:/receiver/a', host: 'ssh:private-b', code: 'repo_not_found' },
{ selector: 'path:/receiver/b', host: 'invalid', code: 'invalid_argument' },
{ selector: 'path:/receiver/b', host: null, code: 'invalid_argument' }
])('refuses $host/$selector before writing', async ({ selector, host, code }) => {
const { store, update, dispatcher } = fixture([repo('a'), repo('b')])
const before = structuredClone(store.getRepos())
expect(
await dispatchUpdate(dispatcher, {
repo: selector,
executionHostId: host,
updates: { displayName: 'Do not save' }
})
).toMatchObject({ ok: false, error: { code } })
expect(update).not.toHaveBeenCalled()
expect(store.getRepos()).toEqual(before)
})

it.each(['duplicate', 'contradictory'] as const)(
'refuses a %s owner catalog before writing',
async (kind) => {
const rows =
kind === 'duplicate'
? [repo('a'), repo('b'), { ...repo('b'), path: '/receiver/duplicate-b' }]
: [{ ...repo('a'), connectionId: 'different-host' }, repo('b')]
const { store, update, dispatcher } = fixture(rows)
const before = structuredClone(store.getRepos())
expect(before).toHaveLength(rows.length)
expect(
await dispatchUpdate(dispatcher, {
repo: 'path:/receiver/b',
executionHostId: 'ssh:private-b',
updates: { displayName: 'Do not save' }
})
).toMatchObject({ ok: false, error: { code: 'repo_not_found' } })
expect(update).not.toHaveBeenCalled()
expect(store.getRepos()).toEqual(before)
}
)
})
17 changes: 8 additions & 9 deletions src/main/runtime/rpc/methods/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,14 @@ export const REPO_METHODS = [
name: 'repo.update',
permission: 'workspace',
params: RepoUpdate,
handler: async (params, context) => ({
repo: projectRepoVisibilityForClient(
await context.runtime.updateRepo(
params.repo,
params.updates as Parameters<typeof context.runtime.updateRepo>[1]
),
context
)
})
handler: async (params, context) => {
// oxlint-disable-next-line typescript/consistent-type-assertions -- SAFETY: The existing update schema and controller patch shape are unchanged; host validation is separate.
const updates = params.updates as Parameters<typeof context.runtime.updateRepo>[1]
const repo = await (params.executionHostId === undefined
? context.runtime.updateRepo(params.repo, updates)
: context.runtime.updateRepo(params.repo, updates, params.executionHostId))
return { repo: projectRepoVisibilityForClient(repo, context) }
}
}),
defineMethod({
name: 'repo.rm',
Expand Down
12 changes: 9 additions & 3 deletions src/main/runtime/runtime-repository-settings-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ export class RuntimeRepositorySettingsController {
return updated
}

async update(repoSelector: string, updates: RepositoryUpdates): Promise<Repo> {
async update(
repoSelector: string,
updates: RepositoryUpdates,
executionHostId?: ExecutionHostId
): Promise<Repo> {
const store = this.requireStore()
const repo = await this.deps.resolveRepo(repoSelector)
const repo = await (executionHostId === undefined
? this.deps.resolveRepo(repoSelector)
: this.deps.resolveRepo(repoSelector, executionHostId))
const sanitizedUpdates = omitUndefined(updates)
if ('worktreeBasePath' in updates && updates.worktreeBasePath === undefined) {
sanitizedUpdates.worktreeBasePath = undefined
Expand All @@ -88,7 +94,7 @@ export class RuntimeRepositorySettingsController {
if ('sourceControlAi' in updates && updates.sourceControlAi === null) {
sanitizedUpdates.sourceControlAi = null
}
const updated = store.updateRepo(repo.id, sanitizedUpdates)
const updated = store.updateRepo(repo.id, sanitizedUpdates, getRepoExecutionHostId(repo))
if (!updated) {
throw new Error('repo_not_found')
}
Expand Down
20 changes: 12 additions & 8 deletions src/renderer/src/components/settings/RepositoryPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { OrcaHooks, RepoHookSettings } from '../../../../shared/orca-yaml-h
import type { Project, ProjectUpdateArgs } from '../../../../shared/project-types'
import type { Repo } from '../../../../shared/repo-types'
import { getRepoKindLabel, isFolderRepo } from '../../../../shared/repo-kind'
import { getRepoExecutionHostId, type ExecutionHostId } from '../../../../shared/execution-host'
import { getRepoExecutionHostId } from '../../../../shared/execution-host'
import type { RepoUpdateOwnerOptions } from '../../store/repos/repo-state'
import { Label } from '../ui/label'
import { Separator } from '../ui/separator'
import { useShallow } from 'zustand/react/shallow'
Expand Down Expand Up @@ -54,7 +55,7 @@ type RepositoryPaneProps = {
updateRepo: (
repoId: string,
updates: RepositoryPaneRepoUpdate,
options?: { hostId?: ExecutionHostId }
options?: RepoUpdateOwnerOptions
) => void | Promise<boolean>
removeProject: (repoId: string) => void
project?: Project | null
Expand Down Expand Up @@ -92,15 +93,18 @@ export function RepositoryPane({
updateProject
}: RepositoryPaneProps): React.JSX.Element {
const isFolder = isFolderRepo(repo)
// Why: this pane renders the switcher-selected host's repo row. Bind every
// edit to that host so identity/host-specific writes land on the selected
// host, not findRepoForHost's focused-host fallback (the same-id/self-pair
// case where local and a runtime share one repo id).
// Keep edits bound to the selected setup's publisher and raw execution owner.
const selectedHostId = getRepoExecutionHostId(repo)
const selectedRawHostId = repo.authoritativeExecutionHostId
const selectedPublisherHostId = repo.catalogOwnerHostId
const updateSelectedRepo = useCallback(
(repoId: string, updates: RepositoryPaneRepoUpdate) =>
updateRepo(repoId, updates, { hostId: selectedHostId }),
[updateRepo, selectedHostId]
updateRepo(repoId, updates, {
hostId: selectedHostId,
...(selectedRawHostId ? { authoritativeExecutionHostId: selectedRawHostId } : {}),
...(selectedPublisherHostId ? { catalogOwnerHostId: selectedPublisherHostId } : {})
}),
[updateRepo, selectedHostId, selectedRawHostId, selectedPublisherHostId]
)
const searchQuery = useAppStore((state) => state.settingsSearchQuery)
const settings = useAppStore((state) => state.settings)
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/src/store/repos/repo-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export type RepoUpdate = Partial<

export type ProjectUpdate = ProjectUpdateArgs['updates']

export type RepoUpdateOwnerOptions = {
hostId?: ExecutionHostId
authoritativeExecutionHostId?: ExecutionHostId
catalogOwnerHostId?: ExecutionHostId
}

export type FolderWorkspaceUpdates = Partial<
Pick<
FolderWorkspace,
Expand Down Expand Up @@ -268,7 +274,7 @@ export type RepoSlice = {
updateRepo: (
projectId: string,
updates: RepoUpdate,
options?: { hostId?: ExecutionHostId }
options?: RepoUpdateOwnerOptions
) => Promise<boolean>
setActiveRepo: (projectId: string | null) => void
reorderRepos: (orderedIds: string[]) => Promise<void>
Expand Down
Loading
Loading