Skip to content
Merged
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
8 changes: 5 additions & 3 deletions src/main/ssh/orcad-migration-catalog-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ const ServerRepositoriesSchema = z.object({
id: z.string().min(1),
path: z.string().min(1),
connectionId: z.string().nullish(),
executionHostId: z.string().nullish()
executionHostId: z.string().nullish(),
projectGroupId: z.string().nullish()
})
)
})
Expand All @@ -179,11 +180,12 @@ export async function listRemoteOrcadRepositories(
throw new Error(`orcad_migration_repositories_failed:${response.error.message}`)
}
return ServerRepositoriesSchema.parse(response.result).repos.map(
({ id, path, connectionId, executionHostId }) => ({
({ id, path, connectionId, executionHostId, projectGroupId }) => ({
id,
path,
...(connectionId != null ? { connectionId } : {}),
...(executionHostId != null ? { executionHostId } : {})
...(executionHostId != null ? { executionHostId } : {}),
...(projectGroupId != null ? { projectGroupId } : {})
})
)
}
Expand Down
16 changes: 13 additions & 3 deletions src/main/ssh/orcad-migration-delta-plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,19 @@ export function orcadDeltaSourceStore(
moved: readonly OrcadMigrationSourceCutover[]
): OrcadDeltaSourceStore {
const ids = orcadMigrationCatalogIds(moved.map((cutover) => cutover.manifest.payload))
// A linked project is the server's already, so a delta never moves it either.
// A linked project or group is the server's already, so a delta never moves it either.
const linked = moved.flatMap((cutover) => cutover.linkedRepositoryIds ?? [])
const linkedGroups = moved.flatMap((cutover) => cutover.linkedProjectGroupIds ?? [])
// Why linked groups subtract like moved ones: a group a new row needs still moves, since the
// server may have deleted it; an unused one never reappears as this desktop's addition.
return orcadSourceStoreWithout(
store,
target,
{ ...ids, repositoryIds: new Set([...ids.repositoryIds, ...linked]) },
{
...ids,
repositoryIds: new Set([...ids.repositoryIds, ...linked]),
projectGroupIds: new Set([...ids.projectGroupIds, ...linkedGroups])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Keep a server-renamed group out of later delta exports.

If a later project is assigned to a linked group renamed on the server, treating that group only as a moved group lets the delta source include its stale desktop row when the project needs it. orcadSourceStoreWithout receives no serverGroupIds here, so the manifest export does not remove the row. The delta can then conflict with the server group and block the project move. Preserve the server record when it still exists, and handle a deleted server group separately.

},
moved.at(-1)?.destinationEnvironmentId
)
}
Expand All @@ -71,7 +78,9 @@ export function orcadSourceStoreWithout(
store: Store,
target: SshTarget,
owned: OrcadMigrationCatalogIds,
destinationEnvironmentId: string | undefined
destinationEnvironmentId: string | undefined,
/** Groups the server's record wins for: never exported, even while a moving row sits in one. */
serverGroupIds: ReadonlySet<string> = new Set()
): OrcadDeltaSourceStore {
const ownedNow = createOrcadMigrationManifest(store, target, {
destinationEnvironmentId,
Expand All @@ -80,6 +89,7 @@ export function orcadSourceStoreWithout(
const view = store.createOrcadMigrationDeltaView(ownedNow)
return {
...view,
serverOwnedProjectGroupIds: serverGroupIds,
getSshTarget: (id) => store.getSshTarget(id),
getSshRemotePtyLeases: (id) => store.getSshRemotePtyLeases(id),
readOrcadMigrationSourceSnapshotChunk: (...args) =>
Expand Down
14 changes: 12 additions & 2 deletions src/main/ssh/orcad-migration-manifest-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ export type OrcadMigrationExportStore = Pick<
| 'getFolderWorkspaces'
| 'getProjectGroups'
| 'getRepos'
>
> & {
/** Groups the server's record wins for: still read for row ownership, never exported. */
serverOwnedProjectGroupIds?: ReadonlySet<string>
}

export type OrcadMigrationManifestExportOptions = {
migrationId?: string
Expand All @@ -47,10 +50,17 @@ export function createOrcadMigrationManifest(
options: OrcadMigrationManifestExportOptions = {}
): OrcadMigrationManifest {
const rewrite = options.rewrite ?? IDENTITY_ORCAD_MIGRATION_PATH_REWRITER
const sourcePayload = restrictCatalog(
const collected = restrictCatalog(
collectOrcadMigrationSourceCatalog(store, target),
options.onlyCatalog
)
const serverOwned = store.serverOwnedProjectGroupIds
const sourcePayload = serverOwned
? {
...collected,
projectGroups: collected.projectGroups.filter((group) => !serverOwned.has(group.id))
}
: collected
const source = {
sshTargetId: target.id,
sshTargetGeneration: target.generation ?? null,
Expand Down
124 changes: 124 additions & 0 deletions src/main/ssh/orcad-migration-repository-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ vi.mock('./orcad-managed-tunnel', () => ({
}))

const { convertSshTargetToManagedOrcad } = await import('./orcad-runtime-conversion')
const { runOrcadDeltaMove } = await import('./orcad-migration-delta-move')
const { runTargetLifecycle } = await import('../ipc/ssh-target-lifecycle-queue')

const TARGET: SshTarget = {
id: 'ssh-shared',
Expand Down Expand Up @@ -363,6 +365,128 @@ describe('moving the host again after its server was stopped', () => {
)
})

function groupDesktopRepo(): string {
const group = store.createProjectGroup({
name: 'Before',
parentPath: '/root',
connectionId: TARGET.id,
createdFrom: 'manual'
})
store.moveProjectToGroup('desktop-repo', group.id)
return group.id
}
const groupName = (owner: Store, id: string) =>
owner.getProjectGroups().find((entry) => entry.id === id)?.name
// The delta move an older build's later edit asks for, against the real server store.
const runDelta = () => {
reconcileManagedOrcadSshTargets(userDataPath, store)
return runOrcadDeltaMove({
userDataPath,
store,
claims: targetStore.getOrcadRuntimeClaims(),
target: store.getSshTarget(TARGET.id)!,
environment: listEnvironments(userDataPath)[0]!,
destination,
censusHost: async () => ({ verdict: 'exited', count: 0 }),
ensureTunnel: async () => {},
runTargetLifecycle
})
}

// Review repro: the linked project's retained group was imported over the server's renamed one.
it('keeps the server’s project group when it changed while managed', async () => {
const groupId = groupDesktopRepo()
await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
server.updateProjectGroup(groupId, { name: 'Renamed on the server' })
stopServer()

await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
expect(groupName(server, groupId)).toBe('Renamed on the server')
expect(server.getRepo('desktop-repo')?.projectGroupId).toBe(groupId)
// This desktop's retained copy stays as it was, for a downgraded build.
expect(groupName(store, groupId)).toBe('Before')
})

it('moves a new project into a group the server holds in changed form', async () => {
const groupId = groupDesktopRepo()
await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
server.updateProjectGroup(groupId, { name: 'Renamed on the server' })
stopServer()
store.addRepo(repo('added-after-stop', '/root/later'))
store.moveProjectToGroup('added-after-stop', groupId)

await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
expect(server.getRepo('added-after-stop')?.projectGroupId).toBe(groupId)
expect(groupName(server, groupId)).toBe('Renamed on the server')
})

it('never offers the server’s group again in a later delta move', async () => {
const groupId = groupDesktopRepo()
await convert()
server.updateProjectGroup(groupId, { name: 'Renamed on the server' })
stopServer()
await convert()
store.addRepo(repo('added-later', '/root/later'))
reconcileManagedOrcadSshTargets(userDataPath, store)
const target = store.getSshTarget(TARGET.id)!
const plan = planOrcadDeltaMove(userDataPath, store, target)
expect(plan.added.map((row) => row.id)).toEqual(['added-later'])
})

// Codex repro: a nested group's moved parent was re-exported in a later delta.
it('keeps the server’s nested groups out of the relink and later deltas', async () => {
const parent = store.createProjectGroup({
name: 'Parent',
parentPath: '/root',
connectionId: TARGET.id,
createdFrom: 'manual'
})
const child = store.createProjectGroup({
name: 'Child',
parentPath: '/root',
parentGroupId: parent.id,
connectionId: TARGET.id,
createdFrom: 'manual'
})
store.moveProjectToGroup('desktop-repo', child.id)
await convert()
server.updateProjectGroup(parent.id, { name: 'Parent renamed on the server' })
stopServer()
await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
expect(groupName(server, parent.id)).toBe('Parent renamed on the server')

store.addRepo(repo('added-later', '/root/later'))
await expect(runDelta()).resolves.toMatchObject({ outcome: 'moved' })
expect(server.getRepo('added-later')).toBeDefined()
expect(groupName(server, parent.id)).toBe('Parent renamed on the server')
})

// Codex repro: hiding the group from the source also hid the host a folder takes from it.
it('still moves a later folder workspace that takes its host from the server’s group', async () => {
const groupId = groupDesktopRepo()
await convert()
stopServer()
await convert()
const folder = store.createFolderWorkspace({
projectGroupId: groupId,
folderPath: '/root/notes'
})
await expect(runDelta()).resolves.toMatchObject({ outcome: 'moved' })
expect(server.getFolderWorkspace(folder.id)?.projectGroupId).toBe(groupId)
})

// Codex repro: a group only a linked project used never reached the server, so it still moves.
it('moves a linked project’s group the server does not hold, so later rows can use it', async () => {
otherDesktopAddsSameCheckout()
const groupId = groupDesktopRepo()
await expect(convert()).resolves.toMatchObject({ outcome: 'converted' })
expect(groupName(server, groupId)).toBe('Before')
store.addRepo(repo('added-later', '/root/later'))
store.moveProjectToGroup('added-later', groupId)
await expect(runDelta()).resolves.toMatchObject({ outcome: 'moved' })
expect(server.getRepo('added-later')?.projectGroupId).toBe(groupId)
})

it('resumes, not relinks, a move the server committed before the reply was lost', async () => {
const commit = destination.commit
destination.commit = async (manifest) => {
Expand Down
76 changes: 65 additions & 11 deletions src/main/ssh/orcad-migration-repository-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import type { SshTargetOrcadClaims } from './ssh-target-orcad-claims'
export type OrcadServerRepository = Pick<Repo, 'id' | 'path'> & {
connectionId?: string | null
executionHostId?: string | null
/** Proves the server holds this group: its record, not this desktop's copy, wins on relink. */
projectGroupId?: string | null
}

/**
Expand Down Expand Up @@ -78,16 +80,56 @@ async function serverHeldRepositoriesToLink(
return state.state === 'committed' ? held.otherId : [...held.otherId, ...held.sameId]
}

/** The source the cutover was cut from: the real store less any project it linked. */
/**
* Source groups the server's projects sit in, and their ancestors, renamed there perhaps since:
* re-importing this desktop's retained copy would conflict with the server's. An ancestor the
* server may lack still moves while a moving row needs it.
*/
function serverHeldProjectGroups(
payload: OrcadMigrationSourceCutover['manifest']['payload'],
linkedRepositoryIds: ReadonlySet<string>,
server: readonly OrcadServerRepository[]
): string[] {
const parentById = new Map(payload.projectGroups.map((group) => [group.id, group.parentGroupId]))
const held = new Set(server.flatMap((repo) => (repo.projectGroupId ? [repo.projectGroupId] : [])))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

🔎 Supported by static analysis

🏁 Script executed:

set -eu
printf '%s\n' '--- changed file ---'
nl -ba src/main/ssh/orcad-migration-repository-link.ts | sed -n '1,240p'
printf '%s\n' '--- relevant symbols ---'
rg -n -F --glob '*.ts' -e 'projectGroupId' -e 'repo.list' -e 'projectGroup' src/main/ssh src/main/persistence/migrating-orcad-catalog || test "$?" -eq 1
printf '%s\n' '--- base to head diff ---'
git diff --stat 432c6d0735acb4752eb18aabed0a61455cad9ba8 0a87349964a181266caea786b6fb54f2d951d89e -- src/main/ssh/orcad-migration-repository-link.ts
git diff --unified=25 432c6d0735acb4752eb18aabed0a61455cad9ba 0a87349964a181266caea786b6fb54f2d951d89e -- src/main/ssh/orcad-migration-repository-link.ts
printf '%s\n' '--- catalog files ---'
rg --files src/main/ssh src/main/persistence/migrating-orcad-catalog | sort | sed -n '1,240p'

Repository: stablyai/orca

Length of output: 41831


🏁 Script executed:

set -eu
printf '%s\n' '--- catalog client contract ---'
nl -ba src/main/ssh/orcad-migration-catalog-client.ts | sed -n '130,205p'
printf '%s\n' '--- source catalog ---'
nl -ba src/main/persistence/migrating-orcad-catalog/orcad-source-catalog.ts
printf '%s\n' '--- group-related source declarations ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' -e 'listProjectGroups' -e 'getProjectGroups' -e 'projectGroups' -e 'project-group' -e 'projectGroup' src/main src/shared | sed -n '1,320p'
printf '%s\n' '--- all repo.list references ---'
rg -n -F --glob '*.ts' -e 'repo.list' -e 'repoList' src | sed -n '1,240p'

Repository: stablyai/orca

Length of output: 41334


🏁 Script executed:

set -eu
printf '%s\n' '--- project-group RPC and handlers ---'
rg --files src/main/runtime/rpc/methods src/main/ipc/repos | rg 'project.group|project-group|repo\.ts|folder-workspace'
for f in src/main/runtime/rpc/methods/project-group.ts src/main/ipc/repos/project-group-handlers.ts src/main/runtime/rpc/methods/repo.ts; do
  if test -f "$f"; then
    printf '\n--- %s ---\n' "$f"
    nl -ba "$f" | sed -n '1,260p'
  fi
done
printf '%s\n' '--- project-group and move persistence symbols ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' -e 'moveProjectToGroup' -e 'deleteProjectGroup' -e 'deleteProject' -e 'projectGroup.list' -e 'projectGroup.create' -e 'projectGroup.update' src/main src/shared | sed -n '1,280p'
printf '%s\n' '--- project group types ---'
nl -ba src/shared/project-group-types.ts | sed -n '1,180p'

Repository: stablyai/orca

Length of output: 22418


🏁 Script executed:

set -eu
printf '%s\n' '--- project-group persistence operations ---'
nl -ba src/main/persistence/tracking-repos/project-group-operations.ts | sed -n '1,190p'
printf '%s\n' '--- runtime project-group controller ---'
nl -ba src/main/runtime/runtime-project-group-controller.ts | sed -n '1,210p'
printf '%s\n' '--- runtime visibility implementation ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' -e 'listVisibleProjectGroups' -e 'visibleProjectGroups' src/main | sed -n '1,160p'

Repository: stablyai/orca

Length of output: 15151


🏁 Script executed:

set -eu
printf '%s\n' '--- retained-source visibility ---'
nl -ba src/main/ssh/orcad-retained-source.ts | sed -n '90,145p'
printf '%s\n' '--- runtime visible catalog ---'
nl -ba src/main/runtime/runtime-visible-catalog.ts | sed -n '1,100p'
printf '%s\n' '--- moveProjectToGroup implementation ---'
nl -ba src/main/persistence/restoring-sessions/folder-workspace-operations.ts | sed -n '235,285p'
printf '%s\n' '--- projectGroup.list client consumers ---'
rg -n -F --glob '*.ts' --glob '*.tsx' -e 'projectGroup.list' src/main src/renderer src/shared | sed -n '1,180p'

Repository: stablyai/orca

Length of output: 11602


🏁 Script executed:

set -eu
printf '%s\n' '--- destination group import and duplicate handling ---'
nl -ba src/main/persistence/migrating-orcad-catalog/orcad-catalog-records.ts | sed -n '1,135p'
nl -ba src/main/persistence/migrating-orcad-catalog/orcad-catalog-records.ts | sed -n '145,190p'
printf '%s\n' '--- migration manifest export filtering ---'
nl -ba src/main/ssh/orcad-migration-manifest-export.ts | sed -n '35,78p'

Repository: stablyai/orca

Length of output: 9450


Include standalone server groups when relinking.

moveProjectToGroup does not delete an empty group. projectGroup.list can still return it. The reconnect code only builds held from repo.list rows, so it can re-export the desktop's stale group and trigger orcad_migration_project_group_id_conflict:<id> when the server already owns that ID. Fetch projectGroup.list with repo.list and include its group IDs when selecting server-owned groups.

const withAncestors = (ids: (string | null | undefined)[], stopAt: ReadonlySet<string>) => {
const out = new Set<string>()
while (ids.length > 0) {
const id = ids.pop()
if (id && !out.has(id) && !stopAt.has(id) && parentById.has(id)) {
out.add(id)
ids.push(parentById.get(id))
}
}
return out
}
const candidates = withAncestors([...held], new Set())
const needed = withAncestors(
[
...payload.repositories
.filter((repo) => !linkedRepositoryIds.has(repo.id))
.map((repo) => repo.projectGroupId),
...payload.folderWorkspaces.map((workspace) => workspace.projectGroupId),
...payload.projectGroups
.filter((group) => !candidates.has(group.id))
.map((group) => group.parentGroupId)
],
held
)
return [...candidates].filter((id) => !needed.has(id))
}

/** The source the cutover was cut from: the real store less any project or group it linked. */
export function linkedOrcadMigrationSource(
store: Store,
cutover: Pick<
OrcadMigrationSourceCutover,
'sshTargetId' | 'destinationEnvironmentId' | 'linkedRepositoryIds'
'sshTargetId' | 'destinationEnvironmentId' | 'linkedRepositoryIds' | 'linkedProjectGroupIds'
>
): (() => OrcadDeltaSourceStore) | undefined {
const linked = cutover.linkedRepositoryIds ?? []
if (linked.length === 0) {
const linkedGroups = cutover.linkedProjectGroupIds ?? []
if (linked.length === 0 && linkedGroups.length === 0) {
return undefined
}
return () => {
Expand All @@ -103,7 +145,8 @@ export function linkedOrcadMigrationSource(
folderWorkspaceIds: new Set(),
projectGroupIds: new Set()
},
cutover.destinationEnvironmentId
cutover.destinationEnvironmentId,
new Set(linkedGroups)
)
}
}
Expand Down Expand Up @@ -190,11 +233,8 @@ export async function linkServerHeldRepositories(args: {
if (cutover.phase !== 'source-fenced' && cutover.phase !== 'destination-staged') {
return cutover
}
const linked = await serverHeldRepositoriesToLink(
cutover,
args.destination,
await args.serverRepositories()
)
const serverRepositories = await args.serverRepositories()
const linked = await serverHeldRepositoriesToLink(cutover, args.destination, serverRepositories)
if (linked.length === 0) {
return cutover
}
Expand All @@ -203,7 +243,21 @@ export async function linkServerHeldRepositories(args: {
// The relinked manifest is cut from the source as fenced, so it must still be that source.
source.verifyFence(cutover)
const linkedRepositoryIds = [...new Set([...(cutover.linkedRepositoryIds ?? []), ...linked])]
const view = linkedOrcadMigrationSource(args.store, { ...cutover, linkedRepositoryIds })?.()
const linkedProjectGroupIds = [
...new Set([
...(cutover.linkedProjectGroupIds ?? []),
...serverHeldProjectGroups(
cutover.manifest.payload,
new Set(linkedRepositoryIds),
serverRepositories
)
])
]
const linkedIds = {
linkedRepositoryIds,
...(linkedProjectGroupIds.length > 0 ? { linkedProjectGroupIds } : {})
}
const view = linkedOrcadMigrationSource(args.store, { ...cutover, ...linkedIds })?.()
const target = args.store.getSshTarget(cutover.sshTargetId)
if (!view || !target) {
throw new Error('orcad_migration_source_fence_lost')
Expand All @@ -219,7 +273,7 @@ export async function linkServerHeldRepositories(args: {
updatedAt: (args.now ?? (() => new Date()))().toISOString(),
manifestSha256: manifest.manifestSha256,
manifest,
linkedRepositoryIds,
...linkedIds,
// The retained rows still hold the linked projects, so the baseline is the whole fenced source.
sourceBaselineFingerprint:
cutover.sourceBaselineFingerprint ?? orcadCatalogFingerprint(cutover.manifest.payload)
Expand Down
6 changes: 6 additions & 0 deletions src/shared/orcad-migration-source-cutover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
import { z } from 'zod'
import {
MAX_ORCAD_MIGRATION_PROJECT_GROUPS,
MAX_ORCAD_MIGRATION_REPOSITORIES,
parseOrcadMigrationManifest,
type OrcadMigrationManifest
Expand Down Expand Up @@ -53,6 +54,11 @@ const CutoverRecordSchema = z.object({
.array(z.string().min(1).max(1_024))
.max(MAX_ORCAD_MIGRATION_REPOSITORIES)
.optional(),
// Source project groups the server already holds, or only linked projects used: its record wins.
linkedProjectGroupIds: z
.array(z.string().min(1).max(1_024))
.max(MAX_ORCAD_MIGRATION_PROJECT_GROUPS)
.optional(),
manifest: z.unknown()
})

Expand Down
Loading