Skip to content
Closed
5 changes: 5 additions & 0 deletions packages/opencode/src/tool/finish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { Config } from "@/config/config"
import { finishGateError, reviewLoopState } from "../session/review-loop"

export const Parameters = Schema.Struct({
reason: Schema.Union([Schema.Literal("success"), Schema.Literal("subagent_wait"), Schema.Literal("failure")]).annotate({
description:
"Why the agent is ending this turn: success when the task is complete, subagent_wait when progress depends on another subagent, or failure when the task could not be completed.",
}),
result: Schema.String.annotate({
description:
"The final result of the task: a concise summary of what was accomplished, presented to the user as the task outcome.",
Expand Down Expand Up @@ -134,6 +138,7 @@ export const FinishTool = Tool.define(
title: "Task completed",
output: params.result,
metadata: {
termination: { reason: params.reason },
review: {
verdict: reviewState.verdict,
reviews: reviewState.reviews,
Expand Down
7 changes: 6 additions & 1 deletion packages/opencode/src/tool/finish.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ End your turn by reporting the outcome. You MUST call this tool exactly once at

Every user message starts a task, even one that needs no tools. Greetings, questions, and conversational or explanatory answers all end with a finish call; answering without using any tools does not remove the requirement. Producing text is never enough on its own — ending your message alone does not complete the turn. The session keeps running until you call finish.

Pass `result` as a concise summary of what you did or answered. This is a silent protocol signal, not something to announce to the user — do not say that you are finishing; just call it as the last step of your reply.
Pass `reason` and `result`. `reason` is required and must be one of:
- `success`: the task completed and the result is usable.
- `subagent_wait`: progress depends on another subagent and the orchestrator should handle that dependency.
- `failure`: the task could not be completed successfully.

Pass `result` as a concise summary of what you did or answered. These are silent protocol signals, not something to announce to the user — do not say that you are finishing; just call it as the last step of your reply.

If the current turn changed files without a completed synchronous review, the first finish call is declined with a review nudge. Running the `review` subagent is strongly recommended; if you have determined that another review pass is unnecessary, call finish again to explicitly skip review and deliver the result, and say so in `result`.

Expand Down
20 changes: 10 additions & 10 deletions packages/opencode/test/session/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1804,7 +1804,7 @@ it.instance(
parts: [{ type: "text", text: "hello" }],
})
yield* llm.text("working on it")
yield* llm.tool("finish", { result: "all done" })
yield* llm.tool("finish", { reason: "success", result: "all done" })

const result = yield* prompt.loop({ sessionID: session.id })

Expand Down Expand Up @@ -1846,7 +1846,7 @@ noLLMServer.instance(
tool: "finish",
state: {
status: "completed",
input: { result: "done" },
input: { reason: "success", result: "done" },
output: "done",
title: "Task completed",
metadata: {},
Expand Down Expand Up @@ -1896,7 +1896,7 @@ it.instance(
tool: "finish",
state: {
status: "completed",
input: { result: "done" },
input: { reason: "success", result: "done" },
output: "done",
title: "Task completed",
metadata: {},
Expand Down Expand Up @@ -1949,7 +1949,7 @@ it.instance(
tool: "finish",
state: {
status: "completed",
input: { result: "done" },
input: { reason: "success", result: "done" },
output: "done",
title: "Task completed",
metadata: {},
Expand Down Expand Up @@ -2080,7 +2080,7 @@ it.instance(
parts: [{ type: "text", text: "Hello." }],
})
yield* llm.push(
reply().text("Hello! How can I help you today?").tool("finish", { result: "Greeted the user." }).stop(),
reply().text("Hello! How can I help you today?").tool("finish", { reason: "success", result: "Greeted the user." }).stop(),
)

const result = yield* prompt.loop({ sessionID: session.id })
Expand Down Expand Up @@ -2122,7 +2122,7 @@ it.instance(
noReply: true,
parts: [{ type: "text", text: "What is the capital of France?" }],
})
yield* llm.push(reply().text("Paris.").tool("finish", { result: "The capital of France is Paris." }).stop())
yield* llm.push(reply().text("Paris.").tool("finish", { reason: "success", result: "The capital of France is Paris." }).stop())

const result = yield* prompt.loop({ sessionID: session.id })

Expand Down Expand Up @@ -2161,7 +2161,7 @@ it.instance(
yield* llm.push(
reply()
.text("An agent loop repeatedly calls the model until the task reaches a terminal state.")
.tool("finish", { result: "Explained the agent loop." })
.tool("finish", { reason: "success", result: "Explained the agent loop." })
.stop(),
)

Expand Down Expand Up @@ -2201,7 +2201,7 @@ it.instance(
parts: [{ type: "text", text: "Write the version to a file and confirm it." }],
})
yield* llm.tool("bash", { command: "printf 1 > version.txt", timeout: 5_000, workdir: path.resolve(dir) })
yield* llm.push(reply().text("Wrote version.txt.").tool("finish", { result: "Wrote the version file." }).stop())
yield* llm.push(reply().text("Wrote version.txt.").tool("finish", { reason: "success", result: "Wrote the version file." }).stop())

const result = yield* prompt.loop({ sessionID: session.id })

Expand Down Expand Up @@ -2242,7 +2242,7 @@ it.instance(
parts: [{ type: "text", text: "List the text files in this project." }],
})
yield* llm.tool("glob", { pattern: "**/*.txt" })
yield* llm.push(reply().text("Found the files.").tool("finish", { result: "Listed the text files." }).stop())
yield* llm.push(reply().text("Found the files.").tool("finish", { reason: "success", result: "Listed the text files." }).stop())

const result = yield* prompt.loop({ sessionID: session.id })

Expand Down Expand Up @@ -2280,7 +2280,7 @@ it.instance(
yield* llm.push(
reply()
.text("No such file exists, so there is nothing to delete.")
.tool("finish", { result: "Nothing to delete — the database file does not exist." })
.tool("finish", { reason: "success", result: "Nothing to delete — the database file does not exist." })
.stop(),
)

Expand Down
4 changes: 2 additions & 2 deletions packages/opencode/test/session/review-delegation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ const scriptPolicyFollowingModel = Effect.gen(function* () {
// session alive for the model to retry.
yield* llm.pushMatch(
reviewMatch,
reply().text(REPORT).tool("finish", { result: "Needs fixes: one Important finding" }),
reply().text(REPORT).tool("finish", { reason: "success", result: "Needs fixes: one Important finding" }),
)
// Retry after the recoverable finish failure. This response supplies the
// required envelope through the real finish call, so the review can complete.
yield* llm.pushMatch(
reviewMatch,
reply().tool("finish", { result: REPORT }),
reply().tool("finish", { reason: "success", result: REPORT }),
)
// Policy present, verdict received: consume it and fix the finding.
yield* llm.pushMatch(policyMatch, reply().text("Fixed the off-by-one in src/cache.ts."))
Expand Down
6 changes: 3 additions & 3 deletions packages/opencode/test/session/review-stagnation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ it.instance(
})
yield* llm.text(REPEAT)
yield* llm.text(REPEAT)
yield* llm.tool("finish", { result: REPEAT })
yield* llm.tool("finish", { reason: "success", result: REPEAT })

yield* user(chat.id, TASK_PROMPT)
const result = yield* prompt.loop({ sessionID: chat.id })
Expand Down Expand Up @@ -1051,7 +1051,7 @@ it.instance(
yield* llm.text(DONE)
yield* llm.text(DONE)
yield* llm.text(DONE)
yield* llm.tool("finish", { result: REPEAT })
yield* llm.tool("finish", { reason: "success", result: REPEAT })

yield* user(chat.id, TASK_PROMPT)
const result = yield* prompt.loop({ sessionID: chat.id })
Expand Down Expand Up @@ -1090,7 +1090,7 @@ it.instance(
yield* llm.text(REPEAT)
yield* llm.text(REPEAT)
yield* llm.text(REPEAT)
yield* llm.tool("finish", { result: "Done." })
yield* llm.tool("finish", { reason: "success", result: "Done." })

yield* user(chat.id, "do the thing", "work")
const result = yield* prompt.loop({ sessionID: chat.id })
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/test/tool/finish-reason.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, test } from "bun:test"
import { Schema } from "effect"
import { Parameters } from "@/tool/finish"

describe("tool.finish termination reason", () => {
test.each(["success", "subagent_wait", "failure"] as const)("accepts %s", (reason) => {
expect(Schema.decodeUnknownSync(Parameters)({ reason, result: "done" })).toEqual({ reason, result: "done" })
})

test("rejects unknown reasons", () => {
expect(() => Schema.decodeUnknownSync(Parameters)({ reason: "cancelled", result: "done" })).toThrow()
})

test("requires a reason", () => {
expect(() => Schema.decodeUnknownSync(Parameters)({ result: "done" })).toThrow()
})
})
30 changes: 15 additions & 15 deletions packages/opencode/test/tool/finish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe("tool.finish – persisted review nudge", () => {

const exit = yield* def
.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -150,7 +150,7 @@ describe("tool.finish – persisted review nudge", () => {
callID: "finish-call-1",
state: {
status: "error",
input: { result: "done" },
input: { reason: "success", result: "done" },
error: "Review nudge",
metadata: { review: { nudged: true, verdict: "pending", reviews: 0, maxIterations: 5 } },
time: { start: now, end: now },
Expand All @@ -159,7 +159,7 @@ describe("tool.finish – persisted review nudge", () => {
const tool = yield* FinishTool
const def = yield* tool.init()
const result = yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -191,7 +191,7 @@ describe("tool.finish – persisted review nudge", () => {
const tool = yield* FinishTool
const def = yield* tool.init()
const result = yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -226,7 +226,7 @@ describe("tool.finish – todo closure safety net", () => {
})

const result = yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -260,7 +260,7 @@ describe("tool.finish – todo closure safety net", () => {
})

yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -295,7 +295,7 @@ describe("tool.finish – todo closure safety net", () => {
})

yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -329,7 +329,7 @@ describe("tool.finish – todo closure safety net", () => {
})

yield* def.execute(
{ result: "done" },
{ reason: "success", result: "done" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -365,7 +365,7 @@ describe("tool.finish – todo closure safety net", () => {
})

yield* def.execute(
{ result: "done A" },
{ reason: "success", result: "done A" },
{
sessionID: chatA.id,
messageID: assistantA.id,
Expand Down Expand Up @@ -396,7 +396,7 @@ describe("tool.finish – todo closure safety net", () => {
expect(before).toHaveLength(0)

const result = yield* def.execute(
{ result: "nothing to do" },
{ reason: "success", result: "nothing to do" },
{
sessionID: chat.id,
messageID: assistant.id,
Expand Down Expand Up @@ -626,7 +626,7 @@ describe("tool.finish – review result gate", () => {
const def = yield* tool.init()

const failure = reviewFailure(
yield* def.execute({ result: "" }, reviewCtx(chat.id, assistant.id)).pipe(Effect.exit),
yield* def.execute({ reason: "success", result: "" }, reviewCtx(chat.id, assistant.id)).pipe(Effect.exit),
)
expect(failure?.message).toContain("Review finish rejected")
expect(failure?.message).toContain("<alphacode-review>")
Expand All @@ -641,7 +641,7 @@ describe("tool.finish – review result gate", () => {

const failure = reviewFailure(
yield* def
.execute({ result: "Looks good to me, ship it." }, reviewCtx(chat.id, assistant.id))
.execute({ reason: "success", result: "Looks good to me, ship it." }, reviewCtx(chat.id, assistant.id))
.pipe(Effect.exit),
)
expect(failure?.message).toContain("Review finish rejected")
Expand All @@ -656,7 +656,7 @@ describe("tool.finish – review result gate", () => {
const def = yield* tool.init()

const result = yield* def.execute(
{ result: `Assessment\n\n${reviewEnvelope(REVIEW_NEEDS_FIXES)}` },
{ reason: "success", result: `Assessment\n\n${reviewEnvelope(REVIEW_NEEDS_FIXES)}` },
reviewCtx(chat.id, assistant.id),
)

Expand All @@ -676,7 +676,7 @@ describe("tool.finish – review result gate", () => {
const def = yield* tool.init()

const result = yield* def.execute(
{ result: `Assessment\n\n${reviewEnvelope(REVIEW_APPROVED)}` },
{ reason: "success", result: `Assessment\n\n${reviewEnvelope(REVIEW_APPROVED)}` },
reviewCtx(chat.id, assistant.id),
)

Expand All @@ -702,7 +702,7 @@ describe("tool.finish – review result gate", () => {
const tool = yield* FinishTool
const def = yield* tool.init()

const result = yield* def.execute({ result: "done" }, reviewCtx(chat.id, assistant.id))
const result = yield* def.execute({ reason: "success", result: "done" }, reviewCtx(chat.id, assistant.id))

expect(result.title).toBe("Task completed")
// The orchestrator assembles the canonical result from the persisted
Expand Down
Loading