Skip to content

fix(workspace-server): tolerate not-ready DB in agent file activity path#2966

Merged
frankh merged 1 commit into
mainfrom
posthog-code/fix-agent-file-activity-db-race
Jul 8, 2026
Merged

fix(workspace-server): tolerate not-ready DB in agent file activity path#2966
frankh merged 1 commit into
mainfrom
posthog-code/fix-agent-file-activity-db-race

Conversation

@posthog

@posthog posthog Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Problem

An unhandled rejection — Database not initialized — call initialize() first — was firing in the workspace-server agent file-activity path (6 occurrences across 4 users, first seen 2026-06-28).

When the agent edits a file, AgentService.emitAgentFileActivityForCurrentBranch fires a fire-and-forget emit. The listener, WorkspaceService.handleAgentFileActivity, is async and its first statement does a synchronous DB read (workspaceRepo.findByTaskId), which throws from the DatabaseService.db getter during the startup/teardown window when the DB is closed (@preDestroy) or not yet initialized. Because the listener is async and TypedEventEmitter.emit discarded the returned promise, that synchronous throw became a rejected promise that escaped into error tracking as noise — it never broke a user-facing flow (branch association is best-effort observability).

Changes

  • Add DatabaseService.isInitialized() and have handleAgentFileActivity bail gracefully when the DB isn't ready, so the best-effort branch-association read no longer throws during start/shutdown.
  • Harden TypedEventEmitter.emit to swallow rejections from async listeners, so a throwing async listener can never become an unhandled rejection again (defense in depth).

How did you test this?

  • Added a unit test asserting handleAgentFileActivity returns without touching the repo when the DB is not initialized.
  • Added a unit test asserting emit does not surface a rejecting async listener as an unhandled rejection (and that later listeners still run).
  • Ran pnpm --filter @posthog/workspace-server exec vitest run workspace (57 passed) and the shared emitter suite (14 passed).
  • Typechecked @posthog/shared and @posthog/workspace-server; Biome clean on changed files.

Automatic notifications

  • Publish to changelog?
  • Alert Sales and Marketing teams?

Created with PostHog Code from an inbox report.

The fire-and-forget `emitAgentFileActivityForCurrentBranch` emit lands on the
async `WorkspaceService.handleAgentFileActivity` listener, whose first statement
is a synchronous repo read. During the startup/teardown window (DB closed via
`@preDestroy` or not yet initialized) the `DatabaseService.db` getter throws.
Because the listener is async and `TypedEventEmitter.emit` discarded the returned
promise, that throw became an unhandled rejection that polluted error tracking.

- Add `DatabaseService.isInitialized()` and bail gracefully from
  `handleAgentFileActivity` when the DB isn't ready — branch association is
  best-effort.
- Harden `TypedEventEmitter.emit` to swallow rejections from async listeners so
  a throwing listener can never become an unhandled rejection again.

Generated-By: PostHog Code
Task-Id: c17e519a-cfe2-412c-856e-17e8870407c5
@github-actions

Copy link
Copy Markdown

React Doctor found no issues in the changed files. 🎉

Reviewed by React Doctor for commit 35af854.

@frankh frankh marked this pull request as ready for review July 8, 2026 09:20
@frankh frankh added the Create Release This will trigger a new release label Jul 8, 2026
@frankh frankh merged commit c26ae84 into main Jul 8, 2026
29 checks passed
@frankh frankh deleted the posthog-code/fix-agent-file-activity-db-race branch July 8, 2026 09:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Create Release This will trigger a new release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant