Skip to content

fix: surface sqlite startup context in standalone readiness errors - #65589

Open
yqecea wants to merge 2 commits into
anomalyco:devfrom
yqecea:sqlite-startup-errors
Open

yqecea wants to merge 2 commits into
anomalyco:devfrom
yqecea:sqlite-startup-errors

Conversation

@yqecea

@yqecea yqecea commented Nov 22, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #65547

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

When SQLite startup fails, the failure reached the log as a bare defect: the formatter loses SQLiteError.code/errno, and the standalone launcher discarded the child's stderr, so the generic "Standalone server exited before reporting readiness" was all a user saw.

Each database setup step now logs its operation name, the file role where the step determines it (main for open/migration, wal for journal_mode), and the driver's SQLiteError.code and numeric errno. The open is logged by tapping the cause of the provided sqliteLayer, because the driver dies inside Layer.orDie and the layer's Cause is the only place that still sees the SqlError. Bun surfaces SQLiteError.code/.errno; node:sqlite surfaces errcode plus its ERR_SQLITE_ERROR marker; the workerd driver has no result-code shape and logs nothing extra.

The launcher always pipes child stderr, keeps the last 8KiB, and appends it to the readiness failure. OPENCODE_PRINT_LOGS=1 still forwards the stream live. The readiness failure waits for the stderr drain to finish, but only in the interruptible region: an ensuring await there deadlocked scope close with a live child, since finalizers run sequentially and the stderr drain's own finalizer runs later.

How did you verify your code works?

  • bun run check passes (36/36 turbo tasks: lint + typecheck across the monorepo).
  • New test asserts the fields via Logger.formatStructured on a real open failure (operation: "open", role: "main", code: "SQLITE_CANTOPEN", errno: 14).
  • New fake-child tests cover the stderr tail in the readiness error, the empty-tail message, and the scope-close path that previously deadlocked.
  • bun test passes for packages/core/test/database.test.ts, packages/core/test/storage-failure.test.ts, and packages/cli/test/standalone-stderr.test.ts.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #55583, #59189

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Nov 22, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Works as described. With a directory as the database path, the base's acp printed only Standalone server exited before reporting readiness. With this PR, the child's stderr tail follows that line. Running serve --stdio directly, the log now has operation=open role=main code=SQLITE_CANTOPEN errno=14. The new cli and core tests pass. I couldn't run the typecheck scripts here: they call bun check, which this environment's bun 1.4.2 doesn't have.

Public API: unchanged. The launcher now always reads the child's stderr instead of ignoring it, and OPENCODE_PRINT_LOGS=1 still forwards it live.

Two small things are worth fixing, both inline: the new role field collides with the logger's existing role=server annotation, and on node:sqlite code is always ERR_SQLITE_ERROR.

Related: #55583, #59189

Comment thread packages/core/src/database/database.ts Outdated
const errno = resultCode(driver)
return Effect.logError("Failed to configure database", {
operation,
...(role === undefined ? {} : { role }),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The server's logger already tags every line with role=server, so the log line ends up with two role keys. Running serve against a bad path gives ... operation=open role=main code=SQLITE_CANTOPEN errno=14 error="..." role=server. Anything that parses these as key/value pairs will keep only one of them. A distinct key such as file: "main" (or databaseFile) would avoid the clash. The test asserts role and would need updating to match.

Option.map((error) => error.errcode),
Option.getOrUndefined,
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

code is a literal ERR_SQLITE_ERROR in this schema, so on node:sqlite errorCode always logs that fixed Node marker. That doesn't tell you anything the errno field doesn't already. node:sqlite errors also carry errstr (SQLite's text for the result code). Decoding errstr, or leaving errorCode undefined here like workerd does, would avoid a misleading code field.

Tag the database file under `file` so it does not collide with the
server logger's `role=server` annotation, and report node:sqlite's
`errstr` rather than its constant `ERR_SQLITE_ERROR` marker.
@yqecea
yqecea force-pushed the sqlite-startup-errors branch from 416267e to c6814a4 Compare November 22, 2026 22:42
@yqecea

yqecea commented Nov 22, 2026

Copy link
Copy Markdown
Author

Both points fixed in c6814a4:

  • The database file is now tagged under file instead of role, so it no longer collides with the logger's role=server annotation.
  • node:sqlite's errorCode now returns errstr instead of the constant ERR_SQLITE_ERROR marker.

Core tests still pass (3/3), full bun check 36/36 on the push hook.

@opencodejr

opencodejr Bot commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Thanks. I checked c6814a4 and both points are addressed. The database file is now logged as file, so it no longer clashes with role=server. On node:sqlite, errorCode now returns errstr instead of the fixed marker. One side effect: errstr is now required by the shared decodeDriverError schema, so resultCode (also used by storage-failure.ts) needs it too. I expect node:sqlite errors to always set errcode and errstr together, but I haven't checked that, and I didn't run the node path.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Both earlier points are fixed. The database file is now logged as file, which I confirmed by running serve against a bad path: operation=open file=main code=SQLITE_CANTOPEN errno=14. On node:sqlite, errorCode now returns errstr. On Node 24, node:sqlite errors carry errcode and errstr together, so resultCode is unaffected. The core database and storage-failure tests pass. Public API unchanged. Nothing new to raise.

This branch has not been deployed

No deployments
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.

Preserve the SQLite result code on standalone server startup failure

1 participant