Skip to content

refactor(agent-core-v2): single producer for turn and step ids - #3662

Merged
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:refact-174-09-08-turn-step-id-convergence
Sep 9, 2026
Merged

refactor(agent-core-v2): single producer for turn and step ids#3662
sailist merged 1 commit into
MoonshotAI:mainfrom
sailist:refact-174-09-08-turn-step-id-convergence

Conversation

@sailist

@sailist sailist commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

Internal refactor (no linked issue).

Problem

Turn and step ids currently have multiple producers. The facade (AgentLoopService) reserves turn ids at prompt submission (burning ids on queue cancel), while the human agent state machine keeps its own turn clock whose value is ignored except for one fragile resetHistory alignment point. The facade then guesses which queue entry the machine drained via queue-id diffs (reconcileDrainedQueueEntry, bindMachineTurn, unboundDrained, machineTurnUnbound). Step ordinals are produced in two places (the loop gate and the engine's currentStep). On top of that, the transcript package's foldFacts re-derives turn ids a third time by counting turn.prompt records, and the kap-server projection layers have to predict future turn ids for queued prompts.

What changed

  • The human agent state machine is now the single producer of turn ids: turn.started carries the machine-assigned id plus the drained queue item id, and the facade adopts it deterministically at gate time. The facade's id reservation, the four-piece binding heuristic, and the resetHistory clock alignment are deleted.
  • The human turn state machine is now the single producer of step ordinals, surfaced as a first-class step.started fact event that flows through the xstate actor tree to the engine. The llm requester layer stays step-agnostic.
  • turn.prompt wire records now carry an explicit turnId; turnKey and the transcript package's foldFacts read it directly, keeping the legacy counting derivation only as a fallback for old records (a hasExplicitTurnId-gated filter tolerates burned-id gaps in new wires).
  • Queued prompts are addressed by promptId: cancel(turnId) is active-turn only, a new cancelQueued(queueId) removes machine queue entries, status().pendingPromptIds replaces pendingTurnIds, and Turn.id resolves once the turn actually starts (RPC turn_id results are read after turn.ready).
  • Dropped PromptPayload.disabledTools and its klient/node-sdk pass-through: the field had no working client consumer (the RPC boundary rejected it unconditionally) — the REST disabled_tools route is untouched.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue (external PRs: the issue must have a maintainer's /approve).
  • 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.

- the human agent state machine now assigns turn ids and echoes the drained queue item in turn.started; the facade adopts machine ids instead of reserving its own
- the turn machine produces the step ordinal and surfaces it via a first-class step.started fact; the llm requester layer stays step-agnostic
- turn.prompt wire records carry an explicit turnId; turnKey and transcript foldFacts read it directly with a counting fallback for legacy records
- queued prompts are addressed by promptId: cancel(turnId) is active-turn only, cancelQueued targets queue entries, Turn.id resolves once the turn starts
- drop PromptPayload.disabledTools and its klient/node-sdk pass-through (no client consumer)
@changeset-bot

changeset-bot Bot commented Sep 9, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: b7c771a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 9, 2026

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

commit: b7c771a

@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: b7c771a1b6

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

await agent.prompt({
input: input.input,
disabledTools: input.disabledTools,
promptId: input.promptId,

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 Preserve the SDK's disabled-tools enforcement

When an embedded SDKRpcClientV2 caller supplies disabledTools (for example, ['Bash']), this forwarding path now drops the property, while the corresponding AgentPromptService.submit call to setSessionDisabledTools was also deleted. The SDK uses Klient's in-memory transport, whose previous contract accepted this field, so existing compiled JavaScript callers now silently leave the requested tools enabled rather than merely encountering the kap-server validation mentioned in the commit. Keep forwarding and enforcing the denylist, or treat its removal as a public breaking change with the required major changeset.

AGENTS.md reference: AGENTS.md:L64-L64

Useful? React with 👍 / 👎.

@sailist
sailist merged commit 68f8984 into MoonshotAI:main Sep 9, 2026
16 checks passed
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