The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 2 | import { expect, it } from "@effect/vitest"; |
| 3 | import type { NotificationHandler } from "@muse-code/sdk"; |
| 4 | import { ProviderInstanceId, type ServerProviderModel } from "@t3tools/contracts"; |
| 5 | import { MuseSettings } from "../settings.ts"; |
| 6 | import { createModelSelection } from "@t3tools/shared/model"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Fiber from "effect/Fiber"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { vi } from "vite-plus/test"; |
| 11 | |
| 12 | import type { MuseSdkHost } from "./sdk.ts"; |
| 13 | import { museModelCapabilities } from "./modelCatalog.ts"; |
| 14 | import { makeMuseTextGeneration } from "./textGeneration.ts"; |
| 15 | |
| 16 | const settings = Schema.decodeSync(MuseSettings)({ enabled: true }); |
| 17 | const modelSelection = createModelSelection( |
| 18 | ProviderInstanceId.make("muse"), |
| 19 | "muse-spark-1.3-contributor", |
| 20 | [{ id: "reasoningEffort", value: "high" }], |
| 21 | ); |
| 22 | const titleInput = { |
| 23 | cwd: "/project/should-not-be-used", |
| 24 | message: "Fix the login form", |
| 25 | modelSelection, |
| 26 | }; |
| 27 | |
| 28 | function fixture( |
| 29 | onTurn?: (emit: (method: string, params: Record<string, unknown>) => void) => void, |
| 30 | acknowledgeTurn = true, |
| 31 | modelCatalog?: Effect.Effect<ReadonlyArray<ServerProviderModel>>, |
| 32 | ) { |
| 33 | let handler: NotificationHandler = () => {}; |
| 34 | let commandSequence = 0; |
| 35 | let activeTurnId: string | undefined; |
| 36 | const mintCommandId = () => `turn-${++commandSequence}`; |
| 37 | const started = Promise.withResolvers<void>(); |
| 38 | const closed = Promise.withResolvers<void>(); |
| 39 | const emit = (method: string, params: Record<string, unknown>) => |
| 40 | handler({ |
| 41 | jsonrpc: "2.0", |
| 42 | method, |
| 43 | params: { |
| 44 | sessionId: "session-1", |
| 45 | turnId: activeTurnId, |
| 46 | ...params, |
| 47 | ...(params.item |
| 48 | ? { |
| 49 | item: { |
| 50 | revision: 1, |
| 51 | turnId: activeTurnId, |
| 52 | ...(params.item as Record<string, unknown>), |
| 53 | }, |
| 54 | } |
| 55 | : {}), |
| 56 | }, |
| 57 | }); |
| 58 | const host: MuseSdkHost = { |
| 59 | initializeResult: { grantedCapabilities: [] }, |
| 60 | connection: { |
| 61 | command: vi.fn(async (method, _params, options) => { |
| 62 | // Connection.command overwrites params.commandId unless supplied in options. |
| 63 | const commandId = options?.commandId ?? mintCommandId(); |
| 64 | if (method === "session/start") return { session: { sessionId: "session-1" } }; |
| 65 | if (method === "turn/start") { |
| 66 | activeTurnId = commandId; |
| 67 | started.resolve(); |
| 68 | onTurn?.(emit); |
| 69 | if (!acknowledgeTurn) return await new Promise<Record<string, unknown>>(() => {}); |
| 70 | return { turnId: commandId }; |
| 71 | } |
| 72 | return {}; |
| 73 | }), |
| 74 | request: vi.fn(async () => ({})), |
| 75 | mintCommandId, |
| 76 | onNotification: (nextHandler) => { |
| 77 | handler = nextHandler; |
| 78 | }, |
| 79 | onProtocolError: () => {}, |
| 80 | onServerRequest: () => {}, |
| 81 | closed: closed.promise, |
| 82 | }, |
| 83 | close: vi.fn(async () => { |
| 84 | closed.resolve(); |
| 85 | }), |
| 86 | exited: new Promise(() => {}), |
| 87 | }; |
| 88 | const createHost = vi.fn(async () => host); |
| 89 | const make = makeMuseTextGeneration(settings, { |
| 90 | createHost, |
| 91 | ...(modelCatalog ? { modelCatalog } : {}), |
| 92 | }); |
| 93 | return { |
| 94 | host, |
| 95 | createHost, |
| 96 | started: started.promise, |
| 97 | closeConnection: closed.resolve, |
| 98 | emit, |
| 99 | make, |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | const finish = |
| 104 | (text: string) => (emit: (method: string, params: Record<string, unknown>) => void) => { |
| 105 | emit("item/updated", { item: { itemId: "answer", kind: "agentMessage", text: "partial" } }); |
| 106 | emit("item/completed", { |
| 107 | item: { itemId: "answer", kind: "agentMessage", revision: 2, text }, |
| 108 | }); |
| 109 | emit("turn/completed", { terminal: "completed" }); |
| 110 | }; |
| 111 | |
| 112 | it.layer(NodeServices.layer)("Muse text generation", (it) => { |
| 113 | it.effect("validates the final item once and closes an isolated restrictive host", () => |
| 114 | Effect.gen(function* () { |
| 115 | const test = fixture(finish('{"title":"Fix login form"}')); |
| 116 | const service = yield* test.make; |
| 117 | expect(yield* service.generateThreadTitle(titleInput)).toEqual({ |
| 118 | title: "Fix login form", |
| 119 | }); |
| 120 | expect(test.createHost).toHaveBeenCalledWith( |
| 121 | expect.objectContaining({ readOnly: true, sessionLogging: true }), |
| 122 | ); |
| 123 | expect(test.createHost).not.toHaveBeenCalledWith( |
| 124 | expect.objectContaining({ cwd: titleInput.cwd }), |
| 125 | ); |
| 126 | expect(test.host.connection.command).toHaveBeenCalledWith( |
| 127 | "session/start", |
| 128 | expect.objectContaining({ |
| 129 | providerId: "meta", |
| 130 | modelId: modelSelection.model, |
| 131 | approvalMode: "denyUnmatched", |
| 132 | }), |
| 133 | ); |
| 134 | expect(test.host.connection.command).toHaveBeenCalledWith( |
| 135 | "turn/start", |
| 136 | expect.objectContaining({ |
| 137 | reasoningEffort: "high", |
| 138 | }), |
| 139 | { commandId: "turn-1" }, |
| 140 | ); |
| 141 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 142 | }), |
| 143 | ); |
| 144 | |
| 145 | it.effect("uses low effort by default and normalizes stale efforts against the model", () => |
| 146 | Effect.gen(function* () { |
| 147 | for (const { saved, variants, expected } of [ |
| 148 | { saved: undefined, variants: undefined, expected: "low" }, |
| 149 | { saved: undefined, variants: ["medium", "xhigh"], expected: "medium" }, |
| 150 | { saved: "max", variants: ["minimal", "max"], expected: "max" }, |
| 151 | { saved: "ultra", variants: ["medium", "xhigh"], expected: "medium" }, |
| 152 | { saved: "high", variants: [], expected: undefined }, |
| 153 | ]) { |
| 154 | const test = fixture( |
| 155 | finish('{"title":"Fix login form"}'), |
| 156 | true, |
| 157 | Effect.succeed([ |
| 158 | { |
| 159 | slug: modelSelection.model, |
| 160 | name: modelSelection.model, |
| 161 | isCustom: false, |
| 162 | capabilities: museModelCapabilities(variants ? { variants } : undefined), |
| 163 | }, |
| 164 | ]), |
| 165 | ); |
| 166 | const service = yield* test.make; |
| 167 | yield* service.generateThreadTitle({ |
| 168 | ...titleInput, |
| 169 | modelSelection: createModelSelection( |
| 170 | ProviderInstanceId.make("muse"), |
| 171 | modelSelection.model, |
| 172 | saved !== undefined ? [{ id: "reasoningEffort", value: saved }] : undefined, |
| 173 | ), |
| 174 | }); |
| 175 | const turn = vi |
| 176 | .mocked(test.host.connection.command) |
| 177 | .mock.calls.find(([method]) => method === "turn/start"); |
| 178 | expect(turn?.[1].reasoningEffort).toBe(expected); |
| 179 | expect(Object.hasOwn(turn?.[1] ?? {}, "reasoningEffort")).toBe(expected !== undefined); |
| 180 | } |
| 181 | }), |
| 182 | ); |
| 183 | |
| 184 | it.effect("ignores output and terminal notifications for another turn", () => |
| 185 | Effect.gen(function* () { |
| 186 | const test = fixture((emit) => { |
| 187 | emit("item/completed", { |
| 188 | item: { |
| 189 | itemId: "other-answer", |
| 190 | kind: "agentMessage", |
| 191 | turnId: "another-turn", |
| 192 | text: '{"title":"Wrong title"}', |
| 193 | }, |
| 194 | }); |
| 195 | emit("turn/completed", { turnId: "another-turn", terminal: "completed" }); |
| 196 | finish('{"title":"Fix login form"}')(emit); |
| 197 | }); |
| 198 | const service = yield* test.make; |
| 199 | expect(yield* service.generateThreadTitle(titleInput)).toEqual({ title: "Fix login form" }); |
| 200 | expect(test.host.connection.command).toHaveBeenCalledWith( |
| 201 | "turn/start", |
| 202 | expect.objectContaining({ sessionId: "session-1" }), |
| 203 | { commandId: "turn-1" }, |
| 204 | ); |
| 205 | }), |
| 206 | ); |
| 207 | |
| 208 | it.effect("rejects a truncated response even when its prefix contains valid JSON", () => |
| 209 | Effect.gen(function* () { |
| 210 | const test = fixture((emit) => { |
| 211 | emit("item/completed", { |
| 212 | item: { |
| 213 | itemId: "answer", |
| 214 | kind: "agentMessage", |
| 215 | text: '{"title":"Incomplete answer"}', |
| 216 | truncated: true, |
| 217 | }, |
| 218 | }); |
| 219 | emit("turn/completed", { terminal: "completed" }); |
| 220 | }); |
| 221 | const service = yield* test.make; |
| 222 | expect(yield* service.generateThreadTitle(titleInput).pipe(Effect.flip)).toMatchObject({ |
| 223 | _tag: "TextGenerationError", |
| 224 | }); |
| 225 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 226 | }), |
| 227 | ); |
| 228 | |
| 229 | it.effect("explains a failed turn by Muse's error category", () => |
| 230 | Effect.gen(function* () { |
| 231 | const message = "API error 429: raw provider text"; |
| 232 | const cases = [ |
| 233 | { |
| 234 | error: { kind: "modelError", quota: { limitKind: "tierLimit", resetsAtMs: 1 } }, |
| 235 | detail: "Muse Code usage limit reached. Try again after it resets.", |
| 236 | }, |
| 237 | { |
| 238 | // Credential proxies can refuse with a 429 quota message and no quota object. |
| 239 | error: { kind: "modelError", message: "upstream 429: quota exceeded for this key" }, |
| 240 | detail: "Muse Code usage limit reached. Try again after it resets.", |
| 241 | }, |
| 242 | { |
| 243 | error: { kind: "authRequired" }, |
| 244 | detail: "Muse Code is not signed in. Run `muse login` on this T3 server host.", |
| 245 | }, |
| 246 | { |
| 247 | error: { kind: "modelError" }, |
| 248 | detail: "Muse Code's model request failed. Try again or choose another Muse model.", |
| 249 | }, |
| 250 | { |
| 251 | error: { kind: "stepLimit" }, |
| 252 | detail: |
| 253 | "Muse Code text generation failed. Check Muse login and availability on this T3 server host.", |
| 254 | }, |
| 255 | { |
| 256 | reason: "upstream 429: quota exceeded for this key", |
| 257 | detail: "Muse Code usage limit reached. Try again after it resets.", |
| 258 | }, |
| 259 | { |
| 260 | reason: "raw provider refusal", |
| 261 | detail: |
| 262 | "Muse Code text generation failed. Check Muse login and availability on this T3 server host.", |
| 263 | }, |
| 264 | ]; |
| 265 | for (const { error, reason, detail } of cases) { |
| 266 | const raw = error === undefined ? reason : "message" in error ? error.message : message; |
| 267 | const test = fixture((emit) => |
| 268 | emit("turn/completed", { |
| 269 | terminal: "failed", |
| 270 | ...(error === undefined |
| 271 | ? { reason } |
| 272 | : { error: { message, ...error, retryable: false } }), |
| 273 | }), |
| 274 | ); |
| 275 | const service = yield* test.make; |
| 276 | const failure = yield* service.generateThreadTitle(titleInput).pipe(Effect.flip); |
| 277 | expect(failure).toMatchObject({ _tag: "TextGenerationError", detail }); |
| 278 | expect(failure.detail).not.toContain(raw); |
| 279 | expect(failure.cause).toMatchObject({ cause: { message: raw } }); |
| 280 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 281 | } |
| 282 | }), |
| 283 | ); |
| 284 | |
| 285 | it.effect("settles a completed turn even when its start acknowledgement never arrives", () => |
| 286 | Effect.gen(function* () { |
| 287 | const test = fixture(finish('{"title":"Fix login form"}'), false); |
| 288 | const service = yield* test.make; |
| 289 | expect(yield* service.generateThreadTitle(titleInput)).toEqual({ title: "Fix login form" }); |
| 290 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 291 | }), |
| 292 | ); |
| 293 | |
| 294 | it.effect("settles when the transport closes before completion", () => |
| 295 | Effect.gen(function* () { |
| 296 | const test = fixture(); |
| 297 | const service = yield* test.make; |
| 298 | const pending = yield* service |
| 299 | .generateThreadTitle(titleInput) |
| 300 | .pipe(Effect.flip, Effect.forkChild); |
| 301 | yield* Effect.promise(() => test.started); |
| 302 | test.closeConnection(); |
| 303 | expect(yield* Fiber.join(pending)).toMatchObject({ _tag: "TextGenerationError" }); |
| 304 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 305 | }), |
| 306 | ); |
| 307 | |
| 308 | it.effect("releases the host when interrupted after the turn starts", () => |
| 309 | Effect.gen(function* () { |
| 310 | const test = fixture(); |
| 311 | const service = yield* test.make; |
| 312 | const fiber = yield* service.generateThreadTitle(titleInput).pipe(Effect.forkChild); |
| 313 | yield* Effect.promise(() => test.started); |
| 314 | yield* Fiber.interrupt(fiber); |
| 315 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 316 | }), |
| 317 | ); |
| 318 | |
| 319 | it.effect("denies approval requests and fails the noninteractive request", () => |
| 320 | Effect.gen(function* () { |
| 321 | const test = fixture((emit) => |
| 322 | emit("approval/requested", { |
| 323 | approvalId: "approval-1", |
| 324 | currentRequirementId: "requirement-1", |
| 325 | availableChoices: [{ choiceId: "deny-1", decision: "denied" }], |
| 326 | }), |
| 327 | ); |
| 328 | const service = yield* test.make; |
| 329 | expect(yield* service.generateThreadTitle(titleInput).pipe(Effect.flip)).toMatchObject({ |
| 330 | _tag: "TextGenerationError", |
| 331 | }); |
| 332 | expect(test.host.connection.command).toHaveBeenCalledWith("approval/decide", { |
| 333 | sessionId: "session-1", |
| 334 | approvalId: "approval-1", |
| 335 | requirementId: "requirement-1", |
| 336 | choiceId: "deny-1", |
| 337 | }); |
| 338 | expect(test.host.close).toHaveBeenCalledOnce(); |
| 339 | }), |
| 340 | ); |
| 341 | }); |