chore: Enable "curly" rule to avoid single-statement if confusion/errors.

This commit is contained in:
cpojer
2026-01-31 16:19:20 +09:00
parent 009b16fab8
commit 5ceff756e1
1266 changed files with 27871 additions and 9393 deletions

View File

@@ -14,12 +14,16 @@ export function readSessionMessages(
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile);
const filePath = candidates.find((p) => fs.existsSync(p));
if (!filePath) return [];
if (!filePath) {
return [];
}
const lines = fs.readFileSync(filePath, "utf-8").split(/\r?\n/);
const messages: unknown[] = [];
for (const line of lines) {
if (!line.trim()) continue;
if (!line.trim()) {
continue;
}
try {
const parsed = JSON.parse(line);
if (parsed?.message) {
@@ -39,7 +43,9 @@ export function resolveSessionTranscriptCandidates(
agentId?: string,
): string[] {
const candidates: string[] = [];
if (sessionFile) candidates.push(sessionFile);
if (sessionFile) {
candidates.push(sessionFile);
}
if (storePath) {
const dir = path.dirname(storePath);
candidates.push(path.join(dir, `${sessionId}.jsonl`));
@@ -71,7 +77,9 @@ export function capArrayByJsonBytes<T>(
items: T[],
maxBytes: number,
): { items: T[]; bytes: number } {
if (items.length === 0) return { items, bytes: 2 };
if (items.length === 0) {
return { items, bytes: 2 };
}
const parts = items.map((item) => jsonUtf8Bytes(item));
let bytes = 2 + parts.reduce((a, b) => a + b, 0) + (items.length - 1);
let start = 0;
@@ -91,13 +99,21 @@ type TranscriptMessage = {
};
function extractTextFromContent(content: TranscriptMessage["content"]): string | null {
if (typeof content === "string") return content.trim() || null;
if (!Array.isArray(content)) return null;
if (typeof content === "string") {
return content.trim() || null;
}
if (!Array.isArray(content)) {
return null;
}
for (const part of content) {
if (!part || typeof part.text !== "string") continue;
if (!part || typeof part.text !== "string") {
continue;
}
if (part.type === "text" || part.type === "output_text" || part.type === "input_text") {
const trimmed = part.text.trim();
if (trimmed) return trimmed;
if (trimmed) {
return trimmed;
}
}
}
return null;
@@ -111,25 +127,33 @@ export function readFirstUserMessageFromTranscript(
): string | null {
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
const filePath = candidates.find((p) => fs.existsSync(p));
if (!filePath) return null;
if (!filePath) {
return null;
}
let fd: number | null = null;
try {
fd = fs.openSync(filePath, "r");
const buf = Buffer.alloc(8192);
const bytesRead = fs.readSync(fd, buf, 0, buf.length, 0);
if (bytesRead === 0) return null;
if (bytesRead === 0) {
return null;
}
const chunk = buf.toString("utf-8", 0, bytesRead);
const lines = chunk.split(/\r?\n/).slice(0, MAX_LINES_TO_SCAN);
for (const line of lines) {
if (!line.trim()) continue;
if (!line.trim()) {
continue;
}
try {
const parsed = JSON.parse(line);
const msg = parsed?.message as TranscriptMessage | undefined;
if (msg?.role === "user") {
const text = extractTextFromContent(msg.content);
if (text) return text;
if (text) {
return text;
}
}
} catch {
// skip malformed lines
@@ -138,7 +162,9 @@ export function readFirstUserMessageFromTranscript(
} catch {
// file read error
} finally {
if (fd !== null) fs.closeSync(fd);
if (fd !== null) {
fs.closeSync(fd);
}
}
return null;
}
@@ -154,14 +180,18 @@ export function readLastMessagePreviewFromTranscript(
): string | null {
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
const filePath = candidates.find((p) => fs.existsSync(p));
if (!filePath) return null;
if (!filePath) {
return null;
}
let fd: number | null = null;
try {
fd = fs.openSync(filePath, "r");
const stat = fs.fstatSync(fd);
const size = stat.size;
if (size === 0) return null;
if (size === 0) {
return null;
}
const readStart = Math.max(0, size - LAST_MSG_MAX_BYTES);
const readLen = Math.min(size, LAST_MSG_MAX_BYTES);
@@ -179,7 +209,9 @@ export function readLastMessagePreviewFromTranscript(
const msg = parsed?.message as TranscriptMessage | undefined;
if (msg?.role === "user" || msg?.role === "assistant") {
const text = extractTextFromContent(msg.content);
if (text) return text;
if (text) {
return text;
}
}
} catch {
// skip malformed
@@ -188,7 +220,9 @@ export function readLastMessagePreviewFromTranscript(
} catch {
// file error
} finally {
if (fd !== null) fs.closeSync(fd);
if (fd !== null) {
fs.closeSync(fd);
}
}
return null;
}
@@ -211,7 +245,9 @@ type TranscriptPreviewMessage = {
};
function normalizeRole(role: string | undefined, isTool: boolean): SessionPreviewItem["role"] {
if (isTool) return "tool";
if (isTool) {
return "tool";
}
switch ((role ?? "").toLowerCase()) {
case "user":
return "user";
@@ -227,8 +263,12 @@ function normalizeRole(role: string | undefined, isTool: boolean): SessionPrevie
}
function truncatePreviewText(text: string, maxChars: number): string {
if (maxChars <= 0 || text.length <= maxChars) return text;
if (maxChars <= 3) return text.slice(0, maxChars);
if (maxChars <= 0 || text.length <= maxChars) {
return text;
}
if (maxChars <= 3) {
return text.slice(0, maxChars);
}
return `${text.slice(0, maxChars - 3)}...`;
}
@@ -253,10 +293,16 @@ function extractPreviewText(message: TranscriptPreviewMessage): string | null {
}
function isToolCall(message: TranscriptPreviewMessage): boolean {
if (message.toolName || message.tool_name) return true;
if (!Array.isArray(message.content)) return false;
if (message.toolName || message.tool_name) {
return true;
}
if (!Array.isArray(message.content)) {
return false;
}
return message.content.some((entry) => {
if (entry?.name) return true;
if (entry?.name) {
return true;
}
const raw = typeof entry?.type === "string" ? entry.type.toLowerCase() : "";
return raw === "toolcall" || raw === "tool_call";
});
@@ -279,10 +325,14 @@ function extractToolNames(message: TranscriptPreviewMessage): string[] {
}
function extractMediaSummary(message: TranscriptPreviewMessage): string | null {
if (!Array.isArray(message.content)) return null;
if (!Array.isArray(message.content)) {
return null;
}
for (const entry of message.content) {
const raw = typeof entry?.type === "string" ? entry.type.trim().toLowerCase() : "";
if (!raw || raw === "text" || raw === "toolcall" || raw === "tool_call") continue;
if (!raw || raw === "text" || raw === "toolcall" || raw === "tool_call") {
continue;
}
return `[${raw}]`;
}
return null;
@@ -304,15 +354,21 @@ function buildPreviewItems(
const shown = toolNames.slice(0, 2);
const overflow = toolNames.length - shown.length;
text = `call ${shown.join(", ")}`;
if (overflow > 0) text += ` +${overflow}`;
if (overflow > 0) {
text += ` +${overflow}`;
}
}
}
if (!text) {
text = extractMediaSummary(message);
}
if (!text) continue;
if (!text) {
continue;
}
let trimmed = text.trim();
if (!trimmed) continue;
if (!trimmed) {
continue;
}
if (role === "user") {
trimmed = stripEnvelope(trimmed);
}
@@ -320,7 +376,9 @@ function buildPreviewItems(
items.push({ role, text: trimmed });
}
if (items.length <= maxItems) return items;
if (items.length <= maxItems) {
return items;
}
return items.slice(-maxItems);
}
@@ -334,7 +392,9 @@ function readRecentMessagesFromTranscript(
fd = fs.openSync(filePath, "r");
const stat = fs.fstatSync(fd);
const size = stat.size;
if (size === 0) return [];
if (size === 0) {
return [];
}
const readStart = Math.max(0, size - readBytes);
const readLen = Math.min(size, readBytes);
@@ -353,7 +413,9 @@ function readRecentMessagesFromTranscript(
const msg = parsed?.message as TranscriptPreviewMessage | undefined;
if (msg && typeof msg === "object") {
collected.push(msg);
if (collected.length >= maxMessages) break;
if (collected.length >= maxMessages) {
break;
}
}
} catch {
// skip malformed lines
@@ -363,7 +425,9 @@ function readRecentMessagesFromTranscript(
} catch {
return [];
} finally {
if (fd !== null) fs.closeSync(fd);
if (fd !== null) {
fs.closeSync(fd);
}
}
}
@@ -377,7 +441,9 @@ export function readSessionPreviewItemsFromTranscript(
): SessionPreviewItem[] {
const candidates = resolveSessionTranscriptCandidates(sessionId, storePath, sessionFile, agentId);
const filePath = candidates.find((p) => fs.existsSync(p));
if (!filePath) return [];
if (!filePath) {
return [];
}
const boundedItems = Math.max(1, Math.min(maxItems, 50));
const boundedChars = Math.max(20, Math.min(maxChars, 2000));