Open History to see the last commit
1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 type OrchestrationV2ProviderCapabilities,
5 ProviderInstanceId,
6 ThreadId,
7} from "@t3tools/contracts";
8import * as Effect from "effect/Effect";
9
10import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
11import { CursorProviderCapabilitiesV2 } from "./Adapters/CursorAdapterV2.ts";
12import { GrokProviderCapabilitiesV2 } from "./Adapters/GrokAdapterV2.ts";
13import {
14 CommandPolicyCapabilityUnsupportedError,
15 CommandPolicyV2,
16 layer as commandPolicyLayer,
17} from "./CommandPolicy.ts";
18
19const commandId = CommandId.make("command-policy-test");
20const threadId = ThreadId.make("command-policy-thread");
21
22const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
23
24function capabilities(
25 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
26): OrchestrationV2ProviderCapabilities {
27 return override(baseCapabilities);
28}
29
30const layer = it.layer(commandPolicyLayer);
31
32layer("CommandPolicyV2", (it) => {
33 it.effect("prefers direct active steering when the provider supports it", () =>
34 Effect.gen(function* () {
35 const policy = yield* CommandPolicyV2;
36
37 const result = yield* policy.decideSteeringExecution({
38 commandId,
39 threadId,
40 providerInstanceId: ProviderInstanceId.make("codex"),
41 capabilities: baseCapabilities,
42 });
43
44 assert.equal(result, "active_steering");
45 }),
46 );
47
48 it.effect("uses interrupt-and-restart steering when direct steering is unavailable", () =>
49 Effect.gen(function* () {
50 const policy = yield* CommandPolicyV2;
51
52 const result = yield* policy.decideSteeringExecution({
53 commandId,
54 threadId,
55 providerInstanceId: ProviderInstanceId.make("codex"),
56 capabilities: capabilities((current) => ({
57 ...current,
58 turns: {
59 ...current.turns,
60 supportsActiveSteering: false,
61 supportsInterrupt: true,
62 supportsSteeringByInterruptRestart: true,
63 },
64 })),
65 });
66
67 assert.equal(result, "interrupt_restart");
68 }),
69 );
70
71 it.effect("uses interrupt-and-restart steering for Grok ACP", () =>
72 Effect.gen(function* () {
73 const policy = yield* CommandPolicyV2;
74
75 const result = yield* policy.decideSteeringExecution({
76 commandId,
77 threadId,
78 providerInstanceId: ProviderInstanceId.make("grok"),
79 capabilities: GrokProviderCapabilitiesV2,
80 });
81
82 assert.equal(result, "interrupt_restart");
83 }),
84 );
85
86 it.effect("honors an explicit interrupt-and-restart request", () =>
87 Effect.gen(function* () {
88 const policy = yield* CommandPolicyV2;
89
90 const result = yield* policy.decideSteeringExecution({
91 commandId,
92 threadId,
93 providerInstanceId: ProviderInstanceId.make("codex"),
94 capabilities: CodexProviderCapabilitiesV2,
95 forceRestart: true,
96 });
97
98 assert.equal(result, "interrupt_restart");
99 }),
100 );
101
102 it.effect("returns typed capability errors for unsupported active steering", () =>
103 Effect.gen(function* () {
104 const policy = yield* CommandPolicyV2;
105
106 const error = yield* policy
107 .decideSteeringExecution({
108 commandId,
109 threadId,
110 providerInstanceId: ProviderInstanceId.make("codex"),
111 capabilities: capabilities((current) => ({
112 ...current,
113 turns: {
114 ...current.turns,
115 supportsActiveSteering: false,
116 supportsInterrupt: false,
117 supportsSteeringByInterruptRestart: false,
118 },
119 })),
120 })
121 .pipe(Effect.flip);
122
123 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
124 assert.equal(error.capability, "active_steering");
125 }),
126 );
127
128 it.effect("guards native fork behind fork and identity capabilities", () =>
129 Effect.gen(function* () {
130 const policy = yield* CommandPolicyV2;
131
132 const error = yield* policy
133 .ensureNativeFork({
134 commandId,
135 threadId,
136 providerInstanceId: ProviderInstanceId.make("codex"),
137 fromSpecificTurn: true,
138 capabilities: capabilities((current) => ({
139 ...current,
140 identity: {
141 ...current.identity,
142 nativeThreadIds: "weak",
143 },
144 })),
145 })
146 .pipe(Effect.flip);
147
148 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
149 assert.equal(error.capability, "native_fork");
150 }),
151 );
152
153 it.effect("uses a native fork when the provider supports the requested source point", () =>
154 Effect.gen(function* () {
155 const policy = yield* CommandPolicyV2;
156
157 const result = yield* policy.decideForkExecution({
158 commandId,
159 threadId,
160 providerInstanceId: ProviderInstanceId.make("codex"),
161 capabilities: CodexProviderCapabilitiesV2,
162 sameProvider: true,
163 hasStrongNativeSource: true,
164 fromSpecificTurn: true,
165 });
166
167 assert.equal(result, "native_fork");
168 }),
169 );
170
171 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
172 Effect.gen(function* () {
173 const policy = yield* CommandPolicyV2;
174
175 const result = yield* policy.decideForkExecution({
176 commandId,
177 threadId,
178 providerInstanceId: ProviderInstanceId.make("cursor"),
179 capabilities: CursorProviderCapabilitiesV2,
180 sameProvider: true,
181 hasStrongNativeSource: true,
182 fromSpecificTurn: true,
183 });
184
185 assert.equal(result, "portable_context");
186 }),
187 );
188
189 it.effect("falls back to portable context when Grok ACP cannot fork natively", () =>
190 Effect.gen(function* () {
191 const policy = yield* CommandPolicyV2;
192
193 const result = yield* policy.decideForkExecution({
194 commandId,
195 threadId,
196 providerInstanceId: ProviderInstanceId.make("grok"),
197 capabilities: GrokProviderCapabilitiesV2,
198 sameProvider: true,
199 hasStrongNativeSource: true,
200 fromSpecificTurn: true,
201 });
202
203 assert.equal(result, "portable_context");
204 }),
205 );
206
207 it.effect("returns a typed error when neither native nor portable fork is available", () =>
208 Effect.gen(function* () {
209 const policy = yield* CommandPolicyV2;
210
211 const error = yield* policy
212 .decideForkExecution({
213 commandId,
214 threadId,
215 providerInstanceId: ProviderInstanceId.make("cursor"),
216 capabilities: capabilities((current) => ({
217 ...current,
218 threads: {
219 ...current.threads,
220 canForkThread: false,
221 },
222 context: {
223 ...current.context,
224 canConsumeHandoffSummaries: false,
225 },
226 })),
227 sameProvider: true,
228 hasStrongNativeSource: true,
229 fromSpecificTurn: true,
230 })
231 .pipe(Effect.flip);
232
233 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
234 assert.equal(error.capability, "context_handoff");
235 }),
236 );
237
238 it.effect("guards rollback behind provider rollback snapshot support", () =>
239 Effect.gen(function* () {
240 const policy = yield* CommandPolicyV2;
241
242 const error = yield* policy
243 .ensureRollback({
244 commandId,
245 threadId,
246 providerInstanceId: ProviderInstanceId.make("codex"),
247 capabilities: capabilities((current) => ({
248 ...current,
249 checkpointing: {
250 ...current.checkpointing,
251 providerRollbackReturnsSnapshot: false,
252 },
253 })),
254 })
255 .pipe(Effect.flip);
256
257 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
258 assert.equal(error.capability, "rollback_snapshot");
259 }),
260 );
261
262 it.effect("guards fork-delta handoff behind context handoff capabilities", () =>
263 Effect.gen(function* () {
264 const policy = yield* CommandPolicyV2;
265
266 const error = yield* policy
267 .ensureContextHandoff({
268 commandId,
269 threadId,
270 providerInstanceId: ProviderInstanceId.make("codex"),
271 strategy: "fork_delta_context",
272 capabilities: capabilities((current) => ({
273 ...current,
274 context: {
275 ...current.context,
276 supportsDeltaHandoff: false,
277 },
278 })),
279 })
280 .pipe(Effect.flip);
281
282 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
283 assert.equal(error.capability, "context_handoff");
284 }),
285 );
286
287 it.effect("guards queued turns behind queued-message support", () =>
288 Effect.gen(function* () {
289 const policy = yield* CommandPolicyV2;
290
291 const error = yield* policy
292 .ensureQueuedMessages({
293 commandId,
294 threadId,
295 providerInstanceId: ProviderInstanceId.make("codex"),
296 capabilities: capabilities((current) => ({
297 ...current,
298 turns: {
299 ...current.turns,
300 supportsQueuedMessages: false,
301 },
302 })),
303 })
304 .pipe(Effect.flip);
305
306 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
307 assert.equal(error.capability, "queued_messages");
308 }),
309 );
310});