1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//! Parallel tool-dispatch types.
//!
//! The per-turn loop in `stream/mod.rs` plans tool calls serially (running the
//! `before_tool` hook gate), then dispatches the allowed ones in parallel via a
//! `FuturesUnordered` set bounded by a `Semaphore`. This module owns the small
//! plan enum that bridges the two phases.
//!
//! Per ADR 0016 (Revised 2026-05-26), each `Allowed` plan carries an optional
//! `conflict_key`. Plans sharing the same key serialize via a per-key
//! `tokio::sync::Mutex` (acquired FIFO) so two writes to the same target can't
//! interleave non-deterministically. Plans with `conflict_key = None` (the
//! default) parallelize freely as before.
use HashMap;
use Arc;
use ToolResultBlock;
use Mutex;
// ---------------------------------------------------------------------------
// Per-turn dispatch plan
// ---------------------------------------------------------------------------
/// A single tool dispatch plan, produced by the serial `before_tool` gate.
///
/// `original_index` is the position of the corresponding `ContentBlock::ToolUse`
/// within the assistant message; it's used to reorder results back into
/// assistant-message order for history.
pub
/// Build per-key serialization mutexes covering every distinct non-`None`
/// `conflict_key` in the plan list. Returned map is empty when no plan has a
/// conflict key — the common case.
pub