Skip to content

Commit 7879934

Browse files
refactor(provider): trace adapter operations without per-chunk spans
1 parent 8a10225 commit 7879934

33 files changed

Lines changed: 4059 additions & 3945 deletions

‎packages/provider-acp/src/server/AcpSessionRuntime.ts‎

Lines changed: 1316 additions & 1338 deletions
Large diffs are not rendered by default.

‎packages/provider-acp/src/server/adapter.ts‎

Lines changed: 71 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,12 +1555,13 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
15551555
return Option.some(yield* effect);
15561556
}),
15571557
);
1558-
const registerNativeResponseAcknowledgement = (
1559-
generation: number,
1560-
transportRequestId: string,
1561-
acknowledgement: Deferred.Deferred<void, EffectAcpErrors.AcpError>,
1562-
) =>
1563-
Effect.gen(function* () {
1558+
const registerNativeResponseAcknowledgement =
1559+
/* Provider events include streaming deltas; avoid a span for each update. */
1560+
Effect.fnUntraced(function* (
1561+
generation: number,
1562+
transportRequestId: string,
1563+
acknowledgement: Deferred.Deferred<void, EffectAcpErrors.AcpError>,
1564+
) {
15641565
yield* Ref.update(nativeResponseAcknowledgements, (current) => {
15651566
const updated = new Map(current);
15661567
updated.set(transportRequestId, { acknowledgement, generation });
@@ -1800,8 +1801,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
18001801
const endedBackgroundTaskIds = yield* Ref.make<ReadonlySet<string>>(new Set());
18011802
const endedBackgroundTaskIdLimit = 128;
18021803

1803-
const setBackgroundTaskRunning = (taskId: string, running: boolean) =>
1804-
Effect.gen(function* () {
1804+
const setBackgroundTaskRunning =
1805+
/* Provider events include streaming deltas; avoid a span for each update. */
1806+
Effect.fnUntraced(function* (taskId: string, running: boolean) {
18051807
if (running && (yield* Ref.get(endedBackgroundTaskIds)).has(taskId)) {
18061808
return;
18071809
}
@@ -1836,10 +1838,11 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
18361838
? setBackgroundTaskRunning(mutation.taskId, true)
18371839
: markBackgroundTaskEnded(mutation.taskId);
18381840

1839-
const trackRunningBackgroundTools = (
1840-
notification: EffectAcpSchema.SessionNotification,
1841-
): Effect.Effect<void> =>
1842-
Effect.gen(function* () {
1841+
const trackRunningBackgroundTools =
1842+
/* Provider events include streaming deltas; avoid a span for each update. */
1843+
Effect.fnUntraced(function* (
1844+
notification: EffectAcpSchema.SessionNotification,
1845+
): Effect.fn.Return<void> {
18431846
if (flavor.extractBackgroundTaskId === undefined) return;
18441847
for (const event of parseSessionUpdateEvent(notification).events) {
18451848
if (event._tag !== "ToolCallUpdated") continue;
@@ -2780,8 +2783,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
27802783
return false;
27812784
};
27822785

2783-
const rearmDeferredFinalize = (context: ActiveAcpTurn) =>
2784-
Effect.gen(function* () {
2786+
const rearmDeferredFinalize =
2787+
/* Provider events include streaming deltas; avoid a span for each update. */
2788+
Effect.fnUntraced(function* (context: ActiveAcpTurn) {
27852789
if (!flavor.deferFinalizeForBackgroundWork) return;
27862790
if (!context.promptSettled || context.finalized) return;
27872791
if (hasDeferredBackgroundWork(context)) return;
@@ -2795,8 +2799,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
27952799
projectedStatus?: ProjectedToolStatus,
27962800
) => Effect.Effect<void> = () => Effect.void;
27972801

2798-
const markAwaitingBackgroundHydration = (context: ActiveAcpTurn, taskId: string) =>
2799-
Effect.gen(function* () {
2802+
const markAwaitingBackgroundHydration =
2803+
/* Provider events include streaming deltas; avoid a span for each update. */
2804+
Effect.fnUntraced(function* (context: ActiveAcpTurn, taskId: string) {
28002805
if (context.awaitingBackgroundHydration.has(taskId)) return;
28012806
context.awaitingBackgroundHydration.add(taskId);
28022807
yield* Effect.gen(function* () {
@@ -2815,8 +2820,9 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
28152820
}).pipe(Effect.forkIn(sessionScope), Effect.asVoid);
28162821
});
28172822

2818-
const markPendingInjectedReport = (context: ActiveAcpTurn, taskId: string) =>
2819-
Effect.gen(function* () {
2823+
const markPendingInjectedReport =
2824+
/* Provider events include streaming deltas; avoid a span for each update. */
2825+
Effect.fnUntraced(function* (context: ActiveAcpTurn, taskId: string) {
28202826
if (!context.promptSettled) return;
28212827
if (context.pendingInjectedReport.has(taskId)) return;
28222828
context.pendingInjectedReport.add(taskId);
@@ -3365,13 +3371,14 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
33653371
});
33663372
});
33673373

3368-
const appendLoadedHistory = (
3369-
notification: EffectAcpSchema.SessionNotification,
3370-
role: "user" | "assistant" | "thought",
3371-
text: string,
3372-
replace = false,
3373-
) =>
3374-
Effect.gen(function* () {
3374+
const appendLoadedHistory =
3375+
/* Provider events include streaming deltas; avoid a span for each update. */
3376+
Effect.fnUntraced(function* (
3377+
notification: EffectAcpSchema.SessionNotification,
3378+
role: "user" | "assistant" | "thought",
3379+
text: string,
3380+
replace = false,
3381+
) {
33753382
if (text.length === 0 && !replace) return;
33763383
const now = yield* DateTime.now;
33773384
yield* Ref.update(snapshot, (current) => {
@@ -5032,47 +5039,47 @@ export const makeAcpAdapterV2 = Effect.fn("makeAcpAdapterV2")(function* (
50325039
}
50335040
});
50345041

5035-
const projectAcpRuntimeSessionUpdateEffect = (
5036-
rawNotification: EffectAcpSchema.SessionNotification,
5037-
) =>
5038-
Effect.gen(function* () {
5039-
if (clientTerminals !== undefined) {
5040-
const embedded = embeddedTerminalIdsFromSessionUpdate(rawNotification);
5041-
if (embedded !== undefined) {
5042-
rememberEmbeddedTerminals({
5043-
...embedded,
5044-
sessionId: rawNotification.sessionId,
5045-
});
5042+
const projectAcpRuntimeSessionUpdateEffect =
5043+
/* Provider events include streaming deltas; avoid a span for each update. */
5044+
Effect.fnUntraced(
5045+
function* (rawNotification: EffectAcpSchema.SessionNotification) {
5046+
if (clientTerminals !== undefined) {
5047+
const embedded = embeddedTerminalIdsFromSessionUpdate(rawNotification);
5048+
if (embedded !== undefined) {
5049+
rememberEmbeddedTerminals({
5050+
...embedded,
5051+
sessionId: rawNotification.sessionId,
5052+
});
5053+
}
50465054
}
5047-
}
5048-
const notification =
5049-
clientTerminals === undefined
5050-
? rawNotification
5051-
: resolveEmbeddedTerminalContent(
5052-
rawNotification,
5053-
clientTerminals.readOutputSnapshot,
5054-
);
5055-
if (notification.update.sessionUpdate === "available_commands_update") {
5056-
yield* (
5057-
flavor.onAvailableCommandsUpdate?.(notification.update.availableCommands) ??
5058-
Effect.void
5059-
);
5060-
}
5061-
if (
5062-
notification.update.sessionUpdate === "config_option_update" ||
5063-
notification.update.sessionUpdate === "current_mode_update"
5064-
) {
5065-
yield* (
5066-
flavor.onSessionConfigurationUpdate?.(
5067-
yield* runtime.getConfigOptions,
5068-
yield* runtime.getModeState,
5069-
) ?? Effect.void
5055+
const notification =
5056+
clientTerminals === undefined
5057+
? rawNotification
5058+
: resolveEmbeddedTerminalContent(
5059+
rawNotification,
5060+
clientTerminals.readOutputSnapshot,
5061+
);
5062+
if (notification.update.sessionUpdate === "available_commands_update") {
5063+
yield* (
5064+
flavor.onAvailableCommandsUpdate?.(notification.update.availableCommands) ??
5065+
Effect.void
5066+
);
5067+
}
5068+
if (
5069+
notification.update.sessionUpdate === "config_option_update" ||
5070+
notification.update.sessionUpdate === "current_mode_update"
5071+
) {
5072+
yield* (
5073+
flavor.onSessionConfigurationUpdate?.(
5074+
yield* runtime.getConfigOptions,
5075+
yield* runtime.getModeState,
5076+
) ?? Effect.void
5077+
);
5078+
}
5079+
yield* handleSessionUpdate(
5080+
flavor.normalizeSessionUpdate?.(notification) ?? notification,
50705081
);
5071-
}
5072-
yield* handleSessionUpdate(
5073-
flavor.normalizeSessionUpdate?.(notification) ?? notification,
5074-
);
5075-
}).pipe(
5082+
},
50765083
Effect.mapError(
50775084
(cause) =>
50785085
new EffectAcpErrors.AcpTransportError({

‎packages/provider-acp/src/server/mcpOverAcpBridge.ts‎

Lines changed: 79 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -139,94 +139,90 @@ export const makeAcpMcpOverAcpBridge = Effect.fn("AcpMcpOverAcpBridge.make")(fun
139139
}).pipe(Effect.mapError(bridgeError)),
140140
);
141141

142-
const disconnect = (
142+
const disconnect = Effect.fn("mcpOverAcpBridge.disconnect")(function* (
143143
request: AcpSchema.DisconnectMcpRequest,
144-
): Effect.Effect<AcpSchema.DisconnectMcpResponse, AcpMcpOverAcpError> =>
145-
Effect.gen(function* () {
146-
const connection = yield* connectionFor(request.connectionId);
147-
connections.delete(request.connectionId);
148-
const sessionId = connection.sessionId;
149-
if (sessionId !== null) {
150-
const response = yield* Effect.tryPromise({
151-
try: (signal) =>
152-
fetchImplementation(options.endpoint, {
153-
method: "DELETE",
154-
signal,
155-
headers: {
156-
authorization: options.authorization,
157-
"mcp-session-id": sessionId,
158-
...(connection.protocolVersion === null
159-
? {}
160-
: { "mcp-protocol-version": connection.protocolVersion }),
161-
},
162-
}),
163-
catch: bridgeError,
164-
});
165-
if (!response.ok && response.status !== 404) {
166-
return yield* Effect.fail(
167-
new AcpMcpOverAcpError(
168-
`T3 Code MCP endpoint rejected disconnect with HTTP ${response.status}.`,
169-
),
170-
);
171-
}
172-
yield* Effect.promise(
173-
() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve(),
144+
): Effect.fn.Return<AcpSchema.DisconnectMcpResponse, AcpMcpOverAcpError> {
145+
const connection = yield* connectionFor(request.connectionId);
146+
connections.delete(request.connectionId);
147+
const sessionId = connection.sessionId;
148+
if (sessionId !== null) {
149+
const response = yield* Effect.tryPromise({
150+
try: (signal) =>
151+
fetchImplementation(options.endpoint, {
152+
method: "DELETE",
153+
signal,
154+
headers: {
155+
authorization: options.authorization,
156+
"mcp-session-id": sessionId,
157+
...(connection.protocolVersion === null
158+
? {}
159+
: { "mcp-protocol-version": connection.protocolVersion }),
160+
},
161+
}),
162+
catch: bridgeError,
163+
});
164+
if (!response.ok && response.status !== 404) {
165+
return yield* Effect.fail(
166+
new AcpMcpOverAcpError(
167+
`T3 Code MCP endpoint rejected disconnect with HTTP ${response.status}.`,
168+
),
174169
);
175170
}
176-
return {};
177-
});
171+
yield* Effect.promise(
172+
() => response.body?.cancel().catch(() => undefined) ?? Promise.resolve(),
173+
);
174+
}
175+
return {};
176+
});
178177

179178
return {
180-
connect: (request) =>
181-
Effect.gen(function* () {
182-
if (request.serverId !== "t3-code") {
183-
return yield* Effect.fail(
184-
new AcpMcpOverAcpError(`Unknown ACP MCP server "${request.serverId}".`),
185-
);
186-
}
187-
if (connections.size >= MAX_CONNECTIONS) {
188-
return yield* Effect.fail(new AcpMcpOverAcpError("Too many MCP-over-ACP connections."));
189-
}
190-
const connectionId = yield* options.allocateConnectionId;
191-
connections.set(connectionId, {
192-
mutex: yield* Semaphore.make(1),
193-
sessionId: null,
194-
protocolVersion: null,
195-
nextRequestId: 0,
196-
});
197-
return { connectionId };
198-
}),
199-
message: (request) =>
200-
Effect.gen(function* () {
201-
const connection = yield* connectionFor(request.connectionId);
202-
const id = ++connection.nextRequestId;
203-
const payloads = yield* send(connection, {
204-
jsonrpc: "2.0",
205-
id,
206-
method: request.method,
207-
...(request.params == null ? {} : { params: request.params }),
208-
});
209-
const response = payloads.find((payload) => asEnvelope(payload)?.id === id);
210-
const envelope = asEnvelope(response);
211-
if (envelope === null) {
212-
return yield* Effect.fail(
213-
new AcpMcpOverAcpError("MCP server did not return a matching response."),
214-
);
215-
}
216-
if (envelope.error !== undefined) {
217-
return yield* Effect.fail(new AcpMcpOverAcpError(errorMessage(envelope.error)));
218-
}
219-
return (envelope.result ?? null) as AcpSchema.MessageMcpResponse;
220-
}),
221-
notification: (request) =>
222-
Effect.gen(function* () {
223-
const connection = yield* connectionFor(request.connectionId);
224-
yield* send(connection, {
225-
jsonrpc: "2.0",
226-
method: request.method,
227-
...(request.params == null ? {} : { params: request.params }),
228-
});
229-
}),
179+
connect: Effect.fn("mcpOverAcpBridge.connect")(function* (request) {
180+
if (request.serverId !== "t3-code") {
181+
return yield* Effect.fail(
182+
new AcpMcpOverAcpError(`Unknown ACP MCP server "${request.serverId}".`),
183+
);
184+
}
185+
if (connections.size >= MAX_CONNECTIONS) {
186+
return yield* Effect.fail(new AcpMcpOverAcpError("Too many MCP-over-ACP connections."));
187+
}
188+
const connectionId = yield* options.allocateConnectionId;
189+
connections.set(connectionId, {
190+
mutex: yield* Semaphore.make(1),
191+
sessionId: null,
192+
protocolVersion: null,
193+
nextRequestId: 0,
194+
});
195+
return { connectionId };
196+
}),
197+
message: Effect.fn("mcpOverAcpBridge.message")(function* (request) {
198+
const connection = yield* connectionFor(request.connectionId);
199+
const id = ++connection.nextRequestId;
200+
const payloads = yield* send(connection, {
201+
jsonrpc: "2.0",
202+
id,
203+
method: request.method,
204+
...(request.params == null ? {} : { params: request.params }),
205+
});
206+
const response = payloads.find((payload) => asEnvelope(payload)?.id === id);
207+
const envelope = asEnvelope(response);
208+
if (envelope === null) {
209+
return yield* Effect.fail(
210+
new AcpMcpOverAcpError("MCP server did not return a matching response."),
211+
);
212+
}
213+
if (envelope.error !== undefined) {
214+
return yield* Effect.fail(new AcpMcpOverAcpError(errorMessage(envelope.error)));
215+
}
216+
return (envelope.result ?? null) as AcpSchema.MessageMcpResponse;
217+
}),
218+
notification: Effect.fn("mcpOverAcpBridge.notification")(function* (request) {
219+
const connection = yield* connectionFor(request.connectionId);
220+
yield* send(connection, {
221+
jsonrpc: "2.0",
222+
method: request.method,
223+
...(request.params == null ? {} : { params: request.params }),
224+
});
225+
}),
230226
disconnect,
231227
dispose: Effect.suspend(() =>
232228
Effect.forEach(

0 commit comments

Comments
 (0)