Skip to content

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

fix(devices): treat any non-true `ok` as a refusal, and render a structured error\n\nFour review threads on the previous commits, all on code I added.\n\n**A non-boolean `ok` was read as success.** `object.get("ok")?.as_bool()?`\nyields `None` for `"false"`, `0` or `null`, so a backend answering sloppily fell\nstraight through to `missing field 'channelId'` — the bug this function exists\nto prevent, reintroduced. Absent `ok` is the success shape; anything present and\nnot `true` is a refusal.\n\n**A non-string `error` was discarded.** `{ ok: false, error: { code, message } }`\nbecame "unspecified error", throwing away the backend's explanation — again the\nfailure mode being fixed. Rendered instead.\n\n**The parse-failure log said `ack fields = None` for exactly the shapes it could\nnot explain.** `as_object()` is `None` for an array, string or null, which are\nthe cases where "what arrived?" is the open question. `describe_ack_shape` names\nthe shape, and computing it before `from_value` consumes the value also removes\nthe clone the success path was paying.\n\n**The expiry-conversion error arm had no test**, the one branch these commits\nadded without one.\n\nRevert-checked, each behaviour separately:\n - `as_bool()?` restored -> a_non_boolean_ok_is_still_treated_as_a_refusal fails\n - string-only error restored -> a_structured_error_payload_is_rendered_rather_than_dropped fails\n - baseline and restored -> 11/11\n

M3gA-Mindfac5c5971f42
diff --git a/src/openhuman/security/devices/tunnel_client.rs b/src/openhuman/security/devices/tunnel_client.rs
index 8e0705cdd4..47d704cf50 100644
--- a/src/openhuman/security/devices/tunnel_client.rs
+++ b/src/openhuman/security/devices/tunnel_client.rs
@@ -177,11 +177,11 @@ pub(crate) fn parse_register_ack(ack: serde_json::Value) -> Result<TunnelRegiste
return Err(format!("[devices/tunnel] tunnel:register failed: {error}"));
}
- serde_json::from_value::<TunnelRegisterResponse>(ack.clone()).map_err(|e| {
- log::error!(
- "[devices/tunnel] parse tunnel:register ack failed: {e}; ack fields = {:?}",
- ack.as_object().map(|o| o.keys().collect::<Vec<_>>())
- );
+ // Describe the shape before `from_value` consumes the value, so the success
+ // path pays no clone and the failure path can still say what arrived.
+ let shape = describe_ack_shape(&ack);
+ serde_json::from_value::<TunnelRegisterResponse>(ack).map_err(|e| {
+ log::error!("[devices/tunnel] parse tunnel:register ack failed: {e}; ack was a {shape}");
format!("[devices/tunnel] parse tunnel:register ack failed: {e}")
})
}
@@ -194,16 +194,41 @@ pub(crate) fn parse_register_ack(ack: serde_json::Value) -> Result<TunnelRegiste
/// unambiguously a refusal and its `error` is the only useful thing in it.
pub(crate) fn backend_ack_error(ack: &serde_json::Value) -> Option<String> {
let object = ack.as_object()?;
- if object.get("ok")?.as_bool()? {
+ // Absent `ok` is the success shape. Anything else is a refusal — including
+ // a present-but-not-`true` value such as the string `"false"` or `0`. An
+ // earlier version asked `as_bool()?`, which yields `None` for those and so
+ // fell through to `missing field 'channelId'`: the exact bug this function
+ // exists to prevent, reintroduced for a backend that answers sloppily.
+ if object.get("ok")?.as_bool() == Some(true) {
return None;
}
- Some(
- object
- .get("error")
- .and_then(|e| e.as_str())
- .unwrap_or("unspecified error")
- .to_string(),
- )
+ // A non-string `error` (say `{ code, message }`) is rendered rather than
+ // discarded — throwing away the backend's explanation is the failure mode
+ // being fixed, and it does not become acceptable because the shape
+ // surprised us.
+ Some(match object.get("error") {
+ None | Some(serde_json::Value::Null) => "unspecified error".to_string(),
+ Some(serde_json::Value::String(text)) => text.clone(),
+ Some(other) => other.to_string(),
+ })
+}
+
+/// A short description of an ACK's shape, for the parse-failure log.
+///
+/// `as_object()` is `None` for an array, a string or `null` — precisely the
+/// cases where "what actually arrived?" is the open question, and precisely
+/// where an earlier version logged the unhelpful `ack fields = None`.
+fn describe_ack_shape(ack: &serde_json::Value) -> String {
+ match ack {
+ serde_json::Value::Object(map) => {
+ format!("object with fields {:?}", map.keys().collect::<Vec<_>>())
+ }
+ serde_json::Value::Array(items) => format!("array of {} element(s)", items.len()),
+ serde_json::Value::String(_) => "string".to_string(),
+ serde_json::Value::Number(_) => "number".to_string(),
+ serde_json::Value::Bool(_) => "bool".to_string(),
+ serde_json::Value::Null => "null".to_string(),
+ }
}
/// Emit `tunnel:connect` to start listening on a channel as `role:"core"`.
diff --git a/src/openhuman/security/devices/tunnel_client_tests.rs b/src/openhuman/security/devices/tunnel_client_tests.rs
index c3deecd5be..593ce80968 100644
--- a/src/openhuman/security/devices/tunnel_client_tests.rs
+++ b/src/openhuman/security/devices/tunnel_client_tests.rs
@@ -152,3 +152,62 @@ fn a_successful_ack_is_not_treated_as_a_failure_envelope() {
let response = parse_register_ack(ack).expect("a successful ack must parse");
assert_eq!(response.channel_id, "ch_1");
}
+
+/// The out-of-range arm of the expiry conversion — the one branch this change
+/// added that had no test, and the one the failure-path requirement and the
+/// diff-coverage gate both land on.
+#[test]
+fn an_unrepresentable_pairing_expiry_is_refused() {
+ let err = serde_json::from_value::<TunnelRegisterResponse>(json!({
+ "channelId": "ch_x",
+ "pairingToken": "pt_x",
+ "pairingExpiresAt": i64::MAX
+ }))
+ .expect_err("a millisecond value no date can represent must not be accepted");
+
+ assert!(
+ err.to_string().contains("out of range"),
+ "the error must say the value is unrepresentable, got: {err}"
+ );
+}
+
+/// A refusal whose `ok` is present but not the boolean `true` is still a
+/// refusal. `as_bool()` yields `None` for `"false"`, so an earlier version read
+/// this as success and fell through to `missing field 'channelId'` — the bug
+/// this function exists to prevent.
+#[test]
+fn a_non_boolean_ok_is_still_treated_as_a_refusal() {
+ for ack in [
+ json!({ "ok": "false", "error": "nope" }),
+ json!({ "ok": 0, "error": "nope" }),
+ json!({ "ok": null, "error": "nope" }),
+ ] {
+ let err = parse_register_ack(ack.clone())
+ .expect_err("a non-boolean `ok` must not be read as success");
+ assert!(err.contains("nope"), "got: {err} for {ack}");
+ }
+
+ // …while an explicit `ok: true` is not a refusal.
+ assert!(backend_ack_error(&json!({ "ok": true })).is_none());
+}
+
+/// A non-string `error` is rendered, not discarded. Throwing away the backend's
+/// explanation is the failure mode being fixed; an unexpected shape does not
+/// make it acceptable.
+#[test]
+fn a_structured_error_payload_is_rendered_rather_than_dropped() {
+ let err = parse_register_ack(json!({
+ "ok": false,
+ "error": { "code": 429, "message": "channel limit" }
+ }))
+ .expect_err("a structured refusal is still a refusal");
+
+ assert!(
+ err.contains("channel limit") && err.contains("429"),
+ "the backend's payload must survive into the message, got: {err}"
+ );
+ assert!(
+ !err.contains("unspecified error"),
+ "a present error payload must not be reported as unspecified, got: {err}"
+ );
+}