Remove all alternate update paths — fleet redeploy is release + upgrade all
Killed: - rebuild_and_restart (in-container cargo self-compile): the MCP tool, the `rebuild` chat command in all four transports, the web-ui bot command, and the underlying function. This was the path that caused the exec() deadlocks. - upgrade_sled gateway MCP tool: second entry point to sled upgrades, defaulted to serving the gateway's own macOS binary to Linux sleds. - GET /api/huskies-binary (both sled and gateway route trees): served current_exe(), wrong platform when the gateway is macOS. Superseded by /api/artifacts/ which now also serves on the gateway route tree. - `huskies upgrade` CLI subcommand and --source flag: third way of doing the same download-and-replace. Escape hatch for a bricked sled is `docker cp` + restart. Kept, distinct jobs: `project-rebuild` (container/image updates), `rebuild gateway` + script/local-release (gateway self-update). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019fHdm92yjvguPi2LiXfLB9
This commit is contained in:
@@ -2,10 +2,10 @@
|
||||
|
||||
/// Wall-clock time captured the first time this server process touches the
|
||||
/// merge subsystem. Used to detect merge_jobs left over from a previous
|
||||
/// server instance: a re-exec on `rebuild_and_restart` keeps the same PID,
|
||||
/// so PID alone cannot distinguish "current" vs "previous" server. This
|
||||
/// timestamp is fresh per-process (the static is reset by execve) and is
|
||||
/// the source of truth for stale-merge detection.
|
||||
/// server instance: PIDs can collide across restarts (PID 1 in a container
|
||||
/// is always the server), so PID alone cannot distinguish "current" vs
|
||||
/// "previous" server. This timestamp is fresh per-process and is the source
|
||||
/// of truth for stale-merge detection.
|
||||
static SERVER_START_TIME: std::sync::OnceLock<f64> = std::sync::OnceLock::new();
|
||||
|
||||
/// Return this server process's start time (lazily captured on first call).
|
||||
|
||||
@@ -224,11 +224,6 @@ pub fn commands() -> &'static [BotCommand] {
|
||||
description: "Clear the current Claude Code session and start fresh",
|
||||
handler: handle_reset_fallback,
|
||||
},
|
||||
BotCommand {
|
||||
name: "rebuild",
|
||||
description: "Rebuild the server binary and restart",
|
||||
handler: handle_rebuild_fallback,
|
||||
},
|
||||
BotCommand {
|
||||
name: "timer",
|
||||
description: "Schedule a deferred agent start: `timer <story_id> <HH:MM>`, `timer list`, `timer cancel <story_id>`",
|
||||
@@ -425,16 +420,6 @@ fn handle_reset_fallback(_ctx: &CommandContext) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Fallback handler for the `rebuild` command when it is not intercepted by
|
||||
/// the async handler in `on_room_message`. In practice this is never called —
|
||||
/// rebuild is detected and handled before `try_handle_command` is invoked.
|
||||
/// The entry exists in the registry only so `help` lists it.
|
||||
///
|
||||
/// Returns `None` to prevent the LLM from receiving "rebuild" as a prompt.
|
||||
fn handle_rebuild_fallback(_ctx: &CommandContext) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Fallback handler for the `cleanup_worktrees` command when it is not
|
||||
/// intercepted by the async handler in `on_room_message`. In practice this is
|
||||
/// never called — cleanup_worktrees is detected and handled before
|
||||
|
||||
@@ -161,27 +161,6 @@ pub(super) async fn handle_incoming_message(
|
||||
return;
|
||||
}
|
||||
|
||||
if crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.bot_user_id,
|
||||
)
|
||||
.is_some()
|
||||
{
|
||||
slog!("[discord] Handling rebuild command from {user} in {channel}");
|
||||
let ack = "Rebuilding server… this may take a moment.";
|
||||
let _ = ctx.transport.send_message(channel, ack, "").await;
|
||||
let response = crate::chat::transport::matrix::rebuild::handle_rebuild(
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.project_root,
|
||||
&ctx.services.agents,
|
||||
)
|
||||
.await;
|
||||
let response = markdown_to_discord(&response);
|
||||
let _ = ctx.transport.send_message(channel, &response, "").await;
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(rmtree_cmd) = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
@@ -525,16 +504,6 @@ mod tests {
|
||||
assert!(result.unwrap().contains("Pipeline Status"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuild_command_extracted_from_discord_message() {
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"Huskies rebuild",
|
||||
"Huskies",
|
||||
"discord-bot",
|
||||
);
|
||||
assert!(result.is_some(), "'Huskies rebuild' should be recognised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reset_command_extracted_from_discord_message() {
|
||||
let result = crate::chat::transport::matrix::reset::extract_reset_command(
|
||||
|
||||
@@ -306,10 +306,8 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message(
|
||||
&& (crate::chat::commands::commands()
|
||||
.iter()
|
||||
.any(|c| c.name == cmd)
|
||||
|| [
|
||||
"assign", "start", "delete", "rebuild", "rmtree", "htop", "timer",
|
||||
]
|
||||
.contains(&cmd.as_str()));
|
||||
|| ["assign", "start", "delete", "rmtree", "htop", "timer"]
|
||||
.contains(&cmd.as_str()));
|
||||
|
||||
if is_known_command {
|
||||
// Proxy to the active project server.
|
||||
@@ -1006,45 +1004,6 @@ pub(in crate::chat::transport::matrix::bot) async fn on_room_message(
|
||||
return;
|
||||
}
|
||||
|
||||
// Check for the rebuild command, which requires async agent and process ops
|
||||
// and cannot be handled by the sync command registry.
|
||||
if super::super::super::rebuild::extract_rebuild_command(
|
||||
&user_message,
|
||||
&ctx.services.bot_name,
|
||||
ctx.matrix_user_id.as_str(),
|
||||
)
|
||||
.is_some()
|
||||
{
|
||||
slog!("[matrix-bot] Handling rebuild command from {sender}");
|
||||
// Acknowledge immediately — the rebuild may take a while or re-exec.
|
||||
let ack = "Rebuilding server… this may take a moment.";
|
||||
let ack_html = markdown_to_html(ack);
|
||||
if let Ok(msg_id) = ctx
|
||||
.transport
|
||||
.send_message(&room_id_str, ack, &ack_html)
|
||||
.await
|
||||
&& let Ok(event_id) = msg_id.parse()
|
||||
{
|
||||
ctx.bot_sent_event_ids.lock().await.insert(event_id);
|
||||
}
|
||||
let response = super::super::super::rebuild::handle_rebuild(
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.project_root,
|
||||
&ctx.services.agents,
|
||||
)
|
||||
.await;
|
||||
let html = markdown_to_html(&response);
|
||||
if let Ok(msg_id) = ctx
|
||||
.transport
|
||||
.send_message(&room_id_str, &response, &html)
|
||||
.await
|
||||
&& let Ok(event_id) = msg_id.parse()
|
||||
{
|
||||
ctx.bot_sent_event_ids.lock().await.insert(event_id);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// In gateway mode, handle the "switch <project>" command to change the
|
||||
// active project without invoking the LLM.
|
||||
if let Some(ref active_project) = ctx.gateway_active_project {
|
||||
|
||||
@@ -35,7 +35,7 @@ pub mod new_project;
|
||||
pub mod project_rebuild;
|
||||
/// `projects` chat command — list all registered gateway projects.
|
||||
pub mod projects;
|
||||
/// Rebuild command — triggers a server rebuild/restart via a bot command.
|
||||
/// `rebuild gateway` command parsing (gateway self-rebuild).
|
||||
pub mod rebuild;
|
||||
/// `release` gateway chat command — build the sled binary and publish it.
|
||||
pub mod release;
|
||||
|
||||
@@ -1,51 +1,19 @@
|
||||
//! Rebuild command: trigger a server rebuild and restart.
|
||||
//! `rebuild gateway` command parsing.
|
||||
//!
|
||||
//! `{bot_name} rebuild` stops all running agents, rebuilds the server binary
|
||||
//! with `cargo build`, and re-execs the process with the new binary. If the
|
||||
//! build fails the error is reported back to the room and the server keeps
|
||||
//! running.
|
||||
//! The old `rebuild` command (in-container cargo build + restart of a sled)
|
||||
//! was removed: fleet binary updates go through `release` + `upgrade all`
|
||||
//! exclusively. Only the gateway self-rebuild command remains here.
|
||||
|
||||
use crate::agents::AgentPool;
|
||||
use crate::chat::util::strip_bot_mention;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A parsed rebuild command.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub struct RebuildCommand;
|
||||
|
||||
/// Parse a rebuild command from a raw message body.
|
||||
///
|
||||
/// Strips the bot mention prefix and checks whether the command word is
|
||||
/// `rebuild`. Returns `None` when the message is not a rebuild command.
|
||||
pub fn extract_rebuild_command(
|
||||
message: &str,
|
||||
bot_name: &str,
|
||||
bot_user_id: &str,
|
||||
) -> Option<RebuildCommand> {
|
||||
let stripped = strip_bot_mention(message, bot_name, bot_user_id);
|
||||
let trimmed = stripped
|
||||
.trim()
|
||||
.trim_start_matches(|c: char| !c.is_alphanumeric());
|
||||
|
||||
let cmd = match trimmed.split_once(char::is_whitespace) {
|
||||
Some((c, _)) => c,
|
||||
None => trimmed,
|
||||
};
|
||||
|
||||
if cmd.eq_ignore_ascii_case("rebuild") {
|
||||
Some(RebuildCommand)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a "rebuild gateway" command from a raw message body.
|
||||
///
|
||||
/// Returns `Some(RebuildCommand)` only when the stripped message begins with
|
||||
/// "rebuild gateway" (case-insensitive). A plain "rebuild" without the
|
||||
/// "gateway" qualifier returns `None` so it falls through to the standard
|
||||
/// server rebuild handler.
|
||||
/// "rebuild gateway" (case-insensitive).
|
||||
pub fn extract_rebuild_gateway_command(
|
||||
message: &str,
|
||||
bot_name: &str,
|
||||
@@ -77,23 +45,6 @@ pub fn extract_rebuild_gateway_command(
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle a rebuild command: trigger server rebuild and restart.
|
||||
///
|
||||
/// Returns a string describing the outcome. On build failure the error
|
||||
/// message is returned so it can be posted to the room; the server keeps
|
||||
/// running. On success this function never returns (the process re-execs).
|
||||
pub async fn handle_rebuild(
|
||||
bot_name: &str,
|
||||
project_root: &Path,
|
||||
agents: &Arc<AgentPool>,
|
||||
) -> String {
|
||||
crate::slog!("[matrix-bot] rebuild command received (bot={bot_name})");
|
||||
match crate::rebuild::rebuild_and_restart(agents, project_root, None).await {
|
||||
Ok(msg) => msg,
|
||||
Err(e) => format!("Rebuild failed: {e}"),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
@@ -103,46 +54,21 @@ mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn extract_with_display_name() {
|
||||
let cmd = extract_rebuild_command("Timmy rebuild", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, Some(RebuildCommand));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_with_full_user_id() {
|
||||
fn extract_gateway_rebuild() {
|
||||
let cmd =
|
||||
extract_rebuild_command("@timmy:home.local rebuild", "Timmy", "@timmy:home.local");
|
||||
extract_rebuild_gateway_command("Timmy rebuild gateway", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, Some(RebuildCommand));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_with_localpart() {
|
||||
let cmd = extract_rebuild_command("@timmy rebuild", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, Some(RebuildCommand));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_case_insensitive() {
|
||||
let cmd = extract_rebuild_command("Timmy REBUILD", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, Some(RebuildCommand));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_non_rebuild_returns_none() {
|
||||
let cmd = extract_rebuild_command("Timmy help", "Timmy", "@timmy:home.local");
|
||||
fn plain_rebuild_is_not_gateway_rebuild() {
|
||||
let cmd = extract_rebuild_gateway_command("Timmy rebuild", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_ignores_extra_args() {
|
||||
// "rebuild" with trailing text is still a rebuild command
|
||||
let cmd = extract_rebuild_command("Timmy rebuild now", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, Some(RebuildCommand));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extract_no_match_returns_none() {
|
||||
let cmd = extract_rebuild_command("Timmy status", "Timmy", "@timmy:home.local");
|
||||
fn non_rebuild_returns_none() {
|
||||
let cmd = extract_rebuild_gateway_command("Timmy status", "Timmy", "@timmy:home.local");
|
||||
assert_eq!(cmd, None);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,27 +208,6 @@ pub(super) async fn handle_incoming_message(
|
||||
return;
|
||||
}
|
||||
|
||||
if crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.bot_user_id,
|
||||
)
|
||||
.is_some()
|
||||
{
|
||||
slog!("[slack] Handling rebuild command from {user} in {channel}");
|
||||
let ack = "Rebuilding server… this may take a moment.";
|
||||
let _ = ctx.transport.send_message(channel, ack, "").await;
|
||||
let response = crate::chat::transport::matrix::rebuild::handle_rebuild(
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.project_root,
|
||||
&ctx.services.agents,
|
||||
)
|
||||
.await;
|
||||
let response = markdown_to_slack(&response);
|
||||
let _ = ctx.transport.send_message(channel, &response, "").await;
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(rmtree_cmd) = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
@@ -488,42 +467,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── rebuild command extraction ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn rebuild_command_extracted_from_slack_message() {
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"Huskies rebuild",
|
||||
"Huskies",
|
||||
"slack-bot",
|
||||
);
|
||||
assert!(result.is_some(), "'Huskies rebuild' should be recognised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuild_command_extracted_plain_no_mention() {
|
||||
// Slack slash-command synthetic messages may not include a bot mention.
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"rebuild",
|
||||
"Huskies",
|
||||
"slack-bot",
|
||||
);
|
||||
assert!(result.is_some(), "plain 'rebuild' should be recognised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_rebuild_slack_message_not_extracted() {
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"Huskies status",
|
||||
"Huskies",
|
||||
"slack-bot",
|
||||
);
|
||||
assert!(
|
||||
result.is_none(),
|
||||
"'status' should not be recognised as rebuild"
|
||||
);
|
||||
}
|
||||
|
||||
// ── reset command extraction ───────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -122,26 +122,6 @@ pub(super) async fn handle_incoming_message(
|
||||
return;
|
||||
}
|
||||
|
||||
if crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.bot_user_id,
|
||||
)
|
||||
.is_some()
|
||||
{
|
||||
slog!("[whatsapp] Handling rebuild command from {sender}");
|
||||
let ack = "Rebuilding server… this may take a moment.";
|
||||
let _ = ctx.transport.send_message(sender, ack, "").await;
|
||||
let response = crate::chat::transport::matrix::rebuild::handle_rebuild(
|
||||
&ctx.services.bot_name,
|
||||
&ctx.services.project_root,
|
||||
&ctx.services.agents,
|
||||
)
|
||||
.await;
|
||||
let _ = ctx.transport.send_message(sender, &response, "").await;
|
||||
return;
|
||||
}
|
||||
|
||||
if let Some(rmtree_cmd) = crate::chat::transport::matrix::rmtree::extract_rmtree_command(
|
||||
message,
|
||||
&ctx.services.bot_name,
|
||||
@@ -397,43 +377,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── rebuild command extraction ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn rebuild_command_extracted_from_plain_message() {
|
||||
// WhatsApp messages arrive without a bot mention prefix.
|
||||
// extract_rebuild_command must recognise "rebuild" by itself.
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"rebuild",
|
||||
"Timmy",
|
||||
"@timmy:home.local",
|
||||
);
|
||||
assert!(result.is_some(), "plain 'rebuild' should be recognised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuild_command_extracted_with_bot_name_prefix() {
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"Timmy rebuild",
|
||||
"Timmy",
|
||||
"@timmy:home.local",
|
||||
);
|
||||
assert!(result.is_some(), "'Timmy rebuild' should be recognised");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn non_rebuild_whatsapp_message_not_extracted() {
|
||||
let result = crate::chat::transport::matrix::rebuild::extract_rebuild_command(
|
||||
"status",
|
||||
"Timmy",
|
||||
"@timmy:home.local",
|
||||
);
|
||||
assert!(
|
||||
result.is_none(),
|
||||
"'status' should not be recognised as rebuild"
|
||||
);
|
||||
}
|
||||
|
||||
// ── reset command extraction ───────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -27,14 +27,6 @@ pub(crate) struct CliArgs {
|
||||
/// forwards all `prompt_permission` tool calls to the gateway over a WebSocket.
|
||||
/// Also readable from the `HUSKIES_UPSTREAM_GATEWAY` env var.
|
||||
pub(crate) upstream_gateway: Option<String>,
|
||||
/// Whether the `upgrade` subcommand was given.
|
||||
pub(crate) upgrade: bool,
|
||||
/// Source URL for the `upgrade` subcommand (`--source <URL>`).
|
||||
///
|
||||
/// If omitted, the upgrade subcommand falls back to
|
||||
/// `HUSKIES_BINARY_SOURCE` env var, then derives the URL from
|
||||
/// `HUSKIES_UPSTREAM_GATEWAY`.
|
||||
pub(crate) upgrade_source: Option<String>,
|
||||
/// Path to a trampoline job file (`--trampoline <path>`).
|
||||
///
|
||||
/// When set, the binary runs as a detached trampoline helper: it kills the
|
||||
@@ -54,8 +46,6 @@ pub(crate) fn parse_cli_args(args: &[String]) -> Result<CliArgs, String> {
|
||||
let mut join_token: Option<String> = None;
|
||||
let mut gateway_url: Option<String> = None;
|
||||
let mut upstream_gateway: Option<String> = None;
|
||||
let mut upgrade = false;
|
||||
let mut upgrade_source: Option<String> = None;
|
||||
let mut trampoline: Option<String> = None;
|
||||
let mut i = 0;
|
||||
|
||||
@@ -136,19 +126,6 @@ pub(crate) fn parse_cli_args(args: &[String]) -> Result<CliArgs, String> {
|
||||
"agent" => {
|
||||
agent = true;
|
||||
}
|
||||
"upgrade" => {
|
||||
upgrade = true;
|
||||
}
|
||||
"--source" => {
|
||||
i += 1;
|
||||
if i >= args.len() {
|
||||
return Err("--source requires a value".to_string());
|
||||
}
|
||||
upgrade_source = Some(args[i].clone());
|
||||
}
|
||||
a if a.starts_with("--source=") => {
|
||||
upgrade_source = Some(a["--source=".len()..].to_string());
|
||||
}
|
||||
"--trampoline" => {
|
||||
i += 1;
|
||||
if i >= args.len() {
|
||||
@@ -186,8 +163,6 @@ pub(crate) fn parse_cli_args(args: &[String]) -> Result<CliArgs, String> {
|
||||
join_token,
|
||||
gateway_url,
|
||||
upstream_gateway,
|
||||
upgrade,
|
||||
upgrade_source,
|
||||
trampoline,
|
||||
})
|
||||
}
|
||||
@@ -197,16 +172,12 @@ pub(crate) fn print_help() {
|
||||
println!("huskies init [OPTIONS] [PATH]");
|
||||
println!("huskies agent --rendezvous <URL> [OPTIONS] [PATH]");
|
||||
println!("huskies --gateway [OPTIONS] [PATH]");
|
||||
println!("huskies upgrade [--source <URL>]");
|
||||
println!();
|
||||
println!("Serve a huskies project.");
|
||||
println!();
|
||||
println!("COMMANDS:");
|
||||
println!(" init Scaffold a new .huskies/ project and start the interactive setup wizard.");
|
||||
println!(" agent Run as a headless build agent — syncs CRDT state, claims and runs work.");
|
||||
println!(
|
||||
" upgrade Fetch a new huskies binary from SOURCE and atomically replace the current"
|
||||
);
|
||||
println!();
|
||||
println!("ARGS:");
|
||||
println!(
|
||||
@@ -236,8 +207,6 @@ pub(crate) fn print_help() {
|
||||
println!(" sled connects to WS URL and forwards all");
|
||||
println!(" prompt_permission calls via the uplink protocol.");
|
||||
println!(" Also readable from HUSKIES_UPSTREAM_GATEWAY env var.");
|
||||
println!(" --source <URL> Binary source URL for the `upgrade` subcommand.");
|
||||
println!(" Falls back to HUSKIES_BINARY_SOURCE env var.");
|
||||
}
|
||||
|
||||
/// Resolve the optional positional path argument into an absolute `PathBuf`.
|
||||
@@ -447,58 +416,6 @@ mod tests {
|
||||
assert!(parse_cli_args(&args).is_err());
|
||||
}
|
||||
|
||||
// ── upgrade subcommand ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_upgrade_subcommand() {
|
||||
let args = vec!["upgrade".to_string()];
|
||||
let result = parse_cli_args(&args).unwrap();
|
||||
assert!(result.upgrade);
|
||||
assert_eq!(result.upgrade_source, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_upgrade_with_source_flag() {
|
||||
let args = vec![
|
||||
"upgrade".to_string(),
|
||||
"--source".to_string(),
|
||||
"http://gateway:3000/api/huskies-binary".to_string(),
|
||||
];
|
||||
let result = parse_cli_args(&args).unwrap();
|
||||
assert!(result.upgrade);
|
||||
assert_eq!(
|
||||
result.upgrade_source,
|
||||
Some("http://gateway:3000/api/huskies-binary".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_upgrade_with_source_equals_syntax() {
|
||||
let args = vec![
|
||||
"upgrade".to_string(),
|
||||
"--source=http://gw:3000/api/b".to_string(),
|
||||
];
|
||||
let result = parse_cli_args(&args).unwrap();
|
||||
assert!(result.upgrade);
|
||||
assert_eq!(
|
||||
result.upgrade_source,
|
||||
Some("http://gw:3000/api/b".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_upgrade_source_missing_value_is_error() {
|
||||
let args = vec!["upgrade".to_string(), "--source".to_string()];
|
||||
assert!(parse_cli_args(&args).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_no_args_upgrade_is_false() {
|
||||
let result = parse_cli_args(&[]).unwrap();
|
||||
assert!(!result.upgrade);
|
||||
assert_eq!(result.upgrade_source, None);
|
||||
}
|
||||
|
||||
// ── resolve_path_arg ────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -65,10 +65,11 @@ pub fn build_gateway_route(state_arc: Arc<GatewayState>) -> impl poem::Endpoint
|
||||
"/gateway/agents/:id/assign",
|
||||
poem::post(gateway_assign_agent_handler),
|
||||
)
|
||||
// Binary self-update: serve the gateway binary so sleds can download it.
|
||||
// Artifact store: sleds download published binaries from here during
|
||||
// `upgrade all` — never from anywhere else.
|
||||
.at(
|
||||
"/api/huskies-binary",
|
||||
poem::get(crate::http::serve_binary_handler),
|
||||
"/api/artifacts/:filename",
|
||||
poem::get(crate::http::serve_artifact_handler),
|
||||
)
|
||||
.data(state_arc)
|
||||
}
|
||||
|
||||
@@ -57,8 +57,8 @@ pub struct AppContext {
|
||||
pub qa_app_process: Arc<std::sync::Mutex<Option<std::process::Child>>>,
|
||||
/// Best-effort shutdown notifier for active bot channels (Slack / WhatsApp).
|
||||
///
|
||||
/// When set, the MCP `rebuild_and_restart` tool uses this to announce the
|
||||
/// shutdown to configured channels before re-execing the server binary.
|
||||
/// When set, restart-inducing paths use this to announce the shutdown to
|
||||
/// configured channels before the process exits.
|
||||
/// `None` when no webhook-based bot transport is configured.
|
||||
pub bot_shutdown: Option<Arc<BotShutdownNotifier>>,
|
||||
/// Watch sender used to signal the Matrix bot task that the server is
|
||||
|
||||
@@ -27,8 +27,6 @@ const GATEWAY_TOOLS: &[&str] = &[
|
||||
// Handled at the gateway so the Matrix bot's perm_rx listener is used
|
||||
// rather than the container's (which has no interactive session attached).
|
||||
"prompt_permission",
|
||||
// Binary self-update: gateway serves its own binary and triggers upgrade on sleds.
|
||||
"upgrade_sled",
|
||||
// One-shot container rebuild: build fresh image, swap container, preserve state.
|
||||
"project_rebuild",
|
||||
];
|
||||
@@ -134,23 +132,6 @@ pub(crate) fn gateway_tool_definitions() -> Vec<Value> {
|
||||
"properties": {}
|
||||
}
|
||||
}),
|
||||
json!({
|
||||
"name": "upgrade_sled",
|
||||
"description": "Trigger a binary self-update on a project sled. The sled downloads the new binary from `source_url` (defaults to this gateway's /api/huskies-binary endpoint), atomically replaces its own executable, drains CRDT persistence so no ops are lost, and re-execs. Without `project`, upgrades the active project.",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"project": {
|
||||
"type": "string",
|
||||
"description": "Name of the project sled to upgrade. Defaults to the currently active project."
|
||||
},
|
||||
"source_url": {
|
||||
"type": "string",
|
||||
"description": "HTTP URL of the binary to install (e.g. 'http://gateway:3000/api/huskies-binary'). Defaults to this gateway's own binary endpoint."
|
||||
}
|
||||
}
|
||||
}
|
||||
}),
|
||||
json!({
|
||||
"name": "project_rebuild",
|
||||
"description": "Rebuild a project's Docker image from its Dockerfile.fragment, swap the container, and preserve all CRDT and pipeline state. In-flight coder/merge work is drained before the swap; if not drainable within the timeout the command refuses. On success returns the new image hash and container ID.",
|
||||
@@ -438,7 +419,6 @@ async fn handle_gateway_tool(
|
||||
"aggregate_pipeline_status" => handle_aggregate_pipeline_status_tool(state, id).await,
|
||||
"agents.list" => handle_agents_list_tool(id),
|
||||
"prompt_permission" => handle_prompt_permission_tool(params, state, id).await,
|
||||
"upgrade_sled" => handle_upgrade_sled_tool(params, state, id).await,
|
||||
"project_rebuild" => handle_project_rebuild_tool(params, state, id).await,
|
||||
_ => JsonRpcResponse::error(id, -32601, format!("Unknown gateway tool: {tool_name}")),
|
||||
}
|
||||
@@ -893,93 +873,6 @@ fn handle_agents_list_tool(id: Option<Value>) -> JsonRpcResponse {
|
||||
)
|
||||
}
|
||||
|
||||
/// Handle the `upgrade_sled` gateway tool.
|
||||
///
|
||||
/// Posts `{"source_url": "<url>"}` to the target sled's `/api/upgrade` endpoint,
|
||||
/// which triggers the sled to download the new binary, drain CRDT persistence,
|
||||
/// and re-exec. Returns 202 text immediately — the sled connection will drop
|
||||
/// shortly after as `exec()` replaces the process.
|
||||
async fn handle_upgrade_sled_tool(
|
||||
params: &Value,
|
||||
state: &GatewayState,
|
||||
id: Option<Value>,
|
||||
) -> JsonRpcResponse {
|
||||
let args = params.get("arguments").unwrap_or(params);
|
||||
|
||||
// Resolve target project URL (explicit project arg or active project).
|
||||
let project_name = args.get("project").and_then(|v| v.as_str());
|
||||
let sled_url = if let Some(name) = project_name {
|
||||
let projects = state.projects.read().await;
|
||||
match projects.get(name).and_then(|e| e.url.clone()) {
|
||||
Some(u) => u,
|
||||
None => {
|
||||
return JsonRpcResponse::error(
|
||||
id,
|
||||
-32602,
|
||||
format!("Project '{name}' not found or has no URL configured"),
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
match state.active_url().await {
|
||||
Ok(u) => u,
|
||||
Err(e) => return JsonRpcResponse::error(id, -32603, e.to_string()),
|
||||
}
|
||||
};
|
||||
|
||||
// Build the binary source URL: caller-supplied or this gateway's own endpoint.
|
||||
let source_url = args
|
||||
.get("source_url")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| {
|
||||
// Default: the gateway serves its own binary at /api/huskies-binary.
|
||||
// Use the same host/port the gateway is bound to.
|
||||
std::env::var("HUSKIES_GATEWAY_BINARY_URL")
|
||||
.unwrap_or_else(|_| format!("http://gateway:{}/api/huskies-binary", state.port))
|
||||
});
|
||||
|
||||
let upgrade_url = format!("{sled_url}/api/upgrade");
|
||||
let body = serde_json::json!({ "source_url": source_url });
|
||||
|
||||
let active_name = project_name.map(|s| s.to_string()).unwrap_or_else(|| {
|
||||
state
|
||||
.active_project
|
||||
.try_read()
|
||||
.map(|g| g.clone())
|
||||
.unwrap_or_default()
|
||||
});
|
||||
|
||||
match state.client.post(&upgrade_url).json(&body).send().await {
|
||||
Ok(resp) if resp.status().is_success() || resp.status().as_u16() == 202 => {
|
||||
JsonRpcResponse::success(
|
||||
id,
|
||||
json!({
|
||||
"content": [{
|
||||
"type": "text",
|
||||
"text": format!(
|
||||
"Upgrade triggered on '{active_name}'. The sled is downloading the new binary from {source_url} and will re-exec momentarily."
|
||||
)
|
||||
}]
|
||||
}),
|
||||
)
|
||||
}
|
||||
Ok(resp) => JsonRpcResponse::error(
|
||||
id,
|
||||
-32603,
|
||||
format!(
|
||||
"Sled returned HTTP {} for upgrade request to {upgrade_url}",
|
||||
resp.status()
|
||||
),
|
||||
),
|
||||
Err(e) => JsonRpcResponse::error(
|
||||
id,
|
||||
-32603,
|
||||
format!("Failed to send upgrade request to {upgrade_url}: {e}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Handle the `project_rebuild` gateway tool.
|
||||
///
|
||||
/// Rebuilds a project's Docker image, swaps the container, and preserves all
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
use crate::agents::move_story_to_stage;
|
||||
use crate::http::context::AppContext;
|
||||
use crate::log_buffer;
|
||||
use crate::slog;
|
||||
use serde_json::{Value, json};
|
||||
|
||||
mod permission;
|
||||
@@ -43,23 +42,6 @@ pub(crate) fn tool_get_server_logs(args: &Value) -> Result<String, String> {
|
||||
Ok(all_lines[start..].join("\n"))
|
||||
}
|
||||
|
||||
/// Rebuild the server binary and re-exec (delegates to `crate::rebuild`).
|
||||
pub(crate) async fn tool_rebuild_and_restart(ctx: &AppContext) -> Result<String, String> {
|
||||
slog!("[rebuild] Rebuild and restart requested via MCP tool");
|
||||
|
||||
// Signal the Matrix bot (if active) so it can send its own shutdown
|
||||
// announcement before the process is replaced. Best-effort: we wait up
|
||||
// to 1.5 s for the message to be delivered.
|
||||
if let Some(ref tx) = ctx.matrix_shutdown_tx {
|
||||
let _ = tx.send(Some(crate::rebuild::ShutdownReason::Rebuild));
|
||||
tokio::time::sleep(std::time::Duration::from_millis(1500)).await;
|
||||
}
|
||||
|
||||
let project_root = ctx.state.get_project_root().unwrap_or_default();
|
||||
let notifier = ctx.bot_shutdown.as_deref();
|
||||
crate::rebuild::rebuild_and_restart(&ctx.services.agents, &project_root, notifier).await
|
||||
}
|
||||
|
||||
/// MCP tool called by Claude Code via `--permission-prompt-tool`.
|
||||
///
|
||||
/// Forwards the permission request through the shared channel to the active
|
||||
|
||||
@@ -335,57 +335,6 @@ mod tests {
|
||||
assert_eq!(servers[0], "huskies");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuild_and_restart_in_tools_list() {
|
||||
use super::super::super::tools_list::handle_tools_list;
|
||||
let resp = handle_tools_list(Some(json!(1)));
|
||||
let tools = resp.result.unwrap()["tools"].as_array().unwrap().clone();
|
||||
let tool = tools.iter().find(|t| t["name"] == "rebuild_and_restart");
|
||||
assert!(
|
||||
tool.is_some(),
|
||||
"rebuild_and_restart missing from tools list"
|
||||
);
|
||||
let t = tool.unwrap();
|
||||
assert!(t["description"].as_str().unwrap().contains("Rebuild"));
|
||||
assert!(t["inputSchema"].is_object());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn rebuild_and_restart_kills_agents_before_build() {
|
||||
// Verify that calling rebuild_and_restart on an empty pool doesn't
|
||||
// panic and proceeds to the build step. We can't test exec() in a
|
||||
// unit test, but we can verify the build attempt happens.
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let ctx = test_ctx(tmp.path());
|
||||
|
||||
// The build will succeed (we're running in the real workspace) and
|
||||
// then exec() will be called — which would replace our test process.
|
||||
// So we only test that the function *runs* without panicking up to
|
||||
// the agent-kill step. We do this by checking the pool is empty.
|
||||
assert_eq!(ctx.services.agents.list_agents().await.unwrap().len(), 0);
|
||||
ctx.services.agents.kill_all_children().await; // should not panic on empty pool
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rebuild_uses_matching_build_profile() {
|
||||
// The build must use the same profile (debug/release) as the running
|
||||
// binary, otherwise cargo build outputs to a different target dir and
|
||||
// current_exe() still points at the old binary.
|
||||
let build_args: Vec<&str> = if cfg!(debug_assertions) {
|
||||
vec!["build", "-p", "huskies"]
|
||||
} else {
|
||||
vec!["build", "--release", "-p", "huskies"]
|
||||
};
|
||||
|
||||
// Tests always run in debug mode, so --release must NOT be present.
|
||||
assert!(
|
||||
!build_args.contains(&"--release"),
|
||||
"In debug builds, rebuild must not pass --release (would put \
|
||||
the binary in target/release/ while current_exe() points to \
|
||||
target/debug/)"
|
||||
);
|
||||
}
|
||||
|
||||
// ── move_story tool tests ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -81,8 +81,6 @@ pub async fn dispatch_tool_call(
|
||||
// Diagnostics
|
||||
"get_server_logs" => diagnostics::tool_get_server_logs(&args),
|
||||
"get_version" => diagnostics::tool_get_version(ctx),
|
||||
// Server lifecycle
|
||||
"rebuild_and_restart" => diagnostics::tool_rebuild_and_restart(ctx).await,
|
||||
// Permission bridge (Claude Code → frontend dialog)
|
||||
"prompt_permission" => diagnostics::tool_prompt_permission(&args, ctx).await,
|
||||
// Token usage
|
||||
|
||||
@@ -78,7 +78,6 @@ mod tests {
|
||||
assert!(names.contains(&"get_server_logs"));
|
||||
assert!(names.contains(&"prompt_permission"));
|
||||
assert!(names.contains(&"get_pipeline_status"));
|
||||
assert!(names.contains(&"rebuild_and_restart"));
|
||||
assert!(names.contains(&"get_token_usage"));
|
||||
assert!(names.contains(&"move_story"));
|
||||
assert!(names.contains(&"unblock_story"));
|
||||
@@ -117,7 +116,7 @@ mod tests {
|
||||
assert!(names.contains(&"convert_item_type"));
|
||||
assert!(names.contains(&"edit"));
|
||||
assert!(names.contains(&"write"));
|
||||
assert_eq!(tools.len(), 85);
|
||||
assert_eq!(tools.len(), 84);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -42,14 +42,6 @@ pub(super) fn system_tools() -> Vec<Value> {
|
||||
"properties": {}
|
||||
}
|
||||
}),
|
||||
json!({
|
||||
"name": "rebuild_and_restart",
|
||||
"description": "Rebuild the server binary from source and re-exec with the new binary. Gracefully stops all running agents before restart. If the build fails, the server stays up and returns the build error.",
|
||||
"inputSchema": {
|
||||
"type": "object",
|
||||
"properties": {}
|
||||
}
|
||||
}),
|
||||
json!({
|
||||
"name": "prompt_permission",
|
||||
"description": "Present a permission request to the user via the web UI. Used by Claude Code's --permission-prompt-tool to delegate permission decisions to the frontend dialog. Returns on approval; returns an error on denial.",
|
||||
|
||||
+2
-31
@@ -124,7 +124,6 @@ pub fn build_routes(
|
||||
|
||||
route = route
|
||||
.at("/api/upgrade", post(upgrade_trigger_handler))
|
||||
.at("/api/huskies-binary", get(serve_binary_handler))
|
||||
.at("/api/artifacts/:filename", get(serve_artifact_handler));
|
||||
|
||||
if let Some(wa_ctx) = whatsapp_ctx {
|
||||
@@ -234,9 +233,9 @@ pub fn debug_crdt_handler(req: &poem::Request) -> poem::Response {
|
||||
|
||||
/// `POST /api/upgrade` — trigger a self-update on the running sled.
|
||||
///
|
||||
/// Accepts `{"source_url": "http://gateway:3000/api/huskies-binary"}` and
|
||||
/// Accepts `{"source_url": "http://<gateway>/api/artifacts/<name>"}` and
|
||||
/// spawns the upgrade task in the background, returning 202 immediately.
|
||||
/// The connection will be dropped when `exec()` replaces the process.
|
||||
/// The sled exits after the binary swap; Docker restarts it.
|
||||
#[poem::handler]
|
||||
pub async fn upgrade_trigger_handler(
|
||||
body: poem::web::Json<serde_json::Value>,
|
||||
@@ -270,34 +269,6 @@ pub async fn upgrade_trigger_handler(
|
||||
.body("Upgrade triggered. The sled will re-exec momentarily.")
|
||||
}
|
||||
|
||||
/// `GET /api/huskies-binary` — serve the running binary so peer sleds can download it.
|
||||
///
|
||||
/// Streams `current_exe()` (the binary that is currently running) as an
|
||||
/// `application/octet-stream` download. Returns 500 if the path cannot be
|
||||
/// resolved or read.
|
||||
#[poem::handler]
|
||||
pub async fn serve_binary_handler() -> poem::Response {
|
||||
let exe = match std::env::current_exe() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return poem::Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(format!("Cannot resolve current executable: {e}"));
|
||||
}
|
||||
};
|
||||
|
||||
match tokio::fs::read(&exe).await {
|
||||
Ok(bytes) => poem::Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header("Content-Type", "application/octet-stream")
|
||||
.header("Content-Disposition", "attachment; filename=\"huskies\"")
|
||||
.body(bytes),
|
||||
Err(e) => poem::Response::builder()
|
||||
.status(StatusCode::INTERNAL_SERVER_ERROR)
|
||||
.body(format!("Cannot read binary at {}: {e}", exe.display())),
|
||||
}
|
||||
}
|
||||
|
||||
/// Canonical artifact filename for sled binaries on this deployment's platform.
|
||||
///
|
||||
/// Sleds run linux/arm64 under OrbStack on Apple Silicon. When amd64 hosts
|
||||
|
||||
@@ -80,19 +80,6 @@ mod cli;
|
||||
|
||||
use cli::{parse_cli_args, resolve_path_arg};
|
||||
|
||||
/// Convert a WebSocket gateway URL into the binary download HTTP URL.
|
||||
///
|
||||
/// `ws://gateway:3000/api/sled-uplink?token=x` → `http://gateway:3000/api/huskies-binary`
|
||||
fn derive_binary_url_from_ws(ws_url: &str) -> Option<String> {
|
||||
let http = ws_url
|
||||
.strip_prefix("wss://")
|
||||
.map(|s| format!("https://{s}"))
|
||||
.or_else(|| ws_url.strip_prefix("ws://").map(|s| format!("http://{s}")))?;
|
||||
// Strip any path and query string, then append the binary endpoint.
|
||||
let base = http.split('/').take(3).collect::<Vec<_>>().join("/");
|
||||
Some(format!("{base}/api/huskies-binary"))
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
// Reap zombie grandchildren on Unix (for native deployments without tini/init).
|
||||
@@ -171,27 +158,6 @@ async fn main() -> Result<(), std::io::Error> {
|
||||
trampoline::run_trampoline(std::path::Path::new(job_path)).await;
|
||||
}
|
||||
|
||||
// ── Upgrade mode: fetch new binary, replace, exit ───────────────────────
|
||||
if cli.upgrade {
|
||||
let source = cli
|
||||
.upgrade_source
|
||||
.clone()
|
||||
.or_else(|| std::env::var("HUSKIES_BINARY_SOURCE").ok())
|
||||
.unwrap_or_else(|| {
|
||||
// Derive from HUSKIES_UPSTREAM_GATEWAY: ws://host:port/... → http://host:port/api/huskies-binary
|
||||
std::env::var("HUSKIES_UPSTREAM_GATEWAY")
|
||||
.ok()
|
||||
.and_then(|ws| derive_binary_url_from_ws(&ws))
|
||||
.unwrap_or_else(|| "http://gateway:3000/api/huskies-binary".to_string())
|
||||
});
|
||||
let target = upgrade::resolve_target_path();
|
||||
if let Err(e) = upgrade::run_cli_upgrade(&source, &target).await {
|
||||
eprintln!("error: {e}");
|
||||
std::process::exit(1);
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// ── Gateway mode: multi-project proxy ────────────────────────────────────
|
||||
if is_gateway {
|
||||
let config_dir = explicit_path.unwrap_or_else(|| cwd.clone());
|
||||
@@ -526,28 +492,4 @@ name = "coder"
|
||||
config::ProjectConfig::load(tmp.path())
|
||||
.unwrap_or_else(|e| panic!("Invalid project.toml: {e}"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_binary_url_strips_ws_scheme_and_path() {
|
||||
let url = derive_binary_url_from_ws("ws://gateway:3000/api/sled-uplink?token=abc");
|
||||
assert_eq!(
|
||||
url.as_deref(),
|
||||
Some("http://gateway:3000/api/huskies-binary")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_binary_url_handles_wss_scheme() {
|
||||
let url = derive_binary_url_from_ws("wss://myhost:443/path");
|
||||
assert_eq!(
|
||||
url.as_deref(),
|
||||
Some("https://myhost:443/api/huskies-binary")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn derive_binary_url_invalid_scheme_returns_none() {
|
||||
let url = derive_binary_url_from_ws("http://not-a-ws-url");
|
||||
assert!(url.is_none());
|
||||
}
|
||||
}
|
||||
|
||||
+3
-138
@@ -1,6 +1,5 @@
|
||||
//! Server rebuild and restart logic shared between the MCP tool and Matrix bot command.
|
||||
//! Shutdown notification and drain-and-exit sequence for server restarts.
|
||||
|
||||
use crate::agents::AgentPool;
|
||||
use crate::chat::ChatTransport;
|
||||
use crate::slog;
|
||||
use std::path::Path;
|
||||
@@ -77,8 +76,8 @@ impl BotShutdownNotifier {
|
||||
// ── Shared shutdown sequence ─────────────────────────────────────────────
|
||||
|
||||
/// Flush CRDT persistence, remove port files, and exit so Docker restarts
|
||||
/// the container with the new binary. Used by both `rebuild_and_restart`
|
||||
/// and `upgrade_and_reexec`.
|
||||
/// the container with the new binary. Called by `upgrade_and_reexec` after
|
||||
/// the binary has been replaced on disk.
|
||||
pub async fn drain_and_exit(project_root: &Path, label: &str) -> ! {
|
||||
crate::crdt_state::flush_persistence(std::time::Duration::from_secs(5)).await;
|
||||
|
||||
@@ -95,140 +94,6 @@ pub async fn drain_and_exit(project_root: &Path, label: &str) -> ! {
|
||||
std::process::exit(0)
|
||||
}
|
||||
|
||||
// ── Rebuild ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Rebuild the server binary and exit for Docker restart.
|
||||
///
|
||||
/// 1. Gracefully stops all running agents (kills PTY children).
|
||||
/// 2. Runs `cargo build [-p huskies]` from the workspace root, matching
|
||||
/// the current build profile (debug or release).
|
||||
/// 3. If the build fails, returns the build error (server stays up).
|
||||
/// 4. If the build succeeds, sends a best-effort shutdown notification (if a
|
||||
/// [`BotShutdownNotifier`] is provided), then calls [`drain_and_exit`] to
|
||||
/// flush persistence and exit. Docker's restart policy brings the
|
||||
/// container back up with the new binary.
|
||||
pub async fn rebuild_and_restart(
|
||||
agents: &AgentPool,
|
||||
project_root: &Path,
|
||||
notifier: Option<&BotShutdownNotifier>,
|
||||
) -> Result<String, String> {
|
||||
slog!("[rebuild] Rebuild and restart requested");
|
||||
|
||||
// 1. Gracefully stop all running agents.
|
||||
let running_count = agents
|
||||
.list_agents()
|
||||
.await
|
||||
.unwrap_or_default()
|
||||
.iter()
|
||||
.filter(|a| a.status == crate::agents::AgentStatus::Running)
|
||||
.count();
|
||||
if running_count > 0 {
|
||||
slog!("[rebuild] Stopping {running_count} running agent(s) before rebuild");
|
||||
}
|
||||
agents.kill_all_children().await;
|
||||
|
||||
// 2. Find the workspace root (parent of the server binary's source).
|
||||
// CARGO_MANIFEST_DIR at compile time points to the `server/` crate;
|
||||
// the workspace root is its parent. When running inside Docker the
|
||||
// compile-time path (/app) no longer exists — the source is bind-mounted
|
||||
// at the project_root instead, so fall back to that.
|
||||
let compile_time_root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
||||
.parent()
|
||||
.filter(|p| p.join("Cargo.toml").exists());
|
||||
let workspace_root = match compile_time_root {
|
||||
Some(p) => p.to_path_buf(),
|
||||
None => {
|
||||
if project_root.join("Cargo.toml").exists() {
|
||||
project_root.to_path_buf()
|
||||
} else {
|
||||
return Err(
|
||||
"Cannot determine workspace root: neither CARGO_MANIFEST_DIR nor project_root contain Cargo.toml".to_string(),
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
let workspace_root = workspace_root.as_path();
|
||||
|
||||
slog!(
|
||||
"[rebuild] Building server from workspace root: {}",
|
||||
workspace_root.display()
|
||||
);
|
||||
|
||||
// 3. Rebuild the frontend bundle so rust-embed picks up the latest assets.
|
||||
let frontend_dir = workspace_root.join("frontend");
|
||||
if frontend_dir.join("package.json").exists() {
|
||||
slog!("[rebuild] Building frontend");
|
||||
let fe_output = tokio::task::spawn_blocking({
|
||||
let frontend_dir = frontend_dir.clone();
|
||||
move || {
|
||||
std::process::Command::new("npm")
|
||||
.args(["run", "build"])
|
||||
.current_dir(&frontend_dir)
|
||||
.output()
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Frontend build task panicked: {e}"))?
|
||||
.map_err(|e| format!("Failed to run npm run build: {e}"))?;
|
||||
|
||||
if !fe_output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&fe_output.stderr);
|
||||
slog!("[rebuild] Frontend build failed:\n{stderr}");
|
||||
return Err(format!("Frontend build failed:\n{stderr}"));
|
||||
}
|
||||
slog!("[rebuild] Frontend build succeeded");
|
||||
}
|
||||
|
||||
// 4. Build the server binary, matching the current build profile so the
|
||||
// re-exec via current_exe() picks up the new binary.
|
||||
let build_args: Vec<&str> = if cfg!(debug_assertions) {
|
||||
vec!["build", "-p", "huskies"]
|
||||
} else {
|
||||
vec!["build", "--release", "-p", "huskies"]
|
||||
};
|
||||
slog!("[rebuild] cargo {}", build_args.join(" "));
|
||||
let output = tokio::task::spawn_blocking({
|
||||
let workspace_root = workspace_root.to_path_buf();
|
||||
move || {
|
||||
std::process::Command::new("cargo")
|
||||
.args(&build_args)
|
||||
.current_dir(&workspace_root)
|
||||
.output()
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map_err(|e| format!("Build task panicked: {e}"))?
|
||||
.map_err(|e| format!("Failed to run cargo build: {e}"))?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
slog!("[rebuild] Build failed:\n{stderr}");
|
||||
return Err(format!("Build failed:\n{stderr}"));
|
||||
}
|
||||
|
||||
// Write the current git HEAD to a file so we can verify which commit is running.
|
||||
if let Ok(head) = std::process::Command::new("git")
|
||||
.args(["rev-parse", "--short", "HEAD"])
|
||||
.current_dir(workspace_root)
|
||||
.output()
|
||||
{
|
||||
let hash = String::from_utf8_lossy(&head.stdout).trim().to_string();
|
||||
let _ = std::fs::write(workspace_root.join(".huskies/build_hash"), &hash);
|
||||
slog!("[rebuild] Build succeeded (commit {hash}), re-execing with new binary");
|
||||
} else {
|
||||
slog!("[rebuild] Build succeeded, re-execing with new binary");
|
||||
}
|
||||
|
||||
// 5. Send shutdown notification before replacing the process so that chat
|
||||
// participants know the bot is going offline. Best-effort only — we
|
||||
// do not abort the rebuild if the send fails.
|
||||
if let Some(n) = notifier {
|
||||
n.notify(ShutdownReason::Rebuild).await;
|
||||
}
|
||||
|
||||
drain_and_exit(project_root, "rebuild").await
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -64,11 +64,6 @@ pub(super) async fn call_rmtree(
|
||||
.await
|
||||
}
|
||||
|
||||
/// Call the Matrix `rebuild` handler.
|
||||
pub(super) async fn call_rebuild(project_root: &Path, agents: &Arc<AgentPool>) -> String {
|
||||
crate::chat::transport::matrix::rebuild::handle_rebuild("web-ui", project_root, agents).await
|
||||
}
|
||||
|
||||
/// Parse and execute a `timer` command.
|
||||
///
|
||||
/// Returns `Err` with a usage string if the timer arguments cannot be parsed.
|
||||
|
||||
@@ -83,7 +83,6 @@ pub async fn execute(
|
||||
let number = parse::parse_number("rmtree", args).map_err(Error::BadArgs)?;
|
||||
Ok(io::call_rmtree(&number, project_root, agents).await)
|
||||
}
|
||||
"rebuild" => Ok(io::call_rebuild(project_root, agents).await),
|
||||
"timer" => io::call_timer(args, project_root)
|
||||
.await
|
||||
.map_err(Error::BadArgs),
|
||||
|
||||
@@ -534,12 +534,12 @@ mod tests {
|
||||
assert_eq!(parse_mode(&args), FireMode::Persistent);
|
||||
}
|
||||
|
||||
/// Regression test: once-mode triggers with a server-restarting action (e.g.
|
||||
/// rebuild_and_restart) must be removed from the store BEFORE the action is
|
||||
/// dispatched. If cancellation happens after dispatch, a server restart
|
||||
/// caused by the action reloads the persisted store and replays the trigger.
|
||||
/// Regression test: once-mode triggers with a server-restarting action
|
||||
/// must be removed from the store BEFORE the action is dispatched. If
|
||||
/// cancellation happens after dispatch, a server restart caused by the
|
||||
/// action reloads the persisted store and replays the trigger.
|
||||
#[test]
|
||||
fn once_mode_rebuild_trigger_cancelled_before_action() {
|
||||
fn once_mode_restart_trigger_cancelled_before_action() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
let path = dir.path().join("triggers.json");
|
||||
let store = EventTriggerStore::load(path.clone());
|
||||
@@ -553,7 +553,7 @@ mod tests {
|
||||
event_kind: None,
|
||||
},
|
||||
TriggerAction::Mcp {
|
||||
method: "rebuild_and_restart".to_string(),
|
||||
method: "server_restarting_action".to_string(),
|
||||
args: serde_json::json!({}),
|
||||
},
|
||||
FireMode::Once,
|
||||
|
||||
@@ -76,22 +76,6 @@ pub async fn upgrade_and_reexec(source_url: &str, project_root: &Path) -> Result
|
||||
crate::rebuild::drain_and_exit(project_root, "upgrade").await
|
||||
}
|
||||
|
||||
// ── CLI upgrade (no re-exec) ─────────────────────────────────────────────
|
||||
|
||||
/// Run the `huskies upgrade` CLI subcommand: download, replace, and exit.
|
||||
///
|
||||
/// Unlike [`upgrade_and_reexec`], this does not flush the CRDT or re-exec
|
||||
/// because the CLI subcommand is run as a standalone command (not the server).
|
||||
/// After this returns the caller should exit.
|
||||
pub async fn run_cli_upgrade(source_url: &str, target: &Path) -> Result<(), String> {
|
||||
fetch_and_replace_binary(source_url, target).await?;
|
||||
println!(
|
||||
"Upgrade complete. New binary installed at {}.",
|
||||
target.display()
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Resolve the path to replace with the new binary.
|
||||
|
||||
Reference in New Issue
Block a user