Skip to content

Commit c4c9f6d

Browse files
authored
fix(logging): report unavailable log tails (#126481)
Only missing-path metadata failures remain empty successes. Operational filesystem errors now reach existing Gateway, UI, CLI, channel, and diagnostics error paths.\n\nCloses #126467
1 parent 13fd888 commit c4c9f6d

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎src/logging/log-tail.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
66
import { resetLogger, setLoggerOverride } from "../logging.js";
77

88
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
9+
const metadataBoundaries = [
10+
"configured stat",
11+
"rolling readdir",
12+
"candidate stat",
13+
"final stat",
14+
] as const;
15+
const operationalErrorCodes = ["EACCES", "EIO", "EMFILE"] as const;
16+
const operationalMetadataFailures = metadataBoundaries.flatMap((boundary) =>
17+
operationalErrorCodes.map((code) => ({ boundary, code })),
18+
);
919

1020
const resolvedRedaction = { mode: "tools" as const, patterns: [/custom-secret-[a-z]+/g] };
1121
type PositionalRead = (
@@ -123,6 +133,44 @@ describe("readConfiguredLogTail", () => {
123133
expect(result.lines[0]?.trimEnd()).toBe("first-line-in-window");
124134
});
125135

136+
it.each(operationalMetadataFailures)(
137+
"rethrows $code from the $boundary boundary",
138+
async ({ boundary, code }) => {
139+
const tempDir = tempDirs.make("openclaw-log-tail-");
140+
const configured = path.join(tempDir, "openclaw-2026-01-22.log");
141+
const candidate = path.join(tempDir, "openclaw-2026-01-21.log");
142+
const error = Object.assign(new Error(`${code} injected`), { code });
143+
const realStat = fs.stat.bind(fs);
144+
145+
if (boundary === "candidate stat") {
146+
await fs.writeFile(candidate, "candidate\n");
147+
} else if (boundary !== "rolling readdir") {
148+
await fs.writeFile(configured, "configured\n");
149+
}
150+
setLoggerOverride({ file: configured });
151+
152+
if (boundary === "configured stat") {
153+
vi.spyOn(fs, "stat").mockRejectedValueOnce(error);
154+
} else if (boundary === "rolling readdir") {
155+
vi.spyOn(fs, "readdir").mockRejectedValueOnce(error);
156+
} else if (boundary === "candidate stat") {
157+
vi.spyOn(fs, "stat").mockImplementation(async (...args: Parameters<typeof fs.stat>) => {
158+
if (String(args[0]) === candidate) {
159+
throw error;
160+
}
161+
return realStat(...args);
162+
});
163+
} else {
164+
vi.spyOn(fs, "stat")
165+
.mockImplementationOnce((...args: Parameters<typeof fs.stat>) => realStat(...args))
166+
.mockRejectedValueOnce(error);
167+
}
168+
169+
const { readConfiguredLogTail } = await import("./log-tail.js");
170+
await expect(readConfiguredLogTail()).rejects.toBe(error);
171+
},
172+
);
173+
126174
it("falls back only within the active profile's rolling log family", async () => {
127175
const tempDir = tempDirs.make("openclaw-log-tail-");
128176
const missing = path.join(tempDir, "openclaw-2026-01-22.log");

‎src/logging/log-tail.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Log tail helpers read recent log lines with optional parsing and redaction.
22
import fs from "node:fs/promises";
33
import path from "node:path";
4+
import { isMissingPathError } from "../infra/errno.js";
45
import { readFileWindowFully } from "../infra/file-read.js";
56
import { clamp } from "../utils.js";
67
import { isRollingLogFilePath, isSameRollingLogFileFamily } from "./log-file-path.js";
@@ -15,6 +16,13 @@ const DEFAULT_MAX_BYTES = 250_000;
1516
const MAX_LIMIT = 5000;
1617
const MAX_BYTES = 1_000_000;
1718

19+
function missingPathToNull(error: unknown): null {
20+
if (!isMissingPathError(error)) {
21+
throw error;
22+
}
23+
return null;
24+
}
25+
1826
/** Payload returned to log-tail callers with cursor and truncation metadata. */
1927
export type LogTailPayload = {
2028
file: string;
@@ -32,7 +40,7 @@ type ParsedLogTailPayload = Omit<LogTailPayload, "lines"> & {
3240

3341
/** Resolves a rolling daily log path to the newest existing rolling log when needed. */
3442
async function resolveLogFile(file: string, options?: { rolling?: boolean }): Promise<string> {
35-
const stat = await fs.stat(file).catch(() => null);
43+
const stat = await fs.stat(file).catch(missingPathToNull);
3644
if (stat) {
3745
return file;
3846
}
@@ -41,7 +49,7 @@ async function resolveLogFile(file: string, options?: { rolling?: boolean }): Pr
4149
}
4250

4351
const dir = path.dirname(file);
44-
const entries = await fs.readdir(dir, { withFileTypes: true }).catch(() => null);
52+
const entries = await fs.readdir(dir, { withFileTypes: true }).catch(missingPathToNull);
4553
if (!entries) {
4654
return file;
4755
}
@@ -51,7 +59,7 @@ async function resolveLogFile(file: string, options?: { rolling?: boolean }): Pr
5159
.filter((entry) => entry.isFile() && isSameRollingLogFileFamily(file, entry.name))
5260
.map(async (entry) => {
5361
const fullPath = path.join(dir, entry.name);
54-
const fileStat = await fs.stat(fullPath).catch(() => null);
62+
const fileStat = await fs.stat(fullPath).catch(missingPathToNull);
5563
return fileStat ? { path: fullPath, mtimeMs: fileStat.mtimeMs } : null;
5664
}),
5765
);
@@ -68,7 +76,7 @@ async function readLogSlice(params: {
6876
maxBytes: number;
6977
filter?: (line: string) => boolean;
7078
}): Promise<Omit<LogTailPayload, "file">> {
71-
const stat = await fs.stat(params.file).catch(() => null);
79+
const stat = await fs.stat(params.file).catch(missingPathToNull);
7280
if (!stat) {
7381
return {
7482
cursor: 0,

0 commit comments

Comments
 (0)