1use std::path::Path;
13
14use anyhow::{Context, Result, bail, ensure};
15
16use super::worker_binary::{bridge_launch, container_upload_ownership_args, stage_profile};
17use super::{Controller, execute_checked};
18use crate::targets::{self, CommandExecutor, CommandSpec, ProcessExecutor};
19use mj_core::worker_launch::{
20 REVIEWER_DIR, ReviewMcpDelivery, ReviewMcpServer, ReviewerLaunchConfig,
21 reviewer_staging_profile_home,
22};
23
24pub fn reviewer_stager() -> mj_client::session::ReviewerStager {
28 mj_client::session::ReviewerStager::new(ControllerReviewerStager)
29}
30
31struct ControllerReviewerStager;
32
33impl mj_client::session::ReviewerStagerBackend for ControllerReviewerStager {
34 fn stage(
35 &self,
36 _config: mj_core::config::Config,
37 session: mj_core::state::SessionRecord,
38 profile_id: String,
39 generation: u64,
40 cancelled: std::sync::Arc<std::sync::atomic::AtomicBool>,
41 ) -> Result<ReviewerLaunchConfig> {
42 let session_id = session.id.clone();
43 let controller = Controller::load()?;
44 let executor = crate::targets::CancellableProcessExecutor::new(cancelled)
45 .with_deadline(std::time::Duration::from_secs(90));
46 controller.stage_reviewer_profile_controlled(
47 &session_id,
48 &profile_id,
49 generation,
50 &[],
51 &executor,
52 )
53 }
54}
55
56impl Controller {
57 pub fn stage_reviewer_profile(
63 &self,
64 session_id: &str,
65 profile_id: &str,
66 generation: u64,
67 ) -> Result<ReviewerLaunchConfig> {
68 self.stage_reviewer_profile_controlled(
69 session_id,
70 profile_id,
71 generation,
72 &[],
73 &ProcessExecutor,
74 )
75 }
76
77 pub fn stage_reviewer_profile_with_mcp(
85 &self,
86 session_id: &str,
87 profile_id: &str,
88 generation: u64,
89 mcp_servers: &[ReviewMcpServer],
90 dispatch_tool: bool,
91 ) -> Result<ReviewerLaunchConfig> {
92 let mut servers = mcp_servers.to_vec();
93 if dispatch_tool {
94 let (_, worker_root) = self.worker_placement(session_id)?;
95 servers.push(review_dispatch_server(&worker_root));
96 }
97 self.stage_reviewer_profile_controlled(
98 session_id,
99 profile_id,
100 generation,
101 &servers,
102 &ProcessExecutor,
103 )
104 }
105
106 pub fn stage_reviewer_profile_controlled(
107 &self,
108 session_id: &str,
109 profile_id: &str,
110 generation: u64,
111 mcp_servers: &[ReviewMcpServer],
112 executor: &impl CommandExecutor,
113 ) -> Result<ReviewerLaunchConfig> {
114 let profile = self
115 .config
116 .profiles
117 .get(profile_id)
118 .with_context(|| format!("unknown profile {profile_id:?}"))?;
119 ensure!(
120 profile.enabled,
121 "reviewer profile {profile_id:?} is disabled"
122 );
123 if !profile.kind.supports_injected_mcp() {
124 bail!(
125 "Muse Code cannot be a reviewer: muse-acp does not accept the required MCP tools. Select another reviewer profile."
126 );
127 }
128 let session = self
129 .state
130 .sessions
131 .get(session_id)
132 .with_context(|| format!("unknown session {session_id}"))?;
133 let target = self
134 .config
135 .targets
136 .get(&session.target_template_id)
137 .context("session target template is missing")?;
138 let execution_policy = profile
139 .kind
140 .effective_execution_policy(target.execution_policy());
141 let (backend, worker_root) = self.worker_placement(session_id)?;
142
143 let staging = tempfile::tempdir().context("create reviewer staging directory")?;
144 let local = staging.path().join("profile");
145 stage_profile(profile, &local).with_context(|| format!("stage profile {profile_id:?}"))?;
146 if !mcp_servers.is_empty()
150 && ReviewMcpDelivery::for_harness(profile.kind) == ReviewMcpDelivery::HarnessProfile
151 {
152 configure_staged_review_mcp(profile.kind, &local, mcp_servers)
153 .with_context(|| format!("configure reviewer MCP servers for {profile_id:?}"))?;
154 }
155 upload_reviewer_profile(executor, &backend, &worker_root, generation, &local)?;
156
157 let (bridge_command, bridge_args) = bridge_launch(profile.kind, execution_policy);
158 let mut environment = profile.environment.clone();
159 environment.remove(profile.home_env());
163 Ok(ReviewerLaunchConfig {
164 profile_id: profile_id.to_owned(),
165 harness: profile.kind,
166 bridge_command: bridge_command.into(),
167 bridge_args,
168 environment,
169 execution_policy,
170 model: None,
171 effort: None,
172 fast_mode: None,
173 generation,
174 mcp_servers: match ReviewMcpDelivery::for_harness(profile.kind) {
178 ReviewMcpDelivery::Acp => mcp_servers.to_vec(),
179 ReviewMcpDelivery::HarnessProfile => Vec::new(),
180 },
181 })
182 }
183}
184
185fn configure_staged_review_mcp(
193 harness: mj_core::config::HarnessKind,
194 profile_stage: &Path,
195 servers: &[ReviewMcpServer],
196) -> Result<()> {
197 let Some(file) = harness.mcp_config_file() else {
198 bail!("{harness:?} does not read MCP servers from its profile");
199 };
200 let kimi = harness == mj_core::config::HarnessKind::Kimi;
201 let path = profile_stage.join(file);
202 let mut document = match std::fs::read(&path) {
203 Ok(body) => serde_json::from_slice::<serde_json::Value>(&body)
204 .with_context(|| format!("parse staged reviewer configuration {}", path.display()))?,
205 Err(error) if error.kind() == std::io::ErrorKind::NotFound => {
206 serde_json::Value::Object(serde_json::Map::new())
207 }
208 Err(error) => {
209 return Err(error)
210 .with_context(|| format!("read staged reviewer configuration {}", path.display()));
211 }
212 };
213 let root = document.as_object_mut().with_context(|| {
214 format!(
215 "staged reviewer configuration {} must contain a JSON object",
216 path.display()
217 )
218 })?;
219 let configured = root
220 .entry("mcpServers")
221 .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()))
222 .as_object_mut()
223 .with_context(|| {
224 format!(
225 "mcpServers in staged reviewer configuration {} must be a JSON object",
226 path.display()
227 )
228 })?;
229 for server in servers {
230 let mut entry = serde_json::json!({
231 "command": server.command,
232 "args": server.args,
233 });
234 if kimi {
235 let object = entry
236 .as_object_mut()
237 .expect("the server entry is a JSON object");
238 object.insert("transport".into(), "stdio".into());
239 object.insert("runtime_id".into(), "local".into());
240 } else {
241 let object = entry
242 .as_object_mut()
243 .expect("the server entry is a JSON object");
244 object.insert("type".into(), "stdio".into());
245 }
246 configured.insert(server.name.clone(), entry);
247 }
248 let mut body = serde_json::to_vec_pretty(&document)?;
249 body.push(b'\n');
250 mj_core::config::atomic_write(&path, &body)
251 .with_context(|| format!("write staged reviewer configuration {}", path.display()))
252}
253
254fn review_dispatch_server(worker_root: &str) -> ReviewMcpServer {
258 let socket = format!(
259 "{worker_root}/{}/{}",
260 REVIEWER_DIR,
261 mj_core::review::mcp::REVIEW_DISPATCH_SOCKET
262 );
263 ReviewMcpServer {
264 name: mj_core::review::mcp::REVIEW_MCP_SERVER_NAME.to_owned(),
265 command: Path::new(worker_root).join("hel"),
266 args: vec![
267 "worker".to_owned(),
268 "review-mcp".to_owned(),
269 "--socket".to_owned(),
270 socket,
271 ],
272 }
273}
274
275fn reviewer_profile_home(worker_root: &str, generation: u64) -> String {
281 reviewer_staging_profile_home(Path::new(worker_root), generation)
282 .to_string_lossy()
283 .into_owned()
284}
285
286fn upload_reviewer_profile(
292 executor: &impl CommandExecutor,
293 locator: &targets::TargetLocator,
294 worker_root: &str,
295 generation: u64,
296 local: &Path,
297) -> Result<()> {
298 let home = reviewer_profile_home(worker_root, generation);
299 match locator {
300 targets::TargetLocator::LocalBare { .. } => {
301 for command in [
302 CommandSpec::new("rm", ["-rf", "--", &home])
303 .purpose("clear the local reviewer profile"),
304 CommandSpec::new("mkdir", ["-p", &home])
305 .purpose("create the local reviewer profile directory"),
306 CommandSpec::new(
307 "cp",
308 [
309 "-R".to_owned(),
310 format!("{}/.", local.display()),
311 home.clone(),
312 ],
313 )
314 .purpose("install the local reviewer profile"),
315 CommandSpec::new("chmod", ["-R", "go-rwx", &home])
316 .purpose("restrict local reviewer profile permissions"),
317 ] {
318 execute_checked(executor, command)?;
319 }
320 }
321 targets::TargetLocator::LocalPodman { container_id, .. }
322 | targets::TargetLocator::LocalDocker { container_id, .. }
323 | targets::TargetLocator::AppleContainer { container_id, .. } => {
324 let engine = match locator {
325 targets::TargetLocator::LocalPodman { .. } => "podman",
326 targets::TargetLocator::LocalDocker { .. } => "docker",
327 targets::TargetLocator::AppleContainer { .. } => "container",
328 _ => unreachable!("matched local container target"),
329 };
330 for arguments in [
331 vec![
332 "exec".to_owned(),
333 container_id.clone(),
334 "rm".to_owned(),
335 "-rf".to_owned(),
336 "--".to_owned(),
337 home.clone(),
338 ],
339 vec![
340 "exec".to_owned(),
341 container_id.clone(),
342 "mkdir".to_owned(),
343 "-p".to_owned(),
344 home.clone(),
345 ],
346 vec![
347 "cp".to_owned(),
348 format!("{}/.", local.display()),
349 format!("{container_id}:{home}"),
350 ],
351 container_upload_ownership_args(container_id, worker_root, &[&home]),
352 vec![
353 "exec".to_owned(),
354 container_id.clone(),
355 "chmod".to_owned(),
356 "-R".to_owned(),
357 "go-rwx".to_owned(),
358 home.clone(),
359 ],
360 ] {
361 execute_checked(
362 executor,
363 CommandSpec::new(engine, arguments).purpose("stage the reviewer profile"),
364 )?;
365 }
366 }
367 targets::TargetLocator::AwsEc2 { ssh, .. }
368 | targets::TargetLocator::SshBare { ssh, .. } => {
369 let incoming = format!("{home}.incoming");
370 execute_checked(
371 executor,
372 crate::targets::ssh_command(ssh, ["mkdir", "-p", worker_root])
373 .purpose("create the reviewer directory"),
374 )?;
375 execute_checked(
376 executor,
377 crate::targets::ssh_command(ssh, ["rm", "-rf", "--", &incoming, &home])
378 .purpose("clear the reviewer profile"),
379 )?;
380 execute_checked(
381 executor,
382 crate::targets::scp_upload(ssh, local, &incoming, true)
383 .purpose("upload the reviewer profile"),
384 )?;
385 execute_checked(
386 executor,
387 crate::targets::ssh_command(ssh, ["mv", &incoming, &home])
388 .purpose("install the reviewer profile"),
389 )?;
390 execute_checked(
391 executor,
392 crate::targets::ssh_command(ssh, ["chmod", "-R", "go-rwx", &home])
393 .purpose("restrict reviewer profile permissions"),
394 )?;
395 }
396 targets::TargetLocator::SshPodman {
397 ssh, container_id, ..
398 }
399 | targets::TargetLocator::SshDocker {
400 ssh, container_id, ..
401 } => {
402 let engine = match locator {
403 targets::TargetLocator::SshPodman { .. } => "podman",
404 targets::TargetLocator::SshDocker { .. } => "docker",
405 _ => unreachable!("matched remote container target"),
406 };
407 let upload = format!("{worker_root}/.reviewer-upload-{generation}");
408 execute_checked(
409 executor,
410 crate::targets::ssh_command(ssh, ["rm", "-rf", "--", &upload])
411 .purpose("clear remote reviewer staging"),
412 )?;
413 execute_checked(
414 executor,
415 crate::targets::scp_upload(ssh, local, &upload, true)
416 .purpose("upload the remote reviewer profile"),
417 )?;
418 for arguments in [
419 vec![
420 engine.to_owned(),
421 "exec".to_owned(),
422 container_id.clone(),
423 "rm".to_owned(),
424 "-rf".to_owned(),
425 "--".to_owned(),
426 home.clone(),
427 ],
428 vec![
429 engine.to_owned(),
430 "exec".to_owned(),
431 container_id.clone(),
432 "mkdir".to_owned(),
433 "-p".to_owned(),
434 home.clone(),
435 ],
436 vec![
437 engine.to_owned(),
438 "cp".to_owned(),
439 format!("{upload}/."),
440 format!("{container_id}:{home}"),
441 ],
442 std::iter::once(engine.to_owned())
443 .chain(container_upload_ownership_args(
444 container_id,
445 worker_root,
446 &[&home],
447 ))
448 .collect(),
449 vec![
450 engine.to_owned(),
451 "exec".to_owned(),
452 container_id.clone(),
453 "chmod".to_owned(),
454 "-R".to_owned(),
455 "go-rwx".to_owned(),
456 home.clone(),
457 ],
458 ] {
459 execute_checked(
460 executor,
461 crate::targets::ssh_command(ssh, arguments)
462 .purpose("stage the remote reviewer profile"),
463 )?;
464 }
465 execute_checked(
466 executor,
467 crate::targets::ssh_command(ssh, ["rm", "-rf", "--", &upload])
468 .purpose("remove remote reviewer staging"),
469 )?;
470 }
471 }
472 if home.trim().is_empty() {
473 bail!("the reviewer profile home resolved to an empty path");
474 }
475 Ok(())
476}
477
478#[cfg(test)]
479mod tests {
480 use std::cell::RefCell;
481 use std::collections::BTreeMap;
482
483 use super::*;
484 use crate::controller::test_support::checkpoint_test_session;
485 use mj_core::config::{Config, HarnessKind, HarnessProfile, TargetTemplate};
486 use mj_core::state::{SessionState, State};
487
488 use crate::targets::CommandOutput;
489
490 struct RecordingExecutor {
491 commands: RefCell<Vec<CommandSpec>>,
492 }
493
494 impl RecordingExecutor {
495 fn new() -> Self {
496 Self {
497 commands: RefCell::new(Vec::new()),
498 }
499 }
500
501 fn script(&self) -> Vec<String> {
503 self.commands
504 .borrow()
505 .iter()
506 .map(|command| format!("{} {}", command.program, command.args.join(" ")))
507 .collect()
508 }
509 }
510
511 impl CommandExecutor for RecordingExecutor {
512 fn execute(&self, command: &CommandSpec) -> Result<CommandOutput> {
513 self.commands.borrow_mut().push(command.clone());
514 Ok(CommandOutput {
515 status: 0,
516 stdout: Vec::new(),
517 stderr: Vec::new(),
518 })
519 }
520 }
521
522 const SESSION_ID: &str = "0123456789abcdef0123456789abcdef";
525
526 fn fixture(directory: &Path, locator: mj_core::state::TargetLocator) -> (Controller, String) {
527 let session_id = SESSION_ID;
528 let mut session = checkpoint_test_session(session_id);
529 session.target_template_id = "local".into();
530 session.state = SessionState::Running;
531 session.target = Some(locator);
532 let mut config = Config::default();
533 config
534 .targets
535 .insert("local".into(), TargetTemplate::LocalBare);
536 for (id, kind) in [
537 ("codex", HarnessKind::Codex),
538 ("claude", HarnessKind::Claude),
539 ] {
540 let home = directory.join(id);
541 std::fs::create_dir_all(&home).unwrap();
542 config.profiles.insert(
543 id.to_owned(),
544 HarnessProfile {
545 enabled: true,
546 kind,
547 home,
548 environment: BTreeMap::from([("EXTRA".into(), "1".into())]),
549 context_window_bytes: None,
550 guardian_review_model: None,
551 },
552 );
553 }
554 (
555 Controller {
556 config,
557 state: State {
558 sessions: BTreeMap::from([(session_id.into(), session)]),
559 ..State::default()
560 },
561 },
562 session_id.to_owned(),
563 )
564 }
565
566 #[test]
567 fn staging_copies_the_chosen_profile_into_the_worker_root() {
568 let directory = tempfile::tempdir().unwrap();
569 let worker_root = directory.path().join(SESSION_ID);
570 std::fs::create_dir_all(directory.path().join("claude")).unwrap();
571 std::fs::write(directory.path().join("claude/CLAUDE.md"), b"reviewer").unwrap();
573 let (controller, session_id) = fixture(
574 directory.path(),
575 mj_core::state::TargetLocator::LocalBare {
576 worker_root: worker_root.clone(),
577 },
578 );
579 let executor = RecordingExecutor::new();
580
581 let config = controller
582 .stage_reviewer_profile_controlled(&session_id, "claude", 0, &[], &executor)
583 .unwrap();
584
585 assert_eq!(config.profile_id, "claude");
586 assert_eq!(config.harness, HarnessKind::Claude);
587 assert_eq!(config.generation, 0);
588 assert_eq!(config.model, None);
589 assert_eq!(config.effort, None);
590 assert!(
592 !config
593 .environment
594 .contains_key(HarnessKind::Claude.home_env())
595 );
596 assert_eq!(
597 config.environment.get("EXTRA").map(String::as_str),
598 Some("1")
599 );
600
601 let home = format!("{}/reviewer/profile", worker_root.display());
602 let script = executor.script();
603 let cleared = script
604 .iter()
605 .position(|line| line.starts_with("rm ") && line.contains(&home))
606 .expect("the previous reviewer profile is cleared");
607 let copied = script
608 .iter()
609 .position(|line| line.starts_with("cp ") && line.ends_with(&home))
610 .expect("the staged profile is installed");
611 assert!(
612 cleared < copied,
613 "a stale profile must go before the new one lands: {script:?}"
614 );
615 assert!(
616 script
617 .iter()
618 .any(|line| line.contains("go-rwx") && line.contains(&home)),
619 "the reviewer profile must not be world readable: {script:?}"
620 );
621 }
622
623 #[test]
624 fn local_container_targets_stage_the_reviewer_through_their_engine() {
625 let directory = tempfile::tempdir().unwrap();
626 let container_id = crate::targets::resource_name(SESSION_ID).unwrap();
627 for (locator, engine) in [
628 (
629 mj_core::state::TargetLocator::LocalPodman {
630 borrowed_from: None,
631 container_id: container_id.clone(),
632 workspace_storage: Default::default(),
633 },
634 "podman",
635 ),
636 (
637 mj_core::state::TargetLocator::LocalDocker {
638 borrowed_from: None,
639 container_id: container_id.clone(),
640 },
641 "docker",
642 ),
643 ] {
644 let (controller, session_id) = fixture(directory.path(), locator);
645 let executor = RecordingExecutor::new();
646
647 controller
648 .stage_reviewer_profile_controlled(&session_id, "codex", 3, &[], &executor)
649 .unwrap();
650
651 let script = executor.script();
652 assert!(
653 script
654 .iter()
655 .all(|line| line.starts_with(&format!("{engine} "))),
656 "a container target is reached only through its engine: {script:?}"
657 );
658 let home = script
659 .iter()
660 .find_map(|line| {
661 line.split(' ')
662 .find(|word| word.contains("/reviewer/profile"))
663 })
664 .expect("the reviewer profile is placed")
665 .to_owned();
666 assert!(
667 home.contains(&format!("/{session_id}")),
668 "the reviewer lives under this session's worker root: {home}"
669 );
670 assert!(
672 !script.iter().any(|line| {
673 line.contains("run") || line.contains("git") || line.contains("create")
674 }),
675 "staging a reviewer provisions nothing: {script:?}"
676 );
677 }
678 }
679
680 #[test]
681 fn an_unknown_profile_is_refused_before_anything_is_copied() {
682 let directory = tempfile::tempdir().unwrap();
683 let (controller, session_id) = fixture(
684 directory.path(),
685 mj_core::state::TargetLocator::LocalBare {
686 worker_root: directory.path().join(SESSION_ID),
687 },
688 );
689 let executor = RecordingExecutor::new();
690
691 let error = controller
692 .stage_reviewer_profile_controlled(&session_id, "missing", 0, &[], &executor)
693 .unwrap_err();
694
695 assert!(format!("{error:#}").contains("unknown profile"));
696 assert!(executor.commands.borrow().is_empty());
697 }
698
699 #[test]
700 fn a_new_generation_travels_to_the_worker_so_it_starts_a_fresh_reviewer() {
701 let directory = tempfile::tempdir().unwrap();
702 let (controller, session_id) = fixture(
703 directory.path(),
704 mj_core::state::TargetLocator::LocalBare {
705 worker_root: directory.path().join(SESSION_ID),
706 },
707 );
708 let executor = RecordingExecutor::new();
709
710 let first = controller
711 .stage_reviewer_profile_controlled(&session_id, "codex", 0, &[], &executor)
712 .unwrap();
713 let second = controller
714 .stage_reviewer_profile_controlled(&session_id, "codex", 1, &[], &executor)
715 .unwrap();
716
717 assert!(first.reusable_for(&first));
718 assert!(
719 !first.reusable_for(&second),
720 "a new generation must not reload the old conversation"
721 );
722 }
723}