@devries-cameron20 / t3code Mirror · read-only / commits / 26433ac070
Fork 0

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, 19:45 UTC

fix(server): boot service log is emptied in place once it passes 10 MiB (#34886)

Julius Marminge committed Nov 12, 2026 at 00:20 UTC 26433ac0701dd286438e24ca5b5b341c6f5313d7
4 files changed +104 -1
apps/server/src/cloud/bootService.test.ts
+13 -1
@@ -29,6 +29,7 @@ import {
29 SERVICE_LAUNCHER_PROTOCOL,
30 SERVICE_RESTART_PENDING_FILE,
31 serviceStateHasPendingUpdate,
32 + serviceUnitLogPathOf,
33 } from "./serviceProtocol.ts";
34
35 const leaseObserver = vi.hoisted(() => ({ contended: (_destination: string) => {} }));
@@ -55,7 +56,7 @@ const windowsTaskOptions = {
56 conhostPath: "C:\\Windows\\System32\\conhost.exe",
57 };
58
58 -it("reads the served T3 home back out of a rendered unit, plist, or task", () => {
59 +it("reads the served T3 home and log back out of a rendered unit, plist, or task", () => {
60 const plan = (baseDir: string) => ({
61 program: [`${baseDir}/runtime/versions/1.2.3/t3`, "__service-launcher"],
62 baseDir,
@@ -71,6 +72,17 @@ it("reads the served T3 home back out of a rendered unit, plist, or task", () =>
72 BootService.renderBootServiceUnit(plan("/home/theo/T3 Data/100%")),
73 ),
74 ).toBe("/home/theo/T3 Data/100%");
75 + expect(
76 + serviceUnitLogPathOf(BootService.renderBootServiceUnit(plan("/home/theo/T3 Data/100%"))),
77 + ).toBe("/home/theo/T3 Data/100%/userdata/logs/boot-service.log");
78 + expect(
79 + serviceUnitLogPathOf(
80 + BootService.renderBootServicePlist(plan("/Users/theo/a&b"), {
81 + homeDir: "/Users/theo",
82 + environmentPath: "/usr/bin",
83 + }),
84 + ),
85 + ).toBe("/Users/theo/a&b/userdata/logs/boot-service.log");
86 expect(
87 BootService.bootServiceBaseDirOf(
88 BootService.renderBootServicePlist(plan("/Users/theo/a&b"), {
apps/server/src/cloud/serviceProtocol.ts
+8
@@ -63,6 +63,14 @@ export function serviceUnitBaseDirOf(contents: string): string | undefined {
63 return task === undefined ? undefined : unquoteWindowsArgument(task);
64 }
65
66 +/** The file systemd or launchd appends the service's output to. */
67 +export function serviceUnitLogPathOf(contents: string): string | undefined {
68 + const systemd = /^StandardOutput=append:(.*)$/m.exec(contents)?.[1];
69 + if (systemd !== undefined) return unquoteSystemdValue(systemd.trim());
70 + const plist = /<key>StandardOutPath<\/key>\s*<string>([^<]*)<\/string>/.exec(contents)?.[1];
71 + return plist === undefined ? undefined : unescapeXmlText(plist);
72 +}
73 +
74 /** The executable an installed unit, plist, or task starts: the launcher's runtime. */
75 export function serviceUnitProgramOf(contents: string): string | undefined {
76 const systemd = /^ExecStart=("(?:\\.|[^"\\])*"|\S+)/m.exec(contents)?.[1];
apps/server/src/serviceLauncher.test.ts
+41
@@ -302,6 +302,47 @@ it.layer(NodeServices.layer)("service state persistence", (it) => {
302 }),
303 );
304
305 + it.effect("keeps the service manager's append log bounded when it starts", () =>
306 + Effect.gen(function* () {
307 + const fs = yield* FileSystem.FileSystem;
308 + const path = yield* Path.Path;
309 + const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-service-launcher-log-" });
310 + const home = path.join(root, "home");
311 + const unitPath = path.join(home, "Library", "LaunchAgents", "t3code.plist");
312 + const plan = unitPlan(root, "1.0.0");
313 + yield* fs.makeDirectory(path.dirname(unitPath), { recursive: true });
314 + yield* fs.writeFileString(
315 + unitPath,
316 + renderBootServicePlist(plan, { homeDir: home, environmentPath: "" }),
317 + );
318 + const oversized = 64 * 1024 * 1024;
319 + yield* fs.makeDirectory(path.dirname(plan.logPath), { recursive: true });
320 + yield* fs.writeFileString(plan.logPath, "");
321 + yield* fs.truncate(plan.logPath, oversized);
322 + yield* writeFakeRuntime(
323 + fs,
324 + path,
325 + path.join(root, "runtime", "versions", "1.0.0"),
326 + "process.exit(0);\n",
327 + );
328 + yield* Effect.promise(() =>
329 + writeServiceState(path.join(root, "runtime", "service-state.json"), {
330 + protocol: SERVICE_LAUNCHER_PROTOCOL,
331 + activeVersion: "1.0.0",
332 + }),
333 + );
334 + vi.stubEnv("HOME", home);
335 + vi.stubEnv("T3_BOOT_SERVICE_UNIT", "t3code.plist");
336 +
337 + yield* Effect.promise(() =>
338 + expect(runLauncher(root)).rejects.toThrow("Active child exited unexpectedly (0)."),
339 + );
340 +
341 + const { size } = yield* fs.stat(plan.logPath);
342 + expect(Number(size)).toBeLessThan(oversized);
343 + }),
344 + );
345 +
346 it.effect("adopts a legacy install record without rewriting it", () =>
347 Effect.gen(function* () {
348 const fs = yield* FileSystem.FileSystem;
apps/server/src/serviceLauncher.ts
+42
@@ -26,6 +26,7 @@ import {
26 parseServiceState,
27 serviceTaskIsDisabled,
28 serviceUnitBaseDirOf,
29 + serviceUnitLogPathOf,
30 SERVICE_ENV_FILE,
31 SERVICE_LAUNCHER_LOCK_FILE,
32 SERVICE_LAUNCHER_CONTEXT_ENV,
@@ -186,6 +187,37 @@ function repointServiceUnit(contents: string, version: string): string {
187 return contents;
188 }
189
190 +const SERVICE_LOG_MAX_BYTES = 10 * 1024 * 1024;
191 +const SERVICE_LOG_CHECK_INTERVAL_MS = 10 * 60_000;
192 +
193 +/**
194 + * The service manager holds the log open for appending, so it is copied aside and emptied in
195 + * place rather than renamed. The copy is best effort: on a full disk the truncate frees space.
196 + */
197 +async function boundServiceLog(logPath: string): Promise<void> {
198 + const size = await NodeFSP.stat(logPath).then(
199 + (stats) => stats.size,
200 + () => 0,
201 + );
202 + if (size <= SERVICE_LOG_MAX_BYTES) return;
203 + const archivePath = `${logPath}.1`;
204 + const pendingArchivePath = `${archivePath}.tmp`;
205 + await NodeFSP.copyFile(logPath, pendingArchivePath)
206 + .then(() => NodeFSP.rename(pendingArchivePath, archivePath))
207 + .catch(() => NodeFSP.rm(pendingArchivePath, { force: true }).catch(() => undefined));
208 + await NodeFSP.truncate(logPath, 0).catch(() => undefined);
209 +}
210 +
211 +async function serviceLogPath(
212 + options: LauncherOptions,
213 + serviceUnit: ServiceUnit | undefined,
214 +): Promise<string | undefined> {
215 + if (options.logFile !== undefined) return options.logFile;
216 + if (serviceUnit === undefined) return undefined;
217 + const contents = await NodeFSP.readFile(serviceUnit.path, "utf8").catch(() => undefined);
218 + return contents === undefined ? undefined : serviceUnitLogPathOf(contents);
219 +}
220 +
221 const DB_FILE_SUFFIXES = ["", "-wal", "-shm"] as const;
222 const RESTORE_MARKER = ".restore-pending";
223
@@ -1265,6 +1297,7 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
1297 options.logFile === undefined ? undefined : NodeFS.openSync(options.logFile, "a", 0o600);
1298 // oxlint-disable-next-line t3code/no-global-process-runtime -- Standalone launcher has no Effect runtime.
1299 const windows = process.platform === "win32";
1300 + let logTimer: NodeJS.Timeout | undefined;
1301 try {
1302 // What the service manager started the launcher with. It locates the unit; service.env
1303 // changes only what the server gets.
@@ -1285,6 +1318,14 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
1318 }
1319 }
1320 const serviceUnit = serviceUnitFromEnvironment(managerEnvironment);
1321 + const logPath = await serviceLogPath(options, serviceUnit);
1322 + if (logPath !== undefined) {
1323 + await boundServiceLog(logPath);
1324 + logTimer = setInterval(
1325 + () => void boundServiceLog(logPath),
1326 + SERVICE_LOG_CHECK_INTERVAL_MS,
1327 + ).unref();
1328 + }
1329 const launcher = new Launcher(baseDir, state, {
1330 environment,
1331 ...(serviceUnit === undefined ? {} : { serviceUnit }),
@@ -1325,6 +1366,7 @@ export async function main(options: LauncherOptions = {}): Promise<void> {
1366 }
1367 throw cause;
1368 } finally {
1369 + clearInterval(logTimer);
1370 if (output !== undefined) NodeFS.closeSync(output);
1371 }
1372 }