Skip to content

Commit 5780879

Browse files
fagnersalesclaude
andcommitted
perf(cli): leave linked-environment result reads to their own PR
The RemoteDelegation.lastReply change overlaps #26664, which already reworks that read, so this PR keeps to the CLI's reads. The newest-position constant moves back into the CLI, its only user. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8f0caec commit 5780879

5 files changed

Lines changed: 61 additions & 96 deletions

File tree

‎apps/server/src/cli/thread.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
OrchestratorMcpThreadWaitResult,
77
ProjectId,
88
RunId,
9-
THREAD_READ_NEWEST_POSITION,
109
ThreadId,
1110
TrimmedNonEmptyString,
1211
type OrchestratorMcpThreadListItem,
@@ -44,6 +43,8 @@ import { projectLocationFlags, resolveCliAuthConfig } from "./config.ts";
4443
*/
4544
const WAIT_SLICE_MS = 15_000;
4645
const READ_PAGE_LIMIT = 100;
46+
/** A `beforePosition` past every item, so the read starts at the newest. */
47+
const NEWEST_POSITION = Number.MAX_SAFE_INTEGER;
4748
const TITLE_MAX_CHARS = 80;
4849

4950
export class ThreadServerNotRunningError extends Schema.TaggedError<ThreadServerNotRunningError>()(
@@ -365,7 +366,7 @@ const readBackward = Effect.fn("cli.thread.readBackward")(function* (
365366
{ threadId, view, limit: nextLimit(), runLimit: 1, beforePosition },
366367
OrchestratorMcpThreadReadResult,
367368
);
368-
const newest = yield* read(THREAD_READ_NEWEST_POSITION).pipe(
369+
const newest = yield* read(NEWEST_POSITION).pipe(
369370
Effect.catchTags({ ThreadToolFailedError: () => Effect.succeed(null) }),
370371
);
371372
if (newest === null || newest.previousPosition === undefined) return undefined;

‎apps/server/src/peer/RemoteDelegation.test.ts‎

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ interface BoxThread {
120120
readonly finished: Deferred.Deferred<void>;
121121
/** While set, reading the thread there waits: completed once a read starts, released by `releaseRead`. */
122122
readonly holdRead: Ref.Ref<boolean>;
123-
/** Reads there refuse `beforePosition`, as a box from before it does. */
124-
readonly forwardReadsOnly: Ref.Ref<boolean>;
125123
readonly readStarted: Deferred.Deferred<void>;
126124
readonly releaseRead: Deferred.Deferred<void>;
127125
readonly holdFirstStop: Ref.Ref<boolean>;
@@ -158,7 +156,6 @@ const makeBoxThread = Effect.gen(function* () {
158156
interruptedOnce: yield* Deferred.make<void>(),
159157
finished: yield* Deferred.make<void>(),
160158
holdRead: yield* Ref.make(false),
161-
forwardReadsOnly: yield* Ref.make(false),
162159
readStarted: yield* Deferred.make<void>(),
163160
releaseRead: yield* Deferred.make<void>(),
164161
holdFirstStop: yield* Ref.make(false),
@@ -309,9 +306,6 @@ const boxToolkitLayer = (thread: BoxThread) => {
309306
yield* Deferred.succeed(thread.readStarted, undefined);
310307
yield* Deferred.await(thread.releaseRead);
311308
}
312-
if (options.beforePosition !== undefined && (yield* Ref.get(thread.forwardReadsOnly))) {
313-
return yield* Effect.die("Unknown key beforePosition");
314-
}
315309
const reply = yield* Ref.get(thread.reply);
316310
const later = yield* Ref.get(thread.laterReply);
317311
const error = yield* Ref.get(thread.error);
@@ -358,13 +352,9 @@ const boxToolkitLayer = (thread: BoxThread) => {
358352
(entry) => options.view === "activity" || entry.type === "assistant_message",
359353
)
360354
: replies.filter((entry) => entry.id === options.itemId);
361-
const inRange = visible.filter(
362-
(entry) =>
363-
entry.position > (options.afterPosition ?? -1) &&
364-
entry.position < (options.beforePosition ?? Number.POSITIVE_INFINITY),
365-
);
366-
const matching =
367-
options.beforePosition === undefined ? inRange.slice(0, 1) : inRange.slice(-1);
355+
const matching = visible
356+
.filter((entry) => entry.position > (options.afterPosition ?? -1))
357+
.slice(0, 1);
368358
return {
369359
items: matching.map(({ id, runId: itemRunId, text, position, type }) => ({
370360
position,
@@ -1246,50 +1236,47 @@ it.effect("fails the task when the box revokes the link, and cancel interrupts i
12461236
).pipe(Effect.provide(NodeHttpServer.layerTest)),
12471237
);
12481238

1249-
it.effect.each([false, true])(
1250-
"a task's result is its own run's whole last reply, not a later run's (forward reads only: %s)",
1251-
(forwardReadsOnly) =>
1252-
Effect.scoped(
1253-
Effect.gen(function* () {
1254-
const thread = yield* makeBoxThread;
1255-
yield* Ref.set(thread.forwardReadsOnly, forwardReadsOnly);
1256-
const b = yield* serveBox(thread);
1257-
const offers = yield* SubscriptionRef.make<
1258-
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1259-
>([]);
1260-
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1261-
const a = yield* makeLaptop(database, offers);
1262-
yield* linkTo(a.links, b, "full-access");
1263-
yield* seedParent(a);
1239+
it.effect("a task's result is its own run's whole last reply, not a later run's", () =>
1240+
Effect.scoped(
1241+
Effect.gen(function* () {
1242+
const thread = yield* makeBoxThread;
1243+
const b = yield* serveBox(thread);
1244+
const offers = yield* SubscriptionRef.make<
1245+
ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>
1246+
>([]);
1247+
const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);
1248+
const a = yield* makeLaptop(database, offers);
1249+
yield* linkTo(a.links, b, "full-access");
1250+
yield* seedParent(a);
12641251

1265-
const result = yield* a.call("delegate_task", {
1266-
task: "Review the diff.",
1267-
role: "review",
1268-
target: { environmentId: box.environmentId },
1269-
clientRequestId: "review-on-box",
1270-
});
1271-
expect(result.isError, JSON.stringify(result.content)).toBe(false);
1272-
const { taskId } = result.structuredContent as { taskId: NodeId };
1273-
// The role reaches the thread there, as it does a child here.
1274-
expect(yield* Ref.get(thread.message)).toBe(
1275-
"Act as the review sub-agent for this task.\n\nReview the diff.",
1276-
);
1252+
const result = yield* a.call("delegate_task", {
1253+
task: "Review the diff.",
1254+
role: "review",
1255+
target: { environmentId: box.environmentId },
1256+
clientRequestId: "review-on-box",
1257+
});
1258+
expect(result.isError, JSON.stringify(result.content)).toBe(false);
1259+
const { taskId } = result.structuredContent as { taskId: NodeId };
1260+
// The role reaches the thread there, as it does a child here.
1261+
expect(yield* Ref.get(thread.message)).toBe(
1262+
"Act as the review sub-agent for this task.\n\nReview the diff.",
1263+
);
12771264

1278-
// A reply longer than one read, then a later run's reply on the same thread.
1279-
const long = "x".repeat(45_000);
1280-
yield* Ref.set(thread.reply, long);
1281-
yield* Ref.set(thread.laterReply, "Someone else asked a follow-up.");
1282-
yield* Ref.set(thread.status, "completed");
1283-
yield* Deferred.succeed(thread.finished, undefined);
1284-
const task = yield* taskResult(a, taskId);
1285-
expect(task?.status).toBe("completed");
1286-
expect(task?.result).toBe(long);
1287-
// Every wait was for the run the launch started.
1288-
const waits = yield* Ref.get(thread.waitedFor);
1289-
expect(waits.length).toBeGreaterThan(0);
1290-
expect(waits.every((runId) => runId === boxRunId)).toBe(true);
1291-
}),
1292-
).pipe(Effect.provide(NodeHttpServer.layerTest)),
1265+
// A reply longer than one read, then a later run's reply on the same thread.
1266+
const long = "x".repeat(45_000);
1267+
yield* Ref.set(thread.reply, long);
1268+
yield* Ref.set(thread.laterReply, "Someone else asked a follow-up.");
1269+
yield* Ref.set(thread.status, "completed");
1270+
yield* Deferred.succeed(thread.finished, undefined);
1271+
const task = yield* taskResult(a, taskId);
1272+
expect(task?.status).toBe("completed");
1273+
expect(task?.result).toBe(long);
1274+
// Every wait was for the run the launch started.
1275+
const waits = yield* Ref.get(thread.waitedFor);
1276+
expect(waits.length).toBeGreaterThan(0);
1277+
expect(waits.every((runId) => runId === boxRunId)).toBe(true);
1278+
}),
1279+
).pipe(Effect.provide(NodeHttpServer.layerTest)),
12931280
);
12941281

12951282
it.effect("a task whose thread there waits on its own task ends with the answer after it", () =>

‎apps/server/src/peer/RemoteDelegation.ts‎

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
type ProviderInstanceId,
1313
ProviderDriverKind,
1414
type RunId,
15-
THREAD_READ_NEWEST_POSITION,
1615
type ThreadId,
1716
} from "@t3tools/contracts";
1817
import * as Context from "effect/Context";
@@ -445,43 +444,26 @@ const make = Effect.gen(function* () {
445444
) {
446445
let reply: OrchestratorMcpThreadTimelineItem | undefined;
447446
let error: OrchestratorMcpThreadTimelineItem | undefined;
448-
const isReply = (item: OrchestratorMcpThreadTimelineItem) =>
449-
item.type === "assistant_message" && (runId === null || item.runId === runId);
450-
const isError = (item: OrchestratorMcpThreadTimelineItem) =>
451-
failed && item.type === "error" && (runId === null || item.runId === runId);
452-
const read = (position: {
453-
readonly afterPosition?: number;
454-
readonly beforePosition?: number;
455-
}) =>
456-
forwarding.readThread(scope, remote.environmentId, {
447+
let afterPosition: number | undefined;
448+
while (true) {
449+
const read = yield* forwarding.readThread(scope, remote.environmentId, {
457450
threadId: remote.threadId,
458451
view: failed ? "activity" : "messages",
459452
limit: 100,
460-
...position,
453+
...(afterPosition === undefined ? {} : { afterPosition }),
461454
});
462-
// A peer that predates `beforePosition` rejects it and is read forward instead;
463-
// a forward read fails again on any other cause, as it did before.
464-
const newest = yield* read({ beforePosition: THREAD_READ_NEWEST_POSITION }).pipe(
465-
Effect.orElseSucceed(() => null),
466-
);
467-
if (newest !== null && newest.previousPosition !== undefined) {
468-
let page = newest;
469-
while (true) {
470-
reply ??= page.items.findLast(isReply);
471-
error ??= page.items.findLast(isError);
472-
const previous = page.previousPosition ?? null;
473-
if ((failed ? error : reply) !== undefined || previous === null) break;
474-
page = yield* read({ beforePosition: previous });
475-
}
476-
} else {
477-
let afterPosition: number | undefined;
478-
while (true) {
479-
const page = yield* read(afterPosition === undefined ? {} : { afterPosition });
480-
reply = page.items.findLast(isReply) ?? reply;
481-
error = page.items.findLast(isError) ?? error;
482-
if (!page.hasMore || page.nextPosition === null) break;
483-
afterPosition = page.nextPosition;
455+
reply =
456+
read.items.findLast(
457+
(item) => item.type === "assistant_message" && (runId === null || item.runId === runId),
458+
) ?? reply;
459+
if (failed) {
460+
error =
461+
read.items.findLast(
462+
(item) => item.type === "error" && (runId === null || item.runId === runId),
463+
) ?? error;
484464
}
465+
if (!read.hasMore || read.nextPosition === null) break;
466+
afterPosition = read.nextPosition;
485467
}
486468
reply = error ?? reply;
487469
if (reply === undefined) return undefined;

‎docs/orchestration-v2/orchestrator-mcp-server.md‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,7 @@ reasoning, checkpoint, handoff, and runtime-request items. Large item text is
396396
bounded and reports whether it was truncated. `afterPosition` and
397397
`nextPosition` support incremental reads; `beforePosition` and
398398
`previousPosition` read back from the newest items, so a caller that wants the
399-
end of a long thread does not page through all of it. Every page still builds
400-
the thread's whole visible index, so each call costs time in proportion to the
401-
thread's length: read only the pages you need.
399+
end of a long thread does not page through all of it.
402400

403401
Thread and message results include required `createdBy` and `creationSource`
404402
provenance. MCP-created threads and user-role messages use `createdBy: "agent"`

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,6 @@ export const OrchestratorMcpThreadReadInput = Schema.Struct({
425425
});
426426
export type OrchestratorMcpThreadReadInput = typeof OrchestratorMcpThreadReadInput.Type;
427427

428-
/** A `beforePosition` past every item, so the read starts at the newest. */
429-
export const THREAD_READ_NEWEST_POSITION = Number.MAX_SAFE_INTEGER;
430-
431428
export const OrchestratorMcpThreadDetail = Schema.Struct({
432429
threadId: ThreadId,
433430
projectId: ProjectId,

0 commit comments

Comments
 (0)