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
use std::{fmt, sync::Arc};
use runifold_core::{
CapabilityId, Checkpoint, CheckpointError, CheckpointErrorKind, CheckpointId, CheckpointStore,
RunContext, Usage,
};
use runifold_model::{Message, ModelRef, ModelResponse};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::conversation::{ConversationId, ConversationVersion, MemoryNamespace};
use crate::{
AgentError, AgentOutcome, TerminalRequirementFailure, TerminalReviewPolicy,
TerminalReviewerDescriptor, TurnReviewPolicy,
};
const CHECKPOINT_KIND: &str = "runifold.agent";
const CHECKPOINT_SCHEMA_VERSION: u32 = 1;
/// Recovery behavior for a checkpoint captured during an external operation.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum ResumePolicy {
/// Reject recovery that could duplicate model cost or external effects.
#[default]
RejectAmbiguous,
/// Explicitly retry an interrupted model-and-callable turn, internal turn
/// review, or terminal review. Durable review candidates and approved turn
/// plans are reused without regenerating them.
RetryInterruptedTurn,
}
/// Persisted Agent execution phase.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "state", rename_all = "snake_case")]
#[non_exhaustive]
pub enum AgentCheckpointPhase {
/// Transcript is stable and ready for the next model turn.
ReadyForTurn,
/// A model-and-callable turn may have partially executed.
TurnInFlight {
/// One-based turn number that may have partially executed.
turn: u32,
},
/// The Agent reached a terminal response.
Completed {
/// Final canonical model response.
response: Box<ModelResponse>,
},
/// A terminal candidate failed its completion contract and cannot be
/// repaired under the configured policy.
TerminalRequirementFailed {
/// Safe failure details retained without the generated body.
failure: TerminalRequirementFailure,
/// Repair turns completed before exhaustion.
attempts: u32,
},
/// A selected model response is durable and has not entered internal
/// turn review.
TurnReviewReady {
/// Response awaiting review before it can affect execution.
response: Box<ModelResponse>,
/// One-based model turn that produced the response.
turn: u32,
},
/// Internal review of a durable model response may have partially
/// executed.
TurnReviewInFlight {
/// Response supplied to the reviewer.
response: Box<ModelResponse>,
/// One-based model turn that produced the response.
turn: u32,
},
/// The response passed review and is durable while its tool plan is
/// applied or its terminal path is completed.
TurnReviewApproved {
/// Approved response reused during recovery without regeneration.
response: Box<ModelResponse>,
/// One-based model turn that produced the response.
turn: u32,
},
/// An internal reviewer permanently rejected a model response.
TurnReviewRejected {
/// Response rejected by the reviewer.
response: Box<ModelResponse>,
/// One-based model turn that produced the response.
turn: u32,
/// Safe reviewer explanation.
reason: String,
/// Internal review repairs completed before rejection.
attempts: u32,
},
/// A turn repair verdict exceeded the configured review repair limit.
TurnReviewExhausted {
/// Last response that required another repair.
response: Box<ModelResponse>,
/// One-based model turn that produced the response.
turn: u32,
/// Last bounded feedback returned by the reviewer.
feedback: Value,
/// Internal review repairs completed before exhaustion.
attempts: u32,
},
/// A locally valid candidate is durable and has not entered review.
TerminalReviewReady {
/// Candidate awaiting semantic review.
response: Box<ModelResponse>,
/// One-based semantic review attempt.
attempt: u32,
},
/// Semantic review of a durable candidate may have partially executed.
TerminalReviewInFlight {
/// Candidate supplied to the reviewer.
response: Box<ModelResponse>,
/// One-based semantic review attempt.
attempt: u32,
},
/// A reviewer permanently rejected a terminal candidate.
TerminalReviewRejected {
/// Candidate rejected by the reviewer.
response: Box<ModelResponse>,
/// Safe reviewer explanation.
reason: String,
/// Review repairs completed before rejection.
attempts: u32,
},
/// A repair verdict exceeded the configured review repair limit.
TerminalReviewExhausted {
/// Last candidate that required another repair.
response: Box<ModelResponse>,
/// Last bounded feedback returned by the reviewer.
feedback: Value,
/// Review repairs completed before exhaustion.
attempts: u32,
},
}
/// Conversation commit preconditions carried through crash recovery.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct DurableConversationCheckpoint {
/// Conversation receiving the completed turn.
pub conversation_id: ConversationId,
/// Isolation namespace loaded before execution.
pub namespace: MemoryNamespace,
/// Transcript version loaded before execution.
pub expected_version: ConversationVersion,
/// Number of leading runtime-only messages excluded from persistence.
pub persisted_prefix_len: u64,
}
/// Versioned Agent state stored in a domain-neutral checkpoint envelope.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct AgentCheckpointState {
/// Stable logical execution identity used for callable idempotency.
pub execution_id: String,
/// Agent identity expected during recovery.
pub agent: String,
/// Model identity expected during recovery.
pub model: ModelRef,
/// Canonical transcript at the last stable boundary.
pub transcript: Vec<Message>,
/// Completed model turns.
pub turns: u32,
/// Completed local tool attempts.
pub tool_calls: u32,
/// Completed successful delegations.
pub delegations: u32,
/// Shared usage snapshot at persistence time.
pub usage: Usage,
/// Stable internal-turn reviewer identity expected throughout recovery.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub turn_reviewer: Option<TerminalReviewerDescriptor>,
/// Internal-turn review scope and repair limit expected during recovery.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub turn_review_policy: Option<TurnReviewPolicy>,
/// Stable capabilities delegated to the internal-turn reviewer.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub turn_reviewer_capabilities: Vec<CapabilityId>,
/// Stable reviewer identity expected throughout recovery.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub terminal_reviewer: Option<TerminalReviewerDescriptor>,
/// Terminal-review repair limit expected during recovery.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub terminal_review_policy: Option<TerminalReviewPolicy>,
/// Stable capabilities delegated to the terminal reviewer.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub terminal_reviewer_capabilities: Vec<CapabilityId>,
/// Current recovery phase.
pub phase: AgentCheckpointPhase,
/// Atomic conversation commit metadata, when this is a durable turn.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub durable_conversation: Option<DurableConversationCheckpoint>,
}
impl AgentCheckpointState {
pub(crate) fn outcome(&self) -> Option<AgentOutcome> {
match &self.phase {
AgentCheckpointPhase::Completed { response } => Some(AgentOutcome {
response: response.as_ref().clone(),
transcript: self.transcript.clone(),
turns: self.turns,
tool_calls: self.tool_calls,
delegations: self.delegations,
usage: self.usage,
}),
_ => None,
}
}
pub(crate) fn terminal_failure(&self) -> Option<AgentError> {
match &self.phase {
AgentCheckpointPhase::TerminalRequirementFailed {
failure, attempts, ..
} => Some(super::agent::completion::failure_error(failure, *attempts)),
AgentCheckpointPhase::TerminalReviewRejected { reason, .. } => {
Some(AgentError::TerminalReviewRejected {
reason: reason.clone(),
})
}
AgentCheckpointPhase::TerminalReviewExhausted { attempts, .. } => {
Some(AgentError::TerminalReviewExhausted {
attempts: *attempts,
})
}
AgentCheckpointPhase::TurnReviewRejected { reason, .. } => {
Some(AgentError::TurnReviewRejected {
reason: reason.clone(),
})
}
AgentCheckpointPhase::TurnReviewExhausted { attempts, .. } => {
Some(AgentError::TurnReviewExhausted {
attempts: *attempts,
})
}
_ => None,
}
}
}
/// Stable handle binding one checkpoint identity to a store.
#[derive(Clone)]
pub struct AgentCheckpoint {
id: CheckpointId,
store: Arc<dyn CheckpointStore>,
}
impl AgentCheckpoint {
/// Creates a new checkpoint handle with a unique identity.
pub fn new(store: Arc<dyn CheckpointStore>) -> Self {
Self {
id: CheckpointId::new(),
store,
}
}
/// Reconnects to an existing checkpoint identity.
pub fn existing(id: CheckpointId, store: Arc<dyn CheckpointStore>) -> Self {
Self { id, store }
}
/// Returns the stable checkpoint identity.
pub const fn id(&self) -> CheckpointId {
self.id
}
/// Loads and validates the latest typed Agent state.
///
/// # Errors
///
/// Returns [`CheckpointError`] when storage or payload validation fails.
pub fn load(&self) -> Result<(Checkpoint, AgentCheckpointState), CheckpointError> {
let checkpoint = self.store.load(self.id)?;
if checkpoint.kind != CHECKPOINT_KIND
|| checkpoint.schema_version != CHECKPOINT_SCHEMA_VERSION
{
return Err(CheckpointError::new(
CheckpointErrorKind::InvalidPayload,
"checkpoint kind or schema version is not supported",
));
}
let state = serde_json::from_value(checkpoint.payload.clone()).map_err(|error| {
CheckpointError::new(CheckpointErrorKind::InvalidPayload, error.to_string())
})?;
Ok((checkpoint, state))
}
}
impl fmt::Debug for AgentCheckpoint {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("AgentCheckpoint")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
pub(crate) struct CheckpointCursor {
handle: AgentCheckpoint,
envelope: Checkpoint,
}
impl CheckpointCursor {
pub(crate) fn create(
handle: &AgentCheckpoint,
run: &RunContext,
state: &AgentCheckpointState,
) -> Result<Self, AgentError> {
let payload = serialize(state)?;
let envelope = Checkpoint::initial(
handle.id,
run.run_id(),
CHECKPOINT_KIND,
CHECKPOINT_SCHEMA_VERSION,
payload,
);
handle.store.compare_and_swap(&envelope, None)?;
Ok(Self {
handle: handle.clone(),
envelope,
})
}
pub(crate) fn loaded(handle: &AgentCheckpoint, envelope: Checkpoint) -> Self {
Self {
handle: handle.clone(),
envelope,
}
}
pub(crate) fn save(&mut self, state: &AgentCheckpointState) -> Result<(), AgentError> {
let next = self.envelope.next(serialize(state)?)?;
self.handle
.store
.compare_and_swap(&next, Some(self.envelope.revision))?;
self.envelope = next;
Ok(())
}
pub(crate) fn next(&self, state: &AgentCheckpointState) -> Result<Checkpoint, AgentError> {
self.envelope.next(serialize(state)?).map_err(Into::into)
}
pub(crate) const fn revision(&self) -> u64 {
self.envelope.revision
}
pub(crate) const fn id(&self) -> CheckpointId {
self.envelope.id
}
}
fn serialize(state: &AgentCheckpointState) -> Result<serde_json::Value, AgentError> {
serde_json::to_value(state).map_err(|error| {
CheckpointError::new(CheckpointErrorKind::InvalidPayload, error.to_string()).into()
})
}