# Preflight Algorithmic Audit Reviewed baseline: `b2877c1`. Scope: the seven stages of [the implementation plan](implementation.md), with attention to algorithmic cost in their shared collaborators. The findings below are optimization opportunities for consideration, not demonstrated operational failures or release blockers. No refactors were implemented in this audit. The selected prepare/resume machinery, pure transcript-work projection, enabled-versus-projected target selection, and independent native checks remain the accepted design. This audit does not propose changing those decisions or adding caches, parallelism, limits, or new readiness requirements. ## Stage Coverage | Stage | Assessment | | --- | --- | | 1: Native checks | Requirement matching uses map lookups and linear scans. Notarius lane membership is linear in available plus required lanes. Finding 1 concerns inherited error aggregation in the shared checker. | | 2: Shared downstream execution | The tool sequence has fixed length; findings accumulate through amortized slice appends. Target construction is linear in selected bindings and variables, apart from existing deterministic dependency ordering. | | 3: Locked lifecycle | Stage count is fixed. Finding 2 concerns output-inventory construction inherited by the new standalone caller. | | 4: Standalone orchestration | Check-group processing and report construction are linear in findings. Preparation and pure projection were expressly selected and are not reconsidered. Finding 2 applies to its discarded lifecycle result. | | 5: CLI and reporting | No additional suboptimal algorithm identified in the new command wiring. Native library formatting remains upstream-owned. | | 6: Recovery and cleanup coverage | No additional algorithmic issue identified in the changed test code; existing lifecycle safety remains applicable to both proposed optimizations. | | 7: Documentation and verification | No additional algorithmic issue identified. Documentation should change only if a later refactor changes observable behavior. | ## 1. Flatten Notarius Failure Aggregation Status: Complete. Priority: P3. Completion: `CheckNotarius` now collects adapter and failed-check errors and joins them once. Diagnostic rendering is linear in total message length rather than repeatedly copying nested prefixes. Added coverage for multiple failed checks, preserved warning severity, and typed adapter causes. Verified with Go 1.27.4: `go test ./internal/preflight ./internal/adapters/notarius` and `git diff --check`. Evidence: [CheckNotarius](../../internal/preflight/downstream.go), especially lines 55–62 and 87–89. The loop assigns `err = errors.Join(err, nextFailure)` for each failed native check. This code predates the sprint; Stage 1 retains it and Stage 2 uses it through the shared downstream path. The planning documents specify preservation of findings and error causes, not this aggregation algorithm. Let `F` be the number of failed checks. Building the nested error tree takes linear work, but rendering it is different: Go 1.27.4's `joinError.Error` recursively renders each child and copies its text into the parent's buffer. A left-nested tree therefore copies prefixes of lengths proportional to `1, 2, ..., F`. For bounded-length failure messages, one rendering takes `Theta(F²)` time and cumulative allocated bytes, with `Theta(F)` nesting depth. The checker renders this error both for its aggregate finding and its wrapped return error. No benchmark is needed to establish the copying pattern. Potential refactor: append the original adapter error and individual failed-check errors to an error slice, then call `errors.Join(failures...)` once before the existing completeness/lane decision. A flat join renders in time linear in the total diagnostic text, with `O(F)` auxiliary references. Keep warning severity, native failure precedence, findings, and `errors.Is`/`errors.As` causes intact. The Scriptorium checker already uses slice accumulation followed by one join. Verification for a future fix: use a synthetic assessment containing multiple failed checks plus an adapter error. Assert all failures and the adapter cause survive, and existing warning-only and incomplete-assessment behavior is unchanged. Avoid timing thresholds or a new diagnostic-count limit. ## 2. Avoid Building A Discarded Run-Artifact Inventory Status: Open for consideration. Priority: P3. Evidence: [standalone lifecycle caller](../../internal/app/session_readiness.go), lines 79–90, and [finalization](../../internal/app/runner_finalization.go), lines 45–107. `finalizeInvocation` always constructs `AvailableArtifacts` through `durableRunArtifacts`. Standalone preflight uses the returned summary only to distinguish a nil result on an early failure; it never consumes the inventory. Let `A` be the number of stored analyze-artifact records, `S` the number of speaker records, and `K` the number of aggregate output records visited. Ignoring bounded stage count and identifier lengths, this unused work costs `O(A log A + S log S + K)` time and `O(A + S + K)` additional space for sorting, deduplication, and materialized report entries. It runs even when preflight checks only a small current configuration and the ledger contains many older artifact records. It also extends the time spent holding the session lock. Potential refactor: separate shared terminal persistence/resource handling from run-specific inventory assembly, allowing standalone preflight to receive a lightweight lifecycle result. Keep normal `run` summaries and partial durable artifact evidence unchanged. Removing this unused projection eliminates that additional traversal/sort/allocation; it does not make the whole command constant-time, because manifest I/O, selected preparation, and readiness checks still have their own costs. Stage 3 deliberately selects shared locking, finalization, and preservation of normal runner partial summaries. This opportunity preserves those decisions; the plan does not require standalone readiness to build an inventory absent from its report. Prefer a small separation of responsibilities over a generic reporting or lifecycle-hook framework. Verification for a future fix: retain ordinary run/partial-summary tests and standalone failure, cancellation, and lock-release tests. Confirm standalone reports are unchanged and terminal persistence still completes while locked.