From 18c9d0d65b97d3a368b012b794ed1dd3fd057719 Mon Sep 17 00:00:00 2001 From: GAJ3T Date: Sun, 1 Nov 2026 06:05:02 -0500 Subject: [PATCH] WIP 13 --- README.md | 21 +- docs/README.md | 12 +- docs/architecture.md | 39 +- docs/build-and-test.md | 36 +- docs/calibration-authoring.md | 14 + docs/calibration-comparison.md | 112 +++ docs/calibration-rehearsal.md | 16 + docs/local-runbook-generation.md | 6 +- docs/runbooks-v1.md | 8 +- prompts/ROADMAP.md | 48 +- prompts/STATE.md | 100 ++- prompts/Step-13-Report.md | 475 +++++++++++++ prompts/Step-13A.md | 188 +++++ .../CalibrationAuthoringService.cs | 4 +- .../CalibrationComparisonModels.cs | 97 +++ .../CalibrationComparisonSession.cs | 378 ++++++++++ .../CalibrationComparisonSource.cs | 45 ++ .../CalibrationRehearsalSession.cs | 70 ++ .../Runbooks/RunbookPreviewService.cs | 21 + .../DynamicsConfigurationService.cs | 13 + .../SyntheticCalibrationProjection.cs | 50 ++ .../Features/Foundation/AddAutoForgeUi.cs | 1 + .../CalibrationComparisonPanel.razor | 101 +++ .../Calibration/CalibrationPage.razor | 39 +- .../CalibrationRehearsalPanel.razor | 6 + .../CalibrationAuthoringUiTests.cs | 35 + .../CalibrationComparisonTests.cs | 652 ++++++++++++++++++ .../CalibrationRehearsalTests.cs | 5 +- .../DynamicsEngineTests.cs | 3 + 29 files changed, 2529 insertions(+), 66 deletions(-) create mode 100644 docs/calibration-comparison.md create mode 100644 prompts/Step-13-Report.md create mode 100644 prompts/Step-13A.md create mode 100644 src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonModels.cs create mode 100644 src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs create mode 100644 src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSource.cs create mode 100644 src/AutoForge.Core/Calibration/SyntheticCalibrationProjection.cs create mode 100644 src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationComparisonPanel.razor create mode 100644 src/tests/AutoForge.IntegrationTests/CalibrationComparisonTests.cs diff --git a/README.md b/README.md index d8fc68b..c43d322 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,7 @@ execution. See [runbook schema/preview contracts](docs/runbooks-v1.md) and the [Step-11 worker report](prompts/Step-11-Report.md). Step-11 was accepted on 2026-10-31. -### Local generation qualified; manual acceptance pending +### Accepted local generation milestone Step-12 adds an explicit local-only Ollama adapter and matched synthetic knowledge/context in `/runbooks`. Model-generated content goes unchanged through @@ -149,8 +149,7 @@ output produced exactly +600 rpm/-10 N*m with every other source value unchanged Real pending conflicts, Cancel/drain, unmount/fresh-owner, manual/stale/units and source-isolation checks passed. A narrow Hybrid event/transport-dispatch fix keeps pending controls responsive; all 596 tests pass (594 retained plus two -corrective cases). Worker qualification is not orchestrator acceptance and does -not authorize Step-13. See +corrective cases). Step-12/12A was accepted on 2026-10-31. See [local generation contracts](docs/local-runbook-generation.md) and the [original Step-12 worker report](prompts/Step-12-Report.md), preserved alongside the [Step-12 completion report](prompts/Step-12-Completion-Report.md). @@ -158,9 +157,23 @@ The [previous Step-12A report](prompts/Step-12A-Report.md) remains historical; current evidence is in the [Step-12A execution report](prompts/Step-12A-Execution-Report.md). +### Verified synthetic comparison; pending manual review + +Step-13 adds explicit current-preview handoff into the existing pinned draft, +separate Proposed Save and owning verified rehearsal-image admission. Evaluation +projects only warm-idle/torque cells into a NEW immutable configuration and +compares two fresh actual Simulation/v2 runs. Fixed acquisition is every five +model steps plus completion, bounded at 3900 steps per run inside unchanged +archive-v1 limits. Canonical measured metrics, proof/config/run/capture lineage, +explicit per-capture save/readback, real RecordedReplay and cancellable/stale +ownership are separate from proposal and programming evidence. +See [comparison usage](docs/calibration-comparison.md) and +[Step-13 worker evidence](prompts/Step-13-Report.md). Step-13 requires manual +acceptance; Step-14 remains blocked. + ### Planned -Durable/full-device backups, dynamics application, broader AI/retrieval/executors and real hardware operations +Durable/full-device backups, broader model application, AI/retrieval/executors and real hardware operations require later approved assignments. Saved recordings are local file archives, not crash-durable live streaming. Replay does not resimulate or interpolate. Dynamics estimates are synthetic, diff --git a/docs/README.md b/docs/README.md index 45757fc..8b55dac 100644 --- a/docs/README.md +++ b/docs/README.md @@ -25,8 +25,10 @@ historical: Completion and Step-12A verify the now operator-installed exact local model/digest. Corrected owner-directed Step-12A execution established fresh cloud-disabled:true startup proof, exact original native model intent and real pending/manual/stale/units/source checks. All 596 tests pass (594 retained plus -two corrective cases). Worker completion is pending manual acceptance; Step-13 -and execution/programming remain unimplemented/unauthorized. +two corrective cases). Step-12/12A is accepted. Step-13 adds an explicit +verified-image-to-NEW-configuration equivalent synthetic comparison, bounded +real captures, canonical metrics/lineage and existing save/readback/replay. +Step-13 awaits manual review; Step-14 and physical execution remain blocked. - [Architecture](architecture.md): actual nine-project graph, responsibilities, route ownership and provider/reference boundaries. @@ -53,6 +55,9 @@ and execution/programming remain unimplemented/unauthorized. - [Verified simulated programming rehearsal](calibration-rehearsal.md): typed isolated target, independently verified supported-image backup, immutable single-use approval, exact readback/recovery and bounded volatile audit. +- [Verified synthetic calibration comparison](calibration-comparison.md): pinned + preview handoff, trusted owning proof, exact immutable projection, equivalent + real fixed-cadence runs, metrics, partial saves and stale/cancel/close behavior. - [Manual runbook schema v1 and read-only preview](runbooks-v1.md): exact allowlisted fields/limits, current authoring source/head/template validation, canonical immutable diff, source staleness and no execution authority. @@ -100,5 +105,6 @@ historical/worker reports for their own execution evidence: [Step-12 worker report](../prompts/Step-12-Report.md) and [Step-12 completion report](../prompts/Step-12-Completion-Report.md) and [Step-12A worker report](../prompts/Step-12A-Report.md), preserved historically, and -[Step-12A execution report](../prompts/Step-12A-Execution-Report.md). +[Step-12A execution report](../prompts/Step-12A-Execution-Report.md) and +[Step-13 worker report](../prompts/Step-13-Report.md). Handoffs are manual; acceptance remains the orchestrator's responsibility. diff --git a/docs/architecture.md b/docs/architecture.md index 0210c12..02e12ee 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -21,7 +21,9 @@ Completion/Step-12A verify the installed exact local model. Real-model verificat now passes under corrected owner-directed Step-12A setup with a verified current cloud-disabled:true runtime and actual original native intent/pending evidence. A narrow event/owner-execution dispatch fix keeps HTTP work from blocking Hybrid -controls. Worker completion is pending manual acceptance; Step-13 is not started. +controls. Step-12/12A is accepted. Step-13 adds trusted owning rehearsal proof, +pure supported NEW-configuration projection and bounded equivalent real +Simulation/v2 comparison/capture/save/replay. Step-13 awaits manual review. ```mermaid flowchart TD @@ -98,7 +100,7 @@ assembly, not the native Client assembly. | `/diagnostics` | `Features/Vehicles/Diagnostics/DiagnosticsPage.razor` | Explicit saved profile/revision/provider/kind selection, static-v1 session lifecycle, separate expected/probed/identified counts, retained partial probe services, identity-gated DTC/static signal operations, clear acknowledgement plus fresh-empty-readback verification, raw/decoded trace, shared live dashboard and session recording/archive inspection | | `/simulation` | `Features/Vehicles/Simulation/SimulationPage.razor` | Explicit profile/config/provider selection, JSON import/export, stopped-by-default dynamics session, lifecycle/step/pacing/scenario controls, source/unit/quality/model-time/config/run-aware telemetry over diagnostic protocol v2, and the same dashboard and recording/archive controls | | `/telemetry-replay` | `Features/Vehicles/Telemetry/Replay/TelemetryReplayPage.razor` | Strict saved capture selection/load, distinct RecordedReplay instance and immutable original metadata, exact time/ordinal prefix navigation, one cancellable clock, partial acknowledgement and shared recorded numeric/bar gauges | -| `/calibration` | `Features/Vehicles/Calibration/CalibrationPage.razor` | Explicit configuration-derived synthetic baseline, immutable Proposed history, complete scalar/table validation, exact head/source tokens and diffs, pinned torque-unit drafts; no application or programming | +| `/calibration` | `Features/Vehicles/Calibration/CalibrationPage.razor` | Explicit baseline/Proposed history and pinned preview handoff; separate owning rehearsal backup/approval/Apply/readback; explicit verified-image NEW-config comparison and canonical report/capture saves | | `/runbooks` | `Features/Vehicles/Runbooks/RunbookPage.razor` | Manual schema-v1 input/template, explicit validation and immutable canonical preview/diff with current/Stale source status; no Save, approval, target/model/command access | | Any unmatched route | `Features/Foundation/NotFoundPage.razor` via UI `Routes.razor` | Explicit not-found page and supported navigation | @@ -236,7 +238,9 @@ before reading the source gate; no inverse lock ordering is introduced. The UI separately owns manual input/errors and last-successful preview labels, and source/unit notifications only rerender. It never validates on render. The slice has no diagnostic/rehearsal/transport/model/archive/network dependency -or Save/approval/Execute control. Optional generated text uses this same strict +or automatic Save/approval/Execute control. Its explicit handoff validates the +expected current preview/workspace/head and returns copied canonical candidate +data to the existing draft; the UI must still explicitly Save. Optional generated text uses this same strict boundary; the manual validator itself has no inference/retrieval dependency. See [runbook schema/preview contracts](runbooks-v1.md). @@ -258,8 +262,9 @@ prior-preview under the same guarded publication. Generation holds its owner gate only across synchronous admission, never cleanup awaits. New manual previews cannot be overwritten. Cancel/unmount drains only page requests, never shared Ollama; source/authoring/rehearsal/recording/replay writes are unreachable. -See [local generation contracts](local-runbook-generation.md). Real generation -remains unverified until operator prerequisites are satisfied. +See [local generation contracts](local-runbook-generation.md). The existing +local model was qualified and Step-12/12A accepted; no runtime setup is part of +comparison. The authoring slice has only configuration/profile read dependencies. Core owns defensively copied applicability, definition and value contracts for the @@ -281,6 +286,30 @@ model-control, archive-save, approval or programming port, and cannot apply its values to a configuration or active source. See [calibration authoring](calibration-authoring.md) for template bounds and limits. +The comparison factory creates one explicitly opened page-owned +`CalibrationComparisonSession` bound to the actual rehearsal owner. Admission +performs fresh complete readonly target verification under current +authoring/rehearsal guards; copied proof has no public constructor. +Core's pure `SyntheticCalibrationProjection` copies all unchanged model, +initial/scenario and reference fields and uses existing configuration validation/ +fingerprints/catalogue registration. Application accesses real dynamics only +through Diagnostics control/v2 signals, never a Simulation implementation. +Two sequential fresh sessions each own an independent accepted capture; ordinary +recording workspaces are untouched. Acquisition advances at most 3900 steps, +reads engine AND transmission every five steps and at completion, and validates +exact Good canonical run/config/module/step/time signals. Source is rechecked +between runs and freshly reread before guarded current-result publication. + +One retained result plus bounded partial run/save evidence is inspectable. +New evaluation or Close retires earlier current status; source/head/target +supersession also computes Stale without rebase. Hybrid events release before +admitted blocking work. Cancel/Close drains outside required gates; resources +dispose even if capture cleanup fails. Explicit saves use existing no-overwrite +archive storage, exact serialized readback and per-ID outcomes. A failed save +may have published: retry checks the existing catalogue and verifies read-only +instead of colliding or overwriting. No compensating file deletion occurs. +See [comparison contracts](calibration-comparison.md). + The separate rehearsal factory creates one page-owned Application session. Client registers `autoforge.simulation.calibration-rehearsal` as an explicit `ISimulatedCalibrationTargetFactory`, not a diagnostic transport fallback. diff --git a/docs/build-and-test.md b/docs/build-and-test.md index 0107d6e..5df8524 100644 --- a/docs/build-and-test.md +++ b/docs/build-and-test.md @@ -47,6 +47,7 @@ dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.cs dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-restore --filter "FullyQualifiedName~CalibrationRehearsalReopenTests|FullyQualifiedName~CalibrationRehearsalTests|FullyQualifiedName~RenderedRehearsal|FullyQualifiedName~RehearsalIsolation" dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-restore --filter "FullyQualifiedName~RunbookPreviewTests|FullyQualifiedName~RunbookUi_|FullyQualifiedName~RunbookIsolation_" dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-restore --filter "FullyQualifiedName~LocalRunbookGenerationTests|FullyQualifiedName~LocalInferenceBoundsTests|FullyQualifiedName~LocalGenerationReviewTests|FullyQualifiedName~LocalGenerationUi_|FullyQualifiedName~LocalGenerationIsolation_|FullyQualifiedName~RunbookPreviewTests" +dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-restore --filter "FullyQualifiedName~Comparison_|FullyQualifiedName~PreviewHandoff_|FullyQualifiedName~TorqueDraft_" ``` ## Step-12A real local-model/native qualification @@ -70,7 +71,40 @@ Final sequential Release build: all nine projects, exit 0, 0 warnings/errors. Full tests: exit 0, 179 unit + 417 integration = 596 passes, zero failed/skipped; all 594 prior regressions retained plus two corrective cases. See [Step-12A execution evidence](../prompts/Step-12A-Execution-Report.md). -Worker completion is pending manual acceptance; Step-13 is not authorized. +Step-12/12A is accepted. + +## Step-13 verified comparison verification + +The focused comparison/pinned-draft selector passes 50 cases, including actual +owning proof, whole-config equality, exact nonuniform/tiny canonical handoff, +reconciled Apply, malformed/unavailable fresh target reads, real wire signal +faults, measured 3900/3901 and fewer-than-ten-warm boundaries, held actual +transport/final-read/save Cancel/Close/source supersession, partial/uncertain +publication retry and real file/readback/replay corruption. +The original full-scenario regressions still independently check all 3000 cruise +model steps; production records 600 sampled cruise signal sets, not continuous +extrema. Native demonstration includes the existing real local model's original +goal/output, separate draft/Save/backup/approval/Apply/full readback, equivalent +3361-step real pair, explicit two-capture readback and existing RecordedReplay +raw trace. Controlled faults/barriers are not relabeled as native physics/model +proof. See [comparison usage](calibration-comparison.md) and +[Step-13 execution evidence](../prompts/Step-13-Report.md). +Step-13 awaits manual review; no Step-14 or runtime installation/setup is implied. +Final sequential Release build: nine projects, exit 0, zero warnings/errors. +Full tests: 179 unit + 466 integration = 645 passes, zero failed/skipped, +retaining all 596 original cases and adding 49 comparison/handoff cases. +The unchanged warm-baseline regression now independently asserts exactly +3361 samples/336.1 s and every one of the 3000 cruise model steps within +45 MPH +/-1 MPH, rather than using the production sampled range as a proxy. + +One intermediate full run failed in the unmodified +`ScenarioOwnerCannotBeReplacedAndResetOrCloseSuppressesOldRunPublication` +cancellation regression; the isolated seven-case owner selector and subsequent +full runs passed. This intermittent result is recorded, not concealed or fixed +outside the assignment. Rapid native diagnostic/simulation navigation also +exposed the unchanged recording panel's pending-refresh Busy initialization +error. Task-owned Client recovery and sequential exact-heading/idle-archive-owner +checks passed for all six existing routes. No recording-navigation change was made. ## Historical Step-12 approval-blocked verification diff --git a/docs/calibration-authoring.md b/docs/calibration-authoring.md index 681dba0..68aef3a 100644 --- a/docs/calibration-authoring.md +++ b/docs/calibration-authoring.md @@ -98,6 +98,20 @@ to Imperial leaves that draft in N*m. Cancelling captures a new Imperial draft in lbf*ft. An untouched subsequent save preserves exact source values and does not create a proposal merely because displayed units changed. +## Explicit preview handoff + +After explicit manual/local validation in `/runbooks`, return to `/calibration` +and choose **Copy current preview into this pinned draft**. Admission validates +that exact current preview, workspace/head, selected configuration and complete +canonical candidate. It preserves this draft's pinned units even if global +preferences changed. Unedited copied fields retain exact source doubles. +Invalid/stale/superseded/different-source input leaves the draft and saved +history unchanged with a visible error. Handoff never Saves, opens/reads/writes +a target or rebases. Choose Save separately to create the Proposed version. +Only the separate [verified comparison](calibration-comparison.md), after actual +rehearsal backup/approval/Apply/full readback, can explicitly project values to +a NEW immutable model configuration. Authoring Save itself is unchanged. + ## Structured canonical diffs Select two explicit saved versions from the same workspace/applicability and diff --git a/docs/calibration-comparison.md b/docs/calibration-comparison.md new file mode 100644 index 0000000..3d37896 --- /dev/null +++ b/docs/calibration-comparison.md @@ -0,0 +1,112 @@ +# Verified synthetic calibration comparison + +Step-13 is implemented pending manual review. This is a narrow synthetic +comparison, not manufacturer prediction, safe tuning advice, firmware proof, +physical authority or a general AI/runbook executor. + +## Explicit connected workflow + +1. Create/select the original warm configuration and authoring Baseline. + Optionally record an actual original warm run through ordinary diagnostics. +2. In `/runbooks`, explicitly validate manual input or original content from + the qualified local model. The decisive goal is warm idle 1250 rpm and + 2000-rpm torque 350 N*m; keep every other value unchanged. +3. Return to `/calibration`; explicitly copy the exact current preview into + this existing pinned draft. Copy validates workspace/head/configuration and + keeps canonical doubles and the draft's prior units. Save separately. +4. Select the isolated rehearsal provider, Open, prepare original two-read + backup, prepare exact current Proposed Apply, explicitly confirm/Approve, + Dispatch and inspect independent complete readback. +5. Open comparison controls explicitly, select THIS owning verified Apply and + `autoforge.simulation.dynamics`, then Evaluate. This does not program a target, + overwrite a configuration/run or promote Proposed to Tested/Approved. +6. Inspect/copy the canonical readonly report. Save both captures explicitly. + Only persisted/readback-verified IDs qualify as saved evidence. Open each + exact ID in existing `/telemetry-replay`, inspect prefix/end/raw bytes and + immutable original provenance. Replay is archive playback, not physics. + +No action is implied by rendering, navigation, AI validation, unit changes or +notifications. Manual validation remains available without a local model. + +## Trusted proof and exact immutable mapping + +An actual idle Ready owning rehearsal must have an original verified backup +and current verified Apply (including genuine post-reconciliation verification). +Admission freshly reads the full target and matches exact instance/source/ +revision/operation/approval/backup/workspace/head/definition, candidate and every +changed/unchanged value. Original backup must equal original supported config +values. Caller success records cannot construct the internal proof capability. +RecoveryRequired, divergent/unavailable, wrong/closed/stale evidence rejects. + +The pure supported mapping changes only Model.WarmIdleTargetRpm and torque +cells. Axis, every other model field, initial conditions, five-phase scenario +and profile/module/model/calibration references remain unchanged. Existing +constructors/validation/fingerprints/catalogue register the NEW immutable config; +original config and authoring binding/history are not rebased. No-change mapping +truthfully retains the same content fingerprint, not a fabricated improvement. + +Proof is historical at its readback time, not perpetual external target +attestation. Owning source guards run between scenarios, and another fresh full +readback plus current-head/lifetime checks precede atomic result publication. + +## Equivalent acquisition and metrics + +Two fresh sequential real Simulation/v2 diagnostic sessions use equivalent +initial state/scenario and distinct run/capture IDs. Physics advances only through +real StepAsync(1). Engine AND transmission signals are read every five completed +steps plus completion, without duplicate final reads. At 0.1 s/step this is +0.5 virtual-second cadence. Maximum is 3900 steps/run and 1562 scheduled +observations; a model needing step 3901 rejects as incomplete. Existing archive +bounds remain 4096 observations, 16 MiB observations, 1 MiB header, 32 MiB archive. +Independent accepted capture APIs leave ordinary unsaved recordings untouched. + +Metrics use exact Good finite canonical module/run/config/step/time values. +Missing, non-Good, wrong-unit/context/nonfinite replies cannot become zero. +At least TEN actual warm samples are required; last ten must each be within +1 rpm of their target and 0.001 m/s of stationary. A shorter window is incomplete. +Report includes warm/error/stationary, model/phase step counts, acceleration/ +braking time, sampled cruise count/range, fuel/distance deltas from initial state, +total virtual time and final actual transmission.shift-count. + +The decisive original/derived pair both completes 3361 steps/336.1 s with phase +counts 100/103/3000/58/100, acceleration 10.3 s and braking 5.8 s. The 3000 cruise +model steps remain independently tested within 45 MPH +/-1 MPH; production +reports 600 sampled cruise signal sets at 20.1168 m/s. Last-ten idle errors are +below 1e-8 rpm with zero stationary speed for 650 and 1250 rpm. Measured fuel is +0.518385971679477 versus 0.5184897031483956 kg; distance 6193.507764002321 versus +6193.389583605566 m; final shift count 4 versus 4. No beneficial sign is promised. +Sampled range is not continuous extrema; final count is not an exact shift +timeline. Request/capture elapsed, diagnostic ticks and virtual model time are +different clocks. + +The readonly canonical report identifies proof/time/preparation/backup/mutation/ +approval/source/head/definition/model/module/profile, exact torque before/after, +original/derived fingerprints, providers/run/capture IDs, completion and metrics. +Display preferences never alter source/report/archive values. + +## Lifetime, partial evidence and save + +One admitted comparison/run/save owner rejects competing requests Busy before +starting another. Hybrid callbacks return promptly; admitted work runs off the +UI dispatcher. Cancel/Close/unmount drains owned diagnostics/capture resources +outside required gates, preventing late healthy publication. New evaluation/ +Close retires earlier current status; source/head/proof supersession labels it +Stale/as-of without rebase. One previous success and bounded latest partial run/ +save evidence remain inspectable, not an unbounded history. + +Failed/limited/cancelled physics never becomes a successful pair. A stopped +capture can represent complete coverage of a partial run; the run's separate +error/metrics/completion determine comparison success. This does not expand or +mislabel archive-v1 stop-reason semantics. + +Pair save is NOT atomic. Each capture uses existing no-overwrite publication and +exact serialized fresh readback. If one fails, successful files and unsaved +capture remain; UI identifies persisted/verified/error per ID. Retry skips +verified captures and only saves missing work. If a failed publication/cleanup +may have left a file, retry checks the catalogue and verifies it read-only. +Mismatched/corrupt readback remains an error; there is no overwrite, rollback +deletion, hidden new ID or fake all-saved state. + +See [authoring](calibration-authoring.md), [rehearsal](calibration-rehearsal.md), +[runbooks](runbooks-v1.md), [recording](telemetry-archive-v1.md), +[replay](telemetry-replay.md) and [build/test evidence](build-and-test.md). diff --git a/docs/calibration-rehearsal.md b/docs/calibration-rehearsal.md index e4398de..2176d37 100644 --- a/docs/calibration-rehearsal.md +++ b/docs/calibration-rehearsal.md @@ -130,6 +130,22 @@ retain inactive backup/approval evidence without granting new-target authority; disposed sessions cannot publish or reopen, whereas completed ordinary Close allows another explicit fresh instance. +## Explicit trusted comparison admission + +After actual verified Apply/full readback, choose **Open explicit verified-image +comparison controls**, select THIS owner's exact Apply operation and the +Simulation dynamics/v2 provider, then Evaluate explicitly. Owning admission +requires idle Ready state, original two-read backup, current Proposed head, +matching target instance/revision/candidate/all unchanged cells and a fresh +full target read. Public records claiming success are insufficient. +RecoveryRequired/divergent/unavailable/closed/stale evidence rejects. An actual +VerifiedAppliedAfterReconciliation may qualify; no resend or Restore is implied. + +Comparison is a separate [NEW-configuration projection](calibration-comparison.md), +not an alteration of rehearsal programming semantics or the original config. +Proof is historical as of its readback time, not perpetual external attestation. +Source/owner guards and a final fresh full read precede current publication. + ## Bounded volatile audit and units At most **32 dispatched mutation outcomes per target** are retained, including diff --git a/docs/local-runbook-generation.md b/docs/local-runbook-generation.md index 340813d..4b22f2e 100644 --- a/docs/local-runbook-generation.md +++ b/docs/local-runbook-generation.md @@ -171,5 +171,9 @@ The qualified native decisive goal was: The exact installed model, current cloud-disabled proof and original +600 rpm/-10 N*m validated diff satisfied that check in the actual Client. Controlled counters/barriers remain distinct from native output/pending evidence. -Worker completion is pending manual acceptance; Step-13 remains blocked; +Step-12/12A is accepted. The separate Step-13 +[verified comparison](calibration-comparison.md) explicitly consumes the current +preview through the existing draft, separate Save and actual rehearsal proof; +generation still cannot perform any of those actions automatically. +Step-13 awaits manual review; Step-14 remains blocked; broader research/retrieval/executors/persistence/physical work is unimplemented. diff --git a/docs/runbooks-v1.md b/docs/runbooks-v1.md index 440e4df..308d61f 100644 --- a/docs/runbooks-v1.md +++ b/docs/runbooks-v1.md @@ -154,6 +154,10 @@ content through this same strict validator, adding captured-source, expected-prior-preview and cancellation admission atomically at publication. Manual Validate behavior/limits remain unchanged. Step-12A actual local-model native verification passes with current cloud-disabled proof, exact original -output and pending lifecycle evidence; manual acceptance is pending. +output and pending lifecycle evidence; Step-12/12A is accepted. See the [execution report](../prompts/Step-12A-Execution-Report.md). -Executor integration remains unimplemented. +Step-13 adds a separate explicit [pinned draft handoff and verified comparison](calibration-comparison.md). +It checks the actual current preview/workspace/head and copies canonical values +only. Save, backup, approval, Apply, readback and evaluation remain separate +commands. Validation/rendering still grants no target or execution authority; +a general runbook executor remains unimplemented. diff --git a/prompts/ROADMAP.md b/prompts/ROADMAP.md index 0a12158..5edfb0f 100644 --- a/prompts/ROADMAP.md +++ b/prompts/ROADMAP.md @@ -3,11 +3,11 @@ ## Status and authority - Initialized: 2026-10-28. -- Current phase: local-only generation accepted; verified synthetic comparison/demonstration next. -- Active assignment: [Step-13](Step-13.md), ready for manual worker handoff. +- Current phase: Step-13 comparison reviewed; owner/drain tail requires focused correction. +- Active assignment: [Step-13A](Step-13A.md), ready for manual worker handoff. - Last accepted milestone: Step-12 with Step-12A, accepted 2026-10-31. - Last accepted correction/follow-up: Step-12A; all frozen gates closed. -- Outstanding follow-up: none; no Step-12B required. +- Outstanding follow-up: Step-13A / R-01 original C-07 owner/drain completion; no Step-12B. - Step-05 status: ACCEPTED after Step-05A/Step-05B, 2026-10-29. - Step-06 status: ACCEPTED after Step-06A, 2026-10-30. - Step-07 status: ACCEPTED after Step-07A/Step-07B, 2026-10-30. @@ -19,7 +19,8 @@ - Step-12A status: ACCEPTED 2026-10-31; worker reports 596 passes, clean Release and actual native exact-intent/lifecycle proof. - Step-12 completion prompt: superseded for execution by owner-requested Step-12A; preserve unchanged. - Owner runtime policy: local models only; no Ollama cloud or cloud fallback. -- Step-13 status: READY FOR MANUAL WORKER HANDOFF; trusted verified-image projection and equivalent synthetic runs. +- Step-13 status: NEEDS CORRECTION / C-07 and AC-03; native/645-case evidence reviewed. +- Step-13A status: READY FOR MANUAL WORKER HANDOFF; one ownership-tail correction only. - Step-14 status: read-only hardware; BLOCKED / NOT ASSIGNED until demonstration acceptance and hardware gates. - Current state and handoff: [STATE.md](STATE.md). - Authority: the master charter, repository/handoff amendment, and the owner's @@ -595,7 +596,8 @@ evidence; they must not silently discard required capabilities. | 11 | Deterministic simulation runbook validation and preview | 10 and corrections accepted | ACCEPTED 2026-10-31 | Strict schema/limits/allowlist/source-head/template, immutable preview/diff/staleness and no execution; reported 179 unit/368 integration passes | | 12 | Local AI and applicable knowledge integration | 11 accepted | ACCEPTED 2026-10-31 AFTER 12A | Actual local-only original model/native preview and exact intent/lifecycle; immutable validator/authority boundaries, reported 596 passes | | 12A | Local-only Ollama setup and real-model qualification | 12 returned incomplete; owner directive | ACCEPTED 2026-10-31 | Cloud-disabled true/current model proof, exact native +600/-10 output, pending conflicts/Cancel/unmount and reproduced responsive Hybrid dispatch fixes | -| 13 | Comparative evaluation and complete synthetic demonstration | 12 and 12A accepted | READY FOR MANUAL WORKER HANDOFF | Trusted verified-image projection to NEW immutable config; equivalent real full scenarios/captures/metrics/save/readback/replay and supported fault matrix, original source unchanged | +| 13 | Comparative evaluation and complete synthetic demonstration | 12 and 12A accepted | NEEDS CORRECTION AFTER REVIEW | Most proof/projection/native/run/archive/645-case evidence supported; C-07 owner/drain tail replacement remains | +| 13A | Retain comparison ownership through cleanup and drain completion | 13 review R-01 / C-07 | READY FOR MANUAL WORKER HANDOFF | Real pre-fix tail-cleanup/reentry/Close proof, owner release outside gates followed by atomic terminal owner/drain publication; supported comparison behavior preserved | | 14 | Verified OBDX Pro GT read-only integration | 13 plus documented device research/access | BLOCKED / NOT ASSIGNED; HARDWARE GATE | Source-backed device support matrix, explicit physical target, real read-only identification/diagnostics/telemetry recording where supported; no write path | | 15 | Advanced dashboards, analysis, and comparisons | 13; 14 for real captures | PLANNED | Persist configurable layouts/thresholds, linked charts, larger-capture analysis, raw-data inspection and multi-run/multi-vehicle comparisons with lineage/uncertainty | | 16 | Broader diagnostic protocols and hardware adapters | 14 | PLANNED | Conformance-backed additional CAN/ISO-TP/UDS/KWP2000/J2534 or legacy adapters where verified; same application ports and capability restrictions | @@ -1116,15 +1118,22 @@ history, not repeated redesign without evidence. - Prepared [Step-13](Step-13.md) for explicit verified-image-to-new-config comparison, equivalent real runs/archives/report and full synthetic demonstration. No completed renumbering or automatic handoff. +- Reviewed Step-13 report/source/tests/graph/docs and actual native evidence. + Worker reports 179 unit + 466 integration = 645 passes, clean nine-project + Release and real model -> verified target -> exact projected equivalent runs/ + captures/save/readback/replay. Most frozen scope supported. +- Found one original C-07/AC-03 lifetime gap: comparison publishes no owner/ + IsBusy=false before owner Release and drain completion. New admission can + replace `_drain` while cleanup remains pending. Prepared [Step-13A](Step-13A.md); + no model permissions, hardware scope or unrelated dynamics fixes. ### Remaining -- Manually execute [Step-13](Step-13.md) and shared gate, then review its frozen - verified-source/projection/equivalent-run/capture/comparison/lifetime/native - matrix. Genuine unmet requirements use focused Step-13A before hardware. -- Step-14 is blocked/unassigned. Step-13 comparison/projection is assigned, - not implemented functionality; local-only generation and earlier work are - accepted. All source/history/safety and numerical gates remain. +- Manually execute [Step-13A](Step-13A.md) and shared gate, then review controlled + actual pre-fix/corrected release-tail/Close/new admission proof and regressions. +- Step-14 is blocked/unassigned. Step-13 code and native comparison evidence are + present but not accepted pending C-07 correction. Local-only generation and + earlier work remain accepted; preserve all numerical/history/safety gates. - Profile/module metadata, synthetic virtual-module communication and deterministic dynamics/v2 and corrected dashboard are accepted; later features remain planned. Preserve the complete sequence/safety gates. @@ -1139,11 +1148,18 @@ Runtime/model availability is an environmental prerequisite, not a permanent attestation; future missing/changed state must be explicit, with no cloud/model fallback or automatic setup added to the app. The orchestrator ran no checks. -Step-13 must keep proof-derived simulation evaluation distinct from actual -rehearsal programming and physical applicability. Fuel/behavior improvements -are synthetic estimates; no manufacturer checksum/layout/recovery claim is -supported. Its bounded sampling must stay inside existing archive limits, and -failure/partial-save evidence cannot become successful complete comparison. +Step-13 R-01 is a concrete new completion-order issue: `_owner=null` and +`IsBusy=false` precede `owner.Release()` and completion of `_drain`. +A delayed cancellation callback/release can leave old work undrained while +new Evaluate/Save replaces the tracked completion. The orchestrator identified +source ordering, not an independently executed failure; Step-13A requires actual +controlled reproduction and correction, reusing accepted local-generation +completion prior art without cleanup under required gates. + +Step-13's actual model/proof/config/metrics/archive/replay evidence remains +preserved. Fuel changes are synthetic estimates, no physical/checksum claim. +Its disclosed intermittent unchanged dynamics regression and known recording +navigation issue are separately tracked, not automatic Step-13A fixes. No blocking Step-10 deficiency remains. Its pending-reopen finding below is historical and closed by accepted Step-10A: diff --git a/prompts/STATE.md b/prompts/STATE.md index c9dd616..77be7b1 100644 --- a/prompts/STATE.md +++ b/prompts/STATE.md @@ -4,23 +4,24 @@ | Field | Value | |---|---| -| Updated | 2026-10-31 | -| Phase | Local-only model generation accepted; verified synthetic comparison next | -| Active step | Step-13 - Verified synthetic calibration comparison and demonstration | -| Assignment | [Step-13.md](Step-13.md) | +| Updated | 2026-11-01 | +| Phase | Step-13 reviewed; comparison ownership/drain tail requires focused correction | +| Active step | Step-13A - Retain comparison ownership through cleanup and drain completion | +| Assignment | [Step-13A.md](Step-13A.md) | | Prerequisites | Step-12 with Step-12A ACCEPTED 2026-10-31 | -| Current executable handoff | [Step-13.md](Step-13.md) - manual worker execution only | +| Current executable handoff | [Step-13A.md](Step-13A.md) - one original C-07 lifetime correction | | Assignment status | READY FOR MANUAL WORKER HANDOFF | | Completion handoff status | [Step-12-Completion.md](Step-12-Completion.md) superseded for execution; preserve unchanged | | Original worker report | [Step-12-Report.md](Step-12-Report.md) present and reviewed; preserve INCOMPLETE/BLOCKED observations | | Completion worker report | [Step-12-Completion-Report.md](Step-12-Completion-Report.md) present and read; setup/restart/quiescence approval unavailable, no chat | | Worker report | [Step-12A-Report.md](Step-12A-Report.md) present and reviewed; explicitly INCOMPLETE/BLOCKED | | Accepted execution report | [Step-12A-Execution-Report.md](Step-12A-Execution-Report.md) - reviewed; original blocked reports preserved | -| Expected worker report | `prompts/Step-13-Report.md` - not yet created | +| Step-13 worker report | [Step-13-Report.md](Step-13-Report.md) present and reviewed; preserve unchanged | +| Expected worker report | `prompts/Step-13A-Report.md` - not yet created | | Owner runtime policy | Local models only; no Ollama cloud or cloud fallback, reaffirmed 2026-10-31 | | Last accepted correction/follow-up | Step-12A - accepted 2026-10-31; includes evidenced Hybrid dispatch fixes | | Last accepted major milestone | Step-12 with Step-12A - accepted 2026-10-31 | -| Outstanding follow-up | None; no Step-12B required | +| Outstanding follow-up | Step-13A / R-01 comparison owner release and drain publication; no Step-12B | | Step-02 status | ACCEPTED after Step-02A | | Step-03 status | ACCEPTED after Step-03A/Step-03B | | Step-04 status | ACCEPTED after Step-04A/Step-04B | @@ -36,10 +37,11 @@ | Step-11 status | ACCEPTED 2026-10-31; no correction required | | Step-12 status | ACCEPTED 2026-10-31 after Step-12A actual local-model/native qualification | | Step-12A status | ACCEPTED 2026-10-31; all frozen setup/model/native/regression gates closed | -| Step-13 status | READY FOR MANUAL WORKER HANDOFF; explicit verified-image-to-new-config comparison | -| Worker execution | Step-12A returned COMPLETE; 179 unit/417 integration = 596 passes and clean nine-project Release/native evidence reviewed | -| Immediate next action | Manually give worker Step-13 and shared gate; no automatic invocation | -| Next orchestrator action | Review returned Step-13 report/source/tests/evidence against its frozen comparison matrix | +| Step-13 status | NEEDS CORRECTION for C-07 / AC-03; other implementation/native evidence substantially supported | +| Step-13A status | READY FOR MANUAL WORKER HANDOFF; no hardware/new feature scope | +| Worker execution | Step-13 returned COMPLETE; 179 unit/466 integration = 645 passes and native real-model/target/run/archive evidence reviewed | +| Immediate next action | Manually give worker Step-13A, original Step-13 and shared gate; no automatic invocation | +| Next orchestrator action | Review executed pre-fix/corrected ownership-tail cases and regressions before Step-13 acceptance | ## Repository baseline at initialization @@ -1597,6 +1599,58 @@ driving gates and all 596 cases remain mandatory. Step-14 hardware remains blocked/unassigned until demonstration acceptance and its separate hardware prerequisites. Completed numbering/history is unchanged. +## Step-13 review and focused lifetime correction + +Decision: **NEEDS CORRECTION**, 2026-11-01, for original C-07 / AC-03. +Reviewed the full [worker report](Step-13-Report.md), actual projection/trusted +source/rehearsal/preview/draft/comparison/save/UI code, relevant new cases, +docs/graph and existing Release artifacts. The orchestrator ran no application +builds/tests/inference/native/hardware operations. + +| Criterion | Review disposition | Evidence / remaining finding | +|---|---|---| +| AC-01 | SUBSTANTIALLY SUPPORTED | Exact current preview-to-pinned-draft, separate Save, owning fresh verified readback/internal proof and independent whole-config projection checks | +| AC-02 | SUBSTANTIALLY SUPPORTED | Actual original local model -> explicit approved target -> two equivalent real v2 runs, exact idle/torque values, canonical metrics and saved/readback/replayed archives | +| AC-03 | NEEDS CORRECTION / R-01 | New comparison completion clears owner/IsBusy before releasing owner and settling drain; new work can replace an undrained owner | +| AC-04 | SUPPORTED ON WORKER EXECUTION EVIDENCE, NOT STEP ACCEPTANCE | Reported clean nine-project Release, 179 unit + 466 integration = 645 passes, zero failed/skipped; native real-model pair plus final rebuilt manual lifecycle recheck | +| AC-05 | SUPPORTED FOR REVIEW, SUBJECT TO R-01 | Matrix/self-review/docs, numerical/source/capture lineage and preservation, honest native/control/intermittent-baseline distinctions | + +R-01 source: +[CalibrationComparisonSession](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs:311) +clears `_owner` and publishes `IsBusy=false` under its gate, then calls +`owner.Release()` and only afterwards completes the task stored in `_drain`. +Start rejects only when `_owner` is non-null. Thus another Evaluate/Save can +admit and replace `_drain` while the preceding owner/drain is unfinished. +Owner cancellation callbacks run under the owner's own gate; a delayed callback +can hold Release and widen this into a real pending-cleanup interval. A later +Close can then track the newer drain rather than all admitted old work. +Keep cleanup outside required state gates, but retain ownership through it and +publish terminal state/owner removal/drain settlement atomically. + +This is a source-ordering finding, not an orchestrator-executed reproduction. +The current held wire/read/save cases hold operation work or await full returned +completion; they do not exercise release-tail replacement. Worker Step-13A must +execute a controlled real pre-fix regression and verify corrected behavior. +Accepted [local generation completion](../src/AutoForge.Application/Features/Vehicles/Runbooks/LocalRunbookGenerationSession.cs:205) +provides prior art: release owner outside the state gate before atomic owner/ +completion/drain publication. Do not add another general lifetime framework. + +Other evidence remains intact: real model response 18.8549 s; exactly +600 rpm/ +-10 N*m; both runs 3361 steps/336.1 s and 1346 actual observations, 600 sampled +cruise sets versus 3000 independently tested model steps; complete native archives/ +replay and unchanged original config/ordinary unsaved capture. All 596 old cases +remain in the 645-case suite. These do not substitute for R-01. + +The report discloses an intermediate TaskCanceledException in an unmodified +dynamics-owner regression; isolated and later full suites passed. This is a +separate disclosed uncertainty without an established Step-13 causal fix. +The known recording-navigation Busy issue is also unchanged/out of scope. +Neither is automatic Step-13A cleanup or a reason to edit accepted dynamics. + +[Step-13A](Step-13A.md) is one correction to the frozen owner/drain contract, +not another model-permission gate or new feature. Last accepted major remains +Step-12/12A; Step-14 remains blocked and no hardware assignment is created. + ## Prerequisites, limitations, and known issues - Step-01A worker evidence establishes portable and native Windows compatibility @@ -1635,18 +1689,18 @@ its separate hardware prerequisites. Completed numbering/history is unchanged. ## Review and progression gate -On manual return, evaluate evidence from [Step-13](Step-13.md) -against its matrix and shared gate using the report, +On manual return, evaluate evidence from [Step-13A](Step-13A.md) +against its matrix, original Step-13 C-07/AC-03 and shared gate using both reports, actual code/tests, dependency graph, Git changes, README/docs, and recorded build/test evidence. Do not accept solely on a success claim; do not claim independent runtime verification. -- Inspect trusted verified-image admission, explicit new-config projection/ - lineage, equivalent real runs/captures/canonical comparison, failure-safe - owner/save/readback/replay and native demonstration. Do not add hardware/RAG. -- If accepted: accept Step-13, update roadmap/state, prepare bounded Step-14 read-only hardware work only with its prerequisites +- Inspect actual pre-fix release-tail/competing admission/Close drain cases, + atomic corrected completion, failure/result/save evidence and existing native + comparison behavior. Preserve supported projection/run/model contracts. +- If accepted: accept Step-13A and corrected Step-13, update roadmap/state, prepare bounded Step-14 read-only hardware work only with its prerequisites and stop for manual handoff. -- If deficient: retain Step-13 open and identify a material unmet contract/ +- If deficient: retain Step-13A open and identify a material unmet contract/ regression; any further corrective assignment must be narrowly evidenced. Planned new features are not corrections. - If blocked: retain the active step, document the prerequisite and required @@ -1742,12 +1796,14 @@ claim; do not claim independent runtime verification. | 2026-10-31 | Step-12A execution returned and ACCEPTED | Actual cloud-disabled setup, exact native original +600/-10 intent, pending owner/conflict/Cancel/unmount, two reproduced Hybrid dispatch corrections and 596-case/clean Release evidence reviewed; no 12B | | 2026-10-31 | Completed Step-12 ACCEPTED | All original and follow-up local-only/model/validator/native gates supported; three earlier blocked reports retained, no orchestrator application/runtime execution | | 2026-10-31 | Step-13 prepared | Explicit verified supported-image projection to NEW immutable configs, equivalent real scenario/capture/comparison/replay and native demonstration; original source/rehearsal isolation preserved, no automatic handoff | +| 2026-11-01 | Step-13 returned and reviewed | Reported 645-case/clean Release and actual model/proof/new-config/real-run/archive/replay evidence inspected; most frozen requirements supported | +| 2026-11-01 | Step-13 needs focused C-07 correction; Step-13A prepared | Owner/IsBusy cleared before owner Release and public drain settlement permits replacement of undrained completion; source ordering finding, controlled pre-fix worker proof required; no Step-14 | ## Manual handoff instruction -Provide [Step-13.md](Step-13.md) and shared [worker gate](WORKER-QUALITY-GATE.md) -to the separate worker and request execution of Step-13 only. -Return `prompts/Step-13-Report.md`, preserving all earlier assignments/reports. -Step-13 remains pending manual acceptance; do not begin Step-14. +Provide [Step-13A.md](Step-13A.md), original [Step-13.md](Step-13.md) and shared +[worker gate](WORKER-QUALITY-GATE.md) to the separate worker. Execute Step-13A +only and return `prompts/Step-13A-Report.md`, preserving the Step-13 report and +all earlier assignments/reports. Step-13 remains unaccepted; no Step-14. Return manually to the orchestrator for review. Reports, including final chat reports, must follow the single plain triple-backtick copy-window requirement. diff --git a/prompts/Step-13-Report.md b/prompts/Step-13-Report.md new file mode 100644 index 0000000..6404323 --- /dev/null +++ b/prompts/Step-13-Report.md @@ -0,0 +1,475 @@ +``` +STEP IDENTIFIER: Step-13 +TITLE: Verified synthetic calibration comparison and demonstration +STATUS: COMPLETE - worker implementation and exit verification passed. +MANUAL GATE: Step-13 acceptance remains pending. STOP; Step-14 remains blocked. +DATE: 2026-11-01 +BASE: 245c464 ("After Step 12"); initially clean worktree; no commit/staging/push. + +OUTCOME + +An explicit current preview can enter the existing unit-pinned canonical draft, +but Save, backup, confirmation/approval, Apply, independent full readback and +comparison remain separate actions. The actual owning rehearsal admits copied +historical verified evidence, not caller-constructed success records. +Only warm-idle target and torque cells project into a NEW immutable configuration. +Two fresh actual Simulation/v2 runs provide bounded real signals/captures, +canonical measured metrics, explicit no-overwrite save/readback and existing +RecordedReplay evidence. Original configurations/runs and ordinary recordings +are not replaced. No physical operation, general executor or runtime manager. + +FINAL VERIFICATION + +Command 1: +dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-restore --filter "FullyQualifiedName~Comparison_|FullyQualifiedName~PreviewHandoff_|FullyQualifiedName~TorqueDraft_" --verbosity minimal +PASSED; exit 0; 50 passed, 0 failed, 0 skipped; approximately 8 s. +This is 49 new comparison/handoff cases plus one retained pinned-draft case. + +Required sequential commands, executed after that focused check: +dotnet build .\src\AutoForge.slnx --configuration Release --no-restore +PASSED; exit 0; all nine projects; 0 warnings, 0 errors; 2.29 s. +dotnet test .\src\AutoForge.slnx --configuration Release --no-build +PASSED; exit 0; 179 unit + 466 integration = 645; 0 failed, 0 skipped. +Unit duration approximately 198 ms; integration approximately 9 s. +All 596 accepted cases retained; 49 integration cases added. + +| Project | Final build | Final tests | +|---|---|---| +| AutoForge.Core | PASSED | N/A, non-test project | +| AutoForge.Diagnostics | PASSED | N/A, non-test project | +| AutoForge.Simulation | PASSED | N/A, non-test project | +| AutoForge.Application | PASSED | N/A, non-test project | +| AutoForge.Components | PASSED | N/A, non-test project | +| AutoForge.UI | PASSED | N/A, non-test project | +| AutoForge.Client | PASSED, actual Windows MAUI Hybrid executable | N/A | +| AutoForge.UnitTests | PASSED | 179 passed, 0 failed/skipped | +| AutoForge.IntegrationTests | PASSED | 466 passed, 0 failed/skipped | + +Earlier focused checks passed 25, then 49 cases as coverage expanded. +Earlier complete builds/suites passed 621 and 644 cases respectively. +One intermediate full 644-case run FAILED with TaskCanceledException in the +unmodified ScenarioOwnerCannotBeReplacedAndResetOrCloseSuppressesOldRunPublication +regression during Reset/disposal. Its actual control/page/session/test files +have no diff. The isolated selector below passed all 7 cases, and subsequent +full 644- and final 645-case runs passed. This intermittent result is disclosed, +not erased or repaired by an unrelated dynamics change: +dotnet test .\src\tests\AutoForge.IntegrationTests\AutoForge.IntegrationTests.csproj --configuration Release --no-build --filter "FullyQualifiedName~ScenarioOwnerCannotBeReplacedAndResetOrCloseSuppressesOldRunPublication|FullyQualifiedName~Comparison_Held" --verbosity minimal + +FUNCTIONS, FILES AND INTERFACES + +| File | Implemented responsibility | +|---|---| +| [SyntheticCalibrationProjection.cs](../src/AutoForge.Core/Calibration/SyntheticCalibrationProjection.cs) | Pure canonical Read/Create; exact supported definition/value validation and all-field immutable constructor projection | +| [DynamicsConfigurationService.cs](../src/AutoForge.Application/Features/Vehicles/Simulation/DynamicsConfigurationService.cs) | IDynamicsConfigurationService.RegisterProjection reuses profile validation and existing canonical catalogue collision handling | +| [CalibrationAuthoringService.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationAuthoringService.cs) | Baseline extraction reuses the pure projection Read; existing Save/head/history contracts remain | +| [RunbookPreviewService.cs](../src/AutoForge.Application/Features/Vehicles/Runbooks/RunbookPreviewService.cs) | CopyCurrentCandidateToDraft validates exact expected current preview/workspace/head and returns complete copied canonical values, without Save/target action | +| [CalibrationRehearsalSession.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationRehearsalSession.cs) | CaptureComparisonSourceAsync plus current owning proof guard; fresh full readonly admission and source/lifetime checks | +| [CalibrationComparisonSource.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSource.cs) | Internal-constructor copied proof bound to actual owning service/generation, original backup and verified Apply lineage | +| [CalibrationComparisonModels.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonModels.cs) | Typed state/failures, immutable bounded samples/metrics/captures/save outcomes, canonical readonly report and explicit result retirement | +| [CalibrationComparisonSession.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs) | Factory, EvaluateAsync, real bounded RunAsync, complete canonical signal validation, SaveCapturesAsync, Cancel/Close/drain | +| [CalibrationPage.razor](../src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationPage.razor) | Explicit preview handoff; prior pinned units and exact typed unedited candidate retained until separate Save | +| [CalibrationRehearsalPanel.razor](../src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationRehearsalPanel.razor) | Explicit comparison opening binds THIS actual owning rehearsal, without automatic evaluation | +| [CalibrationComparisonPanel.razor](../src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationComparisonPanel.razor) | Explicit operation/provider/Evaluate/Cancel/Close/Save controls, readonly report, stale/partial/per-ID errors and async unmount | +| [AddAutoForgeUi.cs](../src/AutoForge.UI/Features/Foundation/AddAutoForgeUi.cs) | Existing UI composition registers comparison factory | +| [CalibrationComparisonTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationComparisonTests.cs) | Actual proof/projection/model/capture/store/replay, independent invariants, faults/boundaries/controlled owners | +| [CalibrationAuthoringUiTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationAuthoringUiTests.cs) | Actual rendered pinned preview handoff and exact tiny/nonuniform canonical Save | +| [CalibrationRehearsalTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationRehearsalTests.cs) | Existing fixture optionally supplies real dynamics or a below-boundary faulting dynamics transport; old default unchanged | +| [DynamicsEngineTests.cs](../src/tests/AutoForge.UnitTests/DynamicsEngineTests.cs) | Strengthens the retained independent warm regression: exact 3361/336.1 and every one of 3000 cruise model steps within 45 MPH +/-1 MPH | + +Nine-project graph unchanged: Core owns pure mapping; Application owns trusted +admission/workflow; UI owns explicit commands. Application/UI do not reference +Simulation implementations or access a private engine sample/oracle. +No new project/package/host/schema, protocol change, importer duplication, +physical port, runbook executor, alternate runtime or dependency installation. + +EXECUTED FROZEN CONTRACT MATRIX + +Named Comparison_* cases below are in the comparison test file linked above. +The preview UI case is in the authoring UI test file linked above. + +| Contract | Named executed evidence and decisive assertion | Result | +|---|---|---| +| C-01 | Comparison_CurrentPreviewHandoffRejectsStaleOrSupersededWithoutSaving (superseded/stale-head/wrong-workspace); PreviewHandoff_KeepsExistingPinnedUnitsAndExactCandidateUntilSeparateSave; real model preview handoff and final native unit-toggle recheck | PASSED: complete canonical candidate, unchanged head/history on Copy/reject, no target reads/writes; separate Save; existing pinned units preserved | +| C-02 | Comparison_ProofCannotBeConstructedByPublicCallerSuccessRecords; Comparison_TrustedAdmissionRejectsWrongOrSupersededActualProof; Comparison_FreshOwningReadRejectsDivergentOrUnavailableProofBeforeProjection; Comparison_RecoveryRequiredRejectsUntilActualReadOnlyReconciliationVerifiesApply | PASSED: no public proof/result constructor; actual original backup, owning Apply/current Proposed/candidate/identity/revision/full readback agree; wrong operation/head/closed/external revision and malformed/unavailable evidence reject | +| C-03 | Comparison_ActualVerifiedApplyProjectsOnlySupportedFieldsAndRunsEquivalentCapturedScenarios; Comparison_NoChangeProjectionRetainsExactContentFingerprintAndCopiesNonuniformCells; Comparison_ProjectionRejectsInvalidCompleteValuesWithoutChangingOriginal | PASSED: independent whole serialized configuration equality after changing ONLY the two supported properties; no-change truthful fingerprint; nonuniform cells; 11 key/unit/axis/shape/bound/nonfinite negatives | +| C-04 | ActualVerifiedApplyProjectsOnlySupportedFieldsAndRunsEquivalentCapturedScenarios; Comparison_3900And3901StepBoundaryIsMeasuredWithUnchangedCaptureLimits; real native pair and all four actual archive raw-context inspections | PASSED: fresh distinct real run/capture IDs, exact Simulation/v2 provider, StepAsync(1), engine AND transmission every five steps plus final, no duplicate final reads; no replay/oracle/canned telemetry as physics | +| C-05 | ActualVerifiedApplyProjectsOnlySupportedFieldsAndRunsEquivalentCapturedScenarios; original real local-model native path through separate Save/backup/approval/Apply/full readback; final native manual recheck of changed UI/lifetime/report paths | PASSED: exact 650->1250 rpm and 2000-rpm 360->350 N*m, all other cells/axes unchanged; both last TEN warm samples within 1 rpm and stationary <=0.001 m/s | +| C-06 | Comparison_RealFileReadbackAndReplayPreserveExactBytesAndRejectWrongReferences; actual model-pair native Save/readback and both actual RecordedReplay prefix/end/raw trace inspections | PASSED: canonical measured metrics and full lineage, distinct archive identities, exact file/readback bytes, actual RecordedReplay not physics; wrong filename/run corruption retains prior replay context | +| C-07 | Comparison_HeldActualSignalTransportCancelsAndDisposesWithoutLatePublication (Cancel/Close); Comparison_HeldFinalActualReadDrainsOrRejectsSupersessionWithoutLateSuccess (Cancel/Close/head/external); Comparison_PendingSaveRejectsCompetingOwnerAndCloseDrainsWithoutDeletingSuccessfulFile; final native observed-pending Cancel/unmount/fresh owner/Close | PASSED: one Busy-gated owner, actual barriers released in finally, drain outside required gates, no late current pair/rebase, prior success retired Stale/as-of and inspectable | +| C-08 | Comparison_ActualTransportSignalFaultNeverPublishesHealthyMetrics (missing/unit/quality/numeric 1e309/wrong kind/run/timeout); Comparison_FewerThanTenActualWarmSamplesRejectsWithoutShortenedPassingWindow; 3900/3901; partial/readback-corrupt/post-publication-failure save tests | PASSED: typed visible errors and partial evidence, no zero/nonfinite/uncertain healthy metrics; exact per-ID uncertainty and retries without overwrite/deletion; unchanged archive caps | +| C-09 | Independent whole-config test; retained WarmBaseline_MeetsConfiguredScenarioThresholdsAndIsReproducible strengthened for ALL 3000 cruise model steps; real transport/store/replay faults and unchanged complete baseline suites listed below | PASSED: producer/consumer faults below feature boundary, independent test-only numerical assertions, no private oracle exposed or invented firmware/checksum/physical-write mechanisms | +| C-10 | Final sequential nine-project Release/645-case suite; actual native original warm capture -> real model -> preview/draft/Save -> verified target -> equivalent pair -> capture Save/readback/actual replay; final native corrections and sequential existing route checks | PASSED: 596 preserved plus 49 new cases; real original model content recorded below, no mock-as-native or physical/unassigned mutation | + +| Acceptance | Executed mapping | Status | +|---|---|---| +| AC-01 | C-01..C-03 actual owning source, independent invariants and atomic negative/current-head guards | PASSED | +| AC-02 | C-04..C-06 actual equivalent scenarios, exact idle/driving gates, real captures/readback/replay | PASSED | +| AC-03 | C-07..C-09 real-boundary barriers, exact limits, partial/corrupt/uncertain saves and preserved isolation | PASSED | +| AC-04 | C-10 final 645-case/nine-project Release and genuine native model/target/model-run/archive path | PASSED | +| AC-05 | Source-path self-review, docs, protected hashes/history, accurate native/control distinction and this sole new prompt report | PASSED; manual acceptance still pending | + +Preserved executed baseline evidence includes: +- [SyntheticDiagnosticsContractTests.cs](../src/tests/AutoForge.UnitTests/SyntheticDiagnosticsContractTests.cs): strict wire/version/identity/service/result contracts. +- [DiagnosticSessionCompositionTests.cs](../src/tests/AutoForge.IntegrationTests/DiagnosticSessionCompositionTests.cs): actual provider/session composition, supported timeout/unsupported/invalid reply behavior and clear/readback semantics. +- [DynamicsWorkflowTests.cs](../src/tests/AutoForge.IntegrationTests/DynamicsWorkflowTests.cs): actual v2 identity/config/telemetry, interruption/reset/close and owner behavior. +- [CalibrationRehearsalLifecycleTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationRehearsalLifecycleTests.cs), [CalibrationRehearsalReviewTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationRehearsalReviewTests.cs), [CalibrationRehearsalReopenTests.cs](../src/tests/AutoForge.IntegrationTests/CalibrationRehearsalReopenTests.cs): real CAS/readback/recovery/reconciliation/Restore and held close/reopen cleanup. +- [TelemetryRecordingIntegrationTests.cs](../src/tests/AutoForge.IntegrationTests/TelemetryRecordingIntegrationTests.cs) and [TelemetryReplayTests.cs](../src/tests/AutoForge.IntegrationTests/TelemetryReplayTests.cs): actual recording/archive corruption/limits and exact replay boundaries. +All were executed in the final full suite; no waiver or skipped case. + +ACTUAL NATIVE DEMONSTRATION - REAL MODEL PATH + +Actual Client executable: +[AutoForge.Client.exe](../src/AutoForge.Client/bin/Release/net10.0-windows10.0.26100.0/win-x64/AutoForge.Client.exe) +Task-owned first Client PID 9248; native WebView2/CDP 9386, not an alternate host. +Initial persisted preferences Metric/Road; initial local archive file count 0. + +Qualified shared runtime, left unchanged throughout this assignment: +Ollama 0.40.13; app uses http://127.0.0.1:11434. +Existing serve PID 50460, created 2026-10-31T14:39:16.1305236-04:00. +Current startup 2026-10-31T14:39:16.200-04:00: +"Ollama cloud disabled: true"; actual store D:\AI\ollama\models. +Model huihui_ai/qwen3-abliterated:8b-v2 +Digest 543f9deff86d8347fd96ea1f3e54e73861ea70d52def71d31ccaeaf013de6ba6 +Installed bytes 5027793060; GGUF/qwen3/8.2B/Q4_K_M; Apache2 reported metadata. +Model metadata is not cryptographic proof of local execution. +No pull/install/configuration/startup/restart/unload/runtime setup was performed. + +Original configuration: +sha256:v1:e8991ae139b54959ec53a4fe64b08dc9bfc03feb533496cf741f2c9f3a2342da +Profile autoforge.synthetic.test-truck revision 1. +Engine module.engine.primary; transmission module.transmission.primary. +Model autoforge.synthetic.longitudinal v1. +Definition autoforge.synthetic.engine-authoring revision 1. +Metadata retained as metadata: hardware.synthetic.engine-a; +software.synthetic.engine-1; firmware unknown; calibration.synthetic.engine-base. +Workspace c50f9c62fd3a411986640c6020e8bbcf. +Baseline 79b55bb68c8940418855a048887c5017; revision 1; history 1/32. +Warm target 650 rpm; axis [650,1000,2000,3200,4200,5000] rpm; +cells [240,300,360,350,300,180] N*m. + +Original native warm run: +diagnostic-session-000001 / dynamics-run-000001 / same original config. +Explicit actual 100-step advance = 10.0 virtual seconds, then Accelerate at 0%. +Independent ordinary capture 4fb599dccbf74b608c4188aa456fe788; +started 2026-11-01T10:01:42.6843819+00:00; +2 actual validated observations, 12562 encoded observation bytes. +Explicit recording Stop and dynamics Close; stopped unsaved capture retained. +Later ordinary /simulation inspection confirmed THIS exact capture remained +Complete and unsaved after the comparison pair; it was not replaced/discarded. + +Exact real model goal: +Propose warm-idle target 1250 rpm and set torque at 2000 rpm to 350 N*m; keep every other value unchanged. +Native generation started 2026-11-01T10:02:57.939Z. +Observed click-to-success latency 18854.89999999106 ms. +Original content passed the unchanged validator without extraction/repair. +Runbook 7878da6a1a424dd893714ab1296f27db. +Preview 68ba217e75844e50ab325e1be6a1df3d; Current / ChangesProposed. +Exact two canonical diffs: warm +600 rpm; torque at 2000 rpm -10 N*m. + +ORIGINAL MODEL CONTENT, VERBATIM (not a second controlled response): +{ + +"schemaVersion":1, +"runbookIdentifier":"7878da6a1a424dd893714ab1296f27db", +"targetKind":"simulation", +"rationale":"Propose warm-idle target 1250 rpm and set torque at 2000 rpm to 350 N*m", +"steps":[ +{ +"stepIdentifier":"calibration-preview", +"operation":"proposeCalibration", +"source":{ +"workspaceIdentifier":"c50f9c62fd3a411986640c6020e8bbcf", +"expectedHeadIdentifier":"79b55bb68c8940418855a048887c5017", +"profileIdentifier":"autoforge.synthetic.test-truck", +"profileRevision":1, +"configurationIdentifier":"sha256:v1:e8991ae139b54959ec53a4fe64b08dc9bfc03feb533496cf741f2c9f3a2342da", +"moduleIdentifier":"module.engine.primary", +"modelIdentifier":"autoforge.synthetic.longitudinal", +"modelVersion":1, +"definitionIdentifier":"autoforge.synthetic.engine-authoring", +"definitionRevision":1 +}, +"values":{ +"scalars":[ +{ +"logicalIdentifier":"engine.warm-idle-target", +"unit":"rpm", +"value":1250 +} +], +"tables":[ +{ +"logicalIdentifier":"engine.torque-curve", +"unit":"N*m", +"axisUnit":"rpm", +"axis":[650,1000,2000,3200,4200,5000], +"cells":[240,300,350,350,300,180] +} +] +} +} +] +} + +Explicit native Copy produced scalar 1250/cells [240,300,350,350,300,180] while +saved head/history stayed Baseline revision 1. Only a separate Save created +Proposed cc7311bb8c374de4bc69e74418496044, revision 2, parent original Baseline. +Original source/fingerprint and all unrequested saved values remained unchanged. + +Actual isolated target/provider autoforge.simulation.calibration-rehearsal: +Instance 9bbd543f8c8f4824a50ee1b72f0a42e9, original revision 0. +Verified two-read backup 1e5518a9d99e4398916be0ebd8df0608. +Apply preparation bee7561f6f9a4f8991a136568a315224. +Explicit confirmation/single-use approval 080d3ad2aad44a028ecc13a79bc3ff96. +Dispatched Apply e50b5940c2ee4b9b8489ee56e61a7f84; +started 2026-11-01T10:08:31.8770393+00:00; +completed 2026-11-01T10:08:31.8786715+00:00. +Acknowledgement True/revision 1, separately from independent full actual readback. +VerifiedApplied / CandidateMatching / actual revision 1. +Full before image 650/[240,300,360,350,300,180]. +Full actual readback 1250/[240,300,350,350,300,180]. +All six axis coordinates, units and unchanged cells matched exactly. + +Explicit comparison 5d89e81c166d40488f3df4590c4e7524. +Admitted historical proof as of 2026-11-01T10:08:46.5550875+00:00. +Derived NEW configuration: +sha256:v1:3ee252697bfb69f36f4b99dca4c0267f7517a17a459666fb47551fa6ed978450 +Baseline: diagnostic-session-000002 / dynamics-run-000002; +capture c731886c10fb4d9ba5c82b5bc387be07. +Derived: diagnostic-session-000003 / dynamics-run-000003; +capture df8ef66c6d7b4acabbea0e63b95b9a0c. +Same explicit actual autoforge.simulation.dynamics provider and equivalent +initial state/five-phase scenario; distinct NEW fresh owned sessions/captures. +Both Complete, 1346 actual v2 observations = 673 engine/transmission sample sets. + +ACTUAL CANONICAL NUMERICAL RESULTS + +| Metric | Baseline | Derived | +|---|---|---| +| Warm target rpm | 650 | 1250 | +| Actual last-ten maximum absolute idle error rpm | 4.084768079337664E-10 | 7.855760486563668E-10 | +| Actual last-ten maximum absolute stationary speed m/s | 0 | 0 | +| Warm phase model steps / sampled sets | 100 / 20 | 100 / 20 | +| Total model steps / virtual seconds | 3361 / 336.1 | 3361 / 336.1 | +| Warm/Accelerate/Cruise/Brake/ReturnIdle model step counts | 100/103/3000/58/100 | 100/103/3000/58/100 | +| Acceleration seconds | 10.3 | 10.3 | +| Braking seconds (round-trip representation) | 5.800000000000001 | 5.800000000000001 | +| Sampled cruise sets / min..max m/s | 600 / 20.1168..20.1168 | 600 / 20.1168..20.1168 | +| Fuel consumed from initial kg | 0.518385971679477 | 0.5184897031483956 | +| Distance from initial m | 6193.507764002321 | 6193.389583605566 | +| Final actual transmission shift count | 4 | 4 | + +Derived-minus-baseline measured fuel +0.00010373146891862195 kg; +distance -0.11818039675472392 m; shift count delta 0. +No improvement/sign requirement invented. Fixed 0.5 virtual-second sampling is +not continuous extrema or a complete shift timeline. Independent test-only +baseline assertions inspect ALL 3000 cruise model steps at 45 MPH +/-1 MPH; +production sampled count 600 is explicitly different. + +UNCHANGED CONFIGURATION INVARIANTS + +Only Model.WarmIdleTargetRpm and EngineTorqueCurve cells changed; axis unchanged. +Independent whole serialized configuration DeepEquals replaces only those two +expected properties before comparison. Original export remained content exact. +Unchanged model fields: StepSeconds, VehicleMassKilograms, WheelRadiusMeters, +DragAreaSquareMeters, RollingResistanceCoefficient, AirDensityKilogramsPerCubicMeter, +GravityMetersPerSecondSquared, DrivetrainEfficiency, MaximumBrakeForceNewtons, +IdleSpeedRpm, MaximumEngineSpeedRpm, LaunchSlipEndSpeedMetersPerSecond, +IdleFuelRateKilogramsPerSecond, FuelKilogramsPerJoule, AmbientTemperatureCelsius, +InitialEngineTemperatureCelsius, EquilibriumEngineTemperatureCelsius, +ThermalResponsePerSecond, ForwardGearRatios, UpshiftSpeedsRpm, DownshiftSpeedsRpm, +FinalDriveRatio. Also unchanged: format/model/profile/revision/vehicle/module/ +calibration references, complete initial conditions and every scenario field. +Original warm/accelerate/cruise/brake/return holds/limits: 10/60/300/60/10 s; +driving target 20.1168 m/s. No fidelity/shift schedule/manufacturer data redesign. + +NATIVE SAVE, READBACK AND ACTUAL RECORDED REPLAY + +Explicit pair Save showed BOTH persisted True/readback verified True. +Real model-pair archives: +c731886c10fb4d9ba5c82b5bc387be07: 8669778 file bytes; +8667485 encoded observation bytes; SHA256 +0B4EFFAF4469A5BD2F60939F9DCC4F74BD5045AF8C2EDCD66301147F15CBD3E9. +df8ef66c6d7b4acabbea0e63b95b9a0c: 8668397 file bytes; +8666104 encoded observation bytes; SHA256 +7DC25DE26831F7AB46280C5DEB18DE753E20C3A31C10E11579190E2C33FC0623. +Both inside unchanged archive bounds: 4096 observations, 16 MiB observations, +1 MiB header, 32 MiB archive. No global ordinary recording takeover. + +Actual native /telemetry-replay selected/loaded each exact saved ID: +Baseline replay instance e2d3f867d30e4dddb934ef63dd39c763. +Applied count 1/1346 inspected as the exact one-observation prefix. +End 1346/1346, capture elapsed 963557 ticks / 0.0963557 s. +Derived replay instance b810e3aa30d34579955b197f411edfca. +End 1346/1346, capture elapsed 1088190 ticks / 0.108819 s. +Both retained original Simulation source, exact profile/session/config/run/v2, +Complete/User source completion and raw original request/response base64. +Final native raw observation 1346: module.transmission.primary/address 20, +read.signals/PositiveResponse; correlations diagnostic-session-000002-001346 +and diagnostic-session-000003-001346; diagnostic ticks 1345->1346. +Independent archive-byte decoding confirmed actual final response model step +3361/time 336.1 and transmission.shift-count = 4 / count / Good with exact +header configuration/run correspondence. Capture elapsed time is not model time. +Navigation closes page owners; archived proof is historical, not current target +authority, and RecordedReplay never re-executes physics. + +FINAL REBUILT NATIVE RECHECK, FAULTS AND OWNERS + +After source-path corrections, final native Client PID 53432 rechecked changed +UI/lifetime/report paths with explicit MANUAL preview, not another claimed LLM. +Final assembly SHA256 matched the final Release build: +Application 19A02DD34C0643D4C8ACF3CB3CD0BD1477D8935898BFE05BF17DE48D97FAE2A7 +UI 3A9443FC98152C3273B6BBC5E0A3B4FF22C3436E963DBC0826553D9F1B209E5F + +Existing Metric draft stayed Metric/N*m while global units became ImperialUS. +Copy then separate Save kept exact candidate cells; Metric/Road restored. +Workspace 93a59ed7655346508c739bae1478ae9f; +Proposed 4c82e4b8246d48a5a488b5d3833bd401, revision 2. +Target 336ab6478e5e4acab281d4d5274049e1, revision 0->1. +Two-read backup f3e492dcc6cc4ce3a9a9a7ca53e16a34. +Preparation bccd73d6000d4a0681f8efa5edb24a39. +Explicit approval a87c89d05e854ea9b5dce5085df1d85a. +Actual verified Apply 79c2e83dea11493eb8df3b64ace100d5. +Final comparison 1e5f51c28c364afc937636d4980cd12e; +proof d0f6fe8979754bd8ad22812bb3fa1f4f as of +2026-11-01T10:22:25.2194362+00:00. +Same original/derived fingerprints and every numerical result above. +Baseline diagnostic-session-000001/dynamics-run-000001; +capture 8805241baade45c4bf4a41347d3d0d95, 8669924 file bytes; +8667631 observation bytes; SHA256 +F12CE555AF187836C44781A3CF87BA84A8D7EA9DDE440210599D00B541F50C18. +Derived diagnostic-session-000002/dynamics-run-000002; +capture c2c4aea34aec42d18e881cbb19430fc0, 8668178 file bytes; +8665885 observation bytes; SHA256 +FD116FF6845FDA98E460AD4E4A42F892899839F675ED651FB6CC44DAD4695916. +Both 1346 observations, explicit Save/readback verified. Final successful native +Save text truthfully said "exact readback verified", not "not saved" beside True. +All four files independently checked actual final raw run/config/step/time/count +before cleanup. Actual native replay evidence above is for the REAL MODEL pair. + +Final native Cancel was triggered only after observing +"Evaluating; operation/drain pending True." +Result: "Cancelled; operation/drain pending False."; visible cancellation; +same earlier successful result retained, explicitly Stale/as-of. +Observed-pending unmount then reached Home without a late comparison report. +Reentry created Idle/fresh owner with no old comparison/target authority. +Explicit comparison Close reached Closed, not a reused prior completion. +Original native export still 650/[240,300,360,350,300,180], original five-phase +scenario unchanged; deliberate saved history remained 2/32 before app cleanup. + +Earlier real-model native checks also established report unchanged by display +units, and a separate deliberate 1250->1300 authoring Save marked the retained +comparison Stale without changing original config or programmed image. +Reevaluation against that new unmatched head visibly rejected owning proof. + +Controlled tests, not native timing claims: +- Held actual wire signal and final target read prove Busy/Cancel/Close/source + supersession; resources dispose once and no late healthy publication. +- Real model 3900-step completion produces 1560 actual observations; required + step 3901 rejects with retained partial evidence and no complete pair. +- Actual shortened warm scenario rejects fewer than ten samples, not a reduced + passing window; true JSON numeric 1e309 and wrong-kind "NaN" are separate faults. +- Second save failure preserves first ID/file and unsaved second capture. +- Readback corruption preserves published ID; retry reads without overwrite. +- Failure reported AFTER final publication checks existing catalogue on retry, + verifies exact original bytes and does not collide, overwrite or delete. +- Held Save Close drains and retains truthful per-ID publication evidence. +- Real file/replay filename/run corruption rejects while retaining prior context. + +SELF-REVIEW AFTER FIRST GREEN + +Walked actual producers/consumers: preview/current-head -> pinned draft/Save -> +owning rehearsal backup/Apply/readback -> internal proof -> pure constructor/ +catalogue -> real Diagnostics control/signals -> independent capture -> store/ +codec/readback/replay -> UI publication/Close. Checked normal, rejected, partial +and concurrent paths, exact validation ordering and authoring->rehearsal gates. + +Issues corrected within Step-13: +1. Copy originally used current display selection rather than existing draft + units; preserve prior pinned units and exact typed unedited fields. +2. Cancellation can return a diagnostic Cancelled reply instead of throwing; + check token before signal classification. First native attempt was Failed + with a cancelled transport reply; final native attempt is correctly Cancelled. +3. Save can fail after publication; retry verifies existing ID read-only, and + closing owners retain per-ID publication evidence without healthy pair claims. +4. Move cancellation callbacks/Close drain outside required state gates; ensure + diagnostic disposal still executes if capture cleanup fails. +5. Retire retained current-result status on admitted reevaluation/Close, perform + another fresh owning full read before publication, and show as-of/Stale. +6. Surface owning IO failure as typed comparison proof failure (new test first + failed with bare IOException, then passed); wrong replay error expectation + was corrected to the existing typed archive contract without changing replay. +7. Include exact torque/source/proof/preparation lineage in the readonly report; + correct contradictory successful-Save text and distinguish sampled/full-step + numerical evidence. Preserve all old behaviors and independent Components. + +Known unchanged baseline/tool observations, not hidden Step-13 fixes: +- The intermediate unmodified cancellation-regression failure is disclosed above. +- Rapid smoke-driver navigation between recording-host routes while the shared + archive refresh was pending produced the existing recording-panel initialization + Busy error ("Another archive command is pending; the current capture was not + changed") in TelemetryRecordingWorkspace.RefreshAsync/TelemetryRecordingPanel. + Those files have no diff and unrelated recording-navigation cleanup is expressly + out of scope. Recovered only the task-owned Client (PID 40612); then verified + all SIX actual rendered route headings with the actual archive owner idle: + target metadata, vehicle profiles, static diagnostics, simulation, runbooks, + calibration. All passed, Blazor error display none, Metric/Road preserved. + No URL-only proxy was accepted as route evidence. +- A cold smoke-driver attempt omitted explicit profile revision; source creation + and dependent actions correctly rejected. Corrected the driver selection only. +- The task-owned readiness observer initially omitted characterData events; + corrected instrumentation, not product code, and separated dispatch/read checks. +- Initial compiler/test-helper issues were corrected; none remain in final build. + +DOCUMENTATION, LIMITATIONS AND PROTECTED HISTORY + +Aligned [README.md](../README.md), [documentation index](../docs/README.md), +[architecture](../docs/architecture.md), [build/test](../docs/build-and-test.md), +[authoring](../docs/calibration-authoring.md), [rehearsal](../docs/calibration-rehearsal.md), +[runbooks](../docs/runbooks-v1.md), [local generation](../docs/local-runbook-generation.md) +and new [comparison usage](../docs/calibration-comparison.md). +Step-12/12A is accepted; Step-13 awaits manual review. + +Proof is volatile/historical owning evidence, not perpetual external attestation. +Supported logical image is not full ECU backup/firmware/checksum/authenticity. +No real-world fuel/shift/safety benefit, hardware identity or physical permission +is inferred. Failed physics may have a Complete stopped archive coverage prefix, +but its separate run error/null metrics prevent successful comparison. +Pair save is not atomic or crash-durable; no rollback deletion is performed. +No model/private telemetry/oracle/test data is substituted into production. + +Protected current SHA256 hashes rechecked unchanged: +[Step-13.md](Step-13.md) +121A97B67A6604BE53E8C5742A7F2001971A99F3B6E9E861671C39704B6BA535 +[STATE.md](STATE.md) +8EBE7AC3FF763B9AE042E1044C88A605A1FAE5FAA897C53AA170CE77893C2327 +[ROADMAP.md](ROADMAP.md) +0018A86E12D17360363E4F4D4EF6BEA4CEC3891A92B5516C5DD8D8C1C799F599 +[WORKER-QUALITY-GATE.md](WORKER-QUALITY-GATE.md) +F9C0B14516B70345E4CE93AD1EDB8856094BC92E26A37AEB16EDE687AD021804 +[Step-12A-Execution-Report.md](Step-12A-Execution-Report.md) +C5056359F33CC95C4D53F2ACAF7DBAB9E59B8B99CE66B0CD99B3475B427621F5 +All other tracked prompt/assignment/historical reports remain unchanged. +Only this new report added under prompts. Git whitespace check passed; +Git LF/CRLF informational notices are not build warnings. +No agents, orchestration edits, staging, commits, pushes or Step-14 work. + +CLEANUP - EXECUTED AND VERIFIED + +Stopped only freshly identity-verified task Client PIDs 9248, 53432 and 40612. +Removed only four exact task-created verified archive IDs listed above; initial +and final user archive file count 0. No unknown/user archive or rollback deletion. +Removed 21 exact task native helper/evidence files, the inspected task-owned +step13-webview profile and three exact owned oversized temporary outputs. +Session root/files folder and all other session material preserved. +Remaining Step13 native targets 0; CDP 9386 listeners 0. +Original stopped unsaved warm capture was preserved during evaluation and +discarded only with final task-owned app process cleanup, not pair acquisition. +Original persisted preferences restored Metric/Road. +Shared Ollama serve PID 50460, original creation time/binary/version/model/digest/ +store/cloud-disabled setup remain untouched and responsive. +Persistent deliverables are source/tests/docs and this report with original +model content, actual IDs/numerics/hashes and honest execution boundaries. + +STOP FOR MANUAL REVIEW. No automatic acceptance or next-step handoff. +``` diff --git a/prompts/Step-13A.md b/prompts/Step-13A.md new file mode 100644 index 0000000..cfbd52d --- /dev/null +++ b/prompts/Step-13A.md @@ -0,0 +1,188 @@ +# Step-13A - Retain comparison ownership through cleanup and drain completion + +## STEP IDENTIFIER + +Step-13A, corrective follow-up to [Step-13](Step-13.md). +Original C-07 / AC-03 has one remaining ownership-tail gap, R-01. +Step-13 is not accepted; Step-14 remains blocked/unassigned. +Read [STATE.md](STATE.md), the preserved [Step-13 report](Step-13-Report.md) +and [WORKER-QUALITY-GATE.md](WORKER-QUALITY-GATE.md). + +## STEP TITLE + +Prevent another comparison/save owner from replacing an unfinished previous +owner/drain, and make Close include all admitted cleanup. + +## OBJECTIVE + +Fix R-01 only. Complete release/cleanup outside required state gates while +keeping the admitted owner/busy/drain identity reserved until terminal +completion is settled atomically. +Execute a real controlled pre-fix regression, correct the source order, +verify behavior and preserve all supported Step-13 functionality. +No new model/hardware/project/format/feature or permission-form work. + +## ARCHITECTURAL CONTEXT + +The Application comparison session owns one evaluation/save operation and its +cancellation/drain. UI releases Hybrid command events and drains that owner on +unmount. Keep the nine-project graph, real Dynamics/v2/capture/store/target +boundaries and private test oracle isolation. +The orchestrator owns review/handoffs only; worker executes code/tests/native. + +## CURRENT IMPLEMENTATION STATE + +Most Step-13 scope is supported by reviewed code and worker evidence: +current preview-to-pinned draft, trusted actual owning readback proof, exact +NEW config projection, equivalent real full scenarios, canonical metrics, +bounded real captures, partial-save/readback/replay and native model path. +Worker reports clean nine-project Release and 179 unit + 466 integration = +645 passes, zero failed/skipped; all 596 earlier cases preserved. +Do not rewrite its report or claim these are new corrective executions. + +R-01 in +[CalibrationComparisonSession.cs](../src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs:311): + +1. ObserveAsync finishes awaited operation work. +2. Under `_gate`, it clears `_owner` and sets `IsBusy=false`/terminal state. +3. Outside `_gate`, it calls `owner.Release()`. +4. Only afterwards does it settle the completion task stored in `_drain`. + +Start checks only `_owner != null`. A competing Evaluate/Save can therefore +admit after step 2 while the old release/completion is unfinished, replacing +`_drain`. A later Close can then follow only the newer completion. +Owner.Cancel invokes linked cancellation callbacks under its own gate; a +delayed callback can keep Release pending and make this a real cleanup interval, +not merely a sequential call after a completed await. + +The orchestrator identified source ordering and ran no reproduction/build/test. +The worker must reproduce the actual endpoint behavior before fixing it. +Existing held signal/final-read/save cases do not hold this completion tail. +Accepted prior art: +[local-generation completion](../src/AutoForge.Application/Features/Vehicles/Runbooks/LocalRunbookGenerationSession.cs:205) +releases the owner outside the state gate BEFORE atomic owner/completion/drain +publication. Reuse the pattern; no general lifetime framework. + +## REQUIRED IMPLEMENTATION + +### Frozen corrective matrix + +| ID | Controlled condition | Required result | +|---|---|---| +| C-01 | Operation work returns while old cancellation/release completion remains held; competing Evaluate and Save | Both reject Busy before new work/admission/config/capture/store effects; same old drain retained, IsBusy truthful until cleanup settles | +| C-02 | Close/Dispose competes with that pending completion tail | Shared Close stays pending through the old cleanup/drain, no replacement completion, all owned resources released once, then Closed with no late result | +| C-03 | Release/drain settlement after success, failure or cancellation | Cleanup outside required state gates, then atomic terminal state/owner removal/completion publication; primary error/cancel and per-ID save evidence preserved, no false success or hung waiter | +| C-04 | After real full drain, next legitimate operation or fresh page lifetime | New owner can admit normally only while open; old completion cannot clear/overwrite it; closed sessions remain closed and fresh entry independent | +| C-05 | Existing evaluation/partial-save/model/native flows and full baseline | All 645 cases retained, focused new regressions and nine-project Release/native affected-lifecycle checks pass; exact data/proof/capture/metrics unaffected | + +Keep `_owner` (or equivalent admission reservation) and the specific drain +until its release is complete. Publish terminal state, clear ownership and +settle that same completion under a coherent gate. Do not expose idle before +the preceding operation has drained or allow `_drain` replacement in the tail. +Preserve shared Close/cancellation and current-result retirement semantics. + +Do NOT put potentially blocking release or cancellation callbacks under the +comparison state gate: a callback can read state or take another required gate. +Avoid a new inverse lock order or awaiting work needed by cleanup while locked. +Observe any cleanup failure explicitly, preserve the original operation error/ +partial-save evidence and settle waiters truthfully; no success-shaped finally. + +Do not fix by merely adding sleeps, relying on current callbacks being quick, +suppressing errors or hiding the Busy state. Do not stop/unload runtime/models, +roll back published files or modify proof/value/physics validation. + +## FILES OR PROJECTS EXPECTED TO CHANGE + +Focused comparison completion/admission/Close code, directly related controlled +tests (reuse existing real comparison fixtures/store/transport seams), and +related comparison/build/status documentation. +UI changes only if directly needed to preserve the affected lifetime behavior. +Only `prompts/Step-13A-Report.md` under `prompts`; all old prompts/reports unchanged. + +## INTERFACE AND DEPENDENCY REQUIREMENTS + +Preserve public comparison/source/projection/archive contracts, proof/current- +head guards, canonical values/metrics/fingerprints, 3900-step/1562-scheduled- +observation cadence and unchanged archive bounds. Preserve single explicit +owner/no unassigned business mutation, partial per-ID save truth and replay. +No new dependencies/projects/hosts, runtime/model installation/configuration, +hardware programming, model permission forms, cloud, tools or Git backup work. + +## TESTING REQUIREMENTS + +Before production fix, add and EXECUTE the failing real feature-boundary test. +Hold cancellation/release after operation work can finish, not only transport +before its reply. Show competing Evaluate/Save actually admits too early or +Close can finish with old admitted cleanup/drain unfinished. Record exact +behavioral failure; compilation failure or a prose sequence is not reproduction. + +Prefer a controlled cancellation callback/cleanup resource below the real +comparison boundary. If an internal cleanup observation seam is unavoidable, +keep it minimal/internal, explain it and exercise actual public workflow with +the same code; no public test controls or mocked comparison coordinator. +Use barriers/schedulers, not arbitrary sleeps or polling for a lucky race. +Release callbacks/barriers/resources in finally so failed assertions drain. + +Then verify C-01..C-04 for normal success, operation failure and cancellation: +Busy identity/call counts, old drain/Close pending state, once-only cleanup, +failure/result/save retention, legitimate retry after full drain and fresh page. +An existing pre-work held HTTP test alone does not prove the corrected tail. +Do not weaken source/target/model/archive checks to make lifetime tests pass. + +Run the smallest combined focused selectors covering comparison/new owner cases, +then sequentially: + +- `dotnet build .\src\AutoForge.slnx --configuration Release --no-restore` +- `dotnet test .\src\AutoForge.slnx --configuration Release --no-build` + +All nine projects/645 cases plus new cases must pass. Restore only after a +genuine chosen-command missing dependency; no SDK/workload/runtime/model update. +Recheck actual native affected Evaluate/Busy/Cancel/Close/unmount/fresh-entry +behavior on the rebuilt Client, including one legitimate completed comparison/ +save/report. Retain exact numerical/proof/capture semantics and old manual/ +unit/source behaviors. Controlled tail barriers are not native timing claims. +Existing real-model content in the original report remains historical evidence, +not a newly executed LLM response; no repeated inference claimed unless run. + +The original report's intermittent unchanged dynamics-owner TaskCanceledException +and known recording-navigation Busy issue are separate disclosures. Do not +change those unrelated files or infer causality from one flaky run. If a new +failure is causally tied to this correction, investigate and fix that cause; +otherwise report exact evidence honestly without waiver or unrelated cleanup. + +## DOCUMENTATION REQUIREMENTS + +Update directly related comparison ownership/drain/error and build/native +evidence docs/README. Step-13/13A remain pending manual acceptance. +Original 645-case/model/numerical/archive report remains unchanged. +No hardware or new milestone claims. + +## ACCEPTANCE CRITERIA + +| ID | Criterion | Evidence | +|---|---|---| +| AC-01 | Actual pre-fix R-01 failure and C-01 corrected competing-admission behavior | Executed real comparison boundary with held release tail, exact old drain and zero new side-effect calls | +| AC-02 | C-02/C-03 safe shared Close/Dispose and atomic terminal completion | Controlled callback/release/drain interleavings, once-only cleanup, truthful success/error/cancel/save evidence and no deadlock | +| AC-03 | C-04 no premature/late owner publication and valid post-drain retry | Same old task/owner reservation until completion, legitimate new work afterwards, independent closed/fresh lifetimes | +| AC-04 | C-05 original 645 regressions/new cases/nine-project Release and affected native paths pass | Exact commands/exits/warnings/counts and native-versus-controlled distinction; original data/authority invariants retained | +| AC-05 | Shared gate, source-path self-review, docs/history/manual stop | Producer/consumer review after green and precise `prompts/Step-13A-Report.md` | + +## OUT-OF-SCOPE WORK + +Projection/physics/schema/metrics redesign, additional comparison capabilities, +model/runtime setup or policy questions, new owner framework, hardware, +recording navigation fixes, unproven unrelated dynamics fixes, packages/hosts/ +projects, staging/commit/push, agents/automatic handoff or Step-14. + +## REPORTING REQUIREMENTS + +Save `prompts/Step-13A-Report.md` entirely inside exactly one plain triple-backtick +fence, no language tag/outside text/nested fences. +Include Step/title/status/R-01; actual pre-fix failure; C-01..C-05/AC-01..AC-05 +named executed evidence; owner/release/drain/Busy/Close/callback order and +pending/completed state; primary errors and partial-save preservation; files/ +interfaces; exact commands/exits/warnings/per-project counts; native versus +controlled outcomes; post-green source-path self-review; docs, deviations/ +blockers, preservation and exact task-only cleanup. +COMPLETE requires all assigned checks; otherwise INCOMPLETE/BLOCKED precisely. +Save and STOP for manual orchestrator review. No old report rewrite or Step-14. diff --git a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationAuthoringService.cs b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationAuthoringService.cs index 15f7a07..5f3a6be 100644 --- a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationAuthoringService.cs +++ b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationAuthoringService.cs @@ -179,9 +179,7 @@ public sealed class CalibrationAuthoringService(IDynamicsConfigurationService co new(SyntheticEngineCalibration.ScalarIdentifier, "rpm", configuration.Model.IdleSpeedRpm, configuration.Model.MaximumEngineSpeedRpm), new(SyntheticEngineCalibration.TableIdentifier, "rpm", "N*m", configuration.Model.EngineTorqueCurve.Select(point => point.EngineSpeedRpm), 1, 1000)); - var values = new CalibrationValues([new(SyntheticEngineCalibration.ScalarIdentifier, "rpm", configuration.Model.WarmIdleTargetRpm)], - [new(SyntheticEngineCalibration.TableIdentifier, "N*m", "rpm", definition.Table.Axis, - configuration.Model.EngineTorqueCurve.Select(point => point.TorqueNewtonMeters))]); + var values = SyntheticCalibrationProjection.Read(configuration); try { definition.Validate(values); } catch (CalibrationAuthoringException exception) { diff --git a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonModels.cs b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonModels.cs new file mode 100644 index 0000000..9b23b13 --- /dev/null +++ b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonModels.cs @@ -0,0 +1,97 @@ +using System.Globalization; +using System.Text; +using AutoForge.Application.Features.Vehicles.Telemetry.Recording; +using AutoForge.Core.Simulation; + +namespace AutoForge.Application.Features.Vehicles.Calibration; + +public enum CalibrationComparisonState { Idle, Evaluating, Saving, Completed, Failed, Cancelled, Closing, Closed } +public enum CalibrationComparisonFailure { Busy, Closed, Proof, Source, Configuration, Signals, StepLimit, Incomplete, Save, Cancelled } +public sealed class CalibrationComparisonException(CalibrationComparisonFailure failure, string message, Exception? inner = null) + : Exception(message, inner) +{ + public CalibrationComparisonFailure Failure { get; } = failure; +} +public sealed record CalibrationComparisonSample(long Step, double TimeSeconds, DynamicsScenarioPhaseKind Phase, + double EngineRpm, double SpeedMetersPerSecond, double FuelKilograms, double DistanceMeters, double ShiftCount); +public sealed record CalibrationComparisonMetrics(int ModelSteps, double TimeSeconds, IReadOnlyList PhaseSteps, + int WarmSampleCount, double MaximumLastTenIdleErrorRpm, double MaximumLastTenIdleSpeedMetersPerSecond, + double AccelerationSeconds, double BrakingSeconds, int CruiseSampleCount, double SampledCruiseMinimumMetersPerSecond, + double SampledCruiseMaximumMetersPerSecond, double FuelConsumedKilograms, double DistanceMeters, double FinalShiftCount); +public sealed class CalibrationComparisonRun +{ + internal CalibrationComparisonRun(DynamicsConfiguration configuration, string provider, string runIdentifier, + IEnumerable samples, CalibrationComparisonMetrics? metrics, + TelemetryCaptureSnapshot capture, string? error) + { + Configuration = configuration; + ProviderIdentifier = provider; + RunIdentifier = runIdentifier; + Samples = Array.AsReadOnly(samples.ToArray()); + Metrics = metrics; + Capture = new(capture.Header, capture.Observations, capture.Completion, capture.IsSaved); + Error = error; + } + public DynamicsConfiguration Configuration { get; } + public string ProviderIdentifier { get; } + public string RunIdentifier { get; } + public IReadOnlyList Samples { get; } + public CalibrationComparisonMetrics? Metrics { get; } + public TelemetryCaptureSnapshot Capture { get; } + public string? Error { get; } + public bool IsComplete => Metrics is not null && Error is null && Capture.IsComplete; +} +public sealed class CalibrationComparisonResult +{ + private bool _retired; + internal CalibrationComparisonResult(CalibrationComparisonSource source, + CalibrationComparisonRun baseline, CalibrationComparisonRun derived) + { + Identifier = Guid.NewGuid().ToString("N"); + Source = source; + Baseline = baseline; + Derived = derived; + } + public string Identifier { get; } + public CalibrationComparisonSource Source { get; } + public CalibrationComparisonRun Baseline { get; } + public CalibrationComparisonRun Derived { get; } + public bool IsCurrent => !Volatile.Read(ref _retired) && Source.IsCurrent; + internal void Retire() => Volatile.Write(ref _retired, true); + public string Report + { + get + { + var report = new StringBuilder("SYNTHETIC CALIBRATION COMPARISON - not manufacturer prediction or physical authority\n"); + report.AppendLine($"Comparison {Identifier}; proof as of {Source.CapturedAt:O}; source {(IsCurrent ? "Current" : "Stale")}"); + report.AppendLine($"Workspace {Source.WorkspaceIdentifier}; head {Source.HeadIdentifier}; definition {Source.Definition.Applicability.DefinitionIdentifier}@{Source.Definition.Applicability.DefinitionRevision}"); + report.AppendLine($"Profile {Source.Original.ProfileIdentifier}@{Source.Original.ProfileRevision}; engine {Source.Original.EngineModuleIdentifier}; transmission {Source.Original.TransmissionModuleIdentifier}; model {Source.Definition.Applicability.ModelIdentifier}@{Source.Definition.Applicability.ModelVersion}; proof {Source.Identifier}; preparation {Source.PreparationIdentifier}."); + report.AppendLine($"Target {Source.VerifiedImage.Identity.InstanceIdentifier}; revision {Source.VerifiedImage.Revision}; Apply {Source.OperationIdentifier}; approval {Source.ApprovalIdentifier}; backup {Source.BackupIdentifier}; {Source.Verification}"); + report.AppendLine("Only warm-idle target and torque cells are projected; all other configuration/initial/scenario/reference values remain equivalent."); + for (var index = 0; index < Baseline.Configuration.Model.EngineTorqueCurve.Count; index++) + { + var before = Baseline.Configuration.Model.EngineTorqueCurve[index]; + var after = Derived.Configuration.Model.EngineTorqueCurve[index]; + report.AppendLine($"Torque axis {Exact(before.EngineSpeedRpm)} rpm: {Exact(before.TorqueNewtonMeters)} -> {Exact(after.TorqueNewtonMeters)} N*m."); + } + Append(report, "Baseline", Baseline); + Append(report, "Derived", Derived); + report.AppendLine($"Fuel delta derived-baseline {Exact(Derived.Metrics!.FuelConsumedKilograms - Baseline.Metrics!.FuelConsumedKilograms)} kg; distance delta {Exact(Derived.Metrics.DistanceMeters - Baseline.Metrics.DistanceMeters)} m; shift-count delta {Exact(Derived.Metrics.FinalShiftCount - Baseline.Metrics.FinalShiftCount)}."); + report.AppendLine("Signals sampled every five completed steps plus completion, not continuous extrema or a complete shift timeline. Replay reads archives, not physics."); + return report.ToString(); + } + } + private static void Append(StringBuilder report, string label, CalibrationComparisonRun run) + { + var metrics = run.Metrics!; + report.AppendLine($"{label}: config {run.Configuration.ConfigurationIdentifier}; provider {run.ProviderIdentifier}; run {run.RunIdentifier}; capture {run.Capture.Header.CaptureId}; capture {run.Capture.Completion!.State}; observations {run.Capture.Observations.Count}"); + report.AppendLine($"Idle target {Exact(run.Configuration.Model.WarmIdleTargetRpm)} rpm; last ten warm samples max error {Exact(metrics.MaximumLastTenIdleErrorRpm)} rpm; max stationary speed {Exact(metrics.MaximumLastTenIdleSpeedMetersPerSecond)} m/s."); + report.AppendLine($"Model steps {metrics.ModelSteps}; total {Exact(metrics.TimeSeconds)} s; phase step counts {string.Join(",", metrics.PhaseSteps)}; acceleration {Exact(metrics.AccelerationSeconds)} s; brake {Exact(metrics.BrakingSeconds)} s."); + report.AppendLine($"Sampled cruise count {metrics.CruiseSampleCount}; speed range {Exact(metrics.SampledCruiseMinimumMetersPerSecond)}..{Exact(metrics.SampledCruiseMaximumMetersPerSecond)} m/s; fuel {Exact(metrics.FuelConsumedKilograms)} kg; distance {Exact(metrics.DistanceMeters)} m; final measured shift count {Exact(metrics.FinalShiftCount)}."); + } + private static string Exact(double value) => value.ToString("R", CultureInfo.InvariantCulture); +} +public sealed record CalibrationComparisonSaveOutcome(string CaptureIdentifier, bool Persisted, bool ReadbackVerified, string? Error); +public sealed record CalibrationComparisonSnapshot(CalibrationComparisonState State, bool IsBusy, + CalibrationComparisonResult? Result, CalibrationComparisonRun? PartialRun, + IReadOnlyList Saves, string? Error); diff --git a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs new file mode 100644 index 0000000..1b796d8 --- /dev/null +++ b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSession.cs @@ -0,0 +1,378 @@ +using AutoForge.Application.Features.Vehicles.Diagnostics; +using AutoForge.Application.Features.Vehicles.Simulation; +using AutoForge.Application.Features.Vehicles.Telemetry.Recording; +using AutoForge.Core.ExecutionTargets; +using AutoForge.Core.Simulation; +using AutoForge.Core.Calibration; +using AutoForge.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace AutoForge.Application.Features.Vehicles.Calibration; + +public sealed class CalibrationComparisonFactory(IDiagnosticSessionService diagnostics, + IDynamicsConfigurationService configurations, ITelemetryArchiveStore archives, TimeProvider clock) +{ + public CalibrationComparisonSession Create(CalibrationRehearsalSession rehearsal) => + new(rehearsal, diagnostics, configurations, archives, clock); +} +public static class CalibrationComparisonServiceCollectionExtensions +{ + public static IServiceCollection AddAutoForgeCalibrationComparison(this IServiceCollection services) + { + services.TryAddSingleton(); + return services; + } +} + +public sealed class CalibrationComparisonSession(CalibrationRehearsalSession rehearsal, + IDiagnosticSessionService diagnostics, IDynamicsConfigurationService configurations, + ITelemetryArchiveStore archives, TimeProvider clock) : IAsyncDisposable +{ + public const int MaximumModelSteps = 3900; + public const int SignalCadenceSteps = 5; + private readonly object _gate = new(); + private CalibrationComparisonSnapshot _snapshot = new(CalibrationComparisonState.Idle, false, null, null, [], null); + private Owner? _owner; + private Task? _drain; + private Task? _close; + private long _generation; + public event Action? Changed; + public IReadOnlyList Providers => diagnostics.Providers; + public CalibrationComparisonSnapshot Snapshot { get { lock (_gate) return _snapshot; } } + + public Task EvaluateAsync(string mutationOperationIdentifier, string workspaceIdentifier, + string headIdentifier, string providerIdentifier, CancellationToken cancellationToken = default) => + Start(CalibrationComparisonState.Evaluating, async (generation, token) => + { + if (!diagnostics.Providers.Any(item => item.Target.Kind == ExecutionTargetKind.Simulation && + item.Target.ProviderIdentifier == providerIdentifier)) + throw Failure(CalibrationComparisonFailure.Configuration, "Explicitly select a registered Simulation dynamics/v2 provider."); + var source = await CaptureSourceAsync(mutationOperationIdentifier, workspaceIdentifier, headIdentifier, token).ConfigureAwait(false); + DynamicsConfiguration derived; + try + { + derived = source.Owner.AtComparisonSource(source, () => + configurations.RegisterProjection(SyntheticCalibrationProjection.Create( + source.Original, source.Definition, source.VerifiedImage.Values))); + } + catch (Exception exception) when (exception is CalibrationAuthoringException or CalibrationRehearsalException or DynamicsConfigurationException) + { + throw Failure(CalibrationComparisonFailure.Proof, $"Verified source projection rejected: {exception.Message}", exception); + } + var baseline = await RunAsync(source.Original, providerIdentifier, generation, token).ConfigureAwait(false); + EnsureSource(source); + var candidate = await RunAsync(derived, providerIdentifier, generation, token).ConfigureAwait(false); + EnsureSource(source); + var confirmed = await CaptureSourceAsync(source.OperationIdentifier, source.WorkspaceIdentifier, source.HeadIdentifier, token).ConfigureAwait(false); + if (confirmed.Generation != source.Generation || + confirmed.VerifiedImage.Revision != source.VerifiedImage.Revision || + !confirmed.VerifiedImage.Identity.Matches(source.VerifiedImage.Identity) || + !confirmed.VerifiedImage.Values.ContentEquals(source.VerifiedImage.Values) || + confirmed.BackupIdentifier != source.BackupIdentifier) + throw Failure(CalibrationComparisonFailure.Proof, "Fresh final owning readback differs from the admitted historical source; no current comparison published."); + if (!baseline.IsComplete || !candidate.IsComplete || + baseline.RunIdentifier == candidate.RunIdentifier || + baseline.Capture.Header.CaptureId == candidate.Capture.Header.CaptureId) + throw Failure(CalibrationComparisonFailure.Incomplete, "Both independent equivalent runs and captures must be complete with distinct lineage."); + var result = new CalibrationComparisonResult(source, baseline, candidate); + source.Owner.AtComparisonSource(source, () => + { + lock (_gate) + { + EnsureOwner(generation, token); + _snapshot = _snapshot with { Result = result, PartialRun = null, Saves = [], Error = null }; + } + return true; + }); + }, cancellationToken); + + private async Task CaptureSourceAsync(string operation, string workspace, string head, CancellationToken token) + { + try { return await rehearsal.CaptureComparisonSourceAsync(rehearsal.NewOperationId(), operation, workspace, head, token).ConfigureAwait(false); } + catch (Exception exception) when (exception is CalibrationAuthoringException or CalibrationRehearsalException or IOException) + { + token.ThrowIfCancellationRequested(); + throw Failure(CalibrationComparisonFailure.Proof, $"Owning comparison proof rejected: {exception.Message}", exception); + } + } + + private async Task RunAsync(DynamicsConfiguration configuration, + string provider, long generation, CancellationToken token) + { + DiagnosticSession? session = null; + SessionTelemetryCapture? capture = null; + var samples = new List(); + var phases = new int[5]; + string? error = null; + CalibrationComparisonMetrics? metrics = null; + try + { + session = await diagnostics.OpenDynamicsAsync(configuration.ProfileIdentifier, + configuration.ProfileRevision, provider, ExecutionTargetKind.Simulation, configuration, token).ConfigureAwait(false); + if (session.ObservationProtocolVersion != 2 || session.DynamicsControl is null || + session.Identity.DynamicsConfigurationIdentifier != configuration.ConfigurationIdentifier || + session.Identity.Target.ProviderIdentifier != provider || session.Identity.Target.Kind != ExecutionTargetKind.Simulation) + throw Failure(CalibrationComparisonFailure.Configuration, "Actual provider did not return the requested fresh Simulation/v2 run."); + var control = session.DynamicsControl; + var status = control.Status; + if (status.StepIndex != 0 || status.VirtualTimeSeconds != 0 || + status.ConfigurationIdentifier != configuration.ConfigurationIdentifier || + status.RunIdentifier != session.Identity.DynamicsRunIdentifier || + status.State != DynamicsLifecycleState.Stopped) + throw Failure(CalibrationComparisonFailure.Configuration, "A comparison must start a fresh exact configuration and initial state, not reuse a running model."); + capture = SessionTelemetryCapture.Start(session, clock); + while (status.State != DynamicsLifecycleState.Completed) + { + token.ThrowIfCancellationRequested(); + if (status.State is DynamicsLifecycleState.Faulted or DynamicsLifecycleState.Closed) + throw Failure(CalibrationComparisonFailure.Incomplete, $"Model did not complete: {status.Failure ?? status.State.ToString()}"); + if (status.StepIndex >= MaximumModelSteps) + throw Failure(CalibrationComparisonFailure.StepLimit, "Model requires step 3901; fixed 3900-step comparison limit reached."); + if (status.ScenarioPhaseIndex is < 0 or > 4) + throw Failure(CalibrationComparisonFailure.Configuration, "Invalid model phase context."); + var phase = configuration.Scenario[status.ScenarioPhaseIndex].Kind; + var beforeStep = status.StepIndex; + status = await control.StepAsync(1, token).ConfigureAwait(false); + if (status.StepIndex != beforeStep + 1 || + status.ConfigurationIdentifier != configuration.ConfigurationIdentifier || + status.RunIdentifier != session.Identity.DynamicsRunIdentifier || + !double.IsFinite(status.VirtualTimeSeconds)) + throw Failure(CalibrationComparisonFailure.Signals, "Step control returned incoherent exact run/configuration/step/time context."); + phases[(int)phase]++; + if (status.StepIndex % SignalCadenceSteps != 0 && status.State != DynamicsLifecycleState.Completed) continue; + var engine = session.ExpectedEndpoints.Single(item => + StringComparer.OrdinalIgnoreCase.Equals(item.ModuleIdentifier, configuration.EngineModuleIdentifier)); + var transmission = session.ExpectedEndpoints.Single(item => + StringComparer.OrdinalIgnoreCase.Equals(item.ModuleIdentifier, configuration.TransmissionModuleIdentifier)); + var engineReply = await session.ReadSignalsAsync(engine, token).ConfigureAwait(false); + var transmissionReply = await session.ReadSignalsAsync(transmission, token).ConfigureAwait(false); + token.ThrowIfCancellationRequested(); + var engineSignals = ValidateReply(engineReply, engine.ModuleIdentifier, status); + var transmissionSignals = ValidateReply(transmissionReply, transmission.ModuleIdentifier, status); + samples.Add(new(status.StepIndex, status.VirtualTimeSeconds, phase, + Number(engineSignals, "engine.speed", "rpm"), Number(engineSignals, "vehicle.speed", "m/s"), + Number(engineSignals, "engine.fuel-consumed", "kg"), Number(engineSignals, "vehicle.distance", "m"), + Number(transmissionSignals, "transmission.shift-count", "count"))); + if (!capture.IsAccepting) + throw Failure(CalibrationComparisonFailure.Incomplete, capture.Snapshot.Completion?.Error ?? "Capture reached an existing archive limit."); + } + var warm = samples.Where(item => item.Phase == DynamicsScenarioPhaseKind.WarmIdle).ToArray(); + var cruise = samples.Where(item => item.Phase == DynamicsScenarioPhaseKind.Cruise).ToArray(); + if (warm.Length < 10 || cruise.Length == 0 || samples.Count == 0) + throw Failure(CalibrationComparisonFailure.Incomplete, "At least ten valid warm samples and a complete sampled cruise are required; no shortened passing window."); + var lastWarm = warm.TakeLast(10).ToArray(); + var idleError = lastWarm.Max(item => Math.Abs(item.EngineRpm - configuration.Model.WarmIdleTargetRpm)); + var idleSpeed = lastWarm.Max(item => Math.Abs(item.SpeedMetersPerSecond)); + if (idleError > 1 || idleSpeed > 0.001) + throw Failure(CalibrationComparisonFailure.Incomplete, "Actual last-ten warm-idle samples did not meet 1 rpm and 0.001 m/s stationary thresholds."); + var final = samples[^1]; + var fuel = final.FuelKilograms - configuration.InitialConditions.FuelConsumedKilograms; + var distance = final.DistanceMeters - configuration.InitialConditions.DistanceMeters; + if (fuel < 0 || distance < 0 || final.ShiftCount < 0 || final.ShiftCount != Math.Truncate(final.ShiftCount)) + throw Failure(CalibrationComparisonFailure.Signals, "Fuel/distance/count observations violate measured cumulative domains."); + metrics = new((int)status.StepIndex, status.VirtualTimeSeconds, Array.AsReadOnly(phases.ToArray()), + warm.Length, idleError, idleSpeed, phases[1] * configuration.Model.StepSeconds, + phases[3] * configuration.Model.StepSeconds, cruise.Length, + cruise.Min(item => item.SpeedMetersPerSecond), cruise.Max(item => item.SpeedMetersPerSecond), + fuel, distance, final.ShiftCount); + } + catch (Exception exception) when (exception is CalibrationComparisonException or DiagnosticSessionException or DiagnosticProviderException or OperationCanceledException or InvalidOperationException or ArgumentException) + { + error = exception.Message; + if (exception is CalibrationComparisonException or OperationCanceledException) throw; + throw Failure(exception is DiagnosticSessionException or DiagnosticProviderException + ? CalibrationComparisonFailure.Configuration : CalibrationComparisonFailure.Signals, exception.Message, exception); + } + finally + { + try + { + if (capture is not null) + { + capture.Stop(); + var snapshot = capture.Snapshot; + var partial = new CalibrationComparisonRun(configuration, provider, + session!.Identity.DynamicsRunIdentifier!, samples, metrics, snapshot, error); + lock (_gate) + { + if (generation == _generation) _snapshot = _snapshot with { PartialRun = partial }; + } + } + } + finally { if (session is not null) await session.DisposeAsync().ConfigureAwait(false); } + } + var captured = capture!.Snapshot; + if (!captured.IsComplete || captured.Observations.Count != samples.Count * 2 || + captured.Observations.Count > 1562) + throw Failure(CalibrationComparisonFailure.Incomplete, "Actual complete capture does not match the bounded scheduled engine/transmission observations."); + return new(configuration, provider, session!.Identity.DynamicsRunIdentifier!, samples, metrics, captured, null); + } + + private static SignalSnapshot ValidateReply(DiagnosticOperationResult reply, + string module, DynamicsRunStatus status) + { + var snapshot = reply.Value; + if (!reply.IsSuccess || snapshot is null || + !StringComparer.OrdinalIgnoreCase.Equals(snapshot.ModuleIdentifier, module) || + snapshot.ModelStepIndex != status.StepIndex || snapshot.ModelTimeSeconds != status.VirtualTimeSeconds || + snapshot.ModelConfigurationIdentifier != status.ConfigurationIdentifier || snapshot.ModelRunIdentifier != status.RunIdentifier) + throw Failure(CalibrationComparisonFailure.Signals, $"Actual {module} signal reply is missing/non-Good or has wrong exact model context: {reply.Error}"); + return snapshot; + } + private static double Number(SignalSnapshot snapshot, string identifier, string unit) + { + var matches = snapshot.Signals.Where(item => item.SignalIdentifier == identifier).ToArray(); + if (matches.Length != 1 || matches[0].Quality != SignalQuality.Good || matches[0].Unit != unit || + matches[0].ModelStepIndex != snapshot.ModelStepIndex || matches[0].ModelTimeSeconds != snapshot.ModelTimeSeconds || + matches[0].ModelConfigurationIdentifier != snapshot.ModelConfigurationIdentifier || + matches[0].ModelRunIdentifier != snapshot.ModelRunIdentifier || + matches[0].ModuleIdentifier != snapshot.ModuleIdentifier || + matches[0].Value.ValueKind != System.Text.Json.JsonValueKind.Number || + !matches[0].Value.TryGetDouble(out var value) || !double.IsFinite(value)) + throw Failure(CalibrationComparisonFailure.Signals, $"Required canonical {identifier}/{unit} is missing, uncertain, nonfinite or has wrong context; no zero fallback."); + return value; + } + + public Task SaveCapturesAsync(CancellationToken cancellationToken = default) => + Start(CalibrationComparisonState.Saving, async (generation, token) => + { + var result = Snapshot.Result ?? throw Failure(CalibrationComparisonFailure.Save, "A complete comparison is required before explicit pair save."); + foreach (var capture in new[] { result.Baseline.Capture, result.Derived.Capture }) + { + if (Snapshot.Saves.Any(item => item.CaptureIdentifier == capture.Header.CaptureId && item.ReadbackVerified)) continue; + var previous = Snapshot.Saves.SingleOrDefault(item => item.CaptureIdentifier == capture.Header.CaptureId); + var persisted = previous?.Persisted ?? false; + string? error = null; + var verified = false; + try + { + token.ThrowIfCancellationRequested(); + if (!persisted && previous is not null) + { + // A failed publication/cleanup may still have left the final no-overwrite file. + persisted = (await archives.ListAsync(token).ConfigureAwait(false)) + .Any(item => item.CaptureId == capture.Header.CaptureId); + } + if (!persisted) { await archives.SaveAsync(capture, token).ConfigureAwait(false); persisted = true; } + var readback = await archives.LoadAsync(capture.Header.CaptureId, token).ConfigureAwait(false); + if (!TelemetryArchiveCodec.Serialize(capture).SequenceEqual(TelemetryArchiveCodec.Serialize(readback))) + throw Failure(CalibrationComparisonFailure.Save, "Saved archive readback differs from the exact original capture/config/run/source."); + verified = true; + } + catch (Exception exception) when (exception is TelemetryArchiveException or CalibrationComparisonException or IOException or OperationCanceledException) + { + error = exception.Message; + } + lock (_gate) + { + EnsureOwner(generation, token, allowCancelled: true); + var outcome = new CalibrationComparisonSaveOutcome(capture.Header.CaptureId, persisted, verified, error); + _snapshot = _snapshot with { Saves = Array.AsReadOnly(_snapshot.Saves + .Where(item => item.CaptureIdentifier != outcome.CaptureIdentifier).Append(outcome).ToArray()) }; + } + Changed?.Invoke(); + if (error is not null) + throw Failure(CalibrationComparisonFailure.Save, $"Partial pair save: {capture.Header.CaptureId}: {error}; successful files retained, retry only missing work."); + } + }, cancellationToken); + + private Task Start(CalibrationComparisonState state, Func work, + CancellationToken external) + { + Owner owner; + long generation; + TaskCompletionSource completion; + lock (_gate) + { + if (_snapshot.State is CalibrationComparisonState.Closed or CalibrationComparisonState.Closing) + throw Failure(CalibrationComparisonFailure.Closed, "This page comparison owner is closed."); + if (_owner is not null) throw Failure(CalibrationComparisonFailure.Busy, "One comparison/run/save owner is active; cancel and drain first."); + external.ThrowIfCancellationRequested(); + owner = new(external); + _owner = owner; + generation = ++_generation; + completion = new(TaskCreationOptions.RunContinuationsAsynchronously); + _drain = completion.Task; + if (state == CalibrationComparisonState.Evaluating) _snapshot.Result?.Retire(); + _snapshot = _snapshot with { State = state, IsBusy = true, Error = null }; + } + Changed?.Invoke(); + _ = ObserveAsync(); + return completion.Task; + + async Task ObserveAsync() + { + Exception? error = null; + try { await Task.Run(() => work(generation, owner.Token)).ConfigureAwait(false); } + catch (Exception exception) { error = exception; } + lock (_gate) + { + _owner = null; + if (_snapshot.State != CalibrationComparisonState.Closing) + _snapshot = _snapshot with { State = error is null ? CalibrationComparisonState.Completed : + error is OperationCanceledException ? CalibrationComparisonState.Cancelled : CalibrationComparisonState.Failed, + IsBusy = false, Error = error?.Message }; + } + owner.Release(); + if (error is null) completion.TrySetResult(); else completion.TrySetException(error); + Changed?.Invoke(); + } + } + private void EnsureOwner(long generation, CancellationToken token, bool allowCancelled = false) + { + if (!allowCancelled) token.ThrowIfCancellationRequested(); + if (generation != _generation || (!allowCancelled && + _snapshot.State is CalibrationComparisonState.Closing or CalibrationComparisonState.Closed)) + throw Failure(CalibrationComparisonFailure.Source, "Comparison lifetime was superseded/closed; no late current result."); + } + private static void EnsureSource(CalibrationComparisonSource source) => source.Owner.AtComparisonSource(source, () => true); + public Task CancelAsync() + { + Owner? owner; + Task drain; + lock (_gate) { owner = _owner; drain = _drain ?? Task.CompletedTask; } + owner?.Cancel(); + return drain; + } + public Task CloseAsync() + { + Owner? owner; + Task? drain; + TaskCompletionSource completion; + lock (_gate) + { + if (_close is not null) return _close; + _snapshot = _snapshot with { State = CalibrationComparisonState.Closing }; + _snapshot.Result?.Retire(); + owner = _owner; + drain = _drain; + completion = new(TaskCreationOptions.RunContinuationsAsynchronously); + _close = completion.Task; + } + _ = CloseCoreAsync(owner, drain, completion); + return completion.Task; + } + private async Task CloseCoreAsync(Owner? owner, Task? drain, TaskCompletionSource completion) + { + Exception? error = null; + try { owner?.Cancel(); } + catch (Exception exception) { error = exception; } + try { if (drain is not null) await drain.ConfigureAwait(false); } + catch (Exception exception) { error = error is null ? exception : new AggregateException(error, exception); } + lock (_gate) _snapshot = _snapshot with { State = CalibrationComparisonState.Closed, IsBusy = false, Error = error?.Message ?? _snapshot.Error }; + completion.TrySetResult(); + Changed?.Invoke(); + } + public async ValueTask DisposeAsync() { await CloseAsync().ConfigureAwait(false); Changed = null; } + private sealed class Owner(CancellationToken external) + { + private readonly object _gate = new(); + private readonly CancellationTokenSource _linked = CancellationTokenSource.CreateLinkedTokenSource(external); + private bool _released; + public CancellationToken Token => _linked.Token; + public void Cancel() { lock (_gate) { if (!_released) _linked.Cancel(); } } + public void Release() { lock (_gate) { _released = true; _linked.Dispose(); } } + } + private static CalibrationComparisonException Failure(CalibrationComparisonFailure kind, string message, Exception? inner = null) => new(kind, message, inner); +} diff --git a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSource.cs b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSource.cs new file mode 100644 index 0000000..cdb37ef --- /dev/null +++ b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationComparisonSource.cs @@ -0,0 +1,45 @@ +using AutoForge.Core.Calibration; +using AutoForge.Core.Simulation; +using AutoForge.Diagnostics; + +namespace AutoForge.Application.Features.Vehicles.Calibration; + +public sealed class CalibrationComparisonSource +{ + internal CalibrationComparisonSource(CalibrationRehearsalSession owner, long generation, + CalibrationWorkspace workspace, DynamicsConfiguration original, + VerifiedCalibrationBackup backup, CalibrationMutationResult mutation, DateTimeOffset capturedAt) + { + Owner = owner; + Generation = generation; + Identifier = Guid.NewGuid().ToString("N"); + WorkspaceIdentifier = workspace.Identifier; + HeadIdentifier = workspace.Head.Identifier; + Definition = workspace.Definition; + Original = original; + BackupIdentifier = backup.Identifier; + OriginalImage = new(backup.Image.Identity, backup.Image.Revision, backup.Image.Values); + VerifiedImage = new(mutation.Actual!.Identity, mutation.Actual.Revision, mutation.Actual.Values); + OperationIdentifier = mutation.OperationIdentifier; + ApprovalIdentifier = mutation.ApprovalIdentifier; + PreparationIdentifier = mutation.Preparation.Identifier; + Verification = mutation.Verification; + CapturedAt = capturedAt; + } + internal CalibrationRehearsalSession Owner { get; } + internal long Generation { get; } + public string Identifier { get; } + public string WorkspaceIdentifier { get; } + public string HeadIdentifier { get; } + public SyntheticEngineCalibration Definition { get; } + public DynamicsConfiguration Original { get; } + public string BackupIdentifier { get; } + public CalibrationTargetImage OriginalImage { get; } + public CalibrationTargetImage VerifiedImage { get; } + public string OperationIdentifier { get; } + public string ApprovalIdentifier { get; } + public string PreparationIdentifier { get; } + public RehearsalVerification Verification { get; } + public DateTimeOffset CapturedAt { get; } + public bool IsCurrent => Owner.IsComparisonSourceCurrent(this); +} diff --git a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationRehearsalSession.cs b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationRehearsalSession.cs index 9d02a3e..558e1d6 100644 --- a/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationRehearsalSession.cs +++ b/src/AutoForge.Application/Features/Vehicles/Calibration/CalibrationRehearsalSession.cs @@ -46,6 +46,76 @@ public sealed class CalibrationRehearsalSession( public IReadOnlyList Providers => Array.AsReadOnly(factories.Select(factory => factory.Descriptor).ToArray()); public CalibrationRehearsalSnapshot Snapshot { get { lock (_gate) return _snapshot; } } + public Task CaptureComparisonSourceAsync(string operationId, + string mutationOperationIdentifier, string workspaceIdentifier, string headIdentifier, + CancellationToken cancellationToken = default) => + Start(operationId, + $"comparison-source:{mutationOperationIdentifier}:{workspaceIdentifier}:{headIdentifier}", + cancellationToken, async owner => + { + var workspace = RequireHead(workspaceIdentifier, headIdentifier, proposed: true); + var backup = RequireBackup(); + var selected = Snapshot; + var selectedMutation = selected.Audit.SingleOrDefault(item => item.OperationIdentifier == mutationOperationIdentifier); + if (selected.State != RehearsalState.Ready || selectedMutation is null || + selectedMutation.Action != CalibrationMutationAction.Apply || + selectedMutation.Verification is not (RehearsalVerification.VerifiedApplied or RehearsalVerification.VerifiedAppliedAfterReconciliation) || + selectedMutation.Preparation.WorkspaceIdentifier != workspaceIdentifier || + selectedMutation.Preparation.HeadIdentifier != headIdentifier) + throw Reject(RehearsalFailure.InvalidImage, "Select actual current owning verified Apply evidence before a comparison read; caller success records are not authority."); + var original = configurations.Select(workspace.Definition.Applicability.ConfigurationIdentifier); + var actual = await ReadValidatedAsync(owner).ConfigureAwait(false); + return authoring.AtCurrentHead(workspaceIdentifier, headIdentifier, () => + { + lock (_gate) + { + EnsureCurrent(owner); + if (_snapshot.State != RehearsalState.Ready) + throw Reject(RehearsalFailure.RecoveryRequired, "Only a current verified Ready Apply image can admit comparison."); + var mutation = _snapshot.Audit.SingleOrDefault(item => item.OperationIdentifier == mutationOperationIdentifier); + if (mutation is null || mutation.Action != CalibrationMutationAction.Apply || + mutation.Verification is not (RehearsalVerification.VerifiedApplied or RehearsalVerification.VerifiedAppliedAfterReconciliation) || + mutation.Actual is null || !SameImage(actual, mutation.Actual) || + _snapshot.CurrentImage is null || !SameImage(actual, _snapshot.CurrentImage) || + mutation.Preparation.WorkspaceIdentifier != workspaceIdentifier || + mutation.Preparation.HeadIdentifier != headIdentifier || + mutation.Preparation.BackupIdentifier != backup.Identifier || + !workspace.Head.Values.ContentEquals(actual.Values) || + !mutation.Preparation.Candidate.ContentEquals(actual.Values) || + !SyntheticCalibrationProjection.Read(original).ContentEquals(backup.Image.Values)) + throw Reject(RehearsalFailure.InvalidImage, + "Fresh full readback, owning verified Apply, original backup and exact current saved candidate/configuration must agree."); + return new CalibrationComparisonSource(this, _generation, workspace, original, backup, mutation, clock.GetUtcNow()); + } + }); + }); + + internal T AtComparisonSource(CalibrationComparisonSource source, Func action) => + authoring.AtCurrentHead(source.WorkspaceIdentifier, source.HeadIdentifier, () => + { + lock (_gate) + { + if (!ReferenceEquals(source.Owner, this) || source.Generation != _generation || + _disposed || _owner is not null || _snapshot.State != RehearsalState.Ready || + _snapshot.CurrentImage is null || !SameImage(_snapshot.CurrentImage, source.VerifiedImage) || + _snapshot.Backup?.Identifier != source.BackupIdentifier || + !_snapshot.Audit.Any(item => item.OperationIdentifier == source.OperationIdentifier && + item.ApprovalIdentifier == source.ApprovalIdentifier && item.Actual is not null && + SameImage(item.Actual, source.VerifiedImage) && item.Verification == source.Verification)) + throw new CalibrationRehearsalException(RehearsalFailure.StaleImage, "Owning target/source/proof was superseded, became busy/recovery/closed or changed revision/image."); + return action(); + } + }); + + internal bool IsComparisonSourceCurrent(CalibrationComparisonSource source) + { + try { return AtComparisonSource(source, () => true); } + catch (Exception exception) when (exception is CalibrationRehearsalException or CalibrationAuthoringException) + { + return false; + } + } + public string NewOperationId() { lock (_gate) diff --git a/src/AutoForge.Application/Features/Vehicles/Runbooks/RunbookPreviewService.cs b/src/AutoForge.Application/Features/Vehicles/Runbooks/RunbookPreviewService.cs index dd74750..4c5318d 100644 --- a/src/AutoForge.Application/Features/Vehicles/Runbooks/RunbookPreviewService.cs +++ b/src/AutoForge.Application/Features/Vehicles/Runbooks/RunbookPreviewService.cs @@ -54,6 +54,7 @@ public sealed class RunbookPreview public RunbookChangeKind Kind { get; } } public sealed record RunbookPreviewSnapshot(RunbookPreview? Preview, RunbookPreviewFreshness Freshness); +public sealed record RunbookDraftHandoff(string PreviewIdentifier, CalibrationWorkspace Workspace, CalibrationValues Values); public sealed class RunbookPreviewService(CalibrationAuthoringService authoring) { @@ -82,6 +83,26 @@ public sealed class RunbookPreviewService(CalibrationAuthoringService authoring) public RunbookPreview Validate(string json) => ValidateCore(json, null, null, default); + public RunbookDraftHandoff CopyCurrentCandidateToDraft( + string previewIdentifier, string workspaceIdentifier, string headIdentifier) + { + return authoring.AtCurrentHead(workspaceIdentifier, headIdentifier, () => + { + lock (_gate) + { + var preview = _preview; + var workspace = authoring.Snapshot!; + if (preview is null || preview.Identifier != previewIdentifier) + throw Error(RunbookPreviewFailure.PreviewSuperseded, "$", "Choose the exact current successful preview; it was replaced or is absent."); + if (!MatchesSource(workspace, preview.Source) || !workspace.Head.Values.ContentEquals(preview.Before)) + throw Error(RunbookPreviewFailure.StaleSource, "$.steps[0].source", "Preview/draft source and saved head must still match exactly; no rebasing is allowed."); + workspace.Definition.Validate(preview.Candidate); + return new RunbookDraftHandoff(preview.Identifier, workspace, + new CalibrationValues(preview.Candidate.Scalars, preview.Candidate.Tables)); + } + }); + } + internal RunbookPreview ValidateGenerated(string json, RunbookSourceToken expectedSource, string? expectedPreviewIdentifier, CancellationToken cancellationToken) => ValidateCore(json, expectedSource, expectedPreviewIdentifier, cancellationToken); diff --git a/src/AutoForge.Application/Features/Vehicles/Simulation/DynamicsConfigurationService.cs b/src/AutoForge.Application/Features/Vehicles/Simulation/DynamicsConfigurationService.cs index a7d3886..76b4ddf 100644 --- a/src/AutoForge.Application/Features/Vehicles/Simulation/DynamicsConfigurationService.cs +++ b/src/AutoForge.Application/Features/Vehicles/Simulation/DynamicsConfigurationService.cs @@ -21,6 +21,7 @@ public interface IDynamicsConfigurationService { DynamicsConfiguration CreateExample(string profileIdentifier, int profileRevision); DynamicsConfiguration Import(string json); + DynamicsConfiguration RegisterProjection(DynamicsConfiguration configuration); IReadOnlyList List(); DynamicsConfiguration Select(string configurationIdentifier); string Export(string configurationIdentifier); @@ -52,6 +53,18 @@ public sealed class DynamicsConfigurationService : IDynamicsConfigurationService return configuration; } + public DynamicsConfiguration RegisterProjection(DynamicsConfiguration configuration) + { + ArgumentNullException.ThrowIfNull(configuration); + try { configuration.ValidateAgainst(_profiles.Select(configuration.ProfileIdentifier, configuration.ProfileRevision)); } + catch (Exception exception) when (exception is ArgumentException or VehicleProfileCatalogException) + { + throw new DynamicsConfigurationException($"Projected configuration reference is invalid: {exception.Message}", exception); + } + Register(configuration); + return configuration; + } + public DynamicsConfiguration Import(string json) { ArgumentNullException.ThrowIfNull(json); diff --git a/src/AutoForge.Core/Calibration/SyntheticCalibrationProjection.cs b/src/AutoForge.Core/Calibration/SyntheticCalibrationProjection.cs new file mode 100644 index 0000000..c35d2d1 --- /dev/null +++ b/src/AutoForge.Core/Calibration/SyntheticCalibrationProjection.cs @@ -0,0 +1,50 @@ +using AutoForge.Core.Simulation; + +namespace AutoForge.Core.Calibration; + +public static class SyntheticCalibrationProjection +{ + public static CalibrationValues Read(DynamicsConfiguration configuration) + { + ArgumentNullException.ThrowIfNull(configuration); + return new( + [new(SyntheticEngineCalibration.ScalarIdentifier, "rpm", configuration.Model.WarmIdleTargetRpm)], + [new(SyntheticEngineCalibration.TableIdentifier, "N*m", "rpm", + configuration.Model.EngineTorqueCurve.Select(point => point.EngineSpeedRpm), + configuration.Model.EngineTorqueCurve.Select(point => point.TorqueNewtonMeters))]); + } + + public static DynamicsConfiguration Create(DynamicsConfiguration original, + SyntheticEngineCalibration definition, CalibrationValues values) + { + ArgumentNullException.ThrowIfNull(original); + ArgumentNullException.ThrowIfNull(definition); + definition.Validate(values); + var source = definition.Applicability; + if (source.ConfigurationIdentifier != original.ConfigurationIdentifier || + !StringComparer.OrdinalIgnoreCase.Equals(source.ProfileIdentifier, original.ProfileIdentifier) || + source.ProfileRevision != original.ProfileRevision || + !StringComparer.OrdinalIgnoreCase.Equals(source.ModuleIdentifier, original.EngineModuleIdentifier) || + source.ModelIdentifier != original.ModelIdentifier || source.ModelVersion != original.ModelVersion || + definition.Scalar.Minimum != original.Model.IdleSpeedRpm || + definition.Scalar.Maximum != original.Model.MaximumEngineSpeedRpm || + !definition.Table.Axis.SequenceEqual(original.Model.EngineTorqueCurve.Select(point => point.EngineSpeedRpm))) + throw new CalibrationAuthoringException(CalibrationFailure.SourceMismatch, + "Projection requires the exact original configuration and supported fixed-axis definition."); + var model = original.Model; + var projected = new DynamicsModelParameters(model.StepSeconds, model.VehicleMassKilograms, + model.WheelRadiusMeters, model.DragAreaSquareMeters, model.RollingResistanceCoefficient, + model.AirDensityKilogramsPerCubicMeter, model.GravityMetersPerSecondSquared, model.DrivetrainEfficiency, + model.MaximumBrakeForceNewtons, model.IdleSpeedRpm, model.MaximumEngineSpeedRpm, + values.Scalars[0].Value, model.LaunchSlipEndSpeedMetersPerSecond, + model.IdleFuelRateKilogramsPerSecond, model.FuelKilogramsPerJoule, model.AmbientTemperatureCelsius, + model.InitialEngineTemperatureCelsius, model.EquilibriumEngineTemperatureCelsius, + model.ThermalResponsePerSecond, + values.Tables[0].Axis.Select((axis, index) => new EngineTorquePoint(axis, values.Tables[0].Cells[index])).ToArray(), + model.ForwardGearRatios, model.UpshiftSpeedsRpm, model.DownshiftSpeedsRpm, model.FinalDriveRatio); + return new(original.FormatVersion, original.ProfileIdentifier, original.ProfileRevision, + original.VehicleIdentifier, original.EngineModuleIdentifier, original.EngineCalibrationReference, + original.TransmissionModuleIdentifier, original.TransmissionCalibrationReference, + original.ModelIdentifier, original.ModelVersion, projected, original.InitialConditions, original.Scenario); + } +} diff --git a/src/AutoForge.UI/Features/Foundation/AddAutoForgeUi.cs b/src/AutoForge.UI/Features/Foundation/AddAutoForgeUi.cs index 606c1c7..1891908 100644 --- a/src/AutoForge.UI/Features/Foundation/AddAutoForgeUi.cs +++ b/src/AutoForge.UI/Features/Foundation/AddAutoForgeUi.cs @@ -26,6 +26,7 @@ public static class ServiceCollectionExtensions services.AddAutoForgeTelemetryReplay(); services.AddAutoForgeCalibrationAuthoring(); services.AddAutoForgeCalibrationRehearsal(); + services.AddAutoForgeCalibrationComparison(); services.AddAutoForgeRunbookPreview(); services.AddAutoForgeLocalRunbookGeneration(); services.AddScoped(); diff --git a/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationComparisonPanel.razor b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationComparisonPanel.razor new file mode 100644 index 0000000..41d8761 --- /dev/null +++ b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationComparisonPanel.razor @@ -0,0 +1,101 @@ +@using AutoForge.Application.Features.Vehicles.Calibration +@using AutoForge.Core.ExecutionTargets +@implements IAsyncDisposable +@inject CalibrationComparisonFactory Factory +@inject CalibrationAuthoringService Authoring + +
+

Explicit verified-image comparison (NEW configuration, synthetic only)

+

This does not change an existing configuration/run or program a target. Select an actual owning verified Apply; approval and full readback remain separate evidence from observed model behavior.

+ @if (_session is { } session) + { + var snapshot = session.Snapshot; +

@snapshot.State; operation/drain pending @snapshot.IsBusy.

+ @if (_error is not null || snapshot.Error is not null) { } + + + + + + + + @if (snapshot.PartialRun is { } partial) + { +

Latest retained run @partial.RunIdentifier; config @partial.Configuration.ConfigurationIdentifier; capture @partial.Capture.Header.CaptureId; samples @partial.Samples.Count; @(partial.IsComplete ? "run complete; pair not yet published" : "partial/incomplete run - not a successful comparison"). @partial.Error

+ } + @if (snapshot.Result is { } result) + { +

Last successful equivalent-run result

+

@(result.IsCurrent ? "Current proof/source" : "Stale/as-of proof: target/source/head changed, closed or became busy; this result is not current").

+ + + +

Pair save is not atomic. Successful files remain; no overwrite or compensating deletion.

+
    + @foreach (var capture in new[] { result.Baseline.Capture, result.Derived.Capture }) + { + var saved = snapshot.Saves.SingleOrDefault(item => item.CaptureIdentifier == capture.Header.CaptureId); +
  • Capture @capture.Header.CaptureId; persisted @(saved?.Persisted ?? false); readback verified @(saved?.ReadbackVerified ?? false); @(saved?.Error ?? (saved?.ReadbackVerified == true ? "exact readback verified" : "not saved/verified yet")).
  • + } +
+

Inspect saved capture IDs with existing RecordedReplay; playback is archive evidence, not rerunning physics. Raw operations are in those captures.

+ } + } +
+ +@code { + [Parameter, EditorRequired] public CalibrationRehearsalSession RehearsalOwner { get; set; } = null!; + private CalibrationComparisonSession? _session; + private string _operation = string.Empty; + private string _provider = string.Empty; + private string? _error; + private bool _disposed; + protected override void OnInitialized() + { + _session = Factory.Create(RehearsalOwner); + _session.Changed += OnChanged; + RehearsalOwner.Changed += OnChanged; + Authoring.Changed += OnChanged; + } + private void Evaluate() + { + var source = Authoring.Snapshot; + if (source is null) { _error = "No current explicitly created authoring source exists."; return; } + var workspace = source.Identifier; + var head = source.Head.Identifier; + var operation = _operation; + var provider = _provider; + Begin(() => _session!.EvaluateAsync(operation, workspace, head, provider)); + } + private void Begin(Func action) => _ = ObserveAsync(action); + private async Task ObserveAsync(Func action) + { + try { await action(); _error = null; } + catch (Exception exception) when (exception is CalibrationComparisonException or CalibrationRehearsalException or AutoForge.Core.Calibration.CalibrationAuthoringException or OperationCanceledException) + { + if (!_disposed) _error = exception.Message; + } + catch (Exception exception) { await DispatchExceptionAsync(exception); } + finally { if (!_disposed) await InvokeAsync(() => { if (!_disposed) StateHasChanged(); }); } + } + private void OnChanged() { if (!_disposed) _ = InvokeAsync(() => { if (!_disposed) StateHasChanged(); }); } + public async ValueTask DisposeAsync() + { + _disposed = true; + RehearsalOwner.Changed -= OnChanged; + Authoring.Changed -= OnChanged; + if (_session is not null) { _session.Changed -= OnChanged; await _session.DisposeAsync(); } + } +} diff --git a/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationPage.razor b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationPage.razor index f537494..9c43a47 100644 --- a/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationPage.razor +++ b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationPage.razor @@ -2,9 +2,11 @@ @using AutoForge.Application.Features.Vehicles.Calibration @using AutoForge.Application.Features.Vehicles.Simulation @using AutoForge.Core.Calibration +@using AutoForge.Application.Features.Vehicles.Runbooks @implements IDisposable @inject CalibrationAuthoringService Authoring @inject IDynamicsConfigurationService Configurations +@inject RunbookPreviewService Previews Synthetic calibration authoring

Synthetic calibration authoring

@@ -105,6 +107,11 @@ + @if (Previews.Snapshot.Preview is { } preview) + { +

Read-only preview @preview.Identifier; @Previews.Snapshot.Freshness. Copying a candidate is not Save, approval or programming.

+ + } }
@@ -185,6 +192,7 @@ public DisplayUnitSelection DisplaySelection { get; set; } = DisplayUnitSelection.Default; private string _selectedConfiguration = string.Empty; private CalibrationWorkspace? _draftWorkspace; + private CalibrationValues? _draftValues; private DisplayUnitSelection _draftUnits = DisplayUnitSelection.Default; private string _scalarText = string.Empty; private string _originalScalarText = string.Empty; @@ -218,12 +226,13 @@ private void Save() => Run(() => { var draft = RequireDraft(); - var scalar = _scalarText == _originalScalarText ? draft.Head.Values.Scalars[0].Value : ParseFinite(_scalarText, "Warm idle"); + var canonical = _draftValues!; + var scalar = _scalarText == _originalScalarText ? canonical.Scalars[0].Value : ParseFinite(_scalarText, "Warm idle"); var cells = new double[_torqueText.Length]; for (var index = 0; index < cells.Length; index++) { cells[index] = _torqueText[index] == _originalTorqueText[index] - ? draft.Head.Values.Tables[0].Cells[index] + ? canonical.Tables[0].Cells[index] : DisplayUnitConverter.ConvertToSource(ParseFinite(_torqueText[index], $"Torque row {index + 1}"), "N*m", _draftUnits); } var values = new CalibrationValues([new(SyntheticEngineCalibration.ScalarIdentifier, "rpm", scalar)], @@ -243,6 +252,16 @@ _status = "Draft cancelled; latest head loaded explicitly."; }); + private void CopyPreview(string identifier) => Run(() => + { + var draft = RequireDraft(); + if (_selectedConfiguration != draft.Definition.Applicability.ConfigurationIdentifier) + throw new CalibrationAuthoringException(CalibrationFailure.SourceMismatch, "The selected configuration differs from this draft source."); + var handoff = Previews.CopyCurrentCandidateToDraft(identifier, draft.Identifier, draft.Head.Identifier); + LoadDraft(handoff.Workspace, handoff.Values, _draftUnits); + _status = $"Copied complete canonical preview {handoff.PreviewIdentifier} into this pinned draft. Save remains a separate explicit action."; + }); + private void Inspect() => Run(() => { var workspace = Authoring.Snapshot!; @@ -255,18 +274,22 @@ _diff = Authoring.Compare(workspace.Identifier, _fromIdentifier, _toIdentifier); }); - private void LoadDraft(CalibrationWorkspace workspace) + private void LoadDraft(CalibrationWorkspace workspace, CalibrationValues? candidate = null, DisplayUnitSelection? pinnedUnits = null) { - var scalarText = Exact(workspace.Head.Values.Scalars[0].Value); - var torqueText = workspace.Head.Values.Tables[0].Cells.Select(value => + var units = pinnedUnits ?? DisplaySelection; + var values = candidate ?? workspace.Head.Values; + workspace.Definition.Validate(values); + var scalarText = Exact(values.Scalars[0].Value); + var torqueText = values.Tables[0].Cells.Select(value => { - var projected = DisplayUnitConverter.Project(value, "N*m", DisplaySelection); + var projected = DisplayUnitConverter.Project(value, "N*m", units); if (!projected.IsAvailable) throw new DisplayUnitConversionException(projected.Error ?? "Torque display conversion unavailable."); return Exact(projected.Value!.Value); }).ToArray(); _draftWorkspace = workspace; + _draftValues = new(values.Scalars, values.Tables); _selectedConfiguration = workspace.Definition.Applicability.ConfigurationIdentifier; - _draftUnits = DisplaySelection; + _draftUnits = units; _scalarText = scalarText; _originalScalarText = scalarText; _torqueText = torqueText; @@ -285,7 +308,7 @@ private void Run(Action action) { try { action(); _error = null; } - catch (Exception exception) when (exception is CalibrationAuthoringException or DisplayUnitConversionException) + catch (Exception exception) when (exception is CalibrationAuthoringException or DisplayUnitConversionException or RunbookPreviewException) { _error = exception.Message; _status = null; diff --git a/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationRehearsalPanel.razor b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationRehearsalPanel.razor index 0031a7d..35300a3 100644 --- a/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationRehearsalPanel.razor +++ b/src/AutoForge.UI/Features/Vehicles/Calibration/CalibrationRehearsalPanel.razor @@ -142,6 +142,11 @@ } }
+ +@if (_showComparison && _session is not null) +{ + +} @code { [CascadingParameter] public DisplayUnitSelection DisplaySelection { get; set; } = DisplayUnitSelection.Default; @@ -154,6 +159,7 @@ private string? _error; private bool _confirm; private bool _disposed; + private bool _showComparison; protected override void OnInitialized() { diff --git a/src/tests/AutoForge.IntegrationTests/CalibrationAuthoringUiTests.cs b/src/tests/AutoForge.IntegrationTests/CalibrationAuthoringUiTests.cs index c5c76b2..8499f34 100644 --- a/src/tests/AutoForge.IntegrationTests/CalibrationAuthoringUiTests.cs +++ b/src/tests/AutoForge.IntegrationTests/CalibrationAuthoringUiTests.cs @@ -2,6 +2,8 @@ using System.Globalization; using AutoForge.Application.Features.DisplayUnits; using AutoForge.Application.Features.Vehicles.Calibration; using AutoForge.Core.Calibration; +using AutoForge.Application.Features.Vehicles.Runbooks; +using System.Text.Json.Nodes; using AutoForge.UI.Features.Vehicles.Calibration; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; @@ -102,6 +104,39 @@ public sealed partial class CalibrationAuthoringTests }); } + [Fact] + public async Task PreviewHandoff_KeepsExistingPinnedUnitsAndExactCandidateUntilSeparateSave() + { + using var provider = CreateProvider(); + var service = provider.GetRequiredService(); + var baseline = service.CreateBaseline(CreateWarm(provider).ConfigurationIdentifier); + var previews = provider.GetRequiredService(); + var input = JsonNode.Parse(previews.CreateInputTemplate())!; + input["steps"]![0]!["values"]!["scalars"]![0]!["value"] = 1250; + var cells = new[] { 240.000000000001, 297.123456789012, 350.000000000001, 355, 280, 195 }; + for (var index = 0; index < cells.Length; index++) + input["steps"]![0]!["values"]!["tables"]![0]!["cells"]![index] = cells[index]; + previews.Validate(input.ToJsonString()); + await using var renderer = new CalibrationEventRenderer(provider); + var host = new CalibrationUnitHost(); + var root = renderer.Attach(host); + await renderer.Dispatcher.InvokeAsync(async () => + { + await renderer.RenderAsync(root); + host.SetUnits(new(DisplayUnitSystem.Metric, SpeedDisplayMode.Road)); + await renderer.ClickAsync(root, "calibration-copy-preview"); + Assert.Contains("Draft input pinned to ImperialUS", renderer.Text(root)); + Assert.Equal(DisplayUnitConverter.Project(cells[2], "N*m", DisplayUnitSelection.Default).Value!.Value.ToString("R", CultureInfo.InvariantCulture), + renderer.Attribute(root, "calibration-torque-2", "value")!.ToString()); + Assert.Same(baseline, service.Snapshot); + await renderer.ClickAsync(root, "calibration-save"); + Assert.Equal(cells, service.Snapshot!.Head.Values.Tables[0].Cells); + Assert.Equal(1250, service.Snapshot.Head.Values.Scalars[0].Value); + Assert.Equal(2, service.Snapshot.History.Count); + Assert.Equal(baseline.Head.Identifier, service.Snapshot.Head.ParentIdentifier); + }); + } + [Fact] public async Task TwoActualEditorsAndSourceReplacement_RejectStaleOrUnconfirmedLossWithoutMutation() { diff --git a/src/tests/AutoForge.IntegrationTests/CalibrationComparisonTests.cs b/src/tests/AutoForge.IntegrationTests/CalibrationComparisonTests.cs new file mode 100644 index 0000000..d3ac7e8 --- /dev/null +++ b/src/tests/AutoForge.IntegrationTests/CalibrationComparisonTests.cs @@ -0,0 +1,652 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using AutoForge.Application.Features.Vehicles.Calibration; +using AutoForge.Application.Features.Vehicles.Diagnostics; +using AutoForge.Application.Features.Vehicles.Runbooks; +using AutoForge.Application.Features.Vehicles.Telemetry.Recording; +using AutoForge.Application.Features.Vehicles.Telemetry.Replay; +using AutoForge.Core.Calibration; +using AutoForge.Core.ExecutionTargets; +using AutoForge.Core.Simulation; +using AutoForge.Core.Vehicles; +using AutoForge.Diagnostics; +using AutoForge.Simulation; +using Microsoft.Extensions.DependencyInjection; + +namespace AutoForge.IntegrationTests; + +public sealed partial class CalibrationRehearsalTests +{ + [Fact] + public async Task Comparison_ActualVerifiedApplyProjectsOnlySupportedFieldsAndRunsEquivalentCapturedScenarios() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + await fixture.OpenAsync(); + var backup = await rehearsal.PrepareBackupAsync(rehearsal.NewOperationId()); + var head = fixture.Save(1250, [240, 300, 350, 350, 300, 180]); + var preparation = await rehearsal.PrepareApplyAsync(rehearsal.NewOperationId(), fixture.Workspace.Identifier, head.Identifier); + var approval = rehearsal.Approve(preparation.Identifier, true); + var operation = rehearsal.NewOperationId(); + var applied = await rehearsal.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + Assert.Equal(RehearsalVerification.VerifiedApplied, applied.Verification); + var store = new ComparisonMemoryStore(); + await using var comparison = new CalibrationComparisonSession(rehearsal, + fixture.Provider.GetRequiredService(), fixture.Configurations, store, TimeProvider.System); + var writes = fixture.Port.WriteCount; + var authoring = fixture.Authoring.Snapshot; + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, + DynamicsDiagnosticTransportFactory.ProviderIdentifier); + var result = Assert.IsType(comparison.Snapshot.Result); + Assert.True(result.IsCurrent); + Assert.Same(authoring, fixture.Authoring.Snapshot); + Assert.Equal(writes, fixture.Port.WriteCount); + Assert.Equal(backup.Identifier, result.Source.BackupIdentifier); + Assert.Equal(operation, result.Source.OperationIdentifier); + Assert.Equal(650, result.Baseline.Configuration.Model.WarmIdleTargetRpm); + Assert.Equal(1250, result.Derived.Configuration.Model.WarmIdleTargetRpm); + Assert.Equal(350, result.Derived.Configuration.Model.EngineTorqueCurve[2].TorqueNewtonMeters); + Assert.NotEqual(result.Baseline.Configuration.ConfigurationIdentifier, result.Derived.Configuration.ConfigurationIdentifier); + Assert.Equal(fixture.Export, fixture.Configurations.Export(fixture.Configuration.ConfigurationIdentifier)); + var original = JsonNode.Parse(fixture.Export)!; + var derived = JsonNode.Parse(fixture.Configurations.Export(result.Derived.Configuration.ConfigurationIdentifier))!; + original["model"]!["warmIdleTargetRpm"] = 1250; + original["model"]!["engineTorqueCurve"]![2]!["torqueNewtonMeters"] = 350; + Assert.True(JsonNode.DeepEquals(original, derived)); + Assert.Equal(3361, result.Baseline.Metrics!.ModelSteps); + Assert.Equal(336.1, result.Baseline.Metrics.TimeSeconds, precision: 8); + Assert.Equal(3000, result.Baseline.Metrics.PhaseSteps[2]); + Assert.Equal(600, result.Baseline.Metrics.CruiseSampleCount); + Assert.InRange(result.Baseline.Metrics.SampledCruiseMinimumMetersPerSecond, 44 * 0.44704, 46 * 0.44704); + Assert.InRange(result.Baseline.Metrics.SampledCruiseMaximumMetersPerSecond, 44 * 0.44704, 46 * 0.44704); + foreach (var run in new[] { result.Baseline, result.Derived }) + { + Assert.True(run.IsComplete); + Assert.InRange(run.Metrics!.MaximumLastTenIdleErrorRpm, 0, 1); + Assert.InRange(run.Metrics.MaximumLastTenIdleSpeedMetersPerSecond, 0, 0.001); + Assert.Equal(run.Samples.Count * 2, run.Capture.Observations.Count); + Assert.InRange(run.Capture.Observations.Count, 1, 1562); + Assert.All(run.Samples.Take(run.Samples.Count - 1), sample => Assert.Equal(0, sample.Step % 5)); + Assert.Equal(run.RunIdentifier, run.Capture.Header.DynamicsRunIdentifier); + Assert.Equal(run.Configuration.ConfigurationIdentifier, run.Capture.Header.DynamicsConfigurationIdentifier); + Assert.All(run.Capture.Observations, observation => Assert.Equal(2, run.Capture.Header.ProtocolVersion)); + } + Assert.NotEqual(result.Baseline.RunIdentifier, result.Derived.RunIdentifier); + Assert.NotEqual(result.Baseline.Capture.Header.CaptureId, result.Derived.Capture.Header.CaptureId); + await comparison.SaveCapturesAsync(); + Assert.Equal(2, store.Saved.Count); + Assert.All(comparison.Snapshot.Saves, save => Assert.True(save.Persisted && save.ReadbackVerified)); + await comparison.SaveCapturesAsync(); + Assert.Equal(2, store.SaveCalls); + Assert.Contains("not continuous extrema", result.Report); + await comparison.CloseAsync(); + Assert.False(result.IsCurrent); + Assert.Same(result, comparison.Snapshot.Result); + await rehearsal.CloseAsync(); + Assert.False(result.IsCurrent); + Assert.Contains("Stale", result.Report); + } + + [Theory] + [InlineData("scalar-key")] + [InlineData("scalar-unit")] + [InlineData("table-unit")] + [InlineData("axis")] + [InlineData("missing-cell")] + [InlineData("idle-low")] + [InlineData("idle-high")] + [InlineData("idle-nan")] + [InlineData("cell-low")] + [InlineData("cell-high")] + [InlineData("cell-infinite")] + public void Comparison_ProjectionRejectsInvalidCompleteValuesWithoutChangingOriginal(string fault) + { + using var fixture = new Fixture(); + var definition = fixture.Workspace.Definition; + var axis = definition.Table.Axis.ToArray(); + var cells = Torque.ToArray(); + if (fault == "axis") axis[2] = 2001; + if (fault == "missing-cell") cells = cells[..^1]; + if (fault == "cell-low") cells[1] = 0; + if (fault == "cell-high") cells[1] = 1001; + if (fault == "cell-infinite") cells[1] = double.PositiveInfinity; + var idle = fault switch { "idle-low" => 649, "idle-high" => 5001, "idle-nan" => double.NaN, _ => 1250 }; + var values = new CalibrationValues( + [new(fault == "scalar-key" ? "unknown" : SyntheticEngineCalibration.ScalarIdentifier, + fault == "scalar-unit" ? "rad/s" : "rpm", idle)], + [new(SyntheticEngineCalibration.TableIdentifier, fault == "table-unit" ? "lbf*ft" : "N*m", "rpm", axis, cells)]); + Assert.Throws(() => SyntheticCalibrationProjection.Create(fixture.Configuration, definition, values)); + Assert.Equal(fixture.Export, fixture.Configurations.Export(fixture.Configuration.ConfigurationIdentifier)); + Assert.Equal(0, fixture.Port.ReadCount); + Assert.Equal(0, fixture.Port.WriteCount); + } + + [Fact] + public void Comparison_NoChangeProjectionRetainsExactContentFingerprintAndCopiesNonuniformCells() + { + using var fixture = new Fixture(); + var unchanged = SyntheticCalibrationProjection.Create(fixture.Configuration, fixture.Workspace.Definition, + SyntheticCalibrationProjection.Read(fixture.Configuration)); + Assert.NotSame(fixture.Configuration, unchanged); + Assert.Equal(fixture.Configuration.ConfigurationIdentifier, unchanged.ConfigurationIdentifier); + var cells = new[] { 241d, 297, 350, 355, 280, 195 }; + var values = new CalibrationValues([new(SyntheticEngineCalibration.ScalarIdentifier, "rpm", 5000)], + [new(SyntheticEngineCalibration.TableIdentifier, "N*m", "rpm", fixture.Workspace.Definition.Table.Axis, cells)]); + var changed = SyntheticCalibrationProjection.Create(fixture.Configuration, fixture.Workspace.Definition, values); + Assert.Equal(cells, changed.Model.EngineTorqueCurve.Select(point => point.TorqueNewtonMeters)); + Assert.Equal(fixture.Configuration.Scenario, changed.Scenario); + Assert.Same(fixture.Configuration.InitialConditions, changed.InitialConditions); + Assert.Equal(fixture.Export, fixture.Configurations.Export(fixture.Configuration.ConfigurationIdentifier)); + } + + [Theory] + [InlineData("superseded")] + [InlineData("stale-head")] + [InlineData("wrong-workspace")] + public void Comparison_CurrentPreviewHandoffRejectsStaleOrSupersededWithoutSaving(string fault) + { + using var fixture = new Fixture(); + var previews = fixture.Provider.GetRequiredService(); + var input = JsonNode.Parse(previews.CreateInputTemplate())!; + input["steps"]![0]!["values"]!["scalars"]![0]!["value"] = 1250; + input["steps"]![0]!["values"]!["tables"]![0]!["cells"]![2] = 350; + var preview = previews.Validate(input.ToJsonString()); + var handoff = previews.CopyCurrentCandidateToDraft(preview.Identifier, fixture.Workspace.Identifier, fixture.Workspace.Head.Identifier); + Assert.Equal(1250, handoff.Values.Scalars[0].Value); + Assert.Equal(350, handoff.Values.Tables[0].Cells[2]); + Assert.Single(fixture.Authoring.Snapshot!.History); + Assert.Equal(0, fixture.Port.ReadCount); + Assert.Equal(0, fixture.Port.WriteCount); + if (fault == "superseded") previews.Validate(input.ToJsonString()); + if (fault == "stale-head") fixture.Save(1300); + var current = fixture.Authoring.Snapshot; + Assert.ThrowsAny(() => previews.CopyCurrentCandidateToDraft(preview.Identifier, + fault == "wrong-workspace" ? "other-workspace" : fixture.Workspace.Identifier, fixture.Workspace.Head.Identifier)); + Assert.Same(current, fixture.Authoring.Snapshot); + } + + [Fact] + public async Task Comparison_PartialPairSavePreservesFirstAndRetriesOnlyMissingCapture() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var store = new ComparisonMemoryStore { FailSaveCall = 2 }; + await using var comparison = CreateComparison(fixture, store); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + var result = comparison.Snapshot.Result!; + await Assert.ThrowsAsync(() => comparison.SaveCapturesAsync()); + Assert.True(store.Saved.ContainsKey(result.Baseline.Capture.Header.CaptureId)); + Assert.False(store.Saved.ContainsKey(result.Derived.Capture.Header.CaptureId)); + Assert.True(comparison.Snapshot.Saves.Single(item => item.CaptureIdentifier == result.Baseline.Capture.Header.CaptureId).ReadbackVerified); + Assert.False(comparison.Snapshot.Saves.Single(item => item.CaptureIdentifier == result.Derived.Capture.Header.CaptureId).Persisted); + store.FailSaveCall = null; + await comparison.SaveCapturesAsync(); + Assert.Equal(3, store.SaveCalls); + Assert.Equal(2, store.Saved.Count); + Assert.All(comparison.Snapshot.Saves, outcome => Assert.True(outcome.ReadbackVerified)); + Assert.Same(result, comparison.Snapshot.Result); + } + + [Fact] + public async Task Comparison_ReadbackCorruptionRetainsPublishedIdWithoutOverwriteAndRetriesReadOnly() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var store = new ComparisonMemoryStore { CorruptReadback = true }; + await using var comparison = CreateComparison(fixture, store); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + await Assert.ThrowsAsync(() => comparison.SaveCapturesAsync()); + var first = Assert.Single(comparison.Snapshot.Saves); + Assert.True(first.Persisted); + Assert.False(first.ReadbackVerified); + Assert.Single(store.Saved); + store.CorruptReadback = false; + await comparison.SaveCapturesAsync(); + Assert.Equal(2, store.SaveCalls); + Assert.Equal(2, store.Saved.Count); + Assert.All(comparison.Snapshot.Saves, outcome => Assert.True(outcome.ReadbackVerified)); + } + + [Fact] + public async Task Comparison_PendingSaveRejectsCompetingOwnerAndCloseDrainsWithoutDeletingSuccessfulFile() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var store = new ComparisonMemoryStore { HoldSave = true }; + await using var comparison = CreateComparison(fixture, store); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + var result = comparison.Snapshot.Result; + var save = comparison.SaveCapturesAsync(); + try + { + await store.SaveEntered.Task.WaitAsync(Deadline); + var busy = Assert.Throws(() => + { + _ = comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, + DynamicsDiagnosticTransportFactory.ProviderIdentifier); + }); + Assert.Equal(CalibrationComparisonFailure.Busy, busy.Failure); + var close = comparison.CloseAsync(); + Assert.False(close.IsCompleted); + store.ReleaseSave.TrySetResult(); + await Assert.ThrowsAnyAsync(() => save); + await close.WaitAsync(Deadline); + Assert.Equal(CalibrationComparisonState.Closed, comparison.Snapshot.State); + Assert.Same(result, comparison.Snapshot.Result); + Assert.InRange(store.Saved.Count, 0, 1); + } + finally + { + store.ReleaseSave.TrySetResult(); + try { await save; } catch (CalibrationComparisonException) { } + } + } + + [Theory] + [InlineData(63.9, true)] + [InlineData(64, false)] + public async Task Comparison_3900And3901StepBoundaryIsMeasuredWithUnchangedCaptureLimits(double warmSeconds, bool complete) + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var json = JsonNode.Parse(fixture.Export)!; + json["scenario"]![0]!["durationSeconds"] = warmSeconds; + var configuration = fixture.Configurations.Import(json.ToJsonString()); + var old = fixture.Authoring.Snapshot!; + fixture.Authoring.ReplaceBaseline(configuration.ConfigurationIdentifier, old.Identifier, old.Head.Identifier, true); + var current = fixture.Authoring.Snapshot!; + await rehearsal.OpenAsync(rehearsal.NewOperationId(), ProviderId, ExecutionTargetKind.Simulation, current.Identifier); + await rehearsal.PrepareBackupAsync(rehearsal.NewOperationId()); + var head = fixture.Save(1250); + var preparation = await rehearsal.PrepareApplyAsync(rehearsal.NewOperationId(), current.Identifier, head.Identifier); + var approval = rehearsal.Approve(preparation.Identifier, true); + var operation = rehearsal.NewOperationId(); + await rehearsal.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + if (complete) + { + await comparison.EvaluateAsync(operation, current.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + Assert.Equal(3900, comparison.Snapshot.Result!.Baseline.Metrics!.ModelSteps); + Assert.Equal(1560, comparison.Snapshot.Result.Baseline.Capture.Observations.Count); + return; + } + var error = await Assert.ThrowsAsync(() => comparison.EvaluateAsync(operation, + current.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + Assert.Equal(CalibrationComparisonFailure.StepLimit, error.Failure); + Assert.Null(comparison.Snapshot.Result); + Assert.NotNull(comparison.Snapshot.PartialRun); + Assert.False(comparison.Snapshot.PartialRun.IsComplete); + Assert.Equal(3900, comparison.Snapshot.PartialRun.Samples[^1].Step); + Assert.Equal(1560, comparison.Snapshot.PartialRun.Capture.Observations.Count); + } + + private static CalibrationComparisonSession CreateComparison(Fixture fixture, ITelemetryArchiveStore store) => new( + fixture.Session, fixture.Provider.GetRequiredService(), fixture.Configurations, store, TimeProvider.System); + private static async Task<(string Operation, CalibrationVersion Head)> PrepareComparisonAsync(Fixture fixture) + { + await fixture.OpenAsync(); + await fixture.Session.PrepareBackupAsync(fixture.Session.NewOperationId()); + var head = fixture.Save(1250, [240, 300, 350, 350, 300, 180]); + var preparation = await fixture.Session.PrepareApplyAsync(fixture.Session.NewOperationId(), fixture.Workspace.Identifier, head.Identifier); + var approval = fixture.Session.Approve(preparation.Identifier, true); + var operation = fixture.Session.NewOperationId(); + await fixture.Session.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + return (operation, head); + } + + [Fact] + public void Comparison_ProofCannotBeConstructedByPublicCallerSuccessRecords() + { + Assert.Empty(typeof(CalibrationComparisonSource).GetConstructors()); + Assert.Empty(typeof(CalibrationComparisonResult).GetConstructors()); + } + + [Theory] + [InlineData("identity")] + [InlineData("revision")] + [InlineData("cell")] + [InlineData("shape")] + [InlineData("read-failure")] + public async Task Comparison_FreshOwningReadRejectsDivergentOrUnavailableProofBeforeProjection(string fault) + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var configurations = fixture.Configurations.List().Count; + var writes = fixture.Port.WriteCount; + fixture.Port.ReadTransform = (_, image) => FaultImage(image, fault); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + var error = await Assert.ThrowsAsync(() => comparison.EvaluateAsync( + operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + Assert.Equal(CalibrationComparisonFailure.Proof, error.Failure); + Assert.Equal(configurations, fixture.Configurations.List().Count); + Assert.Equal(writes, fixture.Port.WriteCount); + Assert.Null(comparison.Snapshot.Result); + } + + [Fact] + public async Task Comparison_RecoveryRequiredRejectsUntilActualReadOnlyReconciliationVerifiesApply() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + await fixture.OpenAsync(); + await rehearsal.PrepareBackupAsync(rehearsal.NewOperationId()); + var head = fixture.Save(1250); + var preparation = await rehearsal.PrepareApplyAsync(rehearsal.NewOperationId(), fixture.Workspace.Identifier, head.Identifier); + var approval = rehearsal.Approve(preparation.Identifier, true); + var operation = rehearsal.NewOperationId(); + fixture.Port.FailAfterWriteRead = true; + await rehearsal.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + Assert.Equal(RehearsalState.RecoveryRequired, rehearsal.Snapshot.State); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + await Assert.ThrowsAsync(() => comparison.EvaluateAsync(operation, + fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + fixture.Port.FailAfterWriteRead = false; + await rehearsal.ReconcileAsync(rehearsal.NewOperationId(), operation); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + Assert.Equal(RehearsalVerification.VerifiedAppliedAfterReconciliation, comparison.Snapshot.Result!.Source.Verification); + Assert.Equal(1, fixture.Port.WriteCount); + } + + [Theory] + [InlineData("cancel")] + [InlineData("close")] + [InlineData("head")] + [InlineData("external")] + public async Task Comparison_HeldFinalActualReadDrainsOrRejectsSupersessionWithoutLateSuccess(string action) + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var finalRead = fixture.Port.ReadCount + 2; + var entered = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var release = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + fixture.Port.BeforeRead = async () => + { + if (fixture.Port.ReadCount != finalRead) return; + entered.TrySetResult(); + await release.Task; + }; + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + var evaluation = comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + try + { + await entered.Task.WaitAsync(Deadline); + Assert.True(comparison.Snapshot.IsBusy); + Assert.Throws(() => + { + _ = comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + }); + var drain = action == "close" ? comparison.CloseAsync() : action == "cancel" ? comparison.CancelAsync() : Task.CompletedTask; + if (action is "cancel" or "close") Assert.False(drain.IsCompleted); + if (action == "head") fixture.Save(1300); + if (action == "external") await fixture.Port.ExternalChangeAsync(1300); + release.TrySetResult(); + await Assert.ThrowsAnyAsync(() => evaluation); + try { await drain.WaitAsync(Deadline); } catch (OperationCanceledException) { } + Assert.Null(comparison.Snapshot.Result); + Assert.NotNull(comparison.Snapshot.PartialRun); + Assert.NotNull(comparison.Snapshot.Error); + Assert.Equal(1, fixture.Port.WriteCount); + if (action == "close") Assert.Equal(CalibrationComparisonState.Closed, comparison.Snapshot.State); + } + finally + { + release.TrySetResult(); + try { await evaluation; } catch (Exception) { } + } + } + + [Fact] + public async Task Comparison_SaveFailureAfterPublicationRetriesReadOnlyWithoutCollisionOrOverwrite() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var store = new ComparisonMemoryStore { FailAfterPublish = true }; + await using var comparison = CreateComparison(fixture, store); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + await Assert.ThrowsAsync(() => comparison.SaveCapturesAsync()); + Assert.Single(store.Saved); + Assert.False(Assert.Single(comparison.Snapshot.Saves).ReadbackVerified); + store.FailAfterPublish = false; + await comparison.SaveCapturesAsync(); + Assert.Equal(2, store.SaveCalls); + Assert.Equal(2, store.Saved.Count); + Assert.All(comparison.Snapshot.Saves, outcome => Assert.True(outcome.ReadbackVerified)); + } + + [Fact] + public async Task Comparison_RealFileReadbackAndReplayPreserveExactBytesAndRejectWrongReferences() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + var root = Path.Combine(Path.GetTempPath(), $"autoforge-step13-{Guid.NewGuid():N}"); + var store = new FileTelemetryArchiveStore(root); + try + { + await using var comparison = CreateComparison(fixture, store); + await comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + await comparison.SaveCapturesAsync(); + await using var replay = new TelemetryReplaySession(store, TimeProvider.System, new SystemReplayWaiter(TimeProvider.System)); + foreach (var run in new[] { comparison.Snapshot.Result!.Baseline, comparison.Snapshot.Result.Derived }) + { + var bytes = TelemetryArchiveCodec.Serialize(run.Capture); + Assert.Equal(bytes, await File.ReadAllBytesAsync(Path.Combine(root, $"{run.Capture.Header.CaptureId}.json"))); + await replay.LoadAsync(run.Capture.Header.CaptureId); + Assert.Equal(bytes, TelemetryArchiveCodec.Serialize(replay.Snapshot.Archive!)); + Assert.Equal(ExecutionTargetKind.RecordedReplay, replay.Snapshot.Context!.Kind); + replay.SeekOrdinal(1); + Assert.Equal(1, replay.Snapshot.AppliedCount); + Assert.Equal(run.Capture.Observations[0].ResponsePayloadBase64, replay.Snapshot.CurrentObservation!.ResponsePayloadBase64); + replay.SeekOrdinal(run.Capture.Observations.Count); + Assert.Equal(run.Capture.Observations.Count, replay.Snapshot.AppliedCount); + Assert.Equal(run.RunIdentifier, replay.Snapshot.Archive!.Header.DynamicsRunIdentifier); + } + var retained = replay.Snapshot.Archive; + var wrongId = Guid.NewGuid().ToString("N"); + await File.WriteAllBytesAsync(Path.Combine(root, $"{wrongId}.json"), TelemetryArchiveCodec.Serialize(retained!)); + await Assert.ThrowsAsync(() => replay.LoadAsync(wrongId)); + Assert.Same(retained, replay.Snapshot.Archive); + var corrupt = JsonNode.Parse(TelemetryArchiveCodec.Serialize(retained!))!; + corrupt["header"]!["dynamicsRunIdentifier"] = "wrong-run"; + await File.WriteAllTextAsync(Path.Combine(root, $"{retained!.Header.CaptureId}.json"), corrupt.ToJsonString()); + await Assert.ThrowsAsync(() => replay.LoadAsync(retained.Header.CaptureId)); + Assert.Same(retained, replay.Snapshot.Archive); + } + finally { if (Directory.Exists(root)) Directory.Delete(root, recursive: true); } + } + + [Theory] + [InlineData("cancel")] + [InlineData("close")] + public async Task Comparison_HeldActualSignalTransportCancelsAndDisposesWithoutLatePublication(string action) + { + var factory = new ComparisonSignalFaultFactory("held"); + using var fixture = new Fixture(dynamics: true, dynamicsFactory: factory); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + var evaluation = comparison.EvaluateAsync(operation, fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier); + try + { + await factory.Entered.Task.WaitAsync(Deadline); + var drain = action == "cancel" ? comparison.CancelAsync() : comparison.CloseAsync(); + Assert.False(drain.IsCompleted); + factory.Release.TrySetResult(); + await Assert.ThrowsAnyAsync(() => evaluation); + try { await drain.WaitAsync(Deadline); } catch (OperationCanceledException) { } + Assert.Equal(action == "close" ? CalibrationComparisonState.Closed : CalibrationComparisonState.Cancelled, comparison.Snapshot.State); + Assert.Null(comparison.Snapshot.Result); + Assert.False(comparison.Snapshot.PartialRun!.IsComplete); + Assert.Equal(1, factory.Disposals); + } + finally + { + factory.Release.TrySetResult(); + try { await evaluation; } catch (OperationCanceledException) { } + } + } + + [Fact] + public async Task Comparison_FewerThanTenActualWarmSamplesRejectsWithoutShortenedPassingWindow() + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + var json = JsonNode.Parse(fixture.Export)!; + json["scenario"]![0]!["durationSeconds"] = 0.4; + var configuration = fixture.Configurations.Import(json.ToJsonString()); + var before = fixture.Authoring.Snapshot!; + fixture.Authoring.ReplaceBaseline(configuration.ConfigurationIdentifier, before.Identifier, before.Head.Identifier, true); + var source = fixture.Authoring.Snapshot!; + await rehearsal.OpenAsync(rehearsal.NewOperationId(), ProviderId, ExecutionTargetKind.Simulation, source.Identifier); + await rehearsal.PrepareBackupAsync(rehearsal.NewOperationId()); + var head = fixture.Save(1250); + var preparation = await rehearsal.PrepareApplyAsync(rehearsal.NewOperationId(), source.Identifier, head.Identifier); + var approval = rehearsal.Approve(preparation.Identifier, true); + var operation = rehearsal.NewOperationId(); + await rehearsal.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + var error = await Assert.ThrowsAsync(() => comparison.EvaluateAsync( + operation, source.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + Assert.Equal(CalibrationComparisonFailure.Incomplete, error.Failure); + Assert.Null(comparison.Snapshot.Result); + Assert.Contains("ten", error.Message); + Assert.Null(comparison.Snapshot.PartialRun!.Metrics); + } + + [Theory] + [InlineData("missing")] + [InlineData("unit")] + [InlineData("quality")] + [InlineData("nonfinite")] + [InlineData("kind")] + [InlineData("run")] + [InlineData("timeout")] + public async Task Comparison_ActualTransportSignalFaultNeverPublishesHealthyMetrics(string fault) + { + var factory = new ComparisonSignalFaultFactory(fault); + using var fixture = new Fixture(dynamics: true, dynamicsFactory: factory); + await using var rehearsal = fixture.Session; + var (operation, head) = await PrepareComparisonAsync(fixture); + await using var comparison = CreateComparison(fixture, new ComparisonMemoryStore()); + await Assert.ThrowsAsync(() => comparison.EvaluateAsync(operation, + fixture.Workspace.Identifier, head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + Assert.Null(comparison.Snapshot.Result); + Assert.False(comparison.Snapshot.PartialRun!.IsComplete); + Assert.Null(comparison.Snapshot.PartialRun.Metrics); + Assert.NotNull(comparison.Snapshot.Error); + Assert.Equal(1, factory.Disposals); + } + + [Theory] + [InlineData("wrong-operation")] + [InlineData("wrong-head")] + [InlineData("external-revision")] + [InlineData("closed")] + public async Task Comparison_TrustedAdmissionRejectsWrongOrSupersededActualProof(string fault) + { + using var fixture = new Fixture(dynamics: true); + await using var rehearsal = fixture.Session; + await fixture.OpenAsync(); + await rehearsal.PrepareBackupAsync(rehearsal.NewOperationId()); + var head = fixture.Save(1250); + var preparation = await rehearsal.PrepareApplyAsync(rehearsal.NewOperationId(), fixture.Workspace.Identifier, head.Identifier); + var approval = rehearsal.Approve(preparation.Identifier, true); + var operation = rehearsal.NewOperationId(); + await rehearsal.ExecuteAsync(operation, approval.Identifier, CalibrationMutationAction.Apply); + if (fault == "external-revision") await fixture.Port.ExternalChangeAsync(1300); + if (fault == "closed") await rehearsal.CloseAsync(); + var before = fixture.Configurations.List().Count; + await using var comparison = new CalibrationComparisonSession(rehearsal, + fixture.Provider.GetRequiredService(), fixture.Configurations, new ComparisonMemoryStore(), TimeProvider.System); + await Assert.ThrowsAnyAsync(() => comparison.EvaluateAsync( + fault == "wrong-operation" ? "forged-success" : operation, fixture.Workspace.Identifier, + fault == "wrong-head" ? "other-head" : head.Identifier, DynamicsDiagnosticTransportFactory.ProviderIdentifier)); + Assert.Null(comparison.Snapshot.Result); + Assert.Equal(before, fixture.Configurations.List().Count); + Assert.NotNull(comparison.Snapshot.Error); + } + + private sealed class ComparisonMemoryStore : ITelemetryArchiveStore + { + public Dictionary Saved { get; } = new(StringComparer.Ordinal); + public int SaveCalls { get; private set; } + public int? FailSaveCall { get; set; } + public bool CorruptReadback { get; set; } + public bool HoldSave { get; set; } + public bool FailAfterPublish { get; set; } + public TaskCompletionSource SaveEntered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + public TaskCompletionSource ReleaseSave { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + public async Task SaveAsync(TelemetryCaptureSnapshot capture, CancellationToken cancellationToken = default) + { + cancellationToken.ThrowIfCancellationRequested(); + SaveCalls++; + if (HoldSave) { SaveEntered.TrySetResult(); await ReleaseSave.Task; } + if (SaveCalls == FailSaveCall) throw new TelemetryArchiveException(TelemetryArchiveFailure.WriteFailure, "controlled second capture failure"); + Saved.Add(capture.Header.CaptureId, TelemetryArchiveCodec.Deserialize(TelemetryArchiveCodec.Serialize(capture))); + if (FailAfterPublish) throw new TelemetryArchiveException(TelemetryArchiveFailure.WriteFailure, "final file published, then controlled cleanup failed"); + } + public Task> ListAsync(CancellationToken cancellationToken = default) => + Task.FromResult>(Saved.Values.Select(capture => new TelemetrySavedCapture( + capture.Header.CaptureId, capture.Header.SessionIdentifier, capture.Header.ProfileIdentifier, + DateTimeOffset.Parse(capture.Header.StartedAtUtc), capture.Completion!.State, capture.Observations.Count)).ToArray()); + public Task LoadAsync(string captureId, CancellationToken cancellationToken = default) + { + var capture = Saved[captureId]; + return Task.FromResult(CorruptReadback + ? new TelemetryCaptureSnapshot(capture.Header with { CaptureId = new string('a', 32) }, capture.Observations, capture.Completion) + : capture); + } + } + + private sealed class ComparisonSignalFaultFactory(string fault) : IDynamicsDiagnosticTransportFactory + { + private readonly DynamicsDiagnosticTransportFactory _inner = new(SyntheticDiagnosticExamples.CreateDefinitions()); + private string Fault { get; } = fault; + public int Disposals { get; private set; } + public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + public TaskCompletionSource Release { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); + public DiagnosticProviderDescriptor Descriptor => _inner.Descriptor; + public IDiagnosticTransport OpenTransport(VehicleProfile profile, DiagnosticSessionIdentity identity) => _inner.OpenTransport(profile, identity); + public DynamicsSessionBinding OpenDynamicsTransport(VehicleProfile profile, DiagnosticSessionIdentity identity, DynamicsConfiguration configuration) + { + var actual = _inner.OpenDynamicsTransport(profile, identity, configuration); + return actual with { Transport = new FaultTransport(actual.Transport, this) }; + } + private sealed class FaultTransport(IDiagnosticTransport inner, ComparisonSignalFaultFactory owner) : IDiagnosticTransport + { + public IReadOnlyList ExpectedEndpoints => inner.ExpectedEndpoints; + public async ValueTask SendAsync(ReadOnlyMemory request, CancellationToken cancellationToken) + { + var reply = await inner.SendAsync(request, cancellationToken); + if (!SyntheticProtocolV1.TryDecodeRequest(request, out var decoded) || decoded?.Service != DiagnosticServices.ReadSignals) return reply; + if (owner.Fault == "held") + { + owner.Entered.TrySetResult(); + await owner.Release.Task; + cancellationToken.ThrowIfCancellationRequested(); + return reply; + } + if (owner.Fault == "timeout") return new(TransportOutcome.Timeout, ReadOnlyMemory.Empty, 5, "controlled actual signal timeout"); + var response = JsonNode.Parse(reply.Payload.Span)!; + var data = response["data"]!; + if (owner.Fault == "run") data["runIdentifier"] = "wrong-run"; + var signals = data["signals"]!.AsArray(); + var index = signals.Select((signal, position) => (signal, position)) + .Single(item => item.signal!["signalIdentifier"]!.GetValue() == "engine.speed").position; + if (owner.Fault == "missing") signals.RemoveAt(index); + if (owner.Fault == "unit") signals[index]!["unit"] = "rad/s"; + if (owner.Fault == "quality") signals[index]!["quality"] = "Uncertain"; + if (owner.Fault == "nonfinite") signals[index]!["value"] = JsonNode.Parse("1e309"); + if (owner.Fault == "kind") signals[index]!["value"] = "NaN"; + return new(reply.Outcome, JsonSerializer.SerializeToUtf8Bytes(response), reply.ElapsedTicks); + } + public async ValueTask DisposeAsync() { owner.Disposals++; await inner.DisposeAsync(); } + } + } +} diff --git a/src/tests/AutoForge.IntegrationTests/CalibrationRehearsalTests.cs b/src/tests/AutoForge.IntegrationTests/CalibrationRehearsalTests.cs index 7148901..cb676fc 100644 --- a/src/tests/AutoForge.IntegrationTests/CalibrationRehearsalTests.cs +++ b/src/tests/AutoForge.IntegrationTests/CalibrationRehearsalTests.cs @@ -1,4 +1,5 @@ using AutoForge.Application.Features.Vehicles.Calibration; +using AutoForge.Application.Features.Vehicles.Diagnostics; using AutoForge.Application.Features.Vehicles.Profiles; using AutoForge.Application.Features.Vehicles.Simulation; using AutoForge.Core.Calibration; @@ -361,11 +362,13 @@ public sealed partial class CalibrationRehearsalTests public CalibrationWorkspace Workspace { get; } public CalibrationRehearsalSession Session { get; } public string Export { get; } - public Fixture(TimeProvider? clock = null) + public Fixture(TimeProvider? clock = null, bool dynamics = false, IDiagnosticTransportFactory? dynamicsFactory = null) { var services = new ServiceCollection(); services.AddLogging(); services.AddAutoForgeVehicleProfiles(); + if (dynamics) services.AddAutoForgeDiagnostics( + [dynamicsFactory ?? new DynamicsDiagnosticTransportFactory(SyntheticDiagnosticExamples.CreateDefinitions())]); services.AddAutoForgeUi(); services.AddSingleton(Port); if (clock is not null) services.AddSingleton(clock); diff --git a/src/tests/AutoForge.UnitTests/DynamicsEngineTests.cs b/src/tests/AutoForge.UnitTests/DynamicsEngineTests.cs index 9c1058c..f2bea56 100644 --- a/src/tests/AutoForge.UnitTests/DynamicsEngineTests.cs +++ b/src/tests/AutoForge.UnitTests/DynamicsEngineTests.cs @@ -29,6 +29,8 @@ public sealed class DynamicsEngineTests Assert.Null(first.Status.Failure); Assert.Equal(1, first.Status.ScenarioProgress); Assert.Equal(first.Samples.Count, second.Samples.Count); + Assert.Equal(3361, first.Samples.Count); + Assert.Equal(336.1, first.Status.VirtualTimeSeconds, precision: 8); Assert.Equal(first.Samples, second.Samples); var model = configuration.Model; @@ -47,6 +49,7 @@ public sealed class DynamicsEngineTests var cruise = first.Samples.Where(sample => sample.Phase == DynamicsScenarioPhaseKind.Cruise).ToArray(); Assert.Equal(3_000, cruise.Length); + Assert.All(cruise, sample => Assert.InRange(sample.VehicleSpeedMetersPerSecond, 44 * 0.44704, 46 * 0.44704)); var cruiseWithinToleranceCount = cruise.Count(sample => Math.Abs(sample.VehicleSpeedMetersPerSecond - target) <= 0.44704); Assert.True(cruiseWithinToleranceCount / (double)cruise.Length >= 0.95);