Skip to content

Commit 8a10225

Browse files
refactor(provider): trace provider runtime operations
1 parent 94c4e73 commit 8a10225

10 files changed

Lines changed: 776 additions & 775 deletions

File tree

‎packages/effect-codex-app-server/src/protocol.ts‎

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,9 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
217217
] as const;
218218
}).pipe(Effect.flatten);
219219

220-
const offerOutgoing = (message: Record<string, unknown>) =>
221-
Effect.gen(function* () {
220+
const offerOutgoing =
221+
/* Every protocol write reaches this helper; request spans cover the operation. */
222+
Effect.fnUntraced(function* (message: Record<string, unknown>) {
222223
const failure = yield* Ref.get(terminationFailure);
223224
if (Option.isSome(failure)) return yield* failure.value;
224225

@@ -447,25 +448,24 @@ export const makeCodexAppServerPatchedProtocol = Effect.fn("makeCodexAppServerPa
447448

448449
yield* Stream.fromQueue(outgoing).pipe(Stream.run(options.stdio.stdout()), Effect.forkScoped);
449450

450-
const request = (method: string, payload?: unknown) =>
451-
Effect.gen(function* () {
452-
const requestId = yield* Ref.modify(
453-
nextRequestId,
454-
(current) => [current, current + 1] as const,
455-
);
456-
const deferred = yield* Deferred.make<unknown, CodexError.CodexAppServerError>();
457-
yield* Ref.update(pending, (current) =>
458-
new Map(current).set(String(requestId), { deferred, method }),
459-
);
460-
yield* offerOutgoing({
461-
id: requestId,
462-
method,
463-
...(payload !== undefined ? { params: payload } : {}),
464-
}).pipe(Effect.tapError(() => removePending(String(requestId))));
465-
return yield* Deferred.await(deferred).pipe(
466-
Effect.onInterrupt(() => removePending(String(requestId))),
467-
);
468-
});
451+
const request = Effect.fn("protocol.request")(function* (method: string, payload?: unknown) {
452+
const requestId = yield* Ref.modify(
453+
nextRequestId,
454+
(current) => [current, current + 1] as const,
455+
);
456+
const deferred = yield* Deferred.make<unknown, CodexError.CodexAppServerError>();
457+
yield* Ref.update(pending, (current) =>
458+
new Map(current).set(String(requestId), { deferred, method }),
459+
);
460+
yield* offerOutgoing({
461+
id: requestId,
462+
method,
463+
...(payload !== undefined ? { params: payload } : {}),
464+
}).pipe(Effect.tapError(() => removePending(String(requestId))));
465+
return yield* Deferred.await(deferred).pipe(
466+
Effect.onInterrupt(() => removePending(String(requestId))),
467+
);
468+
});
469469

470470
const notify = (method: string, payload?: unknown) =>
471471
offerOutgoing({

‎packages/provider-acp-registry/src/server/AcpRegistrySupport.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,8 +1589,8 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
15891589
return distribution;
15901590
});
15911591

1592-
const search: AcpRegistryCatalog["Service"]["search"] = (input) =>
1593-
Effect.gen(function* () {
1592+
const search: AcpRegistryCatalog["Service"]["search"] = Effect.fn("AcpRegistrySupport.search")(
1593+
function* (input) {
15941594
const registry = yield* refreshRegistry();
15951595
const ranked = registry.agents.flatMap((agent) => {
15961596
const distribution = resolveAcpRegistryDistribution({
@@ -1632,10 +1632,11 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
16321632
: "registry",
16331633
})),
16341634
} satisfies AcpRegistrySearchResult;
1635-
});
1635+
},
1636+
);
16361637

1637-
const prepare: AcpRegistryCatalog["Service"]["prepare"] = (input) =>
1638-
Effect.gen(function* () {
1638+
const prepare: AcpRegistryCatalog["Service"]["prepare"] = Effect.fn("AcpRegistrySupport.prepare")(
1639+
function* (input) {
16391640
const registry = yield* refreshRegistry();
16401641
const agent = yield* findAgent(registry, input.agentId);
16411642
const distribution = yield* compatibleDistribution(agent, "auto");
@@ -1658,7 +1659,8 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
16581659
distribution: distribution.kind,
16591660
prepared: true,
16601661
} satisfies AcpRegistryPrepareResult;
1661-
});
1662+
},
1663+
);
16621664

16631665
const validateLocalExecutable = (command: string) =>
16641666
platform === "win32" && /\.(?:cmd|bat)$/iu.test(command)
@@ -1671,8 +1673,8 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
16711673
)
16721674
: Effect.void;
16731675

1674-
const inspect: AcpRegistryCatalog["Service"]["inspect"] = (settings, environment) =>
1675-
Effect.gen(function* () {
1676+
const inspect: AcpRegistryCatalog["Service"]["inspect"] = Effect.fn("AcpRegistrySupport.inspect")(
1677+
function* (settings, environment) {
16761678
if (settings.source === "local") {
16771679
const command = settings.commandPath.trim();
16781680
if (!command) return { status: "unconfigured" } as const;
@@ -1782,10 +1784,11 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
17821784
distribution: distribution.kind,
17831785
runner,
17841786
} as const);
1785-
});
1787+
},
1788+
);
17861789

1787-
const resolve: AcpRegistryCatalog["Service"]["resolve"] = (settings, cwd, environment) =>
1788-
Effect.gen(function* () {
1790+
const resolve: AcpRegistryCatalog["Service"]["resolve"] = Effect.fn("AcpRegistrySupport.resolve")(
1791+
function* (settings, cwd, environment) {
17891792
if (settings.source === "local") {
17901793
const executable = settings.commandPath.trim();
17911794
if (!executable) {
@@ -1875,7 +1878,8 @@ export const makeAcpRegistryCatalog = Effect.fn("AcpRegistryCatalog.make")(funct
18751878
env: spawnEnvironment,
18761879
},
18771880
} satisfies ResolvedAcpRegistryAgent;
1878-
});
1881+
},
1882+
);
18791883

18801884
const uninstallManagedBinary: AcpRegistryCatalog["Service"]["uninstallManagedBinary"] = (input) =>
18811885
hostSettings

‎packages/provider-acp-registry/src/server/auth.ts‎

Lines changed: 87 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -227,109 +227,104 @@ export const makeAcpRegistryAuth = Effect.fn("makeAcpRegistryAuth")(function* (o
227227
return yield* failure("start", "The provider login command did not finish successfully.");
228228
});
229229

230-
const authenticate = (methodId: string, context: ProviderAuthFlow.ProviderAuthFlowContext) =>
231-
Effect.gen(function* () {
232-
if (!options.settings.enabled)
233-
return yield* failure("start", "Enable this provider before signing in.");
234-
const login = Effect.scoped(
235-
Effect.gen(function* () {
236-
const resolved = yield* resolve;
237-
const runtimeScope = yield* Scope.make();
238-
yield* Effect.addFinalizer(() => Scope.close(runtimeScope, Exit.void));
239-
const runtime = yield* makeRuntime(resolved.spawn).pipe(
240-
Effect.provideService(Scope.Scope, runtimeScope),
241-
);
242-
yield* runtime.handleElicitation((request) =>
243-
Effect.gen(function* () {
244-
if (
245-
request.mode !== "url" ||
246-
!("url" in request) ||
247-
!("elicitationId" in request) ||
248-
typeof request.url !== "string" ||
249-
typeof request.elicitationId !== "string"
250-
)
251-
return { action: "decline" } as const;
252-
const url = normalizeAcpRegistryWebUrl(request.url);
253-
const id = request.elicitationId.trim();
254-
if (!url || !id || id.length > 128) return { action: "decline" } as const;
255-
const consent = yield* Deferred.make<boolean>();
256-
yield* context.setInteraction(
257-
{ type: "browser", id, url, requiresConsent: true },
258-
(response) =>
259-
response.type === "browser"
260-
? Deferred.succeed(consent, response.action === "accept").pipe(Effect.asVoid)
261-
: Effect.void,
262-
);
263-
return (yield* Deferred.await(consent))
264-
? ({ action: "accept" } as const)
265-
: ({ action: "decline" } as const);
266-
}),
230+
const authenticate = Effect.fn("auth.authenticate")(function* (
231+
methodId: string,
232+
context: ProviderAuthFlow.ProviderAuthFlowContext,
233+
) {
234+
if (!options.settings.enabled)
235+
return yield* failure("start", "Enable this provider before signing in.");
236+
const login = Effect.scoped(
237+
Effect.gen(function* () {
238+
const resolved = yield* resolve;
239+
const runtimeScope = yield* Scope.make();
240+
yield* Effect.addFinalizer(() => Scope.close(runtimeScope, Exit.void));
241+
const runtime = yield* makeRuntime(resolved.spawn).pipe(
242+
Effect.provideService(Scope.Scope, runtimeScope),
243+
);
244+
yield* runtime.handleElicitation((request) =>
245+
Effect.gen(function* () {
246+
if (
247+
request.mode !== "url" ||
248+
!("url" in request) ||
249+
!("elicitationId" in request) ||
250+
typeof request.url !== "string" ||
251+
typeof request.elicitationId !== "string"
252+
)
253+
return { action: "decline" } as const;
254+
const url = normalizeAcpRegistryWebUrl(request.url);
255+
const id = request.elicitationId.trim();
256+
if (!url || !id || id.length > 128) return { action: "decline" } as const;
257+
const consent = yield* Deferred.make<boolean>();
258+
yield* context.setInteraction(
259+
{ type: "browser", id, url, requiresConsent: true },
260+
(response) =>
261+
response.type === "browser"
262+
? Deferred.succeed(consent, response.action === "accept").pipe(Effect.asVoid)
263+
: Effect.void,
264+
);
265+
return (yield* Deferred.await(consent))
266+
? ({ action: "accept" } as const)
267+
: ({ action: "decline" } as const);
268+
}),
269+
);
270+
const initialized = yield* runtime
271+
.initialize()
272+
.pipe(
273+
Effect.mapError((cause) =>
274+
failure("start", "Could not initialize the selected ACP agent.", cause),
275+
),
267276
);
268-
const initialized = yield* runtime
269-
.initialize()
277+
const method = initialized.authMethods?.find((method) => method.id === methodId);
278+
if (!method)
279+
return yield* failure("start", "The agent no longer advertises this sign-in method.");
280+
if (method.type === "terminal") {
281+
yield* Scope.close(runtimeScope, Exit.void);
282+
yield* runTerminal(resolved, method, context).pipe(Effect.scoped);
283+
// Terminal login is a separate invocation. Reconnect after it exits so
284+
// the ACP process reads the credentials its login command persisted.
285+
const verifiedRuntime = yield* makeRuntime(resolved.spawn);
286+
yield* context.verifying;
287+
yield* verifiedRuntime
288+
.start()
270289
.pipe(
271290
Effect.mapError((cause) =>
272-
failure("start", "Could not initialize the selected ACP agent.", cause),
291+
failure(
292+
"verify",
293+
"The provider could not create a session after terminal sign-in.",
294+
cause,
295+
),
273296
),
274297
);
275-
const method = initialized.authMethods?.find((method) => method.id === methodId);
276-
if (!method)
277-
return yield* failure("start", "The agent no longer advertises this sign-in method.");
278-
if (method.type === "terminal") {
279-
yield* Scope.close(runtimeScope, Exit.void);
280-
yield* runTerminal(resolved, method, context).pipe(Effect.scoped);
281-
// Terminal login is a separate invocation. Reconnect after it exits so
282-
// the ACP process reads the credentials its login command persisted.
283-
const verifiedRuntime = yield* makeRuntime(resolved.spawn);
284-
yield* context.verifying;
285-
yield* verifiedRuntime
286-
.start()
287-
.pipe(
288-
Effect.mapError((cause) =>
289-
failure(
290-
"verify",
291-
"The provider could not create a session after terminal sign-in.",
292-
cause,
293-
),
294-
),
295-
);
296-
} else {
297-
if (method.type === undefined || method.type === "agent") {
298-
if (!runtime.authenticate)
299-
return yield* failure(
300-
"start",
301-
"This runtime does not support explicit ACP sign-in.",
302-
);
303-
yield* runtime
304-
.authenticate(methodId)
305-
.pipe(
306-
Effect.mapError((cause) =>
307-
failure("start", "The ACP agent could not complete sign-in.", cause),
308-
),
309-
);
310-
}
311-
// Legacy env_var methods use the secret-backed environment fields in
312-
// provider settings. Never send their ID to authenticate.
313-
yield* context.verifying;
298+
} else {
299+
if (method.type === undefined || method.type === "agent") {
300+
if (!runtime.authenticate)
301+
return yield* failure("start", "This runtime does not support explicit ACP sign-in.");
314302
yield* runtime
315-
.start()
303+
.authenticate(methodId)
316304
.pipe(
317305
Effect.mapError((cause) =>
318-
failure(
319-
"verify",
320-
"The provider could not create a session after sign-in.",
321-
cause,
322-
),
306+
failure("start", "The ACP agent could not complete sign-in.", cause),
323307
),
324308
);
325309
}
326-
}),
327-
);
328-
yield* Option.isSome(coordinator)
329-
? coordinator.value.withForegroundStartup(agentKey, login)
330-
: login;
331-
yield* options.onChanged(true);
332-
});
310+
// Legacy env_var methods use the secret-backed environment fields in
311+
// provider settings. Never send their ID to authenticate.
312+
yield* context.verifying;
313+
yield* runtime
314+
.start()
315+
.pipe(
316+
Effect.mapError((cause) =>
317+
failure("verify", "The provider could not create a session after sign-in.", cause),
318+
),
319+
);
320+
}
321+
}),
322+
);
323+
yield* Option.isSome(coordinator)
324+
? coordinator.value.withForegroundStartup(agentKey, login)
325+
: login;
326+
yield* options.onChanged(true);
327+
});
333328
const logout = Effect.scoped(
334329
Effect.gen(function* () {
335330
const resolved = yield* resolve;

0 commit comments

Comments
 (0)