Architecture decision record · ADR-0005

The Lattice wire protocol, version 1

The package exports TypeScript types for RowsRequest, RowsResponse, FilterNode, RowOp, Transaction, MutateResult and ColumnSchema.

Status
Proposed
Deciders
Joe Gaudet
Created
Updated
PRD
PRD
Related
None

Context

  • Plan: section 5.3 (decision 16), sections 5.1, 5.4 and 5.5
  • Package: @ember-lattice/protocol

Lattice loads rows from servers in three languages: Node, Ruby and Python. All three must give the same rows for the same request. The grid also must work with servers that already speak the AG Grid server-side row model (SSRM).

A grid request is hostile input. It names columns, ops and aggregate functions. If a server puts these names into SQL without a check, an attacker can read data or break the database.

SQL databases do not agree on some rules. Postgres sorts nulls last in ascending order and first in descending order. MySQL does the opposite. Collations differ. Each database has its own rules for text case. If the protocol does not set these rules, the three servers give different rows.

JSON Schema can check the shape of a message. It cannot check an allow-list, the depth of a recursive tree, or a rule such as start <= end.

Decision drivers

Not recorded in the original ADR.

Options considered

Not recorded in the original ADR.

Decision

Messages

The package exports TypeScript types for RowsRequest, RowsResponse, FilterNode, RowOp, Transaction, MutateResult and ColumnSchema. It has no runtime dependencies and imports nothing from Ember. The build inlines @sindresorhus/is.

The request follows plan 5.3. This ADR adds these fields and rules to plan 5.3:

  1. RowsRequest.count?: boolean asks for count. Plan 5.4 rule 6 needs a way to ask.
  2. Aggregate.as?: string sets the output key of an aggregate. The default key is fn(column), for example sum(salary). The AG Grid translator sets as to the column id.
  3. A group row has the key $count: the number of leaf rows in the group. Column names cannot start with $, so the key cannot collide with a column.
  4. ColumnSchema.key: true marks the unique row id. The key is always the last sort key. Cursor paging needs it.
  5. The filter node uses one discriminant, op: { op: 'and' | 'or', nodes }, { op: 'not', node }, or a leaf { column, op, value? }.
  6. A pivot column has a field. The field is the compact JSON array of the pivot keys and the value key, for example ["east","sum(salary)"]. JSON gives one field for each pivot column, also when a key contains a separator.

The trust boundary

Every server calls parseRowsRequest(input, allowList) before it does any work. The allow-list is a list of ColumnSchema. Each entry names a column, its type, its filter ops, its aggregate functions, and whether it can sort, group or search.

The parser:

  • Rejects unknown keys on every object. This rejects __proto__ and constructor.
  • Looks up columns in a Map, never in a plain object.
  • Checks that each op and each aggregate function fits the column type. For example, it rejects contains on a number even when the allow-list permits it.
  • Rejects an aggregate as that is not an identifier, or that is __proto__, constructor or prototype.
  • Never throws. It returns { ok: true, value } with a fresh copy, or { ok: false, errors }. Each error has a path and a code.

The limits are in LIMITS:

Limit Value
Filter depth (and, or, not nesting; a root leaf is depth 1) 16
Filter nodes in one tree 256
Values in one filter tree (each is one SQL bind parameter) 1000
Items in one list (in values, and/or nodes, columns, aggregates) 1000
Sort items 16
group.by and pivot.by 16
Rows in one page 10000
String value, search, cursor length 1024, 256, 4096
Distinct pivot values in the scope (checked at run time) 200
Errors returned 50

The parser checks a length before it reads a list, and a depth before it reads a node. A server must also limit the request body size. A server rejects a bad request with HTTP 400 and the body { "errors": ProtocolIssue[] }.

Semantics

The reference evaluator, evaluate(request, rows, allowList), is the executable spec. Its rules are possible in plain SQL on Postgres, MySQL and SQLite:

  • Filter logic. Filters use SQL three-valued logic. A comparison with null is unknown. Only a true result keeps a row. So ne, notIn and not do not match null values.
  • Blank. On a string column, blank matches null and the empty string. On other column types, it matches null only. notBlank is the opposite.
  • Text. eq is case-sensitive. contains, startsWith and endsWith ignore case. The conformance data uses ASCII only. %, _ and \ in a value are literal characters. A server that uses LIKE must escape them.
  • Ranges. between includes both ends. inRange is for dates. It includes the start and excludes the end.
  • Dates. Dates are ISO 8601 strings. A date-time without a zone is UTC.
  • Sort. Nulls are last by default, in both directions. Strings sort by binary collation (UTF-16 code unit order for ASCII). The key column is the last sort key, ascending.
  • Quick search. A case-insensitive substring match on the string columns with search: true.
  • Groups. When keys.length < by.length, the server returns group rows for by[keys.length]. Else it returns leaf rows. A group row has the group value, the aggregates and $count. Group rows sort by the group column. A null key is a group.
  • Aggregates. sum, avg, min and max ignore nulls, and give null when no value is present. count counts the values that are not null. first and last take the value from the first and last row in sort order.
  • Pivot. The pivot columns are the distinct pivot keys in the scope, sorted ascending with nulls last. Each group row has a cell for each pivot column. At the leaf level, the response has one aggregate row.
  • Projection. Leaf rows hold only allow-listed columns. With columns, they hold those columns and the key. The key is always in the row, also when columns does not name it. The cursor comes from the full row, not the projected row. So a SQL server selects the sort columns also when columns leaves them out.
  • Paging. An offset range is [start, end). A cursor range is keyset paging. A cursor is opaque: only the server that made it reads it. A cursor response has cursor.next when rows follow, and cursor.prev when rows come before. A cursor is client input. A server must check each decoded cursor value against the type of its sort column and bind it as a parameter, or sign the cursor.
  • Response fields. count is present when the request asks, or when the page is the last page. total is present when aggregate is not empty. pivotColumns is present when pivot is set.

JSON Schema

The package exports a draft 2020-12 schema for each message: rowsRequestSchema, rowsResponseSchema, rowOpSchema, transactionSchema and mutateResultSchema. The schema limits come from LIMITS. The schemas cannot check the allow-list, the filter depth, the node count, start <= end, or duplicate output keys. A test checks that the schema and the parser agree on a set of valid and invalid fixtures.

Conformance

conformanceCases() returns the suite as plain JSON. The committed file packages/protocol/conformance/cases.v1.json holds the same data, and a test keeps them equal. The npm package ships the file. There are three kinds of case:

  • A response case: send the request, and compare the response.
  • A walk case: send the request, then follow cursor.next until it is absent. Compare each page. The last page must have count.
  • An error case: the server must reject the request with an error that has the given code.

The comparison rules (compareResponse): rows come in the same order with the same keys. Numbers match within 1e-9, relative to the larger value. Only the presence of a cursor matters, not its value.

AG Grid translator

fromAgGrid(request, options) translates an IServerSideGetRowsRequest into a RowsRequest. toAgGrid(response, request) translates a RowsResponse into LoadSuccessParams. AG Grid replaces its grand total row with any grandTotalData. So toAgGrid sends total as grandTotalData only when it gets the AG Grid request and the request has no group keys. The translator output is not trusted: the server must still call parseRowsRequest.

The AG Grid shapes are copied from the AG Grid source, branch latest, commit a87e7f20590bad427b8f2af85a0a9e925baa1f0d (package version 36.3.0-beta.20261201.1142; latest release tag release-36.3.0). The files, under https://github.com/ag-grid/ag-grid/blob/a87e7f20590bad427b8f2af85a0a9e925baa1f0d/packages/ag-grid-community/src/:

  • interfaces/iServerSideDatasource.ts
  • interfaces/iServerSideRowModel.ts
  • interfaces/iColumnVO.ts
  • interfaces/iSortModelItem.ts and interfaces/iSort.ts
  • interfaces/iFilter.ts, interfaces/iSetFilter.ts and interfaces/iMultiFilter.ts
  • interfaces/advancedFilterModel.ts
  • filter/provided/iProvidedFilter.ts and filter/provided/iSimpleFilter.ts
  • filter/provided/text/iTextFilter.ts, number/iNumberFilter.ts, date/iDateFilter.ts and bigInt/iBigIntFilter.ts
  • filter/provided/iScalarFilter.ts and filter/provided/scalarFilterHandler.ts

The mapping:

  • Column filter model: each column gives one node, and the nodes join with and. A model with operator is a combined model. A multi filter joins its children with and and skips null children. A set model maps to in, and a null value maps to blank. empty is no condition.
  • Advanced filter model: a join maps to and or or. Each leaf maps by its filterType. A set leaf maps to in or notIn. A boolean leaf maps to eq.
  • Text options map to the Lattice ops. notContains maps to not(contains).
  • inRange excludes both ends, as the AG Grid default (inRangeInclusive is false) does. It maps to gt and lt. With inRangeInclusive: true it maps to between.
  • AG Grid sends group keys and set values as strings. With the columns option, the translator changes them to numbers or booleans by column type.
  • Pivot result fields join the pivot keys and the value key with _, the AG Grid default serverSidePivotResultFieldSeparator.
  • AG Grid does not allow a null group key in the server-side row model. Its warning 190 says "null and undefined values are not allowed for server side row model keys" (validation/errorMessages/errorText.ts, and serverSideRowModel/blocks/blockUtils.ts in ag-grid-enterprise). Lattice null groups do not work with an AG Grid client.
  • The translator rejects date presets, custom filter options, bigint filters and sort.type: 'absolute'. It throws AgGridTranslationError.

v1 changes (2026-12-01)

These changes apply to version 1. The three server ports must follow them.

  1. Cursor and projection. A cursor holds the sort values of the full row. Before this change, the reference evaluator made the cursor from the projected row. When columns left out a sort column, that value became null and a walk skipped rows. Two walk cases now use a projection: one leaves out a sort column, and one leaves out the key.

  2. NUL. The parser rejects every string that contains U+0000, with the code bad_string. This applies to values, in lists, search, cursors, column names, group keys and aggregate as. Postgres cannot store U+0000 in text. The JSON Schema strings have the pattern ^[^\u0000]*$.

  3. Dates. A date or a date-time must have a written year from 0001 to 9999 (ISO 8601 with four digits). The parser rejects other years with the code invalid. The check uses the year as written, not the year after the zone offset.

  4. Pivot width. LIMITS.maxPivotValues is 200. It limits the distinct pivot.by value tuples in the scope of the request (after the filter, the search and the group keys). A null value counts as one value. The number of value aggregates does not change the count. The values come from the data, so the check is at run time: evaluate throws ProtocolValidationError with the code pivot_too_wide and the path pivot. A server must count the distinct pivot values before it builds the pivot SQL, and reject the request with HTTP 400 when the count is too high.

  5. Lone surrogates. The parser rejects every string that contains a lone UTF-16 surrogate, with the code bad_string. A lone surrogate is a high surrogate without a low surrogate after it, or a low surrogate without a high surrogate before it. This applies in the same places as the NUL rule. Ruby and Python reject invalid UTF-8, so all servers must agree. A valid surrogate pair (for example an emoji) is permitted.

  6. Calendar dates. The parser rejects a date or a date-time with a field that does not exist on the calendar, with the code invalid. The month is 01 to 12. The day is 01 to the last day of the month. February has 29 days in a leap year: a year that 4 divides, except a year that 100 divides and 400 does not (2000 and 2024 are leap years, 1900 is not). The hour is 00 to 23, the minute and the second are 00 to 59, and there is no leap second. Hour 24 is permitted only as the end of the day: 24:00, 24:00:00 or 24:00:00.000. The offset hour is 00 to 23 and the offset minute is 00 to 59. Before this change, the parser accepted some of these values, because JavaScript Date moves 2020-02-30 to 2020-03-01.

  7. Tree data (2026-12-02, ADR 0022). RowsRequest.tree and the column flags parent and path are new, optional fields. A request without tree gives the same response as before. A server that does not know tree rejects it with unknown_key. ADR 0022 has the rules.

The conformance suite now has the datasets people, wide for the pivot width case, and tree_parent and tree_path for tree data (item 7). Each case names its dataset, so a port must load every dataset in the file.

Consequences

  • One parser guards every server. The Ruby and Python packages must port the parser and pass the same error cases.
  • The conformance suite fixes null order, collation, three-valued logic and aggregate rules. Each SQL server must state them in SQL: NULLS LAST (or an emulation on MySQL), a binary collation, and LIKE with escaped %, _ and \.
  • For blank, a string column uses col IS NULL OR col = ''. Every other type uses col IS NULL. On Postgres, int_col = '' is an error. On MySQL, '' becomes 0, so = '' also matches 0 and false. A conformance case checks blank on a boolean.
  • The parser limits a filter to 1000 values. One value is one bind parameter, so a query stays under the Postgres and MySQL cap (65535) and the SQLite cap (32766). Old SQLite builds have a cap of 999.
  • A server that uses IN () must not get an empty list: the parser rejects empty lists. The translator maps an empty AG Grid set selection to blank AND notBlank on the same column, so that column must allow both ops.
  • The pivot field format is JSON. Ports must write compact JSON (no spaces) to match.
  • parseRowsRequest rejects an aggregate as that is not an identifier. AG Grid column ids with dots (for example athlete.name) do not work as value columns.
  • AG Grid text equals ignores case by default. Lattice eq does not. The translator keeps the Lattice rule.
  • The translator serves an AG Grid client with a Lattice server. The data-ag-grid adapter (plan 5.5) needs the other direction: a Lattice request to an AG Grid backend. That work is not in this ADR.
  • JSON Schema does not limit the filter depth. Only the parser does.

Confirmation

Not recorded in the original ADR.

Interview record

Each question of the interview with its answer and its source. A fact from the code or a document, a question that the author did not ask, and an assumption are rows too.

QuestionAnswerSource
Where does the text of this page come from?From the Markdown file docs/adr/0005-wire-protocol.md at commit 4c77ac4. The text of each section is the text of the Markdown file. A section that the Markdown file does not have says so.Document: docs/adr/0005-wire-protocol.md

Changelog

DateAuthorChange
2026-12-01Joe GaudetStatus in the Markdown file: proposed
2026-12-01Joe Gaudetv1 changes (2026-12-01)
2026-12-03Joe GaudetCommit 7ce4af2: Lattice 1.0 (#1)
2026-12-04Joe Gaudet (agent)Converted from docs/adr/0005-wire-protocol.md to this page. The text is the text of the Markdown file.

The adr skill of skillsaw wrote this page. The HTML in this file is the source of truth. The diagrams are the Mermaid sources in it.