|
| 1 | +"""One-time setup: create agent, environment, skill, and vault on Anthropic. |
| 2 | +
|
| 3 | +Usage: |
| 4 | + SRE_GITHUB_PAT=ghp_... uv run python -m sre_agent._setup |
| 5 | +
|
| 6 | +Prints the resource IDs to store as GitHub Actions secrets. |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import os |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +import anthropic |
| 16 | + |
| 17 | +SYSTEM_PROMPT = """\ |
| 18 | +You are an SRE incident response agent for the Aignostics Python SDK |
| 19 | +(github.com/aignostics/python-sdk). |
| 20 | +
|
| 21 | +You have been triggered by a BetterStack incident alert. The alert |
| 22 | +includes the incident name, cause, and -- critically -- the GitHub |
| 23 | +Actions run URL from the failed workflow. |
| 24 | +
|
| 25 | +Your job: |
| 26 | +
|
| 27 | +1. Read the incident details: name, cause, and the failed run URL. |
| 28 | +2. Use the GitHub MCP to read the failed workflow run's logs. This is |
| 29 | + your primary source of diagnostic information. |
| 30 | +3. Investigate the root cause: |
| 31 | + - Read the workflow run logs to identify the specific failure. |
| 32 | + - Check recent commits on main (git log in the mounted repo). |
| 33 | + - Read the relevant GitHub Actions workflow YAML files. |
| 34 | + - Use web_search to look up error messages, CVE details, or docs. |
| 35 | +4. Determine if the issue is fixable with a code change. |
| 36 | +5. If fixable: use the GitHub MCP to create a branch, commit the fix, |
| 37 | + and open a draft PR with your analysis in the body. |
| 38 | +6. If not fixable or uncertain: use the GitHub MCP to create an issue |
| 39 | + with your triage findings and recommended next steps. |
| 40 | +
|
| 41 | +Constraints: |
| 42 | +- Always create DRAFT PRs, never regular PRs. Humans must review and merge. |
| 43 | +- Always cite evidence for your root cause analysis. |
| 44 | +- For dependency CVEs: bump the minimum safe version, run the audit check. |
| 45 | +- For test failures: check if the test is flaky (search for prior failures) |
| 46 | + before proposing a code fix. |
| 47 | +- Never modify credentials, secrets, or authentication code. |
| 48 | +- Add the label "sre-agent" to any PR or issue you create. |
| 49 | +- Add the label "skip:test:long_running" to any PR you create. |
| 50 | +
|
| 51 | +The repo uses: uv (package manager), pytest (testing), ruff (linting), |
| 52 | +mypy + pyright (type checking). CI runs on GitHub Actions. |
| 53 | +""" |
| 54 | + |
| 55 | +SKILL_DIR = Path(__file__).resolve().parent.parent.parent / "skills" / "sre-runbook" |
| 56 | + |
| 57 | + |
| 58 | +def main() -> None: |
| 59 | + github_pat = os.environ.get("SRE_GITHUB_PAT", "") |
| 60 | + if not github_pat: |
| 61 | + print("Error: SRE_GITHUB_PAT environment variable is required.", file=sys.stderr) |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + client = anthropic.Anthropic() |
| 65 | + |
| 66 | + # 1. Upload runbook skill |
| 67 | + skill_md = (SKILL_DIR / "SKILL.md").read_bytes() |
| 68 | + skill = client.beta.skills.create( |
| 69 | + display_title="sre-runbook", |
| 70 | + files=[("sre-runbook/SKILL.md", skill_md, "text/markdown")], |
| 71 | + ) |
| 72 | + print(f"Skill created: {skill.id} (version {skill.latest_version})") |
| 73 | + |
| 74 | + # 2. Create environment |
| 75 | + environment = client.beta.environments.create( |
| 76 | + name="sre-incident-response", |
| 77 | + config={"type": "cloud", "networking": {"type": "limited"}}, |
| 78 | + ) |
| 79 | + print(f"Environment created: {environment.id}") |
| 80 | + |
| 81 | + # 3. Create vault with GitHub PAT |
| 82 | + vault = client.beta.vaults.create(name="sre-github") |
| 83 | + client.beta.vaults.credentials.create( |
| 84 | + vault_id=vault.id, |
| 85 | + name="github", |
| 86 | + token=github_pat, |
| 87 | + ) |
| 88 | + print(f"Vault created: {vault.id}") |
| 89 | + |
| 90 | + # 4. Create agent |
| 91 | + agent = client.beta.agents.create( |
| 92 | + name="Aignostics SRE Incident Responder", |
| 93 | + model="claude-sonnet-4-6", |
| 94 | + system=SYSTEM_PROMPT, |
| 95 | + mcp_servers=[ |
| 96 | + { |
| 97 | + "type": "url", |
| 98 | + "name": "github", |
| 99 | + "url": "https://api.githubcopilot.com/mcp/", |
| 100 | + }, |
| 101 | + ], |
| 102 | + tools=[ |
| 103 | + {"type": "agent_toolset_20260401"}, |
| 104 | + {"type": "mcp_toolset", "mcp_server_name": "github"}, |
| 105 | + ], |
| 106 | + skills=[ |
| 107 | + {"type": "custom", "skill_id": skill.id, "version": skill.latest_version}, |
| 108 | + ], |
| 109 | + ) |
| 110 | + print(f"Agent created: {agent.id} (version {agent.version})") |
| 111 | + |
| 112 | + # Print secrets to configure in GitHub Actions |
| 113 | + print("\n--- Store these as GitHub Actions secrets ---") |
| 114 | + print(f"SRE_AGENT_ID={agent.id}") |
| 115 | + print(f"SRE_ENVIRONMENT_ID={environment.id}") |
| 116 | + print(f"SRE_VAULT_ID={vault.id}") |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments