Skip to content

[bidi] Add tuple types for map entries to the schema - #18362

Open
pujagani wants to merge 3 commits into
SeleniumHQ:trunkfrom
pujagani:js-bidi-map-entry-tuples
Open

pujagani wants to merge 3 commits into
SeleniumHQ:trunkfrom
pujagani:js-bidi-map-entry-tuples

Conversation

@pujagani

Copy link
Copy Markdown
Contributor

🔗 Related Issues

BiDi sends a JavaScript Map or object as a list of [key, value] pairs. The spec defines each entry as a fixed pair: script.MappingLocalValue and script.MappingRemoteValue are

[*[(LocalValue / text), LocalValue]]
;   ^ key: a value or plain text   ^ value: always a full value object

so every entry has exactly two elements, and only the key may be plain text. The binding-neutral schema loses that, so no generator can enforce it.

💥 What does this PR do?

Where this shows up: script.callFunction

script.callFunction passes arguments to a function in the page and returns its result:

  • Outbound: script.CallFunctionParameters.arguments → script.LocalValue → script.MapLocalValue.value → script.MappingLocalValue
  • Inbound: script.CallFunctionResult → script.RemoteValue → script.MapRemoteValue.value → script.MappingRemoteValue

For example, calling (prefs) => prefs.get('theme') with a Map of theme → "dark" must send:

{ "type": "map", "value": [ ["theme", { "type": "string", "value": "dark" }] ] }

What is lost today

The schema on trunk (script.MappingLocalValue, script.MappingRemoteValue) projects the entry as:

"list": {
  "list": { "union": [{ "ref": "script.LocalValue" }, { "primitive": "string" }] }
}

That reads as "a list of any number of elements, each a value or a string". It drops two facts from the spec:

Spec says Schema today says
Exactly 2 elements Any number
Position 1 (the value) is a full value object only Any element may be a plain string

So the schema treats all of these as valid script.callFunction arguments, although only the first one is:

["theme", { "type": "string", "value": "dark" }]   // valid
["theme", "dark"]                                  // value is plain text
["theme"]                                          // no value
["theme", { ... }, { ... }]                        // three elements

What each binding does today:

  • JS and Java generate straight from the schema, so they send all of these unchecked and accept them unchecked in responses.
    • JS types an entry as Array<Array<LocalValue | string>>.
    • Java types it as List<List<Object>>, with only null checks.
  • Python and Ruby reject them, but only because their hand-written serializers assume that a list allowing plain strings must be a map of pairs. Nothing in the schema says so.

The changes fix this.

🔧 Implementation Notes

This PR covers only the schema, so the shared format change can be reviewed on its own.

🤖 AI assistance

  • No substantial AI assistance used
  • AI assisted (complete below)
    • Tool(s): Claude Code
    • What was generated: projection change, tests, regenerated schema
    • I reviewed all AI output and can explain the change

🔄 Types of changes

  • New feature (non-breaking change which adds functionality and tests!)

@selenium-ci

Copy link
Copy Markdown
Member

/agentic_describe

@selenium-ci selenium-ci added C-nodejs JavaScript Bindings B-devtools Includes everything BiDi or Chrome DevTools related labels Nov 20, 2026
@qodo-code-review

qodo-code-review Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

PR Summary by Qodo

Preserve fixed key-value tuple types in the BiDi schema

🐞 Bug fix 🧪 Tests 🕐 20-40 Minutes

Grey Divider

AI Description

• Preserve two-position map entries in the shared BiDi schema, including distinct key and value
 types.
• Keep the existing list projection for bindings that do not yet consume tuples.
• Validate tuple contents during schema checks and test projection from fixed-length CDDL arrays.
Merge Risk 🟡 Medium

Blast radius 🟢 Low 💥 1 capability

  • 1 affected end-to-end capability after fault-specific containment; implementation boundaries do not widen its functional scope.
  • The changed projector, committed schema artifact, and generators form one bounded integration whose end-to-end outcome is producing BiDi binding models from the normalized CDDL AST.
  • The tuple data is schema metadata crossing generator boundaries, not an independent storage or runtime capability; the existing script mapping behavior is one part of this same BiDi contract.
Sources

javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]

// Every member of a multi-member array must occur exactly once for its positions to be fixed;
// anything else fails closed rather than projecting a wrong arity.
function arrayNode(members) {
  const node = { list: projectRef(members[0]?.Type) }
  if (members.length < 2) return node
  const once = (m) => (m.Occurrence?.n ?? 1) === 1 && (m.Occurrence?.m ?? 1) === 1
  node.tuple = members.map((m) => (once(m) ? projectRef(m.Type) : { primitive: 'unknown' }))
  return node

common/bidi/schema.json[10285-10301]

          },
          "tuple": [
            {
              "union": [
                {
                  "ref": "script.LocalValue"
                },
                {
                  "primitive": "string"
                }
              ],
              "scalar": "string"
            },
            {
              "ref": "script.LocalValue"
            }
          ]
Behavioral complexity 🟡 Medium 🧩 Compatibility cases

  • Before: every array was projected as a list using the first member type, with no positional metadata.
  • After: multi-member arrays retain that list fallback but additionally expose a tuple whose positions are populated only when each member has exact one-time occurrence; otherwise the position becomes unknown.
  • New correctness condition: the projector must preserve both the legacy list contract and the exact positional contract, while failing closed for variable-arity members rather than emitting a misleading fixed arity.
  • This is a compatibility interaction between old list-only bindings, tuple-aware consumers, and occurrence metadata, not merely a new field.
Sources

javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]

// Every member of a multi-member array must occur exactly once for its positions to be fixed;
// anything else fails closed rather than projecting a wrong arity.
function arrayNode(members) {
  const node = { list: projectRef(members[0]?.Type) }
  if (members.length < 2) return node
  const once = (m) => (m.Occurrence?.n ?? 1) === 1 && (m.Occurrence?.m ?? 1) === 1
  node.tuple = members.map((m) => (once(m) ? projectRef(m.Type) : { primitive: 'unknown' }))
  return node

common/bidi/schema.json[10285-10301]

          },
          "tuple": [
            {
              "union": [
                {
                  "ref": "script.LocalValue"
                },
                {
                  "primitive": "string"
                }
              ],
              "scalar": "string"
            },
            {
              "ref": "script.LocalValue"
            }
          ]
Potential damage 🟡 Medium 🩹 Temporary loss

  • A faulty tuple projection can stop schema validation or binding generation for the affected BiDi contract.
  • Recovery is normally code correction followed by schema regeneration and rebinding; reverting the projector/schema change restores the prior list-only contract.
  • No persistent application state or external side effect is introduced, and the current inspected runtime consumers do not show automatic tuple-based recovery.
  • Recoverable by reverting or correcting the projector and regenerating the schema/bindings.
  • Until regeneration succeeds, the affected binding generation capability is unavailable; any already-published incorrect generated artifact would require replacement, not state repair.
Sources

javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]

// Every member of a multi-member array must occur exactly once for its positions to be fixed;
// anything else fails closed rather than projecting a wrong arity.
function arrayNode(members) {
  const node = { list: projectRef(members[0]?.Type) }
  if (members.length < 2) return node
  const once = (m) => (m.Occurrence?.n ?? 1) === 1 && (m.Occurrence?.m ?? 1) === 1
  node.tuple = members.map((m) => (once(m) ? projectRef(m.Type) : { primitive: 'unknown' }))
  return node

common/bidi/schema.json[10285-10301]

          },
          "tuple": [
            {
              "union": [
                {
                  "ref": "script.LocalValue"
                },
                {
                  "primitive": "string"
                }
              ],
              "scalar": "string"
            },
            {
              "ref": "script.LocalValue"
            }
          ]
Assessed commit d5c653f
Diagram

graph TD
  AST["Normalized CDDL"] --> Projector["Schema projector"] --> Schema["Shared schema"] --> Bindings["Binding generators"]
  Schema --> Checks["Schema checks"]
  Tests["Projection tests"] --> Projector
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Enforce pairs directly in each binding
  • ➕ Could provide immediate runtime or generated-type enforcement.
  • ➖ Duplicates spec rules across bindings.
  • ➖ Leaves the binding-neutral schema unable to express the distinction.

Recommendation: Keep the tuple metadata in the shared schema: it fixes the information loss at its source without changing existing list consumers. Binding-level enforcement can follow; this PR does not make current serializers reject malformed pairs.

Files changed (4) +89 / -7

Enhancement (1) +2 / -0
record.d.tsDeclare tuple metadata on schema type nodes +2/-0

Declare tuple metadata on schema type nodes

• Adds an optional 'tuple' property so embedded schema nodes type-check. The declaration does not change runtime value validation.

javascript/selenium-webdriver/bidi/serialization/record.d.ts

Bug fix (2) +59 / -7
schema.jsonAdd positional types to local and remote map entries +34/-2

Add positional types to local and remote map entries

• Adds two-element tuples to 'script.MappingLocalValue' and 'script.MappingRemoteValue'. Each tuple permits a string or value object as its key but requires a value object in its second position; the existing list type remains.

common/bidi/schema.json

project_bidi_schema.mjsProject fixed array positions and validate tuple contents +25/-5

Project fixed array positions and validate tuple contents

• Projects multi-member arrays into positional tuples alongside their existing list type, marking variable-occurrence positions unknown rather than asserting a fixed arity. Reference collection and schema checks now traverse tuple positions.

javascript/selenium-webdriver/project_bidi_schema.mjs

Tests (1) +28 / -0
project_bidi_schema_test.mjsTest tuple projection and variable-occurrence rejection +28/-0

Test tuple projection and variable-occurrence rejection

• Adds a map-entry fixture verifying distinct key and value positions while preserving the list projection. A second test confirms that a repeating position triggers the schema's unknown-primitive check.

javascript/selenium-webdriver/project_bidi_schema_test.mjs

@qodo-code-review

qodo-code-review Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (0) 📜 Skill insights (0)

Grey Divider


Remediation recommended

1. Repeated array positions get false arity 🐞 Bug ≡ Correctness
Description
arrayNode defaults a null upper occurrence bound to 1, so an unbounded + member is treated as
a fixed tuple position. When a multi-member array contains such a member, checkSchema accepts the
projected tuple rather than rejecting its incorrect fixed arity; the current schema has no such
array, but a later projection would carry the wrong shape.
Code

javascript/selenium-webdriver/project_bidi_schema.mjs[R199-200]

+  const once = (m) => (m.Occurrence?.n ?? 1) === 1 && (m.Occurrence?.m ?? 1) === 1
+  node.tuple = members.map((m) => (once(m) ? projectRef(m.Type) : { primitive: 'unknown' }))
Evidence
The projector defines null and Infinity upper bounds as unbounded, but the new once predicate
converts null to one. The added rejection test covers {n: 0, m: null} only, so it does not
exercise an unbounded member whose lower bound is one.

javascript/selenium-webdriver/project_bidi_schema.mjs[121-124]
javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]
javascript/selenium-webdriver/project_bidi_schema_test.mjs[199-202]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`arrayNode` treats an explicit null upper occurrence bound as if it were absent, incorrectly projecting a repeating `+` member into a fixed tuple position.

## Fix Focus Areas
- javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]
- javascript/selenium-webdriver/project_bidi_schema_test.mjs[199-202]

## Recommended Fix
Default only an absent `Occurrence` or absent `m` to one; keep an explicit null or Infinity maximum unbounded. Add a multi-member array test with `{n: 1, m: null}` that verifies `checkSchema` rejects its projection.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗


Grey Divider

Context sources
Review mode: Auto: ⚖️ Balanced: Schema projection changes affect generated bindings and validation behavior.

Grey Divider

Tip of the day
💡 Did you know, you can keep summaries lean with Findings visible per group, which tucks the rest behind a View link

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment on lines +199 to +200
const once = (m) => (m.Occurrence?.n ?? 1) === 1 && (m.Occurrence?.m ?? 1) === 1
node.tuple = members.map((m) => (once(m) ? projectRef(m.Type) : { primitive: 'unknown' }))

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.

Remediation recommended

1. Repeated array positions get false arity 🐞 Bug ≡ Correctness

arrayNode defaults a null upper occurrence bound to 1, so an unbounded + member is treated as
a fixed tuple position. When a multi-member array contains such a member, checkSchema accepts the
projected tuple rather than rejecting its incorrect fixed arity; the current schema has no such
array, but a later projection would carry the wrong shape.
Agent Prompt
## Issue description
`arrayNode` treats an explicit null upper occurrence bound as if it were absent, incorrectly projecting a repeating `+` member into a fixed tuple position.

## Fix Focus Areas
- javascript/selenium-webdriver/project_bidi_schema.mjs[194-201]
- javascript/selenium-webdriver/project_bidi_schema_test.mjs[199-202]

## Recommended Fix
Default only an absent `Occurrence` or absent `m` to one; keep an explicit null or Infinity maximum unbounded. Add a multi-member array test with `{n: 1, m: null}` that verifies `checkSchema` rejects its projection.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

@selenium-ci

Copy link
Copy Markdown
Member

/agentic_review

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

B-devtools Includes everything BiDi or Chrome DevTools related C-nodejs JavaScript Bindings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants