fix(state): prevent false degraded-mode warning when DB not yet initialized

deriveState() is called during before_agent_start context injection,
before any tool invocation has had a chance to open the DB. Previously,
isDbAvailable() returning false in this path triggered a misleading
"DB unavailable — using filesystem state derivation (degraded mode)"
warning, even though the DB was simply not yet initialized (not failed).

Add a _dbOpenAttempted flag in gsd-db.ts that tracks whether
openDatabase() has been called at least once. The degraded-mode warning
now only fires when the DB was actually attempted and failed to open,
not when it hasn't been initialized yet.

Supersedes #3922.
This commit is contained in:
Jeremy 2026-04-10 08:16:29 -05:00
parent da352847e2
commit 96490d269d
2 changed files with 19 additions and 1 deletions

View file

@ -778,6 +778,7 @@ let currentDb: DbAdapter | null = null;
let currentPath: string | null = null;
let currentPid: number = 0;
let _exitHandlerRegistered = false;
let _dbOpenAttempted = false;
export function getDbProvider(): ProviderName | null {
loadProvider();
@ -788,7 +789,18 @@ export function isDbAvailable(): boolean {
return currentDb !== null;
}
/**
* Returns true if openDatabase() has been called at least once this session.
* Used to distinguish "DB not yet initialized" from "DB genuinely unavailable"
* so that early callers (e.g. before_agent_start context injection) don't
* trigger a false degraded-mode warning.
*/
export function wasDbOpenAttempted(): boolean {
return _dbOpenAttempted;
}
export function openDatabase(path: string): boolean {
_dbOpenAttempted = true;
if (currentDb && currentPath !== path) closeDatabase();
if (currentDb && currentPath === path) return true;

View file

@ -47,6 +47,7 @@ import { extractVerdict } from './verdict-parser.js';
import {
isDbAvailable,
wasDbOpenAttempted,
getAllMilestones,
getMilestone,
getMilestoneSlices,
@ -271,7 +272,12 @@ export async function deriveState(basePath: string): Promise<GSDState> {
_telemetry.markdownDeriveCount++;
}
} else {
logWarning("state", "DB unavailable — using filesystem state derivation (degraded mode)");
// Only warn when DB initialization was attempted and failed — not when
// the DB simply hasn't been opened yet (e.g. during before_agent_start
// context injection which runs before any tool invocation opens the DB).
if (wasDbOpenAttempted()) {
logWarning("state", "DB unavailable — using filesystem state derivation (degraded mode)");
}
result = await _deriveStateImpl(basePath);
_telemetry.markdownDeriveCount++;
}