Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions .changeset/fix-max-steps-prefill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@kilocode/cli": patch
---

Prevent provider errors when agents reach their step limit by sending the final summary instruction as user input instead of an assistant prefill.
2 changes: 1 addition & 1 deletion packages/core/src/session/runner/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const layer = Layer.effect(
system: [agent.info?.system, system.baseline]
.filter((part): part is string => part !== undefined && part.length > 0)
.map(SystemPart.make),
messages: [...toLLMMessages(context, model), ...(isLastStep ? [Message.assistant(MAX_STEPS_PROMPT)] : [])],
messages: [...toLLMMessages(context, model), ...(isLastStep ? [Message.user(MAX_STEPS_PROMPT)] : [])], // kilocode_change - avoid provider-incompatible assistant prefill

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

WARNING: Trailing user message can produce consecutive Anthropic user turns

Native Anthropic/Bedrock lowering maps tool results to role: "user" and does not merge a following user message. On the last step after tools this becomes [assistant tool_use, user tool_result, user MAX_STEPS]; with steps: 1 it is [user prompt, user MAX_STEPS]. Anthropic Messages requires alternating roles, so this can 400 the thinking models this PR is trying to unblock.

Suggestion: if the last converted message is already user (or will lower as user, i.e. tool results), append MAX_STEPS_PROMPT onto that message instead of pushing a new one.


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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed in 1663c27. Anthropic, Bedrock, and Gemini lowering now coalesces adjacent user-compatible blocks, including a tool result followed by the max-step instruction, and focused provider tests cover that request shape.

tools: toolMaterialization?.definitions ?? [],
toolChoice: isLastStep ? "none" : undefined,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/core/test/session-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3022,7 +3022,7 @@ describe("SessionRunnerLLM", () => {
expect(requests[1]?.toolChoice).toMatchObject({ type: "none" })
expect(requests[1]?.tools).toEqual([])
expect(requests[1]?.messages.at(-1)).toMatchObject({
role: "assistant",
role: "user", // kilocode_change - max-step instructions must not become assistant prefill
content: [{ type: "text", text: expect.stringContaining("MAXIMUM STEPS REACHED") }],
})
expect(executions).toEqual(["done"])
Expand Down
2 changes: 1 addition & 1 deletion packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1772,7 +1772,7 @@ export const layer = Layer.effect(
system,
messages: [
...modelMsgs,
...(isLastStep ? [{ role: "assistant" as const, content: MAX_STEPS_PROMPT }] : []),
...(isLastStep ? [{ role: "user" as const, content: MAX_STEPS_PROMPT }] : []), // kilocode_change - avoid provider-incompatible assistant prefill
],
tools,
model,
Expand Down
36 changes: 36 additions & 0 deletions packages/opencode/test/session/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,42 @@ it.instance("loop calls LLM and returns assistant message", () =>
}),
)

// kilocode_change start - guard provider-compatible max-step request shape
it.instance(
"loop sends max steps instruction as a user message",
() =>
Effect.gen(function* () {
const { llm } = yield* useServerConfig((url) => ({
...providerCfg(url),
agent: { build: { steps: 1 } },
}))
const prompt = yield* SessionPrompt.Service
const sessions = yield* Session.Service
const chat = yield* sessions.create({ title: "Pinned" })
yield* prompt.prompt({
sessionID: chat.id,
agent: "build",
noReply: true,
parts: [{ type: "text", text: "finish at the limit" }],
})
yield* llm.text("summary")

yield* prompt.loop({ sessionID: chat.id })

const inputs = yield* llm.inputs
const messages = inputs.at(-1)?.messages
if (!Array.isArray(messages)) throw new Error("expected LLM messages")
expect(messages.at(-1)).toMatchObject({
role: "user",
content: expect.arrayContaining([
{ type: "text", text: expect.stringContaining("MAXIMUM STEPS REACHED") },
]),
})
}),
30_000,
)
// kilocode_change end

noLLMServer.instance(
"new prompt dismisses a pending question",
() =>
Expand Down
Loading