Skip to content

Expose a PBN converter and withdraw the unreleased PBN twins - #401

Merged
zzcgumn merged 6 commits into
developfrom
feature/pbn_capable_c_exports
Sep 27, 2026
Merged

zzcgumn merged 6 commits into
developfrom
feature/pbn_capable_c_exports

Conversation

@zzcgumn

@zzcgumn zzcgumn commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Reworked following @tameware's review question — "have we considered deprecating the twins and providing a PBN to Deal convertor instead?". Investigated, and yes, for the unreleased part of the surface it's a clear win.

  • Expose the parser as dds_c_convert_from_pbn. Deal/DealPBN and DdTableDeal/DdTableDealPBN differ in exactly one field — the holdings; trump, first and currentTrick* are already binary — so one converter replaces a twin per function. Two consumers (the WASM shim at web/dds_web_wasm.cpp:191 and the Python context path at python/src/bindings.cpp:173) already hand-roll this conversion.
  • Withdraw dds_c_solve_board_pbn and dds_c_calc_par_pbn, plus their dds_* layer-2 counterparts. All four arrived in 5f8d4b11, which is not an ancestor of v3.1.0 — no tagged release carried them, so there's no compatibility window to honour. That window closes when v3.2.0 ships.
  • Keep dds_c_calc_dd_table_pbn. It shipped in v3.1.0 (3cbd3ea4), is in the pinned export set, and .NET binds it.
  • Keep the C++ solve_board_pbn / calc_par_pbn overloads as non-exported static-link conveniences. Neither is DLLEXPORT, so neither appears in any export list and neither carries ABI cost.
  • Java FFM binds the surviving twin plus the converter (the DEAL_PBN layout is gone with the twin it served).

Net effect on the pinned ABI: −2 symbols.

On the converter's contract

Copilot correctly spotted that dds_c_convert_from_pbn returns RETURN_NO_FAULT for a deal whose rank character is unrecognized (verified — it returns 1), which contradicted my header's promise of RETURN_PBN_FAULT for malformed input.

The resolution is documentation, not parser validation, because the leniency is deliberate:

  • library/tests/deal_input_validation_test.cpp:40 already pins this exact case ("a card replaced by an invalid rank") as rejected downstream with RETURN_CARD_COUNT, a resolution that came out of a fuzz finding (a 51-card deal reading past rel_rank_storage).
  • That same leniency is what lets a deal string carry a trailing \n/\r\n/\t from a PBN file. Making the parser strict breaks that test and would reject PBN-file line terminators across all 10 legacy *PBN entry points.
  • The converter cannot count cards itself either, since partial deals are legitimate mid-trick.

So the header now states what it actually guarantees: a successful parse is not a validated deal (check the solve/table status for RETURN_CARD_COUNT / RETURN_DUPLICATE_CARDS), and a failed parse does not preserve the output block — it is zeroed before validation, so converting in place into a live deal destroys the previous holdings. Both are pinned by tests.

Not addressed here

.NET needs no change — it binds the legacy dll.h twins (SolveBoardPBN, CalcParPBN) directly and dds_c_calc_dd_table_pbn, and never referenced the withdrawn symbols. Whether .NET should move onto the converter is a separate question that needs input from people who actually use that binding.

dll.h's 10 PBN twins are frozen for compatibility and unchanged.

Follow-up: deprecate the twins that did ship?

This PR only withdraws the two twins that never shipped — free to remove, no compatibility window. Two more PBN twins remain and are a genuinely separate decision, both blocked on the same thing:

  • dds_c_calc_dd_table_pbn / dds_calc_dd_table_pbn — the one surviving modern-layer twin. Shipped in v3.1.0; DdsNative.cs:86 binds it directly.
  • The 10 legacy dll.h twins (SolveBoardPBN, CalcDDtablePBN (+N/X), CalcAllTablesPBN (+N/X), SolveAllChunksPBN, CalcParPBN, AnalysePlayPBN, AnalyseAllPlaysPBN) — frozen for compatibility per this repo's own convention. Several are also bound directly by .NET.

Deprecating either means managing a real compatibility window for a shipped, bound symbol, not a free removal like this PR's withdrawal. Since .NET binds into both pools, this needs input from whoever owns that binding relationship before anyone touches it.

Test plan

  • bazel test //... — 100/100 pass.
  • dds_c_api_test covers the converter: reference-fixture agreement, null args → RETURN_UNKNOWN_FAULT, unparseable string → RETURN_PBN_FAULT, convert-then-solve reproducing the withdrawn twin's result, converter-vs-surviving-twin table agreement, unrecognized rank parsing through to RETURN_CARD_COUNT, and output-clobbering on failure.
  • dds_ffm_smoke_test covers convert-then-solve and pins RETURN_PBN_FAULT end to end through the JVM binding.
  • Export lists regenerated via gen_export_lists.py; export_set_test green.

🤖 Generated with Claude Code

dds_c_solve_board_pbn and dds_c_calc_par_pbn (and dds_c_calc_dd_table_pbn)
were already implemented and tested in the C shim per #284, but the Java
FFM binding (Dds.java) had no PBN support at all. Add the DealPBN /
DdTableDealPBN layouts and downcall handles, with smoke-test coverage
cross-checked against the same reference fixture used by dds_c_api_test.

Also fix specs/dds-public-api.md, which still claimed the shim had only
one PBN twin.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

A documentation coverage claim remains unresolved, so final human review is recommended.

Review effort: Lite
Findings: None

What changed in this PR

Adds Java FFM bindings and smoke tests for all three PBN APIs, and updates the public API documentation.

Changes:

  • Added PBN layouts, handles, and methods.
  • Added PBN solve, table, and par smoke tests.
  • Updated shim capability documentation.
File Description
specs/​dds-public-api.md Documents the PBN shim surface.
jni/​java/​org/​dds/​ffm/​DdsSmokeTest.java Tests PBN operations.
jni/​java/​org/​dds/​ffm/​Dds.java Adds PBN FFM bindings.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@zzcgumn

zzcgumn commented Sep 22, 2026

Copy link
Copy Markdown
Collaborator Author

A documentation coverage claim remains unresolved, so final human review is recommended.

I am not sure what Copilot means by this.

@tameware

Copy link
Copy Markdown
Collaborator

Nor am I! My guess is that it just wants me to look.

@tameware

Copy link
Copy Markdown
Collaborator

I asked GPT 5.4 (via a Copilot Agent) for clarification:

It means Copilot did not find a concrete bug, but it also could not verify the PR’s documentation claim with confidence.

In this PR, that likely refers to the statement that the docs/spec now correctly describe the Java shim’s PBN support. Copilot is basically saying:

  • “I see documentation changed.”
  • “I can’t prove the docs are fully complete/correct.”
  • “A human should make the final call.”

So this is a soft warning, not a real review finding. The key clue is:

  • Findings: None
  • Needs a closer look
  • final human review is recommended

In practice, it usually means: please manually confirm the updated doc matches the actual exported API surface.

tameware
tameware previously approved these changes Sep 22, 2026

@tameware tameware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM.

This is fine if we want to make the existing PBN twins available in every API. Have we considered deprecating the twins and providing a PBN to Deal convertor instead?

@zzcgumn

zzcgumn commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator Author

Have we considered deprecating the twins and providing a PBN to Deal convertor instead?

I will look into this suggestion. A converter might be a cleaner solution, and it shrinks the number of functions we support as part of the API.

Per review on #401: rather than grow one *_pbn twin per entry point in
every API, expose the parser itself. Deal/DealPBN and DdTableDeal/
DdTableDealPBN differ in exactly one field -- the holdings -- so a single
converter lets a binding parse a PBN string into the binary block and
then use the binary entry points.

Withdraw dds_c_solve_board_pbn and dds_c_calc_par_pbn, plus their dds_*
layer-2 counterparts. All four arrived in 5f8d4b1, which is not an
ancestor of v3.1.0, so nothing has shipped them and there is no
compatibility window to honour. dds_c_calc_dd_table_pbn did ship in
v3.1.0 and is bound by .NET, so it stays.

The C++ solve_board_pbn / calc_par_pbn overloads are kept: they cost
nothing at the ABI level and the batch/analyse paths still need them.
.NET is unaffected -- it binds the legacy dll.h twins directly and never
referenced the withdrawn symbols.

Co-Authored-By: Claude Opus 5 <[email protected]>
@zzcgumn zzcgumn changed the title Add PBN bindings to the Java FFM shim Expose a PBN converter and withdraw the unreleased PBN twins Sep 26, 2026
@zzcgumn

zzcgumn commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator Author

@tameware , Claude agreed that your suggestion creates a cleaner API. This pull request has been updated accordingly, but previous reviews are now stale.

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

Two moderate issues and one documentation nit remain unresolved.

Review effort: Lite
Findings: 1 Medium severity

Open (1)

Comment thread library/src/api/dds_c_api.cpp Resolved
Copilot flagged that dds_c_convert_from_pbn returns RETURN_NO_FAULT for a
deal with an unrecognized rank character, contradicting the header's
promise of RETURN_PBN_FAULT for malformed input. Verified: it returns 1.

The parser's leniency is deliberate, not a defect. Skipping unrecognized
characters is what lets a deal string carry a trailing newline from a PBN
file, and deal_input_validation_test already pins this exact case --
"a card replaced by an invalid rank" -- as rejected downstream on card
count, a resolution that came out of a fuzz finding. The converter also
cannot count cards itself, since partial deals are legitimate mid-trick.
So the defect was the contract I wrote, not the parser: making it strict
broke that test and would reject PBN-file line terminators.

Document what the function actually guarantees: a successful parse is not
a validated deal, and a failed parse does not preserve the output block
(it is zeroed before validation, so converting in place into a live deal
destroys the previous holdings). Both are now pinned by tests.

Also from the local branch review:
- Drop the false claim that the C++ solve_board_pbn / calc_par_pbn
  overloads serve the batch/analyse paths. They do not: those call
  convert_from_pbn directly, solve_board_pbn has one in-tree caller and
  calc_par_pbn has none. Neither is DLLEXPORT, so "for C++ consumers"
  was also wrong for shared-library consumers.
- Say "before any tagged release" rather than "before ever shipping", and
  note DDS_VERSION is not bumped between tags.
- Un-stale docs/jni_interface.md: add dds_c_convert_from_pbn and
  DD_TABLE_DEAL_PBN, and correct "the .NET binding uses all of it".
- Pin RETURN_PBN_FAULT in the JVM test instead of just "not success".
- Bump the spec's last-updated stamp.

Co-Authored-By: Claude Opus 5 <[email protected]>

Copilot AI 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.

Copilot review overview

🟡 Changes recommended

The converter’s failure-clearing contract and related parser, ABI, and API documentation need correction.

Review effort: Lite
Findings: 2 Low severity

Open (2)
Resolved since last review (1)

Comment thread jni/java/org/dds/ffm/Dds.java Resolved
Comment thread library/src/api/dds_c_api.h Resolved
Copilot correctly flagged that "unrecognized characters are skipped
rather than refused" overgeneralized: convert_from_pbn's else-if chain
(library/src/pbn.cpp:113-114) explicitly returns 0 for a compass letter
(N/E/S/W) found after the first hand, rather than falling through to the
silent skip that every other unrecognized character gets. Verified with
a new test pinning RETURN_PBN_FAULT for a stray compass letter, distinct
from the existing test pinning silent-skip-then-card-count-rejection for
an unrecognized rank.

Corrected both copies of the claim (dds_c_api.h, Dds.java); the spec
bullet doesn't get into this level of detail, so it needed no change.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Null PBN input leaves the destination unchanged despite the documented failure contract; related documentation also needs correction.

Review effort: Lite
Findings: None

Resolved since last review (2)
Previously missed (3)

In code that hasn't changed since last review

Low severity Narrow clobbering guarantee to valid native parse failures

jni/​java/​org/​dds/​ffm/​Dds.java:324

This repeats the same overbroad failure contract as the C header: passing a null MemorySegment for pbnDeal causes the native guard to return RETURN_UNKNOWN_FAULT before the output is cleared, and Java-side invocation errors can also occur before native parsing. Document the clobbering guarantee only for parse failures after valid segments/pointers have reached the native parser.

Low severity Document valid 80-byte PBN field boundary

library/​src/​api/​dds_c_api.h:77

The parser accepts a complete string whose NUL terminator is at byte 79 (80 bytes including the terminator); it rejects only when no terminator is seen within the 80-byte scan window, as covered by library/tests/pbn_test.cpp:136-149. Saying "shorter than 80 bytes including its terminator" incorrectly excludes that valid boundary case.

Low severity Narrow failure clobbering promise for null inputs

library/​src/​api/​dds_c_api.h:100

When pbn_deal is null, the implementation returns RETURN_UNKNOWN_FAULT before calling convert_from_pbn, so a non-null cards block is left unchanged. The blanket “On failure” wording here is therefore false for null-input failures and can make callers assume stale holdings were clobbered; narrow this promise to parse failures after both pointers have been validated (or explicitly clear cards before that return).

Copilot flagged two more overbroad claims in dds_c_convert_from_pbn's
contract, both confirmed against the implementation before fixing:

- "On failure the output is NOT preserved" was stated as a blanket rule,
  but dds_c_api.cpp's null-pointer guard returns RETURN_UNKNOWN_FAULT
  before convert_from_pbn is ever called, so a null pbn_deal leaves an
  existing cards block completely untouched, not zeroed. The clobbering
  only happens once the string reaches the parser and is rejected with
  RETURN_PBN_FAULT. Pinned with a new test that pre-fills cards with a
  sentinel and asserts it survives a null pbn_deal.

- "must be shorter than 80 bytes including its terminator" excluded a
  valid boundary: library/tests/pbn_test.cpp already pins that a
  terminator at byte 79 (80 bytes total) is accepted, since the parser's
  scan loop runs while bp < 80. Pinned at this layer too: the reference
  deal padded with skip-characters out to exactly that length still
  converts and solves identically.

Corrected both dds_c_api.h and the matching Dds.java javadoc, which also
needed the Java-specific case Copilot pointed out: an exception thrown
before the native call completes leaves cards untouched the same way a
null segment does. The spec doesn't restate either claim at this level
of detail, so it needed no change.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@tameware
tameware requested a lite review from Copilot September 26, 2026 21:01

Copilot AI 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.

Copilot review overview

🔵 Needs a closer look

Public ABI removals and cross-language binding changes warrant final human review.

Review effort: Lite
Findings: None

@tameware tameware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should we deprecate the PBN twin entrypoints?

Comment thread library/src/api/dds_c_api.h Resolved
@tameware on dds_c_api.h:82: "clients with PBN will need to call
dds_c_convert_from_pbn and then the binary solve or par" -- the comment
said solve/par have no PBN twin "because of" this function without
saying what a caller actually does with it. State the calling pattern
directly and name the two entry points, in both dds_c_api.h and the
matching Dds.java javadoc.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@zzcgumn

zzcgumn commented Sep 27, 2026

Copy link
Copy Markdown
Collaborator Author

Should we deprecate the PBN twin entrypoints?

We need input from people who use .Net extension.

@zzcgumn
zzcgumn merged commit 6b50519 into develop Sep 27, 2026
12 checks passed
@zzcgumn
zzcgumn deleted the feature/pbn_capable_c_exports branch October 9, 2026 23:16
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.

3 participants