GeezerVison is showing github.com — Merge upstream/main (4ade36518..d7fe47fd0): top-up 2, 2 commits · Coriou/txcode@0027a0d
File treeExpand file treeCollapse file tree Open diff view settings server/src/orchestration/Layers
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.test.ts +187Lines changed: 187 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,6 +20,7 @@ import { | 20 | 20 | type OrchestrationCommand, | 21 | 21 | ProjectId, | 22 | 22 | ProviderItemId, | | 23 | + RuntimeRequestId, | 23 | 24 | type ServerSettings, | 24 | 25 | ThreadId, | 25 | 26 | TurnId, | @@ -30,6 +31,7 @@ import * as Effect from "effect/Effect"; | 30 | 31 | import * as Exit from "effect/Exit"; | 31 | 32 | import * as Layer from "effect/Layer"; | 32 | 33 | import * as ManagedRuntime from "effect/ManagedRuntime"; | | 34 | +import * as Option from "effect/Option"; | 33 | 35 | import * as PubSub from "effect/PubSub"; | 34 | 36 | import * as Scope from "effect/Scope"; | 35 | 37 | import * as Stream from "effect/Stream"; | @@ -372,6 +374,12 @@ describe("ProviderRuntimeIngestion", () => { | 372 | 374 | engine, | 373 | 375 | dispatch, | 374 | 376 | readModel: () => testRuntime.runPromise(snapshotQuery.getSnapshot()), | | 377 | + readThreadShell: () => | | 378 | + testRuntime.runPromise( | | 379 | + snapshotQuery | | 380 | + .getThreadShellById(asThreadId("thread-1")) | | 381 | + .pipe(Effect.map(Option.getOrThrow)), | | 382 | + ), | 375 | 383 | emit: provider.emit, | 376 | 384 | emitAndDrain, | 377 | 385 | sqlCount: sqlCounter.count, | @@ -2346,6 +2354,185 @@ describe("ProviderRuntimeIngestion", () => { | 2346 | 2354 | expect(message?.streaming).toBe(false); | 2347 | 2355 | }); | 2348 | 2356 |
| | 2357 | + function userInputEvent( | | 2358 | + turnId: string, | | 2359 | + requestId: string, | | 2360 | + responseMode?: "message", | | 2361 | + ): ProviderRuntimeEvent { | | 2362 | + return { | | 2363 | + type: "user-input.requested", | | 2364 | + eventId: asEventId(`requested:${requestId}`), | | 2365 | + provider: ProviderDriverKind.make("codex"), | | 2366 | + threadId: asThreadId("thread-1"), | | 2367 | + turnId: asTurnId(turnId), | | 2368 | + requestId: RuntimeRequestId.make(requestId), | | 2369 | + createdAt: "2026-01-01T00:00:01.000Z", | | 2370 | + payload: { | | 2371 | + ...(responseMode ? { responseMode } : {}), | | 2372 | + questions: ["first", "second"].map((id) => ({ | | 2373 | + id, | | 2374 | + header: id, | | 2375 | + question: `Choose ${id}`, | | 2376 | + options: [{ label: "yes", description: "Continue" }], | | 2377 | + multiSelect: false, | | 2378 | + })), | | 2379 | + }, | | 2380 | + }; | | 2381 | + } | | 2382 | + | | 2383 | + it.each(["completed", "interrupted", "failed", "aborted"] as const)( | | 2384 | + "resolves native questions when their turn is %s", | | 2385 | + async (state) => { | | 2386 | + const harness = await createHarness(); | | 2387 | + const request = userInputEvent("question-turn", "question-request"); | | 2388 | + await harness.emitAndDrain([ | | 2389 | + { | | 2390 | + type: "turn.started", | | 2391 | + eventId: asEventId("question-started"), | | 2392 | + provider: request.provider, | | 2393 | + threadId: request.threadId, | | 2394 | + turnId: request.turnId, | | 2395 | + createdAt: request.createdAt, | | 2396 | + }, | | 2397 | + request, | | 2398 | + ]); | | 2399 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true); | | 2400 | + if (state === "interrupted") { | | 2401 | + await harness.dispatch({ | | 2402 | + type: "thread.turn.interrupt", | | 2403 | + commandId: CommandId.make("question-interrupt"), | | 2404 | + threadId: request.threadId, | | 2405 | + createdAt: "2026-01-01T00:00:02.000Z", | | 2406 | + }); | | 2407 | + } | | 2408 | + const completed: ProviderRuntimeEvent = { | | 2409 | + ...(state === "aborted" | | 2410 | + ? ({ type: "turn.aborted", payload: { reason: "Interrupted by user." } } as const) | | 2411 | + : ({ type: "turn.completed", payload: { state } } as const)), | | 2412 | + eventId: asEventId("question-completed"), | | 2413 | + provider: request.provider, | | 2414 | + threadId: request.threadId, | | 2415 | + turnId: request.turnId, | | 2416 | + createdAt: "2026-01-01T00:00:03.000Z", | | 2417 | + }; | | 2418 | + await harness.emitAndDrain([completed]); | | 2419 | + const thread = (await harness.readModel()).threads[0]!; | | 2420 | + expect(thread.session?.activeTurnId).toBeNull(); | | 2421 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false); | | 2422 | + expect( | | 2423 | + thread.activities.filter((activity) => activity.kind === "user-input.resolved"), | | 2424 | + ).toMatchObject([{ turnId: request.turnId, payload: { requestId: request.requestId } }]); | | 2425 | + | | 2426 | + await harness.emitAndDrain([completed]); | | 2427 | + expect( | | 2428 | + (await harness.readModel()).threads[0]!.activities.filter( | | 2429 | + (activity) => activity.kind === "user-input.resolved", | | 2430 | + ), | | 2431 | + ).toHaveLength(1); | | 2432 | + }, | | 2433 | + ); | | 2434 | + | | 2435 | + it("keeps a stale request dismissed when its turn later completes", async () => { | | 2436 | + const harness = await createHarness(); | | 2437 | + const request = userInputEvent("stale-turn", "stale-question"); | | 2438 | + await harness.emitAndDrain([request]); | | 2439 | + await harness.dispatch({ | | 2440 | + type: "thread.activity.append", | | 2441 | + commandId: CommandId.make("stale-question-response"), | | 2442 | + threadId: request.threadId, | | 2443 | + activity: { | | 2444 | + id: asEventId("stale-question-failed"), | | 2445 | + kind: "provider.user-input.respond.failed", | | 2446 | + tone: "error", | | 2447 | + summary: "User input response failed", | | 2448 | + turnId: request.turnId ?? null, | | 2449 | + payload: { | | 2450 | + requestId: request.requestId, | | 2451 | + detail: "Unknown pending user input request", | | 2452 | + }, | | 2453 | + createdAt: "2026-01-01T00:00:02.000Z", | | 2454 | + }, | | 2455 | + createdAt: "2026-01-01T00:00:02.000Z", | | 2456 | + }); | | 2457 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false); | | 2458 | + await harness.emitAndDrain([ | | 2459 | + { | | 2460 | + type: "turn.completed", | | 2461 | + eventId: asEventId("stale-turn-completed"), | | 2462 | + provider: request.provider, | | 2463 | + threadId: request.threadId, | | 2464 | + turnId: request.turnId, | | 2465 | + createdAt: "2026-01-01T00:00:03.000Z", | | 2466 | + payload: { state: "completed" }, | | 2467 | + }, | | 2468 | + ]); | | 2469 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(false); | | 2470 | + expect( | | 2471 | + (await harness.readModel()).threads[0]!.activities.filter( | | 2472 | + (activity) => activity.kind === "user-input.resolved", | | 2473 | + ), | | 2474 | + ).toMatchObject([{ payload: { requestId: request.requestId } }]); | | 2475 | + }); | | 2476 | + | | 2477 | + it("preserves answered questions and leaves newer, child and async questions pending", async () => { | | 2478 | + const harness = await createHarness(); | | 2479 | + const answered = userInputEvent("old-turn", "answered-question"); | | 2480 | + const unresolved = userInputEvent("old-turn", "old-question"); | | 2481 | + const newer = userInputEvent("new-turn", "new-question"); | | 2482 | + const child = userInputEvent("child-turn", "child-question"); | | 2483 | + const asynchronous = userInputEvent("old-turn", "async-question", "message"); | | 2484 | + const answer: ProviderRuntimeEvent = { | | 2485 | + type: "user-input.resolved", | | 2486 | + eventId: asEventId("normal-answer"), | | 2487 | + provider: answered.provider, | | 2488 | + threadId: answered.threadId, | | 2489 | + turnId: answered.turnId, | | 2490 | + requestId: answered.requestId, | | 2491 | + createdAt: "2026-01-01T00:00:02.000Z", | | 2492 | + payload: { answers: { first: "yes", second: "yes" } }, | | 2493 | + }; | | 2494 | + await harness.emitAndDrain([ | | 2495 | + answered, | | 2496 | + unresolved, | | 2497 | + newer, | | 2498 | + child, | | 2499 | + asynchronous, | | 2500 | + answer, | | 2501 | + { | | 2502 | + type: "turn.started", | | 2503 | + eventId: asEventId("new-turn-started"), | | 2504 | + provider: newer.provider, | | 2505 | + threadId: newer.threadId, | | 2506 | + turnId: newer.turnId, | | 2507 | + createdAt: "2026-01-01T00:00:03.000Z", | | 2508 | + }, | | 2509 | + ]); | | 2510 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true); | | 2511 | + await harness.emitAndDrain([ | | 2512 | + { | | 2513 | + type: "turn.completed", | | 2514 | + eventId: asEventId("old-turn-completed"), | | 2515 | + provider: answered.provider, | | 2516 | + threadId: answered.threadId, | | 2517 | + turnId: answered.turnId, | | 2518 | + createdAt: "2026-01-01T00:00:04.000Z", | | 2519 | + payload: { state: "interrupted" }, | | 2520 | + }, | | 2521 | + ]); | | 2522 | + const thread = (await harness.readModel()).threads[0]!; | | 2523 | + expect(thread.session?.activeTurnId).toBe(newer.turnId); | | 2524 | + expect((await harness.readThreadShell()).hasPendingUserInput).toBe(true); | | 2525 | + expect( | | 2526 | + thread.activities.filter((activity) => activity.kind === "user-input.resolved"), | | 2527 | + ).toMatchObject([ | | 2528 | + { | | 2529 | + id: answer.eventId, | | 2530 | + payload: { requestId: answered.requestId, answers: answer.payload.answers }, | | 2531 | + }, | | 2532 | + { turnId: unresolved.turnId, payload: { requestId: unresolved.requestId } }, | | 2533 | + ]); | | 2534 | + }); | | 2535 | + | 2349 | 2536 | it("keeps streaming while an async question is pending", async () => { | 2350 | 2537 | const harness = await createHarness({ serverSettings: { enableLegacyTokenStreaming: true } }); | 2351 | 2538 | const base = { | |
Collapse file Copy file name to clipboardExpand all lines: apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts +48-1Lines changed: 48 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,6 +36,8 @@ import { formatTokens } from "@t3tools/shared/usageFormat"; | 36 | 36 | import { ProviderService } from "../../provider/Services/ProviderService.ts"; | 37 | 37 | import { ProjectionTurnRepository } from "../../persistence/Services/ProjectionTurns.ts"; | 38 | 38 | import { ProjectionTurnRepositoryLive } from "../../persistence/Layers/ProjectionTurns.ts"; | | 39 | +import { ProjectionThreadActivityRepository } from "../../persistence/Services/ProjectionThreadActivities.ts"; | | 40 | +import { ProjectionThreadActivityRepositoryLive } from "../../persistence/Layers/ProjectionThreadActivities.ts"; | 39 | 41 | import * as CheckpointStore from "../../checkpointing/CheckpointStore.ts"; | 40 | 42 | import { OrchestrationEngineService } from "../Services/OrchestrationEngine.ts"; | 41 | 43 | import { ThreadBackgroundLivenessService } from "../ThreadBackgroundLiveness.ts"; | @@ -955,6 +957,7 @@ const make = Effect.gen(function* () { | 955 | 957 | const projectionSnapshotQuery = yield* ProjectionSnapshotQuery; | 956 | 958 | const providerService = yield* ProviderService; | 957 | 959 | const projectionTurnRepository = yield* ProjectionTurnRepository; | | 960 | + const projectionThreadActivityRepository = yield* ProjectionThreadActivityRepository; | 958 | 961 | const serverSettingsService = yield* ServerSettingsService; | 959 | 962 | const checkpointStore = yield* CheckpointStore.CheckpointStore; | 960 | 963 | const providerCommandId = (event: ProviderRuntimeEvent, tag: string) => | @@ -1931,6 +1934,47 @@ const make = Effect.gen(function* () { | 1931 | 1934 | const proposedPlans = detailedThread?.proposedPlans ?? []; | 1932 | 1935 | const turnId = toTurnId(event.turnId); | 1933 | 1936 | if (turnId) { | | 1937 | + const userInputActivities = | | 1938 | + yield* projectionThreadActivityRepository.listUserInputLifecycleByThreadId({ | | 1939 | + threadId: thread.id, | | 1940 | + }); | | 1941 | + const pendingRequestIds = new Set<string>(); | | 1942 | + for (const activity of userInputActivities) { | | 1943 | + const payload = | | 1944 | + typeof activity.payload === "object" && activity.payload !== null | | 1945 | + ? (activity.payload as Record<string, unknown>) | | 1946 | + : null; | | 1947 | + const requestId = payload?.requestId; | | 1948 | + if (typeof requestId !== "string") continue; | | 1949 | + if ( | | 1950 | + activity.kind === "user-input.requested" && | | 1951 | + activity.turnId === turnId && | | 1952 | + payload?.responseMode !== "message" | | 1953 | + ) { | | 1954 | + pendingRequestIds.add(requestId); | | 1955 | + } else if (activity.kind === "user-input.resolved") { | | 1956 | + pendingRequestIds.delete(requestId); | | 1957 | + } | | 1958 | + } | | 1959 | + // A terminal turn cannot accept native callback answers. Message-mode | | 1960 | + // questions may outlive that turn and still accept a later user message. | | 1961 | + for (const requestId of pendingRequestIds) { | | 1962 | + yield* orchestrationEngine.dispatch({ | | 1963 | + type: "thread.activity.append", | | 1964 | + commandId: yield* providerCommandId(event, "terminal-user-input-resolved"), | | 1965 | + threadId: thread.id, | | 1966 | + activity: { | | 1967 | + id: EventId.make(`${event.eventId}:user-input-resolved:${requestId}`), | | 1968 | + createdAt: now, | | 1969 | + tone: "info", | | 1970 | + kind: "user-input.resolved", | | 1971 | + summary: "User input dismissed", | | 1972 | + payload: { requestId }, | | 1973 | + turnId, | | 1974 | + }, | | 1975 | + createdAt: now, | | 1976 | + }); | | 1977 | + } | 1934 | 1978 | const assistantMessageIds = yield* getAssistantMessageIdsForTurn(thread.id, turnId); | 1935 | 1979 | yield* Effect.forEach( | 1936 | 1980 | assistantMessageIds, | @@ -2229,4 +2273,7 @@ const make = Effect.gen(function* () { | 2229 | 2273 | export const ProviderRuntimeIngestionLive = Layer.effect( | 2230 | 2274 | ProviderRuntimeIngestionService, | 2231 | 2275 | make, | 2232 | | -).pipe(Layer.provide(ProjectionTurnRepositoryLive)); | | 2276 | +).pipe( | | 2277 | + Layer.provide(ProjectionTurnRepositoryLive), | | 2278 | + Layer.provide(ProjectionThreadActivityRepositoryLive), | | 2279 | +); |
Collapse file Copy file name to clipboardExpand all lines: apps/web/src/hooks/useCopyToClipboard.test.ts +80-7Lines changed: 80 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,6 +14,7 @@ describe("writeTextToClipboard", () => { | 14 | 14 | it("reports unavailable clipboard support with structural context", async () => { | 15 | 15 | vi.stubGlobal("window", {}); | 16 | 16 | vi.stubGlobal("navigator", {}); | | 17 | + vi.stubGlobal("document", undefined); | 17 | 18 |
| 18 | 19 | const error = await writeTextToClipboard("plan contents", "plan").then( | 19 | 20 | () => undefined, | @@ -27,6 +28,72 @@ describe("writeTextToClipboard", () => { | 27 | 28 | expect((error as Error).message).not.toContain("plan contents"); | 28 | 29 | }); | 29 | 30 |
| | 31 | + it.each(["success", "denied", "throws"] as const)( | | 32 | + "cleans up the Clipboard API fallback when copying %s", | | 33 | + async (result) => { | | 34 | + const focus = vi.fn(); | | 35 | + const restoreFocus = vi.fn(); | | 36 | + const appendChild = vi.fn(); | | 37 | + const execCommand = vi.fn(() => { | | 38 | + if (result === "throws") throw new Error("copy command failed"); | | 39 | + return result === "success"; | | 40 | + }); | | 41 | + const remove = vi.fn(); | | 42 | + const select = vi.fn(); | | 43 | + const setAttribute = vi.fn(); | | 44 | + const setSelectionRange = vi.fn(); | | 45 | + const textarea = { | | 46 | + focus, | | 47 | + remove, | | 48 | + select, | | 49 | + setAttribute, | | 50 | + setSelectionRange, | | 51 | + style: {}, | | 52 | + value: "", | | 53 | + }; | | 54 | + | | 55 | + vi.stubGlobal("window", {}); | | 56 | + vi.stubGlobal("navigator", {}); | | 57 | + vi.stubGlobal("document", { | | 58 | + activeElement: { focus: restoreFocus }, | | 59 | + body: { appendChild }, | | 60 | + createElement: vi.fn(() => textarea), | | 61 | + execCommand, | | 62 | + }); | | 63 | + | | 64 | + const pendingCopy = writeTextToClipboard("remote command", "command"); | | 65 | + // The fallback must run during the original user gesture, before any await. | | 66 | + expect(execCommand).toHaveBeenCalledWith("copy"); | | 67 | + if (result === "success") { | | 68 | + await expect(pendingCopy).resolves.toBe(true); | | 69 | + } else { | | 70 | + await expect(pendingCopy).rejects.toBeInstanceOf(ClipboardApiUnavailableError); | | 71 | + } | | 72 | + | | 73 | + expect(textarea.value).toBe("remote command"); | | 74 | + expect(textarea.style).toMatchObject({ fontSize: "16px" }); | | 75 | + expect(appendChild).toHaveBeenCalledWith(textarea); | | 76 | + expect(focus).toHaveBeenCalledWith({ preventScroll: true }); | | 77 | + expect(select).toHaveBeenCalledOnce(); | | 78 | + expect(setSelectionRange).toHaveBeenCalledWith(0, "remote command".length); | | 79 | + expect(remove).toHaveBeenCalledOnce(); | | 80 | + expect(restoreFocus).toHaveBeenCalledOnce(); | | 81 | + }, | | 82 | + ); | | 83 | + | | 84 | + it("uses the Clipboard API without touching the fallback when it is available", async () => { | | 85 | + const writeText = vi.fn().mockResolvedValue(undefined); | | 86 | + const execCommand = vi.fn(); | | 87 | + vi.stubGlobal("window", {}); | | 88 | + vi.stubGlobal("navigator", { clipboard: { writeText } }); | | 89 | + vi.stubGlobal("document", { execCommand }); | | 90 | + | | 91 | + await expect(writeTextToClipboard("remote command", "command")).resolves.toBe(true); | | 92 | + | | 93 | + expect(writeText).toHaveBeenCalledWith("remote command"); | | 94 | + expect(execCommand).not.toHaveBeenCalled(); | | 95 | + }); | | 96 | + | 30 | 97 | it("preserves the exact clipboard failure without exposing copied contents", async () => { | 31 | 98 | const cause = new Error("browser clipboard failure"); | 32 | 99 | const writeText = vi.fn().mockRejectedValue(cause); | @@ -47,12 +114,18 @@ describe("writeTextToClipboard", () => { | 47 | 114 | expect((error as Error).message).not.toContain("secret clipboard contents"); | 48 | 115 | }); | 49 | 116 |
| 50 | | - it("keeps empty values as a no-op when clipboard support is available", async () => { | 51 | | - const writeText = vi.fn(); | 52 | | - vi.stubGlobal("window", {}); | 53 | | - vi.stubGlobal("navigator", { clipboard: { writeText } }); | | 117 | + it.each([true, false])( | | 118 | + "keeps empty values as a no-op with Clipboard API support: %s", | | 119 | + async (available) => { | | 120 | + const writeText = vi.fn(); | | 121 | + vi.stubGlobal("window", {}); | | 122 | + const execCommand = vi.fn(); | | 123 | + vi.stubGlobal("navigator", available ? { clipboard: { writeText } } : {}); | | 124 | + vi.stubGlobal("document", { execCommand }); | 54 | 125 |
| 55 | | - await expect(writeTextToClipboard("", "plan")).resolves.toBe(false); | 56 | | - expect(writeText).not.toHaveBeenCalled(); | 57 | | - }); | | 126 | + await expect(writeTextToClipboard("", "plan")).resolves.toBe(false); | | 127 | + expect(writeText).not.toHaveBeenCalled(); | | 128 | + expect(execCommand).not.toHaveBeenCalled(); | | 129 | + }, | | 130 | + ); | 58 | 131 | }); |
|