abhigyanpatwari/GitNexus · error

LadybugDB not initialized. Call initLbug first.

Error message

LadybugDB not initialized. Call initLbug first.

What it means

deleteAllDestinations requires the module-level LadybugDB connection (conn) to have been established by initLbug. The adapter stores the connection in a module global; every query helper guards on it. Calling any query before initialization means there is no database to run the MATCH delete against, so it throws immediately.

Solutions

  1. Call await initLbug(...) once at startup before any adapter query.
  2. Guard call sites: if the adapter exposes an initialized/conn accessor, check it before querying.
  3. Ensure initLbug and the queries run in the same process/module instance (same import path).
  4. Check that the initLbug call did not fail earlier — fix the root init error first.

Example fix

// before
const { deleteAllDestinations } = await import('./lbug-adapter');
await deleteAllDestinations(); // conn undefined

// after
const lbug = await import('./lbug-adapter');
await lbug.initLbug(dbPath);
await lbug.deleteAllDestinations();
Defensive patterns

Strategy: try-catch

Validate before calling

if (typeof (adapter as { conn?: unknown }).conn === 'undefined') {
  await adapter.initLbug(dbPath);
}

Try / catch

try {
  await deleteAllDestinations();
} catch (err) {
  if (String(err.message).includes('not initialized. Call initLbug')) {
    await initLbug(dbPath);
    return deleteAllDestinations();
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling deleteAllDestinations (or any lbug-adapter query helper) before calling initLbug, or after a failed/never-completed initLbug left conn undefined.

Common situations: Importing the adapter and calling queries in a script that skips setup; initLbug called in a different process/module instance; an earlier init failure was swallowed so conn never got set.

Related errors


AI-assisted analysis of abhigyanpatwari/GitNexus@ac9a4e9abd (2026-09-08). Data as JSON: /api/errors/ee7feae7a1a23681. Report an issue: GitHub.

Appendix: source

Thrown at gitnexus/src/core/lbug/lbug-adapter.ts:3285

 * by files outside the write set — which also means it is never deleted when it
 * SHOULD be, so a destination whose last referrer stopped naming it survived as
 * an edgeless orphan that still carried `address`, the cross-repository join
 * key, accumulating on every run. The mirror defect was worse: without a
 * matching graph-wide re-include, a newly added file publishing to a NEW topic
 * wrote neither the destination nor the publisher's edge, silently and with a
 * zero exit.
 *
 * The `springDestinations` phase runs on every persisting analyze and recomputes
 * the full set from the whole file list, so delete-then-re-include is complete.
 * `extractChangedSubgraph` treats `Destination` as graph-wide to supply the
 * other half; the two must be changed together. DETACH DELETE also takes the
 * `CONSUMES_FROM` / `PUBLISHES_TO` edges, which the re-include restores because
 * every one of them has the destination as an endpoint.
 */
export const deleteAllDestinations = async (): Promise<{ nodesDeleted: number }> => {
  const c = conn;
  if (!c) {
    throw new Error('LadybugDB not initialized. Call initLbug first.');
  }
  return withConnLock(async () => {
    let countResult: lbug.QueryResult | lbug.QueryResult[] | undefined;
    try {
      countResult = await c.query('MATCH (n:Destination) RETURN count(n) AS cnt');
      const result = Array.isArray(countResult) ? countResult[0] : countResult;
      const rows = await result.getAll();
      const count = Number(rows[0]?.cnt ?? rows[0]?.[0] ?? 0);
      if (count > 0) {
        await closeQueryResults(await c.query('MATCH (n:Destination) DETACH DELETE n'));
      }
      if (countResult) await closeQueryResults(countResult);
      return { nodesDeleted: count };
    } catch (err) {
      if (countResult) await closeQueryResults(countResult);
      if (classifyDeleteAllError(err) === 'benign-missing-table') {
        return { nodesDeleted: 0 };
      }

View on GitHub (pinned to ac9a4e9abd)