-
Notifications
You must be signed in to change notification settings - Fork 122
FE-1569: Add durable Brunch storage and telemetry #9487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lunelson
wants to merge
10
commits into
ln/fe-1569-brunch-agent-container
Choose a base branch
from
ln/fe-1569-brunch-agent-deployment
base: ln/fe-1569-brunch-agent-container
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,221
β186
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7758848
Prepare Brunch deployment artifact and infrastructure handoff
lunelson 27242f6
Record Brunch image publication status
lunelson 770e71f
Document the trusted Flue SQL boundary
lunelson f7ef50b
Record direct-main deployment verification gaps
lunelson 327efa3
Refresh deployment dependency metadata
lunelson 0e98399
Format the restacked Brunch manifest
lunelson b04e679
Refresh Brunch dependency metadata after restack
lunelson 4dc89cd
Address Brunch deployment review findings
lunelson d1fc6ba
Handle Postgres pool failures
lunelson c079ffb
Add Brunch lifecycle scripts, a health integration test and `test:docβ¦
TimDiekmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /** | ||
| * The deployed conversation-store contract. | ||
| * | ||
| * Production accepts only dedicated Postgres fields. Local development and | ||
| * hermetic tests keep the existing SQLite path, but production can never | ||
| * silently select it. | ||
| */ | ||
|
|
||
| export const POSTGRES_ENV = { | ||
| authMode: "BRUNCH_POSTGRES_AUTH_MODE", | ||
| awsRegion: "BRUNCH_POSTGRES_AWS_REGION", | ||
| database: "BRUNCH_POSTGRES_DATABASE", | ||
| host: "BRUNCH_POSTGRES_HOST", | ||
| password: "BRUNCH_POSTGRES_PASSWORD", | ||
| port: "BRUNCH_POSTGRES_PORT", | ||
| tlsCaPath: "BRUNCH_POSTGRES_TLS_CA_PATH", | ||
| user: "BRUNCH_POSTGRES_USER", | ||
| } as const; | ||
|
|
||
| export interface SqliteDatabaseConfig { | ||
| readonly kind: "sqlite"; | ||
| } | ||
|
|
||
| export interface PostgresDatabaseConfig { | ||
| readonly kind: "postgres"; | ||
| readonly auth: | ||
| | { | ||
| readonly mode: "iam"; | ||
| readonly region: string; | ||
| } | ||
| | { | ||
| readonly mode: "password"; | ||
| readonly password: string; | ||
| }; | ||
| readonly database: string; | ||
| readonly host: string; | ||
| readonly port: number; | ||
| readonly tlsCaPath: string; | ||
| readonly user: string; | ||
| } | ||
|
|
||
| export type DatabaseConfig = SqliteDatabaseConfig | PostgresDatabaseConfig; | ||
|
|
||
| type Environment = Readonly<Record<string, string | undefined>>; | ||
|
|
||
| const valueOf = (environment: Environment, name: string): string => { | ||
| const value = environment[name]?.trim(); | ||
| if (value === undefined || value.length === 0) { | ||
| throw new Error(`Production database configuration requires ${name}.`); | ||
| } | ||
| return value; | ||
| }; | ||
|
|
||
| const absent = (environment: Environment, name: string): void => { | ||
| if (environment[name] !== undefined) { | ||
| throw new Error( | ||
| `Production database configuration does not accept ${name}.`, | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| const portOf = (environment: Environment): number => { | ||
| const name = POSTGRES_ENV.port; | ||
| const source = valueOf(environment, name); | ||
| if (!/^\d+$/u.test(source)) { | ||
| throw new Error(`${name} must be an integer between 1 and 65535.`); | ||
| } | ||
| const port = Number(source); | ||
| if (!Number.isSafeInteger(port) || port < 1 || port > 65_535) { | ||
| throw new Error(`${name} must be an integer between 1 and 65535.`); | ||
| } | ||
| return port; | ||
| }; | ||
|
|
||
| const rejectLegacyProductionInputs = (environment: Environment): void => { | ||
| absent(environment, "DATABASE_URL"); | ||
| absent(environment, "BRUNCH_DEV_DB_PATH"); | ||
| absent(environment, "BRUNCH_CHAT_DB_PATH"); | ||
| }; | ||
|
|
||
| export function loadDatabaseConfig( | ||
| environment: Environment = process.env, | ||
| ): DatabaseConfig { | ||
| if (environment.NODE_ENV !== "production") { | ||
| return { kind: "sqlite" }; | ||
| } | ||
|
|
||
| rejectLegacyProductionInputs(environment); | ||
|
|
||
| const authMode = valueOf(environment, POSTGRES_ENV.authMode); | ||
| const common = { | ||
| kind: "postgres" as const, | ||
| database: valueOf(environment, POSTGRES_ENV.database), | ||
| host: valueOf(environment, POSTGRES_ENV.host), | ||
| port: portOf(environment), | ||
| tlsCaPath: valueOf(environment, POSTGRES_ENV.tlsCaPath), | ||
| user: valueOf(environment, POSTGRES_ENV.user), | ||
| }; | ||
|
|
||
| if (authMode === "iam") { | ||
| absent(environment, POSTGRES_ENV.password); | ||
| return { | ||
| ...common, | ||
| auth: { | ||
| mode: "iam", | ||
| region: valueOf(environment, POSTGRES_ENV.awsRegion), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (authMode === "password") { | ||
| absent(environment, POSTGRES_ENV.awsRegion); | ||
| return { | ||
| ...common, | ||
| auth: { | ||
| mode: "password", | ||
| password: valueOf(environment, POSTGRES_ENV.password), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| throw new Error( | ||
| `${POSTGRES_ENV.authMode} must be either "iam" or "password".`, | ||
| ); | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,35 @@ | ||
| import { postgres } from "@flue/postgres"; | ||
|
|
||
| import { loadDatabaseConfig } from "./database-config.ts"; | ||
| import { conversationDbPath } from "./db-path.ts"; | ||
| import { createPostgresRunner } from "./postgres.ts"; | ||
| import { shutdownBrunchTelemetry } from "./telemetry-bootstrap.ts"; | ||
| import { recordOperationalFailure } from "./telemetry.ts"; | ||
|
|
||
| import type { DatabaseConfig } from "./database-config.ts"; | ||
|
|
||
| /** | ||
| * The substrate's conversation storage β host-authored because Flue requires | ||
| * it of the consuming app. | ||
| * | ||
| * Without this file conversations are process-memory and a restart loses them. | ||
| * Local development and hermetic tests retain SQLite. Production must provide | ||
| * the dedicated Postgres contract and cannot fall back to a task-local file. | ||
| */ | ||
| let config: DatabaseConfig; | ||
| try { | ||
| config = loadDatabaseConfig(); | ||
| } catch (error) { | ||
| try { | ||
| await recordOperationalFailure("database_configuration", error); | ||
| } catch { | ||
| // The database configuration error remains the authoritative startup cause. | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| import { sqlite } from "@flue/runtime/node"; | ||
|
|
||
| import { conversationDbPath } from "./db-path.ts"; | ||
| const database = | ||
| config.kind === "postgres" | ||
| ? postgres(createPostgresRunner(config, shutdownBrunchTelemetry)) | ||
| : (await import("@flue/runtime/node")).sqlite(conversationDbPath()); | ||
|
|
||
| export default sqlite(conversationDbPath()); | ||
| export default database; |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.