agentmux_srv_core/backend/blockcontroller/persistent/queued.rs
1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! The two kinds of message the persistent controller holds before it writes
5//! them to the process: the human path's queue around a spawn
6//! ([`QueuedMessage`]) and the deliveries waiting for the process to take a
7//! write ([`Deferred`]). Each carries where the message came from, for the
8//! record written when it is delivered.
9
10use crate::backend::message_origin::RecordOrigin;
11
12/// A single entry in `pending_send_messages`: the formatted stdin JSON
13/// payload plus whether it's already been persisted to the blockfile.
14/// codex P2 on PR #2360 (sixth review pass, round 7): a stale-resume
15/// retry's redelivery has already been correctly persisted on its
16/// original (failed) attempt — see `retry_after_resume_failure`'s own doc
17/// comment — but the drain used to persist EVERY message it delivers
18/// unconditionally, double-persisting a replayed retry batch into the
19/// blockfile transcript.
20#[derive(Clone, Debug)]
21pub(super) struct QueuedMessage {
22 /// Queue identity (issue #2365): assigned once from
23 /// `PersistentInner::next_message_seq` at first enqueue and preserved
24 /// verbatim across redelivery (`QueuedRetryEntry::seq` carries it
25 /// through a stale-resume retry batch), so dedup / own-message /
26 /// seed checks are exact identity — two genuinely different messages
27 /// with identical text can never be conflated.
28 pub(super) seq: u64,
29 pub(super) json_str: String,
30 pub(super) already_persisted: bool,
31 /// Where the message came from, for the record written when it is
32 /// delivered. `None` only for an `already_persisted` entry, whose record
33 /// was written on its first attempt.
34 pub(super) record: Option<RecordOrigin>,
35}
36
37impl QueuedMessage {
38 pub(super) fn fresh(seq: u64, json_str: String, record: RecordOrigin) -> Self {
39 Self { seq, json_str, already_persisted: false, record: Some(record) }
40 }
41
42 pub(super) fn already_persisted(seq: u64, json_str: String) -> Self {
43 Self { seq, json_str, already_persisted: true, record: None }
44 }
45}
46
47impl PartialEq<str> for QueuedMessage {
48 fn eq(&self, other: &str) -> bool {
49 self.json_str == other
50 }
51}
52
53impl PartialEq<&str> for QueuedMessage {
54 fn eq(&self, other: &&str) -> bool {
55 self.json_str == *other
56 }
57}
58
59impl PartialEq<String> for QueuedMessage {
60 fn eq(&self, other: &String) -> bool {
61 self.json_str == *other
62 }
63}
64
65/// A message waiting in `PersistentInner::deferred_deliveries`: the encoded
66/// stream-json stdin `line` and where the message came from, which the record
67/// of it carries when it is written (`message_origin`).
68#[derive(Clone, Debug)]
69pub(super) struct Deferred {
70 pub(super) line: String,
71 pub(super) record: RecordOrigin,
72 /// The sender's expiry, epoch ms (issue #3894): past it, the message is
73 /// dropped at write time with one log line instead of written to the
74 /// process — the wait for the process to take a write outlived the time
75 /// the message was worth anything. `None` keeps today's behaviour: a
76 /// queued message is always written.
77 pub(super) expires_at_ms: Option<i64>,
78 /// The `[AgentMux]` notes the message carries, with their keys: if the
79 /// message is dropped, they are held again for the next one instead of
80 /// going with it (issue #3894).
81 pub(super) notes: Vec<(String, String)>,
82}
83
84impl std::ops::Deref for Deferred {
85 type Target = str;
86 fn deref(&self) -> &str {
87 &self.line
88 }
89}
90
91#[cfg(test)]
92impl From<String> for Deferred {
93 /// A test's queued line, from the human.
94 fn from(line: String) -> Self {
95 Deferred {
96 line,
97 record: RecordOrigin::new(
98 crate::backend::message_origin::MessageOrigin::user(),
99 ),
100 expires_at_ms: None,
101 notes: Vec::new(),
102 }
103 }
104}