Skip to main content

hatchery_node/
lib.rs

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