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

CommandPolicy.test.ts

221 lines6.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 * as CommandPolicy from "./CommandPolicy.ts";
19​
20const commandId = CommandId.make("command-policy-test");
21const threadId = ThreadId.make("command-policy-thread");
22const activeRunId = RunId.make("command-policy-active-run");
23const activeAttemptId = RunAttemptId.make("command-policy-active-attempt");
24const userMessageId = MessageId.make("command-policy-user-message");
25​
26const baseCapabilities: OrchestrationV2ProviderCapabilities = CodexProviderCapabilitiesV2;
27​
28function capabilities(
29 override: (current: OrchestrationV2ProviderCapabilities) => OrchestrationV2ProviderCapabilities,
30): OrchestrationV2ProviderCapabilities {
31 return override(baseCapabilities);
33​
34function dispatchProjection(
35 sessionCapabilities?: OrchestrationV2ProviderCapabilities,
36): OrchestrationV2ThreadProjection {
37 const providerThreadId = ProviderThreadId.make("command-policy-provider-thread");
38 const providerSessionId = ProviderSessionId.make("command-policy-provider-session");
39 return {
40 runs:
41 sessionCapabilities === undefined
42 ? []
43 : [
44 {
45 id: activeRunId,
46 status: "running",
47 providerThreadId,
48 activeAttemptId,
49 userMessageId,
50 },
51 ],
52 messages: [{ id: userMessageId, text: "first", attachments: [] }],
53 providerThreads:
54 sessionCapabilities === undefined ? [] : [{ id: providerThreadId, providerSessionId }],
55 providerSessions:
56 sessionCapabilities === undefined
57 ? []
58 : [{ id: providerSessionId, capabilities: sessionCapabilities }],
59 providerTurns:
60 sessionCapabilities === undefined
61 ? []
62 : [{ runAttemptId: activeAttemptId, status: "running" }],
63 } as unknown as OrchestrationV2ThreadProjection;
65​
66it("resolves automatic message delivery from authoritative provider capabilities", () => {
67 assert.deepEqual(
68 CommandPolicy.resolveMessageDispatchIntent(
69 dispatchProjection(),
70 { type: "start_immediately" },
71 "auto",
72 ),
73 { type: "start_immediately" },
74 );
75 assert.deepEqual(
76 CommandPolicy.resolveMessageDispatchIntent(
77 dispatchProjection(baseCapabilities),
78 { type: "start_immediately" },
79 "auto",
80 ),
81 { type: "steer_active", targetRunId: activeRunId },
82 );
83 assert.deepEqual(
84 CommandPolicy.resolveMessageDispatchIntent(
85 dispatchProjection(
86 capabilities((current) => ({
87 ...current,
88 turns: {
89 ...current.turns,
90 supportsActiveSteering: false,
91 supportsQueuedMessages: true,
92 supportsSteeringByInterruptRestart: true,
93 },
94 })),
95 ),
96 { type: "start_immediately" },
97 "auto",
98 ),
99 { type: "queue_after_active" },
100 );
101 assert.deepEqual(
102 CommandPolicy.resolveMessageDispatchIntent(
103 dispatchProjection(
104 capabilities((current) => ({
105 ...current,
106 turns: {
107 ...current.turns,
108 supportsActiveSteering: false,
109 supportsQueuedMessages: false,
110 supportsSteeringByInterruptRestart: true,
111 },
112 })),
113 ),
114 { type: "start_immediately" },
115 "auto",
116 ),
117 { type: "restart_active", targetRunId: activeRunId },
118 );
119});
120​
121it.each(["preparing", "starting"] as const)(
122 "queues an automatic message while the handoff run is %s",
123 (status) => {
124 const projection = dispatchProjection(baseCapabilities);
125 assert.deepEqual(
126 CommandPolicy.resolveMessageDispatchIntent(
127 { ...projection, runs: projection.runs.map((run) => ({ ...run, status })) },
128 { type: "start_immediately" },
129 "auto",
130 ),
131 { type: "queue_after_active" },
132 );
133 },
134);
135​
136it.each([
137 ["has not started", []],
138 ["is pending", [{ runAttemptId: activeAttemptId, status: "pending" }]],
139 ["has completed", [{ runAttemptId: activeAttemptId, status: "completed" }]],
140 [
141 "belongs to a previous attempt",
142 [{ runAttemptId: RunAttemptId.make("previous-attempt"), status: "running" }],
143 ],
144])("queues only automatic delivery when the active run's provider turn %s", (_, providerTurns) => {
145 const projection = {
146 ...dispatchProjection(baseCapabilities),
147 providerTurns,
148 } as unknown as OrchestrationV2ThreadProjection;
149 assert.deepEqual(
150 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "auto"),
151 { type: "queue_after_active" },
152 );
153 assert.deepEqual(
154 CommandPolicy.resolveMessageDispatchIntent(projection, { type: "start_immediately" }, "steer"),
155 { type: "steer_active", targetRunId: activeRunId },
156 );
157 assert.deepEqual(
158 CommandPolicy.resolveMessageDispatchIntent(
159 projection,
160 { type: "start_immediately" },
161 "restart",
162 ),
163 { type: "restart_active", targetRunId: activeRunId },
164 );
165});
166​
167it.each(["/compact", " /LOGOUT "])("queues a steer sent while %s runs", (text) => {
168 const projection = dispatchProjection(baseCapabilities);
169 const maintenance = {
170 ...projection,
171 messages: [{ id: userMessageId, text, attachments: [] }],
172 } as unknown as OrchestrationV2ThreadProjection;
173 for (const deliveryIntent of ["auto", "steer"] as const) {
174 assert.deepEqual(
175 CommandPolicy.resolveMessageDispatchIntent(
176 maintenance,
177 { type: "start_immediately" },
178 deliveryIntent,
179 ),
180 { type: "queue_after_active" },
181 );
182 }
183 assert.deepEqual(
184 CommandPolicy.resolveMessageDispatchIntent(maintenance, {
185 type: "steer_active",
186 targetRunId: activeRunId,
187 }),
188 { type: "queue_after_active" },
189 );
190 assert.deepEqual(
191 CommandPolicy.resolveMessageDispatchIntent(
192 maintenance,
193 { type: "start_immediately" },
194 "restart",
195 ),
196 { type: "restart_active", targetRunId: activeRunId },
197 );
198});
199​
200const layer = it.layer(CommandPolicy.layer);
201​
202layer("CommandPolicyV2", (it) => {
203 it.effect("falls back to portable context when Cursor cannot fork natively", () =>
204 Effect.gen(function* () {
205 const policy = yield* CommandPolicy.CommandPolicyV2;
206​
207 const result = yield* policy.decideForkExecution({
208 commandId,
209 threadId,
210 providerInstanceId: ProviderInstanceId.make("cursor"),
211 capabilities: CursorProviderCapabilitiesV2,
212 sameProvider: true,
213 hasStrongNativeSource: true,
214 sourceRunStatus: "completed",
215 fromSpecificTurn: true,
216 });
217​
218 assert.equal(result, "portable_context");
219 }),
220 );
221});