Skip to content

Commit c162362

Browse files
committed
compartment-trigger: make silent-skip diagnostics best-effort
The previous diagnostic instrumentation called getRawSessionMessageCount and getProtectedTailStartOrdinal in the !hasNewRawHistory skip path. Those helpers open OpenCode's session DB in read-only mode, which throws in environments where the OpenCode DB is unavailable (unit tests, fresh Pi sessions before RawMessageProvider registration, etc). The throw propagated up through checkCompartmentTrigger to the event-handler.ts message.updated try/catch wrapper, which caught it and SKIPPED the subsequent updateSessionMeta call. Net effect: session_meta writes for lastResponseTime, cacheTtl, lastContextPercentage, observedSafeInputTokens, and cacheAlertSent silently failed whenever the diagnostic helpers couldn't reach the OpenCode DB. Wrap the diagnostic data collection in its own try/catch. If the DB helpers throw, log without the diagnostic fields and continue. Skip behavior is unchanged. Fixes 4 event-handler.test.ts failures introduced in 5b44bd7.
1 parent 357830d commit c162362

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

packages/plugin/src/hooks/magic-context/compartment-trigger.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,27 @@ export function checkCompartmentTrigger(
205205

206206
const tailInfo = getUnsummarizedTailInfo(db, sessionId, triggerBudget);
207207
if (!tailInfo.hasNewRawHistory) {
208-
const lastCompartmentEnd = getLastCompartmentEndMessage(db, sessionId);
209-
const rawMessageCount = getRawSessionMessageCount(sessionId);
210-
const protectedTailStart = getProtectedTailStartOrdinal(sessionId);
211-
sessionLog(
212-
sessionId,
213-
`compartment trigger: skipped — no new raw history (usage=${usage.percentage.toFixed(1)}% nextStartOrdinal=${tailInfo.nextStartOrdinal} lastCompartmentEnd=${lastCompartmentEnd} rawMessageCount=${rawMessageCount} protectedTailStart=${protectedTailStart})`,
214-
);
208+
// Diagnostic data collection is best-effort. The helpers can throw if
209+
// the OpenCode session DB is unavailable (e.g. in unit-test env or
210+
// when the harness has not yet wired a RawMessageProvider). A throw
211+
// here would propagate to the caller's try/catch and prevent
212+
// downstream state updates (e.g. session-meta writes in event-handler
213+
// line 542). Swallow any failure and log without the diagnostic
214+
// fields so callers see no behavioral change.
215+
try {
216+
const lastCompartmentEnd = getLastCompartmentEndMessage(db, sessionId);
217+
const rawMessageCount = getRawSessionMessageCount(sessionId);
218+
const protectedTailStart = getProtectedTailStartOrdinal(sessionId);
219+
sessionLog(
220+
sessionId,
221+
`compartment trigger: skipped — no new raw history (usage=${usage.percentage.toFixed(1)}% nextStartOrdinal=${tailInfo.nextStartOrdinal} lastCompartmentEnd=${lastCompartmentEnd} rawMessageCount=${rawMessageCount} protectedTailStart=${protectedTailStart})`,
222+
);
223+
} catch (error) {
224+
sessionLog(
225+
sessionId,
226+
`compartment trigger: skipped — no new raw history (usage=${usage.percentage.toFixed(1)}% nextStartOrdinal=${tailInfo.nextStartOrdinal} diagnostic-collection-failed: ${error instanceof Error ? error.message : String(error)})`,
227+
);
228+
}
215229
return { shouldFire: false };
216230
}
217231

0 commit comments

Comments
 (0)