Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

4.4 KB
Raw Blame
// Check load/save idempotence: a second load must change nothing. Known first-load kinds are broad; return per-kind counts for logging so new normalizations remain visible.

import type { ExecutionHostId } from '../execution-host'
import type { WorkspaceSessionState } from '../workspace-session-state-types'
import { loadWorkspaceLayout } from './workspace-layout-load'
import { nameBasedLoadContext } from './workspace-layout-minted-ids'
import { stableJson } from './workspace-layout-load-report'
import { saveWorkspaceLayout } from './workspace-layout-save'

const ORDER = ['sortOrder', '$order']
const TAB_FACTS = [
  'createdAt',
  'color',
  'aiVaultTitle',
  'quickCommandLabel',
  'isPinned',
  'viewMode',
  'worktreeId'
]

/** Known Loader repairs: canonical ordering/ownership, row/label/preview precedence, unique pane IDs and PTY bindings, and legacy retirements. */
export const KNOWN_LOAD_CHANGES: ReadonlySet<string> = new Set([
  ...['row', 'terminalEntry', 'entry', 'group', 'layout', 'sleeping', 'incarnation', 'file'].map(
    (table) => `${table}.*`
  ),
  ...['row', 'terminalEntry', 'entry', 'group'].flatMap((table) =>
    ORDER.map((field) => `${table}.${field}`)
  ),
  'group.tabOrder',
  'group.worktreeId',
  'group.activeTabId',
  'group.recentTabIds',
  'row.title',
  'row.ptyId',
  // Transient handoffs older builds' minimal row mint stored; never restored.
  'row.pendingActivationSpawn',
  'row.recovery',
  'row.customTitle',
  'row.generatedTitle',
  ...TAB_FACTS.flatMap((field) => [`row.${field}`, `terminalEntry.${field}`, `entry.${field}`]),
  'terminalEntry.label',
  'terminalEntry.customLabel',
  'terminalEntry.generatedLabel',
  'terminalEntry.executionHostId',
  'terminalEntry.groupId',
  'entry.executionHostId',
  'entry.groupId',
  'entry.isPreview',
  'file.isPreview',
  'file.worktreeId',
  'browser.worktreeId',
  'sleeping.worktreeId',
  'closedTab.worktreeId',
  ...[
    'root',
    'activeLeafId',
    'expandedLeafId',
    'chatLeafId',
    'ptyIdsByLeafId',
    'titlesByLeafId',
    'lastGridsByLeafId',
    'buffersByLeafId',
    'scrollbackRefsByLeafId'
  ].map((field) => `layout.${field}`),
  ...[
    'unifiedTabs',
    'tabGroups',
    'tabGroupLayouts',
    'tabsByWorktree',
    'terminalSurfaceTombstonesByPaneKey',
    'terminalTopologyRevisionByRepoId',
    'terminalPtyIncarnationsByPaneKey',
    'sleepingAgentSessionsByPaneKey'
  ].map((field) => `session.${field}`)
])

export type LayoutRoundTripFinding =
  /** A first-load change no Loader rule produces. */
  | { kind: 'unknown_change'; change: string }
  /** Loading what was just saved changed it again. */
  | { kind: 'not_idempotent'; change: string }
  | { kind: 'threw'; message: string }

const kinds = (changes: readonly { table: string; field: string }[]) => [
  ...new Set(changes.map((change) => `${change.table}.${change.field}`))
]

export type LayoutRoundTripResult = {
  findings: LayoutRoundTripFinding[]
  /** `table.field` → how many values the first load changed. */
  firstLoadChanges: Record<string, number>
}

/** Loader then Serializer, twice: names and kinds only, never values. */
export function checkLayoutRoundTrip(
  hostId: ExecutionHostId,
  session: WorkspaceSessionState
): LayoutRoundTripResult {
  const firstLoadChanges: Record<string, number> = {}
  try {
    const first = loadWorkspaceLayout(hostId, session, nameBasedLoadContext())
    for (const change of first.changes) {
      const kind = `${change.table}.${change.field}`
      firstLoadChanges[kind] = (firstLoadChanges[kind] ?? 0) + 1
    }
    const findings: LayoutRoundTripFinding[] = kinds(first.changes)
      .filter((change) => !KNOWN_LOAD_CHANGES.has(change))
      .map((change) => ({ kind: 'unknown_change', change }))
    const saved = saveWorkspaceLayout(first)
    const second = loadWorkspaceLayout(hostId, saved, nameBasedLoadContext())
    findings.push(
      ...kinds(second.changes).map((change): LayoutRoundTripFinding => ({
        kind: 'not_idempotent',
        change
      }))
    )
    if (
      second.changes.length === 0 &&
      stableJson(saveWorkspaceLayout(second)) !== stableJson(saved)
    ) {
      findings.push({ kind: 'not_idempotent', change: 'saved document' })
    }
    return { findings, firstLoadChanges }
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error)
    return { findings: [{ kind: 'threw', message }], firstLoadChanges }
  }
}