Skip to content
This repository was archived by the owner on Oct 21, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
type OrchestrationEvent,
type OrchestrationThread,
type ProviderApprovalDecision,
type ServerProvider,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Exit from "effect/Exit";
Expand Down Expand Up @@ -39,6 +40,7 @@ import { makeProviderRegistryLayer } from "../src/provider/testUtils/providerReg
import { ProviderSessionDirectoryLive } from "../src/provider/Layers/ProviderSessionDirectory.ts";
import { ServerSettingsService } from "../src/serverSettings.ts";
import * as AutomationService from "../src/automation/AutomationService.ts";
import * as DelegationService from "../src/delegation/DelegationService.ts";
import * as StorageCleanup from "../src/storageCleanup.ts";
import { makeProviderServiceLive } from "../src/provider/Layers/ProviderService.ts";
import { makeCodexAdapter } from "../src/provider/Layers/CodexAdapter.ts";
Expand Down Expand Up @@ -188,6 +190,7 @@ export interface OrchestrationIntegrationHarness {
readonly engine: OrchestrationEngineShape;
readonly snapshotQuery: ProjectionSnapshotQuery["Service"];
readonly providerService: ProviderService["Service"];
readonly delegation: DelegationService.DelegationService["Service"];
readonly checkpointStore: CheckpointStore.CheckpointStore["Service"];
readonly pendingApprovalRepository: ProjectionPendingApprovalRepository["Service"];
readonly waitForThread: (
Expand Down Expand Up @@ -233,6 +236,8 @@ export interface OrchestrationIntegrationHarness {
interface MakeOrchestrationIntegrationHarnessOptions {
readonly provider?: ProviderDriverKind;
readonly realCodex?: boolean;
/** What the provider registry reports, for flows that pick a provider themselves. */
readonly providers?: ReadonlyArray<ServerProvider>;
/** Tracer for every fiber the harness runtime runs, including reactors. */
readonly tracer?: Tracer.Tracer;
}
Expand Down Expand Up @@ -305,7 +310,7 @@ export const makeOrchestrationIntegrationHarness = (
Layer.provide(AnalyticsService.layerTest),
Layer.provide(providerEventLoggersLayer),
);
const providerRegistryLayer = makeProviderRegistryLayer();
const providerRegistryLayer = makeProviderRegistryLayer(options?.providers);

const checkpointStoreLayer = CheckpointStore.layer.pipe(Layer.provide(VcsDriverRegistry.layer));
const projectionSnapshotQueryLayer = OrchestrationProjectionSnapshotQueryLive;
Expand Down Expand Up @@ -387,6 +392,7 @@ export const makeOrchestrationIntegrationHarness = (
Layer.provideMerge(
Layer.mock(AutomationService.AutomationService)({ start: () => Effect.void }),
),
Layer.provideMerge(DelegationService.layer),
Layer.provideMerge(
Layer.succeed(StorageCleanup.StorageCleanup, {
start: () => Effect.void,
Expand Down Expand Up @@ -463,6 +469,9 @@ export const makeOrchestrationIntegrationHarness = (
const providerService = yield* tryRuntimePromise("load ProviderService service", () =>
runtime.runPromise(Effect.service(ProviderService)),
).pipe(Effect.orDie);
const delegation = yield* tryRuntimePromise("load DelegationService service", () =>
runtime.runPromise(Effect.service(DelegationService.DelegationService)),
).pipe(Effect.orDie);
const checkpointStore = yield* tryRuntimePromise("load CheckpointStore service", () =>
runtime.runPromise(Effect.service(CheckpointStore.CheckpointStore)),
).pipe(Effect.orDie);
Expand Down Expand Up @@ -610,6 +619,7 @@ export const makeOrchestrationIntegrationHarness = (
engine,
snapshotQuery,
providerService,
delegation,
checkpointStore,
pendingApprovalRepository,
waitForThread,
Expand Down
194 changes: 194 additions & 0 deletions apps/server/integration/orchestrationEngine.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ThreadId,
ModelSelection,
ProviderInstanceId,
type ServerProvider,
} from "@t3tools/contracts";
import { assert, it } from "@effect/vitest";
import * as Clock from "effect/Clock";
Expand Down Expand Up @@ -1416,3 +1417,196 @@ it.live("reverts claudeAgent turns and rolls back provider conversation state",
CLAUDE_AGENT_PROVIDER,
),
);

const delegateToCodex = (input: {
readonly childEvents: TestTurnResponse["events"];
readonly expectedResult: ReadonlyArray<string>;
}) =>
Effect.acquireUseRelease(
makeOrchestrationIntegrationHarness({
provider: CODEX_PROVIDER,
providers: [
{
instanceId: defaultInstanceIdForDriver(CODEX_PROVIDER),
driver: CODEX_PROVIDER,
displayName: "Codex",
enabled: true,
installed: true,
status: "ready",
models: [
{
slug: DEFAULT_MODEL_BY_PROVIDER[CODEX_PROVIDER] ?? DEFAULT_MODEL,
name: "Default",
isCustom: false,
isDefault: true,
},
],
} as unknown as ServerProvider,
],
}),
(harness) =>
Effect.gen(function* () {
yield* seedProjectAndThread(harness);
yield* harness.adapterHarness!.queueTurnResponseForNextSession({
events: input.childEvents,
});
// The caller's turn that reads the result.
yield* harness.adapterHarness!.queueTurnResponseForNextSession({ events: [] });

const { threadId: child } = yield* harness.delegation.delegate({
parentThreadId: THREAD_ID,
provider: "codex",
prompt: "Review the uncommitted changes.",
title: "Review changes",
});

const resultId = asMessageId(`delegation:${child}`);
const parent = yield* harness.waitForThread(THREAD_ID, (thread) =>
thread.messages.some((message) => message.id === resultId),
);
const result = parent.messages.find((message) => message.id === resultId)!;
for (const expected of input.expectedResult) {
assert.include(result.text, expected);
}
assert.notInclude(result.text, "It produced no final message.");
}),
(harness) => harness.dispose,
).pipe(Effect.provide(NodeServices.layer));

it.live("delegation reports a buffered final message once the delegated turn completes", () =>
delegateToCodex({
childEvents: [
{
type: "turn.started",
...runtimeBase("evt-delegate-done-1", "2026-02-24T11:00:00.000Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
},
{
// No paragraph break, so the text stays buffered until the turn ends.
type: "message.delta",
...runtimeBase("evt-delegate-done-2", "2026-02-24T11:00:00.100Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
delta: "Found two bugs.",
},
{
type: "turn.completed",
...runtimeBase("evt-delegate-done-3", "2026-02-24T11:00:00.200Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
status: "completed",
},
],
expectedResult: ['Codex finished the delegated task "Review changes".', "Found two bugs."],
}),
);

it.live("delegation reports the buffered partial message of an aborted delegated turn", () =>
delegateToCodex({
childEvents: [
{
type: "turn.started",
...runtimeBase("evt-delegate-abort-1", "2026-02-24T11:01:00.000Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
},
{
type: "message.delta",
...runtimeBase("evt-delegate-abort-2", "2026-02-24T11:01:00.100Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
delta: "One bug so far.",
},
{
type: "turn.aborted",
...runtimeBase("evt-delegate-abort-3", "2026-02-24T11:01:00.200Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
payload: { reason: "Interrupted" },
},
],
expectedResult: [
'Codex was stopped before finishing the delegated task "Review changes".',
"One bug so far.",
],
}),
);

it.live("delegation reports an aborted turn as stopped when the session was reconnecting", () =>
delegateToCodex({
childEvents: [
{
type: "turn.started",
...runtimeBase("evt-delegate-reconnect-1", "2026-02-24T11:02:00.000Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
},
{
type: "message.delta",
...runtimeBase("evt-delegate-reconnect-2", "2026-02-24T11:02:00.100Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
delta: "One bug so far.",
},
{
// Codex reports a reconnect as "starting" while the turn stays active.
type: "session.state.changed",
...runtimeBase("evt-delegate-reconnect-3", "2026-02-24T11:02:00.150Z"),
threadId: THREAD_ID,
payload: { state: "starting" },
},
{
type: "turn.aborted",
...runtimeBase("evt-delegate-reconnect-4", "2026-02-24T11:02:00.200Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
payload: { reason: "Interrupted" },
},
],
expectedResult: [
'Codex was stopped before finishing the delegated task "Review changes".',
"One bug so far.",
],
}),
);

it.live("delegation reports buffered output when an error precedes the failed completion", () =>
delegateToCodex({
childEvents: [
{
type: "turn.started",
...runtimeBase("evt-delegate-error-1", "2026-02-24T11:03:00.000Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
},
{
type: "message.delta",
...runtimeBase("evt-delegate-error-2", "2026-02-24T11:03:00.100Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
delta: "Found one bug.",
},
{
// Codex queues the usage-limit error ahead of the failed turn/completed.
type: "runtime.error",
...runtimeBase("evt-delegate-error-3", "2026-02-24T11:03:00.150Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
payload: { message: "Usage limit reached.", class: "provider_error" },
},
{
type: "turn.completed",
...runtimeBase("evt-delegate-error-4", "2026-02-24T11:03:00.200Z"),
threadId: THREAD_ID,
turnId: FIXTURE_TURN_ID,
status: "failed",
},
],
expectedResult: [
'Codex could not finish the delegated task "Review changes".',
"Found one bug.",
"Usage limit reached.",
],
}),
);
Loading
Loading