Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

threadActivityInspector.test.ts

278 lines8.4 KB

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 };
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];
60​
61describe("buildThreadActivityInspector", () => {
62 it("presents command lifecycle and exit state without cached raw output", () => {
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: "Exit", value: "Process exited with code 0" }),
109 expect.objectContaining({ label: "Attempt history" }),
110 ]),
111 );
112 expect(model.blocks.some((block) => block.label === "Output")).toBe(false);
113 expect(model.structuredDetails).not.toContain("all checks passed");
114 });
115​
116 it("exposes file and web result provenance plus dynamic structured data", () => {
117 const fileSearch: OrchestrationV2TurnItem = {
118 ...itemBase("file-search"),
119 type: "file_search",
120 pattern: "resolveSession",
121 results: [{ fileName: "src/session.ts", line: 42, preview: "function resolveSession()" }],
122 };
123 const fileModel = buildThreadActivityInspector(
124 activityFor(fileSearch),
125 EMPTY_V2_ITEM_SUPPORT,
126 sourceThreadId,
127 );
128 expect(fileModel.fileLinks).toEqual([
129 {
130 label: "src/session.ts — function resolveSession()",
131 path: "src/session.ts",
132 line: 42,
133 },
134 ]);
135​
136 const webSearch: OrchestrationV2TurnItem = {
137 ...itemBase("web-search"),
138 type: "web_search",
139 patterns: ["Effect Schema docs"],
140 results: [
141 { title: "Schema", url: "https://effect.website/docs/schema", snippet: "Typed schemas" },
142 ],
143 };
144 const webModel = buildThreadActivityInspector(
145 activityFor(webSearch),
146 EMPTY_V2_ITEM_SUPPORT,
147 sourceThreadId,
148 );
149 expect(webModel.webLinks).toEqual([
150 { label: "Schema", url: "https://effect.website/docs/schema" },
151 ]);
152 expect(webModel.blocks).toEqual(
153 expect.arrayContaining([
154 expect.objectContaining({ label: "Schema", value: "Typed schemas" }),
155 ]),
156 );
157​
158 const dynamicTool: OrchestrationV2TurnItem = {
159 ...itemBase("dynamic"),
160 type: "dynamic_tool",
161 toolName: "custom",
162 input: { nested: { value: 1 } },
163 output: { ok: true },
164 };
165 const dynamicModel = buildThreadActivityInspector(
166 activityFor(dynamicTool),
167 EMPTY_V2_ITEM_SUPPORT,
168 sourceThreadId,
169 );
170 expect(dynamicModel.blocks).toEqual(
171 expect.arrayContaining([
172 expect.objectContaining({ label: "Input", value: expect.stringContaining('"nested"') }),
173 ]),
174 );
175 expect(dynamicModel.structuredDetails).toContain('"type": "dynamic_tool"');
176 expect(dynamicModel.blocks.some((block) => block.label === "Output")).toBe(false);
177 expect(dynamicModel.structuredDetails).not.toContain('"output"');
178 });
179​
180 it("keeps file links and change counts without cached patch bodies", () => {
181 const item: OrchestrationV2TurnItem = {
182 ...itemBase("file"),
183 type: "file_change",
184 fileName: "src/example.ts",
185 additions: 2,
186 deletions: 1,
187 diffStr: "RAW_PATCH",
188 oldStr: "RAW_BEFORE",
189 newStr: "RAW_AFTER",
190 };
191 const model = buildThreadActivityInspector(activityFor(item), EMPTY_V2_ITEM_SUPPORT, threadId);
192 expect(model.fileLinks).toEqual([{ label: `modify ${item.fileName}`, path: item.fileName }]);
193 expect(model.fields).toContainEqual({ label: "Changes", value: "+2 −1" });
194 expect(model.blocks).toEqual([]);
195 expect(model.structuredDetails).not.toContain("RAW_");
196 expect(model.structuredDetails).toContain(item.fileName);
197 });
198​
199 it("enables rollback only for the matching ready checkpoint", () => {
200 const checkpointId = CheckpointId.make("checkpoint-1");
201 const scopeId = CheckpointScopeId.make("scope-1");
202 const item: OrchestrationV2TurnItem = {
203 ...itemBase("checkpoint"),
204 type: "checkpoint",
205 checkpointId,
206 scopeId,
207 files: [{ path: "src/main.ts", kind: "modified", additions: 2, deletions: 1 }],
208 };
209 const checkpoint = { id: checkpointId, scopeId, status: "ready" } as never;
210 const model = buildThreadActivityInspector(
211 activityFor(item),
212 {
213 ...EMPTY_V2_ITEM_SUPPORT,
214 item,
215 checkpoint,
216 },
217 sourceThreadId,
218 );
219​
220 expect(model.canRollback).toBe(true);
221 expect(model.rollbackTarget).toEqual({
222 threadId: sourceThreadId,
223 checkpointId,
224 scopeId,
225 });
226​
227 const inheritedModel = buildThreadActivityInspector(
228 activityFor(item),
229 {
230 ...EMPTY_V2_ITEM_SUPPORT,
231 item,
232 checkpoint,
233 },
234 ThreadId.make("child-thread"),
235 );
236 expect(inheritedModel.canRollback).toBe(false);
237 expect(inheritedModel.rollbackTarget).toBeNull();
238 });
239​
240 it("prefers live subagent progress from supporting state", () => {
241 const item: OrchestrationV2TurnItem = {
242 ...itemBase("subagent"),
243 type: "subagent",
244 subagentId: nodeId,
245 origin: "provider_native",
246 driver: ProviderDriverKind.make("claude"),
247 providerInstanceId: ProviderInstanceId.make("claude"),
248 childThreadId: null,
249 prompt: "Audit the reducer",
250 progress: "Starting",
251 result: null,
252 };
253 const model = buildThreadActivityInspector(
254 activityFor(item),
255 {
256 ...EMPTY_V2_ITEM_SUPPORT,
257 item,
258 subagent: {
259 origin: "provider_native",
260 status: "running",
261 progress: "Reading projection tests",
262 result: null,
263 } as never,
264 },
265 sourceThreadId,
266 );
267​
268 expect(model.fields).toContainEqual({
269 label: "Delegated task",
270 value: "provider native · running",
271 });
272 expect(model.blocks).toContainEqual({
273 label: "Progress",
274 value: "Reading projection tests",
275 monospaced: false,
276 });
277 });
278});