← Back to Pull requests

feat(spec): declare environment variables a command reads with an env node #1689

Closed
unknown closed this pull request without merging on Nov 30, 2026 14:06 UTC
View on GitHub
unknown unknown commented on Nov 30, 2026 13:19 UTC
Author

A command often reads an environment variable that is not any flag's value. For example, shell reads SHELL to choose the program it opens, and run reads TOOL_HOME to find files another tool writes. Until now a spec had only two ways to declare one. It could bind the variable to a flag (flag … env="X"), which invents a flag nobody asked for. Or it could use a root config property, which documents the variable on every command, including the ones that never read it. Otherwise the variable lived only in prose, where generated docs, JSON output and unknown_env could not see it.

A new env node declares such a variable on the command that reads it, or at the top level for one the whole program reads:

name "wt"
bin "wt"
cmd "shell" help="Open a shell in the worktree" {
  env "SHELL" help="The program to open" default_note="bash"
}
cmd "run" help="Run a session" {
  env "TOOL_HOME" help="Where the tool keeps its sessions" default_note="~/.tool"
}

usage generate markdown now renders:

## `wt shell`

- **Usage:** `wt shell`

Open a shell in the worktree

### Environment

- **`SHELL`** (default: bash) — The program to open

What a declaration does:

  • Docs: the variable appears under Environment on the command's markdown page, in both the compact and detailed themes. In the man page it appears in the command's section, and a top-level env gets an ENVIRONMENT section.

  • JSON: usage generate json includes it as envs on the command.

  • unknown_env: it counts as declared, hidden ones included.

  • usage lint: a new missing-env-help (info) rule reports a variable without help, and duplicate-env (error) reports one declared twice on the same command.
  • Supported properties are help, long_help, default_note, deprecated (a message or #true) and hide. The declaration only documents the variable. usage never reads it or applies the default, and it is not inherited by subcommands. Library users get a new SpecEnv type and a SpecCommand::envs field. cargo-semver-checks reports no breaking change, because SpecCommand already has crate-private fields and cannot be built with a struct literal.

    The Rust derives (#[usage(...)]) do not emit env nodes yet. A spec has to declare them in KDL.

    Proposed in https://github.com/jdx/usage/discussions/1688.

    Validation: cargo test --all --all-features and mise run lint (including semver checks) pass. New tests cover parsing, KDL round-trip, the JSON round-trip fixture, markdown in both themes, the man page, unknown_env, and the lint rules.

    AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.

    🤖 Generated with Claude Code

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    Low Risk

    Documentation, lint, and spec parsing additions with broad test coverage; no runtime behavior change for CLI execution.

    > Overview

    Adds a new env KDL node (and SpecEnv) so specs can document environment variables a command reads without tying them to a flag or global config. Declarations are documentation-only: they surface in generated markdown (compact/detailed Environment sections), man pages (top-level ENVIRONMENT and per-subcommand blocks), and serialized envs on commands, while hidden envs stay off pages but still count for unknown_env.

    > usage lint now flags missing-env-help and duplicate-env on a command. declared_env_names includes command-level env nodes. Includes merge env lists additively (later declarations override same name). Reference docs describe properties (help, long_help, default_note, deprecated, hide) and non-inheritance by subcommands.

    > <sup>Reviewed by Cursor Bugbot for commit 09b9073ee69b29a28dd909949360f435a0020943. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Added command-level environment variable declarations with help text, default notes, deprecation notices, and visibility controls.
    Added environment variable details to generated Markdown and man-page documentation, including for subcommands.
    Updated unknown environment variable checks to recognize command-level declarations.
    Bug Fixes
    Linting now reports duplicate environment variable names and flags visible declarations without help text.
    Documentation
    Expanded the command reference to explain environment declarations and their behavior.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 609 lines (+456 / -153) across 7 files Reviewer: jdx Review duration: 46m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 609 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.384
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1689"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%
    Repository