feat(maestro): spawn protocol types + relay + daemon HTTP for recents/gitroot/presets (#26)
Expose the daemon-side spawn data (#24 recents, #25 gitroot, #22 presets) to the web, mirroring the NAP-329 ListDir broker plumbing end to end so it works across federated hosts. Wire (maestro-protocol): `RecentDir`/`RecentDirs`/`GitRoot` payloads; `DaemonInbound::RecentDirs`/`GitRoot` requests + `ForwardRecentDirs`/ `ForwardGitRoot` relay pairs + their `*Result` replies on both enums. All forward-compatible via the existing `RpcResult` + `#[serde(other)] Unknown`. Relay (broker + registry + registry_client): home daemon → registry → owning daemon → back, with the same fail-fast-while-disconnected + RAII pending-entry cleanup discipline as ListDir. The owning daemon answers RecentDirs from its spawn history and GitRoot by resolving the path against its home dir (shared `dirlist::resolve`) then walking up via `gitroot::find_root`. Daemon HTTP (maestro-daemon): - GET /api/daemons/:id/recent-dirs → RecentDirs - GET /api/daemons/:id/gitroot?path= → GitRoot - Presets ride GET /api/web/info (the documented DaemonInfo sibling) so the form needs no extra round-trip; resolved once at startup from [spawn].presets (a bad preset list now refuses startup). Tests: protocol round-trip + forward-compat for the new variants; broker forward round-trips + fail-fast; HTTP handler tests (self recents/gitroot, presets advertisement, peer 503 without a registry link). Closes #26 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -85,6 +85,36 @@ pub struct DirListing {
|
||||
pub truncated: bool,
|
||||
}
|
||||
|
||||
/// One deduplicated recent working directory for the spawn form's "recent
|
||||
/// dirs" picker (#24/#26). Newest use of a `cwd`, with the command last run
|
||||
/// there. Mirrors the daemon-local `spawn_history::RecentDir`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RecentDir {
|
||||
pub cwd: PathBuf,
|
||||
pub last_command: String,
|
||||
pub last_used: DateTime<Utc>,
|
||||
}
|
||||
|
||||
/// Result of a `RecentDirs` query: a daemon's recently-used working dirs,
|
||||
/// newest-first and deduped by `cwd` (#26). `truncated` mirrors [`DirListing`]:
|
||||
/// true when the daemon capped the list.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct RecentDirs {
|
||||
pub dirs: Vec<RecentDir>,
|
||||
#[serde(default)]
|
||||
pub truncated: bool,
|
||||
}
|
||||
|
||||
/// Result of a `GitRoot` probe: the enclosing git repository toplevel of a
|
||||
/// path on a daemon's host, or `None` when the path isn't inside a repo (#26).
|
||||
/// Drives the spawn form's worktree section — a present `root` is the inferred
|
||||
/// `base_repo`; `None` disables the worktree toggle.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct GitRoot {
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub root: Option<PathBuf>,
|
||||
}
|
||||
|
||||
/// Structured failure used everywhere we don't want to leak a string-only error.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct ErrorInfo {
|
||||
@@ -192,6 +222,34 @@ pub enum DaemonOutbound {
|
||||
daemon_id: String,
|
||||
path: String,
|
||||
},
|
||||
/// Owning daemon's reply to a [`DaemonInbound::RecentDirs`] — its recent
|
||||
/// working dirs. (#26)
|
||||
RecentDirsResult {
|
||||
reply_to: ReplyTo,
|
||||
result: RpcResult<RecentDirs>,
|
||||
},
|
||||
/// Home daemon asks the registry to relay a recent-dirs query to the daemon
|
||||
/// named by `daemon_id` (which may be a peer). Recents are daemon-wide so
|
||||
/// there's no path argument. Reply:
|
||||
/// [`DaemonInbound::ForwardRecentDirsResult`]. (#26)
|
||||
ForwardRecentDirs {
|
||||
reply_to: ReplyTo,
|
||||
daemon_id: String,
|
||||
},
|
||||
/// Owning daemon's reply to a [`DaemonInbound::GitRoot`] — the enclosing
|
||||
/// repo toplevel of the requested path on its host. (#26)
|
||||
GitRootResult {
|
||||
reply_to: ReplyTo,
|
||||
result: RpcResult<GitRoot>,
|
||||
},
|
||||
/// Home daemon asks the registry to relay a gitroot probe to the daemon
|
||||
/// named by `daemon_id` (which may be a peer). Reply:
|
||||
/// [`DaemonInbound::ForwardGitRootResult`]. (#26)
|
||||
ForwardGitRoot {
|
||||
reply_to: ReplyTo,
|
||||
daemon_id: String,
|
||||
path: String,
|
||||
},
|
||||
/// Target daemon answers a [`DaemonInbound::RelaySetup`]: whether it will
|
||||
/// accept relay traffic for the requested session. Registry only allocates
|
||||
/// relay capacity when `accepted == true`.
|
||||
@@ -251,6 +309,31 @@ pub enum DaemonInbound {
|
||||
reply_to: ReplyTo,
|
||||
result: RpcResult<DirListing>,
|
||||
},
|
||||
/// Registry asks a daemon for its recent working dirs (#26). Daemon-wide,
|
||||
/// so no arguments beyond the correlation token; the daemon replies with
|
||||
/// [`DaemonOutbound::RecentDirsResult`].
|
||||
RecentDirs {
|
||||
reply_to: ReplyTo,
|
||||
},
|
||||
/// Registry's reply to [`DaemonOutbound::ForwardRecentDirs`] — the owning
|
||||
/// daemon's recents, relayed back to the home daemon. (#26)
|
||||
ForwardRecentDirsResult {
|
||||
reply_to: ReplyTo,
|
||||
result: RpcResult<RecentDirs>,
|
||||
},
|
||||
/// Registry asks a daemon to probe the enclosing git repo of `path` on its
|
||||
/// host (#26). The daemon expands `~`/relative/empty against its own home
|
||||
/// dir, then replies with [`DaemonOutbound::GitRootResult`].
|
||||
GitRoot {
|
||||
reply_to: ReplyTo,
|
||||
path: String,
|
||||
},
|
||||
/// Registry's reply to [`DaemonOutbound::ForwardGitRoot`] — the owning
|
||||
/// daemon's gitroot probe, relayed back to the home daemon. (#26)
|
||||
ForwardGitRootResult {
|
||||
reply_to: ReplyTo,
|
||||
result: RpcResult<GitRoot>,
|
||||
},
|
||||
/// Registry signals a target daemon that a home daemon wants to attach via
|
||||
/// the registry-mediated relay (T3). Target decides whether to accept the
|
||||
/// relay and replies with [`DaemonOutbound::RelayAck`].
|
||||
@@ -1002,6 +1085,110 @@ mod tests {
|
||||
assert_eq!(res, round_trip(&res));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn daemon_recent_dirs_round_trips_and_tag() {
|
||||
let req = DaemonInbound::RecentDirs {
|
||||
reply_to: Uuid::from_u128(71),
|
||||
};
|
||||
let v = serde_json::to_value(&req).unwrap();
|
||||
assert_eq!(v["type"], "RecentDirs");
|
||||
assert_eq!(req, round_trip(&req));
|
||||
|
||||
let res = DaemonOutbound::RecentDirsResult {
|
||||
reply_to: Uuid::from_u128(71),
|
||||
result: RpcResult::Ok {
|
||||
value: RecentDirs {
|
||||
dirs: vec![RecentDir {
|
||||
cwd: PathBuf::from("/home/naps62/git/maestro"),
|
||||
last_command: "claude".into(),
|
||||
last_used: DateTime::parse_from_rfc3339("2026-06-20T10:00:00Z")
|
||||
.unwrap()
|
||||
.with_timezone(&Utc),
|
||||
}],
|
||||
truncated: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
let v = serde_json::to_value(&res).unwrap();
|
||||
assert_eq!(v["type"], "RecentDirsResult");
|
||||
assert_eq!(res, round_trip(&res));
|
||||
|
||||
let fwd = DaemonOutbound::ForwardRecentDirs {
|
||||
reply_to: Uuid::from_u128(72),
|
||||
daemon_id: "konishi".into(),
|
||||
};
|
||||
let v = serde_json::to_value(&fwd).unwrap();
|
||||
assert_eq!(v["type"], "ForwardRecentDirs");
|
||||
assert_eq!(v["daemon_id"], "konishi");
|
||||
assert_eq!(fwd, round_trip(&fwd));
|
||||
|
||||
let fwd_res = DaemonInbound::ForwardRecentDirsResult {
|
||||
reply_to: Uuid::from_u128(72),
|
||||
result: RpcResult::Ok {
|
||||
value: RecentDirs {
|
||||
dirs: vec![],
|
||||
truncated: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
let v = serde_json::to_value(&fwd_res).unwrap();
|
||||
assert_eq!(v["type"], "ForwardRecentDirsResult");
|
||||
assert_eq!(fwd_res, round_trip(&fwd_res));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn recent_dirs_truncated_defaults_when_absent() {
|
||||
// Older daemons omit `truncated`; it must default to false.
|
||||
let json = serde_json::json!({ "dirs": [] });
|
||||
let parsed: RecentDirs = serde_json::from_value(json).unwrap();
|
||||
assert!(!parsed.truncated);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn daemon_git_root_round_trips_and_tag() {
|
||||
let req = DaemonInbound::GitRoot {
|
||||
reply_to: Uuid::from_u128(81),
|
||||
path: "~/git/maestro/crates".into(),
|
||||
};
|
||||
let v = serde_json::to_value(&req).unwrap();
|
||||
assert_eq!(v["type"], "GitRoot");
|
||||
assert_eq!(v["path"], "~/git/maestro/crates");
|
||||
assert_eq!(req, round_trip(&req));
|
||||
|
||||
let res = DaemonOutbound::GitRootResult {
|
||||
reply_to: Uuid::from_u128(81),
|
||||
result: RpcResult::Ok {
|
||||
value: GitRoot {
|
||||
root: Some(PathBuf::from("/home/naps62/git/maestro")),
|
||||
},
|
||||
},
|
||||
};
|
||||
let v = serde_json::to_value(&res).unwrap();
|
||||
assert_eq!(v["type"], "GitRootResult");
|
||||
assert_eq!(res, round_trip(&res));
|
||||
|
||||
let fwd = DaemonOutbound::ForwardGitRoot {
|
||||
reply_to: Uuid::from_u128(82),
|
||||
daemon_id: "konishi".into(),
|
||||
path: "/work".into(),
|
||||
};
|
||||
let v = serde_json::to_value(&fwd).unwrap();
|
||||
assert_eq!(v["type"], "ForwardGitRoot");
|
||||
assert_eq!(fwd, round_trip(&fwd));
|
||||
|
||||
// A `None` root (path not inside a repo) drops the field from the wire.
|
||||
let none = DaemonInbound::ForwardGitRootResult {
|
||||
reply_to: Uuid::from_u128(82),
|
||||
result: RpcResult::Ok {
|
||||
value: GitRoot { root: None },
|
||||
},
|
||||
};
|
||||
let v = serde_json::to_value(&none).unwrap();
|
||||
assert_eq!(v["type"], "ForwardGitRootResult");
|
||||
assert_eq!(v["result"]["value"], serde_json::json!({}));
|
||||
assert_eq!(none, round_trip(&none));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn daemon_outbound_relay_ack_round_trip_and_tag() {
|
||||
let msg = DaemonOutbound::RelayAck {
|
||||
|
||||
Reference in New Issue
Block a user