Skip to content

Fix non-deterministic section ordering in exported context files#8

Draft
pstreef wants to merge 2 commits intomainfrom
pstreef/deterministic-export-order
Draft

Fix non-deterministic section ordering in exported context files#8
pstreef wants to merge 2 commits intomainfrom
pstreef/deterministic-export-order

Conversation

@pstreef
Copy link

@pstreef pstreef commented Feb 28, 2026

Problem

Prethink output is non-deterministic between CLI runs. ExportContext.aggregateMatchingTables() iterates ExecutionContext.DATA_TABLES which is a ConcurrentHashMap<DataTable<?>, List<?>>. The iteration order of that map determines which data tables appear first in tablesByFqn (a LinkedHashMap), which determines the section order in the generated markdown and CSV file ordering.

Non-determinism chain:

  1. ExecutionContext.DATA_TABLES is a ConcurrentHashMap (see DataTable.insertRow())
  2. aggregateMatchingTables() iterates it — non-deterministic order
  3. Results go into LinkedHashMap tablesByFqn — preserves insertion order, but insertion order varies per run
  4. generateMarkdown() iterates tablesByFqn — sections appear in whatever order the ConcurrentHashMap gave them

Solution

Collect into temporary HashMaps first, then insert into the output LinkedHashMaps in the order specified by the dataTables configuration list. This ensures the output order matches the recipe configuration regardless of ConcurrentHashMap iteration order.

aggregateMatchingTables() iterates ExecutionContext.DATA_TABLES which
is a ConcurrentHashMap with non-deterministic iteration order. The
results feed into a LinkedHashMap that preserves whatever arbitrary
order the ConcurrentHashMap gave, causing context markdown sections
and CSV file ordering to vary between runs on the same codebase.

Collect into temporary maps first, then insert into the output maps
in the order specified by the dataTables configuration list.
The test feeds a LinkedHashMap with entries in reverse order
(TestMapping first, CodingConventions second) compared to the
dataTables config (CodingConventions first, TestMapping second).

Without the fix, the output follows the input iteration order.
With the fix, the output follows the configured dataTables order.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

1 participant