Mercury CLI
Documentation

Code

How to check code an AI agent wrote

Generating code is cheap; checking what an agent wrote is the work. Reading a long diff by eye misses wrong assumptions, broken types and edge cases that only show up at runtime. Mercury CLI puts six concrete checks in the terminal loop so you verify behavior, syntax, types and rollbacks directly against your repository.

Run tests and read structured failures

Start with the Test tool instead of grepping raw shell output. Test supports seven frameworks: pytest, unittest, node-test, vitest, jest, cargo and go.

  • Test run reads each runner's machine reporter (vitest and jest JSON reports, go test -json, node-test and cargo summaries, pytest and unittest reports) and returns passed, failed and skipped counts alongside the name, file, line and message of up to ten failing cases.
  • Test rerunFailed re-runs only the failing tests recorded by the latest run, refusing without running anything if the latest run had no failures or matched no tests.

A tight check loop after an agent edits code looks like this:

{ "op": "run" }

Fix the failing case named in the structured output, then confirm that failure turns green before you run the whole suite again:

{ "op": "rerunFailed" }

Step a failing test into the debugger

When a test fails inside a loop or after several helper calls, guessing from a traceback often patches the symptom instead of the cause. Test debug launches a named failing test directly into Mercury's built-in Debug Adapter Protocol (DAP) Debug tool without writing a launch configuration or reconstructing runner flags.

  • Release archives bundle debugpy (with CPython 3.14) for Python and js-debug for JavaScript and TypeScript. Go (dlv) and Rust (lldb-dap or gdb 14+) work when those debuggers are installed on PATH; if one is missing, Test debug prints the install hint and runs the test without stopping.
  • vitest, jest, Go and cargo place a breakpoint on the named test automatically (vitest, jest and cargo stop at the runner's entry when they cannot find its line); pytest, unittest and node-test pause on entry (or at explicit file and lines) so you can place breakpoints before the test body runs.
  • The vitest debug launch runs that one file without file parallelism or a test timeout, and its answer does not compare the result with the recorded failing run, so a failure that only appears when files run in parallel will pass there.

Set a conditional breakpoint with Debug breakpoints, continue to the failing iteration, and inspect live locals with scopes, variables or evaluate:

{
  "op": "breakpoints",
  "session": "test",
  "file": "src/total.js",
  "breakpoints": [
    {
      "line": 7,
      "condition": "typeof shipping !== 'number'"
    }
  ]
}

Once the live value exposes the cause, call Debug disconnect, apply the fix and verify with Test rerunFailed. See Debug a failing Node test and Debug a failing pytest test for full walkthroughs.

Preview diffs and block broken syntax before writing

Two built-in tools, AstEdit and ChangeSet, let you inspect exact diffs and reject stale writes before any byte hits disk; AstEdit also refuses a rewrite that breaks a file's syntax.

AstEdit matches and rewrites parsed tree-sitter syntax trees rather than raw text (all 57 languages ship in release archives; a clean-clone source build carries 16 until you run fetch-grammars.ts and rebuild). Every rewrite is two calls:

  1. Call AstEdit without apply to get a unified diff per file and a content-addressed plan token (ae-...). Nothing is written, and no write permission is asked.
  2. Call AstEdit with apply: true and that plan token. Mercury re-plans against the current bytes and refuses if any file drifted, or if a rewrite would leave a parsed file unparsable.
{
  "pattern": "foo($A)",
  "rewrite": "bar($A)",
  "path": "src/"
}

For anchored edits, moves or deletions across up to 16 existing text files, ChangeSet checks every member's expected_anchor (minted on Read as fa:<hex12> for a whole file or ra:<hex12>:L<start>+<n> for a line range):

{
  "op": "preview",
  "changes": [
    {
      "file_path": "/repo/src/parse.js",
      "expected_anchor": "fa:7800a5f18a54",
      "edits": [
        {
          "old_string": "const trimmed = value.trim()",
          "new_string": "const trimmed = value.trim().replace(/^[\"']|[\"']$/g, '')"
        }
      ]
    }
  ]
}

preview returns a retained plan (plan_id, kept for 30 minutes) without writing. Passing op: "apply" with that plan_id verifies every anchor before renaming, writes nothing if any file drifted or any path is denied, and confirms the landed bytes by reread.

Check language-server diagnostics after an edit

A passing unit test does not guarantee the rest of the project type-checks. LspRead queries the workspace's language servers for compiler diagnostics across a single file or up to 50 files and directories.

  • Release archives bundle TypeScript and JavaScript (mercury-ts, using the project's own typescript package with a vendored fallback), Python (mercury-pyright), and HTML, CSS, JSON and YAML (mercury-web) servers. Mercury also wires clangd and ruff when they are installed, and servers from a 34-server catalogue (such as rust-analyzer and gopls) when they are installed and the session's folder holds their marker file (such as Cargo.toml or go.mod). Mercury never installs a language server on its own; mercury lsp install <server> shows the command and asks first.
  • A diagnostics response explicitly states whether a server checked the files. A failed start, an unanswered request or an uncovered file is never presented as clean; Mercury names the cause and remedy.
{
  "operation": "diagnostics",
  "paths": ["src/"]
}

Run /lsp status from the composer to see each server's state, restarts and last error, or /lsp restart to restart the workspace's language servers.

Ask a second reader with fresh context

After a long coding session, the main conversation carries every earlier dead end and assumption. Enter /verify in the chat to hand the session's work and an adversarial verification brief to a mercury-crew crewmate in a fresh context. The verifier checks the work independently and ends with VERDICT: PASS, FAIL or PARTIAL.

Be clear about which model runs it: /verify names no model, so its crewmate inherits the parent session's own model unless its Crewmate call names another model or agents.defaultModel is set.

For a committed change, a Crewmate launch with isolation: "worktree", worktree_at and review_receipt reviews that commit on a frozen git worktree. Its one permitted write is the receipt's ## Review section, ending with REVIEW: CLEAN or REVIEW: FINDINGS <count>.

Rewind a change that went wrong

When a turn heads in the wrong direction, run /rewind (alias /checkpoint) to list the turns of the focused chat and restore files, conversation or both to an earlier turn.

  • File restore puts every tracked file back to its saved bytes at that turn, all or nothing. If you edited a tracked file by hand since the session last touched it, Mercury refuses by name and writes nothing until you reconcile it.
  • Pick a single file's Restore row to roll back only that file while leaving other files and the conversation untouched.
  • Before restoring, Mercury saves the current file bytes so Undo the rewind puts them back without changing the conversation (available until your next prompt, still refusing conflicting hand edits).
  • File-history backups live under <config-home>/file-history, capped at 500 MB (pruned oldest session first) alongside records.retentionDays (30 days by default). Restoring to a point before the last /compact is refused by name.

What Mercury does not do here

Knowing where the tool stops matters as much as knowing what it checks:

  • It does not pause for every diff in every mode. Default mode asks before edits and /diff shows uncommitted and per-turn changes, while Implement, Flow and Sovereign modes and Bash writes do not pause for pre-write review of every change.
  • It does not switch models on its own for /verify. /verify runs in fresh context on the parent session's model unless its Crewmate call names another model or agents.defaultModel is set.
  • It does not isolate every crewmate in a git worktree by default. Worktree isolation is opt-in, per launch (isolation: "worktree") or in a custom crewmate definition, inside a git repository; by default crewmates share the session's working directory.
  • It does not enforce file leases inside general file tools. Project file leases are enforced automatically only by the Godot engine service; Edit, Write, ChangeSet, AstEdit, NotebookEdit and Bash treat leases as advisory coordination.
  • It does not checkpoint arbitrary shell writes. /rewind tracks files modified by Mercury's file-editing tools, not files rewritten by arbitrary Bash commands.
  • It does not replace an OS sandbox or container. Workspace trust and command safety rules guard against mistakes alongside an OS sandbox or container rather than acting as a sealed security boundary.
  • It does not send local models the full upfront tool list. Local models and rented TensorFold pods receive a compact prompt and core tool schemas only (plus any tool marked to always load), deferring the rest behind ToolSearch unless MERCURY_LOCAL_PROMPT_PROFILE=0.