Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/coding-agent/src/cli/daemon-ps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -934,8 +934,11 @@ async function forceStopTrackedWorkers(
const failures: string[] = [];
for (const worker of findTrackedWorkers(supervisorSocketPath)) {
const { descriptor } = worker;
const pid = descriptor.pid!;
let cleanupWorkerRecords = await stopTrackedProcess(pid, descriptor.processStartId, assertAdmission);
const process = descriptor.process;
// Legacy and processless descriptors are display/recovery evidence, never a kill target.
if (!process) continue;
const { pid, processStartId } = process;
let cleanupWorkerRecords = await stopTrackedProcess(pid, processStartId, assertAdmission);
if (!cleanupWorkerRecords) {
failures.push(`could not safely stop worker ${descriptor.workerId} (pid ${pid})`);
}
Expand Down Expand Up @@ -1034,9 +1037,11 @@ function isTrackedWorkerDescriptor(value: unknown): value is DaemonWorkerDescrip
descriptor.lifecycle !== "passivated" &&
typeof descriptor.supervisorSocketPath === "string" &&
typeof descriptor.workerId === "string" &&
Number.isInteger(descriptor.pid) &&
(descriptor.pid ?? 0) > 0 &&
(descriptor.processStartId === undefined || typeof descriptor.processStartId === "string") &&
!!descriptor.process &&
Number.isInteger(descriptor.process.pid) &&
descriptor.process.pid > 0 &&
typeof descriptor.process.processStartId === "string" &&
!!descriptor.process.processStartId &&
typeof descriptor.socketPath === "string" &&
typeof descriptor.recoveryJournalPath === "string"
);
Expand Down
40 changes: 31 additions & 9 deletions packages/coding-agent/src/core/agent-session-runtime.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { randomUUID } from "node:crypto";
import { copyFileSync, existsSync, mkdirSync } from "node:fs";
import { basename, join, resolve } from "node:path";
import type { AgentSession } from "./agent-session.js";
Expand Down Expand Up @@ -56,6 +57,10 @@ export interface AgentSessionRuntimeMetadata {
parentSessionId?: string;
parentSessionFile?: string;
rlmChildId?: string;
/** Daemon worker incarnation; absent for legacy and inline top-level runtimes. */
generation?: string;
/** Required for C01-created subagent runtimes; internal only. */
assignmentId?: string;
rlmParentNodeId?: string;
/** Runtime restored from an already-persisted completed registry entry. */
rehydratedCompleted?: boolean;
Expand Down Expand Up @@ -90,6 +95,8 @@ export class AgentSessionRuntime implements SubagentRuntimeHost {
private beforeSessionInvalidate?: () => void;
private subagentRuntimeHost?: SubagentRuntimeHost;
private subagentRuntimes = new Map<string, AgentSessionRuntime>();
/** Assignment currently owning each compatibility child-id map entry. */
private subagentRuntimeAssignments = new Map<string, string>();
private disposePromise?: Promise<void>;

constructor(
Expand Down Expand Up @@ -298,6 +305,7 @@ export class AgentSessionRuntime implements SubagentRuntimeHost {
private async disposeSubagentRuntimes(): Promise<void> {
const runtimes = [...this.subagentRuntimes.values()];
this.subagentRuntimes.clear();
this.subagentRuntimeAssignments.clear();
let disposeError: unknown;
for (const runtime of runtimes) {
try {
Expand Down Expand Up @@ -340,6 +348,7 @@ export class AgentSessionRuntime implements SubagentRuntimeHost {
rlmDepth: options.rlmDepth,
});
}
const assignmentId = options.assignmentId ?? randomUUID();
const runtime = await this.scopedBuild(() =>
createAgentSessionRuntime(this.createRuntime, {
cwd: sessionManager.getCwd(),
Expand Down Expand Up @@ -369,6 +378,7 @@ export class AgentSessionRuntime implements SubagentRuntimeHost {
parentSessionId: options.parentSession.sessionId,
parentSessionFile: options.parentSession.sessionFile,
rlmChildId: options.id,
assignmentId: assignmentId,
rlmParentNodeId: options.rlmParentNodeId,
prompt: options.prompt,
spawnCode: options.spawnCode,
Expand All @@ -377,37 +387,49 @@ export class AgentSessionRuntime implements SubagentRuntimeHost {
}),
);
this.subagentRuntimes.set(options.id, runtime);
this.subagentRuntimeAssignments.set(options.id, assignmentId);
try {
await runtime.session.bindExtensions({});
if (options.parentSession.getRlmChildRunStatus(options.id) === "cancelled") {
if (
this.subagentRuntimeAssignments.get(options.id) !== assignmentId ||
options.parentSession.getRlmChildRunStatus(options.id) === "cancelled"
) {
throw new Error("RLM subagent startup was cancelled");
}
if (runtime.session.sessionName !== options.sessionName) {
runtime.session.setSessionName(options.sessionName);
}
options.onSessionPublished?.(runtime.session);
} catch (error) {
this.subagentRuntimes.delete(options.id);
if (
this.subagentRuntimes.get(options.id) === runtime &&
this.subagentRuntimeAssignments.get(options.id) === assignmentId
) {
this.subagentRuntimes.delete(options.id);
this.subagentRuntimeAssignments.delete(options.id);
}
await runtime.dispose();
throw error;
}
return runtime;
}

async deleteRlmSubagentRuntime(childId: string, session: AgentSession): Promise<void> {
async deleteRlmSubagentRuntime(childId: string, childSession?: AgentSession, assignmentId?: string): Promise<void> {
const runtime = this.subagentRuntimes.get(childId);
if (!runtime) {
await session.disposeAsync();
const currentAssignment = this.subagentRuntimeAssignments.get(childId);
// Inline runtimes have no durable daemon registry. Preserve direct delete
// compatibility, but a named C01 assignment fences stale callbacks.
if (!runtime || (assignmentId !== undefined && currentAssignment !== assignmentId)) {
await childSession?.disposeAsync();
return;
}
this.subagentRuntimes.delete(childId);
const shouldDisposeStaleSession = runtime.session !== session;
this.subagentRuntimeAssignments.delete(childId);
const shouldDisposeStaleSession = !!childSession && runtime.session !== childSession;
try {
await runtime.dispose();
} finally {
if (shouldDisposeStaleSession) {
await session.disposeAsync();
}
if (shouldDisposeStaleSession) await childSession?.disposeAsync();
}
}

Expand Down
Loading