causa-protocol 0.3.0

Causa — a minimal context kernel for Rust agents (facts, ports, adapters)
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
//! Prefix stability: the framework must not change an Anthropic request
//! prefix between two rounds of one run. Covers the two framework-made
//! changes it guards against (system hoisting, whitespace-only assistant
//! text) and the cache anchor rules that go with them.
//!
//! The media half of the same property is asserted here too: every request
//! re-resolves every reference, so a context that is not edited must render
//! the same value for an earlier message on every round.

use causa_kernel::{
    BlockContent, BlockId, BlockMeta, CacheDirective, ContentPart, ContextBlock, ContextFrame,
    GenerationOptions, MediaRef, ModelRef, Role, SealedPart, TextPayload, ToolCallPayload,
    ToolChange, ToolDefinition,
};
use causa_protocol::translation::anthropic::render_anthropic_messages;
use causa_protocol::translation::media::{MediaPayload, MediaSet};
use causa_protocol::translation::openai_chat::render_openai_chat_messages;
use causa_protocol::translation::openai_responses::render_openai_responses_input;
use causa_protocol::translation::render_options::{
    AnthropicRenderOptions, ChatRenderOptions, ResponsesRenderOptions,
};
use serde_json::{Value, json};

fn id(label: &str) -> BlockId {
    let mut value = 0_u128;
    for byte in label.bytes() {
        value = value.wrapping_mul(257).wrapping_add(u128::from(byte));
    }
    BlockId::new(uuid::Uuid::from_u128(value))
}

fn block(label: &str, content: BlockContent, source: Option<&str>) -> ContextBlock {
    ContextBlock::new(id(label), content, {
        let mut meta = BlockMeta::default();
        meta.source = source.map(String::from);
        meta
    })
}

fn text(label: &str, text: &str, source: Option<&str>) -> ContextBlock {
    let role = match source {
        Some("system") => Role::System,
        Some("assistant") | None => Role::Assistant,
        _ => Role::User,
    };
    block(label, BlockContent::text(role, text), source)
}

fn call(label: &str, call_id: &str, name: &str) -> ContextBlock {
    ContextBlock::new(
        id(label),
        BlockContent::ToolCall(ToolCallPayload::new(name, json!({"path": "a"}))),
        BlockMeta::default().with_provider_call_id(call_id),
    )
}

fn frame(blocks: Vec<ContextBlock>) -> ContextFrame {
    ContextFrame { blocks }
}

/// Anthropic requires `max_tokens`; the renderer tests always supply one.
fn anthropic_generation() -> GenerationOptions {
    GenerationOptions::default().with_max_tokens(4096)
}

fn anthropic(frame: &ContextFrame, cache: CacheDirective) -> Value {
    render_anthropic_messages(
        frame,
        &MediaSet::new(),
        &anthropic_generation(),
        &ModelRef::new("claude-test"),
        cache,
        &AnthropicRenderOptions::default(),
    )
    .unwrap()
}

/// The same render with a tool listing, so the \`tools\` clause of the
/// stability assertion is not comparing two nulls.
fn anthropic_with_tools(
    frame: &ContextFrame,
    cache: CacheDirective,
    tools: &[ToolDefinition],
) -> Value {
    let mut blocks = vec![ContextBlock::new(
        id("declaration"),
        BlockContent::ToolChange(ToolChange::new(tools.to_vec(), Vec::new())),
        BlockMeta::default(),
    )];
    blocks.extend(frame.blocks.iter().cloned());
    render_anthropic_messages(
        &ContextFrame { blocks },
        &MediaSet::new(),
        &anthropic_generation(),
        &ModelRef::new("claude-test"),
        cache,
        &AnthropicRenderOptions::default(),
    )
    .unwrap()
}

fn chat(frame: &ContextFrame) -> Value {
    render_openai_chat_messages(
        frame,
        &MediaSet::new(),
        &GenerationOptions::default(),
        &ModelRef::new("chat-test"),
        CacheDirective::None,
        &ChatRenderOptions::default(),
    )
    .unwrap()
}

fn responses(frame: &ContextFrame) -> Value {
    render_openai_responses_input(
        frame,
        &MediaSet::new(),
        &GenerationOptions::default(),
        &ModelRef::new("responses-test"),
        CacheDirective::None,
        &ResponsesRenderOptions::default(),
    )
    .unwrap()
}

/// A system block before all conversation content, a user text, an assistant
/// whitespace-only text, and a normal assistant text.
fn prefix_frame() -> ContextFrame {
    frame(vec![
        text("s0", "be terse", Some("system")),
        text("u0", "hello", Some("user")),
        text("a-blank", "   \n  ", None),
        text("a0", "answer", None),
    ])
}

#[test]
fn whitespace_only_assistant_text_is_omitted_by_anthropic_and_chat_only() {
    let f = prefix_frame();

    let a = anthropic(&f, CacheDirective::None);
    let assistant_blocks: Vec<&Value> = a["messages"]
        .as_array()
        .unwrap()
        .iter()
        .filter(|m| m["role"] == "assistant")
        .flat_map(|m| m["content"].as_array().unwrap().iter())
        .collect();
    assert!(
        assistant_blocks
            .iter()
            .all(|b| b["text"] != json!("   \n  ")),
        "Anthropic omits the blank part: {assistant_blocks:?}"
    );
    assert!(
        assistant_blocks
            .iter()
            .any(|b| b["text"] == json!("answer")),
        "every other part of the turn stays"
    );

    let c = chat(&f);
    let messages = c["messages"].as_array().unwrap();
    assert!(
        messages.iter().all(|m| m["content"] != json!("   \n  ")),
        "Chat omits the blank part"
    );
    assert!(messages.iter().any(|m| m["content"] == json!("answer")));

    // Responses keeps it, as in `pi-ai`.
    let r = responses(&f);
    let items = r["input"].as_array().unwrap();
    assert!(
        items
            .iter()
            .any(|i| i["content"][0]["text"] == json!("   \n  ")),
        "Responses keeps the blank part: {items:?}"
    );
}

#[test]
fn a_chat_turn_left_with_only_sealed_residue_renders_no_assistant_message() {
    // The message-level keys an endpoint returns beside whitespace-only
    // content are kept as a sealed item. With the text omitted, that residue
    // alone would render an assistant message with neither `content` nor
    // `tool_calls`, so the message is omitted as well.
    let f = frame(vec![
        text("u0", "hello", Some("user")),
        block(
            "a-residue",
            BlockContent::Message {
                role: Role::Assistant,
                parts: vec![ContentPart::Sealed(SealedPart::new(
                    "openai_chat_completions",
                    json!({"refusal": null, "annotations": []}),
                ))],
            },
            None,
        ),
        text("a-blank", "  \n", None),
        text("u1", "again", Some("user")),
    ]);
    let c = chat(&f);
    assert_eq!(
        c["messages"],
        json!([
            {"role": "user", "content": "hello"},
            {"role": "user", "content": "again"},
        ])
    );

    // A sealed `content` the endpoint sent itself still renders its message.
    let kept = frame(vec![
        text("u0", "hello", Some("user")),
        block(
            "a-residue",
            BlockContent::Message {
                role: Role::Assistant,
                parts: vec![ContentPart::Sealed(SealedPart::new(
                    "openai_chat_completions",
                    json!({"content": null, "reasoning_content": "thought"}),
                ))],
            },
            None,
        ),
        text("u1", "again", Some("user")),
    ]);
    assert_eq!(
        chat(&kept)["messages"][1],
        json!({"role": "assistant", "content": null, "reasoning_content": "thought"})
    );
}

#[test]
fn top_level_system_is_the_array_form_with_and_without_a_cache_directive() {
    let f = prefix_frame();
    let plain = anthropic(&f, CacheDirective::None);
    assert_eq!(
        plain["system"],
        json!([{"type": "text", "text": "be terse"}]),
        "one block per leading system part, no join"
    );
    let cached = anthropic(&f, CacheDirective::StablePrefix);
    assert_eq!(
        cached["system"],
        json!([{
            "type": "text",
            "text": "be terse",
            "cache_control": {"type": "ephemeral"},
        }]),
        "the same shape, with the marker on the last block"
    );
    assert_eq!(plain["system"][0]["text"], cached["system"][0]["text"]);
}

#[test]
fn several_leading_system_parts_render_as_separate_blocks_without_a_join() {
    let f = frame(vec![
        text("s0", "be terse", Some("system")),
        text("s1", "cite sources", Some("system")),
        text("u0", "hello", Some("user")),
    ]);
    let v = anthropic(&f, CacheDirective::None);
    assert_eq!(
        v["system"],
        json!([
            {"type": "text", "text": "be terse"},
            {"type": "text", "text": "cite sources"},
        ]),
        "one block per leading system part; a join would collapse them into one"
    );
    // Responses keeps the listed exception: one \`instructions\` string.
    let r = responses(&f);
    assert_eq!(
        r["instructions"],
        json!(
            "be terse
cite sources"
        )
    );
}

#[test]
fn an_appended_system_block_leaves_the_top_level_system_unchanged() {
    let before = frame(vec![
        text("s0", "be terse", Some("system")),
        text("u0", "hello", Some("user")),
    ]);
    let after = frame(vec![
        text("s0", "be terse", Some("system")),
        text("u0", "hello", Some("user")),
        text("s1", "second instruction", Some("system")),
    ]);

    let before_a = anthropic(&before, CacheDirective::None);
    let after_a = anthropic(&after, CacheDirective::None);
    assert_eq!(
        before_a["system"], after_a["system"],
        "an append never rewrites the top-level system parameter"
    );
    let messages = after_a["messages"].as_array().unwrap();
    assert_eq!(
        messages.last().unwrap()["role"],
        json!("system"),
        "the appended block renders at its position"
    );
    assert_eq!(
        messages.last().unwrap()["content"],
        json!([{"type": "text", "text": "second instruction"}])
    );

    // Responses: leading system text stays in instructions; a later block
    // becomes an input item.
    let after_r = responses(&after);
    assert_eq!(after_r["instructions"], json!("be terse"));
    let items = after_r["input"].as_array().unwrap();
    assert_eq!(
        items.last().unwrap(),
        &json!({
            "role": "system",
            "content": [{"type": "input_text", "text": "second instruction"}],
        })
    );
}

#[test]
fn consecutive_renders_of_one_tool_loop_share_their_prefix() {
    // Round one: the system prefix, the user request, and the completed read
    // exchange. Round two appends the assistant answer and the next question.
    let round_one = frame(vec![
        text("s0", "be terse", Some("system")),
        text("u0", "read the file", Some("user")),
        call("c0", "toolu_1", "read"),
        block(
            "r0",
            BlockContent::ToolResult(causa_kernel::ToolResultPayload::new(
                id("c0"),
                causa_kernel::ToolResultStatus::Succeeded,
                causa_kernel::ToolOutput::new(json!("file-a")),
            )),
            None,
        ),
        text("a0", "the file says a", None),
        text("u1", "and now?", Some("user")),
    ]);
    // Round two closes the exchange: the second read is declared and answered,
    // and the messages of round one are still at the front of the request.
    let mut blocks = round_one.blocks.clone();
    blocks.push(call("c1", "toolu_2", "read"));
    blocks.push(block(
        "r1",
        BlockContent::ToolResult(causa_kernel::ToolResultPayload::new(
            id("c1"),
            causa_kernel::ToolResultStatus::Succeeded,
            causa_kernel::ToolOutput::new(json!("file-b")),
        )),
        None,
    ));
    blocks.push(text("a1", "the second file says b", None));
    let round_two = frame(blocks);

    let surface = vec![ToolDefinition::new(
        "read",
        "read a file",
        json!({"type": "object"}),
    )];
    let first = anthropic_with_tools(&round_one, CacheDirective::StablePrefix, &surface);
    let second = anthropic_with_tools(&round_two, CacheDirective::StablePrefix, &surface);

    assert_eq!(
        first["system"], second["system"],
        "the system value is stable across rounds"
    );
    assert!(
        first["tools"]
            .as_array()
            .is_some_and(|tools| !tools.is_empty()),
        "the listing must be non-empty or this assertion proves nothing"
    );
    assert_eq!(
        first["tools"], second["tools"],
        "an unchanged tool listing renders the same value"
    );
    // Every message the first round sent is still there, unchanged except that
    // the cache marker may move to a later message.
    let first_messages = first["messages"].as_array().unwrap();
    let second_messages = second["messages"].as_array().unwrap();
    assert!(second_messages.len() >= first_messages.len());
    for (index, message) in first_messages.iter().enumerate() {
        let mut a = message.clone();
        let mut b = second_messages[index].clone();
        for value in [&mut a, &mut b] {
            strip_cache_control(value);
        }
        assert_eq!(a, b, "message {index} changed between rounds");
    }
    // Rendering is deterministic: same context, same bytes.
    assert_eq!(
        anthropic_with_tools(&round_two, CacheDirective::StablePrefix, &surface),
        second
    );
    // The marker lands on a message block, never on a sealed item.
    let anchored = second_messages
        .iter()
        .filter_map(|message| message["content"].as_array())
        .flatten()
        .filter(|block| block.get("cache_control").is_some())
        .collect::<Vec<_>>();
    assert_eq!(anchored.len(), 1, "exactly one message marker");
}

#[test]
fn the_stable_prefix_anchor_skips_a_sealed_item_and_moves_backward() {
    // The assistant turn is one sealed thinking item: written verbatim, so the
    // marker may not be written into it, and the anchor has to move to an
    // earlier message.
    let f = frame(vec![
        text("u0", "look", Some("user")),
        block(
            "a0",
            BlockContent::Message {
                role: Role::Assistant,
                parts: vec![ContentPart::Sealed(SealedPart::new(
                    "anthropic_messages",
                    json!({"type": "thinking", "thinking": "hmm", "signature": "sig"}),
                ))],
            },
            None,
        ),
    ]);
    let v = anthropic(&f, CacheDirective::StablePrefix);
    let messages = v["messages"].as_array().unwrap();
    assert_eq!(messages.len(), 2);
    // Verbatim: the sealed item is exactly what the parser stored.
    assert_eq!(
        messages[1]["content"][0],
        json!({"type": "thinking", "thinking": "hmm", "signature": "sig"}),
        "the sealed item is written verbatim"
    );
    assert!(
        messages[1]["content"][0].get("cache_control").is_none(),
        "no cache marker may be written into a sealed item"
    );
    assert!(
        messages[0]["content"][0].get("cache_control").is_some(),
        "the anchor moves backward to the last non-sealed block"
    );
    // Without the directive nothing is marked and the item is still verbatim.
    let plain = anthropic(&f, CacheDirective::None);
    assert_eq!(
        plain["messages"][1]["content"][0],
        messages[1]["content"][0]
    );
}

fn strip_cache_control(value: &mut Value) {
    match value {
        Value::Object(map) => {
            map.remove("cache_control");
            for child in map.values_mut() {
                strip_cache_control(child);
            }
        }
        Value::Array(items) => {
            for item in items {
                strip_cache_control(item);
            }
        }
        _ => {}
    }
}

#[test]
fn an_unedited_media_reference_renders_the_same_value_on_every_round() {
    let mut media = MediaSet::new();
    media.insert("asset-1", MediaPayload::inline("image/png", "AAAA"));
    let f = frame(vec![
        block(
            "u0",
            BlockContent::Message {
                role: Role::User,
                parts: vec![
                    ContentPart::Text(TextPayload::new("look")),
                    ContentPart::Media(MediaRef::new("image/png", "asset-1")),
                ],
            },
            Some("user"),
        ),
        text("u1", "and now?", Some("user")),
    ]);
    let render = |f: &ContextFrame| {
        render_anthropic_messages(
            f,
            &media,
            &anthropic_generation(),
            &ModelRef::new("claude-test"),
            CacheDirective::StablePrefix,
            &AnthropicRenderOptions::default(),
        )
        .unwrap()
    };
    assert_eq!(render(&f), render(&f));
    assert_eq!(
        render(&f)["messages"][0]["content"][1]["type"],
        json!("image"),
        "the served reference renders as an image"
    );

    // A degraded reference is a value too, and it is stable for the same
    // reason: every request re-resolves from the same table. Two ways to
    // degrade: an unserved reference, and a payload whose type is not an image
    // (the walk inlines every `image/*`; the host pre-filters the rest).
    for set in [MediaSet::new(), {
        let mut unsupported = MediaSet::new();
        unsupported.insert("asset-1", MediaPayload::inline("audio/mpeg", "AAAA"));
        unsupported
    }] {
        let degraded = |f: &ContextFrame| {
            render_anthropic_messages(
                f,
                &set,
                &anthropic_generation(),
                &ModelRef::new("claude-test"),
                CacheDirective::StablePrefix,
                &AnthropicRenderOptions::default(),
            )
            .unwrap()
        };
        let rendered = degraded(&f);
        assert_eq!(
            rendered,
            degraded(&f),
            "a degraded value is stable across renders"
        );
        assert_eq!(
            rendered["messages"][0]["content"][1],
            json!({"type": "text", "text": "[media: image/png asset-1]"}),
            "the reference degrades to the deterministic placeholder"
        );
    }
}