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

CommandPolicy.test.ts

402 lines12.1 KB

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 CommandPolicyCapabilityUnsupportedError,
19 CommandPolicyV2,
20 layer as commandPolicyLayer,
21 resolveMessageDispatchIntent,
22} from "./CommandPolicy.ts";
23​
24const commandId = CommandId.make("command-policy-test");
25const threadId = ThreadId.make("command-policy-thread");
26const activeRunId = RunId.make("command-policy-active-run");
27​
28const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
29​
30function capabilities(
31 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
32): OrchestrationV2ProviderCapabilities {
33 return override(baseCapabilities);
35​
36function dispatchProjection(
37 sessionCapabilities?: OrchestrationV2ProviderCapabilities,
38): OrchestrationV2ThreadProjection {
39 const providerThreadId = ProviderThreadId.make("command-policy-provider-thread");
40 const providerSessionId = ProviderSessionId.make("command-policy-provider-session");
41 return {
42 runs:
43 sessionCapabilities === undefined
44 ? []
45 : [{ id: activeRunId, status: "running", providerThreadId }],
46 providerThreads:
47 sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],
48 providerSessions:
49 sessionCapabilities === undefined
50 ? []
51 : [{ id: providerSessionId, capabilities: sessionCapabilities }],
52 } as unknown as OrchestrationV2ThreadProjection;
54​
55it("resolves automatic message delivery from authoritative provider capabilities", () => {
56 assert.deepEqual(
57 resolveMessageDispatchIntent(dispatchProjection(), { type: "start_immediately" }, "auto"),
58 { type: "start_immediately" },
59 );
60 assert.deepEqual(
61 resolveMessageDispatchIntent(
62 dispatchProjection(baseCapabilities),
63 { type: "start_immediately" },
64 "auto",
65 ),
66 { type: "steer_active", targetRunId: activeRunId },
67 );
68 assert.deepEqual(
69 resolveMessageDispatchIntent(
70 dispatchProjection(
71 capabilities((current) => ({
72 ...current,
73 turns: {
74 ...current.turns,
75 supportsActiveSteering: false,
76 supportsQueuedMessages: true,
77 supportsSteeringByInterruptRestart: true,
78 },
79 })),
80 ),
81 { type: "start_immediately" },
82 "auto",
83 ),
84 { type: "queue_after_active" },
85 );
86 assert.deepEqual(
87 resolveMessageDispatchIntent(
88 dispatchProjection(
89 capabilities((current) => ({
90 ...current,
91 turns: {
92 ...current.turns,
93 supportsActiveSteering: false,
94 supportsQueuedMessages: false,
95 supportsSteeringByInterruptRestart: true,
96 },
97 })),
98 ),
99 { type: "start_immediately" },
100 "auto",
101 ),
102 { type: "restart_active", targetRunId: activeRunId },
103 );
104});
105​
106it("targets the latest active run for explicit steer and restart intent", () => {
107 const projection = dispatchProjection(baseCapabilities);
108 assert.deepEqual(
109 resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
110 { type: "steer_active", targetRunId: activeRunId },
111 );
112 assert.deepEqual(
113 resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "restart"),
114 { type: "restart_active", targetRunId: activeRunId },
115 );
116 assert.deepEqual(
117 resolveMessageDispatchIntent(dispatchProjection(), { type: "start_immediately" }, "steer"),
118 { type: "start_immediately" },
119 );
120});
121​
122const layer = it.layer(commandPolicyLayer);
123​
124layer("CommandPolicyV2", (it) => {
125 it.effect("prefers direct active steering when the provider supports it", () =>
126 Effect.gen(function* () {
127 const policy = yield* CommandPolicyV2;
128​
129 const result = yield* policy.decideSteeringExecution({
130 commandId,
131 threadId,
132 providerInstanceId: ProviderInstanceId.make("codex"),
133 capabilities: baseCapabilities,
134 });
135​
136 assert.equal(result, "active_steering");
137 }),
138 );
139​
140 it.effect("uses interrupt-and-restart steering when direct steering is unavailable", () =>
141 Effect.gen(function* () {
142 const policy = yield* CommandPolicyV2;
143​
144 const result = yield* policy.decideSteeringExecution({
145 commandId,
146 threadId,
147 providerInstanceId: ProviderInstanceId.make("codex"),
148 capabilities: capabilities((current) => ({
149 ...current,
150 turns: {
151 ...current.turns,
152 supportsActiveSteering: false,
153 supportsInterrupt: true,
154 supportsSteeringByInterruptRestart: true,
155 },
156 })),
157 });
158​
159 assert.equal(result, "interrupt_restart");
160 }),
161 );
162​
163 it.effect("uses interrupt-and-restart steering for Grok ACP", () =>
164 Effect.gen(function* () {
165 const policy = yield* CommandPolicyV2;
166​
167 const result = yield* policy.decideSteeringExecution({
168 commandId,
169 threadId,
170 providerInstanceId: ProviderInstanceId.make("grok"),
171 capabilities: GrokProviderCapabilitiesV2,
172 });
173​
174 assert.equal(result, "interrupt_restart");
175 }),
176 );
177​
178 it.effect("honors an explicit interrupt-and-restart request", () =>
179 Effect.gen(function* () {
180 const policy = yield* CommandPolicyV2;
181​
182 const result = yield* policy.decideSteeringExecution({
183 commandId,
184 threadId,
185 providerInstanceId: ProviderInstanceId.make("codex"),
186 capabilities: CodexProviderCapabilitiesV2,
187 forceRestart: true,
188 });
189​
190 assert.equal(result, "interrupt_restart");
191 }),
192 );
193​
194 it.effect("returns typed capability errors for unsupported active steering", () =>
195 Effect.gen(function* () {
196 const policy = yield* CommandPolicyV2;
197​
198 const error = yield* policy
199 .decideSteeringExecution({
200 commandId,
201 threadId,
202 providerInstanceId: ProviderInstanceId.make("codex"),
203 capabilities: capabilities((current) => ({
204 ...current,
205 turns: {
206 ...current.turns,
207 supportsActiveSteering: false,
208 supportsInterrupt: false,
209 supportsSteeringByInterruptRestart: false,
210 },
211 })),
212 })
213 .pipe(Effect.flip);
214​
215 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
216 assert.equal(error.capability, "active_steering");
217 }),
218 );
219​
220 it.effect("guards native fork behind fork and identity capabilities", () =>
221 Effect.gen(function* () {
222 const policy = yield* CommandPolicyV2;
223​
224 const error = yield* policy
225 .ensureNativeFork({
226 commandId,
227 threadId,
228 providerInstanceId: ProviderInstanceId.make("codex"),
229 fromSpecificTurn: true,
230 capabilities: capabilities((current) => ({
231 ...current,
232 identity: {
233 ...current.identity,
234 nativeThreadIds: "weak",
235 },
236 })),
237 })
238 .pipe(Effect.flip);
239​
240 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
241 assert.equal(error.capability, "native_fork");
242 }),
243 );
244​
245 it.effect("uses a native fork when the provider supports the requested source point", () =>
246 Effect.gen(function* () {
247 const policy = yield* CommandPolicyV2;
248​
249 const result = yield* policy.decideForkExecution({
250 commandId,
251 threadId,
252 providerInstanceId: ProviderInstanceId.make("codex"),
253 capabilities: CodexProviderCapabilitiesV2,
254 sameProvider: true,
255 hasStrongNativeSource: true,
256 fromSpecificTurn: true,
257 });
258​
259 assert.equal(result, "native_fork");
260 }),
261 );
262​
263 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
264 Effect.gen(function* () {
265 const policy = yield* CommandPolicyV2;
266​
267 const result = yield* policy.decideForkExecution({
268 commandId,
269 threadId,
270 providerInstanceId: ProviderInstanceId.make("cursor"),
271 capabilities: CursorProviderCapabilitiesV2,
272 sameProvider: true,
273 hasStrongNativeSource: true,
274 fromSpecificTurn: true,
275 });
276​
277 assert.equal(result, "portable_context");
278 }),
279 );
280​
281 it.effect("falls back to portable context when Grok ACP cannot fork natively", () =>
282 Effect.gen(function* () {
283 const policy = yield* CommandPolicyV2;
284​
285 const result = yield* policy.decideForkExecution({
286 commandId,
287 threadId,
288 providerInstanceId: ProviderInstanceId.make("grok"),
289 capabilities: GrokProviderCapabilitiesV2,
290 sameProvider: true,
291 hasStrongNativeSource: true,
292 fromSpecificTurn: true,
293 });
294​
295 assert.equal(result, "portable_context");
296 }),
297 );
298​
299 it.effect("returns a typed error when neither native nor portable fork is available", () =>
300 Effect.gen(function* () {
301 const policy = yield* CommandPolicyV2;
302​
303 const error = yield* policy
304 .decideForkExecution({
305 commandId,
306 threadId,
307 providerInstanceId: ProviderInstanceId.make("cursor"),
308 capabilities: capabilities((current) => ({
309 ...current,
310 threads: {
311 ...current.threads,
312 canForkThread: false,
313 },
314 context: {
315 ...current.context,
316 canConsumeHandoffSummaries: false,
317 },
318 })),
319 sameProvider: true,
320 hasStrongNativeSource: true,
321 fromSpecificTurn: true,
322 })
323 .pipe(Effect.flip);
324​
325 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
326 assert.equal(error.capability, "context_handoff");
327 }),
328 );
329​
330 it.effect("guards rollback behind provider rollback snapshot support", () =>
331 Effect.gen(function* () {
332 const policy = yield* CommandPolicyV2;
333​
334 const error = yield* policy
335 .ensureRollback({
336 commandId,
337 threadId,
338 providerInstanceId: ProviderInstanceId.make("codex"),
339 capabilities: capabilities((current) => ({
340 ...current,
341 checkpointing: {
342 ...current.checkpointing,
343 providerRollbackReturnsSnapshot: false,
344 },
345 })),
346 })
347 .pipe(Effect.flip);
348​
349 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
350 assert.equal(error.capability, "rollback_snapshot");
351 }),
352 );
353​
354 it.effect("guards fork-delta handoff behind context handoff capabilities", () =>
355 Effect.gen(function* () {
356 const policy = yield* CommandPolicyV2;
357​
358 const error = yield* policy
359 .ensureContextHandoff({
360 commandId,
361 threadId,
362 providerInstanceId: ProviderInstanceId.make("codex"),
363 strategy: "fork_delta_context",
364 capabilities: capabilities((current) => ({
365 ...current,
366 context: {
367 ...current.context,
368 supportsDeltaHandoff: false,
369 },
370 })),
371 })
372 .pipe(Effect.flip);
373​
374 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
375 assert.equal(error.capability, "context_handoff");
376 }),
377 );
378​
379 it.effect("guards queued turns behind queued-message support", () =>
380 Effect.gen(function* () {
381 const policy = yield* CommandPolicyV2;
382​
383 const error = yield* policy
384 .ensureQueuedMessages({
385 commandId,
386 threadId,
387 providerInstanceId: ProviderInstanceId.make("codex"),
388 capabilities: capabilities((current) => ({
389 ...current,
390 turns: {
391 ...current.turns,
392 supportsQueuedMessages: false,
393 },
394 })),
395 })
396 .pipe(Effect.flip);
397​
398 assert.instanceOf(error, CommandPolicyCapabilityUnsupportedError);
399 assert.equal(error.capability, "queued_messages");
400 }),
401 );
402});