Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(cli): jarvisctl emits JSON to non-TTY consumers (brain/pipes/scripts)

Ruben Lütke15299f3c4225
diff --git a/jarvis/cli_ctl/render.py b/jarvis/cli_ctl/render.py
index 86738581bc..2c2b1b3e09 100644
--- a/jarvis/cli_ctl/render.py
+++ b/jarvis/cli_ctl/render.py
@@ -16,8 +16,23 @@ _out = Console(highlight=False)
_err = Console(stderr=True, highlight=False)
+def _stdout_isatty() -> bool:
+ """True only for a real interactive terminal. Any failure (an exotic stdio
+ wrapper without ``isatty``, or one that raises) is treated as
+ non-interactive, so non-TTY consumers — the brain's piped subprocess, a
+ shell pipe, a script — receive machine-readable JSON."""
+ try:
+ return bool(sys.stdout.isatty())
+ except Exception: # noqa: BLE001 - non-interactive is the safe default
+ return False
+
+
def emit(payload: Any, *, as_json: bool) -> None:
- if as_json:
+ # Machine-readable JSON when explicitly requested (--json) OR whenever stdout
+ # is not an interactive terminal. The cli_jarvisctl tool runs `jarvisctl`
+ # with a piped stdout, so the brain (and any pipe/script) gets parsable JSON
+ # instead of a Rich table it would have to parse character-by-character.
+ if as_json or not _stdout_isatty():
# ensure_ascii=False keeps umlauts/emoji intact across platforms.
sys.stdout.write(json.dumps(payload, indent=2, ensure_ascii=False) + "\n")
return
diff --git a/tests/unit/cli_ctl/test_render.py b/tests/unit/cli_ctl/test_render.py
index eee528bc3f..dfa0911a7a 100644
--- a/tests/unit/cli_ctl/test_render.py
+++ b/tests/unit/cli_ctl/test_render.py
@@ -10,13 +10,41 @@ def test_emit_json_mode_prints_raw_json(capsys):
assert json.loads(out) == {"a": 1, "ä": "ö"} # UTF-8 preserved, not escaped (i18n-allow)
-def test_emit_human_list_of_dicts_prints_table(capsys):
+def test_emit_human_list_of_dicts_prints_table(capsys, monkeypatch):
+ # Force an interactive terminal so the human Rich-table path is exercised.
+ monkeypatch.setattr(render, "_stdout_isatty", lambda: True)
rows = [{"id": "1", "state": "scheduled"}, {"id": "2", "state": "running"}]
render.emit(rows, as_json=False)
out = capsys.readouterr().out
assert "id" in out and "state" in out and "scheduled" in out
+def test_emit_non_tty_defaults_to_json(capsys, monkeypatch):
+ # The brain / pipes / scripts: stdout is not a TTY → JSON even without --json.
+ monkeypatch.setattr(render, "_stdout_isatty", lambda: False)
+ rows = [{"id": "1", "state": "scheduled"}]
+ render.emit(rows, as_json=False)
+ out = capsys.readouterr().out
+ assert json.loads(out) == [{"id": "1", "state": "scheduled"}]
+
+
+def test_emit_json_flag_wins_over_tty(capsys, monkeypatch):
+ # An explicit --json forces JSON even in an interactive terminal.
+ monkeypatch.setattr(render, "_stdout_isatty", lambda: True)
+ render.emit({"k": "v"}, as_json=True)
+ out = capsys.readouterr().out
+ assert json.loads(out) == {"k": "v"}
+
+
+def test_stdout_isatty_defaults_false_without_isatty(monkeypatch):
+ # An exotic stdout wrapper without isatty must not crash; default to JSON.
+ class _NoIsatty:
+ pass
+
+ monkeypatch.setattr(render.sys, "stdout", _NoIsatty())
+ assert render._stdout_isatty() is False
+
+
def test_error_sets_message_on_stderr(capsys):
render.error("boom")
err = capsys.readouterr().err