From bd77701c9e379467a0b97f1e6e28e6e396e428f7 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 22:38:41 +0000 Subject: [PATCH 1/4] test(server): an agent's cursorless t3_thread_read on a linked environment reads the newest page Fails on main: the forwarded read reaches the linked server without a thread scope, so it pages from the oldest item. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/peer/PeerForwarding.test.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/server/src/peer/PeerForwarding.test.ts b/apps/server/src/peer/PeerForwarding.test.ts index a70c0d25792b8..895a9d5a8e80f 100644 --- a/apps/server/src/peer/PeerForwarding.test.ts +++ b/apps/server/src/peer/PeerForwarding.test.ts @@ -25,6 +25,7 @@ import { McpSchema, McpServer } from "effect/ai"; import * as ServerConfig from "../config.ts"; import * as ThreadLaunch from "../orchestration-v2/ThreadLaunchService.ts"; import * as ThreadManagement from "../orchestration-v2/ThreadManagementService.ts"; +import type { ProjectionTimelinePageOptions } from "../orchestration-v2/ProjectionStore.ts"; import * as ThreadSearch from "../orchestration-v2/ThreadSearch.ts"; import * as ManagedProjectFolders from "../project/ManagedProjectFolders.ts"; import * as SourceControlRepositoryService from "../sourceControl/SourceControlRepositoryService.ts"; @@ -60,6 +61,7 @@ interface Seen { readonly clones: ReadonlyArray; /** Commands dispatched and delegated-task stops, as ` `. */ readonly stops: ReadonlyArray; + readonly timelineReads: ReadonlyArray; } const nothingSeen: Seen = { sends: [], @@ -68,6 +70,7 @@ const nothingSeen: Seen = { projects: [], clones: [], stops: [], + timelineReads: [], }; /** The box user's own thread, which no link started. */ @@ -96,6 +99,12 @@ const boxThreads = (seen: Ref.Ref, linkSession: Ref.Ref) => { getThreadShell: shellOf, getProjectThreadRecords: (input) => projectionOf(input.threadId), getThreadRecords: (threadId) => projectionOf(threadId), + getRunCount: () => Effect.succeed(1), + getTimelinePage: (_threadId, options) => + Ref.update(seen, (current) => ({ + ...current, + timelineReads: [...current.timelineReads, options], + })).pipe(Effect.as({ items: [], totalItems: 0 } as never)), listProjectThreads: () => linked.pipe(Effect.map((shell) => [shell] as never)), getShellSnapshot: () => linked.pipe(Effect.map((shell) => ({ threads: [shell] }) as never)), sendToThread: (input) => @@ -395,6 +404,16 @@ it.effect("an agent here works on a linked environment's threads within its own ], }); + // An agent's cursorless read there gets the newest page, as it does here. + yield* a.call("thread:laptop-full", "t3_thread_read", { + environmentId: box.environmentId, + threadId: boxThread, + limit: 35, + }); + expect((yield* Ref.get(seen)).timelineReads).toMatchObject([ + { beforePosition: expect.any(Number), limit: 35 }, + ]); + const threads = yield* a.call("thread:laptop-full", "t3_thread_list", { environmentId: box.environmentId, projectId: boxProject, From de9ceedf42c9327d86135ee91a662579556f2cfa Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 22:46:57 +0000 Subject: [PATCH 2/4] fix(server): an agent's t3_thread_read on a linked environment starts at the newest page A read forwarded over a link arrives as a client call with no thread, so a cursorless read paged from the oldest item. The linked server now treats a read made for a linked environment's thread (T3-Sender-Thread) as an agent read and serves the newest page. The delegated-task result scan, which pages forward, asks for the start explicitly. Fixes pingdotgg/t3code#39932 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/OrchestratorMcpService.ts | 6 ++++-- apps/server/src/peer/RemoteDelegation.test.ts | 2 +- apps/server/src/peer/RemoteDelegation.ts | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 36cd8caa5708b..4d8be06b8d9e0 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3508,10 +3508,12 @@ const make = Effect.gen(function* () { const view = input.view ?? "messages"; const limit = input.limit ?? DEFAULT_THREAD_READ_LIMIT; const maxChars = input.maxCharsPerItem ?? DEFAULT_THREAD_ITEM_MAX_CHARS; - // Client callers (the CLI, older linked environments) page forward from a cursorless read. + // Agents, here or on a linked environment, read newest first. Other clients (the CLI) + // page forward from a cursorless read. const newestFirst = input.beforePosition !== undefined || - (input.afterPosition === undefined && scope.thread !== undefined); + (input.afterPosition === undefined && + (scope.thread !== undefined || scope.client?.senderThread !== undefined)); const timeline = yield* threadManagement .getTimelinePage(input.threadId, { ...(input.afterPosition === undefined ? {} : { afterPosition: input.afterPosition }), diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts index d4af55cae0313..3c14fb427fc2e 100644 --- a/apps/server/src/peer/RemoteDelegation.test.ts +++ b/apps/server/src/peer/RemoteDelegation.test.ts @@ -1833,7 +1833,7 @@ it.effect.each([ const pages = reads.filter((read) => read.itemId === undefined && read.limit === 100); // The thread's final page starts at its canonical 100-item boundary. const tailPosition = Math.floor((timeline.length - 1) / 100) * 100 - 1; - expect(pages.at(0)?.afterPosition).toBeUndefined(); + expect(pages.at(0)?.afterPosition).toBe(-1); expect( pages.every( (read) => diff --git a/apps/server/src/peer/RemoteDelegation.ts b/apps/server/src/peer/RemoteDelegation.ts index 6e48aa5fa61fe..a612f930ee59c 100644 --- a/apps/server/src/peer/RemoteDelegation.ts +++ b/apps/server/src/peer/RemoteDelegation.ts @@ -632,7 +632,7 @@ const make = Effect.gen(function* () { // Keep the first and expected final activity pages whole. A result on a // middle page then needs an extra text read after the scan. maxCharsPerItem: failed && remainingItems > 100 ? 1 : 20_000, - ...(afterPosition === undefined ? {} : { afterPosition }), + afterPosition: afterPosition ?? -1, }); run ??= read.recentRuns.find((candidate) => candidate.runId === runId); reply = From 4de1e63ab9cb17ea1224adafdf087b76de6659c3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 20 Nov 2026 22:59:35 +0000 Subject: [PATCH 3/4] fix(server): name the newest page when forwarding an agent's t3_thread_read Reading newest first on the linked server whenever a sender thread was attached also changed what older delegated-task result scans got, and the scan's new afterPosition: -1 is invalid for servers before #33428. Leave readThread and RemoteDelegation as on main, and instead add beforePosition to an agent's forwarded read that gives no position or item, so the linked server serves the newest page. Fixes pingdotgg/t3code#39932 Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/OrchestratorMcpService.ts | 6 ++--- apps/server/src/peer/PeerForwarding.ts | 22 ++++++++++++++++++- apps/server/src/peer/RemoteDelegation.test.ts | 2 +- apps/server/src/peer/RemoteDelegation.ts | 2 +- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 4d8be06b8d9e0..36cd8caa5708b 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3508,12 +3508,10 @@ const make = Effect.gen(function* () { const view = input.view ?? "messages"; const limit = input.limit ?? DEFAULT_THREAD_READ_LIMIT; const maxChars = input.maxCharsPerItem ?? DEFAULT_THREAD_ITEM_MAX_CHARS; - // Agents, here or on a linked environment, read newest first. Other clients (the CLI) - // page forward from a cursorless read. + // Client callers (the CLI, older linked environments) page forward from a cursorless read. const newestFirst = input.beforePosition !== undefined || - (input.afterPosition === undefined && - (scope.thread !== undefined || scope.client?.senderThread !== undefined)); + (input.afterPosition === undefined && scope.thread !== undefined); const timeline = yield* threadManagement .getTimelinePage(input.threadId, { ...(input.afterPosition === undefined ? {} : { afterPosition: input.afterPosition }), diff --git a/apps/server/src/peer/PeerForwarding.ts b/apps/server/src/peer/PeerForwarding.ts index 080f701c45cb2..0e0eb79eae400 100644 --- a/apps/server/src/peer/PeerForwarding.ts +++ b/apps/server/src/peer/PeerForwarding.ts @@ -237,6 +237,17 @@ const make = Effect.gen(function* () { Effect.flatMap((enabled) => (enabled ? Effect.void : Effect.fail(linkedEnvironmentsOff))), ); + // A linked server reads a cursorless call oldest first, as the CLI and older delegated-task + // result scans expect, so an agent's read names the newest page explicitly. + const newestFirstRead = ( + input: OrchestratorMcpThreadReadInput, + ): OrchestratorMcpThreadReadInput => + input.afterPosition === undefined && + input.beforePosition === undefined && + input.itemId === undefined + ? { ...input, beforePosition: Number.MAX_SAFE_INTEGER } + : input; + const call: PeerForwarding["Service"]["call"] = (scope, tool, environmentId, params) => { const forwarded: Effect.Effect< Tool.Success, @@ -247,7 +258,16 @@ const make = Effect.gen(function* () { environmentId, params as OrchestratorMcpThreadWaitInput, ) as Effect.Effect, OrchestratorMcpFailure>) - : forward(scope, tool.name, tool.parametersSchema, tool.successSchema, environmentId, params); + : forward( + scope, + tool.name, + tool.parametersSchema, + tool.successSchema, + environmentId, + tool.name === "t3_thread_read" && scope.thread !== undefined + ? newestFirstRead(params as OrchestratorMcpThreadReadInput) + : params, + ); return Effect.andThen(requireEnabled, forwarded); }; diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.ts index 3c14fb427fc2e..d4af55cae0313 100644 --- a/apps/server/src/peer/RemoteDelegation.test.ts +++ b/apps/server/src/peer/RemoteDelegation.test.ts @@ -1833,7 +1833,7 @@ it.effect.each([ const pages = reads.filter((read) => read.itemId === undefined && read.limit === 100); // The thread's final page starts at its canonical 100-item boundary. const tailPosition = Math.floor((timeline.length - 1) / 100) * 100 - 1; - expect(pages.at(0)?.afterPosition).toBe(-1); + expect(pages.at(0)?.afterPosition).toBeUndefined(); expect( pages.every( (read) => diff --git a/apps/server/src/peer/RemoteDelegation.ts b/apps/server/src/peer/RemoteDelegation.ts index a612f930ee59c..6e48aa5fa61fe 100644 --- a/apps/server/src/peer/RemoteDelegation.ts +++ b/apps/server/src/peer/RemoteDelegation.ts @@ -632,7 +632,7 @@ const make = Effect.gen(function* () { // Keep the first and expected final activity pages whole. A result on a // middle page then needs an extra text read after the scan. maxCharsPerItem: failed && remainingItems > 100 ? 1 : 20_000, - afterPosition: afterPosition ?? -1, + ...(afterPosition === undefined ? {} : { afterPosition }), }); run ??= read.recentRuns.find((candidate) => candidate.runId === runId); reply = From c2c90d3c44df2dd278694d391f163b21a147d21f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 02:11:34 +0000 Subject: [PATCH 4/4] fix(server): a forwarded agent t3_thread_read keeps the first page's pull requests The forwarded read names the newest page with beforePosition, which the linked server took for a later page and so dropped the open pull request preview. A beforePosition at or past the thread's end is the first page. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/mcp/OrchestratorMcpService.activity.test.ts | 6 ++++++ apps/server/src/mcp/OrchestratorMcpService.ts | 5 +++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index 3a389a5063645..71bdd5dc18db6 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -267,6 +267,12 @@ it("readThread preserves caller access errors and prefers activity-run status ov }); expect(laterPage.thread.pullRequests).toBeUndefined(); expect(laterPage.thread.pullRequestCount).toBe(8); + const newestPage = yield* service.readThread(makeScope(), { + threadId: parentThreadId, + beforePosition: Number.MAX_SAFE_INTEGER, + }); + expect(newestPage.thread.pullRequests?.map((link) => link.number)).toEqual([2, 8, 7, 6, 5]); + expect(newestPage.thread.openPullRequestCount).toBe(7); Object.assign(projection, { runs: [makeRun({ id: activeRunId, ordinal: 1, status: "completed" })], diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 36cd8caa5708b..10866f0805923 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3547,9 +3547,10 @@ const make = Effect.gen(function* () { timeline.totalItems, shell, yield* Clock.currentTimeMillis, + // A forwarded agent read names the newest page with a beforePosition past the end. input.afterPosition === undefined && - input.beforePosition === undefined && - input.itemId === undefined, + input.itemId === undefined && + (input.beforePosition === undefined || input.beforePosition >= timeline.totalItems), ); const recentRuns = target.runs .toSorted((left, right) => right.ordinal - left.ordinal)