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