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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Shared helpers for background-task resume across channels (#731).
//!
//! When a detached long command finishes, the [`BackgroundTaskManager`] calls
//! the surface's `MessageEnqueueCallback` with the originating `session_id` and
//! a synthetic completion message. Each channel's callback resolves its delivery
//! target, runs a fresh turn feeding the completion text as the prompt, and
//! delivers the result back to that target. The turn-running and weak-agent
//! plumbing is identical across channels and lives here; only target lookup and
//! the SDK-specific send call are per-channel.
//!
//! [`BackgroundTaskManager`]: crate::brain::agent::service::BackgroundTaskManager
use crateAgentService;
use ;
use Uuid;
/// Weak handle to the agent, filled after the service is built (it cannot be
/// captured at service-construction time — the service is mid-build). Every
/// channel's enqueue callback closes over one of these.
pub type AgentHolder = ;
/// A fresh, empty holder to hand to `build_enqueue_callback` before the agent
/// exists; fill it with [`fill`] once the service is constructed.
pub
/// Store a weak ref to the just-built agent so the enqueue callback can reach it.
pub
/// Upgrade the weak holder to a live agent, or `None` if the service is gone.
pub
/// Run the background-completion turn for `session_id`, feeding `context_text`
/// as the prompt on `channel`/`target`. Returns the response content, or `None`
/// when the turn errored or produced nothing to deliver. Delivery to the
/// channel's SDK is the caller's job (it differs per surface).
///
/// Tracked with origin `system` (#12): a completion turn killed mid-tool must
/// be visible to boot recovery, which re-delivers the push instead of
/// replaying the LLM turn.
pub async
/// How long a channel SDK handle may take to become ready after spawn before
/// a pending wake stops waiting and parks instead (#1242).
///
/// Covers the boot race observed in the 2026-08-26/27 logs: an enqueue
/// callback fired while the bot was still authenticating and the wake was
/// dropped outright. The bound matches the pre-existing inline waits
/// (telegram `wait_for_bot`, the ui.rs startup-resume loop) so all callers
/// share one number.
pub const READY_WAIT_SECS: u64 = 30;
/// Poll `fetch` once a second until it yields a value or [`READY_WAIT_SECS`]
/// elapses.
///
/// Replaces the one-shot handle fetches that dropped the wake whenever the
/// SDK client was not ready at the exact instant of the call (#1242).
/// Checks before sleeping, so an already-ready handle costs zero delay.
pub async