Open History to see the last commit
1/**
2 * ProviderEventLoggers — single observability service that owns the two
3 * shared NDJSON streams the provider runtime writes:
4 *
5 * - `native` — provider-protocol events as the SDK emits them, written
6 * from inside each `<X>Adapter` factory.
7 * - `canonical` — runtime events after `ProviderService` has normalized
8 * them onto `ProviderRuntimeEvent`.
9 *
10 * Why a service tag and not constructor options?
11 *
12 * - Adapters are now constructed *inside* drivers (`<X>Driver.create()`),
13 * not at the boot Layer. There is no longer a single `make<X>AdapterLive(options)`
14 * call site where we can hand an `EventNdjsonLogger` in by hand.
15 * - Multiple driver instances per kind (`codex_personal`, `codex_work`)
16 * should share one underlying log writer per stream — opening N writers
17 * against the same rotating file would race the rotation logic. Owning
18 * the loggers on a single tag keeps that invariant intact.
19 * - Tests can swap one (or both) loggers with in-memory recorders by
20 * `Layer.succeed(ProviderEventLoggers, { native, canonical })` instead of
21 * juggling per-Layer option threading.
22 *
23 * Both fields are optional. `makeEventNdjsonLogger` returns `undefined` when
24 * the target directory cannot be created; we forward that as `undefined`
25 * rather than failing the boot Layer, matching the previous best-effort
26 * behavior of `server.ts`.
27 *
28 * @module provider/Layers/ProviderEventLoggers
29 */
30import * as Context from "effect/Context";
31import * as Effect from "effect/Effect";
32import * as Layer from "effect/Layer";
33
34import { ServerConfig } from "../../config.ts";
35import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
36
37export interface ProviderEventLoggersShape {
38 readonly native: EventNdjsonLogger | undefined;
39 readonly canonical: EventNdjsonLogger | undefined;
40}
41
42/**
43 * Shared logger pair for native + canonical provider event streams.
44 *
45 * Service value is intentionally a struct of two optional loggers rather
46 * than two parallel tags. Construction site is one place
47 * (`ProviderEventLoggersLive`); consumers (drivers, `ProviderService`) read
48 * one tag and pluck the field they need.
49 */
50export class ProviderEventLoggers extends Context.Service<
51 ProviderEventLoggers,
52 ProviderEventLoggersShape
53>()("t3/provider/Layers/ProviderEventLoggers") {}
54
55/**
56 * Constant value used by tests / boot layers that want to opt out of native
57 * + canonical logging entirely. Keeps the tag non-optional in the type
58 * system while letting the runtime treat absence as a no-op.
59 */
60export const NoOpProviderEventLoggers: ProviderEventLoggersShape = {
61 native: undefined,
62 canonical: undefined,
63};
64
65/**
66 * Live Layer that builds both loggers from `ServerConfig.providerEventLogPath`.
67 * If the directory create fails for either stream, the corresponding field
68 * is `undefined` and writes from that stream become no-ops downstream.
69 */
70export const ProviderEventLoggersLive = Layer.effect(
71 ProviderEventLoggers,
72 Effect.gen(function* () {
73 const { providerEventLogPath } = yield* ServerConfig;
74 const native = yield* makeEventNdjsonLogger(providerEventLogPath, {
75 stream: "native",
76 });
77 const canonical = yield* makeEventNdjsonLogger(providerEventLogPath, {
78 stream: "canonical",
79 });
80 return {
81 native,
82 canonical,
83 } satisfies ProviderEventLoggersShape;
84 }),
85);