@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, 04:45 UTC

main
ts 270 lines 8.51 KB
Raw
1 import { assert, describe, it } from "@effect/vitest";
2 import { ProviderInstanceId } from "@t3tools/contracts";
3 import * as Cause from "effect/Cause";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
6 import * as EffectAcpErrors from "effect-acp/errors";
7 import type * as EffectAcpSchema from "effect-acp/compat";
8
9 import { ProviderAdapterV2RuntimePolicy } from "@t3tools/provider-core/server/ProviderAdapter";
10
11 import {
12 applyFxModelSelection,
13 fxPermissionDisposition,
14 fxSessionModeForPolicy,
15 fxToolCallWasCancelled,
16 } from "./acpSupport.ts";
17
18 const editRequest = {
19 sessionId: "fx-session-01",
20 toolCall: { toolCallId: "tool-1", kind: "edit", title: "Write notes.md" },
21 options: [
22 { optionId: "allow", name: "Allow", kind: "allow_once" },
23 { optionId: "reject", name: "Reject", kind: "reject_once" },
24 ],
25 } satisfies EffectAcpSchema.RequestPermissionRequest;
26
27 describe("fx permission prompts", () => {
28 // In Auto, fx's own review asks only about what it refused itself; T3
29 // answering those by its Auto policy would approve exactly what fx escalated.
30 it("sends every Auto prompt to the user", () => {
31 assert.equal(
32 fxPermissionDisposition(
33 ProviderAdapterV2RuntimePolicy.make({
34 runtimeMode: "auto",
35 interactionMode: "default",
36 cwd: "/work",
37 }),
38 editRequest,
39 ),
40 "ask",
41 );
42 });
43
44 it("answers Full access prompts itself unless an explicit policy confines them", () => {
45 const fullAccess = {
46 runtimeMode: "full-access",
47 interactionMode: "default",
48 cwd: "/work",
49 } as const;
50 assert.equal(
51 fxPermissionDisposition(ProviderAdapterV2RuntimePolicy.make(fullAccess), editRequest),
52 "allow",
53 );
54 // Runs fx's `ask`, so the policy decides what fx asks about.
55 assert.equal(
56 fxPermissionDisposition(
57 ProviderAdapterV2RuntimePolicy.make({ ...fullAccess, approvalPolicy: "on-request" }),
58 editRequest,
59 ),
60 "ask",
61 );
62 for (const confined of [
63 { approvalPolicy: "on-request" },
64 { sandboxPolicy: { type: "readOnly" } },
65 ] as const) {
66 assert.equal(
67 fxSessionModeForPolicy(ProviderAdapterV2RuntimePolicy.make({ ...fullAccess, ...confined })),
68 "ask",
69 );
70 }
71 });
72 });
73
74 describe("fx model selection", () => {
75 it.effect.each(["unavailable", "rejected"] as const)(
76 "restores the original backend and model when Default is %s",
77 (failure) =>
78 Effect.gen(function* () {
79 let backend = "codex";
80 let model = "codex-custom";
81 const configOptions = (): ReadonlyArray<EffectAcpSchema.SessionConfigOption> => [
82 {
83 id: "provider",
84 name: "provider",
85 type: "select",
86 currentValue: backend,
87 options: ["codex", "gateway"].map((value) => ({ value, name: value })),
88 },
89 {
90 id: "model",
91 name: "model",
92 type: "select",
93 currentValue: model,
94 options: (backend === "codex"
95 ? ["codex-default", "codex-custom"]
96 : ["gateway-fallback", ...(failure === "rejected" ? ["gateway-default"] : [])]
97 ).map((value) => ({ value, name: value })),
98 },
99 ];
100 const runtime = {
101 getConfigOptions: Effect.sync(configOptions),
102 setConfigOption: (id: string, value: string | boolean) =>
103 Effect.gen(function* () {
104 if (id === "provider") {
105 backend = String(value);
106 model = backend === "codex" ? "codex-default" : "gateway-fallback";
107 } else if (id === "model") {
108 if (value === "gateway-default") {
109 return yield* new EffectAcpErrors.AcpRequestError({
110 code: -32602,
111 errorMessage: "Model rejected",
112 });
113 }
114 model = String(value);
115 }
116 return { configOptions: configOptions() };
117 }),
118 };
119 const result = yield* applyFxModelSelection(
120 runtime,
121 { instanceId: ProviderInstanceId.make("fx"), model: "default" },
122 Effect.succeed({ backend: "gateway", model: "gateway-default" }),
123 ).pipe(Effect.exit);
124 assert.isTrue(Exit.isFailure(result));
125 if (Exit.isFailure(result)) {
126 assert.include(
127 Cause.pretty(result.cause),
128 failure === "unavailable"
129 ? "The default fx model is unavailable on its configured backend."
130 : "Model rejected",
131 );
132 }
133 assert.equal(backend, "codex");
134 assert.equal(model, "codex-custom");
135 }),
136 );
137 });
138
139 describe("fx cancellation failures", () => {
140 const diff = { type: "diff", path: "notes.txt", oldText: "old", newText: "new" };
141 const terminal = { type: "terminal", terminalId: "fx-terminal" };
142
143 it.each([
144 { content: ["Cancelled"], cancelled: true },
145 { content: ["Cancelled."], cancelled: true },
146 { content: ['{"error":{"details":{"error":"Cancelled"}}}'], cancelled: true },
147 {
148 content: ['{"error":{"tool":"shell","code":"Cancelled","retryable":false}}'],
149 cancelled: true,
150 },
151 {
152 content: ['{"error":{"tool":"shell","code":"PermissionDenied","retryable":false}}'],
153 cancelled: false,
154 },
155 { content: ['{"error":{"details":{"error":"Permission denied"}}}'], cancelled: false },
156 { content: ["Command failed"], cancelled: false },
157 { content: [""], cancelled: false },
158 { content: ["Cancelled"], followingItems: [diff], cancelled: true },
159 { content: ["Cancelled"], precedingItems: [diff], cancelled: false },
160 {
161 content: ['{"error":{"details":{"error":"Permission denied"}}}', "Cancelled"],
162 cancelled: false,
163 },
164 {
165 content: ["Command failed", '{"error":{"details":{"error":"Cancelled"}}}'],
166 cancelled: false,
167 },
168 {
169 content: ["Cancelled", '{"error":{"details":{"error":"Permission denied"}}}'],
170 cancelled: true,
171 },
172 ])(
173 "recognizes $content as cancellation: $cancelled",
174 ({ content, cancelled, precedingItems = [], followingItems = [] }) => {
175 assert.equal(
176 fxToolCallWasCancelled({
177 update: {
178 content: [
179 ...precedingItems,
180 ...content.map((text) => ({ content: { text } })),
181 ...followingItems,
182 ],
183 },
184 }),
185 cancelled,
186 );
187 },
188 );
189
190 it.each([
191 {
192 exit_code: null,
193 signal: null,
194 timed_out: false,
195 termination_indeterminate: true,
196 content: "",
197 cancelled: true,
198 },
199 {
200 exit_code: null,
201 signal: null,
202 timed_out: false,
203 termination_indeterminate: true,
204 content:
205 '{"error":{"type":"tool_execution_failed","message":"Command started, but its final process status could not be confirmed",',
206 followingContent: ["Cancelled"],
207 cancelled: false,
208 },
209 {
210 exit_code: null,
211 signal: null,
212 timed_out: false,
213 termination_indeterminate: true,
214 content: '{"error":{"message":"Permission denied","details":{"error":"Permission denied"}}}',
215 followingItems: [terminal],
216 cancelled: false,
217 },
218 {
219 exit_code: null,
220 signal: null,
221 timed_out: false,
222 termination_indeterminate: true,
223 content: '{"error":{"type":"tool_execution_failed","message":"clipped",',
224 followingItems: [diff],
225 cancelled: false,
226 },
227 {
228 exit_code: null,
229 signal: null,
230 timed_out: false,
231 termination_indeterminate: true,
232 content: "Still processing",
233 followingContent: ['{"error":{"message":"Permission denied"}}'],
234 followingItems: [diff],
235 cancelled: false,
236 },
237 {
238 exit_code: 1,
239 signal: null,
240 timed_out: false,
241 termination_indeterminate: false,
242 content: "",
243 cancelled: false,
244 },
245 {
246 exit_code: null,
247 signal: null,
248 timed_out: true,
249 termination_indeterminate: true,
250 content: "",
251 cancelled: false,
252 },
253 ])(
254 "recognizes a command result with exit $exit_code, timeout $timed_out",
255 ({ cancelled, content, followingContent = [], followingItems = [], ...commandResult }) => {
256 assert.equal(
257 fxToolCallWasCancelled({
258 update: {
259 command_result: commandResult,
260 content: [
261 ...[content, ...followingContent].map((text) => ({ content: { text } })),
262 ...followingItems,
263 ],
264 },
265 }),
266 cancelled,
267 );
268 },
269 );
270 });