merry-core 0.1.0

Core IDs, schemas, artifacts, events, and error contracts for the Merry agent runtime.
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
//! Serializable trajectory records and their typed payloads.

use crate::{
    ErrorInfo, ToolCallArguments, ToolCallId, ToolName, TrajectoryRecordId,
    trajectory::{
        TrajectoryTurnId,
        serialization::{optional_turn_id_as_string, optional_u64_as_string, u64_as_string},
    },
};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Semantic lane used to render one trajectory record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TrajectoryLane {
    /// User input and other session-owned input.
    Input,
    /// Model output and model lifecycle activity.
    Model,
    /// Tool calls and tool results.
    Tools,
    /// Runtime lifecycle and diagnostic activity.
    System,
}

/// Stable semantic kind for a trajectory record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TrajectoryRecordKind {
    /// User-provided input.
    UserInput,
    /// Assistant/model output.
    AssistantMessage,
    /// A tool call requested by the model.
    ToolCall,
    /// A legacy standalone tool result retained for persisted snapshot compatibility.
    ///
    /// New runtime projections attach tool output to the corresponding `ToolCall`
    /// record instead of emitting this kind separately.
    ToolResult,
    /// Context compaction or a compacted checkpoint.
    Compaction,
    /// A runtime lifecycle or diagnostic record.
    Lifecycle,
}

/// Current lifecycle status of a trajectory record.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TrajectoryRecordStatus {
    /// The runtime has accepted the record but work has not started.
    Pending,
    /// Work represented by the record is in progress.
    Running,
    /// Work completed successfully.
    Succeeded,
    /// Work completed with a failure diagnostic.
    Failed,
    /// Work stopped because cancellation was requested.
    Cancelled,
    /// A lifecycle record is complete without success/failure semantics.
    Completed,
}

/// Kind of exact textual payload retained by the trajectory inspector.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TrajectoryPayloadKind {
    /// UTF-8 tool output or message text.
    Text,
    /// JSON text returned by a tool.
    Json,
}

/// Complete payload projection used by the Web inspector.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TrajectoryPayload {
    kind: TrajectoryPayloadKind,
    content: String,
    truncated: bool,
}

impl TrajectoryPayload {
    /// Creates a payload projection.
    #[must_use]
    pub fn new(kind: TrajectoryPayloadKind, content: String, truncated: bool) -> Self {
        Self {
            kind,
            content,
            truncated,
        }
    }

    /// Returns the payload kind.
    #[must_use]
    pub fn kind(&self) -> TrajectoryPayloadKind {
        self.kind
    }

    /// Borrows the complete payload content.
    #[must_use]
    pub fn content(&self) -> &str {
        &self.content
    }

    /// Returns the legacy truncation marker.
    ///
    /// New runtime projections retain complete content and return `false`.
    #[must_use]
    pub fn truncated(&self) -> bool {
        self.truncated
    }
}

/// Tool-specific evidence attached to one trajectory record.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TrajectoryToolDetails {
    #[schemars(extend("x-merry-output-required" = true))]
    tool_name: Option<ToolName>,
    arguments: ToolCallArguments,
    #[serde(default)]
    #[schemars(extend("x-merry-output-required" = true))]
    arguments_json: String,
    #[schemars(extend("x-merry-output-required" = true))]
    output: Option<TrajectoryPayload>,
}

impl TrajectoryToolDetails {
    /// Creates tool evidence with a registered tool name and arguments.
    #[must_use]
    pub fn new(tool_name: Option<ToolName>, arguments: ToolCallArguments) -> Self {
        let arguments_json = match serde_json::to_string(&arguments) {
            Ok(json) => json,
            Err(_) => "{}".to_owned(),
        };
        Self {
            tool_name,
            arguments,
            arguments_json,
            output: None,
        }
    }

    /// Borrows the registered tool name, when available.
    #[must_use]
    pub fn tool_name(&self) -> Option<&ToolName> {
        self.tool_name.as_ref()
    }

    /// Borrows the exact normalized call arguments.
    #[must_use]
    pub fn arguments(&self) -> &ToolCallArguments {
        &self.arguments
    }

    /// Borrows the exact serialized argument text for lossless inspection.
    #[must_use]
    pub fn arguments_json(&self) -> &str {
        &self.arguments_json
    }

    /// Borrows the complete result payload, when available.
    #[must_use]
    pub fn output(&self) -> Option<&TrajectoryPayload> {
        self.output.as_ref()
    }

    /// Sets or clears the result payload.
    pub fn set_output(&mut self, output: Option<TrajectoryPayload>) {
        self.output = output;
    }
}

/// Detailed evidence for a trajectory record.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum TrajectoryRecordDetails {
    /// No expanded content is available for this record.
    None,
    /// Complete user/assistant text.
    Message {
        /// Content retained for inspection.
        content: String,
        /// Legacy truncation marker retained for snapshot compatibility.
        truncated: bool,
    },
    /// A model tool call and its optional result.
    Tool {
        /// Tool call evidence.
        tool: TrajectoryToolDetails,
    },
}

/// One normalized record in the trajectory read model.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct TrajectoryRecord {
    id: TrajectoryRecordId,
    lane: TrajectoryLane,
    kind: TrajectoryRecordKind,
    label: String,
    #[schemars(extend("x-merry-output-required" = true))]
    summary: Option<String>,
    status: TrajectoryRecordStatus,
    #[serde(with = "u64_as_string")]
    #[schemars(
        with = "String",
        extend(
            "x-merry-wire-type" = "u64",
            "x-merry-output-required" = true
        )
    )]
    start_sequence: u64,
    sequence_order: u32,
    #[serde(with = "optional_turn_id_as_string")]
    #[schemars(
        with = "Option<String>",
        extend(
            "x-merry-wire-type" = "u64",
            "x-merry-output-required" = true
        )
    )]
    turn_id: Option<TrajectoryTurnId>,
    #[serde(with = "optional_u64_as_string")]
    #[schemars(
        with = "Option<String>",
        extend(
            "x-merry-wire-type" = "u64",
            "x-merry-output-required" = true
        )
    )]
    end_sequence: Option<u64>,
    #[schemars(extend("x-merry-output-required" = true))]
    parent_id: Option<TrajectoryRecordId>,
    #[schemars(extend("x-merry-output-required" = true))]
    tool_call_id: Option<ToolCallId>,
    #[schemars(extend("x-merry-output-required" = true))]
    diagnostic: Option<ErrorInfo>,
    #[serde(with = "optional_u64_as_string")]
    #[schemars(
        with = "Option<String>",
        extend(
            "x-merry-wire-type" = "u64",
            "x-merry-output-required" = true
        )
    )]
    started_at_ms: Option<u64>,
    #[serde(with = "optional_u64_as_string")]
    #[schemars(
        with = "Option<String>",
        extend(
            "x-merry-wire-type" = "u64",
            "x-merry-output-required" = true
        )
    )]
    finished_at_ms: Option<u64>,
    details: TrajectoryRecordDetails,
}

impl TrajectoryRecord {
    /// Creates a trajectory record with sequence-based ordering.
    #[must_use]
    pub fn new(
        id: TrajectoryRecordId,
        lane: TrajectoryLane,
        kind: TrajectoryRecordKind,
        label: String,
        status: TrajectoryRecordStatus,
        start_sequence: u64,
    ) -> Self {
        Self {
            id,
            lane,
            kind,
            label,
            summary: None,
            status,
            start_sequence,
            sequence_order: 0,
            turn_id: None,
            end_sequence: None,
            parent_id: None,
            tool_call_id: None,
            diagnostic: None,
            started_at_ms: None,
            finished_at_ms: None,
            details: TrajectoryRecordDetails::None,
        }
    }

    /// Borrows the stable record identifier.
    #[must_use]
    pub fn id(&self) -> &TrajectoryRecordId {
        &self.id
    }

    /// Returns the semantic lane.
    #[must_use]
    pub fn lane(&self) -> TrajectoryLane {
        self.lane
    }

    /// Returns the semantic record kind.
    #[must_use]
    pub fn kind(&self) -> TrajectoryRecordKind {
        self.kind
    }

    /// Borrows the short display label.
    #[must_use]
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Borrows the optional display summary.
    #[must_use]
    pub fn summary(&self) -> Option<&str> {
        self.summary.as_deref()
    }

    /// Returns the current lifecycle status.
    #[must_use]
    pub fn status(&self) -> TrajectoryRecordStatus {
        self.status
    }

    /// Returns the first journal sequence represented by this record.
    #[must_use]
    pub fn start_sequence(&self) -> u64 {
        self.start_sequence
    }

    /// Returns the deterministic order among records sharing a sequence.
    #[must_use]
    pub fn sequence_order(&self) -> u32 {
        self.sequence_order
    }

    /// Returns the logical conversation turn, when the record belongs to one.
    #[must_use]
    pub fn turn_id(&self) -> Option<TrajectoryTurnId> {
        self.turn_id
    }

    /// Returns the final journal sequence when the record is complete.
    #[must_use]
    pub fn end_sequence(&self) -> Option<u64> {
        self.end_sequence
    }

    /// Borrows the optional parent record identifier.
    #[must_use]
    pub fn parent_id(&self) -> Option<&TrajectoryRecordId> {
        self.parent_id.as_ref()
    }

    /// Borrows the optional provider-originated tool call identifier.
    #[must_use]
    pub fn tool_call_id(&self) -> Option<&ToolCallId> {
        self.tool_call_id.as_ref()
    }

    /// Borrows the optional failure diagnostic.
    #[must_use]
    pub fn diagnostic(&self) -> Option<&ErrorInfo> {
        self.diagnostic.as_ref()
    }

    /// Returns the optional real start timestamp. Missing means timing is unknown.
    #[must_use]
    pub fn started_at_ms(&self) -> Option<u64> {
        self.started_at_ms
    }

    /// Returns the optional real finish timestamp. Missing means timing is unknown.
    #[must_use]
    pub fn finished_at_ms(&self) -> Option<u64> {
        self.finished_at_ms
    }

    /// Borrows expanded evidence for this record.
    #[must_use]
    pub fn details(&self) -> &TrajectoryRecordDetails {
        &self.details
    }

    /// Sets a bounded display summary.
    pub fn set_summary(&mut self, summary: Option<String>) {
        self.summary = summary;
    }

    /// Replaces the short display label.
    pub fn set_label(&mut self, label: String) {
        self.label = label;
    }

    /// Stores complete message content for inspection.
    pub fn set_message_details(&mut self, content: String, truncated: bool) {
        self.details = TrajectoryRecordDetails::Message { content, truncated };
    }

    /// Stores a normalized tool call and its registered tool name.
    pub fn set_tool_details(&mut self, tool_name: Option<ToolName>, arguments: ToolCallArguments) {
        self.details = TrajectoryRecordDetails::Tool {
            tool: TrajectoryToolDetails::new(tool_name, arguments),
        };
    }

    /// Sets the deterministic order among records sharing a source sequence.
    pub fn set_sequence_order(&mut self, sequence_order: u32) {
        self.sequence_order = sequence_order;
    }

    /// Reassigns the record to its durable source sequence during replay.
    pub fn set_start_sequence(&mut self, sequence: u64) {
        self.start_sequence = sequence;
    }

    /// Associates the record with a logical conversation turn.
    pub fn set_turn_id(&mut self, turn_id: Option<TrajectoryTurnId>) {
        self.turn_id = turn_id;
    }

    /// Updates the output inside existing tool details.
    pub fn set_tool_output(&mut self, output: Option<TrajectoryPayload>) {
        if let TrajectoryRecordDetails::Tool { tool } = &mut self.details {
            tool.set_output(output);
        }
    }

    /// Marks the record with a new status and ending sequence.
    pub fn finish(&mut self, status: TrajectoryRecordStatus, end_sequence: u64) {
        self.status = status;
        self.end_sequence = Some(end_sequence);
    }

    /// Associates the record with a parent and/or tool call.
    pub fn set_relationship(
        &mut self,
        parent_id: Option<TrajectoryRecordId>,
        tool_call_id: Option<ToolCallId>,
    ) {
        self.parent_id = parent_id;
        self.tool_call_id = tool_call_id;
    }

    /// Stores a diagnostic and marks the record as failed.
    pub fn fail(&mut self, diagnostic: ErrorInfo, end_sequence: u64) {
        self.diagnostic = Some(diagnostic);
        self.finish(TrajectoryRecordStatus::Failed, end_sequence);
    }
}