Skip to content

Commit b027086

Browse files
authored
Merge branch 'main' into t3/fix-mcp-plugin-leak
2 parents a693d77 + 8d11fe1 commit b027086

23 files changed

Lines changed: 673 additions & 84 deletions

‎apps/server/src/cloud/CliTokenManager.test.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as NodeServices from "@effect/platform-node/NodeServices";
22
import { assert, it } from "@effect/vitest";
3+
import * as Cause from "effect/Cause";
34
import * as ConfigProvider from "effect/ConfigProvider";
45
import * as Deferred from "effect/Deferred";
56
import * as Duration from "effect/Duration";
@@ -15,6 +16,8 @@ import * as TestClock from "effect/testing/TestClock";
1516
import * as HttpClient from "effect/http/HttpClient";
1617
import * as HttpClientResponse from "effect/http/HttpClientResponse";
1718

19+
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
20+
import * as ExternalLauncher from "../process/externalLauncher.ts";
1821
import * as CliTokenManager from "./CliTokenManager.ts";
1922

2023
// pk_test_<base64 of "clerk.example.test$">
@@ -164,6 +167,57 @@ const layerDeviceFlow = (server: DeviceFlowServer) =>
164167
const tokenRequests = (requests: ReadonlyArray<RecordedTokenRequest>) =>
165168
requests.filter((request) => request.url.endsWith("/oauth/token"));
166169

170+
// The endpoint sends 200 headers, then the JSON body never finishes.
171+
const layerStalledBody = Layer.succeed(
172+
HttpClient.HttpClient,
173+
HttpClient.make((request) =>
174+
Effect.succeed(
175+
HttpClientResponse.fromWeb(
176+
request,
177+
new Response(
178+
new ReadableStream({
179+
start: (controller) => controller.enqueue(new TextEncoder().encode("{")),
180+
}),
181+
{ status: 200, headers: { "content-type": "application/json" } },
182+
),
183+
),
184+
),
185+
),
186+
);
187+
188+
const expiredStoredToken = new TextEncoder().encode(
189+
JSON.stringify({ accessToken: "expired", refreshToken: "refresh-token-1", expiresAtEpochMs: 0 }),
190+
);
191+
192+
it.effect("a stalled token refresh fails and releases the credential lock", () =>
193+
Effect.gen(function* () {
194+
const manager = yield* CliTokenManager.CloudCliTokenManager;
195+
const refresh = yield* manager.getExisting.pipe(Effect.flip, Effect.forkChild);
196+
const hasCredential = yield* manager.hasCredential.pipe(Effect.forkChild);
197+
198+
yield* TestClock.adjust(Duration.seconds(10));
199+
200+
assert.instanceOf(yield* Fiber.join(refresh), CliTokenManager.CloudCliCredentialRefreshError);
201+
assert.isTrue(yield* Fiber.join(hasCredential));
202+
}).pipe(
203+
Effect.provide(
204+
CliTokenManager.layer.pipe(
205+
Layer.provide(
206+
Layer.mergeAll(
207+
layerStalledBody,
208+
Layer.mock(ServerSecretStore.ServerSecretStore)({
209+
get: () => Effect.succeedSome(expiredStoredToken),
210+
}),
211+
Layer.mock(ExternalLauncher.ExternalLauncher)({}),
212+
NodeServices.layer,
213+
),
214+
),
215+
),
216+
),
217+
provideTestEnv,
218+
),
219+
);
220+
167221
it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) => {
168222
it.effect("requests a device code, shows it, and polls until Clerk grants the token", () =>
169223
Effect.gen(function* () {
@@ -322,4 +376,18 @@ it.layer(NodeServices.layer)("CliTokenManager.deviceAuthorizationLogin", (it) =>
322376
assert.isTrue(isAuthorizationError(result));
323377
}),
324378
);
379+
380+
it.effect("fails instead of hanging when the device authorization response stalls", () =>
381+
Effect.gen(function* () {
382+
const fiber = yield* CliTokenManager.deviceAuthorizationLogin(() => Effect.void).pipe(
383+
Effect.provide(layerStalledBody),
384+
provideTestEnv,
385+
Effect.flip,
386+
Effect.forkChild,
387+
);
388+
yield* TestClock.adjust(Duration.seconds(10));
389+
390+
assert.isTrue(Cause.isTimeoutError(yield* Fiber.join(fiber)));
391+
}),
392+
);
325393
});

‎apps/server/src/cloud/CliTokenManager.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const DEVICE_CODE_GRANT_TYPE = "urn:ietf:params:oauth:grant-type:device_code";
4444
const DEVICE_AUTHORIZATION_DEFAULT_INTERVAL = Duration.seconds(5);
4545
// RFC 8628 §3.5: a slow_down response means "add 5 seconds to the interval".
4646
const DEVICE_AUTHORIZATION_SLOW_DOWN_INCREMENT = Duration.seconds(5);
47+
// Bounds one token or device-authorization request, including reading the
48+
// body. A stalled refresh holds the manager's semaphore and blocks every
49+
// other credential call.
50+
const OAUTH_REQUEST_TIMEOUT = Duration.seconds(10);
4751
const boldTerminalText = (value: string): string => `\u001b[1m${value}\u001b[22m`;
4852

4953
function formatLoopbackAuthorizationPrompt(authorizationUrl: string): string {
@@ -290,11 +294,12 @@ const exchangeToken = Effect.fn("cloud.cli_token.exchange")(function* (
290294
params: Record<string, string>,
291295
) {
292296
const httpClient = (yield* HttpClient.HttpClient).pipe(HttpClient.filterStatusOk);
293-
const response = yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(
297+
return yield* HttpClientRequest.post(metadata.tokenEndpoint).pipe(
294298
HttpClientRequest.bodyUrlParams(params),
295299
httpClient.execute,
300+
Effect.flatMap((response) => readTokenResponse(response, params)),
301+
Effect.timeout(OAUTH_REQUEST_TIMEOUT),
296302
);
297-
return yield* readTokenResponse(response, params);
298303
});
299304

300305
const makePkceRequest = Effect.gen(function* () {
@@ -393,6 +398,7 @@ export const deviceAuthorizationLogin = Effect.fn("cloud.cli_token.device_author
393398
}),
394399
httpClient.execute,
395400
Effect.flatMap(HttpClientResponse.schemaBodyJson(DeviceAuthorizationResponse)),
401+
Effect.timeout(OAUTH_REQUEST_TIMEOUT),
396402
);
397403
// Clerk's advertised lifetime and interval are authoritative.
398404
const expiresIn = Duration.seconds(authorization.expires_in);

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2234,6 +2234,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22342234
readonly apiErrorStatus?: number;
22352235
// null omits the field, as the CLI does on a zero-turn result.
22362236
readonly terminalReason?: SDKResultMessage["terminal_reason"] | null;
2237+
readonly usage?: {
2238+
readonly input_tokens: number;
2239+
readonly output_tokens: number;
2240+
readonly cache_creation_input_tokens: number;
2241+
readonly cache_read_input_tokens: number;
2242+
};
22372243
}) =>
22382244
claudeSdkFrame({
22392245
type: "result",
@@ -2245,7 +2251,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
22452251
result: input.result,
22462252
stop_reason: "end_turn",
22472253
total_cost_usd: 0,
2248-
usage: {
2254+
usage: input.usage ?? {
22492255
input_tokens: 1,
22502256
output_tokens: 1,
22512257
cache_creation_input_tokens: 0,
@@ -2993,6 +2999,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
29932999
uuid: "00000000-0000-4000-8000-000000000901",
29943000
result: "",
29953001
terminalReason,
3002+
usage: {
3003+
input_tokens: 600,
3004+
output_tokens: 40,
3005+
cache_creation_input_tokens: 500,
3006+
cache_read_input_tokens: 2000,
3007+
},
29963008
}),
29973009
);
29983010
if (steered) {
@@ -3002,6 +3014,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
30023014
makeResultFrame({
30033015
uuid: "00000000-0000-4000-8000-000000000902",
30043016
result: "Audit finished after the steer.",
3017+
usage: {
3018+
input_tokens: 3,
3019+
output_tokens: 6,
3020+
cache_creation_input_tokens: 2347,
3021+
cache_read_input_tokens: 9455,
3022+
},
30053023
}),
30063024
);
30073025
}
@@ -3016,6 +3034,25 @@ describe("ClaudeAdapterV2 background wake turns", () => {
30163034
event.turnItem.text === WAKE_ASSISTANT_TEXT,
30173035
),
30183036
);
3037+
// Each result's usage covers only its own segment, so the steered
3038+
// turn bills both segments.
3039+
const completed = harness.events.findLast(
3040+
(event) => event.type === "provider_turn.updated",
3041+
);
3042+
assert.deepEqual(
3043+
completed?.type === "provider_turn.updated"
3044+
? completed.providerTurn.turnTokenUsage
3045+
: undefined,
3046+
{
3047+
usageScope: "main_agent",
3048+
usageStatus: "complete",
3049+
hasSubagents: false,
3050+
inputTokens: 603 + 2847 + 11455,
3051+
outputTokens: 46,
3052+
cachedInputTokens: 11455,
3053+
cacheCreationTokens: 2847,
3054+
},
3055+
);
30193056
}
30203057
assert.lengthOf(harness.terminalEvents(), 1);
30213058
}).pipe(

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,6 +2966,9 @@ interface ActiveClaudeTurnContext {
29662966
readonly rejectedRateLimitTypes: Set<string>;
29672967
readonly rateLimitResetTimes: Map<string, string | null>;
29682968
latestAssistantRateLimited: boolean;
2969+
// Usage of the results that steers ended, oldest first. The final result's
2970+
// usage covers only its own segment, so the turn bills these as well.
2971+
readonly steeredSegmentUsage: Array<SDKResultMessage["usage"]>;
29692972
readonly subagentsByTaskId: Map<string, ActiveClaudeSubagent>;
29702973
readonly subagentsByToolUseId: Map<string, ActiveClaudeSubagent>;
29712974
readonly subagentNodesByTaskId: Map<string, OrchestrationV2ExecutionNode["id"]>;
@@ -5970,6 +5973,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
59705973
input.context.subagentsByTaskId.size > 0 ||
59715974
input.context.subagentsByToolUseId.size > 0,
59725975
input.status,
5976+
input.context.steeredSegmentUsage,
59735977
),
59745978
},
59755979
}),
@@ -7708,6 +7712,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
77087712
const interrupted = (yield* Ref.get(interruptedTurns)).has(context.providerTurnId);
77097713
const wasSteered = (yield* Ref.get(steeredTurns)).has(context.providerTurnId);
77107714
if (!interrupted && wasSteered && isClaudeActiveSteeringAbortResult(message)) {
7715+
context.steeredSegmentUsage.push(message.usage);
77117716
return;
77127717
}
77137718
yield* Ref.update(steeredTurns, (current) => {
@@ -8616,6 +8621,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
86168621
rejectedRateLimitTypes: new Set(),
86178622
rateLimitResetTimes: new Map(),
86188623
latestAssistantRateLimited: false,
8624+
steeredSegmentUsage: [],
86198625
subagentsByTaskId: new Map(),
86208626
subagentsByToolUseId: new Map(),
86218627
subagentNodesByTaskId: new Map(),

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

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3679,6 +3679,94 @@ describe("CodexAdapterV2 post-settle continuation", () => {
36793679
),
36803680
);
36813681

3682+
it.effect("streams a proposed plan a few times a second instead of once per delta", () =>
3683+
Effect.scoped(
3684+
Effect.gen(function* () {
3685+
const scenario = "codex-plan-delta-coalescing";
3686+
const nativeThreadId = `native-${scenario}-thread`;
3687+
const nativeTurnId = `native-${scenario}-turn`;
3688+
const prompt = "Plan the change.";
3689+
// 100 deltas, 20 ms apart: a two second stream.
3690+
const deltas = Array.from({ length: 100 }, (_, index) => `step ${index}\n`);
3691+
const transcript = makeCodexReplayTranscript({
3692+
scenario,
3693+
entries: [
3694+
...codexReplayPreamble({ nativeThreadId, nativeTurnId, prompt }),
3695+
...deltas.map((delta, index): CodexReplay.CodexAppServerReplayEntry => ({
3696+
type: "emit_inbound",
3697+
label: `item/plan/delta/${index}`,
3698+
afterMs: 20,
3699+
frame: {
3700+
method: "item/plan/delta",
3701+
params: { threadId: nativeThreadId, turnId: nativeTurnId, itemId: "plan", delta },
3702+
},
3703+
})),
3704+
{
3705+
type: "emit_inbound",
3706+
label: "item/completed/plan",
3707+
frame: {
3708+
method: "item/completed",
3709+
params: {
3710+
threadId: nativeThreadId,
3711+
turnId: nativeTurnId,
3712+
item: { type: "plan", id: "plan", text: "" },
3713+
},
3714+
},
3715+
},
3716+
{
3717+
type: "emit_inbound",
3718+
label: "turn/completed",
3719+
frame: {
3720+
method: "turn/completed",
3721+
params: {
3722+
threadId: nativeThreadId,
3723+
turn: makeCodexReplayTurn({ id: nativeTurnId, status: "completed" }),
3724+
},
3725+
},
3726+
},
3727+
],
3728+
});
3729+
const harness = yield* makeCodexReplayHarness(transcript);
3730+
yield* harness.runtime.startTurn(
3731+
makeCodexTestTurnInput({
3732+
threadId: harness.threadId,
3733+
providerThread: harness.providerThread,
3734+
now: yield* DateTime.now,
3735+
attemptId: RunAttemptId.make(`attempt-${scenario}`),
3736+
text: prompt,
3737+
}),
3738+
);
3739+
for (let step = 0; step < 200 && harness.terminalEvents().length === 0; step += 1) {
3740+
yield* Effect.yieldNow;
3741+
yield* TestClock.adjust("20 millis");
3742+
}
3743+
yield* harness.firstTerminal;
3744+
3745+
const plans = harness.events.flatMap((event) =>
3746+
event.type === "plan.updated" && event.plan.kind === "proposed_plan" ? [event.plan] : [],
3747+
);
3748+
const planItems = harness.events.flatMap((event) =>
3749+
event.type === "turn_item.updated" && event.turnItem.type === "proposed_plan"
3750+
? [event.turnItem]
3751+
: [],
3752+
);
3753+
// One update per 400 ms of the stream, plus the completion.
3754+
assert.isAtLeast(plans.length, 2);
3755+
assert.isAtMost(plans.length, 7);
3756+
assert.equal(planItems.length, plans.length);
3757+
assert.equal(plans.at(-1)?.markdown, deltas.join(""));
3758+
assert.deepEqual(
3759+
planItems.map((item) => item.streaming),
3760+
[...Array.from({ length: planItems.length - 1 }, () => true), false],
3761+
);
3762+
}).pipe(
3763+
Effect.provide(
3764+
Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
3765+
),
3766+
),
3767+
),
3768+
);
3769+
36823770
const finalAnswerTranscript = (
36833771
scenario: string,
36843772
answers: ReadonlyArray<{

0 commit comments

Comments
 (0)