The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics-next-line nodeBuiltinImport:off -- Mock native realpath to exercise macOS case correction on every platform. |
| 2 | import * as NodeFSP from "node:fs/promises"; |
| 3 | import { vi } from "vite-plus/test"; |
| 4 | import { assert, describe, it } from "@effect/vitest"; |
| 5 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 6 | import { MspError } from "@muse-code/sdk"; |
| 7 | import { |
| 8 | MUSE_DEFAULT_MODEL, |
| 9 | EnvironmentId, |
| 10 | MessageId, |
| 11 | NodeId, |
| 12 | ProjectId, |
| 13 | ProviderInstanceId, |
| 14 | ProviderDriverKind, |
| 15 | ProviderSessionId, |
| 16 | ProviderThreadId, |
| 17 | ProviderTurnId, |
| 18 | RunAttemptId, |
| 19 | RunId, |
| 20 | ThreadId, |
| 21 | type OrchestrationV2AppThread, |
| 22 | type OrchestrationV2ProviderThread, |
| 23 | } from "@t3tools/contracts"; |
| 24 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 25 | import { AgentScope } from "@t3tools/shared/AgentScope"; |
| 26 | import { MuseSettings } from "../settings.ts"; |
| 27 | import * as DateTime from "effect/DateTime"; |
| 28 | import * as Deferred from "effect/Deferred"; |
| 29 | import * as Effect from "effect/Effect"; |
| 30 | import * as Option from "effect/Option"; |
| 31 | import type * as Exit from "effect/Exit"; |
| 32 | import * as Fiber from "effect/Fiber"; |
| 33 | import * as Layer from "effect/Layer"; |
| 34 | import * as Queue from "effect/Queue"; |
| 35 | import * as Schema from "effect/Schema"; |
| 36 | import * as Stream from "effect/Stream"; |
| 37 | import * as TestClock from "effect/testing/TestClock"; |
| 38 | |
| 39 | import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost"; |
| 40 | import * as McpProviderSessions from "@t3tools/provider-core/server/McpProviderSessions"; |
| 41 | import type { MuseItem, MuseSubscriptionUsage } from "./protocol.ts"; |
| 42 | import type { MuseSdkHost, MuseSdkHostOptions } from "./sdk.ts"; |
| 43 | import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator"; |
| 44 | import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter"; |
| 45 | import type { ProviderUsageLimitsReport } from "@t3tools/provider-core/server/snapshot"; |
| 46 | import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests"; |
| 47 | import { makeMuseAdapterV2, type MuseAdapterV2Options } from "./adapter.ts"; |
| 48 | |
| 49 | const testLayer = Layer.mergeAll( |
| 50 | NodeServices.layer, |
| 51 | IdAllocator.layer, |
| 52 | McpProviderSessions.layer, |
| 53 | TestProviderHost.layer().pipe(Layer.provide(NodeServices.layer)), |
| 54 | ); |
| 55 | const MUSE_PROVIDER = ProviderDriverKind.make("muse"); |
| 56 | const INSTANCE_ID = ProviderInstanceId.make("muse_work"); |
| 57 | const THREAD_ID = ThreadId.make("thread-muse-test"); |
| 58 | const MODEL = "muse-spark-1.3-contributor"; |
| 59 | const museSettings = Schema.decodeSync(MuseSettings)({ enabled: true }); |
| 60 | const runtimePolicy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 61 | runtimeMode: "full-access", |
| 62 | interactionMode: "default", |
| 63 | cwd: null, |
| 64 | }); |
| 65 | const modelSelection = (instanceId = INSTANCE_ID) => ({ |
| 66 | instanceId, |
| 67 | model: MODEL, |
| 68 | options: [{ id: "reasoningEffort", value: "max" }], |
| 69 | }); |
| 70 | |
| 71 | interface NativeCall { |
| 72 | readonly method: string; |
| 73 | readonly params: Record<string, unknown>; |
| 74 | readonly commandId?: string; |
| 75 | } |
| 76 | |
| 77 | function pendingPromise<A>() { |
| 78 | let resolve!: (value: A) => void; |
| 79 | let reject!: (reason: unknown) => void; |
| 80 | const promise = new Promise<A>((done, fail) => { |
| 81 | resolve = done; |
| 82 | reject = fail; |
| 83 | }); |
| 84 | return { promise, resolve, reject }; |
| 85 | } |
| 86 | |
| 87 | // Match the SDK: handler registration returns void. |
| 88 | const makeFakeMuse = Effect.fnUntraced(function* (idPrefix = "native") { |
| 89 | const requests = yield* Queue.unbounded<NativeCall>(); |
| 90 | const calls: NativeCall[] = []; |
| 91 | const closed = pendingPromise<void>(); |
| 92 | const exited = pendingPromise<Awaited<MuseSdkHost["exited"]>>(); |
| 93 | let notify: Parameters<MuseSdkHost["connection"]["onNotification"]>[0] = () => {}; |
| 94 | let sequence = 0; |
| 95 | let sessionId = ""; |
| 96 | const closing = pendingPromise<void>(); |
| 97 | let closeCount = 0; |
| 98 | let closeGate: Promise<void> | undefined; |
| 99 | let onProcessExit: MuseSdkHostOptions["onProcessExit"]; |
| 100 | let history: MuseItem[] = []; |
| 101 | const responses = new Map< |
| 102 | string, |
| 103 | Array<Promise<Record<string, unknown>> | Record<string, unknown> | Error> |
| 104 | >(); |
| 105 | const respond = async (call: NativeCall): Promise<Record<string, unknown>> => { |
| 106 | calls.push(call); |
| 107 | Queue.offerUnsafe(requests, call); |
| 108 | if (["session/start", "session/resume", "session/read"].includes(call.method)) { |
| 109 | sessionId = String(call.params.sessionId); |
| 110 | } |
| 111 | const queued = responses.get(call.method)?.shift(); |
| 112 | if (queued instanceof Error) throw queued; |
| 113 | if (queued) return queued; |
| 114 | switch (call.method) { |
| 115 | case "session/start": |
| 116 | case "session/resume": |
| 117 | case "session/read": |
| 118 | sessionId = String(call.params.sessionId); |
| 119 | return { |
| 120 | session: { sessionId, modelId: MODEL, activeTurnId: null }, |
| 121 | history: { items: history }, |
| 122 | }; |
| 123 | case "turn/start": |
| 124 | return { turnId: call.commandId }; |
| 125 | case "turn/steer": |
| 126 | return { turnId: call.params.expectedTurnId }; |
| 127 | case "session/compact": |
| 128 | return { status: "accepted" }; |
| 129 | default: |
| 130 | return {}; |
| 131 | } |
| 132 | }; |
| 133 | const host: MuseSdkHost = { |
| 134 | connection: { |
| 135 | mintCommandId: () => `${idPrefix}-${++sequence}`, |
| 136 | command: (method, params, options) => |
| 137 | respond({ |
| 138 | method, |
| 139 | params, |
| 140 | ...(options?.commandId ? { commandId: options.commandId } : {}), |
| 141 | }), |
| 142 | request: (method, params = {}) => respond({ method, params }), |
| 143 | onNotification: (handler) => { |
| 144 | notify = handler; |
| 145 | }, |
| 146 | onProtocolError: () => {}, |
| 147 | onServerRequest: () => {}, |
| 148 | closed: closed.promise, |
| 149 | }, |
| 150 | initializeResult: { grantedCapabilities: [] }, |
| 151 | exited: exited.promise, |
| 152 | close: async () => { |
| 153 | closeCount += 1; |
| 154 | closing.resolve(); |
| 155 | await closeGate; |
| 156 | closed.resolve(); |
| 157 | exited.resolve({ code: 0, signal: null }); |
| 158 | }, |
| 159 | }; |
| 160 | return { |
| 161 | host, |
| 162 | calls, |
| 163 | closeCount: () => closeCount, |
| 164 | closing: closing.promise, |
| 165 | observeProcessExit: (callback: MuseSdkHostOptions["onProcessExit"]) => { |
| 166 | onProcessExit = callback; |
| 167 | }, |
| 168 | exitProcess: () => Effect.sync(() => onProcessExit?.({ code: 1, signal: null })), |
| 169 | holdClose: (gate: Promise<void>) => { |
| 170 | closeGate = gate; |
| 171 | }, |
| 172 | disconnect: () => |
| 173 | Effect.sync(() => { |
| 174 | closed.resolve(); |
| 175 | exited.resolve({ code: 1, signal: null }); |
| 176 | }), |
| 177 | setHistory: (items: MuseItem[]) => { |
| 178 | history = items; |
| 179 | }, |
| 180 | queueResponse: ( |
| 181 | method: string, |
| 182 | result: Record<string, unknown> | Promise<Record<string, unknown>> | Error, |
| 183 | ) => { |
| 184 | responses.set(method, [...(responses.get(method) ?? []), result]); |
| 185 | }, |
| 186 | emit: (method: string, params: Record<string, unknown>) => |
| 187 | Effect.sync(() => { |
| 188 | notify({ jsonrpc: "2.0", method, params: { sessionId, ...params } }); |
| 189 | }), |
| 190 | emitHostWide: (method: string, params: Record<string, unknown>) => |
| 191 | Effect.sync(() => { |
| 192 | notify({ jsonrpc: "2.0", method, params }); |
| 193 | }), |
| 194 | takeCall: Effect.fnUntraced(function* (method: string) { |
| 195 | while (true) { |
| 196 | const call = yield* Queue.take(requests); |
| 197 | if (call.method === method) return call; |
| 198 | } |
| 199 | }), |
| 200 | }; |
| 201 | }); |
| 202 | |
| 203 | const makeHarness = Effect.fnUntraced(function* ( |
| 204 | fake: Effect.Success<ReturnType<typeof makeFakeMuse>>, |
| 205 | instanceId = INSTANCE_ID, |
| 206 | initialNativeThreadId?: string, |
| 207 | replacement?: Effect.Success<ReturnType<typeof makeFakeMuse>>, |
| 208 | existingProviderThread?: OrchestrationV2ProviderThread, |
| 209 | policy = runtimePolicy, |
| 210 | overrides: Pick< |
| 211 | MuseAdapterV2Options, |
| 212 | "createHost" | "nativeEventLogger" | "modelCatalog" | "continuationRequests" | "onUsageLimits" |
| 213 | > = {}, |
| 214 | ) { |
| 215 | let hostCount = 0; |
| 216 | const adapter = yield* makeMuseAdapterV2({ |
| 217 | instanceId, |
| 218 | settings: museSettings, |
| 219 | environment: { PATH: "/fake/bin" }, |
| 220 | createHost: async (options) => { |
| 221 | const selected = hostCount++ === 0 ? fake : (replacement ?? fake); |
| 222 | selected.observeProcessExit(options.onProcessExit); |
| 223 | return selected.host; |
| 224 | }, |
| 225 | ...overrides, |
| 226 | }); |
| 227 | const runtime = yield* adapter.openSession({ |
| 228 | threadId: THREAD_ID, |
| 229 | providerSessionId: ProviderSessionId.make(`session-${instanceId}`), |
| 230 | modelSelection: modelSelection(instanceId), |
| 231 | runtimePolicy: policy, |
| 232 | ...(initialNativeThreadId ? { initialNativeThreadId } : {}), |
| 233 | }); |
| 234 | const emitted = yield* Queue.unbounded<ProviderAdapter.ProviderAdapterV2Event>(); |
| 235 | const allEvents: ProviderAdapter.ProviderAdapterV2Event[] = []; |
| 236 | const eventsEnded = yield* Deferred.make<Exit.Exit<void, Stream.Error<typeof runtime.events>>>(); |
| 237 | yield* runtime.events.pipe( |
| 238 | Stream.runForEach((event) => |
| 239 | Effect.gen(function* () { |
| 240 | allEvents.push(event); |
| 241 | yield* Queue.offer(emitted, event); |
| 242 | }), |
| 243 | ), |
| 244 | Effect.exit, |
| 245 | Effect.flatMap((exit) => Deferred.succeed(eventsEnded, exit)), |
| 246 | Effect.forkScoped, |
| 247 | ); |
| 248 | const providerThread = yield* runtime.ensureThread({ |
| 249 | threadId: THREAD_ID, |
| 250 | modelSelection: modelSelection(instanceId), |
| 251 | runtimePolicy: policy, |
| 252 | ...(existingProviderThread ? { existingProviderThread } : {}), |
| 253 | }); |
| 254 | const takeEvent = Effect.fnUntraced(function* < |
| 255 | T extends ProviderAdapter.ProviderAdapterV2Event["type"], |
| 256 | >( |
| 257 | type: T, |
| 258 | predicate: ( |
| 259 | event: Extract<ProviderAdapter.ProviderAdapterV2Event, { type: T }>, |
| 260 | ) => boolean = () => true, |
| 261 | ) { |
| 262 | while (true) { |
| 263 | const event = yield* Queue.take(emitted); |
| 264 | if ( |
| 265 | event.type === type && |
| 266 | predicate(event as Extract<ProviderAdapter.ProviderAdapterV2Event, { type: T }>) |
| 267 | ) { |
| 268 | return event as Extract<ProviderAdapter.ProviderAdapterV2Event, { type: T }>; |
| 269 | } |
| 270 | } |
| 271 | }); |
| 272 | return { adapter, runtime, providerThread, takeEvent, allEvents, policy, eventsEnded }; |
| 273 | }); |
| 274 | |
| 275 | const preallocatedProviderThread = Effect.fnUntraced(function* () { |
| 276 | const now = yield* DateTime.now; |
| 277 | return { |
| 278 | id: ProviderThreadId.make("preallocated-muse-thread"), |
| 279 | driver: MUSE_PROVIDER, |
| 280 | providerInstanceId: INSTANCE_ID, |
| 281 | providerSessionId: ProviderSessionId.make(`session-${INSTANCE_ID}`), |
| 282 | appThreadId: THREAD_ID, |
| 283 | ownerNodeId: null, |
| 284 | nativeThreadRef: null, |
| 285 | nativeConversationHeadRef: null, |
| 286 | status: "idle", |
| 287 | firstRunOrdinal: null, |
| 288 | lastRunOrdinal: null, |
| 289 | handoffIds: [], |
| 290 | forkedFrom: null, |
| 291 | createdAt: now, |
| 292 | updatedAt: now, |
| 293 | } satisfies OrchestrationV2ProviderThread; |
| 294 | }); |
| 295 | |
| 296 | const turnInput = Effect.fnUntraced(function* ( |
| 297 | providerThread: OrchestrationV2ProviderThread, |
| 298 | runOrdinal = 1, |
| 299 | policy = runtimePolicy, |
| 300 | ): Effect.fn.Return<ProviderAdapter.ProviderAdapterV2TurnInput> { |
| 301 | const now = yield* DateTime.now; |
| 302 | const threadId = providerThread.appThreadId ?? THREAD_ID; |
| 303 | const selection = modelSelection(providerThread.providerInstanceId); |
| 304 | const appThread: OrchestrationV2AppThread = { |
| 305 | id: threadId, |
| 306 | projectId: ProjectId.make("project-muse-test"), |
| 307 | title: "Muse test", |
| 308 | createdBy: "user", |
| 309 | creationSource: "web", |
| 310 | providerInstanceId: providerThread.providerInstanceId, |
| 311 | modelSelection: selection, |
| 312 | runtimeMode: policy.runtimeMode, |
| 313 | interactionMode: policy.interactionMode, |
| 314 | branch: null, |
| 315 | worktreePath: null, |
| 316 | activeProviderThreadId: providerThread.id, |
| 317 | lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: threadId }, |
| 318 | forkedFrom: null, |
| 319 | createdAt: now, |
| 320 | updatedAt: now, |
| 321 | archivedAt: null, |
| 322 | settledOverride: null, |
| 323 | settledAt: null, |
| 324 | lastVisitedAt: null, |
| 325 | deletedAt: null, |
| 326 | }; |
| 327 | return { |
| 328 | appThread, |
| 329 | threadId, |
| 330 | runId: RunId.make(`run-${runOrdinal}`), |
| 331 | runOrdinal, |
| 332 | providerTurnOrdinal: runOrdinal, |
| 333 | attemptId: RunAttemptId.make(`attempt-${runOrdinal}`), |
| 334 | rootNodeId: NodeId.make(`root-${runOrdinal}`), |
| 335 | providerThread, |
| 336 | message: { |
| 337 | messageId: MessageId.make(`message-${runOrdinal}`), |
| 338 | text: "Hello Muse", |
| 339 | attachments: [], |
| 340 | createdBy: "user", |
| 341 | creationSource: "web", |
| 342 | }, |
| 343 | modelSelection: selection, |
| 344 | runtimePolicy: policy, |
| 345 | }; |
| 346 | }); |
| 347 | |
| 348 | const startConversation = Effect.fnUntraced(function* ( |
| 349 | harness: Effect.Success<ReturnType<typeof makeHarness>>, |
| 350 | fake: Effect.Success<ReturnType<typeof makeFakeMuse>>, |
| 351 | ordinal = 1, |
| 352 | ) { |
| 353 | yield* harness.runtime.startTurn( |
| 354 | yield* turnInput(harness.providerThread, ordinal, harness.policy), |
| 355 | ); |
| 356 | const call = yield* fake.takeCall("turn/start"); |
| 357 | const event = yield* harness.takeEvent( |
| 358 | "provider_turn.updated", |
| 359 | (event) => event.providerTurn.status === "running", |
| 360 | ); |
| 361 | return { nativeId: call.commandId!, providerTurn: event.providerTurn }; |
| 362 | }); |
| 363 | |
| 364 | const SUBSCRIPTION_USAGE = { |
| 365 | window: { usedPercent: 0, windowDurationMins: 300, resetsAtMs: 1791465557000 }, |
| 366 | weekly: { usedPercent: 5, resetsAtMs: 1791763200000 }, |
| 367 | tier: "tier-1", |
| 368 | observedAtMs: 1791447762610, |
| 369 | } satisfies MuseSubscriptionUsage; |
| 370 | const QUOTA_MESSAGE = |
| 371 | "API error 429: Subscription quota exhausted. Your usage window resets soon. (rate_limit_error)"; |
| 372 | const RELAYED_QUOTA_MESSAGE = |
| 373 | "API error 429: All credentials for model muse-wonder-1.0-contributor are cooling down (last error: rate_limit_exceeded: Subscription quota exhausted. Your usage window resets at 2026-10-13T10:45:52Z.) (after 10 provider attempts)"; |
| 374 | |
| 375 | const approval = (turnId: string) => ({ |
| 376 | approvalId: "approval-1", |
| 377 | turnId, |
| 378 | currentRequirementId: { approvalId: "approval-1", sourceIndex: 0 }, |
| 379 | availableChoices: [ |
| 380 | { choiceId: "once", label: "Allow once", decision: "approved", scope: "once" }, |
| 381 | { choiceId: "deny", label: "Deny", decision: "denied", scope: "once" }, |
| 382 | ], |
| 383 | subject: { kind: "shell", command: "touch result.txt" }, |
| 384 | }); |
| 385 | |
| 386 | vi.mock("node:fs/promises", { spy: true }); |
| 387 | |
| 388 | describe("MuseAdapterV2", () => { |
| 389 | it.effect("connects the thread's MCP credential on native start and resume", () => |
| 390 | Effect.gen(function* () { |
| 391 | yield* (yield* McpProviderSessions.McpProviderSessions).set({ |
| 392 | environmentId: EnvironmentId.make("test-environment"), |
| 393 | threadId: THREAD_ID, |
| 394 | providerSessionId: "test-session", |
| 395 | providerInstanceId: INSTANCE_ID, |
| 396 | endpoint: "http://127.0.0.1:43210/mcp", |
| 397 | authorizationHeader: "Bearer thread-scoped-test-token", |
| 398 | browserToolsAvailable: true, |
| 399 | }); |
| 400 | for (const nativeId of [undefined, "saved-native-session"]) { |
| 401 | const fake = yield* makeFakeMuse(); |
| 402 | fake.host.initializeResult.grantedCapabilities.push("sessionMcp"); |
| 403 | yield* makeHarness(fake, INSTANCE_ID, nativeId); |
| 404 | const call = yield* fake.takeCall(nativeId ? "session/resume" : "session/start"); |
| 405 | assert.deepStrictEqual(call.params.config, { |
| 406 | mcpServers: { |
| 407 | "t3-code": { |
| 408 | transport: "streamableHttp", |
| 409 | mode: "optional", |
| 410 | url: "http://127.0.0.1:43210/mcp", |
| 411 | headers: { Authorization: "Bearer thread-scoped-test-token" }, |
| 412 | }, |
| 413 | }, |
| 414 | }); |
| 415 | } |
| 416 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 417 | ); |
| 418 | |
| 419 | it.effect("selects the runtime mode's permission profile on native start and resume", () => |
| 420 | Effect.gen(function* () { |
| 421 | for (const [runtimeMode, profile, approvalMode] of [ |
| 422 | ["approval-required", ":ask-me", "promptUnmatched"], |
| 423 | ["full-access", ":unrestricted", "allowAll"], |
| 424 | ] as const) { |
| 425 | const policy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 426 | ...runtimePolicy, |
| 427 | runtimeMode, |
| 428 | }); |
| 429 | const fresh = yield* makeFakeMuse(); |
| 430 | yield* makeHarness(fresh, INSTANCE_ID, undefined, undefined, undefined, policy); |
| 431 | const started = yield* fresh.takeCall("session/start"); |
| 432 | assert.strictEqual(started.params.permissionProfile, profile); |
| 433 | assert.strictEqual(started.params.approvalMode, approvalMode); |
| 434 | |
| 435 | const saved = yield* makeFakeMuse(); |
| 436 | yield* makeHarness(saved, INSTANCE_ID, "saved-session", undefined, undefined, policy); |
| 437 | const resumed = yield* saved.takeCall("session/resume"); |
| 438 | assert.strictEqual(resumed.params.permissionProfile, profile); |
| 439 | const mode = yield* saved.takeCall("session/setApprovalMode"); |
| 440 | assert.strictEqual(mode.params.mode, approvalMode); |
| 441 | } |
| 442 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 443 | ); |
| 444 | |
| 445 | it.effect("rejects a host without session MCP support before opening a native session", () => |
| 446 | Effect.gen(function* () { |
| 447 | yield* (yield* McpProviderSessions.McpProviderSessions).set({ |
| 448 | environmentId: EnvironmentId.make("test-environment"), |
| 449 | threadId: THREAD_ID, |
| 450 | providerSessionId: "test-session", |
| 451 | providerInstanceId: INSTANCE_ID, |
| 452 | endpoint: "http://127.0.0.1:43210/mcp", |
| 453 | authorizationHeader: "Bearer thread-scoped-test-token", |
| 454 | browserToolsAvailable: false, |
| 455 | }); |
| 456 | const fake = yield* makeFakeMuse(); |
| 457 | const outcome = yield* makeHarness(fake).pipe(Effect.result); |
| 458 | assert.strictEqual(outcome._tag, "Failure"); |
| 459 | assert.isFalse(fake.calls.some((call) => call.method === "session/start")); |
| 460 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 461 | ); |
| 462 | |
| 463 | it.effect("retries a missing native session with a cleared ref on the same healthy host", () => |
| 464 | Effect.gen(function* () { |
| 465 | for (const kind of ["sessionNotFound", "notFound"] as const) { |
| 466 | const fake = yield* makeFakeMuse(); |
| 467 | const allocated = yield* preallocatedProviderThread(); |
| 468 | const adapter = yield* makeMuseAdapterV2({ |
| 469 | instanceId: INSTANCE_ID, |
| 470 | settings: museSettings, |
| 471 | environment: { PATH: "/fake/bin" }, |
| 472 | createHost: async () => fake.host, |
| 473 | }); |
| 474 | const runtime = yield* adapter.openSession({ |
| 475 | threadId: THREAD_ID, |
| 476 | providerSessionId: ProviderSessionId.make(`missing-${kind}`), |
| 477 | modelSelection: modelSelection(), |
| 478 | runtimePolicy, |
| 479 | initialNativeThreadId: "missing-session", |
| 480 | }); |
| 481 | fake.queueResponse( |
| 482 | "session/resume", |
| 483 | new MspError({ code: -32000, message: "Saved session is absent", data: { kind } }), |
| 484 | ); |
| 485 | const failed = yield* runtime |
| 486 | .ensureThread({ |
| 487 | threadId: THREAD_ID, |
| 488 | modelSelection: modelSelection(), |
| 489 | runtimePolicy, |
| 490 | existingProviderThread: { |
| 491 | ...allocated, |
| 492 | nativeThreadRef: { |
| 493 | driver: MUSE_PROVIDER, |
| 494 | nativeId: "missing-session", |
| 495 | strength: "strong", |
| 496 | }, |
| 497 | }, |
| 498 | }) |
| 499 | .pipe(Effect.exit); |
| 500 | assert.strictEqual(failed._tag, "Failure"); |
| 501 | assert.strictEqual(fake.closeCount(), 0); |
| 502 | const fresh = yield* runtime.ensureThread({ |
| 503 | threadId: THREAD_ID, |
| 504 | modelSelection: modelSelection(), |
| 505 | runtimePolicy, |
| 506 | existingProviderThread: allocated, |
| 507 | }); |
| 508 | assert.strictEqual(fresh.id, allocated.id); |
| 509 | assert.notStrictEqual(fresh.nativeThreadRef?.nativeId, "missing-session"); |
| 510 | assert.strictEqual(fake.closeCount(), 0); |
| 511 | yield* runtime.startTurn(yield* turnInput(fresh)); |
| 512 | const admitted = yield* fake.takeCall("turn/start"); |
| 513 | assert.strictEqual(admitted.params.reasoningEffort, "max"); |
| 514 | yield* fake.emit("turn/completed", { turnId: admitted.commandId, terminal: "completed" }); |
| 515 | yield* Stream.runDrain( |
| 516 | runtime.events.pipe(Stream.takeUntil((event) => event.type === "turn.terminal")), |
| 517 | ); |
| 518 | } |
| 519 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 520 | ); |
| 521 | |
| 522 | it.effect("rejects cross-instance ensure requests without closing the current valid host", () => |
| 523 | Effect.gen(function* () { |
| 524 | const fake = yield* makeFakeMuse(); |
| 525 | const harness = yield* makeHarness(fake); |
| 526 | const rejected = yield* harness.runtime |
| 527 | .ensureThread({ |
| 528 | threadId: THREAD_ID, |
| 529 | modelSelection: modelSelection(), |
| 530 | runtimePolicy, |
| 531 | existingProviderThread: { |
| 532 | ...harness.providerThread, |
| 533 | providerInstanceId: ProviderInstanceId.make("other-account"), |
| 534 | }, |
| 535 | }) |
| 536 | .pipe(Effect.exit); |
| 537 | assert.strictEqual(rejected._tag, "Failure"); |
| 538 | assert.strictEqual(fake.closeCount(), 0); |
| 539 | yield* startConversation(harness, fake); |
| 540 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 541 | ); |
| 542 | |
| 543 | it.effect("pins Windows turns to Muse's verbatim root and starts Muse in the plain path", () => |
| 544 | Effect.gen(function* () { |
| 545 | const fake = yield* makeFakeMuse(); |
| 546 | let hostCwd: string | undefined; |
| 547 | const policy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 548 | ...runtimePolicy, |
| 549 | cwd: "C:\\work\\repo", |
| 550 | }); |
| 551 | const harness = yield* makeHarness( |
| 552 | fake, |
| 553 | INSTANCE_ID, |
| 554 | undefined, |
| 555 | undefined, |
| 556 | undefined, |
| 557 | policy, |
| 558 | { |
| 559 | createHost: async (options) => { |
| 560 | hostCwd = options.cwd; |
| 561 | return fake.host; |
| 562 | }, |
| 563 | }, |
| 564 | ); |
| 565 | yield* startConversation(harness, fake); |
| 566 | const turnStart = fake.calls.find((call) => call.method === "turn/start"); |
| 567 | // Muse 1.4.3 accepts only verbatim canonical roots. |
| 568 | assert.deepStrictEqual(turnStart?.params.workspaceRoots, ["\\\\?\\C:\\work\\repo"]); |
| 569 | // The muse.cmd launcher runs under cmd.exe, which cannot start in a verbatim directory. |
| 570 | assert.strictEqual(hostCwd, "C:\\work\\repo"); |
| 571 | }).pipe( |
| 572 | Effect.provideService(HostProcess.Platform, "win32"), |
| 573 | Effect.scoped, |
| 574 | Effect.provide(testLayer), |
| 575 | ), |
| 576 | ); |
| 577 | |
| 578 | it.effect("pins sessions and turns to native canonical case", () => |
| 579 | Effect.gen(function* () { |
| 580 | using _realpath = vi.spyOn(NodeFSP, "realpath").mockResolvedValue("/workspace/eevee"); |
| 581 | const fake = yield* makeFakeMuse(); |
| 582 | const policy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 583 | ...runtimePolicy, |
| 584 | cwd: "/workspace/EEVEE", |
| 585 | }); |
| 586 | const harness = yield* makeHarness( |
| 587 | fake, |
| 588 | INSTANCE_ID, |
| 589 | undefined, |
| 590 | undefined, |
| 591 | undefined, |
| 592 | policy, |
| 593 | ); |
| 594 | yield* startConversation(harness, fake); |
| 595 | const sessionStart = fake.calls.find((call) => call.method === "session/start"); |
| 596 | const turnStart = fake.calls.find((call) => call.method === "turn/start"); |
| 597 | assert.strictEqual(sessionStart?.params.workspaceRoot, "/workspace/eevee"); |
| 598 | assert.deepStrictEqual(turnStart?.params.workspaceRoots, [ |
| 599 | sessionStart?.params.workspaceRoot, |
| 600 | ]); |
| 601 | }).pipe( |
| 602 | Effect.scoped, |
| 603 | Effect.provide(testLayer), |
| 604 | Effect.provideService(HostProcess.Platform, "darwin"), |
| 605 | ), |
| 606 | ); |
| 607 | |
| 608 | it.effect("stops at a Meta usage limit until every exhausted window resets", () => |
| 609 | Effect.gen(function* () { |
| 610 | const fake = yield* makeFakeMuse(); |
| 611 | const reports: Array<ProviderUsageLimitsReport> = []; |
| 612 | const harness = yield* makeHarness( |
| 613 | fake, |
| 614 | INSTANCE_ID, |
| 615 | undefined, |
| 616 | undefined, |
| 617 | undefined, |
| 618 | runtimePolicy, |
| 619 | { onUsageLimits: (report) => Effect.sync(() => void reports.push(report)) }, |
| 620 | ); |
| 621 | yield* fake.emitHostWide("usage/changed", { |
| 622 | ...SUBSCRIPTION_USAGE, |
| 623 | window: { ...SUBSCRIPTION_USAGE.window, usedPercent: 103 }, |
| 624 | }); |
| 625 | const { nativeId } = yield* startConversation(harness, fake); |
| 626 | yield* fake.emit("turn/completed", { |
| 627 | turnId: nativeId, |
| 628 | terminal: "failed", |
| 629 | reason: QUOTA_MESSAGE, |
| 630 | error: { kind: "modelError", message: QUOTA_MESSAGE, retryable: false }, |
| 631 | }); |
| 632 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 633 | assert.strictEqual(terminal.status, "failed"); |
| 634 | assert.strictEqual(terminal.threadDisposition, "reusable"); |
| 635 | assert.strictEqual(terminal.failure?.class, "usage_limit"); |
| 636 | assert.strictEqual(terminal.failure?.resetAt, "2026-10-08T13:19:17.000Z"); |
| 637 | assert.deepStrictEqual( |
| 638 | reports.map((report) => [report.authLabel, report.windows[0]?.usedPercent]), |
| 639 | [["tier-1", 100]], |
| 640 | ); |
| 641 | assert.isTrue( |
| 642 | harness.allEvents.some( |
| 643 | (event) => |
| 644 | event.type === "turn_item.updated" && |
| 645 | event.turnItem.type === "error" && |
| 646 | event.turnItem.title === "Usage limit reached", |
| 647 | ), |
| 648 | ); |
| 649 | |
| 650 | const next = yield* startConversation(harness, fake, 2); |
| 651 | yield* fake.emit("turn/completed", { |
| 652 | turnId: next.nativeId, |
| 653 | terminal: "failed", |
| 654 | error: { message: "API error 429: Rate limit exceeded. (rate_limit_error)" }, |
| 655 | }); |
| 656 | const throttled = yield* harness.takeEvent("turn.terminal"); |
| 657 | assert.strictEqual(throttled.failure?.class, "provider_error"); |
| 658 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 659 | ); |
| 660 | |
| 661 | it.effect("resumes a quota stop at the reset the refusal names", () => |
| 662 | Effect.gen(function* () { |
| 663 | const fake = yield* makeFakeMuse(); |
| 664 | const harness = yield* makeHarness(fake); |
| 665 | const { nativeId } = yield* startConversation(harness, fake); |
| 666 | yield* fake.emit("turn/completed", { |
| 667 | turnId: nativeId, |
| 668 | terminal: "failed", |
| 669 | reason: RELAYED_QUOTA_MESSAGE, |
| 670 | error: { kind: "modelError", message: RELAYED_QUOTA_MESSAGE, retryable: true }, |
| 671 | }); |
| 672 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 673 | assert.strictEqual(terminal.failure?.class, "usage_limit"); |
| 674 | assert.strictEqual(terminal.failure?.resetAt, "2026-10-13T10:45:52.000Z"); |
| 675 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 676 | ); |
| 677 | |
| 678 | it.effect("limits a stop Muse types as quota at the reset it reports", () => |
| 679 | Effect.gen(function* () { |
| 680 | const fake = yield* makeFakeMuse(); |
| 681 | const harness = yield* makeHarness(fake); |
| 682 | const { nativeId } = yield* startConversation(harness, fake); |
| 683 | yield* fake.emit("turn/completed", { |
| 684 | turnId: nativeId, |
| 685 | terminal: "failed", |
| 686 | error: { |
| 687 | kind: "modelError", |
| 688 | message: "Model request refused.", |
| 689 | quota: { limitKind: "tierLimit", resetsAtMs: SUBSCRIPTION_USAGE.window.resetsAtMs }, |
| 690 | retryable: true, |
| 691 | }, |
| 692 | }); |
| 693 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 694 | assert.strictEqual(terminal.failure?.class, "usage_limit"); |
| 695 | assert.strictEqual(terminal.failure?.resetAt, "2026-10-08T13:19:17.000Z"); |
| 696 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 697 | ); |
| 698 | |
| 699 | it.effect("skips an unreadable usage report without ending the session", () => |
| 700 | Effect.gen(function* () { |
| 701 | const fake = yield* makeFakeMuse(); |
| 702 | const reports: Array<ProviderUsageLimitsReport> = []; |
| 703 | const harness = yield* makeHarness( |
| 704 | fake, |
| 705 | INSTANCE_ID, |
| 706 | undefined, |
| 707 | undefined, |
| 708 | undefined, |
| 709 | runtimePolicy, |
| 710 | { onUsageLimits: (report) => Effect.sync(() => void reports.push(report)) }, |
| 711 | ); |
| 712 | yield* fake.emitHostWide("usage/changed", { window: "not usage" }); |
| 713 | yield* fake.emitHostWide("usage/changed", { |
| 714 | ...SUBSCRIPTION_USAGE, |
| 715 | window: { ...SUBSCRIPTION_USAGE.window, usedPercent: 103, resetsAtMs: 1e16 }, |
| 716 | }); |
| 717 | const { nativeId } = yield* startConversation(harness, fake); |
| 718 | yield* fake.emit("turn/completed", { |
| 719 | turnId: nativeId, |
| 720 | terminal: "failed", |
| 721 | error: { message: QUOTA_MESSAGE }, |
| 722 | }); |
| 723 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 724 | assert.strictEqual(terminal.failure?.class, "usage_limit"); |
| 725 | assert.isUndefined(terminal.failure?.resetAt); |
| 726 | assert.deepStrictEqual(reports, []); |
| 727 | assert.strictEqual(fake.closeCount(), 0); |
| 728 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 729 | ); |
| 730 | |
| 731 | it.effect("moves a resumed session on another model back to the catalog default", () => |
| 732 | Effect.gen(function* () { |
| 733 | const fake = yield* makeFakeMuse(); |
| 734 | const harness = yield* makeHarness( |
| 735 | fake, |
| 736 | INSTANCE_ID, |
| 737 | "saved-session", |
| 738 | undefined, |
| 739 | undefined, |
| 740 | runtimePolicy, |
| 741 | { |
| 742 | modelCatalog: Effect.succeed([ |
| 743 | { |
| 744 | slug: "account-default", |
| 745 | name: "Default", |
| 746 | isCustom: false, |
| 747 | isDefault: true, |
| 748 | capabilities: null, |
| 749 | }, |
| 750 | { slug: MODEL, name: "Other", isCustom: false, isDefault: false, capabilities: null }, |
| 751 | ]), |
| 752 | }, |
| 753 | ); |
| 754 | const input = yield* turnInput(harness.providerThread, 1); |
| 755 | const selection = { ...input.modelSelection, model: MUSE_DEFAULT_MODEL }; |
| 756 | yield* harness.runtime.startTurn({ |
| 757 | ...input, |
| 758 | modelSelection: selection, |
| 759 | appThread: { ...input.appThread, modelSelection: selection }, |
| 760 | }); |
| 761 | const setModel = yield* fake.takeCall("session/setModel"); |
| 762 | assert.deepStrictEqual(setModel.params.model, { |
| 763 | modelId: "account-default", |
| 764 | providerId: "meta", |
| 765 | }); |
| 766 | yield* fake.takeCall("turn/start"); |
| 767 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 768 | ); |
| 769 | |
| 770 | it.effect("settles partial messages and pending approvals before a provider failure", () => |
| 771 | Effect.gen(function* () { |
| 772 | const fake = yield* makeFakeMuse(); |
| 773 | const harness = yield* makeHarness(fake); |
| 774 | const { nativeId } = yield* startConversation(harness, fake); |
| 775 | yield* fake.emit("item/started", { |
| 776 | turnId: nativeId, |
| 777 | item: { |
| 778 | itemId: "partial", |
| 779 | turnId: nativeId, |
| 780 | kind: "agentMessage", |
| 781 | status: "inProgress", |
| 782 | revision: 1, |
| 783 | text: "Partial answer", |
| 784 | }, |
| 785 | }); |
| 786 | yield* fake.emit("approval/requested", approval(nativeId)); |
| 787 | yield* harness.takeEvent( |
| 788 | "runtime_request.updated", |
| 789 | (event) => event.runtimeRequest.status === "pending", |
| 790 | ); |
| 791 | yield* fake.emit("turn/completed", { |
| 792 | turnId: nativeId, |
| 793 | terminal: "failed", |
| 794 | error: { message: "Provider rejected this turn: api_key=abc123" }, |
| 795 | }); |
| 796 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 797 | assert.strictEqual(terminal.status, "failed"); |
| 798 | assert.strictEqual(terminal.threadDisposition, "reusable"); |
| 799 | assert.strictEqual( |
| 800 | terminal.failure?.message, |
| 801 | "Provider rejected this turn: api_key=[REDACTED]", |
| 802 | ); |
| 803 | assert.strictEqual( |
| 804 | harness.allEvents.findLast((event) => event.type === "provider_session.updated") |
| 805 | ?.providerSession.lastError, |
| 806 | terminal.failure?.message, |
| 807 | ); |
| 808 | assert.isTrue( |
| 809 | harness.allEvents.some( |
| 810 | (event) => |
| 811 | event.type === "message.updated" && |
| 812 | !event.message.streaming && |
| 813 | event.message.text === "Partial answer", |
| 814 | ), |
| 815 | ); |
| 816 | assert.isTrue( |
| 817 | harness.allEvents.some( |
| 818 | (event) => |
| 819 | event.type === "runtime_request.updated" && event.runtimeRequest.status === "cancelled", |
| 820 | ), |
| 821 | ); |
| 822 | yield* startConversation(harness, fake, 2); |
| 823 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 824 | ); |
| 825 | |
| 826 | it.effect.each(["normal", "inherited pipe"] as const)( |
| 827 | "closes its SDK host exactly once on session teardown with %s shutdown", |
| 828 | (shutdown) => |
| 829 | Effect.gen(function* () { |
| 830 | const fake = yield* makeFakeMuse(); |
| 831 | const pipeClosed = pendingPromise<void>(); |
| 832 | if (shutdown === "inherited pipe") fake.holdClose(pipeClosed.promise); |
| 833 | let hostExited = false; |
| 834 | void fake.host.exited.then(() => { |
| 835 | hostExited = true; |
| 836 | }); |
| 837 | const stopped: Array<{ unit: string; hostExited: boolean }> = []; |
| 838 | const release = makeHarness(fake).pipe( |
| 839 | Effect.scoped, |
| 840 | Effect.provideService(AgentScope, { |
| 841 | ...AgentScope.defaultValue(), |
| 842 | wrap: ({ command, args }) => |
| 843 | Effect.succeed({ command, args, unit: "t3code-muse-test.scope" }), |
| 844 | stop: (unit) => |
| 845 | Effect.sync(() => { |
| 846 | stopped.push({ unit, hostExited }); |
| 847 | pipeClosed.resolve(); |
| 848 | }), |
| 849 | }), |
| 850 | ); |
| 851 | if (shutdown === "inherited pipe") { |
| 852 | yield* Effect.gen(function* () { |
| 853 | const releasing = yield* release.pipe(Effect.forkChild({ startImmediately: true })); |
| 854 | yield* Effect.promise(() => fake.closing); |
| 855 | yield* TestClock.adjust("12999 millis"); |
| 856 | assert.strictEqual(fake.closeCount(), 1); |
| 857 | assert.deepStrictEqual(stopped, []); |
| 858 | yield* TestClock.adjust("1 millis"); |
| 859 | assert.deepStrictEqual( |
| 860 | [...stopped], |
| 861 | [{ unit: "t3code-muse-test.scope", hostExited: false }], |
| 862 | ); |
| 863 | yield* Fiber.join(releasing); |
| 864 | }).pipe(Effect.ensuring(Effect.sync(() => pipeClosed.resolve()))); |
| 865 | } else { |
| 866 | yield* release; |
| 867 | } |
| 868 | assert.strictEqual(fake.closeCount(), 1); |
| 869 | assert.deepStrictEqual(stopped, [ |
| 870 | { unit: "t3code-muse-test.scope", hostExited: shutdown === "normal" }, |
| 871 | ]); |
| 872 | yield* Effect.promise(() => fake.host.exited); |
| 873 | assert.isTrue(hostExited); |
| 874 | }).pipe(Effect.provide(testLayer)), |
| 875 | ); |
| 876 | |
| 877 | it.effect.each([ |
| 878 | { |
| 879 | reason: "another identity", |
| 880 | method: "session/start", |
| 881 | sessionId: "unexpected-session", |
| 882 | initialNativeThreadId: undefined, |
| 883 | history: { items: [] }, |
| 884 | }, |
| 885 | { |
| 886 | reason: "an unavailable projection", |
| 887 | method: "session/resume", |
| 888 | sessionId: "saved-session", |
| 889 | initialNativeThreadId: "saved-session", |
| 890 | history: { items: null, mode: "none", noneReason: "projectionUnavailable" }, |
| 891 | }, |
| 892 | ])( |
| 893 | "closes a newly opened host when native session registration returns $reason", |
| 894 | ({ method, sessionId, initialNativeThreadId, history }) => |
| 895 | Effect.gen(function* () { |
| 896 | const fake = yield* makeFakeMuse(); |
| 897 | fake.queueResponse(method, { |
| 898 | session: { sessionId, modelId: MODEL, activeTurnId: null }, |
| 899 | history, |
| 900 | }); |
| 901 | const result = yield* makeHarness(fake, INSTANCE_ID, initialNativeThreadId).pipe( |
| 902 | Effect.exit, |
| 903 | ); |
| 904 | assert.strictEqual(result._tag, "Failure"); |
| 905 | yield* Effect.promise(() => fake.host.exited); |
| 906 | assert.strictEqual(fake.closeCount(), 1); |
| 907 | assert.strictEqual(fake.calls.filter((call) => call.method === "turn/start").length, 0); |
| 908 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 909 | ); |
| 910 | |
| 911 | it.effect( |
| 912 | "settles an active turn and reaps its scope on raw exit before inherited stdio closes", |
| 913 | () => |
| 914 | Effect.gen(function* () { |
| 915 | const fake = yield* makeFakeMuse(); |
| 916 | const pipeClosed = pendingPromise<void>(); |
| 917 | fake.holdClose(pipeClosed.promise); |
| 918 | const stopped = yield* Deferred.make<void>(); |
| 919 | const units: string[] = []; |
| 920 | let hostExited = false; |
| 921 | let connectionClosed = false; |
| 922 | void fake.host.exited.then(() => { |
| 923 | hostExited = true; |
| 924 | }); |
| 925 | void fake.host.connection.closed.then(() => { |
| 926 | connectionClosed = true; |
| 927 | }); |
| 928 | yield* Effect.gen(function* () { |
| 929 | const harness = yield* makeHarness(fake); |
| 930 | const { nativeId } = yield* startConversation(harness, fake); |
| 931 | yield* fake.emit("approval/requested", approval(nativeId)); |
| 932 | yield* harness.takeEvent( |
| 933 | "runtime_request.updated", |
| 934 | (event) => event.runtimeRequest.status === "pending", |
| 935 | ); |
| 936 | yield* fake.exitProcess(); |
| 937 | yield* Deferred.await(stopped); |
| 938 | assert.deepStrictEqual(units, ["t3code-muse-exit.scope"]); |
| 939 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 940 | assert.strictEqual(terminal.status, "failed"); |
| 941 | assert.strictEqual(terminal.threadDisposition, "broken"); |
| 942 | assert.isFalse(hostExited); |
| 943 | assert.isFalse(connectionClosed); |
| 944 | assert.isTrue( |
| 945 | harness.allEvents.some( |
| 946 | (event) => |
| 947 | event.type === "runtime_request.updated" && |
| 948 | event.runtimeRequest.status === "cancelled", |
| 949 | ), |
| 950 | ); |
| 951 | pipeClosed.resolve(); |
| 952 | yield* fake.disconnect(); |
| 953 | yield* Deferred.await(harness.eventsEnded); |
| 954 | const result = yield* harness.runtime |
| 955 | .startTurn(yield* turnInput(harness.providerThread, 2)) |
| 956 | .pipe(Effect.exit); |
| 957 | assert.strictEqual(result._tag, "Failure"); |
| 958 | assert.strictEqual( |
| 959 | harness.allEvents.filter((event) => event.type === "turn.terminal").length, |
| 960 | 1, |
| 961 | ); |
| 962 | assert.strictEqual(fake.calls.filter((call) => call.method === "turn/start").length, 1); |
| 963 | }).pipe( |
| 964 | Effect.ensuring(Effect.sync(() => pipeClosed.resolve())), |
| 965 | Effect.scoped, |
| 966 | Effect.provideService(AgentScope, { |
| 967 | ...AgentScope.defaultValue(), |
| 968 | wrap: ({ command, args }) => |
| 969 | Effect.succeed({ command, args, unit: "t3code-muse-exit.scope" }), |
| 970 | stop: (unit) => |
| 971 | Effect.gen(function* () { |
| 972 | units.push(unit); |
| 973 | yield* Deferred.succeed(stopped, undefined); |
| 974 | }), |
| 975 | }), |
| 976 | ); |
| 977 | assert.deepStrictEqual(units, ["t3code-muse-exit.scope"]); |
| 978 | }).pipe(Effect.provide(testLayer)), |
| 979 | ); |
| 980 | |
| 981 | it.effect("settles an active turn once when both the connection and host exit unexpectedly", () => |
| 982 | Effect.gen(function* () { |
| 983 | const fake = yield* makeFakeMuse(); |
| 984 | const harness = yield* makeHarness(fake); |
| 985 | const first = yield* startConversation(harness, fake); |
| 986 | yield* fake.emit("item/started", { |
| 987 | turnId: first.nativeId, |
| 988 | item: { |
| 989 | itemId: "background-child", |
| 990 | kind: "subagent", |
| 991 | revision: 1, |
| 992 | status: "inProgress", |
| 993 | turnId: first.nativeId, |
| 994 | subagentId: "subagent-1", |
| 995 | controlStatus: "running", |
| 996 | childSessionId: "child-session-1", |
| 997 | objective: "Finish in the background", |
| 998 | }, |
| 999 | }); |
| 1000 | const child = yield* harness.takeEvent("app_thread.created"); |
| 1001 | yield* fake.emit("turn/completed", { turnId: first.nativeId, terminal: "completed" }); |
| 1002 | yield* harness.takeEvent("turn.terminal"); |
| 1003 | const { nativeId } = yield* startConversation(harness, fake, 2); |
| 1004 | yield* fake.emit("approval/requested", approval(nativeId)); |
| 1005 | yield* harness.takeEvent( |
| 1006 | "runtime_request.updated", |
| 1007 | (event) => event.runtimeRequest.status === "pending", |
| 1008 | ); |
| 1009 | yield* fake.disconnect(); |
| 1010 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1011 | assert.strictEqual(terminal.status, "failed"); |
| 1012 | assert.strictEqual(terminal.threadDisposition, "broken"); |
| 1013 | assert.isTrue( |
| 1014 | harness.allEvents.some( |
| 1015 | (event) => |
| 1016 | event.type === "runtime_request.updated" && event.runtimeRequest.status === "cancelled", |
| 1017 | ), |
| 1018 | ); |
| 1019 | const streamExit = yield* Deferred.await(harness.eventsEnded); |
| 1020 | assert.strictEqual(streamExit._tag, "Failure"); |
| 1021 | assert.isTrue( |
| 1022 | harness.allEvents.some( |
| 1023 | (event) => |
| 1024 | event.type === "subagent.updated" && |
| 1025 | event.subagent.childThreadId === child.appThread.id && |
| 1026 | event.subagent.status === "failed", |
| 1027 | ), |
| 1028 | ); |
| 1029 | assert.isTrue( |
| 1030 | harness.allEvents.some( |
| 1031 | (event) => |
| 1032 | event.type === "node.updated" && |
| 1033 | event.node.threadId === child.appThread.id && |
| 1034 | event.node.kind === "root_turn" && |
| 1035 | event.node.status === "failed", |
| 1036 | ), |
| 1037 | ); |
| 1038 | const result = yield* harness.runtime |
| 1039 | .startTurn(yield* turnInput(harness.providerThread, 3)) |
| 1040 | .pipe(Effect.exit); |
| 1041 | assert.strictEqual(result._tag, "Failure"); |
| 1042 | assert.strictEqual( |
| 1043 | harness.allEvents.filter( |
| 1044 | (event) => event.type === "turn.terminal" && event.runOrdinal === 2, |
| 1045 | ).length, |
| 1046 | 1, |
| 1047 | ); |
| 1048 | assert.strictEqual(fake.calls.filter((call) => call.method === "turn/start").length, 2); |
| 1049 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1050 | ); |
| 1051 | |
| 1052 | it.effect("turns a delivery gap into a broken terminal and rejects subsequent work", () => |
| 1053 | Effect.gen(function* () { |
| 1054 | const fake = yield* makeFakeMuse(); |
| 1055 | const harness = yield* makeHarness(fake); |
| 1056 | yield* startConversation(harness, fake); |
| 1057 | yield* fake.emit("view/gap", {}); |
| 1058 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1059 | assert.strictEqual(terminal.status, "failed"); |
| 1060 | assert.strictEqual(terminal.threadDisposition, "broken"); |
| 1061 | yield* Effect.promise(() => fake.host.exited); |
| 1062 | assert.strictEqual(fake.closeCount(), 1); |
| 1063 | yield* Deferred.await(harness.eventsEnded); |
| 1064 | const result = yield* harness.runtime |
| 1065 | .startTurn(yield* turnInput(harness.providerThread, 2)) |
| 1066 | .pipe(Effect.exit); |
| 1067 | assert.strictEqual(result._tag, "Failure"); |
| 1068 | assert.strictEqual(fake.calls.filter((call) => call.method === "turn/start").length, 1); |
| 1069 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1070 | ); |
| 1071 | |
| 1072 | it.effect("uses the latest native approval requirement and waits for its resolution event", () => |
| 1073 | Effect.gen(function* () { |
| 1074 | const fake = yield* makeFakeMuse(); |
| 1075 | const harness = yield* makeHarness(fake); |
| 1076 | const { nativeId } = yield* startConversation(harness, fake); |
| 1077 | yield* fake.emit("approval/requested", approval(nativeId)); |
| 1078 | const first = yield* harness.takeEvent( |
| 1079 | "runtime_request.updated", |
| 1080 | (event) => event.runtimeRequest.status === "pending", |
| 1081 | ); |
| 1082 | yield* fake.emit("approval/updated", { |
| 1083 | ...approval(nativeId), |
| 1084 | currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 }, |
| 1085 | }); |
| 1086 | const refreshed = yield* harness.takeEvent( |
| 1087 | "runtime_request.updated", |
| 1088 | (event) => event.runtimeRequest.status === "pending", |
| 1089 | ); |
| 1090 | assert.strictEqual(refreshed.runtimeRequest.id, first.runtimeRequest.id); |
| 1091 | yield* harness.runtime.respondToRuntimeRequest({ |
| 1092 | requestId: first.runtimeRequest.id, |
| 1093 | decision: "accept", |
| 1094 | }); |
| 1095 | const decision = yield* fake.takeCall("approval/decide"); |
| 1096 | assert.deepStrictEqual(decision.params.requirementId, { |
| 1097 | approvalId: "approval-1", |
| 1098 | sourceIndex: 1, |
| 1099 | }); |
| 1100 | assert.strictEqual(decision.params.choiceId, "once"); |
| 1101 | assert.isFalse( |
| 1102 | harness.allEvents.some( |
| 1103 | (event) => |
| 1104 | event.type === "runtime_request.updated" && event.runtimeRequest.status === "resolved", |
| 1105 | ), |
| 1106 | ); |
| 1107 | yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: nativeId }); |
| 1108 | const resolved = yield* harness.takeEvent( |
| 1109 | "runtime_request.updated", |
| 1110 | (event) => event.runtimeRequest.status === "resolved", |
| 1111 | ); |
| 1112 | assert.strictEqual(resolved.runtimeRequest.id, first.runtimeRequest.id); |
| 1113 | const repeated = yield* harness.runtime |
| 1114 | .respondToRuntimeRequest({ requestId: first.runtimeRequest.id, decision: "accept" }) |
| 1115 | .pipe(Effect.exit); |
| 1116 | assert.strictEqual(repeated._tag, "Failure"); |
| 1117 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1118 | ); |
| 1119 | |
| 1120 | it.effect("keeps the user's decision when Muse settles before acknowledging it", () => |
| 1121 | Effect.gen(function* () { |
| 1122 | const fake = yield* makeFakeMuse(); |
| 1123 | const harness = yield* makeHarness(fake); |
| 1124 | const { nativeId } = yield* startConversation(harness, fake); |
| 1125 | yield* fake.emit("approval/requested", approval(nativeId)); |
| 1126 | const pending = yield* harness.takeEvent( |
| 1127 | "runtime_request.updated", |
| 1128 | (event) => event.runtimeRequest.status === "pending", |
| 1129 | ); |
| 1130 | const acknowledgement = pendingPromise<Record<string, unknown>>(); |
| 1131 | fake.queueResponse("approval/decide", acknowledgement.promise); |
| 1132 | const responding = yield* harness.runtime |
| 1133 | .respondToRuntimeRequest({ requestId: pending.runtimeRequest.id, decision: "decline" }) |
| 1134 | .pipe(Effect.forkScoped); |
| 1135 | yield* fake.takeCall("approval/decide"); |
| 1136 | yield* fake.emit("approval/resolved", { |
| 1137 | approvalId: "approval-1", |
| 1138 | turnId: nativeId, |
| 1139 | decision: "abort", |
| 1140 | }); |
| 1141 | const resolved = yield* harness.takeEvent( |
| 1142 | "runtime_request.updated", |
| 1143 | (event) => event.runtimeRequest.status === "resolved", |
| 1144 | ); |
| 1145 | assert.strictEqual(resolved.runtimeRequest.decision, "decline"); |
| 1146 | acknowledgement.resolve({}); |
| 1147 | yield* Fiber.join(responding); |
| 1148 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1149 | ); |
| 1150 | |
| 1151 | it.effect("lets a user turn take a held Muse report turn, approvals included", () => |
| 1152 | Effect.gen(function* () { |
| 1153 | const fake = yield* makeFakeMuse(); |
| 1154 | const offers: Array<ProviderContinuationRequests.ProviderContinuationRequest> = []; |
| 1155 | const harness = yield* makeHarness( |
| 1156 | fake, |
| 1157 | INSTANCE_ID, |
| 1158 | undefined, |
| 1159 | undefined, |
| 1160 | undefined, |
| 1161 | runtimePolicy, |
| 1162 | { continuationRequests: { offer: (request) => Effect.sync(() => offers.push(request)) } }, |
| 1163 | ); |
| 1164 | yield* fake.emit("turn/started", { turnId: "report-1" }); |
| 1165 | yield* Effect.yieldNow; |
| 1166 | assert.lengthOf(offers, 1); |
| 1167 | const { nativeId } = yield* startConversation(harness, fake); |
| 1168 | yield* fake.emit("approval/requested", approval("report-1")); |
| 1169 | const asked = yield* harness.takeEvent( |
| 1170 | "runtime_request.updated", |
| 1171 | (event) => event.runtimeRequest.status === "pending", |
| 1172 | ); |
| 1173 | yield* harness.runtime.respondToRuntimeRequest({ |
| 1174 | requestId: asked.runtimeRequest.id, |
| 1175 | decision: "accept", |
| 1176 | }); |
| 1177 | yield* fake.takeCall("approval/decide"); |
| 1178 | yield* fake.emit("approval/resolved", { approvalId: "approval-1", turnId: "report-1" }); |
| 1179 | yield* fake.emit("turn/completed", { turnId: "report-1", terminal: "completed" }); |
| 1180 | yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" }); |
| 1181 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1182 | assert.strictEqual(terminal.status, "completed"); |
| 1183 | const dispatched = yield* offers[0]!.dispatchIfCurrent!(Effect.succeed("run")); |
| 1184 | assert.isTrue(Option.isNone(dispatched)); |
| 1185 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1186 | ); |
| 1187 | |
| 1188 | it.effect.each([ |
| 1189 | { when: "after its turn ended", turnEnded: true }, |
| 1190 | { when: "while its turn runs", turnEnded: false }, |
| 1191 | ] as const)( |
| 1192 | "hides a workflow child's automatic approval $when and retains completed history", |
| 1193 | ({ turnEnded }) => |
| 1194 | Effect.gen(function* () { |
| 1195 | const fake = yield* makeFakeMuse(); |
| 1196 | const harness = yield* makeHarness(fake); |
| 1197 | const { nativeId } = yield* startConversation(harness, fake); |
| 1198 | if (turnEnded) { |
| 1199 | yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" }); |
| 1200 | yield* harness.takeEvent("turn.terminal"); |
| 1201 | } |
| 1202 | const acknowledgement = pendingPromise<Record<string, unknown>>(); |
| 1203 | fake.queueResponse("approval/decide", acknowledgement.promise); |
| 1204 | const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } }; |
| 1205 | const before = harness.allEvents.length; |
| 1206 | yield* fake.emit("approval/requested", child); |
| 1207 | const decision = yield* fake.takeCall("approval/decide"); |
| 1208 | assert.strictEqual(decision.params.choiceId, "once"); |
| 1209 | const snapshot = yield* harness.runtime.readThreadSnapshot!({ |
| 1210 | providerThread: harness.providerThread, |
| 1211 | }); |
| 1212 | assert.deepStrictEqual(snapshot.runtimeRequests, []); |
| 1213 | assert.deepStrictEqual(harness.allEvents.slice(before), []); |
| 1214 | if (!turnEnded) { |
| 1215 | const parent = { |
| 1216 | ...approval(nativeId), |
| 1217 | approvalId: "parent-approval", |
| 1218 | currentRequirementId: { approvalId: "parent-approval", sourceIndex: 0 }, |
| 1219 | }; |
| 1220 | yield* fake.emit("approval/requested", parent); |
| 1221 | const shown = yield* harness.takeEvent("runtime_request.updated"); |
| 1222 | assert.strictEqual(shown.runtimeRequest.status, "pending"); |
| 1223 | yield* harness.takeEvent("provider_session.updated"); |
| 1224 | yield* fake.emit("approval/resolved", parent); |
| 1225 | // The child remains unacknowledged when the visible request stops blocking the turn. |
| 1226 | const session = yield* harness.takeEvent("provider_session.updated"); |
| 1227 | assert.strictEqual(session.providerSession.status, "running"); |
| 1228 | } |
| 1229 | yield* fake.emit("approval/resolved", child); |
| 1230 | const resolved = yield* harness.takeEvent("runtime_request.updated"); |
| 1231 | assert.strictEqual(resolved.runtimeRequest.status, "resolved"); |
| 1232 | assert.strictEqual(resolved.runtimeRequest.decision, "accept"); |
| 1233 | assert.isNull(resolved.runtimeRequest.providerTurnId); |
| 1234 | const node = yield* harness.takeEvent("node.updated"); |
| 1235 | const item = yield* harness.takeEvent("turn_item.updated"); |
| 1236 | assert.strictEqual(node.node.status, "completed"); |
| 1237 | assert.strictEqual(item.turnItem.status, "completed"); |
| 1238 | acknowledgement.resolve({}); |
| 1239 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1240 | ); |
| 1241 | |
| 1242 | it.effect("reveals only the current failed child approval and sends the user's decline", () => |
| 1243 | Effect.gen(function* () { |
| 1244 | const fake = yield* makeFakeMuse(); |
| 1245 | const harness = yield* makeHarness(fake); |
| 1246 | yield* startConversation(harness, fake); |
| 1247 | const outdated = pendingPromise<Record<string, unknown>>(); |
| 1248 | const current = pendingPromise<Record<string, unknown>>(); |
| 1249 | fake.queueResponse("approval/decide", outdated.promise); |
| 1250 | fake.queueResponse("approval/decide", current.promise); |
| 1251 | const child = { |
| 1252 | ...approval("child-run-1"), |
| 1253 | subagentOrigin: { |
| 1254 | subagentId: "a7", |
| 1255 | childRunId: "child-run-1", |
| 1256 | childSessionId: "child-session", |
| 1257 | }, |
| 1258 | }; |
| 1259 | yield* fake.emit("approval/requested", child); |
| 1260 | yield* fake.takeCall("approval/decide"); |
| 1261 | yield* fake.emit("approval/updated", { |
| 1262 | approvalId: child.approvalId, |
| 1263 | availableChoices: child.availableChoices, |
| 1264 | subject: child.subject, |
| 1265 | subagentOrigin: child.subagentOrigin, |
| 1266 | currentRequirementId: { approvalId: "approval-1", sourceIndex: 1 }, |
| 1267 | }); |
| 1268 | const second = yield* fake.takeCall("approval/decide"); |
| 1269 | assert.deepStrictEqual(second.params.requirementId, { |
| 1270 | approvalId: "approval-1", |
| 1271 | sourceIndex: 1, |
| 1272 | }); |
| 1273 | outdated.reject( |
| 1274 | new MspError({ |
| 1275 | code: -32053, |
| 1276 | message: "Stale requirement", |
| 1277 | data: { kind: "approvalRequirementStale" }, |
| 1278 | }), |
| 1279 | ); |
| 1280 | current.reject( |
| 1281 | new MspError({ |
| 1282 | code: -32052, |
| 1283 | message: "Choice rejected", |
| 1284 | data: { kind: "approvalChoiceInvalid" }, |
| 1285 | }), |
| 1286 | ); |
| 1287 | const asked = yield* harness.takeEvent("runtime_request.updated"); |
| 1288 | assert.strictEqual(asked.runtimeRequest.status, "pending"); |
| 1289 | const waiting = yield* harness.takeEvent("provider_session.updated"); |
| 1290 | assert.strictEqual(waiting.providerSession.status, "waiting"); |
| 1291 | const snapshot = yield* harness.runtime.readThreadSnapshot!({ |
| 1292 | providerThread: harness.providerThread, |
| 1293 | }); |
| 1294 | assert.deepStrictEqual(snapshot.runtimeRequests, [asked.runtimeRequest]); |
| 1295 | assert.lengthOf( |
| 1296 | harness.allEvents.filter((event) => event.type === "runtime_request.updated"), |
| 1297 | 1, |
| 1298 | ); |
| 1299 | yield* harness.runtime.respondToRuntimeRequest({ |
| 1300 | requestId: asked.runtimeRequest.id, |
| 1301 | decision: "decline", |
| 1302 | }); |
| 1303 | const decline = yield* fake.takeCall("approval/decide"); |
| 1304 | assert.strictEqual(decline.params.choiceId, "deny"); |
| 1305 | assert.deepStrictEqual(decline.params.requirementId, second.params.requirementId); |
| 1306 | yield* fake.emit("approval/resolved", child); |
| 1307 | const resolved = yield* harness.takeEvent("runtime_request.updated"); |
| 1308 | assert.strictEqual(resolved.runtimeRequest.decision, "decline"); |
| 1309 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1310 | ); |
| 1311 | |
| 1312 | it.effect("does not resurrect a settled child approval when its automatic answer fails", () => |
| 1313 | Effect.gen(function* () { |
| 1314 | const fake = yield* makeFakeMuse(); |
| 1315 | const harness = yield* makeHarness(fake); |
| 1316 | yield* startConversation(harness, fake); |
| 1317 | const acknowledgement = pendingPromise<Record<string, unknown>>(); |
| 1318 | fake.queueResponse("approval/decide", acknowledgement.promise); |
| 1319 | const child = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } }; |
| 1320 | yield* fake.emit("approval/requested", child); |
| 1321 | yield* fake.takeCall("approval/decide"); |
| 1322 | yield* fake.emit("approval/resolved", child); |
| 1323 | const resolved = yield* harness.takeEvent("runtime_request.updated"); |
| 1324 | assert.strictEqual(resolved.runtimeRequest.status, "resolved"); |
| 1325 | acknowledgement.reject( |
| 1326 | new MspError({ |
| 1327 | code: -32051, |
| 1328 | message: "Already resolved", |
| 1329 | data: { kind: "approvalAlreadyResolved" }, |
| 1330 | }), |
| 1331 | ); |
| 1332 | // A later rejected answer provides an observable milestone after the stale rejection. |
| 1333 | fake.queueResponse("approval/decide", new Error("Choice rejected")); |
| 1334 | yield* fake.emit("approval/requested", { |
| 1335 | ...child, |
| 1336 | approvalId: "approval-2", |
| 1337 | currentRequirementId: { approvalId: "approval-2", sourceIndex: 0 }, |
| 1338 | }); |
| 1339 | const asked = yield* harness.takeEvent("runtime_request.updated"); |
| 1340 | assert.strictEqual(asked.runtimeRequest.nativeRequestRef?.nativeId, "approval-2"); |
| 1341 | assert.deepStrictEqual( |
| 1342 | harness.allEvents.flatMap((event) => |
| 1343 | event.type === "runtime_request.updated" ? [event.runtimeRequest.status] : [], |
| 1344 | ), |
| 1345 | ["resolved", "pending"], |
| 1346 | ); |
| 1347 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1348 | ); |
| 1349 | |
| 1350 | it.effect("keeps a workflow child's approval pending for the user past the turn's end", () => |
| 1351 | Effect.gen(function* () { |
| 1352 | const fake = yield* makeFakeMuse(); |
| 1353 | const policy = ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({ |
| 1354 | ...runtimePolicy, |
| 1355 | runtimeMode: "approval-required", |
| 1356 | }); |
| 1357 | const harness = yield* makeHarness( |
| 1358 | fake, |
| 1359 | INSTANCE_ID, |
| 1360 | undefined, |
| 1361 | undefined, |
| 1362 | undefined, |
| 1363 | policy, |
| 1364 | ); |
| 1365 | const { nativeId } = yield* startConversation(harness, fake); |
| 1366 | const childApproval = { ...approval("child-run-1"), subagentOrigin: { subagentId: "a7" } }; |
| 1367 | yield* fake.emit("approval/requested", childApproval); |
| 1368 | const asked = yield* harness.takeEvent("runtime_request.updated"); |
| 1369 | assert.isNull(asked.runtimeRequest.providerTurnId); |
| 1370 | yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" }); |
| 1371 | yield* harness.takeEvent("turn.terminal"); |
| 1372 | assert.isFalse( |
| 1373 | harness.allEvents.some( |
| 1374 | (event) => |
| 1375 | event.type === "runtime_request.updated" && event.runtimeRequest.status !== "pending", |
| 1376 | ), |
| 1377 | ); |
| 1378 | yield* harness.runtime.respondToRuntimeRequest({ |
| 1379 | requestId: asked.runtimeRequest.id, |
| 1380 | decision: "decline", |
| 1381 | }); |
| 1382 | const decision = yield* fake.takeCall("approval/decide"); |
| 1383 | assert.strictEqual(decision.params.choiceId, "deny"); |
| 1384 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1385 | ); |
| 1386 | |
| 1387 | it.effect("validates structured answers and preserves selected labels with a custom note", () => |
| 1388 | Effect.gen(function* () { |
| 1389 | const fake = yield* makeFakeMuse(); |
| 1390 | const harness = yield* makeHarness(fake); |
| 1391 | const { nativeId } = yield* startConversation(harness, fake); |
| 1392 | yield* fake.emit("userInput/requested", { |
| 1393 | userInputId: "question-1", |
| 1394 | turnId: nativeId, |
| 1395 | questions: [ |
| 1396 | { |
| 1397 | id: "approach", |
| 1398 | header: "Approach", |
| 1399 | question: "Which approach?", |
| 1400 | options: [{ label: "Small" }, { label: "Large" }], |
| 1401 | selection: { mode: "single" }, |
| 1402 | }, |
| 1403 | ], |
| 1404 | }); |
| 1405 | const pending = yield* harness.takeEvent( |
| 1406 | "runtime_request.updated", |
| 1407 | (event) => event.runtimeRequest.kind === "user_input", |
| 1408 | ); |
| 1409 | const invalid = yield* harness.runtime |
| 1410 | .respondToRuntimeRequest({ |
| 1411 | requestId: pending.runtimeRequest.id, |
| 1412 | answers: { approach: ["Small", "Large"] }, |
| 1413 | }) |
| 1414 | .pipe(Effect.exit); |
| 1415 | assert.strictEqual(invalid._tag, "Failure"); |
| 1416 | assert.isFalse(fake.calls.some((call) => call.method === "userInput/answer")); |
| 1417 | yield* harness.runtime.respondToRuntimeRequest({ |
| 1418 | requestId: pending.runtimeRequest.id, |
| 1419 | answers: { approach: ["Small", "Keep tests focused"] }, |
| 1420 | }); |
| 1421 | const response = yield* fake.takeCall("userInput/answer"); |
| 1422 | assert.deepStrictEqual(response.params.answers, [ |
| 1423 | { questionId: "approach", selectedLabel: "Small", note: "Keep tests focused" }, |
| 1424 | ]); |
| 1425 | yield* fake.emit("userInput/settled", { |
| 1426 | userInputId: "question-1", |
| 1427 | turnId: nativeId, |
| 1428 | answers: response.params.answers, |
| 1429 | }); |
| 1430 | yield* harness.takeEvent( |
| 1431 | "runtime_request.updated", |
| 1432 | (event) => event.runtimeRequest.status === "resolved", |
| 1433 | ); |
| 1434 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1435 | ); |
| 1436 | |
| 1437 | it.effect("steers only the active native turn and waits for confirmed interruption", () => |
| 1438 | Effect.gen(function* () { |
| 1439 | const fake = yield* makeFakeMuse(); |
| 1440 | const harness = yield* makeHarness(fake); |
| 1441 | const turn = yield* startConversation(harness, fake); |
| 1442 | const input = yield* turnInput(harness.providerThread); |
| 1443 | const invalid = yield* harness.runtime.steerTurn!({ |
| 1444 | threadId: THREAD_ID, |
| 1445 | runId: input.runId, |
| 1446 | providerThread: harness.providerThread, |
| 1447 | providerTurnId: ProviderTurnId.make("unrelated-turn"), |
| 1448 | message: input.message, |
| 1449 | }).pipe(Effect.exit); |
| 1450 | assert.strictEqual(invalid._tag, "Failure"); |
| 1451 | assert.isFalse(fake.calls.some((call) => call.method === "turn/steer")); |
| 1452 | yield* harness.runtime.steerTurn!({ |
| 1453 | threadId: THREAD_ID, |
| 1454 | runId: input.runId, |
| 1455 | providerThread: harness.providerThread, |
| 1456 | providerTurnId: turn.providerTurn.id, |
| 1457 | message: { ...input.message, text: "Focus on the parser" }, |
| 1458 | }); |
| 1459 | assert.strictEqual((yield* fake.takeCall("turn/steer")).params.expectedTurnId, turn.nativeId); |
| 1460 | const interrupt = yield* harness.runtime |
| 1461 | .interruptTurn({ |
| 1462 | providerThread: harness.providerThread, |
| 1463 | providerTurnId: turn.providerTurn.id, |
| 1464 | }) |
| 1465 | .pipe(Effect.forkScoped); |
| 1466 | assert.strictEqual((yield* fake.takeCall("turn/interrupt")).params.turnId, turn.nativeId); |
| 1467 | yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "cancelled" }); |
| 1468 | yield* Fiber.join(interrupt); |
| 1469 | assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "interrupted"); |
| 1470 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1471 | ); |
| 1472 | |
| 1473 | it.effect("resumes the requested native session and reads the same session snapshot", () => |
| 1474 | Effect.gen(function* () { |
| 1475 | const fake = yield* makeFakeMuse(); |
| 1476 | const nativeItem: MuseItem = { |
| 1477 | itemId: "prior-message", |
| 1478 | turnId: "prior-turn", |
| 1479 | kind: "agentMessage", |
| 1480 | status: "completed", |
| 1481 | revision: 1, |
| 1482 | text: "Previous answer", |
| 1483 | }; |
| 1484 | fake.setHistory([nativeItem]); |
| 1485 | fake.queueResponse("session/resume", { |
| 1486 | session: { sessionId: "saved-session", modelId: MODEL, activeTurnId: null }, |
| 1487 | viewCursor: "", |
| 1488 | history: { items: null, mode: "none", noneReason: "excluded" }, |
| 1489 | }); |
| 1490 | const harness = yield* makeHarness(fake, INSTANCE_ID, "saved-session"); |
| 1491 | const resumed = yield* fake.takeCall("session/resume"); |
| 1492 | assert.strictEqual(resumed.params.sessionId, "saved-session"); |
| 1493 | assert.strictEqual(resumed.params.excludeItems, true); |
| 1494 | assert.isFalse(fake.calls.some((call) => call.method === "session/start")); |
| 1495 | assert.strictEqual(harness.providerThread.nativeThreadRef?.nativeId, "saved-session"); |
| 1496 | const snapshot = yield* harness.runtime.readThreadSnapshot!({ |
| 1497 | providerThread: harness.providerThread, |
| 1498 | }); |
| 1499 | assert.strictEqual(snapshot.providerThread.nativeThreadRef?.nativeId, "saved-session"); |
| 1500 | assert.deepStrictEqual(snapshot.providerPayload, [nativeItem]); |
| 1501 | assert.strictEqual(snapshot.messages[0]?.text, "Previous answer"); |
| 1502 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1503 | ); |
| 1504 | |
| 1505 | it.effect.each([ |
| 1506 | { resume: true, label: "resumes the saved session" }, |
| 1507 | { resume: false, label: "starts a fresh session" }, |
| 1508 | ])("completes a turn after an idle host dies and $label", ({ resume }) => |
| 1509 | Effect.gen(function* () { |
| 1510 | const fake = yield* makeFakeMuse("first"); |
| 1511 | const recovery = yield* makeFakeMuse("second"); |
| 1512 | const harness = yield* makeHarness(fake, INSTANCE_ID, "saved-session", recovery); |
| 1513 | yield* fake.disconnect(); |
| 1514 | yield* harness.takeEvent( |
| 1515 | "provider_session.updated", |
| 1516 | (event) => event.providerSession.status === "error", |
| 1517 | ); |
| 1518 | |
| 1519 | const reEnsured = yield* harness.runtime.ensureThread({ |
| 1520 | threadId: THREAD_ID, |
| 1521 | modelSelection: modelSelection(), |
| 1522 | runtimePolicy, |
| 1523 | existingProviderThread: resume |
| 1524 | ? harness.providerThread |
| 1525 | : { ...harness.providerThread, nativeThreadRef: null }, |
| 1526 | }); |
| 1527 | const registered = yield* recovery.takeCall(resume ? "session/resume" : "session/start"); |
| 1528 | assert.strictEqual(registered.params.sessionId, reEnsured.nativeThreadRef?.nativeId); |
| 1529 | assert.strictEqual(reEnsured.id, harness.providerThread.id); |
| 1530 | assert.strictEqual(reEnsured.nativeThreadRef?.nativeId === "saved-session", resume); |
| 1531 | |
| 1532 | yield* harness.runtime.startTurn(yield* turnInput(reEnsured)); |
| 1533 | const started = yield* recovery.takeCall("turn/start"); |
| 1534 | yield* recovery.emit("turn/completed", { |
| 1535 | turnId: started.commandId, |
| 1536 | terminal: "completed", |
| 1537 | }); |
| 1538 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1539 | assert.strictEqual(terminal.status, "completed"); |
| 1540 | assert.strictEqual(terminal.providerThreadId, reEnsured.id); |
| 1541 | assert.strictEqual(terminal.threadDisposition, "reusable"); |
| 1542 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1543 | ); |
| 1544 | |
| 1545 | it.effect("discards a dead host's pending continuation before relaunching", () => |
| 1546 | Effect.gen(function* () { |
| 1547 | const fake = yield* makeFakeMuse("first"); |
| 1548 | const recovery = yield* makeFakeMuse("second"); |
| 1549 | const offered = |
| 1550 | yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>(); |
| 1551 | const harness = yield* makeHarness( |
| 1552 | fake, |
| 1553 | INSTANCE_ID, |
| 1554 | "saved-session", |
| 1555 | recovery, |
| 1556 | undefined, |
| 1557 | runtimePolicy, |
| 1558 | { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } }, |
| 1559 | ); |
| 1560 | yield* fake.emit("turn/started", { turnId: "dead-host-report" }); |
| 1561 | const continuation = yield* Deferred.await(offered); |
| 1562 | assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1563 | yield* fake.disconnect(); |
| 1564 | yield* harness.takeEvent( |
| 1565 | "provider_session.updated", |
| 1566 | (event) => event.providerSession.status === "error", |
| 1567 | ); |
| 1568 | assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1569 | const dispatched = yield* continuation.dispatchIfCurrent!( |
| 1570 | Effect.die("Stale continuation dispatched"), |
| 1571 | ); |
| 1572 | assert.isTrue(Option.isNone(dispatched)); |
| 1573 | |
| 1574 | const reEnsured = yield* harness.runtime.ensureThread({ |
| 1575 | threadId: THREAD_ID, |
| 1576 | modelSelection: modelSelection(), |
| 1577 | runtimePolicy, |
| 1578 | existingProviderThread: harness.providerThread, |
| 1579 | }); |
| 1580 | const input = yield* turnInput(reEnsured); |
| 1581 | yield* harness.runtime.startTurn({ |
| 1582 | ...input, |
| 1583 | message: { ...input.message, createdBy: "agent", creationSource: "provider" }, |
| 1584 | }); |
| 1585 | const running = yield* harness.takeEvent("provider_turn.updated"); |
| 1586 | assert.notStrictEqual(running.providerTurn.nativeTurnRef?.nativeId, "dead-host-report"); |
| 1587 | assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed"); |
| 1588 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1589 | ); |
| 1590 | |
| 1591 | it.effect("serializes interruption behind pending turn admission without losing the target", () => |
| 1592 | Effect.gen(function* () { |
| 1593 | const fake = yield* makeFakeMuse(); |
| 1594 | const harness = yield* makeHarness(fake); |
| 1595 | const admission = pendingPromise<Record<string, unknown>>(); |
| 1596 | fake.queueResponse("turn/start", admission.promise); |
| 1597 | const starting = yield* harness.runtime |
| 1598 | .startTurn(yield* turnInput(harness.providerThread)) |
| 1599 | .pipe(Effect.forkScoped); |
| 1600 | const call = yield* fake.takeCall("turn/start"); |
| 1601 | const running = yield* harness.takeEvent( |
| 1602 | "provider_turn.updated", |
| 1603 | (event) => event.providerTurn.status === "running", |
| 1604 | ); |
| 1605 | const stopping = yield* harness.runtime |
| 1606 | .interruptTurn({ |
| 1607 | providerThread: harness.providerThread, |
| 1608 | providerTurnId: running.providerTurn.id, |
| 1609 | }) |
| 1610 | .pipe(Effect.forkScoped); |
| 1611 | admission.resolve({ turnId: call.commandId }); |
| 1612 | yield* Fiber.join(starting); |
| 1613 | assert.strictEqual((yield* fake.takeCall("turn/interrupt")).params.turnId, call.commandId); |
| 1614 | yield* fake.emit("turn/completed", { turnId: call.commandId, terminal: "cancelled" }); |
| 1615 | yield* Fiber.join(stopping); |
| 1616 | assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "interrupted"); |
| 1617 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1618 | ); |
| 1619 | |
| 1620 | it.effect("keeps an intentionally interrupted compaction closed on ensureThread", () => |
| 1621 | Effect.gen(function* () { |
| 1622 | const fake = yield* makeFakeMuse(); |
| 1623 | const recovery = yield* makeFakeMuse("replacement"); |
| 1624 | const harness = yield* makeHarness(fake, INSTANCE_ID, undefined, recovery); |
| 1625 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
| 1626 | const running = yield* harness.takeEvent("provider_turn.updated"); |
| 1627 | yield* harness.runtime.interruptTurn({ |
| 1628 | providerThread: harness.providerThread, |
| 1629 | providerTurnId: running.providerTurn.id, |
| 1630 | }); |
| 1631 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1632 | assert.strictEqual(terminal.status, "interrupted"); |
| 1633 | assert.strictEqual(terminal.threadDisposition, "broken"); |
| 1634 | assert.strictEqual((yield* Deferred.await(harness.eventsEnded))._tag, "Success"); |
| 1635 | |
| 1636 | const result = yield* harness.runtime |
| 1637 | .ensureThread({ |
| 1638 | threadId: THREAD_ID, |
| 1639 | modelSelection: modelSelection(), |
| 1640 | runtimePolicy, |
| 1641 | existingProviderThread: harness.providerThread, |
| 1642 | }) |
| 1643 | .pipe(Effect.exit); |
| 1644 | assert.strictEqual(result._tag, "Failure"); |
| 1645 | assert.deepStrictEqual(recovery.calls, []); |
| 1646 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1647 | ); |
| 1648 | |
| 1649 | it.effect("reports a no-op compaction as a visible failure and permits another turn", () => |
| 1650 | Effect.gen(function* () { |
| 1651 | const fake = yield* makeFakeMuse(); |
| 1652 | const harness = yield* makeHarness(fake); |
| 1653 | fake.queueResponse("session/compact", { status: "noop", reason: "no_compactable_history" }); |
| 1654 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
| 1655 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1656 | assert.strictEqual(terminal.status, "failed"); |
| 1657 | assert.strictEqual(terminal.threadDisposition, "reusable"); |
| 1658 | yield* startConversation(harness, fake, 2); |
| 1659 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1660 | ); |
| 1661 | |
| 1662 | it.effect("reports a compaction rejected at admission on its own terminal", () => |
| 1663 | Effect.gen(function* () { |
| 1664 | const fake = yield* makeFakeMuse(); |
| 1665 | const harness = yield* makeHarness(fake); |
| 1666 | fake.queueResponse("session/compact", new Error("command rejected: run_active")); |
| 1667 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread)); |
| 1668 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1669 | assert.strictEqual(terminal.status, "failed"); |
| 1670 | assert.strictEqual(terminal.threadDisposition, "reusable"); |
| 1671 | assert.include(terminal.failure?.message, "run_active"); |
| 1672 | yield* startConversation(harness, fake, 2); |
| 1673 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1674 | ); |
| 1675 | |
| 1676 | it.effect("treats recovering an already ended turn as done", () => |
| 1677 | Effect.gen(function* () { |
| 1678 | const fake = yield* makeFakeMuse(); |
| 1679 | const harness = yield* makeHarness(fake); |
| 1680 | const turn = yield* startConversation(harness, fake); |
| 1681 | yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" }); |
| 1682 | assert.strictEqual((yield* harness.takeEvent("turn.terminal")).status, "completed"); |
| 1683 | yield* harness.runtime.interruptTurn({ |
| 1684 | providerThread: harness.providerThread, |
| 1685 | providerTurnId: turn.providerTurn.id, |
| 1686 | requestRuntimeRestart: true, |
| 1687 | }); |
| 1688 | yield* startConversation(harness, fake, 2); |
| 1689 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1690 | ); |
| 1691 | |
| 1692 | it.effect("does not acknowledge Stop on an ended turn while a background subagent runs", () => |
| 1693 | Effect.gen(function* () { |
| 1694 | const fake = yield* makeFakeMuse(); |
| 1695 | const harness = yield* makeHarness(fake); |
| 1696 | const turn = yield* startConversation(harness, fake); |
| 1697 | yield* fake.emit("item/started", { |
| 1698 | turnId: turn.nativeId, |
| 1699 | item: { |
| 1700 | itemId: "background-child", |
| 1701 | kind: "subagent", |
| 1702 | revision: 1, |
| 1703 | status: "inProgress", |
| 1704 | turnId: turn.nativeId, |
| 1705 | subagentId: "subagent-1", |
| 1706 | controlStatus: "running", |
| 1707 | childSessionId: "child-session-1", |
| 1708 | objective: "Finish in the background", |
| 1709 | }, |
| 1710 | }); |
| 1711 | yield* harness.takeEvent("app_thread.created"); |
| 1712 | yield* fake.emit("turn/completed", { turnId: turn.nativeId, terminal: "completed" }); |
| 1713 | yield* harness.takeEvent("turn.terminal"); |
| 1714 | const stopped = yield* harness.runtime |
| 1715 | .interruptTurn({ |
| 1716 | providerThread: harness.providerThread, |
| 1717 | providerTurnId: turn.providerTurn.id, |
| 1718 | requestRuntimeRestart: true, |
| 1719 | }) |
| 1720 | .pipe(Effect.exit); |
| 1721 | assert.strictEqual(stopped._tag, "Failure"); |
| 1722 | assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1723 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1724 | ); |
| 1725 | |
| 1726 | it.effect("ends a compaction whose item names the held wake as its run", () => |
| 1727 | Effect.gen(function* () { |
| 1728 | const fake = yield* makeFakeMuse(); |
| 1729 | const offered = |
| 1730 | yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>(); |
| 1731 | const harness = yield* makeHarness( |
| 1732 | fake, |
| 1733 | INSTANCE_ID, |
| 1734 | undefined, |
| 1735 | undefined, |
| 1736 | undefined, |
| 1737 | runtimePolicy, |
| 1738 | { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } }, |
| 1739 | ); |
| 1740 | const previous = yield* startConversation(harness, fake); |
| 1741 | yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" }); |
| 1742 | yield* harness.takeEvent("turn.terminal"); |
| 1743 | yield* fake.emit("turn/started", { turnId: "background-wake" }); |
| 1744 | yield* Deferred.await(offered); |
| 1745 | yield* fake.emit("turn/completed", { turnId: "background-wake", terminal: "completed" }); |
| 1746 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2)); |
| 1747 | yield* fake.emit("item/completed", { |
| 1748 | item: { |
| 1749 | itemId: "manual-compact", |
| 1750 | kind: "compaction", |
| 1751 | revision: 1, |
| 1752 | status: "completed", |
| 1753 | turnId: "background-wake", |
| 1754 | outcome: "compacted", |
| 1755 | }, |
| 1756 | }); |
| 1757 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1758 | assert.strictEqual(terminal.runOrdinal, 2); |
| 1759 | assert.strictEqual(terminal.status, "completed"); |
| 1760 | assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1761 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1762 | ); |
| 1763 | |
| 1764 | it.effect("drops a held wake when Stop closes the compacting host", () => |
| 1765 | Effect.gen(function* () { |
| 1766 | const fake = yield* makeFakeMuse(); |
| 1767 | const offered = |
| 1768 | yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>(); |
| 1769 | const harness = yield* makeHarness( |
| 1770 | fake, |
| 1771 | INSTANCE_ID, |
| 1772 | undefined, |
| 1773 | undefined, |
| 1774 | undefined, |
| 1775 | runtimePolicy, |
| 1776 | { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } }, |
| 1777 | ); |
| 1778 | const previous = yield* startConversation(harness, fake); |
| 1779 | yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" }); |
| 1780 | yield* harness.takeEvent("turn.terminal"); |
| 1781 | yield* fake.emit("turn/started", { turnId: "background-wake" }); |
| 1782 | const continuation = yield* Deferred.await(offered); |
| 1783 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2)); |
| 1784 | const running = yield* harness.takeEvent( |
| 1785 | "provider_turn.updated", |
| 1786 | (event) => event.providerTurn.status === "running", |
| 1787 | ); |
| 1788 | yield* harness.runtime.interruptTurn({ |
| 1789 | providerThread: harness.providerThread, |
| 1790 | providerTurnId: running.providerTurn.id, |
| 1791 | requestRuntimeRestart: true, |
| 1792 | }); |
| 1793 | assert.isFalse(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1794 | const dispatched = yield* continuation.dispatchIfCurrent!(Effect.void); |
| 1795 | assert.isTrue(Option.isNone(dispatched)); |
| 1796 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1797 | ); |
| 1798 | |
| 1799 | it.effect("keeps a running wake held when Muse declines compaction", () => |
| 1800 | Effect.gen(function* () { |
| 1801 | const fake = yield* makeFakeMuse(); |
| 1802 | const offered = |
| 1803 | yield* Deferred.make<ProviderContinuationRequests.ProviderContinuationRequest>(); |
| 1804 | const harness = yield* makeHarness( |
| 1805 | fake, |
| 1806 | INSTANCE_ID, |
| 1807 | undefined, |
| 1808 | undefined, |
| 1809 | undefined, |
| 1810 | runtimePolicy, |
| 1811 | { continuationRequests: { offer: (request) => Deferred.succeed(offered, request) } }, |
| 1812 | ); |
| 1813 | const previous = yield* startConversation(harness, fake); |
| 1814 | yield* fake.emit("turn/completed", { turnId: previous.nativeId, terminal: "completed" }); |
| 1815 | yield* harness.takeEvent("turn.terminal"); |
| 1816 | yield* fake.emit("turn/started", { turnId: "background-wake" }); |
| 1817 | yield* Deferred.await(offered); |
| 1818 | fake.queueResponse("session/compact", new Error("command rejected: run_active")); |
| 1819 | yield* harness.runtime.compactThread!(yield* turnInput(harness.providerThread, 2)); |
| 1820 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 1821 | assert.strictEqual(terminal.status, "failed"); |
| 1822 | assert.isTrue(yield* harness.runtime.hasPendingBackgroundWork!); |
| 1823 | const stopped = yield* harness.runtime |
| 1824 | .interruptTurn({ |
| 1825 | providerThread: harness.providerThread, |
| 1826 | providerTurnId: terminal.providerTurnId, |
| 1827 | requestRuntimeRestart: true, |
| 1828 | }) |
| 1829 | .pipe(Effect.exit); |
| 1830 | assert.strictEqual(stopped._tag, "Failure"); |
| 1831 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1832 | ); |
| 1833 | |
| 1834 | it.effect("projects native todos with stable identity and removes cancelled steps", () => |
| 1835 | Effect.gen(function* () { |
| 1836 | const fake = yield* makeFakeMuse(); |
| 1837 | const harness = yield* makeHarness(fake); |
| 1838 | const { nativeId } = yield* startConversation(harness, fake); |
| 1839 | yield* fake.emit("item/completed", { |
| 1840 | turnId: nativeId, |
| 1841 | item: { |
| 1842 | itemId: "todo-tool", |
| 1843 | turnId: nativeId, |
| 1844 | kind: "toolCall", |
| 1845 | tool: "write_todos", |
| 1846 | revision: 1, |
| 1847 | status: "completed", |
| 1848 | }, |
| 1849 | }); |
| 1850 | yield* fake.emit("item/started", { |
| 1851 | turnId: nativeId, |
| 1852 | item: { |
| 1853 | itemId: "reminder", |
| 1854 | turnId: nativeId, |
| 1855 | kind: "reminderChild", |
| 1856 | revision: 1, |
| 1857 | status: "inProgress", |
| 1858 | fallbackText: "Reminder child session", |
| 1859 | }, |
| 1860 | }); |
| 1861 | yield* fake.emit("session/todoListChanged", { |
| 1862 | items: [ |
| 1863 | { text: " Investigate ", status: "completed" }, |
| 1864 | { text: "Fix", status: "inProgress" }, |
| 1865 | { text: "Discarded", status: "cancelled" }, |
| 1866 | { text: " ", status: "pending" }, |
| 1867 | ], |
| 1868 | }); |
| 1869 | const first = yield* harness.takeEvent( |
| 1870 | "turn_item.updated", |
| 1871 | (event) => event.turnItem.type === "todo_list", |
| 1872 | ); |
| 1873 | assert.strictEqual(first.turnItem.type, "todo_list"); |
| 1874 | if (first.turnItem.type !== "todo_list") return; |
| 1875 | assert.deepStrictEqual( |
| 1876 | first.turnItem.steps.map(({ text, status }) => ({ text, status })), |
| 1877 | [ |
| 1878 | { text: "Investigate", status: "completed" }, |
| 1879 | { text: "Fix", status: "running" }, |
| 1880 | ], |
| 1881 | ); |
| 1882 | const planId = first.turnItem.planId; |
| 1883 | yield* fake.emit("session/todoListChanged", { |
| 1884 | items: [ |
| 1885 | { text: "Investigate", status: "completed" }, |
| 1886 | { text: "Fix", status: "completed" }, |
| 1887 | ], |
| 1888 | }); |
| 1889 | const next = yield* harness.takeEvent( |
| 1890 | "turn_item.updated", |
| 1891 | (event) => event.turnItem.type === "todo_list", |
| 1892 | ); |
| 1893 | assert.strictEqual(next.turnItem.id, first.turnItem.id); |
| 1894 | assert.strictEqual(next.turnItem.nodeId, first.turnItem.nodeId); |
| 1895 | if (next.turnItem.type !== "todo_list") return; |
| 1896 | assert.strictEqual(next.turnItem.planId, first.turnItem.planId); |
| 1897 | assert.isTrue( |
| 1898 | harness.allEvents.some( |
| 1899 | (event) => |
| 1900 | event.type === "plan.updated" && |
| 1901 | event.plan.id === planId && |
| 1902 | event.plan.status === "completed", |
| 1903 | ), |
| 1904 | ); |
| 1905 | assert.isFalse( |
| 1906 | harness.allEvents.some( |
| 1907 | (event) => event.type === "turn_item.updated" && event.turnItem.type === "dynamic_tool", |
| 1908 | ), |
| 1909 | ); |
| 1910 | assert.isTrue((yield* harness.adapter.getCapabilities()).planning.emitsPlanUpdated); |
| 1911 | assert.isTrue((yield* harness.adapter.getCapabilities()).planning.emitsTodoList); |
| 1912 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1913 | ); |
| 1914 | |
| 1915 | it.effect("keeps each retry chain on one row that settles when the turn moves on", () => |
| 1916 | Effect.gen(function* () { |
| 1917 | const fake = yield* makeFakeMuse(); |
| 1918 | const harness = yield* makeHarness(fake); |
| 1919 | const { nativeId } = yield* startConversation(harness, fake); |
| 1920 | const takeRetryRow = Effect.fnUntraced(function* () { |
| 1921 | const { turnItem } = yield* harness.takeEvent( |
| 1922 | "turn_item.updated", |
| 1923 | (event) => event.turnItem.type === "error", |
| 1924 | ); |
| 1925 | return turnItem.type === "error" ? turnItem : undefined; |
| 1926 | }); |
| 1927 | const retry = Effect.fnUntraced(function* ( |
| 1928 | method: string, |
| 1929 | record: string, |
| 1930 | attempt: number, |
| 1931 | params: Record<string, unknown>, |
| 1932 | ) { |
| 1933 | yield* fake.emit(method, { |
| 1934 | turnId: nativeId, |
| 1935 | attempt, |
| 1936 | nextAttempt: attempt + 1, |
| 1937 | sourceRange: { first: { id: record }, last: { id: record } }, |
| 1938 | ...params, |
| 1939 | }); |
| 1940 | return yield* takeRetryRow(); |
| 1941 | }); |
| 1942 | const timeout = { maxAttempts: 2, retryDelayMs: 1_000, reason: "no first model event" }; |
| 1943 | const turnRetry = yield* retry("turn/retryScheduled", "record-1", 1, timeout); |
| 1944 | assert.strictEqual(turnRetry?.status, "running"); |
| 1945 | assert.strictEqual(turnRetry?.failure.message, "no first model event. Retrying in 1s."); |
| 1946 | assert.deepStrictEqual(turnRetry?.retry, { attempt: 1, maxAttempts: 1, retryDelayMs: 1_000 }); |
| 1947 | const http = { maxAttempts: 10, errorKind: "server", httpStatus: 504, retryDelayMs: 1_000 }; |
| 1948 | const modelRetry = yield* retry("turn/modelRetryScheduled", "record-2", 1, http); |
| 1949 | assert.strictEqual( |
| 1950 | modelRetry?.failure.message, |
| 1951 | "Model request failed (HTTP 504). Retrying in 1s.", |
| 1952 | ); |
| 1953 | const nextChain = yield* retry("turn/retryScheduled", "record-3", 1, timeout); |
| 1954 | assert.strictEqual(new Set([turnRetry?.id, modelRetry?.id, nextChain?.id]).size, 3); |
| 1955 | const sameChain = yield* retry("turn/modelRetryScheduled", "record-4", 2, { |
| 1956 | ...http, |
| 1957 | httpStatus: 429, |
| 1958 | retryDelayMs: 60_000, |
| 1959 | }); |
| 1960 | assert.strictEqual(sameChain?.id, modelRetry?.id); |
| 1961 | assert.deepStrictEqual(sameChain?.retry, { |
| 1962 | attempt: 2, |
| 1963 | maxAttempts: 9, |
| 1964 | retryDelayMs: 60_000, |
| 1965 | }); |
| 1966 | |
| 1967 | yield* fake.emit("item/started", { |
| 1968 | turnId: nativeId, |
| 1969 | item: { |
| 1970 | itemId: "answer", |
| 1971 | turnId: nativeId, |
| 1972 | kind: "agentMessage", |
| 1973 | revision: 1, |
| 1974 | status: "inProgress", |
| 1975 | }, |
| 1976 | }); |
| 1977 | const recovered = [yield* takeRetryRow(), yield* takeRetryRow(), yield* takeRetryRow()]; |
| 1978 | assert.sameMembers( |
| 1979 | recovered.map((row) => `${row?.id} ${row?.status} ${row?.failure.message}`), |
| 1980 | [ |
| 1981 | `${turnRetry?.id} completed no first model event`, |
| 1982 | `${nextChain?.id} completed no first model event`, |
| 1983 | `${sameChain?.id} completed Model request failed (HTTP 429)`, |
| 1984 | ], |
| 1985 | ); |
| 1986 | |
| 1987 | const stalled = yield* retry("turn/modelRetryScheduled", "record-5", 1, http); |
| 1988 | yield* fake.emit("turn/completed", { |
| 1989 | turnId: nativeId, |
| 1990 | terminal: "failed", |
| 1991 | error: { message: "Muse gave up" }, |
| 1992 | }); |
| 1993 | const stopped = yield* takeRetryRow(); |
| 1994 | assert.deepStrictEqual([stopped?.id, stopped?.status], [stalled?.id, "interrupted"]); |
| 1995 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 1996 | ); |
| 1997 | |
| 1998 | it.effect("opens a prompt-and-result child thread for a native subagent", () => |
| 1999 | Effect.gen(function* () { |
| 2000 | const fake = yield* makeFakeMuse(); |
| 2001 | const harness = yield* makeHarness(fake); |
| 2002 | const { nativeId } = yield* startConversation(harness, fake); |
| 2003 | yield* fake.emit("item/started", { |
| 2004 | turnId: nativeId, |
| 2005 | item: { |
| 2006 | itemId: "sub-1", |
| 2007 | kind: "subagent", |
| 2008 | revision: 1, |
| 2009 | status: "inProgress", |
| 2010 | turnId: nativeId, |
| 2011 | subagentId: "subagent-1", |
| 2012 | controlStatus: "running", |
| 2013 | childSessionId: "child-session-1", |
| 2014 | objective: "Check the thing", |
| 2015 | role: "general-purpose", |
| 2016 | }, |
| 2017 | }); |
| 2018 | const created = yield* harness.takeEvent("app_thread.created"); |
| 2019 | const childThreadId = created.appThread.id; |
| 2020 | yield* fake.emit("item/updated", { |
| 2021 | turnId: nativeId, |
| 2022 | item: { |
| 2023 | itemId: "sub-1", |
| 2024 | kind: "subagent", |
| 2025 | revision: 2, |
| 2026 | status: "inProgress", |
| 2027 | turnId: nativeId, |
| 2028 | subagentId: "subagent-1", |
| 2029 | controlStatus: "resultReady", |
| 2030 | childSessionId: "child-session-1", |
| 2031 | objective: "Check the thing", |
| 2032 | result: { summary: "All done" }, |
| 2033 | }, |
| 2034 | }); |
| 2035 | const completed = yield* harness.takeEvent( |
| 2036 | "subagent.updated", |
| 2037 | (event) => event.subagent.status === "completed", |
| 2038 | ); |
| 2039 | assert.strictEqual(completed.subagent.title, created.appThread.title); |
| 2040 | const result = yield* harness.takeEvent( |
| 2041 | "message.updated", |
| 2042 | (event) => event.message.threadId === childThreadId && event.message.role === "assistant", |
| 2043 | ); |
| 2044 | assert.strictEqual(result.message.text, "All done"); |
| 2045 | yield* fake.emit("turn/completed", { turnId: nativeId, terminal: "completed" }); |
| 2046 | const terminal = yield* harness.takeEvent("turn.terminal"); |
| 2047 | assert.strictEqual(terminal.status, "completed"); |
| 2048 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 2049 | ); |
| 2050 | |
| 2051 | it.effect("lists a running subagent under its turn item's native id as Muse reveals it", () => |
| 2052 | Effect.gen(function* () { |
| 2053 | const fake = yield* makeFakeMuse(); |
| 2054 | const harness = yield* makeHarness(fake); |
| 2055 | const { nativeId } = yield* startConversation(harness, fake); |
| 2056 | const revisions = [ |
| 2057 | { revision: 1, status: "inProgress" }, |
| 2058 | { revision: 2, status: "inProgress", subagentId: "" }, |
| 2059 | { revision: 3, status: "inProgress", subagentId: "subagent-1" }, |
| 2060 | { |
| 2061 | revision: 4, |
| 2062 | status: "inProgress", |
| 2063 | subagentId: "subagent-1", |
| 2064 | childSessionId: "child-session-1", |
| 2065 | }, |
| 2066 | { |
| 2067 | revision: 5, |
| 2068 | status: "completed", |
| 2069 | subagentId: "subagent-1", |
| 2070 | childSessionId: "child-session-1", |
| 2071 | }, |
| 2072 | ]; |
| 2073 | const listed: Array<{ nativeId: string | null | undefined; roster: Array<string> }> = []; |
| 2074 | for (const fields of revisions) { |
| 2075 | yield* fake.emit(fields.revision === 1 ? "item/started" : "item/updated", { |
| 2076 | turnId: nativeId, |
| 2077 | item: { itemId: "sub-1", kind: "subagent", turnId: nativeId, ...fields }, |
| 2078 | }); |
| 2079 | const item = yield* harness.takeEvent( |
| 2080 | "turn_item.updated", |
| 2081 | (event) => event.turnItem.type === "subagent", |
| 2082 | ); |
| 2083 | const roster = harness.allEvents.findLast( |
| 2084 | (event) => event.type === "provider_thread.updated", |
| 2085 | ); |
| 2086 | listed.push({ |
| 2087 | nativeId: item.turnItem.nativeItemRef?.nativeId, |
| 2088 | roster: |
| 2089 | roster?.type === "provider_thread.updated" |
| 2090 | ? (roster.providerThread.pendingBackgroundTasks ?? []).map((task) => task.taskId) |
| 2091 | : [], |
| 2092 | }); |
| 2093 | } |
| 2094 | assert.deepStrictEqual(listed, [ |
| 2095 | { nativeId: "sub-1", roster: ["sub-1"] }, |
| 2096 | { nativeId: "sub-1", roster: ["sub-1"] }, |
| 2097 | { nativeId: "subagent-1", roster: ["subagent-1"] }, |
| 2098 | { nativeId: "child-session-1", roster: ["child-session-1"] }, |
| 2099 | { nativeId: "child-session-1", roster: [] }, |
| 2100 | ]); |
| 2101 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 2102 | ); |
| 2103 | |
| 2104 | it.effect("keeps a workflow child's first label after later revisions drop it", () => |
| 2105 | Effect.gen(function* () { |
| 2106 | const fake = yield* makeFakeMuse(); |
| 2107 | const harness = yield* makeHarness(fake); |
| 2108 | const { nativeId } = yield* startConversation(harness, fake); |
| 2109 | const workflowItem = (revision: number, children: unknown) => ({ |
| 2110 | itemId: "wf-1", |
| 2111 | kind: "workflow", |
| 2112 | revision, |
| 2113 | status: "inProgress", |
| 2114 | turnId: nativeId, |
| 2115 | entryId: "Probe workflow", |
| 2116 | children, |
| 2117 | }); |
| 2118 | yield* fake.emit("item/started", { |
| 2119 | turnId: nativeId, |
| 2120 | item: workflowItem(1, [{ childId: "c1", attempt: 1, status: "scheduled", label: "ping" }]), |
| 2121 | }); |
| 2122 | const scheduled = yield* harness.takeEvent("subagent.updated"); |
| 2123 | assert.strictEqual(scheduled.subagent.title, "ping"); |
| 2124 | yield* fake.emit("item/updated", { |
| 2125 | turnId: nativeId, |
| 2126 | item: workflowItem(2, [{ childId: "c1", attempt: 1, status: "started" }]), |
| 2127 | }); |
| 2128 | const started = yield* harness.takeEvent( |
| 2129 | "subagent.updated", |
| 2130 | (event) => event.subagent.status === "running", |
| 2131 | ); |
| 2132 | assert.strictEqual(started.subagent.title, "ping"); |
| 2133 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 2134 | ); |
| 2135 | |
| 2136 | it.effect("shows a workflow child that completed with a failure kind as failed", () => |
| 2137 | Effect.gen(function* () { |
| 2138 | const fake = yield* makeFakeMuse(); |
| 2139 | const harness = yield* makeHarness(fake); |
| 2140 | const { nativeId } = yield* startConversation(harness, fake); |
| 2141 | yield* fake.emit("item/started", { |
| 2142 | turnId: nativeId, |
| 2143 | item: { |
| 2144 | itemId: "wf-1", |
| 2145 | kind: "workflow", |
| 2146 | revision: 1, |
| 2147 | status: "inProgress", |
| 2148 | turnId: nativeId, |
| 2149 | children: [ |
| 2150 | { |
| 2151 | childId: "c1", |
| 2152 | attempt: 1, |
| 2153 | status: "finished", |
| 2154 | terminal: "completed", |
| 2155 | failureKind: "budget_exhausted", |
| 2156 | }, |
| 2157 | ], |
| 2158 | }, |
| 2159 | }); |
| 2160 | const child = yield* harness.takeEvent("subagent.updated"); |
| 2161 | assert.strictEqual(child.subagent.status, "failed"); |
| 2162 | assert.strictEqual(child.subagent.result, "budget_exhausted"); |
| 2163 | }).pipe(Effect.scoped, Effect.provide(testLayer)), |
| 2164 | ); |
| 2165 | }); |