fix(composer): read dropped threads across environments (#15210)

This commit is contained in:
Bilal Bakr authored and GitHub committed 2026-10-14 21:58:12 -05:00
1 parent ec1313c8f8
commit 55dfc5fc4d
21 files changed
+1164 -108

No files matched your search

@@ -23,9 +23,11 @@ import {
type OrchestrationV2Run,
type OrchestrationV2ThreadShellSnapshot,
type OrchestrationV2ThreadProjection,
type OrchestrationV2ThreadTranscript,
type OrchestrationV2ThreadShell,
type OrchestrationV2TurnItem,
ProjectId,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
ProviderInstanceId,
resolveProjectProviderInstanceEnabled,
RunId,
@@ -33,6 +35,7 @@ import {
ThreadId,
type TurnItemId,
} from "@t3tools/contracts";
import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Duration from "effect/Duration";
@@ -45,6 +48,8 @@ import * as Stream from "effect/Stream";
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
import * as Orchestrator from "./Orchestrator.ts";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import { projectedRowEncodedBytes } from "./threadHistoryPaging.ts";
import * as ProviderSessionManager from "./ProviderSessionManager.ts";
import * as ThreadCommandExecutor from "./ThreadCommandExecutor.ts";
import { DispatchModeLimit, exceededDispatchModeLimit } from "./DispatchModeLimit.ts";
@@ -186,6 +191,15 @@ export class ThreadManagementThreadNotFoundError extends Schema.TaggedError<Thre
}
}
export class ThreadTranscriptTooLargeError extends Schema.TaggedError<ThreadTranscriptTooLargeError>()(
"ThreadTranscriptTooLargeError",
{ threadId: ThreadId },
) {
override get message(): string {
return "The thread transcript exceeds the file attachment limit.";
}
}
export class ThreadManagementRunNotFoundError extends Schema.TaggedError<ThreadManagementRunNotFoundError>()(
"ThreadManagementRunNotFoundError",
{
@@ -336,6 +350,13 @@ export interface ThreadManagementServiceShape {
) => Effect.Effect<OrchestrationV2ThreadProjection, Orchestrator.OrchestratorV2Error>;
readonly getCheckpointContext: Orchestrator.OrchestratorV2["Service"]["getCheckpointContext"];
readonly getThreadSnapshot: Orchestrator.OrchestratorV2["Service"]["getThreadSnapshot"];
readonly getThreadTranscript: (
threadId: ThreadId,
) => Effect.Effect<
OrchestrationV2ThreadTranscript,
Orchestrator.OrchestratorV2Error | ThreadTranscriptTooLargeError,
ServerEnvironment.ServerEnvironmentIdentity
>;
readonly getThreadSnapshotWindow: Orchestrator.OrchestratorV2["Service"]["getThreadSnapshotWindow"];
readonly getProjectThreadRecords: <K extends ProjectionRecordField>(
input: { readonly projectId: ProjectId; readonly threadId: ThreadId },
@@ -627,6 +648,32 @@ const make = Effect.gen(function* () {
ensureProjectionTranscript(threadId).pipe(
Effect.andThen(orchestrator.getThreadSnapshot(threadId)),
);
const getThreadTranscript = Effect.fn("orchestrationV2.threadManagement.getThreadTranscript")(
function* (threadId: ThreadId) {
const { projection } = yield* getThreadSnapshot(threadId);
const environment = yield* ServerEnvironment.ServerEnvironmentIdentity;
const transcript = {
threadId: projection.thread.id,
title: projection.thread.title,
updatedAt: projection.updatedAt,
items: projection.visibleTurnItems,
};
let sizeBytes = Buffer.byteLength(
threadTranscriptHeader(yield* environment.getEnvironmentId, transcript),
"utf8",
);
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
return yield* new ThreadTranscriptTooLargeError({ threadId });
}
for (const row of transcript.items) {
sizeBytes += projectedRowEncodedBytes(row) + 1;
if (sizeBytes > PROVIDER_SEND_TURN_MAX_FILE_BYTES) {
return yield* new ThreadTranscriptTooLargeError({ threadId });
}
}
return transcript;
},
);
const getThreadSnapshotWindow: ThreadManagementServiceShape["getThreadSnapshotWindow"] = (
threadId,
options,
@@ -1161,6 +1208,7 @@ const make = Effect.gen(function* () {
getThreadProjection,
getCheckpointContext,
getThreadSnapshot,
getThreadTranscript,
getThreadSnapshotWindow,
getProjectThreadRecords,
getProjectThread,
@@ -5,6 +5,7 @@ import {
EnvironmentAuthenticatedAuth,
EnvironmentAuthenticatedPrincipal,
EnvironmentHttpApi,
EnvironmentId,
EventId,
MessageId,
NodeId,
@@ -34,6 +35,7 @@ import * as HttpApi from "effect/http-api/HttpApi";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import { Etag, HttpRouter } from "effect/http";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts";
import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts";
@@ -226,6 +228,9 @@ const seed = Effect.gen(function* () {
// thread routes never touch projects.
const TestLayer = Layer.mergeAll(
management,
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
getEnvironmentId: Effect.succeed(EnvironmentId.make("compact-transport")),
}),
Layer.effectDiscard(seed),
Layer.mock(OrchestrationEventStore.OrchestrationEventStore)({}),
Layer.mock(ProjectStore.ProjectStoreV2)({}),
@@ -0,0 +1,244 @@
import {
AuthSessionId,
type AuthEnvironmentScope,
EnvironmentAuthenticatedAuth,
EnvironmentAuthenticatedPrincipal,
EnvironmentHttpApi,
EnvironmentId,
EventId,
ORCHESTRATION_PROTOCOL_HEADER,
ORCHESTRATION_PROTOCOL_VERSION_TEXT,
OrchestrationV2AppThread,
OrchestrationV2TurnItem,
OrchestrationV2ThreadTranscript,
PROVIDER_SEND_TURN_MAX_FILE_BYTES,
ThreadId,
} from "@t3tools/contracts";
import { expect, it } from "@effect/vitest";
import { threadTranscriptHeader } from "@t3tools/shared/threadTranscript";
import * as Effect from "effect/Effect";
import * as Context from "effect/Context";
import * as DateTime from "effect/DateTime";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import * as HttpRouter from "effect/http/HttpRouter";
import * as HttpPlatform from "effect/http/HttpPlatform";
import * as Etag from "effect/http/Etag";
import * as NodeServices from "@effect/platform-node/NodeServices";
import * as HttpApi from "effect/http-api/HttpApi";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import * as SqlitePersistence from "../persistence/Sqlite.ts";
import * as ServerSettings from "../serverSettings.ts";
import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts";
import { ProjectEnrichmentService } from "../project/ProjectEnrichmentService.ts";
import * as OrchestrationHttp from "./http.ts";
import { EventSinkV2 } from "./EventSink.ts";
import * as ProjectStore from "./ProjectStore.ts";
import * as CommandReceiptStore from "./CommandReceiptStore.ts";
import * as ThreadManagementService from "./ThreadManagementService.ts";
import * as ProviderAdapterRegistry from "./ProviderAdapterRegistry.ts";
import * as ProviderReplayHarness from "./testkit/ProviderReplayHarness.ts";
class TranscriptTestApi extends HttpApi.make("environment").add(
EnvironmentHttpApi.groups.orchestration,
) {}
const decodeTranscript = Schema.decodeUnknownEffect(
Schema.toCodecJson(OrchestrationV2ThreadTranscript),
);
const now = DateTime.makeUnsafe("2026-09-29T00:00:00.000Z");
const environmentId = EnvironmentId.make("source-بيئة");
const output = "Full tool output. ".repeat(4_000);
const thread = Schema.decodeUnknownSync(OrchestrationV2AppThread)({
id: "source-thread",
projectId: "source-project",
title: "Source 界\nconversation",
providerInstanceId: "codex",
modelSelection: { instanceId: "codex", model: "gpt-5.4" },
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage: { rootThreadId: "source-thread", parentThreadId: null, relationshipToParent: null },
forkedFrom: null,
createdBy: "user",
creationSource: "web",
createdAt: now,
updatedAt: now,
archivedAt: null,
settledOverride: null,
settledAt: null,
lastVisitedAt: null,
deletedAt: null,
});
const items = Schema.decodeUnknownSync(Schema.Array(OrchestrationV2TurnItem))(
Array.from({ length: 200 }, (_, position) => ({
id: `item-${position}`,
threadId: "source-thread",
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal: position,
type: "command_execution",
status: "completed",
title: "Read notes",
input: "cat notes.md",
output: position === 0 ? output : "Done",
startedAt: now,
completedAt: now,
updatedAt: now,
})),
);
it.effect("exports full history with size, scope, and missing-thread checks", () =>
Effect.gen(function* () {
let allowed = true;
const runtime = ProviderReplayHarness.layerWithRegistry(
{ name: "thread-transcript" },
ProviderAdapterRegistry.layerFromAdapters([]),
{ runEffectWorker: false },
);
const services = yield* Layer.build(
Layer.mergeAll(
runtime,
ThreadManagementService.layer.pipe(
Layer.provide(
Layer.mergeAll(runtime, CommandReceiptStore.layer, ServerSettings.layerTest({})),
),
),
OrchestrationEventStore.layer,
ProjectStore.layer,
).pipe(Layer.provideMerge(SqlitePersistence.layerMemory)),
);
const eventSink = Context.get(services, EventSinkV2);
yield* eventSink.write({
events: [
{
id: EventId.make("create-source"),
type: "thread.created",
threadId: thread.id,
occurredAt: now,
payload: thread,
},
],
});
const routes = HttpApiBuilder.layer(TranscriptTestApi).pipe(
Layer.provide(OrchestrationHttp.layer),
Layer.provide(
Layer.succeed(ServerEnvironment.ServerEnvironmentIdentity, {
getEnvironmentId: Effect.succeed(environmentId),
}),
),
Layer.provide(
Layer.succeed(EnvironmentAuthenticatedAuth, (effect) =>
Effect.suspend(() =>
effect.pipe(
Effect.provideService(EnvironmentAuthenticatedPrincipal, {
sessionId: AuthSessionId.make("test-session"),
subject: "test",
method: "bearer-access-token",
scopes: new Set<AuthEnvironmentScope>(allowed ? ["orchestration:read"] : []),
}),
),
),
),
),
Layer.provide(Layer.succeedContext(services)),
Layer.provide(Layer.mock(ProjectEnrichmentService)({})),
Layer.provide(HttpPlatform.layer),
Layer.provide(Etag.layerWeak),
Layer.provide(NodeServices.layer),
);
const { handler, dispose } = HttpRouter.toWebHandler(routes, { disableLogger: true });
yield* Effect.addFinalizer(() => Effect.promise(dispose));
const read = (threadId: ThreadId) =>
Effect.promise(() =>
handler(
new Request(`http://localhost/api/orchestration/threads/${threadId}/transcript`, {
headers: { [ORCHESTRATION_PROTOCOL_HEADER]: ORCHESTRATION_PROTOCOL_VERSION_TEXT },
}),
),
);
const emptyResponse = yield* read(thread.id);
expect(emptyResponse.status).toBe(200);
expect(
(yield* decodeTranscript(yield* Effect.promise(() => emptyResponse.json()))).items,
).toEqual([]);
yield* eventSink.write({
events: items.map((item) => ({
id: EventId.make(`create-${item.id}`),
type: "turn-item.updated" as const,
threadId: thread.id,
occurredAt: now,
payload: item,
})),
});
const response = yield* read(thread.id);
expect(response.status).toBe(200);
const transcript = yield* decodeTranscript(yield* Effect.promise(() => response.json()));
expect(transcript.items).toHaveLength(200);
expect(transcript.items[0]?.item).toMatchObject({ output });
expect(transcript.items[0]?.visibility).toBe("local");
expect(transcript.items[199]?.position).toBe(199);
const emptyOutputRows = transcript.items.map((row, position) =>
position === 0 ? { ...row, item: { ...row.item, output: "" } } : row,
);
const header = threadTranscriptHeader(environmentId, transcript);
const baseSize =
Buffer.byteLength(header, "utf8") +
emptyOutputRows.reduce(
(size, row) => size + Buffer.byteLength(`${JSON.stringify(row)}\n`, "utf8"),
0,
);
const boundaryOutput = "界" + "x".repeat(PROVIDER_SEND_TURN_MAX_FILE_BYTES - baseSize - 3);
for (const [suffix, expectedStatus] of [
["", 200],
["x", 400],
] as const) {
yield* eventSink.write({
events: [
{
id: EventId.make(`boundary-${expectedStatus}`),
type: "turn-item.updated",
threadId: thread.id,
occurredAt: now,
payload: {
...items.find((item) => item.type === "command_execution")!,
output: boundaryOutput + suffix,
},
},
],
});
const response = yield* read(thread.id);
expect(response.status).toBe(expectedStatus);
if (expectedStatus === 400) {
const error = yield* Context.get(services, ThreadManagementService.ThreadManagementService)
.getThreadTranscript(thread.id)
.pipe(
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, {
getEnvironmentId: Effect.succeed(environmentId),
}),
Effect.flip,
);
expect(error).toBeInstanceOf(ThreadManagementService.ThreadTranscriptTooLargeError);
expect(yield* Effect.promise(() => response.json())).toMatchObject({
_tag: "EnvironmentRequestInvalidError",
reason: "thread_transcript_too_large",
});
}
}
expect((yield* read(ThreadId.make("missing"))).status).toBe(404);
allowed = false;
expect((yield* read(thread.id)).status).toBe(403);
}),
);
+32
View File
@@ -5,6 +5,7 @@ import {
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Predicate from "effect/Predicate";
import * as Schema from "effect/Schema";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import * as SqlClient from "effect/sql/SqlClient";
@@ -15,6 +16,7 @@ import {
failEnvironmentNotFound,
requireEnvironmentScope,
} from "../auth/http.ts";
import * as ServerEnvironment from "../environment/ServerEnvironment.ts";
import { traceLocalHandlerWork } from "../cloud/traceRelayRequest.ts";
import * as OrchestrationEventStore from "../persistence/OrchestrationEventStore.ts";
import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts";
@@ -32,6 +34,10 @@ import { buildActiveShellSnapshot, loadShellSnapshotParts } from "./ShellStream.
import { boundedSnapshotResponseFields } from "./ThreadStream.ts";
import { projectThreadProjectionForWire } from "./WireProjection.ts";
const isThreadTranscriptTooLargeError = Schema.is(
ThreadManagementService.ThreadTranscriptTooLargeError,
);
function isThreadNotFound(error: unknown): boolean {
return (
Predicate.hasProperty(error, "cause") &&
@@ -51,6 +57,7 @@ export const layer = HttpApiBuilder.group(
"orchestration",
Effect.fnUntraced(function* (handlers) {
const sql = yield* SqlClient.SqlClient;
const environment = yield* ServerEnvironment.ServerEnvironmentIdentity;
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
const projectStore = yield* ProjectStore.ProjectStoreV2;
@@ -177,6 +184,31 @@ export const layer = HttpApiBuilder.group(
};
}),
)
.handle(
"threadTranscript",
Effect.fn("environment.orchestration.threadTranscript")(function* (args) {
yield* annotateEnvironmentRequest(args.endpoint.name);
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
return yield* threadManagement.getThreadTranscript(args.params.threadId).pipe(
Effect.provideService(ServerEnvironment.ServerEnvironmentIdentity, environment),
traceLocalHandlerWork,
Effect.catch(
Effect.fnUntraced(function* (error) {
if (isThreadTranscriptTooLargeError(error)) {
return yield* failEnvironmentInvalidRequest("thread_transcript_too_large");
}
if (isThreadNotFound(error)) {
return yield* failEnvironmentNotFound("thread_not_found");
}
return yield* failEnvironmentInternal(
"orchestration_thread_snapshot_failed",
error,
);
}),
),
);
}),
)
.handle(
"threadBoundedSnapshot",
Effect.fn("environment.orchestration.threadBoundedSnapshot")(function* (args) {
@@ -203,6 +203,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
getThreadProjection: unused,
getCheckpointContext: unused,
getThreadSnapshot: unused,
getThreadTranscript: unused,
getThreadSnapshotWindow: unused,
getProjectThreadRecords: () => Effect.die("unused project record read"),
getProjectThread: unused,