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

CommandPolicy.test.ts

343 lines10.8 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 { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
17import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing";
18import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
19import * as CommandPolicy from "./CommandPolicy.ts";
20​
21const commandId = CommandId.make("command-policy-test");
22const threadId = ThreadId.make("command-policy-thread");
23const activeRunId = RunId.make("command-policy-active-run");
24const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");
25const userMessageId = MessageId.make("command-policy-user-message");
26​
27const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
28​
29function capabilities(
30 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
31): OrchestrationV2ProviderCapabilities {
32 return override(baseCapabilities);
34​
35function dispatchProjection(
36 sessionCapabilities?: OrchestrationV2ProviderCapabilities,
37): OrchestrationV2ThreadProjection {
38 const providerThreadId = ProviderThreadId.make("command-policy-provider-thread");
39 const providerSessionId = ProviderSessionId.make("command-policy-provider-session");
40 return {
41 runs:
42 sessionCapabilities === undefined
43 ? []
44 : [
45 {
46 id: activeRunId,
47 status: "running",
48 providerThreadId,
49 activeAttemptId,
50 userMessageId,
51 },
52 ],
53 messages: [{ id: userMessageId, text: "first", attachments: [] }],
54 providerThreads:
55 sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],
56 providerSessions:
57 sessionCapabilities === undefined
58 ? []
59 : [{ id: providerSessionId, capabilities: sessionCapabilities }],
60 providerTurns:
61 sessionCapabilities === undefined
62 ? []
63 : [{ runAttemptId: activeAttemptId, status: "running" }],
64 } as unknown as OrchestrationV2ThreadProjection;
66​
67it("resolves automatic message delivery from authoritative provider capabilities", () => {
68 assert.deepEqual(
69 CommandPolicy.resolveMessageDispatchIntent(
70 dispatchProjection(),
71 { type: "start_immediately" },
72 "auto",
73 ),
74 { type: "start_immediately" },
75 );
76 assert.deepEqual(
77 CommandPolicy.resolveMessageDispatchIntent(
78 dispatchProjection(baseCapabilities),
79 { type: "start_immediately" },
80 "auto",
81 ),
82 { type: "steer_active", targetRunId: activeRunId },
83 );
84 assert.deepEqual(
85 CommandPolicy.resolveMessageDispatchIntent(
86 dispatchProjection(
87 capabilities((current) => ({
88 ...current,
89 turns: {
90 ...current.turns,
91 supportsActiveSteering: false,
92 supportsQueuedMessages: true,
93 supportsSteeringByInterruptRestart: true,
94 },
95 })),
96 ),
97 { type: "start_immediately" },
98 "auto",
99 ),
100 { type: "queue_after_active" },
101 );
102 assert.deepEqual(
103 CommandPolicy.resolveMessageDispatchIntent(
104 dispatchProjection(
105 capabilities((current) => ({
106 ...current,
107 turns: {
108 ...current.turns,
109 supportsActiveSteering: false,
110 supportsQueuedMessages: false,
111 supportsSteeringByInterruptRestart: true,
112 },
113 })),
114 ),
115 { type: "start_immediately" },
116 "auto",
117 ),
118 { type: "restart_active", targetRunId: activeRunId },
119 );
120});
121​
122it.each(["preparing", "starting"] as const)(
123 "queues an automatic message while the handoff run is %s",
124 (status) => {
125 const projection = dispatchProjection(baseCapabilities);
126 assert.deepEqual(
127 CommandPolicy.resolveMessageDispatchIntent(
128 { ...projection, runs: projection.runs.map((run) => ({ ...run, status })) },
129 { type: "start_immediately" },
130 "auto",
131 ),
132 { type: "queue_after_active" },
133 );
134 },
135);
136​
137it.each([
138 ["has not started", []],
139 ["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],
140 ["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],
141 [
142 "belongs to a previous attempt",
143 [{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],
144 ],
145])("queues only automatic delivery when the active run's provider turn %s", (_, providerTurns) => {
146 const projection = {
147 ...dispatchProjection(baseCapabilities),
148 providerTurns,
149 } as unknown as OrchestrationV2ThreadProjection;
150 assert.deepEqual(
151 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
152 { type: "queue_after_active" },
153 );
154 assert.deepEqual(
155 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
156 { type: "steer_active", targetRunId: activeRunId },
157 );
158 assert.deepEqual(
159 CommandPolicy.resolveMessageDispatchIntent(
160 projection,
161 { type: "start_immediately" },
162 "restart",
163 ),
164 { type: "restart_active", targetRunId: activeRunId },
165 );
166});
167​
168// regression: #26695 an interrupt-restart steer cancels Copilot's background agents
169it("queues a steer for a provider that cannot steer either way", () => {
170 const projection = dispatchProjection(
171 capabilities((current) => ({
172 ...current,
173 turns: {
174 ...current.turns,
175 supportsActiveSteering: false,
176 supportsSteeringByInterruptRestart: false,
177 supportsQueuedMessages: true,
178 },
179 })),
180 );
181 for (const deliveryIntent of ["auto", "steer"] as const) {
182 assert.deepEqual(
183 CommandPolicy.resolveMessageDispatchIntent(
184 projection,
185 { type: "start_immediately" },
186 deliveryIntent,
187 ),
188 { type: "queue_after_active" },
189 );
190 }
191 assert.deepEqual(
192 CommandPolicy.resolveMessageDispatchIntent(projection, {
193 type: "steer_active",
194 targetRunId: activeRunId,
195 }),
196 { type: "queue_after_active" },
197 );
198 assert.deepEqual(
199 CommandPolicy.resolveMessageDispatchIntent(
200 projection,
201 { type: "start_immediately" },
202 "restart",
203 ),
204 { type: "restart_active", targetRunId: activeRunId },
205 );
206 assert.deepEqual(
207 CommandPolicy.resolveMessageDispatchIntent(projection, {
208 type: "steer_active",
209 targetRunId: RunId.make("command-policy-missing-run"),
210 }),
211 { type: "steer_active", targetRunId: RunId.make("command-policy-missing-run") },
212 );
213});
214​
215// regression: #27014 a Grok follow-up cancels the foreground tool the user wanted to finish
216it("does not steer a Grok follow-up by interrupting its running turn", () => {
217 const grok = makeGrokAcpAdapterFlavor(
218 { makeRuntime: () => Effect.never } as unknown as Parameters<
219 typeof makeGrokAcpAdapterFlavor
220 >[0],
221 "/home/ada",
222 ).capabilities;
223 const projection = dispatchProjection(grok);
224 for (const deliveryIntent of ["auto", "steer"] as const) {
225 const mode = CommandPolicy.resolveMessageDispatchIntent(
226 projection,
227 { type: "start_immediately" },
228 deliveryIntent,
229 );
230 assert.isFalse(
231 mode.type === "steer_active" && !grok.turns.supportsActiveSteering,
232 `${deliveryIntent} follow-up resolved to ${mode.type} by interrupt-restart`,
233 );
234 }
235});
236​
237// regression: #37326 a steered Grok follow-up waits for the turn to end instead of interjecting
238it("steers a Grok follow-up into its running turn without restarting it", () => {
239 const grok = makeGrokAcpAdapterFlavor(
240 { makeRuntime: () => Effect.never } as unknown as Parameters<
241 typeof makeGrokAcpAdapterFlavor
242 >[0],
243 "/home/ada",
244 ).capabilities;
245 const mode = CommandPolicy.resolveMessageDispatchIntent(
246 dispatchProjection(grok),
247 { type: "start_immediately" },
248 "steer",
249 );
250 assert.strictEqual(mode.type, "steer_active");
251 assert.isTrue(grok.turns.supportsActiveSteering);
252});
253​
254it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
255 const projection = dispatchProjection(baseCapabilities);
256 const maintenance = {
257 ...projection,
258 messages: [{ id: userMessageId, text, attachments: [] }],
259 } as unknown as OrchestrationV2ThreadProjection;
260 for (const deliveryIntent of ["auto", "steer"] as const) {
261 assert.deepEqual(
262 CommandPolicy.resolveMessageDispatchIntent(
263 maintenance,
264 { type: "start_immediately" },
265 deliveryIntent,
266 ),
267 { type: "queue_after_active" },
268 );
269 }
270 assert.deepEqual(
271 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
272 type: "steer_active",
273 targetRunId: activeRunId,
274 }),
275 { type: "queue_after_active" },
276 );
277 assert.deepEqual(
278 CommandPolicy.resolveMessageDispatchIntent(
279 maintenance,
280 { type: "start_immediately" },
281 "restart",
282 ),
283 { type: "restart_active", targetRunId: activeRunId },
284 );
285});
286​
287const layer = it.layer(CommandPolicy.layer);
288​
289layer("CommandPolicyV2", (it) => {
290 it.effect("steers a live Cursor turn in place", () =>
291 Effect.gen(function* () {
292 const policy = yield* CommandPolicy.CommandPolicyV2;
293 const result = yield* policy.decideSteeringExecution({
294 commandId,
295 threadId,
296 providerInstanceId: ProviderInstanceId.make("cursor"),
297 capabilities: CursorProviderCapabilitiesV2,
298 });
299 assert.equal(result, "active_steering");
300 }),
301 );
302​
303 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
304 Effect.gen(function* () {
305 const policy = yield* CommandPolicy.CommandPolicyV2;
306​
307 const result = yield* policy.decideForkExecution({
308 commandId,
309 threadId,
310 providerInstanceId: ProviderInstanceId.make("cursor"),
311 capabilities: CursorProviderCapabilitiesV2,
312 sameProvider: true,
313 hasStrongNativeSource: true,
314 sourceRunStatus: "completed",
315 fromSpecificTurn: true,
316 sourceBusy: false,
317 });
318​
319 assert.equal(result, "portable_context");
320 }),
321 );
322​
323 it.effect("forks a busy source through portable context", () =>
324 Effect.gen(function* () {
325 const policy = yield* CommandPolicy.CommandPolicyV2;
326 const decide = (sourceBusy: boolean) =>
327 policy.decideForkExecution({
328 commandId,
329 threadId,
330 providerInstanceId: ProviderInstanceId.make("codex"),
331 capabilities: CodexProviderCapabilitiesV2,
332 sameProvider: true,
333 hasStrongNativeSource: true,
334 sourceRunStatus: "completed",
335 fromSpecificTurn: true,
336 sourceBusy,
337 });
338​
339 assert.equal(yield* decide(false), "native_fork");
340 assert.equal(yield* decide(true), "portable_context");
341 }),
342 );
343});