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

pendingRequests.test.ts

497 lines15.2 KB

1import { EventId, TurnId, type OrchestrationThreadActivity } from "@t3tools/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { derivePendingRequests } from "./pendingRequests.ts";
4​
5let nextActivityId = 0;
6​
7function makeActivity(overrides: {
8 id?: string;
9 createdAt?: string;
10 kind?: string;
11 summary?: string;
12 tone?: OrchestrationThreadActivity["tone"];
13 payload?: Record<string, unknown>;
14 turnId?: string;
15 sequence?: number;
16}): OrchestrationThreadActivity {
17 return {
18 id: EventId.make(overrides.id ?? `activity-${nextActivityId++}`),
19 createdAt: overrides.createdAt ?? "2026-02-23T00:00:00.000Z",
20 kind: overrides.kind ?? "tool.started",
21 summary: overrides.summary ?? "Tool call",
22 tone: overrides.tone ?? "tool",
23 payload: overrides.payload ?? {},
24 turnId: overrides.turnId ? TurnId.make(overrides.turnId) : null,
25 ...(overrides.sequence !== undefined ? { sequence: overrides.sequence } : {}),
26 };
28​
29describe("pending approvals", () => {
30 it.each([{}, { requestType: "unknown" }])(
31 "exposes legacy OpenCode approvals without a known request kind: %j",
32 (legacyPayload) => {
33 const requested = makeActivity({
34 kind: "approval.requested",
35 payload: { requestId: "per-legacy", detail: "*", ...legacyPayload },
36 });
37​
38 expect(derivePendingRequests([requested]).approvals).toEqual([
39 {
40 requestId: "per-legacy",
41 requestKind: "command",
42 createdAt: requested.createdAt,
43 detail: "*",
44 },
45 ]);
46 },
47 );
48​
49 it.each(["tool_user_input", "auth_tokens_refresh"])(
50 "does not turn %s into an approval",
51 (requestType) => {
52 const activity = makeActivity({
53 kind: "approval.requested",
54 payload: { requestId: "not-an-approval", requestType },
55 });
56​
57 expect(derivePendingRequests([activity]).approvals).toEqual([]);
58 },
59 );
60​
61 it("tracks open approvals and removes resolved ones", () => {
62 const activities: OrchestrationThreadActivity[] = [
63 makeActivity({
64 id: "approval-open",
65 createdAt: "2026-02-23T00:00:01.000Z",
66 kind: "approval.requested",
67 summary: "Command approval requested",
68 tone: "approval",
69 payload: {
70 requestId: "req-1",
71 requestKind: "command",
72 detail: "bun run lint",
73 },
74 }),
75 makeActivity({
76 id: "approval-close",
77 createdAt: "2026-02-23T00:00:02.000Z",
78 kind: "approval.resolved",
79 summary: "Approval resolved",
80 tone: "info",
81 payload: { requestId: "req-2" },
82 }),
83 makeActivity({
84 id: "approval-closed-request",
85 createdAt: "2026-02-23T00:00:01.500Z",
86 kind: "approval.requested",
87 summary: "File-change approval requested",
88 tone: "approval",
89 payload: { requestId: "req-2", requestType: "unknown" },
90 }),
91 ];
92​
93 expect(derivePendingRequests(activities).approvals).toEqual([
94 {
95 requestId: "req-1",
96 requestKind: "command",
97 createdAt: "2026-02-23T00:00:01.000Z",
98 detail: "bun run lint",
99 },
100 ]);
101 });
102​
103 it("maps canonical requestType payloads into pending approvals", () => {
104 const activities: OrchestrationThreadActivity[] = [
105 makeActivity({
106 id: "approval-open-request-type",
107 createdAt: "2026-02-23T00:00:01.000Z",
108 kind: "approval.requested",
109 summary: "Command approval requested",
110 tone: "approval",
111 payload: {
112 requestId: "req-request-type",
113 requestType: "command_execution_approval",
114 detail: "pwd",
115 },
116 }),
117 ];
118​
119 expect(derivePendingRequests(activities).approvals).toEqual([
120 {
121 requestId: "req-request-type",
122 requestKind: "command",
123 createdAt: "2026-02-23T00:00:01.000Z",
124 detail: "pwd",
125 },
126 ]);
127 });
128​
129 it("keeps app access approvals and persistence choices from remote activities", () => {
130 const options = [
131 { decision: "decline", label: "Decline" },
132 { decision: "acceptAlways", label: "Always allow Safari" },
133 { decision: "accept", label: "Approve" },
134 ];
135 const activities = [
136 makeActivity({
137 kind: "approval.requested",
138 summary: "App access approval requested",
139 tone: "approval",
140 payload: {
141 requestId: "req-safari",
142 requestType: "mcp_elicitation_approval",
143 detail: "Allow ChatGPT to use Safari?",
144 appName: "Safari",
145 options,
146 },
147 }),
148 ];
149​
150 expect(derivePendingRequests(activities).approvals).toEqual([
151 {
152 requestId: "req-safari",
153 requestKind: "mcp-elicitation",
154 createdAt: "2026-02-23T00:00:00.000Z",
155 detail: "Allow ChatGPT to use Safari?",
156 appName: "Safari",
157 options,
158 },
159 ]);
160 });
161​
162 it("derives dynamic tool requests as actionable generic approvals", () => {
163 const activities: OrchestrationThreadActivity[] = [
164 makeActivity({
165 id: "approval-open-dynamic-tool",
166 createdAt: "2026-02-23T00:00:01.000Z",
167 kind: "approval.requested",
168 summary: "Approval requested",
169 tone: "approval",
170 payload: {
171 requestId: "req-dynamic-tool",
172 requestType: "dynamic_tool_call",
173 detail: "Search the web",
174 },
175 }),
176 ];
177​
178 expect(derivePendingRequests(activities).approvals).toEqual([
179 {
180 requestId: "req-dynamic-tool",
181 requestKind: "command",
182 createdAt: "2026-02-23T00:00:01.000Z",
183 detail: "Search the web",
184 },
185 ]);
186 });
187​
188 it("clears stale pending approvals when provider reports unknown pending request", () => {
189 const activities: OrchestrationThreadActivity[] = [
190 makeActivity({
191 id: "approval-open-stale",
192 createdAt: "2026-02-23T00:00:01.000Z",
193 kind: "approval.requested",
194 summary: "Command approval requested",
195 tone: "approval",
196 payload: {
197 requestId: "req-stale-1",
198 requestType: "unknown",
199 },
200 }),
201 makeActivity({
202 id: "approval-failed-stale",
203 createdAt: "2026-02-23T00:00:02.000Z",
204 kind: "provider.approval.respond.failed",
205 summary: "Provider approval response failed",
206 tone: "error",
207 payload: {
208 requestId: "req-stale-1",
209 detail: "Unknown pending permission request: req-stale-1",
210 },
211 }),
212 ];
213​
214 expect(derivePendingRequests(activities).approvals).toEqual([]);
215 });
216​
217 it("clears stale pending approvals when the backend marks them stale after restart", () => {
218 const activities: OrchestrationThreadActivity[] = [
219 makeActivity({
220 id: "approval-open-stale-restart",
221 createdAt: "2026-02-23T00:00:01.000Z",
222 kind: "approval.requested",
223 summary: "Command approval requested",
224 tone: "approval",
225 payload: {
226 requestId: "req-stale-restart-1",
227 requestKind: "command",
228 },
229 }),
230 makeActivity({
231 id: "approval-failed-stale-restart",
232 createdAt: "2026-02-23T00:00:02.000Z",
233 kind: "provider.approval.respond.failed",
234 summary: "Provider approval response failed",
235 tone: "error",
236 payload: {
237 requestId: "req-stale-restart-1",
238 detail:
239 "Stale pending approval request: req-stale-restart-1. Provider callback state does not survive app restarts or recovered sessions. Restart the turn to continue.",
240 },
241 }),
242 ];
243​
244 expect(derivePendingRequests(activities).approvals).toEqual([]);
245 });
246});
247​
248describe("pending questions", () => {
249 it("preserves native answer keys while ignoring malformed options", () => {
250 const question = {
251 id: " Which path?\n",
252 header: " Path ",
253 question: " Which path?\n",
254 options: [{ label: " Keep spaces ", description: "", value: " native\t" }],
255 multiSelect: false,
256 };
257 const requested = makeActivity({
258 kind: "user-input.requested",
259 payload: {
260 requestId: "native-question",
261 questions: [null, { ...question, options: [...question.options, { label: 42 }] }],
262 },
263 });
264​
265 expect(derivePendingRequests([requested]).userInputs[0]?.questions).toEqual([question]);
266 });
267​
268 it("keeps free-text questions without suggested answers", () => {
269 const question = {
270 id: "0",
271 header: "Question",
272 question: "What should it be named?",
273 options: [],
274 allowCustomAnswer: true,
275 multiSelect: false,
276 };
277 const activities = [
278 makeActivity({
279 id: "async-question",
280 kind: "user-input.requested",
281 summary: "User input requested",
282 payload: { requestId: "async-1", responseMode: "message", questions: [question] },
283 }),
284 ];
285 expect(derivePendingRequests(activities).userInputs[0]?.questions).toEqual([question]);
286 });
287​
288 it("preserves native choice values and the custom-answer restriction", () => {
289 const question = {
290 id: "interaction-result",
291 header: "Result",
292 question: "Which result should be used?",
293 options: [
294 { value: " first\t", label: "Result", description: "First result" },
295 { value: "second", label: "Result", description: "Second result" },
296 ],
297 allowCustomAnswer: false,
298 multiSelect: false,
299 };
300 const activities = [
301 makeActivity({
302 id: "native-user-input",
303 kind: "user-input.requested",
304 summary: "User input requested",
305 payload: { requestId: "req-native-choice", questions: [question] },
306 }),
307 ];
308​
309 expect(derivePendingRequests(activities).userInputs[0]?.questions).toEqual([question]);
310 });
311​
312 it("tracks open structured prompts and removes resolved ones", () => {
313 const activities: OrchestrationThreadActivity[] = [
314 makeActivity({
315 id: "user-input-open",
316 createdAt: "2026-02-23T00:00:01.000Z",
317 kind: "user-input.requested",
318 summary: "User input requested",
319 tone: "info",
320 payload: {
321 requestId: "req-user-input-1",
322 questions: [
323 {
324 id: "sandbox_mode",
325 header: "Sandbox",
326 question: "Which mode should be used?",
327 options: [
328 {
329 label: "workspace-write",
330 description: "Allow workspace writes only",
331 },
332 ],
333 multiSelect: true,
334 },
335 ],
336 },
337 }),
338 makeActivity({
339 id: "user-input-resolved",
340 createdAt: "2026-02-23T00:00:02.000Z",
341 kind: "user-input.resolved",
342 summary: "User input submitted",
343 tone: "info",
344 payload: {
345 requestId: "req-user-input-2",
346 answers: {
347 sandbox_mode: "workspace-write",
348 },
349 },
350 }),
351 makeActivity({
352 id: "user-input-open-2",
353 createdAt: "2026-02-23T00:00:01.500Z",
354 kind: "user-input.requested",
355 summary: "User input requested",
356 tone: "info",
357 payload: {
358 requestId: "req-user-input-2",
359 questions: [
360 {
361 id: "approval",
362 header: "Approval",
363 question: "Continue?",
364 options: [
365 {
366 label: "yes",
367 description: "Continue execution",
368 },
369 ],
370 multiSelect: false,
371 },
372 ],
373 },
374 }),
375 ];
376​
377 expect(derivePendingRequests(activities).userInputs).toEqual([
378 {
379 requestId: "req-user-input-1",
380 createdAt: "2026-02-23T00:00:01.000Z",
381 questions: [
382 {
383 id: "sandbox_mode",
384 header: "Sandbox",
385 question: "Which mode should be used?",
386 options: [
387 {
388 label: "workspace-write",
389 description: "Allow workspace writes only",
390 },
391 ],
392 multiSelect: true,
393 },
394 ],
395 },
396 ]);
397 });
398​
399 it("clears stale pending user-input prompts when the provider reports an orphaned request", () => {
400 const activities: OrchestrationThreadActivity[] = [
401 makeActivity({
402 id: "user-input-open-stale",
403 createdAt: "2026-02-23T00:00:01.000Z",
404 kind: "user-input.requested",
405 summary: "User input requested",
406 tone: "info",
407 payload: {
408 requestId: "req-user-input-stale-1",
409 questions: [
410 {
411 id: "sandbox_mode",
412 header: "Sandbox",
413 question: "Which mode should be used?",
414 options: [
415 {
416 label: "workspace-write",
417 description: "Allow workspace writes only",
418 },
419 ],
420 multiSelect: false,
421 },
422 ],
423 },
424 }),
425 makeActivity({
426 id: "user-input-failed-stale",
427 createdAt: "2026-02-23T00:00:02.000Z",
428 kind: "provider.user-input.respond.failed",
429 summary: "Provider user input response failed",
430 tone: "error",
431 payload: {
432 requestId: "req-user-input-stale-1",
433 detail:
434 "Provider adapter request failed (codex) for item/tool/requestUserInput: Unknown pending Codex user input request: req-user-input-stale-1",
435 },
436 }),
437 ];
438​
439 expect(derivePendingRequests(activities).userInputs).toEqual([]);
440 });
441});
442​
443describe.each(["approval", "user-input"])("%s request completion", (requestKind) => {
444 const requested = makeActivity({
445 id: `${requestKind}-requested`,
446 kind: `${requestKind}.requested`,
447 sequence: 42,
448 payload: {
449 requestId: "request-1",
450 requestKind: "command",
451 questions: [{ id: "answer", header: "Answer", question: "Continue?", options: [] }],
452 },
453 });
454​
455 it.each([`${requestKind}.resolved`, `provider.${requestKind}.respond.failed`])(
456 "keeps %s final across reordered and repeated activities",
457 (kind) => {
458 const closed = makeActivity({
459 id: `${requestKind}-closed`,
460 kind,
461 createdAt: "2026-02-23T00:00:01.000Z",
462 payload: {
463 requestId: "request-1",
464 detail: `Unknown pending ${requestKind} request: request-1`,
465 },
466 });
467 const replayedRequest = { ...requested, id: EventId.make("replayed-request"), sequence: 43 };
468​
469 for (const activities of [
470 [requested, closed, replayedRequest],
471 [closed, requested, replayedRequest],
472 ]) {
473 expect(derivePendingRequests(activities)).toEqual({ approvals: [], userInputs: [] });
474 }
475 },
476 );
477​
478 it("keeps a failed reply retryable unless the text names a stale request", () => {
479 const failed = makeActivity({
480 kind: `provider.${requestKind}.respond.failed`,
481 payload: { requestId: "request-1", detail: "Provider adapter request failed: timeout" },
482 });
483 const pending = derivePendingRequests([requested, failed]);
484 expect(
485 [...pending.approvals, ...pending.userInputs].map((request) => request.requestId),
486 ).toEqual(["request-1"]);
487​
488 const retried = makeActivity({
489 kind: `${requestKind}.resolved`,
490 payload: { requestId: "request-1" },
491 });
492 expect(derivePendingRequests([requested, failed, retried, failed])).toEqual({
493 approvals: [],
494 userInputs: [],
495 });
496 });
497});