holon 0.14.1

A headless, event-driven runtime for long-lived agents
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
use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use holon::provider::{
    AgentProvider, ConversationMessage, ModelBlock, ProviderTurnRequest, ProviderTurnResponse,
};
use serde_json::json;
use tokio::sync::Mutex;

use super::runtime_helpers::{
    compact_request_snapshot, delegated_prompt_text, CompactionRequestSnapshot,
};
use super::runtime_providers::CapturedTurnRequest;

/// Provider that demonstrates max-output recovery flow
pub struct MaxOutputRecoveryProvider {
    calls: Arc<Mutex<usize>>,
}

impl MaxOutputRecoveryProvider {
    pub fn new() -> Self {
        Self {
            calls: Arc::new(Mutex::new(0)),
        }
    }
}

#[async_trait]
impl AgentProvider for MaxOutputRecoveryProvider {
    async fn complete_turn(&self, request: ProviderTurnRequest) -> Result<ProviderTurnResponse> {
        let mut calls = self.calls.lock().await;
        *calls += 1;
        let call_count = *calls;

        // First call: generate extensive text that will hit max_tokens
        if call_count == 1 {
            let extensive_text = "COMPREHENSIVE TECHNICAL REPORT\n\n\
                ## System Architecture Patterns\n\n\
                Modern distributed systems require careful architectural planning. \
                Microservices architecture enables independent scaling and deployment. \
                Event-driven patterns facilitate loose coupling between components.\n\n\
                ## Data Flow Strategies\n\n\
                Data pipelines must handle both batch and streaming workloads efficiently. \
                CQRS patterns separate read and write operations for optimal performance. \
                Event sourcing provides audit trails and enables temporal queries.\n\n\
                ## Security Considerations\n\n\
                Zero-trust security models assume no implicit trust within the network perimeter. \
                End-to-end encryption protects data in transit. \
                Identity and access management must be granular and auditable.\n\n\
                ## Performance Optimization\n\n\
                Caching strategies reduce latency and database load. \
                CDN distribution improves content delivery times. \
                Database indexing and query optimization are essential for scale.\n\n\
                ## Monitoring Approaches\n\n\
                Distributed tracing provides visibility across service boundaries. \
                Metrics collection enables performance analysis and alerting. \
                Log aggregation and analysis support debugging and compliance.";

            return Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: extensive_text.into(),
                }],
                stop_reason: Some("max_tokens".into()),
                input_tokens: 100,
                output_tokens: 1000,
                cache_usage: None,
                request_diagnostics: None,
            });
        }

        // Second call: Sleep-only completion (after max-output recovery)
        if call_count == 2 {
            // Verify that conversation history includes the previous extensive text
            let has_previous_text = request
                .conversation
                .iter()
                .any(|msg| matches!(msg, ConversationMessage::AssistantBlocks(_)));

            assert!(
                has_previous_text,
                "conversation should include previous assistant text"
            );

            return Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::ToolUse {
                    id: "sleep-1".into(),
                    name: "Sleep".into(),
                    input: json!({
                        "reason": "Generated comprehensive technical report covering architecture patterns, data flow strategies, security considerations, performance optimization, and monitoring approaches. All requested sections have been completed."
                    }),
                }],
                stop_reason: None,
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            });
        }

        anyhow::bail!("unexpected provider call: {}", call_count)
    }
}

/// Provider that demonstrates repeated compaction behavior
pub struct RepeatedCompactionProvider {
    calls: Arc<Mutex<usize>>,
    requests: Arc<Mutex<Vec<CompactionRequestSnapshot>>>,
}

impl RepeatedCompactionProvider {
    pub fn new() -> Self {
        Self {
            calls: Arc::new(Mutex::new(0)),
            requests: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub async fn captured_requests(&self) -> Vec<CompactionRequestSnapshot> {
        self.requests.lock().await.clone()
    }
}

#[async_trait]
impl AgentProvider for RepeatedCompactionProvider {
    async fn complete_turn(&self, request: ProviderTurnRequest) -> Result<ProviderTurnResponse> {
        let mut calls = self.calls.lock().await;
        *calls += 1;
        let call_count = *calls;

        let snapshot = compact_request_snapshot(call_count, &request);
        self.requests.lock().await.push(snapshot);

        match call_count {
            1 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: "Round 1: initial reconnaissance. ".repeat(140),
                    },
                    ModelBlock::ToolUse {
                        id: "probe-round-1-exec".into(),
                        name: "ExecCommand".into(),
                        input: json!({
                            "cmd": "printf 'round-1-output'",
                            "max_output_tokens": 12,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            }),
            2 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: "Round 2: follow-up probing continues. ".repeat(140),
                    },
                    ModelBlock::ToolUse {
                        id: "probe-round-2-exec".into(),
                        name: "ExecCommand".into(),
                        input: json!({
                            "cmd": "printf 'round-2-output'",
                            "max_output_tokens": 12,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            }),
            3 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: "Round 3: checkpoint anchor remains unchanged. ".repeat(140),
                    },
                    ModelBlock::ToolUse {
                        id: "probe-round-3-exec".into(),
                        name: "ExecCommand".into(),
                        input: json!({
                            "cmd": "printf 'round-3-output'",
                            "max_output_tokens": 12,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            }),
            4 => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "Round 4: checkpoint-ready continuation with deterministic next step. "
                        .repeat(140),
                }],
                stop_reason: None,
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            }),
            _ => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "Checkpointed review complete; carry the current discovery forward."
                        .into(),
                }],
                stop_reason: None,
                input_tokens: 100,
                output_tokens: 50,
                cache_usage: None,
                request_diagnostics: None,
            }),
        }
    }
}

/// Provider that demonstrates max-output followed by compaction
pub struct MaxOutputThenCompactionProvider {
    calls: Arc<Mutex<usize>>,
    requests: Arc<Mutex<Vec<CompactionRequestSnapshot>>>,
}

impl MaxOutputThenCompactionProvider {
    pub fn new() -> Self {
        Self {
            calls: Arc::new(Mutex::new(0)),
            requests: Arc::new(Mutex::new(Vec::new())),
        }
    }

    pub async fn captured_requests(&self) -> Vec<CompactionRequestSnapshot> {
        self.requests.lock().await.clone()
    }
}

#[async_trait]
impl AgentProvider for MaxOutputThenCompactionProvider {
    async fn complete_turn(&self, request: ProviderTurnRequest) -> Result<ProviderTurnResponse> {
        let mut calls = self.calls.lock().await;
        *calls += 1;
        let call_count = *calls;

        let snapshot = compact_request_snapshot(call_count, &request);
        self.requests.lock().await.push(snapshot);

        match call_count {
            1 => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "[analysis] ".repeat(260),
                }],
                stop_reason: Some("max_tokens".into()),
                input_tokens: 100,
                output_tokens: 1500,
                cache_usage: None,
                request_diagnostics: None,
            }),
            2 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text:
                            "Round 2: recovery continuation introduces structured output evidence. "
                                .repeat(70),
                    },
                    ModelBlock::ToolUse {
                        id: "recovery-round-2-exec".into(),
                        name: "ExecCommand".into(),
                        input: json!({
                            "cmd": "printf 'recovery-round-2-output'",
                            "max_output_tokens": 16,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 100,
                output_tokens: 60,
                cache_usage: None,
                request_diagnostics: None,
            }),
            3 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: "Round 3: continued verification without dropping context. "
                            .repeat(70),
                    },
                    ModelBlock::ToolUse {
                        id: "recovery-round-3-exec".into(),
                        name: "ExecCommand".into(),
                        input: json!({
                            "cmd": "printf 'recovery-round-3-output'",
                            "max_output_tokens": 16,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 100,
                output_tokens: 60,
                cache_usage: None,
                request_diagnostics: None,
            }),
            4 => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "Round 4: follow-up synthesis for compacted checkpoint continuity."
                        .into(),
                }],
                stop_reason: None,
                input_tokens: 100,
                output_tokens: 30,
                cache_usage: None,
                request_diagnostics: None,
            }),
            _ => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "Recovery path complete.".into(),
                }],
                stop_reason: None,
                input_tokens: 100,
                output_tokens: 20,
                cache_usage: None,
                request_diagnostics: None,
            }),
        }
    }
}

/// Provider that demonstrates multi-pass compaction recovery flow
pub struct MultiPassCompactionRecoveryFlowProvider {
    calls: Arc<Mutex<usize>>,
    requests: Arc<Mutex<Vec<CapturedTurnRequest>>>,
    task_id: Arc<Mutex<Option<String>>>,
}

impl MultiPassCompactionRecoveryFlowProvider {
    pub fn new() -> Self {
        Self {
            calls: Arc::new(Mutex::new(0)),
            requests: Arc::new(Mutex::new(Vec::new())),
            task_id: Arc::new(Mutex::new(None)),
        }
    }

    pub async fn set_task_id(&self, task_id: String) {
        let mut id = self.task_id.lock().await;
        *id = Some(task_id);
    }

    pub async fn captured_requests(&self) -> Vec<CapturedTurnRequest> {
        self.requests.lock().await.clone()
    }
}

#[async_trait]
impl AgentProvider for MultiPassCompactionRecoveryFlowProvider {
    async fn complete_turn(&self, request: ProviderTurnRequest) -> Result<ProviderTurnResponse> {
        let call_index = {
            let mut calls = self.calls.lock().await;
            *calls += 1;
            *calls
        };

        self.requests.lock().await.push(CapturedTurnRequest {
            prompt_text: delegated_prompt_text(&request),
            compression_epoch: request
                .prompt_frame
                .cache
                .as_ref()
                .map(|cache| cache.compression_epoch)
                .unwrap_or_default(),
            working_memory_revision: request
                .prompt_frame
                .cache
                .as_ref()
                .map(|cache| cache.working_memory_revision)
                .unwrap_or_default(),
        });

        match call_index {
            1 => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: format!(
                        "Round 1 planning {} {}",
                        "deep-context reconstruction ".repeat(240),
                        "Output token limit hit. Continue exactly where you left off. Do not restart from the top."
                    ),
                }],
                stop_reason: Some("max_tokens".into()),
                input_tokens: 0,
                output_tokens: 0,
                cache_usage: None,
                request_diagnostics: None,
            }),
            2 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: format!(
                            "Round 2 checkpoint signal {}. [Runtime-generated full progress checkpoint request]",
                            "delta capture ".repeat(220)
                        ),
                    },
                    ModelBlock::ToolUse {
                        id: "exec-round-2".into(),
                        name: "ExecCommand".into(),
                        input: serde_json::json!({
                            "cmd": "awk 'BEGIN { for (i=0; i<900; i++) printf \"exec_round_2_marker \" }'",
                            "max_output_tokens": 24,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 0,
                output_tokens: 0,
                cache_usage: None,
                request_diagnostics: None,
            }),
            3 => {
                let task_id = self
                    .task_id
                    .lock()
                    .await
                    .clone()
                    .unwrap_or_else(|| "missing-task".into());
                Ok(ProviderTurnResponse {
                    blocks: vec![
                        ModelBlock::Text {
                            text: format!(
                                "Round 3 follow-up {}",
                                "continuity anchor ".repeat(220)
                            ),
                        },
                        ModelBlock::ToolUse {
                            id: "task-output-round-3".into(),
                            name: "TaskOutput".into(),
                            input: serde_json::json!({
                                "task_id": task_id,
                                "block": false,
                            }),
                        },
                    ],
                    stop_reason: Some("tool_use".into()),
                    input_tokens: 0,
                    output_tokens: 0,
                    cache_usage: None,
                    request_diagnostics: None,
                })
            }
            4 => Ok(ProviderTurnResponse {
                blocks: vec![
                    ModelBlock::Text {
                        text: format!(
                            "Round 4 checkpointing {}",
                            "stable progression ".repeat(220)
                        ),
                    },
                    ModelBlock::ToolUse {
                        id: "exec-round-4".into(),
                        name: "ExecCommand".into(),
                        input: serde_json::json!({
                            "cmd": "awk 'BEGIN { for (i=0; i<840; i++) printf \"exec_round_4_marker \" }'",
                            "max_output_tokens": 24,
                        }),
                    },
                ],
                stop_reason: Some("tool_use".into()),
                input_tokens: 0,
                output_tokens: 0,
                cache_usage: None,
                request_diagnostics: None,
            }),
            _ => Ok(ProviderTurnResponse {
                blocks: vec![ModelBlock::Text {
                    text: "Completed after bounded repeated compaction.".into(),
                }],
                stop_reason: Some("end_turn".into()),
                input_tokens: 0,
                output_tokens: 0,
                cache_usage: None,
                request_diagnostics: None,
            }),
        }
    }
}