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

CommandPolicy.test.ts

365 lines11.5 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 * 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: #32606 a slash command steered into a running turn replaced the task
216it.each([
217 ["/compact", { type: "queue_after_active" }],
218 ["/context", { type: "queue_after_active" }],
219 [" /context all\n", { type: "queue_after_active" }],
220 ["/plugin:review-pr 12", { type: "queue_after_active" }],
221 ["/home/me/x.ts fails", { type: "steer_active", targetRunId: activeRunId }],
222 ["/ context", { type: "steer_active", targetRunId: activeRunId }],
223 ["$skill /context", { type: "steer_active", targetRunId: activeRunId }],
224] as const)("steers %j only when it is not a slash command", (text, expected) => {
225 const projection = dispatchProjection(baseCapabilities);
226 for (const deliveryIntent of ["auto", "steer"] as const) {
227 assert.deepEqual(
228 CommandPolicy.resolveMessageDispatch(projection, {
229 dispatchMode: { type: "start_immediately" },
230 deliveryIntent,
231 text,
232 attachments: [],
233 }),
234 expected,
235 );
236 }
237});
238​
239it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
240 const projection = dispatchProjection(baseCapabilities);
241 const maintenance = {
242 ...projection,
243 messages: [{ id: userMessageId, text, attachments: [] }],
244 } as unknown as OrchestrationV2ThreadProjection;
245 for (const deliveryIntent of ["auto", "steer"] as const) {
246 assert.deepEqual(
247 CommandPolicy.resolveMessageDispatchIntent(
248 maintenance,
249 { type: "start_immediately" },
250 deliveryIntent,
251 ),
252 { type: "queue_after_active" },
253 );
254 }
255 assert.deepEqual(
256 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
257 type: "steer_active",
258 targetRunId: activeRunId,
259 }),
260 { type: "queue_after_active" },
261 );
262 assert.deepEqual(
263 CommandPolicy.resolveMessageDispatchIntent(
264 maintenance,
265 { type: "start_immediately" },
266 "restart",
267 ),
268 { type: "restart_active", targetRunId: activeRunId },
269 );
270});
271​
272const layer = it.layer(CommandPolicy.layer);
273​
274layer("CommandPolicyV2", (it) => {
275 it.effect("steers a live Cursor turn in place", () =>
276 Effect.gen(function* () {
277 const policy = yield* CommandPolicy.CommandPolicyV2;
278 const result = yield* policy.decideSteeringExecution({
279 commandId,
280 threadId,
281 providerInstanceId: ProviderInstanceId.make("cursor"),
282 capabilities: CursorProviderCapabilitiesV2,
283 });
284 assert.equal(result, "active_steering");
285 }),
286 );
287​
288 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
289 Effect.gen(function* () {
290 const policy = yield* CommandPolicy.CommandPolicyV2;
291​
292 const result = yield* policy.decideForkExecution({
293 commandId,
294 threadId,
295 providerInstanceId: ProviderInstanceId.make("cursor"),
296 capabilities: CursorProviderCapabilitiesV2,
297 sameProvider: true,
298 hasStrongNativeSource: true,
299 hasNativeTurnBoundary: false,
300 sourceRunStatus: "completed",
301 fromSpecificTurn: true,
302 sourceBusy: false,
303 });
304​
305 assert.equal(result, "portable_context");
306 }),
307 );
308​
309 it.effect.each([
310 {
311 provider: "codex",
312 capabilities: CodexProviderCapabilitiesV2,
313 hasNativeTurnBoundary: true,
314 busyStrategy: "native_fork",
315 },
316 {
317 provider: "codex without busy forks",
318 capabilities: {
319 ...CodexProviderCapabilitiesV2,
320 threads: { ...CodexProviderCapabilitiesV2.threads, canForkWhileSourceRunning: undefined },
321 },
322 hasNativeTurnBoundary: true,
323 busyStrategy: "portable_context",
324 },
325 {
326 provider: "codex",
327 capabilities: CodexProviderCapabilitiesV2,
328 hasNativeTurnBoundary: false,
329 busyStrategy: "portable_context",
330 },
331 {
332 provider: "claudeAgent",
333 capabilities: ClaudeProviderCapabilitiesV2,
334 hasNativeTurnBoundary: false,
335 busyStrategy: "native_fork",
336 },
337 ] satisfies ReadonlyArray<{
338 provider: string;
339 capabilities: OrchestrationV2ProviderCapabilities;
340 hasNativeTurnBoundary: boolean;
341 busyStrategy: CommandPolicy.ForkExecutionPolicyV2;
342 }>)(
343 "forks a busy $provider source with hasNativeTurnBoundary=$hasNativeTurnBoundary as $busyStrategy",
344 ({ capabilities, hasNativeTurnBoundary, busyStrategy }) =>
345 Effect.gen(function* () {
346 const policy = yield* CommandPolicy.CommandPolicyV2;
347 const decide = (sourceBusy: boolean) =>
348 policy.decideForkExecution({
349 commandId,
350 threadId,
351 providerInstanceId: ProviderInstanceId.make("custom-instance"),
352 capabilities,
353 sameProvider: true,
354 hasStrongNativeSource: true,
355 hasNativeTurnBoundary,
356 sourceRunStatus: "completed",
357 fromSpecificTurn: true,
358 sourceBusy,
359 });
360​
361 assert.equal(yield* decide(false), "native_fork");
362 assert.equal(yield* decide(true), busyStrategy);
363 }),
364 );
365});