-
Notifications
You must be signed in to change notification settings - Fork 0
feat(querier): plan-time body-literal candidate matcher (RFC 0044 slice 1) #671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
92945d5
feat(querier): plan-time body-literal candidate matcher (RFC 0044 sli…
jensholdgaard 63ad166
refactor(querier): export the body matcher via pub use, module private
jensholdgaard a32727e
style: rustfmt import order
jensholdgaard 1dea812
Merge branch 'main' into rfc0044-slice1
jensholdgaard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| //! Plan-time resolution of a `body ==` literal against the tenant's | ||
| //! template registry (RFC 0044 §3.1, the template arm's candidate set). | ||
| //! | ||
| //! The literal is tokenized with the miner's own tokenizer and unified | ||
| //! position-wise against every registered `(template_id, version)`'s | ||
| //! canonical tokens: `Fixed` tokens must match byte-for-byte, `Wildcard` | ||
| //! positions capture the literal's token as the implied parameter value. | ||
| //! The result is a *candidate superset*: separators and stored parameter | ||
| //! values (overflow spills, RFC 0023) are per-record, so exact equality | ||
| //! is settled at scan time against the candidates — but only ever inside | ||
| //! row groups the candidate `template_id`s admit, which is what keeps | ||
| //! the arm prunable. Soundness of the whole inversion rests on the | ||
| //! `CLAUDE.md` §3.3 bit-identical reconstruction invariant. | ||
|
|
||
| use ourios_miner::tokenize::tokenize; | ||
| use ourios_miner::tree::OwnedToken; | ||
|
|
||
| use crate::template_registry::TemplateRegistry; | ||
|
|
||
| /// One template the literal could have mined to: the version-qualified | ||
| /// id plus the parameter values implied by the literal's tokens at the | ||
| /// template's wildcard positions (in wildcard order). | ||
| #[derive(Debug, Clone, PartialEq, Eq)] | ||
| pub struct BodyLiteralMatch { | ||
| pub template_id: u64, | ||
| pub template_version: u32, | ||
| /// The literal's tokens at the template's `Wildcard` positions. | ||
| /// Empty for a zero-parameter template — the whole-body-is-the- | ||
| /// template case (event-name-bodied `GenAI` records). | ||
| pub params: Vec<String>, | ||
| } | ||
|
|
||
| /// Resolve `literal` to every registry entry it unifies with. | ||
| /// | ||
| /// A literal the tokenizer rejects (embedded NUL — the parse-failure | ||
| /// path at ingest, which always retains the body) matches no template: | ||
| /// such records are only ever reachable through the physical body arm, | ||
| /// so an empty candidate set is exact, not lossy. | ||
| #[must_use] | ||
| pub fn body_literal_candidates( | ||
| registry: &TemplateRegistry, | ||
| literal: &str, | ||
| ) -> Vec<BodyLiteralMatch> { | ||
| let Ok(tokenized) = tokenize(literal) else { | ||
| return Vec::new(); | ||
| }; | ||
| let mut matches: Vec<BodyLiteralMatch> = registry | ||
| .iter() | ||
| .filter_map(|(&(template_id, template_version), tokens)| { | ||
| unify(tokens, &tokenized.tokens).map(|params| BodyLiteralMatch { | ||
| template_id, | ||
| template_version, | ||
| params, | ||
| }) | ||
| }) | ||
| .collect(); | ||
| // Deterministic plan output: registry iteration order is a HashMap's. | ||
| matches.sort_by_key(|m| (m.template_id, m.template_version)); | ||
| matches | ||
| } | ||
|
|
||
| /// Position-wise unification: same arity, every `Fixed` equal, every | ||
| /// `Wildcard` capturing. Returns the captured parameter values in | ||
| /// wildcard order. | ||
| fn unify(template: &[OwnedToken], literal_tokens: &[&str]) -> Option<Vec<String>> { | ||
| if template.len() != literal_tokens.len() { | ||
| return None; | ||
| } | ||
| let mut params = Vec::new(); | ||
| for (token, &literal_token) in template.iter().zip(literal_tokens) { | ||
| match token { | ||
| OwnedToken::Fixed(fixed) if fixed == literal_token => {} | ||
| OwnedToken::Fixed(_) => return None, | ||
| OwnedToken::Wildcard => params.push(literal_token.to_owned()), | ||
| } | ||
| } | ||
| Some(params) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use ourios_miner::tree::parse_template; | ||
|
|
||
| fn registry(entries: &[((u64, u32), &str)]) -> TemplateRegistry { | ||
| entries | ||
| .iter() | ||
| .map(|&(key, canonical)| (key, parse_template(canonical))) | ||
| .collect() | ||
| } | ||
|
|
||
| #[test] | ||
| fn zero_param_template_matches_its_exact_body() { | ||
| let reg = registry(&[((7, 1), "claude_code.api_request")]); | ||
| assert_eq!( | ||
| body_literal_candidates(®, "claude_code.api_request"), | ||
| vec![BodyLiteralMatch { | ||
| template_id: 7, | ||
| template_version: 1, | ||
| params: Vec::new(), | ||
| }], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parameterized_template_captures_the_implied_params_in_order() { | ||
| let reg = registry(&[((3, 2), "user <*> logged in from <*>")]); | ||
| assert_eq!( | ||
| body_literal_candidates(®, "user 4711 logged in from 10.0.0.3"), | ||
| vec![BodyLiteralMatch { | ||
| template_id: 3, | ||
| template_version: 2, | ||
| params: vec!["4711".to_owned(), "10.0.0.3".to_owned()], | ||
| }], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_literal_may_unify_with_several_templates_deterministically() { | ||
| let reg = registry(&[ | ||
| ((9, 1), "user <*> logged in"), | ||
| ((2, 1), "<*> <*> logged in"), | ||
| ((5, 1), "user rooted logged out"), | ||
| ]); | ||
| assert_eq!( | ||
| body_literal_candidates(®, "user 42 logged in"), | ||
| vec![ | ||
| BodyLiteralMatch { | ||
| template_id: 2, | ||
| template_version: 1, | ||
| params: vec!["user".to_owned(), "42".to_owned()], | ||
| }, | ||
| BodyLiteralMatch { | ||
| template_id: 9, | ||
| template_version: 1, | ||
| params: vec!["42".to_owned()], | ||
| }, | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn arity_and_fixed_token_mismatches_do_not_unify() { | ||
| let reg = registry(&[((1, 1), "user <*> logged in")]); | ||
| assert!(body_literal_candidates(®, "user 42 logged out").is_empty()); | ||
| assert!(body_literal_candidates(®, "user 42 logged in twice").is_empty()); | ||
| assert!(body_literal_candidates(®, "user 42").is_empty()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn versions_of_one_template_are_independent_candidates() { | ||
| let reg = registry(&[((4, 1), "job <*> finished"), ((4, 2), "job <*> <*>")]); | ||
| assert_eq!( | ||
| body_literal_candidates(®, "job 8 finished"), | ||
| vec![ | ||
| BodyLiteralMatch { | ||
| template_id: 4, | ||
| template_version: 1, | ||
| params: vec!["8".to_owned()], | ||
| }, | ||
| BodyLiteralMatch { | ||
| template_id: 4, | ||
| template_version: 2, | ||
| params: vec!["8".to_owned(), "finished".to_owned()], | ||
| }, | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_nul_bearing_literal_matches_no_template() { | ||
| // The parse-failure ingest path retains such bodies verbatim, so | ||
| // the physical arm alone is exact for them. | ||
| let reg = registry(&[((1, 1), "claude_code.api_request")]); | ||
| assert!(body_literal_candidates(®, "claude\0code").is_empty()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.