openfix-cli 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ """Google Gemini via the official google-genai SDK (Gemini API or Vertex AI)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import uuid
6
+ from typing import Any
7
+
8
+ from .base import Conversation, ProviderError, ToolCall, ToolResult, ToolSpec, Turn, Usage, require_module
9
+
10
+
11
+ class GeminiProvider:
12
+ name = "gemini"
13
+ local = False
14
+
15
+ def __init__(self, model: str, effort: str | None = "high") -> None:
16
+ self.model = model
17
+ self.effort = effort
18
+ genai = require_module("google.genai", "gemini")
19
+ self._types = require_module("google.genai.types", "gemini")
20
+ try:
21
+ self._client = genai.Client() # GEMINI_API_KEY / GOOGLE_API_KEY, or Vertex via GOOGLE_GENAI_USE_VERTEXAI
22
+ except Exception as exc: # noqa: BLE001 - SDK raises plain ValueError without credentials
23
+ raise ProviderError(f"Gemini client could not start: {exc}") from exc
24
+
25
+ def start(self, system: str, tools: list[ToolSpec]) -> Conversation:
26
+ return _GeminiConversation(self, system, tools)
27
+
28
+
29
+ class _GeminiConversation:
30
+ def __init__(self, p: GeminiProvider, system: str, tools: list[ToolSpec]) -> None:
31
+ self.p = p
32
+ t = p._types
33
+ decls = [
34
+ t.FunctionDeclaration(name=s.name, description=s.description, parameters_json_schema=s.parameters)
35
+ for s in tools
36
+ ]
37
+ config = t.GenerateContentConfig(
38
+ system_instruction=system,
39
+ tools=[t.Tool(function_declarations=decls)] if decls else None,
40
+ automatic_function_calling=t.AutomaticFunctionCallingConfig(disable=True),
41
+ )
42
+ self.chat = p._client.chats.create(model=p.model, config=config)
43
+
44
+ def send(self, *, user: str | None = None, tool_results: list[ToolResult] | None = None) -> Turn:
45
+ t = self.p._types
46
+ parts: list[Any] = [
47
+ t.Part.from_function_response(
48
+ name=r.name, response={"error": r.content} if r.is_error else {"result": r.content}
49
+ )
50
+ for r in tool_results or []
51
+ ]
52
+ if user:
53
+ parts.append(t.Part.from_text(text=user))
54
+ try:
55
+ resp = self.chat.send_message(parts)
56
+ except Exception as exc: # noqa: BLE001 - google-genai error types vary by transport
57
+ raise ProviderError(f"Gemini API error: {exc}") from exc
58
+ calls = [
59
+ ToolCall(id=fc.id or f"call_{uuid.uuid4().hex[:8]}", name=fc.name, arguments=dict(fc.args or {}))
60
+ for fc in resp.function_calls or []
61
+ ]
62
+ meta = resp.usage_metadata
63
+ usage = Usage(
64
+ input_tokens=getattr(meta, "prompt_token_count", 0) or 0,
65
+ output_tokens=(getattr(meta, "candidates_token_count", 0) or 0)
66
+ + (getattr(meta, "thoughts_token_count", 0) or 0),
67
+ cache_read_tokens=getattr(meta, "cached_content_token_count", 0) or 0,
68
+ )
69
+ finish = ""
70
+ if resp.candidates:
71
+ finish = str(getattr(resp.candidates[0], "finish_reason", "") or "")
72
+ stop = (
73
+ "tool_use"
74
+ if calls
75
+ else ("max_tokens" if "MAX_TOKENS" in finish else "refusal" if "SAFETY" in finish else "end")
76
+ )
77
+ text = ""
78
+ try:
79
+ text = resp.text or ""
80
+ except (ValueError, AttributeError):
81
+ text = ""
82
+ return Turn(text=text, tool_calls=calls, stop=stop, usage=usage, model=self.p.model) # type: ignore[arg-type]
@@ -0,0 +1,134 @@
1
+ """Any OpenAI-compatible Chat Completions endpoint: OpenAI, xAI Grok, OpenRouter, Groq, DeepSeek,
2
+ Mistral, Together, Fireworks, Azure-style gateways, and local servers (Ollama, vLLM, LM Studio, llama.cpp)."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ import os
8
+ import secrets
9
+ from dataclasses import dataclass
10
+ from typing import Any
11
+
12
+ from .base import Conversation, ProviderError, ToolCall, ToolResult, ToolSpec, Turn, Usage, require_module
13
+
14
+
15
+ @dataclass(frozen=True)
16
+ class Endpoint:
17
+ base_url: str | None
18
+ key_env: str | None
19
+ local: bool = False
20
+ reasoning_effort: bool = False # accepts the `reasoning_effort` parameter
21
+
22
+
23
+ ENDPOINTS: dict[str, Endpoint] = {
24
+ "openai": Endpoint(None, "OPENAI_API_KEY", reasoning_effort=True),
25
+ "xai": Endpoint("https://api.x.ai/v1", "XAI_API_KEY"),
26
+ "openrouter": Endpoint("https://openrouter.ai/api/v1", "OPENROUTER_API_KEY"),
27
+ "groq": Endpoint("https://api.groq.com/openai/v1", "GROQ_API_KEY"),
28
+ "deepseek": Endpoint("https://api.deepseek.com/v1", "DEEPSEEK_API_KEY"),
29
+ "mistral": Endpoint("https://api.mistral.ai/v1", "MISTRAL_API_KEY"),
30
+ "together": Endpoint("https://api.together.xyz/v1", "TOGETHER_API_KEY"),
31
+ "fireworks": Endpoint("https://api.fireworks.ai/inference/v1", "FIREWORKS_API_KEY"),
32
+ "ollama": Endpoint("http://localhost:11434/v1", None, local=True),
33
+ "vllm": Endpoint("http://localhost:8000/v1", None, local=True),
34
+ "lmstudio": Endpoint("http://localhost:1234/v1", None, local=True),
35
+ # Generic: set OPENFIX_COMPAT_BASE_URL (and optionally OPENFIX_COMPAT_API_KEY).
36
+ "compat": Endpoint(None, "OPENFIX_COMPAT_API_KEY"),
37
+ }
38
+
39
+ _EFFORT_MAP = {"low": "low", "medium": "medium", "high": "high", "xhigh": "high", "max": "high"}
40
+
41
+
42
+ class OpenAICompatProvider:
43
+ def __init__(self, name: str, model: str, effort: str | None = "high") -> None:
44
+ if name not in ENDPOINTS:
45
+ raise ProviderError(f"Unknown OpenAI-compatible provider '{name}'")
46
+ self.name = name
47
+ self.model = model
48
+ self.endpoint = ENDPOINTS[name]
49
+ self.effort = effort
50
+ env_prefix = f"OPENFIX_{name.upper()}"
51
+ base_url = os.environ.get(f"{env_prefix}_BASE_URL") or self.endpoint.base_url
52
+ if name == "compat":
53
+ base_url = os.environ.get("OPENFIX_COMPAT_BASE_URL")
54
+ if not base_url:
55
+ raise ProviderError("Set OPENFIX_COMPAT_BASE_URL for the 'compat' provider")
56
+ self.local = self.endpoint.local or bool(
57
+ base_url and any(h in base_url for h in ("://localhost", "://127.0.0.1", "://[::1]"))
58
+ )
59
+ key = os.environ.get(self.endpoint.key_env) if self.endpoint.key_env else None
60
+ if not key and not self.local:
61
+ raise ProviderError(f"Set {self.endpoint.key_env} to use the '{name}' provider")
62
+ sdk = require_module("openai", "openai")
63
+ self._sdk = sdk
64
+ # Local servers ignore the key, but the SDK requires one: use a random throwaway value.
65
+ api_key = key or secrets.token_urlsafe(16)
66
+ self._client = sdk.OpenAI(api_key=api_key, base_url=base_url, max_retries=4, timeout=600)
67
+
68
+ def start(self, system: str, tools: list[ToolSpec]) -> Conversation:
69
+ return _CompatConversation(self, system, tools)
70
+
71
+
72
+ class _CompatConversation:
73
+ def __init__(self, p: OpenAICompatProvider, system: str, tools: list[ToolSpec]) -> None:
74
+ self.p = p
75
+ self.tools = [
76
+ {"type": "function", "function": {"name": t.name, "description": t.description, "parameters": t.parameters}}
77
+ for t in tools
78
+ ]
79
+ self.messages: list[dict[str, Any]] = [{"role": "system", "content": system}]
80
+
81
+ def send(self, *, user: str | None = None, tool_results: list[ToolResult] | None = None) -> Turn:
82
+ for r in tool_results or []:
83
+ content = ("ERROR: " if r.is_error else "") + r.content
84
+ self.messages.append({"role": "tool", "tool_call_id": r.call_id, "content": content})
85
+ if user:
86
+ self.messages.append({"role": "user", "content": user})
87
+ params: dict[str, Any] = {"model": self.p.model, "messages": self.messages}
88
+ if self.tools:
89
+ params["tools"] = self.tools
90
+ if self.p.endpoint.reasoning_effort and self.p.effort:
91
+ params["reasoning_effort"] = _EFFORT_MAP.get(self.p.effort, "medium")
92
+ sdk = self.p._sdk
93
+ try:
94
+ resp = self.p._client.chat.completions.create(**params)
95
+ except sdk.BadRequestError as exc:
96
+ if "reasoning_effort" in params and "reasoning" in str(exc).lower():
97
+ params.pop("reasoning_effort") # model does not support it; retry once without
98
+ resp = self.p._client.chat.completions.create(**params)
99
+ else:
100
+ raise ProviderError(f"{self.p.name} API error: {exc}") from exc
101
+ except sdk.APIStatusError as exc:
102
+ raise ProviderError(f"{self.p.name} API error {exc.status_code}: {exc}") from exc
103
+ except sdk.APIConnectionError as exc:
104
+ raise ProviderError(f"Could not reach {self.p.name}: {exc}") from exc
105
+ if not resp.choices:
106
+ raise ProviderError(f"{self.p.name} returned no choices")
107
+ choice = resp.choices[0]
108
+ msg = choice.message
109
+ assistant: dict[str, Any] = {"role": "assistant", "content": msg.content or ""}
110
+ calls: list[ToolCall] = []
111
+ if msg.tool_calls:
112
+ assistant["tool_calls"] = []
113
+ for tc in msg.tool_calls:
114
+ raw = tc.function.arguments or "{}"
115
+ try:
116
+ args = json.loads(raw)
117
+ if not isinstance(args, dict):
118
+ args = {"_invalid": raw}
119
+ except json.JSONDecodeError:
120
+ args = {"_invalid": raw}
121
+ calls.append(ToolCall(id=tc.id, name=tc.function.name, arguments=args))
122
+ assistant["tool_calls"].append(
123
+ {"id": tc.id, "type": "function", "function": {"name": tc.function.name, "arguments": raw}}
124
+ )
125
+ self.messages.append(assistant)
126
+ usage = Usage()
127
+ if resp.usage:
128
+ usage.input_tokens = resp.usage.prompt_tokens or 0
129
+ usage.output_tokens = resp.usage.completion_tokens or 0
130
+ details = getattr(resp.usage, "prompt_tokens_details", None)
131
+ usage.cache_read_tokens = getattr(details, "cached_tokens", 0) or 0
132
+ finish = choice.finish_reason or "stop"
133
+ stop = "tool_use" if calls else {"length": "max_tokens", "content_filter": "refusal"}.get(finish, "end")
134
+ return Turn(text=msg.content or "", tool_calls=calls, stop=stop, usage=usage, model=resp.model) # type: ignore[arg-type]
@@ -0,0 +1,75 @@
1
+ """A deterministic provider for tests and offline demos.
2
+
3
+ It replays a script of tool calls (a JSON/YAML list) instead of calling a model:
4
+
5
+ - tool: edit_file
6
+ args: {path: src/files.js, old_text: "...", new_text: "..."}
7
+ - tool: submit
8
+ args: {summary: "..."}
9
+
10
+ A script can also map rule ids (or finding ids) to step lists, so one run can replay a fix per finding:
11
+
12
+ javascript/CommandInjection: [ ... ]
13
+ path-traversal-path-join-var: [ ... ]
14
+
15
+ Each conversation uses the entry whose key appears in its task; a finding with no entry gets no steps.
16
+ """
17
+
18
+ from __future__ import annotations
19
+
20
+ import json
21
+ from pathlib import Path
22
+ from typing import Any
23
+
24
+ import yaml
25
+
26
+ from .base import Conversation, ProviderError, ToolCall, ToolResult, ToolSpec, Turn, Usage
27
+
28
+
29
+ class ScriptedProvider:
30
+ name = "scripted"
31
+ local = True
32
+
33
+ def __init__(self, model: str, steps: list[dict[str, Any]] | dict[str, list[dict[str, Any]]] | None = None) -> None:
34
+ self.model = model
35
+ if steps is None:
36
+ cwd = Path.cwd().resolve()
37
+ path = (cwd / model).resolve()
38
+ if cwd not in path.parents or path.suffix not in (".yaml", ".yml", ".json") or not path.is_file():
39
+ raise ProviderError(
40
+ "scripted provider needs a .yaml/.json script inside the current directory: "
41
+ "--model scripted:path/to/script.yaml"
42
+ )
43
+ steps = yaml.safe_load(path.read_text(encoding="utf-8")) or []
44
+ self.steps: list[dict[str, Any]] | dict[str, list[dict[str, Any]]] = steps
45
+ self.transcript: list[dict[str, Any]] = []
46
+
47
+ def start(self, system: str, tools: list[ToolSpec]) -> Conversation:
48
+ return _ScriptedConversation(self, system)
49
+
50
+
51
+ class _ScriptedConversation:
52
+ def __init__(self, p: ScriptedProvider, system: str) -> None:
53
+ self.p = p
54
+ self.i = 0
55
+ self.steps: list[dict[str, Any]] | None = p.steps if isinstance(p.steps, list) else None
56
+ p.transcript.append({"system": system})
57
+
58
+ def send(self, *, user: str | None = None, tool_results: list[ToolResult] | None = None) -> Turn:
59
+ self.p.transcript.append({"user": user, "tool_results": [r.__dict__ for r in tool_results or []]})
60
+ if self.steps is None:
61
+ # Pick this finding's entry: the longest key that appears in the task, so ids win over prefixes.
62
+ assert isinstance(self.p.steps, dict)
63
+ keys = sorted((k for k in self.p.steps if user and k in user), key=len, reverse=True)
64
+ self.steps = list(self.p.steps[keys[0]]) if keys else []
65
+ if self.i >= len(self.steps):
66
+ return Turn(text="(script finished)", tool_calls=[], stop="end", usage=Usage(10, 5))
67
+ step = self.steps[self.i]
68
+ self.i += 1
69
+ if "text" in step and "tool" not in step:
70
+ return Turn(text=str(step["text"]), tool_calls=[], stop="end", usage=Usage(10, 5))
71
+ call = ToolCall(id=f"call_{self.i}", name=step["tool"], arguments=dict(step.get("args") or {}))
72
+ return Turn(text=step.get("say", ""), tool_calls=[call], stop="tool_use", usage=Usage(100, 50))
73
+
74
+ def __repr__(self) -> str: # pragma: no cover
75
+ return json.dumps(self.p.steps)[:80]
openfix/receipt.py ADDED
@@ -0,0 +1,143 @@
1
+ """Fix receipts: a self-contained, verifiable record of what was changed, by which model, under which
2
+ policy, and what independent evidence says about it.
3
+
4
+ A receipt is wrapped in an in-toto v1 Statement whose subject is the exact patch, so it can be signed with
5
+ Sigstore (``cosign attest-blob``) and checked later with ``openfix receipt verify``.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import hashlib
11
+ import json
12
+ import shutil
13
+ import subprocess
14
+ from datetime import datetime, timezone
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ from . import __version__
19
+ from .gitutil import GitError, git
20
+
21
+ RECEIPT_TYPE = "https://github.com/markusweldon/openfix/receipt/v1"
22
+ STATEMENT_TYPE = "https://in-toto.io/Statement/v1"
23
+
24
+
25
+ def sha256(data: str | bytes) -> str:
26
+ return hashlib.sha256(data.encode() if isinstance(data, str) else data).hexdigest()
27
+
28
+
29
+ def now() -> str:
30
+ return datetime.now(timezone.utc).replace(microsecond=0).isoformat()
31
+
32
+
33
+ def build_statement(receipt: dict[str, Any], patch: str) -> dict[str, Any]:
34
+ return {
35
+ "_type": STATEMENT_TYPE,
36
+ "subject": [{"name": "patch.diff", "digest": {"sha256": sha256(patch)}}],
37
+ "predicateType": RECEIPT_TYPE,
38
+ "predicate": receipt,
39
+ }
40
+
41
+
42
+ def write_receipt(out_dir: Path, receipt: dict[str, Any], patch: str, sign: bool = False) -> dict[str, Path]:
43
+ out_dir.mkdir(parents=True, exist_ok=True)
44
+ paths = {"patch": out_dir / "patch.diff", "receipt": out_dir / "receipt.json"}
45
+ paths["patch"].write_text(patch, encoding="utf-8")
46
+ statement = build_statement(receipt, patch)
47
+ paths["receipt"].write_text(json.dumps(statement, indent=2, sort_keys=False) + "\n", encoding="utf-8")
48
+ if sign:
49
+ bundle = out_dir / "receipt.sigstore.json"
50
+ if shutil.which("cosign"):
51
+ res = subprocess.run(
52
+ ["cosign", "sign-blob", "--yes", "--bundle", str(bundle), str(paths["receipt"])],
53
+ capture_output=True,
54
+ text=True,
55
+ timeout=300,
56
+ )
57
+ if res.returncode == 0:
58
+ paths["signature"] = bundle
59
+ else:
60
+ (out_dir / "signing-error.txt").write_text(res.stderr[-4000:], encoding="utf-8")
61
+ else:
62
+ (out_dir / "signing-error.txt").write_text("cosign is not installed; receipt left unsigned\n")
63
+ return paths
64
+
65
+
66
+ def verify_receipt(path: Path, repo: Path | None = None) -> list[tuple[str, bool, str]]:
67
+ """Re-check a receipt offline. Returns (check, ok, detail) rows."""
68
+ path = Path(path)
69
+ receipt_file = path / "receipt.json" if path.is_dir() else path
70
+ patch_file = receipt_file.parent / "patch.diff"
71
+ rows: list[tuple[str, bool, str]] = []
72
+ statement = json.loads(receipt_file.read_text(encoding="utf-8"))
73
+ rows.append(
74
+ (
75
+ "statement type",
76
+ statement.get("_type") == STATEMENT_TYPE and statement.get("predicateType") == RECEIPT_TYPE,
77
+ statement.get("predicateType", "?"),
78
+ )
79
+ )
80
+ pred = statement.get("predicate") or {}
81
+ if not patch_file.is_file():
82
+ rows.append(("patch present", False, f"{patch_file} not found"))
83
+ return rows
84
+ patch = patch_file.read_text(encoding="utf-8")
85
+ want = (statement.get("subject") or [{}])[0].get("digest", {}).get("sha256")
86
+ rows.append(("patch digest", sha256(patch) == want, f"sha256 {sha256(patch)[:16]}..."))
87
+ rows.append(("predicate digest", pred.get("patch", {}).get("sha256") == want, "predicate.patch.sha256"))
88
+ if repo is not None:
89
+ base = pred.get("repository", {}).get("base_commit")
90
+ try:
91
+ git(repo, "cat-file", "-e", f"{base}^{{commit}}")
92
+ rows.append(("base commit exists", True, base or "?"))
93
+ except GitError:
94
+ rows.append(("base commit exists", False, f"{base} not found in {repo}"))
95
+ return rows
96
+ res = subprocess.run(
97
+ ["git", "-C", str(repo), "apply", "--check", "-"], input=patch, capture_output=True, text=True
98
+ )
99
+ rows.append(("patch applies to repo", res.returncode == 0, (res.stderr or "ok").strip()[:200]))
100
+ sig = receipt_file.parent / "receipt.sigstore.json"
101
+ if sig.exists():
102
+ rows.append(
103
+ (
104
+ "signature bundle",
105
+ True,
106
+ "present - verify with: cosign verify-blob --bundle "
107
+ f"{sig} --certificate-identity <id> --certificate-oidc-issuer <issuer> {receipt_file}",
108
+ )
109
+ )
110
+ return rows
111
+
112
+
113
+ def make_receipt(
114
+ *,
115
+ finding: dict[str, Any],
116
+ decision: dict[str, Any],
117
+ repo: dict[str, Any],
118
+ patch: str,
119
+ files: list[tuple[str, int, int]],
120
+ author: dict[str, Any] | None,
121
+ checks: list[dict[str, Any]],
122
+ status: str,
123
+ summary: str,
124
+ fixer: str,
125
+ policy_digest: str | None,
126
+ ) -> dict[str, Any]:
127
+ return {
128
+ "openfix_version": __version__,
129
+ "created_at": now(),
130
+ "status": status, # verified | unverified | failed
131
+ "finding": finding,
132
+ "policy": {"decision": decision, "policy_sha256": policy_digest},
133
+ "repository": repo,
134
+ "patch": {
135
+ "sha256": sha256(patch),
136
+ "files": [{"path": p, "added": a, "removed": r} for p, a, r in files],
137
+ "lines_changed": sum(a + r for _, a, r in files),
138
+ },
139
+ "fixer": fixer, # "agent" or "deterministic"
140
+ "author": author,
141
+ "summary": summary,
142
+ "checks": checks,
143
+ }
openfix/redact.py ADDED
@@ -0,0 +1,47 @@
1
+ """Best-effort credential redaction for anything that leaves the process (prompts, receipts, logs)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from typing import Any
7
+
8
+ SECRET_KEYS = re.compile(
9
+ r"^(password|passwd|secret|token|access_token|refresh_token|api_key|apikey|authorization|"
10
+ r"private_key|client_secret|match|secret_value)$",
11
+ re.IGNORECASE,
12
+ )
13
+
14
+ PATTERNS: list[tuple[re.Pattern[str], str]] = [
15
+ (
16
+ re.compile(r"-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----"),
17
+ "[REDACTED PRIVATE KEY]",
18
+ ),
19
+ (re.compile(r"\b(?:sk-(?:proj-|ant-)?[A-Za-z0-9_-]{20,})\b"), "[REDACTED API KEY]"),
20
+ (re.compile(r"\bgh[pousr]_[A-Za-z0-9]{30,}\b"), "[REDACTED GITHUB TOKEN]"),
21
+ (re.compile(r"\bgithub_pat_[A-Za-z0-9_]{40,}\b"), "[REDACTED GITHUB TOKEN]"),
22
+ (re.compile(r"\b(?:AKIA|ASIA)[A-Z0-9]{16}\b"), "[REDACTED AWS KEY]"),
23
+ (re.compile(r"\bxox[abprs]-[A-Za-z0-9-]{10,}\b"), "[REDACTED SLACK TOKEN]"),
24
+ (re.compile(r"\bAIza[0-9A-Za-z_-]{35}\b"), "[REDACTED GOOGLE KEY]"),
25
+ (re.compile(r"\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b"), "[REDACTED JWT]"),
26
+ ]
27
+
28
+
29
+ def redact_text(text: str) -> str:
30
+ for pattern, replacement in PATTERNS:
31
+ text = pattern.sub(replacement, text)
32
+ return text
33
+
34
+
35
+ def redact(value: Any, _depth: int = 0) -> Any:
36
+ if _depth > 50:
37
+ return "[TRUNCATED]"
38
+ if isinstance(value, dict):
39
+ return {
40
+ k: ("[REDACTED]" if isinstance(k, str) and SECRET_KEYS.match(k) else redact(v, _depth + 1))
41
+ for k, v in value.items()
42
+ }
43
+ if isinstance(value, list):
44
+ return [redact(v, _depth + 1) for v in value]
45
+ if isinstance(value, str):
46
+ return redact_text(value)
47
+ return value
openfix/report.py ADDED
@@ -0,0 +1,92 @@
1
+ """Human-readable output: pull request bodies and run summaries."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any
6
+
7
+ ICON = {"passed": "✅", "failed": "❌", "not_run": "⚪", "inconclusive": "⚠️"}
8
+ STATUS_TEXT = {
9
+ "verified": "✅ Verified: every required check passed",
10
+ "unverified": "⚠️ Unverified: some required checks could not prove the fix",
11
+ "failed": "❌ Failed verification",
12
+ }
13
+
14
+
15
+ def pr_body(receipt: dict[str, Any], receipt_digest: str) -> str:
16
+ f = receipt["finding"]
17
+ loc = f.get("location") or {}
18
+ where = (
19
+ f"`{loc.get('path')}:{loc.get('start_line')}`"
20
+ if loc.get("path")
21
+ else (f"`{(f.get('package') or {}).get('name')}`")
22
+ )
23
+ author = receipt.get("author") or {}
24
+ rows = "\n".join(
25
+ f"| {ICON.get(c['status'], '')} {c['name']} | {c['status']} | {c['detail']} |" for c in receipt["checks"]
26
+ )
27
+ usage = author.get("usage") or {}
28
+ model_line = (
29
+ (
30
+ f"`{author.get('model')}` · {author.get('turns')} turns · "
31
+ f"{usage.get('input_tokens', 0) + usage.get('output_tokens', 0):,} tokens"
32
+ + (f" · ${author['cost_usd']:.2f}" if author.get("cost_usd") is not None else "")
33
+ )
34
+ if author
35
+ else "Deterministic upgrade (no model)"
36
+ )
37
+ return f"""### OpenFix: {f["title"]}
38
+
39
+ **{STATUS_TEXT.get(receipt["status"], receipt["status"])}**
40
+
41
+ | | |
42
+ |---|---|
43
+ | Finding | `{f["rule_id"]}` from **{f["tool"]}** ({f["severity"]}) at {where} |
44
+ | CWE / advisories | {", ".join(f.get("cwe") or []) or "-"} / {", ".join(f.get("advisories") or []) or "-"} |
45
+ | Fixed by | {model_line} |
46
+ | Policy rule | {receipt["policy"]["decision"]["rule"]} |
47
+ | Change | {len(receipt["patch"]["files"])} file(s), {receipt["patch"]["lines_changed"]} line(s) |
48
+
49
+ #### What changed
50
+ {receipt.get("summary") or "-"}
51
+
52
+ #### Evidence
53
+ | Check | Result | Detail |
54
+ |---|---|---|
55
+ {rows}
56
+
57
+ <details><summary>Receipt</summary>
58
+
59
+ Receipt `sha256:{receipt_digest}` · patch `sha256:{receipt["patch"]["sha256"]}`
60
+ · base `{receipt["repository"]["base_commit"]}`.
61
+ Verify locally with `openfix receipt verify <receipt-dir> --repo .`
62
+
63
+ </details>
64
+
65
+ > Generated by [OpenFix](https://github.com/markusweldon/openfix). Review the diff before merging; OpenFix never merges.
66
+ """
67
+
68
+
69
+ def summary_markdown(outcomes: list[dict[str, Any]], meta: dict[str, Any]) -> str:
70
+ lines = [
71
+ f"# OpenFix run {meta['run_id']}",
72
+ "",
73
+ f"Repository `{meta['repo']}` at `{meta['base_commit'][:12]}` · author `{meta['author']}`"
74
+ + (f" · reviewer `{meta['reviewer']}`" if meta.get("reviewer") else ""),
75
+ "",
76
+ "| Finding | Action | Result | Checks | Cost | PR |",
77
+ "|---|---|---|---|---|---|",
78
+ ]
79
+ for o in outcomes:
80
+ checks = " ".join(f"{ICON.get(c['status'], '')}{c['name']}" for c in o.get("checks", []))
81
+ cost = f"${o['cost_usd']:.2f}" if o.get("cost_usd") is not None else "-"
82
+ lines.append(
83
+ f"| {o['label']} | {o['action']} | **{o['status']}** {o.get('detail') or ''} | {checks} | "
84
+ f"{cost} | {o.get('pr_url') or ''} |"
85
+ )
86
+ totals = meta.get("totals", {})
87
+ lines += [
88
+ "",
89
+ f"Total: {totals.get('tokens', 0):,} tokens"
90
+ + (f", ${totals['cost_usd']:.2f}" if totals.get("cost_usd") is not None else ""),
91
+ ]
92
+ return "\n".join(lines) + "\n"