open-code-review icon indicating copy to clipboard operation
open-code-review copied to clipboard

bug(llmloop): preserve task_done(DONE) completion from the grace round

Open AllenMuu opened this issue 3 months ago • 1 comments

OpenCodeReview Version

v1.9.8 (latest release). Affected range: v1.9.3 through v1.9.8. The grace round was introduced in da972b2; v1.9.2 and earlier predate it.

Operating System

macOS (Apple Silicon)

Installation Method

Built from source

LLM Provider

Other OpenAI-compatible endpoint

The deterministic reproduction uses a scripted client, so the defect is not provider-specific.

Bug Description

When a file exhausts its tool-call budget, RunPerFile makes a final grace-round LLM request limited to code_comment and task_done.

If the model calls task_done(DONE) in that round, the tool call is parsed and executed, but its completion state is discarded. RunPerFile returns completed=false, so scan/review callers record the file as incomplete rather than persisting a reusable review_item_done checkpoint. A later ocr resume then re-runs a file that the model explicitly marked complete.

Steps to Reproduce

  1. Configure a test template with MaxToolRequestTimes=1.
  2. Return a valid context-tool call in the first LLM response so the budget is exhausted.
  3. Return task_done with {"state":"DONE"} in the grace-round response.
  4. Observe that RunPerFile returns (completed=false, StopMaxRounds, nil).
  5. Run the same flow through scan mode, finalize the session, then load it with LoadResumeState.
  6. Observe that no completed checkpoint is available and a resumed scan invokes the LLM again for the unchanged file.

This can be reproduced deterministically with a scripted LLM client; no API key is required.

Expected Behavior

A task_done(DONE) executed in the grace round should propagate completion through RunPerFile. Scan/review should persist review_item_done, and a later resume should reuse the unchanged file without another LLM request.

Logs / Error Output

main_task did not complete before stopping

Additional Context

Root cause in v1.9.8:

  • internal/llmloop/loop.go:377-381 invokes runGraceRound after StopMaxRounds and then unconditionally returns completed=false.
  • internal/llmloop/loop.go:432-434 executes grace-round tool calls but discards each returned TaskCheckpoint.
  • task_done(DONE) returns TaskCheckpoint{Completed: true}, but that state never reaches scan/review checkpoint persistence.

The grace round still records its LLM response and executes side effects such as code_comment; only the terminal completion state is lost.

AllenMuu avatar Aug 20 '26 08:08 AllenMuu

I'd like to take this one.

I re-checked current main and the defect is still present: runGraceRound (internal/llmloop/loop.go) discards the TaskCheckpoint returned by executeToolCall, and RunMainTask returns (false, stop, nil) after the grace round, so a task_done(DONE) issued there never reaches the caller. A file the model explicitly marked complete is therefore recorded as incomplete -- scan persists review_item_failed instead of a reusable review_item_done, and ocr resume re-runs it.

Plan: have runGraceRound report whether it completed and propagate that from RunMainTask, leaving the StopEmptyRounds / StopCompression paths untouched, plus unit tests for a completing and a non-completing grace round.

Disclosure: this work is being prepared with AI assistance (Codex, GPT-5-based coding agent). I understand and have reviewed every change myself before submitting it.

JasonBuildAI avatar Oct 09 '26 04:10 JasonBuildAI