kumarabhirup a046cf2349
refactor(web): unify subagent tracking with parent session system
Subagents now use the same ActiveRun infrastructure as parent sessions:
- startSubscribeRun() creates a subscribe-only ActiveRun when sessions_spawn
  tool results are detected, using the same event buffering, persistence,
  and SSE reconnection as parent runs
- Stream/stop/chat routes no longer branch on subagent vs parent; both
  use getActiveRun/subscribeToRun with the session key as map key
- hasRunningSubagentsForParent moved into active-runs.ts to check the
  unified activeRuns map (+ disk registry fallback)
- Deferred finalization on lifecycle/end with 5s safety timeout
- ev.data.text fallback for assistant events without delta field
- 24h cleanup grace for subscribe-only runs (vs 30s for parent)

Reverts the broken childSessionKey registration from 32cfcf14f.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-22 00:35:07 -08:00

27 lines
864 B
TypeScript

/**
* POST /api/chat/stop
*
* Abort an active agent run. Called by the Stop button.
* Works for both parent sessions (by sessionId) and subagent sessions (by sessionKey).
*/
import { abortRun, getActiveRun } from "@/lib/active-runs";
export const runtime = "nodejs";
export async function POST(req: Request) {
const body: { sessionId?: string; sessionKey?: string } = await req
.json()
.catch(() => ({}));
const isSubagentSession = typeof body.sessionKey === "string" && body.sessionKey.includes(":subagent:");
const runKey = isSubagentSession && body.sessionKey ? body.sessionKey : body.sessionId;
if (!runKey) {
return new Response("sessionId or subagent sessionKey required", { status: 400 });
}
const run = getActiveRun(runKey);
const aborted = run?.status === "running" ? abortRun(runKey) : false;
return Response.json({ aborted });
}