Skip to content

Add AggregatedStates and state machine#866

Open
cappel89 wants to merge 13 commits into
mainfrom
feature/add_bl_state_machine_recov
Open

Add AggregatedStates and state machine#866
cappel89 wants to merge 13 commits into
mainfrom
feature/add_bl_state_machine_recov

Conversation

@cappel89
Copy link
Copy Markdown
Member

@cappel89 cappel89 commented May 4, 2026

Summary

This PR introduces a new state: AggregatedStates, the state machine and a scan to move between labels of the aggregated scan. To test, use the config at the bottom.

A few remarks

  • In a follow-up PR, I would like to generalize the state config to allow any BeamlineState to be specified in that YAML file, and the state machine will then simply load the full config
  • Currently, there is no ProgressBar Update, as there appears to be some race condition for moving to labels which require a motor to move, and also the set a signal of that same motor. The IPythonLiveUpdates struggle with this, I have to investigate this in more detail and might need some help (@wakonig )
  • Note: There is a special syntax to access user_parameters "user_parameter:test". This is a dynamic assignment. Currently, there is still a bug that changing the user_parameter will not yet trigger a re-evaluation of the state. This has to be resolved, but we should first agree that we would like to support this, or whether the user_parameter should simply be read upon creation and then remain constant for the state

Besides the two mentioned topics, the PR is ready to be merged. Simply use the config below, and follow the example to load it in the IPythonClient.

alignment: # AggregatedStateConfig -> can have different labels and for each label, different devices
  transition_metadata: # optional field for metadata for each label
    field: value 
  devices:
    samx:
      value: 0
      abs_tol: 0.1
      low_limit:
        value: -20
        abs_tol: 0.1
      high_limit:
        value: 20
        abs_tol: 0.1
      signals:
        velocity:
          value: 5
          abs_tol: 0.1
    bpm4i:
      value: 100
      abs_tol: 10
measurement:
  devices:
    samx:
      value: "user_parameter:test"
      abs_tol: 0.1
      signals:
        velocity:
          value: 20
    samy:
      value: 0
      abs_tol: 0.1
test:
  devices:
    samy:
      value: 0
      abs_tol: 0.1
    bpm4i:
      value: 100
      abs_tol: 10

Assuming this config is stored under state_config.yaml, you can now load it as follows

filename = "./state_config.yaml"
bec.state_machine.load_from_config(name="my_state", config_path=filename)

#closes #831

Copilot AI review requested due to automatic review settings May 4, 2026 13:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds aggregated beamline state support to the BEC stack: a new AggregatedState model/evaluator in bec_lib, a client-facing BeamlineStateMachine loader, and a new scan intended to transition the beamline between aggregated-state labels.

Changes:

  • Added aggregated state config/models and runtime evaluation logic in bec_lib.bl_states.
  • Added a client-side BeamlineStateMachine API and exposed it on BECClient.
  • Added a new server-side _v4_state_transition scan plus tests for aggregated-state/state-machine behavior.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
bec_server/bec_server/scan_server/scans/state_transition_scan.py New v4 scan for moving devices/signals toward a target aggregated-state label.
bec_lib/tests/test_beamline_states.py Expanded tests for aggregated states and the new state-machine loader/update API.
bec_lib/bec_lib/client.py Exposes state_machine alongside beamline_states on the client.
bec_lib/bec_lib/bl_states.py Adds aggregated-state config models, signal resolution, subscriptions, caching, and evaluation.
bec_lib/bec_lib/bl_state_machine.py New helper for loading/updating aggregated state definitions from YAML or dicts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread bec_server/bec_server/scan_server/scans/state_transition_scan.py Outdated
Comment thread bec_server/bec_server/scan_server/scans/state_transition_scan.py Outdated
Comment thread bec_server/bec_server/scan_server/scans/state_transition_scan.py
Comment thread bec_lib/bec_lib/bl_states.py
Comment thread bec_lib/bec_lib/bl_state_machine.py Outdated
Comment thread bec_server/bec_server/scan_server/scans/state_transition_scan.py
Comment thread bec_lib/bec_lib/bl_states.py
Copy link
Copy Markdown
Contributor

@d-perl d-perl left a comment

Choose a reason for hiding this comment

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

I think the concept is good, some comments in code

Comment thread bec_server/bec_server/scan_server/scans/state_transition_scan.py
Comment thread bec_lib/bec_lib/bl_states.py Outdated
Comment thread bec_lib/bec_lib/bl_states.py
Comment thread bec_lib/bec_lib/bl_states.py Outdated
Comment thread bec_lib/bec_lib/bl_states.py
Comment thread bec_lib/bec_lib/bl_states.py Outdated
@cappel89 cappel89 force-pushed the feature/add_bl_state_machine_recov branch 2 times, most recently from ab0e17c to 8dbb9e3 Compare May 12, 2026 14:36
@cappel89 cappel89 force-pushed the feature/add_bl_state_machine_recov branch from def9e20 to fac5fc1 Compare May 19, 2026 08:24
@cappel89 cappel89 force-pushed the feature/add_bl_state_machine_recov branch from a929302 to 2a99e7a Compare May 19, 2026 12:58
@cappel89 cappel89 requested a review from a team May 19, 2026 13:13
@cappel89 cappel89 self-assigned this May 19, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 19, 2026

Codecov Report

❌ Patch coverage is 88.13559% with 49 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
..._server/scan_server/scans/state_transition_scan.py 74.45% 20 Missing and 15 partials ⚠️
bec_lib/bec_lib/bl_states.py 95.63% 4 Missing and 6 partials ⚠️
...rver/bec_server/scan_server/tests/scan_fixtures.py 78.94% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

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