Skip to content

Conversation

@dimacurrentai
Copy link
Contributor

No description provided.

@dimacurrentai
Copy link
Contributor Author

running 1 test
MAROON STEP AT T=0ms
  value: FactorialInput(5)
  state: FactorialEntry, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Next([Value(FactorialInput(5)), Retrn(FactorialDone), Value(FactorialArgument(5)), State(FactorialRecursiveCall)])
MAROON STEP DONE

MAROON STEP AT T=0ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  state: FactorialRecursiveCall, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("f(5)", [Value(FactorialArgument(5)), State(FactorialRecursionPostWrite)])
MAROON STEP DONE

MAROON STEP AT T=0ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  state: FactorialRecursionPostWrite, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Sleep(LogicalTimeDeltaMs(250), [Value(FactorialArgument(5)), State(FactorialRecursionPostSleep)])
MAROON STEP DONE

MAROON STEP AT T=250ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  state: FactorialRecursionPostSleep, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Next([Value(FactorialArgument(5)), Retrn(FactorialRecursionPostRecursiveCall), Value(FactorialArgument(4)), State(FactorialRecursiveCall)])
MAROON STEP DONE

MAROON STEP AT T=250ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  state: FactorialRecursiveCall, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("f(4)", [Value(FactorialArgument(4)), State(FactorialRecursionPostWrite)])
MAROON STEP DONE

MAROON STEP AT T=250ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  state: FactorialRecursionPostWrite, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Sleep(LogicalTimeDeltaMs(200), [Value(FactorialArgument(4)), State(FactorialRecursionPostSleep)])
MAROON STEP DONE

MAROON STEP AT T=450ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  state: FactorialRecursionPostSleep, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Next([Value(FactorialArgument(4)), Retrn(FactorialRecursionPostRecursiveCall), Value(FactorialArgument(3)), State(FactorialRecursiveCall)])
MAROON STEP DONE

MAROON STEP AT T=450ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  state: FactorialRecursiveCall, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("f(3)", [Value(FactorialArgument(3)), State(FactorialRecursionPostWrite)])
MAROON STEP DONE

MAROON STEP AT T=450ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  state: FactorialRecursionPostWrite, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Sleep(LogicalTimeDeltaMs(150), [Value(FactorialArgument(3)), State(FactorialRecursionPostSleep)])
MAROON STEP DONE

MAROON STEP AT T=600ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  state: FactorialRecursionPostSleep, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Next([Value(FactorialArgument(3)), Retrn(FactorialRecursionPostRecursiveCall), Value(FactorialArgument(2)), State(FactorialRecursiveCall)])
MAROON STEP DONE

MAROON STEP AT T=600ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  state: FactorialRecursiveCall, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("f(2)", [Value(FactorialArgument(2)), State(FactorialRecursionPostWrite)])
MAROON STEP DONE

MAROON STEP AT T=600ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  state: FactorialRecursionPostWrite, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Sleep(LogicalTimeDeltaMs(100), [Value(FactorialArgument(2)), State(FactorialRecursionPostSleep)])
MAROON STEP DONE

MAROON STEP AT T=700ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  state: FactorialRecursionPostSleep, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Next([Value(FactorialArgument(2)), Retrn(FactorialRecursionPostRecursiveCall), Value(FactorialArgument(1)), State(FactorialRecursiveCall)])
MAROON STEP DONE

MAROON STEP AT T=700ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(1)
  state: FactorialRecursiveCall, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("f(1)", [Value(FactorialArgument(1)), State(FactorialRecursionPostWrite)])
MAROON STEP DONE

MAROON STEP AT T=700ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(1)
  state: FactorialRecursionPostWrite, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Sleep(LogicalTimeDeltaMs(50), [Value(FactorialArgument(1)), State(FactorialRecursionPostSleep)])
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(1)
  state: FactorialRecursionPostSleep, uses 1 argument(s) above as its local stack.
MAROON STEP RESULT
  Return(FactorialReturnValue(1))
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(2)
  value: FactorialReturnValue(1)
  state: FactorialRecursionPostRecursiveCall, uses 2 argument(s) above as its local stack.
MAROON STEP RESULT
  Return(FactorialReturnValue(2))
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(3)
  value: FactorialReturnValue(2)
  state: FactorialRecursionPostRecursiveCall, uses 2 argument(s) above as its local stack.
MAROON STEP RESULT
  Return(FactorialReturnValue(6))
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  retrn: FactorialRecursionPostRecursiveCall, awaiting to be `return`-ed into here.
  value: FactorialArgument(4)
  value: FactorialReturnValue(6)
  state: FactorialRecursionPostRecursiveCall, uses 2 argument(s) above as its local stack.
MAROON STEP RESULT
  Return(FactorialReturnValue(24))
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  retrn: FactorialDone, awaiting to be `return`-ed into here.
  value: FactorialArgument(5)
  value: FactorialReturnValue(24)
  state: FactorialRecursionPostRecursiveCall, uses 2 argument(s) above as its local stack.
MAROON STEP RESULT
  Return(FactorialReturnValue(120))
MAROON STEP DONE

MAROON STEP AT T=750ms
  value: FactorialInput(5)
  value: FactorialReturnValue(120)
  state: FactorialDone, uses 2 argument(s) above as its local stack.
MAROON STEP RESULT
  Write("5!=120", [State(Completed)])
MAROON STEP DONE

MAROON STEP AT T=750ms
  state: Completed, uses 0 argument(s) above as its local stack.
MAROON STEP RESULT
  Done
MAROON STEP DONE

test tests::test_factorial_task ... ok

@dimacurrentai
Copy link
Contributor Author

➜  ~/github/dimacurrentai/rust-experiments/step11_ws_state_machine/code git:(maroon_call_stack) npx wscat -c ws://0.0.0.0:3000/factorial/5
Connected (press CTRL+C to quit)
< f(5)
< f(4)
< f(3)
< f(2)
< f(1)
< 5!=120
Disconnected (code: 1006, reason: "")

(returned with delays, all as expected)

Copy link

@AdamEther AdamEther left a comment

Choose a reason for hiding this comment

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

Looking at this PR - nice work on the stack machine refactor! This is a solid architectural move toward a more formal execution model.

What I like:

  • The type-safe stack values are a big improvement over the previous runtime approach
  • Stack variable count validation at compile time is clever
  • The Retrn mechanism for proper call/return semantics is well thought out
  • Good incremental approach - keeping the old code commented out during transition

Feedback:

  • Those println! debug statements should probably be behind a debug flag or use proper logging before this goes to prod
  • The commented code is helpful for now but we should plan to clean it up in follow-up PRs
  • Minor: unused_validate_states_vec_or_panic function name suggests it should be removed?

Question: Is there a plan to migrate the other task types (Fibonacci, Divisors, etc.) to this new stack model? The current approach of only refactoring Factorial makes sense for proof-of-concept, but curious about the rollout strategy.

The test updates look good and the new execution model is much cleaner. The stack-based approach will definitely make it easier to reason about execution state.

LGTM with the minor cleanup suggestions!

@dkorolev
Copy link
Owner

Thanks! I'll merge this one for now, and try to find time this weekend to make the next batch of changes.

Cool PRs from you too!

@dkorolev dkorolev merged commit 0c8ee26 into dkorolev:main May 31, 2025
13 checks passed
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