diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index 4a7b08fbd870b..196280f658f27 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -266,6 +266,14 @@ it("readThread preserves caller access errors and prefers activity-run status ov }); expect(laterPage.thread.pullRequests).toBeUndefined(); expect(laterPage.thread.pullRequestCount).toBe(8); + expect(result.recentRuns.length).toBeGreaterThan(0); + expect(laterPage.recentRuns).toEqual([]); + const laterPageWithRuns = yield* service.readThread(makeScope(), { + threadId: parentThreadId, + afterPosition: 0, + runLimit: 1, + }); + expect(laterPageWithRuns.recentRuns).toHaveLength(1); Object.assign(projection, { runs: [makeRun({ id: activeRunId, ordinal: 1, status: "completed" })], diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 6c488afe7d989..6828db13a761b 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -10,6 +10,7 @@ import { NodeId, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT, type OrchestratorMcpCapabilitiesResult, + type OrchestratorMcpThreadListResult, type OrchestratorMcpListScheduledTasksInput, type Project, OrchestratorMcpTarget, @@ -4970,6 +4971,32 @@ describe("OrchestratorMcpService thread list", () => { }).pipe(Effect.provide(service)), ); + it.effect( + "keeps an allProjects page within an MCP client's output limit and pages the rest", + () => + Effect.gen(function* () { + const mcp = yield* OrchestratorMcpService.OrchestratorMcpService; + const extra = Array.from({ length: 100 }, (_, index) => + shell(`thread:c-${index}`, "project:c", false), + ); + shells.push(...extra); + const listed: Array = []; + let cursor: number | null = 0; + while (cursor !== null) { + const page: OrchestratorMcpThreadListResult = yield* mcp.listThreads(client, { + allProjects: true, + limit: 100, + cursor, + }); + // Claude Code saves MCP results over 25k tokens to a file instead of showing them. + assert.isAtMost(Buffer.byteLength(JSON.stringify(page)), 30_000); + listed.push(...page.threads.map((thread) => thread.threadId)); + cursor = page.nextCursor; + } + assert.equal(listed.length, shells.length); + }).pipe(Effect.ensuring(Effect.sync(() => shells.splice(4))), Effect.provide(service)), + ); + it.effect("points a caller outside any thread at allProjects when it omits projectId", () => Effect.gen(function* () { const mcp = yield* OrchestratorMcpService.OrchestratorMcpService; diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 092d9f3241065..78d0f163031aa 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -159,7 +159,7 @@ const DEFAULT_THREAD_ITEM_MAX_CHARS = 500; const THREAD_PULL_REQUEST_PREVIEW_LIMIT = 5; // Claude Code saves MCP results over ~25k tokens to a file instead of showing them. Bytes, not // chars: CJK text runs near one token per char, which a char budget lets through at 30k tokens. -const THREAD_READ_RESULT_MAX_BYTES = 30_000; +const THREAD_RESULT_MAX_BYTES = 30_000; interface ResolvedTarget { readonly modelSelection: ModelSelection; @@ -1229,7 +1229,7 @@ function timelineItemWithin( /** * The items, read from the end the caller pages toward, whose result fits - * THREAD_READ_RESULT_MAX_BYTES; `start` is the first kept index. Never empty for a non-empty page. + * THREAD_RESULT_MAX_BYTES; `start` is the first kept index. Never empty for a non-empty page. */ function timelineItemsWithinBudget( envelope: Omit, @@ -1238,8 +1238,8 @@ function timelineItemsWithinBudget( ): { readonly items: Array; readonly start: number } { // An oversized envelope (say, a huge thread title) must not starve items to one char per read. const budget = Math.max( - THREAD_READ_RESULT_MAX_BYTES - jsonBytes({ ...envelope, items: [] }), - THREAD_READ_RESULT_MAX_BYTES / 2, + THREAD_RESULT_MAX_BYTES - jsonBytes({ ...envelope, items: [] }), + THREAD_RESULT_MAX_BYTES / 2, ); const items: Array = []; let bytes = 0; @@ -3325,14 +3325,27 @@ const make = Effect.gen(function* () { total = filtered.length; page = filtered.slice(cursor, cursor + limit); } - const nextCursor = cursor + limit < total ? cursor + limit : null; - return { + const envelope = { projectId, currentThreadId: parent?.thread.id ?? null, - threads: page.map((shell) => listItemFromShell(scope, shell, nowMs)), - nextCursor, + threads: [], + nextCursor: total, total, } satisfies OrchestratorMcpThreadListResult; + const threads: Array = []; + let bytes = jsonBytes(envelope); + for (const shell of page) { + const thread = listItemFromShell(scope, shell, nowMs); + bytes += jsonBytes(thread) + (threads.length === 0 ? 0 : 1); + if (threads.length > 0 && bytes > THREAD_RESULT_MAX_BYTES) break; + threads.push(thread); + } + const end = cursor + threads.length; + return { + ...envelope, + threads, + nextCursor: end < total ? end : null, + } satisfies OrchestratorMcpThreadListResult; }), readThread: Effect.fn("OrchestratorMcpService.readThread")(function* (scope, input) { const runLimit = input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT; @@ -3356,6 +3369,7 @@ const make = Effect.gen(function* () { : {}), limit, view, + ...(input.author === undefined ? {} : { author: input.author }), ...(input.itemId === undefined ? {} : { itemId: input.itemId }), }) .pipe(Effect.mapError(threadManagementFailure)); @@ -3376,6 +3390,10 @@ const make = Effect.gen(function* () { { concurrency: 1 }, ); const messagesByThreadId = new Map(sourceMessages); + const firstPage = + input.afterPosition === undefined && + input.beforePosition === undefined && + input.itemId === undefined; const thread = threadDetail( scope, target, @@ -3383,13 +3401,12 @@ const make = Effect.gen(function* () { timeline.totalItems, shell, yield* Clock.currentTimeMillis, - input.afterPosition === undefined && - input.beforePosition === undefined && - input.itemId === undefined, + firstPage, ); + // Later pages repeat the first page's runs unless the caller asks for them. const recentRuns = target.runs .toSorted((left, right) => right.ordinal - left.ordinal) - .slice(0, runLimit) + .slice(0, firstPage || input.runLimit !== undefined ? runLimit : 0) .map((run) => threadRun(target, run)); const { items, start } = timelineItemsWithinBudget( { thread, recentRuns, nextPosition: null, hasMore: false, earlierPosition: null }, diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 2c667bf43679f..46175e1970c13 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -6277,7 +6277,7 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }; const userMessage = (inputIntent: string) => add("user_message", { - createdBy: "user", + createdBy: items.length % 3 === 0 ? "agent" : "user", creationSource: "web", messageId: `message:window-equivalence:${round}:${items.length}`, inputIntent, @@ -6455,17 +6455,36 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { for (let cursor: string | null = groupCursor; cursor !== null;) cursor = (yield* windowed(history(cursor, anchorId, false, "work"))).nextCursor; } - for (const view of ["messages", "activity"] as const) { + const authors = { user: ["user_message"], agent: ["assistant_message", "proposed_plan"] }; + let authored = 0; + for (const filter of [ + { view: "messages" }, + { view: "activity" }, + { author: "user" }, + { author: "agent" }, + ] as const) { + const read = Effect.fnUntraced(function* ( + options: ProjectionStore.ProjectionTimelinePageOptions, + ) { + const { items } = yield* windowed(timeline({ ...filter, ...options })); + if (!("author" in filter)) return; + authored += items.length; + for (const { item } of items) { + assert.include(authors[filter.author], item.type); + if (item.type === "user_message") assert.equal(item.createdBy, "user"); + } + }); for (const afterPosition of [-1, Math.floor(totalItems / 3), totalItems - 5]) - yield* windowed(timeline({ view, afterPosition, limit: 20 })); + yield* read({ afterPosition, limit: 20 }); for (const beforePosition of [ Number.POSITIVE_INFINITY, Math.floor(totalItems / 3), totalItems - 5, ]) - yield* windowed(timeline({ view, beforePosition, limit: 20 })); - yield* windowed(timeline({ view, afterPosition: 3, beforePosition: 40, limit: 20 })); + yield* read({ beforePosition, limit: 20 }); + yield* read({ afterPosition: 3, beforePosition: 40, limit: 20 }); } + assert.isAbove(authored, 0); for (const itemId of [newest.sourceItemId, ...tiedWithHidden]) yield* windowed(timeline({ itemId, limit: 1 })); @@ -10227,6 +10246,31 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { assert.deepEqual(earlierPage.items, targetAfterRollback.visibleTurnItems.slice(0, -2)); assert.isTrue(earlierPage.hasMore); assert.isFalse(earlierPage.hasEarlier); + // The source reads its own rows; the fork reads the full index. + const authorPage = ( + threadId: ThreadId, + author: "user" | "agent", + options: Partial = {}, + ) => + projectionStore + .getTimelinePage(threadId, { author, limit: 10, ...options }) + .pipe(Effect.map((page) => page.items.map((row) => [row.position, row.item.type]))); + assert.deepEqual(yield* authorPage(targetThreadId, "user"), [ + [0, "user_message"], + [2, "user_message"], + ]); + assert.deepEqual(yield* authorPage(targetThreadId, "agent", { view: "activity" }), [ + [1, "assistant_message"], + [3, "assistant_message"], + ]); + assert.deepEqual(yield* authorPage(sourceThreadId, "user"), [[0, "user_message"]]); + assert.deepEqual(yield* authorPage(sourceThreadId, "agent", { afterPosition: 0 }), [ + [1, "assistant_message"], + ]); + assert.deepEqual( + yield* authorPage(sourceThreadId, "user", { beforePosition: Number.POSITIVE_INFINITY }), + [[0, "user_message"]], + ); const tracedTimelinePage = (limit: number) => Effect.gen(function* () { const { statements, tracer } = traceSqlStatements(); diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.ts index 5b8a636cc24f1..0be78a58c160e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.ts @@ -440,8 +440,22 @@ export interface ProjectionTimelinePageOptions { readonly itemId?: TurnItemId; readonly attachmentId?: string; readonly view?: "messages" | "activity"; + /** Only this author's messages, in either view. */ + readonly author?: "user" | "agent"; readonly limit: number; } + +/** The item types a timeline page matches; null matches every type. */ +const timelineViewTypes = ( + options: ProjectionTimelinePageOptions, +): ReadonlyArray | null => + options.author === "user" + ? ["user_message"] + : options.author === "agent" + ? ["assistant_message", "proposed_plan"] + : options.view === "activity" + ? null + : ["user_message", "assistant_message", "proposed_plan"]; export interface ProjectionTimelinePage { readonly items: ReadonlyArray; readonly totalItems: number; @@ -7564,11 +7578,16 @@ export const layer: Layer.Layer = if (start === undefined) return { matching: [], totalItems, hasEarlier: false }; const base = yield* local.visibleBefore(start); const hiddenAfter = local.hidden.filter((row) => row.ordinal >= start).length; - const messagesOnly = options.view !== "activity"; + const types = timelineViewTypes(options); + const messagesOnly = types !== null; + // Another agent's t3_thread_send is a user_message too. + const byUser = + options.author === "user" + ? sql`AND json_extract(item.payload_json, '$.createdBy') = 'user'` + : sql``; const candidates = (yield* local.read( messagesOnly - ? sql`AND item.ordinal >= ${start} - AND item.type IN ('user_message', 'assistant_message', 'proposed_plan')` + ? sql`AND item.ordinal >= ${start} AND item.type IN ${sql.in(types)} ${byUser}` : sql`AND item.ordinal >= ${start}`, sql`LIMIT ${firstPosition - base + Math.min(options.limit, totalItems) + 1 + hiddenAfter}`, )).filter(local.isVisible); @@ -7596,8 +7615,7 @@ export const layer: Layer.Layer = (base > 0 && (!messagesOnly || (yield* local.read( - sql`AND item.ordinal < ${start} - AND item.type IN ('user_message', 'assistant_message', 'proposed_plan')`, + sql`AND item.ordinal < ${start} AND item.type IN ${sql.in(types)} ${byUser}`, sql`LIMIT ${local.hidden.length + 1}`, )).some(local.isVisible))); return { matching, totalItems, hasEarlier }; @@ -7680,12 +7698,13 @@ export const layer: Layer.Layer = (row) => `${row.thread_id}:${row.turn_item_id}`, ), ); + const types = timelineViewTypes(options); const inView = index.visible .map((row, position) => ({ ...row, position })) .filter((row) => options.itemId === undefined - ? options.view === "activity" || - ["user_message", "assistant_message", "proposed_plan"].includes(row.item.type) + ? (types === null || types.includes(row.item.type)) && + (options.author !== "user" || row.item.createdBy === "user") : row.sourceItemId === options.itemId, ) .filter( diff --git a/apps/server/src/provider/agentContextBudget.json b/apps/server/src/provider/agentContextBudget.json index 9c305c266f3dd..25fdadacd5dba 100644 --- a/apps/server/src/provider/agentContextBudget.json +++ b/apps/server/src/provider/agentContextBudget.json @@ -1,5 +1,5 @@ { "claudeSystemAppendBytes": 5930, "codexAdditionalContextBytes": 7324, - "mcpToolListJsonBytes": 52145 + "mcpToolListJsonBytes": 52196 } diff --git a/docs/orchestration-v2/orchestrator-mcp-server.md b/docs/orchestration-v2/orchestrator-mcp-server.md index fa2770bb10cf7..bd78e377712b1 100644 --- a/docs/orchestration-v2/orchestrator-mcp-server.md +++ b/docs/orchestration-v2/orchestrator-mcp-server.md @@ -368,7 +368,10 @@ Reads the durable state, recent runs, and visible timeline of any thread in the environment by thread ID. A deleted thread returns `thread_not_found`. The default `messages` view returns user messages, assistant messages, and proposed plans. The `activity` view also returns summarized tool, -reasoning, checkpoint, handoff, and runtime-request items. Large item text is +reasoning, checkpoint, handoff, and runtime-request items. `author: "user"` returns +only messages a user wrote, not ones another agent sent, and `author: "agent"` +only assistant messages and plans. Later pages leave out `recentRuns` unless the +read passes `runLimit`. Large item text is bounded and reports whether it was truncated. An agent in a T3 thread that reads without a cursor gets the newest items, and `beforePosition=earlierPosition` reads older ones. Client callers (the CLI, linked environments, outside MCP diff --git a/packages/contracts/src/orchestratorMcp.ts b/packages/contracts/src/orchestratorMcp.ts index ed6a62bf1eb7f..a7e28efcccfcc 100644 --- a/packages/contracts/src/orchestratorMcp.ts +++ b/packages/contracts/src/orchestratorMcp.ts @@ -446,6 +446,7 @@ export const OrchestratorMcpThreadReadInput = Schema.Struct({ itemId: Schema.optional(TurnItemId), textOffset: Schema.optional(NonNegativeInt), view: Schema.optional(Schema.Literals(["messages", "activity"])), + author: Schema.optional(Schema.Literals(["user", "agent"])), afterPosition: Schema.optional(Schema.Int.check(Schema.isGreaterThanOrEqualTo(-1))), beforePosition: Schema.optional(NonNegativeInt), limit: Schema.optional(clampedPositiveInt(100)).annotate({ description: "Max 100." }),