Skip to content

fix: pass config to engine.connect() on autopilot reconnect#190

Open
voidborne-d wants to merge 1 commit intogarrytan:masterfrom
voidborne-d:fix/autopilot-reconnect-config
Open

fix: pass config to engine.connect() on autopilot reconnect#190
voidborne-d wants to merge 1 commit intogarrytan:masterfrom
voidborne-d:fix/autopilot-reconnect-config

Conversation

@voidborne-d
Copy link
Copy Markdown

Fixes #167. Fixes #164.

Problem

Autopilot's DB health-check reconnect path called engine.connect() with no arguments:

await engine.disconnect();
await (engine as any).connect?.();  // ← no config!

PostgresEngine.connect() dereferences config.poolSize unconditionally, so undefined config → TypeError: "undefined is not an object (evaluating 'config.poolSize')".

Once reconnect crashes, the engine stays disconnected and every subsequent cycle step (sync / extract / embed / health) fails with "No database connection: connect() has not been called". After 5 such cycles the daemon exits. launchd KeepAlive restarts it, but each restart loses adaptive scheduling state and any in-progress embed batch.

Fix

  1. autopilot.ts: Re-load config from disk via loadConfig() + toEngineConfig() and pass it to engine.connect() on reconnect. Handles the edge case where config is missing (no brain configured).

  2. postgres-engine.ts: Guard connect() against undefined config with a descriptive GBrainError (defense in depth — if a future caller forgets to pass config, the error message is clear instead of a cryptic TypeError).

  3. pglite-engine.ts: Same guard for PGLiteEngine.connect().

Tests

New test/autopilot-reconnect.test.ts — 10 tests covering:

  • Source-code audit: autopilot imports config helpers, passes config to connect(), no bare connect() calls
  • Reconnect handles missing config gracefully
  • Both engine connect(undefined) guards throw descriptive errors
  • toEngineConfig() round-trip for postgres and pglite configs
bun test test/autopilot-reconnect.test.ts
  10 pass, 0 fail  [118ms]

Existing test suites unaffected:

bun test test/config.test.ts test/engine-factory.test.ts test/pglite-lock.test.ts test/cli.test.ts test/utils.test.ts
  48 pass, 0 fail

…#167, garrytan#164)

Root cause: autopilot's DB health check called engine.connect() with no
arguments after disconnect.  PostgresEngine.connect() dereferences
config.poolSize unconditionally, so undefined config → TypeError:
"undefined is not an object (evaluating 'config.poolSize')".

Once reconnect crashes, the engine stays disconnected and every
subsequent cycle step (sync / extract / embed / health) fails with
"No database connection".  After 5 such cycles the daemon exits.
launchd KeepAlive restarts it, but each restart loses adaptive
scheduling state and any in-progress embed batch.

Fix:
- autopilot.ts: re-load config from disk via loadConfig() +
  toEngineConfig() and pass it to engine.connect() on reconnect.
  Handles the case where config is missing (no brain configured).
- postgres-engine.ts: guard connect() against undefined config with
  a descriptive GBrainError (defense in depth).
- pglite-engine.ts: same guard for PGLiteEngine.connect().

Closes garrytan#167. Closes garrytan#164.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant