Open History to see the last commit
1import { EMPTY_V2_ITEM_SUPPORT } from "@t3tools/client-runtime/state/item-support";
2import {
3 CheckpointId,
4 CheckpointScopeId,
5 NodeId,
6 ProviderDriverKind,
7 ProviderInstanceId,
8 RunId,
9 ThreadId,
10 TurnItemId,
11 type OrchestrationV2ProjectedTurnItem,
12 type OrchestrationV2TurnItem,
13} from "@t3tools/contracts";
14import * as DateTime from "effect/DateTime";
15import { describe, expect, it } from "vite-plus/test";
16
17import { buildThreadFeed, type ThreadFeedActivity } from "./threadActivity";
18import { buildThreadActivityInspector } from "./threadActivityInspector";
19
20const threadId = ThreadId.make("thread-1");
21const sourceThreadId = ThreadId.make("thread-source");
22const runId = RunId.make("run-1");
23const nodeId = NodeId.make("node-1");
24const startedAt = DateTime.makeUnsafe("2026-06-20T00:00:00.000Z");
25const completedAt = DateTime.makeUnsafe("2026-06-20T00:00:02.000Z");
26
27function itemBase(id: string) {
28 return {
29 id: TurnItemId.make(id),
30 threadId,
31 runId,
32 nodeId,
33 providerThreadId: null,
34 providerTurnId: null,
35 nativeItemRef: null,
36 parentItemId: null,
37 ordinal: 0,
38 status: "completed" as const,
39 title: null,
40 startedAt,
41 completedAt,
42 updatedAt: completedAt,
43 };
44}
45
46function activityFor(item: OrchestrationV2TurnItem): ThreadFeedActivity {
47 const row: OrchestrationV2ProjectedTurnItem = {
48 position: 0,
49 visibility: "inherited",
50 sourceThreadId,
51 sourceItemId: item.id,
52 item,
53 };
54 const group = buildThreadFeed([row])[0];
55 if (group?.type !== "activity-group" || !group.activities[0]) {
56 throw new Error("Expected an activity group");
57 }
58 return group.activities[0];
59}
60
61describe("buildThreadActivityInspector", () => {
62 it("presents command lifecycle, execution support, output, and exit state", () => {
63 const item: OrchestrationV2TurnItem = {
64 ...itemBase("command"),
65 type: "command_execution",
66 input: "vp check",
67 output: "all checks passed",
68 exitCode: 0,
69 };
70 const support = {
71 ...EMPTY_V2_ITEM_SUPPORT,
72 item,
73 run: { status: "completed" } as never,
74 attempts: [
75 {
76 id: "attempt-1",
77 attemptOrdinal: 1,
78 status: "superseded",
79 reason: "initial",
80 },
81 {
82 id: "attempt-2",
83 attemptOrdinal: 2,
84 status: "completed",
85 reason: "steering_restart",
86 },
87 ] as never,
88 node: { kind: "tool_call", status: "completed" } as never,
89 providerSession: {
90 status: "ready",
91 model: "gpt-5.4",
92 cwd: "/workspace/project",
93 } as never,
94 };
95
96 const model = buildThreadActivityInspector(activityFor(item), support, sourceThreadId);
97 expect(model.fields).toEqual(
98 expect.arrayContaining([
99 { label: "Duration", value: "2.0s" },
100 { label: "Run", value: "completed" },
101 { label: "Working directory", value: "/workspace/project" },
102 { label: "Visibility", value: "inherited" },
103 ]),
104 );
105 expect(model.blocks).toEqual(
106 expect.arrayContaining([
107 expect.objectContaining({ label: "Command", value: "vp check" }),
108 expect.objectContaining({ label: "Output", value: "all checks passed" }),
109 expect.objectContaining({ label: "Exit", value: "Process exited with code 0" }),
110 expect.objectContaining({ label: "Attempt history" }),
111 ]),
112 );
113 });
114
115 it("exposes file and web result provenance plus dynamic structured data", () => {
116 const fileSearch: OrchestrationV2TurnItem = {
117 ...itemBase("file-search"),
118 type: "file_search",
119 pattern: "resolveSession",
120 results: [{ fileName: "src/session.ts", line: 42, preview: "function resolveSession()" }],
121 };
122 const fileModel = buildThreadActivityInspector(
123 activityFor(fileSearch),
124 EMPTY_V2_ITEM_SUPPORT,
125 sourceThreadId,
126 );
127 expect(fileModel.fileLinks).toEqual([
128 {
129 label: "src/session.ts — function resolveSession()",
130 path: "src/session.ts",
131 line: 42,
132 },
133 ]);
134
135 const webSearch: OrchestrationV2TurnItem = {
136 ...itemBase("web-search"),
137 type: "web_search",
138 patterns: ["Effect Schema docs"],
139 results: [
140 { title: "Schema", url: "https://effect.website/docs/schema", snippet: "Typed schemas" },
141 ],
142 };
143 const webModel = buildThreadActivityInspector(
144 activityFor(webSearch),
145 EMPTY_V2_ITEM_SUPPORT,
146 sourceThreadId,
147 );
148 expect(webModel.webLinks).toEqual([
149 { label: "Schema", url: "https://effect.website/docs/schema" },
150 ]);
151 expect(webModel.blocks).toEqual(
152 expect.arrayContaining([
153 expect.objectContaining({ label: "Schema", value: "Typed schemas" }),
154 ]),
155 );
156
157 const dynamicTool: OrchestrationV2TurnItem = {
158 ...itemBase("dynamic"),
159 type: "dynamic_tool",
160 toolName: "custom",
161 input: { nested: { value: 1 } },
162 output: { ok: true },
163 };
164 const dynamicModel = buildThreadActivityInspector(
165 activityFor(dynamicTool),
166 EMPTY_V2_ITEM_SUPPORT,
167 sourceThreadId,
168 );
169 expect(dynamicModel.blocks).toEqual(
170 expect.arrayContaining([
171 expect.objectContaining({ label: "Input", value: expect.stringContaining('"nested"') }),
172 expect.objectContaining({ label: "Output", value: expect.stringContaining('"ok"') }),
173 ]),
174 );
175 expect(dynamicModel.structuredDetails).toContain('"type": "dynamic_tool"');
176 });
177
178 it("enables rollback only for the matching ready checkpoint", () => {
179 const checkpointId = CheckpointId.make("checkpoint-1");
180 const scopeId = CheckpointScopeId.make("scope-1");
181 const item: OrchestrationV2TurnItem = {
182 ...itemBase("checkpoint"),
183 type: "checkpoint",
184 checkpointId,
185 scopeId,
186 files: [{ path: "src/main.ts", kind: "modified", additions: 2, deletions: 1 }],
187 };
188 const checkpoint = { id: checkpointId, scopeId, status: "ready" } as never;
189 const model = buildThreadActivityInspector(
190 activityFor(item),
191 {
192 ...EMPTY_V2_ITEM_SUPPORT,
193 item,
194 checkpoint,
195 },
196 sourceThreadId,
197 );
198
199 expect(model.canRollback).toBe(true);
200 expect(model.rollbackTarget).toEqual({
201 threadId: sourceThreadId,
202 checkpointId,
203 scopeId,
204 });
205
206 const inheritedModel = buildThreadActivityInspector(
207 activityFor(item),
208 {
209 ...EMPTY_V2_ITEM_SUPPORT,
210 item,
211 checkpoint,
212 },
213 ThreadId.make("child-thread"),
214 );
215 expect(inheritedModel.canRollback).toBe(false);
216 expect(inheritedModel.rollbackTarget).toBeNull();
217 });
218
219 it("prefers live subagent progress from supporting state", () => {
220 const item: OrchestrationV2TurnItem = {
221 ...itemBase("subagent"),
222 type: "subagent",
223 subagentId: nodeId,
224 origin: "provider_native",
225 driver: ProviderDriverKind.make("claude"),
226 providerInstanceId: ProviderInstanceId.make("claude"),
227 childThreadId: null,
228 prompt: "Audit the reducer",
229 progress: "Starting",
230 result: null,
231 };
232 const model = buildThreadActivityInspector(
233 activityFor(item),
234 {
235 ...EMPTY_V2_ITEM_SUPPORT,
236 item,
237 subagent: {
238 origin: "provider_native",
239 status: "running",
240 progress: "Reading projection tests",
241 result: null,
242 } as never,
243 },
244 sourceThreadId,
245 );
246
247 expect(model.fields).toContainEqual({
248 label: "Delegated task",
249 value: "provider native · running",
250 });
251 expect(model.blocks).toContainEqual({
252 label: "Progress",
253 value: "Reading projection tests",
254 monospaced: false,
255 });
256 });
257});