Skip to content

fix: restore swarm members from persisted lifecycle events - #3970

Merged
chengluyu merged 10 commits into
mainfrom
fix/restored-swarm-members
Sep 23, 2026
Merged

chengluyu merged 10 commits into
mainfrom
fix/restored-swarm-members

Conversation

@chengluyu

@chengluyu chengluyu commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

Related Issue

Reported during testing of the Swarm member list in the desktop client; no separate issue.

Problem

After restarting the server with a foreground Swarm still in progress, reopening the conversation shows no members. The child conversations remain on disk, but their spawn and completion events were transient, so the cold transcript cannot reconstruct the member tasks or their parent tool calls.

What changed

  • Persist subagent spawn, start, completion, failure, and cancellation events in the requesting conversation's wire journal, and register them in the wire record vocabulary and the generated wire manifest. The session inspector gained a renderer for each new record type.
  • Fold those facts into transcript tasks and tool-frame member references, ordered by the recorded Swarm index. Keep parent-conversation ownership explicit so inherited fork history does not create unrelated members. Restore the member's token usage from its completion record, so a reopened conversation matches what the client showed before the restart.
  • Reconcile a member whose task is registered after its spawn — the background worker path — by folding the provisional entry into the registered task instead of leaving a duplicate behind.
  • Drain the live conversation's wire before rebuilding from it. The append log writes in batches and its own reader flushes first; the cold transcript read the file directly, so a rebuild started right after a turn could stop on a stale prefix and keep a tool call without its result.
  • Show an unfinished foreground member as lost when its own conversation is not running and the turn that requested it is not the turn running now. A member left behind by a restart is therefore terminalised even when a new, unrelated turn has already started. Reading history does not start a session or restart work.

Existing conversations recorded before this fix do not contain the required association facts and cannot be reconstructed reliably. This change preserves new runs; it does not infer associations from child names or creation order.

Checklist

  • I have read the CONTRIBUTING document.
  • I have described the reported problem above (no separate issue).
  • I have added tests that prove the fix works.
  • Ran gen-changesets skill.
  • This PR needs no doc update.

Copilot AI balanced review requested due to automatic review settings September 21, 2026 13:46
@changeset-bot

changeset-bot Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fab6751

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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T17:28:14.280305Z fab6751 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2026 •

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

commit: fab6751

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Cold reconstruction can produce stale or duplicate task state and does not reliably identify members lost from earlier turns.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 3 Medium severity

Open (3)
What changed in this PR

Persists subagent lifecycle events so cold transcript reads can reconstruct Swarm members after restarts.

Changes:

  • Makes subagent lifecycle events durable and replayable.
  • Rebuilds member tasks/references and marks interrupted foreground members lost.
  • Adds persistence and cold-reconstruction coverage.
File Description
.changeset/​restore-swarm-members.md Records the user-visible fix.
packages/​agent-core-v2/​src/​session/​subagent/​mirrorAgentRun.ts Makes lifecycle events durable.
packages/​agent-core-v2/​test/​state/​eventDispatcher.test.ts Tests persistence and replay.
packages/​transcript/​src/​history/​foldFacts.ts Reconstructs tasks and member references.
packages/​transcript/​test/​layers.test.ts Tests lifecycle folding.
packages/​kap-server/​src/​services/​transcript/​transcriptService.ts Reconciles restored task liveness.
packages/​kap-server/​test/​services/​transcript.test.ts Tests cold Swarm restoration.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

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

ℹ️ 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".

…ycle records

The manifest inventory drives the vis wire-renderer coverage check, so vis gains a renderer for each new record type and treats them as timeline-neutral in the context projector.
… after its spawn

Some emitters register the task after publishing the spawn, so the durable
spawn record carries no task id and the cold fold keyed the member by its
agent id. A later task.started then created a second task that no subagent
terminal event ever reached, leaving a duplicate stuck in the running state.
Folding task.started now records the agent-to-task association the live
projector keeps, and terminal subagent records update both representations
when both are retained.
…d member

The durable subagent.completed record carries the member's token usage and the
live projector copies it onto the task, but the cold fold dropped it, so a
reopened conversation showed a completed member without usage metadata. The
fold now validates the payload's four counters and restores them.
…ed it

The cold snapshot only terminalised an unfinished foreground member when the
parent agent was idle, so a member left behind by a restart stayed marked as
running forever once the user started a new parent turn before backfill.
The restored agent refs already name the tool call that spawned each member,
so the decision now uses the turn owning that call: a member is lost when its
own loop is not running and its spawning turn is not the turn the parent is
running now.

@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: 1e1201cf02

ℹ️ 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".

… afterwards

A spawn published before its task is registered — the Tower worker path —
carries no task id, so the fold created a placeholder keyed by the agent id
and the later task.started added a second task under the registered id. Only
the registered task was ever terminalised, and the detached placeholder was
skipped by the lost-member pass, so a restored transcript kept a duplicate
worker shown as running forever. task.started now folds the placeholder into
the registered task instead of leaving it behind.
The append log buffers records and writes them in batches, which is why its
own reader flushes the log first. The cold transcript reads the wire file
directly and skipped that drain, so a snapshot could stop on a stale prefix.
When the transcript binding is created after a turn has already ended, the
whole store is built from that snapshot and nothing repairs it afterwards,
leaving the turn's last tool call without its result and its member marked
lost. Reading now drains the live agent's wire first, as session export
already does before it reads the same files.
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@chengluyu

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: fab67513f1

ℹ️ 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 merged commit 895e9d9 into main Sep 23, 2026
16 checks passed
@chengluyu
chengluyu deleted the fix/restored-swarm-members branch September 23, 2026 06:25
@github-actions github-actions Bot mentioned this pull request Sep 23, 2026
7723qqq pushed a commit to 7723qqq/kimi-code that referenced this pull request Oct 5, 2026
Step 9 of the incremental sync to 2.1.1: 24 upstream commits over 321
files. The step brings the TUI mode setting, the jump-to-bottom
indicator, click-to-toggle transcript folds, banner audience targeting
and the workspace-trust hardening.

Upstream's changes in this step, all absorbed:
- add a TUI mode setting for the fullscreen layout (MoonshotAI#4004)
- add a jump-to-bottom indicator and a steady status row (MoonshotAI#3989)
- toggle one transcript fold block with a click (MoonshotAI#3990)
- support audience targeting for startup banners (MoonshotAI#3868)
- harden workspace trust boundaries across git, file tools and the
  local web server (MoonshotAI#3964)
- turn off filesystem watch by default (MoonshotAI#3931)
- add the auto_session_title config option (MoonshotAI#3962)
- prepend a wall-time header to persisted tool results (MoonshotAI#3966)
- report enabled plugins and plugin toggles in telemetry (MoonshotAI#3963)
- include step timing and usage in cold-folded steps (MoonshotAI#3938)
- restore swarm members from persisted lifecycle events (MoonshotAI#3970)
- enforce tower protocol invariants and deny AgentSwarm (MoonshotAI#3976)
- inherit titleKind for default fork titles (MoonshotAI#3974)
- request offline_access in MCP OAuth when the authorization server
  advertises it (MoonshotAI#3979)
- cut CLI startup time and memory in the native binary and config reads
  (MoonshotAI#3975)
- avoid a stack overflow when caching large wire files (MoonshotAI#3957)
- reject non-ASCII mission titles and record token usage (MoonshotAI#3847)
- pick the credential slot and relay from the login region (MoonshotAI#3969)
- add the KIMI_CODE_REPEAT_BREAKER env switch (MoonshotAI#3995)
- update the kimi-webbridge plugin to the 2.0.11 skill (MoonshotAI#3961)
- restructure the PR template and add the write-pr and review-pr skills
  (MoonshotAI#3992)

Fork behaviour that had to survive:
- the TUI controller split: upstream's rewritten activity-pane logic
  (braille thinking/composing spinners, the shared loading-tip kind) was
  ported into the fork's ActivityPaneController, and KimiTUI keeps
  delegating to it
- the i18n layer: the new TUI mode setting, its picker, the restart
  notice and the settings entry all go through `t()` with en/zh entries
- the fork's node-local → host rename: the upstream test files that
  still imported `#/os/backends/node-local/hostFsService` now point at
  `#/os/backends/host/hostFsService`
- the fork's inline media reading: upstream's realpath guard for the
  media tool is covered by a ReadTool-based symlink-escape test
- the fork's removePlugin deletes the plugin root from disk, so the new
  toggle-telemetry fixture keeps its root inside the managed directory
- the Bun toolchain: the retired Node SEA chain stays retired

Verified: typecheck clean across every package and app; full suite
17831 passed / 0 failed / 81 skipped / 1 todo, plus kimi-inspect's own
115 tests; lint 0 errors.
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.

3 participants