TypeScript
perf(server): avoid unused history when acknowledging tasks (#27803)
Adamulek1238f1e61cc7f35
diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.tsindex da4ad5c6d35..a62a0cec644 100644--- a/apps/server/src/mcp/OrchestratorMcpService.ts+++ b/apps/server/src/mcp/OrchestratorMcpService.ts@@ -1703,7 +1703,17 @@ const make = Effect.gen(function* () {task.completionDelivery?.state !== "acknowledged" &&task.completionDelivery?.state !== "disposed") {- const parentProjection = yield* loadProjection(scope.thread.threadId);+ const parentThreadId = scope.thread.threadId;+ const parentProjection = yield* threadManagement+ .getThreadRecords(parentThreadId, ["runs"])+ .pipe(+ Effect.mapError((error) =>+ failure(+ "orchestration_error",+ `Unable to read thread ${parentThreadId}: ${errorMessage(error)}`,+ ),+ ),+ );const observingRun = ThreadManagementService.latestActiveRun(parentProjection);yield* threadManagement.dispatch({diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 0a5e56d98d6..db398ebf8aa 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -2378,8 +2378,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functioconst projection = yield* projectionStore.getThreadRecords(command.parentThreadId,- ["subagents", "runs", "messages", "nodes", "attempts", "turnItems"],- { turnItemTypes: [], messageRoles: ["user"] },+ ["subagents", "runs", "nodes", "attempts", "turnItems"],+ { turnItemTypes: [] },).pipe(Effect.mapError(@@ -2467,8 +2467,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functioreturn;}+ const { messages } = yield* projectionStore+ .getThreadRecords(command.parentThreadId, ["messages"], {+ messageRoles: ["user"],+ messageIds: [delivery.messageId],+ })+ .pipe(+ Effect.mapError(+ (cause) => new OrchestratorProjectionError({ threadId: command.parentThreadId, cause }),+ ),+ );+ const deliveryProjection = { ...projection, messages };const remainingTaskIds = delivery.taskIds.filter((taskId) => taskId !== task.id);- const deliveryRun = completionDeliveryRun(projection, delivery);+ const deliveryRun = completionDeliveryRun(deliveryProjection, delivery);const clearDelivery =remainingTaskIds.length === 0 &&(deliveryRun === undefined || deliveryRun.status === "queued");@@ -2505,7 +2516,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio});return parentRun.id;}- const message = completionDeliveryMessage(projection, delivery);+ const message = completionDeliveryMessage(deliveryProjection, delivery);if (message !== undefined) {yield* emitEvent({type: "message.updated",diff --git a/apps/server/src/peer/RemoteDelegation.test.ts b/apps/server/src/peer/RemoteDelegation.test.tsindex d016bb523fd..3cb268ffebf 100644--- a/apps/server/src/peer/RemoteDelegation.test.ts+++ b/apps/server/src/peer/RemoteDelegation.test.ts@@ -29,6 +29,7 @@ import * as Queue from "effect/Queue";import * as Ref from "effect/Ref";import * as Schedule from "effect/Schedule";import * as Stream from "effect/Stream";+import * as Tracer from "effect/Tracer";import * as SubscriptionRef from "effect/SubscriptionRef";import type * as SqlClient from "effect/sql/SqlClient";import { McpSchema, McpServer } from "effect/ai";@@ -845,15 +846,155 @@ it.effect("a task delegated to the box wakes the laptop's parent when it ends th);expect(Option.isSome(offered)).toBe(true);- const status = yield* a.call("task_status", { taskId: delegated.taskId });+ const messageReads: Array<string> = [];+ const tracer = Tracer.make({+ span(options) {+ const span = new Tracer.NativeSpan(options);+ const end = span.end.bind(span);+ span.end = (endTime, exit) => {+ end(endTime, exit);+ const query = span.attributes.get("db.query.text");+ if (+ typeof query === "string" &&+ /SELECT[\s\S]*FROM orchestration_v2_projection_messages/i.test(query)+ ) {+ messageReads.push(query);+ }+ };+ return span;+ },+ });+ const status = yield* a+ .call("task_status", { taskId: delegated.taskId })+ .pipe(Effect.withTracer(tracer));expect(status.structuredContent).toMatchObject({status: "completed",summary: "All 412 tests pass on the box.",});+ // Delivery validation still reads messages; choosing the observer does not need another read.+ expect(messageReads).toHaveLength(2);+ expect(messageReads.filter((query) => !/\bmessage_id\s+IN\b/i.test(query))).toHaveLength(1);+ const acknowledged = yield* a.projections.getThreadRecords(parentThreadId, ["subagents"], {+ subagentIds: [delegated.taskId],+ });+ expect(acknowledged.subagents[0]?.completionDelivery).toEqual({+ state: "acknowledged",+ observedByRunId: parentRunId,+ });}),).pipe(Effect.provide(NodeHttpServer.layerTest)),);+it.effect.each(["no active parent run", "a newer run owned by another provider"] as const)(+ "preserves remote observation guards with %s",+ (observer) =>+ Effect.scoped(+ Effect.gen(function* () {+ const thread = yield* makeBoxThread;+ const b = yield* serveBox(thread);+ const offers = yield* SubscriptionRef.make<+ ReadonlyArray<ProviderContinuationRequests.ProviderContinuationRequest>+ >([]);+ const database = yield* SqlitePersistence.layerMemory.pipe(Layer.build);+ const a = yield* makeLaptop(database, offers);+ yield* linkTo(a.links, b, "full-access");+ yield* seedParent(a);+ const delegated = yield* delegateToBox(a);++ yield* Ref.set(thread.reply, "Remote result remains available.");+ yield* Ref.set(thread.status, "completed");+ yield* Deferred.succeed(thread.finished, undefined);+ yield* taskResult(a, delegated.taskId);+ yield* SubscriptionRef.changes(offers).pipe(+ Stream.filter((seen) => seen.some((offer) => offer.threadId === parentThreadId)),+ Stream.runHead,+ );++ const parent = yield* a.projections.getThreadRecords(+ parentThreadId,+ ["runs", "subagents"],+ {+ subagentIds: [delegated.taskId],+ },+ );+ expect(parent.subagents[0]?.completionDelivery?.state).not.toBe("acknowledged");+ const parentRun = parent.runs.find((run) => run.id === parentRunId);+ if (parentRun === undefined) return yield* Effect.die("The parent run was not seeded.");+ const now = yield* DateTime.now;+ const otherInstanceId = ProviderInstanceId.make("codex-other");+ const observingRun: OrchestrationV2Run =+ observer === "no active parent run"+ ? { ...parentRun, status: "completed", completedAt: now }+ : {+ id: RunId.make("run:laptop-other-provider"),+ threadId: parentThreadId,+ ordinal: parentRun.ordinal + 1,+ providerInstanceId: otherInstanceId,+ modelSelection: { instanceId: otherInstanceId, model: "gpt-5.4" },+ providerThreadId: null,+ userMessageId: MessageId.make("message:laptop-other-provider"),+ rootNodeId: null,+ activeAttemptId: null,+ status: "preparing",+ requestedAt: now,+ startedAt: null,+ completedAt: null,+ checkpointId: null,+ contextHandoffId: null,+ };+ yield* a.sink.write({+ commandId: CommandId.make("command:change-remote-result-observer"),+ events: [+ {+ id: EventId.make("event:change-remote-result-observer"),+ type: "run.updated",+ threadId: parentThreadId,+ runId: observingRun.id,+ providerInstanceId: observingRun.providerInstanceId,+ occurredAt: now,+ payload: observingRun,+ },+ ],+ });++ const status = yield* a.call("task_status", { taskId: delegated.taskId });+ if (observer === "no active parent run") {+ expect(status.isError).toBe(true);+ expect(status.content).toEqual([+ {+ type: "text",+ text: JSON.stringify({+ _tag: "OrchestratorMcpFailure",+ code: "parent_not_active",+ message: "The calling provider no longer owns an active thread run.",+ }),+ },+ ]);+ const unacknowledged = yield* a.projections.getThreadRecords(+ parentThreadId,+ ["subagents"],+ { subagentIds: [delegated.taskId] },+ );+ expect(unacknowledged.subagents[0]?.result).toBe("Remote result remains available.");+ expect(unacknowledged.subagents[0]?.completionDelivery?.state).not.toBe("acknowledged");+ return;+ }+ expect(status.isError, JSON.stringify(status.content)).not.toBe(true);+ expect(status.structuredContent).toMatchObject({+ status: "completed",+ summary: "Remote result remains available.",+ });+ const acknowledged = yield* a.projections.getThreadRecords(parentThreadId, ["subagents"], {+ subagentIds: [delegated.taskId],+ });+ expect(acknowledged.subagents[0]?.completionDelivery).toEqual({+ state: "acknowledged",+ observedByRunId: null,+ });+ }),+ ).pipe(Effect.provide(NodeHttpServer.layerTest)),+);+it.effect("with the beta off here, a task already running there still ends, and no new one starts",() =>