Add MCP thread management and Codex turn mapping

- Add scoped thread list, read, send, wait, and interrupt tools
- Track creation provenance and map provider turns across adapters
- Centralize thread lifecycle operations in a management service
This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-08-14 11:22:52 +02:00
1 parent 34c6d5ea18
commit c8b20831e2
38 files changed
+2085 -48

No files matched your search

@@ -1,4 +1,4 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it } from "@effect/vitest";
import * as DateTime from "effect/DateTime";
import * as Schema from "effect/Schema";
@@ -89,6 +89,8 @@ describe("orchestration V2 contracts", () => {
it("decodes command and domain event shapes for command-to-projection tests", () => {
const command = decodeOrchestrationV2Command({
type: "message.dispatch",
createdBy: "user",
creationSource: "web",
commandId: "command-1",
threadId: "thread-1",
messageId: "message-1",
@@ -132,6 +134,8 @@ describe("orchestration V2 contracts", () => {
it("decodes app-owned delegated task commands", () => {
const command = decodeOrchestrationV2Command({
type: "delegated_task.request",
createdBy: "user",
creationSource: "web",
commandId: "command-delegate-1",
parentThreadId: "thread-parent-1",
parentRunId: "run-parent-1",
@@ -295,6 +299,8 @@ describe("orchestration V2 contracts", () => {
it("decodes thread projections with an ordered turn item rendering stream", () => {
const projection = Schema.decodeUnknownSync(OrchestrationV2ThreadProjection)({
thread: {
createdBy: "user",
creationSource: "web",
id: "thread-1",
projectId: "project-1",
title: "Thread",
+29 -2
View File
@@ -36,6 +36,23 @@ import {
RuntimeMode,
} from "./orchestration.ts";
export const OrchestrationV2Actor = Schema.Literals(["user", "agent", "system"]);
export type OrchestrationV2Actor = typeof OrchestrationV2Actor.Type;
export const OrchestrationV2CreationSource = Schema.Literals([
"web",
"mobile",
"mcp",
"provider",
"server",
]);
export type OrchestrationV2CreationSource = typeof OrchestrationV2CreationSource.Type;
const OrchestrationV2CreationFields = {
createdBy: OrchestrationV2Actor,
creationSource: OrchestrationV2CreationSource,
} as const;
export const OrchestrationV2NativeRefStrength = Schema.Literals(["strong", "weak", "none"]);
export type OrchestrationV2NativeRefStrength = typeof OrchestrationV2NativeRefStrength.Type;
@@ -125,7 +142,7 @@ export const OrchestrationV2ContextTransfer = Schema.Struct({
"superseded",
]),
resolution: Schema.NullOr(OrchestrationV2ContextTransferResolution),
createdBy: Schema.Literals(["user", "agent", "system"]),
createdBy: OrchestrationV2Actor,
error: Schema.NullOr(Schema.String),
createdAt: Schema.DateTimeUtc,
updatedAt: Schema.DateTimeUtc,
@@ -260,6 +277,7 @@ export const OrchestrationV2ProviderCapabilities = Schema.Struct({
export type OrchestrationV2ProviderCapabilities = typeof OrchestrationV2ProviderCapabilities.Type;
export const OrchestrationV2AppThread = Schema.Struct({
...OrchestrationV2CreationFields,
id: ThreadId,
projectId: ProjectId,
title: TrimmedNonEmptyString,
@@ -397,7 +415,7 @@ export const OrchestrationV2Subagent = Schema.Struct({
runId: Schema.NullOr(RunId),
parentNodeId: NodeId,
origin: Schema.Literals(["provider_native", "app_owned"]),
createdBy: Schema.Literals(["agent", "user", "system"]),
createdBy: OrchestrationV2Actor,
provider: ProviderKind,
providerThreadId: Schema.NullOr(ProviderThreadId),
childThreadId: Schema.NullOr(ThreadId),
@@ -540,6 +558,7 @@ export const OrchestrationV2RuntimeRequest = Schema.Struct({
export type OrchestrationV2RuntimeRequest = typeof OrchestrationV2RuntimeRequest.Type;
export const OrchestrationV2ConversationMessage = Schema.Struct({
...OrchestrationV2CreationFields,
id: MessageId,
threadId: ThreadId,
runId: Schema.NullOr(RunId),
@@ -675,6 +694,7 @@ export type OrchestrationV2WebSearchResult = typeof OrchestrationV2WebSearchResu
export const OrchestrationV2TurnItem = Schema.Union([
Schema.Struct({
...OrchestrationV2TurnItemBaseFields,
...OrchestrationV2CreationFields,
type: Schema.Literal("user_message"),
messageId: MessageId,
inputIntent: OrchestrationV2UserMessageInputIntent,
@@ -988,6 +1008,7 @@ export const OrchestrationV2ShellThreadStatus = Schema.Union([
export type OrchestrationV2ShellThreadStatus = typeof OrchestrationV2ShellThreadStatus.Type;
export const OrchestrationV2ThreadShell = Schema.Struct({
...OrchestrationV2CreationFields,
id: ThreadId,
projectId: ProjectId,
title: Schema.String,
@@ -1159,6 +1180,7 @@ const OrchestrationV2TurnItemJsonBaseFields = {
export const OrchestrationV2TurnItemJson = Schema.Union([
Schema.Struct({
...OrchestrationV2TurnItemJsonBaseFields,
...OrchestrationV2CreationFields,
type: Schema.Literal("user_message"),
messageId: MessageId,
inputIntent: OrchestrationV2UserMessageInputIntent,
@@ -1433,6 +1455,7 @@ export type OrchestrationV2StoredEventJson = typeof OrchestrationV2StoredEventJs
export const OrchestrationV2Command = Schema.Union([
Schema.Struct({
type: Schema.Literal("thread.create"),
...OrchestrationV2CreationFields,
commandId: CommandId,
threadId: ThreadId,
projectId: ProjectId,
@@ -1445,6 +1468,7 @@ export const OrchestrationV2Command = Schema.Union([
}),
Schema.Struct({
type: Schema.Literal("message.dispatch"),
...OrchestrationV2CreationFields,
commandId: CommandId,
threadId: ThreadId,
messageId: MessageId,
@@ -1496,6 +1520,7 @@ export const OrchestrationV2Command = Schema.Union([
}),
Schema.Struct({
type: Schema.Literal("thread.fork"),
...OrchestrationV2CreationFields,
commandId: CommandId,
sourceThreadId: ThreadId,
targetThreadId: ThreadId,
@@ -1505,6 +1530,7 @@ export const OrchestrationV2Command = Schema.Union([
}),
Schema.Struct({
type: Schema.Literal("thread.merge_back"),
...OrchestrationV2CreationFields,
commandId: CommandId,
sourceThreadId: ThreadId,
targetThreadId: ThreadId,
@@ -1513,6 +1539,7 @@ export const OrchestrationV2Command = Schema.Union([
}),
Schema.Struct({
type: Schema.Literal("delegated_task.request"),
...OrchestrationV2CreationFields,
commandId: CommandId,
parentThreadId: ThreadId,
parentRunId: RunId,
+57 -1
View File
@@ -1,15 +1,27 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it } from "@effect/vitest";
import * as Schema from "effect/Schema";
import {
OrchestratorMcpCreateThreadsInput,
OrchestratorMcpDelegateTaskInput,
OrchestratorMcpDelegateTaskResult,
OrchestratorMcpThreadInterruptInput,
OrchestratorMcpThreadListInput,
OrchestratorMcpThreadReadInput,
OrchestratorMcpThreadSendInput,
OrchestratorMcpThreadStartInput,
OrchestratorMcpThreadWaitInput,
} from "./orchestratorMcp.ts";
const decodeCreateThreadsInput = Schema.decodeUnknownSync(OrchestratorMcpCreateThreadsInput);
const decodeDelegateTaskInput = Schema.decodeUnknownSync(OrchestratorMcpDelegateTaskInput);
const decodeDelegateTaskResult = Schema.decodeUnknownSync(OrchestratorMcpDelegateTaskResult);
const decodeThreadInterruptInput = Schema.decodeUnknownSync(OrchestratorMcpThreadInterruptInput);
const decodeThreadListInput = Schema.decodeUnknownSync(OrchestratorMcpThreadListInput);
const decodeThreadReadInput = Schema.decodeUnknownSync(OrchestratorMcpThreadReadInput);
const decodeThreadSendInput = Schema.decodeUnknownSync(OrchestratorMcpThreadSendInput);
const decodeThreadStartInput = Schema.decodeUnknownSync(OrchestratorMcpThreadStartInput);
const decodeThreadWaitInput = Schema.decodeUnknownSync(OrchestratorMcpThreadWaitInput);
describe("orchestrator MCP contracts", () => {
it("decodes cross-provider delegated task requests and durable results", () => {
@@ -60,4 +72,48 @@ describe("orchestrator MCP contracts", () => {
expect(request.threads[0]?.prompt).toBeUndefined();
expect(request.threads[1]?.target?.driverKind).toBe("claudeAgent");
});
it("decodes project-scoped thread orchestration requests", () => {
expect(
decodeThreadStartInput({
prompt: "Run the first loop iteration.",
clientRequestId: "start-loop-1",
}).prompt,
).toBe("Run the first loop iteration.");
expect(
decodeThreadListInput({
statuses: ["running", "completed"],
includeSubagents: false,
limit: 25,
}).statuses,
).toEqual(["running", "completed"]);
expect(
decodeThreadReadInput({
threadId: "thread-loop-1",
view: "activity",
afterPosition: 10,
}).afterPosition,
).toBe(10);
expect(
decodeThreadSendInput({
threadId: "thread-loop-1",
message: "Continue with the next iteration.",
mode: "steer",
clientRequestId: "send-loop-2",
}).mode,
).toBe("steer");
expect(
decodeThreadWaitInput({
threadId: "thread-loop-1",
runId: "run-loop-2",
timeoutMs: 5_000,
}).runId,
).toBe("run-loop-2");
expect(
decodeThreadInterruptInput({
threadId: "thread-loop-1",
reason: "Loop converged.",
}).reason,
).toBe("Loop converged.");
});
});
+201
View File
@@ -2,12 +2,24 @@ import * as Schema from "effect/Schema";
import {
ContextTransferId,
IsoDateTime,
MessageId,
NodeId,
NonNegativeInt,
PositiveInt,
ProjectId,
RunId,
ThreadId,
TrimmedNonEmptyString,
TurnItemId,
} from "./baseSchemas.ts";
import { ProviderInteractionMode, RuntimeMode } from "./orchestration.ts";
import {
OrchestrationV2Actor,
OrchestrationV2CreationSource,
OrchestrationV2RunStatus,
OrchestrationV2TurnItemStatus,
} from "./orchestrationV2.ts";
import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
const OrchestratorMcpPrompt = TrimmedNonEmptyString.check(Schema.isMaxLength(120_000));
@@ -127,6 +139,8 @@ export const OrchestratorMcpCreatedThread = Schema.Struct({
runId: Schema.NullOr(RunId),
status: OrchestratorMcpCreatedThreadStatus,
title: Schema.String,
createdBy: OrchestrationV2Actor,
creationSource: OrchestrationV2CreationSource,
providerInstanceId: ProviderInstanceId,
model: Schema.String,
});
@@ -137,6 +151,187 @@ export const OrchestratorMcpCreateThreadsResult = Schema.Struct({
});
export type OrchestratorMcpCreateThreadsResult = typeof OrchestratorMcpCreateThreadsResult.Type;
export const OrchestratorMcpThreadStartInput = Schema.Struct({
prompt: OrchestratorMcpPrompt,
title: Schema.optional(OrchestratorMcpTitle),
target: Schema.optional(OrchestratorMcpTarget),
clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
runtimeMode: Schema.optional(OrchestratorMcpRuntimeMode),
interactionMode: Schema.optional(OrchestratorMcpInteractionMode),
});
export type OrchestratorMcpThreadStartInput = typeof OrchestratorMcpThreadStartInput.Type;
export const OrchestratorMcpThreadStatus = Schema.Union([
Schema.Literal("idle"),
OrchestrationV2RunStatus,
]);
export type OrchestratorMcpThreadStatus = typeof OrchestratorMcpThreadStatus.Type;
export const OrchestratorMcpThreadListInput = Schema.Struct({
statuses: Schema.optional(
Schema.Array(OrchestratorMcpThreadStatus).check(Schema.isMaxLength(10)),
),
titleContains: Schema.optional(TrimmedNonEmptyString.check(Schema.isMaxLength(256))),
includeSubagents: Schema.optional(Schema.Boolean),
cursor: Schema.optional(NonNegativeInt),
limit: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(100))),
});
export type OrchestratorMcpThreadListInput = typeof OrchestratorMcpThreadListInput.Type;
export const OrchestratorMcpThreadListItem = Schema.Struct({
threadId: ThreadId,
title: Schema.String,
createdBy: OrchestrationV2Actor,
creationSource: OrchestrationV2CreationSource,
status: OrchestratorMcpThreadStatus,
latestRunId: Schema.NullOr(RunId),
providerInstanceId: ProviderInstanceId,
model: Schema.String,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
parentThreadId: Schema.NullOr(ThreadId),
relationshipToParent: Schema.NullOr(Schema.Literals(["fork", "subagent"])),
itemCount: NonNegativeInt,
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export type OrchestratorMcpThreadListItem = typeof OrchestratorMcpThreadListItem.Type;
export const OrchestratorMcpThreadListResult = Schema.Struct({
projectId: ProjectId,
currentThreadId: ThreadId,
threads: Schema.Array(OrchestratorMcpThreadListItem),
nextCursor: Schema.NullOr(NonNegativeInt),
total: NonNegativeInt,
});
export type OrchestratorMcpThreadListResult = typeof OrchestratorMcpThreadListResult.Type;
export const OrchestratorMcpThreadReadInput = Schema.Struct({
threadId: ThreadId,
view: Schema.optional(Schema.Literals(["messages", "activity"])),
afterPosition: Schema.optional(NonNegativeInt),
limit: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(100))),
runLimit: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(50))),
maxCharsPerItem: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(50_000))),
});
export type OrchestratorMcpThreadReadInput = typeof OrchestratorMcpThreadReadInput.Type;
export const OrchestratorMcpThreadDetail = Schema.Struct({
threadId: ThreadId,
projectId: ProjectId,
title: Schema.String,
createdBy: OrchestrationV2Actor,
creationSource: OrchestrationV2CreationSource,
status: OrchestratorMcpThreadStatus,
latestRunId: Schema.NullOr(RunId),
activeRunId: Schema.NullOr(RunId),
providerInstanceId: ProviderInstanceId,
model: Schema.String,
runtimeMode: RuntimeMode,
interactionMode: ProviderInteractionMode,
branch: Schema.NullOr(Schema.String),
worktreePath: Schema.NullOr(Schema.String),
parentThreadId: Schema.NullOr(ThreadId),
relationshipToParent: Schema.NullOr(Schema.Literals(["fork", "subagent"])),
runCount: NonNegativeInt,
itemCount: NonNegativeInt,
pendingRequestCount: NonNegativeInt,
archived: Schema.Boolean,
createdAt: IsoDateTime,
updatedAt: IsoDateTime,
});
export type OrchestratorMcpThreadDetail = typeof OrchestratorMcpThreadDetail.Type;
export const OrchestratorMcpThreadRun = Schema.Struct({
runId: RunId,
ordinal: PositiveInt,
status: OrchestrationV2RunStatus,
providerInstanceId: ProviderInstanceId,
model: Schema.String,
requestedAt: IsoDateTime,
startedAt: Schema.NullOr(IsoDateTime),
completedAt: Schema.NullOr(IsoDateTime),
});
export type OrchestratorMcpThreadRun = typeof OrchestratorMcpThreadRun.Type;
export const OrchestratorMcpThreadTimelineItem = Schema.Struct({
position: NonNegativeInt,
visibility: Schema.Literals(["local", "inherited", "synthetic"]),
sourceThreadId: ThreadId,
itemId: TurnItemId,
runId: Schema.NullOr(RunId),
messageId: Schema.NullOr(MessageId),
createdBy: Schema.NullOr(OrchestrationV2Actor),
creationSource: Schema.NullOr(OrchestrationV2CreationSource),
type: Schema.String,
status: OrchestrationV2TurnItemStatus,
title: Schema.NullOr(Schema.String),
text: Schema.NullOr(Schema.String),
textTruncated: Schema.Boolean,
updatedAt: IsoDateTime,
});
export type OrchestratorMcpThreadTimelineItem = typeof OrchestratorMcpThreadTimelineItem.Type;
export const OrchestratorMcpThreadReadResult = Schema.Struct({
thread: OrchestratorMcpThreadDetail,
recentRuns: Schema.Array(OrchestratorMcpThreadRun),
items: Schema.Array(OrchestratorMcpThreadTimelineItem),
nextPosition: Schema.NullOr(NonNegativeInt),
hasMore: Schema.Boolean,
});
export type OrchestratorMcpThreadReadResult = typeof OrchestratorMcpThreadReadResult.Type;
export const OrchestratorMcpThreadSendInput = Schema.Struct({
threadId: ThreadId,
message: OrchestratorMcpPrompt,
mode: Schema.optional(Schema.Literals(["auto", "queue", "steer", "restart"])),
clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
});
export type OrchestratorMcpThreadSendInput = typeof OrchestratorMcpThreadSendInput.Type;
export const OrchestratorMcpThreadSendResult = Schema.Struct({
threadId: ThreadId,
messageId: MessageId,
runId: RunId,
status: OrchestrationV2RunStatus,
delivery: Schema.Literals(["started", "queued", "steered", "restarted"]),
});
export type OrchestratorMcpThreadSendResult = typeof OrchestratorMcpThreadSendResult.Type;
export const OrchestratorMcpThreadWaitInput = Schema.Struct({
threadId: ThreadId,
runId: Schema.optional(RunId),
timeoutMs: Schema.optional(Schema.Number),
});
export type OrchestratorMcpThreadWaitInput = typeof OrchestratorMcpThreadWaitInput.Type;
export const OrchestratorMcpThreadWaitResult = Schema.Struct({
threadId: ThreadId,
runId: Schema.NullOr(RunId),
status: OrchestratorMcpThreadStatus,
timedOut: Schema.Boolean,
});
export type OrchestratorMcpThreadWaitResult = typeof OrchestratorMcpThreadWaitResult.Type;
export const OrchestratorMcpThreadInterruptInput = Schema.Struct({
threadId: ThreadId,
runId: Schema.optional(RunId),
reason: Schema.optional(Schema.String.check(Schema.isMaxLength(2_000))),
clientRequestId: Schema.optional(OrchestratorMcpClientRequestId),
});
export type OrchestratorMcpThreadInterruptInput = typeof OrchestratorMcpThreadInterruptInput.Type;
export const OrchestratorMcpThreadInterruptResult = Schema.Struct({
threadId: ThreadId,
runId: Schema.NullOr(RunId),
status: Schema.Union([
Schema.Literal("interrupt_requested"),
Schema.Literal("no_active_run"),
Schema.Literals(["completed", "failed", "cancelled", "interrupted", "rolled_back"]),
]),
});
export type OrchestratorMcpThreadInterruptResult = typeof OrchestratorMcpThreadInterruptResult.Type;
export const OrchestratorMcpProviderCapability = Schema.Struct({
providerInstanceId: ProviderInstanceId,
driverKind: ProviderDriverKind,
@@ -165,6 +360,8 @@ export const OrchestratorMcpCapabilitiesResult = Schema.Struct({
asyncPolling: Schema.Boolean,
cancellation: Schema.Boolean,
batchThreadCreation: Schema.Boolean,
threadManagement: Schema.Boolean,
incrementalThreadRead: Schema.Boolean,
maxBatchThreads: Schema.Number,
}),
});
@@ -182,6 +379,10 @@ export class OrchestratorMcpFailure extends Schema.TaggedErrorClass<Orchestrator
"interaction_mode_escalation_denied",
"task_not_found",
"task_not_cancellable",
"thread_not_found",
"run_not_found",
"thread_not_sendable",
"thread_not_interruptible",
"invalid_request",
"orchestration_error",
]),