Reduce replayed task events without duplicate accounting
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
- 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.
- 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.
- Sort unique events by seq. Sequence numbers need not be contiguous. Start at new, which can transition only to queued.
- 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.
- 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}
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor