Files
orca/src/relay/git-status-upstream-negative-cache.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

186 lines
6.1 KiB
TypeScript

import { createGitConfigSnapshotRunner } from '../shared/git-config-snapshot-runner'
import { getEffectiveGitUpstreamStatus } from '../shared/git-effective-upstream'
import type { GitCommandRunner } from '../shared/git-effective-upstream'
import type { GitUpstreamStatus } from '../shared/git-status-types'
const NO_EFFECTIVE_UPSTREAM_CACHE_TTL_MS = 5 * 60_000
const MAX_NO_EFFECTIVE_UPSTREAM_CACHE_ENTRIES = 512
type NoEffectiveUpstreamCacheIdentity = {
worktreePath: string
branchName: string
upstreamName?: string
}
type NoEffectiveUpstreamCacheEntry = {
status: GitUpstreamStatus
expiresAt: number
}
const noEffectiveUpstreamByIdentity = new Map<string, NoEffectiveUpstreamCacheEntry>()
const noEffectiveUpstreamInFlight = new Map<string, Promise<GitUpstreamStatus>>()
const retiredNoEffectiveUpstreamInFlight = new Map<string, Promise<GitUpstreamStatus>>()
const noEffectiveUpstreamWriteGeneration = new Map<string, number>()
function noEffectiveUpstreamCacheKey(identity: NoEffectiveUpstreamCacheIdentity): string {
return [identity.worktreePath, identity.branchName, identity.upstreamName ?? ''].join('\0')
}
function readCachedNoEffectiveUpstreamStatus(
cacheKey: string,
nowMs = Date.now()
): GitUpstreamStatus | null {
const entry = noEffectiveUpstreamByIdentity.get(cacheKey)
if (!entry) {
return null
}
if (entry.expiresAt <= nowMs) {
noEffectiveUpstreamByIdentity.delete(cacheKey)
return null
}
return entry.status
}
function hasPendingNoEffectiveUpstreamProbe(cacheKey: string): boolean {
return (
noEffectiveUpstreamInFlight.has(cacheKey) || retiredNoEffectiveUpstreamInFlight.has(cacheKey)
)
}
function trimNoEffectiveUpstreamWriteGeneration(): void {
for (const cacheKey of noEffectiveUpstreamWriteGeneration.keys()) {
if (noEffectiveUpstreamWriteGeneration.size <= MAX_NO_EFFECTIVE_UPSTREAM_CACHE_ENTRIES) {
break
}
if (hasPendingNoEffectiveUpstreamProbe(cacheKey)) {
continue
}
noEffectiveUpstreamWriteGeneration.delete(cacheKey)
}
}
function cacheNoEffectiveUpstreamStatus(
cacheKey: string,
status: GitUpstreamStatus,
probedSameNameOriginRef: boolean,
writeGeneration: number,
nowMs = Date.now()
): void {
// Why: hasConfiguredPushTarget controls publish behavior; keep that signal
// fresh rather than serving a stale positive from status polling.
if (status.hasUpstream || status.hasConfiguredPushTarget) {
noEffectiveUpstreamByIdentity.delete(cacheKey)
noEffectiveUpstreamWriteGeneration.set(cacheKey, writeGeneration + 1)
trimNoEffectiveUpstreamWriteGeneration()
return
}
if ((noEffectiveUpstreamWriteGeneration.get(cacheKey) ?? 0) !== writeGeneration) {
return
}
// Why: only cache negatives after probing origin/<branch>; other resolution
// paths can fail without proving the same-name publish branch is absent.
if (!probedSameNameOriginRef) {
return
}
noEffectiveUpstreamByIdentity.set(cacheKey, {
status,
expiresAt: nowMs + NO_EFFECTIVE_UPSTREAM_CACHE_TTL_MS
})
while (noEffectiveUpstreamByIdentity.size > MAX_NO_EFFECTIVE_UPSTREAM_CACHE_ENTRIES) {
const oldest = noEffectiveUpstreamByIdentity.keys().next()
if (oldest.done) {
break
}
noEffectiveUpstreamByIdentity.delete(oldest.value)
noEffectiveUpstreamWriteGeneration.delete(oldest.value)
}
trimNoEffectiveUpstreamWriteGeneration()
}
export async function readOrProbeNoEffectiveUpstreamStatus(
identity: NoEffectiveUpstreamCacheIdentity,
runGit: GitCommandRunner,
options: { bypassCache?: boolean } = {}
): Promise<GitUpstreamStatus> {
const cacheKey = noEffectiveUpstreamCacheKey(identity)
if (options.bypassCache !== true) {
const cachedStatus = readCachedNoEffectiveUpstreamStatus(cacheKey)
if (cachedStatus) {
return cachedStatus
}
const inFlight = noEffectiveUpstreamInFlight.get(cacheKey)
if (inFlight) {
return inFlight
}
}
let probedSameNameOriginRef = false
const snapshotRunner = createGitConfigSnapshotRunner(runGit)
const writeGeneration = noEffectiveUpstreamWriteGeneration.get(cacheKey) ?? 0
const probe = getEffectiveGitUpstreamStatus((args) => {
if (args[0] === 'rev-parse' && args.includes(`refs/remotes/origin/${identity.branchName}`)) {
probedSameNameOriginRef = true
}
return snapshotRunner(args)
}).then((status) => {
cacheNoEffectiveUpstreamStatus(cacheKey, status, probedSameNameOriginRef, writeGeneration)
return status
})
if (options.bypassCache !== true) {
noEffectiveUpstreamInFlight.set(cacheKey, probe)
}
try {
return await probe
} finally {
if (noEffectiveUpstreamInFlight.get(cacheKey) === probe) {
noEffectiveUpstreamInFlight.delete(cacheKey)
trimNoEffectiveUpstreamWriteGeneration()
}
}
}
export function clearNoEffectiveUpstreamStatusCache(): void {
noEffectiveUpstreamByIdentity.clear()
noEffectiveUpstreamInFlight.clear()
retiredNoEffectiveUpstreamInFlight.clear()
noEffectiveUpstreamWriteGeneration.clear()
}
export function clearNoEffectiveUpstreamStatusCacheEntry(
identity: NoEffectiveUpstreamCacheIdentity
): void {
const cacheKey = noEffectiveUpstreamCacheKey(identity)
retireNoEffectiveUpstreamProbe(cacheKey)
noEffectiveUpstreamByIdentity.delete(cacheKey)
noEffectiveUpstreamInFlight.delete(cacheKey)
noEffectiveUpstreamWriteGeneration.set(
cacheKey,
(noEffectiveUpstreamWriteGeneration.get(cacheKey) ?? 0) + 1
)
}
function retireNoEffectiveUpstreamProbe(cacheKey: string): void {
const retiredProbe = noEffectiveUpstreamInFlight.get(cacheKey)
if (!retiredProbe) {
return
}
retiredNoEffectiveUpstreamInFlight.set(cacheKey, retiredProbe)
void retiredProbe
.finally(() => {
if (retiredNoEffectiveUpstreamInFlight.get(cacheKey) === retiredProbe) {
retiredNoEffectiveUpstreamInFlight.delete(cacheKey)
trimNoEffectiveUpstreamWriteGeneration()
}
})
.catch(() => undefined)
}
export function getNoEffectiveUpstreamStatusCacheCountForTests(): number {
return noEffectiveUpstreamByIdentity.size
}
export function getNoEffectiveUpstreamStatusGenerationCountForTests(): number {
return noEffectiveUpstreamWriteGeneration.size
}