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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Deferred / circular wiring performed during startup.
//!
//! Several runtime components hold references to each other in cycles that
//! cannot be expressed with plain owning `Arc`s during construction:
//!
//! - **Agent ↔ SpawnAgentTool**: the agent owns the spawn tool (via its tool
//! list), and the spawn tool needs to spawn sub-agents through the agent.
//! The spawn tool therefore holds a `Weak<Agent>`, installed *after* the
//! agent is constructed via `set_agent`.
//! - **Agent ↔ self**: the agent spawns background tasks that re-enter the
//! agent loop, so it keeps a `Weak<Agent>` self-reference (`set_self_ref`).
//! - **Tools / Agent ↔ ChannelHub**: the `ChannelHub` is built *after* the
//! agent and tools (it needs the agent for delivery notes), but the spawn
//! tool, terminal tool, CLI-agent tool, and the agent itself all need to
//! push notifications/progress through the hub. They each hold a
//! `Weak<ChannelHub>`, installed once the hub exists.
//!
//! Using `Weak` references breaks the reference cycles so the graph can be
//! dropped cleanly, but it forces the wiring to be *deferred* until both
//! endpoints exist. This module centralizes those deferred calls so the
//! ordering and arguments live in one place rather than being scattered
//! through `core.rs`. The two phases mirror the two construction moments:
//! [`wire_agent_cycles`] runs right after the agent is built (before the hub
//! exists), and [`wire_hub_cycles`] runs right after the `ChannelHub` is built.
use Arc;
use crateAgent;
use crateChannelHub;
/// Phase 1: wiring that only needs the constructed [`Agent`] (no hub yet).
///
/// Must be called immediately after the agent is constructed, in the same
/// order as the original inline calls:
/// 1. give the spawn tool a weak agent reference,
/// 2. give the agent a weak self-reference for background task spawning.
pub async
/// Phase 2: wiring that needs the constructed [`ChannelHub`].
///
/// Must be called immediately after the hub is constructed, in the same order
/// as the original inline calls:
/// 1. spawn tool → hub (background mode notifications),
/// 2. terminal tool → hub + agent (background command progress/completion and
/// loop re-engagement),
/// 3. CLI-agent tool → hub,
/// 4. agent → hub (background task notifications).
///
/// Takes ownership of the optional tool handles since they are not needed after
/// this point in startup (matching the original inline consumption).
pub async