Repository navigation
fix(agent-core-v2): deduplicate restored task notifications - #4836
Conversation
🦋 Changeset detectedLatest commit: 22a4319 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 78c3582ece
ℹ️ 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".
| if (record.type === 'turn.prompt') { | ||
| recordDelivery(record['origin'] as PromptOrigin | undefined); |
There was a problem hiding this comment.
Do not treat a started prompt as delivered
If the process exits after turn.prompt is persisted but before the notification's message.appended event, this marks the task as delivered even though it never entered the conversation. LoopJournal writes turn.prompt immediately at turn.started (journal.ts:361-370), so the previous restore path would retry this completion, whereas the new scan suppresses it permanently and silently loses the background-task result on resume. Only index a record that proves the notification was appended, or pair the prompt with a later delivery marker.
AGENTS.md reference: AGENTS.md:L130-L132
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eb0c246fd5
ℹ️ 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".
| complete(prompt: ActivePrompt, result: TurnResult): void { | ||
| const waiter = this.waiters.get(prompt.id); | ||
| if (waiter !== undefined) this.settleCompletion(waiter, result); |
There was a problem hiding this comment.
Settle every prompt materialized into the turn
When a notification such as a background-task completion is appended to an already active turn, materialized() calls launched() for that notification's separate waiter, but this method now settles only the primary turn.prompt. The notification handle therefore remains pending forever and its submitted/started lifecycle never emits prompt.completed; previously every launched waiter subscribed to the shared turn.result. Settle all prompt IDs materialized into the completed turn, not only the original prompt.
AGENTS.md reference: AGENTS.md:L130-L132
Useful? React with 👍 / 👎.
| const requestId = promptOnly || result.type === 'cancelled' ? undefined : result.type === 'completed' ? this.lastRequestId : this.activeRequestTrace?.requestId; | ||
| const error = result.type === 'failed' ? toKimiErrorPayload(result.error) : undefined; | ||
| const interruptReason = result.type === 'completed' ? undefined : interruptReasonFor(result); | ||
| this.options.prompts.complete(turn.prompt, result); |
There was a problem hiding this comment.
Keep prompt completion after the turn's terminal events
On a failed turn, this now publishes prompt.completed before both turn.ended and the error event dispatched below. Klient's existing contract test in packages/klient/test/e2e/invalid-input-matrix.test.ts:1020-1026 explicitly requires turn.started → turn.ended → error → prompt.completed; clients that treat prompt completion as the terminal marker can now stop listening before receiving the failure details, and that e2e test will fail. Preserve the public terminal ordering even if the durable completion record must be written earlier.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 22a4319e65
ℹ️ 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".
| for (const waiter of this.waiters.values()) { | ||
| if (waiter.launchedTurn?.id === turnId) this.settleCompletion(waiter, result); |
There was a problem hiding this comment.
Avoid rescanning every historical prompt waiter
In long-lived sessions, every submitted or notification prompt remains in waiters, so this full-map scan makes each turn progressively more expensive and total finalization work quadratic in the number of prompts processed since startup. Track the waiters launched for each turn (or remove settled waiters while retaining their terminal projection) so completing a turn touches only its own prompts.
Useful? React with 👍 / 👎.
* fix(agent-core-v2): truncate oversized skill content (#4834) * fix(kap-server): bind the main agent when a session is created (#4839) * fix(kap-server): bind the main agent when a session is created * fix(kap-server): reject a profile on prompt submissions * fix(kap-server): reject a profile change through the session profile * fix(kap-server): wait for model configuration before binding a new session * fix(agent-core-v2): deduplicate restored task notifications (#4836) * fix(agent-core-v2): deduplicate restored task notifications * chore: add notification replay fix changeset * fix(agent-core-v2): preserve task notifications after prompt-only crashes * fix(agent-core-v2): complete prompts before ending turns * test: align lifecycle snapshots with prompt completion order * fix(agent-core-v2): settle all prompts during turn finalization * fix(kap-server): drop reserved metadata keys from session profile updates (#4842) * fix(kap-server): drop reserved metadata keys from session profile updates * fix(kap-server): keep stored reserved metadata keys on profile updates * docs: split and rewrite the server API reference (#4841) * docs: split and rewrite the server API reference * docs: redirect old server API anchors to the split pages * docs: describe profile metadata as a replacement * docs: mark question responses as answered-only * fix(kap-server): avoid heartbeat timeout after scheduling stalls (#4823) * fix(kap-server): avoid heartbeat timeout reaping after stalls or clock jumps * fix(kap-server): grace heartbeat window for short scheduling stalls * fix(kap-server): treat only clearly late heartbeats as scheduling stalls * fix(kap-server): time heartbeats on a monotonic clock and drain queued input before reaping --------- Co-authored-by: Kimi Code (Grok Bot) <[email protected]> * fix(kap-server): undo a turn only once when requests race (#4846) * docs: describe heartbeat stall handling (#4849) * docs: describe heartbeat stall handling * docs: explain the monotonic clock in the heartbeat notes * fix(agent-core-v2): acquire session lock before loop restoration (#4848) * fix(agent-core-v2): acquire session lock before loop restoration * chore: add session lock lifecycle changeset * fix(agent-core-v2): hold session lock through loop restoration - add an operation-scoped lease to the session run gate - hold it across loop initialization, reset, and pending drains - cover idle timeout and release ordering * fix(agent-core-v2): keep the output of stopped tool calls (#4854) * fix(kap-server): drop the partial reply of a retried step (#4855) * fix(agent-core-v2): dedupe historical tool schemas (#4862) * fix(agent-core-v2): dedupe historical tool schemas * chore: add duplicate tool schema changeset * fix(agent-core-v2): preserve first tool schema declaration * fix(agent-core-v2): remind on changed tool schemas * fix(agent-core-v2): block stale tool schema calls * fix(agent-core-v2): normalize registry schemas in pending checks * fix(agent-core-v2): import registry tool metadata type * fix(agent-core-v2): preserve abort handling during validation * fix(agent-core-v2): refine validation hook preparation * fix(agent-core-v2): make validation hooks synchronous * fix(agent-core-v2): preserve tool result notes on fork (#4867) * fix(agent-core-v2): preserve tool result notes on fork * chore: add fork tool result note changeset * fix(agent-core-v2): scope tool result note restoration to forks * fix(agent-core-v2): normalize inherited tool result notes * fix(agent-core-v2): isolate fork note restoration * feat(tui): sweep running titles toward text and add KIMI_CODE_TUI_ANIMATION_FPS (#4843) (#4870) * chore: sync web dist from code-app (#4871) code-app: 8ab17294e031323186d357d121c5360b8353900e Co-authored-by: kimi-agent-bot <[email protected]> * ci: release packages (#4835) Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * chore: sync web dist from code-app code-app: 5835391dac3a2b9e0c65ffbd418f0d6042a7adb2 --------- Co-authored-by: Haozhe <[email protected]> Co-authored-by: qer <[email protected]> Co-authored-by: Kimi Code (Grok Bot) <[email protected]> Co-authored-by: grapedge <[email protected]> Co-authored-by: kimi-agent-bot <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Requirement or Bug
Users with completed background tasks can receive the same completion notifications again after resuming a compacted conversation or undoing a turn. This PR restores once-only notification delivery across resume and undo while preserving first delivery for undelivered tasks and tasks that complete during reset.
Bug Reproduction Steps
A separate regression case covers exiting after
turn.promptis persisted but beforemessage.appended: this is not delivery, so resume must still restore the notification.Root Cause
restoreTaskCompletionsrestores terminal tasks from the complete physical journal, but previously rebuilt the delivered set only from active messages, pending entries, and legacytask.waitDeliveredrecords. Compaction and undo remove old notification messages from the active view, so already delivered tasks incorrectly appear undelivered.This is a fundamental fix: the delivered set now also reuses task origins from the complete journal's durable
context.append_messagerecords. Aturn.promptalone is deliberately not treated as delivered because it is written at turn start, before the notification message is appended. No new notification protocol or wire format is introduced.Code Changes
@moonshot-ai/kimi-code.Behavior Changes and Affected Users
TurnEndedcould be persisted beforePromptCompleted, allowing late completion writes to race undoTurnEnded,AgentErrorEventfor failures, thenPromptCompleted; every materialized prompt is settledAffected module:
agent-core-v2task completion restoration. Public APIs and persisted record shapes are unchanged. Current messages, pending entries, and legacytask.waitDeliveredcompatibility remain intact. The three modified test files contain 94 tests, with no net change in test declaration count.Follow-up: Prompt Completion Ordering
The first CI run also exposed a pre-existing completion-order race:
PromptProjectionpublished durableprompt.completedfromturn.result.then(...)after the publicTurnEnded. When undo started in that gap, the late completion record could land after the undo switch triple and failswitchBranch()validation withrecords.write_failed.endTurn()now explicitly completes every waiter materialized into the turn afterTurnEndedand, for failures,AgentErrorEvent. The deferredturn.result.then(...)completion path has been removed, whileturn.resultstill resolves last. Existing public terminal ordering is preserved:turn.ended → error → prompt.completed, and the related snapshots retain that contract.Checklist
Verification
agent-core-v2typecheck passed.pnpm run buildpassed on the final release base.reverse()warnings inrpc-events.test.ts.