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