Skip to content

Expand how much of the codebase is using Effect; fix excess error emission#270

Merged
vcarl merged 9 commits intomainfrom
vc-expand-effect-radius
Feb 4, 2026
Merged

Expand how much of the codebase is using Effect; fix excess error emission#270
vcarl merged 9 commits intomainfrom
vc-expand-effect-radius

Conversation

@vcarl
Copy link
Member

@vcarl vcarl commented Feb 3, 2026

Swaps out the way the database loads, and shift more responsibility for startup into AppRuntime (an Effect).

This also reworks how the database is initalized, which might help with some of the weirdo oddball failures we've been seeing. I'm using a new creation mechanism, and it demanded I explciitly provide a "Scope" reference. I think this is going to mean that all access relies on the same connection, which has been the main problem so far.

@what-the-diff
Copy link

what-the-diff bot commented Feb 3, 2026

PR Summary

  • Introduction of AppRuntime.ts
    A new file has been added that handles the management of application runtime. This allows for the continued connection with our database and makes it easier to utilize effects with async/await frameworks, improving our app's effectiveness.

  • Revamping of Database.ts
    The ManagedRuntime and other utility functions have been subtly integrated into AppRuntime to ensure a better streamlined process, numbing out dead code for runtime management while adding a reactive processing layer. This enhances the performance of the application.

  • Refactoring imports
    Several files have been changed to carry out imports from AppRuntime instead of Database. This results in a neat separation of tasks and encourages clean code practices.

  • Enhanced database integrity checks
    The update has improved error handling during the database integrity check process, providing clearer logging and better context for error messages. As a result, data integrity and trustworthiness are boosted.

  • Discord bot initialization update
    The initialization of the discord bot has been fine-tuned to include effect-based command registration and better shutdown handling, enhancing reliability during operation.

  • Removal of outdated code
    Redundant sections of the code associated with the previous database runtime management and async handling methods have been removed, thus reducing unnecessary clutter and increasing efficiency.

  • Incorporation of dependency injections
    Context types have been adopted, and our integration with Reactivity has been improved to enhance observability and keep track of application performance successfully. This helps us maintain a high-performing application.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the application's Effect-based runtime management to improve database initialization reliability and reduce excess error emissions. The changes consolidate runtime management into a new AppRuntime module and convert more of the startup logic to use Effect patterns.

Changes:

  • Created new AppRuntime.ts to manage the Effect-based ManagedRuntime, moving it from Database.ts
  • Converted startup logic in server.ts to use Effect.gen for initialization and graceful shutdown
  • Changed getMessageStats to return undefined instead of throwing NotFoundError for empty messages
  • Converted registerCommand to return an Effect and updated initDiscordBot to be an Effect
  • Refactored database integrity check to run as a daemon with 6-hour intervals

Reviewed changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
vite.config.ts Sets build target to "esnext" for SSR to enable top-level await
app/server.ts Converts startup to Effect-based initialization with graceful shutdown handlers
app/AppRuntime.ts New file managing ManagedRuntime and providing database access functions
app/Database.ts Removes ManagedRuntime, adds checkpointWal function, refactors integrity check to daemon
app/discord/gateway.ts Converts initDiscordBot to Effect and removes shutdown handlers
app/discord/deployCommands.server.ts Converts registerCommand to return Effect
app/discord/activityTracker.ts Adds null checks for getMessageStats returning undefined
app/helpers/discord.ts Changes getMessageStats to return undefined instead of NotFoundError
app/helpers/metrics.ts Reorders imports (no functional change)
app/routes/*.tsx Updates imports from Database to AppRuntime
app/models/*.ts Updates imports to use AppRuntime for runtime functions and Database for types
app/discord/*.ts Updates imports from Database to AppRuntime
app/helpers/cohortAnalysis.ts Updates imports from Database to AppRuntime
app/effects/runtime.ts Updates imports from Database to AppRuntime

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +6 to +8
// App layer: database + PostHog + feature flags
// FeatureFlagServiceLive depends on both DatabaseService and PostHogService
const AppLayer = Layer.mergeAll(DatabaseLayer);
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment mentions "database + PostHog + feature flags" but the AppLayer only merges DatabaseLayer. If PostHog and feature flags are intended to be part of this layer, they're missing from the implementation. If they're not yet implemented, the comment should be updated to reflect the current state.

Copilot uses AI. Check for mistakes.
app/server.ts Outdated
Comment on lines 80 to 143
const handleShutdown = (signal: string) =>
runtime
.runPromise(
Effect.gen(function* () {
yield* logEffect("info", "Server", `Received ${signal}`);
try {
yield* checkpointWal();
yield* logEffect("info", "Server", "Database WAL checkpointed");
} catch (e) {
yield* logEffect("error", "Server", "Error checkpointing WAL", {
error: String(e),
});
}
process.exit(0);
}),
)
.then(() => runtime.dispose().then(() => console.log("ok")));
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The shutdownMetrics() function is no longer being called in the new shutdown handler. The old gateway.ts had a call to shutdownMetrics() before shutdownDatabase(), but this has been removed. PostHog events may not be flushed properly during shutdown, potentially losing analytics data. Consider adding a call to shutdownMetrics() in the shutdown handler in server.ts, or ensuring PostHog is properly disposed through the runtime.

Copilot uses AI. Check for mistakes.
@github-actions
Copy link

github-actions bot commented Feb 3, 2026

Preview environment removed

The preview for this PR has been cleaned up.

vcarl and others added 8 commits February 3, 2026 19:14
Use SqliteClient.make with Layer.scoped so the connection lifetime is
tied to the layer scope (Effect.scoped was closing it immediately).
Inline PRAGMA busy_timeout into the layer construction. Convert
integrity check to Effect.repeat(Schedule) and checkpointWal to return
an Effect. Remove runtime, bridge functions, and shutdown helpers that
move to AppRuntime in the next commit.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Move ManagedRuntime, layer composition, and bridge functions (db, run,
runTakeFirst, runTakeFirstOrThrow, posthogClient) from Database.ts into
a dedicated AppRuntime.ts. The AppLayer now includes DatabaseLayer,
PostHogServiceLive, and FeatureFlagServiceLive. Update all consumer
imports to use #~/AppRuntime.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Wrap the startup sequence in an Effect.gen pipeline: init Discord
gateway, fork integrity check as a daemon fiber, register commands,
and wire up graceful shutdown (WAL checkpoint + runtime dispose).

Convert gateway init and registerCommand to return Effects so they
compose into the startup pipeline. Move shutdown handler from gateway
to server.ts where the runtime is available.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@vcarl vcarl merged commit ddcd6c5 into main Feb 4, 2026
5 checks passed
@vcarl vcarl deleted the vc-expand-effect-radius branch February 4, 2026 00:35
@vcarl vcarl restored the vc-expand-effect-radius branch February 4, 2026 00:57
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