Skip to content
Merged
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
65 changes: 65 additions & 0 deletions apps/server/src/mcp/OrchestratorMcpService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,71 @@ describe("OrchestratorMcpService thread lookup", () => {
);
});

describe("OrchestratorMcpService thread list", () => {
const client: McpInvocationScope = {
environmentId: EnvironmentId.make("environment:thread-list"),
requestNamespace: "client:thread-list",
thread: undefined,
client: { sessionId: "thread-list", label: "Claude Code", access: "full-access" },
capabilities: new Set(["orchestration"]),
issuedAt: 1,
};
const shell = (id: string, projectId: string, snoozed: boolean) => ({
...liveThreadShell(ThreadId.make(id)),
projectId: ProjectId.make(projectId),
status: "completed" as const,
snoozedUntil: snoozed ? DateTime.makeUnsafe("2099-01-01T00:00:00Z") : null,
});
const shells = [
shell("thread:a-snoozed", "project:a", true),
shell("thread:a-awake", "project:a", false),
shell("thread:b-snoozed", "project:b", true),
];
const service = layerOrchestratorService.pipe(
Layer.provide(
Layer.mergeAll(
NodeServices.layer,
Layer.mock(ThreadManagementService.ThreadManagementService)({
listProjectThreads: (input) =>
Effect.succeed(
shells.filter(
(thread) => input.projectId === undefined || thread.projectId === input.projectId,
),
),
}),
ServerSettings.layerTest(),
Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
list: () => Effect.succeed([]),
}),
Layer.mock(ProjectService.ProjectService)({}),
Layer.mock(SecretRequests.SecretRequests)({}),
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
),
),
);

it.effect("lists snoozed threads across every project with allProjects", () =>
Effect.gen(function* () {
const mcp = yield* OrchestratorMcpService.OrchestratorMcpService;
const listed = yield* mcp.listThreads(client, {
allProjects: true,
projectId: ProjectId.make("project:a"),
snoozed: true,
});
assert.equal(listed.projectId, null);
assert.deepStrictEqual(
listed.threads.map((thread) => [thread.threadId, thread.projectId]),
[
["thread:a-snoozed", "project:a"],
["thread:b-snoozed", "project:b"],
],
);
}).pipe(Effect.provide(service)),
);
});

describe("OrchestratorMcpService thread stop", () => {
const driver = ProviderDriverKind.make("codex");
const instanceId = ProviderInstanceId.make("codex");
Expand Down
7 changes: 5 additions & 2 deletions apps/server/src/mcp/OrchestratorMcpService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,7 @@ function listItemFromShell(
): OrchestratorMcpThreadListItem {
return {
threadId: shell.id,
projectId: shell.projectId,
title: shell.title,
link: formatThreadLink({ environmentId, threadId: shell.id }),
createdBy: shell.createdBy,
Expand Down Expand Up @@ -2764,10 +2765,12 @@ const make = Effect.gen(function* () {
),
listThreads: Effect.fn("OrchestratorMcpService.listThreads")(function* (scope, input) {
const { parent } = yield* loadCaller(scope);
const projectId = yield* resolveProjectTarget(parent, input.projectId);
const projectId = input.allProjects
? null
: yield* resolveProjectTarget(parent, input.projectId);
const projectThreads = yield* threadManagement
.listProjectThreads({
projectId,
projectId: projectId ?? undefined,
includeSubagents: input.includeSubagents !== false,
})
.pipe(
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/mcp/toolkits/orchestrator/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const CreateThreadsTool = Tool.make("create_threads", {

const ThreadListTool = Tool.make("t3_thread_list", {
description:
"List threads in a project, newest first. Omit `projectId` for yours. Filter by status, title, settled, or snoozed. Page with the cursor.",
"List threads newest first, in one project or with `allProjects`. Page with `cursor`.",
parameters: OrchestratorMcpThreadListInput,
success: OrchestratorMcpThreadListResult,
failure: OrchestratorMcpFailure,
Expand Down
56 changes: 32 additions & 24 deletions apps/server/src/orchestration-v2/ThreadManagementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,14 @@ export class ThreadManagementProjectionLoadError extends Schema.TaggedError<Thre
export class ThreadManagementProjectThreadsListError extends Schema.TaggedError<ThreadManagementProjectThreadsListError>()(
"ThreadManagementProjectThreadsListError",
{
projectId: ProjectId,
projectId: Schema.optional(ProjectId),
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Unable to list threads in project ${this.projectId}.`;
return this.projectId === undefined
? "Unable to list threads."
: `Unable to list threads in project ${this.projectId}.`;
}
}

Expand Down Expand Up @@ -404,8 +406,9 @@ export interface ThreadManagementServiceShape {
Orchestrator.OrchestratorV2Error
>;
readonly getThreadShell: Orchestrator.OrchestratorV2["Service"]["getThreadShell"];
/** Omit `projectId` to list every project's threads. */
readonly listProjectThreads: (input: {
readonly projectId: ProjectId;
readonly projectId?: ProjectId | undefined;
readonly includeSubagents: boolean;
}) => Effect.Effect<ReadonlyArray<OrchestrationV2ThreadShell>, ThreadManagementError>;
readonly sendToThread: (
Expand Down Expand Up @@ -736,27 +739,32 @@ const make = Effect.gen(function* () {
);

const listProjectThreads: ThreadManagementServiceShape["listProjectThreads"] = (input) =>
orchestrator.getShellSnapshot({ projectId: input.projectId, location: "active" }).pipe(
Effect.mapError(
(cause) =>
new ThreadManagementProjectThreadsListError({
projectId: input.projectId,
cause,
}),
),
Effect.map((snapshot) =>
snapshot.threads
.filter(
(thread) =>
input.includeSubagents || thread.lineage.relationshipToParent !== "subagent",
)
.toSorted(
(left, right) =>
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
right.id.localeCompare(left.id),
),
),
);
orchestrator
.getShellSnapshot({
location: "active",
...(input.projectId === undefined ? {} : { projectId: input.projectId }),
})
.pipe(
Effect.mapError(
(cause) =>
new ThreadManagementProjectThreadsListError({
projectId: input.projectId,
cause,
}),
),
Effect.map((snapshot) =>
snapshot.threads
.filter(
(thread) =>
input.includeSubagents || thread.lineage.relationshipToParent !== "subagent",
)
.toSorted(
(left, right) =>
DateTime.toEpochMillis(right.updatedAt) - DateTime.toEpochMillis(left.updatedAt) ||
right.id.localeCompare(left.id),
),
),
);

const sendToThread: ThreadManagementServiceShape["sendToThread"] = Effect.fn(
"ThreadManagementService.sendToThread",
Expand Down
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 5135,
"codexAdditionalContextBytes": 5687,
"mcpToolListJsonBytes": 45861
"mcpToolListJsonBytes": 45842
}
5 changes: 4 additions & 1 deletion packages/contracts/src/orchestratorMcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ export const OrchestratorMcpEnvironmentTarget = Schema.optional(
export const OrchestratorMcpThreadListInput = Schema.Struct({
environmentId: OrchestratorMcpEnvironmentTarget,
projectId: OrchestratorMcpProjectTarget,
allProjects: Schema.optional(Schema.Boolean),
statuses: Schema.optional(
Schema.Array(OrchestratorMcpThreadStatus).check(Schema.isMaxLength(10)),
),
Expand All @@ -353,6 +354,7 @@ export type OrchestratorMcpThreadPullRequest = typeof OrchestratorMcpThreadPullR

export const OrchestratorMcpThreadListItem = Schema.Struct({
threadId: ThreadId,
projectId: ProjectId,
title: Schema.String,
link: Schema.optional(Schema.String),
createdBy: OrchestrationV2Actor,
Expand All @@ -378,7 +380,8 @@ export const OrchestratorMcpThreadListItem = Schema.Struct({
export type OrchestratorMcpThreadListItem = typeof OrchestratorMcpThreadListItem.Type;

export const OrchestratorMcpThreadListResult = Schema.Struct({
projectId: ProjectId,
/** null with `allProjects`. */
projectId: Schema.NullOr(ProjectId),
currentThreadId: Schema.NullOr(ThreadId),
threads: Schema.Array(OrchestratorMcpThreadListItem),
nextCursor: Schema.NullOr(NonNegativeInt),
Expand Down
Loading