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