hatchery-node 0.2.4

Hatchery node: the complete node kit over the gate4agent transport (PTY/inline sessions, file browser, local git, worktrees, mailbox, browser station, session restore)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
//! A2: dials core operator control (`connect_core_operator`), not C2.
//! Uses restream g4a bridge + core NodeOperatorMesh (A2); C2 is opaque restreamer only.

#![cfg(windows)]

use hatchery_c2::protocol::{
    C2ManagedSessionRecord, C2NodeEvent, C2NodeResponse, C2NodeSnapshot, C2SessionStatus,
    NodeId, NodeRoute, NodeTransportState, RoutedNodeEvent,
    C2_CHILD_ENVIRONMENT_PROFILE_CAPABILITY, C2_TERMINAL_FRAME_EVENTS_CAPABILITY,
};
use hatchery_c2_client::{connect_core_operator, C2ControlHandle};
use gate4agent_catalog::EnvMutation;
use hatchery_node::protocol::{
    CapabilityId, ManagedSessionState, NodeRequest, SessionAddress, SessionMode,
    SpawnDeadlineMs, SpawnEnvironmentProfileId, SpawnEnvironmentProfileRevision,
    SpawnFieldProvenance, SpawnIdempotencyKey, SpawnOverride, SpawnOverrides,
    SpawnProfileDefaults, SpawnProfileId, SpawnProfileRevision, SpawnRequiredCapabilities,
    SpawnSpec, SpawnTarget, WorkspaceId, SPAWN_RUNTIME_RAW_PTY_LIFECYCLE,
};
use hatchery_node::{
    NodeEnvironmentProfile, NodeServer, NodeServerConfig, SpawnProfileRegistry, WorkspaceConfig,
};
use gate4agent_runtime_native::{
    NativeChildEnvironmentResolveError, NativeChildEnvironmentResolver, NativeLaunchProfile,
    NativeLaunchProfileId,
};
use gate4agent_types::{AgentId, TerminalControl, TerminalFrame, TerminalSize, TransportKind};
use serde_json::json;
use std::ffi::{OsStr, OsString};
use std::path::Path;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use tokio::sync::watch;
use tokio::time::{sleep, timeout};

const SENTINEL_KEY: &str = "GATE4AGENT_TEST_PROFILE_SENTINEL";
const SENTINEL_VALUE: &str = "profile-child-only-7c39a1";

struct SentinelResolver;

impl NativeChildEnvironmentResolver for SentinelResolver {
    fn resolve_child_environment(
        &self,
    ) -> Result<Vec<EnvMutation>, NativeChildEnvironmentResolveError> {
        Ok(vec![EnvMutation {
            key: OsString::from(SENTINEL_KEY),
            value: Some(OsString::from(SENTINEL_VALUE)),
        }])
    }
}

fn require_headless_windows_fixture() {
    assert_eq!(
        std::env::var_os("GATE4AGENT_HEADLESS_SUPERVISOR").as_deref(),
        Some(OsStr::new("1")),
        "Windows PTY tests must run through windows-headless-supervisor",
    );
    unsafe {
        const SEM_FAILCRITICALERRORS: u32 = 0x0001;
        const SEM_NOGPFAULTERRORBOX: u32 = 0x0002;
        const SEM_NOOPENFILEERRORBOX: u32 = 0x8000;
        const WER_FAULT_REPORTING_NO_UI: u32 = 0x0020;

        #[link(name = "kernel32")]
        extern "system" {
            fn SetErrorMode(mode: u32) -> u32;
            fn WerSetFlags(flags: u32) -> i32;
        }

        SetErrorMode(
            SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX,
        );
        let _ = WerSetFlags(WER_FAULT_REPORTING_NO_UI);
    }
}

fn endpoint(label: &str) -> String {
    static NEXT: AtomicU64 = AtomicU64::new(1);
    let nonce = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_nanos();
    format!(
        r"\\.\pipe\gate4agent-environment-profile-{label}-{}-{nonce}-{}",
        std::process::id(),
        NEXT.fetch_add(1, Ordering::Relaxed),
    )
}

fn agent(value: &str) -> AgentId {
    AgentId::new(value).unwrap()
}

fn exact_cmd_launcher() -> String {
    let configured = std::env::var_os("ComSpec").expect("Windows ComSpec is unavailable");
    std::fs::canonicalize(Path::new(&configured))
        .expect("Windows command processor is unavailable")
        .into_os_string()
        .into_string()
        .expect("Windows command processor path is not Unicode")
}


async fn snapshot(
    control: &C2ControlHandle,
    route: &NodeRoute,
    context: &str,
) -> C2NodeSnapshot {
    let response = control
        .request(route.clone(), NodeRequest::Snapshot)
        .await
        .unwrap_or_else(|error| panic!("C2 snapshot route failed during {context}: {error:?}"));
    match response.response {
        Ok(C2NodeResponse::Snapshot { snapshot, .. }) => snapshot,
        response => panic!("C2 snapshot returned an unexpected response: {response:?}"),
    }
}

fn session_count(snapshot: &C2NodeSnapshot) -> usize {
    snapshot
        .workspaces
        .iter()
        .map(|workspace| workspace.sessions.len())
        .sum()
}

fn assert_raw_inventory_record(
    record: &C2ManagedSessionRecord,
    session: Option<&SessionAddress>,
    workspace_id: &WorkspaceId,
    expected_state: ManagedSessionState,
) {
    assert_eq!(record.provider, agent("claude"));
    assert_eq!(record.mode, SessionMode::Pty);
    assert_eq!(record.state, expected_state);
    assert_eq!(&record.workspace_id, workspace_id);
    assert_eq!(record.active_session.as_ref(), session);
    assert!(record.environment_profile.is_none());
    assert!(record.bundle.is_none());
    assert!(record.context_id.is_none());
    assert!(record.context.is_none());
    assert!(!record.provider_identity_present);
    assert!(record.created_at_unix_ms > 0);
    assert!(record.updated_at_unix_ms >= record.created_at_unix_ms);
}

async fn wait_running(
    control: &C2ControlHandle,
    route: &NodeRoute,
    address: &SessionAddress,
) {
    timeout(Duration::from_secs(15), async {
        loop {
            let current = snapshot(control, route, "running wait").await;
            let session = current
                .workspaces
                .iter()
                .flat_map(|workspace| &workspace.sessions)
                .find(|session| {
                    session.instance_id == address.session.instance_id
                        && session.generation == address.session.generation
                })
                .expect("spawned environment-profile session is missing from the C2 snapshot");
            assert_eq!(session.transport, TransportKind::Pty);
            if session.status == C2SessionStatus::Running {
                return;
            }
            assert!(
                !matches!(
                    session.status,
                    C2SessionStatus::Exited { .. } | C2SessionStatus::Failed
                ),
                "cmd.exe raw PTY exited before becoming healthy",
            );
            sleep(Duration::from_millis(20)).await;
        }
    })
    .await
    .expect("cmd.exe raw PTY did not become healthy through C2");
}

async fn wait_stopped(
    control: &C2ControlHandle,
    route: &NodeRoute,
    address: &SessionAddress,
) {
    timeout(Duration::from_secs(10), async {
        loop {
            let current = snapshot(control, route, "stopped wait").await;
            let session = current
                .workspaces
                .iter()
                .flat_map(|workspace| &workspace.sessions)
                .find(|session| {
                    session.instance_id == address.session.instance_id
                        && session.generation == address.session.generation
                })
                .expect("stopped environment-profile session disappeared before Remove");
            if matches!(
                session.status,
                C2SessionStatus::Exited { .. } | C2SessionStatus::Failed
            ) {
                return;
            }
            sleep(Duration::from_millis(20)).await;
        }
    })
    .await
    .expect("cmd.exe raw PTY did not stop through C2");
}

async fn wait_sentinel_terminal_frame(
    events: &mut watch::Receiver<Option<RoutedNodeEvent>>,
    route: &NodeRoute,
    address: &SessionAddress,
) -> TerminalFrame {
    let deadline = tokio::time::Instant::now() + Duration::from_secs(15);
    loop {
        let observed = events.borrow_and_update().clone();
        match observed {
            Some(RoutedNodeEvent {
                node_id,
                cursor,
                event:
                    C2NodeEvent::TerminalFrame {
                        address: event_address,
                        frame,
                    },
            }) if node_id == route.node_id
                && cursor.incarnation_id == route.expected_incarnation_id
                && event_address == *address
                && frame.contents.contains(SENTINEL_VALUE) => return frame,
            Some(RoutedNodeEvent {
                node_id,
                cursor,
                event: C2NodeEvent::ResyncRequired { .. },
            }) if node_id == route.node_id
                && cursor.incarnation_id == route.expected_incarnation_id =>
            {
                panic!("C2 terminal stream required resync before sentinel proof")
            }
            _ => {}
        }
        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
        if remaining.is_zero() || timeout(remaining, events.changed()).await.is_err() {
            panic!("profile sentinel did not reach a routed C2 TerminalFrame");
        }
        if events.has_changed().is_err() {
            panic!("authenticated C2 event stream closed before terminal proof");
        }
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn windows_fixture_spawn_spec_environment_profile_runs_exact_pty_end_to_end() {
    require_headless_windows_fixture();
    let node_endpoint = endpoint("node");
    let control_endpoint = endpoint("control");
    let node_id = NodeId::new("environment-profile-fixture-node").unwrap();
    let workspace_id = WorkspaceId::new("primary").unwrap();
    let node_token = "environment-profile-fixture-node-token";
    let c2_token = "environment-profile-fixture-c2-token";
    let spawn_profile_id = SpawnProfileId::new("environment-profile").unwrap();
    let spawn_profile_revision =
        SpawnProfileRevision::new("environment-profile-r1").unwrap();
    let environment_profile_id =
        SpawnEnvironmentProfileId::new("sentinel-environment").unwrap();
    let environment_profile_revision =
        SpawnEnvironmentProfileRevision::new("sentinel-environment-r1").unwrap();
    let terminal_size = TerminalSize {
        rows: 30,
        columns: 120,
    };

    let profiles = SpawnProfileRegistry::new([SpawnProfileDefaults {
        profile_id: spawn_profile_id.clone(),
        revision: spawn_profile_revision.clone(),
        provider: agent("claude"),
        mode: SessionMode::Pty,
        terminal_size,
        prompt: None,
        bundle_id: None,
        context_id: None,
        environment_profile_id: Some(environment_profile_id.clone()),
    }])
    .unwrap();
    let node_config = NodeServerConfig::new(
        &node_endpoint,
        node_token,
        node_id.clone(),
        [WorkspaceConfig::new(
            workspace_id.clone(),
            std::env::current_dir().unwrap(),
        )
        .unwrap()],
    )
    .unwrap()
    .with_spawn_profiles(profiles);
    let mut server = NodeServer::new_exact_launcher_fixture(
        node_config,
        agent("claude"),
        exact_cmd_launcher(),
    )
    .unwrap();
    let native_profile = NativeLaunchProfile::new(
        NativeLaunchProfileId::new("sentinel-environment-pty").unwrap(),
        agent("claude"),
        TransportKind::Pty,
        vec![OsString::from(SENTINEL_KEY)],
        Arc::new(SentinelResolver),
    )
    .unwrap();
    server
        .install_environment_profile(
            NodeEnvironmentProfile::new(
                environment_profile_id.clone(),
                environment_profile_revision.clone(),
                agent("claude"),
                [native_profile],
            )
            .unwrap(),
        )
        .unwrap();
    let node_shutdown = server.shutdown_handle();
    let node_task = tokio::spawn(server.run());


    let _rig = hatchery_node::restream_test_rig::RestreamRig::start(
        hatchery_node::restream_test_rig::RestreamRigConfig {
            node_id: node_id.clone(),
            local_endpoint: node_endpoint.clone(),
            local_token: node_token.to_owned(),
            control_endpoint: control_endpoint.clone(),
            operator_token: c2_token.to_owned(),
        },
    )
    .await;

    let route = _rig.route();
    let (control, mut events) = connect_core_operator(&control_endpoint, c2_token).await.unwrap();
    let negotiated = &control.hello().compatibility.as_ref().unwrap().capabilities;
    assert!(negotiated.iter().any(|capability| {
        capability.as_str() == C2_CHILD_ENVIRONMENT_PROFILE_CAPABILITY
    }));
    assert!(negotiated.iter().any(|capability| {
        capability.as_str() == C2_TERMINAL_FRAME_EVENTS_CAPABILITY
    }));
    let (terminal_events_tx, mut terminal_events) = watch::channel(None::<RoutedNodeEvent>);
    let event_collector = tokio::spawn(async move {
        while let Some(event) = events.recv().await {
            if matches!(
                &event.event,
                C2NodeEvent::TerminalFrame { .. } | C2NodeEvent::ResyncRequired { .. }
            ) {
                terminal_events_tx.send_replace(Some(event));
            }
        }
    });
    let initial = snapshot(&control, &route, "initial health check").await;
    assert_eq!(session_count(&initial), 0);
    assert!(initial.session_records.is_empty());

    let required_capabilities = SpawnRequiredCapabilities::new([CapabilityId::new(
        SPAWN_RUNTIME_RAW_PTY_LIFECYCLE,
    )
    .unwrap()])
    .unwrap();
    let spec = SpawnSpec {
        target: SpawnTarget {
            node_id: node_id.clone(),
            workspace_id: workspace_id.clone(),
            worktree_id: None,
        },
        profile_id: spawn_profile_id,
        expected_profile_revision: spawn_profile_revision,
        overrides: SpawnOverrides {
            provider: SpawnOverride::Inherit,
            mode: SpawnOverride::Inherit,
            terminal_size: SpawnOverride::Inherit,
            prompt: SpawnOverride::Inherit,
            bundle_id: SpawnOverride::Inherit,
            context_id: SpawnOverride::Inherit,
            environment_profile_id: SpawnOverride::Inherit,
            approval_level: None,
            network_allowlist: None,
            browser_profile_id: None,
        },
        deadline_ms: SpawnDeadlineMs::new(20_000).unwrap(),
        idempotency_key: SpawnIdempotencyKey::new("environment-profile-once").unwrap(),
        required_capabilities,
    };
    let first = control
        .request(
            route.clone(),
            NodeRequest::SpawnSpec { spec: spec.clone() },
        )
        .await
        .unwrap();
    let receipt = match first.response {
        Ok(C2NodeResponse::SpawnSpecAccepted { receipt }) => receipt,
        response => panic!("environment-profile SpawnSpec failed: {response:?}"),
    };
    let environment_receipt = receipt
        .environment_profile
        .as_ref()
        .expect("SpawnSpec receipt omitted the selected environment profile");
    assert_eq!(
        serde_json::to_value(environment_receipt).unwrap(),
        json!({
            "profile_id": environment_profile_id,
            "profile_revision": environment_profile_revision,
        }),
    );
    assert!(!serde_json::to_string(&receipt)
        .unwrap()
        .contains(SENTINEL_VALUE));
    assert_eq!(receipt.provider, agent("claude"));
    assert_eq!(receipt.mode, SessionMode::Pty);
    assert_eq!(receipt.terminal_size, terminal_size);
    assert_eq!(
        receipt.provenance.environment_profile_id,
        SpawnFieldProvenance::Profile,
    );
    wait_running(&control, &route, &receipt.session).await;

    assert!(matches!(
        control
            .request(
                route.clone(),
                NodeRequest::Input {
                    session: receipt.session.clone(),
                    text: format!("echo %{SENTINEL_KEY}%"),
                },
            )
            .await
            .unwrap()
            .response,
        Ok(C2NodeResponse::Accepted)
    ));
    assert!(matches!(
        control
            .request(
                route.clone(),
                NodeRequest::TerminalControl {
                    session: receipt.session.clone(),
                    control: TerminalControl::Enter,
                },
            )
            .await
            .unwrap()
            .response,
        Ok(C2NodeResponse::Accepted)
    ));
    let sentinel_frame =
        wait_sentinel_terminal_frame(&mut terminal_events, &route, &receipt.session).await;
    assert!(!sentinel_frame.formatted.is_empty());
    assert!(sentinel_frame.contents.contains(SENTINEL_VALUE));
    let live = snapshot(&control, &route, "raw inventory check").await;
    assert_eq!(live.session_records.len(), 1);
    let live_record = &live.session_records[0];
    assert_raw_inventory_record(
        live_record,
        Some(&receipt.session),
        &workspace_id,
        ManagedSessionState::Live,
    );
    let record_id = live_record.record_id.clone();
    let display_name = live_record.display_name.clone();

    let replay = control
        .request(
            route.clone(),
            NodeRequest::SpawnSpec { spec: spec.clone() },
        )
        .await
        .unwrap();
    match replay.response {
        Ok(C2NodeResponse::SpawnSpecAccepted {
            receipt: replayed_receipt,
        }) => assert_eq!(replayed_receipt, receipt),
        response => panic!("environment-profile idempotent replay failed: {response:?}"),
    }
    let after_replay = snapshot(&control, &route, "idempotent replay check").await;
    assert_eq!(session_count(&after_replay), 1);
    assert_eq!(after_replay.session_records.len(), 1);
    assert_eq!(after_replay.session_records[0].record_id, record_id);
    assert_eq!(after_replay.session_records[0].display_name, display_name);
    assert_raw_inventory_record(
        &after_replay.session_records[0],
        Some(&receipt.session),
        &workspace_id,
        ManagedSessionState::Live,
    );

    assert!(matches!(
        control
            .request(
                route.clone(),
                NodeRequest::Stop {
                    session: receipt.session.clone(),
                    force: true,
                },
            )
            .await
            .unwrap()
            .response,
        Ok(C2NodeResponse::Accepted)
    ));
    wait_stopped(&control, &route, &receipt.session).await;
    let stopped = snapshot(&control, &route, "post-stop inventory check").await;
    assert_eq!(stopped.session_records.len(), 1);
    assert_eq!(stopped.session_records[0].record_id, record_id);
    assert_eq!(stopped.session_records[0].display_name, display_name);
    assert_raw_inventory_record(
        &stopped.session_records[0],
        None,
        &workspace_id,
        ManagedSessionState::Unavailable,
    );
    assert!(matches!(
        control
            .request(
                route.clone(),
                NodeRequest::Remove {
                    session: receipt.session,
                },
            )
            .await
            .unwrap()
            .response,
        Ok(C2NodeResponse::Accepted)
    ));
    let healthy = snapshot(&control, &route, "post-remove health check").await;
    assert_eq!(session_count(&healthy), 0);
    assert_eq!(healthy.session_records.len(), 1);
    assert_eq!(healthy.session_records[0].record_id, record_id);
    assert_eq!(healthy.session_records[0].display_name, display_name);
    assert_raw_inventory_record(
        &healthy.session_records[0],
        None,
        &workspace_id,
        ManagedSessionState::Unavailable,
    );
    assert_eq!(
        http.status().await.unwrap().nodes[&node_id].transport,
        NodeTransportState::Online,
    );

    drop(_rig);
    drop(control);
    timeout(Duration::from_secs(2), event_collector)
        .await
        .expect("C2 event stream did not close")
        .expect("C2 event drain failed");
    node_shutdown.request_shutdown().await.unwrap();
    timeout(Duration::from_secs(10), node_task)
        .await
        .expect("node shutdown timed out")
        .expect("node task panicked")
        .expect("node shutdown failed");
}