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

CommandPolicy.test.ts

706 lines22.3 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 MessageId,
5 type OrchestrationV2ProviderCapabilities,
6 type OrchestrationV2ThreadProjection,
7 ProviderInstanceId,
8 ProviderSessionId,
9 ProviderThreadId,
10 RunAttemptId,
11 RunId,
12 ThreadId,
13} from "@t3tools/contracts";
14import * as Effect from "effect/Effect";
15​
16import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
17import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing";
18import { GrokProviderCapabilitiesV2 } from "@t3tools/provider-grok/testing";
19import * as CommandPolicy from "./CommandPolicy.ts";
20​
21const commandId = CommandId.make("command-policy-test");
22const threadId = ThreadId.make("command-policy-thread");
23const activeRunId = RunId.make("command-policy-active-run");
24const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");
25const userMessageId = MessageId.make("command-policy-user-message");
26​
27const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
28​
29function capabilities(
30 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
31): OrchestrationV2ProviderCapabilities {
32 return override(baseCapabilities);
34​
35function dispatchProjection(
36 sessionCapabilities?: OrchestrationV2ProviderCapabilities,
37): OrchestrationV2ThreadProjection {
38 const providerThreadId = ProviderThreadId.make("command-policy-provider-thread");
39 const providerSessionId = ProviderSessionId.make("command-policy-provider-session");
40 return {
41 runs:
42 sessionCapabilities === undefined
43 ? []
44 : [
45 {
46 id: activeRunId,
47 status: "running",
48 providerThreadId,
49 activeAttemptId,
50 userMessageId,
51 },
52 ],
53 messages: [{ id: userMessageId, text: "first", attachments: [] }],
54 providerThreads:
55 sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],
56 providerSessions:
57 sessionCapabilities === undefined
58 ? []
59 : [{ id: providerSessionId, capabilities: sessionCapabilities }],
60 providerTurns:
61 sessionCapabilities === undefined
62 ? []
63 : [{ runAttemptId: activeAttemptId, status: "running" }],
64 } as unknown as OrchestrationV2ThreadProjection;
66​
67it("resolves automatic message delivery from authoritative provider capabilities", () => {
68 assert.deepEqual(
69 CommandPolicy.resolveMessageDispatchIntent(
70 dispatchProjection(),
71 { type: "start_immediately" },
72 "auto",
73 ),
74 { type: "start_immediately" },
75 );
76 assert.deepEqual(
77 CommandPolicy.resolveMessageDispatchIntent(
78 dispatchProjection(baseCapabilities),
79 { type: "start_immediately" },
80 "auto",
81 ),
82 { type: "steer_active", targetRunId: activeRunId },
83 );
84 assert.deepEqual(
85 CommandPolicy.resolveMessageDispatchIntent(
86 dispatchProjection(
87 capabilities((current) => ({
88 ...current,
89 turns: {
90 ...current.turns,
91 supportsActiveSteering: false,
92 supportsQueuedMessages: true,
93 supportsSteeringByInterruptRestart: true,
94 },
95 })),
96 ),
97 { type: "start_immediately" },
98 "auto",
99 ),
100 { type: "queue_after_active" },
101 );
102 assert.deepEqual(
103 CommandPolicy.resolveMessageDispatchIntent(
104 dispatchProjection(
105 capabilities((current) => ({
106 ...current,
107 turns: {
108 ...current.turns,
109 supportsActiveSteering: false,
110 supportsQueuedMessages: false,
111 supportsSteeringByInterruptRestart: true,
112 },
113 })),
114 ),
115 { type: "start_immediately" },
116 "auto",
117 ),
118 { type: "restart_active", targetRunId: activeRunId },
119 );
120});
121​
122it.each(["preparing", "starting"] as const)(
123 "queues an automatic message while the handoff run is %s",
124 (status) => {
125 const projection = dispatchProjection(baseCapabilities);
126 assert.deepEqual(
127 CommandPolicy.resolveMessageDispatchIntent(
128 { ...projection, runs: projection.runs.map((run) => ({ ...run, status })) },
129 { type: "start_immediately" },
130 "auto",
131 ),
132 { type: "queue_after_active" },
133 );
134 },
135);
136​
137it.each([
138 ["has not started", []],
139 ["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],
140 ["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],
141 [
142 "belongs to a previous attempt",
143 [{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],
144 ],
145])("queues only automatic delivery when the active run's provider turn %s", (_, providerTurns) => {
146 const projection = {
147 ...dispatchProjection(baseCapabilities),
148 providerTurns,
149 } as unknown as OrchestrationV2ThreadProjection;
150 assert.deepEqual(
151 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
152 { type: "queue_after_active" },
153 );
154 assert.deepEqual(
155 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
156 { type: "steer_active", targetRunId: activeRunId },
157 );
158 assert.deepEqual(
159 CommandPolicy.resolveMessageDispatchIntent(
160 projection,
161 { type: "start_immediately" },
162 "restart",
163 ),
164 { type: "restart_active", targetRunId: activeRunId },
165 );
166});
167​
168it("targets the latest active run for explicit steer and restart intent", () => {
169 const projection = dispatchProjection(baseCapabilities);
170 assert.deepEqual(
171 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
172 { type: "steer_active", targetRunId: activeRunId },
173 );
174 assert.deepEqual(
175 CommandPolicy.resolveMessageDispatchIntent(
176 projection,
177 { type: "start_immediately" },
178 "restart",
179 ),
180 { type: "restart_active", targetRunId: activeRunId },
181 );
182 assert.deepEqual(
183 CommandPolicy.resolveMessageDispatchIntent(
184 dispatchProjection(),
185 { type: "start_immediately" },
186 "steer",
187 ),
188 { type: "start_immediately" },
189 );
190});
191​
192it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
193 const projection = dispatchProjection(baseCapabilities);
194 const maintenance = {
195 ...projection,
196 messages: [{ id: userMessageId, text, attachments: [] }],
197 } as unknown as OrchestrationV2ThreadProjection;
198 for (const deliveryIntent of ["auto", "steer"] as const) {
199 assert.deepEqual(
200 CommandPolicy.resolveMessageDispatchIntent(
201 maintenance,
202 { type: "start_immediately" },
203 deliveryIntent,
204 ),
205 { type: "queue_after_active" },
206 );
207 }
208 assert.deepEqual(
209 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
210 type: "steer_active",
211 targetRunId: activeRunId,
212 }),
213 { type: "queue_after_active" },
214 );
215 assert.deepEqual(
216 CommandPolicy.resolveMessageDispatchIntent(
217 maintenance,
218 { type: "start_immediately" },
219 "restart",
220 ),
221 { type: "restart_active", targetRunId: activeRunId },
222 );
223});
224​
225const layer = it.layer(CommandPolicy.layer);
226​
227layer("CommandPolicyV2", (it) => {
228 it.effect("prefers direct active steering when the provider supports it", () =>
229 Effect.gen(function* () {
230 const policy = yield* CommandPolicy.CommandPolicyV2;
231​
232 const result = yield* policy.decideSteeringExecution({
233 commandId,
234 threadId,
235 providerInstanceId: ProviderInstanceId.make("codex"),
236 capabilities: baseCapabilities,
237 });
238​
239 assert.equal(result, "active_steering");
240 }),
241 );
242​
243 it.effect("uses interrupt-and-restart steering when direct steering is unavailable", () =>
244 Effect.gen(function* () {
245 const policy = yield* CommandPolicy.CommandPolicyV2;
246​
247 const result = yield* policy.decideSteeringExecution({
248 commandId,
249 threadId,
250 providerInstanceId: ProviderInstanceId.make("codex"),
251 capabilities: capabilities((current) => ({
252 ...current,
253 turns: {
254 ...current.turns,
255 supportsActiveSteering: false,
256 supportsInterrupt: true,
257 supportsSteeringByInterruptRestart: true,
258 },
259 })),
260 });
261​
262 assert.equal(result, "interrupt_restart");
263 }),
264 );
265​
266 it.effect("uses interrupt-and-restart steering for Grok ACP", () =>
267 Effect.gen(function* () {
268 const policy = yield* CommandPolicy.CommandPolicyV2;
269​
270 const result = yield* policy.decideSteeringExecution({
271 commandId,
272 threadId,
273 providerInstanceId: ProviderInstanceId.make("grok"),
274 capabilities: GrokProviderCapabilitiesV2,
275 });
276​
277 assert.equal(result, "interrupt_restart");
278 }),
279 );
280​
281 it.effect("honors an explicit interrupt-and-restart request", () =>
282 Effect.gen(function* () {
283 const policy = yield* CommandPolicy.CommandPolicyV2;
284​
285 const result = yield* policy.decideSteeringExecution({
286 commandId,
287 threadId,
288 providerInstanceId: ProviderInstanceId.make("codex"),
289 capabilities: CodexProviderCapabilitiesV2,
290 forceRestart: true,
291 });
292​
293 assert.equal(result, "interrupt_restart");
294 }),
295 );
296​
297 it.effect("reports the actually missing capability across the steering matrix", () =>
298 Effect.gen(function* () {
299 const policy = yield* CommandPolicy.CommandPolicyV2;
300 const providerInstanceId = ProviderInstanceId.make("codex");
301 const fallbackDetail =
302 "providerInstanceId cannot steer active turns directly or by interrupt-and-restart";
303 const forcedRestartDetail =
304 "providerInstanceId cannot satisfy a required interrupt-and-restart";
305​
306 const cases: ReadonlyArray<{
307 readonly forceRestart: boolean;
308 readonly supportsActiveSteering: boolean;
309 readonly supportsInterrupt: boolean;
310 readonly supportsSteeringByInterruptRestart: boolean;
311 readonly expected:
312 | { readonly type: "policy"; readonly value: CommandPolicy.SteeringExecutionPolicyV2 }
313 | {
314 readonly type: "error";
315 readonly capability: CommandPolicy.CommandPolicyCapability;
316 readonly detail: string;
317 };
318 }> = [
319 {
320 forceRestart: false,
321 supportsActiveSteering: true,
322 supportsInterrupt: false,
323 supportsSteeringByInterruptRestart: false,
324 expected: { type: "policy", value: "active_steering" },
325 },
326 {
327 forceRestart: false,
328 supportsActiveSteering: true,
329 supportsInterrupt: false,
330 supportsSteeringByInterruptRestart: true,
331 expected: { type: "policy", value: "active_steering" },
332 },
333 {
334 forceRestart: false,
335 supportsActiveSteering: true,
336 supportsInterrupt: true,
337 supportsSteeringByInterruptRestart: false,
338 expected: { type: "policy", value: "active_steering" },
339 },
340 {
341 forceRestart: false,
342 supportsActiveSteering: true,
343 supportsInterrupt: true,
344 supportsSteeringByInterruptRestart: true,
345 expected: { type: "policy", value: "active_steering" },
346 },
347 {
348 forceRestart: false,
349 supportsActiveSteering: false,
350 supportsInterrupt: true,
351 supportsSteeringByInterruptRestart: true,
352 expected: { type: "policy", value: "interrupt_restart" },
353 },
354 {
355 forceRestart: false,
356 supportsActiveSteering: false,
357 supportsInterrupt: true,
358 supportsSteeringByInterruptRestart: false,
359 expected: {
360 type: "error",
361 capability: "interrupt_restart_steering",
362 detail: fallbackDetail,
363 },
364 },
365 {
366 forceRestart: false,
367 supportsActiveSteering: false,
368 supportsInterrupt: false,
369 supportsSteeringByInterruptRestart: true,
370 expected: { type: "error", capability: "active_steering", detail: fallbackDetail },
371 },
372 {
373 forceRestart: false,
374 supportsActiveSteering: false,
375 supportsInterrupt: false,
376 supportsSteeringByInterruptRestart: false,
377 expected: { type: "error", capability: "active_steering", detail: fallbackDetail },
378 },
379 {
380 forceRestart: true,
381 supportsActiveSteering: true,
382 supportsInterrupt: true,
383 supportsSteeringByInterruptRestart: true,
384 expected: { type: "policy", value: "interrupt_restart" },
385 },
386 {
387 forceRestart: true,
388 supportsActiveSteering: false,
389 supportsInterrupt: true,
390 supportsSteeringByInterruptRestart: true,
391 expected: { type: "policy", value: "interrupt_restart" },
392 },
393 {
394 forceRestart: true,
395 supportsActiveSteering: true,
396 supportsInterrupt: true,
397 supportsSteeringByInterruptRestart: false,
398 expected: {
399 type: "error",
400 capability: "interrupt_restart_steering",
401 detail: forcedRestartDetail,
402 },
403 },
404 {
405 forceRestart: true,
406 supportsActiveSteering: false,
407 supportsInterrupt: true,
408 supportsSteeringByInterruptRestart: false,
409 expected: {
410 type: "error",
411 capability: "interrupt_restart_steering",
412 detail: forcedRestartDetail,
413 },
414 },
415 {
416 forceRestart: true,
417 supportsActiveSteering: true,
418 supportsInterrupt: false,
419 supportsSteeringByInterruptRestart: true,
420 expected: {
421 type: "error",
422 capability: "interrupt_restart_steering",
423 detail: forcedRestartDetail,
424 },
425 },
426 {
427 forceRestart: true,
428 supportsActiveSteering: false,
429 supportsInterrupt: false,
430 supportsSteeringByInterruptRestart: true,
431 expected: {
432 type: "error",
433 capability: "interrupt_restart_steering",
434 detail: forcedRestartDetail,
435 },
436 },
437 {
438 forceRestart: true,
439 supportsActiveSteering: true,
440 supportsInterrupt: false,
441 supportsSteeringByInterruptRestart: false,
442 expected: {
443 type: "error",
444 capability: "interrupt_restart_steering",
445 detail: forcedRestartDetail,
446 },
447 },
448 {
449 forceRestart: true,
450 supportsActiveSteering: false,
451 supportsInterrupt: false,
452 supportsSteeringByInterruptRestart: false,
453 expected: {
454 type: "error",
455 capability: "interrupt_restart_steering",
456 detail: forcedRestartDetail,
457 },
458 },
459 ];
460​
461 for (const entry of cases) {
462 const decision = policy.decideSteeringExecution({
463 commandId,
464 threadId,
465 providerInstanceId,
466 forceRestart: entry.forceRestart,
467 capabilities: capabilities((current) => ({
468 ...current,
469 turns: {
470 ...current.turns,
471 supportsActiveSteering: entry.supportsActiveSteering,
472 supportsInterrupt: entry.supportsInterrupt,
473 supportsSteeringByInterruptRestart: entry.supportsSteeringByInterruptRestart,
474 },
475 })),
476 });
477​
478 if (entry.expected.type === "policy") {
479 assert.equal(yield* decision, entry.expected.value);
480 continue;
481 }
482​
483 const error = yield* decision.pipe(Effect.flip);
484 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
485 assert.equal(error.commandId, commandId);
486 assert.equal(error.threadId, threadId);
487 assert.equal(error.providerInstanceId, providerInstanceId);
488 assert.equal(error.capability, entry.expected.capability);
489 assert.equal(error.detail, entry.expected.detail);
490 }
491 }),
492 );
493​
494 it.effect("returns typed capability errors for unsupported active steering", () =>
495 Effect.gen(function* () {
496 const policy = yield* CommandPolicy.CommandPolicyV2;
497​
498 const error = yield* policy
499 .decideSteeringExecution({
500 commandId,
501 threadId,
502 providerInstanceId: ProviderInstanceId.make("codex"),
503 capabilities: capabilities((current) => ({
504 ...current,
505 turns: {
506 ...current.turns,
507 supportsActiveSteering: false,
508 supportsInterrupt: false,
509 supportsSteeringByInterruptRestart: false,
510 },
511 })),
512 })
513 .pipe(Effect.flip);
514​
515 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
516 assert.equal(error.capability, "active_steering");
517 }),
518 );
519​
520 it.effect("guards native fork behind fork and identity capabilities", () =>
521 Effect.gen(function* () {
522 const policy = yield* CommandPolicy.CommandPolicyV2;
523​
524 const error = yield* policy
525 .ensureNativeFork({
526 commandId,
527 threadId,
528 providerInstanceId: ProviderInstanceId.make("codex"),
529 fromSpecificTurn: true,
530 capabilities: capabilities((current) => ({
531 ...current,
532 identity: {
533 ...current.identity,
534 nativeThreadIds: "weak",
535 },
536 })),
537 })
538 .pipe(Effect.flip);
539​
540 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
541 assert.equal(error.capability, "native_fork");
542 }),
543 );
544​
545 it.effect("uses a native fork when the provider supports the requested source point", () =>
546 Effect.gen(function* () {
547 const policy = yield* CommandPolicy.CommandPolicyV2;
548​
549 const result = yield* policy.decideForkExecution({
550 commandId,
551 threadId,
552 providerInstanceId: ProviderInstanceId.make("codex"),
553 capabilities: CodexProviderCapabilitiesV2,
554 sameProvider: true,
555 hasStrongNativeSource: true,
556 sourceRunStatus: "completed",
557 fromSpecificTurn: true,
558 });
559​
560 assert.equal(result, "native_fork");
561 }),
562 );
563​
564 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
565 Effect.gen(function* () {
566 const policy = yield* CommandPolicy.CommandPolicyV2;
567​
568 const result = yield* policy.decideForkExecution({
569 commandId,
570 threadId,
571 providerInstanceId: ProviderInstanceId.make("cursor"),
572 capabilities: CursorProviderCapabilitiesV2,
573 sameProvider: true,
574 hasStrongNativeSource: true,
575 sourceRunStatus: "completed",
576 fromSpecificTurn: true,
577 });
578​
579 assert.equal(result, "portable_context");
580 }),
581 );
582​
583 it.effect("falls back to portable context when Grok ACP cannot fork natively", () =>
584 Effect.gen(function* () {
585 const policy = yield* CommandPolicy.CommandPolicyV2;
586​
587 const result = yield* policy.decideForkExecution({
588 commandId,
589 threadId,
590 providerInstanceId: ProviderInstanceId.make("grok"),
591 capabilities: GrokProviderCapabilitiesV2,
592 sameProvider: true,
593 hasStrongNativeSource: true,
594 sourceRunStatus: "completed",
595 fromSpecificTurn: true,
596 });
597​
598 assert.equal(result, "portable_context");
599 }),
600 );
601​
602 it.effect("returns a typed error when neither native nor portable fork is available", () =>
603 Effect.gen(function* () {
604 const policy = yield* CommandPolicy.CommandPolicyV2;
605​
606 const error = yield* policy
607 .decideForkExecution({
608 commandId,
609 threadId,
610 providerInstanceId: ProviderInstanceId.make("cursor"),
611 capabilities: capabilities((current) => ({
612 ...current,
613 threads: {
614 ...current.threads,
615 canForkThread: false,
616 },
617 context: {
618 ...current.context,
619 canConsumeHandoffSummaries: false,
620 },
621 })),
622 sameProvider: true,
623 hasStrongNativeSource: true,
624 sourceRunStatus: "completed",
625 fromSpecificTurn: true,
626 })
627 .pipe(Effect.flip);
628​
629 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
630 assert.equal(error.capability, "context_handoff");
631 }),
632 );
633​
634 it.effect("guards rollback behind provider rollback snapshot support", () =>
635 Effect.gen(function* () {
636 const policy = yield* CommandPolicy.CommandPolicyV2;
637​
638 const error = yield* policy
639 .ensureRollback({
640 commandId,
641 threadId,
642 providerInstanceId: ProviderInstanceId.make("codex"),
643 capabilities: capabilities((current) => ({
644 ...current,
645 checkpointing: {
646 ...current.checkpointing,
647 providerRollbackReturnsSnapshot: false,
648 },
649 })),
650 })
651 .pipe(Effect.flip);
652​
653 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
654 assert.equal(error.capability, "rollback_snapshot");
655 }),
656 );
657​
658 it.effect("guards fork-delta handoff behind context handoff capabilities", () =>
659 Effect.gen(function* () {
660 const policy = yield* CommandPolicy.CommandPolicyV2;
661​
662 const error = yield* policy
663 .ensureContextHandoff({
664 commandId,
665 threadId,
666 providerInstanceId: ProviderInstanceId.make("codex"),
667 strategy: "fork_delta_context",
668 capabilities: capabilities((current) => ({
669 ...current,
670 context: {
671 ...current.context,
672 supportsDeltaHandoff: false,
673 },
674 })),
675 })
676 .pipe(Effect.flip);
677​
678 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
679 assert.equal(error.capability, "context_handoff");
680 }),
681 );
682​
683 it.effect("guards queued turns behind queued-message support", () =>
684 Effect.gen(function* () {
685 const policy = yield* CommandPolicy.CommandPolicyV2;
686​
687 const error = yield* policy
688 .ensureQueuedMessages({
689 commandId,
690 threadId,
691 providerInstanceId: ProviderInstanceId.make("codex"),
692 capabilities: capabilities((current) => ({
693 ...current,
694 turns: {
695 ...current.turns,
696 supportsQueuedMessages: false,
697 },
698 })),
699 })
700 .pipe(Effect.flip);
701​
702 assert.instanceOf(error, CommandPolicy.CommandPolicyCapabilityUnsupportedError);
703 assert.equal(error.capability, "queued_messages");
704 }),
705 );
706});