Skip to content

fix: validate time_range_minutes from CLI args in run.py#207

Open
Sanjays2402 wants to merge 1 commit into
fuzzylabs:mainfrom
Sanjays2402:fix/run-time-range-validation
Open

fix: validate time_range_minutes from CLI args in run.py#207
Sanjays2402 wants to merge 1 commit into
fuzzylabs:mainfrom
Sanjays2402:fix/run-time-range-validation

Conversation

@Sanjays2402
Copy link
Copy Markdown

What

Fix inconsistent handling of time_range_minutes in sre_agent/run.py between the CLI-argument and environment-variable code paths. The CLI path now validates the value the same way the env path does.

Why

The documented invocation in DEVELOPMENT.md is:

uv run python run.py /aws/containerinsights/no-loafers-for-you/application cartservice

…optionally with a third positional argument for minutes. In _load_request_from_args_or_env (src/sre_agent/run.py:L26), the CLI branch calls int(sys.argv[3]) directly, while the env branch (L38-L46) already wraps the parse in try/except ValueError and rejects <= 0.

Two real issues follow:

  1. Unhandled crash on typo. A non-integer third argument raises ValueError: invalid literal for int() with base 10: 'abc' with a full traceback instead of the friendly "time_range_minutes must be an integer." message the env path prints.
  2. Silent acceptance of invalid values. python -m sre_agent.run foo bar 0 and ... -5 are accepted on the CLI path, even though the env path rejects both. The same-shape validation in src/sre_agent/cli/mode/remote/aws/ecs/steps.py:L402-L409 also rejects <= 0, so the CLI path is the outlier.

Reproduction (before this patch):

$ python -c "import sys; sys.argv=['run.py','logs','svc','abc']; \
    from sre_agent.run import _load_request_from_args_or_env; \
    _load_request_from_args_or_env()"
ValueError: invalid literal for int() with base 10: 'abc'

$ python -c "import sys; sys.argv=['run.py','logs','svc','-5']; \
    from sre_agent.run import _load_request_from_args_or_env; \
    print(_load_request_from_args_or_env())"
('logs', 'svc', -5)   # silently accepted, feeds a negative timedelta into CloudWatch

How

  • Extract the parse-and-validate logic into _parse_time_range_minutes(raw: str) -> int and call it from both the CLI-args branch and the env branch.
  • Error messages now use the generic label time_range_minutes so they read naturally for both sys.argv[3] and TIME_RANGE_MINUTES.
  • No change to valid-input behaviour: int(raw) with raw > 0 returns the same value as before.

Extra

No new dependencies.

Checklist

  • I have run application tests ensuring nothing has broken.
  • I have updated the documentation if required. (No user-visible docs change; error strings only.)
  • I have added tests which cover my changes.

Testing

  • Added tests/test_run_args.py covering: valid input, non-integer input, zero, negatives, and a round-trip through _load_request_from_args_or_env for both failure modes and the happy path.
  • Local run (uv run pytest tests/): 8 passed (1 pre-existing + 7 new).
  • ruff check --config=ruff.toml and ruff format --config=ruff.toml --check: clean.
  • uv run mypy --config-file=mypy.ini $(find src/sre_agent -name '*.py'): Success: no issues found in 94 source files.

Type of change

Bug fix.

The CLI-argument path in _load_request_from_args_or_env called int(sys.argv[3])
directly, so passing a non-integer third argument crashed with an unhandled
ValueError and non-positive values (0, -5) were silently accepted. The
environment-variable path already validated both cases. Factor the
parse-and-validate into a helper and use it for both paths so the documented
invocation (python -m sre_agent.run <log_group> <service> <minutes>) fails
cleanly on bad input.
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.

1 participant