Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion praxis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
This is the Rust workspace for praxis mode (#1714). It has two crates:

- `cortex-praxis-filters` (`filters/`): the Cortex filters that run inside
Praxis, a port of the Go inference parser (`parse`), and the row builder and
Praxis, ports of the Go inference, MCP and A2A parsers (`parse`), and the row builder and
emitter that send rows to Cortex (`record`).
- `cortex-gateway` (`server/`): the Praxis server with the Cortex filters
registered (#1719).
Expand Down Expand Up @@ -152,11 +152,16 @@ and this suite then shows the Rust gap. To regenerate, from this directory:

```sh
GOWORK=off go -C ../core test ./plugins/inferenceparser -run TestFixtures -update
GOWORK=off go -C ../core test ./plugins/mcpparser -run TestFixtures -update
GOWORK=off go -C ../core test ./plugins/a2aparser -run TestFixtures -update
```

`filters/tests/parity.rs` reads each case under
`../core/plugins/inferenceparser/testdata/`. It feeds the body to the Rust parser
in several ways (whole, in small chunks, gzip-coded, with CRLF line endings).
`filters/tests/mcp_parity.rs` and `filters/tests/a2a_parity.rs` do the same for
`../core/plugins/mcpparser/testdata/` and `../core/plugins/a2aparser/testdata/`
(whole, and for an SSE case also in chunks and with CRLF line endings).
Each result must equal the Go `expected.json`.

When the two differ, fix the Rust code. Do not change Go behavior or a Go
Expand Down
3 changes: 2 additions & 1 deletion praxis/filters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ publish = false

[dependencies]
serde = { version = "1", features = ["derive"] }
serde_json = { version = "1", features = ["raw_value"] }
# float_roundtrip: a number parses to the nearest float64, as in Go (gojson::any).
serde_json = { version = "1", features = ["raw_value", "float_roundtrip"] }
http = "1"
flate2 = "1"
zstd = "0.14"
Expand Down
116 changes: 116 additions & 0 deletions praxis/filters/src/parse/a2a/marshal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//! Port of Go's encoding/json Marshal (encode.go) for a value that json.Unmarshal
//! decoded into an `any`: objects, arrays, strings, float64, bool and null.

use std::fmt::Write;

use serde_json::Value;

/// Go's json.Marshal of a decoded `any`. Object keys are sorted by bytes (the
/// serde_json `Map` of this workspace is a `BTreeMap`), and every number is a float64.
pub(crate) fn go_marshal(v: &Value) -> String {
let mut out = String::new();
encode(&mut out, v);
out
}

fn encode(out: &mut String, v: &Value) {
match v {
Value::Null => out.push_str("null"),
Value::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
Value::Number(n) => float(out, n.as_f64().unwrap_or_default()),
Value::String(s) => string(out, s),
Value::Array(a) => {
out.push('[');
for (i, e) in a.iter().enumerate() {
if i > 0 {
out.push(',');
}
encode(out, e);
}
out.push(']');
}
Value::Object(m) => {
out.push('{');
for (i, (k, e)) in m.iter().enumerate() {
if i > 0 {
out.push(',');
}
string(out, k);
out.push(':');
encode(out, e);
}
out.push('}');
}
}
}

/// Port of floatEncoder for float64: the shortest digits, in 'e' format when
/// abs < 1e-6 or abs >= 1e21, else in 'f' format.
fn float(out: &mut String, f: f64) {
let abs = f.abs();
if abs != 0.0 && !(1e-6..1e21).contains(&abs) {
// Rust writes "1e21" and "1.5e-7". Go writes "1e+21" and, after its
// clean-up of "e-07" to "e-7", the same negative exponent as Rust.
let s = format!("{f:e}");
match s.split_once("e-") {
Some(_) => out.push_str(&s),
None => out.push_str(&s.replacen('e', "e+", 1)),
}
} else {
// Display gives the shortest digits and never an exponent, as Go's 'f' with -1.
let _ = write!(out, "{f}");
}
}

/// Port of appendString with escapeHTML set.
fn string(out: &mut String, s: &str) {
out.push('"');
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\u{8}' => out.push_str("\\b"),
'\u{c}' => out.push_str("\\f"),
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
'<' | '>' | '&' | '\u{2028}' | '\u{2029}' | '\0'..='\u{1f}' => {
let _ = write!(out, "\\u{:04x}", c as u32);
}
c => out.push(c),
}
}
out.push('"');
}

#[cfg(test)]
mod tests {
use super::*;
use crate::parse::inference::gojson;

fn marshal(json: &str) -> String {
let v = gojson::any(gojson::parse(json).unwrap()).unwrap().unwrap();
go_marshal(&v)
}

#[test]
fn go_marshal_matches_go() {
assert_eq!(
marshal(r#"{"b":1,"a":[true,null,"<&>"]}"#),
r#"{"a":[true,null,"\u003c\u0026\u003e"],"b":1}"#
);
assert_eq!(marshal("1e21"), "1e+21");
assert_eq!(marshal("1e-7"), "1e-7");
assert_eq!(marshal("1.5e-10"), "1.5e-10");
assert_eq!(marshal("0.000001"), "0.000001");
assert_eq!(marshal("123456789012345680000"), "123456789012345680000");
assert_eq!(marshal("1.5"), "1.5");
assert_eq!(marshal("-0.0"), "-0");
assert_eq!(marshal("-0"), "-0");
assert_eq!(
marshal(r#"" \u0001\b\u2028\u2029\u007f\"\\/\f\n\r\t\u001f""#),
"\" \\u0001\\b\\u2028\\u2029\u{7f}\\\"\\\\/\\f\\n\\r\\t\\u001f\""
);
assert_eq!(marshal(r#"{"\u003c":"é"}"#), r#"{"\u003c":"é"}"#);
}
}
Loading
Loading