diff --git a/src/main/github/github-issue-link-title-fill.ts b/src/main/github/github-issue-link-title-fill.ts index adda9a14202..2beede2b6f4 100644 --- a/src/main/github/github-issue-link-title-fill.ts +++ b/src/main/github/github-issue-link-title-fill.ts @@ -24,6 +24,8 @@ export type GitHubIssueLinkTitleFillStore = { getRepos: () => Repo[] } +const NOTIFY_WINDOW_MS = 2_000 + type Job = { worktreeId: string hostId: ExecutionHostId @@ -53,31 +55,46 @@ export function installGitHubIssueLinkTitleFill(deps: { let draining = false let disposed = false - const apply = (job: Job, title: string): void => { + const pendingNotify = new Set() + let lastNotifyAt = 0 + const flushNotify = (): void => { + lastNotifyAt = Date.now() + for (const repoId of pendingNotify) { + deps.notifyChanged(repoId) + } + pendingNotify.clear() + } + + // The job's link as it is now; null once removed, retitled or its workspace re-created. + const currentTarget = (job: Job) => { const meta = deps.store.getWorktreeMetaForHost(job.worktreeId, job.hostId) if (!meta || meta.instanceId !== job.instanceId) { - return + return null } const items = getWorkspaceAttachments(meta) const index = items.findIndex( (item) => getWorkspaceReferenceIdentity(item) === job.identity && !item.title?.trim() ) - if (index === -1) { + return index === -1 ? null : { meta, items, index } + } + + const apply = (job: Job, title: string): void => { + const target = currentTarget(job) + if (!target) { return } - // Restate the current review selection (null included) so the merge cannot re-pick one. - const reviewSelection = Object.fromEntries( - WORKSPACE_ATTACHMENT_NUMBER_SLOTS.filter(([, , type]) => type !== 'issue').map(([slot]) => [ - slot, - meta[slot] ?? null - ]) - ) + const { meta, items, index } = target + const reviewSlots = WORKSPACE_ATTACHMENT_NUMBER_SLOTS.filter(([, , type]) => type !== 'issue') + // A cleared review selection must be restated or the merge re-picks one; a set one it keeps. + const keepClearedSelection = reviewSlots.every(([slot]) => (meta[slot] ?? null) === null) + ? Object.fromEntries(reviewSlots.map(([slot]) => [slot, null])) + : {} deps.store.setWorktreeMeta(hostWorktreeTarget({ id: job.worktreeId, hostId: job.hostId }), { - ...reviewSelection, + ...keepClearedSelection, linkedItemsBase: items, linkedItems: items.map((item, i) => (i === index ? { ...item, title } : item)) }) - deps.notifyChanged(getRepoIdFromWorktreeId(job.worktreeId)) + pendingNotify.add(getRepoIdFromWorktreeId(job.worktreeId)) } const drain = async (): Promise => { @@ -91,11 +108,13 @@ export function installGitHubIssueLinkTitleFill(deps: { if (disposed) { return } - const repo = findRepoRowForHostScopedWrite( - deps.store.getRepos(), - getRepoIdFromWorktreeId(job.worktreeId), - job.hostId - ) + const repo = currentTarget(job) + ? findRepoRowForHostScopedWrite( + deps.store.getRepos(), + getRepoIdFromWorktreeId(job.worktreeId), + job.hostId + ) + : undefined if (!repo) { continue } @@ -103,13 +122,30 @@ export function installGitHubIssueLinkTitleFill(deps: { if (title?.trim() && !disposed) { apply(job, title.trim()) } + // One client refresh per repo per window during a burst, not one per filled title. + if (Date.now() - lastNotifyAt >= NOTIFY_WINDOW_MS) { + flushNotify() + } } } finally { draining = false + if (!disposed) { + flushNotify() + } } } const unsubscribe = deps.store.onWorktreeMetaWritten(({ worktreeId, hostId, previous, next }) => { + // Most writes (stamps, flags, comments) leave links untouched; skip them without normalizing. + if ( + previous && + previous.instanceId === next.instanceId && + previous.linkedItems === next.linkedItems && + previous.linkedIssue === next.linkedIssue && + previous.linkedWorkItem === next.linkedWorkItem + ) { + return + } const known = new Set( previous?.instanceId === next.instanceId ? untitledGitHubIssues(previous).map(getWorkspaceReferenceIdentity) diff --git a/src/main/github/github-issue-link-title-wiring.ts b/src/main/github/github-issue-link-title-wiring.ts index 8c3900bf3dc..e666220ca9d 100644 --- a/src/main/github/github-issue-link-title-wiring.ts +++ b/src/main/github/github-issue-link-title-wiring.ts @@ -6,6 +6,7 @@ import { getLocalProjectGhExecOptions } from '../workspaces/execution-host/proje import { getIssue } from './issues' import { getWorkItemByOwnerRepo } from './client/fetch/get-work-item' import { installGitHubIssueLinkTitleFill } from './github-issue-link-title-fill' +import { readGitHubRepoUnlessWslStopped } from '../ipc/source-control/github-repo-routing' const GITHUB_ISSUE_PATH = /^\/([^/]+)\/([^/]+)\/issues\/(\d+)$/ @@ -58,12 +59,27 @@ async function readGitHubIssueLinkTitle( export function wireGitHubIssueLinkTitleFill( store: Store, - notifyWorktreesChanged: (repoId: string, options: { invalidateGitScans: false }) => void + runtime: { + invalidateResolvedWorktreeCache: () => void + notifyWorktreesChanged: (repoId: string, options: { invalidateGitScans: false }) => void + } ): () => void { return installGitHubIssueLinkTitleFill({ store, - readIssueTitle: (repo, link) => readGitHubIssueLinkTitle(store, repo, link), - // Metadata-only change: publish it without forcing Git rescans. - notifyChanged: (repoId) => notifyWorktreesChanged(repoId, { invalidateGitScans: false }) + // Background reads never start a stopped WSL distro; the link just stays untitled. + readIssueTitle: (repo, link) => + readGitHubRepoUnlessWslStopped( + store, + repo, + () => readGitHubIssueLinkTitle(store, repo, link), + { + wake: false + } + ), + notifyChanged: (repoId) => { + // Metadata-only change: refresh resolved rows for remote clients without forcing Git rescans. + runtime.invalidateResolvedWorktreeCache() + runtime.notifyWorktreesChanged(repoId, { invalidateGitScans: false }) + } }) } diff --git a/src/main/orcad/orcad-entry.ts b/src/main/orcad/orcad-entry.ts index 40357e25b0e..da51c916fe2 100644 --- a/src/main/orcad/orcad-entry.ts +++ b/src/main/orcad/orcad-entry.ts @@ -227,11 +227,7 @@ async function startOrcadRuntime( accountLaunch?.attachToRuntime(runtime) const { wireGitHubIssueLinkTitleFill } = await import('../github/github-issue-link-title-wiring') - registerCleanup( - wireGitHubIssueLinkTitleFill(profileStore, (repoId, options) => - runtime.notifyWorktreesChanged(repoId, options) - ) - ) + registerCleanup(wireGitHubIssueLinkTitleFill(profileStore, runtime)) const { installOrcadEmulatorBridge } = await import('./orcad-emulator-bridge') installOrcadEmulatorBridge(runtime, registerCleanup) diff --git a/src/main/persistence/loading-store/worktree-identity-metadata.ts b/src/main/persistence/loading-store/worktree-identity-metadata.ts index e3a48038fa0..5b2c74cf061 100644 --- a/src/main/persistence/loading-store/worktree-identity-metadata.ts +++ b/src/main/persistence/loading-store/worktree-identity-metadata.ts @@ -267,7 +267,12 @@ export function setWorktreeMetaForHost( } scheduleSave(scheduling, WORKTREE_METADATA_DOMAINS) for (const listener of runtime.worktreeMetaWriteListeners ?? []) { - listener({ worktreeId, hostId: executionHostId, previous: existing, next: updated }) + try { + listener({ worktreeId, hostId: executionHostId, previous: existing, next: updated }) + } catch (error) { + // The row is already written; an observer's failure must not report the write as failed. + console.error('[worktree-meta] write listener failed:', error) + } } return updated } diff --git a/src/main/startup/main-process-runtime-service.ts b/src/main/startup/main-process-runtime-service.ts index 45d8562cbcf..0306a69d3b1 100644 --- a/src/main/startup/main-process-runtime-service.ts +++ b/src/main/startup/main-process-runtime-service.ts @@ -161,9 +161,7 @@ export function initializeMainProcessRuntime(): OrcaRuntimeService { ) wireRecipeWorkspaceRemoval(runtime, store, () => app.getPath('userData')) // Each host titles its own workspaces' untitled GitHub issue links once, when they are linked. - const disposeIssueLinkTitleFill = wireGitHubIssueLinkTitleFill(store, (repoId, options) => - runtime.notifyWorktreesChanged(repoId, options) - ) + const disposeIssueLinkTitleFill = wireGitHubIssueLinkTitleFill(store, runtime) app.once('will-quit', disposeIssueLinkTitleFill) state.runtime = runtime // Opening a recipe VM's workspace from a client wakes the VM through the runtime's one owner.