The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PRIMARY_LOCAL_ENVIRONMENT_ID } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | makeLocalFileTracer, |
| 4 | makeTraceSink, |
| 5 | nodeProcessRuntimeAttributes, |
| 6 | processServiceInstanceId, |
| 7 | type SignalExport, |
| 8 | } from "@t3tools/shared/observability"; |
| 9 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 10 | import * as SharedObservability from "@t3tools/shared/observability"; |
| 11 | import * as OtelEnvironment from "@t3tools/shared/otelEnvironment"; |
| 12 | import { releaseChannelOf, releaseResourceAttributes } from "@t3tools/shared/releaseResource"; |
| 13 | import { |
| 14 | parsePersistedServerObservabilitySettings, |
| 15 | type PersistedServerObservabilitySettings, |
| 16 | } from "@t3tools/shared/serverSettings"; |
| 17 | import * as Context from "effect/Context"; |
| 18 | import * as DateTime from "effect/DateTime"; |
| 19 | import * as Effect from "effect/Effect"; |
| 20 | import * as FileSystem from "effect/FileSystem"; |
| 21 | import * as Layer from "effect/Layer"; |
| 22 | import * as Logger from "effect/Logger"; |
| 23 | import * as Option from "effect/Option"; |
| 24 | import * as Path from "effect/Path"; |
| 25 | import * as PlatformError from "effect/PlatformError"; |
| 26 | import * as References from "effect/References"; |
| 27 | import * as Ref from "effect/Ref"; |
| 28 | import * as Schema from "effect/Schema"; |
| 29 | import * as Scope from "effect/Scope"; |
| 30 | import * as Semaphore from "effect/Semaphore"; |
| 31 | import * as SynchronizedRef from "effect/SynchronizedRef"; |
| 32 | import * as Tracer from "effect/Tracer"; |
| 33 | import { OtlpExporter, OtlpLogger, OtlpTracer } from "effect/observability"; |
| 34 | |
| 35 | import desktopPackageJson from "../../package.json" with { type: "json" }; |
| 36 | |
| 37 | import * as DesktopEnvironment from "./DesktopEnvironment.ts"; |
| 38 | |
| 39 | const DESKTOP_LOG_FILE_MAX_BYTES = 10 * 1024 * 1024; |
| 40 | const DESKTOP_LOG_FILE_MAX_FILES = 10; |
| 41 | const DESKTOP_BACKEND_CHILD_LOG_FIBER_ID = "#backend-child"; |
| 42 | const DESKTOP_TRACE_BATCH_WINDOW_MS = 1_000; |
| 43 | const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES = 1024 * 1024; |
| 44 | const DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_CHUNKS = 256; |
| 45 | |
| 46 | export interface RotatingLogFileWriter { |
| 47 | readonly writeBytes: (chunk: Uint8Array) => Effect.Effect<void>; |
| 48 | readonly writeText: (chunk: string) => Effect.Effect<void>; |
| 49 | } |
| 50 | |
| 51 | export interface DesktopBackendOutputLogShape { |
| 52 | readonly beginSession: (input: { readonly details: string }) => Effect.Effect<void>; |
| 53 | readonly writeOutputChunk: ( |
| 54 | streamName: "stdout" | "stderr", |
| 55 | chunk: Uint8Array, |
| 56 | ) => Effect.Effect<void>; |
| 57 | readonly persistFailureSnapshot: (input: { readonly details: string }) => Effect.Effect<void>; |
| 58 | readonly persistFailure: (input: { readonly details: string }) => Effect.Effect<void>; |
| 59 | readonly discardSession: Effect.Effect<void>; |
| 60 | } |
| 61 | |
| 62 | export class DesktopBackendOutputLogFactory extends Context.Service< |
| 63 | DesktopBackendOutputLogFactory, |
| 64 | { |
| 65 | readonly forInstance: (id: string) => Effect.Effect<DesktopBackendOutputLogShape>; |
| 66 | } |
| 67 | >()("@t3tools/desktop/app/DesktopObservability/DesktopBackendOutputLogFactory") {} |
| 68 | |
| 69 | const textEncoder = new TextEncoder(); |
| 70 | const textDecoder = new TextDecoder(); |
| 71 | |
| 72 | export type DesktopLogAnnotations = Record<string, unknown>; |
| 73 | |
| 74 | export interface DesktopComponentLogger { |
| 75 | readonly annotate: <A, E, R>( |
| 76 | effect: Effect.Effect<A, E, R>, |
| 77 | annotations?: DesktopLogAnnotations, |
| 78 | ) => Effect.Effect<A, E, R>; |
| 79 | readonly logDebug: (message: string, annotations?: DesktopLogAnnotations) => Effect.Effect<void>; |
| 80 | readonly logInfo: (message: string, annotations?: DesktopLogAnnotations) => Effect.Effect<void>; |
| 81 | readonly logWarning: ( |
| 82 | message: string, |
| 83 | annotations?: DesktopLogAnnotations, |
| 84 | ) => Effect.Effect<void>; |
| 85 | readonly logError: (message: string, annotations?: DesktopLogAnnotations) => Effect.Effect<void>; |
| 86 | } |
| 87 | |
| 88 | export function makeComponentLogger(component: string): DesktopComponentLogger { |
| 89 | const annotate: DesktopComponentLogger["annotate"] = (effect, annotations) => |
| 90 | effect.pipe( |
| 91 | Effect.annotateLogs({ |
| 92 | component, |
| 93 | ...annotations, |
| 94 | }), |
| 95 | ); |
| 96 | |
| 97 | return { |
| 98 | annotate, |
| 99 | logDebug: (message, annotations) => annotate(Effect.logDebug(message), annotations), |
| 100 | logInfo: (message, annotations) => annotate(Effect.logInfo(message), annotations), |
| 101 | logWarning: (message, annotations) => annotate(Effect.logWarning(message), annotations), |
| 102 | logError: (message, annotations) => annotate(Effect.logError(message), annotations), |
| 103 | }; |
| 104 | } |
| 105 | |
| 106 | class DesktopLogFileWriterConfigurationError extends Schema.TaggedError<DesktopLogFileWriterConfigurationError>()( |
| 107 | "DesktopLogFileWriterConfigurationError", |
| 108 | { |
| 109 | option: Schema.Literals(["maxBytes", "maxFiles"]), |
| 110 | value: Schema.Number, |
| 111 | }, |
| 112 | ) { |
| 113 | override get message() { |
| 114 | return `${this.option} must be >= 1 (received ${this.value})`; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | type DesktopLogFileWriterError = |
| 119 | | DesktopLogFileWriterConfigurationError |
| 120 | | PlatformError.PlatformError; |
| 121 | |
| 122 | const sanitizeLogValue = (value: string): string => value.replace(/\s+/g, " ").trim(); |
| 123 | |
| 124 | const DesktopBackendChildLogRecord = Schema.Struct({ |
| 125 | message: Schema.String, |
| 126 | level: Schema.Literals(["INFO", "ERROR"]), |
| 127 | timestamp: Schema.String, |
| 128 | annotations: Schema.Record(Schema.String, Schema.Unknown), |
| 129 | spans: Schema.Record(Schema.String, Schema.Unknown), |
| 130 | fiberId: Schema.String, |
| 131 | }); |
| 132 | |
| 133 | const encodeDesktopBackendChildLogRecord = Schema.encodeEffect( |
| 134 | Schema.fromJsonString(DesktopBackendChildLogRecord), |
| 135 | ); |
| 136 | |
| 137 | const DesktopBackendOutputLogNoop: DesktopBackendOutputLogShape = { |
| 138 | beginSession: () => Effect.void, |
| 139 | writeOutputChunk: () => Effect.void, |
| 140 | persistFailureSnapshot: () => Effect.void, |
| 141 | persistFailure: () => Effect.void, |
| 142 | discardSession: Effect.void, |
| 143 | }; |
| 144 | |
| 145 | interface BufferedBackendOutputChunk { |
| 146 | readonly streamName: "stdout" | "stderr"; |
| 147 | readonly chunk: Uint8Array; |
| 148 | readonly offset: number; |
| 149 | } |
| 150 | |
| 151 | interface BackendOutputSession { |
| 152 | readonly runId: string; |
| 153 | readonly startDetails: string; |
| 154 | readonly chunks: ReadonlyArray<BufferedBackendOutputChunk>; |
| 155 | readonly byteLength: number; |
| 156 | } |
| 157 | |
| 158 | function appendBoundedOutputChunk( |
| 159 | session: BackendOutputSession, |
| 160 | streamName: "stdout" | "stderr", |
| 161 | chunk: Uint8Array, |
| 162 | ): BackendOutputSession { |
| 163 | if (chunk.byteLength === 0) { |
| 164 | return session; |
| 165 | } |
| 166 | |
| 167 | const retainedChunk = |
| 168 | chunk.byteLength > DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES |
| 169 | ? chunk.slice(chunk.byteLength - DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES) |
| 170 | : chunk.slice(); |
| 171 | const chunks = [...session.chunks, { streamName, chunk: retainedChunk, offset: 0 }]; |
| 172 | let byteLength = session.byteLength + retainedChunk.byteLength; |
| 173 | let overflow = Math.max(0, byteLength - DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_BYTES); |
| 174 | let firstRetainedIndex = 0; |
| 175 | |
| 176 | while (overflow > 0) { |
| 177 | const first = chunks[firstRetainedIndex]; |
| 178 | if (!first) break; |
| 179 | const retainedByteLength = first.chunk.byteLength - first.offset; |
| 180 | if (retainedByteLength <= overflow) { |
| 181 | overflow -= retainedByteLength; |
| 182 | byteLength -= retainedByteLength; |
| 183 | firstRetainedIndex += 1; |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | chunks[firstRetainedIndex] = { |
| 188 | ...first, |
| 189 | offset: first.offset + overflow, |
| 190 | }; |
| 191 | byteLength -= overflow; |
| 192 | overflow = 0; |
| 193 | } |
| 194 | |
| 195 | const excessChunks = Math.max( |
| 196 | 0, |
| 197 | chunks.length - firstRetainedIndex - DESKTOP_BACKEND_OUTPUT_BUFFER_MAX_CHUNKS, |
| 198 | ); |
| 199 | for (let index = firstRetainedIndex; index < firstRetainedIndex + excessChunks; index += 1) { |
| 200 | const chunk = chunks[index]; |
| 201 | byteLength -= chunk ? chunk.chunk.byteLength - chunk.offset : 0; |
| 202 | } |
| 203 | firstRetainedIndex += excessChunks; |
| 204 | |
| 205 | return { |
| 206 | ...session, |
| 207 | chunks: chunks.slice(firstRetainedIndex), |
| 208 | byteLength, |
| 209 | }; |
| 210 | } |
| 211 | |
| 212 | const currentDesktopRunId = Effect.gen(function* () { |
| 213 | const annotations = yield* References.CurrentLogAnnotations; |
| 214 | const runId = annotations.runId; |
| 215 | return typeof runId === "string" && runId.length > 0 ? runId : "unknown"; |
| 216 | }); |
| 217 | |
| 218 | const refreshFileSize = ( |
| 219 | fileSystem: FileSystem.FileSystem, |
| 220 | filePath: string, |
| 221 | ): Effect.Effect<number, never> => |
| 222 | fileSystem.stat(filePath).pipe( |
| 223 | Effect.map((stat) => Number(stat.size)), |
| 224 | Effect.orElseSucceed(() => 0), |
| 225 | ); |
| 226 | |
| 227 | export const makeRotatingLogFileWriter = Effect.fn("makeRotatingLogFileWriter")(function* (input: { |
| 228 | readonly filePath: string; |
| 229 | readonly maxBytes?: number; |
| 230 | readonly maxFiles?: number; |
| 231 | }): Effect.fn.Return< |
| 232 | RotatingLogFileWriter, |
| 233 | DesktopLogFileWriterError, |
| 234 | FileSystem.FileSystem | Path.Path |
| 235 | > { |
| 236 | const fileSystem = yield* FileSystem.FileSystem; |
| 237 | const path = yield* Path.Path; |
| 238 | const maxBytes = input.maxBytes ?? DESKTOP_LOG_FILE_MAX_BYTES; |
| 239 | const maxFiles = input.maxFiles ?? DESKTOP_LOG_FILE_MAX_FILES; |
| 240 | const directory = path.dirname(input.filePath); |
| 241 | const baseName = path.basename(input.filePath); |
| 242 | |
| 243 | if (maxBytes < 1) { |
| 244 | return yield* new DesktopLogFileWriterConfigurationError({ |
| 245 | option: "maxBytes", |
| 246 | value: maxBytes, |
| 247 | }); |
| 248 | } |
| 249 | if (maxFiles < 1) { |
| 250 | return yield* new DesktopLogFileWriterConfigurationError({ |
| 251 | option: "maxFiles", |
| 252 | value: maxFiles, |
| 253 | }); |
| 254 | } |
| 255 | |
| 256 | yield* fileSystem.makeDirectory(directory, { recursive: true }); |
| 257 | |
| 258 | const withSuffix = (index: number) => `${input.filePath}.${index}`; |
| 259 | const currentSize = yield* Ref.make(yield* refreshFileSize(fileSystem, input.filePath)); |
| 260 | const mutex = yield* Semaphore.make(1); |
| 261 | |
| 262 | const pruneOverflowBackups = Effect.gen(function* () { |
| 263 | const entries = yield* fileSystem.readDirectory(directory).pipe(Effect.orElseSucceed(() => [])); |
| 264 | for (const entry of entries) { |
| 265 | if (!entry.startsWith(`${baseName}.`)) continue; |
| 266 | const suffixText = entry.slice(baseName.length + 1); |
| 267 | const suffix = Number(suffixText); |
| 268 | if (!Number.isInteger(suffix) || String(suffix) !== suffixText || suffix <= maxFiles) |
| 269 | continue; |
| 270 | yield* fileSystem.remove(path.join(directory, entry), { force: true }).pipe(Effect.ignore); |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | const rotate = Effect.gen(function* () { |
| 275 | yield* fileSystem.remove(withSuffix(maxFiles), { force: true }).pipe(Effect.ignore); |
| 276 | for (let index = maxFiles - 1; index >= 1; index -= 1) { |
| 277 | const source = withSuffix(index); |
| 278 | const sourceExists = yield* fileSystem.exists(source).pipe(Effect.orElseSucceed(() => false)); |
| 279 | if (sourceExists) { |
| 280 | yield* fileSystem.rename(source, withSuffix(index + 1)); |
| 281 | } |
| 282 | } |
| 283 | const currentExists = yield* fileSystem |
| 284 | .exists(input.filePath) |
| 285 | .pipe(Effect.orElseSucceed(() => false)); |
| 286 | if (currentExists) { |
| 287 | yield* fileSystem.rename(input.filePath, withSuffix(1)); |
| 288 | } |
| 289 | yield* Ref.set(currentSize, 0); |
| 290 | }).pipe( |
| 291 | Effect.catch(() => |
| 292 | refreshFileSize(fileSystem, input.filePath).pipe( |
| 293 | Effect.flatMap((size) => Ref.set(currentSize, size)), |
| 294 | ), |
| 295 | ), |
| 296 | ); |
| 297 | |
| 298 | const writeBytes = (chunk: Uint8Array): Effect.Effect<void> => { |
| 299 | if (chunk.byteLength === 0) return Effect.void; |
| 300 | |
| 301 | return mutex.withPermits(1)( |
| 302 | Effect.gen(function* () { |
| 303 | const beforeSize = yield* Ref.get(currentSize); |
| 304 | if (beforeSize > 0 && beforeSize + chunk.byteLength > maxBytes) { |
| 305 | yield* rotate; |
| 306 | } |
| 307 | |
| 308 | yield* fileSystem.writeFile(input.filePath, chunk, { flag: "a" }); |
| 309 | const afterSize = (yield* Ref.get(currentSize)) + chunk.byteLength; |
| 310 | yield* Ref.set(currentSize, afterSize); |
| 311 | |
| 312 | if (afterSize > maxBytes) { |
| 313 | yield* rotate; |
| 314 | } |
| 315 | }).pipe( |
| 316 | Effect.catch(() => |
| 317 | refreshFileSize(fileSystem, input.filePath).pipe( |
| 318 | Effect.flatMap((size) => Ref.set(currentSize, size)), |
| 319 | ), |
| 320 | ), |
| 321 | ), |
| 322 | ); |
| 323 | }; |
| 324 | |
| 325 | yield* pruneOverflowBackups; |
| 326 | |
| 327 | return { |
| 328 | writeBytes, |
| 329 | writeText: (chunk) => writeBytes(textEncoder.encode(chunk)), |
| 330 | } satisfies RotatingLogFileWriter; |
| 331 | }); |
| 332 | |
| 333 | const noPersistedObservabilitySettings: PersistedServerObservabilitySettings = { |
| 334 | otlpTracesUrl: undefined, |
| 335 | otlpMetricsUrl: undefined, |
| 336 | otlpLogsUrl: undefined, |
| 337 | }; |
| 338 | |
| 339 | const readPersistedObservabilitySettings: Effect.Effect< |
| 340 | PersistedServerObservabilitySettings, |
| 341 | never, |
| 342 | FileSystem.FileSystem | DesktopEnvironment.DesktopEnvironment |
| 343 | > = Effect.gen(function* () { |
| 344 | const fileSystem = yield* FileSystem.FileSystem; |
| 345 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 346 | const raw = yield* fileSystem.readFileString(environment.serverSettingsPath).pipe(Effect.option); |
| 347 | return Option.isNone(raw) |
| 348 | ? noPersistedObservabilitySettings |
| 349 | : parsePersistedServerObservabilitySettings(raw.value); |
| 350 | }); |
| 351 | |
| 352 | const resolveOtlpEndpoints = Effect.gen(function* () { |
| 353 | const otel = yield* OtelEnvironment.load; |
| 354 | if (otel.disabled) { |
| 355 | return { |
| 356 | traces: undefined, |
| 357 | metrics: undefined, |
| 358 | logs: undefined, |
| 359 | warnings: otel.warnings, |
| 360 | resourceAttributes: otel.resourceAttributes, |
| 361 | }; |
| 362 | } |
| 363 | |
| 364 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 365 | const persisted = yield* readPersistedObservabilitySettings; |
| 366 | const signalExport: SignalExport = { |
| 367 | protocol: environment.otlpProtocol, |
| 368 | headers: Option.getOrUndefined(environment.otlpHeaders), |
| 369 | exportIntervalMs: environment.otlpExportIntervalMs, |
| 370 | }; |
| 371 | return { |
| 372 | traces: OtelEnvironment.resolveSignalEndpoint( |
| 373 | otel, |
| 374 | "traces", |
| 375 | { url: Option.getOrUndefined(environment.otlpTracesUrl), export: signalExport }, |
| 376 | { bootstrapUrl: undefined, settingsUrl: persisted.otlpTracesUrl }, |
| 377 | ), |
| 378 | metrics: OtelEnvironment.resolveSignalEndpoint( |
| 379 | otel, |
| 380 | "metrics", |
| 381 | { url: Option.getOrUndefined(environment.otlpMetricsUrl), export: signalExport }, |
| 382 | { bootstrapUrl: undefined, settingsUrl: persisted.otlpMetricsUrl }, |
| 383 | ), |
| 384 | logs: OtelEnvironment.resolveSignalEndpoint( |
| 385 | otel, |
| 386 | "logs", |
| 387 | { url: Option.getOrUndefined(environment.otlpLogsUrl), export: signalExport }, |
| 388 | { bootstrapUrl: undefined, settingsUrl: persisted.otlpLogsUrl }, |
| 389 | ), |
| 390 | warnings: otel.warnings, |
| 391 | resourceAttributes: otel.resourceAttributes, |
| 392 | }; |
| 393 | }); |
| 394 | |
| 395 | const writeDevelopmentConsoleOutput = ( |
| 396 | streamName: "stdout" | "stderr", |
| 397 | chunk: Uint8Array, |
| 398 | ): Effect.Effect<void> => |
| 399 | Effect.sync(() => { |
| 400 | const output = streamName === "stderr" ? process.stderr : process.stdout; |
| 401 | output.write(chunk); |
| 402 | }).pipe(Effect.ignore); |
| 403 | |
| 404 | const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackendChildLogRecord")( |
| 405 | function* ( |
| 406 | logFile: RotatingLogFileWriter, |
| 407 | input: { |
| 408 | readonly message: string; |
| 409 | readonly level: "INFO" | "ERROR"; |
| 410 | readonly annotations: Record<string, unknown>; |
| 411 | }, |
| 412 | ): Effect.fn.Return<void> { |
| 413 | return yield* Effect.gen(function* () { |
| 414 | const timestamp = DateTime.formatIso(yield* DateTime.now); |
| 415 | const encoded = yield* encodeDesktopBackendChildLogRecord({ |
| 416 | message: input.message, |
| 417 | level: input.level, |
| 418 | timestamp, |
| 419 | annotations: input.annotations, |
| 420 | spans: {}, |
| 421 | fiberId: DESKTOP_BACKEND_CHILD_LOG_FIBER_ID, |
| 422 | }); |
| 423 | yield* logFile.writeText(`${encoded}\n`); |
| 424 | }).pipe(Effect.ignore({ log: true })); |
| 425 | }, |
| 426 | ); |
| 427 | |
| 428 | const PRIMARY_BACKEND_LOG_INSTANCE_ID = PRIMARY_LOCAL_ENVIRONMENT_ID; |
| 429 | |
| 430 | const sanitizeInstanceIdForFileName = (id: string): string => id.replace(/[^a-zA-Z0-9._-]+/g, "_"); |
| 431 | |
| 432 | const backendLogFilePathForInstance = ( |
| 433 | environment: DesktopEnvironment.DesktopEnvironment["Service"], |
| 434 | id: string, |
| 435 | ): string => { |
| 436 | // Keep the primary log path stable for ops scripts and packaged-build inspection. |
| 437 | if (id === PRIMARY_BACKEND_LOG_INSTANCE_ID) { |
| 438 | return environment.path.join(environment.logDir, "server-child.log"); |
| 439 | } |
| 440 | const sanitized = sanitizeInstanceIdForFileName(id); |
| 441 | return environment.path.join(environment.logDir, `server-child-${sanitized}.log`); |
| 442 | }; |
| 443 | |
| 444 | // IDs sharing a sanitized filename must share a writer to avoid currentSize races. |
| 445 | const makeBackendOutputSinkForInstance = ( |
| 446 | environment: DesktopEnvironment.DesktopEnvironment["Service"], |
| 447 | id: string, |
| 448 | ): Effect.Effect< |
| 449 | Option.Option<RotatingLogFileWriter>, |
| 450 | never, |
| 451 | FileSystem.FileSystem | Path.Path | Scope.Scope |
| 452 | > => |
| 453 | makeRotatingLogFileWriter({ |
| 454 | filePath: backendLogFilePathForInstance(environment, id), |
| 455 | }).pipe(Effect.option); |
| 456 | |
| 457 | const makeBackendOutputLogShape = ( |
| 458 | environment: DesktopEnvironment.DesktopEnvironment["Service"], |
| 459 | id: string, |
| 460 | sink: Option.Option<RotatingLogFileWriter>, |
| 461 | ): Effect.Effect<DesktopBackendOutputLogShape> => |
| 462 | Option.match(sink, { |
| 463 | onNone: () => Effect.succeed(DesktopBackendOutputLogNoop), |
| 464 | // The enclosing operation owns the span for this inline handler. |
| 465 | onSome: Effect.fnUntraced(function* (logFile) { |
| 466 | const sessionRef = yield* Ref.make(Option.none<BackendOutputSession>()); |
| 467 | const writeFailure = Effect.fn("desktop.observability.backendOutput.writeFailure")(function* ( |
| 468 | session: BackendOutputSession, |
| 469 | details: string, |
| 470 | ) { |
| 471 | yield* writeBackendChildLogRecord(logFile, { |
| 472 | message: "backend child process failure output start", |
| 473 | level: "ERROR", |
| 474 | annotations: { |
| 475 | component: "desktop-backend-child", |
| 476 | runId: session.runId, |
| 477 | instanceId: id, |
| 478 | phase: "START", |
| 479 | details: session.startDetails, |
| 480 | }, |
| 481 | }); |
| 482 | for (const output of session.chunks) { |
| 483 | yield* writeBackendChildLogRecord(logFile, { |
| 484 | message: "backend child process output", |
| 485 | level: output.streamName === "stderr" ? "ERROR" : "INFO", |
| 486 | annotations: { |
| 487 | component: "desktop-backend-child", |
| 488 | runId: session.runId, |
| 489 | instanceId: id, |
| 490 | stream: output.streamName, |
| 491 | text: textDecoder.decode(output.chunk.subarray(output.offset)), |
| 492 | }, |
| 493 | }); |
| 494 | } |
| 495 | yield* writeBackendChildLogRecord(logFile, { |
| 496 | message: "backend child process failure output end", |
| 497 | level: "ERROR", |
| 498 | annotations: { |
| 499 | component: "desktop-backend-child", |
| 500 | runId: session.runId, |
| 501 | instanceId: id, |
| 502 | phase: "END", |
| 503 | details: sanitizeLogValue(details), |
| 504 | }, |
| 505 | }); |
| 506 | }); |
| 507 | return { |
| 508 | beginSession: Effect.fn("desktop.observability.backendOutput.beginSession")(function* ({ |
| 509 | details, |
| 510 | }) { |
| 511 | const runId = yield* currentDesktopRunId; |
| 512 | yield* Ref.set( |
| 513 | sessionRef, |
| 514 | Option.some({ |
| 515 | runId, |
| 516 | startDetails: sanitizeLogValue(details), |
| 517 | chunks: [], |
| 518 | byteLength: 0, |
| 519 | }), |
| 520 | ); |
| 521 | }), |
| 522 | writeOutputChunk: Effect.fnUntraced(function* (streamName, chunk) { |
| 523 | if (environment.isDevelopment) { |
| 524 | yield* writeDevelopmentConsoleOutput(streamName, chunk); |
| 525 | } |
| 526 | yield* Ref.update( |
| 527 | sessionRef, |
| 528 | Option.map((session) => appendBoundedOutputChunk(session, streamName, chunk)), |
| 529 | ); |
| 530 | }), |
| 531 | persistFailureSnapshot: Effect.fn( |
| 532 | "desktop.observability.backendOutput.persistFailureSnapshot", |
| 533 | )(function* ({ details }) { |
| 534 | const session = yield* Ref.get(sessionRef); |
| 535 | if (Option.isSome(session)) { |
| 536 | yield* writeFailure(session.value, details); |
| 537 | } |
| 538 | }), |
| 539 | persistFailure: Effect.fn("desktop.observability.backendOutput.persistFailure")(function* ({ |
| 540 | details, |
| 541 | }) { |
| 542 | const session = yield* Ref.modify(sessionRef, (current) => [current, Option.none()]); |
| 543 | if (Option.isNone(session)) return; |
| 544 | yield* writeFailure(session.value, details); |
| 545 | }), |
| 546 | discardSession: Ref.set(sessionRef, Option.none()), |
| 547 | } satisfies DesktopBackendOutputLogShape; |
| 548 | }), |
| 549 | }); |
| 550 | |
| 551 | const layerBackendOutputLogFactory = Layer.effect( |
| 552 | DesktopBackendOutputLogFactory, |
| 553 | Effect.gen(function* () { |
| 554 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 555 | const fileSystem = yield* FileSystem.FileSystem; |
| 556 | const path = yield* Path.Path; |
| 557 | const factoryScope = yield* Scope.Scope; |
| 558 | const cacheRef = yield* SynchronizedRef.make< |
| 559 | ReadonlyMap<string, Option.Option<RotatingLogFileWriter>> |
| 560 | >(new Map()); |
| 561 | |
| 562 | const makeForId = (id: string): Effect.Effect<DesktopBackendOutputLogShape> => |
| 563 | SynchronizedRef.modifyEffect(cacheRef, (cache) => { |
| 564 | const cacheKey = backendLogFilePathForInstance(environment, id); |
| 565 | const cached = cache.get(cacheKey); |
| 566 | if (cached !== undefined) { |
| 567 | return makeBackendOutputLogShape(environment, id, cached).pipe( |
| 568 | Effect.map((outputLog) => [outputLog, cache] as const), |
| 569 | ); |
| 570 | } |
| 571 | return makeBackendOutputSinkForInstance(environment, id).pipe( |
| 572 | Effect.provideService(FileSystem.FileSystem, fileSystem), |
| 573 | Effect.provideService(Path.Path, path), |
| 574 | Scope.provide(factoryScope), |
| 575 | Effect.map((sink) => { |
| 576 | const next = new Map(cache); |
| 577 | next.set(cacheKey, sink); |
| 578 | return { sink, next }; |
| 579 | }), |
| 580 | Effect.flatMap(({ sink, next }) => |
| 581 | makeBackendOutputLogShape(environment, id, sink).pipe( |
| 582 | Effect.map( |
| 583 | (outputLog) => |
| 584 | [ |
| 585 | outputLog, |
| 586 | next as ReadonlyMap<string, Option.Option<RotatingLogFileWriter>>, |
| 587 | ] as const, |
| 588 | ), |
| 589 | ), |
| 590 | ), |
| 591 | ); |
| 592 | }); |
| 593 | |
| 594 | return DesktopBackendOutputLogFactory.of({ |
| 595 | forInstance: Effect.fn("DesktopBackendOutputLogFactory.forInstance")(function (id) { |
| 596 | return makeForId(id); |
| 597 | }), |
| 598 | }); |
| 599 | }), |
| 600 | ); |
| 601 | |
| 602 | const layerTelemetry = Layer.unwrap( |
| 603 | Effect.gen(function* () { |
| 604 | const environment = yield* DesktopEnvironment.DesktopEnvironment; |
| 605 | const endpoints = yield* resolveOtlpEndpoints; |
| 606 | // Unpackaged runs launch a script, so Electron reports its own version. |
| 607 | const serviceVersion = environment.isPackaged |
| 608 | ? environment.appVersion |
| 609 | : desktopPackageJson.version; |
| 610 | const resource = { |
| 611 | serviceName: "t3code-desktop", |
| 612 | serviceVersion, |
| 613 | attributes: { |
| 614 | "service.namespace": "t3code", |
| 615 | "service.runtime": "desktop", |
| 616 | "t3code.desktop.mode": environment.isDevelopment ? "development" : "packaged", |
| 617 | "service.instance.id": processServiceInstanceId(endpoints.resourceAttributes), |
| 618 | ...releaseResourceAttributes( |
| 619 | environment.isDevelopment ? "dev" : releaseChannelOf(serviceVersion), |
| 620 | endpoints.resourceAttributes, |
| 621 | ), |
| 622 | "host.name": endpoints.resourceAttributes["host.name"] || (yield* HostProcess.Hostname), |
| 623 | ...nodeProcessRuntimeAttributes(), |
| 624 | }, |
| 625 | }; |
| 626 | |
| 627 | // Logger.layer replaces the logger set. tracerLogger would duplicate OTLP in-span messages. |
| 628 | const layerLogger = Logger.layer( |
| 629 | endpoints.logs === undefined |
| 630 | ? [Logger.consolePretty(), Logger.tracerLogger] |
| 631 | : [ |
| 632 | Logger.consolePretty(), |
| 633 | OtlpLogger.make({ |
| 634 | url: endpoints.logs.url, |
| 635 | exportInterval: `${endpoints.logs.export.exportIntervalMs} millis`, |
| 636 | headers: endpoints.logs.export.headers, |
| 637 | resource, |
| 638 | }), |
| 639 | ], |
| 640 | { mergeWithExisting: false }, |
| 641 | ).pipe( |
| 642 | Layer.provide(OtlpExporter.layerFlusher), |
| 643 | Layer.provide( |
| 644 | SharedObservability.layerOtlpSerialization( |
| 645 | endpoints.logs?.export.protocol ?? environment.otlpProtocol, |
| 646 | ), |
| 647 | ), |
| 648 | ); |
| 649 | |
| 650 | const layerTracer = Layer.unwrap( |
| 651 | Effect.gen(function* () { |
| 652 | const tracePath = environment.path.join(environment.logDir, "desktop.trace.ndjson"); |
| 653 | const sink = yield* makeTraceSink({ |
| 654 | filePath: tracePath, |
| 655 | maxBytes: DESKTOP_LOG_FILE_MAX_BYTES, |
| 656 | maxFiles: DESKTOP_LOG_FILE_MAX_FILES, |
| 657 | batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS, |
| 658 | }); |
| 659 | // Unscoped: Electron can exit without closing the app scope, e.g. when the display drops. |
| 660 | process.once("exit", sink.flushSync); |
| 661 | const delegate = |
| 662 | endpoints.traces === undefined |
| 663 | ? undefined |
| 664 | : yield* OtlpTracer.make({ |
| 665 | url: endpoints.traces.url, |
| 666 | exportInterval: `${endpoints.traces.export.exportIntervalMs} millis`, |
| 667 | headers: endpoints.traces.export.headers, |
| 668 | resource, |
| 669 | }).pipe( |
| 670 | Effect.provide( |
| 671 | SharedObservability.layerOtlpSerialization(endpoints.traces.export.protocol), |
| 672 | ), |
| 673 | ); |
| 674 | const tracer = yield* makeLocalFileTracer({ |
| 675 | filePath: tracePath, |
| 676 | maxBytes: DESKTOP_LOG_FILE_MAX_BYTES, |
| 677 | maxFiles: DESKTOP_LOG_FILE_MAX_FILES, |
| 678 | batchWindowMs: DESKTOP_TRACE_BATCH_WINDOW_MS, |
| 679 | sink, |
| 680 | ...(delegate ? { delegate } : {}), |
| 681 | }); |
| 682 | |
| 683 | return Layer.succeed(Tracer.Tracer, tracer); |
| 684 | }), |
| 685 | ).pipe(Layer.provide(OtlpExporter.layerFlusher)); |
| 686 | |
| 687 | const layerOtelWarnings = Layer.effectDiscard( |
| 688 | Effect.forEach(endpoints.warnings, (warning) => Effect.logWarning(warning)), |
| 689 | ); |
| 690 | |
| 691 | return layerOtelWarnings.pipe( |
| 692 | Layer.provideMerge(Layer.mergeAll(layerLogger, layerTracer)), |
| 693 | Layer.provide(OtelEnvironment.layerResourceAttributes(endpoints.resourceAttributes)), |
| 694 | ); |
| 695 | }), |
| 696 | ); |
| 697 | |
| 698 | export const layer = Layer.mergeAll( |
| 699 | layerBackendOutputLogFactory, |
| 700 | layerTelemetry, |
| 701 | Layer.succeed(References.MinimumLogLevel, "Info"), |
| 702 | Layer.succeed(Tracer.MinimumTraceLevel, "Info"), |
| 703 | Layer.succeed(References.TracerTimingEnabled, true), |
| 704 | ); |