#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tier {
Everyday,
Advanced,
Hidden,
}
pub fn tier_of(verb: &str) -> Tier {
match crate::cli::commands::command_help_tier(verb) {
"everyday" => Tier::Everyday,
"hidden" => Tier::Hidden,
_ => Tier::Advanced,
}
}
pub fn everyday_verbs() -> Vec<&'static str> {
crate::cli::commands::root_commands_for_help_visibility("everyday")
}
fn primary_loop_verbs(catalog: &crate::cli::commands::CommandCatalogOutput) -> Vec<&'static str> {
everyday_verbs()
.into_iter()
.filter(|verb| {
catalog
.command_by_display(verb)
.is_some_and(|entry| entry.help_rank <= 70)
})
.collect()
}
pub fn advanced_verbs() -> Vec<&'static str> {
crate::cli::commands::root_commands_for_advanced_help()
}
fn catalog_summary(catalog: &crate::cli::commands::CommandCatalogOutput, verb: &str) -> String {
catalog
.command_by_display(verb)
.map(|entry| entry.summary.clone())
.unwrap_or_default()
}
pub fn print_help(cmd: &clap::Command, topic: &[String]) -> std::io::Result<()> {
crate::cli::render::write_stdout(&render_help(cmd, topic))
.map_err(|err| std::io::Error::other(err.to_string()))
}
pub fn render_help(cmd: &clap::Command, topic: &[String]) -> String {
use std::fmt::Write;
let mut out = String::new();
match topic {
[] => {
let catalog = crate::cli::commands::build_command_catalog();
let _ = writeln!(out, "Heddle — AI-native version control");
let _ = writeln!(out);
let _ = writeln!(out, "Common loop:");
for name in primary_loop_verbs(&catalog) {
let blurb = catalog_summary(&catalog, name);
if blurb.is_empty() {
continue;
}
let _ = writeln!(out, " {:<10} {}", name, blurb);
}
let _ = writeln!(out);
let _ = writeln!(
out,
"Existing Git: heddle status -> heddle adopt -> heddle verify -> heddle commit -m \"...\" -> heddle push"
);
let _ = writeln!(
out,
"Isolated work: heddle start <name> --path ../<name> -> heddle commit -m \"...\" -> heddle ready -> heddle land"
);
let _ = writeln!(out);
let _ = writeln!(
out,
"Nearby: `heddle undo`, `heddle verify`, `heddle push`, `heddle pull`."
);
let _ = writeln!(
out,
"Start here: `heddle init`, `heddle adopt`, or `heddle clone`."
);
let _ = writeln!(
out,
"Coming from Git? Run `heddle help git-concepts` for the concept map."
);
let _ = writeln!(out);
let _ = writeln!(
out,
"Output: text is the default; pass `--output json` for the \
full machine contract (stable `output_kind`, exit codes, recovery \
templates), or `--output json-compact` for the decision surface \
only (fewer tokens, same `output_kind`). No TTY/pipe auto-detection. \
Details: `heddle help output-formats`."
);
let _ = writeln!(out);
let _ = writeln!(
out,
"Run `heddle help model` for the short mental model, \
`heddle help advanced` for power surfaces, automation, and Git interop, \
or `heddle help <topic>` for a topic page (e.g. `git-concepts`, \
`git-overlay`, \
`threads`, `daemon`, `signals`, `bridge`, `operation-ids`, \
`remotes`, `output-formats`, `git-dependencies`)."
);
}
[name] if name == "advanced" => {
let catalog = crate::cli::commands::build_command_catalog();
let _ = writeln!(out, "{}", ADVANCED_HELP);
for (title, verbs) in crate::cli::commands::advanced_help_groups() {
let mut lines = Vec::new();
for name in verbs {
let blurb = catalog_summary(&catalog, name);
if blurb.is_empty() {
continue;
}
let canonical = crate::cli::commands::command_canonical_command(name)
.map(|canonical| format!(" [use `{canonical}`]"))
.unwrap_or_default();
lines.push(format!(" {name:<14} {blurb}{canonical}"));
}
if lines.is_empty() {
continue;
}
let _ = writeln!(out, "{title}:");
for line in lines {
let _ = writeln!(out, "{line}");
}
let _ = writeln!(out);
}
}
[name] if topic_text(name).is_some() => {
let _ = writeln!(out, "{}", topic_text(name).expect("checked above"));
}
path => {
if let Some(mut subcommand) = help_command_for_path(cmd, path) {
let _ = write!(out, "{}", subcommand.render_help());
} else {
let name = path.join(" ");
let _ = writeln!(
out,
"no topic or command '{name}'. Run `heddle help advanced` for \
the full advanced list, or `heddle help` for the \
curated everyday surface."
);
}
}
}
out
}
pub fn print_direct_help_for_raw(
cmd: &clap::Command,
raw: &[String],
) -> Option<std::io::Result<()>> {
let rendered = render_direct_help_for_raw(cmd, raw)?;
Some(
crate::cli::render::write_stdout(&rendered)
.map_err(|err| std::io::Error::other(err.to_string())),
)
}
pub fn render_direct_help_for_raw(cmd: &clap::Command, raw: &[String]) -> Option<String> {
let path = command_path_from_raw_help_request(cmd, raw)?;
Some(match help_command_for_path(cmd, &path) {
Some(mut subcommand) => {
if command_has_long_help_content(&subcommand) {
subcommand.render_long_help().to_string()
} else {
subcommand.render_help().to_string()
}
}
None => render_help(cmd, &path),
})
}
fn command_has_long_help_content(command: &clap::Command) -> bool {
command.get_long_about().is_some()
|| command.get_after_long_help().is_some()
|| command.get_arguments().any(|arg| {
!arg.is_hide_set()
&& (arg.get_long_help().is_some()
|| arg
.get_possible_values()
.iter()
.any(|value| value.get_help().is_some()))
})
}
const CAPTURE_AGENT_FLAG_IDS: &[&str] = &[
"agent_provider",
"agent_model",
"agent_session",
"agent_segment",
"policy",
"no_policy",
"no_agent",
"split",
"into",
"paths",
];
fn reveal_capture_agent_flags(command: clap::Command) -> clap::Command {
command.mut_args(|arg| {
if CAPTURE_AGENT_FLAG_IDS.contains(&arg.get_id().as_str()) {
arg.hide(false)
} else {
arg
}
})
}
pub fn print_capture_agent_help(cmd: &clap::Command) -> std::io::Result<()> {
crate::cli::render::write_stdout(&render_capture_agent_help(cmd))
.map_err(|err| std::io::Error::other(err.to_string()))
}
pub fn render_capture_agent_help(cmd: &clap::Command) -> String {
let capture = find_subcommand_or_alias(cmd, "capture")
.expect("capture subcommand exists in the clap command tree");
let bin_name = format!("{} {}", cmd.get_name(), capture.get_name());
let mut help = reveal_capture_agent_flags(capture.clone()).bin_name(bin_name);
help.render_long_help().to_string()
}
fn help_command_for_path(cmd: &clap::Command, path: &[String]) -> Option<clap::Command> {
if path.is_empty() {
return None;
}
let mut current = cmd;
let mut bin_name = cmd.get_name().to_string();
let mut canonical_path = Vec::new();
for part in path {
let subcommand = find_subcommand_or_alias(current, part)?;
bin_name.push(' ');
bin_name.push_str(part);
canonical_path.push(subcommand.get_name().to_string());
current = subcommand;
}
let mut help = current.clone().bin_name(bin_name);
for arg in cmd
.get_arguments()
.filter(|arg| arg.is_global_set() && !arg.is_hide_set())
{
help = help.arg(arg.clone());
}
if crate::cli::commands::command_runtime_contract(&canonical_path.join(" "))
.is_some_and(|contract| contract.supports_op_id)
&& let Some(arg) = cmd
.get_arguments()
.find(|arg| arg.get_long() == Some("op-id"))
{
help = help.arg(arg.clone().hide(false).value_name("UUID"));
}
Some(help)
}
fn global_option_takes_value(command: &clap::Command, token: &str) -> Option<bool> {
command
.get_arguments()
.find(|arg| {
arg.get_long()
.is_some_and(|long| token == format!("--{long}"))
|| arg
.get_short()
.is_some_and(|short| token == format!("-{short}"))
})
.map(|arg| arg.get_action().takes_values())
}
fn find_subcommand_or_alias<'a>(
command: &'a clap::Command,
name: &str,
) -> Option<&'a clap::Command> {
command.find_subcommand(name).or_else(|| {
command
.get_subcommands()
.find(|subcommand| subcommand.get_all_aliases().any(|alias| alias == name))
})
}
fn command_path_from_raw_help_request(cmd: &clap::Command, raw: &[String]) -> Option<Vec<String>> {
if !raw.iter().any(|arg| arg == "--help" || arg == "-h") {
return None;
}
if raw
.iter()
.all(|arg| arg == "--help" || arg == "-h" || arg.starts_with('-'))
{
return None;
}
let mut current = cmd;
let mut path = Vec::new();
let mut skip_next = false;
for token in raw {
if skip_next {
skip_next = false;
continue;
}
if token == "--help" || token == "-h" {
continue;
}
if let Some(takes_value) = global_option_takes_value(current, token) {
skip_next = takes_value;
continue;
}
if token.starts_with('-') {
continue;
}
if let Some(subcommand) = find_subcommand_or_alias(current, token) {
path.push(subcommand.get_name().to_string());
current = subcommand;
}
}
(!path.is_empty()).then_some(path)
}
pub fn render_for_args(args: &[&str]) -> Option<String> {
use clap::{CommandFactory, Parser};
use crate::cli::cli_args::{Cli, Commands};
let command = Cli::command();
let raw: Vec<String> = args.iter().map(|arg| (*arg).to_string()).collect();
if raw.is_empty() || raw == ["--help"] || raw == ["-h"] || raw == ["help"] {
return Some(render_help(&command, &[]));
}
if let Some(rendered) = render_direct_help_for_raw(&command, &raw) {
return Some(rendered);
}
if let Ok(cli) = Cli::try_parse_from(std::iter::once("heddle".to_string()).chain(raw.clone()))
&& let Commands::Help { topics } = &cli.command
{
return Some(render_help(&command, topics));
}
if let Ok(cli) = Cli::try_parse_from(std::iter::once("heddle".to_string()).chain(raw.clone()))
&& let Commands::Capture(args) = &cli.command
&& args.help_agent
{
return Some(render_capture_agent_help(&command));
}
None
}
pub fn topic_text(topic: &str) -> Option<&'static str> {
Some(match topic {
"advanced" => ADVANCED_HELP,
"agent-flags" => AGENT_FLAGS_TOPIC,
"agent" | "daemon" => DAEMON_TOPIC,
"output-formats" | "output-format" | "output" => OUTPUT_FORMATS_TOPIC,
"clone" => CLONE_TOPIC,
"git-overlay" => GIT_OVERLAY_TOPIC,
"git-concepts" | "git-concept-map" | "git-veteran" => GIT_CONCEPTS_TOPIC,
"model" | "mental-model" | "concepts" => MODEL_TOPIC,
"threads" => THREADS_TOPIC,
"operation-ids" | "idempotency" => OPERATION_IDS_TOPIC,
"remotes" => REMOTES_TOPIC,
"git-dependencies" | "git-deps" | "git-dependency" => GIT_DEPENDENCIES_TOPIC,
"review" => REVIEW_TOPIC,
"discuss" | "discussions" => DISCUSS_TOPIC,
"bridge" | "footer" | "notes" => BRIDGE_TOPIC,
"signals" | "risk-signals" => SIGNALS_TOPIC,
_ => return None,
})
}
const ADVANCED_HELP: &str = "Advanced commands for power users, agents, automation, Git interop, and recovery.\n\
\n\
The default `heddle help` curates the native loop: init/adopt/clone,\n\
status/diff/commit/start, ready/land/push/pull, resolve/continue/abort,\n\
doctor/verify. Power nouns such as thread/workspace/remote/bridge/agent and\n\
Git adapter commands live behind this topic. Use `heddle help\n\
<verb>` for curated topics or `heddle <verb> --help` for the full clap-derived\n\
docs.\n\
\n\
This is intentional. The everyday surface stays minimal so first-time users aren't\n\
overwhelmed; agents and power users reach for the advanced affordances when they\n\
need them.\n";
const OUTPUT_FORMATS_TOPIC: &str = r#"Output formats — `--output text | json | json-compact`.
`text` is the default, always. There is no TTY/pipe auto-detection — the
default never switches under you, so scripts and humans see the same thing
until a flag says otherwise.
`--output json` emits the full machine contract: a stable `output_kind`
discriminator, exit codes, and recovery templates. Schemas per verb:
`heddle schemas <verb>`; the catalog of which commands emit what:
`heddle help --output json`.
`--output json-compact` emits only the decision-surface fields —
`output_kind`, `status`/`coordination_status`, `blockers`, `next_action`,
`changed_paths`, `conflicts` — fewer tokens, same `output_kind`, so callers
can still dispatch on it. Commands advertise `supports_json_compact` in the
command catalog.
Related: `heddle help operation-ids` for idempotent retries, `heddle help
agent-flags` for capture attribution overrides.
"#;
const CLONE_TOPIC: &str = r#"Cloning — Git repositories and Heddle remotes.
heddle clone <remote> <dir> [--thread <name>] [--depth <n>]
Run `heddle clone --help` for the flag list.
# Which thread the clone lands on (no --thread)
- Git-overlay clones (cloning a Git repository) land on the remote's
advertised default branch (its Git HEAD); if the remote advertises
none, they fall back to a thread named `main`, then to the
alphabetically first imported thread.
- Native-local and hosted Heddle clones target `main` directly with no
fallback chain; if the remote has no `main` thread the clone fails —
pass `--thread <name>` to select one.
- Clone never prompts.
# Shallow clones (--depth, Heddle remotes only)
--depth 0 (the default) clones full history. --depth N fetches only the
tip plus N generations of ancestry (--depth 1: the tip plus its immediate parents),
so `heddle log` stops at the depth boundary; history older than that is
not present locally — re-clone at a greater --depth (or --depth 0) to
obtain it. Git-overlay clones reject a nonzero --depth; --depth 0 is accepted
and clones full history.
Depth controls history extent only — how many states the clone fetches —
and says nothing about object contents. Whether a state's blobs are
present locally or fetched lazily is a separate concern that `--depth`
never governs. Advanced/planned flags `--lazy` and `--filter blob:none`
skip blob content and hydrate it on demand for hosted/network Heddle
remotes; local and Git-overlay clone paths reject them today.
See `heddle help threads` for the thread model and `heddle help remotes`
for remote management.
"#;
const AGENT_FLAGS_TOPIC: &str = r#"Agent automation flags for `heddle capture`.
These flags are hidden from the everyday `heddle capture --help` so it stays
terse for human use. They let an automated caller override agent attribution
and split captures across threads. Run `heddle capture --help-agent` to see
them inline in capture's own help.
Attribution overrides (each falls back to the matching env var, then config):
--agent-provider <NAME> Override HEDDLE_AGENT_PROVIDER.
--agent-model <NAME> Override HEDDLE_AGENT_MODEL.
--agent-session <ID> Override the active agent session id (HEDDLE_SESSION_ID).
--agent-segment <ID> Override the active session segment (HEDDLE_SESSION_SEGMENT).
--policy <ID> Override HEDDLE_AGENT_POLICY.
--no-policy Omit policy attribution.
--no-agent Omit agent attribution.
Path splitting (no env equivalent):
--split Split selected paths into another thread instead of
capturing the whole worktree.
--into <THREAD> Target thread when using --split.
--path <PATH> Repository-relative path prefix to include with
--split (repeatable).
Attribution precedence (highest first): explicit flag, active thread actor,
env var, harness probe, active session, user config, repo config. See
`crates/cli/src/cli/commands/snapshot.rs` for the full cascade.
"#;
const DAEMON_TOPIC: &str = "Two daemons — both have legitimate uses; they are not interchangeable.\n\
\n\
`heddle daemon` — FUSE mount-daemon control plane. Owns FUSE sessions for\n\
`--workspace virtualized --daemon` threads. Linux only.\n\
Subcommands: serve | status | stop.\n\
\n\
`heddle agent serve` — Local gRPC daemon over a Unix socket inside the repo's\n\
`.heddle/sockets/`. Hosts the local agent\n\
services (state-review, discussion, signal, operation-log\n\
query, hook) so agents avoid per-command\n\
process startup latency. Mode: same-user only;\n\
peer-credential checks are enforced.\n";
const MODEL_TOPIC: &str = r#"Heddle mental model — the everyday loop in one screen.
Heddle is built around saved states and isolated threads. Git compatibility is
an output and interop layer, not the thing you have to think about first.
Core nouns:
- State: a captured tree with a stable change id, attribution, intent, and
provenance. States are what `log`, `show`, `diff`, `undo`, and agents can
reason about.
- Thread: a named line of work with its own checkout and captured history.
Use it for risky edits, agent work, or parallel experiments without stash
juggling.
- Capture: a cheap recoverable save point on the current thread.
- Commit: the normal human save path. In native Heddle it saves the state; in a
Git-overlay repo it saves the Heddle state and writes the matching Git
checkpoint as one operation.
- Checkpoint: the explicit Git-overlay boundary for already-captured work.
- Verify: the proof surface. It says whether Heddle, Git mapping, worktree,
remotes, active operations, clone state, and machine contracts agree.
Everyday loop:
heddle status
heddle diff
heddle commit -m "..."
heddle start <name> --path ../<name>
heddle ready
heddle land --thread <name>
heddle undo
heddle verify
Existing Git checkout:
heddle status
heddle adopt # or the exact adopt/import command status prints
heddle verify
If a command refuses, read the first `Next:` line. Heddle fails closed when it
cannot prove the move is safe.
"#;
const GIT_CONCEPTS_TOPIC: &str = r#"Git to Heddle concept map.
| Git concept | Heddle concept + semantic difference |
|-------------|--------------------------------------|
| `git commit` | `heddle commit -m "..."`: saves a Heddle State. In native Heddle this is the authored snapshot; in Git-overlay mode it also writes the matching Git checkpoint when safe. |
| Git commit SHA | Heddle `hd-...` change id. Use it with `heddle show`, `log`, and `diff`; Git SHAs remain the interop handle for Git tooling. |
| `git branch foo` | `heddle start foo` for a working thread, or `heddle thread create foo` for a ref only. A thread is a unit of work with checkout, captured history, metadata, and readiness state, not just a movable ref. |
| `git checkout foo` / `git switch foo` | `heddle thread switch foo`. Heddle switches between thread checkouts and may auto-capture the thread you leave; raw Git checkout only moves the Git layer. |
| `git tag v1.0` | `heddle thread marker create v1.0`. A marker names a Heddle State; it is for pinning a state in Heddle history, not for creating a signed or annotated Git tag object. |
| `git remote add origin <url>` | `heddle remote add origin <url>`. Heddle remotes can be native Heddle endpoints, hosted addresses, local paths, or Git remotes depending on repository mode. |
| `git push` / `git pull` | `heddle push` / `heddle pull`. Heddle pushes or pulls the selected thread/state through its remote contract and refuses when verification says the mapping is unsafe. |
| `git fetch` | `heddle fetch`. Fetch updates remote knowledge without making your current thread's checkout silently absorb changes. |
| `git rebase` to catch up | `heddle sync`. Sync refreshes a stale thread onto its target when replay is clean; conflicts route through `heddle resolve` / `heddle continue`. |
Reconciliation examples:
git branch feature/auth
git checkout feature/auth
# Heddle: create/resume an isolated unit of work instead
heddle start feature/auth --path ../feature-auth
git tag v1.0
# Heddle: pin the current State by name
heddle thread marker create v1.0
git fetch origin
git rebase origin/main
# Heddle: update remote knowledge, then refresh the current thread when safe
heddle fetch origin
heddle sync
For an existing Git checkout, start with `heddle status`; it prints the exact
`heddle adopt` command when Heddle needs to import Git refs first.
"#;
const THREADS_TOPIC: &str = "Threads — Heddle's unit of in-progress work.\n\
\n\
A thread is a named line of work with its own checkout, its own captured\n\
history, and a target it eventually merges into. It is *not* a git branch:\n\
the git-overlay branch is downstream plumbing (created at checkpoint),\n\
not the primary object. You start isolated work with `heddle start <name> --path <dir>`, switch\n\
between threads with `heddle thread switch <name>`, and integrate with\n\
`heddle land` (or check readiness without merging via `heddle ready`).\n\
\n\
# Threads vs. git branches\n\
\n\
- A thread carries an isolated checkout (its own directory), captured\n\
state history, agent/task metadata, a freshness verdict against its\n\
target, and a workflow state (Ready/Blocked/Merged/...). A git branch\n\
is just a ref.\n\
- Multiple threads coexist on disk simultaneously without `git stash` /\n\
`git worktree` gymnastics. Each thread's working tree is its own.\n\
- `heddle commit` captures work and writes the Git-facing checkpoint.\n\
Use `heddle capture` and `heddle checkpoint` separately when you want\n\
finer-grained Heddle states before producing Git commits.\n\
\n\
# Workspace modes (`--workspace`)\n\
\n\
The `--workspace` flag on `heddle start` selects how the thread's\n\
checkout is realized on disk. These are storage strategies, not\n\
workflow states:\n\
\n\
- `materialized` — clonefile/reflink the captured tree into the thread's\n\
directory (APFS / btrfs / XFS-with-reflinks / bcachefs / ReFS). Real\n\
`read(2)`-able bytes; ~zero disk cost until the agent diverges blocks.\n\
Day-one default on reflink-capable hosts.\n\
- `virtualized` — project the captured tree through a content-addressed\n\
FUSE/FSKit/ProjFS mount. Nothing on disk until the kernel asks.\n\
Requires the `mount` feature.\n\
- `solid` — full file copies, no shared extents. Strong isolation;\n\
the right choice on ext4/NTFS hosts that have neither reflinks nor a\n\
usable mount API.\n\
- `auto` (default) — pick `materialized` when reflinks are available,\n\
`virtualized` when a mount is available, otherwise `solid`.\n\
\n\
A `solid` thread and a `materialized` thread are interchangeable from\n\
the workflow's point of view — `capture`, `land`, `switch`, etc. behave\n\
identically. The mode only controls bytes-on-disk semantics.\n\
\n\
# Isolated checkout path\n\
\n\
- Use `heddle start <name> --path <dir>` when you want an isolated\n\
checkout. It creates the thread ref and materializes the checkout in\n\
one step.\n\
- Advanced split form: `heddle thread create <name>` creates only the\n\
ref, and `heddle thread promote <name> --path <dir>` materializes it\n\
later. Use this only when you intentionally need to create the ref\n\
now and materialize the checkout later.\n\
- `--workspace` on `heddle start` selects byte storage for that checkout;\n\
it is not a separate workflow path.\n\
\n\
# Sync: stale\n\
\n\
A thread is `current` when its base is the tip of its target, and\n\
`stale` once the target has advanced past it. `heddle status` and\n\
`heddle thread show` print this as `Sync: stale`.\n\
\n\
Resolution paths:\n\
\n\
- `heddle sync` — refresh the current thread onto its target when\n\
the replay is clean. The fast path for a stale thread with no\n\
conflicts.\n\
- If `sync` reports conflicts or other blockers, use\n\
`heddle resolve` or `heddle continue` to handle the conflicts.\n\
- `heddle land` will refresh-then-merge for you when the replay is\n\
clean; it fails closed when manual resolution is required.\n\
\n\
# `switch` vs. `git checkout` vs. `thread switch`\n\
\n\
These three look similar but operate at different layers:\n\
\n\
- `heddle thread switch <name>` — change which *thread* is active.\n\
Each thread has its own checkout; switching may auto-capture\n\
outstanding work on the thread you're leaving. Pair with the shell\n\
hook (`heddle shell init`) to auto-cd into the target thread's\n\
directory.\n\
- `heddle switch <state>` — move the *current thread's* worktree to a\n\
specific captured state. It refuses with uncommitted changes unless\n\
`--force` is passed; it does not change which thread is active.\n\
- `git checkout` — operates on the git-overlay branch and index\n\
directly. Heddle's thread metadata, captured state, and workflow\n\
state are not updated. Reach for it only when you specifically want\n\
the git-layer view; the thread-aware verbs are the supported path.\n\
\n\
# Capture vs. checkpoint\n\
\n\
- `heddle capture` records a recoverable Heddle step on the current\n\
thread — for undo, provenance, and review. Captures are\n\
fine-grained and accumulate freely as work progresses.\n\
- `heddle commit -m \"...\"` is the one-step human path: capture the\n\
current work and write the Git-facing checkpoint.\n\
- `heddle checkpoint` commits the current captured work to the\n\
git-overlay branch/index. It refuses when the worktree has changes\n\
that haven't been captured yet — capture first, then checkpoint.\n\
- The split lets agents and tools take many small captures (cheap,\n\
reversible) without producing a noisy git history; checkpoints are\n\
the durable downstream record.\n\
\n\
See also: `heddle help advanced` for the full operational surface,\n\
`heddle thread --help` for the thread subcommand list.\n";
const OPERATION_IDS_TOPIC: &str = "Idempotency — machine retries for supported mutating commands.\n\
\n\
Commands that advertise `supports_op_id: true` in `heddle help --output json`\n\
accept `--op-id <UUID>` or `HEDDLE_OPERATION_ID`. Replaying the same id\n\
with the same body returns the recorded outcome; with a different body it\n\
returns a typed conflict.\n\
\n\
`op_id_behavior: explicit_replay` means the caller must provide the id.\n\
`op_id_behavior: generated_resume` is reserved for commands that also\n\
advertise `persists_op_id: true` and can save a generated id across an\n\
interrupted retry loop. Commands with `op_id_behavior: none` reject --op-id.\n\
\n\
The dedup store is file-backed locally (`.heddle/state/operation_dedup.bin`,\n\
rmp-serde, 7-day default retention) and Postgres-backed in hosted deployments.\n\
\n\
Without an id, dedup is bypassed and the call executes normally. For the\n\
authoritative per-command contract, use `heddle help --output json`.\n";
const REMOTES_TOPIC: &str = "Remotes — local, Git-overlay, and hosted destinations.\n\
\n\
Core loop:\n\
\n\
heddle remote add origin <url-or-path>\n\
heddle remote set-default origin\n\
heddle fetch\n\
heddle push\n\
heddle pull\n\
heddle verify\n\
\n\
Remote values may be hosted endpoints, Git URLs, file URLs, or local bare Git\n\
paths depending on the workflow. Top-level `fetch`, `push`, and `pull` use the\n\
default remote unless a positional remote name is supplied, for example\n\
`heddle fetch backup`. `heddle bridge git status` shows Git-overlay mapping and\n\
drift before a sync operation changes refs.\n\
\n\
When a remote action is unsafe, Heddle reports the blocker and one primary\n\
next command instead of falling back to raw Git.\n";
const GIT_DEPENDENCIES_TOPIC: &str = "Git executable dependencies — what works without `git` on PATH.\n\
\n\
Supported Git-overlay workflows use native/library paths and are tested with\n\
`PATH` stripped of `git`: `init`, `status`, local/bare `clone`, `bridge git\n\
import`, `bridge git status`, `bridge git sync/export` where implemented,\n\
`thread list`, `workspace`, `log`, `show`, `diff`, `checkpoint`, `merge`,\n\
`ready`, and `fsck`.\n\
\n\
Heddle is Git-compatible, not Git-binary-dependent. Public CLI runtime paths\n\
must not spawn a `git` process; Git-format reads, writes, transport, index,\n\
and ref updates go through native/library code.\n\
\n\
If Heddle detects an externally-started raw Git sequencer operation, it leaves\n\
Git metadata, refs, index, and worktree files unchanged and reports a Heddle\n\
preservation command. Finish or abort that operation with the Git-compatible\n\
tool that started it, then run `heddle verify`.\n\
\n\
Unsupported native Git-overlay capabilities, such as filtered/lazy Git clones,\n\
fail closed with recovery advice instead of silently invoking a `git` binary.\n\
`merge --git-commit` writes Git objects and refs natively.\n\
\n\
Run `heddle help --output json` to inspect the public command surface, and\n\
`heddle doctor` / `heddle fsck --full` when a repository reports integrity or\n\
bridge-state problems.\n";
const REVIEW_TOPIC: &str = "Review surface — `heddle review show | sign | next | health`.\n\
\n\
`show <state>` — render the review payload (summary, agent narrative,\n\
in-budget signals, anchored discussions).\n\
`--all-signals` also surfaces hidden ones.\n\
`sign <state>` — submit a `read | agent_preview | agent_co_review`\n\
signature. `--symbols file:symbol` scopes to\n\
specific symbols; default is the whole change.\n\
`next` — show the next locally discoverable review item, or explain\n\
why none is available.\n\
`health [--window N]`\n\
— per-module signal fire-rate over the last N states.\n\
\n\
Tick budget: at most 3 signals per state by default. Priority:\n\
invariant_adjacency > self_flagged_uncertainty > pattern_deviation >\n\
novelty > test_reachability.\n";
const DISCUSS_TOPIC: &str = "`heddle discuss open | append | resolve | list | show`\n\
\n\
Discussions anchor at the symbol level (file + symbol name, no line range)\n\
so they survive renames and cross-file moves. Each discussion accumulates\n\
turns and resolves into one of three terminal states:\n\
\n\
- `resolve <id> --mode into-annotation` with `--annotation-kind`,\n\
`--annotation-content`, optional `--annotation-tags`. Atomically\n\
creates the annotation and bidirectionally links it.\n\
- `resolve <id> --mode by-edit` with `--state` (defaults to HEAD).\n\
Records that a subsequent edit addressed the discussion.\n\
- `resolve <id> --mode dismiss` requires non-empty `--reason`.\n\
\n\
Visibility: `--visibility public|internal|team:NAME|restricted:LABEL`.\n\
Defaults to the repo's namespace policy.\n";
const GIT_OVERLAY_TOPIC: &str = "Git-overlay quick start\n\
\n\
Use this when you want Heddle's captured states, isolated threads, merge\n\
previews, undo, provenance, and machine-safe JSON with Git compatibility kept\n\
behind the bridge/adapter.\n\
\n\
Start in an existing Git checkout:\n\
\n\
heddle status\n\
heddle adopt --ref <branch> # use the exact command printed by status\n\
heddle verify\n\
\n\
Save and sync ordinary work:\n\
\n\
heddle diff\n\
heddle commit -m \"...\" # one Heddle state + one Git commit\n\
heddle push\n\
\n\
Isolate risky work:\n\
\n\
heddle start <name> --path ../<name>\n\
cd ../<name>\n\
heddle commit -m \"...\"\n\
heddle ready\n\
cd -\n\
heddle land --thread <name> --no-push # add --push when ready to publish\n\
\n\
Recover or prove state:\n\
\n\
heddle undo\n\
heddle verify\n\
\n\
State-specific recovery:\n\
\n\
Worktree has unsaved edits: heddle commit -m \"...\"\n\
Captured in Heddle but not Git: heddle commit -m \"...\"\n\
Git refs changed externally: heddle adopt --ref <branch>\n";
const BRIDGE_TOPIC: &str = "Git bridge — adopt existing Git repos through an adapter.\n\
\n\
Use the bridge when you are standing in a normal Git checkout and want Heddle's\n\
captured states, isolated threads, merge previews, undo, and machine-safe JSON\n\
while keeping Git remotes and commits available as interoperability surfaces.\n\
\n\
First run:\n\
\n\
heddle status\n\
heddle adopt --ref <branch> # use the exact command printed by status\n\
heddle verify\n\
\n\
Manual setup, when you want one ref at a time:\n\
\n\
heddle init\n\
heddle bridge git import --ref <branch>\n\
\n\
Daily loop:\n\
\n\
heddle status\n\
heddle commit -m \"...\" # save work as Heddle + Git\n\
heddle push # Git-overlay remotes use the top-level verb\n\
heddle start <name> --path ../<name>\n\
heddle ready --thread <name> # or cd into ../<name> and run heddle ready\n\
heddle land --thread <name> --no-push # add --push to land and push together\n\
\n\
Recovery and inspection:\n\
\n\
heddle bridge git status\n\
heddle bridge git reconcile --ref <branch> --preview\n\
heddle doctor\n\
heddle verify --output json\n\
\n\
Export metadata for Git readers:\n\
\n\
Every exported commit carries a footer at the tail of the commit message:\n\
\n\
Heddle-State: <change_id>\n\
Heddle-URL: <hosted_url>/state/<change_id> (omitted if no hosted URL)\n\
Heddle-Annotations-Omitted: <count>\n\
\n\
This is the durable record — every reader on every host sees it regardless\n\
of remote configuration.\n\
\n\
Per-scope annotation drop counts and signal counts ride on the opt-in\n\
Git note at `refs/notes/heddle`. Heddle reads and writes that ref natively;\n\
people who still inspect the repository through another Git client can opt\n\
that client into showing notes, but Heddle itself does not require a Git\n\
executable on the system.\n";
const SIGNALS_TOPIC: &str = "Risk signals — five modules behind a pure trait.\n\
\n\
- `invariant_adjacency` — fires when a changed symbol carries an\n\
Invariant or `enforces`-tagged annotation.\n\
- `self_flagged_uncertainty` — passthrough of agent-emitted self-flags\n\
from the captured state's intent.\n\
- `pattern_deviation` — fires when a symbol's body diverges\n\
from siblings or the prior version\n\
(tree-sitter token similarity).\n\
- `novelty` — fires when a function shape is unique\n\
in the repo corpus.\n\
- `test_reachability` — fires when no test statically reaches\n\
the changed symbol via tree-sitter\n\
call-graph traversal. The reason text\n\
is honest: this is *not* runtime\n\
coverage.\n\
\n\
Configure under `[review.signals]` in `.heddle/config.toml`. Each module\n\
ships fires-correctly + stays-quiet tests; defaults are conservative\n\
so a fresh repo isn't noisy.\n";
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn everyday_verbs_in_curated_list_have_everyday_tier() {
for verb in everyday_verbs() {
assert_eq!(tier_of(verb), Tier::Everyday, "{verb}");
}
}
#[test]
fn topic_text_returns_none_for_unknown() {
assert!(topic_text("definitely-not-a-topic").is_none());
}
#[test]
fn topic_text_returns_some_for_advertised_topics() {
for topic in [
"advanced",
"agent-flags",
"git-concepts",
"git-concept-map",
"git-veteran",
"git-overlay",
"agent",
"daemon",
"threads",
"model",
"mental-model",
"concepts",
"operation-ids",
"idempotency",
"remotes",
"git-dependencies",
"review",
"discuss",
"discussions",
"bridge",
"footer",
"notes",
"signals",
"risk-signals",
"output-formats",
"output-format",
"output",
"clone",
] {
assert!(topic_text(topic).is_some(), "{topic}");
}
}
#[test]
fn tier_of_advanced_verbs_classifies_correctly() {
for verb in advanced_verbs() {
let t = tier_of(verb);
assert!(
matches!(t, Tier::Advanced),
"expected Advanced for {verb}, got {t:?}"
);
}
}
#[test]
fn advanced_verbs_lists_tip_referenced_commands() {
let advanced: std::collections::HashSet<&str> = advanced_verbs().into_iter().collect();
for verb in [
"query",
"capture",
"checkpoint",
"continue",
"abort",
"shell",
"git-overlay",
] {
assert!(
advanced.contains(verb),
"`{verb}` is referenced in user-facing tips but is not \
advertised by `heddle help advanced`"
);
}
}
#[test]
fn everyday_verbs_surface_the_core_loop() {
let everyday: std::collections::HashSet<&str> = everyday_verbs().into_iter().collect();
for verb in [
"init", "clone", "status", "start", "commit", "ready", "diff", "land", "resolve",
"undo", "log", "show", "pull", "push", "doctor", "verify",
] {
assert!(
everyday.contains(verb),
"`{verb}` is part of the core loop but is not advertised on \
the everyday surface"
);
}
for verb in ["review", "discuss", "context", "thread", "bridge"] {
assert!(
!everyday.contains(verb),
"`{verb}` belongs behind advanced/topic help, not the core-loop surface"
);
}
}
#[test]
fn agent_flags_topic_lists_hidden_capture_flags() {
let text = topic_text("agent-flags").expect("agent-flags topic should exist");
for flag in [
"--agent-provider",
"--agent-model",
"--agent-session",
"--agent-segment",
"--policy",
"--no-policy",
"--no-agent",
"--split",
"--into",
"--path",
] {
assert!(text.contains(flag), "agent-flags topic missing `{flag}`");
}
for env in [
"HEDDLE_AGENT_PROVIDER",
"HEDDLE_AGENT_MODEL",
"HEDDLE_AGENT_POLICY",
"HEDDLE_SESSION_ID",
"HEDDLE_SESSION_SEGMENT",
] {
assert!(text.contains(env), "agent-flags topic missing env `{env}`");
}
}
#[test]
fn capture_agent_flags_hidden_by_default_revealed_on_demand() {
use clap::CommandFactory;
let cmd = crate::cli::cli_args::Cli::command();
let capture = cmd
.find_subcommand("capture")
.expect("capture subcommand exists");
for id in CAPTURE_AGENT_FLAG_IDS {
let arg = capture
.get_arguments()
.find(|arg| arg.get_id().as_str() == *id)
.unwrap_or_else(|| panic!("capture has no `{id}` arg"));
assert!(arg.is_hide_set(), "`{id}` should be hidden by default");
}
let revealed = reveal_capture_agent_flags(capture.clone());
for id in CAPTURE_AGENT_FLAG_IDS {
let arg = revealed
.get_arguments()
.find(|arg| arg.get_id().as_str() == *id)
.expect("revealed arg present");
assert!(
!arg.is_hide_set(),
"`{id}` should be revealed by --help-agent"
);
}
}
fn wants_reveal(args: &[&str]) -> bool {
use clap::Parser;
use crate::cli::cli_args::{Cli, Commands};
let argv = std::iter::once("heddle").chain(args.iter().copied());
match Cli::try_parse_from(argv) {
Ok(cli) => matches!(&cli.command, Commands::Capture(a) if a.help_agent),
Err(_) => false,
}
}
#[test]
fn capture_help_agent_is_capture_scoped() {
assert!(
wants_reveal(&["capture", "--help-agent"]),
"capture --help-agent should request the reveal help"
);
assert!(
!wants_reveal(&["status", "--help-agent"]),
"--help-agent on a non-capture verb is not a capture reveal request"
);
assert!(
!wants_reveal(&["capture", "--help"]),
"plain --help is clap's help, not the agent reveal"
);
}
#[test]
fn capture_help_agent_handles_every_global_form_clap_accepts() {
assert!(
wants_reveal(&["-C", "/tmp/repo", "capture", "--help-agent"]),
"`-C <path> capture --help-agent` should reveal — clap parses the path"
);
assert!(
wants_reveal(&["--output", "text", "capture", "--help-agent"]),
"`--output text capture --help-agent` should reveal"
);
assert!(
wants_reveal(&["-C", "capture", "capture", "--help-agent"]),
"`-C capture capture --help-agent` (repo dir named `capture`) should reveal"
);
assert!(
wants_reveal(&["-vC", "/tmp/repo", "capture", "--help-agent"]),
"`-vC <path> capture --help-agent` (clustered short globals) should reveal"
);
assert!(
wants_reveal(&["-vC", "capture", "capture", "--help-agent"]),
"`-vC capture capture --help-agent` (clustered, repo dir named `capture`) should reveal"
);
assert!(
wants_reveal(&["-C/tmp/repo", "capture", "--help-agent"]),
"`-C<path> capture --help-agent` (attached) should reveal"
);
assert!(
wants_reveal(&["capture", "--help-agent"]),
"plain `capture --help-agent` should reveal"
);
assert!(
!wants_reveal(&["-C", "capture", "status", "--help-agent"]),
"`-C capture status --help-agent` — verb is `status`, no reveal"
);
assert!(
!wants_reveal(&["-vC", "capture", "status", "--help-agent"]),
"`-vC capture status --help-agent` (clustered) — verb is `status`, no reveal"
);
assert!(
!wants_reveal(&["--output", "text", "status", "--help-agent"]),
"`--output text status --help-agent` — verb is `status`, no reveal"
);
}
#[test]
fn output_blurb_stated_once_not_per_command() {
let marker = "full machine contract";
let top = render_for_args(&["--help"]).expect("top-level help renders");
assert_eq!(
top.matches(marker).count(),
1,
"top-level help should state the --output contract exactly once: {top}"
);
assert!(
topic_text("output-formats")
.expect("output-formats topic exists")
.contains(marker),
"the output-formats topic carries the full contract"
);
for argv in [
&["clone", "--help"][..],
&["status", "--help"][..],
&["commit", "--help"][..],
&["thread", "--help"][..],
&["push", "--help"][..],
] {
let help = render_for_args(argv).expect("command help renders");
assert!(
!help.contains(marker),
"`{argv:?}` should not restate the --output machine contract: {help}"
);
assert!(
help.contains("heddle help output-formats"),
"`{argv:?}` should breadcrumb to the output-formats topic: {help}"
);
}
}
#[test]
fn clone_help_fits_one_screen() {
let help = render_for_args(&["clone", "--help"]).expect("clone help renders");
let lines = help.lines().count();
assert!(
lines <= 40,
"clone --help should fit one screen (<= 40 lines), got {lines}:\n{help}"
);
assert!(
help.contains("Advanced/planned flags: see `heddle help clone`."),
"clone --help keeps the hidden-flags affordance: {help}"
);
assert!(
help.contains("heddle help clone"),
"clone --help points at the clone topic for the full behavior: {help}"
);
}
#[test]
fn advanced_help_renders_area_groups() {
use clap::CommandFactory;
let cmd = crate::cli::cli_args::Cli::command();
let advanced = render_help(&cmd, &["advanced".to_string()]);
for header in [
"Threads and integration:",
"States and history:",
"Recovery and integrity:",
"Repo and environment:",
"Agents and automation:",
"Git interop:",
"Admin and maintenance:",
] {
assert!(
advanced.contains(&format!("\n{header}\n")),
"advanced help should render the `{header}` group: {advanced}"
);
}
assert!(
!advanced.contains("Advanced commands:"),
"the flat list header is replaced by area groups: {advanced}"
);
}
#[test]
fn advanced_help_groups_cover_every_advanced_verb() {
let grouped: Vec<&str> = crate::cli::commands::advanced_help_groups()
.into_iter()
.flat_map(|(_, verbs)| verbs)
.collect();
let mut deduped = grouped.clone();
deduped.sort_unstable();
deduped.dedup();
assert_eq!(
deduped.len(),
grouped.len(),
"no verb may appear in two advanced-help groups: {grouped:?}"
);
let grouped: std::collections::HashSet<&str> = grouped.into_iter().collect();
let flat: std::collections::HashSet<&str> = advanced_verbs().into_iter().collect();
let missing: Vec<&&str> = flat.difference(&grouped).collect();
assert!(
missing.is_empty(),
"advanced verbs missing from every group — native advanced root \
commands must register a help_category in the command contract \
table: {missing:?}"
);
let extra: Vec<&&str> = grouped.difference(&flat).collect();
assert!(
extra.is_empty(),
"grouped verbs not on the advanced surface: {extra:?}"
);
}
#[test]
fn verb_blurbs_resolve_from_command_catalog() {
let catalog = crate::cli::commands::build_command_catalog();
for verb in everyday_verbs().into_iter().chain(advanced_verbs()) {
if catalog.command_by_display(verb).is_none() {
continue;
}
let blurb = catalog_summary(&catalog, verb);
assert!(
!blurb.is_empty(),
"verb `{verb}` is cataloged but its summary is empty. \
The curated help printer needs a non-empty catalog summary."
);
}
}
#[test]
fn hidden_flags_carry_discovery_affordances() {
use clap::CommandFactory;
fn walk(cmd: &clap::Command, path: &str, violations: &mut Vec<String>) {
let after_help = [
cmd.get_after_help().map(ToString::to_string),
cmd.get_after_long_help().map(ToString::to_string),
]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("\n");
let after_lower = after_help.to_lowercase();
let breadcrumb_marker = after_lower.contains("hidden")
|| after_lower.contains("advanced flag")
|| after_lower.contains("advanced/planned flags");
let points_at_reveal =
after_help.contains("heddle help ") || after_help.contains("--help-agent");
for arg in cmd.get_arguments() {
if !arg.is_hide_set() || arg.is_global_set() {
continue;
}
let flag_help = arg
.get_long_help()
.or_else(|| arg.get_help())
.map(ToString::to_string)
.unwrap_or_default();
if flag_help.starts_with("Internal") {
continue;
}
let named_inline = arg
.get_long()
.is_some_and(|long| after_help.contains(&format!("--{long}")));
if breadcrumb_marker && (points_at_reveal || named_inline) {
continue;
}
let name = arg
.get_long()
.map(|long| format!("--{long}"))
.unwrap_or_else(|| arg.get_id().to_string());
violations.push(format!("`{path}` hides `{name}`"));
}
for sub in cmd.get_subcommands() {
if sub.is_hide_set() {
continue;
}
walk(sub, &format!("{path} {}", sub.get_name()), violations);
}
}
let cmd = crate::cli::cli_args::Cli::command();
let mut violations = Vec::new();
walk(&cmd, cmd.get_name(), &mut violations);
assert!(
violations.is_empty(),
"hidden flags without a discovery affordance (heddle#646): either \
prefix the flag's help with `Internal` (plumbing, not for users) \
or add an after-help breadcrumb that mentions the hidden/advanced \
flags and names the flag or a reveal surface:\n {}",
violations.join("\n ")
);
}
}