Skip to content

Step limit ends a run with an assistant-role prompt, which models without prefill reject #62641

Closed as duplicate

Description

@adamdotdevin

When an agent sets steps and a run reaches it, the session runner (@opencode/core 2.2.6, SessionRunner.runStep) sends the transcript plus Message.assistant(MAX_STEPS_PROMPT) with toolChoice: "none". Claude Opus 5.5 rejects any request ending on an assistant message:

400 invalid_request_error: This model does not support assistant message prefill. The conversation must end with a user message.

So a capped run fails on its last step instead of summarising.

Repro: an agent with steps: 2 on claude-opus-5-5 and a task that needs 3 or more tool calls.

Expected: the max-steps instruction goes as a user or system message, so the final text-only turn works on every model.

Seen in production on OpenCode Console's hosted agents: a subagent with steps: 8 failed on every call (7 successful requests, then the 400 on the 8th).

Activity

  1. opencodejr commented on Nov 7, 2026

    @opencodejr
    Contributor

    Thanks for the report. I reproduced this on 2.2.6, and it still happens on the latest dev (c33eaa1).

    Steps: a project with three files, the build agent set to steps: 2, and a simulated model that keeps answering with read tool calls while logging every request OpenCode sends.

    What happens: when the step limit is reached, the final request has messages [system, user, assistant (tool call), tool, assistant] and tool_choice: "none". The last assistant message is MAX_STEPS_PROMPT ("CRITICAL - MAXIMUM STEPS REACHED …"). This comes from Message.assistant(MAX_STEPS_PROMPT) in packages/core/src/session/runner/llm.ts, which is unchanged on dev. A model without prefill support gets a request ending on an assistant message, so the capped turn can't succeed.

    There were no real provider credentials here. To mimic Opus 5.5, the simulated model refuses requests that end on an assistant message. That means the 400 … text in the recording below comes from the simulated model, not from Anthropic. The request body OpenCode built is real. The script below reproduces it.

    recording.mp4
    Reproduction script (drive.ts)
    // Repro for #62641: when an agent's `steps` limit is reached, the final model
    // request ends with an *assistant* message (MAX_STEPS_PROMPT) and tool_choice "none".
    // Models without assistant prefill (e.g. Claude Opus 5.5) reject such requests with
    // "This model does not support assistant message prefill." The simulated model below
    // applies that same rule, so the run fails exactly like the reported 400.
    import { Effect, Stream } from "effect"
    import { Llm, OpenCodeDriver } from "opencode-drive"
    import { writeFileSync, mkdirSync } from "node:fs"
    
    const OUT = "/workspace/repro/requests"
    mkdirSync(OUT, { recursive: true })
    
    const lastMessage = (body: any) => {
      const msgs = body?.messages ?? body?.input ?? []
      return msgs[msgs.length - 1]
    }
    
    export default OpenCodeDriver.use(
      {
        ...(process.env.OC_DEV ? { opencode: { dev: process.env.OC_DEV } } : {}),
        project: {
          git: true,
          files: { "a.txt": "A\n", "b.txt": "B\n", "c.txt": "C\n" },
        },
        config: { agents: { build: { steps: 2 } } } as any,
        tui: { recording: true },
      },
      ({ ui, llm }) =>
        Effect.gen(function* () {
          yield* llm.serve((request, index) => {
            const body: any = request.body
            writeFileSync(`${OUT}/${index}.json`, JSON.stringify(body, null, 2))
            const last = lastMessage(body)
            const role = last?.role ?? last?.type
            console.log(`request #${index + 1}: last message role=${role} tool_choice=${JSON.stringify(body?.tool_choice)}`)
            if (role === "assistant") {
              // Provider rule taken as given by the report: no assistant prefill.
              return Stream.make(Llm.text(
                "400 invalid_request_error: This model does not support assistant message prefill. The conversation must end with a user message.",
              ))
            }
            const files = ["a.txt", "b.txt", "c.txt"]
            return Stream.make(
              Llm.toolCall({ index: 0, id: `call_${index}`, name: "read", input: { path: files[index % 3] } }),
              Llm.finish("tool-calls"),
            )
          })
          yield* ui.submit("Read a.txt, b.txt and c.txt one at a time and summarise them")
          yield* ui.waitFor("assistant message prefill", { timeout: 60000 } as any)
          yield* Effect.sleep(1500)
          yield* ui.screenshot("max-steps-prefill")
        }),
    )

    Run it with opencode-drive: opencode-drive run ./drive.ts. It drives opencode from your PATH; set OC_DEV to the path of an OpenCode checkout to run that from source instead.

  2. ccclucky commented on Nov 9, 2026

    @ccclucky

    I'd like to take this one.

    Confirmed on current dev: SessionRunner.runStep appends Message.assistant(MAX_STEPS_PROMPT) with toolChoice: "none", so the step-limited request ends on an assistant message. Providers without assistant prefill (the reported Claude Opus 5.5, and other non-prefill models) reject that request with 400, so a capped run fails on its final step instead of summarizing.

    Plan: send the same instruction as a Message.user(...) tail, the pattern the other one-off instructions already use (compaction.ts, generate.ts). toolChoice: "none" and the existing "keep tool definitions on the final step to preserve the cached prefix" behavior stay as they are, and the transcript prefix is unchanged, so nothing before the appended message moves in the cache.

    Regression test: a step-limited run's final request ends with a user message carrying MAX_STEPS_PROMPT while toolChoice is none, and the existing step-limit scenarios keep passing. I'll follow up with the PR.

  3. ccclucky commented on Nov 9, 2026

    @ccclucky

    I missed an earlier PR when I claimed this: #56257 already sends the step-limit instruction as a user message, and #62725 was closed as its duplicate. Leaving it to that PR, so I am not taking this one.

  4. neriousy commented on Nov 10, 2026

    @neriousy
    Member

    Thanks for the report. This is the same issue as #56250, so we're tracking it there. Closing as a duplicate. Please add any extra details on #56250.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions