agent-first-data 0.16.1

A naming convention that lets AI agents understand your data without being told what it means.
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
"""Tests for AFDATA output formatting — driven by shared spec/fixtures."""

import json
import os

from agent_first_data import (
    json_result,
    json_error,
    json_progress,
    json_log,
    LogLevel,
    validate_protocol_event,
    validate_protocol_stream,
    EventDecodeError,
    DecodedResult,
    DecodedError,
    DecodedProgress,
    DecodedLog,
    decode_protocol_event,
    RedactionPolicy,
    OutputStyle,
    OutputOptions,
    redacted_value,
    redact_url_secrets,
    output_json,
    output_yaml,
    output_plain,
    normalize_utc_offset,
    is_valid_rfc3339_date,
    is_valid_rfc3339_time,
)
from agent_first_data.format import (
    _format_bytes_human,
    _format_with_commas,
    _extract_currency_code,
    parse_size,
)

FIXTURES_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "spec", "fixtures")


def _load(name):
    with open(os.path.join(FIXTURES_DIR, name)) as f:
        return json.load(f)


def _redaction_options(case):
    opts = case.get("options", {})
    policy = RedactionPolicy(opts["policy"]) if "policy" in opts else None
    return OutputOptions(policy=policy, secret_names=opts.get("secret_names", ()))


# --- Redact fixtures ---


def test_redact_url_fixtures():
    for case in _load("redact_url.json"):
        name = case["name"]
        options = _redaction_options(case)
        got = redact_url_secrets(case["input"], secret_names=options.secret_names)
        assert got == case["expected"], f"[redact_url/{name}] got {got!r}"


def test_redaction_options_fixtures():
    for case in _load("redaction_options.json"):
        name = case["name"]
        output_options = _redaction_options(case)
        expected = case["expected"]

        got = redacted_value(case["input"], secret_names=output_options.secret_names, policy=output_options.policy)
        assert got == expected, f"[redaction_options/{name}] value mismatch: {got}"

        got_json = json.loads(output_json(case["input"], options=output_options))
        assert got_json == expected, f"[redaction_options/{name}] json mismatch: {got_json}"

        if "expected_yaml" in case:
            got_yaml = output_yaml(case["input"], options=output_options)
            assert got_yaml == case["expected_yaml"], f"[redaction_options/{name}] yaml mismatch: {got_yaml!r}"
        if "expected_plain" in case:
            got_plain = output_plain(case["input"], options=output_options)
            assert got_plain == case["expected_plain"], f"[redaction_options/{name}] plain mismatch: {got_plain!r}"


def test_security_fixtures():
    fixture = _load("security.json")
    for case in fixture["redaction_cases"]:
        name = case["name"]
        output_options = _redaction_options(case)
        assert redacted_value(case["input"], secret_names=output_options.secret_names, policy=output_options.policy) == case["expected"]
        outputs = (
            output_json(case["input"], options=output_options),
            output_yaml(case["input"], options=output_options),
            output_plain(case["input"], options=output_options),
        )
        for output in outputs:
            for needle in case["must_contain"]:
                assert needle in output, f"[security/{name}] output missing {needle!r}: {output}"
            for needle in case["must_not_contain"]:
                assert needle not in output, f"[security/{name}] output leaked {needle!r}: {output}"


# --- Protocol fixtures ---


def _build_protocol_case_event(typ, args):
    """Build an Event from a protocol.json builder case.

    args vocabulary: "result" (payload), "code"+"message" (error),
    "hint" (-> .hint()), "retryable" (bool -> .retryable_if()),
    "fields" (object -> .fields()), "trace" (object -> .trace()), and complete
    progress/log payloads.
    """
    kind = typ.split("_", 1)[0]

    if kind == "result":
        builder = json_result(args["result"])
    elif kind == "error":
        builder = json_error(args["code"], args["message"])
        if "hint" in args:
            builder = builder.hint(args["hint"])
        if "retryable" in args:
            builder = builder.retryable_if(args["retryable"])
    elif kind == "progress":
        builder = json_progress({"message": args["message"], **args.get("fields", {})})
    elif kind == "log":
        builder = json_log({"level": args["level"], "message": args["message"], **args.get("fields", {})})
    else:
        raise ValueError(f"unknown fixture type: {typ}")

    if "fields" in args and kind not in ("progress", "log"):
        builder = builder.fields(args["fields"])
    if "trace" in args:
        builder = builder.trace(args["trace"])
    return builder.build()


def test_protocol_fixtures():
    for case in _load("protocol.json"):
        name = case["name"]
        if "invalid" in case:
            try:
                validate_protocol_event(case["invalid"], strict=False)
            except ValueError:
                pass
            else:
                raise AssertionError(f"[protocol/{name}] invalid event unexpectedly passed")
            continue

        result = _build_protocol_case_event(case["type"], case["args"]).to_dict()
        validate_protocol_event(result, strict=False)
        validate_protocol_event(result, strict=True)
        expected = case["expected"]
        assert result == expected, (
            f"[protocol/{name}] deep equality failed:\n"
            f"  got:      {result}\n"
            f"  expected: {expected}"
        )


def test_protocol_stream_fixtures():
    for case in _load("protocol_streams.json"):
        name = case["name"]
        valid = case["valid"]
        try:
            validate_protocol_stream(case["events"], strict=False)
        except ValueError as exc:
            assert not valid, f"[protocol_streams/{name}] unexpected error: {exc}"
        else:
            assert valid, f"[protocol_streams/{name}] invalid stream unexpectedly passed"


def test_protocol_strict_fixtures():
    for case in _load("protocol_strict.json"):
        try:
            validate_protocol_stream(case["events"], strict=True)
        except ValueError:
            assert not case["valid"], case["name"]
        else:
            assert case["valid"], case["name"]


def test_error_builder_rejects_reserved_extension_fields():
    # 0.16 spec: reserved fields must not be writable, errors are collected at build()
    try:
        json_error("explicit", "message").fields({"code": "wrong", "message": "wrong", "hint": "wrong", "detail": 1}).build()
        assert False, "should have raised EventBuildError"
    except Exception as e:
        assert "cannot override reserved field" in str(e)


# --- Helper fixtures ---


def test_helper_fixtures():
    for case in _load("helpers.json"):
        name = case["name"]
        for tc in case["cases"]:
            inp, expected = tc
            if name == "format_bytes_human":
                got = _format_bytes_human(inp)
                assert got == expected, f"[helpers/{name}({inp})] got {got!r}"
            elif name == "format_with_commas":
                got = _format_with_commas(inp)
                assert got == expected, f"[helpers/{name}({inp})] got {got!r}"
            elif name == "extract_currency_code":
                got = _extract_currency_code(inp)
                assert got == expected, f"[helpers/{name}({inp!r})] got {got!r}"
            elif name == "parse_size":
                got = parse_size(inp)
                assert got == expected, f"[helpers/{name}({inp!r})] got {got!r}"
            elif name == "normalize_utc_offset":
                got = normalize_utc_offset(inp)
                assert got == expected, f"[helpers/{name}({inp!r})] got {got!r}"
            elif name == "is_valid_rfc3339_date":
                got = is_valid_rfc3339_date(inp)
                assert got == expected, f"[helpers/{name}({inp!r})] got {got!r}"
            elif name == "is_valid_rfc3339_time":
                got = is_valid_rfc3339_time(inp)
                assert got == expected, f"[helpers/{name}({inp!r})] got {got!r}"


def test_output_format_fixtures():
    for case in _load("output_formats.json"):
        name = case["name"]
        inp = json.loads(json.dumps(case["input"]))

        got_json = json.loads(output_json(inp))
        assert got_json == case["expected_json"], f"[output/{name}] json mismatch: {got_json}"

        got_yaml = output_yaml(inp)
        assert got_yaml == case["expected_yaml"], f"[output/{name}] yaml mismatch: {got_yaml!r}"

        got_plain = output_plain(inp)
        assert got_plain == case["expected_plain"], f"[output/{name}] plain mismatch: {got_plain!r}"


def test_output_yaml_raw_keeps_suffix_keys_and_structure():
    options = OutputOptions(
        policy=RedactionPolicy.RedactionTraceOnly,
        style=OutputStyle.Raw,
    )
    out = output_yaml(
        {
            "code": "result",
            "rows": [{"api_key_secret": "sk-live-1", "duration_ms": 42}],
            "trace": {"request_secret": "top-secret"},
        },
        options=options,
    )

    assert "rows:\n  -" in out
    assert 'api_key_secret: "sk-live-1"' in out
    assert "duration_ms: 42" in out
    assert 'request_secret: "***"' in out
    assert 'duration: "42ms"' not in out


def test_output_plain_raw_keeps_suffix_keys_and_redacts_trace():
    options = OutputOptions(
        policy=RedactionPolicy.RedactionTraceOnly,
        style=OutputStyle.Raw,
    )
    out = output_plain(
        {
            "duration_ms": 42,
            "trace": {"request_secret": "top-secret"},
        },
        options=options,
    )

    assert "duration_ms=42" in out
    assert "trace.request_secret=***" in out
    assert "duration=42ms" not in out


def test_output_with_options_defaults_to_readable_style():
    out = output_yaml(
        {"duration_ms": 42},
        options=OutputOptions(
            policy=RedactionPolicy.RedactionNone,
            style=OutputStyle.Readable,
        ),
    )
    assert 'duration: "42ms"' in out
    assert "duration_ms:" not in out


def test_output_json_exception_field_is_readable():
    out = output_json({"error": Exception("timeout")})
    parsed = json.loads(out)
    assert parsed["error"] == "timeout"


def test_output_json_unsupported_value_does_not_leak_secret():
    class SecretRepr:
        def __repr__(self) -> str:
            return "Secret(sk-live-123)"

    out = output_json({"meta": SecretRepr(), "api_key_secret": "sk-live-123"})
    assert "sk-live-123" not in out
    parsed = json.loads(out)
    assert parsed["api_key_secret"] == "***"
    assert parsed["meta"].startswith("<unsupported:")


def test_output_json_circular_reference():
    v = {}
    v["self"] = v
    out = output_json(v)
    parsed = json.loads(out)
    assert parsed["self"] == "<unsupported:circular>"


def test_output_json_with_trace_only_redacts_only_trace():
    out = output_json(
        {
            "code": "ok",
            "result": {"api_key_secret": "sk-live-123"},
            "trace": {"request_secret": "top-secret"},
        },
        options=OutputOptions.for_policy(RedactionPolicy.RedactionTraceOnly),
    )
    parsed = json.loads(out)
    assert parsed["trace"]["request_secret"] == "***"
    assert parsed["result"]["api_key_secret"] == "sk-live-123"


def test_output_json_with_none_keeps_secrets():
    out = output_json(
        {"api_key_secret": "sk-live-123"},
        options=OutputOptions.for_policy(RedactionPolicy.RedactionNone),
    )
    parsed = json.loads(out)
    assert parsed["api_key_secret"] == "sk-live-123"


def test_output_options_for_policy_sets_only_redaction_policy():
    options = OutputOptions.for_policy(RedactionPolicy.RedactionNone)
    assert options.policy == RedactionPolicy.RedactionNone
    assert options.secret_names == ()
    assert options.style == OutputStyle.Readable


def test_redacted_value_returns_safe_copy():
    inp = {"api_key_secret": "sk-live-123", "nested": {"token_secret": "tok"}}
    got = redacted_value(inp)
    assert got["api_key_secret"] == "***"
    assert got["nested"]["token_secret"] == "***"
    assert inp["api_key_secret"] == "sk-live-123"


def test_redacted_value_redacts_secret_subtree_by_default():
    inp = {"db_secret": {"password_secret": "real", "host": "localhost"}}
    default = redacted_value(inp)
    assert default["db_secret"] == "***"


def test_max_depth_marker_is_not_secret_redaction_marker():
    inp = "leaf"
    for _ in range(300):
        inp = {"next": inp}
    out = output_json(inp)
    assert "<afdata:max-depth>" in out
    assert "***" not in out


# --- decode_protocol_event ---


def test_decode_protocol_event_result():
    event = json_result({"rows": 2}).build()
    decoded = decode_protocol_event(output_json(event.to_dict()))
    assert isinstance(decoded, DecodedResult)
    assert decoded.result == {"rows": 2}
    assert decoded.trace == {}


def test_decode_protocol_event_error():
    event = json_error("not_found", "missing").hint("check the id").field("id", "abc").retryable().build()
    decoded = decode_protocol_event(output_json(event.to_dict()))
    assert isinstance(decoded, DecodedError)
    assert decoded.code == "not_found"
    assert decoded.message == "missing"
    assert decoded.retryable is True
    assert decoded.hint == "check the id"
    assert decoded.fields == {"id": "abc"}
    assert decoded.trace == {}


def test_decode_protocol_event_progress():
    event = json_progress({"message": "halfway", "percent": 50}).build()
    decoded = decode_protocol_event(output_json(event.to_dict()))
    assert isinstance(decoded, DecodedProgress)
    assert decoded.progress == {"message": "halfway", "percent": 50}
    assert decoded.trace == {}


def test_decode_protocol_event_log():
    event = json_log({"level": "warn", "message": "slow query", "duration_ms": 900}).build()
    decoded = decode_protocol_event(output_json(event.to_dict()))
    assert isinstance(decoded, DecodedLog)
    assert decoded.log == {"level": "warn", "message": "slow query", "duration_ms": 900}
    assert decoded.trace == {}


def test_decode_protocol_event_invalid_json_raises():
    import pytest

    with pytest.raises(EventDecodeError):
        decode_protocol_event("not json")


def test_decode_protocol_event_invalid_envelope_raises():
    import pytest

    with pytest.raises(EventDecodeError):
        decode_protocol_event(json.dumps({"kind": "result"}))


def test_decode_protocol_event_fails_strict_validation():
    import pytest

    # Missing trace fails the strict profile even though the shape is otherwise valid.
    with pytest.raises(EventDecodeError):
        decode_protocol_event(json.dumps({"kind": "result", "result": {}}))