agent_first_data/lib.rs
1//! Agent-First Data (AFDATA) output formatting and protocol templates.
2//!
3//! Public APIs, grouped by concern (see each item's own docs for details;
4//! the full symbol list is the crate root's own rustdoc index, not repeated
5//! here — it drifts out of sync with a hand-maintained count otherwise):
6//! - Protocol v1 builders: [`json_result`], [`json_error`], [`json_progress`], [`json_log`]
7//! (each returns a builder; call `.build()`)
8//! - Protocol reader: [`decode_protocol_event`] parses and base-validates one protocol
9//! line into a typed [`DecodedEvent`]
10//! - Redaction: [`redacted_value`] / [`Redactor::value`] (JSON values), [`redact_url_secrets`] /
11//! [`Redactor::url`] (URL strings), [`redact_urls_in_text`] / [`Redactor::urls_in_text`]
12//! (explicit prose URL spans), [`redact_argv`] / [`Redactor::argv`] (command lines) —
13//! `Redactor` carries custom `secret_names`/`url_names`/`reveal_names`/`policy`;
14//! [`Redactor::value_at`] / [`Redactor::is_secret_path`] judge one selected value by
15//! its original route ([`document::PathStep`], from [`document::locate`]), so it reads
16//! exactly as it does inside the whole redacted document
17//! - Output rendering: [`render`] — the single `value × format × options → String` entry point
18//! for JSON, YAML, and plain (logfmt) output
19//! - Parse utilities: [`normalize_utc_offset`], [`is_valid_rfc3339_date`],
20//! [`is_valid_rfc3339_time`], [`is_valid_rfc3339`], [`is_valid_bcp47`]
21//! - Closed-world CLI compiler: [`CliSpec`], [`CommandSpec`], [`ArgSpec`],
22//! [`Combination`], and [`OutputSpec`] generate parsing, typed
23//! [`ResolvedInvocation`] values, output plans, and help-v2 from one registry;
24//! [`CliMain`] runs a registry CLI's whole lifecycle from `main`.
25//! - Established CLI utilities: [`cli_parse_output`], [`cli_parse_log_filters`]
26//! (returns [`LogFilters`]) and [`CliEmitter`] (events, raw bytes, exit code).
27//! - Domain errors and validation: [`ErrorSpec`] / [`ErrorCatalog`] declare
28//! stable public errors; [`lint_value`] and the `assert_*` helpers validate
29//! deterministic field contracts, protocol events and rendered output in tests.
30//! - Documents: [`document::Document`] provides source-preserving in-memory
31//! edits and typed [`document::Document::decode`]; [`document::DocumentFile`]
32//! adds capped reads, safe first creation, atomic commits, and
33//! [`document::DocumentFile::edit_and_validate`].
34//! - (feature `skill`): [`skill::validate_skill`] / [`skill::validate_skill_named`] — strict
35//! Agent Skills `SKILL.md` front-matter validation
36//! - (feature `skill-admin`): [`skill::run_skill_admin`] — install/uninstall/status a spore's
37//! embedded Agent Skill in the standard skill folders or one explicit directory; returns a typed
38//! [`skill::SkillReport`]. With `cli`, [`skill::skill_cli_commands`] and [`skill::SkillCliRequest`] mount the
39//! standard `skill status` / `install` / `uninstall` verbs on a program's own CLI
40//! - (feature `tracing`): [`afdata_tracing::AfdataLayer`] is a composable AFDATA
41//! logging layer with injectable writers and a nested-value
42//! [`afdata_tracing::StructuredLogHandle`]; [`afdata_tracing::try_init`] is
43//! the global-subscriber convenience entry point.
44//!
45//! The shared cross-language contract (which of these exist, under what name, in each of
46//! Rust/Python/TypeScript/Go) is tracked in `spec/api-surface.json` and cross-checked by
47//! `scripts/validate_api_surface.py`.
48
49#[cfg(feature = "tracing")]
50pub mod afdata_tracing;
51
52#[cfg(feature = "stream-redirect")]
53pub mod stream_redirect;
54
55#[cfg(feature = "skill-admin")]
56#[path = "skill.rs"]
57mod skill_admin;
58
59// The standard `skill` verbs for a program built on the closed-world CLI.
60#[cfg(all(feature = "cli", feature = "skill-admin"))]
61#[path = "skill_cli.rs"]
62mod skill_cli;
63
64#[cfg(feature = "skill")]
65#[path = "skill_validation.rs"]
66pub mod skill;
67
68/// Format-independent document values (dot-path access, typed coercion, and
69/// pluggable JSON/TOML/YAML/dotenv/INI backends, plus a read-only Markdown
70/// block reader).
71pub mod document;
72
73/// Reading a value that named where it is — an environment variable, an
74/// address inside a config file, a stream, a terminal prompt — and the policy
75/// that separates a printable value from a credential. The grammar itself is
76/// [`cli_spec::SourceSet`]; what may be done with the result is carried by the
77/// return type, [`value_source::SecretString`].
78#[cfg(feature = "cli")]
79pub mod value_source;
80
81mod error_catalog;
82
83// The closed-world CLI compiler: spec types, build gates, argv resolution, and
84// the help-v2 model. Nothing else in this crate may reference it — only the
85// adapter below does — which `cargo build --no-default-features` proves.
86#[cfg(feature = "cli")]
87mod cli_spec;
88
89// The AFDATA CLI surface: output format parsing, the emitter, and version
90// payloads.
91#[cfg(feature = "cli")]
92mod cli;
93
94// The one place the compiler and AFDATA meet.
95#[cfg(feature = "cli")]
96mod cli_afdata;
97
98// The one atomic file installation every writer in this crate goes through.
99mod atomic_file;
100pub use atomic_file::{AtomicError, AtomicStep, AtomicWrite, write_atomic};
101mod formatting;
102mod lint;
103mod lint_input;
104mod output;
105mod protocol;
106mod redaction;
107mod suffix;
108mod validation;
109
110#[cfg(feature = "cli")]
111pub use cli::{
112 CliEmitter, CliEmitterError, LogFilters, build_cli_version, cli_parse_log_filters,
113 cli_parse_output, cli_render_version,
114};
115#[cfg(feature = "cli")]
116pub use cli_afdata::{
117 CliHandler, CliMain, CliStop, PreparedCli, ProcessEmitter, build_afdata_cli, cli_error_event,
118 cli_help_event, cli_invocation_invalid_event, cli_output_setup_failed_event,
119 cli_output_write_failed_event, cli_version_event, render_cli_reference, run_with_plan,
120};
121#[cfg(feature = "cli")]
122pub use cli_spec::{
123 ArgSpec, ArgSyntax, ArgValueType, BoundCliSpec, BoundInvocation, BoundOutcome, BuiltCliSpec,
124 CliError, CliErrorRule, CliHelpV2, CliOutcome, CliShape, CliSpec, CliSpecError, CliValue,
125 Combination, CommandSpec, ExitCodeSpec, FixedValue, HostScheme, InvocationReadError,
126 InvocationReadProblem, OutputLifecycle, OutputPlan, OutputSpec, ResolvedDocs, ResolvedHelp,
127 ResolvedInvocation, ResolvedVersion, SourceError, SourceScheme, SourceSet, SyntheticInvocation,
128 ValueSource,
129};
130pub use error_catalog::{ErrorCatalog, ErrorCatalogError, ErrorSpec};
131pub use formatting::render;
132pub use lint::{
133 LintFinding, LintOptions, RedactionCanaryError, assert_no_lint_findings,
134 assert_redaction_canary_absent, assert_strict_event, lint_field_value, lint_value,
135};
136pub use output::OutputFormat;
137#[cfg(feature = "cli")]
138pub use output::OutputTo;
139pub use protocol::{
140 BuildError, DecodedError, DecodedEvent, DecodedLog, DecodedProgress, DecodedResult,
141 ErrorBuilder, Event, EventDecodeError, LogBuilder, LogLevel, ProgressBuilder,
142 ProtocolViolation, ResultBuilder, build_cli_error, decode_protocol_event, json_error, json_log,
143 json_progress, json_result, validate_protocol_event, validate_protocol_stream,
144};
145pub use redaction::{
146 OutputOptions, PlainStyle, RedactionPolicy, Redactor, redact_argv, redact_url_secrets,
147 redact_urls_in_text, redacted_value,
148};
149pub use validation::{
150 is_valid_bcp47, is_valid_rfc3339, is_valid_rfc3339_date, is_valid_rfc3339_time,
151 normalize_utc_offset,
152};
153
154#[cfg(test)]
155pub(crate) use formatting::{extract_currency_code, format_bytes_human, format_with_commas};
156
157#[cfg(test)]
158mod tests;