Skip to content

fix(training-agent): defer pickTaskRegistry pool lookup so prod uses Postgres on cold boot#4073

Merged
bokelley merged 1 commit into
mainfrom
bokelley/lazy-task-registry-pool
May 4, 2026
Merged

fix(training-agent): defer pickTaskRegistry pool lookup so prod uses Postgres on cold boot#4073
bokelley merged 1 commit into
mainfrom
bokelley/lazy-task-registry-pool

Conversation

@bokelley
Copy link
Copy Markdown
Contributor

@bokelley bokelley commented May 4, 2026

Summary

The diagnostic logging from #4067 + the state-store fix in #4072 surfaced a long-standing silent regression:

"Database not initialized" at pickTaskRegistry (registry.js:124)

Same root cause as #4072. mountTenantRoutes() runs before initializeDatabase(), so getPool() at construction throws. pickTaskRegistry's try/catch silently downgrades to InMemoryTaskRegistry — meaning every cold-booted production machine has been running with the in-memory registry since #463 shipped, defeating the whole point of adcp_decisioning_tasks. Buyer creates a media buy on machine A, polls on machine B, sees "task not found" with ~50% probability.

What changed

Wrap the pool in the same lazy PgQueryable adapter pickStateStore uses (#4072). getPool() runs at first query, not at construction; by then the DB is initialized.

const lazyPool = {
  query: (text: string, values?: unknown[]) => getPool().query(text, values),
};
return createPostgresTaskRegistry({ pool: lazyPool });

Drops the try/catch entirely — there's no scenario where construction can fail now, and a real DB error at first query should surface, not be swallowed.

Test plan

  • npx tsc -p server/tsconfig.json --noEmit clean
  • tenant-smoke 3/3 pass
  • After merge: deploy completes; fly logs show Tenant registry initialized with no preceding Database not initialized from pickTaskRegistry

🤖 Generated with Claude Code

…tually uses Postgres on cold boot

Same boot-ordering issue the state-store fix in #4072 closed:
`mountTenantRoutes()` runs before `initializeDatabase()`, so
`getPool()` at construction threw "Database not initialized."
`pickTaskRegistry`'s try/catch silently downgraded to the in-memory
registry — every cold-booted production machine has been running with
`InMemoryTaskRegistry` since #463 shipped, defeating the whole point
of `adcp_decisioning_tasks`. Buyer creates a media buy on machine A,
polls on machine B, sees "task not found" with ~50% probability.

Surfaced by #4067's diagnostic logging — the post-#4072 deploy log
showed "Database not initialized" at pickTaskRegistry (registry.js:124)
immediately followed by the success path (state-store now lazy, so
init completed, but task registry was already in-memory).

Wrap the pool in the same lazy `PgQueryable` adapter `pickStateStore`
uses. `getPool()` runs at first query; the Postgres backend now
actually gets used in production.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@bokelley bokelley merged commit 0c9379d into main May 4, 2026
19 checks passed
@bokelley bokelley deleted the bokelley/lazy-task-registry-pool branch May 4, 2026 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant