using AutoForge.Application.Features.Vehicles.Calibration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; namespace AutoForge.Application.Features.Vehicles.Runbooks; public sealed class LocalRunbookGenerationFactory(ILocalInferencePort port, ICalibrationKnowledgeProvider knowledge, CalibrationAuthoringService authoring, RunbookPreviewService previews, TimeProvider clock) { public LocalRunbookGenerationSession Create() => new(port, knowledge, authoring, previews, clock); } public static class LocalRunbookGenerationServiceCollectionExtensions { public static IServiceCollection AddAutoForgeLocalRunbookGeneration(this IServiceCollection services) { services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); return services; } } public sealed class LocalRunbookGenerationSession(ILocalInferencePort port, ICalibrationKnowledgeProvider knowledge, CalibrationAuthoringService authoring, RunbookPreviewService previews, TimeProvider clock) : IAsyncDisposable { public static readonly TimeSpan OperationDeadline = TimeSpan.FromSeconds(180); public const int MaximumGoalLength = 2000; public const int MaximumRetainedContent = 131072; private readonly object _gate = new(); private LocalGenerationSnapshot _snapshot = new(LocalGenerationState.Idle, false, false, false, [], null, null, null, null); private Owner? _owner; private Task? _closeTask; private long _generation; public event Action? Changed; public LocalGenerationSnapshot Snapshot { get { lock (_gate) return _snapshot; } } public void SetLocalOnlyAcknowledgement(bool acknowledge) { lock (_gate) { EnsureAvailable(); _snapshot = _snapshot with { LocalOnlyAcknowledged = acknowledge }; } Changed?.Invoke(); } public Task RefreshAsync(CancellationToken cancellationToken = default) => Start(async owner => { var models = await port.ReadCatalogueAsync(owner.Token).ConfigureAwait(false); lock (_gate) { EnsureCurrent(owner); var selected = _snapshot.SelectedModel; if (selected is not null && !models.Any(model => model.Name == selected.Name && model.Digest == selected.Digest && model.Eligibility == LocalModelEligibility.Candidate)) selected = null; _snapshot = _snapshot with { Models = Array.AsReadOnly(models.ToArray()), CatalogueFresh = true, SelectedModel = selected, Error = null }; } }, cancellationToken, catalogueOperation: true); public Task SelectModelAsync(string name, CancellationToken cancellationToken = default) { LocalModelSummary model; lock (_gate) { EnsureAvailable(); model = _snapshot.CatalogueFresh ? _snapshot.Models.SingleOrDefault(item => item.Name == name) ?? throw Reject(LocalGenerationFailure.ModelRequired, "Select an exact explicitly refreshed installed model.") : throw Reject(LocalGenerationFailure.ModelRequired, "Explicitly refresh the local catalogue first."); if (model.Eligibility == LocalModelEligibility.Rejected) throw Reject(LocalGenerationFailure.ModelIneligible, model.Reason!); return Start(async owner => { var selected = await port.VerifyModelAsync(new(model.Name, model.Digest), owner.Token).ConfigureAwait(false); lock (_gate) { EnsureCurrent(owner); _snapshot = _snapshot with { SelectedModel = selected, Error = null }; } }, cancellationToken); } } public Task GenerateAsync(string goal, CancellationToken cancellationToken = default) => Start(async owner => { var context = owner.Context ?? throw new LocalGenerationException(LocalGenerationFailure.Context, "No captured generation context exists."); string? content = null; try { content = await port.GenerateAsync(context, owner.Token).ConfigureAwait(false); lock (_gate) { EnsureCurrent(owner); if (_snapshot.SelectedModel?.Identity != context.Model.Identity) throw Reject(LocalGenerationFailure.ModelChanged, "Selected model changed; output was not admitted."); var preview = previews.ValidateGenerated(content, context.Source, context.ExpectedPreviewIdentifier, owner.Token); var evidence = Evidence(context, content, preview.Identifier, null); _snapshot = _snapshot with { Latest = evidence, LastSuccessful = evidence, Error = null }; } } catch (RunbookPreviewException exception) { var failure = exception.Failure switch { RunbookPreviewFailure.StaleSource => LocalGenerationFailure.StaleSource, RunbookPreviewFailure.PreviewSuperseded => LocalGenerationFailure.Superseded, _ => LocalGenerationFailure.Validation }; var error = new LocalGenerationException(failure, $"Original model content failed authoritative preview admission: {exception.Message}", exception, content); PublishFailureEvidence(owner, context, content, error); throw error; } catch (LocalGenerationException exception) { PublishFailureEvidence(owner, context, content ?? exception.UnvalidatedContent, exception); throw; } catch (OperationCanceledException exception) { var error = CancellationError(owner, exception); PublishFailureEvidence(owner, context, content, error); throw error; } }, cancellationToken, () => CaptureContext(goal)); public LocalGenerationContext PrepareContext(string goal) { LocalGenerationContext context; lock (_gate) { EnsureAvailable(); context = CaptureContext(goal, requireAcknowledgement: false); _snapshot = _snapshot with { Latest = Evidence(context, "", null, null), Error = null }; } Changed?.Invoke(); return context; } private LocalGenerationContext CaptureContext(string goal, bool requireAcknowledgement = true) { if (_snapshot.SelectedModel is null || !_snapshot.CatalogueFresh) throw Reject(LocalGenerationFailure.ModelRequired, "Select an eligible installed model after explicit catalogue refresh; no substitute model is used."); if (requireAcknowledgement && !_snapshot.LocalOnlyAcknowledged) throw Reject(LocalGenerationFailure.LocalOnlyAcknowledgementRequired, "Explicitly acknowledge operator-verified cloud-disabled local-only operation before sending goal/source context."); if (string.IsNullOrWhiteSpace(goal) || goal.Length > MaximumGoalLength) throw Reject(LocalGenerationFailure.InvalidGoal, "An explicit nonblank goal of at most 2000 UTF-16 code units is required."); try { new System.Text.UTF8Encoding(false, true).GetByteCount(goal); } catch (System.Text.EncoderFallbackException) { throw Reject(LocalGenerationFailure.InvalidGoal, "Goal has invalid Unicode; it was not rewritten or sent."); } var source = authoring.Snapshot ?? throw Reject(LocalGenerationFailure.Context, "Explicitly create/select a synthetic authoring source first."); var pack = knowledge.Get(source.Definition.Applicability); if (!pack.Matches(source.Definition.Applicability) || string.IsNullOrWhiteSpace(pack.Identifier) || pack.Identifier != pack.Identifier.Trim() || pack.Revision <= 0 || pack.Facts.Count == 0 || pack.Facts.Any(string.IsNullOrWhiteSpace)) throw Reject(LocalGenerationFailure.Context, "Knowledge pack is not exactly applicable to the selected model/definition."); var selected = _snapshot.SelectedModel; var model = new LocalModelSelection(selected.Identity with { }, selected.Size, selected.Format, selected.Family, Array.AsReadOnly(selected.Capabilities.ToArray()), selected.License); return new(goal, model, pack, new(source.Identifier, source.Head.Identifier, source.Definition.Applicability with { }), previews.CreateInputTemplate(source), source.Definition.Scalar.Minimum, source.Definition.Scalar.Maximum, Array.AsReadOnly(source.Definition.Table.Axis.ToArray()), previews.Snapshot.Preview?.Identifier); } private void PublishFailureEvidence(Owner owner, LocalGenerationContext context, string? content, LocalGenerationException error) { lock (_gate) { if (owner.Generation == _generation && _snapshot.State != LocalGenerationState.Closed) _snapshot = _snapshot with { Latest = Evidence(context, content ?? "", null, error) }; } } private static LocalGenerationEvidence Evidence(LocalGenerationContext context, string content, string? previewId, LocalGenerationException? error) => new(context, content.Length > MaximumRetainedContent ? content[..MaximumRetainedContent] : content, content.Length > MaximumRetainedContent, previewId, error?.Failure, error?.Message); private Task Start(Func operation, CancellationToken external, Func? captureContext = null, bool catalogueOperation = false) { Owner owner; lock (_gate) { EnsureAvailable(); external.ThrowIfCancellationRequested(); var context = captureContext?.Invoke(); owner = new(++_generation, external, clock) { Context = context, CatalogueOperation = catalogueOperation }; _owner = owner; _snapshot = _snapshot with { State = LocalGenerationState.Working, IsBusy = true, Error = null, Latest = context is null ? _snapshot.Latest : Evidence(context, "", null, null) }; } Changed?.Invoke(); // A replaceable transport can block before its first await; admission must still release the UI. var work = Task.Run(() => RunAsync(owner, operation)); _ = work.ContinueWith(completed => CompleteOwner(owner, completed), CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default); return owner.Done.Task; } private async Task RunAsync(Owner owner, Func operation) { try { await operation(owner).ConfigureAwait(false); } catch (OperationCanceledException exception) { throw CancellationError(owner, exception); } catch (Exception exception) when (exception is HttpRequestException or IOException or InvalidOperationException) { throw new LocalGenerationException(LocalGenerationFailure.Metadata, $"Local generation operation failed: {exception.Message}", exception); } } private void CompleteOwner(Owner owner, Task work) { var exception = work.Exception?.InnerExceptions.Count == 1 ? work.Exception.InnerException : work.Exception; var error = exception as LocalGenerationException; if (exception is not null && error is null) error = new(LocalGenerationFailure.Metadata, $"Local operation failed: {exception.Message}", exception); if (work.IsCanceled) error = CancellationError(owner, new OperationCanceledException()); owner.Release(); lock (_gate) { _owner = null; if (_snapshot.State != LocalGenerationState.Closed && _snapshot.State != LocalGenerationState.Closing) _snapshot = _snapshot with { State = error is null ? LocalGenerationState.Idle : error.Failure == LocalGenerationFailure.Cancelled ? LocalGenerationState.Cancelled : LocalGenerationState.Failed, IsBusy = false, Error = error?.Message, CatalogueFresh = error is not null && owner.CatalogueOperation ? false : _snapshot.CatalogueFresh }; if (error is null) owner.Done.TrySetResult(); else owner.Done.TrySetException(error); owner.Drain.TrySetResult(); } Changed?.Invoke(); } public Task CancelAsync() { Owner? owner; lock (_gate) { if (_snapshot.State is LocalGenerationState.Closed or LocalGenerationState.Closing) throw Reject(LocalGenerationFailure.Closed, "This generation page session is closed."); owner = _owner; if (owner is null) return Task.CompletedTask; owner.ExplicitCancellation = true; } owner.Cancel(); return owner.Drain.Task; } public Task CloseAsync() { Owner? owner; TaskCompletionSource completion; lock (_gate) { if (_closeTask is not null) return _closeTask; completion = new(TaskCreationOptions.RunContinuationsAsynchronously); _closeTask = completion.Task; ++_generation; owner = _owner; if (owner is not null) owner.ExplicitCancellation = true; _snapshot = _snapshot with { State = LocalGenerationState.Closing }; } _ = CloseCoreAsync(owner, completion); return completion.Task; } private async Task CloseCoreAsync(Owner? owner, TaskCompletionSource completion) { owner?.Cancel(); if (owner is not null) await owner.Drain.Task.ConfigureAwait(false); lock (_gate) { _snapshot = _snapshot with { State = LocalGenerationState.Closed, IsBusy = false }; completion.TrySetResult(); } Changed?.Invoke(); } public async ValueTask DisposeAsync() { try { await CloseAsync().ConfigureAwait(false); } finally { Changed = null; } } private void EnsureAvailable() { if (_snapshot.State is LocalGenerationState.Closed or LocalGenerationState.Closing) throw Reject(LocalGenerationFailure.Closed, "This page-owned generation session is closed; re-entry creates a fresh owner."); if (_owner is not null) throw Reject(LocalGenerationFailure.Busy, "One catalogue/selection/generation owner is active; cancel and drain before another request."); } private void EnsureCurrent(Owner owner) { owner.Token.ThrowIfCancellationRequested(); if (owner.Generation != _generation || _snapshot.State is LocalGenerationState.Closed or LocalGenerationState.Closing) throw new OperationCanceledException("Old generation owner cannot publish."); } private LocalGenerationException Reject(LocalGenerationFailure failure, string message) { _snapshot = _snapshot with { Error = message }; return new(failure, message); } private static LocalGenerationException CancellationError(Owner owner, Exception exception) => new(owner.Deadline.IsCancellationRequested && !owner.ExplicitCancellation && !owner.External.IsCancellationRequested ? LocalGenerationFailure.Timeout : LocalGenerationFailure.Cancelled, owner.Deadline.IsCancellationRequested && !owner.ExplicitCancellation && !owner.External.IsCancellationRequested ? "The complete local operation exceeded 180 seconds." : "Local generation cancelled; no late preview was admitted.", exception); private sealed class Owner { private readonly object _gate = new(); private readonly CancellationTokenSource _linked; private bool _released; public Owner(long generation, CancellationToken external, TimeProvider clock) { Generation = generation; External = external; Deadline = new(OperationDeadline, clock); _linked = CancellationTokenSource.CreateLinkedTokenSource(external, Deadline.Token); Token = _linked.Token; } public long Generation { get; } public CancellationToken External { get; } public CancellationTokenSource Deadline { get; } public CancellationToken Token { get; } public bool ExplicitCancellation { get; set; } public LocalGenerationContext? Context { get; init; } public bool CatalogueOperation { get; init; } public TaskCompletionSource Done { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public TaskCompletionSource Drain { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously); public void Cancel() { lock (_gate) { if (!_released) _linked.Cancel(); } } public void Release() { lock (_gate) { _released = true; _linked.Dispose(); Deadline.Dispose(); } } } }