agent-first-data 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,13 +19,6 @@ console.log(render(event.toJSON(), "plain"));
19
19
 
20
20
  Useful names use TypeScript casing: `render`, `outputOptionsForPolicy`, `redactedValue`, `redactUrlSecrets`, `normalizeUtcOffset`, `isValidRfc3339Date`, `isValidRfc3339Time`, `isValidRfc3339`, `isValidBcp47`, `decodeProtocolEvent`, `cliParseOutput`, `cliParseLogFilters`, `buildCliError`, `buildCliVersion`, and `cliHandleVersionOrContinue`.
21
21
 
22
- Skill admin and stream redirection are not re-exported from the package root; import them from their own subpaths:
23
-
24
- ```typescript
25
- import { runSkillAdmin } from "agent-first-data/skill";
26
- import { installStreamRedirectFromRawArgs } from "agent-first-data/stream-redirect";
27
- ```
28
-
29
22
  ## Behavior Notes
30
23
 
31
24
  - Default redaction replaces every `_secret` or configured secret-name subtree with `***`, including objects and arrays.
@@ -33,12 +26,10 @@ import { installStreamRedirectFromRawArgs } from "agent-first-data/stream-redire
33
26
  - YAML keeps original keys and values (structure-preserving, like JSON), sorting keys by UTF-16 code unit order and quoting/escaping unsafe keys and string scalars. Plain strips formatting suffixes, formats values, sorts the same way, and renders nested objects/arrays as canonical JSON.
34
27
  - Logging records use `kind:"log"` with a nested `log` payload and a separate `level` field, so error-level logs are not terminal protocol errors.
35
28
  - `buildCliError(message, hint?)` returns a strict-ready CLI error with `error.retryable:false` and `trace:{}`.
36
- - Use `cliHandleVersionOrContinue()` before argument parsing so bare `--version` stays conventional and `--version --output json|yaml|plain` stays structured.
37
- - Use `installStreamRedirectFromRawArgs()` (from `agent-first-data/stream-redirect`) before version/help handling if a CLI exposes `--stdout-file` or `--stderr-file`; stderr is redirected as native diagnostics, not JSON. Node fd-level redirection is process-lifetime because the standard library does not expose `dup2` restoration.
29
+ - Use `cliHandleVersionOrContinue()` before argument parsing so `--version`/`-V` always answers with a structured protocol-v1 `kind:"result"` version event — JSON by default, or `--output yaml|plain`/`--json` for another format; there is no conventional bare-text form. Pass your own value-taking global flag names so their value is not mistaken for the subcommand boundary.
38
30
 
39
31
  ## Reference
40
32
 
41
- - Full convention and API groups: [docs/overview.md](https://github.com/agentfirstkit/agent-first-data/blob/main/docs/overview.md)
42
33
  - Formal cross-language contract: [spec/agent-first-data.md](https://github.com/agentfirstkit/agent-first-data/blob/main/spec/agent-first-data.md)
43
34
  - Conformance fixtures: [spec/fixtures](https://github.com/agentfirstkit/agent-first-data/tree/main/spec/fixtures)
44
35
  - Agent skill: [skills/agent-first-data/SKILL.md](https://github.com/agentfirstkit/agent-first-data/blob/main/skills/agent-first-data/SKILL.md)
package/dist/cli.d.ts CHANGED
@@ -165,29 +165,42 @@ export declare class CliEmitter {
165
165
  */
166
166
  finishResult(payload: JsonValue): number;
167
167
  }
168
- /** Build a standard CLI version value. */
169
- export declare function buildCliVersion(version: string): Event;
170
168
  /**
171
- * Render CLI version output.
172
- * Pass an OutputFormat for AFDATA JSON/YAML/plain. Pass null to preserve
173
- * conventional "<name> <version>" text.
169
+ * Build a standard CLI version event: a `kind:"result"` event whose payload is
170
+ * `{ code: "version", name, version }`, plus `display_name`/`build` when given.
171
+ * `name` is the short/bin identity (e.g. `"afdata"`); `displayName` is an
172
+ * optional human-facing product name (e.g. `"Agent-First Data"`); `build` is an
173
+ * opaque caller-supplied identifier (a git commit SHA, for example) — its
174
+ * meaning is entirely up to the caller. Both are `undefined` when unavailable,
175
+ * and simply absent from the payload. Note the JSON field is `display_name`
176
+ * (snake_case) even though the parameter is `displayName`.
174
177
  */
175
- export declare function cliRenderVersion(name: string, version: string, format?: OutputFormat | null): string;
178
+ export declare function buildCliVersion(name: string, displayName: string | undefined, version: string, build: string | undefined): Event;
179
+ /**
180
+ * Render a CLI version response as a protocol-v1 event in `format`. There is no
181
+ * conventional-text path — `format` is required.
182
+ */
183
+ export declare function cliRenderVersion(name: string, displayName: string | undefined, version: string, build: string | undefined, format: OutputFormat): string;
176
184
  /**
177
185
  * Render version output if --version/-V is present; otherwise return undefined.
178
186
  * Throws for malformed version requests, for example `--version --output xml`.
179
187
  *
180
- * One blessed behavior, not configurable: a bare version request renders
181
- * conventional "<name> <version>" text; `--json` or `--output <fmt>` /
182
- * `--output=<fmt>` alongside it selects the structured AFDATA payload
183
- * instead.
188
+ * The one blessed behavior: `--version` always answers with a protocol-v1
189
+ * `kind:"result"` version event — JSON by default, or `--output yaml|plain` (or
190
+ * `--json`) for another format. There is no conventional bare-text path.
191
+ *
192
+ * `valueFlags` is the caller's own value-taking global long flags (with or
193
+ * without leading dashes; the leading dashes are stripped internally). It is the
194
+ * TS analog of Rust's `&clap::Command`: the pre-parser consults it so a global
195
+ * flag's space-separated value (e.g. `--log request,startup`) is never mistaken
196
+ * for the subcommand boundary, which would hide a later `--version`/`--output`.
184
197
  *
185
198
  * Only a top-level version request is recognized: scanning stops at the first
186
199
  * positional argument (the subcommand) or `--`, so `tool sub --version <value>`
187
200
  * leaves `--version` for the subcommand's parser rather than printing the
188
201
  * tool version.
189
202
  */
190
- export declare function cliHandleVersionOrContinue(rawArgs: string[], name: string, version: string): string | undefined;
203
+ export declare function cliHandleVersionOrContinue(rawArgs: string[], valueFlags: string[], name: string, displayName: string | undefined, version: string, build: string | undefined): string | undefined;
191
204
  /**
192
205
  * Build a standard CLI parse error value. This function cannot fail.
193
206
  * Use when flag parsing fails or a flag value is invalid.
package/dist/cli.js CHANGED
@@ -274,34 +274,70 @@ export class CliEmitter {
274
274
  return this.finish(jsonResult(payload).build(), 0);
275
275
  }
276
276
  }
277
- /** Build a standard CLI version value. */
278
- export function buildCliVersion(version) {
279
- return jsonResult({ code: "version", version }).build();
277
+ /**
278
+ * Build a standard CLI version event: a `kind:"result"` event whose payload is
279
+ * `{ code: "version", name, version }`, plus `display_name`/`build` when given.
280
+ * `name` is the short/bin identity (e.g. `"afdata"`); `displayName` is an
281
+ * optional human-facing product name (e.g. `"Agent-First Data"`); `build` is an
282
+ * opaque caller-supplied identifier (a git commit SHA, for example) — its
283
+ * meaning is entirely up to the caller. Both are `undefined` when unavailable,
284
+ * and simply absent from the payload. Note the JSON field is `display_name`
285
+ * (snake_case) even though the parameter is `displayName`.
286
+ */
287
+ export function buildCliVersion(name, displayName, version, build) {
288
+ return jsonResult({
289
+ code: "version",
290
+ name,
291
+ version,
292
+ ...(displayName !== undefined ? { display_name: displayName } : {}),
293
+ ...(build !== undefined ? { build } : {}),
294
+ }).build();
280
295
  }
281
296
  /**
282
- * Render CLI version output.
283
- * Pass an OutputFormat for AFDATA JSON/YAML/plain. Pass null to preserve
284
- * conventional "<name> <version>" text.
297
+ * Render a CLI version response as a protocol-v1 event in `format`. There is no
298
+ * conventional-text path — `format` is required.
285
299
  */
286
- export function cliRenderVersion(name, version, format = null) {
287
- const rendered = format === null ? `${name} ${version}` : render(buildCliVersion(version), format);
300
+ export function cliRenderVersion(name, displayName, version, build, format) {
301
+ const rendered = render(buildCliVersion(name, displayName, version, build), format);
288
302
  return `${rendered.replace(/\n+$/u, "")}\n`;
289
303
  }
304
+ /**
305
+ * Split a flag argument into its long name (leading dashes stripped, any
306
+ * `=value` suffix dropped) and inline value. Returns `undefined` name for a
307
+ * non-flag or the bare `-`. `--output=` yields an empty-string inline value.
308
+ */
309
+ function splitFlag(arg) {
310
+ if (!arg.startsWith("-") || arg === "-")
311
+ return { name: undefined, inlineValue: undefined };
312
+ const eq = arg.indexOf("=");
313
+ const flag = eq === -1 ? arg : arg.slice(0, eq);
314
+ const inlineValue = eq === -1 ? undefined : arg.slice(eq + 1);
315
+ const name = flag.replace(/^-+/u, "");
316
+ if (name === "")
317
+ return { name: undefined, inlineValue: undefined };
318
+ return { name, inlineValue };
319
+ }
290
320
  /**
291
321
  * Render version output if --version/-V is present; otherwise return undefined.
292
322
  * Throws for malformed version requests, for example `--version --output xml`.
293
323
  *
294
- * One blessed behavior, not configurable: a bare version request renders
295
- * conventional "<name> <version>" text; `--json` or `--output <fmt>` /
296
- * `--output=<fmt>` alongside it selects the structured AFDATA payload
297
- * instead.
324
+ * The one blessed behavior: `--version` always answers with a protocol-v1
325
+ * `kind:"result"` version event — JSON by default, or `--output yaml|plain` (or
326
+ * `--json`) for another format. There is no conventional bare-text path.
327
+ *
328
+ * `valueFlags` is the caller's own value-taking global long flags (with or
329
+ * without leading dashes; the leading dashes are stripped internally). It is the
330
+ * TS analog of Rust's `&clap::Command`: the pre-parser consults it so a global
331
+ * flag's space-separated value (e.g. `--log request,startup`) is never mistaken
332
+ * for the subcommand boundary, which would hide a later `--version`/`--output`.
298
333
  *
299
334
  * Only a top-level version request is recognized: scanning stops at the first
300
335
  * positional argument (the subcommand) or `--`, so `tool sub --version <value>`
301
336
  * leaves `--version` for the subcommand's parser rather than printing the
302
337
  * tool version.
303
338
  */
304
- export function cliHandleVersionOrContinue(rawArgs, name, version) {
339
+ export function cliHandleVersionOrContinue(rawArgs, valueFlags, name, displayName, version, build) {
340
+ const valueFlagSet = new Set(valueFlags.map((flag) => flag.replace(/^-+/u, "")));
305
341
  let versionRequested = false;
306
342
  let outputFormat;
307
343
  let outputError;
@@ -314,6 +350,7 @@ export function cliHandleVersionOrContinue(rawArgs, name, version) {
314
350
  // git/cargo/clap: this pre-parser only owns a top-level version request.
315
351
  if (!arg.startsWith("-"))
316
352
  break;
353
+ const { name: flagName, inlineValue } = splitFlag(arg);
317
354
  if (arg === "--version" || arg === "-V") {
318
355
  versionRequested = true;
319
356
  i += 1;
@@ -329,15 +366,21 @@ export function cliHandleVersionOrContinue(rawArgs, name, version) {
329
366
  i += 1;
330
367
  continue;
331
368
  }
332
- if (arg === "--output" || arg.startsWith("--output=")) {
369
+ // `--output-to` takes a value but does not affect version text output.
370
+ // Consume its space-separated value so it is not mistaken for the
371
+ // subcommand boundary (which would hide a later `--version`/`--output`).
372
+ if (flagName === "output-to") {
373
+ const hasSpaceValue = inlineValue === undefined && rawArgs[i + 1] !== undefined && !rawArgs[i + 1].startsWith("-");
374
+ i += hasSpaceValue ? 2 : 1;
375
+ continue;
376
+ }
377
+ if (flagName === "output") {
333
378
  let value;
334
- let step = 1;
335
- if (arg.startsWith("--output=")) {
336
- value = arg.slice("--output=".length);
379
+ if (inlineValue !== undefined) {
380
+ value = inlineValue;
337
381
  }
338
382
  else if (rawArgs[i + 1] !== undefined && !rawArgs[i + 1].startsWith("-")) {
339
383
  value = rawArgs[i + 1];
340
- step = 2;
341
384
  }
342
385
  if (value === undefined) {
343
386
  outputError = new Error("missing value for --output: expected json, yaml, or plain");
@@ -356,16 +399,20 @@ export function cliHandleVersionOrContinue(rawArgs, name, version) {
356
399
  outputError = e;
357
400
  }
358
401
  }
359
- i += step;
402
+ i += inlineValue !== undefined || value === undefined ? 1 : 2;
360
403
  continue;
361
404
  }
362
- i += 1;
405
+ // Any other flag: consult the caller's own value-taking global flags so a
406
+ // flag's space-separated value is never mistaken for the subcommand
407
+ // boundary above.
408
+ const hasSpaceValue = inlineValue === undefined && rawArgs[i + 1] !== undefined && !rawArgs[i + 1].startsWith("-");
409
+ i += hasSpaceValue && flagName !== undefined && valueFlagSet.has(flagName) ? 2 : 1;
363
410
  }
364
411
  if (!versionRequested)
365
412
  return undefined;
366
413
  if (outputError !== undefined)
367
414
  throw outputError;
368
- return cliRenderVersion(name, version, outputFormat ?? null);
415
+ return cliRenderVersion(name, displayName, version, build, outputFormat ?? "json");
369
416
  }
370
417
  /**
371
418
  * Build a standard CLI parse error value. This function cannot fail.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-first-data",
3
- "version": "0.20.0",
3
+ "version": "0.22.0",
4
4
  "description": "A naming convention that lets AI agents understand your data without being told what it means, plus a CLI and library for reading and safely editing structured JSON, TOML, YAML, dotenv, and INI documents.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,14 +14,6 @@
14
14
  ".": {
15
15
  "import": "./dist/index.js",
16
16
  "types": "./dist/index.d.ts"
17
- },
18
- "./skill": {
19
- "import": "./dist/skill.js",
20
- "types": "./dist/skill.d.ts"
21
- },
22
- "./stream-redirect": {
23
- "import": "./dist/stream_redirect.js",
24
- "types": "./dist/stream_redirect.d.ts"
25
17
  }
26
18
  },
27
19
  "files": [
@@ -1,280 +0,0 @@
1
- ---
2
- name: agent-first-data
3
- description: Apply and review the formal Agent-First Data specification using the Rust/Go/Python/TypeScript libraries or afdata CLI for naming, redaction, formatting, protocol envelopes, logging, linting, validation, and reading or safely editing structured JSON/TOML/YAML/dotenv/INI documents (and the +++/--- frontmatter block of a Markdown page) by dot-path. Use proactively when writing or reviewing structured data, configs, logs, transport payloads, database/wire fields, CLI output, compatibility-sensitive public and persistent field names, or when reading or editing a config/document/Markdown-frontmatter file from the shell.
4
- ---
5
-
6
- <!-- Canonical source: skills/agent-first-data/SKILL.md.
7
- Mirrored byte-for-byte into go/python/typescript assets/ by scripts/sync_offline_assets.py.
8
- Edit only here; run that script (checked by scripts/test.sh) to propagate. -->
9
-
10
- # Agent-First Data
11
-
12
- Use AFDATA when naming fields or reviewing structured output. The contract is naming-based: suffixes communicate units, formatting, and redaction semantics without relying on free-text inference.
13
-
14
- ## Specification workflow
15
-
16
- Treat the formal specification as normative, not the skill summary or an
17
- example payload. In a repository checkout, read these spore-root-relative
18
- sources as needed:
19
-
20
- - `spec/agent-first-data.md` — complete cross-language naming, formatting,
21
- redaction, protocol, logging, and CLI contract.
22
- - `spec/registry.json` — exact suffix metadata.
23
- - `spec/protocol-v1.schema.json` — protocol event schema.
24
- - `spec/fixtures/` — cross-language conformance cases.
25
-
26
- When only the installed skill is available, use the bundled equivalents:
27
-
28
- - `references/rules.md` — detailed suffix rules, output formatting, protocol templates, logging, CLI flags, and review checklist.
29
- - `references/registry.json` — machine-readable suffix registry; use this as the exact source for suffix metadata.
30
- - `references/protocol-v1.schema.json` — protocol event schema for `kind:"result"`, `kind:"error"`, `kind:"progress"`, and `kind:"log"` envelopes.
31
-
32
- If an example or README conflicts with the formal spec, follow the spec and
33
- report the discrepancy. Do not invent suffix meanings or reduce the protocol
34
- schema.
35
-
36
- ## Library workflow
37
-
38
- Use the library at serialization boundaries so redaction and formatting are
39
- not reimplemented ad hoc. Rust applications can build a typed protocol event
40
- and render it in the required output format:
41
-
42
- ```bash
43
- cargo add agent-first-data --no-default-features
44
- pip install agent-first-data
45
- npm install agent-first-data
46
- go get github.com/agentfirstkit/agent-first-data/go
47
- ```
48
-
49
- ```rust
50
- use agent_first_data::{json_result, render, OutputFormat, OutputOptions};
51
- use serde_json::json;
52
-
53
- let event = json_result(json!({
54
- "latency_ms": 1280,
55
- "api_key_secret": "sk-live-example",
56
- })).build();
57
- let rendered = render(event.as_value(), OutputFormat::Json, &OutputOptions::default());
58
- assert!(rendered.contains("\"api_key_secret\":\"***\""));
59
- ```
60
-
61
- For a finite CLI execution implemented in Rust, create one `CliEmitter`, select
62
- `OutputFormat` through `cli_parse_output`, enable strict protocol, and emit one
63
- terminal result or error. Handle version before clap with
64
- `cli_handle_version_or_continue(raw_args, name, version)`.
65
- For non-CLI HTTP/MCP/SSE serialization, call `redacted_value()` or
66
- `redact_url_secrets()` explicitly at the boundary.
67
-
68
- The same library contract is available for Go, Python, and TypeScript. Use the
69
- runtime's native names while preserving the same builders, output formats,
70
- redaction behavior, registry, schema, and conformance fixtures; consult its
71
- root-level `go/README.md`, `python/README.md`, or `typescript/README.md` when
72
- working in that runtime.
73
-
74
- ## Naming and redaction decisions
75
-
76
- - Add unit suffixes to ambiguous numeric fields: `_ms`, `_s`, `_bytes`, `_percent`, `_sats`, `_msats`, currency suffixes, and timestamp suffixes.
77
- - Use `_secret` for values/subtrees that must redact to `***` in every output format.
78
- - Use `_url` only for whole URL values whose userinfo password or suffix-named secret query params should be scrubbed.
79
- - Do not scan arbitrary free text for secrets. Rename fields or configure explicit secret names at the serialization boundary.
80
- - Before changing public API, wire, database, or persistent field names, report the compatibility impact and get approval.
81
-
82
- ## Evaluation rules
83
-
84
- - Trigger this skill for field naming, structured output, configs, logs, CLI output, protocol events, MCP/HTTP/SSE payloads, database columns, wire fields, or persisted JSON. Do not wait for the user to say "AFDATA" when the work changes those surfaces.
85
- - In a repository checkout, treat `spec/agent-first-data.md`,
86
- `spec/registry.json`, and `spec/protocol-v1.schema.json` as authoritative; in
87
- an installed skill, use their bundled `references/` equivalents. Do not
88
- replace them with invented suffix meanings, a reduced schema, or free-text
89
- interpretation.
90
- - For secrets, fix the field name or serializer configuration. Use `_secret` or configured exact secret names; do not rely on scanning arbitrary strings or object debug output.
91
- - Run `afdata lint` on JSON/JSONL examples, JSON Schema, MCP schemas, or serialized samples when the CLI is available. Run `afdata validate` on finite protocol events/streams. If a check cannot run, report that explicitly.
92
- - Before modifying an external project's public API, wire format, database schema, or persisted field names, stop and report the compatibility impact; request approval before applying the change.
93
-
94
- ## Redaction behavior contract
95
-
96
- Fields named `_secret` or `_url` and values passing through AFDATA output rendering (`render`, in any format) are automatically redacted: secrets become `***`, URL userinfo passwords and suffix-named query parameters are scrubbed. When serializing outside output formatters (HTTP, MCP, SSE), call `redacted_value()` or `redact_url_secrets()` at the serialization boundary. PII redaction, domain-specific privacy policies (header allowlists, API scope sensitivity), and per-field secret naming are each spore's responsibility; the library provides field-name-based mechanics only.
97
-
98
- ## Logging behavior contract
99
-
100
- One-time CLI output uses `json_log()` or `CliEmitter` for a single event; serialization via `render()` applies output formatting. Construct the emitter in finite mode for a one-shot command (`result`→stdout, `error`/`progress`/`log`→stderr) or stream mode for an interleaved event stream (all events on one destination) — see the Output stream contract. Long-running services or processes depending on structured logging (tonic, sqlx, hyper, etc. via tracing) initialize with `afdata_tracing::try_init()` (Rust only) to wire process-wide logging; other languages emit log events explicitly via builders or integrate their own structured logging.
101
-
102
- ## CLI workflow
103
-
104
- Install `afdata` when a shell workflow needs validation, formatting, or skill
105
- administration without embedding a library:
106
-
107
- ```bash
108
- cargo install agent-first-data
109
- ```
110
-
111
- Use it for deterministic checks:
112
-
113
- ```bash
114
- afdata lint payload.json
115
- afdata validate events.jsonl
116
- afdata render payload.json --output yaml
117
- afdata skill validate skills/example-skill
118
- ```
119
-
120
- - Every command's first positional is its input; `-` reads stdin explicitly.
121
- There is no implicit stdin fallback, and omitting the input is always a
122
- usage error that names the fix — never a hang.
123
- - Run `afdata lint` for ordinary JSON/JSONL, JSON Schema, MCP input/output
124
- schemas, or a structured document (`--input-format`/extension detection
125
- picks TOML/YAML/dotenv/INI; JSON/JSONL stays the default dual-mode input).
126
- - Run `afdata validate` for AFDATA protocol events or finite event streams.
127
- - Run `afdata render` to apply AFDATA redaction/formatting to arbitrary JSON
128
- or JSONL. `--output` accepts `json`, `yaml`, or `plain`; JSON and YAML keep
129
- original keys/values (structure-preserving), Plain is the lossy human
130
- renderer.
131
- - Treat findings as contract issues unless the owning tool intentionally
132
- documents a non-AFDATA field.
133
-
134
- ## Output stream contract
135
-
136
- AFDATA CLIs route protocol events by consumption mode, not by event shape.
137
-
138
- - A **finite one-shot command** (the default) puts `kind:"result"` on `stdout`
139
- and `kind:"error"`/`progress`/`log` on `stderr`, so `x=$(tool …)` never
140
- captures a failure as data, `tool … >/dev/null` never swallows a diagnostic,
141
- and `tool … | next` never pipes an error envelope in as input. The exit
142
- code — not the stream — is set by the terminal kind; routing follows `kind`,
143
- not the exit code.
144
- - An **event stream** (interleaved events consumed in order) keeps every event,
145
- including `error`, on one stream so ordering survives.
146
- - `afdata` and any CLI built on the library expose `--output-to
147
- <split|stdout|stderr>` (default `split`). `stdout`/`stderr` collapse the whole
148
- stream — errors included — onto one destination, for a consumer that would
149
- rather read one stream and branch on `kind`. `--output` picks the format,
150
- `--output-to` picks the destination. Send an event stream to a file by
151
- collapsing it (`--output-to stdout`) and redirecting (`--stdout-file PATH`).
152
- - The raw-scalar readers (`value`/`paths`/`keys`) are intrinsically split (raw
153
- data on `stdout`, error envelope on `stderr`) and reject a non-default
154
- `--output-to`.
155
- - When building a CLI on the library, choose the mode by how the output is
156
- consumed — do not reflexively pick finite. A plain one-shot shell command uses
157
- **finite** mode (errors → stderr, so shell capture and pipelines stay safe). A
158
- tool whose output is itself an event stream (interleaved `progress`/`log` then
159
- a terminal `result`/`error` — e.g. streaming query rows), or that is
160
- agent-facing and wants the caller to read a single structured channel and
161
- branch on `kind`, uses **stream/unified** mode (every event, errors included,
162
- on `stdout`). A stream's terminal error MUST stay on the stream, so such a tool
163
- keeps even its usage errors on `stdout` for one consistent channel. Never write
164
- to `stderr` ad hoc — route every diagnostic through the emitter so redaction
165
- and framing still apply.
166
- - Emit a terminal event with the emitter's `finish` / `finish_result` helpers,
167
- which map the write outcome to a broken-pipe-safe process exit code (success →
168
- the given code, a hung-up reader → 0, any other write failure → a nonzero
169
- output-failure code) instead of hand-rolling that dance — they work in either
170
- mode (the emitter's mode decides the stream; `finish` only maps the outcome).
171
- Construct an error with the error builder (`json_error(code, message)` with
172
- `hint`/`retryable`/extra fields as needed) — that builder *is* the error type —
173
- and pass the built event to `finish`; there is no separate error-emitting
174
- convenience.
175
-
176
- ## Document workflow
177
-
178
- Use `get`/`value`/`paths`/`keys` to read a JSON, TOML, YAML, dotenv, or INI
179
- document and `set`/`add`/`remove`/`unset` to edit one in place, instead of
180
- `sed`, regular expressions, or a generic reserializer — those can reorder
181
- keys, drop comments, or change quoting/anchors in the rest of the file. Every
182
- document command's first positional is `FILE` (or `-` for stdin on a read
183
- command); mutation commands never read stdin and reject `-` as a usage error.
184
- To edit only the metadata block of a Markdown page, pass `--input-format
185
- toml-frontmatter` (a `+++` block) or `yaml-frontmatter` (a `---` block): the
186
- same read/mutate commands then address dot-paths inside the frontmatter and
187
- leave the Markdown body byte-for-byte untouched. Frontmatter is never
188
- auto-detected — the format must be named explicitly, since extension detection
189
- only ever resolves to a whole-file format — so reach for it deliberately
190
- instead of `sed`-ing a page's `+++`/`---` header.
191
-
192
- - `get FILE` returns the whole document as one AFDATA record
193
- (`code:"document"`); `get FILE KEY` narrows it to one dot-path value in the
194
- same shape. Pass every known sensitive legacy field name with
195
- `--secret-name FIELD` (an exact field name, not a dot-path). A secret-named
196
- leaf (`_secret`/`_SECRET`, or an exact `--secret-name` match) is still
197
- redacted to `"***"` even when directly targeted — use `value
198
- --reveal-secret` when the real value is genuinely needed.
199
- - `value FILE KEY` writes only the raw scalar to stdout, with no AFDATA
200
- envelope and no forced trailing newline, for direct use in shell
201
- substitution: `value=$(afdata value config.toml server.host)`. Only scalars
202
- are supported; a container errors (`document_not_scalar`) — use `get` for a
203
- subtree. A secret-named leaf errors (`document_secret_redacted`) unless
204
- `--reveal-secret` is passed. On any failure `value`'s stdout is always
205
- empty — the error envelope goes to stderr instead — so
206
- `port=$(afdata value config.toml server.port)` never captures a JSON error
207
- as data. `--default VAL` prints `VAL` instead of erroring when the path is
208
- absent or the value is `null` (an empty string is a real value and does not
209
- trigger it), collapsing the common "read or fall back" shell idiom into one
210
- call with no `2>/dev/null` guard needed.
211
- - `paths FILE [KEY]` and `keys FILE [KEY]` enumerate a container's immediate
212
- children, one per line, with the same empty-stdout-on-failure contract as
213
- `value`. **`paths` output feeds back into afdata** (full grammar-escaped
214
- dot-paths from the root — pipe straight into `value`/`get`/`unset` or
215
- extend with `"$p.field"`); **`keys` output goes to external tools** (raw,
216
- unescaped key names/array indices — what a package manager or another CLI
217
- expects). Never use `paths`'s output as a literal name, or `keys`'s output
218
- as a dot-path — they only diverge on a key containing a dot or space, so
219
- the wrong one works until it silently doesn't. A scalar target errors
220
- (`document_not_container`); `--missing-ok` turns a missing KEY specifically
221
- into empty output + exit 0 (other errors still fail). Path grammar does not
222
- escape spaces or other IFS characters, so the only correct consumption
223
- shape is a `while IFS= read -r` loop, never `for x in $(...)`:
224
-
225
- ```sh
226
- while IFS= read -r p; do
227
- afdata value config.toml "$p.slug"
228
- done < <(afdata paths config.toml extra.tools)
229
- ```
230
- - Escape a literal dot as `\.` and a literal backslash as `\\` in a dot-path;
231
- every command shares this grammar (`paths`'s escaped output re-parses
232
- through the exact same rules it was emitted from), so an unrecognized
233
- escape or empty segment is always an error, never a guess.
234
- - `add`/`remove` operate on a keyed list — an array of objects addressed by a
235
- slug — and always require an explicit `--slug-field FIELD`; the CLI never
236
- infers an identity field. Keyed editing is implemented for JSON and YAML
237
- only; TOML, dotenv, and INI return a structured "not implemented" error, so
238
- edit their list entries by writing the whole value another way. Adding a
239
- slug that already exists, or removing/unsetting one that does not, is
240
- always an error (`document_slug_exists`/`document_slug_not_found`/
241
- `document_path_not_found`) — never a silent no-op; wrap the call yourself
242
- when idempotence is wanted.
243
- - `set`'s bare VALUE (and `add`'s `FIELD=VALUE`) is always a plain string —
244
- no shape-guessing, so a leading-zero ID or SHA never gets reinterpreted as
245
- a number. Writing an exact type goes through `--value-type
246
- string|number|bool|null|json` instead; `json` is the only entry point for
247
- an array or object. Overwriting an *existing* scalar of a different kind
248
- with a bare VALUE is a usage error that names both escape hatches (keep the
249
- type with `--value-type <kind>`, or convert explicitly with `--value-type
250
- string`) — a brand-new key never needs `--value-type`.
251
- - For `set`, choose `--secret-from stdin|prompt|fd:<N>|env:<VAR>` instead of a
252
- positional value when the value must not land in shell history or `ps`:
253
- `stdin` (pipe), `prompt` (human, terminal echo off), `fd:N` (Unix
254
- automation that needs stdin for something else), or `env:VAR` (already in
255
- the process environment). There is no inline `--secret-from=VALUE`-style
256
- argv source — that would defeat the point.
257
- - Mutations preserve the rest of the source document — comments, key order,
258
- unrelated formatting — and refuse to write through a symlink or (on Unix) a
259
- multi-link hardlink target. Treat a failed mutation as leaving the original
260
- file untouched rather than assuming a partial write, and reread before
261
- retrying. A format backend that isn't compiled in, or input outside the
262
- documented dotenv/INI dialect, is a structured error to report, not a cue to
263
- hand-edit the file. A successful mutation's result carries the `path`
264
- actually written, for confirming which file changed when scripting several.
265
- - Branch on `error.code`, not the message text: document errors use stable
266
- codes (`document_path_not_found`, `document_type_mismatch`,
267
- `document_slug_not_found`, `document_slug_exists`, `document_parse_failed`,
268
- `document_io_failed`, and a few narrower ones), while a malformed
269
- invocation (`--input-format`, `--value-type`, `FIELD=VALUE`, a mutation
270
- given `-`, …) is always `document_usage_error` at exit 2 — distinct from
271
- every runtime document error, which exits 1.
272
-
273
- ## Review checklist
274
-
275
- 1. Numeric fields with units carry the right suffix and safe integer/string representation.
276
- 2. Timestamps use `_epoch_s`, `_epoch_ms`, decimal-string `_epoch_ns`, or `_rfc3339`.
277
- 3. Secrets use `_secret` or configured exact secret names; no fallback path leaks them.
278
- 4. CLI output uses AFDATA helpers for JSON/YAML/plain formatting and structured errors, and routes by consumption mode (finite: `result`→stdout, `error`→stderr; stream: one destination) rather than writing to stderr ad hoc.
279
- 5. Logs use AFDATA structured logging or equivalent field-name redaction.
280
- 6. Transport payloads preserve the protocol envelope when AFDATA compliance is claimed.
@@ -1,66 +0,0 @@
1
- {
2
- "$schema": "https://json-schema.org/draft/2020-12/schema",
3
- "$id": "https://agentfirstkit.org/schemas/agent-first-data/protocol-v1.schema.json",
4
- "title": "Agent-First Data Protocol v1 Event",
5
- "type": "object",
6
- "oneOf": [
7
- {"$ref": "#/$defs/resultEvent"},
8
- {"$ref": "#/$defs/errorEvent"},
9
- {"$ref": "#/$defs/progressEvent"},
10
- {"$ref": "#/$defs/logEvent"}
11
- ],
12
- "$defs": {
13
- "trace": {
14
- "type": "object"
15
- },
16
- "resultEvent": {
17
- "type": "object",
18
- "properties": {
19
- "kind": {"const": "result"},
20
- "result": true,
21
- "trace": {"$ref": "#/$defs/trace"}
22
- },
23
- "required": ["kind", "result"],
24
- "additionalProperties": false
25
- },
26
- "errorEvent": {
27
- "type": "object",
28
- "properties": {
29
- "kind": {"const": "error"},
30
- "error": {
31
- "type": "object",
32
- "properties": {
33
- "code": {"type": "string", "minLength": 1},
34
- "message": {"type": "string", "minLength": 1},
35
- "hint": {"type": "string"}
36
- },
37
- "required": ["code", "message"],
38
- "additionalProperties": true
39
- },
40
- "trace": {"$ref": "#/$defs/trace"}
41
- },
42
- "required": ["kind", "error"],
43
- "additionalProperties": false
44
- },
45
- "progressEvent": {
46
- "type": "object",
47
- "properties": {
48
- "kind": {"const": "progress"},
49
- "progress": true,
50
- "trace": {"$ref": "#/$defs/trace"}
51
- },
52
- "required": ["kind", "progress"],
53
- "additionalProperties": false
54
- },
55
- "logEvent": {
56
- "type": "object",
57
- "properties": {
58
- "kind": {"const": "log"},
59
- "log": true,
60
- "trace": {"$ref": "#/$defs/trace"}
61
- },
62
- "required": ["kind", "log"],
63
- "additionalProperties": false
64
- }
65
- }
66
- }