203 lines
6.1 KiB
TypeScript
203 lines
6.1 KiB
TypeScript
import { describe, expect, it } from "vite-plus/test";
|
|
import { ThreadId } from "@t3tools/contracts";
|
|
|
|
import {
|
|
deriveThreadRelationshipGraph,
|
|
immediateThreadRelationships,
|
|
relatedThreadIds,
|
|
resolveMergeBackTargetThreadId,
|
|
walkThreadRelationships,
|
|
} from "./threadRelationships.ts";
|
|
|
|
describe("thread relationships", () => {
|
|
it("keeps missing parents and cycles navigable without recursive traversal", () => {
|
|
const root = ThreadId.make("thread-root");
|
|
const child = ThreadId.make("thread-child");
|
|
const missing = ThreadId.make("thread-missing");
|
|
const graph = deriveThreadRelationshipGraph({
|
|
threads: [
|
|
{
|
|
id: root,
|
|
title: "Root",
|
|
status: "completed",
|
|
forkedFrom: { type: "run", threadId: child, runId: "run-cycle" },
|
|
lineage: { rootThreadId: root, parentThreadId: child, relationshipToParent: "fork" },
|
|
},
|
|
{
|
|
id: child,
|
|
title: "Child",
|
|
status: "completed",
|
|
forkedFrom: { type: "run", threadId: missing, runId: "run-missing" },
|
|
lineage: { rootThreadId: root, parentThreadId: missing, relationshipToParent: "fork" },
|
|
},
|
|
] as never,
|
|
projection: null,
|
|
});
|
|
|
|
expect(graph.nodes.get(missing)?.missing).toBe(true);
|
|
expect(relatedThreadIds(graph, root)).toEqual([child]);
|
|
expect(relatedThreadIds(graph, child)).toEqual([root, missing]);
|
|
expect(
|
|
walkThreadRelationships(graph, root).map(({ threadId, depth }) => [threadId, depth]),
|
|
).toEqual([
|
|
[child, 1],
|
|
[missing, 2],
|
|
]);
|
|
expect(immediateThreadRelationships(graph, root).map(({ threadId }) => threadId)).toEqual([
|
|
child,
|
|
]);
|
|
});
|
|
|
|
it("combines subagent and transfer edges with archived shell state", () => {
|
|
const parent = ThreadId.make("thread-parent");
|
|
const child = ThreadId.make("thread-child");
|
|
const transferTarget = ThreadId.make("thread-transfer");
|
|
const graph = deriveThreadRelationshipGraph({
|
|
threads: [
|
|
{
|
|
id: parent,
|
|
title: "Parent",
|
|
status: "completed",
|
|
archivedAt: null,
|
|
forkedFrom: null,
|
|
lineage: { rootThreadId: parent, parentThreadId: null, relationshipToParent: null },
|
|
},
|
|
{
|
|
id: child,
|
|
title: "Subagent",
|
|
status: "completed",
|
|
archivedAt: "2026-06-24T00:00:00.000Z",
|
|
forkedFrom: null,
|
|
lineage: {
|
|
rootThreadId: parent,
|
|
parentThreadId: parent,
|
|
relationshipToParent: "subagent",
|
|
},
|
|
},
|
|
] as never,
|
|
projection: {
|
|
thread: { id: parent },
|
|
subagents: [{ childThreadId: child, status: "completed" }],
|
|
contextTransfers: [
|
|
{
|
|
sourceThreadId: child,
|
|
targetThreadId: transferTarget,
|
|
status: "completed",
|
|
},
|
|
],
|
|
} as never,
|
|
});
|
|
|
|
expect(graph.nodes.get(child)?.thread?.archivedAt).not.toBeNull();
|
|
expect(graph.nodes.get(transferTarget)?.missing).toBe(true);
|
|
expect(graph.edges).toEqual(
|
|
expect.arrayContaining([
|
|
expect.objectContaining({
|
|
sourceThreadId: parent,
|
|
targetThreadId: child,
|
|
kind: "subagent",
|
|
}),
|
|
expect.objectContaining({
|
|
sourceThreadId: child,
|
|
targetThreadId: transferTarget,
|
|
kind: "transfer",
|
|
}),
|
|
]),
|
|
);
|
|
});
|
|
|
|
it("keeps the live shell when an archived snapshot contains the same thread id", () => {
|
|
const parent = ThreadId.make("thread-parent");
|
|
const staleParent = ThreadId.make("thread-stale-parent");
|
|
const child = ThreadId.make("thread-child");
|
|
const liveChild = {
|
|
id: child,
|
|
title: "Live child",
|
|
status: "running",
|
|
archivedAt: null,
|
|
forkedFrom: { type: "run", threadId: parent, runId: "run-live" },
|
|
lineage: {
|
|
rootThreadId: parent,
|
|
parentThreadId: parent,
|
|
relationshipToParent: "fork",
|
|
},
|
|
} as const;
|
|
const staleArchivedChild = {
|
|
...liveChild,
|
|
title: "Stale archived child",
|
|
status: "completed",
|
|
archivedAt: "2026-06-24T00:00:00.000Z",
|
|
forkedFrom: { type: "run", threadId: staleParent, runId: "run-stale" },
|
|
lineage: {
|
|
rootThreadId: staleParent,
|
|
parentThreadId: staleParent,
|
|
relationshipToParent: "fork",
|
|
},
|
|
} as const;
|
|
|
|
const graph = deriveThreadRelationshipGraph({
|
|
threads: [liveChild, staleArchivedChild] as never,
|
|
projection: null,
|
|
});
|
|
|
|
expect(graph.nodes.get(child)?.thread).toMatchObject({
|
|
title: "Live child",
|
|
status: "running",
|
|
archivedAt: null,
|
|
});
|
|
expect(graph.edges).toEqual([
|
|
expect.objectContaining({
|
|
sourceThreadId: parent,
|
|
targetThreadId: child,
|
|
}),
|
|
]);
|
|
expect(graph.nodes.has(staleParent)).toBe(false);
|
|
});
|
|
|
|
it("resolves merge-back only for forks and prefers the recorded fork source", () => {
|
|
const source = ThreadId.make("thread-source");
|
|
const fallbackParent = ThreadId.make("thread-parent");
|
|
const fork = ThreadId.make("thread-fork");
|
|
|
|
expect(
|
|
resolveMergeBackTargetThreadId({
|
|
thread: {
|
|
id: fork,
|
|
forkedFrom: { type: "run", threadId: source, runId: "run-source" },
|
|
lineage: {
|
|
rootThreadId: source,
|
|
parentThreadId: fallbackParent,
|
|
relationshipToParent: "fork",
|
|
},
|
|
},
|
|
} as never),
|
|
).toBe(source);
|
|
expect(
|
|
resolveMergeBackTargetThreadId({
|
|
thread: {
|
|
id: fork,
|
|
forkedFrom: null,
|
|
lineage: {
|
|
rootThreadId: source,
|
|
parentThreadId: fallbackParent,
|
|
relationshipToParent: "fork",
|
|
},
|
|
},
|
|
} as never),
|
|
).toBe(fallbackParent);
|
|
expect(
|
|
resolveMergeBackTargetThreadId({
|
|
thread: {
|
|
id: fork,
|
|
forkedFrom: null,
|
|
lineage: {
|
|
rootThreadId: source,
|
|
parentThreadId: fallbackParent,
|
|
relationshipToParent: "subagent",
|
|
},
|
|
},
|
|
} as never),
|
|
).toBeNull();
|
|
});
|
|
});
|