fix(acp): bind MCP credentials to activated threads
Passes the current target thread MCP configuration through ACP load, resume, and fork requests instead of reusing the credentials captured when the provider process opened. Addresses https://github.com/pingdotgg/t3code/pull/2829#discussion_r3507807130 Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
380845ba7a
commit
b9fc3631c9
3 files changed
+83
-14
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, describe, it } from "@effect/vitest";
|
||||
import {
|
||||
EnvironmentId,
|
||||
MessageId,
|
||||
type ModelSelection,
|
||||
NodeId,
|
||||
@@ -30,6 +31,7 @@ import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
import type * as EffectAcpProtocol from "effect-acp/protocol";
|
||||
|
||||
import { ServerConfig } from "../../config.ts";
|
||||
import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
|
||||
import * as AcpSessionRuntime from "../../provider/acp/AcpSessionRuntime.ts";
|
||||
import { layer as idAllocatorLayer, IdAllocatorV2 } from "../IdAllocator.ts";
|
||||
import {
|
||||
@@ -107,6 +109,21 @@ function rawProtocolMethod(event: EffectAcpProtocol.AcpProtocolLogEvent): string
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function rawProtocolRequest(
|
||||
event: EffectAcpProtocol.AcpProtocolLogEvent,
|
||||
): { readonly method?: unknown; readonly params?: unknown } | undefined {
|
||||
if (event.stage !== "raw" || typeof event.payload !== "string") return undefined;
|
||||
for (const line of event.payload.split("\n")) {
|
||||
const trimmed = line.trim();
|
||||
if (trimmed.length === 0) continue;
|
||||
const decoded = Option.getOrUndefined(decodeUnknownJson(trimmed));
|
||||
if (typeof decoded === "object" && decoded !== null && "method" in decoded) {
|
||||
return decoded;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function makeTurnInput(input: {
|
||||
readonly threadId: ThreadId;
|
||||
readonly providerThread: OrchestrationV2ProviderThread;
|
||||
@@ -175,7 +192,8 @@ describe("AcpAdapterV2", () => {
|
||||
const mockAgentPath = yield* path.fromFileUrl(
|
||||
new URL("../../../scripts/acp-mock-agent.ts", import.meta.url),
|
||||
);
|
||||
const makeRuntime = makeMockRuntime({ childProcessSpawner, mockAgentPath });
|
||||
const protocolEvents = yield* Queue.bounded<EffectAcpProtocol.AcpProtocolLogEvent>(256);
|
||||
const makeRuntime = makeMockRuntime({ childProcessSpawner, mockAgentPath, protocolEvents });
|
||||
|
||||
const instanceId = ProviderInstanceId.make("acp-test");
|
||||
const adapter = makeAcpAdapterV2({
|
||||
@@ -192,6 +210,17 @@ describe("AcpAdapterV2", () => {
|
||||
});
|
||||
const sourceThreadId = ThreadId.make("thread-acp-native-fork-source");
|
||||
const targetThreadId = ThreadId.make("thread-acp-native-fork-target");
|
||||
McpProviderSession.setMcpProviderSession({
|
||||
environmentId: EnvironmentId.make("environment-acp-native-fork"),
|
||||
threadId: targetThreadId,
|
||||
providerSessionId: "mcp-session-acp-native-fork",
|
||||
providerInstanceId: instanceId,
|
||||
endpoint: "http://127.0.0.1:43123/mcp",
|
||||
authorizationHeader: "Bearer target-thread-token",
|
||||
});
|
||||
yield* Effect.addFinalizer(() =>
|
||||
Effect.sync(() => McpProviderSession.clearMcpProviderSession(targetThreadId)),
|
||||
);
|
||||
const runtimePolicy = ProviderAdapterV2RuntimePolicy.make({
|
||||
runtimeMode: "full-access",
|
||||
interactionMode: "default",
|
||||
@@ -217,11 +246,33 @@ describe("AcpAdapterV2", () => {
|
||||
sourceProviderThread,
|
||||
targetThreadId,
|
||||
});
|
||||
const forkRequest = Option.getOrThrow(
|
||||
yield* Stream.fromQueue(protocolEvents).pipe(
|
||||
Stream.filter(
|
||||
(event) =>
|
||||
event.direction === "outgoing" && rawProtocolMethod(event) === "session/fork",
|
||||
),
|
||||
Stream.map(rawProtocolRequest),
|
||||
Stream.runHead,
|
||||
),
|
||||
);
|
||||
|
||||
assert.equal(sourceProviderThread.nativeThreadRef?.nativeId, "mock-session-1");
|
||||
assert.equal(forkedProviderThread.nativeThreadRef?.nativeId, "mock-session-1-fork");
|
||||
assert.equal(forkedProviderThread.appThreadId, targetThreadId);
|
||||
assert.equal(forkedProviderThread.forkedFrom?.providerThreadId, sourceProviderThread.id);
|
||||
assert.deepEqual(forkRequest.params, {
|
||||
sessionId: "mock-session-1",
|
||||
cwd: process.cwd(),
|
||||
mcpServers: [
|
||||
{
|
||||
type: "http",
|
||||
name: "t3-code",
|
||||
url: "http://127.0.0.1:43123/mcp",
|
||||
headers: [{ name: "Authorization", value: "Bearer target-thread-token" }],
|
||||
},
|
||||
],
|
||||
});
|
||||
}).pipe(Effect.provide(testLayer), Effect.scoped),
|
||||
);
|
||||
|
||||
|
||||
@@ -1902,12 +1902,16 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
|
||||
const supportsImagePrompts =
|
||||
started.initializeResult.agentCapabilities?.promptCapabilities?.image === true;
|
||||
|
||||
const activateSession = Effect.fnUntraced(function* (sessionId: string) {
|
||||
const activateSession = Effect.fnUntraced(function* (
|
||||
sessionId: string,
|
||||
threadId: ThreadId,
|
||||
) {
|
||||
const activationOptions = { mcpServers: acpMcpServers(threadId) };
|
||||
if (canLoadSession) {
|
||||
return yield* runtime.loadSession(sessionId);
|
||||
return yield* runtime.loadSession(sessionId, activationOptions);
|
||||
}
|
||||
if (canResumeSession) {
|
||||
return yield* runtime.resumeSession(sessionId);
|
||||
return yield* runtime.resumeSession(sessionId, activationOptions);
|
||||
}
|
||||
return yield* new ProviderAdapterProtocolError({
|
||||
driver,
|
||||
@@ -2123,7 +2127,7 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
|
||||
}
|
||||
const requestedSessionId = nativeThreadId(driver, turnInput.providerThread);
|
||||
if ((yield* Ref.get(activeSessionId)) !== requestedSessionId) {
|
||||
const activated = yield* activateSession(requestedSessionId);
|
||||
const activated = yield* activateSession(requestedSessionId, turnInput.threadId);
|
||||
yield* Ref.set(activeSessionId, activated.sessionId);
|
||||
yield* Ref.set(activeSessionSetup, activated);
|
||||
yield* configureSession(activated, turnInput.modelSelection, turnInput.runtimePolicy);
|
||||
@@ -2330,7 +2334,10 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
|
||||
loadingRole: null,
|
||||
loadingIndex: 0,
|
||||
});
|
||||
const activated = yield* activateSession(sessionId);
|
||||
const activated = yield* activateSession(
|
||||
sessionId,
|
||||
threadInput.providerThread.appThreadId,
|
||||
);
|
||||
yield* Ref.set(activeSessionId, activated.sessionId);
|
||||
yield* Ref.set(activeSessionSetup, activated);
|
||||
const nextSelection = threadInput.modelSelection ?? input.modelSelection;
|
||||
@@ -2453,7 +2460,9 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
|
||||
loadingRole: null,
|
||||
loadingIndex: 0,
|
||||
});
|
||||
const activated = yield* runtime.loadSession(sessionId);
|
||||
const activated = yield* runtime.loadSession(sessionId, {
|
||||
mcpServers: acpMcpServers(snapshotInput.providerThread.appThreadId),
|
||||
});
|
||||
yield* Ref.set(activeSessionId, activated.sessionId);
|
||||
yield* Ref.set(activeSessionSetup, activated);
|
||||
yield* Ref.set(activeSelection, null);
|
||||
@@ -2512,7 +2521,9 @@ export function makeAcpAdapterV2(options: AcpAdapterV2Options): ProviderAdapterV
|
||||
});
|
||||
}
|
||||
const sourceSessionId = nativeThreadId(driver, forkInput.sourceProviderThread);
|
||||
const forked = yield* runtime.forkSession(sourceSessionId);
|
||||
const forked = yield* runtime.forkSession(sourceSessionId, {
|
||||
mcpServers: acpMcpServers(forkInput.targetThreadId),
|
||||
});
|
||||
yield* Ref.set(activeSessionId, forked.sessionId);
|
||||
yield* Ref.set(activeSessionSetup, forked);
|
||||
yield* Ref.set(activeSelection, null);
|
||||
|
||||
@@ -113,6 +113,10 @@ export interface AcpSessionRuntimeStartResult {
|
||||
readonly modelConfigId: string | undefined;
|
||||
}
|
||||
|
||||
export interface AcpSessionActivationOptions {
|
||||
readonly mcpServers?: ReadonlyArray<EffectAcpSchema.McpServer>;
|
||||
}
|
||||
|
||||
export class AcpSessionRuntime extends Context.Service<
|
||||
AcpSessionRuntime,
|
||||
{
|
||||
@@ -206,12 +210,15 @@ export class AcpSessionRuntime extends Context.Service<
|
||||
readonly getConfigOptions: Effect.Effect<ReadonlyArray<EffectAcpSchema.SessionConfigOption>>;
|
||||
readonly loadSession: (
|
||||
sessionId: string,
|
||||
options?: AcpSessionActivationOptions,
|
||||
) => Effect.Effect<AcpSessionRuntimeStartResult, EffectAcpErrors.AcpError>;
|
||||
readonly resumeSession: (
|
||||
sessionId: string,
|
||||
options?: AcpSessionActivationOptions,
|
||||
) => Effect.Effect<AcpSessionRuntimeStartResult, EffectAcpErrors.AcpError>;
|
||||
readonly forkSession: (
|
||||
sessionId: string,
|
||||
options?: AcpSessionActivationOptions,
|
||||
) => Effect.Effect<AcpSessionRuntimeStartResult, EffectAcpErrors.AcpError>;
|
||||
readonly listSessions: (
|
||||
cursor?: string,
|
||||
@@ -827,13 +834,13 @@ export const make = (
|
||||
}),
|
||||
getModeState: Ref.get(modeStateRef),
|
||||
getConfigOptions: Ref.get(configOptionsRef),
|
||||
loadSession: (sessionId) =>
|
||||
loadSession: (sessionId, activationOptions) =>
|
||||
start.pipe(
|
||||
Effect.flatMap(() => {
|
||||
const requestPayload = {
|
||||
sessionId,
|
||||
cwd: options.cwd,
|
||||
mcpServers: options.mcpServers ?? [],
|
||||
mcpServers: activationOptions?.mcpServers ?? options.mcpServers ?? [],
|
||||
} satisfies EffectAcpSchema.LoadSessionRequest;
|
||||
return runLoggedRequest(
|
||||
"session/load",
|
||||
@@ -843,13 +850,13 @@ export const make = (
|
||||
}),
|
||||
Effect.flatMap((response) => adoptSession(sessionId, response)),
|
||||
),
|
||||
resumeSession: (sessionId) =>
|
||||
resumeSession: (sessionId, activationOptions) =>
|
||||
start.pipe(
|
||||
Effect.flatMap(() => {
|
||||
const requestPayload = {
|
||||
sessionId,
|
||||
cwd: options.cwd,
|
||||
mcpServers: options.mcpServers ?? [],
|
||||
mcpServers: activationOptions?.mcpServers ?? options.mcpServers ?? [],
|
||||
} satisfies EffectAcpSchema.ResumeSessionRequest;
|
||||
return runLoggedRequest(
|
||||
"session/resume",
|
||||
@@ -859,13 +866,13 @@ export const make = (
|
||||
}),
|
||||
Effect.flatMap((response) => adoptSession(sessionId, response)),
|
||||
),
|
||||
forkSession: (sessionId) =>
|
||||
forkSession: (sessionId, activationOptions) =>
|
||||
start.pipe(
|
||||
Effect.flatMap(() => {
|
||||
const requestPayload = {
|
||||
sessionId,
|
||||
cwd: options.cwd,
|
||||
mcpServers: options.mcpServers ?? [],
|
||||
mcpServers: activationOptions?.mcpServers ?? options.mcpServers ?? [],
|
||||
} satisfies EffectAcpSchema.ForkSessionRequest;
|
||||
return runLoggedRequest(
|
||||
"session/fork",
|
||||
|
||||
Reference in new issue
Block a user