aion-server 0.30.0

Aion workflow server library: HTTP, gRPC, WebSocket, and worker endpoints. Run it with the `aion` binary from the aion-cli crate.
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
//! What the translator must and must not produce.

use aion_core::{ActivityId, RunId, WorkflowId};

use super::*;

fn event(kind: ActivityEventKind) -> ActivityEvent {
    ActivityEvent {
        workflow_id: WorkflowId::new(uuid::Uuid::nil()),
        run_id: RunId::new(uuid::Uuid::from_u128(1)),
        activity_id: ActivityId::from_sequence_position(1),
        attempt: 1,
        agent_id: uuid::Uuid::from_u128(2),
        agent_role: "assistant".to_owned(),
        emitted_at: chrono::Utc::now(),
        worker_seq: 0,
        store_seq: None,
        ephemeral: false,
        kind,
    }
}

#[test]
fn a_token_delta_becomes_a_delta_frame() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Delta {
        message_id: "m-1".to_owned(),
        text_fragment: "hel".to_owned(),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::Delta {
            turn_id: "t-1".to_owned(),
            text: "hel".to_owned(),
        }]
    );
}

#[test]
fn a_completed_assistant_message_is_not_streamed_twice() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Message {
        role: MessageRole::Assistant,
        text: "hello".to_owned(),
    }));
    assert!(
        produced.is_empty(),
        "the deltas already carried this text; a second frame would double it"
    );
}

#[test]
fn a_progress_note_becomes_a_thought() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Progress {
        detail: ProgressDetail::Note {
            text: "considering".to_owned(),
        },
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::Thought {
            turn_id: "t-1".to_owned(),
            text: "considering".to_owned(),
        }]
    );
}

#[test]
fn a_tool_result_is_named_by_the_call_that_produced_it() {
    let mut frames = TurnFrames::new("t-1");
    let started = frames.translate(event(ActivityEventKind::ToolCall {
        tool: "check_document".to_owned(),
        call_id: "c-1".to_owned(),
        input: serde_json::json!({ "path": "a.awl" }),
    }));
    assert_eq!(
        started,
        vec![AssistantSessionEvent::ToolCall {
            turn_id: "t-1".to_owned(),
            call_id: "c-1".to_owned(),
            name: "check_document".to_owned(),
            status: AssistantToolCallStatus::Started,
            input: Some(serde_json::json!({ "path": "a.awl" })),
            output: None,
        }]
    );
    let finished = frames.translate(event(ActivityEventKind::ToolResult {
        call_id: "c-1".to_owned(),
        output: serde_json::json!({ "diagnostics": [] }),
        is_error: false,
    }));
    assert_eq!(
        finished,
        vec![AssistantSessionEvent::ToolCall {
            turn_id: "t-1".to_owned(),
            call_id: "c-1".to_owned(),
            name: "check_document".to_owned(),
            status: AssistantToolCallStatus::Completed,
            input: None,
            output: Some(serde_json::json!({ "diagnostics": [] })),
        }]
    );
}

#[test]
fn a_result_with_no_remembered_call_says_so_rather_than_inventing_a_tool() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::ToolResult {
        call_id: "orphan".to_owned(),
        output: serde_json::Value::Null,
        is_error: true,
    }));
    match produced.as_slice() {
        [AssistantSessionEvent::ToolCall { name, status, .. }] => {
            assert_eq!(name, UNMATCHED_TOOL);
            assert_eq!(*status, AssistantToolCallStatus::Failed);
        }
        other => panic_free_failure(other),
    }
}

#[test]
fn a_permission_request_reaches_the_transcript_joined_to_its_decision() {
    let mut frames = TurnFrames::new("t-1");
    let held = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission".to_owned(),
        value: serde_json::json!({ "toolCall": { "toolCallId": "c-1" } }),
    }));
    assert!(
        held.is_empty(),
        "the ask alone says nothing about what was answered"
    );
    let decided = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission/decision".to_owned(),
        value: serde_json::json!({
            "policy": "deny",
            "toolCallId": "c-1",
            "outcome": { "outcome": "selected", "optionId": "reject-1" }
        }),
    }));
    assert_eq!(
        decided,
        vec![AssistantSessionEvent::PermissionAsk {
            turn_id: "t-1".to_owned(),
            request: serde_json::json!({ "toolCall": { "toolCallId": "c-1" } }),
            decided: AssistantPermissionDecision::Deny,
        }]
    );
}

#[test]
fn an_allow_once_policy_is_recorded_as_allow_once() {
    let mut frames = TurnFrames::new("t-1");
    let _held = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission".to_owned(),
        value: serde_json::Value::Null,
    }));
    let decided = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission/decision".to_owned(),
        value: serde_json::json!({
            "policy": "allow_once",
            "outcome": { "outcome": "selected", "optionId": "allow-1" }
        }),
    }));
    match decided.as_slice() {
        [AssistantSessionEvent::PermissionAsk { decided, .. }] => {
            assert_eq!(*decided, AssistantPermissionDecision::AllowOnce);
        }
        other => panic_free_failure(other),
    }
}

#[test]
fn a_cancelled_outcome_is_recorded_verbatim_rather_than_reported_as_a_denial() {
    let mut frames = TurnFrames::new("t-1");
    let decided = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission/decision".to_owned(),
        value: serde_json::json!({
            "policy": "allow_once",
            "outcome": { "outcome": "cancelled" }
        }),
    }));
    match decided.as_slice() {
        [AssistantSessionEvent::Raw { source, value, .. }] => {
            assert_eq!(source, "session/request_permission/decision");
            assert_eq!(value["decision"]["outcome"]["outcome"], "cancelled");
        }
        // A `permission_ask` here would put a decision on the record that was
        // never taken, and the wire carries only `allow_once` and `deny`.
        other => panic_free_failure(other),
    }
}

#[test]
fn an_unanswered_permission_request_still_reaches_the_record() {
    let mut frames = TurnFrames::new("t-1");
    let _held = frames.translate(event(ActivityEventKind::Raw {
        source: "session/request_permission".to_owned(),
        value: serde_json::json!({ "toolCall": { "toolCallId": "c-9" } }),
    }));
    let flushed = frames.flush();
    match flushed.as_slice() {
        [AssistantSessionEvent::Raw { source, value, .. }] => {
            assert_eq!(source, "session/request_permission/unanswered");
            assert_eq!(value["toolCall"]["toolCallId"], "c-9");
        }
        other => panic_free_failure(other),
    }
    assert!(
        frames.flush().is_empty(),
        "a flushed request is not flushed twice"
    );
}

#[test]
fn an_unmapped_frame_passes_through_verbatim_rather_than_vanishing() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: "session/update/undecodable".to_owned(),
        value: serde_json::json!({ "error": "bad frame" }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::Raw {
            turn_id: Some("t-1".to_owned()),
            source: "session/update/undecodable".to_owned(),
            value: serde_json::json!({ "error": "bad frame" }),
        }]
    );
}

#[test]
fn a_usage_estimate_is_kept_as_a_raw_frame_rather_than_dropped() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Progress {
        detail: ProgressDetail::UsageEstimate {
            input_tokens: Some(10),
            output_tokens: Some(3),
        },
    }));
    match produced.as_slice() {
        [AssistantSessionEvent::Raw { source, value, .. }] => {
            assert_eq!(source, "progress");
            assert_eq!(value["input_tokens"], serde_json::json!(10));
        }
        other => panic_free_failure(other),
    }
}

#[test]
fn the_terminal_stop_is_left_to_the_turns_own_result() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Stop {
        reason: aion_core::StopKind::EndTurn,
    }));
    assert!(
        produced.is_empty(),
        "the turn's result carries the stop reason AND the final message"
    );
}

/// Fails a test that matched the wrong shape, without a `panic!` macro.
///
/// The workspace denies `panic`, and a wrong-shape match still has to fail
/// loudly: `assert_eq!` against a rendering of what was produced does both.
fn panic_free_failure(produced: &[AssistantSessionEvent]) {
    assert_eq!(
        format!("{produced:?}"),
        "<the expected frame shape>",
        "the translator produced an unexpected shape"
    );
}

/// T4's classification arm: the harness's own `available_commands_update`
/// becomes a CLASSIFIED frame, not a `raw` a console would have to parse ACP
/// out of.
///
/// The payload is written as the wire form an agent actually sends —
/// `availableCommands` with each entry's `name`, `description`, and the
/// `unstructured` input's `hint` — rather than built from the schema's Rust
/// types, so this tests the reading against the protocol rather than against
/// one crate's own `Serialize`.
#[test]
fn an_available_commands_update_becomes_a_classified_frame() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: AVAILABLE_COMMANDS_SOURCE.to_owned(),
        value: serde_json::json!({
            "availableCommands": [
                { "name": "compact", "description": "compact the conversation" },
                {
                    "name": "plan",
                    "description": "draft a plan",
                    "input": { "hint": "what to plan" },
                },
            ]
        }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::AvailableCommands {
            commands: vec![
                AssistantCommand {
                    name: "compact".to_owned(),
                    description: "compact the conversation".to_owned(),
                    input_hint: None,
                },
                AssistantCommand {
                    name: "plan".to_owned(),
                    description: "draft a plan".to_owned(),
                    input_hint: Some("what to plan".to_owned()),
                },
            ],
        }]
    );
}

/// An EMPTY advertisement is a withdrawal, and it is classified as one: the
/// agent said it now serves nothing, and a console must stop offering what it
/// used to. This is why an unreadable payload cannot be reported as empty.
#[test]
fn an_empty_advertisement_is_a_withdrawal_rather_than_a_raw_frame() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: AVAILABLE_COMMANDS_SOURCE.to_owned(),
        value: serde_json::json!({ "availableCommands": [] }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::AvailableCommands {
            commands: Vec::new(),
        }]
    );
}

/// A payload this server cannot read as a command list passes through VERBATIM.
///
/// Never as an empty advertisement: an empty list means the agent withdrew every
/// command, and reporting a shape change that way would take a working control
/// off an operator's surface for a reason that was never stated. Nothing is
/// dropped either — the frame is on the record for whoever has to work out what
/// the agent actually sent.
#[test]
fn an_unreadable_command_payload_is_kept_verbatim_and_never_read_as_a_withdrawal() {
    let mut frames = TurnFrames::new("t-1");
    let value = serde_json::json!({ "commands": ["compact"] });
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: AVAILABLE_COMMANDS_SOURCE.to_owned(),
        value: value.clone(),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::Raw {
            turn_id: Some("t-1".to_owned()),
            source: AVAILABLE_COMMANDS_SOURCE.to_owned(),
            value,
        }]
    );
}

/// One malformed entry does not take the advertisement down with it: the
/// commands the agent named readably are still offerable, and one it named
/// without a description is one nothing could have offered anyway.
#[test]
fn a_malformed_entry_is_dropped_and_the_rest_of_the_advertisement_stands() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: AVAILABLE_COMMANDS_SOURCE.to_owned(),
        value: serde_json::json!({
            "availableCommands": [
                { "name": "compact" },
                { "name": "plan", "description": "draft a plan" },
            ]
        }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::AvailableCommands {
            commands: vec![AssistantCommand {
                name: "plan".to_owned(),
                description: "draft a plan".to_owned(),
                input_hint: None,
            }],
        }]
    );
}

/// The source label this server classifies on is the one the ADAPTER actually
/// produces — `update_raw`'s prefix plus the ACP `sessionUpdate` discriminator.
/// Written out independently here, so a rename on either side fails rather than
/// silently turning every advertisement back into a `raw` frame nobody reads.
#[test]
fn the_classified_source_is_the_label_the_adapter_emits() {
    assert_eq!(
        AVAILABLE_COMMANDS_SOURCE,
        format!(
            "{}/available_commands_update",
            aion_integration_acp::translate::UPDATE_SOURCE_PREFIX
        )
    );
}

/// The classification arm for `config_option_update`: an ungrouped select —
/// the model picker's usual shape — becomes the classified frame with its
/// category and current value intact.
#[test]
fn a_config_option_update_becomes_a_classified_frame() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: "session/update/config_option_update".to_owned(),
        value: serde_json::json!({
            "sessionUpdate": "config_option_update",
            "configOptions": [{
                "id": "model",
                "name": "Model",
                "category": "model",
                "type": "select",
                "currentValue": "opus",
                "options": [
                    { "value": "opus", "name": "Opus", "description": "the main one" },
                    { "value": "fable", "name": "Fable" }
                ]
            }]
        }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::ConfigOptions {
            options: vec![aion_core::AssistantConfigOption {
                id: "model".to_owned(),
                name: "Model".to_owned(),
                description: None,
                category: Some("model".to_owned()),
                value: aion_core::AssistantConfigValue::Select {
                    choices: vec![
                        aion_core::AssistantConfigChoice {
                            id: "opus".to_owned(),
                            name: "Opus".to_owned(),
                            description: Some("the main one".to_owned()),
                            group: None,
                        },
                        aion_core::AssistantConfigChoice {
                            id: "fable".to_owned(),
                            name: "Fable".to_owned(),
                            description: None,
                            group: None,
                        },
                    ],
                    current: "opus".to_owned(),
                },
            }],
        }]
    );
}

/// Grouped select options flatten with the group's label riding on each
/// choice, so the agent's own grouping reaches the surface without a choice
/// being lost to it.
#[test]
fn grouped_select_choices_flatten_with_their_group_label() -> Result<(), String> {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: "session/update/config_option_update".to_owned(),
        value: serde_json::json!({
            "configOptions": [{
                "id": "model",
                "name": "Model",
                "type": "select",
                "currentValue": "opus",
                "options": [{
                    "group": "anthropic",
                    "name": "Anthropic",
                    "options": [{ "value": "opus", "name": "Opus" }]
                }]
            }]
        }),
    }));
    let [AssistantSessionEvent::ConfigOptions { options }] = produced.as_slice() else {
        return Err(format!("expected one classified frame, got {produced:?}"));
    };
    let aion_core::AssistantConfigValue::Select { choices, .. } = &options[0].value else {
        return Err(format!("expected a select, got {:?}", options[0].value));
    };
    assert_eq!(choices[0].group.as_deref(), Some("Anthropic"));
    assert_eq!(choices[0].id, "opus");
    Ok(())
}

/// A boolean option is a toggle; an option of a kind this server has no shape
/// for is skipped while the rest of the advertisement stands.
#[test]
fn a_boolean_option_is_a_toggle_and_an_unknown_kind_is_skipped() {
    let mut frames = TurnFrames::new("t-1");
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: "session/update/config_option_update".to_owned(),
        value: serde_json::json!({
            "configOptions": [
                { "id": "burst", "name": "Burst", "type": "boolean", "currentValue": true },
                { "id": "prose", "name": "Prose", "type": "freeform_text", "currentValue": "x" }
            ]
        }),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::ConfigOptions {
            options: vec![aion_core::AssistantConfigOption {
                id: "burst".to_owned(),
                name: "Burst".to_owned(),
                description: None,
                category: None,
                value: aion_core::AssistantConfigValue::Toggle { current: true },
            }],
        }]
    );
}

/// An unreadable option payload passes through verbatim rather than becoming
/// an EMPTY advertisement — an empty list is a withdrawal, and this frame is
/// this server not knowing what the agent said. The commands twin of this
/// case pins the same rule.
#[test]
fn an_unreadable_config_payload_passes_through_verbatim() {
    let mut frames = TurnFrames::new("t-1");
    let value = serde_json::json!({ "sessionUpdate": "config_option_update", "somethingElse": 1 });
    let produced = frames.translate(event(ActivityEventKind::Raw {
        source: "session/update/config_option_update".to_owned(),
        value: value.clone(),
    }));
    assert_eq!(
        produced,
        vec![AssistantSessionEvent::Raw {
            turn_id: Some("t-1".to_owned()),
            source: "session/update/config_option_update".to_owned(),
            value,
        }]
    );
}