Skip to content

"CLI-Anything: Making ALL Software Agent-Native" -- CLI-Hub: https://clianything.cc/

Python

fix(sbox): exit non-zero on command failures (PR #251 review)

KarandeepSWrancheebddbc51585
diff --git a/README.md b/README.md
index 75ae536a7..b8a9d348d 100644
--- a/README.md
+++ b/README.md
@@ -1000,7 +1000,7 @@ Each application received complete, production-ready CLI interfaces — not demo
<td>Game Development</td>
<td><code>cli-anything-sbox</code></td>
<td>Source 2 game engine - direct file I/O on .scene/.prefab/.vmat/.vsnd JSON</td>
-<td align="center">✅ 222</td>
+<td align="center">✅ 238</td>
</tr>
<tr>
<td align="center"><strong>🐞 <a href="lldb/agent-harness/">LLDB</a></strong></td>
@@ -1166,7 +1166,7 @@ cli-anything/
├── 🛡️ adguardhome/agent-harness/ # AdGuard Home CLI (36 tests)
├── 🦙 ollama/agent-harness/ # Ollama CLI (98 tests)
├── 🎮 godot/agent-harness/ # Godot Engine CLI (24 tests)
-├── 📦 sbox/agent-harness/ # s&box CLI (222 tests: 155 unit + 17 orchestrator + 50 e2e)
+├── 📦 sbox/agent-harness/ # s&box CLI (238 tests: 155 unit + 17 orchestrator + 50 e2e + 16 exit-code)
├── 🎨 sketch/agent-harness/ # Sketch CLI (19 tests, Node.js)
├── 🔬 renderdoc/agent-harness/ # RenderDoc CLI (59 tests)
├── 🟩 nsight-graphics/agent-harness/ # Nsight Graphics CLI (40 tests)
diff --git a/README_CN.md b/README_CN.md
index 61b996f70..2b0d7013d 100644
--- a/README_CN.md
+++ b/README_CN.md
@@ -374,7 +374,7 @@ AI Agent 推理能力很强,但操控真实专业软件的能力很弱。现
| 💸 "GUI 自动化三天两头崩" | 告别截图、点击和 RPA 的脆弱性,纯命令行操控,结构化接口 |
| 📊 "Agent 需要结构化数据" | 内置 JSON 输出供 Agent 直接消费,同时保留可读格式方便调试 |
| 🔧 "定制集成太贵了" | 一个插件就能为任意代码库自动生成 CLI,经过验证的 7 阶段流水线 |
-| ⚡ "原型和生产之间差十万八千里" | 1,741+ 测试用例,全部在真实软件上验证通过,覆盖 13 款主流应用 |
+| ⚡ "原型和生产之间差十万八千里" | 1,757+ 测试用例,全部在真实软件上验证通过,覆盖 13 款主流应用 |
---
@@ -606,7 +606,7 @@ CLI-Anything 适用于任何有代码库的软件 —— 不限领域,不限
<td>游戏开发</td>
<td><code>cli-anything-sbox</code></td>
<td>Source 2 游戏引擎 - 直接读写 .scene/.prefab/.vmat/.vsnd JSON</td>
-<td align="center">✅ 222</td>
+<td align="center">✅ 238</td>
</tr>
<tr>
<td align="center"><strong>🎨 Sketch</strong></td>
@@ -617,7 +617,7 @@ CLI-Anything 适用于任何有代码库的软件 —— 不限领域,不限
</tr>
<tr>
<td align="center" colspan="4"><strong>合计</strong></td>
-<td align="center"><strong>✅ 1,741</strong></td>
+<td align="center"><strong>✅ 1,757</strong></td>
</tr>
</table>
@@ -655,7 +655,7 @@ nsight-graphics 40 passed ✅ (36 unit + 4 e2e)
unrealinsights 50 passed ✅ (49 unit + 1 e2e, 9 backend-gated e2e skipped)
sketch 19 passed ✅ (19 jest, Node.js)
──────────────────────────────────────────────────────────────────────────────
-TOTAL 1,741 passed ✅ 100% pass rate
+TOTAL 1,757 passed ✅ 100% pass rate
```
---
@@ -725,7 +725,7 @@ cli-anything/
├── 🟩 nsight-graphics/agent-harness/ # Nsight Graphics CLI(40 项测试)
├── 🐞 lldb/agent-harness/ # LLDB CLI(27 项测试)
├── 📈 unrealinsights/agent-harness/ # Unreal Insights CLI(50 项测试)
-├── 📦 sbox/agent-harness/ # s&box CLI(222 项测试: 155 单元 + 17 orchestrator + 50 e2e)
+├── 📦 sbox/agent-harness/ # s&box CLI(238 项测试: 155 单元 + 17 orchestrator + 50 e2e + 16 exit-code)
└── 🎨 sketch/agent-harness/ # Sketch CLI(19 项测试,Node.js)
```
@@ -962,7 +962,7 @@ MIT License — 可自由使用、修改和分发。
**CLI-Anything** — *一行命令,让任何软件成为 Agent 的原生工具。*
-<sub>为 AI Agent 时代而生 | 13 款专业软件实测 | 1,741 项测试全部通过</sub>
+<sub>为 AI Agent 时代而生 | 13 款专业软件实测 | 1,757 项测试全部通过</sub>
<br>
diff --git a/sbox/agent-harness/cli_anything/sbox/core/session.py b/sbox/agent-harness/cli_anything/sbox/core/session.py
index d5bdce835..577c0c04c 100644
--- a/sbox/agent-harness/cli_anything/sbox/core/session.py
+++ b/sbox/agent-harness/cli_anything/sbox/core/session.py
@@ -3,6 +3,7 @@
import copy
import json
import os
+import sys
import time
from pathlib import Path
from typing import Any, Dict, List, Optional
@@ -69,7 +70,9 @@ class Session:
def load( self ) -> None:
"""Load session from disk.
- If the file does not exist or is malformed, resets to empty state.
+ If the file does not exist or is malformed, resets to empty state. A
+ malformed file is preserved as ``<path>.corrupt`` so the user's undo
+ history is not silently destroyed.
"""
try:
with open( self._session_path, "r", encoding="utf-8" ) as f:
@@ -80,7 +83,20 @@ class Session:
self._data = merged
else:
self._data = _empty_session_data()
- except (OSError, json.JSONDecodeError, ValueError):
+ except (OSError, json.JSONDecodeError, ValueError) as exc:
+ # Preserve the malformed file before resetting so the user can
+ # recover state if needed; emit a stderr warning so the reset is
+ # not silent.
+ try:
+ backup = self._session_path + ".corrupt"
+ if os.path.isfile( self._session_path ):
+ os.replace( self._session_path, backup )
+ sys.stderr.write(
+ f"warning: session file at {self._session_path} could not be read "
+ f"({exc}); preserved as {backup} and reset to empty state\n"
+ )
+ except OSError:
+ pass
self._data = _empty_session_data()
def save( self ) -> None:
diff --git a/sbox/agent-harness/cli_anything/sbox/core/test_orchestrator.py b/sbox/agent-harness/cli_anything/sbox/core/test_orchestrator.py
index e3837e9b6..fc9cea907 100644
--- a/sbox/agent-harness/cli_anything/sbox/core/test_orchestrator.py
+++ b/sbox/agent-harness/cli_anything/sbox/core/test_orchestrator.py
@@ -213,6 +213,14 @@ def run_single_combo(
}
png_path = collect_screenshot(data_path, output_dir, strategy, size, seed)
+ # If sentinel reported success but no screenshot was produced, surface
+ # this as a failure rather than reporting a successful run with no PNG.
+ if png_path is None:
+ return {
+ "success": False,
+ "error": "screenshot files not produced (no metadata.json or rgba)",
+ "combo": combo,
+ }
return {"success": True, "png_path": png_path, "combo": combo}
finally:
diff --git a/sbox/agent-harness/cli_anything/sbox/sbox_cli.py b/sbox/agent-harness/cli_anything/sbox/sbox_cli.py
index b58e0226d..c2a7b9bcc 100644
--- a/sbox/agent-harness/cli_anything/sbox/sbox_cli.py
+++ b/sbox/agent-harness/cli_anything/sbox/sbox_cli.py
@@ -9,6 +9,7 @@ import click
import json
import os
import shlex
+import sys
from typing import Any, Optional
from cli_anything.sbox.core import (
@@ -52,11 +53,20 @@ def _output(ctx: click.Context, data: Any, human_fn=None) -> None:
def _output_error(ctx: click.Context, message: str) -> None:
- """Output an error message in the appropriate format."""
+ """Output an error message and signal failure to scripts and agents.
+
+ In one-shot mode, exits the process with code 1 after printing so callers
+ observe a non-zero exit on failure. In REPL mode (``ctx.obj["repl"]`` is
+ truthy), prints and returns so the REPL loop continues. Centralising the
+ exit-on-error here keeps per-command try/except blocks simple while still
+ propagating failures correctly.
+ """
if ctx.obj.get( "json" ):
click.echo( json.dumps( {"error": message} ) )
else:
click.echo( f"Error: {message}", err=True )
+ if not ctx.obj.get( "repl" ):
+ sys.exit( 1 )
def _format_table(rows: list, headers: list) -> str:
@@ -329,6 +339,9 @@ def project_validate( ctx, no_refs, no_guids, no_inputs ):
return "\n".join( lines )
_output( ctx, result, human )
+ # Validation failure must exit non-zero so CI can gate on this command.
+ if not result.get( "ok", True ) and not ctx.obj.get( "repl" ):
+ sys.exit( 1 )
except click.ClickException:
raise
except Exception as exc:
@@ -1602,6 +1615,11 @@ def server_info( ctx ):
try:
exe = sbox_backend.find_executable( "sbox-server" )
version = sbox_backend.get_sbox_version()
+ # get_sbox_version is documented to return {"error": ...} on failure
+ # rather than raising, so check explicitly to propagate exit code.
+ if isinstance( version, dict ) and version.get( "error" ):
+ _output_error( ctx, f"Failed to read s&box version: {version['error']}" )
+ return
result = {"executable": exe, "version": version}
_output( ctx, result, lambda d: _format_status_block( {
"executable": d["executable"],
@@ -1664,6 +1682,12 @@ def asset_info( ctx, asset_path ):
"""Show asset details."""
try:
result = export_mod.get_asset_info( asset_path )
+ # _parse_json_asset embeds parse failures as json_info.error; surface
+ # those so a malformed scene/prefab yields a non-zero exit code.
+ json_info_error = result.get( "json_info", {} ).get( "error" ) if isinstance( result.get( "json_info" ), dict ) else None
+ if json_info_error:
+ _output_error( ctx, f"{result.get( 'name', asset_path )}: {json_info_error}" )
+ return
_output( ctx, result, lambda d: _format_status_block( d, f"Asset: {d.get( 'name', '' )}" ) )
except Exception as exc:
_output_error( ctx, str( exc ) )
@@ -1677,6 +1701,12 @@ def asset_compile( ctx, asset_path ):
try:
from cli_anything.sbox.utils import sbox_backend
result = sbox_backend.run_resource_compiler( asset_path )
+ if not result.get( "success", False ):
+ rc = result.get( "return_code", "?" )
+ stderr = ( result.get( "stderr" ) or "" ).strip()
+ detail = f": {stderr}" if stderr else ""
+ _output_error( ctx, f"Resource compilation failed (return code {rc}){detail}" )
+ return
_output( ctx, result, lambda d: f"Compiled: {d.get( 'asset_path', asset_path )}" )
except click.ClickException:
raise
@@ -2250,6 +2280,10 @@ def test_run( ctx, strategies, sizes, seeds, seed_count, timeout ):
}
_output( ctx, summary, lambda d: _format_status_block( d, "Test Run Complete" ) )
+ # Partial-failure: scripts/agents need a non-zero exit when any combo
+ # fails, otherwise the summary is misleading (printed N failures, exit 0).
+ if failed > 0 and not ctx.obj.get( "repl" ):
+ sys.exit( 1 )
except Exception as exc:
_output_error( ctx, str( exc ) )
@@ -2303,8 +2337,16 @@ Example: project info
@click.pass_context
def repl( ctx ):
"""Enter interactive REPL mode."""
- from cli_anything.sbox.utils.repl_skin import ReplSkin
- skin = ReplSkin()
+ # Mark REPL mode so _output_error prints + returns instead of sys.exit(1).
+ # Shared via obj=ctx.obj on the inner cli invocation below so child
+ # commands see the same flag.
+ ctx.obj["repl"] = True
+
+ try:
+ from cli_anything.sbox.utils.repl_skin import ReplSkin
+ skin = ReplSkin( "sbox", version="1.0.0" )
+ except (ImportError, TypeError):
+ skin = None
def echo( msg ):
if skin and hasattr( skin, "info" ):
@@ -2355,7 +2397,7 @@ def repl( ctx ):
extra_args.extend( ["--project", project_path] )
try:
- cli( extra_args + args, standalone_mode=False, parent=ctx.parent )
+ cli( extra_args + args, standalone_mode=False, obj=ctx.obj )
except SystemExit:
# Click may raise SystemExit on --help or errors; absorb it in REPL mode
pass
diff --git a/sbox/agent-harness/cli_anything/sbox/tests/TEST.md b/sbox/agent-harness/cli_anything/sbox/tests/TEST.md
index fd6b4444c..fd807cab6 100644
--- a/sbox/agent-harness/cli_anything/sbox/tests/TEST.md
+++ b/sbox/agent-harness/cli_anything/sbox/tests/TEST.md
@@ -10,7 +10,8 @@ the `agent-harness/` directory.
| `test_core.py` | All 13 core modules: project, scene, prefab, codegen, input_config, collision_config, material, sound, localization, session, export, validate, plus the s&box backend resolver | **155** |
| `test_full_e2e.py` | CLI surface via subprocess invocation, project workflows end-to-end, sbox_backend integration | **50** |
| `test_orchestrator.py` | Map-generation test orchestrator (combo matrix, sentinel polling, RGBA -> PNG conversion, config I/O) | **17** |
-| **Total** | | **222** |
+| `test_exit_codes.py` | One-shot CLI exit-code contract: failures exit 1 (human + JSON modes), success exits 0, validation failure exits 1, REPL absorbs SystemExit | **16** |
+| **Total** | | **238** |
## 2. Running the Tests
@@ -103,6 +104,15 @@ python -m pytest cli_anything/sbox/tests/ -v
| `TestRgbaConversion` | 2 | RGBA byte-array -> PNG via Pillow |
| `TestDataPathResolution` | 2 | `FileSystem.Data` path resolution across editor and standalone |
+### `test_exit_codes.py` (16 tests, 3 classes)
+
+| Class | Tests | Coverage |
+|---|---|---|
+| `TestOneShotFailureExitsNonZero` | 9 | Failure paths in one-shot mode return exit 1: missing scene/project file (human + JSON), missing localization key, missing required args, invalid JSON in `--properties`, asset compile without s&box install. Includes the `--help` exit-0 baseline. |
+| `TestOneShotSuccessExitsZero` | 2 | Confirms the success path was not regressed by the exit-1-on-failure change |
+| `TestProjectValidateExitsNonZero` | 2 | `project validate` exits 1 when `ok=False` (broken refs) so CI can gate on it; clean project still exits 0 |
+| `TestReplModeAbsorbsExit` | 3 | `_output_error` does not call `sys.exit` when `ctx.obj["repl"] = True`; one-shot is the safe default when the key is absent |
+
## 4. Coverage by Source Module
| Module | Unit | E2E | Subprocess |
@@ -121,7 +131,7 @@ python -m pytest cli_anything/sbox/tests/ -v
| `core/validate.py` | 4 | - | 2 (validate clean, validate broken) |
| `core/test_orchestrator.py` | 17 (full file) | - | - |
| `utils/sbox_backend.py` | - | 3 (installation, version, executable - skipped if no s&box) | - |
-| `sbox_cli.py` (CLI) | - | - | 40 (full TestCLISubprocess) |
+| `sbox_cli.py` (CLI) | - | - | 40 (full TestCLISubprocess) + 16 (test_exit_codes: failure exit-code contract + REPL absorb) |
## 5. E2E Prerequisites
@@ -136,9 +146,9 @@ python -m pytest cli_anything/sbox/tests/ -v
- `server start` and `launch` commands are tested only via help-text smoke; running an actual s&box server / editor process is out of scope (would require live network sockets and a running game).
- `session undo`/`redo` is tested at the unit level; no E2E workflow exercises the undo journal in a realistic edit-undo-edit sequence.
-- `asset compile` is help-tested only; invoking the s&box `resourcecompiler.exe` requires a full s&box install and is left to the upstream maintainers' CI.
+- `asset compile` is help-tested only; invoking the s&box `resourcecompiler.exe` requires a full s&box install and is left to the upstream maintainers' CI. The non-zero exit-code path on missing/failed compile is exercised by `test_exit_codes.py::test_asset_compile_missing_sbox_exits_one`.
## 7. Known Test-Environment Quirks
-- On Windows, `subprocess.Popen` with inheritable handles can hit `OSError: [WinError 6] The handle is invalid` after ~25-30 sequential subprocess calls in some sandboxed environments (tested in PowerShell, MSYS2 bash). Mitigation: run `TestCLISubprocess` in isolation (`pytest cli_anything/sbox/tests/test_full_e2e.py -k TestCLISubprocess`) or use `--forked` if `pytest-forked` is installed. Tests pass cleanly in non-sandboxed terminals.
+- On Windows + Python 3.14 + pytest, `subprocess.Popen` can hit `OSError: [WinError 6] The handle is invalid` before the child process starts because the parent's stdin handle is not inheritable. Both `TestCLISubprocess._run` (in `test_full_e2e.py`) and `_run` in `test_exit_codes.py` pass `stdin=subprocess.DEVNULL` to work around this. If you add new subprocess invocations in tests, mirror that pattern.
- All file I/O is UTF-8 explicit (`open(..., encoding="utf-8")`). Earlier versions defaulted to the platform codec on Windows; that was fixed before upstream submission.
diff --git a/sbox/agent-harness/cli_anything/sbox/tests/test_exit_codes.py b/sbox/agent-harness/cli_anything/sbox/tests/test_exit_codes.py
new file mode 100644
index 000000000..462867f79
--- /dev/null
+++ b/sbox/agent-harness/cli_anything/sbox/tests/test_exit_codes.py
@@ -0,0 +1,236 @@
+"""Verify one-shot CLI invocations exit non-zero on failure.
+
+Reviewer feedback at HKUDS/CLI-Anything PR #251 flagged that handlers were
+catching exceptions, printing via ``_output_error()``, then returning normally.
+That made scripts and agents see failures as success. These tests pin the
+contract: any failed command in one-shot mode must exit with code 1, while
+the success path stays exit 0 and the REPL absorbs the exit so the loop
+continues.
+
+The subprocess tests use ``python -m cli_anything.sbox`` rather than the
+installed ``cli-anything-sbox`` binary so the suite is portable across
+environments where the launcher script may behave differently (e.g. Windows
++ Python 3.14 native launcher quirks).
+"""
+
+import json
+import os
+import subprocess
+import sys
+
+import pytest
+
+
+CLI = [sys.executable, "-m", "cli_anything.sbox"]
+
+
+def _run(args, cwd=None, env=None, timeout=30):
+ """Run the CLI as a subprocess and return CompletedProcess (no check).
+
+ ``stdin=DEVNULL`` is required on Windows + Python 3.14 + pytest where the
+ parent test runner's stdin handle is not inheritable, otherwise Popen
+ raises ``OSError: [WinError 6] The handle is invalid`` before the child
+ even starts.
+ """
+ return subprocess.run(
+ CLI + list(args),
+ capture_output=True,
+ text=True,
+ timeout=timeout,
+ cwd=cwd,
+ env=env,
+ stdin=subprocess.DEVNULL,
+ )
+
+
+# ---------------------------------------------------------------------------
+# One-shot exit codes - failure paths exit non-zero
+# ---------------------------------------------------------------------------
+
+
+class TestOneShotFailureExitsNonZero:
+ """Failure exits 1 in both human and JSON modes, success exits 0."""
+
+ def test_help_exits_zero(self):
+ result = _run(["--help"])
+ assert result.returncode == 0
+
+ def test_scene_info_missing_file_exits_one_human_mode(self, tmp_path):
+ """Missing file raises FileNotFoundError -> exit 1, error on stderr."""
+ result = _run(["scene", "info", str(tmp_path / "missing.scene")])
+ assert result.returncode == 1
+ assert "Error" in result.stderr
+
+ def test_scene_info_missing_file_exits_one_json_mode(self, tmp_path):
+ """JSON mode emits {"error": ...} on stdout AND exits 1."""
+ result = _run(["--json", "scene", "info", str(tmp_path / "missing.scene")])
+ assert result.returncode == 1
+ data = json.loads(result.stdout)
+ assert "error" in data
+ assert "missing.scene" in data["error"]
+
+ def test_project_info_outside_project_exits_one(self, tmp_path):
+ """Running project info from a directory with no .sbproj exits 1."""
+ result = _run(["project", "info"], cwd=str(tmp_path))
+ assert result.returncode == 1
+
+ def test_project_info_explicit_missing_path_exits_one(self):
+ """--project pointing at a missing path exits 1."""
+ result = _run(
+ ["--project", "/definitely/nonexistent/sbox/project", "project", "info"]
+ )
+ assert result.returncode == 1
+
+ def test_localization_get_missing_key_exits_one(self, tmp_path):
+ """Querying a missing key emits an error and exits 1."""
+ loc_path = tmp_path / "en.json"
+ loc_path.write_text(json.dumps({"existing": "value"}), encoding="utf-8")
+ result = _run(
+ ["localization", "get", str(loc_path), "--key", "missing_key"]
+ )
+ assert result.returncode == 1
+
+ def test_scene_remove_object_missing_args_exits_one(self, tmp_path):
+ """remove-object with no --name or --guid raises ClickException -> exit 1."""
+ scene_path = tmp_path / "stub.scene"
+ scene_path.write_text(json.dumps({"GameObjects": []}), encoding="utf-8")
+ result = _run(["scene", "remove-object", str(scene_path)])
+ assert result.returncode == 1
+
+ def test_codegen_invalid_json_properties_exits_one(self):
+ """Invalid JSON in --properties triggers JSONDecodeError -> exit 1."""
+ result = _run([
+ "codegen", "component", "--name", "Bad", "--properties", "not-json",
+ ])
+ assert result.returncode == 1
+
+ def test_asset_compile_missing_sbox_exits_one(self, tmp_path):
+ """asset compile with no s&box install (or missing file) exits 1.
+
+ Either find_executable raises (no install) or the missing file path
+ triggers FileNotFoundError. Both paths must exit 1.
+ """
+ result = _run(["asset", "compile", str(tmp_path / "nope.vmat")])
+ assert result.returncode == 1
+
+
+class TestOneShotSuccessExitsZero:
+ """Successful one-shot commands still exit 0 after the fix."""
+
+ def test_project_new_exits_zero(self, tmp_path):
+ result = _run([
+ "--json", "project", "new",
+ "--name", "exit_test",
+ "--output-dir", str(tmp_path),
+ ])
+ assert result.returncode == 0
+ data = json.loads(result.stdout)
+ assert data["name"] == "exit_test"
+
+ def test_project_info_on_valid_project_exits_zero(self, tmp_path):
+ _run([
+ "--json", "project", "new",
+ "--name", "info_test",
+ "--output-dir", str(tmp_path),
+ ])
+ result = _run([
+ "--json", "--project", str(tmp_path), "project", "info",
+ ])
+ assert result.returncode == 0
+ data = json.loads(result.stdout)
+ assert data["title"] == "info_test"
+
+
+# ---------------------------------------------------------------------------
+# Validation failure exits non-zero (project_validate ok=False)
+# ---------------------------------------------------------------------------
+
+
+class TestProjectValidateExitsNonZero:
+ """project validate must exit 1 when ok=False so CI can gate on it."""
+
+ def test_validate_clean_project_exits_zero(self, tmp_path):
+ _run([
+ "--json", "project", "new",
+ "--name", "clean", "--output-dir", str(tmp_path),
+ ])
+ result = _run([
+ "--json", "--project", str(tmp_path), "project", "validate",
+ ])
+ assert result.returncode == 0
+ data = json.loads(result.stdout)
+ assert data["ok"] is True
+
+ def test_validate_broken_ref_exits_one(self, tmp_path):
+ """A scene that references a missing prefab makes ok=False -> exit 1."""
+ _run([
+ "--json", "project", "new",
+ "--name", "brokenproj", "--output-dir", str(tmp_path),
+ ])
+ scene_path = tmp_path / "Assets" / "scenes" / "minimal.scene"
+ scene_data = json.loads(scene_path.read_text(encoding="utf-8"))
+ scene_data["GameObjects"].append({
+ "__guid": "11111111-1111-1111-1111-111111111111",
+ "Name": "BrokenRef",
+ "Position": "0,0,0",
+ "Tags": "",
+ "Components": [{
+ "__guid": "22222222-2222-2222-2222-222222222222",
+ "__type": "Sandbox.PrefabScene",
+ "PrefabSource": "missing/prefab/path.prefab",
+ }],
+ })
+ scene_path.write_text(json.dumps(scene_data), encoding="utf-8")
+
+ result = _run([
+ "--json", "--project", str(tmp_path), "project", "validate",
+ ])
+ assert result.returncode == 1
+ data = json.loads(result.stdout)
+ assert data["ok"] is False
+ assert data["issue_count"] >= 1
+
+
+# ---------------------------------------------------------------------------
+# REPL mode unit tests - _output_error must NOT sys.exit when repl=True
+# ---------------------------------------------------------------------------
+
+
+class TestReplModeAbsorbsExit:
+ """Direct unit tests on _output_error to prove REPL mode doesn't exit.
+
+ These tests don't go through the interactive REPL loop because the
+ upstream-shared repl_skin uses unicode glyphs (e.g. ●) that fail to
+ encode on Windows cp1252 console when stdin is piped, which is unrelated
+ to the exit-code contract under test.
+ """
+
+ def test_output_error_does_not_exit_in_repl_mode(self):
+ from click import Context
+ from cli_anything.sbox import sbox_cli
+
+ ctx = Context(sbox_cli.cli)
+ ctx.obj = {"json": False, "repl": True}
+ # Must NOT raise SystemExit when repl flag is set.
+ sbox_cli._output_error(ctx, "test error")
+
+ def test_output_error_exits_in_oneshot_mode(self):
+ from click import Context
+ from cli_anything.sbox import sbox_cli
+
+ ctx = Context(sbox_cli.cli)
+ ctx.obj = {"json": False, "repl": False}
+ with pytest.raises(SystemExit) as exc_info:
+ sbox_cli._output_error(ctx, "test error")
+ assert exc_info.value.code == 1
+
+ def test_output_error_exits_when_repl_key_missing(self):
+ """Defaulting to one-shot when repl flag absent matches the contract."""
+ from click import Context
+ from cli_anything.sbox import sbox_cli
+
+ ctx = Context(sbox_cli.cli)
+ ctx.obj = {"json": False} # no 'repl' key
+ with pytest.raises(SystemExit) as exc_info:
+ sbox_cli._output_error(ctx, "test error")
+ assert exc_info.value.code == 1
diff --git a/sbox/agent-harness/cli_anything/sbox/tests/test_full_e2e.py b/sbox/agent-harness/cli_anything/sbox/tests/test_full_e2e.py
index 1aaffa05a..9e4ade871 100644
--- a/sbox/agent-harness/cli_anything/sbox/tests/test_full_e2e.py
+++ b/sbox/agent-harness/cli_anything/sbox/tests/test_full_e2e.py
@@ -593,6 +593,7 @@ class TestCLISubprocess:
text=True,
check=check,
timeout=30,
+ stdin=subprocess.DEVNULL,
)
def test_help(self):
@@ -1070,7 +1071,12 @@ class TestCLISubprocess:
assert data["duplicate_guids"] == []
def test_project_validate_broken_json( self, tmp_path ):
- """project validate flags a broken model reference."""
+ """project validate flags a broken model reference and exits 1.
+
+ Validation failures must exit non-zero so CI can gate on this command;
+ check=False is required because subprocess.run with check=True would
+ raise CalledProcessError before our assertions run.
+ """
self._run( ["--json", "project", "new", "--name", "v2", "--output-dir", str( tmp_path )] )
scene_path = os.path.join( str( tmp_path ), "Assets", "scenes", "broken.scene" )
self._run( ["--json", "scene", "new", "--name", "broken", "-o", scene_path, "--no-defaults"] )
@@ -1080,8 +1086,11 @@ class TestCLISubprocess:
"--json", "scene", "add-component", scene_path, guid, "Sandbox.ModelRenderer",
"--properties", '{"Model": "models/missing/whoops.vmdl"}',
] )
- result = self._run( ["--json", "--project", str( tmp_path ), "project", "validate", "--no-inputs"] )
- assert result.returncode == 0
+ result = self._run(
+ ["--json", "--project", str( tmp_path ), "project", "validate", "--no-inputs"],
+ check=False,
+ )
+ assert result.returncode == 1
data = json.loads( result.stdout )
assert data["ok"] is False
assert any( "missing/whoops" in b["ref"] for b in data["broken_refs"] )