From 6c94cb64bdc5646469d0fbdfadf56abebe84a832 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Mon, 24 Aug 2026 19:39:49 +0530 Subject: [PATCH] fix: reject unexpected trailing arguments in transact/retract (#303) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `(transact [facts] {opts})` — the options map placed *after* the facts vector instead of before it — was silently accepted as if no options were given: the map was dropped and every fact's valid-time defaulted to the transaction's wall-clock timestamp instead of the intended :valid-from. This made :valid-at appear to filter by transaction time rather than a fact's actual valid-from, since facts only became visible once the query timestamp caught up to real ingestion time. The read path (:valid-at filtering, net_asserted_facts, and the on-disk index after checkpoint/reopen) was already correct — this was a write-side parser gap: parse_transact/parse_retract only read the elements they expected and never checked for leftover ones. Both now hard-error on trailing arguments instead of silently discarding them. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01Nsh3ZnmoK6PTaUbp4HcqHY --- src/query/datalog/parser.rs | 56 +++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 3 deletions(-) diff --git a/src/query/datalog/parser.rs b/src/query/datalog/parser.rs index 6ec43bbd19..4b2a0aefc3 100644 --- a/src/query/datalog/parser.rs +++ b/src/query/datalog/parser.rs @@ -994,7 +994,7 @@ fn parse_transact(elements: &[EdnValue]) -> Result { let first_element = &elements[0]; // Check if the first element is a map (transaction-level options) - let (tx_valid_from, tx_valid_to, facts_element) = if first_element.is_map() { + let (tx_valid_from, tx_valid_to, facts_element, consumed) = if first_element.is_map() { let map = first_element.as_map().ok_or_else(|| { "internal error: is_map() true but as_map() returned None".to_string() })?; @@ -1002,11 +1002,20 @@ fn parse_transact(elements: &[EdnValue]) -> Result { let facts_elem = elements.get(1).ok_or_else(|| { "Transact with options requires a facts vector after the map".to_string() })?; - (from, to, facts_elem) + (from, to, facts_elem, 2) } else { - (None, None, first_element) + (None, None, first_element, 1) }; + if elements.len() > consumed { + return Err(format!( + "transact takes (transact [facts]) or (transact {{opts}} [facts]); found {} \ + unexpected trailing argument(s) — the valid-time options map must come BEFORE \ + the facts vector, not after", + elements.len() - consumed + )); + } + let facts_vector = facts_element .as_vector() .ok_or("Transact argument must be a vector of facts")?; @@ -1122,6 +1131,13 @@ fn parse_retract(elements: &[EdnValue]) -> Result { return Err("Retract requires a vector of facts".to_string()); } + if elements.len() > 1 { + return Err(format!( + "retract takes (retract [facts]); found {} unexpected trailing argument(s)", + elements.len() - 1 + )); + } + // SAFETY: is_empty() check above guarantees index 0 exists #[allow(clippy::indexing_slicing)] let facts_vector = elements[0] @@ -2420,6 +2436,40 @@ mod tests { assert!(tx.valid_to.is_none()); } + #[test] + fn test_parse_transact_rejects_options_map_after_facts() { + // The valid-time options map must come BEFORE the facts vector: + // (transact {opts} [facts]). Putting it after — (transact [facts] {opts}) — + // must be a hard parse error, not a silent no-op that drops :valid-from. + let result = parse_datalog_command( + r#"(transact [[:alice :employment/status :active]] {:valid-from "2023-01-01"})"#, + ); + assert!(result.is_err(), "expected a parse error"); + let msg = result.unwrap_err(); + assert!( + msg.contains("unexpected") || msg.contains("trailing"), + "error should mention the unexpected trailing argument, was: {}", + msg + ); + } + + #[test] + fn test_parse_transact_rejects_extra_trailing_argument() { + // Trailing garbage after (transact {opts} [facts]) must also be rejected. + let result = parse_datalog_command( + r#"(transact {:valid-from "2023-01-01"} [[:alice :employment/status :active]] :bogus)"#, + ); + assert!(result.is_err(), "expected a parse error"); + } + + #[test] + fn test_parse_retract_rejects_extra_trailing_argument() { + let result = parse_datalog_command( + r#"(retract [[:alice :employment/status :active]] {:valid-from "2023-01-01"})"#, + ); + assert!(result.is_err(), "expected a parse error"); + } + #[test] fn test_parse_as_of_and_valid_at_together() { let cmd = parse_datalog_command(