Skip to content

Refactor FxA state machine to simplify transitions#7359

Open
skhamis wants to merge 1 commit into
mozilla:mainfrom
skhamis:fxa-state-machine-refactor
Open

Refactor FxA state machine to simplify transitions#7359
skhamis wants to merge 1 commit into
mozilla:mainfrom
skhamis:fxa-state-machine-refactor

Conversation

@skhamis
Copy link
Copy Markdown
Contributor

@skhamis skhamis commented May 8, 2026

Now that FxiOS has the new state machine, we're able to now re-look at the state machine system as a whole and we realized we can simplify it now that we're not merging with existing systems on the consumers. My main goals were:

  1. Make it easy to reason about how clients move through the FxA flows
  2. Make it simple to add/modify existing states
  3. Try limit any API churn (for now), and simplify the internal parts before any public-facing API changes (if any)

@bendk had to manage two state machines in android which was the reason for the initial system, but now that they've migrated fully over, the idea came that we can remove the internal state completely and move to a flat-state-machine style. It's in draft now as I think this is a strong denature and want to get thoughts on this before moving on.

Pull Request checklist

  • Breaking changes: This PR follows our breaking change policy
    • This PR follows the breaking change policy:
      • This PR has no breaking API changes, or
      • There are corresponding PRs for our consumer applications that resolve the breaking changes and have been approved
  • Quality: This PR builds and tests run cleanly
    • Note:
      • For changes that need extra cross-platform testing, consider adding [ci full] to the PR title.
      • If this pull request includes a breaking change, consider cutting a new release after merging.
  • Tests: This PR includes thorough tests or an explanation of why it does not
  • Changelog: This PR includes a changelog entry in CHANGELOG.md or an explanation of why it does not need one
    • Any breaking changes to Swift or Kotlin binding APIs are noted explicitly
  • Dependencies: This PR follows our dependency management guidelines
    • Any new dependencies are accompanied by a summary of the due diligence applied in selecting them.

@skhamis skhamis requested a review from bendk May 8, 2026 21:41
@skhamis skhamis force-pushed the fxa-state-machine-refactor branch from 6b5e283 to bd490c0 Compare May 8, 2026 23:12
Copy link
Copy Markdown
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

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

I like this approach a lot, it feels way more direct than having the internal states implicitly trigger a FirefoxAccount method call. I just left some comments/questions about the details.

impl StateMachineErr {
/// Programming errors (logic / invalid-transition) become `Fatal` and
/// ignore `target_if_handled`; everything else becomes `Handled`.
pub fn from_cause(cause: Error, target_if_handled: FxaState) -> Self {
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.

Nit: StateMachineErr:from_cause() and Result<T, StateMachineError>::err_state() do pretty much the same thing but have very different names. I can't think of a good single name, but maybe we could get a pair of names that seem related like StateMachineErr::new() and Result<T, StateMachineError>::map_err_to_state_machine_err() . I don't really love those names either though, so whatever you want to go with is good with me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah good callouts, I'll go to ::new() as that feels the most "rust-y". I feel like for chaining on the result maybe going to_state_machine_err has the strongest readability imo.

let scope_refs: Vec<&str> = scopes.iter().map(String::as_str).collect();
let oauth_url = account
.begin_oauth_flow(&service, &scope_refs, &entrypoint)
.err_state(|| S::Disconnected)?;
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.

I think there could be a default handler that would keep the user on their current state. Would that be better than having to write out the err_state for every call? I'm not sure, but I think I like having to write out the error handling logic explicitly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah this is a tough one, I do agree with writing out the error handling logic explicitly cause you kinda need to think about "what does this mean and where to do we go", but I think keeping the user in the current state as default might bite us later and I don't think we'll be adding that many states for FxA that we'd need to be ergonomic about it? Happy to still continue if we think so.

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.

Nope, I was just thinking out loud and wondering what you thought. The current design is good for me, let's keep using it.

@skhamis skhamis force-pushed the fxa-state-machine-refactor branch from bd490c0 to 2ceb842 Compare May 12, 2026 01:03
@skhamis skhamis marked this pull request as ready for review May 12, 2026 19:22
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.

2 participants