Skip to content

B6: Exhaustive NodeServiceError→gRPC error mapping + parity tests - #1257

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1254-explicit-grpc-error-mapping
May 30, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1254-explicit-grpc-error-mapping

Conversation

@malibio

@malibio malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator

Closes #1254.

Summary

  • From<NodeServiceError> for OpsError (ops/mod.rs) is now exhaustive — catch-all other => Internal replaced with explicit arms for every remaining variant
  • InvalidUpdate → ValidationFailed → InvalidArgument (was Internal)
  • Collection-validation variants (InvalidCollectionPath, CollectionCycle, CollectionDepthExceeded) → ValidationFailed → InvalidArgument
  • Genuinely-internal variants (DatabaseError, TransactionFailed, SerializationError, QueryFailed, BulkOperationFailed, InitializationError) mapped explicitly to Internal — compile error on any future unmapped variant
  • ops_error_to_status: ValidationFailed → invalid_argument (was failed_precondition)
  • fetch_node routes through service_error_to_status instead of bare Status::internal
  • 19 table-driven parity tests asserting NodeServiceError variant → tonic::Code for every variant

Test plan

  • cargo test -p nodespace-daemon — 19 new error_mapping_* tests pass
  • cargo test --workspace --exclude nodespace-agent — all green (agent/Ollama tests require live Ollama, pre-existing skip)
  • cargo clippy --workspace --all-targets -- -D warnings — clean

🤖 Generated with Claude Code

)

Replace catch-all `other => OpsError::Internal` in `From<NodeServiceError> for OpsError` with explicit arms for all remaining variants: `InvalidUpdate` → `ValidationFailed` (client error → `InvalidArgument`); genuinely internal variants (`DatabaseError`, `TransactionFailed`, `SerializationError`, `QueryFailed`, `BulkOperationFailed`, `InitializationError`) mapped explicitly to `Internal`; collection-validation variants (`InvalidCollectionPath`, `CollectionCycle`, `CollectionDepthExceeded`) → `ValidationFailed`. `ValidationFailed` in `ops_error_to_status` now maps to `invalid_argument` instead of `failed_precondition`. `fetch_node` routes through `service_error_to_status` instead of a bare `Status::internal`. Adds 19 table-driven parity tests asserting every variant's `tonic::Code`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio

malibio commented May 30, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review — Initial Review

Review Type: Initial Review


Requirements Check (Issue #1254)

  • ✅ From<NodeServiceError> for OpsError has no catch-all other => arm — all 18 variants are mapped explicitly. A new unmapped variant will now be a compile error.
  • ✅ InvalidUpdate maps to ValidationFailed → InvalidArgument (was swallowed as Internal).
  • ✅ ops_error_to_status remains exhaustive over OpsError (no _ =>).
  • ✅ Parity tests assert the variant→Status code for every NodeServiceError variant; VersionConflict → Aborted is explicitly covered.
  • ✅ fetch_node routes through service_error_to_status instead of bare Status::internal.
  • ⚠️ Partially met — Status::internal call sites in the daemon are only partially audited (see findings below). The issue explicitly required auditing lines 670, 711, and 947.
  • ✅ No #[allow(...)] or lint suppressions added.

Code Review Findings

🟡 Important: ExportMarkdown catch-all at line 670 was not converted

📁 packages/daemon/src/services/node_service.rs:665-670

The issue (acceptance criterion 4) required auditing all Status::internal call sites and re-mapping any client-actionable errors. The MarkdownError enum has exactly 4 variants (InvalidParams, NotFound, CreationFailed, Internal). The current match arm handles NotFound and InvalidParams explicitly but collapses CreationFailed into a generic Status::internal via the catch-all.

MarkdownError::CreationFailed signals a failure during node creation driven by the client's import payload — this is structurally closer to Status::failed_precondition or Status::invalid_argument than Status::internal. Using a catch-all here means a new MarkdownError variant added in the future would silently become Internal — the exact anti-pattern this issue was designed to prevent.

Recommendation: Make this match arm exhaustive by adding an arm for MarkdownError::CreationFailed:

MarkdownError::CreationFailed(m) => Status::failed_precondition(format!("Node creation failed: {m}")),
MarkdownError::Internal(m) => Status::internal(format!("ExportMarkdown failed: {m}")),

Engineering principle: Exhaustiveness at error-type boundaries is the defensive posture this issue establishes — it should be applied consistently, not just on the primary error path.

🟡 Important: get_nodes_batch at line 711 still uses bare Status::internal

📁 packages/daemon/src/services/node_service.rs:711

get_nodes returns Result<Vec<Node>, NodeServiceError>. The issue listed this call site as requiring audit. It is currently mapped with |e| Status::internal(format!("get_nodes_batch failed: {e}")), bypassing the service_error_to_status routing entirely.

While most NodeServiceError variants from a bulk-read are genuinely internal, a NodeNotFound or ValidationFailed variant from get_nodes would be misclassified as Internal here. Replacing with .map_err(service_error_to_status) costs nothing and closes the gap.

🟢 Suggestion: PlaybookValidationFailed arm uses err.to_string() with a moved binding

📁 packages/core/src/ops/mod.rs:69-71

NodeServiceError::PlaybookValidationFailed { .. } => {
    OpsError::InvalidParams(err.to_string())
}

The { .. } pattern ignores the errors field while err is still in scope (consumed by the match arm). This compiles because the match arm body can still access the outer binding. However, it silently drops the structured errors: String field, replacing it with the full thiserror-formatted message string. A more explicit and forward-compatible form would be:

NodeServiceError::PlaybookValidationFailed { errors } => {
    OpsError::InvalidParams(errors)
}

This is consistent with the style used for all other arms (bind the payload, use it directly) and avoids the indirect err.to_string() round-trip.

🟢 Suggestion: B7 commit bundled in a B6 PR

The PR includes two commits: the B6 error mapping work (closes #1254) and the B7 ordering-jitter removal (closes #1253/#1256). The B7 work touches fractional_ordering.rs and sqlite_store.rs — unrelated files — and the grpc_round_trip.rs formatting change originates from the B7 cargo-fmt run.

While neither PR description hides this (both issues are referenced), bundling unrelated changes makes bisect harder and violates the atomicity principle. The issue itself noted "Builds on B3/B4 — parallel-safe with C1a." For a squash-merge branch this is low risk, but worth noting for the team's workflow.


Nitpicks

  • Nit — packages/daemon/tests/grpc_round_trip.rs:981-989: The formatting change (GetRootsRequest { limit, offset } → multi-line) is cargo-fmt churn from the B7 commit, not related to B6. Harmless, but adds noise to the diff.
  • Nit — Test helper fn to_status in the test module could be inlined as a type alias or removed; calling service_error_to_status(err) directly in each test assertion is slightly more explicit about what is being exercised. Minor readability preference.

Summary

The core of this PR is solid. The primary deliverables — exhaustive From<NodeServiceError> for OpsError, the ValidationFailed → invalid_argument fix, the fetch_node routing fix, and 19 table-driven parity tests covering every variant — are all correctly implemented and well-structured. The compile-error-on-new-variant guarantee is the most durable outcome.

The two Important findings relate to the issue's own acceptance criterion 4 ("audit Status::internal call sites") being incompletely applied: ExportMarkdown's catch-all and the get_nodes_batch bypass were listed as explicit call sites to audit and neither was converted. These are quick to fix.

Recommendation: REQUEST CHANGES — address the two Status::internal bypass sites (670 and 711) before merge to fully satisfy acceptance criterion 4.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See detailed review comment above. Two acceptance-criterion-4 call sites (ExportMarkdown catch-all at line 670, get_nodes_batch bypass at line 711) were not converted to route through service_error_to_status. Quick fixes — otherwise the core deliverable (exhaustive error mapping + 19 parity tests) is well implemented.

…hrough service_error_to_status

- ExportMarkdown match now covers all 4 MarkdownError variants explicitly (CreationFailed → failed_precondition, Internal → internal); no catch-all
- get_nodes_batch .map_err now routes through service_error_to_status instead of bare Status::internal
- PlaybookValidationFailed arm destructures `errors` field directly instead of calling err.to_string()

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio
malibio merged commit 5b844ff into main May 30, 2026
@malibio
malibio deleted the worktree-issue-1254-explicit-grpc-error-mapping branch May 30, 2026 12:33
malibio added a commit that referenced this pull request Jun 22, 2026
)

* B6: Exhaustive NodeServiceError→gRPC error mapping + parity tests (#1254)

Replace catch-all `other => OpsError::Internal` in `From<NodeServiceError> for OpsError` with explicit arms for all remaining variants: `InvalidUpdate` → `ValidationFailed` (client error → `InvalidArgument`); genuinely internal variants (`DatabaseError`, `TransactionFailed`, `SerializationError`, `QueryFailed`, `BulkOperationFailed`, `InitializationError`) mapped explicitly to `Internal`; collection-validation variants (`InvalidCollectionPath`, `CollectionCycle`, `CollectionDepthExceeded`) → `ValidationFailed`. `ValidationFailed` in `ops_error_to_status` now maps to `invalid_argument` instead of `failed_precondition`. `fetch_node` routes through `service_error_to_status` instead of a bare `Status::internal`. Adds 19 table-driven parity tests asserting every variant's `tonic::Code`.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

* Address review: exhaust MarkdownError match + route get_nodes_batch through service_error_to_status

- ExportMarkdown match now covers all 4 MarkdownError variants explicitly (CreationFailed → failed_precondition, Internal → internal); no catch-all
- get_nodes_batch .map_err now routes through service_error_to_status instead of bare Status::internal
- PlaybookValidationFailed arm destructures `errors` field directly instead of calling err.to_string()

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude Sonnet 4.6 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

B6: Exhaustive core→gRPC error mapping + parity tests (no catch-all to Status::internal)

1 participant