meerkat-core 0.8.21

Core agent logic for Meerkat (no I/O deps)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Small, store-issued session-persistence carriers.
//!
//! These values cross the core/runtime boundary without entering [`Session`].
//! They name provisional physical state only; a store must compare every field
//! with its exact current rows before promotion.

use crate::error::AgentError;
use crate::lifecycle::RunId;
use crate::types::SessionId;
use async_trait::async_trait;

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ProvisionalTailAuthorityError {
    #[error("provisional tail for session {session_id} has invalid physical identity: {detail}")]
    Invalid {
        session_id: SessionId,
        detail: String,
    },
}

/// Store-issued identity of one uncommitted WholeBlob candidate.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct WholeBlobProvisionalTailAuthority {
    session_id: SessionId,
    base_store_revision: u64,
    base_blob_sha256: String,
    run_id: RunId,
    candidate_blob_sha256: String,
    candidate_sequence: u64,
}

impl WholeBlobProvisionalTailAuthority {
    pub fn issued(
        session_id: SessionId,
        base_store_revision: u64,
        base_blob_sha256: String,
        run_id: RunId,
        candidate_blob_sha256: String,
        candidate_sequence: u64,
    ) -> Result<Self, ProvisionalTailAuthorityError> {
        if base_store_revision == 0
            || base_blob_sha256.is_empty()
            || candidate_blob_sha256.is_empty()
            || candidate_sequence == 0
        {
            return Err(ProvisionalTailAuthorityError::Invalid {
                session_id,
                detail: "WholeBlob authority requires a nonzero base revision and sequence plus nonempty base/candidate digests".to_string(),
            });
        }
        Ok(Self {
            session_id,
            base_store_revision,
            base_blob_sha256,
            run_id,
            candidate_blob_sha256,
            candidate_sequence,
        })
    }

    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn base_store_revision(&self) -> u64 {
        self.base_store_revision
    }

    #[must_use]
    pub fn base_blob_sha256(&self) -> &str {
        &self.base_blob_sha256
    }

    #[must_use]
    pub fn run_id(&self) -> &RunId {
        &self.run_id
    }

    #[must_use]
    pub fn candidate_blob_sha256(&self) -> &str {
        &self.candidate_blob_sha256
    }

    #[must_use]
    pub const fn candidate_sequence(&self) -> u64 {
        self.candidate_sequence
    }
}

/// Store-issued identity of one uncommitted HeadCanonical physical tail.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HeadCanonicalProvisionalTailAuthority {
    authority_version: u16,
    session_id: SessionId,
    base_store_revision: u64,
    base_committed_head_token: String,
    physical_store_revision: u64,
    physical_head_token: String,
    run_id: RunId,
    candidate_sequence: u64,
}

impl HeadCanonicalProvisionalTailAuthority {
    pub const VERSION: u16 = 1;

    #[allow(clippy::too_many_arguments)]
    pub fn issued(
        session_id: SessionId,
        base_store_revision: u64,
        base_committed_head_token: String,
        physical_store_revision: u64,
        physical_head_token: String,
        run_id: RunId,
        candidate_sequence: u64,
    ) -> Result<Self, ProvisionalTailAuthorityError> {
        let expected_physical_revision = base_store_revision
            .checked_add(candidate_sequence)
            .filter(|_| candidate_sequence != 0);
        if base_store_revision == 0
            || expected_physical_revision != Some(physical_store_revision)
            || base_committed_head_token.is_empty()
            || physical_head_token.is_empty()
            || base_committed_head_token == physical_head_token
        {
            return Err(ProvisionalTailAuthorityError::Invalid {
                session_id,
                detail: "HeadCanonical authority requires an exact committed base, a nonzero contiguous candidate sequence, and a distinct matching physical revision/token".to_string(),
            });
        }
        Ok(Self {
            authority_version: Self::VERSION,
            session_id,
            base_store_revision,
            base_committed_head_token,
            physical_store_revision,
            physical_head_token,
            run_id,
            candidate_sequence,
        })
    }

    #[must_use]
    pub const fn authority_version(&self) -> u16 {
        self.authority_version
    }

    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn base_store_revision(&self) -> u64 {
        self.base_store_revision
    }

    #[must_use]
    pub fn base_committed_head_token(&self) -> &str {
        &self.base_committed_head_token
    }

    #[must_use]
    pub const fn physical_store_revision(&self) -> u64 {
        self.physical_store_revision
    }

    #[must_use]
    pub fn physical_head_token(&self) -> &str {
        &self.physical_head_token
    }

    #[must_use]
    pub fn run_id(&self) -> &RunId {
        &self.run_id
    }

    #[must_use]
    pub const fn candidate_sequence(&self) -> u64 {
        self.candidate_sequence
    }
}

/// Profile-specific authority for one successful provisional physical write.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RunCheckpointAuthority {
    WholeBlob(WholeBlobProvisionalTailAuthority),
    HeadCanonical(HeadCanonicalProvisionalTailAuthority),
}

impl RunCheckpointAuthority {
    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        match self {
            Self::WholeBlob(authority) => authority.session_id(),
            Self::HeadCanonical(authority) => authority.session_id(),
        }
    }

    #[must_use]
    pub fn run_id(&self) -> &RunId {
        match self {
            Self::WholeBlob(authority) => authority.run_id(),
            Self::HeadCanonical(authority) => authority.run_id(),
        }
    }

    #[must_use]
    pub const fn candidate_sequence(&self) -> u64 {
        match self {
            Self::WholeBlob(authority) => authority.candidate_sequence(),
            Self::HeadCanonical(authority) => authority.candidate_sequence(),
        }
    }

    #[must_use]
    pub fn whole_blob(&self) -> Option<&WholeBlobProvisionalTailAuthority> {
        match self {
            Self::WholeBlob(authority) => Some(authority),
            Self::HeadCanonical(_) => None,
        }
    }

    #[must_use]
    pub fn head_canonical(&self) -> Option<&HeadCanonicalProvisionalTailAuthority> {
        match self {
            Self::WholeBlob(_) => None,
            Self::HeadCanonical(authority) => Some(authority),
        }
    }
}

/// Latest successful provisional physical write for one active run.
///
/// The store binds the profile authority to the exact candidate's bounded
/// logical facts when it mints the receipt. Promotion must revalidate all three
/// against the same physical candidate; the authority alone is insufficient.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunCheckpointReceipt {
    authority: RunCheckpointAuthority,
    conversation_digest: String,
    message_count: u64,
}

impl RunCheckpointReceipt {
    pub fn issued(
        authority: RunCheckpointAuthority,
        conversation_digest: String,
        message_count: u64,
    ) -> Result<Self, ProvisionalTailAuthorityError> {
        let session_id = authority.session_id().clone();
        if conversation_digest.is_empty() {
            return Err(ProvisionalTailAuthorityError::Invalid {
                session_id,
                detail: "run checkpoint receipt requires a nonempty conversation digest"
                    .to_string(),
            });
        }
        if usize::try_from(message_count).is_err() {
            return Err(ProvisionalTailAuthorityError::Invalid {
                session_id,
                detail: "run checkpoint receipt message count exceeds the host index range"
                    .to_string(),
            });
        }
        Ok(Self {
            authority,
            conversation_digest,
            message_count,
        })
    }

    #[must_use]
    pub fn authority(&self) -> &RunCheckpointAuthority {
        &self.authority
    }

    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        self.authority.session_id()
    }

    #[must_use]
    pub fn run_id(&self) -> &RunId {
        self.authority.run_id()
    }

    #[must_use]
    pub const fn candidate_sequence(&self) -> u64 {
        self.authority.candidate_sequence()
    }

    #[must_use]
    pub fn conversation_digest(&self) -> &str {
        &self.conversation_digest
    }

    #[must_use]
    pub const fn message_count(&self) -> u64 {
        self.message_count
    }

    #[must_use]
    pub fn whole_blob(&self) -> Option<&WholeBlobProvisionalTailAuthority> {
        self.authority.whole_blob()
    }

    #[must_use]
    pub fn head_canonical(&self) -> Option<&HeadCanonicalProvisionalTailAuthority> {
        self.authority.head_canonical()
    }
}

/// Periodic session persistence hook.
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait SessionCheckpointer: Send + Sync {
    /// Rebind the actor's fixed-size committed base after a runtime-owned
    /// control-only store mutation.
    ///
    /// The receipt is minted from the store outcome that committed the
    /// control mutation. Implementations must confirm that exact authority;
    /// they must not reload or compare the accumulated Session document.
    async fn acknowledge_control_commit(
        &self,
        _receipt: &SessionControlCommitReceipt,
    ) -> Result<(), AgentError> {
        Err(AgentError::InternalError(
            "session checkpointer cannot acknowledge runtime control commits".to_string(),
        ))
    }

    /// Persist one in-run physical successor.
    ///
    /// `previous` is the exact last successful receipt for this session/run.
    /// The actor removes its retained copy before awaiting and installs only a
    /// successfully returned successor, so cancellation or error cannot promote
    /// stale physical state.
    async fn checkpoint_run(
        &self,
        session: &mut crate::Session,
        run_id: &RunId,
        previous: Option<&RunCheckpointReceipt>,
    ) -> Result<Option<RunCheckpointReceipt>, AgentError>;
}

/// Fixed-size store authority returned by a runtime-owned control mutation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SessionControlCommitReceipt {
    session_id: SessionId,
    store_revision: u64,
    authority_token: String,
}

impl SessionControlCommitReceipt {
    pub fn new(
        session_id: SessionId,
        store_revision: u64,
        authority_token: impl Into<String>,
    ) -> Result<Self, String> {
        let authority_token = authority_token.into();
        if store_revision == 0 {
            return Err("session control commit revision must be non-zero".to_string());
        }
        if authority_token.is_empty() {
            return Err("session control commit authority token must be non-empty".to_string());
        }
        Ok(Self {
            session_id,
            store_revision,
            authority_token,
        })
    }

    #[must_use]
    pub fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    #[must_use]
    pub const fn store_revision(&self) -> u64 {
        self.store_revision
    }

    #[must_use]
    pub fn authority_token(&self) -> &str {
        &self.authority_token
    }
}

#[cfg(test)]
#[allow(clippy::expect_used)]
mod tests {
    use super::*;

    #[test]
    fn head_canonical_authority_requires_exact_base_plus_candidate_sequence() {
        let session_id = SessionId::new();
        let run_id = RunId::new();
        let issued = HeadCanonicalProvisionalTailAuthority::issued(
            session_id.clone(),
            41,
            "committed".to_string(),
            44,
            "physical".to_string(),
            run_id.clone(),
            3,
        )
        .expect("base 41 plus sequence 3 is physical revision 44");
        assert_eq!(issued.physical_store_revision(), 44);
        assert_eq!(issued.candidate_sequence(), 3);

        for (physical_revision, physical_token, sequence) in [
            (43, "physical", 3),
            (44, "physical", 0),
            (44, "", 3),
            (44, "committed", 3),
        ] {
            assert!(
                HeadCanonicalProvisionalTailAuthority::issued(
                    session_id.clone(),
                    41,
                    "committed".to_string(),
                    physical_revision,
                    physical_token.to_string(),
                    run_id.clone(),
                    sequence,
                )
                .is_err()
            );
        }
        assert!(
            HeadCanonicalProvisionalTailAuthority::issued(
                session_id,
                41,
                String::new(),
                44,
                "physical".to_string(),
                run_id,
                3,
            )
            .is_err()
        );
    }

    #[test]
    fn run_checkpoint_receipt_binds_authority_digest_and_count() {
        let authority = WholeBlobProvisionalTailAuthority::issued(
            SessionId::new(),
            7,
            "base-blob".to_string(),
            RunId::new(),
            "candidate-blob".to_string(),
            1,
        )
        .expect("valid WholeBlob authority");
        let receipt = RunCheckpointReceipt::issued(
            RunCheckpointAuthority::WholeBlob(authority),
            "conversation".to_string(),
            9,
        )
        .expect("bounded candidate facts");

        assert!(receipt.whole_blob().is_some());
        assert!(receipt.head_canonical().is_none());
        assert_eq!(receipt.conversation_digest(), "conversation");
        assert_eq!(receipt.message_count(), 9);
        assert_eq!(receipt.candidate_sequence(), 1);
    }

    #[test]
    fn run_checkpoint_receipt_rejects_empty_conversation_digest() {
        let authority = WholeBlobProvisionalTailAuthority::issued(
            SessionId::new(),
            7,
            "base-blob".to_string(),
            RunId::new(),
            "candidate-blob".to_string(),
            1,
        )
        .expect("valid WholeBlob authority");

        assert!(
            RunCheckpointReceipt::issued(
                RunCheckpointAuthority::WholeBlob(authority),
                String::new(),
                9,
            )
            .is_err()
        );
    }

    #[test]
    fn session_control_commit_receipt_requires_bounded_store_authority() {
        let session_id = SessionId::new();
        let receipt = SessionControlCommitReceipt::new(session_id.clone(), 7, "row-sha256:exact")
            .expect("non-zero revision and non-empty token should be accepted");
        assert_eq!(receipt.session_id(), &session_id);
        assert_eq!(receipt.store_revision(), 7);
        assert_eq!(receipt.authority_token(), "row-sha256:exact");

        assert!(
            SessionControlCommitReceipt::new(session_id.clone(), 0, "row-sha256:exact").is_err()
        );
        assert!(SessionControlCommitReceipt::new(session_id, 7, "").is_err());
    }
}