fix(agent-core-v2): harden wire restore and background task lifecycle - #1635
Conversation
…ioned logs (8 files) - await the migration rewrite in wireRecordService.restore() so restore only resolves once the migrated log is durable and rewrite failures surface - serialize AppendLogStore.rewrite() behind the log flush so appends arriving during the whole-file replace land after the new content - reject wire logs missing the metadata envelope (missingWireMetadataError) in restore, session fork, and agent create instead of fabricating a current-version envelope over legacy records
- add IAgentTaskService.stopAllOnExit: suppress terminal notifications, then SIGTERM → grace → SIGKILL for every active task (v1 stopBackgroundTasksOnExit parity, gated by keepAliveOnExit) - call stopAllOnExit from agentLifecycle.remove before scope disposal, and abort live tasks in AgentTaskService.dispose as a last resort for disposal paths that bypass the graceful close - honor [task] killGracePeriodMs in the stop grace window and bind the v1 KIMI_CODE_BACKGROUND_KEEP_ALIVE_ON_EXIT env override for keepAliveOnExit
- persist task records under <sessionScope>/agents/<agentId>/tasks/ (v1's per-agent layout) so tasks written by older versions are found on resume - stop one agent's restore from loading, marking lost, or re-notifying another agent's tasks - add a per-agent isolation test covering loadFromDisk + reconcile
🦋 Changeset detectedLatest commit: 9fcca23 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: 942ad7c0f8
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ddbc96d52f
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 96cc1e4cde
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9bee9d83e8
ℹ️ 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".
|
@codex review |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubCodex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
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". |
Related Issue
No linked issue. This fixes three related
agent-core-v2durability, restore-compatibility, and task-lifecycle problems described below.Problem
flush()report success. Non-empty unversioned logs were also stamped as current instead of being rejected.What changed
1. Wire and AppendLog durability
AgentWireRecordService.restore()awaits migration rewrites and propagates rewrite failures. Agent create and session fork reject non-empty logs without a protocol metadata envelope; empty logs still receive normal metadata.AppendLogStore.rewrite(records)now has an explicit cutover contract:recordsreplaces history already durable before the call, while queued or in-flight appends remain an ordered live tail after the atomic replacement.flush()remains pending through the post-rewrite durable append.flush,read, orrewritecalls return the same error instead of guessing whether storage committed and risking duplicates.2. Background process teardown
AgentLifecycleService.remove()awaitsAgentTaskService.stopAllOnExit()before activity drain and scope disposal.killGracePeriodMs, then calls the task's force-stop callback when needed. ThekeepAliveOnExitopt-out skips task-manager teardown for independently living work such as processes.[background]values are merged beneath[task]; the environment-onlykeepAliveOnExitoverlay no longer hidesmaxRunningTasksorkillGracePeriodMs.3. Task persistence compatibility
<sessionDir>/agents/<id>/tasks/layout.Review follow-up
background.kill_grace_period_msin the mirrored English and Chinese configuration references.keepAliveOnExitin this fix is intentionally scoped to task-owned work that can live independently of the Session scope, such as a detached process. Session-scoped subagents still end with their Session lifecycle; this PR does not claim to detach a DI child scope from its owner.Verification
agent-core-v2: 236 files / 3288 tests passed with one worker.agent-core-v2build, typecheck, and domain lint passed (848 files).kap-servertypecheck passed.git diff --checkpassed.Checklist
gen-changesetsskill. (3 focusedpatchchangesets)gen-docsskill. (Updated mirrored EN/ZH config references)