Skip to content

fix(agent-core-v2): align goal turn budgets - #1692

Merged
chengluyu merged 3 commits into
mainfrom
agent/goal-v2-01-turn-budget
Jul 14, 2026
Merged

fix(agent-core-v2): align goal turn budgets#1692
chengluyu merged 3 commits into
mainfrom
agent/goal-v2-01-turn-budget

Conversation

@chengluyu

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue. The problem is described below.

Problem

A goal with a turn budget of N could use only N-1 complete logical goal turns in agent-core-v2. The final allowed turn was counted at step start and immediately transitioned the goal to blocked, so it could be stopped before completing and could not report completion on that turn.

The root cause was that turn accounting and turn-budget enforcement happened in the same operation. This differed from the v1 contract, which checks admission before a turn, counts the admitted turn, lets it finish, and enforces the budget at the next turn boundary.

What changed

  • Separate goal turn accounting from lifecycle blocking so the admitted Nth turn remains active for its full logical turn.
  • Enforce the reached turn budget at turn end, preventing the N+1 continuation from starting.
  • Preserve immediate mid-turn stopping for token and wall-clock budgets, and for a turn budget that explicitly blocks an already-running goal.
  • Add regression coverage for an ordinary final allowed turn, the continuation boundary, and completion winning over the budget block.
  • Add a patch changeset for the CLI bundle.

Boundaries

This change only adjusts agent-core-v2 goal turn-budget timing. It does not alter token or wall-clock budget semantics, other goal lifecycle behavior, or the existing behavior where a user prompt submitted after exhaustion may run as a non-goal turn.

Validation

  • pnpm --filter @moonshot-ai/agent-core-v2 lint:domain
  • pnpm --filter @moonshot-ai/agent-core-v2 typecheck
  • pnpm --filter @moonshot-ai/agent-core-v2 exec vitest run test/agent/goal/goal.test.ts test/agent/goal/goalOps.test.ts test/agent/goal/injection/goalInjection.test.ts test/agent/goal/tools/goal-tools.test.ts — 96 tests passed
  • pnpm --filter @moonshot-ai/agent-core-v2 test — 3352 tests passed
  • pnpm changeset status
  • git diff --check

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

@changeset-bot

changeset-bot Bot commented Jul 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 66b543b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 14, 2026

Copy link
Copy Markdown
pnpm dlx https://pkg.pr.new/@moonshot-ai/kimi-code@66b543b
npx https://pkg.pr.new/@moonshot-ai/kimi-code@66b543b

commit: 66b543b

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 969479ec1c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/goal/goalService.ts Outdated
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: b8b9992997

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chengluyu
chengluyu force-pushed the agent/goal-v2-01-turn-budget branch from b8b9992 to 1a5358f Compare July 14, 2026 12:54
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1a5358f96a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread packages/agent-core-v2/src/agent/goal/goalService.ts
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chengluyu
chengluyu force-pushed the agent/goal-v2-01-turn-budget branch from a429b6e to 66b543b Compare July 14, 2026 13:10
@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: a429b6e32f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chengluyu
chengluyu merged commit e53cd79 into main Jul 14, 2026
14 checks passed
@chengluyu
chengluyu deleted the agent/goal-v2-01-turn-budget branch July 14, 2026 14:11
@github-actions github-actions Bot mentioned this pull request Jul 14, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 66b543b50e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

this.emitGoalUpdated(this.toSnapshot(next));
this.telemetry.track2('goal_continued', { turns_used: next.turnsUsed });
return this.blockIfBudgetReached(next) ?? this.toSnapshot(next);
return this.toSnapshot(next);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Defer turn-budget blocking during usage records

When a final allowed goal turn starts, this now leaves the goal active with turnsUsed === turnBudget; however the real loop records LLM usage before executing any requested tools, and accountTokenUsage() still calls blockIfBudgetReached(). In a turn-budgeted goal where the Nth turn tries to call UpdateGoal complete, the usage record blocks the goal first, so the tool executes against a blocked goal and cannot complete it. Please make usage accounting ignore turn-budget exhaustion for the currently admitted turn, or otherwise defer turn-budget enforcement until turn.ended.

Useful? React with 👍 / 👎.

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.

1 participant