Skip to content

Fix CLI domain failures exiting with code 0#993

Open
Nih1tGupta wants to merge 1 commit into
mainfrom
fix/cli-nonzero-exit-on-failure
Open

Fix CLI domain failures exiting with code 0#993
Nih1tGupta wants to merge 1 commit into
mainfrom
fix/cli-nonzero-exit-on-failure

Conversation

@Nih1tGupta

@Nih1tGupta Nih1tGupta commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Closes #992

Summary

  • Propagate non-zero exit codes from Typer/Click when run_cli() uses standalone_mode=False, so contract() / fail() outcomes (e.g. CapabilityNotImplemented, ambiguous_pot, validation errors) reach the OS process instead of always exiting 0.
  • Exit with code 1 when graph workbench results emit "ok": false via _emit_graph_result(), closing the gap for in-band failures that never raised an exception (e.g. invalid graph nudge events).

Problem

The CLI printed correct structured errors (JSON or human) but often returned exit code 0, so scripts, CI, and cmd && next_step treated failed commands as successful. Parser/usage errors already exited non-zero; many runtime/domain failures did not.

Root causes:

  1. host_cli.run_cli() discarded the return value from app(..., standalone_mode=False). Click returns command exit codes instead of propagating typer.Exit to the process in that mode.
  2. _emit_graph_result() printed graph envelopes with "ok": false without raising, so some graph commands exited 0 despite failure payloads.

Solution

File Change
adapters/inbound/cli/host_cli.py Capture exit_code = app(...) and raise typer.Exit(code) when non-zero
adapters/inbound/cli/commands/graph.py After emitting a failed graph envelope, raise typer.Exit(EXIT_VALIDATION) when ok is false

Documented exit contract (unchanged): 0 ok / 1 validation / 2 unavailable / 3 degraded / 4 auth.

Test plan

  • potpie --json cloud status → exit 2, code: not_implemented
  • potpie --json graph status (ambiguous pot repo) → exit 1, ok: false
  • potpie --json graph nudge --event bogus --session s1 --pot <id> → exit 1, ok: false
  • potpie pot create (missing name) → exit 2 (parser error, unchanged)
  • potpie --json graph catalog --pot <id> → exit 0, ok: true
  • potpie --json cloud status && echo fail → chain stops (does not print)
  • Unit: test_graph_cli_contract, test_cli_usage_errors, test_sentry_daemon, test_cli_ergonomics, test_cli_bootstrap_status (145 tests)

Manual verification tip: check Potpie’s exit code without piping (cmd; echo $?), since $? after | jq reflects jq, not potpie.

@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The CLI's graph command now raises a validation exit code when the result payload indicates failure. The host CLI now captures the Typer app's invocation return value and propagates it as a non-zero exit code when truthy.

Changes

CLI Exit Code Propagation

Layer / File(s) Summary
Graph command failure exit
potpie/context-engine/adapters/inbound/cli/commands/graph.py
_emit_graph_result raises typer.Exit(code=EXIT_VALIDATION) when payload.get("ok", True) is False, after emitting the graph envelope.
Host CLI exit code propagation
potpie/context-engine/adapters/inbound/cli/host_cli.py
run_cli() captures the Typer app's return value into exit_code and raises typer.Exit(code=int(exit_code)) when it is truthy, in addition to existing Abort/ClickException handling.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the main change: fixing CLI failures that incorrectly exit with code 0.
Description check ✅ Passed The description directly matches the code changes and explains the exit-code propagation fix in detail.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@potpie/context-engine/adapters/inbound/cli/commands/graph.py`:
- Around line 396-398: The downstream ok re-checks are dead code because
_emit_graph_result already raises typer.Exit when payload.get("ok", True) is
False. Remove the redundant post-call checks in _emit_inbox_result,
_emit_quality_result, and _emit_graph_read so those callers just rely on
_emit_graph_result’s exit behavior and no longer repeat unreachable validation
logic.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro

Run ID: 2aa8b21a-dd58-423b-8d78-ab3a1b930c76

📥 Commits

Reviewing files that changed from the base of the PR and between a191a5e and 95e4b30.

📒 Files selected for processing (2)
  • potpie/context-engine/adapters/inbound/cli/commands/graph.py
  • potpie/context-engine/adapters/inbound/cli/host_cli.py

Comment thread potpie/context-engine/adapters/inbound/cli/commands/graph.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Domain errors and ok:false JSON exit 0 — automation cannot detect CLI failures

1 participant