@@ -37,7 +37,6 @@ import {
3737 type OrchestrationV2ProviderThread,
3838 OrchestrationV2UserInputQuestion,
3939 ProjectId,
40- type PromptSuggestion,
4140 ProviderInstanceId,
4241 type ProviderInstanceEnvironment,
4342 ProviderDriverKind,
@@ -92,6 +91,7 @@ import * as ProviderEventLoggers from "@t3tools/provider-core/server/ProviderEve
9291import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
9392import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
9493import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
94+ import * as PromptSuggestions from "../../provider/PromptSuggestions.ts";
9595import * as ClaudeAdapterV2 from "./ClaudeAdapterV2.ts";
9696import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
9797import { ClaudeOrchestratorReplayHarness } from "./ClaudeAdapterV2.testkit.ts";
@@ -3700,16 +3700,17 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37003700 });
37013701
37023702 const makePromptSuggestionHarness = Effect.gen(function* () {
3703- const published: Array<readonly [ThreadId, PromptSuggestion | null]> = [] ;
3703+ const store = yield* PromptSuggestions.make ;
37043704 const harness = yield* makeWakeHarnessWithOptions({
37053705 close: (sdkMessages) => Queue.shutdown(sdkMessages),
3706- promptSuggestions: {
3707- publish: (threadId, suggestion) =>
3708- Effect.sync(() => {
3709- published.push([threadId, suggestion]);
3710- }),
3711- },
3706+ promptSuggestions: store,
37123707 });
3708+ const current = (threadId: ThreadId = harness.threadId) =>
3709+ store.stream(threadId).pipe(
3710+ Stream.take(1),
3711+ Stream.runCollect,
3712+ Effect.map(([suggestion]) => suggestion),
3713+ );
37133714 const now = yield* DateTime.now;
37143715 const startTurn = Effect.fnUntraced(function* (
37153716 ordinal: number,
@@ -3742,25 +3743,21 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37423743 yield* Queue.take(harness.terminalReceipts);
37433744 return input;
37443745 });
3745- const publishedSuggestions = () => published.filter(([, suggestion]) => suggestion !== null);
3746- return { harness, published, publishedSuggestions, startTurn, completeTurn };
3746+ return { harness, store, current, startTurn, completeTurn };
37473747 });
37483748
37493749 it.effect("publishes Claude's suggestion after a completed turn until the next turn starts", () =>
37503750 Effect.gen(function* () {
3751- const { harness, published , completeTurn } = yield* makePromptSuggestionHarness;
3751+ const { harness, current , completeTurn } = yield* makePromptSuggestionHarness;
37523752 const first = yield* completeTurn(1);
37533753 yield* harness.offerAndWait(promptSuggestionFrame);
3754- assert.deepEqual(published.at(-1), [
3755- harness.threadId,
3756- {
3757- id: "00000000-0000-4000-8000-000000000701",
3758- runId: first.runId,
3759- text: "Add a regression test for the sidebar.",
3760- },
3761- ]);
3754+ assert.deepEqual(yield* current(), {
3755+ id: "00000000-0000-4000-8000-000000000701",
3756+ runId: first.runId,
3757+ text: "Add a regression test for the sidebar.",
3758+ });
37623759 yield* completeTurn(2);
3763- assert.deepEqual(published.at(-1), [harness.threadId, null] );
3760+ assert.isNull(yield* current() );
37643761 }).pipe(
37653762 Effect.provide(
37663763 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -3770,12 +3767,11 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37703767
37713768 it.effect("drops a suggestion that arrives once the next turn has started", () =>
37723769 Effect.gen(function* () {
3773- const { harness, publishedSuggestions, startTurn, completeTurn } =
3774- yield* makePromptSuggestionHarness;
3770+ const { harness, current, startTurn, completeTurn } = yield* makePromptSuggestionHarness;
37753771 yield* completeTurn(1);
37763772 yield* startTurn(2);
37773773 yield* harness.offerAndWait(promptSuggestionFrame);
3778- assert.lengthOf(publishedSuggestions(), 0 );
3774+ assert.isNull(yield* current() );
37793775 }).pipe(
37803776 Effect.provide(
37813777 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -3785,11 +3781,11 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37853781
37863782 it.effect("clears a suggestion for another app thread of the session on rollback", () =>
37873783 Effect.gen(function* () {
3788- const { harness, published , completeTurn } = yield* makePromptSuggestionHarness;
3784+ const { harness, current , completeTurn } = yield* makePromptSuggestionHarness;
37893785 const otherThreadId = ThreadId.make("thread-claude-suggestion-other");
37903786 yield* completeTurn(1, otherThreadId);
37913787 yield* harness.offerAndWait(promptSuggestionFrame);
3792- assert.equal(published.at(-1)?.[0], otherThreadId);
3788+ assert.isNotNull(yield* current( otherThreadId) );
37933789 yield* harness.runtime.rollbackThread({
37943790 providerThread: harness.providerThread,
37953791 providerThreadTurns: [],
@@ -3799,7 +3795,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
37993795 appRunOrdinal: 0,
38003796 },
38013797 });
3802- assert.deepEqual(published.at(-1), [ otherThreadId, null] );
3798+ assert.isNull(yield* current( otherThreadId) );
38033799 }).pipe(
38043800 Effect.provide(
38053801 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -3809,11 +3805,10 @@ describe("ClaudeAdapterV2 background wake turns", () => {
38093805
38103806 it.effect("clears the suggestion when the thread rolls back", () =>
38113807 Effect.gen(function* () {
3812- const { harness, published, publishedSuggestions, completeTurn } =
3813- yield* makePromptSuggestionHarness;
3808+ const { harness, current, completeTurn } = yield* makePromptSuggestionHarness;
38143809 yield* completeTurn(1);
38153810 yield* harness.offerAndWait(promptSuggestionFrame);
3816- assert.lengthOf(publishedSuggestions(), 1 );
3811+ assert.isNotNull(yield* current() );
38173812 yield* harness.runtime.rollbackThread({
38183813 providerThread: harness.providerThread,
38193814 providerThreadTurns: [],
@@ -3823,7 +3818,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
38233818 appRunOrdinal: 0,
38243819 },
38253820 });
3826- assert.deepEqual(published.at(-1), [harness.threadId, null] );
3821+ assert.isNull(yield* current() );
38273822 }).pipe(
38283823 Effect.provide(
38293824 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
@@ -3833,20 +3828,19 @@ describe("ClaudeAdapterV2 background wake turns", () => {
38333828
38343829 it.effect("clears the suggestion when a background wake starts and offers none after it", () =>
38353830 Effect.gen(function* () {
3836- const { harness, published, publishedSuggestions, completeTurn } =
3837- yield* makePromptSuggestionHarness;
3831+ const { harness, current, completeTurn } = yield* makePromptSuggestionHarness;
38383832 yield* completeTurn(1);
38393833 yield* harness.offerAndWait(promptSuggestionFrame);
3840- assert.lengthOf(publishedSuggestions(), 1 );
3834+ assert.isNotNull(yield* current() );
38413835 yield* harness.offerAndWait(wakeTurnInit);
3842- assert.deepEqual(published.at(-1), [harness.threadId, null] );
3836+ assert.isNull(yield* current() );
38433837 yield* harness.offerAndWait(
38443838 makeAssistantTextFrame({
38453839 uuid: "00000000-0000-4000-8000-000000000711",
38463840 text: "The background task finished.",
38473841 }),
38483842 );
3849- assert.deepEqual(published.at(-1), [harness.threadId, null] );
3843+ assert.isNull(yield* current() );
38503844 yield* harness.offerAndWait(
38513845 claudeSdkFrame({
38523846 type: "prompt_suggestion",
@@ -3855,7 +3849,40 @@ describe("ClaudeAdapterV2 background wake turns", () => {
38553849 session_id: WAKE_NATIVE_SESSION,
38563850 }),
38573851 );
3858- assert.lengthOf(publishedSuggestions(), 1);
3852+ assert.isNull(yield* current());
3853+ }).pipe(
3854+ Effect.provide(
3855+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3856+ ),
3857+ ),
3858+ );
3859+
3860+ it.effect("leaves a newer run's suggestion from another provider alone", () =>
3861+ Effect.gen(function* () {
3862+ const { harness, store, current, completeTurn } = yield* makePromptSuggestionHarness;
3863+ yield* completeTurn(1);
3864+ // The thread switched providers while this session stayed open.
3865+ const newer = { id: "newer", runId: RunId.make("run-other-provider"), text: "Keep going" };
3866+ yield* store.claim(harness.threadId, newer.runId);
3867+ yield* store.publish(harness.threadId, newer);
3868+ yield* harness.offerAndWait(promptSuggestionFrame);
3869+ assert.deepEqual(yield* current(), newer);
3870+ yield* harness.offerAndWait(wakeTurnInit);
3871+ assert.deepEqual(yield* current(), newer);
3872+ }).pipe(
3873+ Effect.provide(
3874+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3875+ ),
3876+ ),
3877+ );
3878+
3879+ it.effect("releases a turn's claim even when no suggestion arrived", () =>
3880+ Effect.gen(function* () {
3881+ const { harness, store, current, completeTurn } = yield* makePromptSuggestionHarness;
3882+ const first = yield* completeTurn(1);
3883+ yield* harness.offerAndWait(wakeTurnInit);
3884+ yield* store.publish(harness.threadId, { id: "late", runId: first.runId, text: "Late" });
3885+ assert.isNull(yield* current());
38593886 }).pipe(
38603887 Effect.provide(
38613888 Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
0 commit comments