Skip to content

fix(terminal): keep post-spawn reconcile alive through locks - #6684

Merged
AmethystLiang merged 1 commit into
mainfrom
fix-terminal-reconcile-transient-locks
Jun 29, 2026
Merged

AmethystLiang merged 1 commit into
mainfrom
fix-terminal-reconcile-transient-locks

Conversation

@AmethystLiang

Copy link
Copy Markdown
Contributor

Summary

  • keep post-spawn PTY size reconcile polling alive when a transient mobile presence lock or PTY lock skips a frame
  • add a regression test that clears a mobile presence lock before the reconcile window ends

Follow-up to #6649.

Tests

  • pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • pnpm exec oxlint src/renderer/src/components/terminal-pane/pty-connection.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The reconcilePtySizeAfterSpawn rAF tick loop in pty-connection.ts is changed so that detecting a mobile-fit override or PTY lock causes the current frame to be skipped rather than the reconciliation loop to be permanently cancelled. The loop now continues until MAX_RECONCILE_FRAMES regardless, only performing safeFit and transport.resize on frames where the PTY is not parked or locked. A new test in pty-connection.test.ts exercises this by toggling the driver state to mobile mid-reconciliation and asserting that transport.resize fires after it returns to idle.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes summary and tests, but it omits required Screenshots, AI Review Report, Security Audit, and Notes sections. Add the missing template sections, including screenshot status, AI review details with cross-platform coverage, security audit, and notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main terminal reconcile change and lock-handling behavior.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: f06deae9-5828-4d30-a487-839db8738d82

📥 Commits

Reviewing files that changed from the base of the PR and between 6e2a3a4 and 0d0966d.

📒 Files selected for processing (2)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts

Comment on lines +2161 to +2205
globalThis.requestAnimationFrame = vi.fn((callback: FrameRequestCallback) => {
frameCallbacks.push(callback)
return frameCallbacks.length
})
const runNextFrame = (): void => {
const callback = frameCallbacks.shift()
if (!callback) {
throw new Error('expected a queued animation frame')
}
callback(0)
}

const { connectPanePty } = await import('./pty-connection')
const { setDriverForPty } = await import('@/lib/pane-manager/mobile-driver-state')

const ptyId = 'pty-post-spawn-transient-lock'
setDriverForPty(ptyId, { kind: 'mobile', clientId: 'phone-1' })
try {
const transport = createMockTransport(ptyId)
transportFactoryQueue.push(transport)
mockStoreState = {
...mockStoreState,
tabsByWorktree: { 'wt-1': [{ id: 'tab-1', ptyId: null }] },
ptyIdsByTabId: { 'tab-1': [] }
}
const pane = createPane(1)
pane.terminal.cols = 80
pane.terminal.rows = 24

connectPanePty(pane as never, createManager(1) as never, createDeps() as never)
runNextFrame()
await flushAsyncTicks()

pane.terminal.cols = 120
pane.terminal.rows = 40
runNextFrame()
expect(transport.resize).not.toHaveBeenCalled()

setDriverForPty(ptyId, { kind: 'idle' })
runNextFrame()

expect(transport.resize).toHaveBeenCalledWith(120, 40)
} finally {
setDriverForPty(ptyId, { kind: 'idle' })
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Restore the rAF mock and dispose the binding.

This test leaves globalThis.requestAnimationFrame replaced and a reconcile loop still queued after the assertion. Restore the global and dispose the pane binding in finally to avoid cross-test pollution.

Proposed fix
   it('continues post-spawn size reconcile after a transient mobile presence lock', async () => {
     const frameCallbacks: FrameRequestCallback[] = []
+    const originalRequestAnimationFrame = globalThis.requestAnimationFrame
     globalThis.requestAnimationFrame = vi.fn((callback: FrameRequestCallback) => {
       frameCallbacks.push(callback)
       return frameCallbacks.length
     })
@@
     const { connectPanePty } = await import('./pty-connection')
     const { setDriverForPty } = await import('`@/lib/pane-manager/mobile-driver-state`')
 
     const ptyId = 'pty-post-spawn-transient-lock'
+    let binding: { dispose(): void } | null = null
     setDriverForPty(ptyId, { kind: 'mobile', clientId: 'phone-1' })
     try {
@@
-      connectPanePty(pane as never, createManager(1) as never, createDeps() as never)
+      binding = connectPanePty(pane as never, createManager(1) as never, createDeps() as never)
@@
     } finally {
+      binding?.dispose()
       setDriverForPty(ptyId, { kind: 'idle' })
+      globalThis.requestAnimationFrame = originalRequestAnimationFrame
     }
   })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
globalThis.requestAnimationFrame = vi.fn((callback: FrameRequestCallback) => {
frameCallbacks.push(callback)
return frameCallbacks.length
})
const runNextFrame = (): void => {
const callback = frameCallbacks.shift()
if (!callback) {
throw new Error('expected a queued animation frame')
}
callback(0)
}
const { connectPanePty } = await import('./pty-connection')
const { setDriverForPty } = await import('@/lib/pane-manager/mobile-driver-state')
const ptyId = 'pty-post-spawn-transient-lock'
setDriverForPty(ptyId, { kind: 'mobile', clientId: 'phone-1' })
try {
const transport = createMockTransport(ptyId)
transportFactoryQueue.push(transport)
mockStoreState = {
...mockStoreState,
tabsByWorktree: { 'wt-1': [{ id: 'tab-1', ptyId: null }] },
ptyIdsByTabId: { 'tab-1': [] }
}
const pane = createPane(1)
pane.terminal.cols = 80
pane.terminal.rows = 24
connectPanePty(pane as never, createManager(1) as never, createDeps() as never)
runNextFrame()
await flushAsyncTicks()
pane.terminal.cols = 120
pane.terminal.rows = 40
runNextFrame()
expect(transport.resize).not.toHaveBeenCalled()
setDriverForPty(ptyId, { kind: 'idle' })
runNextFrame()
expect(transport.resize).toHaveBeenCalledWith(120, 40)
} finally {
setDriverForPty(ptyId, { kind: 'idle' })
}
const frameCallbacks: FrameRequestCallback[] = []
const originalRequestAnimationFrame = globalThis.requestAnimationFrame
globalThis.requestAnimationFrame = vi.fn((callback: FrameRequestCallback) => {
frameCallbacks.push(callback)
return frameCallbacks.length
})
const runNextFrame = (): void => {
const callback = frameCallbacks.shift()
if (!callback) {
throw new Error('expected a queued animation frame')
}
callback(0)
}
const { connectPanePty } = await import('./pty-connection')
const { setDriverForPty } = await import('`@/lib/pane-manager/mobile-driver-state`')
const ptyId = 'pty-post-spawn-transient-lock'
let binding: { dispose(): void } | null = null
setDriverForPty(ptyId, { kind: 'mobile', clientId: 'phone-1' })
try {
const transport = createMockTransport(ptyId)
transportFactoryQueue.push(transport)
mockStoreState = {
...mockStoreState,
tabsByWorktree: { 'wt-1': [{ id: 'tab-1', ptyId: null }] },
ptyIdsByTabId: { 'tab-1': [] }
}
const pane = createPane(1)
pane.terminal.cols = 80
pane.terminal.rows = 24
binding = connectPanePty(pane as never, createManager(1) as never, createDeps() as never)
runNextFrame()
await flushAsyncTicks()
pane.terminal.cols = 120
pane.terminal.rows = 40
runNextFrame()
expect(transport.resize).not.toHaveBeenCalled()
setDriverForPty(ptyId, { kind: 'idle' })
runNextFrame()
expect(transport.resize).toHaveBeenCalledWith(120, 40)
} finally {
binding?.dispose()
setDriverForPty(ptyId, { kind: 'idle' })
globalThis.requestAnimationFrame = originalRequestAnimationFrame
}

Comment on lines +2211 to +2220
// Mobile legitimately parks the PTY at phone dims; a transient guard
// should only skip this frame, not cancel the reconcile window.
if (!getFitOverrideForPty(ptyId) && !isPtyLocked(ptyId)) {
safeFit(pane)
const cols = pane.terminal.cols
const rows = pane.terminal.rows
if (cols > 0 && rows > 0 && (cols !== lastSentCols || rows !== lastSentRows)) {
// Initial spawn-time sync is authoritative, so it bypasses the
// visibility gate that onResize honors (but not the mobile guards above).
transport.resize(cols, rows)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Invalidate the sent-size baseline after a mobile skip.

If mobile parks the PTY at phone dimensions and the desktop size after unlock equals the original spawn size, lastSentCols/Rows still match and no restore resize is sent. Track that a guarded frame was skipped and force the first unblocked valid frame to reassert desktop dimensions.

Proposed fix
     let frame = 0
     let lastSentCols = spawnCols
     let lastSentRows = spawnRows
+    let skippedMobileGuardFrame = false
     const tick = (): void => {
       if (disposed || transport.getPtyId() !== ptyId) {
         return
       }
       // Mobile legitimately parks the PTY at phone dims; a transient guard
       // should only skip this frame, not cancel the reconcile window.
-      if (!getFitOverrideForPty(ptyId) && !isPtyLocked(ptyId)) {
+      const mobileGuardActive = Boolean(getFitOverrideForPty(ptyId) || isPtyLocked(ptyId))
+      if (mobileGuardActive) {
+        skippedMobileGuardFrame = true
+      } else {
         safeFit(pane)
         const cols = pane.terminal.cols
         const rows = pane.terminal.rows
-        if (cols > 0 && rows > 0 && (cols !== lastSentCols || rows !== lastSentRows)) {
+        if (
+          cols > 0 &&
+          rows > 0 &&
+          (skippedMobileGuardFrame || cols !== lastSentCols || rows !== lastSentRows)
+        ) {
           // Initial spawn-time sync is authoritative, so it bypasses the
           // visibility gate that onResize honors (but not the mobile guards above).
           transport.resize(cols, rows)
+          skippedMobileGuardFrame = false
           lastSentCols = cols
           lastSentRows = rows
         }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Mobile legitimately parks the PTY at phone dims; a transient guard
// should only skip this frame, not cancel the reconcile window.
if (!getFitOverrideForPty(ptyId) && !isPtyLocked(ptyId)) {
safeFit(pane)
const cols = pane.terminal.cols
const rows = pane.terminal.rows
if (cols > 0 && rows > 0 && (cols !== lastSentCols || rows !== lastSentRows)) {
// Initial spawn-time sync is authoritative, so it bypasses the
// visibility gate that onResize honors (but not the mobile guards above).
transport.resize(cols, rows)
let frame = 0
let lastSentCols = spawnCols
let lastSentRows = spawnRows
let skippedMobileGuardFrame = false
const tick = (): void => {
if (disposed || transport.getPtyId() !== ptyId) {
return
}
// Mobile legitimately parks the PTY at phone dims; a transient guard
// should only skip this frame, not cancel the reconcile window.
const mobileGuardActive = Boolean(getFitOverrideForPty(ptyId) || isPtyLocked(ptyId))
if (mobileGuardActive) {
skippedMobileGuardFrame = true
} else {
safeFit(pane)
const cols = pane.terminal.cols
const rows = pane.terminal.rows
if (
cols > 0 &&
rows > 0 &&
(skippedMobileGuardFrame || cols !== lastSentCols || rows !== lastSentRows)
) {
// Initial spawn-time sync is authoritative, so it bypasses the
// visibility gate that onResize honors (but not the mobile guards above).
transport.resize(cols, rows)
skippedMobileGuardFrame = false
lastSentCols = cols
lastSentRows = rows

@AmethystLiang
AmethystLiang merged commit 4a07a1d into main Jun 29, 2026
2 checks passed
@AmethystLiang
AmethystLiang deleted the fix-terminal-reconcile-transient-locks branch June 29, 2026 02:24
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant