Skip to content

fix(llmloop): count a grace-round task_done as completion - #1678

Merged
lizhengfeng101 merged 4 commits into
alibaba:mainfrom
JasonBuildAI:fix/llmloop-grace-round-done
Oct 15, 2026
Merged

lizhengfeng101 merged 4 commits into
alibaba:mainfrom
JasonBuildAI:fix/llmloop-grace-round-done

Conversation

@JasonBuildAI

@JasonBuildAI JasonBuildAI commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Description

ocr scan and ocr review run a grace round once the tool-request or token budget is spent. That round only offers code_comment and task_done, and its prompt tells the model: "Call task_done if you have nothing more to report." When the model did exactly that, the completion was dropped. runGraceRound threw away the TaskCheckpoint and RunMainTask returned completed=false no matter what.

So a file the model had explicitly finished was recorded as incomplete. Scan wrote review_item_failed instead of a reusable review_item_done, the review manifest put the group into failed coverage, and ocr resume re-ran a file that was already done (#1018).

This change makes runGraceRound report whether it executed a successful task_done, and RunMainTask return that as its result. Stops that never run a grace round (StopEmptyRounds, StopCompression) are untouched, and a task_done(FAILED) in the grace round still leaves the subtask incomplete, the same as in the main loop. Two MainLoopStop comments that described completion as implying StopNone are updated to match, since a grace-round completion keeps the budget stop that led to it.

One consequence worth naming: when the aggregate token budget stops a group and the grace round then finishes it, that group completes, so the run no longer classifies it as failed(budget) or records the token_budget_reached warning for it. That follows from the group actually finishing, and later groups are still gated on TotalTokensUsed(), so the budget itself is still enforced.

One thing I deliberately left alone: the --max-tokens-budget help text says a group over budget "is reported as failed(budget)". That is still what happens unless the grace round finishes the task, and the same wording is mirrored in action.yml, the skill files and four translated doc pages, so rewording it felt like a separate change. Happy to send that as a follow-up if you want it tightened.

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring (no functional changes)
  • Documentation update
  • CI / Build / Tooling

How Has This Been Tested?

make is not available on my Windows machine, so I ran what make check and make test run, over the same package list:

  • gofmt -s -l . and go vet over the same 24 packages — both clean
  • go test -race -count=1 over the Makefile's 24 packages — all pass
  • scripts/verify-english-only.go — pass (648 files); the three touched files already carry the SPDX header and no new files are added
  • go tool cover — 90.6% total, above the 90% threshold

New tests:

  • TestRunMainTask_GraceRoundTaskDoneCompletes — a task_done(DONE) in the grace round now returns completed=true
  • TestRunMainTask_GraceRoundTaskDoneFailedStaysIncomplete — a task_done(FAILED) there does not
  • TestDispatchSubtasks_GraceRoundTaskDoneCheckpointed — end to end through scan: the file gets a checkpoint, session.LoadResumeState can reuse it, and no scan_subtask_error warning is recorded

Both new tests fail on the unpatched code (the scan one fails with "all 1 file scan(s) failed") and pass with the fix.

  • make test passes locally
  • Manual testing (describe below)

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI disclosure: I prepared this change with AI assistance (OpenAI Codex, a GPT-5-based coding agent), at my own request. I have read and understood the whole diff, and I went over the added tests before pushing. The commit is not attributed to the AI, and I will answer review comments myself.

Related Issues

closes #1018

Note: #1019 also targets this issue and proposes a much wider change (cancellation and FAILED semantics). That PR has been conflicting since mid-September. This PR deliberately stays minimal: it only carries the completion state out of the grace round.

@CLAassistant

CLAassistant commented Oct 9, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

The grace round after an exhausted budget offers only code_comment and
task_done, and asks the model to call task_done when it has nothing further
to report. That checkpoint was discarded and RunMainTask returned
completed=false, so a file the model had finished was recorded as
incomplete: scan wrote review_item_failed instead of a reusable
review_item_done, and a later resume re-ran it.

Report grace-round completion and return it. Stops that never run a grace
round, and a task_done(FAILED), are unchanged.

closes alibaba#1018
@JasonBuildAI
JasonBuildAI force-pushed the fix/llmloop-grace-round-done branch from a585390 to 8334e96 Compare October 9, 2026 09:07

@NanaseInori NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Propagating a successful grace-round task_done fixes the lost completion state. The scan caller checks completed before classifying a budget stop, so the retained stop reason does not turn that success back into a failure. The added tests cover successful completion, FAILED remaining incomplete, and a reusable scan checkpoint. No blocking issue found in the code review; tests were not rerun in this review.

@JasonBuildAI

Copy link
Copy Markdown
Contributor Author

Thanks @NanaseInori for the review and for double-checking the budget-stop interaction — much appreciated. I also ran the \internal/llmloop\ and \internal/scan\ tests on the current head and they pass.

@lizhengfeng101

Copy link
Copy Markdown
Contributor

@JasonBuildAI Thanks for the focused fix. I agree that a successful grace-round task_done should count as completion and produce a reusable checkpoint.

I would like us to keep task completion and visibility of review limits separate, though. As you noted in the description, the current callers only handle the budget stop on the incomplete path. With this change, a single task can hit a limit, finish in the grace round, and report zero findings without a corresponding warning in the final result. The progress log alone does not cover JSON consumers, and there may be no subsequent task or round to trigger another budget warning.

Could you include the following in this PR before we merge?

  • Preserve completed=true and the reusable checkpoint, but also emit a structured warning when the tool-request round limit or aggregate token budget leads to a grace round, even if that round completes successfully.
  • Surface that warning in the final terminal output and JSON. For zero findings, avoid an unqualified "Looks good to me" message when review limits were reached.
  • Add regression coverage for both limits with a successful grace-round completion, zero findings, and no subsequent work: the limit remains visible while the completion checkpoint remains reusable.

This does not require treating the task as failed or redefining budget_exceeded; a distinct warning would be sufficient. Broader budget-state or documentation changes can remain separate follow-ups.

A task that spends its tool-request rounds or token budget and then
completes in the grace round reported success with no trace of the
limit, so a zero-finding result could read as clean. Record a
review_limit_reached warning with the stop reason and the round's
verdict, and qualify the zero-finding message in text and JSON output.
Completion, the reusable checkpoint, and budget_exceeded are unchanged.
@JasonBuildAI

Copy link
Copy Markdown
Contributor Author

Thanks @lizhengfeng101 — agreed on keeping task completion and limit visibility separate. I pushed a follow-up commit (45421ac) covering the three points.

  • Structured warning: RunMainTask now records a review_limit_reached warning whenever a grace round follows an exhausted limit (StopMaxRounds or StopTokenBudget), however that round ends. The message carries the stop reason and the round's verdict (...; the grace round completed the task vs ...; the grace round did not complete the task), so both cases stay distinguishable for humans and JSON consumers.
  • Output: the zero-finding branches of both outputTextWithWarnings and outputJSONWithWarnings now switch on subtask errors, then this warning, before the "Looks good to me" fallback. A limit now reads as "A review limit was reached before finishing; some findings may be missing." The structured entry itself keeps its existing shape: warnings[] in JSON, [ocr] WARNING [review_limit_reached] on stderr in text.
  • Regression tests: TestDispatchSubtasks_GraceRoundLimitVisibleWithZeroFindings covers both limits with successful grace-round completion, zero findings, and no subsequent work — it asserts exactly one visible limit warning, BudgetExceeded() stays false, and the checkpoint is still reusable through session.LoadResumeState. TestRunMainTask_TokenBudgetGraceRoundTaskDoneCompletes covers the token budget at the loop level, plus new text/JSON output tests. Existing tests that assumed "exactly one warning" now filter by type, since the limit warning legitimately joins the list.

Nothing here redefines budget_exceeded or treats the task as failed: completed=true and the reusable checkpoint are unchanged. Verification on the current head: go vet, gofmt -s -l ., and the internal/llmloop, internal/scan, internal/agent, and cmd/opencodereview test suites all pass; total coverage across the 24 runnable packages is 90.8% (internal/delegate's tests need a working bash, which this Windows machine lacks).

@lizhengfeng101 lizhengfeng101 left a comment

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.

LGTM

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants