fix(workspace-server): tolerate not-ready DB in agent file activity path#2966
Draft
posthog[bot] wants to merge 1 commit into
Draft
fix(workspace-server): tolerate not-ready DB in agent file activity path#2966posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
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
|
React Doctor found no issues in the changed files. 🎉 Reviewed by React Doctor for commit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.emitAgentFileActivityForCurrentBranchfires a fire-and-forget emit. The listener,WorkspaceService.handleAgentFileActivity, isasyncand its first statement does a synchronous DB read (workspaceRepo.findByTaskId), which throws from theDatabaseService.dbgetter during the startup/teardown window when the DB is closed (@preDestroy) or not yet initialized. Because the listener is async andTypedEventEmitter.emitdiscarded 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
DatabaseService.isInitialized()and havehandleAgentFileActivitybail gracefully when the DB isn't ready, so the best-effort branch-association read no longer throws during start/shutdown.TypedEventEmitter.emitto 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?
handleAgentFileActivityreturns without touching the repo when the DB is not initialized.emitdoes not surface a rejecting async listener as an unhandled rejection (and that later listeners still run).pnpm --filter @posthog/workspace-server exec vitest run workspace(57 passed) and the shared emitter suite (14 passed).@posthog/sharedand@posthog/workspace-server; Biome clean on changed files.Automatic notifications
Created with PostHog Code from an inbox report.