Skip to main content

agent_id_cli/
prime.rs

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
22Use `agent-id` to register and look up the permanent human-readable identity for a coding-agent session.
23
24The identity is keyed by the harness session ID. Do not invent a name or register a second name for the same session.
25
26Session discovery, in order:
27
281. An explicit `SESSION_ID` argument or `--session-id ID`.
292. `AGENT_ID_SESSION_ID`.
30
31The realm is discovered from `--realm NAME`, `AGENT_REALM` (for tests/overrides), or `$XDG_CONFIG_HOME/agent-id/realm` with `$HOME/.config/agent-id/realm` as the fallback. If no realm configuration exists, one is automatically selected and saved to the realm file.
32
33The registry is durable and defaults to `$XDG_DATA_HOME/agent-id`, or `$HOME/.local/share/agent-id` when `XDG_DATA_HOME` is unset. Set `AGENT_ID_HOME` for tests or an isolated registry.
34
35When you need to understand the identity system or discover other agents, run `agent-id prime`. The extension provides this guidance once per session branch, including tree navigation, without repeating it on every prompt.
36
37A missing session ID is an error. A missing lookup is an error; register the session first.
38
39## Examples
40
41```bash
42agent-id register "$AGENT_ID_SESSION_ID"
43agent-id register --family Oak "$AGENT_ID_SESSION_ID"
44agent-id lookup "$AGENT_ID_SESSION_ID"
45agent-id annotate --summary "Implementing checkout retries" "$AGENT_ID_SESSION_ID"
46agent-id annotate --clear-summary "$AGENT_ID_SESSION_ID"
47agent-id annotate --state working "$AGENT_ID_SESSION_ID"
48agent-id annotate --state blocked "$AGENT_ID_SESSION_ID"
49agent-id annotate --clear-state "$AGENT_ID_SESSION_ID"
50agent-id annotate --cwd "$PWD" "$AGENT_ID_SESSION_ID"
51agent-id annotate --clear-cwd "$AGENT_ID_SESSION_ID"
52agent-id register --json --family Oak "$AGENT_ID_SESSION_ID"
53```
54
55The default 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."#;
56
57    if prelude_only {
58        return prelude.to_string();
59    }
60    format!(
61        "{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 the 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 the session ID explicitly\n--json              Print the complete assignment as JSON\n```\n\nFails if the identifier has not been registered.\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--session-id ID     Provide the session ID explicitly\n--json              Print the complete assignment as JSON\n```\n\nSummary, state, and working-directory updates are independent; omitted fields remain unchanged. State timestamps are generated by agent-id and are available in JSON as `state.updated_at`. Working-directory metadata is an optional host-provided path and is not inferred from prompt text.\n\n### `agent-id discover` — List recent identities\n\n```\n--limit N           Maximum records (default 20; zero means all)\n--recent HOURS      Only records updated within this many hours\n--realm NAME        Only records in this realm\n--json              Print the complete assignments as JSON\n```\n\nResults are sorted by `updated_at`, newest first. Human-readable results include summaries, states, and working directories when present; timestamps remain available in JSON.\n\n### `agent-id prune` — Remove old identity assignments\n\n```\n--before TIMESTAMP  Delete records updated before this RFC 3339 timestamp\n--dry-run           Preview matches 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 manual in a JSON object\n```"
62    )
63}