Skip to content

perf(autoresearch): replace sleep-based reset waits with spin-poll - #2274

Merged
zackees merged 2 commits into
masterfrom
feat/spin-poll-boot-detection
Apr 17, 2026
Merged

zackees merged 2 commits into
masterfrom
feat/spin-poll-boot-detection

Conversation

@zackees

@zackees zackees commented Apr 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Eliminates fixed sleep(3.0) delays after device reset by using fbuild 2.1.16's reset_device(wait_for_output=True), which polls for serial output and returns as soon as the device reboots.

Before: reset + 3s sleep + 3s boot_wait = ~6s wasted per reset
After: reset + wait_for_output (~0.5s) + 1s boot_wait = ~1.5s

Changes

  • FbuildSerialAdapter.reset_device(): passes wait_for_output=True, timeout=5.0
  • gpio.py: removes sleep(3.0) after reset, reduces boot_wait from 3.0 to 1.0

Test plan

  • Unit tests pass (test_serial_interface_reset.py)
  • Autoresearch on real ESP32-S3: I2S test passes, all 4 patterns OK

Closes #2271
Depends on fbuild 2.1.16 (fbuild#68, fbuild#69)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved device reset and boot recovery to react to actual reset results rather than always using a fixed delay.
    • Reconnection now adjusts boot wait time dynamically, shortening waits after successful resets to speed initialization.
    • Reset operations now wait for device output (with a short timeout) to confirm progress, improving serial reliability.

…put=True)

FbuildSerialAdapter.reset_device() now passes wait_for_output=True
and timeout=5.0 to fbuild 2.1.16's SerialMonitor, which polls for
serial output instead of returning immediately. This eliminates the
fixed 3-second sleep after DTR reset.

- gpio.py: boot_wait reduced from 3.0 to 1.0 (device already producing output)
- serial_interface.py: passes wait_for_output=True, timeout=5.0 to fbuild

Typical ESP32-S3 USB-CDC: reset+ready in ~1.3s instead of 6s.

Closes #2271

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

coderabbitai Bot commented Apr 17, 2026 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 384787a2-a337-46e9-8305-72d504e18930

📥 Commits

Reviewing files that changed from the base of the PR and between 6a84fd6 and 68b4a18.

📒 Files selected for processing (1)
  • ci/autoresearch/gpio.py

📝 Walkthrough

Walkthrough

DTR-reset recovery now captures reset result and uses output-driven semantics; reconnect boot_wait is set dynamically (1.0s on successful reset, 3.0s on failure) instead of always sleeping 3s. Fbuild adapter reset_device forwards wait_for_output=True with a 5.0s output wait.

Changes

Cohort / File(s) Summary
Autoresearch DTR reset logic
ci/autoresearch/gpio.py
Capture reset_ok from reset calls; remove fixed post-reset sleep; compute boot_wait = 1.0 if reset_ok truthy else 3.0; call client.connect(boot_wait=boot_wait, drain_boot=True) accordingly.
Serial adapter reset semantics
ci/util/serial_interface.py
FbuildSerialAdapter.reset_device() now calls underlying monitor with wait_for_output=True and output_wait=5.0 so reset blocks until serial output (fallback and error handling unchanged).

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant TestRunner
    participant SerialInterface
    participant Device
    participant RpcClient

    TestRunner->>SerialInterface: request reset_device(board=None, wait_for_output=True)
    SerialInterface->>Device: toggle DTR / send reset
    Device-->>SerialInterface: emits serial output (boot signatures)
    SerialInterface-->>TestRunner: return reset_ok (True/False)
    alt reset_ok True
        TestRunner->>RpcClient: connect(boot_wait=1.0, drain_boot=True)
    else reset_ok False
        TestRunner->>RpcClient: connect(boot_wait=3.0, drain_boot=True)
    end
    RpcClient->>Device: read lines / drain boot output
    RpcClient-->>TestRunner: connected
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Possibly related PRs

Poem

🐰 A tiny hop, a gentle reset, I wait with twitching ear,
I listen for the boot-time beep that tells the board is near.
No idle seconds wasted now, we wake when output's born,
Quick as a thump, we bind and run—hello new silicon morn! 🥕

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Linked Issues check ❓ Inconclusive The PR partially addresses #2271's objectives: it implements reset_device(wait_for_output=True) for post-reset waits and reduces boot_wait timeouts, but does not implement signature-based polling in RpcClient.connect() as originally specified. Verify whether full signature-based polling in RpcClient.connect() was deferred to a future PR or if the current solution (reduced boot_wait + reset_device polling) adequately addresses the issue's core performance goals.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'perf(autoresearch): replace sleep-based reset waits with spin-poll' accurately captures the main change: converting fixed sleep delays to polling-based waits for device output during reset recovery.
Out of Scope Changes check ✅ Passed All changes in FbuildSerialAdapter and gpio.py are directly scoped to the reset-wait optimization objective; no extraneous modifications detected.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/spin-poll-boot-detection

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

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
ci/autoresearch/gpio.py (1)

5-13: ⚠️ Potential issue | 🔴 Critical

Import asyncio for the new fallback sleeps.

Lines 77 and 270 now call asyncio.sleep(...), but this module is not imported, so the pyserial fallback reset path raises NameError.

Proposed fix
 from __future__ import annotations
 
+import asyncio
 from dataclasses import dataclass
 from typing import TYPE_CHECKING

Also applies to: 77-77, 270-270

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@ci/autoresearch/gpio.py` around lines 5 - 13, The module uses asyncio.sleep
at lines referenced (fallback reset paths) but asyncio isn't imported; add
"import asyncio" to the top-level imports alongside the other imports in
ci.autoresearch.gpio so that the calls to asyncio.sleep(...) (used in the
fallback reset logic) don't raise NameError; ensure the import is placed with
the existing standard-library imports so it is available to the functions that
perform the pyserial fallback sleeps.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@ci/autoresearch/gpio.py`:
- Around line 70-77: The reset paths currently ignore the boolean result of
serial_interface.reset_device(...) and _pyserial_dtr_reset(port) and proceed
with a shortened reconnect boot_wait; change both locations (the block shown and
the similar code in run_pin_discovery / lines ~263-270) to check the return
value and fail fast if reset failed: call await
serial_interface.reset_device(...) and if it returns False raise or return a
descriptive error (do not continue with boot_wait=1.0), and similarly capture
the boolean from _pyserial_dtr_reset(port) and abort with a clear error instead
of proceeding to asyncio.sleep/short reconnect; ensure the reconnect logic only
uses the shortened boot_wait when the reset boolean is True.

In `@ci/util/serial_interface.py`:
- Around line 225-230: The reset_device call swallows KeyboardInterrupt and
silently returns False on failure; update the try/except around the await
self._run_in_thread(self._monitor.reset_device, ...) to first add an explicit
except KeyboardInterrupt: raise, change the positional args to keywords
(board=board, wait_for_output=True, timeout=5.0) when calling
self._monitor.reset_device via self._run_in_thread, and replace the generic
except Exception: return False behavior with either re-raising the exception or
logging a clear error and then raising (i.e., do not return False on failure) so
callers see the real failure.

---

Outside diff comments:
In `@ci/autoresearch/gpio.py`:
- Around line 5-13: The module uses asyncio.sleep at lines referenced (fallback
reset paths) but asyncio isn't imported; add "import asyncio" to the top-level
imports alongside the other imports in ci.autoresearch.gpio so that the calls to
asyncio.sleep(...) (used in the fallback reset logic) don't raise NameError;
ensure the import is placed with the existing standard-library imports so it is
available to the functions that perform the pyserial fallback sleeps.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2314f89b-9f7c-4522-9b9f-0b3c3f41e483

📥 Commits

Reviewing files that changed from the base of the PR and between c57bcec and 6a84fd6.

📒 Files selected for processing (2)
  • ci/autoresearch/gpio.py
  • ci/util/serial_interface.py

Comment thread ci/autoresearch/gpio.py Resolved
Comment thread ci/util/serial_interface.py Resolved
…er wait on failure

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@zackees
zackees merged commit 4326797 into master Apr 17, 2026
63 of 77 checks passed
@zackees
zackees deleted the feat/spin-poll-boot-detection branch April 17, 2026 21:39
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.

feat(autoresearch): replace RpcClient boot_wait sleep with signature polling

1 participant