atman-runtime 1.11.0

atman flow execution runtime: evaluator, tool dispatch, provider dispatch, executor, memory stores
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
use crate::error::RuntimeError;
use crate::message::{Message, MessagePart};
use crate::tool::{BoxFut, Tier, Tool, ToolArgs, ToolCtx, ToolResult};
use crate::value::Value;

pub const FINAL_ANSWER_TOOL: &str = "final.answer";

struct Candidate<'a> {
    answer: &'a str,
    summary: Option<&'a str>,
}

fn raw_candidate(message: &Message) -> Result<Option<Candidate<'_>>, &'static str> {
    let final_calls = message
        .parts
        .iter()
        .filter(
            |part| matches!(part, MessagePart::ToolUse { name, .. } if name == FINAL_ANSWER_TOOL),
        )
        .count();
    if final_calls == 0 {
        return Ok(None);
    }
    if final_calls != 1 {
        return Err("final.answer must be the only tool call in the assistant response");
    }
    let tool_calls = message
        .parts
        .iter()
        .filter(|part| matches!(part, MessagePart::ToolUse { .. }))
        .count();
    let has_text = message
        .parts
        .iter()
        .any(|part| matches!(part, MessagePart::Text { text } if !text.trim().is_empty()));
    if tool_calls != 1 || has_text {
        return Err(
            "final.answer must be emitted alone, without sibling tool calls or assistant text",
        );
    }
    let Some(MessagePart::ToolUse { input, intent, .. }) = message.parts.iter().find(
        |part| matches!(part, MessagePart::ToolUse { name, .. } if name == FINAL_ANSWER_TOOL),
    ) else {
        unreachable!();
    };
    let Some(answer) = input
        .get("message")
        .and_then(serde_json::Value::as_str)
        .filter(|answer| !answer.trim().is_empty())
    else {
        return Err("final.answer requires a non-empty `message`");
    };
    Ok(Some(Candidate {
        answer,
        summary: intent.as_ref().map(|intent| intent.as_str()),
    }))
}

fn fallback_summary(answer: &str) -> String {
    let first_line = answer
        .lines()
        .map(str::trim)
        .find(|line| !line.is_empty())
        .unwrap_or("Completed internal work")
        .trim_start_matches(['#', '*', '-', '>', '`'])
        .trim();
    crate::message::ToolCallIntent::new(first_line)
        .or_else(|| crate::message::ToolCallIntent::new("Completed internal work"))
        .expect("fallback final-answer summary is non-empty")
        .as_str()
        .to_owned()
}

pub fn attempted(message: &Message) -> bool {
    message
        .parts
        .iter()
        .any(|part| matches!(part, MessagePart::ToolUse { name, .. } if name == FINAL_ANSWER_TOOL))
}

pub fn validation_error(message: &Message) -> Option<&'static str> {
    raw_candidate(message).err()
}

pub fn summary(message: &Message) -> Option<String> {
    message
        .parts
        .iter()
        .find_map(|part| match part {
            MessagePart::FinalAnswerSummary { text } => {
                (!text.trim().is_empty()).then(|| text.clone())
            }
            MessagePart::ToolUse {
                name,
                input,
                intent,
                ..
            } if name == FINAL_ANSWER_TOOL => intent
                .as_ref()
                .map(|intent| intent.as_str().to_owned())
                .or_else(|| {
                    input
                        .get("message")
                        .and_then(serde_json::Value::as_str)
                        .filter(|answer| !answer.trim().is_empty())
                        .map(fallback_summary)
                }),
            _ => None,
        })
        .or_else(|| {
            (message.origin == crate::message::MessageOrigin::FinalAnswer)
                .then(|| message.text_concat())
                .filter(|answer| !answer.trim().is_empty())
                .map(|answer| fallback_summary(&answer))
        })
}

pub fn extract(message: &Message) -> Option<String> {
    if message.origin == crate::message::MessageOrigin::FinalAnswer {
        return (!message.text_concat().trim().is_empty()).then(|| message.text_concat());
    }
    raw_candidate(message)
        .ok()
        .flatten()
        .map(|candidate| candidate.answer.to_owned())
}

pub fn normalized_for_history(message: &Message) -> Option<Message> {
    if message.origin == crate::message::MessageOrigin::FinalAnswer {
        return extract(message)
            .and_then(|_| summary(message))
            .map(|_| message.clone());
    }
    let candidate = raw_candidate(message).ok().flatten()?;
    let mut normalized = message.clone();
    normalized.origin = crate::message::MessageOrigin::FinalAnswer;
    normalized
        .parts
        .retain(|part| matches!(part, MessagePart::Thinking { .. }));
    normalized.parts.push(MessagePart::FinalAnswerSummary {
        text: candidate
            .summary
            .map(str::to_owned)
            .unwrap_or_else(|| fallback_summary(candidate.answer)),
    });
    normalized.parts.push(MessagePart::Text {
        text: candidate.answer.to_owned(),
    });
    Some(normalized)
}

pub struct FinalAnswer;

impl Tool for FinalAnswer {
    fn name(&self) -> &str {
        FINAL_ANSWER_TOOL
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some(
            "Deliver the final user-facing answer after all thinking and tool work is complete. Use _atman_intent to summarize the completed work for the collapsed activity header.",
        )
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {
                "message": {
                    "type": "string",
                    "description": "Complete final answer in Markdown."
                }
            },
            "required": ["message"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            match args.named("message").or_else(|| args.positional.first()) {
                Some(Value::Str(message)) => Ok(Value::Str(message.clone())),
                Some(other) => Err(RuntimeError::TypeMismatch {
                    expected: "string".into(),
                    actual: other.kind_name().into(),
                }),
                None => Err(RuntimeError::ToolFailed(
                    "final.answer: missing `message`".into(),
                )),
            }
        })
    }
}

pub struct ExtractFinalAnswer;

impl Tool for ExtractFinalAnswer {
    fn name(&self) -> &str {
        "extract_final_answer"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn description(&self) -> Option<&str> {
        Some("Extract a final.answer control payload from an assistant Message.")
    }

    fn input_schema(&self) -> serde_json::Value {
        serde_json::json!({
            "type": "object",
            "properties": {"message": {"description": "Assistant Message value."}},
            "required": ["message"]
        })
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let value = args.named("message").or_else(|| args.positional.first());
            match value {
                Some(Value::Message(message)) => {
                    Ok(extract(message).map(Value::Str).unwrap_or(Value::Unit))
                }
                Some(Value::Str(_)) | None => Ok(Value::Unit),
                Some(other) => Err(RuntimeError::TypeMismatch {
                    expected: "message or string".into(),
                    actual: other.kind_name().into(),
                }),
            }
        })
    }
}

pub struct FinalizeResponse;

impl Tool for FinalizeResponse {
    fn name(&self) -> &str {
        "finalize_response"
    }

    fn tier(&self) -> Tier {
        Tier::Zero
    }

    fn requires_call_intent(&self) -> bool {
        false
    }

    fn call<'a>(&'a self, args: ToolArgs, _ctx: &'a ToolCtx) -> BoxFut<'a, ToolResult> {
        Box::pin(async move {
            let message = match args.named("message").or_else(|| args.positional.first()) {
                Some(Value::Message(message)) => message,
                Some(other) => {
                    return Err(RuntimeError::TypeMismatch {
                        expected: "message".into(),
                        actual: other.kind_name().into(),
                    });
                }
                None => {
                    return Err(RuntimeError::MissingArg(
                        "finalize_response: message".into(),
                    ));
                }
            };
            normalized_for_history(message)
                .map(Value::Message)
                .ok_or_else(|| {
                    RuntimeError::ToolFailed(
                        validation_error(message)
                            .unwrap_or("invalid final.answer control")
                            .into(),
                    )
                })
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event::TurnId;

    fn control_message() -> Message {
        Message {
            turn_id: TurnId::now(),
            role: crate::message::MessageRole::Assistant,
            parts: vec![MessagePart::ToolUse {
                id: "answer-1".into(),
                name: FINAL_ANSWER_TOOL.into(),
                input: serde_json::json!({"message": "Done."}),
                intent: None,
            }],
            origin: crate::message::MessageOrigin::User,
        }
    }

    #[test]
    fn final_answer_schema_requires_summary_intent() {
        let spec = crate::tool::tool_spec(&FinalAnswer);
        assert_eq!(
            spec.input_schema["required"],
            serde_json::json!(["message", "_atman_intent"])
        );
        assert!(
            spec.input_schema["properties"]
                .get("_atman_intent")
                .is_some()
        );
        assert_eq!(
            spec.input_schema["properties"]["_atman_intent"],
            serde_json::json!({
                "type": "string",
                "minLength": 1,
                "maxLength": 120,
                "pattern": "\\S"
            })
        );
    }

    #[test]
    fn normalizes_missing_summary_intent_with_answer_fallback() {
        let normalized = normalized_for_history(&control_message()).unwrap();
        assert_eq!(normalized.text_concat(), "Done.");
        assert_eq!(summary(&normalized).as_deref(), Some("Done."));
        assert_eq!(
            normalized.origin,
            crate::message::MessageOrigin::FinalAnswer
        );
    }

    #[test]
    fn normalizes_valid_control_to_plain_assistant_text() {
        let mut message = control_message();
        let MessagePart::ToolUse { intent, .. } = &mut message.parts[0] else {
            unreachable!();
        };
        *intent = crate::message::ToolCallIntent::new("Completed requested work.");
        let normalized = normalized_for_history(&message).unwrap();
        assert_eq!(normalized.text_concat(), "Done.");
        assert!(!normalized.parts.iter().any(
            |part| matches!(part, MessagePart::ToolUse { name, .. } if name == FINAL_ANSWER_TOOL)
        ));
    }

    #[test]
    fn preserves_final_answer_summary_for_replay() {
        let mut message = control_message();
        let MessagePart::ToolUse { intent, .. } = &mut message.parts[0] else {
            unreachable!();
        };
        *intent = crate::message::ToolCallIntent::new("Checked the renderer and tests.");

        let normalized = normalized_for_history(&message).unwrap();
        assert_eq!(
            summary(&normalized).as_deref(),
            Some("Checked the renderer and tests.")
        );
        assert_eq!(normalized.text_concat(), "Done.");
    }

    #[test]
    fn rejects_final_control_mixed_with_text_or_other_tools() {
        let mut message = control_message();
        let MessagePart::ToolUse { intent, .. } = &mut message.parts[0] else {
            unreachable!();
        };
        *intent = crate::message::ToolCallIntent::new("Completed requested work.");
        message.parts.push(MessagePart::Text {
            text: "preface".into(),
        });
        assert_eq!(
            validation_error(&message),
            Some(
                "final.answer must be emitted alone, without sibling tool calls or assistant text"
            )
        );

        message.parts.pop();
        message.parts.push(MessagePart::ToolUse {
            id: "read-1".into(),
            name: "fs.read".into(),
            input: serde_json::json!({"path": "README.md"}),
            intent: crate::message::ToolCallIntent::new("Read documentation."),
        });
        assert_eq!(
            validation_error(&message),
            Some(
                "final.answer must be emitted alone, without sibling tool calls or assistant text"
            )
        );
    }

    #[test]
    fn rejects_empty_or_repeated_final_controls() {
        let mut message = control_message();
        let MessagePart::ToolUse { input, intent, .. } = &mut message.parts[0] else {
            unreachable!();
        };
        *input = serde_json::json!({"message": "  "});
        *intent = crate::message::ToolCallIntent::new("Completed requested work.");
        assert_eq!(
            validation_error(&message),
            Some("final.answer requires a non-empty `message`")
        );

        message.parts.push(message.parts[0].clone());
        assert_eq!(
            validation_error(&message),
            Some("final.answer must be the only tool call in the assistant response")
        );
    }
}