@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 381 lines 12 KB
Raw
1 import { assert, it } from "@effect/vitest";
2 import {
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";
14 import * as Effect from "effect/Effect";
15
16 import { ClaudeProviderCapabilitiesV2 } from "./Adapters/ClaudeAdapterV2.ts";
17 import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
18 import { CursorProviderCapabilitiesV2 } from "@t3tools/provider-cursor/testing";
19 import { makeGrokAcpAdapterFlavor } from "@t3tools/provider-grok/testing";
20 import * as CommandPolicy from "./CommandPolicy.ts";
21
22 const commandId = CommandId.make("command-policy-test");
23 const threadId = ThreadId.make("command-policy-thread");
24 const activeRunId = RunId.make("command-policy-active-run");
25 const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");
26 const userMessageId = MessageId.make("command-policy-user-message");
27
28 const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
29
30 function capabilities(
31 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
32 ): OrchestrationV2ProviderCapabilities {
33 return override(baseCapabilities);
34 }
35
36 function 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;
66 }
67
68 it("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
123 it.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
138 it.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
170 it("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
217 it("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
239 it("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 it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
256 const projection = dispatchProjection(baseCapabilities);
257 const maintenance = {
258 ...projection,
259 messages: [{ id: userMessageId, text, attachments: [] }],
260 } as unknown as OrchestrationV2ThreadProjection;
261 for (const deliveryIntent of ["auto", "steer"] as const) {
262 assert.deepEqual(
263 CommandPolicy.resolveMessageDispatchIntent(
264 maintenance,
265 { type: "start_immediately" },
266 deliveryIntent,
267 ),
268 { type: "queue_after_active" },
269 );
270 }
271 assert.deepEqual(
272 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
273 type: "steer_active",
274 targetRunId: activeRunId,
275 }),
276 { type: "queue_after_active" },
277 );
278 assert.deepEqual(
279 CommandPolicy.resolveMessageDispatchIntent(
280 maintenance,
281 { type: "start_immediately" },
282 "restart",
283 ),
284 { type: "restart_active", targetRunId: activeRunId },
285 );
286 });
287
288 const layer = it.layer(CommandPolicy.layer);
289
290 layer("CommandPolicyV2", (it) => {
291 it.effect("steers a live Cursor turn in place", () =>
292 Effect.gen(function* () {
293 const policy = yield* CommandPolicy.CommandPolicyV2;
294 const result = yield* policy.decideSteeringExecution({
295 commandId,
296 threadId,
297 providerInstanceId: ProviderInstanceId.make("cursor"),
298 capabilities: CursorProviderCapabilitiesV2,
299 });
300 assert.equal(result, "active_steering");
301 }),
302 );
303
304 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
305 Effect.gen(function* () {
306 const policy = yield* CommandPolicy.CommandPolicyV2;
307
308 const result = yield* policy.decideForkExecution({
309 commandId,
310 threadId,
311 providerInstanceId: ProviderInstanceId.make("cursor"),
312 capabilities: CursorProviderCapabilitiesV2,
313 sameProvider: true,
314 hasStrongNativeSource: true,
315 hasNativeTurnBoundary: false,
316 sourceRunStatus: "completed",
317 fromSpecificTurn: true,
318 sourceBusy: false,
319 });
320
321 assert.equal(result, "portable_context");
322 }),
323 );
324
325 it.effect.each([
326 {
327 provider: "codex",
328 capabilities: CodexProviderCapabilitiesV2,
329 hasNativeTurnBoundary: true,
330 busyStrategy: "native_fork",
331 },
332 {
333 provider: "codex without busy forks",
334 capabilities: {
335 ...CodexProviderCapabilitiesV2,
336 threads: { ...CodexProviderCapabilitiesV2.threads, canForkWhileSourceRunning: undefined },
337 },
338 hasNativeTurnBoundary: true,
339 busyStrategy: "portable_context",
340 },
341 {
342 provider: "codex",
343 capabilities: CodexProviderCapabilitiesV2,
344 hasNativeTurnBoundary: false,
345 busyStrategy: "portable_context",
346 },
347 {
348 provider: "claudeAgent",
349 capabilities: ClaudeProviderCapabilitiesV2,
350 hasNativeTurnBoundary: false,
351 busyStrategy: "native_fork",
352 },
353 ] satisfies ReadonlyArray<{
354 provider: string;
355 capabilities: OrchestrationV2ProviderCapabilities;
356 hasNativeTurnBoundary: boolean;
357 busyStrategy: CommandPolicy.ForkExecutionPolicyV2;
358 }>)(
359 "forks a busy $provider source with hasNativeTurnBoundary=$hasNativeTurnBoundary as $busyStrategy",
360 ({ capabilities, hasNativeTurnBoundary, busyStrategy }) =>
361 Effect.gen(function* () {
362 const policy = yield* CommandPolicy.CommandPolicyV2;
363 const decide = (sourceBusy: boolean) =>
364 policy.decideForkExecution({
365 commandId,
366 threadId,
367 providerInstanceId: ProviderInstanceId.make("custom-instance"),
368 capabilities,
369 sameProvider: true,
370 hasStrongNativeSource: true,
371 hasNativeTurnBoundary,
372 sourceRunStatus: "completed",
373 fromSpecificTurn: true,
374 sourceBusy,
375 });
376
377 assert.equal(yield* decide(false), "native_fork");
378 assert.equal(yield* decide(true), busyStrategy);
379 }),
380 );
381 });