Files
t3code/packages/client-runtime/src/state/threadSubagents.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

152 lines
4.7 KiB
TypeScript

import { describe, expect, it } from "vite-plus/test";
import {
NodeId,
ProviderDriverKind,
ProviderInstanceId,
RunId,
ThreadId,
type OrchestrationV2Subagent,
} from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { deriveThreadTurnSubagents, resolveSubagentPillSegment } from "./threadSubagents.ts";
const at = (iso: string) => DateTime.makeUnsafe(iso);
function subagent(
overrides: Omit<Partial<OrchestrationV2Subagent>, "id"> & { readonly id: string },
): OrchestrationV2Subagent {
const { id, ...rest } = overrides;
return {
id: NodeId.make(id),
threadId: ThreadId.make("thread-1"),
runId: RunId.make("run-1"),
parentNodeId: NodeId.make("node-1"),
origin: "app_owned",
createdBy: "agent",
driver: ProviderDriverKind.make("codex"),
providerInstanceId: ProviderInstanceId.make("codex"),
providerThreadId: null,
childThreadId: ThreadId.make("thread-child"),
nativeTaskRef: null,
prompt: "Do the thing",
title: "Worker",
model: "gpt-5.4",
status: "running",
result: null,
startedAt: at("2026-06-20T00:00:00.000Z"),
completedAt: null,
updatedAt: at("2026-06-20T00:00:01.000Z"),
...rest,
};
}
const run = (id: string, status: "running" | "completed") =>
({ id: RunId.make(id), status }) as never;
const activeRun = run("run-1", "running");
const finishedRun = run("run-1", "completed");
describe("deriveThreadTurnSubagents", () => {
it("returns null when the thread has never spawned an agent", () => {
expect(deriveThreadTurnSubagents({ runs: [activeRun], subagents: [] })).toBeNull();
});
it("scopes the roster to the active run and orders it by start time", () => {
const turn = deriveThreadTurnSubagents({
runs: [run("run-0", "completed"), activeRun],
subagents: [
subagent({ id: "b", startedAt: at("2026-06-20T00:00:05.000Z") }),
subagent({ id: "old", runId: RunId.make("run-0") }),
subagent({ id: "a", startedAt: at("2026-06-20T00:00:02.000Z") }),
],
});
expect(turn?.runId).toBe("run-1");
expect(turn?.subagents.map((entry) => entry.id)).toEqual(["a", "b"]);
expect(turn?.turnActive).toBe(true);
});
it("falls back to the most recently updated agent's run once the turn settles", () => {
const turn = deriveThreadTurnSubagents({
runs: [finishedRun, run("run-2", "completed")],
subagents: [
subagent({ id: "old", status: "completed", updatedAt: at("2026-06-20T00:00:01.000Z") }),
subagent({
id: "recent",
runId: RunId.make("run-2"),
status: "completed",
updatedAt: at("2026-06-20T00:00:09.000Z"),
}),
],
});
expect(turn?.runId).toBe("run-2");
expect(turn?.turnActive).toBe(false);
expect(turn?.subagents.map((entry) => entry.id)).toEqual(["recent"]);
});
it("counts idle as neither working nor done", () => {
const turn = deriveThreadTurnSubagents({
runs: [activeRun],
subagents: [
subagent({ id: "a", status: "running" }),
subagent({ id: "b", status: "idle" }),
subagent({ id: "c", status: "failed" }),
],
});
expect(turn?.liveCount).toBe(1);
expect(turn?.settledCount).toBe(1);
expect(turn?.subagents).toHaveLength(3);
});
});
describe("resolveSubagentPillSegment", () => {
it("shows how many of the turn's agents are still working", () => {
const turn = deriveThreadTurnSubagents({
runs: [activeRun],
subagents: [
subagent({ id: "a", status: "running" }),
subagent({ id: "b", status: "waiting" }),
subagent({ id: "c", status: "completed" }),
],
});
expect(resolveSubagentPillSegment(turn)).toEqual({
label: "2/3",
accessibilityLabel: "2 of 3 agents working",
});
});
it("reports the finished roster while the turn is still running", () => {
const turn = deriveThreadTurnSubagents({
runs: [activeRun],
subagents: [subagent({ id: "a", status: "completed" })],
});
expect(resolveSubagentPillSegment(turn)).toEqual({
label: "1 done",
accessibilityLabel: "1 agent done",
});
});
it("hides itself once the turn is over and nothing is working", () => {
const turn = deriveThreadTurnSubagents({
runs: [finishedRun],
subagents: [subagent({ id: "a", status: "completed" })],
});
expect(resolveSubagentPillSegment(turn)).toBeNull();
expect(resolveSubagentPillSegment(null)).toBeNull();
});
it("stays visible after the turn ends while an agent is still working", () => {
const turn = deriveThreadTurnSubagents({
runs: [finishedRun],
subagents: [subagent({ id: "a", status: "running" })],
});
expect(resolveSubagentPillSegment(turn)?.label).toBe("1/1");
});
});