Skip to content

fix(kap-server): carry the live subagent roster in the session snapshot - #1623

Closed
wbxl2000 wants to merge 11 commits into
mainfrom
fix/kap-server-subagent-roster
Closed

fix(kap-server): carry the live subagent roster in the session snapshot#1623
wbxl2000 wants to merge 11 commits into
mainfrom
fix/kap-server-subagent-roster

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

No linked issue — the problem is explained below.

Problem

Refreshing the web UI while subagents are still running lost the AgentSwarm card's member list on the kap-server backend. The swarm identity metadata (description / parentToolCallId / swarmIndex) only rode the subagent.spawned WS event, which is not replayed after a refresh, and REST /tasks did not provide the live foreground swarm roster. The protocol already defines an optional subagents snapshot field, but kap-server did not populate it.

The initial roster implementation also had lifecycle gaps. A child agent's turn.ended could clear every sibling; Ctrl+B detach and its later terminal event were not reflected in the durable snapshot; and late ordinary lifecycle events could overwrite a terminal status. On the Web side, the client collapsed an omitted legacy field and an authoritative subagents: [] into the same value, so a resync could retain a stale running member after the main turn had cleared the roster. Detached Agent tasks also need their real task identity so REST and WS cannot create duplicate or mismatched cards.

What changed

  • Added a per-session live subagent roster to SessionEventBroadcaster, updated inside its existing dispatch queue.
  • Kept the full swarm identity and lifecycle fields needed to rebuild the Web card after reconnect, including the optional agent_id alongside a detached task's canonical id.
  • Cleared the roster only when the main agent's turn ends; a child agent's own turn.ended no longer clears sibling entries.
  • Folded Agent task.started and task.terminated into the matching member so detach and its final state survive refresh, with exact task binding preventing an old run from terminating a respawned run.
  • Made terminal state monotonic: ordinary subagent lifecycle and task.started only update a running member; task.terminated remains authoritative in either event order; only subagent.spawned starts a new run for the same agent.
  • Passed the optional subagents roster through both kap-server snapshot assembly paths.
  • Preserved optional-field presence in the Web client. An omitted roster remains compatible with older servers. A present roster, including [], removes stale foreground roster members while preserving Bash/tool tasks and REST-owned detached Agent tasks (run_in_background: true); a non-empty roster replaces unknown foreground members.
  • Seeded snapshot ownership into the raw-event projector so a detach event arriving after a refresh still adopts the task resource ID. A present roster clears omitted projector entries first; active aliases are the only cross-ID reducer match, terminal history is not reused for a resumed run, and stale task IDs cannot relabel the current member.
  • Kept a newly detached running row when an older in-flight REST poll omits its task ID, preventing a detach-before-response flicker while the next poll catches up.
  • Added multi-subagent, detach, respawn, terminal-ordering, snapshot, HTTP-mapping, REST/WS identity, omitted/present roster, stale REST ordering, and Web merge regressions through existing public surfaces.

The branch includes current main@5d1f9049c through an ordinary merge commit. This only refreshes the test/DI baseline; no downstream stop or foreground-task branch is copied into this PR. #1648 remains the separate stacked foreground REST projection change.

Verification

  • Current base-relative diff: 21 files, 1,887 insertions, 85 deletions (tests make up most of the increase).
  • kap-server roster/broadcaster/snapshot: 50 tests passed; kap-server typecheck.
  • Protocol snapshot tests: 8 passed; protocol typecheck.
  • Web targeted projector/reducer/merge/client tests: 137 tests passed.
  • Web full suite: 28 files / 515 tests passed; typecheck and production build passed.
  • Web check:style: baseline mode, 29 existing findings, no failure.
  • agent-core-v2 lint:domain: passed (848 files).
  • Root type-aware oxlint: 0 errors (existing warnings only); changed-file oxlint: 0 errors.
  • git diff --check: passed.
  • changeset status --since=origin/main: @moonshot-ai/kimi-code patch.
  • Ran gen-changesets and the gen-docs impact check. No user guide change is needed for restoring this existing behavior; the skill's changelog sync prerequisite is absent in this checkout (docs/scripts/sync-changelog.mjs), so no changelog sync was claimed.
  • Independent read-only lifecycle, Web contract, and public-text audits found no actionable issues or context-specific internal identifiers.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • I have run the required changeset and documentation impact checks.

@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5991a4b

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

Accumulate the per-session live subagent roster in the broadcaster's
dispatch queue and expose it via getSnapshotState, so the web client
can rebuild swarm cards after a page refresh. Both snapshot assembly
paths (SnapshotReader and the legacy resume path) pass it through as
the optional subagents field.
@wbxl2000
wbxl2000 force-pushed the fix/kap-server-subagent-roster branch from be6a123 to 0217112 Compare July 13, 2026 13:27

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

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

Comment thread packages/kap-server/src/transport/ws/v1/subagentRosterTracker.ts
@pkg-pr-new

pkg-pr-new Bot commented Jul 13, 2026

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

commit: 5991a4b

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 2b55fc120d

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

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 95b12c30d1

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

Comment thread packages/kap-server/src/transport/ws/v1/subagentRosterTracker.ts
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

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

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

Comment thread packages/kap-server/src/transport/ws/v1/subagentRosterTracker.ts
@wbxl2000

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: 4e9e4764ce

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

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 0552bbc38a

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

Comment thread packages/kap-server/src/transport/ws/v1/subagentRosterTracker.ts
Comment thread apps/kimi-web/src/lib/taskMerge.ts Outdated
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 8fae3d8fc9

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

Comment thread apps/kimi-web/src/api/daemon/eventReducer.ts Outdated
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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: 9c5e10015b

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

Comment thread apps/kimi-web/src/lib/taskMerge.ts Outdated
Comment thread apps/kimi-web/src/api/daemon/agentEventProjector.ts
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

2 similar comments
@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: d2d8e2f778

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

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

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

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

sessionId,
journal,
tracker: new InFlightTurnTracker(),
roster: new SubagentRosterTracker(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Seed the roster on first activation

When the first v1 WS/snapshot activation for an already-running session happens after subagent.spawned has fired (for example, a prompt was started via REST/TUI before the web UI subscribes), this initializes an empty roster and only then attaches the agent event listeners. The missed spawn metadata is not replayed into SubagentRosterTracker, and later lifecycle/progress events ignore unknown subagent IDs, so /snapshot still returns subagents: [] for the live swarm until a new spawn occurs. Seed the tracker from an already-active run or activate the broadcaster before prompt execution starts.

Useful? React with 👍 / 👎.

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