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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
//! Rebuild Tool
//!
//! Lets the agent build OpenCrabs from source and reload automatically.
//! The build runs as a DETACHED background command through the same
//! `BackgroundTaskManager` every long shell command uses (#1748), so the
//! agent turn isn't blocked for the minutes a release build takes and the
//! surfaces that show detached progress (live timer, tasks_list, status
//! files) work for rebuilds too. A rebuild-specific completion hook
//! exec-restarts into the new binary when the build finishes.
use super::error::Result;
use super::r#trait::{Tool, ToolCapability, ToolExecutionContext, ToolResult};
use async_trait::async_trait;
use serde_json::Value;
use std::path::PathBuf;
use uuid::Uuid;
/// Map an originating channel + chat id (+ optional forum topic) to a cron
/// `deliver_to` target so the background rebuild can report completion and
/// failure into the chat that asked (#305). Only channels with a scheduler
/// delivery arm map; the TUI has no channel target and everything else
/// returns None.
///
/// Telegram forum topics ride the #1451 grammar (`telegram:chat:thread`,
/// parsed by the scheduler's `parse_telegram_target`): when the pending
/// request carries an origin topic the report lands IN that topic (#1457),
/// otherwise the plain `telegram:chat` form keeps the historical default
/// topic delivery. Discord/Slack have no thread component here.
pub(crate) fn rebuild_deliver_target(
channel: &str,
chat_id: Option<&str>,
thread_id: Option<&str>,
) -> Option<String> {
let chat_id = chat_id?.trim();
if chat_id.is_empty() {
return None;
}
match channel {
"telegram" => {
let thread = thread_id.map(str::trim).filter(|t| !t.is_empty());
match thread {
Some(t) => Some(format!("telegram:{chat_id}:{t}")),
None => Some(format!("telegram:{chat_id}")),
}
}
"discord" | "slack" => Some(format!("{channel}:{chat_id}")),
_ => None,
}
}
/// The detached build command: identical compiler semantics to
/// `SelfUpdater::build` (`RUSTFLAGS="-C target-cpu=native"` release build).
/// Source-tree prep (lazy clone / `git pull --ff-only`) happens in-process
/// BEFORE the spawn, so this is a pure compile step the BackgroundTaskManager
/// can time, log and account like any detached command.
pub(crate) fn detached_build_command() -> String {
"RUSTFLAGS='-C target-cpu=native' cargo build --release".to_string()
}
/// Completion hook for the detached rebuild (#1748). Runs INSTEAD of the
/// generic background-task delivery:
///
/// - success: persist the completion report (the post-exec wake-up turn
/// reads it back, #1105), fan the status out to the originating channel
/// (#305) and AWAIT those sends before exec() replaces the process
/// (#1105), then exec-restart into the fresh binary.
/// - failure: deliver the compiler-output tail into the originating session
/// through the one gated route (TUI and channel sessions alike; replaces
/// the old TUI-only `SessionNotifier`, #304) and into the channel target.
/// Nothing restarts.
pub(crate) fn rebuild_completion_hook(
session_id: Uuid,
deliver_to: Option<String>,
project_root: PathBuf,
fallback_binary: PathBuf,
msg_service: crate::services::MessageService,
mgr: std::sync::Arc<crate::brain::agent::service::background_tasks::BackgroundTaskManager>,
) -> crate::brain::agent::service::background_tasks::CompletionHook {
use crate::brain::agent::service::background_tasks::HookContext;
Box::new(move |ctx: HookContext| {
Box::pin(async move {
let result = ctx.result;
if result.success {
// Same binary resolution as SelfUpdater::build_streaming:
// prefer the freshly built artifact, fall back to the
// running exe path.
let built = project_root
.join("target")
.join("release")
.join("opencrabs");
let built_path = if built.exists() {
built
} else {
fallback_binary
};
// Persist the completion report to the session DB so the
// agent sees context about what triggered the reload on the
// next turn after the exec restart (#1105).
let report = format!(
"✅ Background rebuild succeeded — binary at {}. Hot-reloading now.",
built_path.display()
);
if let Err(e) = msg_service
.create_message(session_id, "assistant".to_string(), report)
.await
{
tracing::error!("rebuild: failed to persist completion report: {e}");
}
// Pre-exec warning (#1748 bonus): exec kills every in-flight
// detached task. The rebuild itself is already marked
// finished by the manager, so anything still running for
// this session is OTHER work about to be interrupted; say
// so before it happens, not just in the post-restart report.
let others = mgr.running_tasks(session_id);
if !others.is_empty() {
let names = others
.iter()
.map(|t| t.label.as_str())
.collect::<Vec<_>>()
.join(", ");
tracing::warn!(
"rebuild: exec will interrupt in-flight detached tasks for session \
{session_id}: {names}"
);
let note = format!(
"⚠️ Reloading now: these detached tasks were still running and \
get interrupted by the restart: {names}"
);
if let Err(e) = msg_service
.create_message(session_id, "assistant".to_string(), note)
.await
{
tracing::error!("rebuild: failed to persist pre-exec warning: {e}");
}
}
// Channel fan-out, awaited BEFORE exec() so the detached
// sends aren't killed mid-flight (#1105).
if let Some(ref target) = deliver_to
&& let Some(h) = crate::cron::scheduler::deliver_result(
target,
"opencrabs rebuild",
"✅ Rebuilt from source — reloading into the new binary now.",
None,
None,
)
.await
&& let Err(e) = h.await
{
tracing::warn!("rebuild: completion notice task failed: {e}");
}
// exec() replaces the entire process, this hook included.
if let Err(e) = crate::brain::SelfUpdater::restart_into(&built_path, session_id) {
tracing::error!("rebuild: restart into {} failed: {e}", built_path.display());
// The user was told a reload is coming; a failed restart
// must not leave them waiting on one (#304 spirit).
let note = format!("⚠️ Rebuild finished but the reload failed: {e}");
crate::brain::agent::service::session_routes::deliver_to_session(
session_id,
crate::brain::agent::service::QueuedUserMessage::plain(note),
true,
);
}
} else {
// Cap the compiler dump: chats don't want 2 MB of cargo
// output, the last stretch carries the actual errors.
let mut tail = result.output.as_str();
if tail.len() > 4000 {
let mut cut = tail.len() - 4000;
while cut < tail.len() && !tail.is_char_boundary(cut) {
cut += 1;
}
tail = tail.get(cut..).unwrap_or(tail);
}
let msg = format!("⚠️ Background rebuild failed:\n{tail}");
// Session route: reaches the originating TUI session AND
// channel-owned sessions (replaces the old TUI-only notifier).
crate::brain::agent::service::session_routes::deliver_to_session(
session_id,
crate::brain::agent::service::QueuedUserMessage::plain(msg.clone()),
true,
);
// Channel target (parity with the old deliver_rebuild_status):
// detached send, no exec follows a failure.
if let Some(ref target) = deliver_to
&& let Some(h) = crate::cron::scheduler::deliver_result(
target,
"opencrabs rebuild",
&msg,
None,
None,
)
.await
&& let Err(e) = h.await
{
tracing::warn!("rebuild: completion notice task failed: {e}");
}
}
})
})
}
/// Shared rebuild launch path (#1748): prep the source tree synchronously,
/// resolve the originating channel, then hand a pure compile to the detached
/// manager with the exec-restart hook attached. Used by the agent tool AND
/// the TUI /rebuild command so both get identical behavior.
pub(crate) async fn run_detached_rebuild(
mgr: &std::sync::Arc<crate::brain::agent::service::background_tasks::BackgroundTaskManager>,
session_id: Uuid,
service_context: &crate::services::ServiceContext,
) -> anyhow::Result<()> {
// Prepare the source tree BEFORE detaching: auto_detect + lazy
// clone / `git pull --ff-only` are quick when the source exists, and
// their failures should reach the requester synchronously instead of as
// a detached command that dies seconds in.
let updater =
crate::brain::SelfUpdater::auto_detect().map_err(|e| anyhow::anyhow!("rebuild: {e}"))?;
updater
.ensure_source_tree()
.map_err(|e| anyhow::anyhow!("rebuild: failed to prepare source tree: {e}"))?;
let project_root = updater.project_root().to_path_buf();
let fallback_binary = updater.binary_path().to_path_buf();
// Resolve WHERE this turn came from so the build's completion and
// failure notices land back in that chat (#305). The pending-request
// row for the current turn is alive while this runs and carries
// channel + chat id. TUI turns map to None: the session route in the
// hook covers TUI delivery.
let deliver_to = match crate::db::PendingRequestRepository::new(service_context.pool().clone())
.find_latest_for_session(session_id)
.await
{
Ok(Some(req)) => {
let target = rebuild_deliver_target(
&req.channel,
req.channel_chat_id.as_deref(),
req.channel_thread_id.as_deref(),
);
match &target {
Some(t) => tracing::info!("rebuild: status will be delivered to {t}"),
None => tracing::debug!(
"rebuild: no channel delivery target for channel '{}'",
req.channel
),
}
target
}
Ok(None) => None,
Err(e) => {
tracing::warn!("rebuild: pending-request lookup failed (no delivery): {e}");
None
}
};
let msg_service = crate::services::MessageService::new(service_context.clone());
mgr.clone().spawn_command_with_hook(
session_id,
project_root.clone(),
"opencrabs rebuild".to_string(),
detached_build_command(),
rebuild_completion_hook(
session_id,
deliver_to,
project_root,
fallback_binary,
msg_service,
std::sync::Arc::clone(mgr),
),
);
Ok(())
}
/// Agent-callable tool that runs a background rebuild from source.
pub struct RebuildTool;
impl RebuildTool {
pub fn new() -> Self {
Self
}
}
impl Default for RebuildTool {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl Tool for RebuildTool {
fn name(&self) -> &str {
"rebuild"
}
fn description(&self) -> &str {
"Build OpenCrabs from source (cargo build --release) in the BACKGROUND and auto-reload \
when it's done. MAINTAINER path, rare: only for applying LOCAL SOURCE EDITS. To \
upgrade to the latest published release use the `evolve` tool instead (rebuild \
compiles what is on disk; evolve fetches what was released). Returns \
immediately: the build runs detached (it does not block you) with a live \
timer on your surface, and OpenCrabs \
exec-restarts into the new binary automatically when the build finishes, resuming \
this session. On build failure a message is delivered; nothing restarts."
}
fn input_schema(&self) -> Value {
serde_json::json!({
"type": "object",
"properties": {},
"required": []
})
}
fn capabilities(&self) -> Vec<ToolCapability> {
vec![ToolCapability::SystemModification]
}
async fn execute(&self, _input: Value, context: &ToolExecutionContext) -> Result<ToolResult> {
// The build runs DETACHED via the shared BackgroundTaskManager (#1748):
// live timer, status file and DB accounting come free, and a
// rebuild-specific completion hook exec-restarts into the new binary
// when the compile finishes.
let Some(mgr) = context.background_manager.as_ref() else {
return Ok(ToolResult::error(
"rebuild: no background task manager available in this context".to_string(),
));
};
let Some(service_context) = context.service_context.as_ref() else {
return Ok(ToolResult::error(
"rebuild: no service context available to schedule the background build"
.to_string(),
));
};
match run_detached_rebuild(mgr, context.session_id, service_context).await {
Ok(()) => Ok(ToolResult::success(
"🔨 Rebuild running detached: the live timer shows progress and it does not \
block you. OpenCrabs exec-restarts into the new binary automatically when \
it's done; keep working."
.to_string(),
)),
Err(e) => Ok(ToolResult::error(format!("{e:#}"))),
}
}
}