diff --git a/docs/user-guide.md b/docs/user-guide.md index edbf265..d6bc1cb 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -44,7 +44,9 @@ send externally. If inspection cannot finish within its bounds, the request is r ## Reading a result Before Run, **Expected route** shows three boxes: **Sender → Covenant → Destination**. It comes only from the server's -current preview. When you change the inputs or the configuration, it says *stale* instead of showing an old answer. A +current preview. Switching the workload keeps the prompt and previews again at once, so the scheme shows the new +workload's outcome for the same request. When you change other inputs or the configuration, it says *stale* instead of +showing an old answer. A blocked preview says the destination would not be contacted. Web search adds a separate *external retrieval (Tavily)* badge, even when inference is local. diff --git a/src/Covenant.Ui/Components/RequestComposer.razor b/src/Covenant.Ui/Components/RequestComposer.razor index 6cc7d27..8696da0 100644 --- a/src/Covenant.Ui/Components/RequestComposer.razor +++ b/src/Covenant.Ui/Components/RequestComposer.razor @@ -155,7 +155,7 @@ @if (ready.SwitchToProfile is { } alt) { - + } } else @@ -334,7 +334,7 @@ private void OnProfile(ChangeEventArgs e) => Pg.SelectProfile(e.Value?.ToString()); - private void OnWorkload(string? profileId) => Pg.SelectProfile(profileId); + private Task OnWorkload(string? profileId) => Pg.SelectProfileAndPreviewAsync(profileId); private void OnCatalogModel(ChangeEventArgs e) { diff --git a/src/Covenant.Ui/Components/RoutePreviewPanel.razor b/src/Covenant.Ui/Components/RoutePreviewPanel.razor index 8747cdb..e214ee4 100644 --- a/src/Covenant.Ui/Components/RoutePreviewPanel.razor +++ b/src/Covenant.Ui/Components/RoutePreviewPanel.razor @@ -85,10 +85,7 @@ private void OnChanged() => _ = InvokeAsync(StateHasChanged); - private async Task SwitchAndPreviewAsync(string profileId) - { - if (Pg.SelectProfile(profileId)) await Pg.PreviewRouteAsync(); - } + private Task SwitchAndPreviewAsync(string profileId) => Pg.SelectProfileAndPreviewAsync(profileId); public void Dispose() { diff --git a/src/Covenant.Ui/Logic/RoutePreview.cs b/src/Covenant.Ui/Logic/RoutePreview.cs index aeb3c4e..27dc266 100644 --- a/src/Covenant.Ui/Logic/RoutePreview.cs +++ b/src/Covenant.Ui/Logic/RoutePreview.cs @@ -9,6 +9,11 @@ public sealed record RoutePreview(string Key, SimulateResponseDto? Result, strin public static string KeyOf(PlaygroundForm f) => string.Join("\u001f", f.ApplicationId ?? "", f.ProfileId ?? "", f.RequestedModel ?? "", f.Prompt); + /// The form holds enough for a Host dry run: a sender and a prompt. The workload switch uses this to decide + /// whether the Expected route can follow it at once (dry run only — no model call, no record, no spend). + public static bool CanPreview(PlaygroundForm f) + => !string.IsNullOrWhiteSpace(f.ApplicationId) && !string.IsNullOrWhiteSpace(f.Prompt); + public bool IsCurrent(PlaygroundForm f, int? activeVersion = null) => string.Equals(Key, KeyOf(f), StringComparison.Ordinal) && (activeVersion is null || Result?.ConfigVersion == activeVersion); diff --git a/src/Covenant.Ui/Services/PlaygroundState.cs b/src/Covenant.Ui/Services/PlaygroundState.cs index 180ecd9..f3ff38f 100644 --- a/src/Covenant.Ui/Services/PlaygroundState.cs +++ b/src/Covenant.Ui/Services/PlaygroundState.cs @@ -170,22 +170,28 @@ public async Task ApplyExampleAsync(ExampleResolution resolution) public async Task PreviewRouteAsync() { - if (Running || string.IsNullOrWhiteSpace(Form.ApplicationId) || string.IsNullOrWhiteSpace(Form.Prompt)) return; + if (Running || !RoutePreview.CanPreview(Form)) return; var key = RoutePreview.KeyOf(Form); - Preview = new RoutePreview(key, null, null, Loading: true); + var pending = new RoutePreview(key, null, null, Loading: true); + Preview = pending; Notify(); + RoutePreview done; try { var result = await _admin.SimulateAsync(new SimulateRequestDto { ApplicationId = Form.ApplicationId!, ProfileId = Form.ProfileId, Prompt = Form.Prompt, Model = Form.RequestedModel, }); - Preview = new RoutePreview(key, result, null, Loading: false); + done = new RoutePreview(key, result, null, Loading: false); } catch (Exception ex) { - Preview = new RoutePreview(key, null, "Route preview unavailable: " + ex.Message, Loading: false); + done = new RoutePreview(key, null, "Route preview unavailable: " + ex.Message, Loading: false); } + // Latest request wins: a quick second workload switch starts a newer dry run, and this older answer must not + // replace it (it would show the previous workload's route as "stale"). + if (!ReferenceEquals(Preview, pending)) return; + Preview = done; Notify(); } public bool CanRun => !Running && Problem is null; @@ -277,6 +283,15 @@ public bool SelectProfile(string? profileId) return ok; } + /// The composer's workload switch: the prompt is kept and the Expected route follows the switch at once, so the + /// same request visibly changes outcome (e.g. Internal → served locally, Public → rejected at Classify). The preview is + /// the Host's dry run for the NEW workload — nothing is sent to a model and nothing runs until Run. + public async Task SelectProfileAndPreviewAsync(string? profileId) + { + if (!SelectProfile(profileId)) return; + if (RoutePreview.CanPreview(Form)) await PreviewRouteAsync(); + } + /// After a manual edit: a preset-specific note is dropped once the form no longer holds that preset. public void NotifyFormChanged() { diff --git a/tests/Covenant.Tests/PreviewFreshnessTests.cs b/tests/Covenant.Tests/PreviewFreshnessTests.cs index d5a0123..dd6b62f 100644 --- a/tests/Covenant.Tests/PreviewFreshnessTests.cs +++ b/tests/Covenant.Tests/PreviewFreshnessTests.cs @@ -16,4 +16,26 @@ public void Changing_policy_configuration_invalidates_preview_even_when_prompt_i form.Prompt = "Different request"; Assert.False(preview.IsCurrent(form, 3)); } + + [Fact] + public void Switching_workload_makes_the_old_preview_stale_and_the_form_still_previewable_with_the_same_prompt() + { + // The Internal/Public comparison: the prompt is kept, only the workload changes. The old dry run no longer + // describes the form, and the form still holds everything a new dry run needs — so the switch re-previews. + var form = new PlaygroundForm { ApplicationId = "demo-app", ProfileId = "internal-default", Prompt = "SSN on file: 000-12-3456" }; + var preview = new RoutePreview(RoutePreview.KeyOf(form), new SimulateResponseDto { ConfigVersion = 4 }, null, false); + form.ProfileId = "public-demo"; + Assert.False(preview.IsCurrent(form, 4)); + Assert.True(RoutePreview.CanPreview(form)); + Assert.Equal("SSN on file: 000-12-3456", form.Prompt); + } + + [Theory] + [InlineData(null, "Hello", false)] + [InlineData("", "Hello", false)] + [InlineData("demo-app", "", false)] + [InlineData("demo-app", " ", false)] + [InlineData("demo-app", "Hello", true)] + public void A_dry_run_needs_a_sender_and_a_prompt(string? application, string prompt, bool expected) + => Assert.Equal(expected, RoutePreview.CanPreview(new PlaygroundForm { ApplicationId = application, Prompt = prompt })); }