Skip to content

fix: stop individual swarm members from the web UI - #1645

Open
wbxl2000 wants to merge 4 commits into
mainfrom
codex/fix-swarm-member-stop
Open

fix: stop individual swarm members from the web UI#1645
wbxl2000 wants to merge 4 commits into
mainfrom
codex/fix-swarm-member-stop

Conversation

@wbxl2000

@wbxl2000 wbxl2000 commented Jul 13, 2026

Copy link
Copy Markdown
Collaborator

Related Issue

Related to #1623. Independent from the stacked foreground-polling PR #1648.

Problem

The web swarm card could display a running or suspended member but had no member-level stop action. The /api/v1 cancel route only understood task-service IDs, while AgentSwarm members are owned by the session swarm domain. Two additional lookup bugs made this worse: one caller could retain only its newest batch, and an old terminal Agent task could hide a resumed active swarm member with the same Agent ID. The route also assumed every swarm was launched by main, so members launched by another agent could not be stopped.

A later cancellation gap remained around the requester-side SubagentStart hook: stop could succeed while the only emitted event stayed subagent.started, leaving other clients and replay stuck on a running row.

What changed

The stop path is explicit and stays within existing domains:

  1. The web swarm row sends its Agent ID to POST /api/v1/sessions/{sid}/tasks/{id}:cancel and applies the cancelled terminal state.
  2. kap-server resolves an exact task ID first, then an active Agent-task alias, then asks the session swarm to stop by Agent ID, and only then uses terminal Agent-task history for idempotent responses. The REST boundary does not guess a caller.
  3. The session swarm service keeps every batch for each caller plus a session-wide Agent-ID-to-active-batch index. The batch records its real caller, so targeted stop aborts one active or suspended member and publishes cancellation to the correct caller; caller cancellation still aborts every owned batch. A bounded 128-entry LRU preserves recent terminal responses.
  4. The existing Agent lifecycle signal reaches Bash task cancellation and the node-local process-tree kill path; the real-process regression proves the selected child tree exits while an unrelated tree survives.
  5. mirrorAgentRun now sends exactly one cancelled failure whether stop lands before, during, or after the requester start hook. The early completion rejection is observed while a hook is pending, so cancellation does not become an unhandled rejection. This follow-up does not change stop lookup.

Explicit cancellation is carried as an optional subagent.failed.cancelled marker so refresh/replay and the live Web state agree. Repeated stop stays idempotent. This PR does not include the session-close/background cleanup work from #1635.

Verification

  • agent-core-v2: earlier full run 236 files / 3271 tests; current hook/swarm run 2 files / 104 tests; process-tree/Bash/task/session-init run 4 files / 107 tests; typecheck; lint:domain.
  • kap-server: test/tasks.test.ts, 16 tests passed, including a real non-main-caller /api/v1 cancel during a deferred start hook; typecheck.
  • protocol: 28 files / 519 tests passed; typecheck.
  • Web: 3 targeted files / 78 tests; typecheck; production build.
  • root lint passed.
  • Web check:style: baseline mode, 29 existing warnings, no failure.
  • targeted oxlint: 0 errors; git diff --check origin/main.
  • two read-only follow-up audits found no actionable issues or 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.
  • Ran gen-changesets; the existing web-stop-swarm-members CLI patch changeset covers this follow-up.
  • Ran the gen-docs impact check; no user guide change is needed. The skill's changelog sync prerequisite is absent in this checkout (docs/scripts/sync-changelog.mjs).

@wbxl2000

Copy link
Copy Markdown
Collaborator Author

@codex review

@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@1b7551d
npx https://pkg.pr.new/@moonshot-ai/kimi-code@1b7551d

commit: 1b7551d

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

ℹ️ 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/routes/tasks.ts Outdated
@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 1b7551d

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

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

ℹ️ 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/routes/tasks.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: 8678de1fe2

ℹ️ 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/agent-core-v2/src/session/agentLifecycle/mirrorAgentRun.ts Outdated
@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: 1b7551dd42

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

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