gcf 2.5.2

The AI-native wire format for structured data. 50-92% fewer tokens than JSON, with multi-turn delta encoding for agent loops. 100% comprehension on every frontier model. Zero dependencies (except serde).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Property-based round-trip tests for GCF v2.0.

use gcf::{decode_generic, encode_generic};
use serde_json::{json, Value};
use std::collections::HashMap;

const DEFAULT_ITERATIONS: usize = 100_000;

fn get_iterations() -> usize {
    std::env::var("GCF_ITERATIONS")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(DEFAULT_ITERATIONS)
}

// Simple xorshift32 PRNG.
struct Rng(u32);
impl Rng {
    fn new(seed: u32) -> Self {
        Self(seed)
    }
    fn next(&mut self) -> u32 {
        self.0 ^= self.0 << 13;
        self.0 ^= self.0 >> 17;
        self.0 ^= self.0 << 5;
        self.0
    }
    fn int(&mut self, max: u32) -> u32 {
        self.next() % max
    }
    fn float(&mut self) -> f64 {
        (self.next() as f64) / (u32::MAX as f64)
    }
}

fn gen_value(rng: &mut Rng, depth: usize, max_depth: usize) -> Value {
    if depth >= max_depth {
        return gen_scalar(rng);
    }
    match rng.int(10) {
        0 => Value::Null,
        1 => Value::Bool(rng.float() < 0.5),
        2 => json!(gen_number(rng)),
        3 | 4 => Value::String(gen_string(rng)),
        5 | 6 => gen_object(rng, depth, max_depth),
        7 | 8 => gen_array(rng, depth, max_depth),
        _ => gen_scalar(rng),
    }
}

fn gen_scalar(rng: &mut Rng) -> Value {
    match rng.int(5) {
        0 => Value::Null,
        1 => Value::Bool(rng.float() < 0.5),
        2 => json!(gen_number(rng)),
        _ => Value::String(gen_string(rng)),
    }
}

fn gen_number(rng: &mut Rng) -> f64 {
    match rng.int(7) {
        0 => 0.0,
        1 => (rng.int(1000)) as f64,
        2 => -(rng.int(1000) as f64),
        3 => (rng.int(1000000) as f64) + rng.float(),
        4 => -0.0_f64,
        5 => ((rng.int(999) + 1) as f64) * 1e18,
        6 => ((rng.int(999) + 1) as f64) * 1e-10,
        _ => rng.float() * 2000.0 - 1000.0,
    }
}

fn gen_string(rng: &mut Rng) -> String {
    let n = rng.int(20) as usize;
    let chars = b"abcdefghijklmnopqrstuvwxyz0123456789";
    let special = b" |,=\"\\#@\n\t~^+-.";
    let mut s = String::with_capacity(n);
    for _ in 0..n {
        if rng.float() < 0.2 {
            s.push(special[rng.int(special.len() as u32) as usize] as char);
        } else {
            s.push(chars[rng.int(chars.len() as u32) as usize] as char);
        }
    }
    s
}

fn gen_bare_key(rng: &mut Rng) -> String {
    let chars = b"abcdefghijklmnopqrstuvwxyz_";
    let n = 1 + rng.int(8) as usize;
    (0..n)
        .map(|_| chars[rng.int(chars.len() as u32) as usize] as char)
        .collect()
}

fn gen_object(rng: &mut Rng, depth: usize, max_depth: usize) -> Value {
    let n = rng.int(6) as usize;
    let mut map = serde_json::Map::new();
    for _ in 0..n {
        let key = gen_bare_key(rng);
        if !map.contains_key(&key) {
            map.insert(key, gen_value(rng, depth + 1, max_depth));
        }
    }
    Value::Object(map)
}

fn gen_array(rng: &mut Rng, depth: usize, max_depth: usize) -> Value {
    let n = rng.int(6) as usize;
    let mut arr = Vec::with_capacity(n);
    match rng.int(4) {
        0 => {
            for _ in 0..n {
                arr.push(gen_scalar(rng));
            }
        }
        1 => {
            let fields: Vec<String> = (0..1 + rng.int(4) as usize)
                .map(|_| gen_bare_key(rng))
                .collect();
            for _ in 0..n {
                let mut obj = serde_json::Map::new();
                for f in &fields {
                    if rng.float() > 0.2 {
                        obj.insert(f.clone(), gen_scalar(rng));
                    }
                }
                arr.push(Value::Object(obj));
            }
        }
        2 => {
            for _ in 0..n {
                let mut obj = serde_json::Map::new();
                obj.insert(gen_bare_key(rng), gen_scalar(rng));
                if rng.float() < 0.3 && depth + 1 < max_depth {
                    obj.insert(gen_bare_key(rng), gen_value(rng, depth + 2, max_depth));
                }
                arr.push(Value::Object(obj));
            }
        }
        _ => {
            for _ in 0..n {
                arr.push(gen_value(rng, depth + 1, max_depth));
            }
        }
    }
    Value::Array(arr)
}

fn numeric_equal(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Number(an), Value::Number(bn)) => an.as_f64() == bn.as_f64(),
        _ => false,
    }
}

fn structural_equal(a: &Value, b: &Value) -> bool {
    match (a, b) {
        (Value::Object(am), Value::Object(bm)) => {
            let mut ak: Vec<&String> = am.keys().collect();
            let mut bk: Vec<&String> = bm.keys().collect();
            ak.sort();
            bk.sort();
            ak == bk && ak.iter().all(|k| structural_equal(&am[*k], &bm[*k]))
        }
        (Value::Array(aa), Value::Array(ba)) => {
            aa.len() == ba.len() && aa.iter().zip(ba).all(|(x, y)| structural_equal(x, y))
        }
        (Value::Number(_), Value::Number(_)) => numeric_equal(a, b),
        _ => a == b,
    }
}

// A fixed nested schema: a scalar leaf or an ordered set of named sub-shapes.
enum FlatShape {
    Scalar,
    Nested(Vec<(String, FlatShape)>),
}

fn gen_flat_shape(
    rng: &mut Rng,
    depth: usize,
    max_depth: usize,
    key_fn: fn(&mut Rng) -> String,
) -> FlatShape {
    if depth >= max_depth || rng.float() < 0.45 {
        return FlatShape::Scalar;
    }
    let mut sub = Vec::new();
    let mut seen = std::collections::HashSet::new();
    for _ in 0..1 + rng.int(3) as usize {
        let k = key_fn(rng);
        if seen.insert(k.clone()) {
            sub.push((k, gen_flat_shape(rng, depth + 1, max_depth, key_fn)));
        }
    }
    if sub.is_empty() {
        FlatShape::Scalar
    } else {
        FlatShape::Nested(sub)
    }
}

fn materialize_flat_shape(rng: &mut Rng, shape: &FlatShape) -> Value {
    match shape {
        FlatShape::Scalar => gen_scalar(rng),
        FlatShape::Nested(sub) => {
            let mut map = serde_json::Map::new();
            for (k, s) in sub {
                // A nested sub-object is sometimes null (intermediate null — the case the
                // pre-fix encoder dropped) instead of a full object.
                let v = if !matches!(s, FlatShape::Scalar) && rng.float() < 0.3 {
                    Value::Null
                } else {
                    materialize_flat_shape(rng, s)
                };
                map.insert(k.clone(), v);
            }
            Value::Object(map)
        }
    }
}

fn gen_flattenable_array(rng: &mut Rng, key_fn: fn(&mut Rng) -> String) -> Value {
    let mut schema: Vec<(String, FlatShape)> = vec![("id".to_string(), FlatShape::Scalar)];
    let mut seen = std::collections::HashSet::new();
    seen.insert("id".to_string());
    let mut has_nested = false;
    for _ in 0..1 + rng.int(3) as usize {
        let k = key_fn(rng);
        if !seen.insert(k.clone()) {
            continue;
        }
        let s = gen_flat_shape(rng, 1, 3, key_fn);
        if !matches!(s, FlatShape::Scalar) {
            has_nested = true;
        }
        schema.push((k, s));
    }
    if !has_nested {
        // Synthesize a nested field to force the flatten path. Adversarial key funcs
        // collide often, so only insert if the key isn't already present (mirrors the
        // Go SDK fallback dedup).
        let k = key_fn(rng);
        if seen.insert(k.clone()) {
            let inner = FlatShape::Nested(vec![(
                gen_bare_key(rng),
                FlatShape::Nested(vec![(gen_bare_key(rng), FlatShape::Scalar)]),
            )]);
            schema.push((k, inner));
        }
    }
    let rows = 2 + rng.int(6) as usize;
    let mut arr = Vec::with_capacity(rows);
    for _ in 0..rows {
        let mut row = serde_json::Map::new();
        for (f, s) in &schema {
            let x = rng.float();
            if x < 0.12 {
                continue; // field absent this row
            } else if x < 0.24 {
                row.insert(f.clone(), Value::Null); // field present-null (top-level null)
            } else {
                row.insert(f.clone(), materialize_flat_shape(rng, s));
            }
        }
        arr.push(Value::Object(row));
    }
    Value::Array(arr)
}

// Aligned arrays whose shared fields are fixed-shape nested objects, with a field
// or an intermediate nested level sometimes null/absent — the v3.2 flatten path the
// scalar-only generator never produces, so flatten/unflatten and its null-at-depth
// losslessness edge would otherwise be unexercised.
#[test]
fn test_flatten_roundtrip() {
    let iterations = get_iterations();
    let mut rng = Rng::new(7);
    for i in 0..iterations {
        let val = gen_flattenable_array(&mut rng, gen_bare_key);
        let gcf = encode_generic(&val);
        let decoded = decode_generic(&gcf).unwrap_or_else(|e| {
            panic!(
                "iteration {}: decode failed: {}\n  input: {}\n  gcf: {:?}",
                i, e, val, gcf
            );
        });
        assert!(
            structural_equal(&val, &decoded),
            "iteration {}: round-trip mismatch\n  input: {}\n  decoded: {}\n  gcf: {:?}",
            i,
            val,
            decoded,
            gcf
        );
    }
}

// Adversarial key alphabet: mixes the empty string and every arrangement of '>'
// (the flatten path separator) with plain keys. The empty key and '>'-bearing keys
// are exactly what the flatten-eligibility guard must exclude: an empty path segment
// (leading/trailing/bare '>') the decoder cannot invert. Plain keys are included so
// flatten still triggers.
fn gen_adversarial_key(rng: &mut Rng) -> String {
    const KEYS: &[&str] = &[
        "", ">", ">>", "a>b", "a>", ">b", ">a>", "a>>b", "a", "b", "c", "id", "m", "n",
    ];
    KEYS[rng.int(KEYS.len() as u32) as usize].to_string()
}

// Same flatten round-trip as test_flatten_roundtrip, but with keys that include the
// empty string and every arrangement of '>'. This exercises the empty-key exclusion in
// analyze_flattenable (src/generic.rs): without it, an empty key produces a bare/leading/
// trailing '>' path segment that the decoder cannot invert, silently corrupting the array.
// The liveness assert confirms the generator actually produced the adversarial keys.
#[test]
fn test_flatten_roundtrip_adversarial_keys() {
    let iterations = std::env::var("GCF_ITERATIONS")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(200_000usize);
    let mut rng = Rng::new(20260806);
    let mut saw_empty = false;
    let mut saw_gt = false;
    for i in 0..iterations {
        let val = gen_flattenable_array(&mut rng, gen_adversarial_key);
        if let Value::Array(rows) = &val {
            for row in rows {
                if let Value::Object(m) = row {
                    for k in m.keys() {
                        if k.is_empty() {
                            saw_empty = true;
                        }
                        if k.contains('>') {
                            saw_gt = true;
                        }
                    }
                }
            }
        }
        let gcf = encode_generic(&val);
        let decoded = decode_generic(&gcf).unwrap_or_else(|e| {
            panic!(
                "iteration {}: decode failed: {}\n  input: {}\n  gcf: {:?}",
                i, e, val, gcf
            );
        });
        assert!(
            structural_equal(&val, &decoded),
            "iteration {}: round-trip mismatch\n  input: {}\n  decoded: {}\n  gcf: {:?}",
            i,
            val,
            decoded,
            gcf
        );
    }
    assert!(
        saw_empty && saw_gt,
        "generator liveness: expected to see empty ({}) and '>'-bearing ({}) top-level keys",
        saw_empty,
        saw_gt
    );
}

#[test]
fn test_random_roundtrip() {
    let iterations = get_iterations();
    let mut rng = Rng::new(42);
    for i in 0..iterations {
        let val = gen_value(&mut rng, 0, 4);
        let gcf = encode_generic(&val);
        let decoded = decode_generic(&gcf).unwrap_or_else(|e| {
            panic!(
                "iteration {}: decode failed: {}\n  input: {}\n  gcf: {:?}",
                i, e, val, gcf
            );
        });
        assert!(
            structural_equal(&val, &decoded),
            "iteration {}: round-trip mismatch\n  input:   {}\n  decoded: {}\n  gcf: {:?}",
            i,
            val,
            decoded,
            gcf
        );
    }
}

#[test]
fn test_adversarial_roundtrip() {
    let collision_strings = vec![
        "true",
        "false",
        "-",
        "~",
        "^",
        "0",
        "1",
        "42",
        "-1",
        "3.14",
        "1e10",
        "-0",
        "",
        " ",
        "  ",
        " x",
        "x ",
        "#",
        "# comment",
        "@0",
        "@handle",
        "+1",
        ".5",
        "+.3",
        "01",
        "00",
        "null",
        "NULL",
        "|",
        ",",
        "=",
        "\"",
        "\\",
        "\n",
        "\r",
        "\t",
        "a|b",
        "a,b",
        "a=b",
        "hello world",
    ];

    let iterations = get_iterations();
    let mut rng = Rng::new(99);
    for i in 0..iterations {
        let val = if rng.float() < 0.3 {
            Value::String(
                collision_strings[rng.int(collision_strings.len() as u32) as usize].to_string(),
            )
        } else {
            gen_value(&mut rng, 0, 3)
        };
        let gcf = encode_generic(&val);
        let decoded = decode_generic(&gcf).unwrap_or_else(|e| {
            panic!(
                "iteration {}: decode failed: {}\n  input: {}\n  gcf: {:?}",
                i, e, val, gcf
            );
        });
        assert!(
            structural_equal(&val, &decoded),
            "iteration {}: round-trip mismatch\n  input:   {}\n  decoded: {}\n  gcf: {:?}",
            i,
            val,
            decoded,
            gcf
        );
    }
}