Skip to main content

agentmux_srv_core/backend/reactive/
held_request.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A held jekt row turned back into the request it was accepted as, for the replay (`server/jekt_held.rs`, which
5//! re-exports it). It reads only the row and the ack policy, so the reactive tests reach it from the core.
6
7use crate::backend::reactive::types::{InjectionRequest, JektTier};
8use crate::backend::storage::jekt_held::HeldJekt;
9
10/// The request a held row is delivered as: addressed by the target's UID,
11/// with the verdicts it was accepted with — never re-verified (a signature
12/// is valid for five minutes) and never raised — and its original send time.
13pub fn request_from_held(row: &HeldJekt) -> InjectionRequest {
14    let non_empty = |s: &str| (!s.is_empty()).then(|| s.to_string());
15    let mut req = InjectionRequest {
16        target_agent: row.target_uid.clone(),
17        // The row keeps the name the sender addressed; the marker says it
18        // rather than the UID the replay routes by (#31433).
19        marker_target: (!row.target_agent.is_empty() && row.target_agent != row.target_uid)
20            .then(|| crate::backend::reactive::sanitize::route_name(&row.target_agent))
21            .flatten(),
22        message: row.message.clone(),
23        source_agent: non_empty(&row.source_agent),
24        request_id: Some(row.request_id.clone()),
25        priority: non_empty(&row.priority),
26        jekt_tier: JektTier::from_wire(&row.jekt_tier),
27        // Never raised: a message from another machine keeps the tier and proof it was accepted with.
28        delivery_tier: Some(if matches!(row.delivery_tier.as_str(), "lan" | "wan") { row.delivery_tier.clone() } else { "host".to_string() }),
29        ..Default::default()
30    };
31    if row.delivery_tier == "wan" {
32        let wan: serde_json::Value = serde_json::from_str(&row.wan_json).unwrap_or_default();
33        req.wan_instance = serde_json::from_value(wan["instance"].clone()).ok();
34        req.wan_verified = (wan["verified"] == true && req.wan_instance.is_some()).then_some(true);
35    }
36    req.sig_verified = row.sig_verified;
37    // Not restored: reagent verification is WAN-only and no held row carries a
38    // real reagent verdict (a verified WAN peer is held on its `wan_verified`
39    // verdict, restored above), and a replayed `Some(true)` without its key id
40    // would read as a failed verification.
41    req.reagent_verified = None;
42    req.lan_verified = row.lan_verified;
43    req.channel_verified = row.channel_verified;
44    req.is_transcript_request = row.is_transcript_request;
45    req.transcript_request_escalate_forced = row.transcript_request_escalate_forced;
46    req.audit_source_uid = row.audit_source_uid.clone();
47    req.held_sent_at_ms = Some(row.sent_at_ms);
48    // The sender's expiry rides again, so a delivery that ends up waiting
49    // once more (the controller's startup queue, a re-hold) still drops the
50    // message at its own deadline rather than delivering it stale.
51    req.expires_at_ms = row.sender_expires_at_ms;
52    // The row has no `reply` column; srv remembers an hour of `reply: none` ids, which covers most holds.
53    req.reply = crate::backend::ack_policy::asked_for_no_reply(&row.request_id).then(|| "none".to_string());
54    req.files = (!row.files_json.is_empty())
55        .then(|| serde_json::from_str(&row.files_json).ok())
56        .flatten();
57    req
58}