pulsehive-runtime 2.0.1

Runtime execution engine for PulseHive multi-agent SDK
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
//! Integration tests for workflow agents — Sequential, Parallel, Loop, and nested compositions.
//!
//! Tests exercise the full HiveMind::deploy() → workflow::dispatch_agent() → agentic loop path.

use std::pin::Pin;
use std::sync::Mutex;
use std::time::Duration;

use async_trait::async_trait;
use futures::StreamExt;
use futures_core::Stream;

use pulsehive_core::agent::{AgentDefinition, AgentKind, AgentOutcome, LlmAgentConfig};
use pulsehive_core::error::{PulseHiveError, Result};
use pulsehive_core::event::HiveEvent;
use pulsehive_core::lens::Lens;
use pulsehive_core::llm::*;
use pulsehive_runtime::hivemind::{HiveMind, Task};

// ── Mock LLM Provider ────────────────────────────────────────────────

struct MockLlm {
    responses: Mutex<Vec<LlmResponse>>,
}

impl MockLlm {
    fn new(responses: Vec<LlmResponse>) -> Self {
        Self {
            responses: Mutex::new(responses),
        }
    }

    fn text(content: &str) -> LlmResponse {
        LlmResponse {
            content: Some(content.into()),
            tool_calls: vec![],
            usage: TokenUsage::default(),
        }
    }
}

#[async_trait]
impl LlmProvider for MockLlm {
    async fn chat(
        &self,
        _messages: Vec<Message>,
        _tools: Vec<ToolDefinition>,
        _config: &LlmConfig,
    ) -> Result<LlmResponse> {
        let mut responses = self.responses.lock().unwrap();
        if responses.is_empty() {
            Err(PulseHiveError::llm("No more scripted responses"))
        } else {
            Ok(responses.remove(0))
        }
    }

    async fn chat_stream(
        &self,
        _messages: Vec<Message>,
        _tools: Vec<ToolDefinition>,
        _config: &LlmConfig,
    ) -> Result<Pin<Box<dyn Stream<Item = Result<LlmChunk>> + Send>>> {
        Err(PulseHiveError::llm("Not used in tests"))
    }
}

// ── Helpers ──────────────────────────────────────────────────────────

fn build_hive(provider: MockLlm) -> HiveMind {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("test.db");
    Box::leak(Box::new(dir));

    HiveMind::builder()
        .substrate_path(&path)
        .llm_provider("mock", provider)
        .build()
        .unwrap()
}

fn llm_agent(name: &str) -> AgentDefinition {
    AgentDefinition {
        name: name.into(),
        kind: AgentKind::Llm(Box::new(LlmAgentConfig {
            system_prompt: "You are a test agent.".into(),
            tools: vec![],
            lens: Lens::default(),
            llm_config: LlmConfig::new("mock", "test-model"),
            experience_extractor: None,
            refresh_every_n_tool_calls: None,
        })),
    }
}

/// Collect events with a timeout — doesn't stop on first AgentCompleted
/// (workflows emit multiple completions). Uses a short settling period
/// after the last event to detect when all agents are done.
async fn collect_workflow_events(
    mut stream: Pin<Box<dyn Stream<Item = HiveEvent> + Send>>,
    timeout: Duration,
) -> Vec<HiveEvent> {
    let mut events = vec![];
    let deadline = tokio::time::Instant::now() + timeout;

    loop {
        tokio::select! {
            event = stream.next() => {
                match event {
                    Some(e) => events.push(e),
                    None => break,
                }
            }
            _ = tokio::time::sleep_until(deadline) => {
                break;
            }
        }
    }

    events
}

/// Count AgentStarted events with a specific kind tag
fn count_started(events: &[HiveEvent], kind: pulsehive_core::agent::AgentKindTag) -> usize {
    events
        .iter()
        .filter(|e| matches!(e, HiveEvent::AgentStarted { kind: k, .. } if *k == kind))
        .count()
}

/// Count AgentCompleted events with Complete outcome
fn count_completed(events: &[HiveEvent]) -> usize {
    events
        .iter()
        .filter(|e| {
            matches!(
                e,
                HiveEvent::AgentCompleted {
                    outcome: AgentOutcome::Complete { .. },
                    ..
                }
            )
        })
        .count()
}

// ── Tests ────────────────────────────────────────────────────────────

#[tokio::test]
async fn test_deploy_sequential_workflow() {
    let provider = MockLlm::new(vec![
        MockLlm::text("Step 1 done"),
        MockLlm::text("Step 2 done"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "pipeline".into(),
        kind: AgentKind::Sequential(vec![llm_agent("step-1"), llm_agent("step-2")]),
    };
    let task = Task::new("Run pipeline");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // Should have AgentStarted events for: top-level spawn, Sequential wrapper, step-1, step-2
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 2,
        "Should have at least 2 LLM AgentStarted events. Events: {events:?}"
    );
    // Both LLM children should complete
    assert!(
        count_completed(&events) >= 2,
        "Should have at least 2 AgentCompleted events. Events: {events:?}"
    );
}

#[tokio::test]
async fn test_deploy_parallel_workflow() {
    let provider = MockLlm::new(vec![
        MockLlm::text("Alpha done"),
        MockLlm::text("Beta done"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "fan-out".into(),
        kind: AgentKind::Parallel(vec![llm_agent("alpha"), llm_agent("beta")]),
    };
    let task = Task::new("Fan out work");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // Both parallel children should start and complete
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 2,
        "Both parallel children should start. Events: {events:?}"
    );
    assert!(
        count_completed(&events) >= 2,
        "Both parallel children should complete. Events: {events:?}"
    );
}

#[tokio::test]
async fn test_deploy_loop_workflow() {
    let provider = MockLlm::new(vec![
        MockLlm::text("Iteration 1"),
        MockLlm::text("Done [LOOP_DONE]"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "refiner".into(),
        kind: AgentKind::Loop {
            agent: Box::new(llm_agent("worker")),
            max_iterations: 5,
        },
    };
    let task = Task::new("Refine until done");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // Should have 2 LLM starts (2 iterations before [LOOP_DONE])
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 2,
        "Loop should run 2 iterations. Events: {events:?}"
    );
}

#[tokio::test]
async fn test_deploy_nested_sequential_parallel() {
    // Sequential([Parallel([A, B]), C])
    // A and B run concurrently, then C runs after both complete
    let provider = MockLlm::new(vec![
        MockLlm::text("A result"),
        MockLlm::text("B result"),
        MockLlm::text("C sees A and B"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "nested".into(),
        kind: AgentKind::Sequential(vec![
            AgentDefinition {
                name: "parallel-phase".into(),
                kind: AgentKind::Parallel(vec![llm_agent("A"), llm_agent("B")]),
            },
            llm_agent("C"),
        ]),
    };
    let task = Task::new("Nested workflow");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // All 3 LLM children should start and complete
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 3,
        "All 3 LLM agents should start. Events: {events:?}"
    );
    assert!(
        count_completed(&events) >= 3,
        "All 3 LLM agents should complete. Events: {events:?}"
    );
}

#[tokio::test]
async fn test_deploy_nested_parallel_sequential() {
    // Parallel([Sequential([A, B]), C])
    // Sequential(A→B) and C run concurrently
    let provider = MockLlm::new(vec![
        MockLlm::text("A done"),
        MockLlm::text("B after A"),
        MockLlm::text("C concurrent"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "par-seq".into(),
        kind: AgentKind::Parallel(vec![
            AgentDefinition {
                name: "seq-branch".into(),
                kind: AgentKind::Sequential(vec![llm_agent("A"), llm_agent("B")]),
            },
            llm_agent("C"),
        ]),
    };
    let task = Task::new("Parallel with sequential branch");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 3,
        "All 3 LLM agents should start. Events: {events:?}"
    );
}

#[tokio::test]
async fn test_deploy_deep_nesting() {
    // Sequential([Parallel([Loop { A, 2 }, B])])
    // Triple nesting: seq > par > loop
    let provider = MockLlm::new(vec![
        MockLlm::text("Loop iter 1"),
        MockLlm::text("Loop iter 2"),
        MockLlm::text("B result"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "deep".into(),
        kind: AgentKind::Sequential(vec![AgentDefinition {
            name: "par-phase".into(),
            kind: AgentKind::Parallel(vec![
                AgentDefinition {
                    name: "loop-branch".into(),
                    kind: AgentKind::Loop {
                        agent: Box::new(llm_agent("looper")),
                        max_iterations: 2,
                    },
                },
                llm_agent("B"),
            ]),
        }]),
    };
    let task = Task::new("Deep nesting test");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // looper runs 2x + B runs 1x = at least 3 LLM starts
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 3,
        "Should have at least 3 LLM starts (2 loop + 1 B). Events: {events:?}"
    );
}

// ── Sprint 6: Watch Integration Tests (#56, #57) ────────────────────

fn llm_agent_with_refresh(name: &str, refresh: Option<usize>) -> AgentDefinition {
    AgentDefinition {
        name: name.into(),
        kind: AgentKind::Llm(Box::new(LlmAgentConfig {
            system_prompt: "You are a test agent.".into(),
            tools: vec![],
            lens: Lens::default(),
            llm_config: LlmConfig::new("mock", "test-model"),
            experience_extractor: None,
            refresh_every_n_tool_calls: refresh,
        })),
    }
}

/// #56: Sequential agents naturally share experiences (B perceives A's at start)
#[tokio::test]
async fn test_sequential_experience_sharing() {
    let provider = MockLlm::new(vec![
        MockLlm::text("Agent A discovered something"),
        MockLlm::text("Agent B building on A's work"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "seq-share".into(),
        kind: AgentKind::Sequential(vec![llm_agent("agent-a"), llm_agent("agent-b")]),
    };
    let task = Task::new("Sequential sharing test");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // Agent A should record an experience, Agent B should perceive the substrate
    let has_experience_recorded = events
        .iter()
        .any(|e| matches!(e, HiveEvent::ExperienceRecorded { .. }));
    assert!(
        has_experience_recorded,
        "Agent A should record an experience. Events: {events:?}"
    );

    // Both agents should have perceived the substrate
    let perceive_count = events
        .iter()
        .filter(|e| matches!(e, HiveEvent::SubstratePerceived { .. }))
        .count();
    assert!(
        perceive_count >= 2,
        "Both agents should perceive substrate. Got {perceive_count}. Events: {events:?}"
    );
}

/// #56: Watch events appear in the event stream
#[tokio::test]
async fn test_watch_events_in_stream() {
    let provider = MockLlm::new(vec![MockLlm::text("Done")]);
    let hive = build_hive(provider);

    let agent = llm_agent("watcher");
    let task = Task::new("Watch test");

    let stream = hive.deploy(vec![agent], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(5)).await;

    // The agent records an experience → Watch should emit WatchNotification
    // Note: Watch delivery is async, so it may or may not arrive within the timeout.
    // We just verify no panics and the event stream works correctly.
    let watch_count = events
        .iter()
        .filter(|e| matches!(e, HiveEvent::WatchNotification { .. }))
        .count();
    // WatchNotification may or may not arrive depending on timing — just log it
    tracing::info!(watch_events = watch_count, "Watch events received");

    // Core assertion: agent completed successfully with Watch subscription active
    assert!(
        count_completed(&events) >= 1,
        "Agent should complete even with Watch subscription active. Events: {events:?}"
    );
}

/// #57: Multiple concurrent agents complete without errors (stress-lite)
#[tokio::test]
async fn test_stress_3_concurrent_agents() {
    let provider = MockLlm::new(vec![
        MockLlm::text("Agent 1 done"),
        MockLlm::text("Agent 2 done"),
        MockLlm::text("Agent 3 done"),
    ]);
    let hive = build_hive(provider);

    let workflow = AgentDefinition {
        name: "stress".into(),
        kind: AgentKind::Parallel(vec![
            llm_agent_with_refresh("worker-1", Some(2)),
            llm_agent_with_refresh("worker-2", Some(2)),
            llm_agent_with_refresh("worker-3", Some(2)),
        ]),
    };
    let task = Task::new("Stress test");

    let stream = hive.deploy(vec![workflow], vec![task]).await.unwrap();
    let events = collect_workflow_events(stream, Duration::from_secs(10)).await;

    // All 3 should start
    assert!(
        count_started(&events, pulsehive_core::agent::AgentKindTag::Llm) >= 3,
        "All 3 agents should start. Events: {events:?}"
    );

    // All 3 should complete
    assert!(
        count_completed(&events) >= 3,
        "All 3 agents should complete. Events: {events:?}"
    );
}