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

CommandPolicy.test.ts

405 lines13.0 KB

1import { assert, it } from "@effect/vitest";
2import {
3 CommandId,
4 MessageId,
5 type OrchestrationV2ProviderCapabilities,
6 type OrchestrationV2ThreadProjection,
7 ProviderInstanceId,
8 ProviderSessionId,
9 ProviderThreadId,
10 RunAttemptId,
11 RunId,
12 ThreadId,
13} from "@t3tools/contracts";
14import * as Effect from "effect/Effect";
15​
16import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
17import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
18import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing";
19import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
20import * as CommandPolicy from "./CommandPolicy.ts";
21​
22const commandId = CommandId.make("command-policy-test");
23const threadId = ThreadId.make("command-policy-thread");
24const activeRunId = RunId.make("command-policy-active-run");
25const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");
26const userMessageId = MessageId.make("command-policy-user-message");
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 : [
46 {
47 id: activeRunId,
48 status: "running",
49 providerThreadId,
50 activeAttemptId,
51 userMessageId,
52 },
53 ],
54 messages: [{ id: userMessageId, text: "first", attachments: [] }],
55 providerThreads:
56 sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],
57 providerSessions:
58 sessionCapabilities === undefined
59 ? []
60 : [{ id: providerSessionId, capabilities: sessionCapabilities }],
61 providerTurns:
62 sessionCapabilities === undefined
63 ? []
64 : [{ runAttemptId: activeAttemptId, status: "running" }],
65 } as unknown as OrchestrationV2ThreadProjection;
67​
68it("resolves automatic message delivery from authoritative provider capabilities", () => {
69 assert.deepEqual(
70 CommandPolicy.resolveMessageDispatchIntent(
71 dispatchProjection(),
72 { type: "start_immediately" },
73 "auto",
74 ),
75 { type: "start_immediately" },
76 );
77 assert.deepEqual(
78 CommandPolicy.resolveMessageDispatchIntent(
79 dispatchProjection(baseCapabilities),
80 { type: "start_immediately" },
81 "auto",
82 ),
83 { type: "steer_active", targetRunId: activeRunId },
84 );
85 assert.deepEqual(
86 CommandPolicy.resolveMessageDispatchIntent(
87 dispatchProjection(
88 capabilities((current) => ({
89 ...current,
90 turns: {
91 ...current.turns,
92 supportsActiveSteering: false,
93 supportsQueuedMessages: true,
94 supportsSteeringByInterruptRestart: true,
95 },
96 })),
97 ),
98 { type: "start_immediately" },
99 "auto",
100 ),
101 { type: "queue_after_active" },
102 );
103 assert.deepEqual(
104 CommandPolicy.resolveMessageDispatchIntent(
105 dispatchProjection(
106 capabilities((current) => ({
107 ...current,
108 turns: {
109 ...current.turns,
110 supportsActiveSteering: false,
111 supportsQueuedMessages: false,
112 supportsSteeringByInterruptRestart: true,
113 },
114 })),
115 ),
116 { type: "start_immediately" },
117 "auto",
118 ),
119 { type: "restart_active", targetRunId: activeRunId },
120 );
121});
122​
123it.each(["preparing", "starting"] as const)(
124 "queues an automatic message while the handoff run is %s",
125 (status) => {
126 const projection = dispatchProjection(baseCapabilities);
127 assert.deepEqual(
128 CommandPolicy.resolveMessageDispatchIntent(
129 { ...projection, runs: projection.runs.map((run) => ({ ...run, status })) },
130 { type: "start_immediately" },
131 "auto",
132 ),
133 { type: "queue_after_active" },
134 );
135 },
136);
137​
138it.each([
139 ["has not started", []],
140 ["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],
141 ["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],
142 [
143 "belongs to a previous attempt",
144 [{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],
145 ],
146])("queues only automatic delivery when the active run's provider turn %s", (_, providerTurns) => {
147 const projection = {
148 ...dispatchProjection(baseCapabilities),
149 providerTurns,
150 } as unknown as OrchestrationV2ThreadProjection;
151 assert.deepEqual(
152 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
153 { type: "queue_after_active" },
154 );
155 assert.deepEqual(
156 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
157 { type: "steer_active", targetRunId: activeRunId },
158 );
159 assert.deepEqual(
160 CommandPolicy.resolveMessageDispatchIntent(
161 projection,
162 { type: "start_immediately" },
163 "restart",
164 ),
165 { type: "restart_active", targetRunId: activeRunId },
166 );
167});
168​
169// regression: #26695 an interrupt-restart steer cancels Copilot's background agents
170it("queues a steer for a provider that cannot steer either way", () => {
171 const projection = dispatchProjection(
172 capabilities((current) => ({
173 ...current,
174 turns: {
175 ...current.turns,
176 supportsActiveSteering: false,
177 supportsSteeringByInterruptRestart: false,
178 supportsQueuedMessages: true,
179 },
180 })),
181 );
182 for (const deliveryIntent of ["auto", "steer"] as const) {
183 assert.deepEqual(
184 CommandPolicy.resolveMessageDispatchIntent(
185 projection,
186 { type: "start_immediately" },
187 deliveryIntent,
188 ),
189 { type: "queue_after_active" },
190 );
191 }
192 assert.deepEqual(
193 CommandPolicy.resolveMessageDispatchIntent(projection, {
194 type: "steer_active",
195 targetRunId: activeRunId,
196 }),
197 { type: "queue_after_active" },
198 );
199 assert.deepEqual(
200 CommandPolicy.resolveMessageDispatchIntent(
201 projection,
202 { type: "start_immediately" },
203 "restart",
204 ),
205 { type: "restart_active", targetRunId: activeRunId },
206 );
207 assert.deepEqual(
208 CommandPolicy.resolveMessageDispatchIntent(projection, {
209 type: "steer_active",
210 targetRunId: RunId.make("command-policy-missing-run"),
211 }),
212 { type: "steer_active", targetRunId: RunId.make("command-policy-missing-run") },
213 );
214});
215​
216// regression: #27014 a Grok follow-up cancels the foreground tool the user wanted to finish
217it("does not steer a Grok follow-up by interrupting its running turn", () => {
218 const grok = makeGrokAcpAdapterFlavor(
219 { makeRuntime: () => Effect.never } as unknown as Parameters<
220 typeof makeGrokAcpAdapterFlavor
221 >[0],
222 "/home/ada",
223 ).capabilities;
224 const projection = dispatchProjection(grok);
225 for (const deliveryIntent of ["auto", "steer"] as const) {
226 const mode = CommandPolicy.resolveMessageDispatchIntent(
227 projection,
228 { type: "start_immediately" },
229 deliveryIntent,
230 );
231 assert.isFalse(
232 mode.type === "steer_active" && !grok.turns.supportsActiveSteering,
233 `${deliveryIntent} follow-up resolved to ${mode.type} by interrupt-restart`,
234 );
235 }
236});
237​
238// regression: #37326 a steered Grok follow-up waits for the turn to end instead of interjecting
239it("steers a Grok follow-up into its running turn without restarting it", () => {
240 const grok = makeGrokAcpAdapterFlavor(
241 { makeRuntime: () => Effect.never } as unknown as Parameters<
242 typeof makeGrokAcpAdapterFlavor
243 >[0],
244 "/home/ada",
245 ).capabilities;
246 const mode = CommandPolicy.resolveMessageDispatchIntent(
247 dispatchProjection(grok),
248 { type: "start_immediately" },
249 "steer",
250 );
251 assert.strictEqual(mode.type, "steer_active");
252 assert.isTrue(grok.turns.supportsActiveSteering);
253});
254​
255// regression: #32606 a slash command steered into a running turn replaced the task
256it.each([
257 ["/compact", { type: "queue_after_active" }],
258 ["/context", { type: "queue_after_active" }],
259 [" /context all\n", { type: "queue_after_active" }],
260 ["/plugin:review-pr 12", { type: "queue_after_active" }],
261 ["/home/me/x.ts fails", { type: "steer_active", targetRunId: activeRunId }],
262 ["/ context", { type: "steer_active", targetRunId: activeRunId }],
263 ["$skill /context", { type: "steer_active", targetRunId: activeRunId }],
264] as const)("steers %j only when it is not a slash command", (text, expected) => {
265 const projection = dispatchProjection(baseCapabilities);
266 for (const deliveryIntent of ["auto", "steer"] as const) {
267 assert.deepEqual(
268 CommandPolicy.resolveMessageDispatch(projection, {
269 dispatchMode: { type: "start_immediately" },
270 deliveryIntent,
271 text,
272 attachments: [],
273 }),
274 expected,
275 );
276 }
277});
278​
279it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
280 const projection = dispatchProjection(baseCapabilities);
281 const maintenance = {
282 ...projection,
283 messages: [{ id: userMessageId, text, attachments: [] }],
284 } as unknown as OrchestrationV2ThreadProjection;
285 for (const deliveryIntent of ["auto", "steer"] as const) {
286 assert.deepEqual(
287 CommandPolicy.resolveMessageDispatchIntent(
288 maintenance,
289 { type: "start_immediately" },
290 deliveryIntent,
291 ),
292 { type: "queue_after_active" },
293 );
294 }
295 assert.deepEqual(
296 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
297 type: "steer_active",
298 targetRunId: activeRunId,
299 }),
300 { type: "queue_after_active" },
301 );
302 assert.deepEqual(
303 CommandPolicy.resolveMessageDispatchIntent(
304 maintenance,
305 { type: "start_immediately" },
306 "restart",
307 ),
308 { type: "restart_active", targetRunId: activeRunId },
309 );
310});
311​
312const layer = it.layer(CommandPolicy.layer);
313​
314layer("CommandPolicyV2", (it) => {
315 it.effect("steers a live Cursor turn in place", () =>
316 Effect.gen(function* () {
317 const policy = yield* CommandPolicy.CommandPolicyV2;
318 const result = yield* policy.decideSteeringExecution({
319 commandId,
320 threadId,
321 providerInstanceId: ProviderInstanceId.make("cursor"),
322 capabilities: CursorProviderCapabilitiesV2,
323 });
324 assert.equal(result, "active_steering");
325 }),
326 );
327​
328 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
329 Effect.gen(function* () {
330 const policy = yield* CommandPolicy.CommandPolicyV2;
331​
332 const result = yield* policy.decideForkExecution({
333 commandId,
334 threadId,
335 providerInstanceId: ProviderInstanceId.make("cursor"),
336 capabilities: CursorProviderCapabilitiesV2,
337 sameProvider: true,
338 hasStrongNativeSource: true,
339 hasNativeTurnBoundary: false,
340 sourceRunStatus: "completed",
341 fromSpecificTurn: true,
342 sourceBusy: false,
343 });
344​
345 assert.equal(result, "portable_context");
346 }),
347 );
348​
349 it.effect.each([
350 {
351 provider: "codex",
352 capabilities: CodexProviderCapabilitiesV2,
353 hasNativeTurnBoundary: true,
354 busyStrategy: "native_fork",
355 },
356 {
357 provider: "codex without busy forks",
358 capabilities: {
359 ...CodexProviderCapabilitiesV2,
360 threads: { ...CodexProviderCapabilitiesV2.threads, canForkWhileSourceRunning: undefined },
361 },
362 hasNativeTurnBoundary: true,
363 busyStrategy: "portable_context",
364 },
365 {
366 provider: "codex",
367 capabilities: CodexProviderCapabilitiesV2,
368 hasNativeTurnBoundary: false,
369 busyStrategy: "portable_context",
370 },
371 {
372 provider: "claudeAgent",
373 capabilities: ClaudeProviderCapabilitiesV2,
374 hasNativeTurnBoundary: false,
375 busyStrategy: "native_fork",
376 },
377 ] satisfies ReadonlyArray<{
378 provider: string;
379 capabilities: OrchestrationV2ProviderCapabilities;
380 hasNativeTurnBoundary: boolean;
381 busyStrategy: CommandPolicy.ForkExecutionPolicyV2;
382 }>)(
383 "forks a busy $provider source with hasNativeTurnBoundary=$hasNativeTurnBoundary as $busyStrategy",
384 ({ capabilities, hasNativeTurnBoundary, busyStrategy }) =>
385 Effect.gen(function* () {
386 const policy = yield* CommandPolicy.CommandPolicyV2;
387 const decide = (sourceBusy: boolean) =>
388 policy.decideForkExecution({
389 commandId,
390 threadId,
391 providerInstanceId: ProviderInstanceId.make("custom-instance"),
392 capabilities,
393 sameProvider: true,
394 hasStrongNativeSource: true,
395 hasNativeTurnBoundary,
396 sourceRunStatus: "completed",
397 fromSpecificTurn: true,
398 sourceBusy,
399 });
400​
401 assert.equal(yield* decide(false), "native_fork");
402 assert.equal(yield* decide(true), busyStrategy);
403 }),
404 );
405});