diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index 6a72d8a6a7f58..16172dadb3c12 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -1871,6 +1871,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio effect.pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))), ); + /** + * Subagent threads work for their parent, so deleting the parent takes them too. They are found + * by their lineage, not the parent's subagent records: a provider can report a subagent's thread + * before its record. + */ + const liveSubagentThreadIds = (threadId: ThreadId) => + projectionStore + .listLiveSubagentThreadIds(threadId) + .pipe(Effect.mapError((cause) => new OrchestratorProjectionError({ threadId, cause }))); + const getProjectionWithPendingEvents = /* This helper runs for individual orchestration events; the enclosing operation owns the span. */ Effect.fnUntraced(function* ( @@ -15252,7 +15262,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio const dispatchOnce = Effect.fn("orchestrationV2.dispatch.once")(function* ( command: OrchestrationV2ServerCommand, - sideChatIds: ReadonlyArray, + cascadeIds: ReadonlyArray, ): Effect.fn.Return< { readonly events: ReadonlyArray; @@ -15371,13 +15381,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio return { events: [], effects: [] }; } } - // The dispatch wrapper holds each side chat's lock, so none can commit after this plan. + // The dispatch wrapper holds each cascaded thread's lock, so none can commit after this plan. const plans = [ yield* planDeletion(command), - ...(yield* Effect.forEach(sideChatIds, (threadId) => + ...(yield* Effect.forEach(cascadeIds, (threadId) => planDeletion({ ...command, - commandId: CommandId.make(`${command.commandId}:side-chat:${threadId}`), + commandId: CommandId.make(`${command.commandId}:cascade:${threadId}`), threadId, }), )), @@ -15392,7 +15402,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio const parentArchivedAt = (yield* findThread(command.threadId))?.archivedAt ?? null; yield* dispatchThreadMutation(command, events, effects); // Unarchive restores the side chats this parent's archive took, not ones archived before it. - for (const threadId of sideChatIds) { + for (const threadId of cascadeIds) { const sideChatArchivedAt = (yield* findThread(threadId))?.archivedAt ?? null; const follows = command.type === "thread.archive" @@ -15765,10 +15775,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio } satisfies OrchestratorV2DispatchResult; }); - /** `sideChatIds` are the live side chats a delete, archive, or unarchive cascades to, read under their locks. */ + /** `cascadeIds` are the live threads a delete, archive, or unarchive cascades to, read under their locks. */ const dispatchWithReceiptEffect = Effect.fn("orchestrationV2.dispatch.withReceipt")(function* ( command: OrchestrationV2ServerCommand, - sideChatIds: ReadonlyArray = [], + cascadeIds: ReadonlyArray = [], cancelledTask?: OrchestrationV2Subagent, ): Effect.fn.Return { yield* Effect.annotateCurrentSpan({ @@ -15822,7 +15832,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ); } - const plan = yield* dispatchOnce(command, sideChatIds).pipe( + const plan = yield* dispatchOnce(command, cascadeIds).pipe( Effect.tap((planned) => refuseAboveRuntimeModeCeiling(command, planned.events)), Effect.filterOrFail( (planned) => @@ -16057,6 +16067,30 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ), ); + // Breadth first, each thread locked before its side chats and subagent threads are read, so none + // can join after the read. Thread locks are not reentrant, so each thread is queued once. + const withDeletionCascade = Effect.fnUntraced(function* ( + command: Extract, + pending: ReadonlyArray, + locked: ReadonlyArray = [], + ): Effect.fn.Return { + const [threadId, ...rest] = pending; + if (threadId === undefined) return yield* dispatchWithReceiptEffect(command, locked.slice(1)); + return yield* threadDispatch.withLock( + threadId, + Effect.gen(function* () { + const known = new Set([...locked, ...pending]); + const members = new Set( + [ + ...(yield* liveSideChatIds(threadId)), + ...(yield* liveSubagentThreadIds(threadId)), + ].filter((id) => !known.has(id)), + ); + return yield* withDeletionCascade(command, [...rest, ...members], [...locked, threadId]); + }), + ); + }); + // dispatchWithReceiptEffect owns the dispatch span; this wrapper selects the thread locks. const dispatchWithReceipt = Effect.fnUntraced(function* (command: OrchestrationV2ServerCommand) { const dispatch = threadDispatch.withLock( @@ -16077,12 +16111,13 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ? dispatch : threadDispatch.withLock(command.sourceThreadId, dispatch); } - if ( - command.type === "thread.delete" || - command.type === "thread.archive" || - command.type === "thread.unarchive" - ) { - const cascade = threadDispatch.withLock( + if (command.type === "thread.delete") { + return yield* threadDispatch.withDeletionLock( + withDeletionCascade(command, [command.threadId]), + ); + } + if (command.type === "thread.archive" || command.type === "thread.unarchive") { + return yield* threadDispatch.withLock( command.threadId, liveSideChatIds(command.threadId).pipe( Effect.flatMap((sideChatIds) => @@ -16093,9 +16128,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ), ), ); - return yield* command.type === "thread.delete" - ? threadDispatch.withDeletionLock(cascade) - : cascade; } // Taken before the lineage read, so a message whose read yields keeps its // place ahead of later messages to the same thread. diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index b4e42493c35db..97f322b59a044 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -3719,6 +3719,97 @@ it.effect("lists every thread in the shell snapshot past SQLite's bound variable ); it.layer(layerTest)("ProjectionStoreV2", (it) => { + it.effect("lists a thread's undeleted subagent threads by lineage through their index", () => + Effect.gen(function* () { + const store = yield* ProjectionStore.ProjectionStoreV2; + const sql = yield* SqlClient.SqlClient; + const now = yield* DateTime.now; + const projectId = ProjectId.make("project:subagent-lineage"); + const parentId = ThreadId.make("thread:subagent-lineage:parent"); + const otherId = ThreadId.make("thread:subagent-lineage:other"); + const thread = ( + name: string, + parentThreadId: ThreadId | null, + relationshipToParent: "fork" | "subagent" | null, + archived = false, + ) => { + const id = ThreadId.make(`thread:subagent-lineage:${name}`); + return { + createdBy: "user" as const, + creationSource: "web" as const, + id, + projectId, + title: name, + providerInstanceId, + modelSelection, + runtimeMode: "full-access" as const, + interactionMode: "default" as const, + branch: null, + worktreePath: null, + activeProviderThreadId: null, + lineage: { parentThreadId, relationshipToParent, rootThreadId: parentThreadId ?? id }, + forkedFrom: null, + createdAt: now, + updatedAt: now, + archivedAt: archived ? now : null, + settledOverride: null, + settledAt: null, + lastVisitedAt: null, + deletedAt: null, + }; + }; + const threads = [ + thread("parent", null, null), + thread("other", null, null), + thread("live", parentId, "subagent"), + thread("archived", parentId, "subagent", true), + thread("deleted", parentId, "subagent"), + thread("fork", parentId, "fork"), + thread("other-child", otherId, "subagent"), + ]; + for (const payload of threads) { + yield* store.apply({ + id: EventId.make(`event:subagent-lineage:${payload.id}`), + type: "thread.created", + threadId: payload.id, + occurredAt: now, + payload, + }); + } + const deleted = threads.find((payload) => payload.title === "deleted")!; + yield* store.apply({ + id: EventId.make("event:subagent-lineage:deleted"), + type: "thread.deleted", + threadId: deleted.id, + occurredAt: now, + payload: { ...deleted, deletedAt: now }, + }); + + const traced = traceSqlStatements(); + assert.deepEqual( + [ + ...(yield* store + .listLiveSubagentThreadIds(parentId) + .pipe(Effect.withTracer(traced.tracer))), + ], + [ + ThreadId.make("thread:subagent-lineage:archived"), + ThreadId.make("thread:subagent-lineage:live"), + ], + ); + const query = traced.statements.find((statement) => + statement.includes("FROM orchestration_v2_projection_threads"), + ); + assert.isDefined(query); + assert.match( + (yield* sql.unsafe<{ readonly detail: string }>(`EXPLAIN QUERY PLAN ${query}`)) + .map((row) => row.detail) + .join("\n"), + /SEARCH orchestration_v2_projection_threads USING INDEX orchestration_v2_projection_threads_live_subagent_parent_idx/, + ); + }), + ); + // regression: #21969 keep corrupt plan JSON in the typed read error it.effect("returns a typed read error for malformed stored plan JSON", () => Effect.gen(function* () { diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 4a48997db5fc4..b06c6bffa3330 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -108,6 +108,7 @@ import { JAVASCRIPT_TRIM_WHITESPACE, } from "./deferredCommandMessage.ts"; import { readableSearchText } from "./messageSearchIndex.ts"; +import { LINEAGE_PARENT_THREAD_ID, LIVE_SUBAGENT_THREAD_CONDITION } from "./subagentThreadIndex.ts"; import { isFoldableWorkItem, isThreadHistoryUserTurn, @@ -568,6 +569,10 @@ export interface ProjectionStoreV2Shape { readonly listAppOwnedChildThreadIds: ( threadId: ThreadId, ) => Effect.Effect, ProjectionStoreV2Error>; + /** Undeleted threads whose lineage names this thread as their subagent parent, archived or not. */ + readonly listLiveSubagentThreadIds: ( + threadId: ThreadId, + ) => Effect.Effect, ProjectionStoreV2Error>; readonly hasPendingDelegatedCompletion: ( childThreadId: ThreadId, ) => Effect.Effect; @@ -6563,6 +6568,18 @@ export const layer: Layer.Layer = Effect.map((rows) => [...new Set(rows.map((row) => ThreadId.make(row.child_thread_id)))]), Effect.mapError(controlReadError(threadId)), ); + // Served by migration 101's partial index, which spells the same expression and predicate. + const listLiveSubagentThreadIds: ProjectionStoreV2Shape["listLiveSubagentThreadIds"] = ( + threadId, + ) => + sql<{ readonly thread_id: string }>`SELECT thread_id + FROM orchestration_v2_projection_threads + WHERE ${sql.literal(LINEAGE_PARENT_THREAD_ID)} = ${threadId} + AND ${sql.literal(LIVE_SUBAGENT_THREAD_CONDITION)} + ORDER BY thread_id`.pipe( + Effect.map((rows) => rows.map((row) => ThreadId.make(row.thread_id))), + Effect.mapError(controlReadError(threadId)), + ); const hasPendingDelegatedCompletion: ProjectionStoreV2Shape["hasPendingDelegatedCompletion"] = Effect.fn("ProjectionStore.hasPendingDelegatedCompletion")((childThreadId) => sql`SELECT 1 FROM orchestration_v2_projection_subagents AS task @@ -9659,6 +9676,7 @@ export const layer: Layer.Layer = getSubagentCount: readOnReader(getSubagentCount), getRunCount: readOnReader(getRunCount), listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds), + listLiveSubagentThreadIds: readOnReader(listLiveSubagentThreadIds), hasPendingDelegatedCompletion: readOnReader(hasPendingDelegatedCompletion), getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal), getTurnItem: readOnReader(getTurnItem), diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts index 7aa18f0956e39..15f191e861c07 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts @@ -2246,6 +2246,142 @@ layer("ProviderEventIngestorV2", (it) => { }), ); + // A provider can report a subagent's thread after its parent was deleted. Its checkout was + // renamed too, so the branch correction must not undo the delete. + it.effect("records a subagent thread of a deleted parent as deleted", () => + Effect.gen(function* () { + const { worktreePath, command } = yield* makeSharedWorktree; + yield* command(worktreePath, ["branch", "-m", "generated-branch"]); + const now = yield* DateTime.now; + const eventSink = yield* EventSink.EventSinkV2; + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const rootEvent = yield* threadCreatedEvent(now); + if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture"); + const childThreadId = idAllocator.derive.threadFromProviderThread({ + driver: CODEX_DRIVER, + nativeThreadId: "workflow-member-after-delete", + }); + const providerSessionId = yield* idAllocator.allocate.providerSession({ + providerInstanceId: modelSelection.instanceId, + threadId: rootEvent.threadId, + }); + yield* eventSink.write({ + events: [ + rootEvent, + { + ...rootEvent, + id: yield* idAllocator.allocate.event({ threadId: rootEvent.threadId }), + type: "thread.deleted", + payload: { ...rootEvent.payload, deletedAt: now, updatedAt: now }, + }, + ], + }); + + const stored = yield* ingestor.ingestNormalized({ + providerSessionId, + providerInstanceId: modelSelection.instanceId, + threadId: rootEvent.threadId, + event: { + type: "app_thread.created", + driver: CODEX_DRIVER, + appThread: { + ...rootEvent.payload, + id: childThreadId, + title: "Inspect checkout", + branch: "t3/abcd1234", + worktreePath, + activeProviderThreadId: null, + createdBy: "agent", + creationSource: "provider", + lineage: { + parentThreadId: rootEvent.threadId, + relationshipToParent: "subagent", + rootThreadId: rootEvent.threadId, + }, + }, + }, + }); + assert.deepEqual( + stored.map((event) => event.event.type), + ["thread.created", "thread.deleted"], + ); + assert.isNotNull((yield* projectionStore.getThread(childThreadId)).deletedAt); + }), + ); + + // A parent's delete holds the parent's lock while it reads the parent's subagent threads, so a + // subagent thread committed in between would stay live. + it.effect("holds a subagent thread's parent until the thread is committed", () => + Effect.gen(function* () { + const now = yield* DateTime.now; + const eventSink = yield* EventSink.EventSinkV2; + const threadCommands = yield* ThreadCommandExecutor.ThreadCommandExecutor; + const idAllocator = yield* IdAllocator.IdAllocatorV2; + const rootEvent = yield* threadCreatedEvent(now); + if (rootEvent.type !== "thread.created") throw new Error("Expected thread fixture"); + const childThreadId = idAllocator.derive.threadFromProviderThread({ + driver: CODEX_DRIVER, + nativeThreadId: "workflow-member-parent-lock", + }); + yield* eventSink.write({ events: [rootEvent] }); + const beforeCommit = yield* Deferred.make(); + const allowCommit = yield* Deferred.make(); + const ingestor = yield* ProviderEventIngestor.ProviderEventIngestorV2.pipe( + Effect.provide(Layer.fresh(ProviderEventIngestor.layer)), + Effect.provideService( + EventSink.EventSinkV2, + EventSink.EventSinkV2.of({ + ...eventSink, + write: (input) => + input.events.some((event) => event.threadId === childThreadId) + ? Deferred.succeed(beforeCommit, undefined).pipe( + Effect.andThen(Deferred.await(allowCommit)), + Effect.andThen(eventSink.write(input)), + ) + : eventSink.write(input), + }), + ), + ); + const creation = yield* ingestor + .ingestNormalized({ + providerSessionId: yield* idAllocator.allocate.providerSession({ + providerInstanceId: modelSelection.instanceId, + threadId: rootEvent.threadId, + }), + providerInstanceId: modelSelection.instanceId, + threadId: rootEvent.threadId, + event: { + type: "app_thread.created", + driver: CODEX_DRIVER, + appThread: { + ...rootEvent.payload, + id: childThreadId, + title: "Inspect checkout", + activeProviderThreadId: null, + createdBy: "agent", + creationSource: "provider", + lineage: { + parentThreadId: rootEvent.threadId, + relationshipToParent: "subagent", + rootThreadId: rootEvent.threadId, + }, + }, + }, + }) + .pipe(Effect.forkScoped); + yield* Deferred.await(beforeCommit); + assert.sameMembers( + [...(yield* threadCommands.activeKeys)], + [rootEvent.threadId, childThreadId], + ); + yield* Deferred.succeed(allowCommit, undefined); + yield* Fiber.join(creation); + assert.deepEqual([...(yield* threadCommands.activeKeys)], []); + }), + ); + it.effect( "recovers a missing Claude child once and preserves saved metadata on later recovery", () => diff --git a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts index 45ed0d1c78180..d23913045cc58 100644 --- a/apps/server/src/orchestration-v2/ProviderEventIngestor.ts +++ b/apps/server/src/orchestration-v2/ProviderEventIngestor.ts @@ -484,6 +484,40 @@ export const layer: Layer.Layer< ) return []; const now = yield* DateTime.now; + // The parent's delete took its subagent threads; one reported after it is born deleted. + const parentThreadId = offered.lineage.parentThreadId; + const parent = + existing === null && + offered.lineage.relationshipToParent === "subagent" && + parentThreadId !== null && + parentThreadId !== offered.id + ? yield* projections.getThread(parentThreadId).pipe( + Effect.catchTags({ + ProjectionStoreThreadNotFoundError: () => Effect.succeed(null), + }), + ) + : null; + if (parent !== null && parent.deletedAt !== null) { + return [ + yield* makeDomainEvent(input, { + type: "thread.created", + threadId: offered.id, + payload: offered, + occurredAt: now, + }), + yield* makeDomainEvent(input, { + type: "thread.deleted", + threadId: offered.id, + payload: { + ...offered, + deletedAt: now, + titleRegeneration: null, + updatedAt: now, + }, + occurredAt: now, + }), + ]; + } return [ yield* makeDomainEvent(input, { type: existing === null ? "thread.created" : "thread.metadata-updated", @@ -814,6 +848,9 @@ export const layer: Layer.Layer< (stored) => stored.event.type === "thread.created", ); if (created?.event.type !== "thread.created") return storedEvents; + // An update from the created payload would undo a thread born deleted. + if (storedEvents.some((stored) => stored.event.type === "thread.deleted")) + return storedEvents; const child = created.event.payload; if (child.worktreePath === null || child.branch === null) return storedEvents; // Git renames before scanning committed threads. If that scan missed this @@ -885,10 +922,18 @@ export const layer: Layer.Layer< }); }), ), - (effect) => - input.event.type === "app_thread.created" - ? threadCommands.withLock(input.event.appThread.id, effect) - : effect, + // A subagent thread's creation also holds its parent, parent first as a thread delete + // locks them, so the delete either finds the child or the child sees it deleted. + (effect) => { + if (input.event.type !== "app_thread.created") return effect; + const { id, lineage } = input.event.appThread; + const locked = threadCommands.withLock(id, effect); + return lineage.relationshipToParent === "subagent" && + lineage.parentThreadId !== null && + lineage.parentThreadId !== id + ? threadCommands.withLock(lineage.parentThreadId, locked) + : locked; + }, ), ), }); diff --git a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts index cb9550efe5799..ef36904447104 100644 --- a/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts +++ b/apps/server/src/orchestration-v2/ProviderTurnControlService.test.ts @@ -173,6 +173,7 @@ function layerProjectionStore( getSubagentCount: () => Effect.die("unused subagent count"), getRunCount: () => Effect.die("unused run count"), listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"), + listLiveSubagentThreadIds: () => Effect.die("unused listLiveSubagentThreadIds"), getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"), getTurnItem: () => Effect.die("unused turn item read"), getMessagesById: () => Effect.die("unused getMessagesById"), diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index f7e6e03bb460f..8a5b7513695a3 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -3,6 +3,7 @@ import { SourceControlProviderKind } from "@t3tools/contracts"; import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts"; import { createOrchestrationV2TurnItemVisibility } from "@t3tools/shared/orchestrationV2Timeline"; import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; +import { makeSubagentChildThread } from "@t3tools/provider-core/server/subagentProjection"; import * as SourceControlProviderRegistry from "../sourceControl/SourceControlProviderRegistry.ts"; import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; import * as NodeServices from "@effect/platform-node/NodeServices"; @@ -1082,6 +1083,138 @@ it.layer(layerProjectDeletionTest)("project deletion during thread commands", (i }), ); + const seedDelegatingParent = Effect.fn(function* (key: string) { + const projects = yield* ProjectService.ProjectService; + const orchestrator = yield* Orchestrator.OrchestratorV2; + const projectId = ProjectId.make(`${key}-project`); + const threadId = ThreadId.make(`${key}-parent`); + yield* projects.create({ + commandId: CommandId.make(`${key}-project`), + projectId, + title: key, + workspaceRoot: `/work/${key}`, + }); + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`${key}-create`), + threadId, + projectId, + title: key, + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + yield* orchestrator.dispatch({ + type: "message.dispatch", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`${key}-message`), + threadId, + messageId: MessageId.make(`${key}-message`), + text: "Delegate the work.", + attachments: [], + modelSelection, + dispatchMode: { type: "start_immediately" }, + }); + return { projectId, threadId }; + }); + + const delegate = Effect.fn(function* (parentThreadId: ThreadId, key: string) { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const run = (yield* orchestrator.getThreadProjection(parentThreadId)).runs[0]!; + yield* orchestrator.dispatch({ + type: "delegated_task.request", + createdBy: "agent", + creationSource: "mcp", + commandId: CommandId.make(`${key}-request`), + parentThreadId, + parentRunId: run.id, + parentNodeId: run.rootNodeId!, + task: `Work on ${key}.`, + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + completionWake: "always", + }); + return (yield* orchestrator.getThreadProjection(parentThreadId)).subagents[0]!; + }); + + it.effect("deletes a parent's subagent threads and theirs with it", () => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const { threadId } = yield* seedDelegatingParent("delete-subagents"); + const childThreadId = (yield* delegate(threadId, "delete-subagents-child")).childThreadId!; + const grandchildThreadId = (yield* delegate(childThreadId, "delete-subagents-grandchild")) + .childThreadId!; + const childRun = (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]; + assert.isDefined(childRun); + assert.isFalse(ThreadManagementService.isTerminalRunStatus(childRun.status), childRun.status); + + yield* orchestrator.dispatch({ + type: "thread.delete", + commandId: CommandId.make("delete-subagents-delete"), + threadId, + }); + for (const id of [threadId, childThreadId, grandchildThreadId]) { + assert.isNotNull((yield* orchestrator.getThreadProjection(id)).thread.deletedAt, id); + } + assert.equal( + (yield* orchestrator.getThreadProjection(childThreadId)).runs[0]?.status, + "cancelled", + ); + }), + ); + + // Codex reports a subagent's thread before the parent's subagent record, so a delete in between + // finds the child only by its lineage. An archived subagent thread is still its parent's. + it.effect("deletes subagent threads that have no subagent record on their parent", () => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const sink = yield* EventSink.EventSinkV2; + const { threadId } = yield* seedDelegatingParent("delete-subagent-unrecorded"); + const parent = yield* orchestrator.getThreadProjection(threadId); + const now = yield* DateTime.now; + const children = ["live", "archived"].map((state) => ({ + ...makeSubagentChildThread({ + parentThread: parent.thread, + childThreadId: ThreadId.make(`delete-subagent-unrecorded-${state}`), + parentNodeId: parent.runs[0]!.rootNodeId!, + activeProviderThreadId: null, + providerInstanceId: modelSelection.instanceId, + modelSelection, + title: `Unrecorded ${state} subagent`, + now, + createdBy: "agent", + creationSource: "provider", + }), + archivedAt: state === "archived" ? now : null, + })); + yield* sink.write({ + events: children.map((child) => ({ + id: EventId.make(`${child.id}-created`), + type: "thread.created" as const, + threadId: child.id, + occurredAt: now, + payload: child, + })), + }); + assert.lengthOf((yield* orchestrator.getThreadProjection(threadId)).subagents, 0); + + yield* orchestrator.dispatch({ + type: "thread.delete", + commandId: CommandId.make("delete-subagent-unrecorded-delete"), + threadId, + }); + for (const id of [threadId, ...children.map((child) => child.id)]) { + assert.isNotNull((yield* orchestrator.getThreadProjection(id)).thread.deletedAt, id); + } + }), + ); + // A fork from a side chat onto its parent used to lock the side chat, then the parent: the // reverse of the parent delete's order. it.effect("a parent delete and a fork from its side chat onto the parent do not deadlock", () => diff --git a/apps/server/src/orchestration-v2/subagentThreadIndex.ts b/apps/server/src/orchestration-v2/subagentThreadIndex.ts new file mode 100644 index 0000000000000..eb1e862906383 --- /dev/null +++ b/apps/server/src/orchestration-v2/subagentThreadIndex.ts @@ -0,0 +1,10 @@ +/** The parent a thread's lineage names; a malformed payload names none. */ +export const LINEAGE_PARENT_THREAD_ID = `CASE WHEN json_valid(payload_json) + THEN json_extract(payload_json, '$.lineage.parentThreadId') END`; + +/** + * A subagent thread that is not deleted, archived or not. The partial index on it is only used + * when the read spells this text the same way. + */ +export const LIVE_SUBAGENT_THREAD_CONDITION = `deleted_at IS NULL AND CASE WHEN json_valid(payload_json) + THEN json_extract(payload_json, '$.lineage.relationshipToParent') = 'subagent' ELSE 0 END`; diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index dcf435b670ca5..5371f6c4da028 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -108,6 +108,7 @@ import Migration0097 from "./Migrations/097_IssueTrackerBindings.ts"; import Migration0098 from "./Migrations/098_ActiveShellIndexIndexes.ts"; import Migration0099 from "./Migrations/099_BackgroundRecoveryIndexes.ts"; import Migration0100 from "./Migrations/100_TurnStartReadIndexes.ts"; +import Migration0101 from "./Migrations/101_SubagentThreadParentIndex.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -211,6 +212,7 @@ const migrationEntries = [ [98, "ActiveShellIndexIndexes", Migration0098], [99, "BackgroundRecoveryIndexes", Migration0099], [100, "TurnStartReadIndexes", Migration0100], + [101, "SubagentThreadParentIndex", Migration0101], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/101_SubagentThreadParentIndex.ts b/apps/server/src/persistence/Migrations/101_SubagentThreadParentIndex.ts new file mode 100644 index 0000000000000..347f53ed1ffbb --- /dev/null +++ b/apps/server/src/persistence/Migrations/101_SubagentThreadParentIndex.ts @@ -0,0 +1,20 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +import { + LINEAGE_PARENT_THREAD_ID, + LIVE_SUBAGENT_THREAD_CONDITION, +} from "../../orchestration-v2/subagentThreadIndex.ts"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // A thread delete finds its subagent threads by their lineage, including ones whose parent has + // no subagent record for them yet. ProjectionStore.listLiveSubagentThreadIds must spell the same + // expression and predicate. json_valid keeps a malformed payload writable. + yield* sql.unsafe(` + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_threads_live_subagent_parent_idx + ON orchestration_v2_projection_threads(${LINEAGE_PARENT_THREAD_ID}) + WHERE ${LIVE_SUBAGENT_THREAD_CONDITION} + `); +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index 982ea60a2b340..813cde66295c4 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -80,6 +80,7 @@ describe("V2 preview upgrade", () => { [98, "ActiveShellIndexIndexes"], [99, "BackgroundRecoveryIndexes"], [100, "TurnStartReadIndexes"], + [101, "SubagentThreadParentIndex"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -203,6 +204,7 @@ describe("V2 preview upgrade", () => { [98, "ActiveShellIndexIndexes"], [99, "BackgroundRecoveryIndexes"], [100, "TurnStartReadIndexes"], + [101, "SubagentThreadParentIndex"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), );