Record algorithmic review findings

This commit is contained in:
2026-11-27 18:17:25 +00:00
parent 54ba768a28
commit d4036eea47

View File

@@ -7,8 +7,9 @@ against all 19 stages of [implementation.md](implementation.md) and the
`ecad94a5a53f87847b1fd81fd042eee9c8d3ea08`. Native compatibility was checked
with Audita v4.2.0 at `973b24e46618dc785751fa7c803e7c16c900353f`.
This document records required remediations. Implementation code and upstream
repositories were left unchanged during review.
This document records required remediations and separately identified algorithmic
improvements for consideration. Implementation code and upstream repositories
were left unchanged during review.
## 1. Reject empty paths for explicitly selected catalog entries
@@ -122,6 +123,86 @@ inline vocabulary and preparation. Include the null case in the controlled
native Audita compatibility check. Retain rejection of genuinely invalid alias
types and upstream-invalid null scalar fields. Default tests remain offline.
## 3. Index prepared-input records for resume comparison
Priority: P3
Status: Pending consideration
Affected stage: 8. This is an inherited algorithm extended by the glossary
cutover, rather than a newly introduced regression.
### Finding and complexity
`internal/stage/prepare_resume.go:254-269` scans every manifest input for each
expected input. The `matched` map prevents a record from being consumed twice,
but the inner loop still visits every record, including after finding a match.
For E expected inputs and M manifest inputs, successful matching performs
Theta(E*M) record visits. With N audio files, both lists grow with N, giving
Theta(N^2) matching work, separately from file hashing and vocabulary resolution.
The existing count checks do not eliminate this successful-case cost.
The sprint added four glossary kinds and normalized configuration-snapshot
comparison to this path. Neither the roadmap nor Stage 8 selects the nested
matching algorithm as a requirement. Ordinary D&D sessions may have few audio
files; this is a scalability and simplification opportunity, not evidence of a
measured production bottleneck or incorrect resume decisions.
### Potential refactoring
Build a single index using the existing matching identity: kind, with the
destination basename for audio records. Retain record multiplicity explicitly
and compare checksums, normalized snapshots, or remote object identity within
the appropriate entry. Managed-kind cardinality and ambiguity checks can remain
linear. The intended result is expected O(E+M) matching time with O(E+M)
auxiliary space; the current matcher already allocates O(E) consumed-record state.
String normalization and file-content processing remain additional costs.
Preserve rejection of missing, extra managed, ambiguous, and reused records;
a map assignment that silently overwrites duplicates would change behavior.
Preserve case-insensitive checksum comparison and the existing remote-audio
identity fields. Retain representative local/remote and snapshot cases, and add
synthetic reordered/duplicate-record coverage when making this refactor. A
benchmark or a real audio workload is unnecessary to establish the complexity.
## 4. Validate report bounds without buffering discarded contents
Priority: P3
Status: Pending consideration
Affected stages: 10 and 11, principally the new polish-stage report check.
### Finding and complexity
`internal/adapters/audita/subprocess.go` reads each discovered report with
`fileops.ReadFileBounded` and discards the returned bytes. The new check at
`internal/stage/polish.go:115-117` repeats that operation. The helper uses
`io.ReadAll(io.LimitReader(...))` in `internal/fileops/read_bounded.go`.
Neither caller parses or retains report content; they retain only its path.
For a report of B bytes within the existing limit L, each validation performs
O(B) I/O and O(B) temporary allocation. An oversized report can allocate O(L)
before rejection. The normal subprocess path performs two such reads. These
are fixed linear passes, not quadratic work, and the existing 16 MiB per-report
cap already bounds their memory exposure.
### Potential refactoring
Use a small shared bounded-file validation helper that retains the existing
regular/readable-file and path checks while streaming at most L+1 bytes to
`io.Discard`. This preserves read-error detection and limit enforcement with
O(B) time and O(1) auxiliary memory relative to report length. It can replace
the discarded-content reads at both boundaries without weakening stage-side
validation of another runner implementation. A broader file-operations
abstraction or cross-boundary validation cache is unnecessary.
Keep the limit in its existing central constant, preserve optional-report and
nonzero-exit behavior, and retain errors identifying the responsible Audita
boundary and limit. Test below/at/above-limit files and existing invalid-file
cases when implementing the helper. Content-consuming bounded reads elsewhere
are outside this finding.
## Stage-by-stage review coverage
| Stage | Evidence examined and result |
@@ -160,3 +241,39 @@ types and upstream-invalid null scalar fields. Default tests remain offline.
Automatic provider checks, synthetic-transcript feasibility errors, all-four-role
runtime authority, and the clean source/format cutover are explicit user-selected
scope. They are not findings or reasons to restore the earlier behavior.
## Algorithmic review coverage
The follow-up review used Narratio `54ba768a28eee317985b0be4bb284d5f77ff00f1`
and the same implementation and catalog revisions described above. Findings 1
and 2 remain pending; Findings 3 and 4 are lower-priority refactoring opportunities.
No additional ordinary correctness bug was found in this pass.
| Stage | Algorithmic assessment |
| --- | --- |
| 1 | Reviewed the catalog schema and offline check script. Neither adds a pairwise entry comparison or runtime catalog traversal. |
| 2 | Schema compilation uses `sync.Once`; collection ID uniqueness uses a hash set. JSON conversion and validation traverse the data. |
| 3 | Catalog path adaptation traverses sorted IDs; selection is a map lookup. No eager vocabulary-file loading was added. |
| 4 | Campaign/session selection visits a fixed set of fields. Native composition remains ConfigKit-owned. |
| 5 | Party sorting is explicitly prescribed. Vocabulary uses native composition, hash-based duplicate checks, and deterministic serialization; those selected algorithms were retained. |
| 6 | Four descriptors are fixed-size. Each prepared-input lookup scans manifest inputs and streams its file digest; four lookups remain linear in manifest size and total bytes. |
| 7 | Preparation materializes four documents using existing change detection and deterministic record ordering. No growing cross-product of glossary entries is added. |
| 8 | Finding 3 identifies quadratic record matching in the extended resume path. Effective vocabulary comparison and content hashing are explicit design choices. |
| 9 | Publication adds a fixed four verified input kinds; archival streams file contents. Restore uses a fixed-size descriptor lookup per object. |
| 10 | Argument construction visits the supplied files; report discovery checks three fixed names. Finding 4 covers discarded-content buffering. |
| 11 | Glossary evidence covers four files and a fixed-size digest input. Finding 4 covers the additional whole-report read. |
| 12 | Synthetic transcript size is fixed. Preflight uses one native invocation and invocation-scoped temporary files. |
| 13 | Sequential downstream checks are explicitly selected. Requirement processing uses map lookups rather than comparing every requirement with every binding. |
| 14 | The runner performs one post-preparation reprojection over the fixed canonical stage set; the new hook does not repeat it before every stage. |
| 15 | Validation loads one manifest and resolves four prepared documents; it does not resolve live vocabulary or run preparation. |
| 16 | Consumer binding changes use existing resolvers. Examples and source-ID rejection add no new growing algorithm. |
| 17 | Compatibility checks use explicit local inputs and controlled providers. Catalog fixture copying visits each file once. |
| 18 | Documentation changes add no runtime algorithm. |
| 19 | Reviewed the assembled paths and focused recovery tests. No additional asymptotic issue was identified in the newly added orchestration. |
Focused glossary, configuration, artifact-policy, artifacts, stage, preflight,
Audita-adapter, and application tests passed with Go 1.27.4. Complexity findings
come from the implemented loops and allocation paths; no real-world performance
measurements or paid provider calls were used. The proposed refactorings retain
native ConfigKit semantics, required ordering, content verification, and the
selected preflight policy.