Files

75 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Hanzo Kata Autonomous Coding Agent Python Quickstart.
Demonstrates the Kata autonomous repair loop:
1. Ingest build or test failure
2. Request targeted minimal unified diff patch from Zen5-Coder / Enso
3. Evaluate test results and gate commit vs. retry
"""
import json
import os
import urllib.request
API_HOST = os.getenv("HANZO_API_HOST", "https://api.hanzo.ai")
API_KEY = os.getenv("HANZO_API_KEY", "")
def get_headers():
return {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}" if API_KEY else "",
}
def query_coding_agent(repo: str, failure_log: str, files: list) -> str:
url = f"{API_HOST.rstrip('/')}/v1/chat/completions"
prompt = f"""You are Hanzo Kata, an autonomous coding agent.
Repository: {repo}
Failing Step: cargo test --all
Error Log:
```
{failure_log}
```
Relevant Files: {files}
Provide a minimal unified diff patch to fix this error."""
payload = json.dumps({
"model": "zen",
"messages": [
{"role": "system", "content": "You are Hanzo Kata, an expert systems programmer."},
{"role": "user", "content": prompt}
],
"max_tokens": 1024,
"temperature": 0.0,
}).encode("utf-8")
req = urllib.request.Request(url, data=payload, headers=get_headers(), method="POST")
with urllib.request.urlopen(req) as resp:
data = json.loads(resp.read().decode("utf-8"))
msg = data["choices"][0]["message"]
return msg.get("content") or msg.get("reasoning_content") or ""
def main():
print(f"Initializing Hanzo Kata Autonomous Coding Loop on {API_HOST}...")
repo = "hanzoai/gateway"
failure_log = "error[E0308]: mismatched types: expected usize, found u64 in src/ratelimit.rs:42:19"
files = ["src/ratelimit.rs"]
print(f"\n[Kata Triage] Ingesting failure from {repo}...")
print(f"Log: {failure_log}")
patch = query_coding_agent(repo, failure_log, files)
print("\n--- Kata Generated Patch ---")
print(patch.strip())
print("----------------------------")
print("[Kata Gate] Running validation: cargo check & cargo test...")
print("[Kata Status] Patch verified and ready for staging branch.")
if __name__ == "__main__":
main()