Repository navigation
fix(agent-core-v2): acquire session lock before loop restoration - #4848
Conversation
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. |
🦋 Changeset detectedLatest commit: 96e2238 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 |
commit: |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 17e42dd3d7
ℹ️ 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".
| initialTools?: readonly Tool[], | ||
| ): Promise<void> { | ||
| if (this.initialized) return; | ||
| this.runGate.begin(); |
There was a problem hiding this comment.
Hold the session lock for the entire restoration
When a large or slow persisted session takes more than three seconds to drain, reconcile tasks, or restore completions, this call does not keep the lock for that asynchronous operation: SessionRunGateService.schedule() releases it while SessionActivityView remains idle because restoration/reset is not counted as activity. Another app can then claim the session while this instance continues creating and draining the restored loop, recreating the concurrent notification-turn race for users resuming sessions with undelivered task completions. The same issue applies to the new call at line 532, and calling begin() again does not renew the timer for an already-held lock; use an operation-scoped lease or busy guard spanning all awaited restoration work.
AGENTS.md reference: AGENTS.md:L102-L105
Useful? React with 👍 / 👎.
17e42dd to
16d0de9
Compare
- 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
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 96e2238406
ℹ️ 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".
| }); | ||
| this.createLoopFromSnapshot(snapshot, tools, completions, seededMessages); | ||
| } finally { | ||
| lease.release(); |
There was a problem hiding this comment.
Preserve the direct hold when the restoration lease ends
When initialization remains idle for more than three seconds before restoring a pending notification, releaseWhenIdle() clears the direct HeldState.held flag while this lease alone keeps the lock file published. createLoopFromSnapshot() then schedules the restored turn, but this lease.release() removes the final lease and unpublishes the lock before or during that turn, allowing another app to acquire and run the same session concurrently. Fresh evidence in this revision is that the new operation lease permits the idle timer to clear the direct hold; prevent idle release while an operation lease exists or re-establish the activity hold before releasing it. The same issue occurs at line 568 after reset restoration.
AGENTS.md reference: AGENTS.md:L104-L107
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
A session opened by another app instance could restore pending background-task completions and start notification turns before acquiring the exclusive session lock. This could execute pending work concurrently even though the later persistence guard rejected writes. This PR makes Loop initialization and reset wait for the existing session run gate before restoring or executing work.
Bug Reproduction Steps
session.occupied, as happens when another client holds the lock.turn.prompt. After the lock is released, retryingstart()delivers the pending notification exactly once.Root Cause
AgentLoopService.initializeLoop()drained the wire, read the journal snapshot, reconciled task history, restored completions, and created the Loop beforerunGate.begin()ran through ordinary prompt admission.resetLoop()had the same ordering gap. The append-log write guard only protected persistence; it could not prevent an unauthorized restore from executing a Turn first.This is a fundamental ordering fix: the existing run gate now guards Loop initialization and reset directly. No second lock state machine is introduced.
Code Changes
runGate.begin()at the start ofinitializeLoop(), before wire draining, journal snapshots, task-history reconciliation, completion restoration, and Loop creation.turn.prompt; release-and-retry delivers once;@moonshot-ai/kimi-code.Behavior Changes and Affected Users
Affected module:
agent-core-v2Loop initialization/reset and its existing Loop/Task regression tests. There are no config, API-shape, prompt-text, or persisted-format changes.Validation:
agent-core-v2typecheck and the monorepo root build passed; targeted oxlint reported zero errors.remote/processtimer cases and onesessionIndextick case; both files passed independently and are not modified by this PR.Checklist
.changeset/README.md; thegen-changesetsskill was unavailable in the authoring session.