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