Skip to content

Fix CLI domain failures exiting with code 0 - #993

Merged
nndn merged 1 commit into
mainfrom
fix/cli-nonzero-exit-on-failure
Jul 28, 2026
Merged

Fix CLI domain failures exiting with code 0#993
nndn merged 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

Summary by CodeRabbit

  • Bug Fixes
    • Improved CLI error handling for graph validation failures.
    • Ensured non-zero command results consistently produce the appropriate CLI exit status.
    • Prevented successful-looking command completion when validation errors occur.

Walkthrough

The CLI now propagates graph validation failures and non-zero Typer application return values as process exit codes after emitting available error output.

Changes

CLI Exit Code Propagation

Layer / File(s) Summary
Graph command failure exit handling
potpie/cli/commands/graph.py
_emit_graph_result raises EXIT_VALIDATION when the normalized graph payload has ok: false, after emitting the error output.
CLI exit code capture and propagation
potpie/cli/main.py
run_cli captures the Typer app return value and raises typer.Exit with its integer value when it is non-zero.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the main change: fixing CLI failures that were incorrectly exiting with code 0.
Description check ✅ Passed The description directly describes the same exit-code fix and the affected CLI paths.
Linked Issues check ✅ Passed The changes implement the linked issue by propagating non-zero CLI exits and raising validation exits for ok:false graph results.
Out of Scope Changes check ✅ Passed The modified files and behavior are directly tied to the issue and don't introduce unrelated scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
📋 Issue Planner

Built with CodeRabbit's Coding Plans for faster development and fewer bugs.

View plan used: #992


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/cli/commands/graph.py
@Nih1tGupta
Nih1tGupta force-pushed the fix/cli-nonzero-exit-on-failure branch from 95e4b30 to 4a54292 Compare July 28, 2026 12:08

@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.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
potpie/cli/main.py (1)

94-105: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Route timeline recent through graph failure handling.

timeline_recent enters contract() at Line 694 but ultimately calls _emit_read, which emits an ok: false payload without raising. This bypasses _emit_graph_result and can still return exit code 0 for failed timeline graph reads. Use _graph_command plus _emit_graph_read, or explicitly raise typer.Exit(EXIT_VALIDATION) after emitting a failed result.

🤖 Prompt for 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.

In `@potpie/cli/main.py` around lines 94 - 105, Update timeline_recent so failed
graph reads cannot return exit code 0: route its contract/read flow through
_graph_command and _emit_graph_read, or after emitting a failed result
explicitly raise typer.Exit(EXIT_VALIDATION). Preserve successful timeline
output and ensure the failure path uses the existing graph failure handling.
🤖 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.

Outside diff comments:
In `@potpie/cli/main.py`:
- Around line 94-105: Update timeline_recent so failed graph reads cannot return
exit code 0: route its contract/read flow through _graph_command and
_emit_graph_read, or after emitting a failed result explicitly raise
typer.Exit(EXIT_VALIDATION). Preserve successful timeline output and ensure the
failure path uses the existing graph failure handling.

ℹ️ Review info
⚙️ Run configuration

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

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 1ec10da4-d30a-4567-8ff0-e3670ea2da0c

📥 Commits

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

📒 Files selected for processing (2)
  • potpie/cli/commands/graph.py
  • potpie/cli/main.py

@nndn
nndn self-requested a review July 28, 2026 12:13
@nndn
nndn merged commit 6fae3f3 into main Jul 28, 2026
19 checks passed
@nndn
nndn deleted the fix/cli-nonzero-exit-on-failure branch July 28, 2026 12:14
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

2 participants