Skip to content

fix: validate logic capture modes before clearing data - #313

Open
YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/logic-capture-mode-preflight
Open

YashAnand69 wants to merge 1 commit into
fossasia:mainfrom
YashAnand69:fix/logic-capture-mode-preflight

Conversation

@YashAnand69

@YashAnand69 YashAnand69 commented Oct 10, 2026 •

Copy link
Copy Markdown

An invalid logic-analyzer capture mode currently raises only after stopping the previous capture, clearing the device buffer and invalidating its local metadata. A typo can therefore destroy an acquisition before reporting the error.

Resolve the mode names before changing channel mappings or issuing device commands. Preserve the existing KeyError and valid-mode protocol behavior. This change does not expand the set of accepted mode names.

Validation: ten regressions fail on unchanged main and six valid-mode controls pass. All 16 new cases plus 33 existing hardware-independent CLI, Pico and gas-sensor tests pass (49 total). Configured Black, Flake8, Bandit and pydocstyle checks, package and Sphinx builds pass; existing documentation warnings remain. Tests record device commands and verify all prior channel metadata is preserved. No physical-device or full hardware integration validation was performed. Developed and tested with Codex assistance.

No matching issue ownership claim or overlapping open PR found. Capture timing, event counts and firmware protocol remain unchanged.

Summary by Sourcery

Validate logic-analyzer capture modes before modifying acquisition state or issuing device commands.

Bug Fixes:

  • Validate all logic-analyzer capture modes before stopping an existing capture or clearing device and local acquisition data, preserving prior state when a mode is invalid.

Tests:

  • Add hardware-independent regression coverage for invalid modes preserving acquisition state and valid modes retaining expected capture behavior.

Summary by CodeRabbit

  • Bug Fixes
    • Invalid logic-analyzer capture modes are now rejected before device commands, channel settings, or existing capture state are changed.
    • Valid capture modes continue to configure the selected channel and issue the expected capture commands.

@sourcery-ai

sourcery-ai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

The capture path now resolves mode names before any state mutation or device command, preventing invalid requests from destroying an existing acquisition while preserving KeyError behavior and the valid-mode protocol. New mock-connection tests cover state preservation across channel and mode combinations and confirm unchanged behavior for all supported modes.

Sequence diagram for logic analyzer capture mode validation

sequenceDiagram
    participant Caller
    participant LogicAnalyzer
    participant Device

    Caller->>LogicAnalyzer: capture(channels, events, modes)
    LogicAnalyzer->>LogicAnalyzer: MODES[m]
    alt invalid mode
        MODES[m]-->>LogicAnalyzer: KeyError
        LogicAnalyzer-->>Caller: KeyError
        Note over LogicAnalyzer,Device: Existing acquisition and device state remain unchanged
    else valid modes
        LogicAnalyzer->>LogicAnalyzer: _check_arguments(channels, events)
        LogicAnalyzer->>LogicAnalyzer: stop()
        LogicAnalyzer->>Device: clear_buffer(CP.MAX_SAMPLES)
        LogicAnalyzer->>LogicAnalyzer: _invalidate_buffer()
        LogicAnalyzer->>Device: _configure_trigger(channels)
        LogicAnalyzer->>Device: capture protocol commands
    end
Loading

File-Level Changes

Change Details Files
Validate every requested capture mode before mutating analyzer state or communicating with the device.
  • Resolve mode names through the existing MODES mapping at the start of capture.
  • Leave existing KeyError behavior and accepted mode names unchanged.
  • Keep channel validation, stopping, buffer clearing, metadata invalidation, and protocol setup after validation.
pslab/instrument/logic_analyzer.py
Add hardware-independent regression coverage for invalid-mode state preservation and valid-mode protocol compatibility.
  • Use a command-recording connection to verify invalid captures issue no device commands and preserve channel metadata and mappings.
  • Cover varied channel inputs and invalid modes, including mixed valid/invalid requests.
  • Verify every valid mode still starts capture and sets expected channel state and protocol commands.
tests/test_logic_capture_modes.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a74a52f8-5a21-483f-943b-0fbc36fb4bf0

📥 Commits

Reviewing files that changed from the base of the PR and between 08de0d0 and 666a0c7.


📒 Files selected for processing (2)
  • pslab/instrument/logic_analyzer.py
  • tests/test_logic_capture_modes.py

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.



📝 Walkthrough

Walkthrough

LogicAnalyzer.capture now resolves mode names before checking channels or changing capture state. Tests cover invalid modes that leave device and analyzer state unchanged, and valid modes that issue expected commands and update channel state.

Changes

Logic capture mode validation

Layer / File(s) Summary
Resolve modes before capture setup
pslab/instrument/logic_analyzer.py, tests/test_logic_capture_modes.py
capture converts mode names through MODES before argument checks and capture setup. Tests verify that invalid modes raise KeyError without changing device or analyzer state, and that valid modes issue expected commands and update channel state.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix


Merge Risk | ⚪ Minimal · up to 666a0

Merge Risk: ⚪ Minimal · up to 666a0

No actionable issue remains on the supplied evidence; the change is ready to merge after normal checks.

Security Architecture Review

Security architecture risk: ⚪ Minimal · up to 666a0

The change prevents invalid mode names from destroying an existing acquisition. Recognized modes retain the existing device-control behavior, and no material security risk was identified. Calls containing multiple invalid arguments may now report a different error first.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The affected destructive outcome is loss of the attached analyzer’s acquisition and local channel metadata. The inspected production change adds no caller, connection, device command, or privilege; it removes setup side effects for unrecognized modes.

Trust Boundaries and Controls

  • observed — Caller-supplied mode names are resolved through the fixed mapping before the method reaches device-control operations. An unrecognized name therefore cannot reach stop, clear-buffer, or capture-start through this invocation. This is an input-validation ordering improvement, not a new authentication or authorization boundary.

Resilience and Maintainability Implications

  • inferred — The reorder does not introduce atomic setup or automatic recovery from transport failures. Existing helpers can still fail after local or device state changes. Those limitations predate this PR; recognized-mode failure and recovery paths remain unchanged, while invalid-mode rejection now preserves the previous acquisition.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: validating logic capture modes before clearing data.
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.

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@sourcery-ai sourcery-ai 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.

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

This branch has not been deployed

No deployments
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