Skip to content

[search-position] Add the "Position" page of ChessBase's search dialog - #11

Open
tuliovianna-lab wants to merge 6 commits into
itshak:mainfrom
tuliovianna-lab:feat/search-position
Open

tuliovianna-lab wants to merge 6 commits into
itshak:mainfrom
tuliovianna-lab:feat/search-position

Conversation

@tuliovianna-lab

Copy link
Copy Markdown
Contributor

Summary

Thank you for integrating the "Game data" search into v0.3.0. This PR adds the next page of ChessBase's search dialog, Position, on top of your v0.3 query architecture. It touches roadmap item 5 (advanced-position-search-and-index-feeds, task 1.2): an explicitly defined partial-board predicate, with gigachess replay as the oracle.

GameDataQuery::position takes a PositionQuery, combined with every "Game data" field:

Dialog PositionQuery cbvault search
"Look for" board look_for --look-for "Kg1 Rf1 Pf2g2h2"
"Or" board (several men per square) or --or BOARD
"Exclude" board (several men per square) exclude --exclude BOARD
White / black jokers Man::Any(colour) X / x
Mirror: Horizontal / Vertical mirror_horizontal, mirror_vertical --mirror h|v|hv
First / Length / Last (defaults 1, 1, 40) first, length, last --first-move, --length, --last-move
Sacrifice sacrifice: refused, QueryError::Unsupported --sacrifice
cbvault search "Mega Database 2026.2cbh" --look-for "Bh7 kg8" --mirror h --elo 2600- --elo-mode both --result 1-0

Design

  • No new stage. The fragment compiles to bitboards, one set per orientation ([colour][role] plus a mask per colour for jokers). The existing replay stage tests them on each main-line position with piece_bb / occ_color, a dozen integer operations and no allocation.
  • Same walk, early stop. SelectedReplay::walk drives a sink that stops as soon as the answer is known: at the first match (unless an ending is also asked for), after "Last", or when the main line ends. With --last-move 1, the whole Mega takes 0.45 s.
  • Ending-only queries are untouched. A combined sink cost about 4% on "mate, 2025 on", so an ending-only query keeps the original FinalBoard sink. matches_ending became matches_replay, which dispatches on whether a pattern is present.
  • Refused instead of guessed. "Sacrifice" is ChessBase's own look-ahead, and which sacrifices it finds is not established. Empty boards, two men on one "Look for" square, and First after Last are Invalid.

Semantics, and what is still open

ChessBase's help ("Search mask – Position") establishes three things:

  • the "Look for" men must stand on their squares;
  • the "Exclude" men must not;
  • the jokers are "any white / any black man", and Length is counted in plies.

It leaves four points open. I implemented the reading below for each, but have not compared them with ChessBase's counts yet; they are listed in docs/search.md and in task 2.5 of the change:

  • Horizontal mirror: reflects files (a↔h).
  • Vertical mirror: reflects ranks (1↔8) with the colours swapped, following the help's "bishop on h7 or h2" example.
  • "Or": "Look for" and at least one "Or" man, not a second whole fragment.
  • Start position: counts as move 1.

Only the main line is searched; whether ChessBase also searches variations is open as well.

I computed the Mega 2026 counts for each competing reading, so a single ChessBase search settles each point. For example, a white bishop on h7 with the horizontal mirror gives 389,913 games if the mirror is a↔h, 401,207 if it is 1↔8 with the same colour, and 342,106 if it is 1↔8 with the colours swapped. I'm happy to share the list.

Measurements

Mega Database 2026 (2CBH, 11,740,862 records), warm, CBVAULT_QUERY_REPORT=1 cargo bench -p cbvault --bench query:

Query Matches 1 thread 10 threads
White bishop on h7, moves 1-40 (replays every game) 211,902 44.680 s 7.120 s
White pawn on e2, move 1 11,737,194 2.597 s 0.448 s
White bishop on h7, Elo ≥ 2700 both 611 0.440 s 0.084 s

Shared replay stage, before (v0.3.0) and after this PR. Mega times are the mean of three alternating runs.

Query Before After
"mate, 2025 on", Mega 2026, 1 thread 2.70 s 2.67 s
"mate, 2025 on", Mega 2026, 10 threads 0.416 s 0.407 s
query_mate_1_thread, fixture 3.60 ms 3.53 ms

On the fixture, the new query_position_1_thread takes 4.01 ms.

Verification

  • cargo fmt --check passes.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings passes.
  • cargo test --workspace --all-features passes, including:
    • tests/query_position.rs: every board, the jokers, the mirrors, First / Length / Last, and combinations with names and endings, on classic and 2CBH fixtures. It also checks invariance across thread counts, that a fragment only in a variation does not match, the refusals and cancellation.
    • cli_search.rs::position_options_reach_the_query.
  • OpenSpec change search-position (proposal, spec delta, tasks).

Provenance

The code is original. The controls and their meanings come from ChessBase's dialog and public help page. No third-party position-search code was read. gigachess stays the only chess core, and no dependency was added.

🤖 Generated with Claude Code

tuliovianna-lab and others added 2 commits November 30, 2026 09:29
- query::PositionQuery on GameDataQuery::position: "Look for", "Or" and
  "Exclude" boards of men (a piece of a colour, or a joker for any man of a
  colour), horizontal and vertical mirrors (colours swapped), and First,
  Length, Last with the dialog's defaults. "Sacrifice" is refused as
  unsupported.
- The fragment compiles to bitboards per orientation; the replay stage tests
  it on each main-line position in the same single replay as the endings, and
  stops once the answer is known. Ending-only queries keep their own sink.
- parse_fragment and the cbvault search options --look-for, --or, --exclude,
  --mirror, --first-move, --length, --last-move, --sacrifice.
- Tests on classic and 2CBH fixtures, CLI tests, fixture and whole-database
  benchmarks, docs/search.md and the OpenSpec change search-position.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ovenance and measurements

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@itshak

itshak commented Dec 1, 2026

Copy link
Copy Markdown
Owner

Thank you for this PR — the bitboard compilation, single-replay design, and keeping FinalBoard untouched for ending-only queries are all very clean, and the Mega 2026 measurements are helpful.

Before we merge, there are a few items to address:

1. ChessBase parity checks (OpenSpec task 2.5)

Task 2.5 is still unchecked, and docs/search.md lists five open semantic points:

  • Horizontal mirror axis (a↔h)
  • Vertical mirror axis (1↔8 with colours swapped)
  • "Or" combination (Look for AND at least one Or man)
  • Start position counting as move 1
  • Main line only vs. searching variations

Since you already computed the competing Mega 2026 counts, could you run those spot checks against ChessBase and record the exact query + counts?

2. Stop immediately at Last and on resume()

  • Last boundary (ending.rs / position.rs): MainLine::play captures board.fullmove_number() before the move, and MainLine::played passes that pre-move number to Tracker::position. After Black's move Last, board.fullmove_number() has already incremented to Last + 1, so the walker currently has to decode White's move Last + 1 before Tracker sees move_number > last and stops. Checking the post-move board.fullmove_number() > last (when endings aren't also requested) avoids decoding an extra ply on every non-matching game and prevents a corrupt move at Last + 1 from failing a valid 1..=Last search.
  • MainLine::resume() (ending.rs): resume() is currently a no-op {}. When end_line() / Token::EndOfLine finishes the main line and pops a variation, MainLine::self.main stays true until the next play() call, so stopped() returns false on resume() and forces the walker to decode the first move of the first variation before stopping. Setting self.main = false; in MainLine::resume() stops cleanly at the end of the main line without touching variation moves.

3. Additional test coverage (crates/cbvault/tests/query_position.rs)

Could you also add tests for:

  • Variation handling on twocbh_db (currently only tested on classic_db).
  • A positive mirror_horizontal && mirror_vertical match (all 4 orientations).
  • Boundary cases: a custom start position (Black to move, fullmove_number > 1), Length spanning up to Last, conflicting look_for/exclude on the same square, and a corrupt/illegal move immediately after Last (once item 2 is fixed).
  • An independent, simple exhaustive gigachess reference predicate on the fixture, as specified in openspec/changes/search-position/specs/cbvault/spec.md.

(Note on GameDataQuery::position: adding a public field is a struct-literal break for callers without ..Default::default(), which is fine pre-1.0 per ADR-008; we'll bump the minor version to 0.4.0 when releasing.)

tuliovianna-lab and others added 4 commits December 1, 2026 20:26
…eview tests

- Tracker::passed: after a main-line move, a next move past Last settles the
  answer, so the move after Last is never decoded (a corrupt one no longer
  fails a valid 1..=Last search).
- MainLine::resume ends the main line, so no variation move is decoded.
- Tests: a 2CBH variation, both mirrors together, a set-up start (Black to
  move on move 10), Length up to Last, the same man in Look for and Exclude,
  a corrupt move right after Last, and an exhaustive square-by-square
  gigachess reference over every man on every square plus 2,000
  pseudo-random combinations of boards, mirrors and windows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
"Vertical" reflects across the vertical axis (files a<->h); "Horizontal"
across the horizontal axis (ranks 1<->8) with the colours swapped. Checked
against ChessBase on Mega Database 2026 (white bishop on h7): Horizontal
344,642 (cbvault 342,106), Vertical 395,103 (cbvault 389,913); the dialog
draws the axis as a red line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Mirror axes, "Or" and the start position agree with ChessBase on Mega
Database 2026; ChessBase reads past move Last by a rule not yet
established, recorded with the counts in docs/search.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measured on Mega Database 2026: ChessBase looks at every position from the
start through move max(Last, First + 1), plus the position after White's
following move; First does not cut the positions before it. Eleven searches
agree within 0.03% (docs/search.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tuliovianna-lab

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review. Everything is addressed in 99b4257, 1761dd0, 0f1986b and 0c6e426.

2. Early stops

  • Once the next move falls outside the window, the answer is settled, so that move is never decoded. A test with a corrupt word right after the window checks this.
  • resume() now ends the main line.

3. Tests

  • A 2CBH variation, both mirrors together (a positive match only the reflection through the centre finds), and a set-up start (Black to move on move 10).
  • Length up to the window's end, and the same man in Look for and Exclude.
  • An exhaustive square-by-square gigachess reference: every man on every square, plus 2,000 pseudo-random combinations of boards, mirrors and windows.

1. ChessBase spot checks (Mega Database 2026, 11 searches)

  • Mirrors. They were the wrong way round. The dialog draws each axis as a red line: "Vertical" reflects across the vertical axis (a↔h), and "Horizontal" across the horizontal axis (1↔8) with the colours swapped. Fixed.
  • "Or" and the start position. "Or" works as "Look for and at least one Or man", and the start position counts. Both agree with ChessBase.
  • Variations. The dialog has an "Include lines in search" box, off by default, so main-line-only is ChessBase's default. The box is not offered here.
  • The move window. It is not what the dialog's labels suggest. ChessBase looks at every position from the start through move max(Last, First + 1), plus the position after White's following move. "First" does not cut the positions before it. cbvault now does the same.

All eleven counts agree within 0.03%:

Search (defaults unless stated) ChessBase cbvault
wBh7, Horizontal 344,642 344,630
wBh7, Vertical 395,103 395,089
wBc4, Or wNf5 47,245 47,244
wPe2, Last 1 11,737,625 11,737,194
wPe4 bPe5 wNf3, Last 1 1,323,164 1,323,125
wPe4 bPe5 wNf3, Last 2 1,323,164 1,323,125
wKg1, Last 5 2,125,503 2,125,073
wKg1, Last 10 7,308,826 7,308,249
bKg8, Last 10 6,702,349 6,701,773
wKg1, First 10, Last 10 7,794,831 7,794,229
wKg1, First 10, Last 40 9,239,530 9,239,209

The small remaining gap is not explained yet. It is recorded in docs/search.md.

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.

2 participants