The best way to control Claude Code, Codex and more from any computer or phone
perf(server): avoid discarded history when retrieving remote task results (#26664)
Adamulek123 committed
Nov 19, 2026 at 21:03 UTC
1e7021025038c18f2a7d11b63818334224fe31ee
2 files changed
+358
-59
apps/server/src/peer/RemoteDelegation.test.ts
+324
-57
@@ -9,6 +9,7 @@ import {
9
type ModelSelection,
10
NodeId,
11
type OrchestrationV2Run,
12
+ type OrchestratorMcpThreadReadInput,
13
ProjectId,
14
ProviderDriverKind,
15
ProviderInstanceId,
@@ -34,6 +35,7 @@ import * as Schedule from "effect/Schedule";
35
import * as Stream from "effect/Stream";
36
import * as SubscriptionRef from "effect/SubscriptionRef";
37
import * as Tracer from "effect/Tracer";
38
+import * as FetchHttpClient from "effect/http/FetchHttpClient";
39
import * as TestClock from "effect/testing/TestClock";
40
import type * as SqlClient from "effect/sql/SqlClient";
41
import { McpSchema, McpServer } from "effect/ai";
@@ -88,7 +90,18 @@ type LaunchedWith = Pick<
90
>;
91
92
/** The thread the box launches for the task, which the test finishes. */
93
+interface TimelineEntry {
94
+ readonly id: TurnItemId;
95
+ readonly runId: RunId;
96
+ readonly text: string;
97
+ readonly type: "assistant_message" | "error";
98
+}
99
+
100
interface BoxThread {
101
+ readonly timeline: Ref.Ref<ReadonlyArray<TimelineEntry> | null>;
102
+ readonly pageSize: Ref.Ref<number>;
103
+ readonly holdRefill: Ref.Ref<boolean>;
104
+ readonly beforeTail: Ref.Ref<Effect.Effect<void>>;
105
readonly launched: Ref.Ref<ThreadId | null>;
106
/** The link session that launched it, as the box stamps it. */
107
readonly linkOrigin: Ref.Ref<{ readonly sessionId: string; readonly label: string } | null>;
@@ -145,6 +158,10 @@ interface BoxThread {
158
159
const makeBoxThread = Effect.gen(function* () {
160
return {
161
+ timeline: yield* Ref.make<ReadonlyArray<TimelineEntry> | null>(null),
162
+ pageSize: yield* Ref.make(1),
163
+ holdRefill: yield* Ref.make(false),
164
+ beforeTail: yield* Ref.make<Effect.Effect<void>>(Effect.void),
165
launched: yield* Ref.make<ThreadId | null>(null),
166
linkOrigin: yield* Ref.make<{ readonly sessionId: string; readonly label: string } | null>(
167
null,
@@ -361,7 +378,13 @@ const boxToolkitLayer = (thread: BoxThread) => {
378
}),
379
getTimelinePage: (threadId, options) =>
380
Effect.gen(function* () {
364
- if (yield* Ref.get(thread.holdRead)) {
381
+ if (options.afterPosition === 299 && options.itemId === undefined) {
382
+ yield* Ref.getAndSet(thread.beforeTail, Effect.void).pipe(Effect.flatten);
383
+ }
384
+ if (
385
+ (yield* Ref.get(thread.holdRead)) ||
386
+ (options.itemId !== undefined && (yield* Ref.get(thread.holdRefill)))
387
+ ) {
388
yield* Deferred.succeed(thread.readStarted, undefined);
389
yield* Deferred.await(thread.releaseRead);
390
}
@@ -372,61 +395,63 @@ const boxToolkitLayer = (thread: BoxThread) => {
395
const laterError = yield* Ref.get(thread.laterError);
396
const restart = yield* Ref.get(thread.restart);
397
// One item per page, with errors available only in the activity view.
375
- const replies = [
376
- ...(reply === null
377
- ? []
378
- : [
379
- {
380
- id: TurnItemId.make("item:reply"),
381
- runId,
382
- text: reply,
383
- type: "assistant_message",
384
- },
385
- ]),
386
- ...(error === null
387
- ? []
388
- : [{ id: TurnItemId.make("item:error"), runId, text: error, type: "error" }]),
389
- ...(later === null
390
- ? []
391
- : [
392
- {
393
- id: TurnItemId.make("item:later-reply"),
394
- runId: boxLaterRunId,
395
- text: later,
396
- type: "assistant_message",
397
- },
398
- ]),
399
- ...(laterError === null
400
- ? []
401
- : [
402
- {
403
- id: TurnItemId.make("item:later-error"),
404
- runId: boxLaterRunId,
405
- text: laterError,
406
- type: "error",
407
- },
408
- ]),
409
- ...(queued === null
410
- ? []
411
- : [
412
- {
413
- id: TurnItemId.make("item:queued-reply"),
414
- runId: boxQueuedRunId,
415
- text: queued,
416
- type: "assistant_message",
417
- },
418
- ]),
419
- ...(restart === null
420
- ? []
421
- : [
422
- {
423
- id: TurnItemId.make("item:restarted-reply"),
424
- runId: continuationOf(restart.of),
425
- text: restart.reply,
426
- type: "assistant_message",
427
- },
428
- ]),
429
- ].map((entry, position) => ({ ...entry, position }));
398
+ const replies = (
399
+ (yield* Ref.get(thread.timeline)) ?? [
400
+ ...(reply === null
401
+ ? []
402
+ : [
403
+ {
404
+ id: TurnItemId.make("item:reply"),
405
+ runId,
406
+ text: reply,
407
+ type: "assistant_message",
408
+ },
409
+ ]),
410
+ ...(error === null
411
+ ? []
412
+ : [{ id: TurnItemId.make("item:error"), runId, text: error, type: "error" }]),
413
+ ...(later === null
414
+ ? []
415
+ : [
416
+ {
417
+ id: TurnItemId.make("item:later-reply"),
418
+ runId: boxLaterRunId,
419
+ text: later,
420
+ type: "assistant_message",
421
+ },
422
+ ]),
423
+ ...(laterError === null
424
+ ? []
425
+ : [
426
+ {
427
+ id: TurnItemId.make("item:later-error"),
428
+ runId: boxLaterRunId,
429
+ text: laterError,
430
+ type: "error",
431
+ },
432
+ ]),
433
+ ...(queued === null
434
+ ? []
435
+ : [
436
+ {
437
+ id: TurnItemId.make("item:queued-reply"),
438
+ runId: boxQueuedRunId,
439
+ text: queued,
440
+ type: "assistant_message",
441
+ },
442
+ ]),
443
+ ...(restart === null
444
+ ? []
445
+ : [
446
+ {
447
+ id: TurnItemId.make("item:restarted-reply"),
448
+ runId: continuationOf(restart.of),
449
+ text: restart.reply,
450
+ type: "assistant_message",
451
+ },
452
+ ]),
453
+ ]
454
+ ).map((entry, position) => ({ ...entry, position }));
455
const visible =
456
options.itemId === undefined
457
? replies.filter(
@@ -435,7 +460,7 @@ const boxToolkitLayer = (thread: BoxThread) => {
460
: replies.filter((entry) => entry.id === options.itemId);
461
const matching = visible
462
.filter((entry) => entry.position > (options.afterPosition ?? -1))
438
- .slice(0, 1);
463
+ .slice(0, yield* Ref.get(thread.pageSize));
464
return {
465
items: matching.map(({ id, runId: itemRunId, text, position, type }) => ({
466
position,
@@ -918,6 +943,28 @@ const taskResult = (laptopEnv: Effect.Success<ReturnType<typeof makeLaptop>>, ta
943
),
944
);
945
946
+const captureThreadReads = (nativeFetch: typeof globalThis.fetch) => {
947
+ const reads: Array<OrchestratorMcpThreadReadInput> = [];
948
+ const fetch: typeof globalThis.fetch = (input, init) => {
949
+ const body = init?.body instanceof Uint8Array ? new TextDecoder().decode(init.body) : null;
950
+ if (body?.startsWith("{")) {
951
+ const rpc: {
952
+ method?: string;
953
+ params?: { name?: string; arguments?: OrchestratorMcpThreadReadInput };
954
+ } = JSON.parse(body);
955
+ if (
956
+ rpc.method === "tools/call" &&
957
+ rpc.params?.name === "t3_thread_read" &&
958
+ rpc.params.arguments !== undefined
959
+ ) {
960
+ reads.push(rpc.params.arguments);
961
+ }
962
+ }
963
+ return nativeFetch(input, init);
964
+ };
965
+ return { reads, fetch };
966
+};
967
+
968
it.effect("a task delegated to the box wakes the laptop's parent when it ends there", () =>
969
Effect.scoped(
970
Effect.gen(function* () {
@@ -1605,6 +1652,226 @@ it.effect.each([false, true])(
1652
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1653
);
1654
1655
+it.effect.each(["tail-error", "middle-error", "reply-fallback", "cancel-refill"] as const)(
1656
+ "reads a failed task through full activity pages (%s)",
1657
+ (scenario) =>
1658
+ Effect.gen(function* () {
1659
+ const { reads, fetch } = captureThreadReads(yield* FetchHttpClient.Fetch);
1660
+ return yield* Effect.scoped(
1661
+ Effect.gen(function* () {
1662
+ const thread = yield* makeBoxThread;
1663
+ const b = yield* serveBox(thread);
1664
+ const offers = yield* SubscriptionRef.make<
1665
+ ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1666
+ >([]);
1667
+ const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1668
+ const a = yield* makeLaptop(database, offers);
1669
+ yield* linkTo(a.links, b, "full-access");
1670
+ yield* seedParent(a);
1671
+ const delegated = yield* delegateToBox(a);
1672
+ const result = `Provider details: ${"detail ".repeat(8_000)}end.`;
1673
+ const middle = scenario !== "tail-error";
1674
+ const ownType = scenario === "reply-fallback" ? "assistant_message" : "error";
1675
+ const history = (count: number, runId: RunId, prefix: string): Array<TimelineEntry> =>
1676
+ Array.from({ length: count }, (_, index) => ({
1677
+ id: TurnItemId.make(`${prefix}:${index}`),
1678
+ runId,
1679
+ text: "discarded history ".repeat(120),
1680
+ type: "assistant_message",
1681
+ }));
1682
+ const timeline: Array<TimelineEntry> = [
1683
+ ...history(middle ? 150 : 300, boxRunId, "before"),
1684
+ { id: TurnItemId.make("selected"), runId: boxRunId, text: result, type: ownType },
1685
+ ...(middle ? history(200, boxLaterRunId, "later") : []),
1686
+ {
1687
+ id: TurnItemId.make("other-error"),
1688
+ runId: boxLaterRunId,
1689
+ text: "Wrong run",
1690
+ type: "error",
1691
+ },
1692
+ ];
1693
+ yield* Ref.set(thread.timeline, timeline);
1694
+ yield* Ref.set(thread.pageSize, 100);
1695
+ yield* Ref.set(thread.status, "failed");
1696
+ if (scenario === "cancel-refill") yield* Ref.set(thread.holdRefill, true);
1697
+ yield* Deferred.succeed(thread.finished, undefined);
1698
+ if (scenario === "cancel-refill") {
1699
+ yield* Deferred.await(thread.readStarted);
1700
+ yield* a.threads.stopDelegatedTasks({
1701
+ threadId: parentThreadId,
1702
+ commandId: CommandId.make("command:stop:mid-refill"),
1703
+ });
1704
+ yield* Deferred.succeed(thread.releaseRead, undefined);
1705
+ expect(yield* taskResult(a, delegated.taskId)).toMatchObject({ status: "interrupted" });
1706
+ yield* Deferred.await(thread.interruptedOnce);
1707
+ expect(yield* Ref.get(thread.stops)).toContain("thread.stop");
1708
+ } else {
1709
+ expect(yield* taskResult(a, delegated.taskId)).toMatchObject({
1710
+ status: "failed",
1711
+ result,
1712
+ });
1713
+ }
1714
+ const pages = reads.filter((read) => read.itemId === undefined && read.limit === 100);
1715
+ // A page moves on by however many items the response budget left it.
1716
+ const remaining = (read: OrchestratorMcpThreadReadInput) =>
1717
+ timeline.length - 1 - (read.afterPosition ?? -1);
1718
+ expect(pages[0]).toMatchObject({
1719
+ view: "activity",
1720
+ limit: 100,
1721
+ maxCharsPerItem: 20_000,
1722
+ });
1723
+ expect(pages.slice(1).some((read) => remaining(read) > 100)).toBe(true);
1724
+ for (const read of pages.slice(1))
1725
+ expect(read.maxCharsPerItem).toBe(remaining(read) > 100 ? 1 : 20_000);
1726
+ const forwards = pages.slice(1).map((read) => read.afterPosition ?? -1);
1727
+ expect(forwards).toEqual([...forwards].sort((left, right) => left - right));
1728
+ expect(new Set(forwards).size).toBe(forwards.length);
1729
+ const refills = reads.filter((read) => read.itemId !== undefined);
1730
+ expect(refills[0]).toMatchObject({ itemId: "selected", textOffset: middle ? 1 : 20_000 });
1731
+ if (scenario !== "cancel-refill") expect(refills).toHaveLength(middle ? 3 : 2);
1732
+ }),
1733
+ ).pipe(
1734
+ Effect.provide(NodeHttpServer.layerTest),
1735
+ Effect.provideService(FetchHttpClient.Fetch, fetch),
1736
+ );
1737
+ }),
1738
+);
1739
+
1740
+it.effect.each([
1741
+ "terminal",
1742
+ "long",
1743
+ "later-run",
1744
+ "sparse",
1745
+ "growing",
1746
+ "shrinking",
1747
+ "cancel",
1748
+ "no-run",
1749
+ "cancelled",
1750
+ "interrupted",
1751
+] as const)("reads a successful task with an aligned tail probe (%s)", (scenario) =>
1752
+ Effect.gen(function* () {
1753
+ const { reads, fetch } = captureThreadReads(yield* FetchHttpClient.Fetch);
1754
+ return yield* Effect.scoped(
1755
+ Effect.gen(function* () {
1756
+ const thread = yield* makeBoxThread;
1757
+ const b = yield* serveBox(thread);
1758
+ const offers = yield* SubscriptionRef.make<
1759
+ ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1760
+ >([]);
1761
+ const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1762
+ const a = yield* makeLaptop(database, offers);
1763
+ yield* linkTo(a.links, b, "full-access");
1764
+ yield* seedParent(a);
1765
+ const result =
1766
+ scenario === "long"
1767
+ ? `Complete ${"detail ".repeat(12_000)}end.`
1768
+ : "Authoritative result.";
1769
+ const middle = scenario === "later-run" || scenario === "shrinking";
1770
+ const history = (count: number, runId: RunId, prefix: string): Array<TimelineEntry> =>
1771
+ Array.from({ length: count }, (_, index) => ({
1772
+ id: TurnItemId.make(`${prefix}:${index}`),
1773
+ runId,
1774
+ text: "History",
1775
+ type: scenario === "sparse" && index % 2 === 0 ? "error" : "assistant_message",
1776
+ }));
1777
+ const timeline: Array<TimelineEntry> = [
1778
+ ...history(middle ? 150 : 300, boxRunId, "before"),
1779
+ {
1780
+ id: TurnItemId.make("selected"),
1781
+ runId: scenario === "no-run" ? boxLaterRunId : boxRunId,
1782
+ text: result,
1783
+ type: "assistant_message",
1784
+ },
1785
+ ...(middle ? history(200, boxLaterRunId, "later") : []),
1786
+ ];
1787
+ yield* Ref.set(thread.timeline, timeline);
1788
+ yield* Ref.set(thread.pageSize, 100);
1789
+ if (scenario === "growing") {
1790
+ yield* Ref.set(
1791
+ thread.beforeTail,
1792
+ Ref.set(thread.timeline, [...timeline, ...history(101, boxLaterRunId, "growth")]),
1793
+ );
1794
+ }
1795
+ if (scenario === "shrinking") {
1796
+ yield* Ref.set(thread.beforeTail, Ref.set(thread.timeline, timeline.slice(0, 151)));
1797
+ }
1798
+ if (scenario === "cancel") {
1799
+ yield* Ref.set(
1800
+ thread.beforeTail,
1801
+ Effect.gen(function* () {
1802
+ yield* Deferred.succeed(thread.readStarted, undefined);
1803
+ yield* Deferred.await(thread.releaseRead);
1804
+ }),
1805
+ );
1806
+ }
1807
+ if (scenario === "no-run") yield* Ref.set(thread.runless, true);
1808
+ const delegated = yield* delegateToBox(a);
1809
+ const status =
1810
+ scenario === "cancelled" || scenario === "interrupted" ? scenario : "completed";
1811
+ const modelNote = scenario === "terminal" || scenario === "long";
1812
+ if (modelNote) yield* Ref.set(thread.executedModel, "gpt-5.4-mini");
1813
+ yield* Ref.set(thread.status, status);
1814
+ yield* Deferred.succeed(thread.finished, undefined);
1815
+ if (scenario === "cancel") {
1816
+ yield* Deferred.await(thread.readStarted);
1817
+ yield* a.threads.stopDelegatedTasks({
1818
+ threadId: parentThreadId,
1819
+ commandId: CommandId.make("command:stop:tail-probe"),
1820
+ });
1821
+ yield* Deferred.succeed(thread.releaseRead, undefined);
1822
+ expect(yield* taskResult(a, delegated.taskId)).toMatchObject({ status: "interrupted" });
1823
+ yield* Deferred.await(thread.interruptedOnce);
1824
+ expect(yield* Ref.get(thread.stops)).toContain("thread.stop");
1825
+ } else {
1826
+ expect(yield* taskResult(a, delegated.taskId)).toMatchObject({
1827
+ status,
1828
+ result: modelNote
1829
+ ? `Requested model gpt-5.4; the provider reported running gpt-5.4-mini.\n\n${result}`
1830
+ : result,
1831
+ });
1832
+ }
1833
+ const pages = reads.filter((read) => read.itemId === undefined && read.limit === 100);
1834
+ // The thread's final page starts at its canonical 100-item boundary.
1835
+ const tailPosition = Math.floor((timeline.length - 1) / 100) * 100 - 1;
1836
+ expect(pages.at(0)?.afterPosition).toBeUndefined();
1837
+ expect(
1838
+ pages.every(
1839
+ (read) =>
1840
+ read.view === "messages" && read.limit === 100 && read.maxCharsPerItem === 20_000,
1841
+ ),
1842
+ ).toBe(true);
1843
+ if (scenario === "cancelled" || scenario === "interrupted") {
1844
+ // A run that did not complete keeps the established scan, without the probe.
1845
+ expect(pages.map((read) => read.afterPosition)).not.toContain(tailPosition);
1846
+ const forwards = pages.slice(1).map((read) => read.afterPosition ?? -1);
1847
+ expect(forwards.length).toBeGreaterThan(0);
1848
+ expect(forwards).toEqual([...forwards].sort((left, right) => left - right));
1849
+ } else {
1850
+ expect(pages.at(1)?.afterPosition).toBe(tailPosition);
1851
+ const rest = pages.slice(2);
1852
+ if (scenario === "later-run" || scenario === "shrinking" || scenario === "growing") {
1853
+ // An inconclusive tail falls back to scanning from where the first page ended.
1854
+ expect(rest.length).toBeGreaterThan(0);
1855
+ const resumed = rest.map((read) => read.afterPosition ?? -1);
1856
+ expect(resumed[0]).toBeLessThan(tailPosition);
1857
+ expect(resumed).toEqual([...resumed].sort((left, right) => left - right));
1858
+ } else {
1859
+ // A terminal page holding the run's last reply ends the read there.
1860
+ expect(rest).toHaveLength(0);
1861
+ }
1862
+ }
1863
+ const refills = reads.filter((read) => read.itemId !== undefined);
1864
+ expect(refills).toHaveLength(scenario === "long" ? 4 : 0);
1865
+ if (scenario === "long")
1866
+ expect(refills.map((read) => read.textOffset)).toEqual([20_000, 40_000, 60_000, 80_000]);
1867
+ }),
1868
+ ).pipe(
1869
+ Effect.provide(NodeHttpServer.layerTest),
1870
+ Effect.provideService(FetchHttpClient.Fetch, fetch),
1871
+ );
1872
+ }),
1873
+);
1874
+
1875
it.effect("keeps a successful remote reply despite an error item in the run's activity", () =>
1876
Effect.scoped(
1877
Effect.gen(function* () {
apps/server/src/peer/RemoteDelegation.ts
+34
-2
@@ -615,18 +615,23 @@ const make = Effect.gen(function* () {
615
remote: OrchestrationV2RemoteTaskChild,
616
runId: RunId | null,
617
failed = false,
618
+ probeSuccess = true,
619
) {
620
let reply: OrchestratorMcpThreadTimelineItem | undefined;
621
let error: OrchestratorMcpThreadTimelineItem | undefined;
622
let afterPosition: number | undefined;
623
let run: OrchestratorMcpThreadRun | undefined;
624
+ let remainingItems = 0;
625
+ let tailProbed = false;
626
while (true) {
627
const read = yield* forwarding.readThread(scope, remote.environmentId, {
628
threadId: remote.threadId,
629
view: failed ? "activity" : "messages",
630
limit: 100,
631
runLimit: 50,
629
- maxCharsPerItem: 20_000,
632
+ // Keep the first and expected final activity pages whole. A result on a
633
+ // middle page then needs an extra text read after the scan.
634
+ maxCharsPerItem: failed && remainingItems > 100 ? 1 : 20_000,
635
...(afterPosition === undefined ? {} : { afterPosition }),
636
});
637
run ??= read.recentRuns.find((candidate) => candidate.runId === runId);
@@ -641,7 +646,28 @@ const make = Effect.gen(function* () {
646
) ?? error;
647
}
648
if (!read.hasMore || read.nextPosition === null) break;
649
+ if (!failed && probeSuccess && !tailProbed) {
650
+ tailProbed = true;
651
+ const tailPosition = Math.floor((read.thread.itemCount - 1) / 100) * 100 - 1;
652
+ if (tailPosition > read.nextPosition) {
653
+ const tail = yield* forwarding.readThread(scope, remote.environmentId, {
654
+ threadId: remote.threadId,
655
+ view: "messages",
656
+ limit: 100,
657
+ maxCharsPerItem: 20_000,
658
+ afterPosition: tailPosition,
659
+ });
660
+ const tailReply = tail.items.findLast(
661
+ (item) => item.type === "assistant_message" && (runId === null || item.runId === runId),
662
+ );
663
+ if (!tail.hasMore && tailReply !== undefined) {
664
+ reply = tailReply;
665
+ break;
666
+ }
667
+ }
668
+ }
669
afterPosition = read.nextPosition;
670
+ remainingItems = read.thread.itemCount - afterPosition - 1;
671
}
672
reply = error ?? reply;
673
const executedModel = run?.executedModel ?? undefined;
@@ -861,7 +887,13 @@ const make = Effect.gen(function* () {
887
waited.resultRun !== undefined && !sharedThread
888
? waited.resultRun
889
: { runId: waited.runId, status: waited.status };
864
- const result = yield* lastReply(scope, remote, resultRun.runId, resultRun.status === "failed");
890
+ const result = yield* lastReply(
891
+ scope,
892
+ remote,
893
+ resultRun.runId,
894
+ resultRun.status === "failed",
895
+ resultRun.status === "completed",
896
+ );
897
yield* complete(
898
parentThreadId,
899
taskId,