Skip to content

Commit 48421fa

Browse files
Alb11747codexclaude
committed
fix(server): keep prompt suggestions with their current run
A retained provider session can outlive a provider switch. Scope suggestion publication and cleanup to the run that claimed the thread, so late results cannot replace or clear its newer suggestion. Co-Authored-By: GPT-6.1-Sol at high effort <[email protected]> Co-Authored-By: Claude Opus 5.5 at high effort, implementation <[email protected]> Co-Authored-By: GPT-6.1-Sol at xhigh effort, fork audit and final review <[email protected]>
1 parent 7db534b commit 48421fa

6 files changed

Lines changed: 228 additions & 104 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 63 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
9291
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
9392
import type * as ProviderContinuationRequests from "@t3tools/provider-core/server/ProviderContinuationRequests";
9493
import { makeProviderFailure } from "@t3tools/provider-core/server/failure";
94+
import * as PromptSuggestions from "../../provider/PromptSuggestions.ts";
9595
import * as ClaudeAdapterV2 from "./ClaudeAdapterV2.ts";
9696
import * as IdAllocator from "@t3tools/provider-core/server/IdAllocator";
9797
import { 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),

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3611,7 +3611,7 @@ export interface ClaudeAdapterV2Options {
36113611
readonly scopedLimitNames?: Ref.Ref<ClaudeScopedLimitNames>;
36123612
readonly onUsageLimits?: ManagedServerProvider["applyUsageLimits"];
36133613
/** Store for the composer's suggested next prompt; defaults to dropping it. */
3614-
readonly promptSuggestions?: Pick<typeof PromptSuggestions.PromptSuggestions.Service, "publish">;
3614+
readonly promptSuggestions?: Omit<typeof PromptSuggestions.PromptSuggestions.Service, "stream">;
36153615
readonly continuationRequests?: {
36163616
readonly offer: (
36173617
request: ProviderContinuationRequests.ProviderContinuationRequest,
@@ -3631,7 +3631,8 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
36313631
const continuationRequests = adapterOptions.continuationRequests ?? {
36323632
offer: () => Effect.void,
36333633
};
3634-
const promptSuggestions = adapterOptions.promptSuggestions ?? { publish: () => Effect.void };
3634+
const promptSuggestions =
3635+
adapterOptions.promptSuggestions ?? PromptSuggestions.PromptSuggestions.defaultValue();
36353636

36363637
const userInvocableSkillNames = (cwd: string | null) =>
36373638
discoverClaudeSkills(
@@ -3790,15 +3791,18 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
37903791
// Native threads whose last turn completed with nothing after it yet.
37913792
// Only these may take the prompt suggestion that follows a `result`.
37923793
const promptSuggestionEligibleNativeThreads = new Set<string>();
3793-
// The app thread each native thread's published suggestion belongs to;
3794-
// one session can serve several app threads.
3795-
const promptSuggestionThreadByNativeThread = new Map<string, ThreadId>();
3794+
// The app thread and run that last claimed suggestions through each
3795+
// native thread; one session can serve several app threads.
3796+
const promptSuggestionClaimByNativeThread = new Map<
3797+
string,
3798+
{ readonly threadId: ThreadId; readonly runId: RunId }
3799+
>();
37963800
const revokePromptSuggestion = Effect.fnUntraced(function* (nativeThreadId: string) {
37973801
promptSuggestionEligibleNativeThreads.delete(nativeThreadId);
3798-
const threadId = promptSuggestionThreadByNativeThread.get(nativeThreadId);
3799-
if (threadId === undefined) return;
3800-
promptSuggestionThreadByNativeThread.delete(nativeThreadId);
3801-
yield* promptSuggestions.publish(threadId, null);
3802+
const claim = promptSuggestionClaimByNativeThread.get(nativeThreadId);
3803+
if (claim === undefined) return;
3804+
promptSuggestionClaimByNativeThread.delete(nativeThreadId);
3805+
yield* promptSuggestions.clear(claim.threadId, claim.runId);
38023806
});
38033807
// Subagent frames can precede the task_started that registers their
38043808
// subagent (the same race rememberPendingClaudeSubagentLaunch covers).
@@ -7103,7 +7107,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
71037107
promptSuggestionEligibleNativeThreads.has(nativeThreadId) &&
71047108
(yield* Ref.get(activeTurn)) === null
71057109
) {
7106-
promptSuggestionThreadByNativeThread.set(nativeThreadId, settled.input.threadId);
71077110
yield* promptSuggestions.publish(settled.input.threadId, {
71087111
id: message.uuid,
71097112
runId: settled.input.runId,
@@ -9054,7 +9057,11 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
90549057
const startedAt = yield* DateTime.now;
90559058
const nativeThreadId = yield* getNativeThreadId(turnInput.providerThread);
90569059
yield* revokePromptSuggestion(nativeThreadId);
9057-
yield* promptSuggestions.publish(turnInput.threadId, null);
9060+
promptSuggestionClaimByNativeThread.set(nativeThreadId, {
9061+
threadId: turnInput.threadId,
9062+
runId: turnInput.runId,
9063+
});
9064+
yield* promptSuggestions.claim(turnInput.threadId, turnInput.runId);
90589065
const nativeTurnId = `turn:${turnInput.attemptId}`;
90599066
// Fresh for every offer: Claude acks a prompt whose uuid its
90609067
// transcript already holds without running a turn, and a session
@@ -9507,7 +9514,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
95079514

95089515
const closeSession = Effect.fnUntraced(function* () {
95099516
yield* Effect.forEach(
9510-
[...promptSuggestionThreadByNativeThread.keys()],
9517+
[...promptSuggestionClaimByNativeThread.keys()],
95119518
revokePromptSuggestion,
95129519
{ discard: true },
95139520
);

0 commit comments

Comments
 (0)