Skip to main content

aidaemon_core/
receipt.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::state::RunId;
5use crate::tool::SourceTrust;
6
7/// How far a call got before its outcome was decided.
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(rename_all = "snake_case")]
10pub enum InvocationStage {
11    /// Refused before the tool ran (unknown tool, bad arguments, policy,
12    /// or skipped because the run had to stop).
13    RejectedBeforeDispatch,
14    /// The tool's code was invoked.
15    Dispatched,
16}
17
18#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
19#[serde(rename_all = "snake_case")]
20pub enum ReceiptStatus {
21    Succeeded,
22    /// Ran and reported failure, or returned a [`crate::ToolError`].
23    Failed,
24    /// A hook or the user refused it.
25    Denied,
26    TimedOut,
27    Cancelled,
28    /// Never started: a limit was reached or the stuck detector stopped the
29    /// run earlier in the same batch.
30    Skipped,
31    UnknownTool,
32    InvalidArguments,
33    /// The call may or may not have completed: the process stopped while it
34    /// was running. An abandoned run does not re-run it, even if idempotent.
35    Indeterminate,
36    /// A hook answered in the tool's place ([`crate::ToolDecision::Answer`]):
37    /// the tool did not run now. Not a success: no work happened.
38    Answered,
39}
40
41/// The durable record of one tool call. Every call produces exactly one,
42/// refusals included, so "what actually happened" never depends on what the
43/// model says happened.
44#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
45pub struct Receipt {
46    /// The run that made the call. Sessions continued with
47    /// [`crate::RunState::with_user_message`] hold receipts of several runs,
48    /// and turn numbers restart in each run.
49    pub run_id: RunId,
50    pub call_id: String,
51    pub tool: String,
52    pub arguments: Value,
53    pub turn: u32,
54    pub stage: InvocationStage,
55    pub status: ReceiptStatus,
56    pub read_only: bool,
57    pub duration_ms: u64,
58    /// Why it was denied or failed, when known.
59    #[serde(default, skip_serializing_if = "Option::is_none")]
60    pub reason: Option<String>,
61    /// Structured evidence from [`crate::ToolOutput::details`].
62    #[serde(default, skip_serializing_if = "Value::is_null")]
63    pub details: Value,
64    /// Who authored the content this call returned. Kernel-generated text
65    /// (refusals, timeouts, cancellations) is `Trusted`.
66    #[serde(default)]
67    pub source: SourceTrust,
68    /// The run had already read untrusted content when this call was made.
69    #[serde(default)]
70    pub after_untrusted: bool,
71}
72
73impl Receipt {
74    pub fn succeeded(&self) -> bool {
75        self.status == ReceiptStatus::Succeeded
76    }
77}