Files
t3code/apps/server/src/orchestration-v2/ThreadForkService.test.ts
T
+36 de34391427 feat(orchestrator): introduce new orchestrator (#2829)
Co-authored-by: maria-rcks <[email protected]>
Co-authored-by: Bilal Bakr <[email protected]>
Co-authored-by: shivam <[email protected]>
Co-authored-by: Vitalii Yehorov <[email protected]>
Co-authored-by: Jake Leventhal <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Alex Southwell <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Nicholas Wasmiller <[email protected]>
Co-authored-by: PB <[email protected]>
Co-authored-by: Exotic <[email protected]>
Co-authored-by: Yash Singh <[email protected]>
Co-authored-by: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Theo Browne <[email protected]>
Co-authored-by: Gabriel De Andrade <[email protected]>
Co-authored-by: Dara Adedeji <[email protected]>
Co-authored-by: scratchyone <[email protected]>
Co-authored-by: Dominic Roy <[email protected]>
Co-authored-by: chukfinley <[email protected]>
Co-authored-by: Primož Ajdišek <[email protected]>
Co-authored-by: benthecarman <[email protected]>
Co-authored-by: NaveDanan <[email protected]>
Co-authored-by: aaditagrawal <[email protected]>
Co-authored-by: Aditya Garud <[email protected]>
Co-authored-by: Nick Anisimov <[email protected]>
Co-authored-by: MacKinley Smith <[email protected]>
Co-authored-by: Yordis Prieto <[email protected]>
Co-authored-by: t3-code[bot] <269035359+t3-code[bot]@users.noreply.github.com>
Co-authored-by: AKolenda <[email protected]>
Co-authored-by: Guillermo Casanova <[email protected]>
Co-authored-by: Otavio Salvador <[email protected]>
Co-authored-by: Shirish Pothi <[email protected]>
Co-authored-by: Ishaan Kothari <[email protected]>
Co-authored-by: Bob Fowler <[email protected]>
Co-authored-by: Anton Bezdenezhnykh <[email protected]>
Co-authored-by: ValeraZSD <[email protected]>
Co-authored-by: Ephraim <[email protected]>
Co-authored-by: Ryan Ilano <[email protected]>
Co-authored-by: Alex <[email protected]>
Co-authored-by: maco <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Tristan Knight <[email protected]>
Co-authored-by: PR Batch Tester <[email protected]>
Co-authored-by: oliver <[email protected]>
Co-authored-by: kamkm <[email protected]>
Signed-off-by: Yordis Prieto <[email protected]>
2026-10-02 12:22:22 -07:00

194 lines
6.6 KiB
TypeScript

import { assert, it } from "@effect/vitest";
import {
ContextTransferId,
MessageId,
type ModelSelection,
type OrchestrationV2AppThread,
type OrchestrationV2Run,
type OrchestrationV2ThreadProjection,
ProjectId,
ProviderInstanceId,
ProviderThreadId,
RunId,
ThreadId,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import * as Effect from "effect/Effect";
import * as ThreadForkService from "./ThreadForkService.ts";
const sourceThreadId = ThreadId.make("thread:fork-snoozed-source");
const targetThreadId = ThreadId.make("thread:fork-awake-target");
const sourceRunId = RunId.make("run:fork-snoozed-source");
const providerInstanceId = ProviderInstanceId.make("codex");
const modelSelection = {
instanceId: providerInstanceId,
model: "gpt-5.4",
} satisfies ModelSelection;
const sourceCreatedAt = DateTime.makeUnsafe("2026-07-24T09:00:00.000Z");
const snoozedAt = DateTime.makeUnsafe("2026-07-24T09:05:00.000Z");
const snoozedUntil = DateTime.makeUnsafe("2026-07-25T09:00:00.000Z");
const forkCreatedAt = DateTime.makeUnsafe("2026-07-24T09:10:00.000Z");
function makeSourceThread(): OrchestrationV2AppThread {
return {
createdBy: "user",
creationSource: "web",
id: sourceThreadId,
projectId: ProjectId.make("project:fork-snooze"),
title: "Snoozed source",
providerInstanceId,
modelSelection,
runtimeMode: "full-access",
interactionMode: "plan",
branch: "feature/source",
worktreePath: "/tmp/source-worktree",
activeProviderThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
lineage: {
parentThreadId: null,
relationshipToParent: null,
rootThreadId: sourceThreadId,
},
forkedFrom: null,
createdAt: sourceCreatedAt,
updatedAt: snoozedAt,
archivedAt: null,
settledOverride: null,
settledAt: null,
lastVisitedAt: null,
snoozedUntil,
snoozedAt,
deletedAt: null,
};
}
function makeSourceRun(status: OrchestrationV2Run["status"]): OrchestrationV2Run {
return {
id: sourceRunId,
threadId: sourceThreadId,
ordinal: 1,
providerInstanceId,
modelSelection,
providerThreadId: ProviderThreadId.make("provider-thread:fork-snoozed-source"),
userMessageId: MessageId.make("message:fork-snoozed-source"),
rootNodeId: null,
activeAttemptId: null,
status,
queuePosition: null,
requestedAt: sourceCreatedAt,
startedAt: sourceCreatedAt,
completedAt: snoozedAt,
checkpointId: null,
contextHandoffId: null,
};
}
function makeSourceProjection(sourceRun: OrchestrationV2Run): OrchestrationV2ThreadProjection {
return {
thread: makeSourceThread(),
runs: [sourceRun],
attempts: [],
nodes: [],
subagents: [],
providerSessions: [],
providerThreads: [],
providerTurns: [],
runtimeRequests: [],
messages: [],
plans: [],
turnItems: [],
checkpointScopes: [],
checkpoints: [],
contextHandoffs: [],
contextTransfers: [],
visibleTurnItems: [],
updatedAt: snoozedAt,
};
}
const planFork = (sourceRun: OrchestrationV2Run) =>
Effect.gen(function* () {
const service = yield* ThreadForkService.ThreadForkServiceV2;
return yield* service.plan({
sourceProjection: makeSourceProjection(sourceRun),
sourceRun,
sourceProviderThread: undefined,
canonicalSourcePoint: {
threadId: sourceThreadId,
runId: sourceRunId,
},
transferId: ContextTransferId.make("context-transfer:fork-snoozed-source"),
targetThreadId,
title: "Awake fork",
createdBy: "user",
creationSource: "mobile",
createdAt: forkCreatedAt,
});
}).pipe(Effect.provide(ThreadForkService.layer));
it("treats usage-limited and other provider-finished runs as forkable", () => {
assert.isTrue(ThreadForkService.isForkableSourceRunStatus("completed"));
assert.isTrue(ThreadForkService.isForkableSourceRunStatus("waiting"));
assert.isTrue(ThreadForkService.isForkableSourceRunStatus("failed"));
assert.isTrue(ThreadForkService.isForkableSourceRunStatus("interrupted"));
assert.isTrue(ThreadForkService.isForkableSourceRunStatus("cancelled"));
assert.isFalse(ThreadForkService.isForkableSourceRunStatus("running"));
assert.isFalse(ThreadForkService.isForkableSourceRunStatus("starting"));
assert.isFalse(ThreadForkService.isForkableSourceRunStatus("queued"));
assert.isFalse(ThreadForkService.isForkableSourceRunStatus("preparing"));
assert.isFalse(ThreadForkService.isForkableSourceRunStatus("rolled_back"));
});
it.effect("keeps a fork awake when its source thread is snoozed", () =>
Effect.gen(function* () {
const sourceThread = makeSourceThread();
const sourceRun = makeSourceRun("completed");
const result = yield* planFork(sourceRun);
assert.isNull(result.targetThread.snoozedUntil);
assert.isNull(result.targetThread.snoozedAt);
assert.equal(result.targetThread.projectId, sourceThread.projectId);
assert.equal(result.targetThread.providerInstanceId, sourceThread.providerInstanceId);
assert.deepEqual(result.targetThread.modelSelection, sourceThread.modelSelection);
assert.equal(result.targetThread.runtimeMode, sourceThread.runtimeMode);
assert.equal(result.targetThread.interactionMode, sourceThread.interactionMode);
assert.equal(result.targetThread.branch, sourceThread.branch);
assert.equal(result.targetThread.worktreePath, sourceThread.worktreePath);
assert.isNull(result.targetThread.activeProviderThreadId);
assert.deepEqual(result.targetThread.lineage, {
parentThreadId: sourceThreadId,
relationshipToParent: "fork",
rootThreadId: sourceThreadId,
});
assert.deepEqual(result.targetThread.forkedFrom, {
type: "run",
threadId: sourceThreadId,
runId: sourceRunId,
});
}),
);
it.effect("forks from a usage-limited failed run", () =>
Effect.gen(function* () {
const result = yield* planFork(makeSourceRun("failed"));
assert.deepEqual(result.targetThread.forkedFrom, {
type: "run",
threadId: sourceThreadId,
runId: sourceRunId,
});
}),
);
it.effect("rejects in-progress and rolled-back fork sources", () =>
Effect.gen(function* () {
for (const status of ["running", "rolled_back"] as const) {
const sourceRun = makeSourceRun(status);
const error = yield* planFork(sourceRun).pipe(Effect.flip);
assert.equal(error._tag, "ThreadForkPlanError");
assert.equal(error.sourceThreadId, sourceThreadId);
assert.equal(error.targetThreadId, targetThreadId);
assert.equal(error.cause, ThreadForkService.forkableSourceRunStatusError(sourceRun));
}
}),
);