import { OrchestrationV2ThreadBoundedSnapshot, ThreadId } from "@t3tools/contracts";
import { describe, expect, it } from "@effect/vitest";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { PrimaryConnectionTarget, type PreparedConnection } from "../connection/model.ts";
import * as RpcHttp from "../rpc/http.ts";
import * as BoundedThreadSnapshotHttp from "./boundedThreadSnapshotHttp.ts";
import * as ThreadSnapshotLoader from "./threadSnapshotHttp.ts";
const TARGET = new PrimaryConnectionTarget({
environmentId: "environment-bounded" as never,
label: "Bounded",
httpBaseUrl: "https://environment.example.test",
wsBaseUrl: "wss://environment.example.test",
});
const PREPARED: PreparedConnection = {
environmentId: TARGET.environmentId,
label: TARGET.label,
httpBaseUrl: TARGET.httpBaseUrl,
socketUrl: TARGET.wsBaseUrl,
httpAuthorization: null,
target: TARGET,
};
const THREAD_ID = ThreadId.make("thread-bounded");
const NOW = "2026-06-20T00:00:00.000Z";
const FULL_SNAPSHOT_BODY = {
snapshotSequence: 9,
projection: {
thread: {
createdBy: "user",
creationSource: "web",
id: String(THREAD_ID),
projectId: "project-1",
title: "Full fallback thread",
providerInstanceId: "codex",
modelSelection: { instanceId: "codex", model: "gpt-5" },
runtimeMode: "full-access",
interactionMode: "default",
branch: null,
worktreePath: null,
activeProviderThreadId: null,
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: String(THREAD_ID),
},
forkedFrom: null,
createdAt: NOW,
updatedAt: NOW,
archivedAt: null,
deletedAt: null,
settledOverride: null,
settledAt: null,
},
runs: [],
attempts: [],
nodes: [],
subagents: [],
providerSessions: [],
providerThreads: [],
providerTurns: [],
runtimeRequests: [],
messages: [],
plans: [],
turnItems: [],
checkpointScopes: [],
checkpoints: [],
contextHandoffs: [],
contextTransfers: [],
visibleTurnItems: [],
updatedAt: NOW,
},
};
const AT = "2026-06-20T00:00:00.000Z";
const command = (id: string, ordinal: number) => ({
id,
type: "command_execution",
threadId: String(THREAD_ID),
runId: null,
nodeId: null,
providerThreadId: null,
providerTurnId: null,
nativeItemRef: null,
parentItemId: null,
ordinal,
status: "completed",
title: null,
input: id,
exitCode: 0,
startedAt: AT,
completedAt: AT,
updatedAt: AT,
});
const LOCAL_ITEMS = [command("local-1", 1), command("local-2", 2)];
const DEPENDENCY = command("older-dependency", 0);
const VISIBLE = LOCAL_ITEMS.map((item, position) => ({
position,
visibility: "local",
sourceThreadId: String(THREAD_ID),
sourceItemId: item.id,
item,
}));
const boundedBody = (compact: boolean) => ({
snapshotSequence: 12,
projection: {
...FULL_SNAPSHOT_BODY.projection,
turnItems: compact ? [DEPENDENCY] : [...LOCAL_ITEMS, DEPENDENCY],
visibleTurnItems: VISIBLE,
},
historyCursor: "bounded-cursor",
hasMoreHistory: true,
latestLocalTurnOrdinal: 2,
...(compact ? { turnItemsOmitLocalVisible: true } : {}),
});
const BoundedJson = Schema.toCodecJson(OrchestrationV2ThreadBoundedSnapshot);
const idTableBody = () => {
const decodeBoundedJson = Schema.decodeSync(BoundedJson);
const snapshot = decodeBoundedJson(boundedBody(true));
const encodeBoundedJson = Schema.encodeSync(BoundedJson);
return encodeBoundedJson({ ...snapshot, idTable: true });
};
describe("boundedThreadSnapshotLoader", () => {
it.effect.each([
["an older server that ignores the opt-in", () => boundedBody(false)],
["a server that sends compact turnItems", () => boundedBody(true)],
["a server that also sends an ID table", idTableBody],
] as const)("requests compact turnItems and loads %s", ([, body]) => {
const urls: string[] = [];
const fetchFn = ((input: RequestInfo | URL) => {
urls.push(String(input));
return Promise.resolve(Response.json(body()));
}) satisfies typeof fetch;
return Effect.gen(function* () {
const loader = yield* ThreadSnapshotLoader.ThreadSnapshotLoader;
const result = yield* loader.load(PREPARED, THREAD_ID);
expect(new URL(urls[0]!).searchParams.get("compactTurnItems")).toBe("1");
expect(new URL(urls[0]!).searchParams.get("compactIds")).toBe("1");
expect(result._tag).toBe("present");
if (result._tag !== "present") return;
expect(result.snapshot.projection.turnItems.map((item) => String(item.id))).toEqual([
"local-1",
"local-2",
"older-dependency",
]);
expect("turnItemsOmitLocalVisible" in result.snapshot).toBe(false);
expect(result.history).toEqual({
historyCursor: "bounded-cursor",
hasMoreHistory: true,
latestLocalTurnOrdinal: 2,
});
}).pipe(
Effect.provide(
Layer.provide(BoundedThreadSnapshotHttp.layer, RpcHttp.layerRemoteHttpClient(fetchFn)),
),
);
});
it.effect("treats structured EnvironmentResourceNotFoundError from bounded as missing", () => {
const fetchFn = ((input: RequestInfo | URL) => {
const url = String(input);
expect(url).toContain(`/api/orchestration/threads/${THREAD_ID}/bounded`);
return Promise.resolve(
Response.json(
{
_tag: "EnvironmentResourceNotFoundError",
code: "not_found",
reason: "thread_not_found",
traceId: "trace-bounded-missing",
},
{ status: 404 },
),
);
}) satisfies typeof fetch;
return Effect.gen(function* () {
const loader = yield* ThreadSnapshotLoader.ThreadSnapshotLoader;
const result = yield* loader.load(PREPARED, THREAD_ID);
expect(result).toEqual({ _tag: "missing" });
}).pipe(
Effect.provide(
Layer.provide(BoundedThreadSnapshotHttp.layer, RpcHttp.layerRemoteHttpClient(fetchFn)),
),
);
});
it.effect(
"uses socket fallback directly when the bounded endpoint has a transient failure",
() => {
const fetchFn = ((input: RequestInfo | URL) => {
expect(String(input)).toContain("/bounded");
return Promise.resolve(new Response("Service Unavailable", { status: 503 }));
}) satisfies typeof fetch;
return Effect.gen(function* () {
const loader = yield* ThreadSnapshotLoader.ThreadSnapshotLoader;
const result = yield* loader.load(PREPARED, THREAD_ID);
expect(result).toEqual({ _tag: "unavailable" });
}).pipe(
Effect.provide(
Layer.provide(BoundedThreadSnapshotHttp.layer, RpcHttp.layerRemoteHttpClient(fetchFn)),
),
);
},
);
});