The reviewer runs an opencode agent with `bash: "*": allow` over a checkout of the PR author's branch, and the pod holds a Gitea Write credential. Those two facts had no wall between them. Security - _build_env now allow-lists the subprocess environment instead of inheriting it, so PRAGENT_BOT_TOKEN and WEBHOOK_SECRET never reach the agent. This was the live hole: a PR body or an AGENTS.md could ask the agent to `curl` the token out, and it had both the value and the tool. - sanitize_workdir deletes author-controlled agent-instruction files from the checkout before opencode starts (AGENTS.md at any depth, CLAUDE.md, .cursorrules, a repo opencode.json/.opencode, copilot-instructions.md). opencode loads nested AGENTS.md as instructions, so a PR could otherwise ship its own system prompt. They are still reviewed, as data. - The brief fences PR title/body and diff in --- UNTRUSTED --- markers under a trust-boundary preamble; the pragent agent, the three lens subagents and the review-methodology skill now treat injection attempts as a critical finding to report rather than an instruction to obey. - .pr-review.json is read from the PR's base branch, not the head sha. Its `instructions` field is spliced into the reviewer's prompt, so head-ref reading let any author rewrite the reviewer's rules. Fields are length-capped. - Untar rejects escaping symlinks, parent traversal, and writes through a planted symlink (tar-slip). - The image runs as uid 10001 instead of root. Robustness - Bounded review concurrency (PRAGENT_MAX_CONCURRENT_REVIEWS, default 2). Each review forks an opencode process; a thread per delivery was a fork bomb on a burst of labels or Gitea retries. - An in-flight (repo, index, sha) claim closes the check-then-act race in the sha-marker dedupe, where two deliveries a second apart both read "not yet reviewed" and both posted. - Request bodies are capped before being read into memory. Correctness - parse_diff_anchors counts a whitespace-stripped blank context line. Skipping it desynced the new-line counter for the rest of the hunk and silently misplaced every later inline comment in that file. - post_inline_review's body-only fallback folds the anchored findings into the body. It previously posted a summary saying "N inline comment(s) below" with no comments and no findings — losing them all on the one path that matters. - fetch_pr_diff's files-endpoint fallback emits real a// b/ prefixes (so changed_files and the anchor parser work on it) and reports both HTTP statuses in its error instead of the same one twice. - The CI workflow template pins PRAGENT_ENGINE=ollama; review_pr defaults to opencode, which does not exist on a Gitea Actions runner. Tests: 68 -> 101, covering each of the above. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B11e8TZZxJyzHW7jj7KWUN
pragent .opencode/ — the review factory
pragent's review runs on opencode (the AI-coding-agent CLI). This directory is
a portable factory: the opencode.json + .opencode/ are dropped into a
checked-out copy of the target repo at the PR head sha, then opencode run is
launched there. The pragent primary agent reviews the diff with real tools
(subagents, LSP/linters via bash, webfetch references) and emits a structured
findings JSON. A thin Python shell posts that JSON back to Gitea as inline
comments + language-highlighted suggested-fix blocks + a summary (dedupe + anchor validation stay
deterministic in Python).
Layout
opencode.json provider (headroom → glm-5.2:cloud), model, lsp, permission, default_agent
.opencode/
agents/
pragent.md PRIMARY reviewer — reads .pragent/brief.md, runs linters, emits findings JSON
security.md subagent — injection/auth/secrets/supply-chain lens (dormant)
tests.md subagent — missing/weak test coverage lens (dormant)
perf.md subagent — N+1 / O(n²) / hot-path lens (dormant)
skills/
review-methodology/SKILL.md severity rubric, what to report, anchoring rules
findings-schema/SKILL.md the exact output JSON shape
commands/
review.md /review slash command (local interactive use)
README.md this file
How a review runs
flowchart TD
WH["webhook_server.py<br/>HMAC + AI-REVIEW gate + dedupe"] --> RP["ai_review.review_pr"]
RP --> ARCH["fetch repo archive @ head sha<br/>→ /tmp/pragent-work/<repo>-<sha>"]
ARCH --> BRIEF["write .pragent/brief.md<br/>(title, body, diff, config, prior, sha)"]
BRIEF --> DROP["drop opencode.json + .opencode/ into workdir"]
DROP --> OC["opencode run --pure --agent pragent --dir <workdir><br/>--model headroom/glm-5.2:cloud"]
OC --> PR["pragent primary<br/>load skills · run linters · read code · delegate lenses"]
PR --> JSON["final message: summary + ```json findings```"]
JSON --> PARSE["ai_review.parse_review_output<br/>{summary, findings}"]
PARSE --> ANCHOR["parse_diff_anchors → split_findings"]
ANCHOR --> POST["post_inline_review<br/>summary + inline lang-tagged fix block + ref links + sha marker"]
Lean by default
The pragent primary does the whole review in one pass for small/medium diffs
(no subagent calls). It delegates to @security / @tests / @perf subagents
ONLY on large (>~400 lines) or security-sensitive diffs. Token cost scales with
PR size. Subagent recursion is capped by the primary's steps budget.
--pure is passed at runtime so the reviewer doesn't load the host user's heavy
global opencode plugins (supermemory/dcp/morph/pty) which hang cold-start. In the
deploy pod there's no global config, so --pure is a no-op there — but it keeps
host-local runs deterministic.
Extending the factory
Add a review lens (subagent)
- Create
.opencode/agents/<name>.mdwithmode: subagent,hidden: true, adescription, and a read-onlypermission(deny edit/write, allow bash/webfetch,task: denyso it can't recurse). The body is its system prompt; end it by requiring the same findings-JSON shape. - Allow it in the primary's
permission.tasklist inpragent.md:task: "*": "deny" "security": "allow" "tests": "allow" "<name>": "allow" # add this - Mention in
pragent.md's "Delegate on heavy diffs" step when to invoke it.
That's it — the primary can now @<name> it via the Task tool. It stays dormant
(the primary decides when), so adding it costs nothing for small PRs.
Add a skill
mkdir .opencode/skills/<name> && touch .opencode/skills/<name>/SKILL.md- Frontmatter:
name: <name>(kebab-case, matches dir),description:(specific enough for the agent to pick it). Body = the knowledge. - Refer to it from
pragent.md("Call theskilltool for<name>").
Per-language expertise is free: the host user already has 29 global skills
(golang-, react-, k8s, terraform, testing, typescript, …). opencode
auto-discovers them via the skill tool — the pragent primary loads a matching
one when the repo's language fits. To ship a pragent-specific one, just drop it
here.
Change the output shape
Edit .opencode/skills/findings-schema/SKILL.md (the schema doc) AND the Python
parser in pilot/ai_review.py (parse_findings) + the renderers
(inline_comment_body, summary_bullets, format_review_body). Keep them in
sync — the parser is tolerant but the agent and parser must agree on field names.
Switch model / provider
Edit opencode.json provider + model. The provider points at the on-network
headroom proxy (http://100.74.17.70:8789/v1, Anthropic /v1/messages format,
apiKey: ollama) → glm-5.2:cloud. To use a different model, add a provider and
reference it as <provider>/<model-id>.
Local one-shot review (no webhook)
cd ~/Projects/pragent
opencode run --pure --agent pragent --dir . \
--model headroom/glm-5.2:cloud \
"Read .pragent/brief.md if present, else review \`git diff HEAD\`, and output findings."
Or in the TUI: /review (uses .opencode/commands/review.md).
Engine flag
PRAGENT_ENGINE=opencode (default once wired) uses this factory. =ollama
falls back to the legacy direct model call in pilot/ai_review.py. The two
share all Gitea I/O, dedupe, and posting logic.