Skip to content

fix(opencode): implement Work and Code distinct Review subagents - #201

Merged
mdev34-lab merged 1 commit into
devfrom
fix/runtime-specific-reviewers
Oct 5, 2026
Merged

mdev34-lab merged 1 commit into
devfrom
fix/runtime-specific-reviewers

Conversation

@kilo-code-bot

@kilo-code-bot kilo-code-bot Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #113

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Adds distinct Review subagents for the Work and Code runtimes.

The generic review subagent becomes a request rather than an identity. The task tool resolves which reviewer actually runs from the agent that made the request: a work parent dispatches work-review, a code parent dispatches code-review, and any other parent keeps the existing generic review. Every value in the routing table is itself a member of the reviewer set, so a routed request still passes the report gate.

Why they are not read-only. The reviewers share one reviewPermissions block, extracted from the existing review definition, so they keep the plan-equivalent inspection permissions including bash for reproducing and verifying runtime behaviour, with edit denial enforced at the permission seam. An earlier version of this PR defined both new reviewers with a blanket "*": "deny", which would have silently reverted #165. The split is domain focus only: all three reviewers are identical in capability and in the standard they apply, and none of their descriptions advertise a weaker bar.

Why the reviewer names are centralised. The report gate that requires every reviewer to deliver a parseable <alphacode-review> envelope is keyed on agent name. A name missing from the set completes a review without a verdict rather than failing loudly. The names therefore live in one module (REVIEW_AGENTS for the gate, REVIEW_ROUTING for dispatch) instead of being compared inline at each of the four call sites. Centralising reduces the failure mode; it does not remove it, and the module says so.

Parent-agent resolution. A session row's agent is null for the default session, so the newest user message naming an agent is the fallback that actually identifies what is driving the session. MessageV2.page returns messages oldest-first, so this uses findLast; reading the oldest message would invert the routing in exactly the mid-session agent switch it exists to handle. ctx.agent is deliberately not consulted, because on the subtask path it is the child's agent.

Permission keying. The task permission prompt stays keyed on the requested subagent_type, not the resolved reviewer, because stored always-allow grants and user config rules are written against that name. Re-keying on the runtime routing decision would silently re-prompt returning users and silently drop a permission.task.review deny rule.

Stale "read-only" wording that #165 made false is corrected, and the two new reviewer prompts no longer claim they cannot run commands.

How did you verify your code works?

bun typecheck clean in packages/opencode.

  • test/session/review-specialization.test.ts 15/0
  • test/session/review-loop.test.ts 28/0
  • test/session/review-delegation.test.ts 5/0
  • test/session/review-stagnation.test.ts 58/0
  • test/tool/task.test.ts 35/0
  • test/tool/finish.test.ts 19/0
  • test/tool/finish-reason.test.ts 15/0
  • test/tool/review-finish-history.test.ts 1/0

One pre-existing failure in test/agent/work-code.test.ts ("permission-denied tools are hidden while finish remains available") reproduces unchanged on dev; it exercises only the plan agent and ToolRegistry, neither of which this PR touches.

The ordering fix is mutation-verified: reverting findLast to find drops the mixed-agent routing cases to 12 pass / 3 fail, and restoring returns 15/0.

The four findings raised by the Kilo Code review bot on this PR are being addressed; see the PR conversation.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions

Copy link
Copy Markdown

This PR doesn't fully meet our contributing guidelines and PR template.

What needs to be fixed:

  • PR description is missing required template sections. Please use the PR template.

Please edit this PR description to address the above within 2 hours, or it will be automatically closed.

If you believe this was flagged incorrectly, please let a maintainer know.

@github-actions

Copy link
Copy Markdown

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

@kilo-code-bot

kilo-code-bot Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Author

Code Review Summary

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 1
WARNING 2
SUGGESTION 1
Issue Details (click to expand)

CRITICAL

File Line Issue
packages/opencode/src/tool/task.ts 149 resolveParentAgent swallows parent-session NotFound via catchCause, so a task against a deleted/invalid parent silently routes to the generic review instead of failing. The pre-change ?? ctx.agent fallback tier was dropped.

WARNING

File Line Issue
packages/opencode/src/tool/task.ts 186 Permission prompt keyed on requested subagent_type while metadata.subagent_type carries the resolved reviewer; config grants/deny rules written against work-review/code-review never match, and stored tool input disagrees with the gating rule.
packages/opencode/src/tool/task.ts 347 Background delivery re-resolves the parent agent separately from the child's pinned agent at line 275; a mid-session agent switch could deliver the notification under a different agent than the one that dispatched it.

SUGGESTION

File Line Issue
packages/opencode/src/agent/review-agents.ts 31 REVIEW_ROUTING is a plain Record<string, string> with no exhaustiveness guard; a new runtime without an entry silently falls back to generic review.
Files Reviewed (14 files)
  • packages/opencode/src/agent/agent.ts - 1 issue (reviewPermissions extraction verified consistent)
  • packages/opencode/src/agent/prompt/code-review.txt - clean
  • packages/opencode/src/agent/prompt/work-review.txt - clean
  • packages/opencode/src/agent/review-agents.ts - 1 issue
  • packages/opencode/src/session/prompt.ts - clean
  • packages/opencode/src/session/prompt/finish-nudge.txt - clean
  • packages/opencode/src/session/prompt/gpt.txt - clean
  • packages/opencode/src/session/prompt/review-loop.txt - clean
  • packages/opencode/src/tool/finish.ts - clean
  • packages/opencode/src/tool/task.ts - 3 issues
  • packages/opencode/src/tool/task.txt - clean
  • packages/opencode/test/session/review-delegation.test.ts - clean
  • packages/opencode/test/session/review-loop.test.ts - clean
  • packages/opencode/test/session/review-specialization.test.ts - clean

Fix these issues in Kilo Cloud

Previous Review Summaries (2 snapshots, latest commit 32ae7f4)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit 32ae7f4)

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 1
WARNING 2
SUGGESTION 1
Issue Details (click to expand)

CRITICAL

File Line Issue
packages/opencode/src/tool/task.ts 149 resolveParentAgent swallows parent-session NotFound via catchCause, so a task against a deleted/invalid parent silently routes to the generic review instead of failing. The pre-change ?? ctx.agent fallback tier was dropped.

WARNING

File Line Issue
packages/opencode/src/tool/task.ts 186 Permission prompt keyed on requested subagent_type while metadata.subagent_type carries the resolved reviewer; config grants/deny rules written against work-review/code-review never match, and stored tool input disagrees with the gating rule.
packages/opencode/src/tool/task.ts 347 Background delivery re-resolves the parent agent separately from the child's pinned agent at line 275; a mid-session agent switch could deliver the notification under a different agent than the one that dispatched it.

SUGGESTION

File Line Issue
packages/opencode/src/agent/review-agents.ts 31 REVIEW_ROUTING is a plain Record<string, string> with no exhaustiveness guard; a new runtime without an entry silently falls back to generic review.
Files Reviewed (14 files)
  • packages/opencode/src/agent/agent.ts - 1 issue (reviewPermissions extraction verified consistent)
  • packages/opencode/src/agent/prompt/code-review.txt - clean
  • packages/opencode/src/agent/prompt/work-review.txt - clean
  • packages/opencode/src/agent/review-agents.ts - 1 issue
  • packages/opencode/src/session/prompt.ts - clean
  • packages/opencode/src/session/prompt/finish-nudge.txt - clean
  • packages/opencode/src/session/prompt/gpt.txt - clean
  • packages/opencode/src/session/prompt/review-loop.txt - clean
  • packages/opencode/src/tool/finish.ts - clean
  • packages/opencode/src/tool/task.ts - 3 issues
  • packages/opencode/src/tool/task.txt - clean
  • packages/opencode/test/session/review-delegation.test.ts - clean
  • packages/opencode/test/session/review-loop.test.ts - clean
  • packages/opencode/test/session/review-specialization.test.ts - clean

Fix these issues in Kilo Cloud

Previous review

This review did not finish. The model reached its output limit before it
could write the review — a reasoning model can spend the whole budget thinking.
Re-run the review, or lower the model's thinking effort, and it should get
further. Any inline comments below are from an earlier review.


Reviewed by free · Input: 0 · Output: 0 · Cached: 0

@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

@kilo-code-bot kilo-code-bot Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test

* here is itself a member of `REVIEW_AGENTS`, so a routed request still passes
* the report gate.
*/
export const REVIEW_ROUTING: Record<string, string> = {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test

@kilo-code-bot kilo-code-bot Bot left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of runtime-specific Review subagents (Work/Code). 4 findings on changed code.

* here is itself a member of `REVIEW_AGENTS`, so a routed request still passes
* the report gate.
*/
export const REVIEW_ROUTING: Record<string, string> = {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SUGGESTION: REVIEW_ROUTING is typed as a plain Record<string, string> with no exhaustiveness guard, so adding a new runtime (e.g. desktop) without a routing entry silently falls back to the generic review agent — the same silent-degradation failure mode the file's own docblock warns about for REVIEW_AGENTS. Since every value must itself be a member of REVIEW_AGENTS, a discriminated-union type keyed on the known parent agents (e.g. Record<'work' | 'code', (typeof REVIEW_AGENTS)[number]>) would make a missing entry a compile error rather than a runtime fallback. The guard test in review-specialization.test.ts only checks the three existing entries, so this gap is currently unproven either way.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Comment thread packages/opencode/src/tool/task.ts Outdated
// it is the CHILD's agent, so it would select a reviewer by the child's
// identity. Hydration is deferred into the fallback so a pinned session
// never pays for it.
const resolveParentAgent = Effect.fnUntraced(function* (session: { agent?: string }) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CRITICAL: resolveParentAgent swallows the parent session's NotFound error. sessions.get(ctx.sessionID) at line 122 already throws NotFound for a missing parent, but the fallback at lines 151-153 catches all causes with Effect.catchCause(() => Effect.succeed([])) and then returns undefined from findLast(...)?.info.agent. A task dispatched against a deleted/invalid parent session therefore resolves parentAgent to undefined, resolveReviewer falls back to the generic review agent, and the subagent runs with the wrong prompt and permissions instead of failing loudly. The original code at line 344 also used catchCause, but it had ?? ctx.agent as a final tier, so a missing session still resolved to a real agent. That tier was dropped in this change. Either rethrow NotFound (let the session-get failure propagate) or catch only the specific not-found cause and fail the task explicitly.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Comment thread packages/opencode/src/tool/task.ts Outdated
// grants and user config rules (`permission.task.review`) are written
// against it. Re-keying on the runtime routing decision would silently
// re-prompt returning users and silently drop their `deny` rule.
patterns: [params.subagent_type],

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: The permission prompt is keyed on params.subagent_type (the requested name, e.g. "review") while the metadata.subagent_type sent in the same prompt carries the resolved reviewer (work-review / code-review). The comment at lines 181-185 argues this is correct because user grants and permission.task.review rules are written against the requested name — but the metadata field is what UIs, audit logs, and any consumer of part.state.input.subagent_type read (see subagent-data.ts:312, tool.ts:381, session-data.ts:413). A user who configured an always-allow or deny rule against work-review or code-review directly will see it silently never match, and the stored tool input will disagree with the rule that actually gated it. Align the two: either send the requested name in metadata too, or document explicitly why the divergence is intentional and update the comment.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

Comment thread packages/opencode/src/tool/task.ts Outdated
currentParent.agent ?? parentMessages.find((message) => message.info.role === "user")?.info.agent ?? ctx.agent
// path, so it is not a candidate; the shared resolution above reads the
// parent session and its newest user message instead.
const parentAgent = yield* resolveParentAgent(currentParent)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: The background delivery path re-resolves the parent agent via resolveParentAgent(currentParent) here, but the child run itself is launched with agent: next.name (the resolved reviewer) at line 275. So a background review's delivery agent is decided by two different mechanisms: the child session row's pinned agent for its own turns, and a fresh parent-agent read for the synthetic notification injected into the parent. If the parent session's pinned agent and its newest user message ever disagree (mid-session agent switch, the exact case this routing exists to handle), the background notification can be written under a different agent than the one that dispatched it. Pick one resolution site and reuse it for both the child agent and the notification agent.


Reply with @kilocode-bot fix it to have Kilo Code address this issue.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

Feel free to open a new pull request that follows our guidelines.

Issue #113. The generic `review` subagent becomes a request rather than an
identity: the task tool resolves which reviewer runs from the parent agent, so
`work` dispatches work-review, `code` dispatches code-review, and any other
parent keeps the existing generic `review`. Every routing target is itself a
member of the reviewer set, so a routed request still passes the report gate.

All three reviewers share one `reviewPermissions` block, extracted from the
existing `review` definition, so they keep the plan-equivalent inspection
permissions including `bash` for reproducing and verifying runtime behaviour,
with edit denial enforced at the permission seam. The split is domain focus
only: identical capability, identical standard, no description implying a
weaker bar.

The report gate is keyed on agent name, and a name missing from the set
completes a review without a verdict rather than failing loudly. The names
therefore live in one module, `REVIEW_AGENTS` for the gate and `REVIEW_ROUTING`
for dispatch, with a `ReviewAgent` union making "a routed target is itself a
reviewer" a compile error. Centralising reduces the failure mode; it does not
remove it, and the module says so.

Parent-agent resolution reads the session row, then the newest user message
naming an agent, because a session row's `agent` is null for the default
session. `MessageV2.page` returns messages oldest-first, so this uses
`findLast`; reading the oldest message would invert the routing in exactly the
mid-session agent switch it exists to handle. `ctx.agent` is deliberately not
consulted, because on the subtask path it is the child's agent. The read is
memoized and yielded only when a review routes or a background result is
delivered, so other delegations never pay for it.

An unreadable parent fails the dispatch explicitly instead of resolving to no
agent and silently falling back to the generic reviewer with the wrong prompt
and permissions. The catch is narrowed to `NotFound` so any other cause stays a
defect rather than being reinterpreted.

The task permission prompt is keyed on the requested `subagent_type` together
with the resolved name, because stored always-allow grants and user config
rules are written against either, and a deny on one name must not be bypassed
by an allow on the other.

Stale read-only wording that #165 made false is corrected, and the two new
reviewer prompts no longer claim they cannot run commands.
@mdev34-lab
mdev34-lab force-pushed the fix/runtime-specific-reviewers branch from 32ae7f4 to d412f67 Compare September 27, 2026 23:11
@mdev34-lab
mdev34-lab merged commit 21fa9d6 into dev Oct 5, 2026
6 of 7 checks passed
@mdev34-lab
mdev34-lab deleted the fix/runtime-specific-reviewers branch October 5, 2026 20:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(agent): give Work and Code distinct Review subagents

1 participant