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

pendingRequests.test.ts

516 lines15.7 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]).toMatchObject({
286 questions: [question],
287 dismissible: true,
288 });
289 });
290​
291 it("only marks async questions as dismissible", () => {
292 const question = {
293 id: "0",
294 header: "Question",
295 question: "Continue?",
296 options: [{ label: "Yes", description: "" }],
297 multiSelect: false,
298 };
299 const native = makeActivity({
300 kind: "user-input.requested",
301 payload: { requestId: "native-1", questions: [question] },
302 });
303 expect(derivePendingRequests([native]).userInputs[0]?.dismissible).toBe(false);
304 });
305​
306 it("preserves native choice values and the custom-answer restriction", () => {
307 const question = {
308 id: "interaction-result",
309 header: "Result",
310 question: "Which result should be used?",
311 options: [
312 { value: " first\t", label: "Result", description: "First result" },
313 { value: "second", label: "Result", description: "Second result" },
314 ],
315 allowCustomAnswer: false,
316 multiSelect: false,
317 };
318 const activities = [
319 makeActivity({
320 id: "native-user-input",
321 kind: "user-input.requested",
322 summary: "User input requested",
323 payload: { requestId: "req-native-choice", questions: [question] },
324 }),
325 ];
326​
327 expect(derivePendingRequests(activities).userInputs[0]?.questions).toEqual([question]);
328 });
329​
330 it("tracks open structured prompts and removes resolved ones", () => {
331 const activities: OrchestrationThreadActivity[] = [
332 makeActivity({
333 id: "user-input-open",
334 createdAt: "2026-02-23T00:00:01.000Z",
335 kind: "user-input.requested",
336 summary: "User input requested",
337 tone: "info",
338 payload: {
339 requestId: "req-user-input-1",
340 questions: [
341 {
342 id: "sandbox_mode",
343 header: "Sandbox",
344 question: "Which mode should be used?",
345 options: [
346 {
347 label: "workspace-write",
348 description: "Allow workspace writes only",
349 },
350 ],
351 multiSelect: true,
352 },
353 ],
354 },
355 }),
356 makeActivity({
357 id: "user-input-resolved",
358 createdAt: "2026-02-23T00:00:02.000Z",
359 kind: "user-input.resolved",
360 summary: "User input submitted",
361 tone: "info",
362 payload: {
363 requestId: "req-user-input-2",
364 answers: {
365 sandbox_mode: "workspace-write",
366 },
367 },
368 }),
369 makeActivity({
370 id: "user-input-open-2",
371 createdAt: "2026-02-23T00:00:01.500Z",
372 kind: "user-input.requested",
373 summary: "User input requested",
374 tone: "info",
375 payload: {
376 requestId: "req-user-input-2",
377 questions: [
378 {
379 id: "approval",
380 header: "Approval",
381 question: "Continue?",
382 options: [
383 {
384 label: "yes",
385 description: "Continue execution",
386 },
387 ],
388 multiSelect: false,
389 },
390 ],
391 },
392 }),
393 ];
394​
395 expect(derivePendingRequests(activities).userInputs).toEqual([
396 {
397 requestId: "req-user-input-1",
398 createdAt: "2026-02-23T00:00:01.000Z",
399 dismissible: false,
400 questions: [
401 {
402 id: "sandbox_mode",
403 header: "Sandbox",
404 question: "Which mode should be used?",
405 options: [
406 {
407 label: "workspace-write",
408 description: "Allow workspace writes only",
409 },
410 ],
411 multiSelect: true,
412 },
413 ],
414 },
415 ]);
416 });
417​
418 it("clears stale pending user-input prompts when the provider reports an orphaned request", () => {
419 const activities: OrchestrationThreadActivity[] = [
420 makeActivity({
421 id: "user-input-open-stale",
422 createdAt: "2026-02-23T00:00:01.000Z",
423 kind: "user-input.requested",
424 summary: "User input requested",
425 tone: "info",
426 payload: {
427 requestId: "req-user-input-stale-1",
428 questions: [
429 {
430 id: "sandbox_mode",
431 header: "Sandbox",
432 question: "Which mode should be used?",
433 options: [
434 {
435 label: "workspace-write",
436 description: "Allow workspace writes only",
437 },
438 ],
439 multiSelect: false,
440 },
441 ],
442 },
443 }),
444 makeActivity({
445 id: "user-input-failed-stale",
446 createdAt: "2026-02-23T00:00:02.000Z",
447 kind: "provider.user-input.respond.failed",
448 summary: "Provider user input response failed",
449 tone: "error",
450 payload: {
451 requestId: "req-user-input-stale-1",
452 detail:
453 "Provider adapter request failed (codex) for item/tool/requestUserInput: Unknown pending Codex user input request: req-user-input-stale-1",
454 },
455 }),
456 ];
457​
458 expect(derivePendingRequests(activities).userInputs).toEqual([]);
459 });
460});
461​
462describe.each(["approval", "user-input"])("%s request completion", (requestKind) => {
463 const requested = makeActivity({
464 id: `${requestKind}-requested`,
465 kind: `${requestKind}.requested`,
466 sequence: 42,
467 payload: {
468 requestId: "request-1",
469 requestKind: "command",
470 questions: [{ id: "answer", header: "Answer", question: "Continue?", options: [] }],
471 },
472 });
473​
474 it.each([`${requestKind}.resolved`, `provider.${requestKind}.respond.failed`])(
475 "keeps %s final across reordered and repeated activities",
476 (kind) => {
477 const closed = makeActivity({
478 id: `${requestKind}-closed`,
479 kind,
480 createdAt: "2026-02-23T00:00:01.000Z",
481 payload: {
482 requestId: "request-1",
483 detail: `Unknown pending ${requestKind} request: request-1`,
484 },
485 });
486 const replayedRequest = { ...requested, id: EventId.make("replayed-request"), sequence: 43 };
487​
488 for (const activities of [
489 [requested, closed, replayedRequest],
490 [closed, requested, replayedRequest],
491 ]) {
492 expect(derivePendingRequests(activities)).toEqual({ approvals: [], userInputs: [] });
493 }
494 },
495 );
496​
497 it("keeps a failed reply retryable unless the text names a stale request", () => {
498 const failed = makeActivity({
499 kind: `provider.${requestKind}.respond.failed`,
500 payload: { requestId: "request-1", detail: "Provider adapter request failed: timeout" },
501 });
502 const pending = derivePendingRequests([requested, failed]);
503 expect(
504 [...pending.approvals, ...pending.userInputs].map((request) => request.requestId),
505 ).toEqual(["request-1"]);
506​
507 const retried = makeActivity({
508 kind: `${requestKind}.resolved`,
509 payload: { requestId: "request-1" },
510 });
511 expect(derivePendingRequests([requested, failed, retried, failed])).toEqual({
512 approvals: [],
513 userInputs: [],
514 });
515 });
516});