Skip to content

fix(ports): stop live-port indicator flickering (scan debounce + re-init loop) - #5510

Merged
AmethystLiang merged 6 commits into
stablyai:mainfrom
gsxdsm:gsxdsm/Fix-live-port-icon-flicker
Jul 10, 2026
Merged

AmethystLiang merged 6 commits into
stablyai:mainfrom
gsxdsm:gsxdsm/Fix-live-port-icon-flicker

Conversation

@gsxdsm

@gsxdsm gsxdsm commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

What

The live-port "plug" icon on a worktree row flickers off/on instead of staying solid. Two independent causes in the workspace port scanner, both surfaced while testing the live ports UI.

Cause 1 — transient scan failures drop ports for a cycle

WorkspacePortScanner polls each execution host every 30s. On a transient failure (SSH/IPC latency), the catch substituted an empty unavailable scan, so that host's ports vanished from the merged result for the cycle → the affected worktree's ports.length hit 0 → the plug icon unmounted, then reappeared next poll.

Fix: debounce per-host failures — reuse the host's last good scan until failures reach a tolerance (2). A reachable host reporting zero ports has no unavailableReason, so a genuine port close still clears immediately. Logic extracted to a pure helper (workspace-port-scan-debounce.ts) with unit tests.

Cause 2 — scanner re-init loop (~600ms)

Diagnostics showed scans repeating roughly every 600ms with identical results and no advertised-URL events — the scanner was re-initializing in a tight loop:

  • The poll effect depended on the refresh callback's identity.
  • refresh gets a new identity whenever repos/settings are replaced in the store (frequent in a busy workspace).
  • Each re-init ran setWorkspacePortScan(null) (plug disappears) and installWindowVisibilityInterval scans immediately on install (plug reappears) → a ~600ms null → rescan blink even though the ports never changed.

Fix: call the latest refresh through a ref and key the poll/advertised-URL effects on stable value-signatures (enabled state, active scan key, host-set signature, runtime kind) instead of the churning callback identity. Incidental store churn no longer re-inits the scanner; it still polls every 30s, reacts to advertised-URL events, and rescans on a real host-set/runtime change.

Tests

workspace-port-scan-debounce.test.ts — 6 cases: single-failure tolerance, drop-after-tolerance, immediate clear on a real (reachable) empty scan, failure-streak reset, per-host isolation, and state pruning. oxlint clean; existing WorktreeCardPorts test passes.

Notes

Split out of #5501 (the touch-visibility icon fix) — these port-scanner fixes are independent of that change. A possible follow-up: the underlying repos/settings reference churn (~600ms) is a separate minor perf item; it no longer affects the port scanner but may cause extra re-renders elsewhere.


Open in Stage

gsxdsm and others added 2 commits June 16, 2026 09:46
…lures

A worktree's plug icon renders only when it has ports. The scanner polls
each execution host every 30s and, on a transient failure (SSH/IPC latency),
substituted an empty "unavailable" scan — dropping that host's ports for the
cycle and blinking the affected row's live-port indicator off, then on again
next poll.

Debounce per-host failures: reuse the host's last good scan until failures
reach a tolerance, so a single dropped poll no longer zeroes the row. A
reachable host reporting no ports has no unavailableReason, so a genuine port
close still clears immediately. Logic extracted to a pure helper with tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The scan poll effect depended on the `refresh` callback identity, which gets a
fresh reference whenever repos/settings are replaced (frequent in a busy
workspace). Each change re-ran the effect, which reset the scan to null and
kicked an immediate re-scan — a tight ~600ms loop that blinked the plug icon
off/on even though the scanned ports never changed.

Drive the poll and advertised-url effects off the latest refresh via a ref and
key them on stable value-signatures (enable state, active scan key, host-set
signature, runtime kind) instead of the callback identity. Incidental store
churn no longer re-inits the scanner; it still polls every 30s, reacts to
advertised-url events, and rescans on a real host-set or runtime change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@ghost

ghost commented Jun 16, 2026 •

Copy link
Copy Markdown

Ready to review this PR? Stage has broken it down into 3 individual chapters for you:

Title
1 Implement port scan failure debouncing logic
2 Integrate failure debouncing into WorkspacePortScanner
3 Stabilize scanner effects against store churn
Open in Stage

Chapters generated by Stage for commit a78046c on Jun 16, 2026 4:50pm UTC.

- Prune the per-host failure counter for failed-only hosts that disappear,
  not just hosts present in lastGood — avoids an unbounded failures map and a
  stale failure streak if the host reappears
- Canonicalize scanTargetsSignature by sorting so a reordered-but-unchanged
  host set does not re-init the scan and reintroduce flicker

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jun 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 055b5d21-548e-4d38-8747-2c6b5651f72a

📥 Commits

Reviewing files that changed from the base of the PR and between 3b53d0b and a78046c.

📒 Files selected for processing (3)
  • src/renderer/src/components/ports/WorkspacePortScanner.tsx
  • src/renderer/src/lib/workspace-port-scan-debounce.test.ts
  • src/renderer/src/lib/workspace-port-scan-debounce.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/renderer/src/lib/workspace-port-scan-debounce.test.ts
  • src/renderer/src/components/ports/WorkspacePortScanner.tsx
  • src/renderer/src/lib/workspace-port-scan-debounce.ts

📝 Walkthrough

Walkthrough

A new workspace-port-scan-debounce module is introduced with two exported types (KeyedPortScan, PortScanDebounceState), a createPortScanDebounceState constructor, and a reconcileTransientPortScanFailures function. The function prunes stale per-key state, retains the last reachable scan result across consecutive unavailable polls up to a configurable tolerance, and only surfaces unavailable results once the tolerance is exceeded.

WorkspacePortScanner is updated to hold debounceStateRef and refreshRef, compute a stable scanTargetsSignature as an effect dependency, route all scan results through reconcileTransientPortScanFailures before applying state updates, and replace direct refresh references in all event/timer/retry paths with refreshRef.current(). A Vitest suite covering the reconciliation logic is also added.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description covers the user-visible change, root causes, fixes, and testing. However, it is missing required sections: Screenshots, Testing checklist completion, AI Review Report, Security Audit, and Notes. Add the missing template sections: Screenshots (or 'No visual change'), completed Testing checklist, AI Review Report with cross-platform checks, Security Audit, and platform-specific Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% 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 change: fixing live-port indicator flickering by addressing scan debounce and re-initialization loop issues.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

@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: 1

🧹 Nitpick comments (2)
src/renderer/src/components/ports/WorkspacePortScanner.tsx (1)

67-69: ⚡ Quick win

Make scanTargetsSignature order-insensitive.

This signature is treated as a host-set key, but join('|') is order-sensitive. If host ordering changes with the same hosts, the effect will still re-initialize and can reintroduce null→rescan churn.

Proposed fix
 const scanTargetsSignature = useMemo(
-  () => scanTargets.map(workspacePortScanKeyForTarget).join('|'),
+  () => scanTargets.map(workspacePortScanKeyForTarget).sort().join('|'),
   [scanTargets]
 )
src/renderer/src/lib/workspace-port-scan-debounce.test.ts (1)

126-140: ⚡ Quick win

Add a regression that prunes state.failures for failure-only keys.

Current pruning coverage exercises keys present in lastGood, but not keys that only ever failed. Add that case to lock behavior and prevent stale failure streak reuse.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6437ac6c-dbe8-475a-8819-0a9cd2d9195c

📥 Commits

Reviewing files that changed from the base of the PR and between c8f583c and 3b53d0b.

📒 Files selected for processing (3)
  • src/renderer/src/components/ports/WorkspacePortScanner.tsx
  • src/renderer/src/lib/workspace-port-scan-debounce.test.ts
  • src/renderer/src/lib/workspace-port-scan-debounce.ts

@gsxdsm

gsxdsm commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Both nitpicks from the latest CodeRabbit review (reviewed at 3b53d0b6, before the follow-up commit) are already addressed in a78046c5b:

  • scanTargetsSignature order-insensitivity — now scanTargets.map(workspacePortScanKeyForTarget).sort().join('|'), so a reordered-but-unchanged host set no longer re-inits the scan.
  • Failure-only-key prune regression test — added prunes the failure counter for a failed-only host that disappears, and the prune now walks both lastGood and failures.

@AmethystLiang AmethystLiang 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.

Thanks — rebased and resolved against current main.

  • Keeps the last good per-host port scan through one transient failure, while a reachable empty result still clears immediately.
  • Limits host-change and advertised-URL refreshes to the affected host, avoiding unnecessary remote scan work.
  • Adds coverage for retention, host changes/removal, hidden-window resumption, and scan-state stability.

@AmethystLiang
AmethystLiang merged commit 7d216de into stablyai:main Jul 10, 2026
1 check passed
brennanb2025 added a commit that referenced this pull request Jul 10, 2026
#8174)

launch-agent-in-new-tab.ts crossed the 300-line oxlint max-lines limit
(303 counted lines) after #5510 and #7944 both grew it; verify only runs
on PRs, so the over-limit state landed on main via a merge race and now
fails lint for every open PR.

Move the web-runtime host launch branch (stale-local-tab pruning plus
createWebRuntimeSessionTerminal call) into launch-agent-web-host-tab.ts.
No behavior change; the i18n key is kept so locale catalogs are
untouched.

Co-authored-by: Brennan Benson <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…nit loop) (stablyai#5510)

* fix(ports): stop live-port indicator flickering on transient scan failures

A worktree's plug icon renders only when it has ports. The scanner polls
each execution host every 30s and, on a transient failure (SSH/IPC latency),
substituted an empty "unavailable" scan — dropping that host's ports for the
cycle and blinking the affected row's live-port indicator off, then on again
next poll.

Debounce per-host failures: reuse the host's last good scan until failures
reach a tolerance, so a single dropped poll no longer zeroes the row. A
reachable host reporting no ports has no unavailableReason, so a genuine port
close still clears immediately. Logic extracted to a pure helper with tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(ports): stop port scan re-init loop that flickered live-port icons

The scan poll effect depended on the `refresh` callback identity, which gets a
fresh reference whenever repos/settings are replaced (frequent in a busy
workspace). Each change re-ran the effect, which reset the scan to null and
kicked an immediate re-scan — a tight ~600ms loop that blinked the plug icon
off/on even though the scanned ports never changed.

Drive the poll and advertised-url effects off the latest refresh via a ref and
key them on stable value-signatures (enable state, active scan key, host-set
signature, runtime kind) instead of the callback identity. Incidental store
churn no longer re-inits the scanner; it still polls every 30s, reacts to
advertised-url events, and rescans on a real host-set or runtime change.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* Address PR review feedback (stablyai#5510)

- Prune the per-host failure counter for failed-only hosts that disappear,
  not just hosts present in lastGood — avoids an unbounded failures map and a
  stale failure streak if the host reappears
- Canonicalize scanTargetsSignature by sorting so a reordered-but-unchanged
  host set does not re-init the scan and reintroduce flicker

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>

* fix(ports): keep host scans targeted and stable

* test(ports): cover scan debounce state

---------

Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Co-authored-by: Jinjing <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#8174)

launch-agent-in-new-tab.ts crossed the 300-line oxlint max-lines limit
(303 counted lines) after stablyai#5510 and stablyai#7944 both grew it; verify only runs
on PRs, so the over-limit state landed on main via a merge race and now
fails lint for every open PR.

Move the web-runtime host launch branch (stale-local-tab pruning plus
createWebRuntimeSessionTerminal call) into launch-agent-web-host-tab.ts.
No behavior change; the i18n key is kept so locale catalogs are
untouched.

Co-authored-by: Brennan Benson <[email protected]>
@gsxdsm
gsxdsm deleted the gsxdsm/Fix-live-port-icon-flicker branch October 31, 2026 17:58
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.

3 participants