Skip to content

llm.ts: V2 runner dropped V1's 'last step' signal — AI never warned to finalize work #30866

Closed

Description

Description

File: packages/core/src/session/runner/llm.ts:256

V1 (packages/opencode/src/session/prompt.ts:1349-1350,1461) sends max-steps.txt (which says "CRITICAL - MAXIMUM STEPS REACHED. Do NOT make any tool calls. Respond with text only.") as an assistant message on the last step:

const maxSteps = agent.steps ?? Infinity
const isLastStep = step >= maxSteps
// L1461:
messages: [...modelMsgs, ...(isLastStep ? [{ role: "assistant", content: MAX_STEPS }] : [])],

V2 has no equivalent. The AI is never told "this is your last turn." On step 24 (the actual last step), the AI creates tool calls that will never be settled — the session terminates right after. This compounds with bug #3 (hard MAX_STEPS cutoff) to waste the final turn entirely.

Fix: Before the last iteration, inject the max-steps.txt content as a system message.

OpenCode version

v1.16.0 (commit 3cf1cef)

Activity

  1. github-actions commented on Jun 5, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of or closely related to existing issues. Please check:

    If these are intentionally separate reports, that's fine — just worth cross-linking them.

  2. lexlian commented on Jun 8, 2026

    @lexlian

    I fixed the issue with PR #31328.

    V2 was missing two things V1 had: (1) the agent's configured step limit was ignored — the runner used a hardcoded MAX_STEPS = 25 instead of agent.steps ?? 25 (see #30865); and (2) the synthetic assistant message from max-steps.txt was never appended to the final LLM request, so the model would emit tool calls on the cutoff step that never get settled.

    The fix in packages/core/src/session/runner/llm.ts mirrors V1's pattern: it now resolves the agent's step limit at the top of run() for the for-loop bound, threads a step counter (Ref<number>) so runTurnAttempt can read the current iteration, and appends the max-steps prompt as an assistant message on the final iteration. Both regressions are covered by four new tests in packages/core/test/session-runner.test.ts.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions