Files
orca/src/shared/task-source-context.ts
T
Neil 77f23b013f refactor(shared): drop the shared/types barrel and import from the real modules (#14447)
#14397 split `shared/types.ts` into 46 per-domain modules but kept the path as
a re-export barrel so the import sites did not have to change. This removes
the barrel: every consumer now imports from the module that actually declares
the type, and `src/shared/types.ts` is deleted.

Barrels hide where a type lives, make every consumer look like it depends on
the whole domain, and let an unrelated edit invalidate a module that ~2,000
files transitively import.

2,323 import declarations across 2,321 files. Rewritten mechanically: each
specifier was resolved to an absolute path via the TypeScript AST and
recomputed, rather than string-substituted, so alias forms (`@/../../shared/
types`) and per-specifier `type` modifiers survive.

Four cases the mechanical pass had to handle, each found by a gate rather than
by reading the diff:

- Modules inside `src/shared` import the barrel as `./types`, not
  `shared/types`. A pre-filter on the latter string skipped 176 of them and
  left imports dangling at a deleted file, which surfaced as confusing
  `Property 'x' is optional in type 'Repo' but required in Pick<Repo, ...>`
  errors rather than "module not found".
- The barrel RENAMED one type on the way through
  (`WorkspaceSource as WorkspaceCreateTelemetrySource`), so the original name
  in the owning module has to be re-aliased at each consumer.
- Three test files put `;(globalThis as ...)` on the line after the import.
  TypeScript parses that `;` as the import statement's terminator, so
  replacing through `statement.getEnd()` deletes it and breaks ASI. The
  rewrite now stops at the module specifier.
- A file that already imported directly from a module got a SECOND import
  from it, because the barrel re-exported those same names — which trips
  `import/no-duplicates` under `--deny-warnings`. A post-pass merges
  declarations sharing a specifier and type-only-ness; the `import type` plus
  `import` pair from one module is left alone, since that form is allowed.

Splitting one barrel import into several genuinely adds lines, which pushed
`terminal-layout-pty-ownership.ts` to 301 counted lines: its 107-character
import must wrap, and neither local type collapses onto one line (101 and 116
characters). Rather than contort a type declaration to fit a line budget,
`collectLeafIds` and `pruneLeaves` move to `terminal-pane-layout-tree.ts` —
they are pure structural operations on the layout tree and independent of PTY
ownership. `visible-worktrees.ts` similarly loses its own mini-barrel
re-export of `isDefaultBranchWorkspace`, with the four real consumers
repointed at the declaring module. No `max-lines` bypass added.

Verified: cold `tsc --noEmit` green on node, cli, and web (buildinfo deleted
first — these projects are `composite: true` and reuse stale caches); the full
`pnpm lint` green, not just bare oxlint — the narrower local check is what let
the duplicate imports reach CI; max-lines ratchet OK at 344.
2026-08-13 22:48:24 -07:00

236 lines
6.8 KiB
TypeScript

import {
LOCAL_EXECUTION_HOST_ID,
type ExecutionHostId,
normalizeExecutionHostId,
parseExecutionHostId,
toRuntimeExecutionHostId,
toSshExecutionHostId
} from './execution-host'
import {
areTaskProviderIdentitiesEqual,
isStoredTaskProviderIdentity,
normalizeTaskProviderIdentity,
taskProviderIdentityCachePart,
type TaskProviderIdentity
} from './task-provider-identity'
import type { TaskProvider } from './task-providers'
import type { GlobalSettings } from './global-settings-types'
import type { Repo } from './repo-types'
export type {
GitHubTaskProviderIdentity,
GitLabTaskProviderIdentity,
JiraTaskProviderIdentity,
LinearTaskProviderIdentity,
TaskProviderIdentity
} from './task-provider-identity'
export type { TaskProvider } from './task-providers'
export type TaskSourceContext = {
kind: 'task-source'
provider: TaskProvider
projectId: string
hostId: ExecutionHostId
projectHostSetupId?: string | null
repoId?: string | null
providerIdentity?: TaskProviderIdentity | null
accountLabel?: string | null
}
export type WorkspaceRunContext = {
kind: 'workspace-run'
projectId: string
hostId: ExecutionHostId
projectHostSetupId: string
repoId: string
path: string
}
export type TaskSourceContextInput = Omit<TaskSourceContext, 'kind' | 'hostId'> & {
kind?: 'task-source'
hostId?: string | null
}
export function normalizeTaskSourceContext(
input: TaskSourceContextInput
): TaskSourceContext | null {
const projectId = normalizeNonEmptyString(input.projectId)
if (!projectId) {
return null
}
const provider = normalizeTaskProvider(input.provider)
if (!provider) {
return null
}
return {
kind: 'task-source',
provider,
projectId,
hostId: normalizeExecutionHostId(input.hostId) ?? LOCAL_EXECUTION_HOST_ID,
projectHostSetupId: normalizeNonEmptyString(input.projectHostSetupId),
repoId: normalizeNonEmptyString(input.repoId),
providerIdentity: normalizeTaskProviderIdentity(provider, input.providerIdentity),
accountLabel: normalizeNonEmptyString(input.accountLabel)
}
}
export function normalizeStoredTaskSourceContext(value: unknown): TaskSourceContext | null {
if (!value || typeof value !== 'object') {
return null
}
const input = value as Record<string, unknown>
const provider = normalizeTaskProvider(input.provider)
if (
!provider ||
typeof input.projectId !== 'string' ||
(input.kind !== undefined && input.kind !== 'task-source') ||
!isNullableOptionalString(input.hostId) ||
!isNullableOptionalString(input.projectHostSetupId) ||
!isNullableOptionalString(input.repoId) ||
!isNullableOptionalString(input.accountLabel) ||
!isStoredTaskProviderIdentity(provider, input.providerIdentity)
) {
return null
}
if (
typeof input.hostId === 'string' &&
input.hostId.trim().length > 0 &&
normalizeExecutionHostId(input.hostId) === null
) {
return null
}
return normalizeTaskSourceContext(input as TaskSourceContextInput)
}
export function buildTaskSourceContextFromRepo(args: {
provider: TaskProvider
projectId: string
repo: Pick<Repo, 'id' | 'connectionId' | 'executionHostId'>
projectHostSetupId?: string | null
providerIdentity?: TaskProviderIdentity | null
accountLabel?: string | null
}): TaskSourceContext | null {
return normalizeTaskSourceContext({
provider: args.provider,
projectId: args.projectId,
hostId: getRepoHostId(args.repo),
repoId: args.repo.id,
projectHostSetupId: args.projectHostSetupId,
providerIdentity: args.providerIdentity,
accountLabel: args.accountLabel
})
}
export function areTaskSourceContextsEqual(
a: TaskSourceContext | null | undefined,
b: TaskSourceContext | null | undefined
): boolean {
if (a === b) {
return true
}
if (!a || !b) {
return !a && !b
}
return (
a.provider === b.provider &&
a.projectId === b.projectId &&
a.hostId === b.hostId &&
(a.projectHostSetupId ?? null) === (b.projectHostSetupId ?? null) &&
(a.repoId ?? null) === (b.repoId ?? null) &&
(a.accountLabel ?? null) === (b.accountLabel ?? null) &&
areTaskProviderIdentitiesEqual(a.providerIdentity, b.providerIdentity)
)
}
export function getTaskSourceRuntimeSettings(
context: Pick<TaskSourceContext, 'hostId'> | null | undefined
): Pick<GlobalSettings, 'activeRuntimeEnvironmentId'> {
const parsed = parseExecutionHostId(context?.hostId)
return {
activeRuntimeEnvironmentId: parsed?.kind === 'runtime' ? parsed.environmentId : null
}
}
export function getTaskSourceCacheScope(
context: Pick<TaskSourceContext, 'provider' | 'hostId' | 'projectId' | 'projectHostSetupId'> & {
providerIdentity?: TaskProviderIdentity | null
repoId?: string | null
}
): string {
return [
context.provider,
context.hostId,
context.projectId,
context.projectHostSetupId ?? '',
context.repoId ?? '',
taskProviderIdentityCachePart(context.providerIdentity)
]
.map(encodeCachePart)
.join(':')
}
export function buildWorkspaceRunContext(args: {
projectId: string
hostId: string | null | undefined
projectHostSetupId: string
repoId: string
path: string
}): WorkspaceRunContext | null {
const projectId = normalizeNonEmptyString(args.projectId)
const projectHostSetupId = normalizeNonEmptyString(args.projectHostSetupId)
const repoId = normalizeNonEmptyString(args.repoId)
const repoPath = normalizeNonEmptyString(args.path)
if (!projectId || !projectHostSetupId || !repoId || !repoPath) {
return null
}
return {
kind: 'workspace-run',
projectId,
hostId: normalizeExecutionHostId(args.hostId) ?? LOCAL_EXECUTION_HOST_ID,
projectHostSetupId,
repoId,
path: repoPath
}
}
function getRepoHostId(repo: Pick<Repo, 'connectionId' | 'executionHostId'>): ExecutionHostId {
const explicit = normalizeExecutionHostId(repo.executionHostId)
if (explicit) {
return explicit
}
const connectionId = normalizeNonEmptyString(repo.connectionId)
return connectionId ? toSshExecutionHostId(connectionId) : LOCAL_EXECUTION_HOST_ID
}
function normalizeTaskProvider(value: unknown): TaskProvider | null {
switch (value) {
case 'github':
case 'gitlab':
case 'linear':
case 'jira':
return value
default:
return null
}
}
function normalizeNonEmptyString(value: unknown): string | null {
const trimmed = typeof value === 'string' ? value.trim() : ''
return trimmed ? trimmed : null
}
function isNullableOptionalString(value: unknown): boolean {
return value === undefined || value === null || typeof value === 'string'
}
function encodeCachePart(value: string): string {
return encodeURIComponent(value)
}
export function runtimeHostIdFromEnvironmentId(
environmentId: string | null | undefined
): ExecutionHostId {
const trimmed = normalizeNonEmptyString(environmentId)
return trimmed ? toRuntimeExecutionHostId(trimmed) : LOCAL_EXECUTION_HOST_ID
}