Open History to see the last commit
1import {
2 EventId,
3 ProjectId,
4 ProviderInstanceId,
5 ThreadId,
6 TurnId,
7 type OrchestrationEvent,
8 type OrchestrationThread,
9 type OrchestrationThreadActivity,
10} from "@t3tools/contracts";
11import { describe, expect, it } from "vite-plus/test";
12
13import { buildThreadFeed, type ThreadFeedActivity } from "../../mobile/src/lib/threadActivity.ts";
14import { deriveWorkLogEntries } from "../../web/src/session-logic.ts";
15import {
16 projectActivityEvent,
17 projectActivityPayload,
18 projectThreadDetailSnapshot,
19} from "../src/orchestration/ActivityPayloadProjection.ts";
20
21function makeActivity(
22 id: string,
23 itemType: string,
24 data: Record<string, unknown>,
25): OrchestrationThreadActivity {
26 return {
27 id: EventId.make(id),
28 tone: "tool",
29 kind: "tool.completed",
30 summary: `Completed ${itemType}`,
31 payload: {
32 itemType,
33 title: itemType,
34 detail: `${itemType} detail`,
35 status: "completed",
36 requestKind: "command",
37 data,
38 },
39 turnId: TurnId.make(`turn-${id}`),
40 createdAt: "2026-07-27T00:00:00.000Z",
41 };
42}
43
44function makeThread(activities: ReadonlyArray<OrchestrationThreadActivity>): OrchestrationThread {
45 return {
46 id: ThreadId.make("thread-projection"),
47 projectId: ProjectId.make("project-projection"),
48 title: "Activity projection",
49 modelSelection: {
50 instanceId: ProviderInstanceId.make("codex"),
51 model: "gpt-5.4",
52 },
53 runtimeMode: "full-access",
54 interactionMode: "default",
55 branch: null,
56 worktreePath: null,
57 latestTurn: null,
58 createdAt: "2026-07-27T00:00:00.000Z",
59 updatedAt: "2026-07-27T00:00:00.000Z",
60 archivedAt: null,
61 settledOverride: null,
62 settledAt: null,
63 deletedAt: null,
64 messages: [],
65 proposedPlans: [],
66 activities,
67 checkpoints: [],
68 session: null,
69 };
70}
71
72const fixtures = [
73 makeActivity("command", "command_execution", {
74 item: {
75 command: ["bash", "-lc", "pnpm test"],
76 input: { command: "fallback input", ignored: "input bulk" },
77 result: { command: "fallback result", aggregatedOutput: "x".repeat(10_000) },
78 commandActions: [{ type: "unknown", output: "y".repeat(5_000) }],
79 },
80 command: "fallback data",
81 kind: "execute",
82 toolCallId: "tool-command",
83 rawOutput: {
84 content: "\n```\nfirst useful line\nsecond line",
85 stdout: "unused stdout",
86 ignored: "raw bulk",
87 },
88 ignored: "top-level bulk",
89 }),
90 makeActivity("file-change", "file_change", {
91 item: {
92 changes: [
93 { oldPath: "src/old.ts", newPath: "src/new.ts", patch: "large patch".repeat(1_000) },
94 { filePath: "src/second.ts" },
95 ],
96 },
97 ignored: "top-level bulk",
98 }),
99 makeActivity("dynamic", "dynamic_tool_call", {
100 toolCallId: "tool-dynamic",
101 rawOutput: {
102 stdout: "dynamic summary\nlong output".repeat(1_000),
103 },
104 ignored: "top-level bulk",
105 }),
106 makeActivity("collab", "collab_agent_tool_call", {
107 kind: "delegate",
108 rawOutput: {
109 content: "``` \n```",
110 stdout: "must not be used when content is present",
111 },
112 ignored: "top-level bulk",
113 }),
114 makeActivity("mcp", "mcp_tool_call", {
115 item: {
116 server: "repository",
117 tool: "search",
118 arguments: { query: "activity projection" },
119 aggregatedOutput: "mcp payload remains available",
120 },
121 ignored: "MCP data is rendered verbatim",
122 }),
123 makeActivity("search", "web_search", {
124 rawOutput: {
125 totalFiles: 42,
126 truncated: true,
127 content: "ignored because totalFiles wins",
128 },
129 ignored: "top-level bulk",
130 }),
131 makeActivity("image", "image_view", {
132 ignored: "top-level bulk",
133 }),
134] satisfies ReadonlyArray<OrchestrationThreadActivity>;
135
136describe("projectActivityPayload", () => {
137 function comparableActivity(activity: ThreadFeedActivity) {
138 return {
139 ...activity,
140 fullDetail: activity.getFullDetail(),
141 copyText: activity.getCopyText(),
142 getFullDetail: undefined,
143 getCopyText: undefined,
144 };
145 }
146
147 function comparableThreadFeed(activities: ReadonlyArray<OrchestrationThreadActivity>) {
148 return buildThreadFeed(makeThread(activities)).map((entry) =>
149 entry.type === "activity-group"
150 ? {
151 ...entry,
152 activities: entry.activities.map(comparableActivity),
153 }
154 : entry,
155 );
156 }
157
158 it("drops unread bulk while retaining command, file, tool, and summary inputs", () => {
159 const projected = projectActivityPayload(fixtures[0]!);
160 expect(projected.payload).toEqual({
161 itemType: "command_execution",
162 title: "command_execution",
163 detail: "command_execution detail",
164 status: "completed",
165 requestKind: "command",
166 data: {
167 item: {
168 command: ["bash", "-lc", "pnpm test"],
169 input: { command: "fallback input" },
170 result: { command: "fallback result" },
171 },
172 command: "fallback data",
173 toolCallId: "tool-command",
174 kind: "execute",
175 rawOutput: { content: "first useful line" },
176 },
177 });
178
179 expect(projectActivityPayload(fixtures[1]!).payload).toMatchObject({
180 data: {
181 files: [{ path: "src/new.ts" }, { path: "src/old.ts" }, { path: "src/second.ts" }],
182 },
183 });
184 });
185
186 it("passes MCP tool data through unchanged", () => {
187 expect(projectActivityPayload(fixtures[4]!)).toBe(fixtures[4]);
188 });
189
190 it("keeps current web and mobile derived output identical for every tool item type", () => {
191 for (const activity of fixtures) {
192 const projected = projectActivityPayload(activity);
193 expect(deriveWorkLogEntries([projected])).toEqual(deriveWorkLogEntries([activity]));
194 expect(comparableThreadFeed([projected])).toEqual(comparableThreadFeed([activity]));
195 }
196 });
197
198 it("projects snapshot and event transports without mutating their sources", () => {
199 const activity = fixtures[0]!;
200 const thread = makeThread([activity]);
201 const snapshot = { snapshotSequence: 7, thread };
202 const projectedSnapshot = projectThreadDetailSnapshot(snapshot);
203
204 expect(projectedSnapshot.thread.activities[0]).not.toBe(activity);
205 expect(snapshot.thread.activities[0]).toBe(activity);
206
207 const event = {
208 sequence: 8,
209 eventId: EventId.make("event-activity"),
210 aggregateKind: "thread",
211 aggregateId: thread.id,
212 occurredAt: "2026-07-27T00:00:01.000Z",
213 commandId: null,
214 causationEventId: null,
215 correlationId: null,
216 metadata: {},
217 type: "thread.activity-appended",
218 payload: {
219 threadId: thread.id,
220 activity,
221 },
222 } satisfies Extract<OrchestrationEvent, { type: "thread.activity-appended" }>;
223
224 const projectedEvent = projectActivityEvent(event);
225 expect(projectedEvent).not.toBe(event);
226 expect(
227 projectedEvent.type === "thread.activity-appended"
228 ? projectedEvent.payload.activity
229 : undefined,
230 ).toEqual(projectActivityPayload(activity));
231 expect(event.payload.activity).toBe(activity);
232 });
233});