Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

refactor(rpc): give the log envelope a single definition

M3gA-Mind118a62708be3
diff --git a/src/core/types.rs b/src/core/types.rs
index 277db68b5a..3378d117b7 100644
--- a/src/core/types.rs
+++ b/src/core/types.rs
@@ -4,7 +4,6 @@
//! responses, as well as maintaining application state across subsystems.
use serde::{Deserialize, Serialize};
-use serde_json::json;
/// Standard response structure for commands that include execution logs.
///
@@ -65,11 +64,12 @@ impl InvocationResult {
/// - `logs.is_empty()` -> `inv.value`
/// - `!logs.is_empty()` -> `{ "result": inv.value, "logs": inv.logs }`
pub fn invocation_to_rpc_json(inv: InvocationResult) -> serde_json::Value {
- if inv.logs.is_empty() {
- inv.value
- } else {
- json!({ "result": inv.value, "logs": inv.logs })
- }
+ // Delegates rather than repeating the rule. This function and
+ // `RpcOutcome::into_cli_compatible_json` are the two ways a controller
+ // result reaches a caller, and they carried independent copies of the same
+ // six lines — so a fix to one would have silently left the other on the old
+ // shape. See `crate::rpc::apply_log_envelope` (#6080).
+ crate::rpc::apply_log_envelope(inv.value, inv.logs)
}
/// Standard JSON-RPC 2.0 request format.
@@ -288,6 +288,48 @@ mod tests {
assert_eq!(json["logs"][0], "info");
}
+ /// The two controller return paths must produce byte-identical JSON for the
+ /// same (value, logs) pair.
+ ///
+ /// `RpcOutcome::into_cli_compatible_json` (the registry path, 152 call
+ /// sites) and `invocation_to_rpc_json` (the dynamic-dispatch path) used to
+ /// carry independent copies of the same envelope rule. Nothing linked them,
+ /// so a fix or a normalisation applied to one would have left the other
+ /// answering the old shape — the divergence would have been invisible until
+ /// a caller hit the wrong path. Both now delegate to
+ /// `rpc::apply_log_envelope`; this asserts they still agree, so a future
+ /// edit cannot re-fork them without failing here.
+ ///
+ /// It deliberately asserts *agreement*, not a particular shape: the shape
+ /// itself is the open question in #6080, and pinning it here would enshrine
+ /// the defect as intended behaviour.
+ #[test]
+ fn both_controller_return_paths_apply_the_same_log_envelope() {
+ for (value, logs) in [
+ (json!({ "data": true }), Vec::<String>::new()),
+ (json!({ "data": true }), vec!["one".to_string()]),
+ (json!(null), vec!["log on a null value".to_string()]),
+ (json!([1, 2, 3]), Vec::<String>::new()),
+ // A value that already carries a `result` key: the envelope must
+ // treat it as opaque data, not as an envelope to flatten.
+ (json!({ "result": "inner", "logs": ["not mine"] }), vec![]),
+ ] {
+ let via_dispatch = invocation_to_rpc_json(InvocationResult {
+ value: value.clone(),
+ logs: logs.clone(),
+ });
+ let via_registry = crate::rpc::RpcOutcome::new(value.clone(), logs.clone())
+ .into_cli_compatible_json()
+ .expect("a serde_json::Value always serializes");
+
+ assert_eq!(
+ via_dispatch, via_registry,
+ "the dispatch and registry paths disagreed for value={value} logs={logs:?}; \
+ both must go through rpc::apply_log_envelope"
+ );
+ }
+ }
+
#[test]
fn command_response_serde_roundtrip() {
let resp = CommandResponse {
diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs
index 58ac926cf7..77465c3049 100644
--- a/src/rpc/mod.rs
+++ b/src/rpc/mod.rs
@@ -44,9 +44,9 @@ impl<T: Serialize> RpcOutcome<T> {
/// Converts the outcome into a CLI-compatible JSON value.
///
- /// The resulting JSON shape matches the core CLI expectations:
- /// - If no logs are present, the value is returned directly.
- /// - If logs are present, an object with `result` and `logs` keys is returned.
+ /// The shape is decided by [`apply_log_envelope`], which is the single
+ /// definition of the rule — see its docs for the rule itself and for why
+ /// having one definition matters (#6080).
///
/// # Errors
///
@@ -54,11 +54,46 @@ impl<T: Serialize> RpcOutcome<T> {
pub fn into_cli_compatible_json(self) -> Result<serde_json::Value, String> {
let RpcOutcome { value, logs } = self;
let value = serde_json::to_value(value).map_err(|e| e.to_string())?;
- if logs.is_empty() {
- Ok(value)
- } else {
- Ok(json!({ "result": value, "logs": logs }))
- }
+ Ok(apply_log_envelope(value, logs))
+ }
+}
+
+/// Apply the log envelope to an already-serialized handler value.
+///
+/// **This is the one definition of the rule.** Both controller return paths go
+/// through it:
+///
+/// * the registry path — every `RpcOutcome::into_cli_compatible_json` call
+/// (152 call sites across the domains), and
+/// * the dynamic-dispatch path — `core::types::invocation_to_rpc_json`, used
+/// by `core::dispatch` for internal / legacy methods.
+///
+/// # The rule, and the defect it currently encodes (#6080)
+///
+/// ```text
+/// logs.is_empty() -> value (bare)
+/// otherwise -> { "result": value, "logs": … } (wrapped)
+/// ```
+///
+/// So a controller's **wire shape is decided by its log vector, not by its
+/// schema**. Two methods in one namespace can answer differently, and a handler
+/// that later gains a log line silently changes its own response shape with no
+/// schema change — which is #6080. That defect is deliberately **preserved
+/// byte-for-byte here**: normalising it is a wire change across every
+/// controller and needs a maintainer's ruling, not a quiet fix inside a
+/// refactor.
+///
+/// What this function buys today is that the rule exists **once**. Before it,
+/// the same six lines were written independently in `rpc::RpcOutcome` and in
+/// `core::types::invocation_to_rpc_json`; a fix applied to one would have left
+/// the other on the old behaviour, and nothing linked them. When the ruling
+/// lands, this is the only body that has to change.
+#[must_use]
+pub fn apply_log_envelope(value: serde_json::Value, logs: Vec<String>) -> serde_json::Value {
+ if logs.is_empty() {
+ value
+ } else {
+ json!({ "result": value, "logs": logs })
}
}