Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ProviderEventLoggers.ts

83 lines3.3 KB

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 { Context, Effect, Layer } from "effect";
31​
32import { ServerConfig } from "../../config.ts";
33import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
34​
35export interface ProviderEventLoggersShape {
36 readonly native: EventNdjsonLogger | undefined;
37 readonly canonical: EventNdjsonLogger | undefined;
39​
40/**
41 * Shared logger pair for native + canonical provider event streams.
42 *
43 * Service value is intentionally a struct of two optional loggers rather
44 * than two parallel tags. Construction site is one place
45 * (`ProviderEventLoggersLive`); consumers (drivers, `ProviderService`) read
46 * one tag and pluck the field they need.
47 */
48export class ProviderEventLoggers extends Context.Service<
49 ProviderEventLoggers,
50 ProviderEventLoggersShape
51>()("t3/provider/ProviderEventLoggers") {}
52​
53/**
54 * Constant value used by tests / boot layers that want to opt out of native
55 * + canonical logging entirely. Keeps the tag non-optional in the type
56 * system while letting the runtime treat absence as a no-op.
57 */
58export const NoOpProviderEventLoggers: ProviderEventLoggersShape = {
59 native: undefined,
60 canonical: undefined,
61};
62​
63/**
64 * Live Layer that builds both loggers from `ServerConfig.providerEventLogPath`.
65 * If the directory create fails for either stream, the corresponding field
66 * is `undefined` and writes from that stream become no-ops downstream.
67 */
68export const ProviderEventLoggersLive = Layer.effect(
69 ProviderEventLoggers,
70 Effect.gen(function* () {
71 const { providerEventLogPath } = yield* ServerConfig;
72 const native = yield* makeEventNdjsonLogger(providerEventLogPath, {
73 stream: "native",
74 });
75 const canonical = yield* makeEventNdjsonLogger(providerEventLogPath, {
76 stream: "canonical",
77 });
78 return {
79 native,
80 canonical,
81 } satisfies ProviderEventLoggersShape;
82 }),
83);