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
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::state::RunId;
use crate::tool::SourceTrust;
/// How far a call got before its outcome was decided.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum InvocationStage {
/// Refused before the tool ran (unknown tool, bad arguments, policy,
/// or skipped because the run had to stop).
RejectedBeforeDispatch,
/// The tool's code was invoked.
Dispatched,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReceiptStatus {
Succeeded,
/// Ran and reported failure, or returned a [`crate::ToolError`].
Failed,
/// A hook or the user refused it.
Denied,
TimedOut,
Cancelled,
/// Never started: a limit was reached or the stuck detector stopped the
/// run earlier in the same batch.
Skipped,
UnknownTool,
InvalidArguments,
/// The call may or may not have completed: the process stopped while it
/// was running. An abandoned run does not re-run it, even if idempotent.
Indeterminate,
/// A hook answered in the tool's place ([`crate::ToolDecision::Answer`]):
/// the tool did not run now. Not a success: no work happened.
Answered,
}
/// The durable record of one tool call. Every call produces exactly one,
/// refusals included, so "what actually happened" never depends on what the
/// model says happened.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Receipt {
/// The run that made the call. Sessions continued with
/// [`crate::RunState::with_user_message`] hold receipts of several runs,
/// and turn numbers restart in each run.
pub run_id: RunId,
pub call_id: String,
pub tool: String,
pub arguments: Value,
pub turn: u32,
pub stage: InvocationStage,
pub status: ReceiptStatus,
pub read_only: bool,
pub duration_ms: u64,
/// Why it was denied or failed, when known.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
/// Structured evidence from [`crate::ToolOutput::details`].
#[serde(default, skip_serializing_if = "Value::is_null")]
pub details: Value,
/// Who authored the content this call returned. Kernel-generated text
/// (refusals, timeouts, cancellations) is `Trusted`.
#[serde(default)]
pub source: SourceTrust,
/// The run had already read untrusted content when this call was made.
#[serde(default)]
pub after_untrusted: bool,
}
impl Receipt {
pub fn succeeded(&self) -> bool {
self.status == ReceiptStatus::Succeeded
}
}