1use anyhow::Result;
2use serde_json::json;
3
4use crate::cli::PrimeArgs;
5
6pub fn execute(args: &PrimeArgs) -> Result<()> {
7 let documentation = generate(args.prelude);
8 if args.json {
9 println!(
10 "{}",
11 serde_json::to_string_pretty(&json!({"documentation": documentation}))?
12 );
13 } else {
14 println!("{documentation}");
15 }
16 Ok(())
17}
18
19pub fn generate(prelude_only: bool) -> String {
20 let prelude = r#"# agent-id — Portable agent identity registry
21
22Agent ID provides the permanent human-readable identity for a coding-agent session. The identity is keyed by the harness session ID; never invent a name or register a second name for the same session.
23
24## OMP workflow
25
26The OMP extension automatically looks up or registers the current session, records its working directory and session file, and manages its lifecycle state.
27
28Call `agent-id current --json` to inspect the complete current assignment. The command uses `AGENT_ID_SESSION_ID` and never registers a missing identity. Call `agent-id discover` directly when you need to find other identities. Discover omits only identities in the `stopped` state by default; use `agent-id discover --all` to include them. Inside Herdr, discover adds live pane, workspace, status, and worktree context when the reported OMP session file matches an assignment.
29
30Lifecycle hooks publish `working`, `idle`, and `stopped` automatically. Use `agent-id annotate` to publish `waiting` or `blocked`, or to set or clear a summary or namespaced extension value when an explicit update is needed. Automatic summaries use completed agent turns; explicit updates remain authoritative.
31
32## CLI fallback
33
34Direct CLI use is normally unnecessary under OMP. If the extension is unavailable, the CLI resolves a session from:
35
361. An explicit `SESSION_ID` argument or `--session-id ID`.
372. `AGENT_ID_SESSION_ID`.
38
39A missing session ID is an error. A missing lookup is an error; register the session first.
40
41## Examples
42
43```bash
44agent-id discover
45agent-id discover --recent 24 --json
46agent-id discover --all
47agent-id register "$AGENT_ID_SESSION_ID"
48agent-id register --family Oak "$AGENT_ID_SESSION_ID"
49agent-id lookup "$AGENT_ID_SESSION_ID"
50agent-id annotate --summary "Implementing checkout retries" "$AGENT_ID_SESSION_ID"
51agent-id annotate --clear-summary "$AGENT_ID_SESSION_ID"
52agent-id annotate --state waiting "$AGENT_ID_SESSION_ID"
53agent-id annotate --state blocked "$AGENT_ID_SESSION_ID"
54agent-id annotate --clear-state "$AGENT_ID_SESSION_ID"
55agent-id annotate --cwd "$PWD" "$AGENT_ID_SESSION_ID"
56agent-id annotate --clear-cwd "$AGENT_ID_SESSION_ID"
57```
58
59The default CLI output is the full name. Use `--json` when a tool needs the session ID, name parts, realm, slug, optional timestamped summary, typed activity state, working directory, and created/updated timestamps."#;
60
61 if prelude_only {
62 return prelude.to_string();
63 }
64 format!(
65 "{prelude}\n\n## Commands\n\n### `agent-id register [SESSION_ID]` — Allocate a permanent identity\n\n```\n--family NAME Prefer a family name, useful for child agents\n--realm NAME Select a computer realm\n--session-id ID Provide the session ID explicitly\n--json Print the complete assignment as JSON\n```\n\nRegistration is idempotent: an existing session keeps its identity and receives a new `updated_at`.\n\n### `agent-id lookup [IDENTIFIER]` — Read an existing identity\n\nIDENTIFIER may be a session ID, canonical name, or slug. Without an explicit identifier, lookup uses the session environment.\n\n```\n--session-id ID Provide an explicit session ID\n--json Print the complete assignment as JSON\n```\n\nFails if the identifier has not been registered.\n\n### `agent-id current` — Read the current session identity\n\n```\n--json Print the complete assignment as JSON\n```\n\nUses `AGENT_ID_SESSION_ID` to identify the current session and never registers a missing identity.\n\n### `agent-id annotate [SESSION_ID]` — Update activity metadata\n\n```\n--summary TEXT Set a concise summary (maximum 240 characters)\n--clear-summary Remove the summary\n--state VALUE Set working, idle, waiting, blocked, or stopped\n--clear-state Remove the activity state\n--cwd PATH Set the current working directory\n--clear-cwd Remove the current working directory\n--extension OWNER=JSON Set namespaced unstructured JSON metadata; repeatable\n--clear-extension NAME Remove one extension namespace; repeatable\n--session-id ID Provide the session ID explicitly\n--json Print the complete assignment as JSON\n```\n\nSummary, state, working-directory, and extension metadata updates are independent; omitted fields remain unchanged. Agent ID timestamps mutable fields. Extension updates atomically replace one owner's JSON value.\n\n### `agent-id discover` — List identities\n\n```\n--limit N Maximum records (default 20; zero means all)\n--recent HOURS Only include records updated within this many hours\n--realm NAME Only include records in this realm\n--all Include stopped assignments\n--json Print assignments and available runtime projections as JSON\n```\n\nStopped assignments are excluded by default. Use `--all` to include them. Results are sorted by `updated_at`, newest first. Inside Herdr, matching OMP session-file metadata adds a non-persistent live runtime projection.\n\n### `agent-id prune` — Remove old identity assignments\n\n```\n--before TIMESTAMP Delete records updated before this timestamp\n--dry-run Preview matching records without deleting\n--json Print the prune report as JSON\n```\n\nPruning applies by default and removes both session records and matching name claims.\n\n### `agent-id prime` — Print this workflow manual\n\n```\n--prelude Omit the command reference\n--json Wrap the documentation in a JSON object\n```"
66 )
67}