From 6a193756750c4ca69610d4bd5fd3ca036de32cda Mon Sep 17 00:00:00 2001 From: angrojo Date: Sat, 21 Nov 2026 00:09:39 +0100 Subject: [PATCH 1/3] fix(server): linked environments read the newest thread messages by default t3_thread_read documents newest-first order as the default page. When called on a linked environment, requests arrive without a local thread scope (scope.thread === undefined), which caused cursorless reads to fall back to forward paging from the thread start. Recognize scope.client.linked in readThread so linked environment callers receive the newest items by default as documented, while leaving forward paging intact for the CLI. Fixes #39932 --- .../OrchestratorMcpService.activity.test.ts | 19 ++++++++++++++++++- apps/server/src/mcp/OrchestratorMcpService.ts | 5 +++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts index 3a389a5063645..a1294de3648f9 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.activity.test.ts @@ -505,14 +505,28 @@ it("readThread returns the newest items that fit the response budget", async () maxCharsPerItem: 1_500, }; const result = yield* service.readThread(makeScope(), read); - // Linked environments and the CLI read as clients without a cursor and follow only hasMore. + // The CLI reads as a client without a cursor and follows only hasMore. const first = yield* service.readThread({ ...makeScope(), thread: undefined }, read); const fromStart = yield* service.readThread(makeScope(), { ...read, afterPosition: -1 }); + const linked = yield* service.readThread( + { + ...makeScope(), + thread: undefined, + client: { + sessionId: "laptop", + label: "T3 Code ยท laptop", + access: "auto", + linked: true, + }, + }, + read, + ); expect(pageOptions).toEqual([ { beforePosition: Number.POSITIVE_INFINITY, limit: 100, view: "activity" }, { limit: 100, view: "activity" }, { afterPosition: -1, limit: 100, view: "activity" }, + { beforePosition: Number.POSITIVE_INFINITY, limit: 100, view: "activity" }, ]); // Claude Code saves results over ~50k chars of this JSON to a file instead of showing them. expect(JSON.stringify(result).length).toBeLessThan(31_000); @@ -524,6 +538,9 @@ it("readThread returns the newest items that fit the response budget", async () expect(first.hasMore).toBe(true); expect(first.nextPosition).toBe(first.items.at(-1)?.position); expect(fromStart.items[0]?.position).toBe(0); + expect(linked.items.at(-1)?.position).toBe(99); + expect(linked.earlierPosition).toBe(linked.items[0]?.position); + expect(linked.hasMore).toBe(false); }).pipe(Effect.provide(layer), Effect.runPromise); }); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 36cd8caa5708b..1fb1bd5502bfd 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3508,10 +3508,11 @@ 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. + // Client callers (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?.linked === true)); const timeline = yield* threadManagement .getTimelinePage(input.threadId, { ...(input.afterPosition === undefined ? {} : { afterPosition: input.afterPosition }), From 00bd9314c28b87d8d2d1023b7b345c8048cc6876 Mon Sep 17 00:00:00 2001 From: angrojo Date: Sat, 21 Nov 2026 00:46:22 +0100 Subject: [PATCH 2/3] fix(peer): start delegated task result scan explicitly from thread start Because cursorless reads on linked environments now default to newest first, initialize lastReply's afterPosition to -1 so remote result scans continue to page forward from the start of the thread. --- apps/server/src/peer/RemoteDelegation.test.ts | 2 +- apps/server/src/peer/RemoteDelegation.ts | 29 +++++++++++-------- 2 files changed, 18 insertions(+), 13 deletions(-) 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..848d7f47bee4c 100644 --- a/apps/server/src/peer/RemoteDelegation.ts +++ b/apps/server/src/peer/RemoteDelegation.ts @@ -7,6 +7,7 @@ import { OrchestratorMcpFailure, type OrchestratorMcpDelegateTaskInput, type OrchestratorMcpTaskCancelResult, + type OrchestratorMcpThreadReadResult, type OrchestratorMcpThreadRun, type OrchestratorMcpThreadSendInput, type OrchestratorMcpThreadSendResult, @@ -619,21 +620,25 @@ const make = Effect.gen(function* () { ) { let reply: OrchestratorMcpThreadTimelineItem | undefined; let error: OrchestratorMcpThreadTimelineItem | undefined; - let afterPosition: number | undefined; + let afterPosition: number | null | undefined = -1; let run: OrchestratorMcpThreadRun | undefined; let remainingItems = 0; let tailProbed = false; while (true) { - const read = yield* forwarding.readThread(scope, remote.environmentId, { - threadId: remote.threadId, - view: failed ? "activity" : "messages", - limit: 100, - runLimit: 50, - // 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 }), - }); + const read: OrchestratorMcpThreadReadResult = yield* forwarding.readThread( + scope, + remote.environmentId, + { + threadId: remote.threadId, + view: failed ? "activity" : "messages", + limit: 100, + runLimit: 50, + // 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 === null ? {} : { afterPosition }), + }, + ); run ??= read.recentRuns.find((candidate) => candidate.runId === runId); reply = read.items.findLast( @@ -667,7 +672,7 @@ const make = Effect.gen(function* () { } } afterPosition = read.nextPosition; - remainingItems = read.thread.itemCount - afterPosition - 1; + remainingItems = read.thread.itemCount - (afterPosition ?? 0) - 1; } reply = error ?? reply; const executedModel = run?.executedModel ?? undefined; From 970673551b6bcac260930897f86201def7896242 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sat, 21 Nov 2026 00:50:12 +0000 Subject: [PATCH 3/3] refactor(server): simplify the delegated result scan cursor The scan always holds a number: it starts at -1 and breaks before a null nextPosition, so the cursor needs no null or undefined cases. Co-Authored-By: Claude Opus 5.5 --- apps/server/src/peer/RemoteDelegation.ts | 30 ++++++++++-------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/apps/server/src/peer/RemoteDelegation.ts b/apps/server/src/peer/RemoteDelegation.ts index 848d7f47bee4c..5ed03b795b686 100644 --- a/apps/server/src/peer/RemoteDelegation.ts +++ b/apps/server/src/peer/RemoteDelegation.ts @@ -7,7 +7,6 @@ import { OrchestratorMcpFailure, type OrchestratorMcpDelegateTaskInput, type OrchestratorMcpTaskCancelResult, - type OrchestratorMcpThreadReadResult, type OrchestratorMcpThreadRun, type OrchestratorMcpThreadSendInput, type OrchestratorMcpThreadSendResult, @@ -620,25 +619,22 @@ const make = Effect.gen(function* () { ) { let reply: OrchestratorMcpThreadTimelineItem | undefined; let error: OrchestratorMcpThreadTimelineItem | undefined; - let afterPosition: number | null | undefined = -1; + // Linked peers read newest first without a cursor; this scan runs forward. + let afterPosition = -1; let run: OrchestratorMcpThreadRun | undefined; let remainingItems = 0; let tailProbed = false; while (true) { - const read: OrchestratorMcpThreadReadResult = yield* forwarding.readThread( - scope, - remote.environmentId, - { - threadId: remote.threadId, - view: failed ? "activity" : "messages", - limit: 100, - runLimit: 50, - // 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 === null ? {} : { afterPosition }), - }, - ); + const read = yield* forwarding.readThread(scope, remote.environmentId, { + threadId: remote.threadId, + view: failed ? "activity" : "messages", + limit: 100, + runLimit: 50, + // 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, + }); run ??= read.recentRuns.find((candidate) => candidate.runId === runId); reply = read.items.findLast( @@ -672,7 +668,7 @@ const make = Effect.gen(function* () { } } afterPosition = read.nextPosition; - remainingItems = read.thread.itemCount - (afterPosition ?? 0) - 1; + remainingItems = read.thread.itemCount - afterPosition - 1; } reply = error ?? reply; const executedModel = run?.executedModel ?? undefined;