From 8717f787760df5c92ee8aaa9daab298dd74fb2df Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 19:51:59 +0000 Subject: [PATCH] fix(session): report a rejected record.set value as invalid_value A record.set value that does not decode into the record's type was reported as `internal: engine/transport failure`, pointing at the engine instead of the caller's value. Add DbError::InvalidValue (kind Data) for a remote write whose value the record's codec rejects, map it to a new RpcError::InvalidValue, and carry it on the AimX wire as `invalid_value`. aimdb-client and the WASM bridge name it. Older clients decode the unknown code as `internal`, as before. Fixes #454 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WGkDJ9KUrNCqA8p5JA2Hyo --- aimdb-client/CHANGELOG.md | 4 ++++ aimdb-client/src/engine.rs | 5 +++++ aimdb-client/tests/aimx_session.rs | 12 ++++++++++++ aimdb-core/CHANGELOG.md | 6 ++++++ aimdb-core/src/error.rs | 17 +++++++++++++++++ aimdb-core/src/session/aimx/codec.rs | 3 +++ aimdb-core/src/session/aimx/dispatch.rs | 1 + aimdb-core/src/session/mod.rs | 3 +++ aimdb-core/src/typed_record/record.rs | 24 ++++++------------------ aimdb-wasm-adapter/CHANGELOG.md | 3 +++ aimdb-wasm-adapter/src/ws_bridge.rs | 1 + 11 files changed, 61 insertions(+), 18 deletions(-) diff --git a/aimdb-client/CHANGELOG.md b/aimdb-client/CHANGELOG.md index d9725321..d3f3e1e9 100644 --- a/aimdb-client/CHANGELOG.md +++ b/aimdb-client/CHANGELOG.md @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- **A rejected `set_record` value is `invalid_value` (#454).** A value the + record's type cannot decode now fails with `ServerError { code: + "invalid_value" }` instead of `internal: engine/transport failure`. + - **`get_record` on a ring returns `unsupported` (#444).** A ring has no current value; use `drain_record` or `subscribe`. `get_record` no longer moves the connection's drain cursor, so `drain_record` returns every unread event. diff --git a/aimdb-client/src/engine.rs b/aimdb-client/src/engine.rs index 54b18389..8889b28e 100644 --- a/aimdb-client/src/engine.rs +++ b/aimdb-client/src/engine.rs @@ -583,6 +583,11 @@ fn rpc_err(e: RpcError) -> ClientError { ClientError::server_error("not_found", "method or record not found", None) } RpcError::Denied => ClientError::server_error("denied", "permission denied", None), + RpcError::InvalidValue => ClientError::server_error( + "invalid_value", + "the value does not match the record's type", + None, + ), RpcError::Unsupported => ClientError::server_error( "unsupported", "not supported for this record (a ring has no current value: \ diff --git a/aimdb-client/tests/aimx_session.rs b/aimdb-client/tests/aimx_session.rs index de46fcb4..c8804898 100644 --- a/aimdb-client/tests/aimx_session.rs +++ b/aimdb-client/tests/aimx_session.rs @@ -106,6 +106,18 @@ async fn aimx_roundtrip_over_uds_production_server() { assert_eq!(after_set.stamp(), set_ts); assert!(set_ts >= seeded); + // RPC: a value the record's type rejects is the caller's fault, not the + // engine's (#454): `invalid_value`, the stored value untouched, and the + // connection still serves the next call. + match conn.set_record("setting", json!({ "level": "high" })).await { + Err(aimdb_client::ClientError::ServerError { code, .. }) => { + assert_eq!(code, "invalid_value") + } + other => panic!("expected invalid_value, got {other:?}"), + } + let unchanged = conn.get_record("setting").await.unwrap(); + assert_eq!(unchanged.value, json!({ "level": 7 })); + // Streaming: a producer feeds `events`; the subscription routes updates back. let producer = db.producer(key_events_2).expect("producer"); tokio::spawn(async move { diff --git a/aimdb-core/CHANGELOG.md b/aimdb-core/CHANGELOG.md index 4dfcb6fe..559482de 100644 --- a/aimdb-core/CHANGELOG.md +++ b/aimdb-core/CHANGELOG.md @@ -50,6 +50,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 an unknown name answer `not_found` at once. A ring registered with `buffer_raw()` and no `buffer_cfg()` answers `not_found`, since core cannot tell it is a ring. +- **A rejected `record.set` value answers `invalid_value` (#454).** A value + that does not decode into the record's type was reported as `internal`, + blaming the engine. It now fails with the new `DbError::InvalidValue` + (kind `Data`), which AimX sends as `RpcError::InvalidValue` / wire code + `invalid_value`. Older clients decode the unknown code as `internal`, as + before. ### Added diff --git a/aimdb-core/src/error.rs b/aimdb-core/src/error.rs index 6d2bdf9b..1d1e68c9 100644 --- a/aimdb-core/src/error.rs +++ b/aimdb-core/src/error.rs @@ -155,6 +155,10 @@ pub enum DbError { #[error("Permission denied: {operation}")] PermissionDenied { operation: String }, + /// A remote write's value does not decode into the record's type + #[error("Value rejected: it does not decode into '{type_name}'")] + InvalidValue { type_name: String }, + // ===== Configuration Errors ===== /// Missing required configuration parameter #[error("Missing configuration parameter: {parameter}")] @@ -249,6 +253,13 @@ impl DbError { } } + /// Builds an [`InvalidValue`](DbError::InvalidValue). + pub fn invalid_value(type_name: impl Into) -> Self { + DbError::InvalidValue { + type_name: type_name.into(), + } + } + /// Builds a [`RecordKeyNotFound`](DbError::RecordKeyNotFound). pub fn record_key_not_found(key: impl Into) -> Self { DbError::RecordKeyNotFound { key: key.into() } @@ -311,6 +322,8 @@ impl DbError { Self::ConnectionFailed { .. } => DbErrorKind::Transport, + Self::InvalidValue { .. } => DbErrorKind::Data, + Self::MissingConfiguration { .. } | Self::InvalidConfiguration { .. } | Self::CyclicDependency { .. } @@ -403,6 +416,10 @@ mod tests { DbError::permission_denied("write"), DbError::PermissionDenied { .. } )); + assert!(matches!( + DbError::invalid_value("T"), + DbError::InvalidValue { .. } + )); assert!(matches!( DbError::record_key_not_found("k"), DbError::RecordKeyNotFound { .. } diff --git a/aimdb-core/src/session/aimx/codec.rs b/aimdb-core/src/session/aimx/codec.rs index 6bf1e1ce..c4922531 100644 --- a/aimdb-core/src/session/aimx/codec.rs +++ b/aimdb-core/src/session/aimx/codec.rs @@ -149,6 +149,7 @@ fn err_code(e: &RpcError) -> &'static str { RpcError::NotFound => "not_found", RpcError::Unsupported => "unsupported", RpcError::Denied => "denied", + RpcError::InvalidValue => "invalid_value", RpcError::Internal => "internal", RpcError::VersionMismatch => "version_mismatch", // Client-side only; a server has no reason to send it. @@ -161,6 +162,7 @@ fn code_err(s: &str) -> RpcError { "not_found" => RpcError::NotFound, "unsupported" => RpcError::Unsupported, "denied" => RpcError::Denied, + "invalid_value" => RpcError::InvalidValue, "version_mismatch" => RpcError::VersionMismatch, _ => RpcError::Internal, } @@ -519,6 +521,7 @@ mod tests { RpcError::NotFound, RpcError::Unsupported, RpcError::Denied, + RpcError::InvalidValue, RpcError::Internal, RpcError::VersionMismatch, ] { diff --git a/aimdb-core/src/session/aimx/dispatch.rs b/aimdb-core/src/session/aimx/dispatch.rs index 568fedee..56c0f33a 100644 --- a/aimdb-core/src/session/aimx/dispatch.rs +++ b/aimdb-core/src/session/aimx/dispatch.rs @@ -555,6 +555,7 @@ fn map_db_err(e: DbError) -> RpcError { match e { DbError::RecordKeyNotFound { .. } | DbError::InvalidRecordId { .. } => RpcError::NotFound, DbError::PermissionDenied { .. } => RpcError::Denied, + DbError::InvalidValue { .. } => RpcError::InvalidValue, _ => RpcError::Internal, } } diff --git a/aimdb-core/src/session/mod.rs b/aimdb-core/src/session/mod.rs index 56dea973..b416c557 100644 --- a/aimdb-core/src/session/mod.rs +++ b/aimdb-core/src/session/mod.rs @@ -275,6 +275,9 @@ pub enum RpcError { Unsupported, /// The caller lacks permission for this operation. Denied, + /// The caller's value was refused: it does not decode into the record's + /// type, e.g. a `record.set` value that fails the type's deserializer. + InvalidValue, /// The handler failed. Internal, /// The peer's declared protocol version is incompatible with this server's diff --git a/aimdb-core/src/typed_record/record.rs b/aimdb-core/src/typed_record/record.rs index 683eae62..0e2b583e 100644 --- a/aimdb-core/src/typed_record/record.rs +++ b/aimdb-core/src/typed_record/record.rs @@ -1201,8 +1201,6 @@ impl JsonRecordAccess for TypedRecord< } fn set_from_json(&self, json_value: serde_json::Value) -> crate::DbResult<()> { - use crate::DbError; - log_debug!( "set_from_json called for type: {}", core::any::type_name::() @@ -1210,33 +1208,23 @@ impl JsonRecordAccess for TypedRecord< let codec = self.remote_write_codec("set_from_json")?; - let value: T = codec.decode(&json_value).ok_or_else(|| { - DbError::runtime_error(alloc::format!( - "Failed to deserialize JSON to type '{}'. \ - JSON structure does not match the expected schema.", - core::any::type_name::() - )) - })?; + let value: T = codec + .decode(&json_value) + .ok_or_else(|| crate::DbError::invalid_value(core::any::type_name::()))?; self.commit_remote_write(value) } fn set_from_json_bytes(&self, bytes: &[u8]) -> crate::DbResult<()> { - use crate::DbError; - log_debug!( "set_from_json_bytes called for type: {}", core::any::type_name::() ); let codec = self.remote_write_codec("set_from_json_bytes")?; - let value: T = codec.decode_slice(bytes).ok_or_else(|| { - DbError::runtime_error(alloc::format!( - "Failed to deserialize JSON to type '{}'. \ - JSON structure does not match the expected schema.", - core::any::type_name::() - )) - })?; + let value: T = codec + .decode_slice(bytes) + .ok_or_else(|| crate::DbError::invalid_value(core::any::type_name::()))?; self.commit_remote_write(value) } diff --git a/aimdb-wasm-adapter/CHANGELOG.md b/aimdb-wasm-adapter/CHANGELOG.md index 8ad4a1db..b9357f0e 100644 --- a/aimdb-wasm-adapter/CHANGELOG.md +++ b/aimdb-wasm-adapter/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed (breaking, wire) +- A `record.set` value the record's type rejects now reports + `invalid_value` instead of `internal` (#454). + - **`query()` resolves with `{ records }` (#429).** `total` is gone; use `records.length`. diff --git a/aimdb-wasm-adapter/src/ws_bridge.rs b/aimdb-wasm-adapter/src/ws_bridge.rs index ecac85a5..289a4708 100644 --- a/aimdb-wasm-adapter/src/ws_bridge.rs +++ b/aimdb-wasm-adapter/src/ws_bridge.rs @@ -842,6 +842,7 @@ fn rpc_err_str(e: &RpcError) -> &'static str { RpcError::NotFound => "not_found", RpcError::Unsupported => "unsupported (a ring has no current value)", RpcError::Denied => "denied", + RpcError::InvalidValue => "invalid_value (the value does not match the record's type)", RpcError::VersionMismatch => { "version_mismatch (server rejected the client protocol version)" }