-
Notifications
You must be signed in to change notification settings - Fork 918
fix(kap-server): keep foreground tasks out of REST polling #1644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0217112
fix(kap-server): carry the live subagent roster in the session snapshot
wbxl2000 2b55fc1
fix(kap-server): keep roster through child turn completion
wbxl2000 2bd6a8b
fix(kap-server): project run_in_background onto REST tasks
wbxl2000 b3ff5bd
fix(kap-server): keep foreground tasks out of polling
wbxl2000 6ed6743
Merge branch 'fix/kap-server-subagent-roster' into codex/fix-foregrou…
wbxl2000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| web: Fix swarm member lists disappearing after a page refresh on the v2 backend. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@moonshot-ai/kimi-code": patch | ||
| --- | ||
|
|
||
| web: Keep foreground subagents out of the dock's background-task list on the v2 backend, where they appeared as unstoppable entries. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
packages/kap-server/src/transport/ws/v1/subagentRosterTracker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /** | ||
| * `SubagentRosterTracker` — accumulates the per-session roster of live | ||
| * subagent tasks so a reconnecting client can rebuild swarm cards from the | ||
| * session snapshot. The refresh flow subscribes at the snapshot watermark, so | ||
| * earlier `subagent.spawned` events — the only carriers of the swarm identity | ||
| * metadata — are never replayed to it. | ||
| * | ||
| * Owned by the `SessionEventBroadcaster` and updated inside its per-session | ||
| * dispatch queue — same pattern as `InFlightTurnTracker`, keeping the roster, | ||
| * the journal watermark, and fan-out order mutually consistent. | ||
| * | ||
| * Lifetime: the roster is dropped on the main agent's `turn.ended`. After the | ||
| * main turn ends, the swarm's `<agent_swarm_result>` tool output is in the wire | ||
| * transcript and becomes the restore source; this also bounds the roster's | ||
| * lifetime (background subagents that outlive a turn are a known, pre-existing | ||
| * bound — same trade-off as `InFlightTurnTracker`). | ||
| */ | ||
|
|
||
| import { MAIN_AGENT_ID } from '@moonshot-ai/agent-core-v2'; | ||
| import type { Event, SnapshotSubagent } from '@moonshot-ai/protocol'; | ||
|
|
||
| export class SubagentRosterTracker { | ||
| private readonly bySession = new Map<string, Map<string, SnapshotSubagent>>(); | ||
|
|
||
| apply(sessionId: string, event: Event): void { | ||
| switch (event.type) { | ||
| case 'subagent.spawned': { | ||
| let roster = this.bySession.get(sessionId); | ||
| if (!roster) { | ||
| roster = new Map(); | ||
| this.bySession.set(sessionId, roster); | ||
| } | ||
| roster.set(event.subagentId, { | ||
| id: event.subagentId, | ||
| session_id: sessionId, | ||
| kind: 'subagent', | ||
| description: event.description ?? event.subagentName ?? 'Sub Agent', | ||
| status: 'running', | ||
| subagent_phase: 'queued', | ||
| subagent_type: event.subagentName, | ||
| parent_tool_call_id: event.parentToolCallId, | ||
| swarm_index: event.swarmIndex, | ||
| run_in_background: event.runInBackground, | ||
| created_at: new Date().toISOString(), | ||
| }); | ||
| return; | ||
| } | ||
| case 'subagent.started': { | ||
| const entry = this.bySession.get(sessionId)?.get(event.subagentId); | ||
| if (!entry) return; | ||
| entry.subagent_phase = 'working'; | ||
| entry.suspended_reason = undefined; | ||
| // Keep an existing started_at: a resumed (previously suspended) | ||
| // subagent re-fires `subagent.started`. | ||
| entry.started_at ??= new Date().toISOString(); | ||
| return; | ||
| } | ||
| case 'subagent.suspended': { | ||
| const entry = this.bySession.get(sessionId)?.get(event.subagentId); | ||
| if (!entry) return; | ||
| entry.subagent_phase = 'suspended'; | ||
| entry.suspended_reason = event.reason; | ||
| return; | ||
| } | ||
| case 'subagent.completed': { | ||
| const entry = this.bySession.get(sessionId)?.get(event.subagentId); | ||
| if (!entry) return; | ||
| entry.subagent_phase = 'completed'; | ||
| entry.status = 'completed'; | ||
| entry.completed_at = new Date().toISOString(); | ||
| entry.output_preview = event.resultSummary; | ||
| return; | ||
| } | ||
| case 'subagent.failed': { | ||
| const entry = this.bySession.get(sessionId)?.get(event.subagentId); | ||
| if (!entry) return; | ||
| entry.subagent_phase = 'failed'; | ||
| entry.status = 'failed'; | ||
| entry.completed_at = new Date().toISOString(); | ||
| entry.output_preview = event.error; | ||
| return; | ||
| } | ||
| case 'turn.ended': { | ||
| if (event.agentId !== MAIN_AGENT_ID) return; | ||
| // After turn end the swarm's `<agent_swarm_result>` tool output is in | ||
| // the wire transcript and becomes the restore source; dropping the | ||
| // roster here also bounds its lifetime. | ||
| this.bySession.delete(sessionId); | ||
| return; | ||
| } | ||
| default: | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| /** Fresh copies — callers must not mutate the tracked entries. */ | ||
| get(sessionId: string): SnapshotSubagent[] { | ||
| const roster = this.bySession.get(sessionId); | ||
| if (!roster) return []; | ||
| return Array.from(roster.values(), (entry) => ({ ...entry })); | ||
| } | ||
|
|
||
| clear(sessionId: string): void { | ||
| this.bySession.delete(sessionId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.