Skip to content

Fix x-play-rule-rejected non-ASCII metadata drop and validation-error fingerprint collision (post-#2644 follow-up) #2647

Closed

Description

@mstomar125

Overview

PR #2644 (closing #2642, ADR-060 §2's reject action type) merged with two review findings from its adversarial-review round still unaddressed. Both are real, independently confirmed bugs in code that is already on main.

Problem Statement

1. Non-ASCII rejection message drops the x-play-rule-rejected gRPC metadata

In packages/daemon/src/services/node_service.rs, the OpsError::PlayRuleRejected arm of ops_error_to_status builds a JSON payload (node_id/play_id/rule_id/message) and attaches it as gRPC metadata under x-play-rule-rejected. The value is parsed via MetadataValue<Ascii>:

if let Ok(json) = serde_json::to_string(&payload) {
    if let Ok(val) =
        json.parse::<tonic::metadata::MetadataValue<tonic::metadata::Ascii>>()
    {
        status.metadata_mut().insert("x-play-rule-rejected", val);
    }
}

message is the author-supplied rejection text from a playbook reject action — realistically containing an em dash, curly quotes, accents, or emoji. Any non-ASCII byte in that message makes the JSON string fail to parse as MetadataValue<Ascii>, and the failure is silently swallowed by the inner if let Ok(...). The structured metadata — the entire point of this mapping, per its own comment ("lets a caller distinguish this from every other FAILED_PRECONDITION this daemon returns") — silently disappears for any non-trivially-punctuated message. The one test covering this path (error_mapping_play_rule_rejected_returns_failed_precondition_with_metadata) only exercises a pure-ASCII message, so the gap has no regression coverage.

2. load_active_plays's validation-error logging collapses distinct errors into one fingerprint

In packages/core/src/playbook/engine.rs, load_active_plays (and its near-duplicate loops in handle_play_created and handle_play_updated) logs each validate_play error like this:

for err in &errors {
    warn!("  Validation error: {}", err);
    let _ = create_or_update_log_node(
        &self.node_service,
        &node.id,
        "validation",   // rule_name — constant for every error
        0,              // error_location_index — constant for every error
        PlayErrorType::CompileError,
        &err.to_string(),
        "n/a",
    )
    .await;
}

error_fingerprint (packages/core/src/playbook/logging.rs) hashes (play_id, rule_name, error_location_index, error_type) — deliberately excluding the error message itself, so the same structural error dedupes to the same log node. But rule_name/error_location_index are hardcoded constants here, not derived from the actual error. If a single play has two genuinely different validation errors (e.g. a reject action declared on a Reactive rule AND a separate rule with a missing required action param), both produce the identical fingerprint hash(play_id, "validation", 0, compile_error). create_or_update_log_node's dedup path only bumps occurrences and updates last_seen/trigger_node_id — it never updates the log node's message/content. The second error's text is silently discarded, and the log node's occurrences: 2 misleadingly implies a repeat of the same issue rather than two distinct ones. An operator investigating a failed-to-load play sees only one of the two real problems.

Every PlayValidationError variant (packages/core/src/playbook/validation.rs) already carries a location: String field (e.g. "rule[1].action[2]", "rule[0].trigger") that uniquely identifies which rule/action/trigger the error is about — this is the natural per-error identity to fold into the fingerprint instead of a constant placeholder.

Proposed Solution

Metadata fix

Replace the MetadataValue<Ascii> parse with tonic's binary-metadata convention: a -bin-suffixed key (x-play-rule-rejected-bin) carrying the JSON payload as raw bytes via MetadataValue::<Binary>::from_bytes(...), which is infallible (tonic base64-encodes it on the wire automatically) — so the payload can never be silently dropped again. Add a regression test with a real non-ASCII message (em dash, curly quotes, accented characters, emoji) asserting the metadata round-trips intact.

Fingerprint fix

Add a location() accessor to PlayValidationError returning the location field common to every variant, and use it (plus, if same-location-different-kind collisions are a concern, a per-variant discriminant) as the rule_name/identity passed into create_or_update_log_node, instead of the hardcoded "validation"/0. Apply the same fix to all three near-identical validation-error-logging loops (load_active_plays, handle_play_created, handle_play_updated) — ideally by extracting one shared helper so the fix cannot regress in only one of the three copies.

Acceptance Criteria

  • x-play-rule-rejected (or its renamed binary equivalent) metadata is present on the gRPC Status for a PlayRuleRejected error whose message contains non-ASCII bytes
  • A test exercises a real non-ASCII rejection message end-to-end through ops_error_to_status and asserts the metadata payload round-trips
  • Two structurally different PlayValidationErrors on the same play produce two distinct error_fingerprint values (and therefore two distinct playbook_log nodes / occurrence counts), not one
  • The fix applies to all three validation-error-logging call sites in packages/core/src/playbook/engine.rs (load_active_plays, handle_play_created, handle_play_updated), not just load_active_plays
  • Code passes bun run quality:fix
  • Tests pass (bun run test:all)

Technical Specifications

Reference Files

  • packages/daemon/src/services/node_service.rs: ops_error_to_status's OpsError::PlayRuleRejected arm (~line 2468) and its tests (~line 3631)
  • packages/core/src/playbook/engine.rs: load_active_plays (~line 159-215), handle_play_created (~line 515-573), handle_play_updated (~line 620-710) — each has its own copy of the validation-error logging loop
  • packages/core/src/playbook/logging.rs: error_fingerprint and create_or_update_log_node — the dedup mechanism whose inputs need the fix
  • packages/core/src/playbook/validation.rs: PlayValidationError enum (~line 37-132) — every variant already carries a location: String field usable as the per-error identity

Implementation Details

  • tonic = "0.12" is already a dependency; tonic::metadata::MetadataValue::<tonic::metadata::Binary>::from_bytes(&[u8]) is infallible and MetadataMap::insert_bin/get_bin are the corresponding accessors.
  • No frontend/Tauri consumer currently reads x-play-rule-rejected (confirmed via repo-wide grep), so changing the metadata key name/encoding has no cross-package follow-on work required.

Non-Goals

Related Issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions