Open History to see the last commit
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);
33}
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;
65}
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
237it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
238 const projection = dispatchProjection(baseCapabilities);
239 const maintenance = {
240 ...projection,
241 messages: [{ id: userMessageId, text, attachments: [] }],
242 } as unknown as OrchestrationV2ThreadProjection;
243 for (const deliveryIntent of ["auto", "steer"] as const) {
244 assert.deepEqual(
245 CommandPolicy.resolveMessageDispatchIntent(
246 maintenance,
247 { type: "start_immediately" },
248 deliveryIntent,
249 ),
250 { type: "queue_after_active" },
251 );
252 }
253 assert.deepEqual(
254 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
255 type: "steer_active",
256 targetRunId: activeRunId,
257 }),
258 { type: "queue_after_active" },
259 );
260 assert.deepEqual(
261 CommandPolicy.resolveMessageDispatchIntent(
262 maintenance,
263 { type: "start_immediately" },
264 "restart",
265 ),
266 { type: "restart_active", targetRunId: activeRunId },
267 );
268});
269
270const layer = it.layer(CommandPolicy.layer);
271
272layer("CommandPolicyV2", (it) => {
273 it.effect("steers a live Cursor turn in place", () =>
274 Effect.gen(function* () {
275 const policy = yield* CommandPolicy.CommandPolicyV2;
276 const result = yield* policy.decideSteeringExecution({
277 commandId,
278 threadId,
279 providerInstanceId: ProviderInstanceId.make("cursor"),
280 capabilities: CursorProviderCapabilitiesV2,
281 });
282 assert.equal(result, "active_steering");
283 }),
284 );
285
286 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
287 Effect.gen(function* () {
288 const policy = yield* CommandPolicy.CommandPolicyV2;
289
290 const result = yield* policy.decideForkExecution({
291 commandId,
292 threadId,
293 providerInstanceId: ProviderInstanceId.make("cursor"),
294 capabilities: CursorProviderCapabilitiesV2,
295 sameProvider: true,
296 hasStrongNativeSource: true,
297 sourceRunStatus: "completed",
298 fromSpecificTurn: true,
299 sourceBusy: false,
300 });
301
302 assert.equal(result, "portable_context");
303 }),
304 );
305
306 it.effect("forks a busy source through portable context", () =>
307 Effect.gen(function* () {
308 const policy = yield* CommandPolicy.CommandPolicyV2;
309 const decide = (sourceBusy: boolean) =>
310 policy.decideForkExecution({
311 commandId,
312 threadId,
313 providerInstanceId: ProviderInstanceId.make("codex"),
314 capabilities: CodexProviderCapabilitiesV2,
315 sameProvider: true,
316 hasStrongNativeSource: true,
317 sourceRunStatus: "completed",
318 fromSpecificTurn: true,
319 sourceBusy,
320 });
321
322 assert.equal(yield* decide(false), "native_fork");
323 assert.equal(yield* decide(true), "portable_context");
324 }),
325 );
326});