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
66 changes: 49 additions & 17 deletions apps/server/src/orchestration-v2/Orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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* (
Expand Down Expand Up @@ -15252,7 +15262,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio

const dispatchOnce = Effect.fn("orchestrationV2.dispatch.once")(function* (
command: OrchestrationV2ServerCommand,
sideChatIds: ReadonlyArray<ThreadId>,
cascadeIds: ReadonlyArray<ThreadId>,
): Effect.fn.Return<
{
readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
Expand Down Expand Up @@ -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,
}),
)),
Expand All @@ -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"
Expand Down Expand Up @@ -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<ThreadId> = [],
cascadeIds: ReadonlyArray<ThreadId> = [],
cancelledTask?: OrchestrationV2Subagent,
): Effect.fn.Return<OrchestratorV2DispatchResult, OrchestratorV2Error> {
yield* Effect.annotateCurrentSpan({
Expand Down Expand Up @@ -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) =>
Expand Down Expand Up @@ -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<OrchestrationV2ServerCommand, { readonly type: "thread.delete" }>,
pending: ReadonlyArray<ThreadId>,
locked: ReadonlyArray<ThreadId> = [],
): Effect.fn.Return<OrchestratorV2DispatchResult, OrchestratorV2Error> {
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(
Expand All @@ -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) =>
Expand All @@ -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.
Expand Down
91 changes: 91 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand Down
18 changes: 18 additions & 0 deletions apps/server/src/orchestration-v2/ProjectionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -568,6 +569,10 @@ export interface ProjectionStoreV2Shape {
readonly listAppOwnedChildThreadIds: (
threadId: ThreadId,
) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
/** Undeleted threads whose lineage names this thread as their subagent parent, archived or not. */
readonly listLiveSubagentThreadIds: (
threadId: ThreadId,
) => Effect.Effect<ReadonlyArray<ThreadId>, ProjectionStoreV2Error>;
readonly hasPendingDelegatedCompletion: (
childThreadId: ThreadId,
) => Effect.Effect<boolean, ProjectionStoreV2Error>;
Expand Down Expand Up @@ -6563,6 +6568,18 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
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
Expand Down Expand Up @@ -9659,6 +9676,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
getSubagentCount: readOnReader(getSubagentCount),
getRunCount: readOnReader(getRunCount),
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
listLiveSubagentThreadIds: readOnReader(listLiveSubagentThreadIds),
hasPendingDelegatedCompletion: readOnReader(hasPendingDelegatedCompletion),
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
getTurnItem: readOnReader(getTurnItem),
Expand Down
Loading
Loading