Skip to main content

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//!   real serialized values 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 across Codex, Claude Code, opencode, and Hermes; 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::AtomicWrite;
101mod formatting;
102mod lint;
103mod output;
104mod protocol;
105mod redaction;
106mod validation;
107
108#[cfg(feature = "cli")]
109pub use cli::{
110    CliEmitter, CliEmitterError, LogFilters, build_cli_version, cli_parse_log_filters,
111    cli_parse_output, cli_render_version,
112};
113#[cfg(feature = "cli")]
114pub use cli_afdata::{
115    CliHandler, CliMain, CliStop, PreparedCli, ProcessEmitter, build_afdata_cli, cli_error_event,
116    cli_help_event, cli_invocation_invalid_event, cli_output_setup_failed_event,
117    cli_output_write_failed_event, cli_version_event, render_cli_reference, run_with_plan,
118};
119#[cfg(feature = "cli")]
120pub use cli_spec::{
121    ArgSpec, ArgSyntax, ArgValueType, BoundCliSpec, BoundInvocation, BoundOutcome, BuiltCliSpec,
122    CliError, CliErrorRule, CliHelpV2, CliOutcome, CliShape, CliSpec, CliSpecError, CliValue,
123    Combination, CommandSpec, ExitCodeSpec, FixedValue, HostScheme, InvocationReadError,
124    InvocationReadProblem, OutputLifecycle, OutputPlan, OutputSpec, ResolvedDocs, ResolvedHelp,
125    ResolvedInvocation, ResolvedVersion, SourceError, SourceScheme, SourceSet, SyntheticInvocation,
126    ValueSource,
127};
128pub use error_catalog::{ErrorCatalog, ErrorCatalogError, ErrorSpec};
129pub use formatting::render;
130pub use lint::{
131    LintFinding, LintOptions, LintSeverity, RedactionCanaryError, assert_no_lint_findings,
132    assert_no_lint_findings_with_options, assert_redaction_canary_absent, assert_strict_event,
133    lint_field_value, lint_value,
134};
135pub use output::OutputFormat;
136#[cfg(feature = "cli")]
137pub use output::OutputTo;
138pub use protocol::{
139    BuildError, DecodedError, DecodedEvent, DecodedLog, DecodedProgress, DecodedResult,
140    ErrorBuilder, Event, EventDecodeError, LogBuilder, LogLevel, ProgressBuilder,
141    ProtocolViolation, ResultBuilder, build_cli_error, decode_protocol_event, json_error, json_log,
142    json_progress, json_result, validate_protocol_event, validate_protocol_stream,
143};
144pub use redaction::{
145    OutputOptions, PlainStyle, RedactionPolicy, Redactor, redact_argv, redact_url_secrets,
146    redact_urls_in_text, redacted_value,
147};
148pub use validation::{
149    is_valid_bcp47, is_valid_rfc3339, is_valid_rfc3339_date, is_valid_rfc3339_time,
150    normalize_utc_offset,
151};
152
153#[cfg(test)]
154pub(crate) use formatting::{extract_currency_code, format_bytes_human, format_with_commas};
155
156#[cfg(test)]
157mod tests;