refactor(hooks): fix empty catches and remove AI slop from code comments

This commit is contained in:
YeonGyu-Kim
2026-04-03 21:37:16 +09:00
parent 6b431c5522
commit 5c7299830d
17 changed files with 900 additions and 565 deletions
@@ -11,6 +11,27 @@ import type { Client } from "./client"
import { PLACEHOLDER_TEXT } from "./message-builder"
import { incrementEmptyContentAttempt } from "./state"
import { fixEmptyMessagesWithSDK } from "./empty-content-recovery-sdk"
import { log } from "../../shared/logger"
async function showToastSafely(
client: Client,
body: {
title: string
message: string
variant: "error" | "warning" | "success"
duration: number
},
failureContext: string,
): Promise<void> {
try {
await client.tui.showToast({ body })
} catch (error) {
log(`[auto-compact] failed to show toast: ${failureContext}`, {
title: body.title,
error: error instanceof Error ? error.message : String(error),
})
}
}
export async function fixEmptyMessages(params: {
sessionID: string
@@ -32,30 +53,30 @@ export async function fixEmptyMessages(params: {
})
if (!result.fixed && result.scannedEmptyCount === 0) {
await params.client.tui
.showToast({
body: {
title: "Empty Content Error",
message: "No empty messages found in storage. Cannot auto-recover.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Empty Content Error",
message: "No empty messages found in storage. Cannot auto-recover.",
variant: "error",
duration: 5000,
},
"sqlite empty message not found",
)
return false
}
if (result.fixed) {
await params.client.tui
.showToast({
body: {
title: "Session Recovery",
message: `Fixed ${result.fixedMessageIds.length} empty message(s). Retrying...`,
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Session Recovery",
message: `Fixed ${result.fixedMessageIds.length} empty message(s). Retrying...`,
variant: "warning",
duration: 3000,
},
"sqlite empty message fixed",
)
}
return result.fixed
@@ -83,16 +104,16 @@ export async function fixEmptyMessages(params: {
const emptyTextPartIds = findMessagesWithEmptyTextParts(params.sessionID)
const allIds = [...new Set([...emptyMessageIds, ...emptyTextPartIds])]
if (allIds.length === 0) {
await params.client.tui
.showToast({
body: {
title: "Empty Content Error",
message: "No empty messages found in storage. Cannot auto-recover.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Empty Content Error",
message: "No empty messages found in storage. Cannot auto-recover.",
variant: "error",
duration: 5000,
},
"empty message not found",
)
return false
}
@@ -112,16 +133,16 @@ export async function fixEmptyMessages(params: {
}
if (fixed) {
await params.client.tui
.showToast({
body: {
title: "Session Recovery",
message: `Fixed ${fixedMessageIds.length} empty message(s). Retrying...`,
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Session Recovery",
message: `Fixed ${fixedMessageIds.length} empty message(s). Retrying...`,
variant: "warning",
duration: 3000,
},
"empty messages fixed",
)
}
return fixed
@@ -13,8 +13,32 @@ import { sanitizeEmptyMessagesBeforeSummarize } from "./message-builder"
import { fixEmptyMessages } from "./empty-content-recovery"
import { resolveCompactionModel } from "../shared/compaction-model-resolver"
import { log } from "../../shared/logger"
const SUMMARIZE_RETRY_TOTAL_TIMEOUT_MS = 120_000
declare function setTimeout(handler: () => void, timeout?: number): unknown
async function showToastSafely(
client: Client,
body: {
title: string
message: string
variant: "error" | "warning" | "success"
duration: number
},
failureContext: string,
): Promise<void> {
try {
await client.tui.showToast({ body })
} catch (error) {
log(`[auto-compact] failed to show toast: ${failureContext}`, {
title: body.title,
error: error instanceof Error ? error.message : String(error),
})
}
}
export async function runSummarizeRetryStrategy(params: {
sessionID: string
msg: Record<string, unknown>
@@ -40,16 +64,16 @@ export async function runSummarizeRetryStrategy(params: {
const elapsedTimeMs = now - retryState.firstAttemptTime
if (elapsedTimeMs >= SUMMARIZE_RETRY_TOTAL_TIMEOUT_MS) {
clearSessionState(params.autoCompactState, params.sessionID)
await params.client.tui
.showToast({
body: {
title: "Auto Compact Timed Out",
message: "Compaction retries exceeded the timeout window. Please start a new session.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Auto Compact Timed Out",
message: "Compaction retries exceeded the timeout window. Please start a new session.",
variant: "error",
duration: 5000,
},
"retry timeout",
)
return
}
@@ -74,17 +98,17 @@ export async function runSummarizeRetryStrategy(params: {
}
} else {
clearSessionState(params.autoCompactState, params.sessionID)
await params.client.tui
.showToast({
body: {
title: "Recovery Failed",
message:
"Max recovery attempts (3) reached for empty content error. Please start a new session.",
variant: "error",
duration: 10000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Recovery Failed",
message:
"Max recovery attempts (3) reached for empty content error. Please start a new session.",
variant: "error",
duration: 10000,
},
"empty content recovery exhausted",
)
return
}
}
@@ -106,16 +130,16 @@ export async function runSummarizeRetryStrategy(params: {
try {
await sanitizeEmptyMessagesBeforeSummarize(params.sessionID, params.client)
await params.client.tui
.showToast({
body: {
title: "Auto Compact",
message: `Summarizing session (attempt ${retryState.attempt}/${RETRY_CONFIG.maxAttempts})...`,
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Auto Compact",
message: `Summarizing session (attempt ${retryState.attempt}/${RETRY_CONFIG.maxAttempts})...`,
variant: "warning",
duration: 3000,
},
"summarize retry attempt",
)
const { providerID: targetProviderID, modelID: targetModelID } = resolveCompactionModel(
params.pluginConfig,
@@ -132,20 +156,26 @@ export async function runSummarizeRetryStrategy(params: {
})
clearSessionState(params.autoCompactState, params.sessionID)
return
} catch {
} catch (error) {
log("[auto-compact] summarize retry attempt failed", {
sessionID: params.sessionID,
attempt: retryState.attempt,
error: error instanceof Error ? error.message : String(error),
})
const remainingTimeMs = SUMMARIZE_RETRY_TOTAL_TIMEOUT_MS - (Date.now() - retryState.firstAttemptTime)
if (remainingTimeMs <= 0) {
clearSessionState(params.autoCompactState, params.sessionID)
await params.client.tui
.showToast({
body: {
title: "Auto Compact Timed Out",
message: "Compaction retries exceeded the timeout window. Please start a new session.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Auto Compact Timed Out",
message: "Compaction retries exceeded the timeout window. Please start a new session.",
variant: "error",
duration: 5000,
},
"summarize retry timeout after failure",
)
return
}
@@ -162,28 +192,28 @@ export async function runSummarizeRetryStrategy(params: {
return
}
} else {
await params.client.tui
.showToast({
body: {
title: "Summarize Skipped",
message: "Missing providerID or modelID.",
variant: "warning",
duration: 3000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Summarize Skipped",
message: "Missing providerID or modelID.",
variant: "warning",
duration: 3000,
},
"missing summarize model info",
)
}
}
clearSessionState(params.autoCompactState, params.sessionID)
await params.client.tui
.showToast({
body: {
title: "Auto Compact Failed",
message: "All recovery attempts failed. Please start a new session.",
variant: "error",
duration: 5000,
},
})
.catch(() => {})
await showToastSafely(
params.client,
{
title: "Auto Compact Failed",
message: "All recovery attempts failed. Please start a new session.",
variant: "error",
duration: 5000,
},
"summarize retry failed",
)
}