The best way to control Claude Code, Codex and more from any computer or phone

main
ts 278 lines 10.5 KB
Raw
1 import type * as Cause from "effect/Cause";
2 import * as Flag from "effect/cli/Flag";
3 import * as GlobalFlag from "effect/cli/GlobalFlag";
4 import * as Clock from "effect/Clock";
5 import * as Context from "effect/Context";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import * as Fiber from "effect/Fiber";
9 import * as FileSystem from "effect/FileSystem";
10 import * as Layer from "effect/Layer";
11 import * as Logger from "effect/Logger";
12 import * as LogLevel from "effect/LogLevel";
13 import * as Option from "effect/Option";
14 import { Path } from "effect/Path";
15 import * as Queue from "effect/Queue";
16 import { CurrentLogAnnotations, MinimumLogLevel } from "effect/References";
17 import packageJson from "../../package.json" with { type: "json" };
18 import { dotAlchemyDirectory } from "../AlchemyContext.ts";
19 import { rootDir } from "../Auth/Paths.ts";
20 import { makePlainConsoleSink } from "../Util/ConsoleSink.ts";
21 import { LogAnnotation } from "../Util/LogAnnotation.ts";
22 import {
23 formatJsonRecord,
24 jsonLogger,
25 LOG_FORMAT_ENV,
26 LOG_LEVEL_ENV,
27 type LogRecord,
28 safeStringify,
29 } from "../Util/LogRecord.ts";
30 import { processTrace } from "../Util/TraceParent.ts";
31
32 /** Logs older than this are pruned (by mtime) on CLI start. */
33 const MAX_LOG_AGE = Duration.days(7);
34
35 /** The `--log-level` / `ALCHEMY_LOG_LEVEL` spellings. */
36 const logLevelChoices: Record<string, LogLevel.LogLevel> = {
37 all: "All",
38 trace: "Trace",
39 debug: "Debug",
40 info: "Info",
41 warn: "Warn",
42 warning: "Warn",
43 error: "Error",
44 fatal: "Fatal",
45 none: "None",
46 };
47
48 /**
49 * `--log-level`: the lowest level printed on the console. Registered on the
50 * CLI root in place of Effect's built-in flag, which also lowers the
51 * minimum log level and so would drop records from the run log; the run
52 * log records Debug whatever the console shows.
53 */
54 export const LogLevelFlag = GlobalFlag.Setting("log-level")({
55 flag: Flag.ChoiceWithValue("log-level", Object.entries(logLevelChoices)).pipe(
56 Flag.optional,
57 Flag.withDescription("Lowest level printed on the console"),
58 Flag.withMetavar("<all|trace|debug|info|warn|error|fatal|none>"),
59 ),
60 });
61
62 /** How the terminal shows log records. */
63 export type LogFormat = "pretty" | "json";
64
65 /**
66 * `--log-format pretty|json`: `json` prints one {@link LogRecord} per line
67 * on stderr and leaves rendering to the reader. Registered on the CLI root;
68 * the console logger reads the parsed value (see {@link consoleSettings}).
69 */
70 export const LogFormatFlag = GlobalFlag.Setting("log-format")({
71 flag: Flag.Literals("log-format", ["pretty", "json"]).pipe(
72 Flag.withDescription("Print log records as text or as JSON lines on stderr"),
73 Flag.optional,
74 ),
75 });
76
77 /** What the console shows, and how. */
78 export interface ConsoleSettings {
79 /** Records below this level stay in the run log only. */
80 readonly floor: LogLevel.LogLevel;
81 readonly format: LogFormat;
82 }
83
84 const flagValue = <A>(context: Context.Context<never>, flag: Context.Key<any, Option.Option<A>>) =>
85 Context.getOption(context, flag).pipe(Option.flatten, Option.getOrUndefined);
86
87 /**
88 * The console settings in `context`: the parsed `--log-level` and
89 * `--log-format` flags inside a command, and otherwise (before the command
90 * line is parsed, or in a child process that never sees it) the `ALCHEMY_LOG_*` environment variables. The console keeps its
91 * usual Info+ noise floor by default; the run log captures Debug regardless
92 * (#1231).
93 */
94 export const consoleSettings = (context: Context.Context<never>): ConsoleSettings => ({
95 floor:
96 flagValue(context, LogLevelFlag) ??
97 logLevelChoices[process.env[LOG_LEVEL_ENV]?.toLowerCase() ?? ""] ??
98 "Info",
99 format:
100 (flagValue(context, LogFormatFlag) ?? process.env[LOG_FORMAT_ENV]) === "json"
101 ? "json"
102 : "pretty",
103 });
104
105 /** The current fiber's {@link consoleSettings}. */
106 export const currentConsoleSettings: Effect.Effect<ConsoleSettings> = Effect.map(
107 Effect.context<never>(),
108 consoleSettings,
109 );
110
111 /** {@link ConsoleSettings} as environment, for a child process that renders the run's logs. */
112 export const consoleSettingsEnv = (settings: ConsoleSettings): Record<string, string> => ({
113 [LOG_LEVEL_ENV]: settings.floor,
114 [LOG_FORMAT_ENV]: settings.format,
115 });
116
117 const jsonSink = jsonLogger((line) => process.stderr.write(`${line}\n`));
118 const prettySink = makePlainConsoleSink();
119
120 /**
121 * Terminal sink: forwards records at or above the logging fiber's
122 * {@link consoleSettings} floor to `sink`, or to the sink its settings
123 * select.
124 */
125 export const makeConsoleLogger = (sink?: Logger.Logger<unknown, void>) =>
126 Logger.make<unknown, void>((options) => {
127 const settings = consoleSettings(options.fiber.context);
128 if (LogLevel.isGreaterThanOrEqualTo(options.logLevel, settings.floor)) {
129 (sink ?? (settings.format === "json" ? jsonSink : prettySink)).log(options);
130 }
131 });
132
133 /** Shared terminal logger for CLI entrypoints that do not need a run file. */
134 export const ConsoleLogLive = Layer.mergeAll(
135 Logger.layer([makeConsoleLogger()]),
136 Layer.succeed(MinimumLogLevel, "Debug"),
137 );
138
139 /** Drops everything — the fallback when the log file cannot be opened. */
140 const noopLogger = Logger.make<unknown, void>(() => {});
141
142 /**
143 * The run log's file logger alone. The run log is the only log file: the
144 * top-level CLI process writes it, and records its children already showed
145 * on the terminal (the `alchemy dev` exec child's) go here without being
146 * printed again.
147 */
148 export const RunLog = Context.Reference<Logger.Logger<unknown, void>>("alchemy/Cli/RunLog", {
149 defaultValue: () => noopLogger,
150 });
151
152 /** Delete run logs in `dir` older than {@link MAX_LOG_AGE}. Best-effort. */
153 const pruneRunLogs = (dir: string) =>
154 Effect.gen(function* () {
155 const fs = yield* FileSystem.FileSystem;
156 const path = yield* Path;
157 const now = yield* Clock.currentTimeMillis;
158 const entries = yield* fs.readDirectory(dir).pipe(Effect.orElseSucceed(() => [] as string[]));
159 // Only run logs: the directory may hold other tools' logs (the test
160 // runner's `test/`).
161 yield* Effect.forEach(
162 entries.filter((entry) => entry.endsWith(".ndjson")),
163 (entry) =>
164 Effect.gen(function* () {
165 const file = path.join(dir, entry);
166 const mtime = Option.getOrUndefined((yield* fs.stat(file)).mtime)?.getTime();
167 if (mtime !== undefined && now - mtime > Duration.toMillis(MAX_LOG_AGE)) {
168 yield* fs.remove(file);
169 }
170 }).pipe(Effect.ignore),
171 { discard: true },
172 );
173 });
174
175 /**
176 * A run log file that is created on its first record, so a run that sends
177 * nothing to it leaves nothing behind (and a `profile` command run outside
178 * a project never creates a `.alchemy` there). The file starts with a `run`
179 * record describing the run, so it reads standalone.
180 *
181 * A logger is called synchronously but the file is written asynchronously,
182 * so the logger only queues each record as a JSON line, and one writer fiber
183 * creates the file on the first line and appends the rest. Closing the log
184 * ends the queue and waits for the writer, so no queued record is lost.
185 *
186 * @internal exported for tests.
187 */
188 export const lazyRunLog = Effect.fn(function* (
189 dir: string,
190 name: string,
191 scope: LogRecord["scope"],
192 ) {
193 const fs = yield* FileSystem.FileSystem;
194 const path = yield* Path;
195 const file = path.join(dir, name);
196 const lines = yield* Queue.unbounded<string, Cause.Done>();
197 const append = (batch: ReadonlyArray<string>) =>
198 fs.writeFileString(file, batch.map((line) => `${line}\n`).join(""), { flag: "a" });
199 const writer = yield* Effect.gen(function* () {
200 const first = yield* Queue.take(lines);
201 yield* fs.makeDirectory(dir, { recursive: true });
202 yield* pruneRunLogs(dir);
203 yield* append([safeStringify(yield* runRecord(scope)), first]);
204 return yield* Effect.forever(Effect.flatMap(Queue.takeAll(lines), append));
205 }).pipe(
206 // The queue ending stops the writer. Any other failure (an unwritable
207 // directory) drops the log, never the command: a shut-down queue
208 // ignores further records.
209 Effect.catchCause(() => Queue.shutdown(lines)),
210 Effect.forkDetach,
211 );
212 yield* Effect.addFinalizer(() => Effect.andThen(Queue.end(lines), Fiber.await(writer)));
213 // Formatted now, so a record keeps the annotations it was logged with.
214 return Logger.make<unknown, void>((logged) => {
215 Queue.offerUnsafe(lines, formatJsonRecord.log(logged));
216 });
217 });
218
219 /**
220 * The two run logs: records scoped `global` (the state store, auth,
221 * profiles; see `globalLogs`) go to `~/.alchemy/logs` (relocated by
222 * `ALCHEMY_HOME`), everything else to the project's `.alchemy/log`. Every
223 * run writes `{timestamp}-pid{pid}.ndjson` (NDJSON log records) at Debug
224 * level — failures record their full causes there even though the terminal
225 * only shows the friendly message. Console output stays at Info+ unless
226 * `--log-level` lowers the floor (see {@link consoleSettings}).
227 *
228 * Only the top-level CLI process writes them; its children's records reach
229 * them through it. Commands that install their own loggers with
230 * `mergeWithExisting: true` compose with these; a replacing `Logger.layer`
231 * scopes them out for that subtree only.
232 */
233 export const GlobalLogLive = Layer.unwrap(
234 Effect.gen(function* () {
235 const path = yield* Path;
236 const stamp = new Date(yield* Clock.currentTimeMillis)
237 .toISOString()
238 .slice(0, 19)
239 .replaceAll(":", "-");
240 const name = `${stamp}-pid${process.pid}.ndjson`;
241 const global = yield* lazyRunLog(path.join(rootDir(), "logs"), name, "global");
242 const project = yield* lazyRunLog(
243 path.resolve(yield* dotAlchemyDirectory, "log"),
244 name,
245 "project",
246 );
247 const runLog = Logger.make<unknown, void>((options) =>
248 (options.fiber.getRef(CurrentLogAnnotations)[LogAnnotation.scope] === "global"
249 ? global
250 : project
251 ).log(options),
252 );
253 return Layer.mergeAll(
254 // Logs stay on this machine: telemetry exports spans and metrics only.
255 Logger.layer([makeConsoleLogger(), runLog]),
256 Layer.succeed(RunLog, runLog),
257 Layer.succeed(MinimumLogLevel, "Debug"),
258 );
259 }).pipe(Effect.orElseSucceed(() => Layer.empty)),
260 );
261
262 /** First line of every run log: enough context to read it standalone. */
263 const runRecord = (scope: LogRecord["scope"]) =>
264 Effect.map(Clock.currentTimeMillis, (time): LogRecord => ({
265 time,
266 level: "info",
267 kind: "run",
268 process: "cli",
269 scope,
270 trace: processTrace,
271 attributes: {},
272 data: {
273 version: packageJson.version,
274 pid: process.pid,
275 cwd: process.cwd(),
276 argv: process.argv.slice(2),
277 },
278 }));