Workspace/Coding labs
Loading progress

Reduce replayed task events without duplicate accounting

Advanced75 min

Implement reduce_task_events(events) for one application task stream, validating lifecycle transitions, sorting out-of-order events, deduplicating exact replays, and summing each event cost once.

Your task

  1. Each event is a dict containing exactly event_id, seq, state, and cost. event_id is a nonempty string, seq a positive integer, and cost a nonnegative integer; booleans are invalid.
  2. Identical repeated event IDs are replays and count once. The same ID with different content or different IDs with the same sequence number raise ValueError.
  3. Sort unique events by seq. Sequence numbers need not be contiguous. Start at new, which can transition only to queued.
  4. Allow queued to working, failed, or canceled; working to awaiting_input, completed, failed, or canceled; awaiting_input to working, failed, or canceled. Terminal states have no outgoing transitions.
  5. Return {"state": final_state, "total_cost": summed_cost, "event_count": unique_count}. Empty input returns new, zero cost, zero events. This is an application lifecycle simulation, not an A2A schema.

Examples

EXAMPLE 1

Inputcompleted(seq=4,cost=3), queued(seq=1,cost=0), working(seq=2,cost=2), replay of working

Output{"state": "completed", "total_cost": 5, "event_count": 3}

Sorting recovers event order, and the replay contributes no additional cost.
solution.pyPython 3.12