From a5e5d5968a3cf980409a525bd16d8d227c97fb62 Mon Sep 17 00:00:00 2001 From: Luis C Date: Wed, 4 Nov 2026 23:28:38 -0500 Subject: [PATCH] feat(config): reject an owner-prefixed deployment.name and add --validate-config A name such as replicate/foo made the deployment path replicate/replicate/foo, which 404s at the Push step. --- README.md | 9 ++++- cog_safe_push/config.py | 13 ++++++- cog_safe_push/main.py | 9 +++++ test/test_main.py | 76 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c20ff48..a0c291a 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,7 @@ usage: cog-safe-push [-h] [--config CONFIG] [--help-config] [--fuzz-iterations FUZZ_ITERATIONS] [--fuzz-prompt FUZZ_PROMPT] [--parallel PARALLEL] [--ignore-schema-compatibility] [-v] - [--push-official-model] + [--push-official-model] [--validate-config] [model] Safely push a Cog model, with tests @@ -175,6 +175,9 @@ options: -v, --verbose Increase verbosity level (max 3) --push-official-model Push to the official model defined in the config + --validate-config Load and validate the config, then exit without + testing or pushing anything. Needs no + REPLICATE_API_TOKEN, cog, or network access. ``` ### Using a configuration file @@ -278,6 +281,10 @@ deployment: The deployment will be created under the specified owner (or model owner if not specified) and will use the hardware configuration you provide. +`deployment.name` is the short name only. A name that includes the owner, such as `replicate/my-model-deployment`, is rejected when the config loads. Set `deployment.owner` instead. + +To check a config in CI without testing or pushing, run `cog-safe-push --config cog-safe-push.yaml --validate-config`. It needs no `REPLICATE_API_TOKEN`, `cog`, or network access. + ## Nota bene * This is alpha software. If you find a bug, please open an issue! diff --git a/cog_safe_push/config.py b/cog_safe_push/config.py index e252a42..8472c79 100644 --- a/cog_safe_push/config.py +++ b/cog_safe_push/config.py @@ -2,7 +2,7 @@ import sys from typing import Optional -from pydantic import BaseModel, ConfigDict, model_validator +from pydantic import BaseModel, ConfigDict, field_validator, model_validator from .exceptions import ArgumentError @@ -84,6 +84,17 @@ class DeploymentConfig(BaseModel): name: str | None = None hardware: str | None = None + @field_validator("name") + @classmethod + def name_must_not_include_owner(cls, name: str | None) -> str | None: + if name is not None and "/" in name: + owner, _, short_name = name.partition("/") + raise ValueError( + f'deployment.name must not include the owner ("{name}"); ' + f"set deployment.owner: {owner} and deployment.name: {short_name}" + ) + return name + class Config(BaseModel): model_config = ConfigDict(extra="forbid") diff --git a/cog_safe_push/main.py b/cog_safe_push/main.py index a892290..245bc31 100644 --- a/cog_safe_push/main.py +++ b/cog_safe_push/main.py @@ -160,6 +160,11 @@ def parse_args_and_config() -> tuple[Config, bool, bool]: help="Push to the official model defined in the config", action="store_true", ) + parser.add_argument( + "--validate-config", + help="Load and validate the config, then exit without testing or pushing anything. Needs no REPLICATE_API_TOKEN, cog, or network access.", + action="store_true", + ) args = parser.parse_args() if args.verbose > 3: @@ -207,6 +212,10 @@ def parse_args_and_config() -> tuple[Config, bool, bool]: if not config.test_model: config.test_model = config.model + "-test" + if args.validate_config: + print(f"OK: {config_path or 'command line'} is a valid config") + sys.exit(0) + return config, args.no_push, args.push_official_model diff --git a/test/test_main.py b/test/test_main.py index c49c356..f8ff12d 100644 --- a/test/test_main.py +++ b/test/test_main.py @@ -1,8 +1,10 @@ import pytest from cog_safe_push import log +from cog_safe_push import main as main_module from cog_safe_push.exceptions import ArgumentError, TestCaseFailedError from cog_safe_push.main import ( + main, parse_args_and_config, parse_input_value, parse_inputs, @@ -523,3 +525,77 @@ async def run(self): assert "[3] Test case failed: Test case failed: Invalid input" in error_output # Should not have prediction URL in the third error assert "Invalid input; Prediction URL:" not in error_output + + +def write_config(tmp_path, deployment_name): + config_path = tmp_path / "cog-safe-push.yaml" + config_path.write_text( + f""" +model: user/model +deployment: + owner: replicate + name: {deployment_name} +""" + ) + return config_path + + +def test_config_rejects_owner_prefixed_deployment_name(tmp_path, monkeypatch): + config_path = write_config(tmp_path, "replicate/foo") + monkeypatch.setattr("sys.argv", ["cog-safe-push", "--config", str(config_path)]) + with pytest.raises(ArgumentError, match="deployment.name must not include"): + parse_args_and_config() + + +def test_config_accepts_plain_deployment_name(tmp_path, monkeypatch): + config_path = write_config(tmp_path, "foo") + monkeypatch.setattr("sys.argv", ["cog-safe-push", "--config", str(config_path)]) + config, _, _ = parse_args_and_config() + assert config.deployment is not None + assert config.deployment.name == "foo" + assert config.deployment.owner == "replicate" + + +def test_validate_config_valid(tmp_path, monkeypatch, capsys): + config_path = write_config(tmp_path, "foo") + monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False) + monkeypatch.setattr( + "sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"] + ) + with pytest.raises(SystemExit) as exc_info: + parse_args_and_config() + assert exc_info.value.code == 0 + assert "OK" in capsys.readouterr().out + + +def test_validate_config_invalid(tmp_path, monkeypatch, capsys): + config_path = write_config(tmp_path, "replicate/foo") + monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False) + monkeypatch.setattr( + "sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"] + ) + with pytest.raises(SystemExit) as exc_info: + main() + assert exc_info.value.code == 1 + err = capsys.readouterr().err + assert "deployment.name must not include the owner" in err + assert "deployment.owner: replicate and deployment.name: foo" in err + + +def test_validate_config_makes_no_network_calls(tmp_path, monkeypatch): + import socket + + def fail(*_args, **_kwargs): + raise AssertionError("network used") + + monkeypatch.setattr(socket, "socket", fail) + monkeypatch.setattr(socket, "create_connection", fail) + monkeypatch.setattr(main_module, "run_config", fail) + config_path = write_config(tmp_path, "foo") + monkeypatch.delenv("REPLICATE_API_TOKEN", raising=False) + monkeypatch.setattr( + "sys.argv", ["cog-safe-push", "--config", str(config_path), "--validate-config"] + ) + with pytest.raises(SystemExit) as exc_info: + main() + assert exc_info.value.code == 0