perf(mobile): route graph-sync snapshot fanout through the notify coalescer (#9421)
* perf(mobile): route graph-sync snapshot fanout through the notify coalescer syncWindowGraph no longer unconditionally re-serializes+fans every worktree's mobile snapshot to every client. Serve-hydrate fast path skips the build-then-drop loop when no serve-owned pty exists (desktop case); changed-worktree gating (strict superset of snapshot inputs) routes only real changes through the existing coalescer. No-op syncs 720->0 emits; single-worktree churn 720->120 (~83% fewer bytes to phone). Real changes still propagate. 11-round GPT-5.6-Sol xHIGH review converged 2-consecutive-clean; independently verified tsc+830 tests. Co-authored-by: Orca <help@stably.ai> * fix(mobile): bump snapshotVersion on preserved prune frames + carry split-group layout Pre-merge adversarial review found two stale-phone regressions: (1) preserved-headless snapshots did not bump snapshotVersion, so a pruned-but-serve-preserved worktree change was dropped by clients' same-epoch freshness gate (orca-runtime.ts:22316); (2) serve-only no-op hydrate dropped the phone's split-group layout (orca-runtime.ts:3731). Both fixed + mutation-verified regression tests. 3 typechecks + suites green. Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -2270,6 +2270,16 @@ export class OrcaRuntimeService {
|
||||
private authoritativeWindowId: number | null = null
|
||||
private tabs = new Map<string, RuntimeSyncedTab>()
|
||||
private mobileSessionTabsByWorktree = new Map<string, RuntimeMobileSessionTabsSnapshot>()
|
||||
// Why: renderer publication ordering must be judged against the renderer's
|
||||
// own last-accepted (epoch, version) — never against the stored snapshot's
|
||||
// version, which main-local touches bump independently and can push
|
||||
// permanently ahead of the renderer's counter. The renderer reuses one pair
|
||||
// for byte-identical content, so a same-epoch version <= this one is a no-op
|
||||
// resend (or stale) and is skipped without touching the stored entry.
|
||||
private acceptedRendererMobileSnapshotByWorktree = new Map<
|
||||
string,
|
||||
{ publicationEpoch: string; rendererVersion: number }
|
||||
>()
|
||||
private clientSessionTabSelections = new ClientSessionTabSelectionStore()
|
||||
// Why: idempotency map for mobile terminal creation — a retried create with the
|
||||
// same clientMutationId returns the in-flight operation instead of duplicating.
|
||||
@@ -3455,8 +3465,10 @@ export class OrcaRuntimeService {
|
||||
throw new Error('Runtime graph publisher does not match the authoritative window')
|
||||
}
|
||||
|
||||
const previousTabs = this.tabs
|
||||
const previousLeaves = this.leaves
|
||||
this.tabs = new Map(graph.tabs.map((tab) => [tab.tabId, tab]))
|
||||
this.syncMobileSessionTabs(graph.mobileSessionTabs)
|
||||
const changedMobileWorktrees = this.syncMobileSessionTabs(graph.mobileSessionTabs)
|
||||
const nextLeaves = new Map<string, RuntimeLeafRecord>()
|
||||
const graphSyncedAt = this.nextTitleObservationSequence()
|
||||
|
||||
@@ -3577,7 +3589,40 @@ export class OrcaRuntimeService {
|
||||
|
||||
this.leaves = nextLeaves
|
||||
this.rebuildLeafPtyIndex()
|
||||
this.notifyMobileSessionTabSnapshots()
|
||||
// Why: the emitted client payload is a function of the stored snapshot AND
|
||||
// the tab/leaf graph (handles/titles/connected resolve from leaf state), so
|
||||
// a graph-only change — e.g. a restored leaf binding its ptyId while the
|
||||
// snapshot pair is unchanged — must also fan out, or a paired client stays
|
||||
// on pending-handle forever. Schedule the union on the same 50ms trailing
|
||||
// edge as the OSC-title path; the coalescer emit reads the latest state at
|
||||
// fire time so no final version is ever lost.
|
||||
for (const worktreeId of this.collectMobileVisibleGraphChangedWorktrees(
|
||||
previousTabs,
|
||||
previousLeaves
|
||||
)) {
|
||||
if (changedMobileWorktrees.has(worktreeId)) {
|
||||
continue
|
||||
}
|
||||
const stored = this.mobileSessionTabsByWorktree.get(worktreeId)
|
||||
if (!stored) {
|
||||
continue
|
||||
}
|
||||
// Why: web clients drop same-epoch frames whose version isn't strictly
|
||||
// newer, so a graph-only change must mint a fresh stored version (like
|
||||
// the PTY touch path does) or the re-emitted payload — e.g. the
|
||||
// pending-handle → ready flip — is discarded and the client stays stale.
|
||||
// The accepted-renderer tracking is untouched: this is a main-local bump.
|
||||
this.mobileSessionTabsByWorktree.set(worktreeId, {
|
||||
...stored,
|
||||
snapshotVersion: stored.snapshotVersion + 1
|
||||
})
|
||||
changedMobileWorktrees.add(worktreeId)
|
||||
}
|
||||
for (const worktreeId of changedMobileWorktrees) {
|
||||
if (this.mobileSessionTabsByWorktree.has(worktreeId)) {
|
||||
this.mobileSessionTabsNotifyCoalescer.schedule(worktreeId)
|
||||
}
|
||||
}
|
||||
this.graphStatus = 'ready'
|
||||
this.setTerminalSideEffectConsumerAvailable(windowId !== HEADLESS_RUNTIME_WINDOW_ID)
|
||||
this.refreshWritableFlags()
|
||||
@@ -3598,6 +3643,46 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
}
|
||||
|
||||
// Why: toMobileSessionTabsResult resolves handles/titles from this.tabs and
|
||||
// this.leaves, so any tab/leaf delta a graph sync installs can flip the
|
||||
// client payload (pending-handle → ready, tab title) with zero change to the
|
||||
// stored snapshot. Compare exactly the projection-relevant fields and report
|
||||
// the affected worktrees; false positives only cost a coalesced no-op emit.
|
||||
private collectMobileVisibleGraphChangedWorktrees(
|
||||
previousTabs: Map<string, RuntimeSyncedTab>,
|
||||
previousLeaves: Map<string, RuntimeLeafRecord>
|
||||
): Set<string> {
|
||||
const changed = new Set<string>()
|
||||
for (const [tabId, tab] of this.tabs) {
|
||||
const prev = previousTabs.get(tabId)
|
||||
if (!prev || prev.title !== tab.title) {
|
||||
changed.add(tab.worktreeId)
|
||||
}
|
||||
}
|
||||
for (const [tabId, tab] of previousTabs) {
|
||||
if (!this.tabs.has(tabId)) {
|
||||
changed.add(tab.worktreeId)
|
||||
}
|
||||
}
|
||||
for (const [leafKey, leaf] of this.leaves) {
|
||||
const prev = previousLeaves.get(leafKey)
|
||||
if (
|
||||
!prev ||
|
||||
prev.ptyId !== leaf.ptyId ||
|
||||
prev.connected !== leaf.connected ||
|
||||
prev.paneTitle !== leaf.paneTitle
|
||||
) {
|
||||
changed.add(leaf.worktreeId)
|
||||
}
|
||||
}
|
||||
for (const [leafKey, leaf] of previousLeaves) {
|
||||
if (!this.leaves.has(leafKey)) {
|
||||
changed.add(leaf.worktreeId)
|
||||
}
|
||||
}
|
||||
return changed
|
||||
}
|
||||
|
||||
async listMobileSessionTabs(
|
||||
worktreeSelector: string,
|
||||
clientNavigationId?: string
|
||||
@@ -3645,6 +3730,18 @@ export class OrcaRuntimeService {
|
||||
if (!session) {
|
||||
return reconciledWorktreeIds
|
||||
}
|
||||
// Why: with no serve-owned ptyId anywhere in the session and no offscreen
|
||||
// browser backend, the serve-only hydrate provably builds zero tabs for
|
||||
// every worktree — skip the per-worktree rebuild entirely (hot on every
|
||||
// graph sync). Scoped to onlyServeOwnedTerminals so full hydrates are
|
||||
// untouched.
|
||||
if (
|
||||
options.onlyServeOwnedTerminals === true &&
|
||||
!this.offscreenBrowserBackend &&
|
||||
!this.workspaceSessionHasServeOwnedPty(session)
|
||||
) {
|
||||
return reconciledWorktreeIds
|
||||
}
|
||||
const entries =
|
||||
worktreeId !== undefined
|
||||
? ([[worktreeId, session.tabsByWorktree[worktreeId] ?? []]] as const)
|
||||
@@ -3714,57 +3811,146 @@ export class OrcaRuntimeService {
|
||||
? mergedActiveTab.parentTabId
|
||||
: mergedActiveTab.id
|
||||
: null
|
||||
this.mobileSessionTabsByWorktree.set(entryWorktreeId, {
|
||||
const nextTabGroups: RuntimeMobileSessionTabGroup[] = hasPersistedSplit
|
||||
? this.appendBrowserTabOrder(
|
||||
this.distributeHeadlessTabsAcrossGroups(
|
||||
persistedGroups.map((group) => ({
|
||||
id: group.id,
|
||||
activeTabId: group.activeTabId,
|
||||
tabOrder: [...group.tabOrder],
|
||||
...(group.recentTabIds ? { recentTabIds: [...group.recentTabIds] } : {})
|
||||
})),
|
||||
this.collectHeadlessParentTabOrder(mergedTerminalTabs),
|
||||
activeTopLevelId
|
||||
),
|
||||
mergedBrowserOrder,
|
||||
undefined,
|
||||
// Why: distribute drops browser ids (terminal-only), so carry each
|
||||
// browser's persisted group forward instead of coalescing left.
|
||||
this.collectBrowserGroupAssignment(persistedGroups, mergedBrowserOrder)
|
||||
)
|
||||
: options.onlyServeOwnedTerminals === true && existing?.tabGroups
|
||||
? this.appendBrowserTabOrder(
|
||||
this.mergeMobileSessionTabGroups(
|
||||
entryWorktreeId,
|
||||
existing.tabGroups,
|
||||
mergedTerminalTabs,
|
||||
mergedActiveTab?.type === 'terminal' ? mergedActiveTab : null
|
||||
),
|
||||
mergedBrowserOrder
|
||||
)
|
||||
: [
|
||||
{
|
||||
id: groupId,
|
||||
activeTabId: mergedActiveTab?.id
|
||||
? (activeTab?.parentTabId ?? mergedActiveTab.id)
|
||||
: (tabOrder[0] ?? null),
|
||||
tabOrder
|
||||
}
|
||||
]
|
||||
// Why: merging runtime tabs INTO a renderer publication must not reclass
|
||||
// the snapshot as headless-built — the preservation predicate would then
|
||||
// treat the renderer's own tabs as runtime-owned and resurrect tabs the
|
||||
// renderer later closes. Keep the renderer base epoch with a merge suffix
|
||||
// (idempotent) so ownership stays derivable from the epoch.
|
||||
const mergedIntoRendererPublication =
|
||||
options.onlyServeOwnedTerminals === true &&
|
||||
existing !== undefined &&
|
||||
!this.isHeadlessBuiltMobileSessionPublicationBase(existing.publicationEpoch)
|
||||
const nextSnapshot: RuntimeMobileSessionTabsSnapshot = {
|
||||
worktree: existing?.worktree ?? entryWorktreeId,
|
||||
publicationEpoch: `headless-hydrated:${Date.now().toString(36)}`,
|
||||
publicationEpoch: mergedIntoRendererPublication
|
||||
? this.getMergedMobileSessionPublicationEpoch(existing, tabs)
|
||||
: `headless-hydrated:${Date.now().toString(36)}`,
|
||||
snapshotVersion: (existing?.snapshotVersion ?? 0) + 1,
|
||||
activeGroupId: existing?.activeGroupId ?? groupId,
|
||||
activeTabId: mergedActiveTab?.id ?? null,
|
||||
activeTabType: mergedActiveTab?.type ?? null,
|
||||
tabGroups: hasPersistedSplit
|
||||
? this.appendBrowserTabOrder(
|
||||
this.distributeHeadlessTabsAcrossGroups(
|
||||
persistedGroups.map((group) => ({
|
||||
id: group.id,
|
||||
activeTabId: group.activeTabId,
|
||||
tabOrder: [...group.tabOrder],
|
||||
...(group.recentTabIds ? { recentTabIds: [...group.recentTabIds] } : {})
|
||||
})),
|
||||
this.collectHeadlessParentTabOrder(mergedTerminalTabs),
|
||||
activeTopLevelId
|
||||
),
|
||||
mergedBrowserOrder,
|
||||
undefined,
|
||||
// Why: distribute drops browser ids (terminal-only), so carry each
|
||||
// browser's persisted group forward instead of coalescing left.
|
||||
this.collectBrowserGroupAssignment(persistedGroups, mergedBrowserOrder)
|
||||
)
|
||||
: options.onlyServeOwnedTerminals === true && existing?.tabGroups
|
||||
? this.appendBrowserTabOrder(
|
||||
this.mergeMobileSessionTabGroups(
|
||||
entryWorktreeId,
|
||||
existing.tabGroups,
|
||||
mergedTerminalTabs,
|
||||
mergedActiveTab?.type === 'terminal' ? mergedActiveTab : null
|
||||
),
|
||||
mergedBrowserOrder
|
||||
)
|
||||
: [
|
||||
{
|
||||
id: groupId,
|
||||
activeTabId: mergedActiveTab?.id
|
||||
? (activeTab?.parentTabId ?? mergedActiveTab.id)
|
||||
: (tabOrder[0] ?? null),
|
||||
tabOrder
|
||||
}
|
||||
],
|
||||
...(hasPersistedSplit && persistedLayout ? { tabGroupLayout: persistedLayout } : {}),
|
||||
tabGroups: nextTabGroups,
|
||||
// Why: the serve-only rebuild runs on every graph sync — carry the
|
||||
// existing split layout forward or each sync drops it and fans out.
|
||||
...(hasPersistedSplit && persistedLayout
|
||||
? { tabGroupLayout: persistedLayout }
|
||||
: options.onlyServeOwnedTerminals === true && existing?.tabGroupLayout
|
||||
? { tabGroupLayout: existing.tabGroupLayout }
|
||||
: {}),
|
||||
tabs: mergedTabs
|
||||
})
|
||||
}
|
||||
// Why: the serve-only hydrate runs on EVERY graph sync; when the rebuilt
|
||||
// projection matches the existing snapshot, keep the existing object and
|
||||
// (epoch, version) untouched so identity-based change detection stays a
|
||||
// pure no-op and unchanged serve/browser worktrees never fan out.
|
||||
if (existing && this.headlessMobileSnapshotContentUnchanged(existing, nextSnapshot)) {
|
||||
continue
|
||||
}
|
||||
this.mobileSessionTabsByWorktree.set(entryWorktreeId, nextSnapshot)
|
||||
}
|
||||
return reconciledWorktreeIds
|
||||
}
|
||||
|
||||
// Why: content equality for the hydrate's idempotence check — compares every
|
||||
// client-visible field EXCEPT publicationEpoch/snapshotVersion (both are
|
||||
// freshly minted on each rebuild and would defeat the comparison). Tab and
|
||||
// group objects are rebuilt each hydrate, so compare by value, not identity.
|
||||
private headlessMobileSnapshotContentUnchanged(
|
||||
existing: RuntimeMobileSessionTabsSnapshot,
|
||||
next: RuntimeMobileSessionTabsSnapshot
|
||||
): boolean {
|
||||
if (
|
||||
existing.worktree !== next.worktree ||
|
||||
existing.activeGroupId !== next.activeGroupId ||
|
||||
existing.activeTabId !== next.activeTabId ||
|
||||
existing.activeTabType !== next.activeTabType
|
||||
) {
|
||||
return false
|
||||
}
|
||||
// Why: this runs per persisted worktree on EVERY graph sync whenever a
|
||||
// serve PTY exists, so compare structurally instead of stable-stringifying
|
||||
// both sides (which allocated six full serialized trees per worktree).
|
||||
return (
|
||||
this.mobileSnapshotValueEqual(existing.tabs, next.tabs) &&
|
||||
this.mobileSnapshotValueEqual(existing.tabGroups ?? null, next.tabGroups ?? null) &&
|
||||
this.mobileSnapshotValueEqual(existing.tabGroupLayout ?? null, next.tabGroupLayout ?? null)
|
||||
)
|
||||
}
|
||||
|
||||
// Deep structural equality over plain snapshot JSON (objects/arrays/scalars).
|
||||
// Key order is irrelevant; a mismatch only costs a coalesced no-op emit.
|
||||
private mobileSnapshotValueEqual(a: unknown, b: unknown): boolean {
|
||||
if (a === b) {
|
||||
return true
|
||||
}
|
||||
if (Array.isArray(a) || Array.isArray(b)) {
|
||||
if (!Array.isArray(a) || !Array.isArray(b) || a.length !== b.length) {
|
||||
return false
|
||||
}
|
||||
for (let index = 0; index < a.length; index++) {
|
||||
if (!this.mobileSnapshotValueEqual(a[index], b[index])) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
if (a !== null && b !== null && typeof a === 'object' && typeof b === 'object') {
|
||||
const aRecord = a as Record<string, unknown>
|
||||
const bRecord = b as Record<string, unknown>
|
||||
const aKeys = Object.keys(aRecord)
|
||||
if (aKeys.length !== Object.keys(bRecord).length) {
|
||||
return false
|
||||
}
|
||||
for (const key of aKeys) {
|
||||
if (
|
||||
!Object.hasOwn(bRecord, key) ||
|
||||
!this.mobileSnapshotValueEqual(aRecord[key], bRecord[key])
|
||||
) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Why: keep an existing snapshot's browser tabs in sync with the live bridge
|
||||
// without rebuilding stable terminal state. Replaces browser entries with the
|
||||
// current live set and rewrites the browser portion of the primary group order.
|
||||
@@ -3882,6 +4068,27 @@ export class OrcaRuntimeService {
|
||||
return typeof ptyId === 'string' && ptyId.startsWith('serve-')
|
||||
}
|
||||
|
||||
// Why: strict superset of hasServeOwnedPtyBinding's inputs (tab.ptyId +
|
||||
// layout leaf ptyIds are what the built tabs' bindings are derived from), so
|
||||
// the serve-only hydrate fast-path can never hide a serve terminal.
|
||||
private workspaceSessionHasServeOwnedPty(session: WorkspaceSessionState): boolean {
|
||||
for (const tabs of Object.values(session.tabsByWorktree ?? {})) {
|
||||
for (const tab of tabs) {
|
||||
if (this.isServeOwnedPtyId(tab.ptyId)) {
|
||||
return true
|
||||
}
|
||||
const leafPtyIds = session.terminalLayoutsByTabId?.[tab.id]?.ptyIdsByLeafId
|
||||
if (
|
||||
leafPtyIds &&
|
||||
Object.values(leafPtyIds).some((ptyId) => this.isServeOwnedPtyId(ptyId))
|
||||
) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
private hasServeOwnedPtyBinding(tab: RuntimeMobileSessionTerminalTab): boolean {
|
||||
if (this.isServeOwnedPtyId(tab.ptyId)) {
|
||||
return true
|
||||
@@ -3913,6 +4120,47 @@ export class OrcaRuntimeService {
|
||||
)
|
||||
}
|
||||
|
||||
// Why: a snapshot tab can keep a serve/SSH-owned ptyId after the runtime
|
||||
// terminal died and was de-persisted, so id shape alone must not preserve it
|
||||
// against a renderer publication. Require the binding to be backed by a live
|
||||
// PTY or by the persisted workspace session (a dormant persisted serve/SSH
|
||||
// binding is still re-hydratable, so it stays preserved).
|
||||
private hasLiveOrPersistedServeOrSshOwnedPtyBinding(
|
||||
worktreeId: string,
|
||||
tab: RuntimeMobileSessionTerminalTab
|
||||
): boolean {
|
||||
const boundPtyIds = [
|
||||
tab.ptyId,
|
||||
...Object.values(tab.parentLayout?.ptyIdsByLeafId ?? {})
|
||||
].filter((ptyId): ptyId is string => this.isServeOrSshOwnedPtyId(ptyId))
|
||||
if (boundPtyIds.length === 0) {
|
||||
return false
|
||||
}
|
||||
// Why: exited PTY records are archived in ptysById, so require a connected
|
||||
// record — a dead serve shell whose persisted binding is also gone must
|
||||
// stop being preserved.
|
||||
if (boundPtyIds.some((ptyId) => this.ptysById.get(ptyId)?.connected === true)) {
|
||||
return true
|
||||
}
|
||||
const session = this.store?.getWorkspaceSession?.()
|
||||
if (!session) {
|
||||
return false
|
||||
}
|
||||
const persistedTab = (session.tabsByWorktree?.[worktreeId] ?? []).find(
|
||||
(candidate) => candidate.id === tab.parentTabId
|
||||
)
|
||||
if (!persistedTab) {
|
||||
return false
|
||||
}
|
||||
const persistedPtyIds = new Set(
|
||||
[
|
||||
persistedTab.ptyId,
|
||||
...Object.values(session.terminalLayoutsByTabId?.[persistedTab.id]?.ptyIdsByLeafId ?? {})
|
||||
].filter((ptyId): ptyId is string => typeof ptyId === 'string')
|
||||
)
|
||||
return boundPtyIds.some((ptyId) => persistedPtyIds.has(ptyId))
|
||||
}
|
||||
|
||||
// Why: a tab needs authoritative runtime teardown (kill + de-persist + prune)
|
||||
// only when the renderer can't durably tear it down: either it's serve/SSH
|
||||
// (preserved + re-hydrated, would resurrect) or the renderer graph never
|
||||
@@ -22407,10 +22655,20 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
}
|
||||
|
||||
private syncMobileSessionTabs(snapshots: RuntimeMobileSessionTabsSnapshot[] | undefined): void {
|
||||
// Returns the worktrees whose stored snapshot object changed during this
|
||||
// sync, so the caller can fan out only actually-changed worktrees.
|
||||
private syncMobileSessionTabs(
|
||||
snapshots: RuntimeMobileSessionTabsSnapshot[] | undefined
|
||||
): Set<string> {
|
||||
const changedWorktreeIds = new Set<string>()
|
||||
if (snapshots === undefined) {
|
||||
return
|
||||
return changedWorktreeIds
|
||||
}
|
||||
// Why: snapshots are immutable — every writer replaces the map entry with a
|
||||
// new object, and the accept gate below drops semantically-unchanged
|
||||
// renderer resends before they replace an entry — so reference identity
|
||||
// before/after detects exactly the entries that actually changed.
|
||||
const before = new Map(this.mobileSessionTabsByWorktree)
|
||||
// Why: renderer graphs own renderer tabs, but headless serve terminals never enter that graph unless we preserve their bindings.
|
||||
this.hydrateHeadlessMobileSessionTabsFromWorkspaceSession(undefined, {
|
||||
allowAttachedWindow: true,
|
||||
@@ -22420,29 +22678,85 @@ export class OrcaRuntimeService {
|
||||
for (const snapshot of snapshots) {
|
||||
nextWorktrees.add(snapshot.worktree)
|
||||
const existing = this.mobileSessionTabsByWorktree.get(snapshot.worktree)
|
||||
const nextSnapshot = this.mergePreservedHeadlessMobileSessionTabs(snapshot, existing)
|
||||
// Why: judge renderer publication ordering against the renderer's own
|
||||
// last-accepted (epoch, version) — the renderer reuses one pair for
|
||||
// byte-identical content, so a same-epoch version <= the accepted one is
|
||||
// a no-op resend (or a stale frame) and must be skipped. Never compare
|
||||
// against the stored snapshot's version: main-local touches bump it
|
||||
// independently and would reject genuinely newer renderer revisions.
|
||||
const accepted = this.acceptedRendererMobileSnapshotByWorktree.get(snapshot.worktree)
|
||||
if (
|
||||
!existing ||
|
||||
nextSnapshot.publicationEpoch !== existing.publicationEpoch ||
|
||||
nextSnapshot.snapshotVersion >= existing.snapshotVersion
|
||||
accepted &&
|
||||
accepted.publicationEpoch === snapshot.publicationEpoch &&
|
||||
snapshot.snapshotVersion <= accepted.rendererVersion &&
|
||||
// Why: preservation is main-only state — a serve/SSH binding (or live
|
||||
// browser page) can disappear without the renderer bumping its version,
|
||||
// so a resend of the EXACT accepted revision (content-identical to the
|
||||
// accepted publication, safe to re-merge) must still fall through to
|
||||
// the merge, which prunes stale preserved tabs. Strictly-older frames
|
||||
// stay skipped: their content is outdated, and the next accepted-pair
|
||||
// resend performs the prune.
|
||||
!(
|
||||
existing &&
|
||||
snapshot.snapshotVersion === accepted.rendererVersion &&
|
||||
this.storedMobileSnapshotHasStalePreservedTab(existing, snapshot)
|
||||
)
|
||||
) {
|
||||
this.mobileSessionTabsByWorktree.set(snapshot.worktree, nextSnapshot)
|
||||
continue
|
||||
}
|
||||
const nextSnapshot = this.mergePreservedHeadlessMobileSessionTabs(snapshot, existing)
|
||||
// Why: clients drop same-epoch frames whose version isn't strictly newer,
|
||||
// and main-local touches may already have emitted a higher version than
|
||||
// the renderer's counter — keep the stored version strictly monotonic so
|
||||
// the accepted content is never discarded as stale downstream.
|
||||
const storedVersion = existing
|
||||
? Math.max(nextSnapshot.snapshotVersion, existing.snapshotVersion + 1)
|
||||
: nextSnapshot.snapshotVersion
|
||||
this.mobileSessionTabsByWorktree.set(
|
||||
snapshot.worktree,
|
||||
storedVersion === nextSnapshot.snapshotVersion
|
||||
? nextSnapshot
|
||||
: { ...nextSnapshot, snapshotVersion: storedVersion }
|
||||
)
|
||||
this.acceptedRendererMobileSnapshotByWorktree.set(snapshot.worktree, {
|
||||
publicationEpoch: snapshot.publicationEpoch,
|
||||
rendererVersion: snapshot.snapshotVersion
|
||||
})
|
||||
}
|
||||
for (const [worktreeId, existing] of [...this.mobileSessionTabsByWorktree.entries()]) {
|
||||
if (!nextWorktrees.has(worktreeId)) {
|
||||
const preserved = this.buildPreservedHeadlessMobileSessionSnapshot(existing)
|
||||
if (preserved) {
|
||||
this.mobileSessionTabsByWorktree.set(worktreeId, preserved)
|
||||
// Why: preservation filters existing.tabs in place (same objects) and
|
||||
// the merge epoch hashes the preserved identities idempotently, so an
|
||||
// equal epoch with every tab object retained means the recomputation
|
||||
// was a no-op — keep the entry so no-op syncs don't fan out.
|
||||
const preservedIsNoOp =
|
||||
preserved.publicationEpoch === existing.publicationEpoch &&
|
||||
preserved.tabs.length === existing.tabs.length &&
|
||||
preserved.tabs.every((tab, index) => tab === existing.tabs[index])
|
||||
if (!preservedIsNoOp) {
|
||||
this.mobileSessionTabsByWorktree.set(worktreeId, preserved)
|
||||
}
|
||||
// Why: the stored entry is no longer the renderer's publication, so a
|
||||
// future renderer frame must be re-merged even if it reuses the pair.
|
||||
this.acceptedRendererMobileSnapshotByWorktree.delete(worktreeId)
|
||||
nextWorktrees.add(worktreeId)
|
||||
} else {
|
||||
this.mobileSessionTabsByWorktree.delete(worktreeId)
|
||||
this.acceptedRendererMobileSnapshotByWorktree.delete(worktreeId)
|
||||
// Why: drop any pending coalesced notify so a stale snapshot can't land after the removed frame.
|
||||
this.mobileSessionTabsNotifyCoalescer.cancel(worktreeId)
|
||||
this.notifyMobileSessionTabsRemoved(worktreeId)
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const [worktreeId, snapshot] of this.mobileSessionTabsByWorktree) {
|
||||
if (before.get(worktreeId) !== snapshot) {
|
||||
changedWorktreeIds.add(worktreeId)
|
||||
}
|
||||
}
|
||||
return changedWorktreeIds
|
||||
}
|
||||
|
||||
private mergePreservedHeadlessMobileSessionTabs(
|
||||
@@ -22511,6 +22825,8 @@ export class OrcaRuntimeService {
|
||||
return {
|
||||
...existing,
|
||||
publicationEpoch: this.getMergedMobileSessionPublicationEpoch(existing, tabs),
|
||||
// Why: mint a fresh version or clients' same-epoch gate drops the prune frame.
|
||||
snapshotVersion: existing.snapshotVersion + 1,
|
||||
activeGroupId:
|
||||
existing.activeGroupId ?? this.getHeadlessMobileSessionGroupId(existing.worktree),
|
||||
activeTabId: activeTab?.id ?? null,
|
||||
@@ -22525,6 +22841,26 @@ export class OrcaRuntimeService {
|
||||
}
|
||||
}
|
||||
|
||||
// Why: the accepted-revision no-op gate must not fossilize preserved runtime
|
||||
// tabs. A stored merged snapshot's tabs that are absent from the incoming
|
||||
// renderer publication exist only via preservation; if any such tab no longer
|
||||
// passes the preservation predicate (binding removed from the live PTY table
|
||||
// and persisted session, or browser page closed), the stored snapshot is
|
||||
// stale even though the renderer revision is unchanged.
|
||||
private storedMobileSnapshotHasStalePreservedTab(
|
||||
existing: RuntimeMobileSessionTabsSnapshot,
|
||||
incoming: RuntimeMobileSessionTabsSnapshot
|
||||
): boolean {
|
||||
const incomingIds = new Set(
|
||||
incoming.tabs.flatMap((tab) => this.getMobileSessionSnapshotTabIdentityKeys(tab))
|
||||
)
|
||||
return existing.tabs.some(
|
||||
(tab) =>
|
||||
!this.getMobileSessionSnapshotTabIdentityKeys(tab).some((id) => incomingIds.has(id)) &&
|
||||
!this.shouldPreserveHeadlessMobileSessionTab(existing, tab)
|
||||
)
|
||||
}
|
||||
|
||||
private collectPreservedHeadlessMobileSessionTabs(
|
||||
existing: RuntimeMobileSessionTabsSnapshot,
|
||||
incoming?: RuntimeMobileSessionTabsSnapshot
|
||||
@@ -22546,17 +22882,31 @@ export class OrcaRuntimeService {
|
||||
): boolean {
|
||||
// Why: headless offscreen browser tabs exist only server-side, so a renderer-graph merge must keep them, not prune as "not in the graph".
|
||||
if (tab.type === 'browser') {
|
||||
if (!this.offscreenBrowserBackend) {
|
||||
return false
|
||||
}
|
||||
// Why: in a renderer-based merged snapshot the browser entries can also
|
||||
// be renderer-owned, so only pages the offscreen bridge still lists are
|
||||
// runtime-owned and preservable; a pure renderer epoch preserves none.
|
||||
return (
|
||||
Boolean(this.offscreenBrowserBackend) &&
|
||||
this.isHeadlessMobileSessionPublication(snapshot.publicationEpoch)
|
||||
this.isHeadlessBuiltMobileSessionPublicationBase(snapshot.publicationEpoch) ||
|
||||
(snapshot.publicationEpoch.includes(':headless-merge:') &&
|
||||
typeof tab.browserPageId === 'string' &&
|
||||
this.getLiveBrowserTabsByPageId(snapshot.worktree).has(tab.browserPageId))
|
||||
)
|
||||
}
|
||||
if (tab.type !== 'terminal') {
|
||||
return false
|
||||
}
|
||||
// Why: a merged renderer snapshot carries BOTH renderer-owned and
|
||||
// runtime-owned tabs, so the epoch alone must not preserve every terminal —
|
||||
// that resurrects renderer tabs the renderer already closed. Broad
|
||||
// preservation applies only to genuinely headless-built snapshots; in a
|
||||
// renderer-based one, only tabs with a live-or-persisted serve/SSH binding
|
||||
// are runtime-owned and preservable.
|
||||
return (
|
||||
this.isHeadlessMobileSessionPublication(snapshot.publicationEpoch) ||
|
||||
this.hasServeOwnedPtyBinding(tab)
|
||||
this.isHeadlessBuiltMobileSessionPublicationBase(snapshot.publicationEpoch) ||
|
||||
this.hasLiveOrPersistedServeOrSshOwnedPtyBinding(snapshot.worktree, tab)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -22568,6 +22918,15 @@ export class OrcaRuntimeService {
|
||||
)
|
||||
}
|
||||
|
||||
// Why: `:headless-merge:` only marks that runtime tabs were merged in — the
|
||||
// BASE epoch still says who published the snapshot. A renderer-based merged
|
||||
// snapshot must not be classified as headless-built, or its renderer tabs
|
||||
// read as runtime-owned.
|
||||
private isHeadlessBuiltMobileSessionPublicationBase(publicationEpoch: string): boolean {
|
||||
const base = publicationEpoch.split(':headless-merge:')[0]
|
||||
return base.startsWith('headless:') || base.startsWith('headless-hydrated:')
|
||||
}
|
||||
|
||||
private getMergedMobileSessionPublicationEpoch(
|
||||
snapshot: RuntimeMobileSessionTabsSnapshot,
|
||||
preservedTabs: readonly RuntimeMobileSessionSnapshotTab[]
|
||||
|
||||
Reference in New Issue
Block a user