Files
orca/src/main/runtime/runtime-worktree-ps-activity.ts
T
Kelvin Amoaba 3347d47a7d fix(mobile): use desktop's PR lanes and status-dot rollup in the workspace list (#33966)
* fix(mobile): ask before running orca.yaml default-tab commands from the phone

The host's repo.hooks reply now carries desktop's setup config, so the phone
gates the Run/Skip choice and the trust prompt on defaultTabs commands too.

* fix(setup): keep the local script in run-both setup config without orca.yaml commands

* test(setup): drop the shared duplicate-host trust test desktop already covers

* refactor(setup): derive host setup trust from the shared setup config

* fix(mobile): count every host's repo rows before honouring repo-wide setup trust

* fix(mobile): show desktop's PR lanes and failed/interrupted status dots on the phone

The host builds worktree.ps linkedPR with the desktop's PR-lane cache reader
and sends the desktop sidebar dot as an optional sidebarStatus field.

* fix(mobile): keep uncached PR badges and let finished turns show through working titles

* fix(mobile): suppress agent panes' permission and parent titles in the host status dot

* test(runtime): expect sidebarStatus on the compact ps summary

* test(runtime): keep only host-specific sidebar status cases

* refactor(mobile): keep host title suppression and the uncached PR lane in one place each

* test(wire): pass the terminal title evidence map to new host builds

* fix(runtime): read each workspace's PR through its own host's repo
2026-10-27 03:20:58 +00:00

265 lines
9.6 KiB
TypeScript

import type { RuntimeSyncedTab, RuntimeWorktreePsSummary } from '../../shared/runtime-types'
import type { Repo } from '../../shared/repo-types'
import type { WorkspaceSessionState } from '../../shared/workspace-session-state-types'
import { getRepoExecutionHostId, type ExecutionHostId } from '../../shared/execution-host'
import { parsePaneKey } from '../../shared/stable-pane-id'
import { getWorkspaceSessionTabActivityIds } from '../../shared/workspace-session-tab-activity'
import type { RuntimeStore } from './runtime-store-contract'
import type { RuntimeLeafRecord, RuntimePtyWorktreeRecord } from './runtime-terminal-state-records'
import type { RuntimeWorktreePsSummaryLookup } from './runtime-worktree-summary-paths'
import {
getLatestPtyTitle,
getLeafDisplayRecord,
getLeafWorktreeStatus,
getPtyDisplayRecord,
getSavedTabWorktreeStatus,
maxTimestamp,
mergeWorktreeSummaryStatus,
type TitleDisplayClear
} from './runtime-worktree-status-projection'
import { worktreeIdsEqual } from '../../shared/worktree/id'
import type { PtyPaneOf } from './terminal/surface/pty-pane-query'
/** A live terminal whose title reads working or needs-permission, kept per pane so agent rows can suppress it. */
export type RuntimeTerminalTitleEvidence = {
status: 'working' | 'permission'
paneKey: string | null
ptyId: string | null
tabId: string | null
}
export function applyRuntimeWorktreePsTerminalActivity(args: {
freshPtyLiveness: ReadonlySet<string> | null
/** Filled with every PTY this pass counts as live. */
countedPtyIds: Set<string>
leaves: Iterable<RuntimeLeafRecord>
ptysById: ReadonlyMap<string, RuntimePtyWorktreeRecord>
tabs: ReadonlyMap<string, RuntimeSyncedTab>
session: WorkspaceSessionState | null | undefined
getPaneKey: (leaf: RuntimeLeafRecord) => string
paneOf: PtyPaneOf
getTitleDisplayClear: (ptyId: string) => TitleDisplayClear | null
getSummary: RuntimeWorktreePsSummaryLookup
}): Map<string, RuntimeTerminalTitleEvidence[]> {
const titleEvidence = new Map<string, RuntimeTerminalTitleEvidence[]>()
const savedTabOwnerById = new Map<string, { worktreeId: string; title: string }>()
for (const [worktreeId, tabs] of Object.entries(args.session?.tabsByWorktree ?? {})) {
for (const tab of tabs) {
savedTabOwnerById.set(tab.id, { worktreeId, title: tab.title })
}
}
const savedLayoutTabIdByPtyId = new Map<string, string>()
for (const [tabId, layout] of Object.entries(args.session?.terminalLayoutsByTabId ?? {})) {
for (const ptyId of Object.values(layout?.ptyIdsByLeafId ?? {})) {
if (ptyId) {
savedLayoutTabIdByPtyId.set(ptyId, tabId)
}
}
}
const countedPtyIds = args.countedPtyIds
for (const leaf of args.leaves) {
if (
!leaf.ptyId ||
!leaf.connected ||
(args.freshPtyLiveness !== null && !args.freshPtyLiveness.has(leaf.ptyId))
) {
continue
}
const freshOwner = args.ptysById.get(leaf.ptyId)
if (
args.freshPtyLiveness !== null &&
freshOwner?.connected &&
!worktreeIdsEqual(freshOwner.worktreeId, leaf.worktreeId)
) {
continue
}
const summary = args.getSummary(leaf.worktreeId)
if (!summary) {
continue
}
countedPtyIds.add(leaf.ptyId)
summary.hasHostSidebarActivity = true
const previousLastOutputAt = summary.lastOutputAt
summary.liveTerminalCount += 1
summary.hasAttachedPty = true
summary.lastOutputAt = maxTimestamp(summary.lastOutputAt, leaf.lastOutputAt)
const leafStatus = getLeafWorktreeStatus(
getLeafDisplayRecord(leaf, args.getTitleDisplayClear(leaf.ptyId)),
args.tabs.get(leaf.tabId)?.title ?? null
)
if (leafStatus === 'working' || leafStatus === 'permission') {
addTerminalTitleEvidence(titleEvidence, summary.worktreeId, {
status: leafStatus,
paneKey: args.getPaneKey(leaf),
ptyId: leaf.ptyId,
tabId: leaf.tabId
})
}
mergeWorktreeSummaryStatus(summary, leafStatus)
if (
leaf.preview &&
(summary.preview.length === 0 || (leaf.lastOutputAt ?? -1) >= (previousLastOutputAt ?? -1))
) {
summary.preview = leaf.preview
}
}
for (const pty of args.ptysById.values()) {
if (
!pty.connected ||
countedPtyIds.has(pty.ptyId) ||
(args.freshPtyLiveness !== null && !args.freshPtyLiveness.has(pty.ptyId))
) {
continue
}
const persistedTabId = savedLayoutTabIdByPtyId.get(pty.ptyId)
const displayTitle = getLatestPtyTitle(
getPtyDisplayRecord(pty, args.getTitleDisplayClear(pty.ptyId))
)
let owner = persistedTabId ? savedTabOwnerById.get(persistedTabId) : undefined
if (args.freshPtyLiveness !== null) {
owner = { worktreeId: pty.worktreeId, title: owner?.title ?? displayTitle ?? '' }
}
const recorded = args.paneOf(pty)
if (!owner && persistedTabId && recorded.tabId === persistedTabId) {
owner = { worktreeId: pty.worktreeId, title: displayTitle ?? '' }
}
const pane = parsePaneKey(recorded.paneKey ?? '')
const hasExplicitOwner =
recorded.tabId !== null && pane?.tabId === recorded.tabId && pane.leafId.length > 0
const savedOwner = recorded.tabId ? savedTabOwnerById.get(recorded.tabId) : undefined
const hasSavedLayout =
recorded.tabId !== null &&
Object.hasOwn(args.session?.terminalLayoutsByTabId ?? {}, recorded.tabId)
if (!owner && hasExplicitOwner && !hasSavedLayout) {
owner = {
worktreeId: savedOwner?.worktreeId ?? pty.worktreeId,
title: savedOwner?.title ?? displayTitle ?? ''
}
}
if (!owner) {
continue
}
const summary = args.getSummary(owner.worktreeId)
if (!summary) {
continue
}
countedPtyIds.add(pty.ptyId)
const previousLastOutputAt = summary.lastOutputAt
summary.liveTerminalCount += 1
summary.hasAttachedPty = true
summary.hasHostSidebarActivity = true
summary.lastOutputAt = maxTimestamp(summary.lastOutputAt, pty.lastOutputAt)
const ptyStatus = getSavedTabWorktreeStatus(owner.title, true)
if (ptyStatus === 'working' || ptyStatus === 'permission') {
addTerminalTitleEvidence(titleEvidence, summary.worktreeId, {
status: ptyStatus,
paneKey: recorded.paneKey,
ptyId: pty.ptyId,
tabId: recorded.tabId ?? persistedTabId ?? null
})
}
mergeWorktreeSummaryStatus(summary, ptyStatus)
if (
pty.preview &&
(summary.preview.length === 0 || (pty.lastOutputAt ?? -1) >= (previousLastOutputAt ?? -1))
) {
summary.preview = pty.preview
}
}
return titleEvidence
}
function addTerminalTitleEvidence(
evidenceByWorktreeId: Map<string, RuntimeTerminalTitleEvidence[]>,
worktreeId: string,
evidence: RuntimeTerminalTitleEvidence
): void {
const existing = evidenceByWorktreeId.get(worktreeId)
if (existing) {
existing.push(evidence)
} else {
evidenceByWorktreeId.set(worktreeId, [evidence])
}
}
export function applyRuntimeWorktreePsSessionActivity(args: {
store: RuntimeStore | null
summaries: Map<string, RuntimeWorktreePsSummary>
repoById: ReadonlyMap<string, Repo>
ptysById: ReadonlyMap<string, RuntimePtyWorktreeRecord>
tabs: ReadonlyMap<string, RuntimeSyncedTab>
paneOf: PtyPaneOf
/** Non-minting: a listing must not issue handles, only recognise the ones already bound. */
getTerminalHandlesForPty: (ptyId: string) => readonly string[]
getSummary: RuntimeWorktreePsSummaryLookup
}): {
mirroredWorktreeIdByTabId: Map<string, string>
connectedPtyEvidence: {
tabIds: Set<string>
paneKeys: Set<string>
ptyIdByTerminalHandle: Map<string, string>
}
} {
const mirroredWorktreeIdByTabId = new Map<string, string>()
const sessionsByHostId = new Map<ExecutionHostId, WorkspaceSessionState>()
for (const summary of args.summaries.values()) {
const repo = args.repoById.get(summary.repoId)
const session = args.store?.getWorkspaceSession?.(repo ? getRepoExecutionHostId(repo) : 'local')
if (session) {
sessionsByHostId.set(repo ? getRepoExecutionHostId(repo) : 'local', session)
}
}
for (const session of sessionsByHostId.values()) {
for (const [worktreeId, tabs] of Object.entries(session.tabsByWorktree ?? {})) {
for (const tab of tabs) {
mirroredWorktreeIdByTabId.set(tab.id, worktreeId)
}
if (tabs.length === 0) {
continue
}
const summary = args.getSummary(worktreeId)
if (summary && tabs.some((tab) => tab.ptyId && args.ptysById.get(tab.ptyId)?.connected)) {
summary.hasHostSidebarActivity = true
}
}
for (const worktreeId of getWorkspaceSessionTabActivityIds(session)) {
const summary = args.getSummary(worktreeId)
if (summary) {
summary.hasHostSidebarActivity = true
}
}
if (session.activeWorktreeId) {
const summary = args.getSummary(session.activeWorktreeId)
if (summary) {
summary.isActive = true
}
}
}
for (const [tabId, tab] of args.tabs) {
if (!mirroredWorktreeIdByTabId.has(tabId)) {
mirroredWorktreeIdByTabId.set(tabId, tab.worktreeId)
}
}
const connectedPtyEvidence = {
tabIds: new Set<string>(),
paneKeys: new Set<string>(),
ptyIdByTerminalHandle: new Map<string, string>()
}
for (const pty of args.ptysById.values()) {
if (!pty.connected) {
continue
}
const { tabId, paneKey } = args.paneOf(pty)
if (tabId) {
connectedPtyEvidence.tabIds.add(tabId)
}
if (paneKey) {
connectedPtyEvidence.paneKeys.add(paneKey)
}
for (const terminalHandle of args.getTerminalHandlesForPty(pty.ptyId)) {
connectedPtyEvidence.ptyIdByTerminalHandle.set(terminalHandle, pty.ptyId)
}
}
return { mirroredWorktreeIdByTabId, connectedPtyEvidence }
}