Horizon Settings > AI Agent: turn agent control on, see who is connected, copy the details

Horizon Settings gains a row: "AI Agent..." and "No AI: Off". No AI hides the
AI Agent page and closes the control socket at once.

The AI Agent page has an Agent Control switch (horizon-extras.txt
agentControl, off by default): on, the game the player starts opens the
default control endpoint, so horizon-ctl and MCP clients reach it without
--control. Turning it off closes the endpoint at once; dropping a control
server now also stops its listener thread. The page shows whether an agent
is connected, when it last sent a command and the socket, and Copy
Connection Details copies the socket path, a horizon-ctl line to try and the
MCP config for this game.

agent_control_e2e.py --scenario setting covers it: Agent Control on opens the
endpoint without --control, the pages list their widgets, Copy works, and
turning the switch off removes the endpoint while the game keeps running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-23 03:04:19 +02:00
parent b117930ae4
commit 768f149190
9 changed files with 370 additions and 20 deletions
+52 -2
View File
@@ -13,7 +13,11 @@ use std::{
io::{self, BufRead, BufReader, Read, Write},
net::{Ipv4Addr, TcpListener, TcpStream},
path::{Path, PathBuf},
sync::mpsc,
sync::{
Arc,
atomic::{AtomicBool, AtomicUsize, Ordering},
mpsc,
},
thread,
};
@@ -151,6 +155,12 @@ pub struct Server {
path: PathBuf,
requests: mpsc::Receiver<Request>,
sender: mpsc::Sender<Request>,
/// Clients connected now (an MCP server keeps one open; horizon-ctl one per command).
clients: Arc<AtomicUsize>,
/// Set when the server is dropped: the listener stops accepting and exits.
closed: Arc<AtomicBool>,
/// The TCP port, to wake the listener when closing.
port: Option<u16>,
}
/// `$HORIZON_CONTROL`, else `horizon-control.sock` in the user's runtime
@@ -174,10 +184,15 @@ impl Server {
}
let (sender, requests) = mpsc::channel();
let local = sender.clone();
let clients = Arc::new(AtomicUsize::new(0));
let counted = clients.clone();
let closed = Arc::new(AtomicBool::new(false));
let mut tcp_port = None;
if uses_tcp(path) {
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))?;
let token = random_token();
let port = listener.local_addr()?.port();
tcp_port = Some(port);
write_private(
path,
&serde_json::json!({ "transport": "tcp", "port": port, "token": token })
@@ -186,6 +201,8 @@ impl Server {
spawn_listener(
move || listener.accept().map(|(s, _)| s),
sender,
counted,
closed.clone(),
move |stream: TcpStream| {
let _ = stream.set_nodelay(true);
let mut stream = Stream::Tcp(stream);
@@ -199,6 +216,8 @@ impl Server {
spawn_listener(
move || listener.accept().map(|(s, _)| s),
sender,
counted,
closed.clone(),
|s| Some(Stream::Unix(s)),
)?;
}
@@ -207,6 +226,9 @@ impl Server {
path: path.to_owned(),
requests,
sender: local,
clients,
closed,
port: tcp_port,
})
}
/// Commands from inside the process only (`--benchmark` without `--control`).
@@ -216,8 +238,15 @@ impl Server {
path: PathBuf::new(),
requests,
sender,
clients: Arc::default(),
closed: Arc::default(),
port: None,
}
}
/// How many clients are connected.
pub fn clients(&self) -> usize {
self.clients.load(Ordering::Relaxed)
}
/// Sends commands as a socket client would, from another thread.
pub fn sender(&self) -> mpsc::Sender<Request> {
self.sender.clone()
@@ -234,6 +263,8 @@ impl Server {
fn spawn_listener<S: Send + 'static>(
mut accept_next: impl FnMut() -> io::Result<S> + Send + 'static,
sender: mpsc::Sender<Request>,
clients: Arc<AtomicUsize>,
closed: Arc<AtomicBool>,
accept: impl Fn(S) -> Option<Stream> + Send + Sync + 'static,
) -> io::Result<()> {
let accept = std::sync::Arc::new(accept);
@@ -241,17 +272,24 @@ fn spawn_listener<S: Send + 'static>(
.name("horizon-control".into())
.spawn(move || {
loop {
let Ok(stream) = accept_next() else {
let next = accept_next();
if closed.load(Ordering::Relaxed) {
return;
}
let Ok(stream) = next else {
thread::sleep(std::time::Duration::from_millis(10));
continue;
};
let sender = sender.clone();
let accept = accept.clone();
let clients = clients.clone();
let _ = thread::Builder::new()
.name("horizon-control-client".into())
.spawn(move || {
if let Some(stream) = accept(stream) {
clients.fetch_add(1, Ordering::Relaxed);
serve(stream, &sender);
clients.fetch_sub(1, Ordering::Relaxed);
}
});
}
@@ -287,6 +325,18 @@ fn authenticate(stream: &mut Stream, token: &str) -> bool {
impl Drop for Server {
fn drop(&mut self) {
if self.path.as_os_str().is_empty() {
return;
}
// Wake the listener so it sees `closed` and exits (agent control turned off in-game).
self.closed.store(true, Ordering::Relaxed);
match self.port {
Some(port) => drop(TcpStream::connect((Ipv4Addr::LOCALHOST, port))),
#[cfg(unix)]
None => drop(UnixStream::connect(&self.path)),
#[cfg(not(unix))]
None => {}
}
let _ = std::fs::remove_file(&self.path);
}
}
+23 -2
View File
@@ -109,6 +109,9 @@ pub struct Extras {
pub sort_key: String,
/// No AI: every AI feature hidden and off (`horizon_core::no_ai`; the launcher writes it too).
pub no_ai: bool,
/// Agents may control the game the player starts: it opens its control socket for
/// horizon-ctl and MCP clients (off by default; an agent's own launch always opens it).
pub agent_control: bool,
}
impl Default for Extras {
fn default() -> Self {
@@ -138,6 +141,7 @@ impl Default for Extras {
sort: false,
sort_key: "key.keyboard.unknown".into(),
no_ai: false,
agent_control: false,
}
}
}
@@ -202,6 +206,7 @@ impl Extras {
"sort" => e.sort = flag(e.sort),
"sortKey" if value.starts_with("key.") => e.sort_key = value.to_owned(),
horizon_core::no_ai::KEY => e.no_ai = flag(e.no_ai),
"agentControl" => e.agent_control = flag(e.agent_control),
_ => {}
}
}
@@ -209,7 +214,7 @@ impl Extras {
}
pub fn serialize(&self) -> String {
format!(
"fullbright:{}\ndynamicLights:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n{}:{}\n",
"fullbright:{}\ndynamicLights:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n{}:{}\nagentControl:{}\n",
bool_text(self.fullbright),
bool_text(self.dynamic_lights),
bool_text(self.zoom),
@@ -240,6 +245,7 @@ impl Extras {
self.sort_key,
horizon_core::no_ai::KEY,
bool_text(self.no_ai),
bool_text(self.agent_control),
)
}
/// Binds the extra key `name` (`ZOOM_KEY`, `SORT_KEY`); false for
@@ -457,6 +463,9 @@ pub enum Item {
MouseTweaksWheel,
Sort,
SortKey,
/// Not on the Extras page: Horizon Settings shows No AI, its AI Agent page Agent Control.
NoAi,
AgentControl,
}
/// The Extras page: headers and items.
pub fn rows() -> Vec<Result<Item, &'static str>> {
@@ -535,6 +544,8 @@ impl Item {
Item::FpsLows => "Minimum and 1% low",
Item::Keystrokes => "Keystrokes",
Item::KeystrokesCps => "Clicks per second",
Item::NoAi => "No AI",
Item::AgentControl => "Agent Control",
}
}
/// The switch this setting belongs to: it does nothing while that is off.
@@ -548,7 +559,7 @@ impl Item {
MouseTweaksWheel => MouseTweaks,
SortKey => Sort,
Fullbright | DynamicLights | Zoom | Minimap | Fps | Keystrokes | MouseTweaks | Sort
| AutoReconnect => {
| AutoReconnect | NoAi | AgentControl => {
return None;
}
})
@@ -592,6 +603,12 @@ impl Item {
}
Item::Keystrokes => "Movement keys, jump and mouse buttons as you press them.",
Item::KeystrokesCps => "Clicks in the last second under each mouse button.",
Item::NoAi => {
"Hides and turns off every AI feature: agent control, horizon-ctl and the MCP servers. The launcher's switch too."
}
Item::AgentControl => {
"Your own AI agent can play this game through horizon-ctl or an MCP client, on this computer only."
}
}
}
/// The frame-time cost the main switch of an extra shows.
@@ -622,6 +639,8 @@ impl Item {
Item::MouseTweaks => e.mouse_tweaks,
Item::MouseTweaksWheel => e.mouse_tweaks_wheel,
Item::Sort => e.sort,
Item::NoAi => e.no_ai,
Item::AgentControl => e.agent_control,
_ => return None,
})
}
@@ -677,6 +696,8 @@ impl Item {
}
Item::MinimapShape => e.minimap_round = !e.minimap_round,
Item::MinimapZoom => e.minimap_zoom = next(&MINIMAP_ZOOMS, e.minimap_zoom, back),
Item::NoAi => e.no_ai = !e.no_ai,
Item::AgentControl => e.agent_control = !e.agent_control,
Item::ZoomKey | Item::SortKey => {}
}
}
+8 -12
View File
@@ -3,6 +3,7 @@
mod advancements;
mod advancements_screen;
mod agent;
mod agent_settings;
mod allays;
mod armor_stands;
mod banners;
@@ -1427,6 +1428,10 @@ struct Game<'a> {
item_columns: Option<machines::ItemColumns>,
creative_screen: Creative,
control: Option<horizon_client::control::Server>,
/// The Agent Control setting the socket was last opened or closed for (`agent_settings`).
agent_control_on: bool,
/// When an agent's last command came.
agent_last: Option<Instant>,
frame_stats: horizon_client::control::FrameStats,
/// This play session's frame and tick times, for opt-in performance statistics.
perf: horizon_client::telemetry::Session,
@@ -11645,6 +11650,8 @@ impl<'a> Game<'a> {
item_columns: None,
creative_screen: prepared.creative_screen,
control: None,
agent_control_on: false,
agent_last: None,
frame_stats: Default::default(),
perf: Default::default(),
pending_screenshot: None,
@@ -12439,18 +12446,7 @@ fn start_game<'a>(
game.mods.dev = game.options.dev_mods.clone();
game.start_mods();
}
if game.options.control.is_some() && game.settings.extras.no_ai {
// No AI turns agent control off: the socket is never opened.
log::warn!(
"Horizon: control socket not opened: {}",
horizon_core::no_ai::REFUSAL
);
} else if let Some(path) = &game.options.control {
let server = horizon_client::control::Server::start(path)
.map_err(|e| format!("control socket {}: {e}", path.display()))?;
log::info!("Horizon: control socket {}", server.path().display());
game.control = Some(server);
}
game.start_agent_control()?;
if let Some(scenario) = game.options.benchmark.clone() {
let control = game
.control
@@ -0,0 +1,140 @@
//! Agent control as a setting (Horizon Settings > AI Agent): when the control socket is open, who
//! is connected, and the details an agent needs. The socket opens when the game was started for
//! an agent (`--control`, `--control-socket`: horizon-ctl launch, the launcher's MCP `play`) or
//! when the player turned Agent Control on, and never under No AI (`horizon_core::no_ai`).
//! Turning either switch in the game opens or closes it at once.
use super::Game;
use horizon_client::{Result, control};
use std::path::PathBuf;
impl Game<'_> {
/// The endpoint: the one the command line gave, else the default horizon-ctl looks for.
fn agent_socket_path(&self) -> PathBuf {
self.options
.control
.clone()
.unwrap_or_else(control::default_path)
}
/// At startup: open the socket if the game was started for an agent or Agent Control is on.
pub(super) fn start_agent_control(&mut self) -> Result<()> {
self.agent_control_on = self.settings.extras.agent_control;
if self.options.benchmark.is_some() && self.options.control.is_none() {
return Ok(());
}
if self.settings.extras.no_ai {
if self.options.control.is_some() {
log::warn!(
"Horizon: control socket not opened: {}",
horizon_core::no_ai::REFUSAL
);
}
return Ok(());
}
if let Some(path) = &self.options.control {
// Asked for on the command line: failing to listen is an error.
let server = control::Server::start(path)
.map_err(|e| format!("control socket {}: {e}", path.display()))?;
log::info!("Horizon: control socket {}", server.path().display());
self.control = Some(server);
} else if self.agent_control_on {
self.open_agent_socket();
}
Ok(())
}
/// The setting's socket: a failure (another game holds the default endpoint) is logged and
/// shown on the AI Agent page, not fatal.
fn open_agent_socket(&mut self) {
let path = self.agent_socket_path();
match control::Server::start(&path) {
Ok(server) => {
log::info!("Horizon: control socket {}", server.path().display());
self.control = Some(server);
}
Err(e) => log::warn!("Horizon: control socket {}: {e}", path.display()),
}
}
/// After a settings change: No AI closes the socket; Agent Control turned on or off opens or
/// closes it.
pub(super) fn sync_agent_control(&mut self) {
let wanted = self.settings.extras.agent_control;
let changed = wanted != self.agent_control_on;
self.agent_control_on = wanted;
let open = self
.control
.as_ref()
.is_some_and(|c| !c.path().as_os_str().is_empty());
if self.settings.extras.no_ai || (changed && !wanted) {
if open {
log::info!("Horizon: control socket closed");
self.control = None;
}
} else if changed && wanted && self.control.is_none() {
self.open_agent_socket();
}
}
/// An agent's command arrived.
pub(super) fn agent_command_seen(&mut self) {
self.agent_last = Some(std::time::Instant::now());
}
/// The AI Agent page's lines: the state, then where agents connect.
pub(super) fn agent_status_lines(&self) -> Vec<String> {
let server = self
.control
.as_ref()
.filter(|c| !c.path().as_os_str().is_empty());
let mut lines = vec![match server {
Some(s) if s.clients() > 0 => {
let ago = self
.agent_last
.map(|t| format!(", last command {} s ago", t.elapsed().as_secs()))
.unwrap_or_default();
format!(
"Connected: {} agent connection{}{ago}",
s.clients(),
if s.clients() == 1 { "" } else { "s" }
)
}
Some(_) => "Waiting for an agent.".into(),
None => "Off: agents can't reach this game.".into(),
}];
if let Some(s) = server {
if self.options.control.is_some() && !self.agent_control_on {
lines.push("Opened for this session by the program that started the game.".into());
}
// The full path is too wide for the page: Copy Connection Details has it.
let name = s.path().file_name().unwrap_or_default().to_string_lossy();
lines.push(format!(
"Socket: {name} (copy the details for its full path)"
));
}
lines.push("Local only: agents on this computer, never the network.".into());
lines
}
/// What Copy Connection Details puts on the clipboard: the socket, a horizon-ctl line to try,
/// and an MCP client's config for this game.
pub(super) fn agent_details(&self) -> String {
let socket = self.agent_socket_path();
let ctl = std::env::current_exe()
.unwrap_or_default()
.with_file_name(if cfg!(windows) {
"horizon-ctl.exe"
} else {
"horizon-ctl"
});
let config = serde_json::json!({ "mcpServers": { "horizon-game": {
"command": ctl, "args": ["--socket", socket, "mcp"] } } });
format!(
"Horizon agent control (this computer only)\nSocket: {}\nTry it: \"{}\" --socket \"{}\" state\nMCP config:\n{}\n",
socket.display(),
ctl.display(),
socket.display(),
serde_json::to_string_pretty(&config).unwrap_or_default()
)
}
}
@@ -117,6 +117,7 @@ impl Game<'_> {
self.answer(request, event_loop);
}
while let Some(request) = self.control.as_ref().and_then(|c| c.try_recv()) {
self.agent_command_seen();
self.answer(request, event_loop);
if self.pending_save.is_some() {
break;
@@ -125,6 +126,11 @@ impl Game<'_> {
if self.control.is_some() {
self.watch_events();
}
if self.screen == Screen::Options
&& self.ui.options.page() == horizon_client::options_ui::Page::Agent
{
self.ui.options.agent_status = self.agent_status_lines();
}
}
fn answer(&mut self, request: horizon_client::control::Request, event_loop: &ActiveEventLoop) {
@@ -565,6 +571,7 @@ impl Game<'_> {
"graphics-advanced" => Page::GraphicsAdvanced,
"extras" => Page::Extras,
"horizon" => Page::Horizon,
"agent" => Page::Agent,
"shader-packs" => Page::ShaderPacks,
other => return Err(format!("unknown options page {other}").into()),
};
@@ -200,6 +200,13 @@ impl Game<'_> {
self.settings.save(&Self::options_path());
self.apply_settings();
self.sync_mods();
self.sync_agent_control();
self.ui.options.agent_status = self.agent_status_lines();
}
Event::CopyAgentDetails => {
use horizon_client::clipboard::Clipboard;
let text = self.agent_details();
self.clipboard.set(&text);
}
Event::Close if self.ui.options.resource_error.is_some() => {}
Event::Close => {
@@ -370,7 +370,8 @@ impl Loading {
let _ = std::fs::create_dir_all(&dir);
packs::open_folder(&dir);
}
Event::ButtonClick => {}
// The startup menu runs before the game opens a control socket.
Event::ButtonClick | Event::CopyAgentDetails => {}
}
}
}
+64
View File
@@ -874,6 +874,8 @@ pub enum Page {
/// Horizon Settings: every page Horizon adds, as a mod's settings
/// screen hangs off vanilla's Options.
Horizon,
/// AI Agent: agent control on or off, whether an agent is connected, the connection details.
Agent,
}
#[derive(Clone, Debug, PartialEq)]
enum Act {
@@ -894,6 +896,7 @@ enum Act {
/// Pick shader pack `i` of `OptionsUi::shader_packs` (again: off).
ToggleShaderPack(usize),
OpenShaderPackFolder,
CopyAgentDetails,
}
enum WKind {
Button {
@@ -943,6 +946,8 @@ pub enum Event {
OpenModFolder,
/// Create `shaderpacks/` if needed and open it.
OpenShaderPackFolder,
/// Copy how an agent connects (the AI Agent page).
CopyAgentDetails,
}
/// `SliderableValueSet.applyValueImmediately`: these apply 600 ms after
/// the last change (`OptionInstanceSliderButton.delayedApplyAt`).
@@ -998,6 +1003,8 @@ pub struct OptionsUi {
pub shader_packs: crate::shader_pack::Folder,
/// The pack that failed to load last and why (set by the game).
pub shader_pack_error: Option<(String, String)>,
/// The AI Agent page's status lines (set by the game while it shows).
pub agent_status: Vec<String>,
}
impl Default for OptionsUi {
fn default() -> Self {
@@ -1028,6 +1035,7 @@ impl Default for OptionsUi {
graphics: Default::default(),
shader_packs: Default::default(),
shader_pack_error: None,
agent_status: Vec::new(),
}
}
}
@@ -1086,6 +1094,7 @@ fn horizon_page_tooltip(page: Page) -> Option<&'static str> {
Page::Extras => {
"Optional additions, all off by default: the game stays vanilla until you turn one on."
}
Page::Agent => "Let your own AI agent play: horizon-ctl and MCP clients on this computer.",
Page::Mods => {
"Mods made for Horizon, from the horizon-mods folder. Each runs in a sandbox and stays off until you turn it on."
}
@@ -1107,6 +1116,11 @@ fn pairs(opts: &[Opt]) -> Vec<Row> {
.collect()
}
impl OptionsUi {
/// A switch of `horizon-extras.txt` as the Extras page draws it: "Caption: On".
fn extra_button(&self, s: &Settings, item: crate::extras::Item) -> Widget {
let label = format!("{}: {}", item.caption(), item.value(&s.extras));
button(0.0, 0.0, 150.0, Text::literal(label), Act::Extra(item))
}
fn settings_search_active(&self) -> bool {
self.settings_search.focused
&& matches!(self.page(), Page::Video | Page::Controls | Page::KeyBinds)
@@ -1200,6 +1214,7 @@ impl OptionsUi {
Page::PadBinds => "Controller Binds",
Page::Extras => "Horizon Extras",
Page::Horizon => "Horizon Settings",
Page::Agent => "AI Agent",
}
}
fn scrollbar_x(&self, width: f32) -> f32 {
@@ -1316,8 +1331,31 @@ impl OptionsUi {
open("Extras...", Page::Extras),
Some(open("Mods...", Page::Mods)),
),
// No AI hides the AI Agent page and everything AI.
if s.extras.no_ai {
Row::Buttons(self.extra_button(s, crate::extras::Item::NoAi), None)
} else {
Row::Buttons(
open("AI Agent...", Page::Agent),
Some(self.extra_button(s, crate::extras::Item::NoAi)),
)
},
]
}
Page::Agent => {
let mut rows = vec![Row::Buttons(
self.extra_button(s, crate::extras::Item::AgentControl),
Some(button(
0.0,
0.0,
150.0,
Text::literal("Copy Connection Details"),
Act::CopyAgentDetails,
)),
)];
rows.extend(self.agent_status.iter().cloned().map(Row::Note));
rows
}
Page::Video => {
// Horizon's graphics screens, Simple and Advanced.
let mut rows = vec![Row::Buttons(
@@ -2499,6 +2537,7 @@ impl OptionsUi {
events.push(Event::Changed);
}
Act::OpenShaderPackFolder => events.push(Event::OpenShaderPackFolder),
Act::CopyAgentDetails => events.push(Event::CopyAgentDetails),
Act::None => {}
}
}
@@ -4146,8 +4185,33 @@ mod tests {
Page::Controller,
Page::Extras,
Page::Mods,
Page::Agent,
]
);
// No AI hides the AI Agent page.
let mut no_ai = Settings::default();
no_ai.extras.no_ai = true;
let (widgets, _, _, _) = ui.layout(&no_ai, 427.0, 240.0);
assert!(!widgets.iter().any(|w| matches!(
&w.kind,
WKind::Button {
act: Act::Push(Page::Agent),
..
}
)));
// The AI Agent page: the switch, Copy and the status the game sets.
ui.push(Page::Agent);
ui.agent_status = vec!["Waiting for an agent.".into()];
let (widgets, _, _, _) = ui.layout(&s, 427.0, 240.0);
let acts: Vec<&Act> = widgets
.iter()
.filter_map(|w| match &w.kind {
WKind::Button { act, .. } => Some(act),
_ => None,
})
.collect();
assert!(acts.contains(&&Act::Extra(crate::extras::Item::AgentControl)));
assert!(acts.contains(&&Act::CopyAgentDetails));
// Vanilla's own pages keep the Horizon pages they had.
ui.stack = vec![Page::Main, Page::Video];
let video = pushes(&mut ui);
+67 -3
View File
@@ -54,7 +54,7 @@ POSITIONAL = {
'click_button': ['label'], 'type': ['text'], 'wait': ['ticks'], 'wait_ready': ['seconds'],
'command': ['command'], 'look_at_block': ['x', 'y', 'z'], 'click': ['id'], 'set': ['id', 'value'],
'give': ['item', 'count'], 'options_get': ['keys'], 'options_set': ['key', 'value'],
'wait_for': ['condition', 'value'],
'wait_for': ['condition', 'value'], 'options': ['page'],
}
# Booleans the game takes as bare words.
BARE = {'click_slot': ['right', 'shift'], 'click': ['right', 'middle', 'shift']}
@@ -635,9 +635,73 @@ def no_ai_check(mode, ctl, resources):
shutil.rmtree(work, ignore_errors=True)
def setting_check(mode, ctl, out, resources):
"""Agent Control turned on in Horizon Settings > AI Agent: the game the player starts (no
--control) opens the default endpoint, the page shows the agent connected, Copy Connection
Details copies the socket, and turning the switch off closes the endpoint at once."""
work, env = scratch(mode, resources)
socket = work / 'c.sock'
env['HORIZON_CONTROL'] = str(socket) # the default endpoint, this home's
(work / 'horizon-extras.txt').write_text('agentControl:true\n')
args = [str(ctl.with_name('horizon-client'))]
if sys.platform == 'darwin':
args.append('--offscreen')
game = subprocess.Popen(args, cwd=work, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
d = None
try:
deadline = time.time() + 300
while not socket.exists():
assert game.poll() is None, f'the game exited ({game.returncode})'
assert time.time() < deadline, 'Agent Control on, but no control socket'
time.sleep(0.5)
d = (Cli if mode == 'cli' else Mcp)(ctl, socket, work, env)
assert d('state')['screen'] == 'title' # the player's own game, at its title screen
d('options', page='horizon')
tree = widgets(d)
assert {'ai-agent', 'no-ai'} <= set(tree), sorted(tree)
d('screenshot', path=str(out / f'{mode}-horizon-settings.png'))
d('options', page='agent')
tree = widgets(d)
switch = next(w for w in tree.values() if w.get('label', '').startswith('Agent Control'))
assert switch['label'] == 'Agent Control: On', switch
d('screenshot', path=str(out / f'{mode}-ai-agent.png'))
copy = next(w for w in tree.values() if w.get('label') == 'Copy Connection Details')
d('click', id=copy['id'])
# Off: the endpoint goes away and the next command can't reach the game.
try:
d('click', id=switch['id'])
except Failed:
pass # the reply may be lost with the socket
deadline = time.time() + 30
while socket.exists():
assert time.time() < deadline, 'Agent Control off, but the socket stayed'
time.sleep(0.5)
assert 'agentControl:false' in (work / 'horizon-extras.txt').read_text()
assert game.poll() is None, 'the game exited when Agent Control went off'
print(f'[{mode}] PASS: setting')
return True
except (Failed, AssertionError, StopIteration, json.JSONDecodeError, subprocess.TimeoutExpired) as e:
print(f'[{mode}] FAIL: setting: {e!r}')
return False
finally:
if d is not None:
try:
d.close()
except Exception:
pass
game.terminate()
try:
game.wait(30)
except subprocess.TimeoutExpired:
game.kill()
shutil.rmtree(work, ignore_errors=True)
def run(mode, ctl, out, resources, which='survival'):
if which == 'no-ai':
return no_ai_check(mode, ctl, resources)
if which == 'setting':
return setting_check(mode, ctl, out, resources)
work, env = scratch(mode, resources)
socket = work / 'c.sock'
driver = (Cli if mode == 'cli' else Mcp)(ctl, socket, work, env)
@@ -666,7 +730,7 @@ def run(mode, ctl, out, resources, which='survival'):
def main():
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('--mode', choices=['cli', 'mcp', 'both'], default='both')
parser.add_argument('--scenario', choices=[*SCENARIOS, 'no-ai', 'all'], default='all')
parser.add_argument('--scenario', choices=[*SCENARIOS, 'no-ai', 'setting', 'all'], default='all')
parser.add_argument('--bin-dir', help='prebuilt horizon-client and horizon-ctl (default: build with the fast profile)')
parser.add_argument('--out', default=str(ROOT / '.local/agent-control-e2e'), help='screenshots go here')
args = parser.parse_args()
@@ -684,7 +748,7 @@ def main():
out = Path(args.out)
out.mkdir(parents=True, exist_ok=True)
modes = ['cli', 'mcp'] if args.mode == 'both' else [args.mode]
scenarios = [*SCENARIOS, 'no-ai'] if args.scenario == 'all' else [args.scenario]
scenarios = [*SCENARIOS, 'no-ai', 'setting'] if args.scenario == 'all' else [args.scenario]
results = {f'{m}/{s}': run(m, bins / 'horizon-ctl', out, resources, s) for m in modes for s in scenarios}
print(json.dumps(results))
sys.exit(0 if all(results.values()) else 1)