feat(desktop): route backend child output through a per-instance log factory
Backend child output and session boundaries used to land in one shared server-child.log via the DesktopBackendOutputLog singleton. With a second backend instance on the way, intermixing two processes' stdout streams into one file makes triage harder than it needs to be. Replace the singleton with DesktopBackendOutputLogFactory.forInstance(id) that vends a rotating writer per backend id. The primary keeps the historical server-child.log path so existing ops tooling, packaged-build log inspection, and habit don't break; non-primary instances land in server-child-<sanitized-id>.log. A SynchronizedRef-backed cache keyed by id ensures repeated forInstance lookups on the same id reuse the writer (important under restart loops that re-resolve the factory). Each emitted record now carries an instanceId annotation so cross-file greps can still associate records belonging to the same backend even if log paths drift later. The redundant "instance=<id>" prefix on session boundary details is dropped — that info now lives in the structured annotation and in the file path. DesktopBackendManager pulls its writer from the factory at instance construction time so each instance carries a fixed writer for its lifetime. DesktopBackendManager.test.ts stubs the factory directly; DesktopObservability.test.ts asserts the new instanceId annotation.
This commit is contained in:
1 parent
425c7d0b32
commit
563820ed75
5 files changed
+115
-31
No files matched your search
@@ -125,7 +125,8 @@ describe("DesktopObservability", () => {
|
||||
}).pipe(Effect.provide(environmentLayer));
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
const outputLog = yield* DesktopObservability.DesktopBackendOutputLog;
|
||||
const factory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
|
||||
const outputLog = yield* factory.forInstance("primary");
|
||||
yield* outputLog.writeSessionBoundary({
|
||||
phase: "START",
|
||||
details: "pid=123 port=3773 cwd=/repo",
|
||||
@@ -145,6 +146,7 @@ describe("DesktopObservability", () => {
|
||||
assert.equal(boundary.level, "INFO");
|
||||
assert.equal(boundary.annotations.component, "desktop-backend-child");
|
||||
assert.equal(boundary.annotations.runId, "test-run");
|
||||
assert.equal(boundary.annotations.instanceId, "primary");
|
||||
assert.equal(boundary.annotations.phase, "START");
|
||||
assert.equal(boundary.annotations.details, "pid=123 port=3773 cwd=/repo");
|
||||
|
||||
@@ -152,6 +154,7 @@ describe("DesktopObservability", () => {
|
||||
assert.equal(output.level, "INFO");
|
||||
assert.equal(output.annotations.component, "desktop-backend-child");
|
||||
assert.equal(output.annotations.runId, "test-run");
|
||||
assert.equal(output.annotations.instanceId, "primary");
|
||||
assert.equal(output.annotations.stream, "stdout");
|
||||
assert.equal(output.annotations.text, "hello server\n");
|
||||
}).pipe(
|
||||
|
||||
@@ -13,7 +13,9 @@ import * as PlatformError from "effect/PlatformError";
|
||||
import * as References from "effect/References";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Scope from "effect/Scope";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import * as SynchronizedRef from "effect/SynchronizedRef";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
import { OtlpSerialization, OtlpTracer } from "effect/unstable/observability";
|
||||
|
||||
@@ -40,10 +42,23 @@ export interface DesktopBackendOutputLogShape {
|
||||
) => Effect.Effect<void>;
|
||||
}
|
||||
|
||||
export class DesktopBackendOutputLog extends Context.Service<
|
||||
DesktopBackendOutputLog,
|
||||
DesktopBackendOutputLogShape
|
||||
>()("t3/desktop/BackendOutputLog") {}
|
||||
// Factory for per-instance backend output logs. `forInstance(id)` returns
|
||||
// a writer that targets a distinct rotating log file — the primary
|
||||
// instance keeps `server-child.log` so the historical path stays stable
|
||||
// for ops; other instances get `server-child-<sanitized-id>.log`.
|
||||
//
|
||||
// Writers are cached per id within a single factory instance so repeated
|
||||
// `forInstance` calls (e.g. during a backend restart that re-resolves
|
||||
// services) reuse the same rotating writer rather than racing each other
|
||||
// on the same file.
|
||||
export interface DesktopBackendOutputLogFactoryShape {
|
||||
readonly forInstance: (id: string) => Effect.Effect<DesktopBackendOutputLogShape>;
|
||||
}
|
||||
|
||||
export class DesktopBackendOutputLogFactory extends Context.Service<
|
||||
DesktopBackendOutputLogFactory,
|
||||
DesktopBackendOutputLogFactoryShape
|
||||
>()("t3/desktop/BackendOutputLogFactory") {}
|
||||
|
||||
const textEncoder = new TextEncoder();
|
||||
const textDecoder = new TextDecoder();
|
||||
@@ -293,13 +308,34 @@ const writeBackendChildLogRecord = Effect.fn("desktop.observability.writeBackend
|
||||
},
|
||||
);
|
||||
|
||||
const backendOutputLogLayer = Layer.effect(
|
||||
DesktopBackendOutputLog,
|
||||
Effect.gen(function* () {
|
||||
const environment = yield* DesktopEnvironment.DesktopEnvironment;
|
||||
const PRIMARY_BACKEND_LOG_INSTANCE_ID = "primary";
|
||||
|
||||
const sanitizeInstanceIdForFileName = (id: string): string => id.replace(/[^a-zA-Z0-9._-]+/g, "_");
|
||||
|
||||
const backendLogFilePathForInstance = (
|
||||
environment: DesktopEnvironment.DesktopEnvironmentShape,
|
||||
id: string,
|
||||
): string => {
|
||||
// Primary keeps the historical "server-child.log" path so ops scripts
|
||||
// and packaged-build log inspection still find it where it always lived.
|
||||
if (id === PRIMARY_BACKEND_LOG_INSTANCE_ID) {
|
||||
return environment.path.join(environment.logDir, "server-child.log");
|
||||
}
|
||||
const sanitized = sanitizeInstanceIdForFileName(id);
|
||||
return environment.path.join(environment.logDir, `server-child-${sanitized}.log`);
|
||||
};
|
||||
|
||||
const makeBackendOutputLogForInstance = (
|
||||
environment: DesktopEnvironment.DesktopEnvironmentShape,
|
||||
id: string,
|
||||
): Effect.Effect<
|
||||
DesktopBackendOutputLogShape,
|
||||
never,
|
||||
FileSystem.FileSystem | Path.Path | Scope.Scope
|
||||
> =>
|
||||
Effect.gen(function* () {
|
||||
const writer = yield* makeRotatingLogFileWriter({
|
||||
filePath: environment.path.join(environment.logDir, "server-child.log"),
|
||||
filePath: backendLogFilePathForInstance(environment, id),
|
||||
}).pipe(Effect.option);
|
||||
|
||||
return Option.match(writer, {
|
||||
@@ -316,6 +352,7 @@ const backendOutputLogLayer = Layer.effect(
|
||||
annotations: {
|
||||
component: "desktop-backend-child",
|
||||
runId,
|
||||
instanceId: id,
|
||||
phase,
|
||||
details: sanitizeLogValue(details),
|
||||
},
|
||||
@@ -333,6 +370,7 @@ const backendOutputLogLayer = Layer.effect(
|
||||
annotations: {
|
||||
component: "desktop-backend-child",
|
||||
runId,
|
||||
instanceId: id,
|
||||
stream: streamName,
|
||||
text: textDecoder.decode(chunk),
|
||||
},
|
||||
@@ -341,6 +379,43 @@ const backendOutputLogLayer = Layer.effect(
|
||||
),
|
||||
}) satisfies DesktopBackendOutputLogShape,
|
||||
});
|
||||
});
|
||||
|
||||
const backendOutputLogFactoryLayer = Layer.effect(
|
||||
DesktopBackendOutputLogFactory,
|
||||
Effect.gen(function* () {
|
||||
const environment = yield* DesktopEnvironment.DesktopEnvironment;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const factoryScope = yield* Scope.Scope;
|
||||
// Per-id cache so repeated forInstance(id) calls reuse the same
|
||||
// rotating writer instead of opening a second handle on the same
|
||||
// file. Each writer pins itself to the factory's scope so all log
|
||||
// resources tear down together at app exit. Mutex serializes
|
||||
// concurrent first-time lookups for the same id.
|
||||
const cacheRef = yield* SynchronizedRef.make<ReadonlyMap<string, DesktopBackendOutputLogShape>>(
|
||||
new Map(),
|
||||
);
|
||||
|
||||
const makeForId = (id: string): Effect.Effect<DesktopBackendOutputLogShape> =>
|
||||
SynchronizedRef.modifyEffect(cacheRef, (cache) => {
|
||||
const cached = cache.get(id);
|
||||
if (cached !== undefined) return Effect.succeed([cached, cache] as const);
|
||||
return makeBackendOutputLogForInstance(environment, id).pipe(
|
||||
Effect.provideService(FileSystem.FileSystem, fileSystem),
|
||||
Effect.provideService(Path.Path, path),
|
||||
Scope.provide(factoryScope),
|
||||
Effect.map((shape) => {
|
||||
const next = new Map(cache);
|
||||
next.set(id, shape);
|
||||
return [shape, next as ReadonlyMap<string, DesktopBackendOutputLogShape>] as const;
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
return DesktopBackendOutputLogFactory.of({
|
||||
forInstance: (id) => makeForId(id),
|
||||
});
|
||||
}),
|
||||
);
|
||||
|
||||
@@ -387,7 +462,7 @@ const tracerLayer = Layer.unwrap(
|
||||
).pipe(Layer.provideMerge(OtlpSerialization.layerJson));
|
||||
|
||||
export const layer = Layer.mergeAll(
|
||||
backendOutputLogLayer,
|
||||
backendOutputLogFactoryLayer,
|
||||
desktopLoggerLayer,
|
||||
tracerLayer,
|
||||
Layer.succeed(Tracer.MinimumTraceLevel, "Info"),
|
||||
|
||||
@@ -113,21 +113,24 @@ interface MakeInstanceInput {
|
||||
|
||||
// Helper that constructs a primary backend instance using the factory
|
||||
// directly. The factory's deps (FileSystem, ChildProcessSpawner,
|
||||
// HttpClient, DesktopBackendOutputLog) are provided per-test via a
|
||||
// scoped layer; tests yield the returned Effect inside `Effect.scoped`
|
||||
// HttpClient, DesktopBackendOutputLogFactory) are provided per-test via
|
||||
// a scoped layer; tests yield the returned Effect inside `Effect.scoped`
|
||||
// to drive the instance's lifecycle.
|
||||
function makeTestInstance(input: MakeInstanceInput) {
|
||||
const stubLog: DesktopObservability.DesktopBackendOutputLogShape = {
|
||||
writeSessionBoundary: () => Effect.void,
|
||||
writeOutputChunk: () => Effect.void,
|
||||
...input.backendOutputLog,
|
||||
};
|
||||
const servicesLayer = Layer.mergeAll(
|
||||
FileSystem.layerNoop({
|
||||
exists: () => Effect.succeed(true),
|
||||
}),
|
||||
input.spawnerLayer,
|
||||
input.httpClientLayer ?? healthyHttpClientLayer,
|
||||
Layer.succeed(DesktopObservability.DesktopBackendOutputLog, {
|
||||
writeSessionBoundary: () => Effect.void,
|
||||
writeOutputChunk: () => Effect.void,
|
||||
...input.backendOutputLog,
|
||||
} satisfies DesktopObservability.DesktopBackendOutputLogShape),
|
||||
Layer.succeed(DesktopObservability.DesktopBackendOutputLogFactory, {
|
||||
forInstance: () => Effect.succeed(stubLog),
|
||||
} satisfies DesktopObservability.DesktopBackendOutputLogFactoryShape),
|
||||
);
|
||||
|
||||
const instance = DesktopBackendManager.makeBackendInstance({
|
||||
|
||||
@@ -18,9 +18,9 @@
|
||||
// readiness latch) only for instances that want them — the primary's
|
||||
// spec passes the window's handleBackendReady/handleBackendNotReady,
|
||||
// other pool instances pass nothing.
|
||||
// - log writes still go through the shared `DesktopBackendOutputLog`
|
||||
// service for now; step 3 of the migration will split that per
|
||||
// instance.
|
||||
// - log writes go through a per-instance writer that the factory
|
||||
// pulls from `DesktopBackendOutputLogFactory.forInstance(spec.id)`,
|
||||
// so each instance lands in its own rotating file.
|
||||
|
||||
import * as Brand from "effect/Brand";
|
||||
import * as Cause from "effect/Cause";
|
||||
@@ -342,12 +342,13 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
|
||||
| FileSystem.FileSystem
|
||||
| ChildProcessSpawner.ChildProcessSpawner
|
||||
| HttpClient.HttpClient
|
||||
| DesktopObservability.DesktopBackendOutputLog
|
||||
| DesktopObservability.DesktopBackendOutputLogFactory
|
||||
| Scope.Scope
|
||||
> {
|
||||
const parentScope = yield* Scope.Scope;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const backendOutputLog = yield* DesktopObservability.DesktopBackendOutputLog;
|
||||
const backendOutputLogFactory = yield* DesktopObservability.DesktopBackendOutputLogFactory;
|
||||
const backendOutputLog = yield* backendOutputLogFactory.forInstance(spec.id);
|
||||
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
|
||||
const httpClient = yield* HttpClient.HttpClient;
|
||||
const state = yield* Ref.make(initialState);
|
||||
@@ -483,7 +484,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
|
||||
if (Option.isSome(pid)) {
|
||||
yield* backendOutputLog.writeSessionBoundary({
|
||||
phase: "END",
|
||||
details: `instance=${spec.id} pid=${pid.value} ${reason}`,
|
||||
details: `pid=${pid.value} ${reason}`,
|
||||
});
|
||||
}
|
||||
yield* spec.onShutdown?.() ?? Effect.void;
|
||||
@@ -505,7 +506,7 @@ export const makeBackendInstance = Effect.fn("makeBackendInstance")(function* (
|
||||
}));
|
||||
yield* backendOutputLog.writeSessionBoundary({
|
||||
phase: "START",
|
||||
details: `instance=${spec.id} pid=${pid} port=${config.bootstrap.port} cwd=${config.cwd}`,
|
||||
details: `pid=${pid} port=${config.bootstrap.port} cwd=${config.cwd}`,
|
||||
});
|
||||
}),
|
||||
onReady: Effect.fn("desktop.backendInstance.onReady")(function* () {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// point for the concurrent-Windows+WSL-backend feature; see the design
|
||||
// notes below before extending it.
|
||||
//
|
||||
// Current state (step 2):
|
||||
// Current state (step 3):
|
||||
// - `DesktopBackendManager.ts` no longer exposes a Context.Service. It
|
||||
// is a per-instance factory (`makeBackendInstance(spec)`); the pool
|
||||
// calls it once for the Windows primary at startup.
|
||||
@@ -32,11 +32,13 @@
|
||||
// Migration sequence (each step is its own commit):
|
||||
// 1. Reshape `DesktopBackendManager` into an instance factory and route
|
||||
// consumers through the pool. Pool still holds a single instance.
|
||||
// 2. (this commit) Drop `DesktopState.backendReady`. The window owns
|
||||
// its own readiness latch, driven by the primary instance's
|
||||
// onReady/onShutdown callbacks.
|
||||
// 3. Per-instance log routing: split `DesktopBackendOutputLog` into a
|
||||
// factory keyed by instance id.
|
||||
// 2. Drop `DesktopState.backendReady`. The window owns its own
|
||||
// readiness latch, driven by the primary instance's onReady /
|
||||
// onShutdown callbacks.
|
||||
// 3. (this commit) Per-instance log routing: replace the singleton
|
||||
// DesktopBackendOutputLog with a factory that vends one rotating
|
||||
// writer per instance id (primary keeps server-child.log; others go
|
||||
// to server-child-<sanitized-id>.log).
|
||||
// 4. Add register/unregister so WSL backend can be added on demand.
|
||||
// 5. Wire WSL distro startup through the pool; remove `setWslBackend`
|
||||
// mode-swap IPC in favor of `enableWslBackend` / `setWslDistro`.
|
||||
|
||||
Reference in new issue
Block a user