-
Notifications
You must be signed in to change notification settings - Fork 1
feat(cli): kai command + rename distribution to kai-security #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| """The ``kai`` command-line entry point. | ||
|
|
||
| A thin dispatcher over the existing modules, giving the friendly verbs the | ||
| docs promise: | ||
|
|
||
| kai audit <repo> analyze a repository (setup → exploit pipeline) | ||
| kai view <run_dir> open a finished run as interactive HTML | ||
| kai report <run_dir> render a run's findings (Markdown, or --format html) | ||
|
|
||
| ``kai pipeline`` / ``kai agent`` remain available as direct aliases into the | ||
| full :mod:`kai.main` interface. The distribution is published as | ||
| ``kai-security``; the command and the import package stay ``kai``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
|
|
||
| _USAGE = """\ | ||
| kai-security — automated vulnerability discovery, verification, and patching | ||
|
|
||
| usage: kai-security <command> [options] | ||
|
|
||
| commands: | ||
| audit <repo> Analyze a repository for vulnerabilities (setup → exploit) | ||
| view <run_dir> Open a finished run as interactive HTML (findings + trace) | ||
| report <run_dir> Render a run's findings as Markdown (default) or HTML | ||
|
|
||
| pipeline Full pipeline interface (kai audit is the friendly alias) | ||
| agent Run a single agent | ||
|
|
||
| Run `kai-security <command> -h` for command-specific options. | ||
| """ | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| """Dispatch a ``kai`` subcommand. Returns a process exit code.""" | ||
|
|
||
| argv = list(sys.argv[1:] if argv is None else argv) | ||
| if not argv or argv[0] in ("-h", "--help", "help"): | ||
| sys.stdout.write(_USAGE) | ||
| return 0 | ||
|
|
||
| command, rest = argv[0], argv[1:] | ||
|
|
||
| if command in ("audit", "pipeline"): | ||
| from kai.main import main as kai_main | ||
|
|
||
| kai_main(["pipeline", *rest]) | ||
| return 0 | ||
| if command == "agent": | ||
| from kai.main import main as kai_main | ||
|
|
||
| kai_main(["agent", *rest]) | ||
| return 0 | ||
| if command == "view": | ||
| from kai.viewer.__main__ import main as view_main | ||
|
|
||
| return view_main(rest) | ||
| if command == "report": | ||
| from kai.report import main as report_main | ||
|
|
||
| return report_main(rest) | ||
|
|
||
| sys.stderr.write(f"kai-security: unknown command {command!r}\n\n") | ||
| sys.stdout.write(_USAGE) | ||
| return 2 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """Tests for the unified ``kai`` CLI dispatcher.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from kai import cli | ||
|
|
||
|
|
||
| def _write_run(dir_path: Path) -> None: | ||
| exploits = [ | ||
| { | ||
| "exploit_id": "e1", "status": "verified", "confirmed": True, | ||
| "hypothesis": "Reentrancy in withdraw drains the vault.", | ||
| "file": "Vault.sol", "function": "withdraw", "category": "active_exploit", | ||
| "severity": "critical", "cvss_score": 9.1, | ||
| } | ||
| ] | ||
| (dir_path / "exploits.json").write_text(json.dumps(exploits), encoding="utf-8") | ||
|
|
||
|
|
||
| def test_help_and_no_args_print_usage(capsys: pytest.CaptureFixture[str]) -> None: | ||
| assert cli.main([]) == 0 | ||
| assert "usage: kai <command>" in capsys.readouterr().out | ||
| assert cli.main(["--help"]) == 0 | ||
| assert "audit" in capsys.readouterr().out | ||
|
|
||
|
|
||
| def test_unknown_command_returns_2(capsys: pytest.CaptureFixture[str]) -> None: | ||
| assert cli.main(["bogus"]) == 2 | ||
| err = capsys.readouterr().err | ||
| assert "unknown command 'bogus'" in err | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "command,expected", | ||
| [ | ||
| (["audit", "/repo", "--verbose"], ["pipeline", "/repo", "--verbose"]), | ||
| (["pipeline", "--recipe", "r.json"], ["pipeline", "--recipe", "r.json"]), | ||
| (["agent", "setup", "--input", "{}"], ["agent", "setup", "--input", "{}"]), | ||
| ], | ||
| ) | ||
| def test_audit_pipeline_agent_delegate_to_kai_main( | ||
| command: list[str], expected: list[str], monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| captured: list[list[str]] = [] | ||
| monkeypatch.setattr("kai.main.main", lambda argv: captured.append(argv)) | ||
| assert cli.main(command) == 0 | ||
| assert captured == [expected] | ||
|
|
||
|
|
||
| def test_view_delegates_and_writes_html(tmp_path: Path) -> None: | ||
| _write_run(tmp_path) | ||
| out = tmp_path / "v.html" | ||
| assert cli.main(["view", str(tmp_path), "-o", str(out)]) == 0 | ||
| assert out.exists() and out.read_text(encoding="utf-8").startswith("<!DOCTYPE html>") | ||
|
|
||
|
|
||
| def test_report_delegates(tmp_path: Path, capsys: pytest.CaptureFixture[str]) -> None: | ||
| _write_run(tmp_path) | ||
| assert cli.main(["report", str(tmp_path)]) == 0 | ||
| assert "Security findings" in capsys.readouterr().out | ||
|
|
||
| out = tmp_path / "r.html" | ||
| assert cli.main(["report", str(tmp_path), "--format", "html", "-o", str(out)]) == 0 | ||
| assert out.exists() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.