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