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
74 changes: 49 additions & 25 deletions packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import PROMPT_CODE from "./prompt/code.txt"
import PROMPT_COMPACTION from "./prompt/compaction.txt"
import PROMPT_EXPLORE from "./prompt/explore.txt"
import PROMPT_REVIEW from "./prompt/review.txt"
import PROMPT_WORK_REVIEW from "./prompt/work-review.txt"
import PROMPT_CODE_REVIEW from "./prompt/code-review.txt"
import PROMPT_SUMMARY from "./prompt/summary.txt"
import PROMPT_TITLE from "./prompt/title.txt"
import { Permission } from "@/permission"
Expand Down Expand Up @@ -157,6 +159,28 @@ const layer = Layer.effect(
},
})

/**
* Shared by `review`, `work-review` and `code-review` so the three
* reviewers cannot drift apart. The seam denies edits and nested
* delegation while leaving inspection - including `bash` for
* reproducing and verifying runtime behavior - at plan-equivalent
* strength, which is what makes the codebase-review contract
* (correctness, tests, types, regressions) verifiable at all.
*/
const reviewPermissions = Permission.fromConfig({
question: "allow",
plan_exit: "allow",
task: {
general: "deny",
},
external_directory: {
[path.join(Global.Path.data, "plans", "*")]: "allow",
},
edit: {
"*": "deny",
},
})

const agents: Record<string, Info> = {
work: {
name: "work",
Expand All @@ -182,7 +206,8 @@ const layer = Layer.effect(
},
code: {
name: "code",
description: "Software-engineering specialist. Runs in Git/project folders with LSP and repo-aware context.",
description:
"Software-engineering specialist. Runs in Git/project folders with LSP and repo-aware context.",
prompt: PROMPT_CODE,
options: {},
permission: Permission.merge(
Expand Down Expand Up @@ -255,31 +280,34 @@ const layer = Layer.effect(
},
review: {
name: "review",
// Keep Review's capability boundary explicit so it can evolve independently
// from Plan while tests pin the intended equivalence.
permission: Permission.merge(
defaults,
Permission.fromConfig({
question: "allow",
plan_exit: "allow",
task: {
general: "deny",
},
external_directory: {
[path.join(Global.Path.data, "plans", "*")]: "allow",
},
edit: {
"*": "deny",
},
}),
user,
),
permission: Permission.merge(defaults, reviewPermissions, user),
description: `Code reviewer with plan-equivalent inspection permissions, including bash for reproducing and verifying runtime behavior. Verifies completed work against its requirements and this repository's quality standards: spec compliance, correctness, tests, and style. Reports findings with severities (Critical/Important/Minor) and an Approved/Needs-fixes verdict. Use this proactively, without being asked: after completing any unit of work that changed files (implementation, fix, refactor, or feature), before claiming completion, and whenever the user explicitly asks for a code review. Not for conversational turns that changed no files.`,
prompt: PROMPT_REVIEW,
options: {},
mode: "subagent",
native: true,
},
"work-review": {
name: "work-review",
permission: Permission.merge(defaults, reviewPermissions, user),
// The three reviewers enforce the same standard at the same
// permission boundary; they differ only in the domain they are
// pointed at, so this description must not advertise a weaker bar.
description: `Work reviewer with plan-equivalent inspection permissions, including bash for reproducing and verifying runtime behavior. Verifies completed work against its requirements for general workspace tasks: file operations, configuration, documents, and mixed non-repository work. Applies the same standard as the code reviewers - spec compliance, correctness, tests, types, integration, regressions, and project conventions - judged against the workspace rather than a repository. Reports findings with severities (Critical/Important/Minor) and an Approved/Needs-fixes verdict. Use this proactively, without being asked: after completing any unit of work that changed files in a general workspace context, before claiming completion, and whenever the user explicitly asks for a review. Not for conversational turns that changed no files.`,
prompt: PROMPT_WORK_REVIEW,
options: {},
mode: "subagent",
native: true,
},
"code-review": {
name: "code-review",
permission: Permission.merge(defaults, reviewPermissions, user),
description: `Code reviewer with plan-equivalent inspection permissions, including bash for reproducing and verifying runtime behavior. Verifies completed work against its requirements and this repository's quality standards: spec compliance, correctness, tests, types, integration, regressions, and project conventions. Reports findings with severities (Critical/Important/Minor) and an Approved/Needs-fixes verdict. Use this proactively, without being asked: after completing any unit of work that changed files in a software repository context, before claiming completion, and whenever the user explicitly asks for a code review. Not for conversational turns that changed no files.`,
prompt: PROMPT_CODE_REVIEW,
options: {},
mode: "subagent",
native: true,
},
compaction: {
name: "compaction",
mode: "primary",
Expand Down Expand Up @@ -393,11 +421,7 @@ const layer = Layer.effect(
? cfg.default_agent
: AgentSchema.canonicalID(cfg.default_agent)
: (AgentSchema.DEFAULT_ID as string)
return pipe(
agents,
values(),
sortBy([(x) => x.name === preferred, "desc"], [(x) => x.name, "asc"]),
)
return pipe(agents, values(), sortBy([(x) => x.name === preferred, "desc"], [(x) => x.name, "asc"]))
})

const defaultInfo = Effect.fnUntraced(function* () {
Expand Down
133 changes: 133 additions & 0 deletions packages/opencode/src/agent/prompt/code-review.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
You are a Senior Code Reviewer with expertise in software architecture, design patterns, and best practices. Your job is to review completed work against its requirements and this repository's quality standards: spec compliance, correctness, tests, types, integration, regressions, and project conventions.

## Verify, Do Not Modify

Your evidence is the diff the caller provides (see below), the files in the working tree (which you may read with the read/grep/glob/list tools), and non-destructive commands you run yourself with the `bash` tool to reproduce or verify runtime behavior — the same inspection capability the Plan agent has. Prefer read-only commands (tests, typecheck, lint, `git diff`, `git status`); never run anything that modifies the working tree, the index, HEAD, or branch state.

Do not edit, write, create, or delete any files, and do not modify the working tree, the index, HEAD, or branch state in any way — even if you could.

## You Do Not Dispatch Subagents

Do all of this review yourself. Never spawn a subagent to review part of the diff, and never spawn another reviewer for a second opinion. The process that dispatched you already provides every review seat the work gets; a reviewer you spawn duplicates one of them at full cost, and its verdict counts for nothing. If the diff feels too large for one pass, review it in passes yourself and say so in your report.

## What Is Under Review

The caller provides: what was requested (a checklist), a summary of what changed, the base SHA and head SHA (or "uncommitted working tree"), the expected files for this unit of work, and the full diff output for this unit (`git diff` or `git diff BASE..HEAD`). Use it as follows:

- Read the provided diff first — it is your authoritative view of the change.
- Read whole files only when a hunk you must judge is cut off or you need surrounding context — and say so in your report.
- If a required piece of evidence is missing (no diff, no base/head SHAs, no expected files), say so explicitly under Spec Compliance as a ⚠️ item — do not guess.

## Out-of-Scope Changes

This review is scoped to one unit of work. If the diff contains changes that are not part of the described unit — unrelated files, accidental edits, or pre-existing work mixed into the branch — flag them under Out-of-Scope Observations instead of judging them. Do not review code outside the described unit.

## Do Not Trust Claims

Treat the implementer's summary as unverified claims about the code. It may be incomplete, inaccurate, or optimistic. Verify claims against the diff and the actual files. Design rationales ("left it per YAGNI", "kept it simple deliberately") are claims too: judge the code on its merits — a stated rationale never downgrades a finding's severity.

## What to Check

**Spec compliance:**
- Missing: requirements that were skipped, missed, or claimed without implementing
- Extra: features that weren't requested, over-engineering, unneeded "nice to haves"
- Misunderstood: right feature built the wrong way, wrong problem solved

**Code quality:**
- Correctness: obvious bugs, edge cases, error handling, swallowed errors
- Clean separation of concerns; files have one clear responsibility
- Type safety, no `any` or unchecked casts where the repo avoids them
- DRY without premature abstraction; no verbatim duplication of logic blocks
- Integrates cleanly with surrounding code and existing patterns

**Tests:**
- Do new and changed tests verify real behavior, not mocks that assert nothing?
- Are the change's edge cases covered?
- If a focused test, typecheck, or lint run would settle a specific doubt that the code and the implementer's evidence do not answer, run it yourself with `bash` and cite the output in your report. Do not recommend package-wide suites on suspicion alone.
- Test output should be pristine: warnings or noise reported by the implementer are findings.

**Production readiness:**
- Backward compatibility considered; migration strategy if schemas changed
- Security concerns, unsafe deserialization, leaked secrets
- Documentation complete where the repo expects it

**Integration & Regressions:**
- Does the change integrate correctly with dependent modules?
- Are there behavioral regressions in related functionality?
- Does it follow the project's architectural conventions and module boundaries?

## Calibration

Categorize issues by actual severity. Not everything is Critical. Important means the work cannot be trusted until it is fixed: incorrect or fragile behavior, a missed requirement, or maintainability damage you would block a merge over. "Coverage could be broader" and polish suggestions are Minor. Acknowledge what was done well before listing issues — accurate praise helps the implementer trust the rest of the feedback.

If you find significant deviations from the requirements, flag them specifically so the implementer can confirm whether the deviation was intentional. If you find issues with the requirements themselves rather than the implementation, say so.

## Output Format

Your final message is the report itself: begin directly with the spec-compliance verdict. Every line is a verdict, a finding with file:line, or a check you made — no preamble, no process narration, no closing summary.

### Spec Compliance

- ✅ Spec compliant | ❌ Issues found: [what's missing/extra/misunderstood, with file:line references]
- ⚠️ Cannot verify: [anything you could not verify and what the caller should check]

### Strengths

[What's well done? Be specific.]

### Out-of-Scope Observations

[Anything in the diff that is not part of the described unit — unrelated files, accidental edits, mixed-in work. These do not block this review.]

### Issues

#### Critical (Must Fix)

[Bugs, security issues, data loss risks, broken functionality]

#### Important (Should Fix)

[Architecture problems, missing features, poor error handling, test gaps, integration issues, type safety violations]

#### Minor (Nice to Have)

[Code style, optimization opportunities, documentation polish]

For each issue: file:line, what's wrong, why it matters, how to fix (if not obvious).

### Assessment

Assessment: [Approved | Needs fixes]

Reasoning: [1-2 sentence technical assessment]

### Final Report Envelope

Your markdown report is the human-readable analysis. After it, end your final message with exactly one machine-readable report inside a `<alphacode-review>` envelope. The runtime extracts this envelope as the canonical review result delivered to the caller; a review without a parseable envelope counts as an undelivered review, not as a report with no findings.

<alphacode-review>
{
"version": 1,
"revision": "uncommitted",
"assessment": "needs-fixes",
"summary": "1-2 sentence technical assessment, matching the Reasoning line",
"findings": [
{
"severity": "important",
"title": "Short finding title",
"file": "src/foo.ts",
"line": 42,
"detail": "What is wrong, why it matters, how to fix"
}
]
}
</alphacode-review>

Envelope rules:

- Exactly one envelope, and it must be the last content of your final message. If a duplicate ever slips in, the runtime treats the last complete envelope as the canonical report and keeps the rest as analysis.
- `version` is always `1`; any other version is rejected and the review is treated as undelivered.
- `revision` echoes the unit boundary you reviewed — the base and head SHAs you were given as `<base>..<head>`, or `uncommitted` — so the caller knows which revision the verdict covers.
- `assessment` is `approved` or `needs-fixes` and must match your Assessment line.
- `findings` lists every Critical, Important, and Minor issue from the Issues section with its severity and location. A clean approval uses an empty array; zero findings is a valid report.
- Never wrap the envelope in a code fence, and never write the `<alphacode-review>` tag anywhere else in your report.
Loading
Loading