Skip to main content

hatchery_node/
lib.rs

1//! Native Gate4Agent node server and runtime.
2
3mod bundle_catalog;
4
5mod context_pack;
6
7mod harness_mcp_proxy;
8
9mod bundle_provider;
10
11mod git_worktree;
12pub mod plugins;
13pub mod provider_inventory;
14pub mod session_signals;
15
16mod host_directory_browser;
17
18mod environment_profiles;
19
20mod session_environment;
21
22mod worktree_service;
23
24pub(crate) use gate4agent_node_core::session_registry;
25
26mod standalone_workspace;
27
28#[cfg(windows)]
29mod workspace_file_windows;
30
31#[cfg(unix)]
32mod workspace_file_unix;
33
34#[cfg(feature = "dig2-station-probe")]
35mod dig2_station_lease;
36#[cfg(feature = "dig2-station-probe")]
37mod dig2_station_probe;
38pub mod kit;
39pub mod local_infra;
40pub mod node_control;
41#[cfg(feature = "wireguard")]
42pub(crate) use gate4agent_node_core::kit_wireguard;
43mod network_allowlist_catalog;
44pub(crate) use gate4agent_node_core::platform;
45pub mod enroll_client;
46pub mod uplink_journal;
47pub mod restream_session;
48pub mod uplink_calls;
49pub mod machine_env;
50pub use machine_env::collect as collect_machine_environment;
51pub mod restream_g4a_bridge;
52#[cfg(feature = "d2b")]
53pub mod browser_capture;
54pub mod grok_leader;
55pub mod m4a_accounts;
56pub mod m4a_adopt;
57pub mod m4a_wake;
58pub mod windows_process;
59pub mod m4a_attach;
60#[cfg(unix)]
61pub mod keystore_proxy;
62/// 0.2.15 name of [`keystore_proxy`]. Unix only, same as the proxy itself.
63#[cfg(unix)]
64pub mod m4a_signer_proxy {
65    pub use super::keystore_proxy::*;
66}
67pub mod door;
68#[cfg(feature = "local-server")]
69pub mod local_server;
70pub mod wake_pipeline;
71#[cfg(unix)]
72pub mod isolation;
73pub use kit::{force_link, linked_core_names};
74#[cfg(feature = "m4a")]
75pub use kit::start_mailbox;
76#[cfg(feature = "m4a")]
77pub use kit::MailboxHandle;
78#[cfg(feature = "wireguard")]
79pub use kit_wireguard::{
80    bring_up_node_wireguard, node_wg_from_env, spawn_linux_wireguard_retry, NodeWgPeerConfig,
81    NodeWgPeerError, WgStatus, LINK_TAG as WIREGUARD_LINK_TAG, WG_ADDRESS_ENV, WG_INTERFACE_ENV,
82    WG_KEEPALIVE_ENV, WG_LISTEN_PORT_ENV, WG_PEER_ADDRESS_ENV, WG_PEER_ENDPOINT_ENV,
83    WG_PEER_KEY_ENV, WG_PRIVATE_KEY_ENV,
84};
85pub use gate4agent_node_core::{node_log, wg_status};
86mod provider_runtime;
87mod server;
88pub(crate) use gate4agent_node_core::spawn_spec;
89
90#[cfg(feature = "fixture")]
91pub use bundle_catalog::protect_bundle_source_tree_fixture;
92pub use bundle_catalog::{
93    BundleCatalog, BundleCatalogError, NodeBundle, NodeBundleError, NodeBundleFile,
94    MAX_BUNDLE_CATALOG_ENTRIES, MAX_BUNDLE_FILES, MAX_BUNDLE_FILE_BYTES, MAX_BUNDLE_PATH_BYTES,
95    MAX_BUNDLE_TOTAL_BYTES,
96};
97pub use environment_profiles::{
98    NodeEnvironmentProfile, NodeEnvironmentProfileError, MAX_NODE_ENVIRONMENT_PROFILES,
99};
100pub use gate4agent_node_protocol::WorktreeServiceMode;
101pub use gate4agent_runtime_native::{
102    orca_home_roots, HistorySourceLayout, NativeHistoryConfig, NativeHistoryRoot,
103};
104pub use network_allowlist_catalog::{
105    claude_allowed_domains_from_permits, claude_bash_sandbox_network_os_supported,
106    claude_settings_network_overlay_args, claude_station_network_settings_json,
107    codex_network_access_config_overlay, load_network_allowlist_catalog_file,
108    parse_network_allowlist_catalog_text, permit_peer_to_allowed_domain,
109    provider_native_mapping_supported, resolve_network_allowlist_catalog,
110    resolve_provider_native_launch_overlay, NetworkAllowlistCatalog, NetworkAllowlistCatalogError,
111    NetworkAllowlistEntry, NetworkPermitProtocol, NetworkPermitSketch, ProviderNativeNetworkSketch,
112    CLAUDE_STATION_NETWORK_SETTINGS_FILE, MAX_NETWORK_ALLOWLIST_CATALOG_ENTRIES,
113    MAX_NETWORK_PERMITS_PER_ENTRY, NETWORK_ALLOWLIST_CATALOG_ENV,
114    NETWORK_ALLOWLIST_CATALOG_SCHEMA_VERSION,
115};
116#[cfg(feature = "fixture")]
117pub use server::SpawnManagedWorktreeV2FailureProbe;
118pub use server::node_wake::{NodeLetter, NodeWake};
119pub use server::{
120    default_node_endpoint, default_state_path, NodeServer, NodeServerConfig, NodeServerError,
121    NodeShutdownHandle, WorkspaceConfig,
122};
123pub use session_environment::{
124    NodeSecretReference, NodeSecretResolveError, NodeSecretResolver, NodeSecretValue,
125    NodeSecretValueError, NodeSessionEnvironmentMutation, NodeSessionFile,
126    NodeSessionMaterializationProfile, NodeSessionMaterializationProfileError,
127    NodeSessionPathBinding, NodeSessionPathClass, MAX_NODE_SECRET_REFERENCE_BYTES,
128    MAX_NODE_SECRET_VALUE_BYTES, MAX_SESSION_ENVIRONMENT_ENTRIES,
129    MAX_SESSION_MATERIALIZATION_FILES, MAX_SESSION_MATERIALIZATION_FILE_BYTES,
130    MAX_SESSION_MATERIALIZATION_RELATIVE_PATH_BYTES,
131};
132pub use spawn_spec::{
133    SpawnProfileRegistry, SpawnProfileRegistryError, DEFAULT_SPAWN_PROFILE_ID, MAX_SPAWN_PROFILES,
134};
135pub use worktree_service::ManagedWorktreeProfile;
136
137#[cfg(windows)]
138pub use server::DEFAULT_NODE_ENDPOINT;
139
140pub use gate4agent_node_protocol as protocol;
141
142#[cfg(test)]
143mod kit_manifest {
144    #[test]
145    fn default_features_are_the_full_kit() {
146        let manifest = include_str!("../Cargo.toml");
147        let features = manifest
148            .split("[features]")
149            .nth(1)
150            .expect("features section")
151            .split("\n[")
152            .next()
153            .expect("features body");
154        assert!(
155            features.contains("default = [\"kit\"]"),
156            "default build must include the kit"
157        );
158        let kit = feature_array(features, "kit");
159        for core in ["m4a", "d2b", "session-restore", "wireguard", "memory"] {
160            assert!(
161                kit.iter().any(|item| item == core),
162                "kit feature missing {core}: {kit:?}"
163            );
164        }
165        assert!(
166            !features.contains("bare = "),
167            "there is no light mode"
168        );
169        let m4a = feature_array(features, "m4a");
170        assert_eq!(m4a, ["dep:mail4agent"]);
171        let d2b = feature_array(features, "d2b");
172        // The browser stream (websocket + frame encoding) rides with dig2browser.
173        assert_eq!(d2b, ["dep:dig2browser", "dep:tokio-tungstenite", "dep:futures-util", "dep:image"]);
174        let wg = feature_array(features, "wireguard");
175        assert_eq!(wg, ["gate4agent-node-core/wireguard"]);
176        let code = manifest
177            .lines()
178            .map(|line| line.split('#').next().unwrap_or(""))
179            .collect::<Vec<_>>()
180            .join("\n");
181        assert!(
182            !code.contains("hatchery-tui") && !code.contains("hatchery_tui"),
183            "node must not depend on the hatchery TUI"
184        );
185        assert!(manifest.contains("dig2browser = { version = \"0.5.3\""));
186        assert!(manifest.contains("mail4agent = { version = \"0.4.17\""));
187        assert!(!manifest.contains("[patch.crates-io]"), "published state has no patch table");
188        assert!(!manifest.contains("../../../"), "no path dependency leaves the workspace");
189        assert!(manifest.contains("claude-session-restore = { version = \"0.1.3\""));
190        assert!(manifest.contains("codex-session-restore = { version = \"0.1.3\""));
191        assert!(manifest.contains("grok-session-restore"));
192        assert!(manifest.contains("kimi-session-restore"));
193    }
194
195    fn feature_array(features: &str, name: &str) -> Vec<String> {
196        let line_start = features
197            .find(&format!("{name} = "))
198            .unwrap_or_else(|| panic!("missing feature {name}"));
199        let rest = &features[line_start + name.len() + 3..];
200        if rest.starts_with("[]") {
201            return Vec::new();
202        }
203        assert!(rest.starts_with("["), "{name} must be an array");
204        let end = rest.find(']').unwrap_or_else(|| panic!("{name} array"));
205        rest[1..end]
206            .split(',')
207            .map(|item| item.trim().trim_matches('"').to_owned())
208            .filter(|item| !item.is_empty())
209            .collect()
210    }
211}
212
213
214#[cfg(test)]
215mod deploy_artifacts {
216    //! Static checks for the three kit deploy modes. No Docker build, no
217    //! QEMU boot, no network. Lives in `--lib` so the ordinary node test
218    //! cycle refuses missing or gutted deploy entry points.
219
220    #[test]
221    fn service_unit_is_mode1_kit_entry() {
222        let unit = include_str!("../deploy/node/gate4agent-node.service");
223        assert!(unit.contains("ExecStart=/usr/local/bin/gate4agent-node"));
224        assert!(unit.contains("gate4agent node (standard kit)"));
225        assert!(
226            !unit.contains("INSTALL_PROVIDER_CLIS"),
227            "mode 1 unit must not install provider CLIs"
228        );
229        assert!(
230            unit.contains("GATE4AGENT_WG_INTERFACE") || unit.contains("WireGuard"),
231            "unit must document optional WG env"
232        );
233    }
234
235    #[test]
236    fn dockerfile_and_build_script_are_mode2() {
237        let dockerfile = include_str!("../deploy/node/Dockerfile");
238        assert!(dockerfile.contains("cargo build --locked --release -p hatchery-node"));
239        assert!(dockerfile.contains("ENTRYPOINT [\"gate4agent-node\"]"));
240        assert!(!dockerfile.contains("COPY mail4agent"));
241        assert!(!dockerfile.contains("COPY gate4agent"), "crates.io only: no sibling checkouts");
242        assert!(
243            !dockerfile.contains("GATE4AGENT_NODE_TOKEN"),
244            "image must not bake a node token"
245        );
246        let build = include_str!("../deploy/node/docker-build.sh");
247        assert!(build.contains("docker build"));
248        assert!(build.contains("gate4agent-node:kit"));
249        assert!(!build.contains("copy_tree \"$WS/mail4agent\""));
250    }
251
252    #[test]
253    fn qemu_guest_script_is_mode3_underlay_only() {
254        let script = include_str!("../deploy/node/qemu-guest.sh");
255        assert!(script.contains("Mode 3"));
256        assert!(script.contains("QEMU_NET"));
257        assert!(script.contains("tap") && script.contains("user"));
258        assert!(
259            script.contains("Kernel WireGuard is not a QEMU network"),
260            "script must keep WG out of the QEMU NIC story"
261        );
262        assert!(script.contains("GATE4AGENT_WG_"));
263        assert!(script.contains("gate4agent-node"));
264        // Guest boot stays opt-in: this test only reads the script text.
265        assert!(
266            !script.contains("cargo test"),
267            "qemu guest script must not be wired into cargo test"
268        );
269    }
270
271    #[test]
272    fn deploy_readme_names_three_modes() {
273        let readme = include_str!("../deploy/node/README.md");
274        assert!(readme.contains("Mode 1 — service"));
275        assert!(readme.contains("Mode 2 — container"));
276        assert!(readme.contains("Mode 3 — QEMU guest"));
277        assert!(readme.contains("no light mode"));
278    }
279}