Workspace/Coding labs
Loading progress

Replay a bounded agent lifecycle

Intermediate70 min

Implement replay_run(events, max_calls) with a single pending call, correlated observations, a tool budget, evidence-required completion, and terminal-state protection.

Your task

  1. Start in ready with zero calls. Accept a list of dictionaries and a nonnegative exact integer max_calls.
  2. Support start {type}, call {type,id}, result {type,id,value}, finish {type,answer}, cancel {type}, and error {type,message}; all named data fields must be nonempty strings.
  3. start moves ready to running. call is allowed only while running, must use a previously unseen ID, consumes budget, and moves to waiting.
  4. result is allowed only while waiting and must match the pending ID; store its string value and return to running.
  5. finish is allowed only while running with at least one stored observation, then enters completed. cancel is allowed from any nonterminal state. error is allowed while running or waiting.
  6. Reject invalid shapes, illegal transitions, budget overflow, and any event after a terminal state with ValueError. Return state, calls, pending, observations, and answer; incomplete traces preserve their current state.

Examples

EXAMPLE 1

Inputstart, call(c1), result(c1,"A7=8"), finish("A7 has 8")

Outputstate=completed, calls=1, pending=None

The required observation precedes completion.
EXAMPLE 2

Inputstart, call(c1)

Outputstate=waiting, calls=1, pending="c1"

End of the event list is not completion.
solution.pyPython 3.12