The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(rpc): give the log envelope a single definition\n\nThe rule that decides a controller's response shape\n\n logs.is_empty() -> value (bare)\n otherwise -> { "result": value, "logs": … } (wrapped)\n\nwas written out independently in two places:\n\n * `rpc::RpcOutcome::into_cli_compatible_json` — the registry path, reached\n by 152 call sites across the domains;\n * `core::types::invocation_to_rpc_json` — the dynamic-dispatch path, live\n via `core/dispatch.rs:64` for internal and legacy methods.\n\nSix identical lines, with nothing linking them. #6080 cites only the first.\nA fix applied there alone would have left the dispatch path answering the old\nshape, and the divergence would have been invisible until a caller hit the\nwrong path.\n\nBoth now delegate to `rpc::apply_log_envelope`. Behaviour is preserved\nbyte-for-byte: this changes where the rule lives, not what it does.\n\n**#6080 stays open after this commit, deliberately.** Normalising the envelope is a wire\nchange across every controller and needs a maintainer's ruling, which the\nissue does not yet carry. The evidence for that ruling is posted on the issue:\nthe frontend peelers are defensive and tolerate either shape, no frontend code\nconsumes log *content* (all six `.logs` references are envelope discriminators\nor unrelated state), and there is no central peel — `coreRpcClient.ts:861`\nunwraps only the outer JSON-RPC `result`. What this commit buys is that when\nthe ruling lands it is a one-line change in one body instead of two.\n\nThe new test asserts the two paths **agree** for the same `(value, logs)`\npair, not that they produce a particular shape — pinning the shape would\nenshrine the defect as intended behaviour, which is the thing #6080 is about.\n\nFault injected to prove it bites: re-forked `invocation_to_rpc_json` to emit\n`payload` instead of `result`, exactly as an accidental future edit would.\n\n assertion `left == right` failed: the dispatch and registry paths\n disagreed for value={"data":true} logs=["one"]; both must go through\n rpc::apply_log_envelope\n left: Object {"logs": [...], "payload": {...}}\n right: Object {"logs": [...], "result": {...}}\n\nat `src/core/types.rs:331`. Restored: 292 passed, 0 failed.\n\nAlso drops the now-orphaned top-level `use serde_json::json;` from\n`core/types.rs` — the only non-test use of the macro moved out with the rule.\n\nRefs #6080\n
M3gA-Minda620e4e6fa3b
diff --git a/src/core/types.rs b/src/core/types.rsindex 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.rsindex 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 })}}