Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions artifacts/agent_artifact_bundle_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"artifact_id": "agent_artifact_bundle_example_v1",
"bundle": {
"branch": "feat/agent-artifact-bundle-example",
"changed_files": [
"artifacts/agent_artifact_bundle_example.json",
"scripts/generate_agent_artifact_bundle_example.py",
"tests/test_agent_artifact_bundle.py"
],
"mcp_context_output_ref": "artifacts/mcp_context_layer_example.json",
"ok": true,
"result": "PASS",
"safe_pr_gate": {
"allow_dirty": false,
"allowed_prefixes": [],
"branch": "feat/agent-artifact-bundle-example",
"changed_paths": [
"artifacts/agent_artifact_bundle_example.json",
"scripts/generate_agent_artifact_bundle_example.py",
"tests/test_agent_artifact_bundle.py"
],
"ok": true,
"problems": [],
"result": "PASS",
"status_short": []
},
"validation_evidence": [
{
"command": "python -m compileall -q scripts/agent_artifact_bundle.py scripts/generate_agent_artifact_bundle_example.py",
"result": "pass"
},
{
"command": "pytest tests/test_agent_artifact_bundle.py -q",
"result": "pass"
}
]
},
"evaluation_mode": "deterministic",
"external_apis": "none",
"generated_by": "AgentArtifactBundleExampleGenerator",
"llm_judges": "none",
"schema_version": "agent_artifact_bundle_example.v1",
"version": "1.0"
}
71 changes: 71 additions & 0 deletions scripts/generate_agent_artifact_bundle_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#!/usr/bin/env python3
"""Generate a deterministic agent artifact bundle example."""

from __future__ import annotations

import json
import sys
from pathlib import Path
from typing import Any

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))

from scripts.agent_artifact_bundle import build_agent_artifact_bundle
from scripts.safe_pr_gate import GateState

ARTIFACT_ID = "agent_artifact_bundle_example_v1"
OUTPUT_PATH = REPO_ROOT / "artifacts" / "agent_artifact_bundle_example.json"

EXAMPLE_STATE = GateState(
branch="feat/agent-artifact-bundle-example",
status_short=(),
changed_paths=(
"artifacts/agent_artifact_bundle_example.json",
"scripts/generate_agent_artifact_bundle_example.py",
"tests/test_agent_artifact_bundle.py",
),
)
VALIDATION_COMMANDS = [
"python -m compileall -q scripts/agent_artifact_bundle.py scripts/generate_agent_artifact_bundle_example.py",
"pytest tests/test_agent_artifact_bundle.py -q",
]
VALIDATION_RESULTS = ["pass", "pass"]
MCP_CONTEXT_OUTPUT_REF = "artifacts/mcp_context_layer_example.json"


def build_agent_artifact_bundle_example() -> dict[str, Any]:
return {
"artifact_id": ARTIFACT_ID,
"bundle": build_agent_artifact_bundle(
EXAMPLE_STATE,
allow_main=False,
validation_commands=VALIDATION_COMMANDS,
validation_results=VALIDATION_RESULTS,
mcp_context_output_ref=MCP_CONTEXT_OUTPUT_REF,
),
"evaluation_mode": "deterministic",
"external_apis": "none",
"generated_by": "AgentArtifactBundleExampleGenerator",
"llm_judges": "none",
"schema_version": "agent_artifact_bundle_example.v1",
"version": "1.0",
}


def generate_agent_artifact_bundle_example(output_path: Path = OUTPUT_PATH) -> Path:
artifact = build_agent_artifact_bundle_example()
output_path.parent.mkdir(parents=True, exist_ok=True)
output_path.write_text(json.dumps(artifact, indent=2, sort_keys=True) + "\n", encoding="utf-8")
return output_path


def main() -> int:
output_path = generate_agent_artifact_bundle_example()
print(output_path.relative_to(REPO_ROOT).as_posix())
return 0


if __name__ == "__main__":
raise SystemExit(main())
48 changes: 48 additions & 0 deletions tests/test_agent_artifact_bundle.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from __future__ import annotations

import json
from pathlib import Path

import pytest

import scripts.agent_artifact_bundle as agent_artifact_bundle
from scripts.generate_agent_artifact_bundle_example import (
ARTIFACT_ID,
generate_agent_artifact_bundle_example,
)
from scripts.safe_pr_gate import GateState, evaluate_gate

ARTIFACT_PATH = Path("artifacts/agent_artifact_bundle_example.json")


def test_build_agent_artifact_bundle_is_deterministic_and_includes_optional_metadata() -> None:
state = GateState(
Expand Down Expand Up @@ -160,3 +167,44 @@ def test_main_reports_main_branch_as_deterministic_error_json(
"ok": False,
"result": "ERROR",
}


def test_agent_artifact_bundle_example_matches_generator_output(tmp_path: Path) -> None:
output_path = tmp_path / "agent_artifact_bundle_example.json"
generate_agent_artifact_bundle_example(output_path)

assert output_path.read_text(encoding="utf-8") == ARTIFACT_PATH.read_text(encoding="utf-8")


def test_agent_artifact_bundle_example_has_stable_schema_and_content() -> None:
artifact = json.loads(ARTIFACT_PATH.read_text(encoding="utf-8"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Adhere to the general rule for loading JSON files: handle FileNotFoundError and json.JSONDecodeError by raising a RuntimeError that includes the repo-relative path. Additionally, validate that the decoded payload is a dictionary before proceeding.

    try:
        artifact = json.loads(ARTIFACT_PATH.read_text(encoding="utf-8"))
    except (FileNotFoundError, json.JSONDecodeError) as exc:
        raise RuntimeError(f"Failed to load artifact from {ARTIFACT_PATH.as_posix()}: {exc}") from exc
    if not isinstance(artifact, dict):
        raise RuntimeError(f"Expected artifact in {ARTIFACT_PATH.as_posix()} to be a dictionary, got {type(artifact).__name__}")
References
  1. When loading JSON files, handle FileNotFoundError and JSONDecodeError by raising a RuntimeError that includes a repo-relative path. Additionally, validate that the decoded payload is of the expected type (e.g., a dictionary) before proceeding.


assert set(artifact) == {
"artifact_id",
"bundle",
"evaluation_mode",
"external_apis",
"generated_by",
"llm_judges",
"schema_version",
"version",
}
assert artifact["artifact_id"] == ARTIFACT_ID
assert artifact["schema_version"] == "agent_artifact_bundle_example.v1"
assert artifact["version"] == "1.0"
assert artifact["evaluation_mode"] == "deterministic"
assert artifact["external_apis"] == "none"
assert artifact["llm_judges"] == "none"

bundle = artifact["bundle"]
assert bundle["branch"] == "feat/agent-artifact-bundle-example"
assert bundle["ok"] is True
assert bundle["result"] == "PASS"
assert bundle["changed_files"] == [
"artifacts/agent_artifact_bundle_example.json",
"scripts/generate_agent_artifact_bundle_example.py",
"tests/test_agent_artifact_bundle.py",
]
assert bundle["mcp_context_output_ref"] == "artifacts/mcp_context_layer_example.json"
assert bundle["safe_pr_gate"]["ok"] is True
assert bundle["safe_pr_gate"]["result"] == "PASS"
Loading