Skip to main content

agentmux_srv/server/
jekt_entry.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Entry points that hand a jekt to the reactive handler themselves rather
5//! than through `reactive::deliver()`: `/api/bus/inject`, the WebSocket
6//! `bus:inject` message, widget `agents.send` and the chat bridges. Each
7//! readies its request here, so a `transcript_request` body reads
8//! `TIER=sensitive`, with the recipient's own `ESCALATE`, on every entry
9//! point alike (SPEC_JEKT_TRANSCRIPT_REQUEST_TIER_RULES_2026_08_22.md).
10
11use std::sync::Arc;
12
13use super::AppState;
14use crate::backend::reactive::types::{InjectionRequest, InjectionResponse};
15use crate::backend::storage::store::Store;
16
17/// The request `/api/bus/inject` and `bus:inject` deliver: host tier, from
18/// the named sender, readied by [`prepare_host_entry`].
19pub(super) fn bus_inject_request(
20    state: &AppState,
21    from: &str,
22    target: &str,
23    message: &str,
24    priority: Option<String>,
25    audit_source_uid: String,
26) -> InjectionRequest {
27    let mut req = InjectionRequest {
28        target_agent: target.to_string(),
29        message: message.to_string(),
30        source_agent: Some(from.to_string()),
31        priority,
32        delivery_tier: Some("host".to_string()),
33        audit_source_uid,
34        ..Default::default()
35    };
36    prepare_host_entry(state, &mut req);
37    req
38}
39
40/// Readies a host-tier request for the handler, as `deliver()` does: checks
41/// the claimed sender's signature (a sender with a key on file and no valid
42/// signature reads `TRUST=unverified`) and resolves the transcript-request
43/// fields against the recipient's `conversation_visibility`.
44pub(super) fn prepare_host_entry(state: &AppState, req: &mut InjectionRequest) {
45    super::reactive::verify_jekt_signature(state, req);
46    super::reactive::resolve_transcript_request_tier_fields(&state.mstore, req);
47}
48
49/// Whether a request the handler could not deliver may go to the MessageBus
50/// queue instead: only when the target has no registration ("not found"),
51/// and never for a transcript request, since that queue renders no marker
52/// and so no `TIER=sensitive`.
53pub(super) fn bus_fallback_allowed(req: &InjectionRequest, resp: &InjectionResponse) -> bool {
54    let not_found = resp.error.as_deref().is_some_and(|e| e.contains("not found"));
55    not_found && !req.is_transcript_request
56}
57
58/// Delivers a message a chat bridge (Slack, Discord, Telegram, WhatsApp)
59/// received, with its transcript-request fields resolved against this
60/// channel's store first. `raw_text` is the sender's own text, before the
61/// bridge's `[Platform @user]: ` envelope in `req.message`.
62pub(crate) fn inject_from_bridge(mut req: InjectionRequest, raw_text: &str) -> InjectionResponse {
63    resolve_bridge_fields(crate::backend::identity_spawn::row_store(), &mut req, raw_text);
64    crate::backend::reactive::handler::get_global_handler().inject_message(req)
65}
66
67/// Resolves the transcript-request fields of a bridge request from the raw
68/// text the platform delivered, leaving the enveloped `req.message` as the
69/// recipient sees it.
70pub(crate) fn resolve_bridge_fields(store: Option<&Arc<Store>>, req: &mut InjectionRequest, raw_text: &str) {
71    let enveloped = std::mem::replace(&mut req.message, raw_text.to_string());
72    resolve_with_store(store, req);
73    req.message = enveloped;
74}
75
76/// `resolve_transcript_request_tier_fields` for a caller that reaches the
77/// store through the boot-time handle. Before boot attaches one, a
78/// transcript request reads as it does under `ask`: `TIER=sensitive` and
79/// `ESCALATE=required`.
80pub(crate) fn resolve_with_store(store: Option<&Arc<Store>>, req: &mut InjectionRequest) {
81    match store {
82        Some(store) => super::reactive::resolve_transcript_request_tier_fields(store, req),
83        None => {
84            if crate::backend::reactive::transcript_request::parse_transcript_request(&req.message).is_some() {
85                req.is_transcript_request = true;
86                req.transcript_request_escalate_forced = true;
87            }
88        }
89    }
90}
91
92#[cfg(test)]
93#[path = "tests/jekt_entry_tests.rs"]
94mod tests;