@devries-cameron20 / t3code Mirror · read-only / commits / 34766e6795
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 10:45 UTC

perf(cli): thread show reads only the end of long threads (#24087)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor Vogel committed Oct 30, 2026 at 16:02 UTC 34766e67959ceb30e84abfd0e1d9d7468ef0ddeb
2 files changed +149 -5
apps/server/src/cli/thread.test.ts
+88 -1
@@ -2,7 +2,7 @@ import { assert, it } from "@effect/vitest";
2 import { RunId, ThreadId } from "@t3tools/contracts";
3 import * as Effect from "effect/Effect";
4
5 -import { type ThreadTools, waitForRun } from "./thread.ts";
5 +import { readLatestMessages, type ThreadTools, waitForRun } from "./thread.ts";
6
7 const threadId = ThreadId.make("thread-1");
8 const runId = RunId.make("run-1");
@@ -73,3 +73,90 @@ it.effect("--wait reports its run's result when a later run is the one waiting",
73 });
74 }),
75 );
76 +
77 +/** Serves t3_thread_read pages the way the server does: positions count every item. */
78 +const timelineTools = (types: ReadonlyArray<string>) => {
79 + const items = types.map((type, position) => ({ position, itemId: `item-${position}`, type }));
80 + const served: Array<number> = [];
81 + const call = ((_tool: string, input: unknown) => {
82 + const args = input as { readonly afterPosition?: number; readonly limit: number };
83 + // Scanning one match past the page keeps reads of huge fixtures linear.
84 + const matching: typeof items = [];
85 + for (let position = (args.afterPosition ?? -1) + 1; position < items.length; position++) {
86 + const item = items[position]!;
87 + if (["user_message", "assistant_message", "proposed_plan"].includes(item.type)) {
88 + matching.push(item);
89 + if (matching.length > args.limit) break;
90 + }
91 + }
92 + const page = matching.slice(0, args.limit);
93 + served.push(page.length);
94 + return Effect.succeed({
95 + thread: { itemCount: items.length },
96 + items: page,
97 + nextPosition: page.at(-1)?.position ?? null,
98 + hasMore: matching.length > page.length,
99 + });
100 + }) as ThreadTools["call"];
101 + const latest = (count: number) =>
102 + items
103 + .filter((item) => item.type === "user_message" || item.type === "assistant_message")
104 + .slice(-count)
105 + .map((item) => item.itemId);
106 + return { tools: { call } satisfies ThreadTools, served, latest };
107 +};
108 +
109 +const tail = (tools: number) => [...Array<string>(tools).fill("tool_call"), "assistant_message"];
110 +
111 +it.effect("show reads the end of a long thread, not its whole history", () =>
112 + Effect.gen(function* () {
113 + const { tools, served, latest } = timelineTools([
114 + ...Array.from({ length: 1000 }, (_, index) =>
115 + index % 2 === 0 ? "user_message" : "assistant_message",
116 + ),
117 + ...Array.from({ length: 10 }, () => tail(30)).flat(),
118 + "proposed_plan",
119 + ]);
120 +
121 + const { messages } = yield* readLatestMessages(tools, threadId, 10);
122 +
123 + assert.deepStrictEqual(
124 + messages.map((item) => item.itemId),
125 + latest(10),
126 + );
127 + assert.isBelow(
128 + served.reduce((sum, count) => sum + count, 0),
129 + 100,
130 + );
131 + }),
132 +);
133 +
134 +it.effect("show widens back to the first item when the thread has fewer messages", () =>
135 + Effect.gen(function* () {
136 + const { tools, latest } = timelineTools([
137 + "user_message",
138 + ...Array.from({ length: 4 }, () => tail(150)).flat(),
139 + ]);
140 +
141 + const { messages } = yield* readLatestMessages(tools, threadId, 10);
142 +
143 + assert.deepStrictEqual(
144 + messages.map((item) => item.itemId),
145 + latest(10),
146 + );
147 + assert.lengthOf(messages, 5);
148 + }),
149 +);
150 +
151 +it.effect("show returns a --messages count larger than one call can spread", () =>
152 + Effect.gen(function* () {
153 + const { tools, latest } = timelineTools(Array<string>(200_000).fill("assistant_message"));
154 +
155 + const { messages } = yield* readLatestMessages(tools, threadId, 200_000);
156 +
157 + assert.deepStrictEqual(
158 + messages.map((item) => item.itemId),
159 + latest(200_000),
160 + );
161 + }),
162 +);
apps/server/src/cli/thread.ts
+61 -4
@@ -333,6 +333,63 @@ const readTimeline = Effect.fn("cli.thread.readTimeline")(function* (
333 return { thread: page.thread, items };
334 });
335
336 +/**
337 + * The latest `count` user and assistant messages, oldest first. Positions
338 + * count every timeline item, so the read starts near the end and widens
339 + * backward only while tool and plan items leave too few messages.
340 + */
341 +export const readLatestMessages = Effect.fn("cli.thread.readLatestMessages")(function* (
342 + tools: ThreadTools,
343 + threadId: ThreadId,
344 + count: number,
345 +) {
346 + let { thread } = yield* tools.call(
347 + "t3_thread_read",
348 + { threadId, limit: 1, runLimit: 1 },
349 + OrchestratorMcpThreadReadResult,
350 + );
351 + let messages: Array<OrchestratorMcpThreadTimelineItem> = [];
352 + // The newest window has no upper bound, so items added since the first
353 + // read are still shown.
354 + let before: number | undefined;
355 + let start = thread.itemCount;
356 + let span = Math.max(count, READ_PAGE_LIMIT);
357 + do {
358 + const end = before;
359 + start = Math.max(0, start - span);
360 + const window: Array<OrchestratorMcpThreadTimelineItem> = [];
361 + let afterPosition = start - 1;
362 + let page: OrchestratorMcpThreadReadResult;
363 + do {
364 + page = yield* tools.call(
365 + "t3_thread_read",
366 + {
367 + threadId,
368 + view: "messages",
369 + limit: READ_PAGE_LIMIT,
370 + runLimit: 1,
371 + ...(afterPosition < 0 ? {} : { afterPosition }),
372 + },
373 + OrchestratorMcpThreadReadResult,
374 + );
375 + window.push(
376 + ...page.items.filter(
377 + (item) =>
378 + (item.type === "user_message" || item.type === "assistant_message") &&
379 + (end === undefined || item.position < end),
380 + ),
381 + );
382 + afterPosition = page.nextPosition ?? Number.POSITIVE_INFINITY;
383 + } while (page.hasMore && afterPosition < (end ?? Number.POSITIVE_INFINITY) - 1);
384 + thread = page.thread;
385 + // A window can hold the whole --messages count, too many to spread as arguments.
386 + messages = window.concat(messages);
387 + before = start;
388 + span *= 2;
389 + } while (messages.length < count && start > 0);
390 + return { thread, messages: messages.slice(Math.max(0, messages.length - count)) };
391 +});
392 +
393 /** Item text past the read's per-item cap. */
394 const readFullText = Effect.fn("cli.thread.readFullText")(function* (
395 tools: ThreadTools,
@@ -662,11 +719,11 @@ const threadShowCommand = Command.make("show", {
719 yield* withThreadTools(
720 flags,
721 Effect.fn("cli.thread.show")(function* (tools) {
665 - const { thread, items } = yield* readTimeline(tools, threadId, "messages");
666 - const conversation = items.filter(
667 - (item) => item.type === "user_message" || item.type === "assistant_message",
722 + const { thread, messages: latest } = yield* readLatestMessages(
723 + tools,
724 + threadId,
725 + flags.messages,
726 );
669 - const latest = conversation.slice(Math.max(0, conversation.length - flags.messages));
727 const messages = yield* Effect.forEach(latest, (item) =>
728 readFullText(tools, threadId, item).pipe(
729 Effect.map((text) => ({