Files
t3code/apps/server/src/orchestration-v2/RunExecutionService.test.ts
T
Julius Marminge 6b64376033 Share Codex sessions across orchestration threads
- Track explicit thread-to-session bindings and detach without stopping siblings
- Apply runtime settings per Codex thread and filter multiplexed events by turn
- Add migration, projection, lifecycle, and concurrency coverage
2026-08-03 11:45:02 -04:00

138 lines
4.6 KiB
TypeScript

import { assert, it } from "@effect/vitest";
import {
MessageId,
NodeId,
ProviderDriverKind,
ProviderThreadId,
ProviderTurnId,
RunAttemptId,
RunId,
ThreadId,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import type { ProviderAdapterV2Event } from "./ProviderAdapter.ts";
import {
makeProviderEventRoutingState,
type ProviderEventRouteIdentity,
routeProviderEvent,
} from "./RunExecutionService.ts";
const driver = ProviderDriverKind.make("codex");
it.effect("routes shared-runtime events only to their owning root run", () =>
Effect.gen(function* () {
const now = yield* DateTime.now;
const first: ProviderEventRouteIdentity = {
threadId: ThreadId.make("thread:shared-runtime:first"),
runId: RunId.make("run:shared-runtime:first"),
attemptId: RunAttemptId.make("attempt:shared-runtime:first"),
providerThreadId: ProviderThreadId.make("provider-thread:shared-runtime:first"),
};
const second: ProviderEventRouteIdentity = {
threadId: ThreadId.make("thread:shared-runtime:second"),
runId: RunId.make("run:shared-runtime:second"),
attemptId: RunAttemptId.make("attempt:shared-runtime:second"),
providerThreadId: ProviderThreadId.make("provider-thread:shared-runtime:second"),
};
const firstTurnId = ProviderTurnId.make("provider-turn:shared-runtime:first");
const turnEvent: ProviderAdapterV2Event = {
type: "provider_turn.updated",
driver,
threadId: first.threadId,
providerTurn: {
id: firstTurnId,
providerThreadId: first.providerThreadId,
nodeId: NodeId.make("node:shared-runtime:first"),
runAttemptId: first.attemptId,
nativeTurnRef: null,
ordinal: 1,
status: "running",
startedAt: now,
completedAt: null,
},
};
const messageEvent: ProviderAdapterV2Event = {
type: "message.updated",
driver,
message: {
createdBy: "agent",
creationSource: "provider",
id: MessageId.make("message:shared-runtime:first"),
threadId: first.threadId,
runId: first.runId,
nodeId: NodeId.make("node:shared-runtime:first"),
role: "assistant",
text: "first only",
attachments: [],
streaming: false,
createdAt: now,
updatedAt: now,
},
};
const terminalEvent: ProviderAdapterV2Event = {
type: "turn.terminal",
driver,
providerTurnId: firstTurnId,
status: "completed",
};
const firstInitial = makeProviderEventRoutingState({
identity: first,
providerTurnId: null,
});
const secondInitial = makeProviderEventRoutingState({
identity: second,
providerTurnId: null,
});
const [firstTurnAccepted, firstAfterTurn] = routeProviderEvent(turnEvent, first, firstInitial);
const [secondTurnAccepted, secondAfterTurn] = routeProviderEvent(
turnEvent,
second,
secondInitial,
);
assert.isTrue(firstTurnAccepted);
assert.isFalse(secondTurnAccepted);
assert.isTrue(routeProviderEvent(messageEvent, first, firstAfterTurn)[0]);
assert.isFalse(routeProviderEvent(messageEvent, second, secondAfterTurn)[0]);
assert.isTrue(routeProviderEvent(terminalEvent, first, firstAfterTurn)[0]);
assert.isFalse(routeProviderEvent(terminalEvent, second, secondAfterTurn)[0]);
}),
);
it("does not route a superseded attempt through a reused provider thread", () => {
const threadId = ThreadId.make("thread:shared-runtime:restart");
const providerThreadId = ProviderThreadId.make("provider-thread:shared-runtime:restart");
const oldAttempt: ProviderEventRouteIdentity = {
threadId,
runId: RunId.make("run:shared-runtime:restart"),
attemptId: RunAttemptId.make("attempt:shared-runtime:restart:old"),
providerThreadId,
};
const newAttempt: ProviderEventRouteIdentity = {
...oldAttempt,
attemptId: RunAttemptId.make("attempt:shared-runtime:restart:new"),
};
const oldTurnEvent: ProviderAdapterV2Event = {
type: "provider_turn.updated",
driver,
threadId,
providerTurn: {
id: ProviderTurnId.make("provider-turn:shared-runtime:restart:old"),
providerThreadId,
nodeId: NodeId.make("node:shared-runtime:restart:old"),
runAttemptId: oldAttempt.attemptId,
nativeTurnRef: null,
ordinal: 1,
status: "interrupted",
startedAt: null,
completedAt: null,
},
};
const newState = makeProviderEventRoutingState({ identity: newAttempt, providerTurnId: null });
assert.isFalse(routeProviderEvent(oldTurnEvent, newAttempt, newState)[0]);
});