6 Commits

Author SHA1 Message Date
Pekka Enberg
230f652b5b deps: update brace-expansion to fix quadratic parsing of trailing braces
Expanding input shaped like {a}}}}...,z} takes time quadratic in the
number of literal } characters. The parser rewrites the string and
restarts the scan once per brace, and each rewrite also grows the
working string by about 25 characters. A 128 KB pattern blocks the
event loop for close to half a minute. The max and maxLength caps do
not help because the cost is in parsing, before any results exist.

Every major line in use here is affected, so this moves each pinned
copy to the patched release on its own line: 1.1.21, 2.1.7 and 5.0.12.
npm also re-synced stale metadata for the locally linked
@tursodatabase packages while rewriting the lockfiles.

Tests: ran the advisory's reproduction against serverless/javascript
before and after npm ci. On 2.1.4, n=32000 took 2358 ms. On 2.1.7 it
took 15 ms.
2026-10-14 09:49:10 +03:00
Preston Thorpe
6ac915124f Merge 'batch API: full per-statement results in every driver, fixing a complaint we've carried since libSQL' from Glauber Costa
Ever since the libSQL days, the batch API has had the same complaint:
you
could not get the result of each individual statement, and when a batch
failed you could not tell which statement failed — or what had already
taken effect. With Turso we have a blank slate, and it turns out the
protocol never was the limitation: a batch response carries complete
per-statement information (results, last insert rowid, error codes, and
the execution statistics `rows_read`, `rows_written`,
`query_duration_ms`; PROTOCOL.md §6.2, §8.4, §9.1). The drivers just
dropped it. This PR takes the opportunity to do it right.
Every driver now exposes one `batch()` API that returns everything the
protocol reports, per statement, in one HTTP round trip:
- **Per-statement results**: rows, affected row count, last insert
  rowid, and the server-side execution statistics.
- **Failure visibility**: statements execute in order and stop at the
  first failure; the error identifies the failing statement by index
  *and* carries one entry per statement — the completed statement's
  result, or null/None for the failing statement and the ones that never
  ran. In a non-atomic batch the completed statements are committed, and
  the caller can finally see exactly what took effect.
- **Atomic mode**: the transactional variant wraps the statements in
  `BEGIN <mode>` / `COMMIT` with a conditional `ROLLBACK`, carried by
the
  same single request.
Per driver:
- **JavaScript** (`serverless/javascript`, existing public API —
extended
  compatibly): each `ResultSet` gains `lastInsertRowid`, `rowsRead`,
  `rowsWritten`, `queryDurationMs`; the thrown `DatabaseError` gains
  `batchIndex` and `batchResults`. `batch()` moves from the cursor
  endpoint to a pipeline batch request because the cursor stream's
  `step_end` does not carry the statistics. Non-atomic batches are now
  gated on the previous statement succeeding — previously statements
  after a failure kept executing server-side.
- **Rust** (new API, `bindings/rust` + `serverless/rust` in one commit
  per the driver-parity rule): `Connection::batch` /
  `Connection::transactional_batch`,
`BatchStatement`/`IntoBatchStatement`
  inputs, `BatchResult` with stats accessors, and
  `Error::BatchStatementFailed { index, error, results }`. The sync
  driver shares the embedded `Connection`, so all three backends expose
  the same API.
- **Python** (new API, `bindings/python` + `serverless/python` in one
  commit): `Connection.batch(statements, mode=None)` on the DB-API
  surface (sync, aio, and the sync-database driver via inheritance),
  raising the classified DB-API exception with `batch_index` and
  `batch_results` attributes.
The parity requirement itself is codified first, in
`serverless/conformance/differential/README.md` ("Driver API parity")
with a pointer from the AGENTS.md core principles.
**Deliberately skipped: Go.** The Go serverless driver is a plain
`database/sql` driver, and `database/sql` has no batch surface, so it is
less obvious what to do there: libsql-client-go's precedent is splitting
multi-statement SQL client-side (with a vendored ANTLR SQLite parser)
inside `Exec`, while the alternative is a driver-specific `Batch` API
outside the standard interface. That trade-off deserves its own
discussion and PR.
The batch behavior is property-tested: a `param_batch` operation in the
shared differential vocabulary (`spec/ops.json`) generates 1-4
parameterized statements — including planted failures and atomic modes —
and all three language harnesses (fast-check, hypothesis, hegel) assert
that the embedded and serverless drivers agree on every statement's
result, the failing index, and the partial results carried by the error.
Building those harnesses caught two real driver bugs before the tests
ever ran: the embedded JS driver had no batch error surface at all, and
the embedded Rust driver reported the connection's stale change counter
for row-returning statements inside a batch.
Follow-ups, out of scope here:
- Whether the JS `compat` (libsql-compat) layer should surface the new
  per-statement `ResultSet` fields on success; it is kept libsql-shaped
  here (the error path does preserve `batchIndex`/`batchResults`).
- Driver-wide preservation of `extended_code` on errors (today only the
  Python serverless `ServerError` keeps it).
- A batch entry containing multi-statement SQL ("a; b") errors on the
  embedded drivers but the server prepares only the first statement — an
  embedded/serverless divergence worth its own discussion.
Also rides along in the JS commit: four env-gated integration tests
still
asserted positional access on expanded rows, which f52724d45 removed on
purpose; CI never runs those tests, so they had drifted.
Tested against a live libsql-server and against Turso Cloud: Rust 44
unit + 58 conformance tests plus the hegel parity property, Python 73
serverless + 236 embedded plus the hypothesis parity property,
JavaScript 42 integration tests plus the fast-check parity property, and
protocol-level coverage asserting a parameterized multi-statement batch
in a single pipeline POST.
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Closes #8535
2026-08-29 08:36:30 -07:00
Amp
577078e479 serverless/js: update vulnerable transitive dependencies
Refresh tar, brace-expansion, and js-yaml to fixed versions in the serverless package and differential test locks.

Tests: npm ci --ignore-scripts; npm run build; npx ava integration-tests/protocol.test.mjs; npm audit --package-lock-only
Co-authored-by: pedro muniz <pedro.muniz.carlo@gmail.com>
2026-08-29 16:58:27 +03:00
Glauber Costa
db451100e8 differential: specify and exercise the batch API parity contract
State the parity requirement in the serverless differential conformance
spec — each serverless driver exposes the same public API as its
language's embedded driver, and non-JavaScript serverless drivers track
the JavaScript driver's capabilities — and reference it from the
AGENTS.md core principles.

Add the param_batch operation to the shared vocabulary (spec/ops.json,
documented in operations.md) and implement it in the JavaScript,
Python, and Rust harnesses: 1-4 statements through the batch() API —
parameterized INSERTs drawing from the adversarial value pool, SELECTs,
and statements from error_sqls — optionally atomic via a mode of
deferred or immediate. The embedded and serverless drivers must agree
on every statement's result; on failure they must agree on the failing
statement's index and on the per-statement results carried by the
error, entry by entry. An agreed batch failure leaves well-defined
state, so the harnesses keep comparing later operations — that is what
catches an atomic batch that reports the right index but fails to roll
back. The server-side execution statistics are excluded from the
comparison: the serverless driver reports them and the embedded driver
does not, by design.

The embedded drivers execute a batch as a sequential loop that stops at
the first error, so the comparison is the oracle for the serverless
batch: one pipeline request must behave exactly like sequential
execution that stops at the first failure.

The Rust generator fails the suite deterministically, with an
actionable message, when ops.json contains an operation it does not
implement, instead of panicking mid-case on an unknown id.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-28 10:40:26 -07:00
Glauber Costa
367fe14dff serverless: send the remote encryption key with every request in all drivers
Only the JavaScript serverless driver supported a remote encryption key,
sent as the x-turso-encryption-key header. Add the same support to the
Go, Python, and Rust drivers:

- Go: remote_encryption_key DSN parameter and
  Connector.WithRemoteEncryptionKey
- Python: connect(remote_encryption_key=...)
- Rust: Builder::with_remote_encryption_key

Document the header in PROTOCOL.md section 3.1 and make it an enforced
property: the shared differential spec gains a tests.encryption_header
entry, and every harness now runs a property test against a local stub
HTTP server asserting that a driver configured with a key attaches the
header to every request on both the pipeline and cursor endpoints, and
that a driver without a key never sends it. These tests need no Turso
Cloud database and never skip.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-07 12:54:33 -05:00
Glauber Costa
c3653536f5 serverless: add differential test suite for serverless drivers
Add property-based differential tests that compare an embedded Turso
driver against the serverless driver of the same language, running
against a live Turso Cloud database configured with TURSO_DATABASE_URL
and TURSO_AUTH_TOKEN. The tests skip when the variables are unset.

Random operation sequences (DDL, DML, queries, parameter binding,
transactions, batches, triggers, error cases) execute on both drivers,
and every result must match: success or failure, column names and
counts, row counts, value type tags, cell values, affected row counts,
and last insert rowids. Because the workloads are generated, the suite
finds divergences no hand-written test covers, and fast-check shrinks
failures to a minimal reproducing sequence.

The shared operation vocabulary lives in
serverless/conformance/differential/spec/ops.json so every language
harness faces the same workloads; each harness lives next to the driver
it tests. The JavaScript harness in serverless/javascript/differential
covers @tursodatabase/serverless against the native
@tursodatabase/database driver; since the serverless driver mirrors the
embedded API, one adapter drives both sides and any API divergence
fails the suite.

Co-authored-by: Pekka Enberg <penberg@iki.fi>
2026-07-22 17:30:34 +03:00