Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
fix(cli): honour --fail-on-skip in backup dry runs (#2110)
Kevinab18eedd9f93
diff --git a/semantica/cli.py b/semantica/cli.pyindex 54dcea5f..72b73d50 100644--- a/semantica/cli.py+++ b/semantica/cli.py@@ -59,6 +59,9 @@ if TYPE_CHECKING:from .core.orchestrator import Semanticaconsole = Console()+# --json reserves stdout for the machine-readable payload, so warnings go+# here instead. See the --json promise in the CLI context setup below.+err_console = Console(stderr=True)# Supported interpreter range: ``requires-python = ">=3.11,<3.14"`` in# pyproject.toml. tests/test_python_support_policy.py fails if the two drift.@@ -658,8 +661,9 @@ def main(# Reinitialize the module-level console if --no-color was requested so# all subsequent console.print() calls in this invocation respect the flag.if no_color:- global console # noqa: PLW0603+ global console, err_console # noqa: PLW0603console = Console(no_color=True)+ err_console = Console(stderr=True, no_color=True)ctx.obj = CLIContext(config_path=_resolve_config_path(config_path),config=config,@@ -1759,7 +1763,10 @@ def _info(cli_ctx: CLIContext, text: str) -> None:def _warn(cli_ctx: CLIContext, text: str) -> None:- console.print(f"[{_WARN_STY}] ⚠[/{_WARN_STY}] {text}")+ # Under --json the payload owns stdout, so the warning goes to stderr and+ # the output stays one parseable document.+ stream = err_console if cli_ctx.json_output else console+ stream.print(f"[{_WARN_STY}] ⚠[/{_WARN_STY}] {text}")def _dry(cli_ctx: CLIContext, action: str, *, json_out: bool = False, **fields: Any) -> None:@@ -5657,7 +5664,7 @@ def backup_info(cli_ctx: CLIContext, local_json: bool) -> None:@click.option("--strip-config", is_flag=True, default=False,help="Exclude semantica.yaml to avoid storing credentials at rest.")@click.option("--fail-on-skip", is_flag=True, default=False,- help="Exit non-zero when a configured store path does not exist.")+ help="Exit non-zero when a configured store path is missing or empty.")@click.option("--dry-run", "local_dry", is_flag=True, default=False)@click.option("--quiet", "local_quiet", is_flag=True, default=False)@click.pass_obj@@ -5677,7 +5684,22 @@ def backup_create(def _action() -> None:if _is_dry(cli_ctx, local_dry):- _dry(cli_ctx, "backup create", destination=destination, include=include)+ # A dry run is a preview of the real command, so it has to report+ # the same skipped stores and honour --fail-on-skip (#2109).+ cfg = cli_ctx.config.to_dict()+ _, skipped = _collect_backup_sources(+ cfg, cli_ctx.config_path, include, strip_config,+ )+ _warn_skipped(cli_ctx, skipped)+ if fail_on_skip:+ _raise_on_skipped(skipped, target="in the archive")+ _dry(+ cli_ctx,+ "backup create",+ destination=destination,+ include=include,+ skipped=skipped,+ )returnif keyfile:@@ -5842,15 +5864,15 @@ def backup_sync(cli_ctx: CLIContext, destination: str, include: str,sources, skipped = _collect_backup_sources(cfg, cli_ctx.config_path, include, strip_config=False,)- if _is_dry(cli_ctx, local_dry):- _dry(cli_ctx, "sync backup", destination=str(dest), include=include,- files=[a for a, _ in sources])- return# A skipped store used to be discarded here, so a sync after a partial# create reported success for an incomplete store set (#2014)._warn_skipped(cli_ctx, skipped)if fail_on_skip:_raise_on_skipped(skipped, target="included in the sync")+ if _is_dry(cli_ctx, local_dry):+ _dry(cli_ctx, "sync backup", destination=str(dest), include=include,+ files=[a for a, _ in sources], skipped=skipped)+ returndest.mkdir(parents=True, exist_ok=True)try:os.chmod(str(dest), 0o700)diff --git a/tests/test_cli_commands.py b/tests/test_cli_commands.pyindex 43c6993e..bf120ba0 100644--- a/tests/test_cli_commands.py+++ b/tests/test_cli_commands.py@@ -3603,6 +3603,181 @@ class TestBackup:assert "Configured store path does not exist" in _flatten(result.output)assert not os.path.exists("out.tar.gz")+ def test_create_dry_run_fail_on_skip_exits_nonzero(self, runner):+ # #2109: a dry run has to match the real command.+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "create", "out.tar.gz",+ "--strip-config", "--dry-run", "--fail-on-skip"],+ )+ assert result.exit_code != 0+ flat = _flatten(result.output)+ assert "Skipping graph store" in flat+ assert "empty_graph" in flat+ assert "Configured store path does not exist" in flat+ assert not os.path.exists("out.tar.gz")++ def test_create_dry_run_reports_a_skipped_store(self, runner):+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "create", "out.tar.gz",+ "--strip-config", "--dry-run"],+ )+ _ok(result)+ flat = _flatten(result.output)+ assert "Skipping graph store" in flat+ assert "empty_graph" in flat+ assert not os.path.exists("out.tar.gz")++ def test_create_dry_run_json_stays_parseable_when_a_store_is_skipped(self, runner):+ # #2110 review: the skip warning used to land on stdout ahead of the+ # payload, so --json could not be parsed in exactly the case this PR+ # fixes.+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "--json", "backup", "create",+ "out.tar.gz", "--strip-config", "--dry-run"],+ )+ _ok(result)+ payload = json.loads(result.stdout)+ assert payload["skipped"] == [+ {"store": "graph", "backend": "local", "path": "empty_graph"}+ ]+ assert "Skipping graph store" in result.stderr+ assert not os.path.exists("out.tar.gz")++ def test_sync_dry_run_json_stays_parseable_when_a_store_is_skipped(self, runner):+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "--json", "backup", "sync",+ "sync_dest", "--dry-run"],+ )+ _ok(result)+ payload = json.loads(result.stdout)+ assert payload["skipped"] == [+ {"store": "graph", "backend": "local", "path": "empty_graph"}+ ]+ assert "Skipping graph store" in result.stderr+ assert not os.path.exists("sync_dest")++ def test_no_color_strips_colour_from_the_json_warning_on_stderr(+ self, runner, monkeypatch+ ):+ # Under --json the warning goes to err_console, not console. Give that+ # console a forced terminal first, so a rebind that misses it would+ # leave escape codes in the captured stderr.+ monkeypatch.setenv("TERM", "xterm-256color")+ original = cli_module.err_console+ cli_module.err_console = cli_module.Console(stderr=True, force_terminal=True)+ try:+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ [+ "--config",+ "cfg.yaml",+ "--json",+ "--no-color",+ "backup",+ "sync",+ "sync_dest",+ "--dry-run",+ ],+ )+ _ok(result)+ assert "Skipping graph store" in result.stderr+ assert "\x1b[" not in result.stderr+ finally:+ cli_module.err_console = original++ def test_dry_run_json_reports_no_skips_when_every_store_is_present(self, runner):+ with runner.isolated_filesystem():+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "--json", "backup", "create",+ "out.tar.gz", "--strip-config", "--dry-run"],+ )+ _ok(result)+ assert json.loads(result.stdout)["skipped"] == []+def test_collect_backup_sources_marks_empty_directories_as_skipped(self, runner):with runner.isolated_filesystem():os.makedirs("empty_graph")@@ -3690,6 +3865,60 @@ class TestBackup:assert "Configured store path does not exist" in _flatten(result.output)assert not os.path.exists("sync_dest")+ def test_sync_dry_run_fail_on_skip_exits_nonzero(self, runner):+ # #2109: the dry-run branch used to return before the skip handling.+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "sync", "sync_dest",+ "--dry-run", "--fail-on-skip"],+ )+ assert result.exit_code != 0+ flat = _flatten(result.output)+ assert "Skipping graph store" in flat+ assert "empty_graph" in flat+ assert "Configured store path does not exist" in flat+ assert not os.path.exists("sync_dest")++ def test_sync_dry_run_reports_a_skipped_store(self, runner):+ with runner.isolated_filesystem():+ os.makedirs("empty_graph")+ os.makedirs("vecs")+ with open("vecs/index.json", "w") as f:+ f.write("{}")+ with open("cfg.yaml", "w") as f:+ f.write(+ "graph_db:\n"+ " backend: local\n"+ " path: empty_graph\n"+ "vector_store:\n"+ " backend: local\n"+ " path: vecs\n"+ )+ result = runner.invoke(+ cli_module.main,+ ["--config", "cfg.yaml", "backup", "sync", "sync_dest",+ "--dry-run"],+ )+ _ok(result)+ flat = _flatten(result.output)+ assert "Skipping graph store" in flat+ assert "empty_graph" in flat+ assert not os.path.exists("sync_dest")+def test_sync_without_skips_does_not_warn(self, runner):with runner.isolated_filesystem():os.makedirs("kg")