Skip to content
56 changes: 51 additions & 5 deletions cli/src/main/kotlin/loop/cli/Host.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import loop.core.workflow.WorkflowFile
import loop.core.workflow.WorkflowLoader
import loop.runner.acp.AcpSettings
import loop.runner.acp.AgyPreflight
import loop.runner.acp.GeminiRiskReviewer
import loop.runner.claudecode.ClaudeCodePreflight
import loop.runner.claudecode.ClaudeCodeRiskReviewer
import loop.runner.claudecode.ClaudeCodeSettings
Expand Down Expand Up @@ -445,9 +446,10 @@ fun runnerFor(
* The read only reviewer of the merge risk assessment (m2-28), whatever `runner.kind` is (m4-06, bl-145), since the
* reviewer is a concern apart from the runner that did the work: the model the workflow names in `risk.reviewer` picks
* its runtime ([RiskReviewerRuntime], bl-214). A Codex model gets Codex's reviewer, with the settings of `runners.codex`
* ([codexReviewerSettings]); any other model Claude Code's, with the [riskReviewerCommand]. The program is resolved as
* [resolveCommand] does, so the reviewer starts exactly the file the startup check found; null when it cannot be found,
* or `runners.codex` cannot be read, so the auto merge gate falls back naming the reviewer setup, and the startup warns
* ([codexReviewerSettings]); a Gemini model Gemini's (bl-205), with [geminiReviewerSettings]; any other model Claude
* Code's, with the [riskReviewerCommand]. The program is resolved as [resolveCommand] does, so the reviewer starts exactly
* the file the startup check found; null when it cannot be found, or the runner section cannot be read, so the auto
* merge gate falls back naming the reviewer setup, and the startup warns
* ([riskReviewerWarning]). Its env is the agent env policy and it refuses reads under [holdoutDir], like an agent.
*/
fun riskReviewerFor(
Expand All @@ -469,6 +471,14 @@ fun riskReviewerFor(
CodexRiskReviewer(settings.copy(command = command), hostEnv, removedEnvNames)
}
}
RiskReviewerRuntime.Gemini -> {
val settings = geminiReviewerSettings(workflow, holdoutDir).getOrNull()
val argv = settings?.argv
argv?.let { resolveCommand(it.first(), hostEnv) }?.let { program ->
val command = AcpSettings.commandOf(listOf(program) + argv.drop(1))
GeminiRiskReviewer(settings.copy(command = command), hostEnv, removedEnvNames)
}
}
}

/** bl-214: the runtime of the merge risk reviewer of [workflow], from the model of `risk.reviewer`. */
Expand Down Expand Up @@ -498,16 +508,46 @@ fun codexReviewerSettings(
}

/**
* The program the merge risk reviewer runs (m4-06, bl-214). For a Claude Code reviewer: under `runner.kind:
* bl-205: the settings of the Gemini merge risk reviewer, with [holdoutDir]. Under `runner.kind: acp` with `preset: agy`
* they are `runners.acp` read as the ACP runner reads it, which the loader already checked: its `command`,
* `read_timeout_ms` and `strict_mcp` apply. Under any other runner they are the defaults with `agy acp`. The model and
* the time limit are always the review's.
*/
fun geminiReviewerSettings(
workflow: WorkflowFile,
holdoutDir: Path? = null,
): Result<AcpSettings> =
runCatching {
val settings =
if (geminiReviewerOwnsCommand(workflow)) {
AcpSettings.of(workflow.runner.section(AcpSettings.SECTION), holdoutDir)
} else {
AcpSettings(command = AcpSettings.DEFAULT_AGY_COMMAND, preset = AcpSettings.PRESET_AGY, holdoutDir = holdoutDir)
}
settings.also { it.argv }
}

/** bl-205: true when the Gemini reviewer's command is `runners.acp.command`, the runner being `acp` with `preset: agy`. */
private fun geminiReviewerOwnsCommand(workflow: WorkflowFile): Boolean =
workflow.runner.kind == AcpSettings.KIND &&
(workflow.runner.sections[AcpSettings.SECTION] as? Map<*, *>)?.get("preset")?.let { it as? String }?.trim() ==
AcpSettings.PRESET_AGY

/**
* The program the merge risk reviewer runs (m4-06, bl-214, bl-205). For a Claude Code reviewer: under `runner.kind:
* claude-code` the command of its section, which the loader checked; under any other runner `runners.claude-code.command`
* when it is set and not blank, else [DEFAULT_REVIEWER_COMMAND]. For a Codex reviewer: the first word of the command of
* [codexReviewerSettings], or [CodexAppServerSettings.DEFAULT_COMMAND]'s when the section cannot be read.
* [codexReviewerSettings], or [CodexAppServerSettings.DEFAULT_COMMAND]'s when the section cannot be read. For a Gemini
* reviewer: the first word of the command of [geminiReviewerSettings], or `agy` when it cannot be read.
*/
fun riskReviewerCommand(workflow: WorkflowFile): String =
when (riskReviewerRuntime(workflow)) {
RiskReviewerRuntime.Codex ->
codexReviewerSettings(workflow).getOrNull()?.argv?.first()
?: CodexAppServerSettings.DEFAULT_COMMAND.substringBefore(' ')
RiskReviewerRuntime.Gemini ->
geminiReviewerSettings(workflow).getOrNull()?.argv?.first()
?: AcpSettings.DEFAULT_AGY_COMMAND.substringBefore(' ')
RiskReviewerRuntime.ClaudeCode ->
if (workflow.runner.kind == ClaudeCodeSettings.KIND) {
ClaudeCodeSettings.of(workflow.runner.section(ClaudeCodeSettings.KIND)).command
Expand Down Expand Up @@ -549,6 +589,12 @@ fun riskReviewerWarning(
when (runtime) {
RiskReviewerRuntime.ClaudeCode -> "Install Claude Code or set runners.claude-code.command"
RiskReviewerRuntime.Codex -> "Install Codex or set runners.codex.command"
RiskReviewerRuntime.Gemini ->
if (geminiReviewerOwnsCommand(workflow)) {
"Install the Antigravity CLI or set runners.acp.command"
} else {
"Install the Antigravity CLI so agy is on PATH"
}
}
return "kinds.code.delivery.risk.reviewer is $model, but its command $command cannot be found, so no merge risk " +
"reviewer can run and every result waits for loop approve. $fix"
Expand Down
12 changes: 10 additions & 2 deletions cli/src/main/kotlin/loop/cli/RiskReviewerRuntime.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ package loop.cli
/**
* The runtime that runs the merge risk reviewer (bl-214), chosen by the model `kinds.code.delivery.risk.reviewer` names,
* not by `runner.kind`: the reviewer is a concern apart from the runner that did the work (m4-06). [of] is the one place
* the choice is made, so a reviewer of another runtime (bl-205, Gemini) adds its value and its model names here.
* the choice is made, so a reviewer of another runtime adds its value and its model names here, as Gemini did (bl-205).
*/
enum class RiskReviewerRuntime {
/** Claude Code, the reviewer of m2-28: every model no other runtime claims, aliases such as `sonnet` included. */
ClaudeCode,

/** Codex (bl-214): a model of the `gpt-` or `codex-` families, or of the `o` series such as `o3` and `o4-mini`. */
Codex,

/** Gemini (bl-205), through `agy acp`: a model whose name starts with `gemini-`. */
Gemini,
;

companion object {
private val CODEX_MODEL = Regex("""^(gpt-|codex-|o\d).*""")
private const val GEMINI_PREFIX = "gemini-"

/** The runtime of [model], trimmed and in any case; [ClaudeCode] for a model no other runtime claims, or none. */
fun of(model: String?): RiskReviewerRuntime {
val name = model?.trim()?.lowercase() ?: return ClaudeCode
return if (CODEX_MODEL.matches(name)) Codex else ClaudeCode
return when {
CODEX_MODEL.matches(name) -> Codex
name.startsWith(GEMINI_PREFIX) -> Gemini
else -> ClaudeCode
}
}
}
}
85 changes: 85 additions & 0 deletions cli/src/test/kotlin/loop/cli/DeliveryExecutorsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import loop.api.RiskReviewError
import loop.api.RiskReviewRequest
import loop.core.workflow.WorkflowFile
import loop.core.workflow.WorkflowLoader
import loop.runner.acp.GeminiRiskReviewer
import loop.runner.claudecode.ClaudeCodeRiskReviewer
import loop.runner.codex.CodexRiskReviewer
import loop.tracker.github.GitHubResponse
Expand Down Expand Up @@ -351,4 +352,88 @@ class DeliveryExecutorsTest {
"codex is not on PATH, and claude does not stand in for it",
)
}

/** bl-205: a fake `agy` in [dir] that logs its arguments and directory to `agy.log`, then exits 1. */
private fun fakeAgy(dir: Path): Path {
Files.createDirectories(dir)
val log = dir.resolve("agy.log")
return dir.resolve("agy").also {
it.writeText("#!/bin/sh\n{ echo \"ARGS \$*\"; echo \"PWD \$(pwd)\"; } >> '$log'\nexit 1\n")
Files.setPosixFilePermissions(it, PosixFilePermissions.fromString("rwxr-xr-x"))
}
}

@Test
fun `bl-205 gemini-4-neon-high picks the Gemini reviewer, sonnet keeps Claude Code and gpt-6_1-sol keeps Codex`() {
val bin = root.resolve("bin-all")
val claude = fakeClaude(bin)
val codex = fakeCodex(bin)
val agy = fakeAgy(bin)
val host = mapOf("PATH" to "$bin:/usr/bin:/bin", "HOME" to root.toString())
val runners = " claude-code:\n command: claude"

assertEquals(
"GeminiRiskReviewer(command='$agy' acp)",
riskReviewerFor(autoMergeWorkflow("claude-code", runners, "gemini", "gemini-4-neon-high"), host, emptySet()).toString(),
)
assertEquals(
"ClaudeCodeRiskReviewer(command=$claude)",
riskReviewerFor(autoMergeWorkflow("claude-code", runners, "sonnet", "sonnet"), host, emptySet()).toString(),
)
assertEquals(
"CodexRiskReviewer(command='$codex' 'app-server')",
riskReviewerFor(autoMergeWorkflow("claude-code", runners, "gpt", "gpt-6.1-sol"), host, emptySet()).toString(),
)
}

@Test
fun `bl-205 a Gemini reviewer runs agy acp under preset agy, found on the host PATH, in the workspace`() =
runBlocking {
val bin = root.resolve("bin-agy")
fakeAgy(bin)
val workflow =
autoMergeWorkflow("codex-app-server", " codex:\n command: codex app-server", reviewer = "gemini-4-neon-high")
val host = mapOf("PATH" to "$bin:/usr/bin:/bin", "HOME" to root.toString())

val reviewer = assertIs<GeminiRiskReviewer>(riskReviewerFor(workflow, host, emptySet()))
assertEquals(
"GeminiRiskReviewer(command='${bin.resolve("agy")}' acp)",
reviewer.toString(),
"the default command agy acp, its program found on the host PATH",
)

val workspace = Files.createDirectories(root.resolve("workspaces/t-3")).toRealPath()
val result = reviewer.review(RiskReviewRequest(workspace, "gemini-4-neon-high", "Review this.", 10_000))

assertIs<RiskReviewError.Crashed>(result.exceptionOrNull(), "the fake exits before any answer")
val logged = bin.resolve("agy.log").readText()
assertContains(logged, "ARGS acp --disable-telemetry --sandbox --gemini-dir ")
assertContains(logged, "PWD $workspace")
}

@Test
fun `bl-205 under runner acp with preset agy the Gemini reviewer takes runners acp command`() {
val agy = fakeAgy(root.resolve("pinned agy"))
val workflow =
autoMergeWorkflow(
"acp",
" acp:\n command: \"'$agy' acp\"\n preset: agy\n model: gemini-4-neon-medium",
reviewer = "gemini-4-neon-high",
)

val reviewer = riskReviewerFor(workflow, mapOf("PATH" to "/usr/bin:/bin"), emptySet())

assertEquals("GeminiRiskReviewer(command='$agy' acp)", reviewer.toString())
}

@Test
fun `bl-205 a Gemini reviewer whose agy cannot be found gives no reviewer, so the gate falls back`() {
fakeClaude(root.resolve("only-claude-2"))
val workflow = autoMergeWorkflow("claude-code", " claude-code:\n command: claude", reviewer = "gemini-4-neon-high")

assertNull(
riskReviewerFor(workflow, mapOf("PATH" to root.resolve("only-claude-2").toString()), emptySet()),
"agy is not on PATH, and claude does not stand in for it",
)
}
}
21 changes: 19 additions & 2 deletions cli/src/test/kotlin/loop/cli/RiskReviewerRuntimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package loop.cli
import kotlin.test.Test
import kotlin.test.assertEquals

/** bl-214: the model of `risk.reviewer` picks the runtime that runs the merge risk reviewer, Codex for a Codex model. */
/**
* bl-214 and bl-205: the model of `risk.reviewer` picks the runtime that runs the merge risk reviewer, Codex for a Codex
* model and Gemini for a Gemini model.
*/
class RiskReviewerRuntimeTest {
@Test
fun `bl-214 a gpt, codex or o series model runs on Codex, whatever its case`() {
Expand All @@ -12,9 +15,23 @@ class RiskReviewerRuntimeTest {
}
}

@Test
fun `bl-205 a gemini model runs on Gemini, whatever its case`() {
listOf("gemini-4-neon-high", "Gemini-3.8-flash-low", " gemini-4-carbon-medium ").forEach {
assertEquals(RiskReviewerRuntime.Gemini, RiskReviewerRuntime.of(it), it)
}
}

@Test
fun `bl-205 gemini-4-neon-high picks Gemini, sonnet keeps Claude Code and gpt-6_1-sol keeps Codex`() {
assertEquals(RiskReviewerRuntime.Gemini, RiskReviewerRuntime.of("gemini-4-neon-high"))
assertEquals(RiskReviewerRuntime.ClaudeCode, RiskReviewerRuntime.of("sonnet"))
assertEquals(RiskReviewerRuntime.Codex, RiskReviewerRuntime.of("gpt-6.1-sol"))
}

@Test
fun `bl-214 a Claude model, an alias, another name or no model stays on Claude Code`() {
listOf("claude-sonnet-5-5", "sonnet", "opus", "fable", "ollama-llama", "openai", "gpt", null).forEach {
listOf("claude-sonnet-5-5", "sonnet", "opus", "fable", "ollama-llama", "openai", "gpt", "gemini", null).forEach {
assertEquals(RiskReviewerRuntime.ClaudeCode, RiskReviewerRuntime.of(it), "$it")
}
}
Expand Down
21 changes: 21 additions & 0 deletions cli/src/test/kotlin/loop/cli/RiskReviewerWarningTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,27 @@ class RiskReviewerWarningTest {
)
}

@Test
fun `bl-205 a Gemini reviewer model whose agy cannot be found warns, naming agy and its fix`() {
useCodex(delivery = AUTO_MERGE.replace("claude-sonnet-5-5", "gemini-4-neon-high"))

assertEquals(
"kinds.code.delivery.risk.reviewer is gemini-4-neon-high, but its command agy cannot be found, so no " +
"merge risk reviewer can run and every result waits for loop approve. Install the Antigravity CLI so " +
"agy is on PATH",
riskReviewerWarning(workflow(), mapOf("PATH" to root.resolve("empty").toString())),
)
}

@Test
fun `bl-205 a Gemini reviewer model whose agy is on PATH gives no warning, even without claude`() {
val bin = root.resolve("bin-agy")
executable(bin.resolve("agy"))
useCodex(delivery = AUTO_MERGE.replace("claude-sonnet-5-5", "gemini-4-neon-high"))

assertNull(riskReviewerWarning(workflow(), mapOf("PATH" to "$bin")))
}

private companion object {
const val PULL_REQUEST =
" policy: POLICY\n" +
Expand Down
20 changes: 18 additions & 2 deletions docs/plugins/runner-acp.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ The `acp` runner drives an Agent Client Protocol (ACP) agent over JSON-RPC 2.0 o
4. [Launch and streams](#launch-and-streams)
5. [Session startup and turns](#session-startup-and-turns)
6. [Isolated folder lifetime](#isolated-folder-lifetime)
7. [Context limits and rotation](#context-limits-and-rotation)
8. [Capabilities](#capabilities)
7. [Guards and holdout protection](#guards-and-holdout-protection)
8. [Merge risk reviewer](#merge-risk-reviewer)
9. [Context limits and rotation](#context-limits-and-rotation)
10. [Capabilities](#capabilities)

## Configuration

Expand Down Expand Up @@ -116,6 +118,20 @@ Every `session/request_permission` request is evaluated fail-closed in Kotlin us
- **Human-input prompts:** unsupported human-input requests end the turn immediately with `turn_input_required` and stop the session; no later turn of it starts (see [Isolated folder lifetime](#isolated-folder-lifetime)).
- **Protected workspace roots check (`preset: agy`):** before every session and before every `session/prompt`, inspects `.agents/`, `.agent/`, `_agents/`, and `_agent/` directly in directories between the workspace and the repository root (ignoring nested paths `agy` never loads). Any protected root that is a symbolic link (even if tracked) is refused on every turn. Every on-disk customization file (`hooks.json`, `mcp_config.json`, `skills.json`, `rules.json`, `plugins.json`, or any file under `plugins/`, `rules/`, or `hooks/`) is compared against the host-resolved base commit SHA (`git ls-tree` mode and `git hash-object` blob SHA, independent of the git index). When no base commit can be resolved from `AttemptContext.baseRef`, `main`, `master`, or `origin/HEAD`, the check fails closed naming `kinds.code.guards.base` and `kinds.code.delivery.pull_request.base`. Any mismatch fails the turn naming the path and ends the session: no later turn of it respawns `agy` (see [Isolated folder lifetime](#isolated-folder-lifetime)).

## Merge risk reviewer

The merge risk reviewer follows the model of `kinds.code.delivery.risk.reviewer`, not `runner.kind`. Since bl-205 a model whose name starts with `gemini-`, such as `gemini-4-neon-high`, runs on Gemini through this runner, so a workflow on Claude Code or Codex can use a Gemini model as its independent reviewer, and a machine without Claude Code can run `policy: auto` with merge. `GeminiRiskReviewer` runs one `agy acp` turn of `AcpRunner` under `preset: agy` in the issue workspace:

- **Command:** `runners.acp.command` under `runner.kind: acp` with `preset: agy`, with its `read_timeout_ms`; under any other runner `agy acp`. The program is looked up on the host `PATH`, and under `policy: auto` with merge `loop run` warns at startup when it cannot be found.
- **Model and time:** the reviewer's model, trimmed and in lower case as the routing reads it, is set with `session/set_model`, and the turn ends at `kinds.code.delivery.risk.timeout_ms`, which stops `agy`.
- **Read only:** `AcpSettings.readOnly`, which is not a profile key. Every command, write, edit, delete, move and MCP tool permission request is declined (`the merge risk reviewer runs read only`); reads and searches are allowed except under the holdout folder, and, as for a worker, reads outside the workspace are allowed. No `mcpServers` are passed. Everything else of `preset: agy` holds: the version pin, the isolated `--gemini-dir`, `--sandbox`, the protected roots check and the env policy.
- **No kept folder:** a review never resumes, and its isolated folder is removed when it ends.
- **Protected roots:** the review keeps the protected workspace roots check, so it fails closed with `protected_workspace_root` when the pull request touches an agent customization root (`.agents`, `.agent`, `_agents`, `_agent`), or when no base commit can be resolved from `main`, `master` or `origin/HEAD`, and the gate falls back to `loop approve`.
- **Context files:** unlike the Claude Code reviewer, the review turns off no workspace context file (`AGENTS.md`, `GEMINI.md`): `agy` loads them, and the gate's `protected_paths` condition is what keeps a pull request that edits them from merging on its own.
- **Answer:** the text of the last agent message, as for the Codex reviewer, with the session's tokens and no cost. A message is a run of `agent_message_chunk` texts that a tool call, an update of one or a chunk with another `messageId` ends, and a message with no text is skipped, so a tool call update after the final message keeps it. A failed turn, an input request, a launch failure, no agent message or no terminal event is a crash, and the gate falls back to `loop approve`.

Any other model keeps its reviewer: a Codex model (`gpt-6.1-sol`) runs on Codex (bl-214) and any other, such as `sonnet`, on Claude Code (m4-06). See [Gemini merge risk reviewer](../spec-decisions.md#gemini-merge-risk-reviewer-extension-bl-205).

## Context limits and rotation

Loop ships default context windows of `1048576` tokens in `ContextConfig.DEFAULT_WINDOWS` for `google/gemini-4-neon-high`, `google/gemini-4-neon-medium`, `google/gemini-4-neon-low`, `google/gemini-4-neon-xhigh`, `google/gemini-3.8-flash-high`, `google/gemini-3.8-flash-medium`, and `google/gemini-3.8-flash-low`.
Expand Down
Loading