|
| 1 | +# Llama Server Hardening Implementation Plan |
| 2 | + |
| 3 | +> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking. |
| 4 | +
|
| 5 | +**Goal:** Harden the local llama-server integration so chat responses are parsed consistently, startup survives caller cancellation, and runtime downloads are more resilient. |
| 6 | + |
| 7 | +**Architecture:** Centralize `/v1/chat/completions` parsing in a single helper used by translation and text-processing flows. Keep model startup as a shared background task owned by `QwenModelHost`, and strengthen runtime download behavior inside `NativeRuntimeUpdater` so transport failures do not corrupt the bootstrap path. |
| 8 | + |
| 9 | +**Tech Stack:** .NET 10, xUnit, NSubstitute, System.Text.Json, HttpClient |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +### Task 1: Centralize chat response parsing |
| 14 | + |
| 15 | +**Files:** |
| 16 | +- Modify: `src/LiveLingo.Core/Engines/LlamaTranslationEngine.cs` |
| 17 | +- Modify: `src/LiveLingo.Core/Processing/QwenTextProcessor.cs` |
| 18 | +- Create: `src/LiveLingo.Core/Processing/LlamaServerChatResponse.cs` |
| 19 | +- Test: `tests/LiveLingo.Core.Tests/Processing/LlamaServerChatResponseTests.cs` |
| 20 | + |
| 21 | +- [ ] **Step 1: Write the failing test** |
| 22 | + |
| 23 | +```csharp |
| 24 | +[Fact] |
| 25 | +public void GetAssistantText_reads_text_from_content_array() |
| 26 | +{ |
| 27 | + const string json = """ |
| 28 | + {"choices":[{"message":{"content":[{"type":"text","text":"Hi"}]}}]} |
| 29 | + """; |
| 30 | + using var doc = JsonDocument.Parse(json); |
| 31 | + Assert.Equal("Hi", LlamaServerChatResponse.GetAssistantText(doc.RootElement)); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +- [ ] **Step 2: Run test to verify it fails** |
| 36 | + |
| 37 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter LlamaServerChatResponseTests` |
| 38 | +Expected: FAIL because the helper does not yet exist or does not handle the response shape. |
| 39 | + |
| 40 | +- [ ] **Step 3: Write minimal implementation** |
| 41 | + |
| 42 | +Create a helper that: |
| 43 | +- reads `choices[0].message.content` |
| 44 | +- falls back to `reasoning_content` when `content` is blank |
| 45 | +- accepts either string content or OpenAI-style content arrays |
| 46 | +- strips Qwen `<think>` wrappers in one place |
| 47 | +- emits a short diagnostic string for empty-output logs |
| 48 | + |
| 49 | +- [ ] **Step 4: Run test to verify it passes** |
| 50 | + |
| 51 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter LlamaServerChatResponseTests` |
| 52 | +Expected: PASS |
| 53 | + |
| 54 | +- [ ] **Step 5: Commit** |
| 55 | + |
| 56 | +```bash |
| 57 | +git add src/LiveLingo.Core/Engines/LlamaTranslationEngine.cs \ |
| 58 | + src/LiveLingo.Core/Processing/QwenTextProcessor.cs \ |
| 59 | + src/LiveLingo.Core/Processing/LlamaServerChatResponse.cs \ |
| 60 | + tests/LiveLingo.Core.Tests/Processing/LlamaServerChatResponseTests.cs |
| 61 | +git commit -m "test: harden llama chat response parsing" |
| 62 | +``` |
| 63 | + |
| 64 | +### Task 2: Make Qwen model startup shareable across cancelled callers |
| 65 | + |
| 66 | +**Files:** |
| 67 | +- Modify: `src/LiveLingo.Core/Processing/QwenModelHost.cs` |
| 68 | +- Test: `tests/LiveLingo.Core.Tests/Processing/QwenModelHostTests.cs` |
| 69 | + |
| 70 | +- [ ] **Step 1: Write the failing test** |
| 71 | + |
| 72 | +```csharp |
| 73 | +[Fact] |
| 74 | +public async Task GetOrStartServerAsync_keeps_background_load_running_after_first_waiter_cancels() |
| 75 | +{ |
| 76 | + // Arrange host with a gate-controlled server startup. |
| 77 | + // Cancel the first caller before startup completes. |
| 78 | + // Assert a second caller later receives the same loaded endpoint. |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +- [ ] **Step 2: Run test to verify it fails** |
| 83 | + |
| 84 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter QwenModelHostTests` |
| 85 | +Expected: FAIL because startup is still bound to the first caller cancellation path. |
| 86 | + |
| 87 | +- [ ] **Step 3: Write minimal implementation** |
| 88 | + |
| 89 | +Update `QwenModelHost` so: |
| 90 | +- one shared `_ensureServerTask` owns startup/download work |
| 91 | +- the task is created under lock and run with `CancellationToken.None` |
| 92 | +- each caller can still cancel only its own wait via `WaitAsync(ct)` |
| 93 | +- reset paths clear the cached task |
| 94 | +- completion still verifies the server reached `Loaded` |
| 95 | + |
| 96 | +- [ ] **Step 4: Run test to verify it passes** |
| 97 | + |
| 98 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter QwenModelHostTests` |
| 99 | +Expected: PASS |
| 100 | + |
| 101 | +- [ ] **Step 5: Commit** |
| 102 | + |
| 103 | +```bash |
| 104 | +git add src/LiveLingo.Core/Processing/QwenModelHost.cs \ |
| 105 | + tests/LiveLingo.Core.Tests/Processing/QwenModelHostTests.cs |
| 106 | +git commit -m "test: preserve shared llama startup across cancellations" |
| 107 | +``` |
| 108 | + |
| 109 | +### Task 3: Make runtime downloads resumable and non-destructive |
| 110 | + |
| 111 | +**Files:** |
| 112 | +- Modify: `src/LiveLingo.Core/Processing/NativeRuntimeUpdater.cs` |
| 113 | +- Test: `tests/LiveLingo.Core.Tests/Processing/NativeRuntimeUpdaterTests.cs` |
| 114 | + |
| 115 | +- [ ] **Step 1: Write the failing test** |
| 116 | + |
| 117 | +```csharp |
| 118 | +[Fact] |
| 119 | +public async Task EnsureLatestLlamaServerAsync_resumes_partial_archive_download() |
| 120 | +{ |
| 121 | + // Arrange an HTTP handler that first returns partial content and then serves the tail. |
| 122 | + // Seed an archive file with partial bytes. |
| 123 | + // Assert the completed file length matches the advertised total. |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +- [ ] **Step 2: Run test to verify it fails** |
| 128 | + |
| 129 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter NativeRuntimeUpdaterTests` |
| 130 | +Expected: FAIL because downloads always restart from scratch. |
| 131 | + |
| 132 | +- [ ] **Step 3: Write minimal implementation** |
| 133 | + |
| 134 | +Add a resumable download helper that: |
| 135 | +- reuses a stable archive filename |
| 136 | +- sends `Range` when partial bytes already exist |
| 137 | +- appends only on `206 Partial Content` |
| 138 | +- retries transient HTTP / IO / timeout failures with backoff |
| 139 | +- validates final size when `Content-Range` exposes total length |
| 140 | + |
| 141 | +- [ ] **Step 4: Run test to verify it passes** |
| 142 | + |
| 143 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter NativeRuntimeUpdaterTests` |
| 144 | +Expected: PASS |
| 145 | + |
| 146 | +- [ ] **Step 5: Commit** |
| 147 | + |
| 148 | +```bash |
| 149 | +git add src/LiveLingo.Core/Processing/NativeRuntimeUpdater.cs \ |
| 150 | + tests/LiveLingo.Core.Tests/Processing/NativeRuntimeUpdaterTests.cs |
| 151 | +git commit -m "test: resume llama runtime downloads" |
| 152 | +``` |
| 153 | + |
| 154 | +### Task 4: Verify integration points only for the redesigned path |
| 155 | + |
| 156 | +**Files:** |
| 157 | +- Modify: `src/LiveLingo.Core/Processing/LlamaServerProcessManager.cs` |
| 158 | +- Verify: `tests/LiveLingo.Core.Tests/Processing/LlamaServerChatResponseTests.cs` |
| 159 | +- Verify: `tests/LiveLingo.Core.Tests/Processing/QwenModelHostTests.cs` |
| 160 | +- Verify: `tests/LiveLingo.Core.Tests/Processing/NativeRuntimeUpdaterTests.cs` |
| 161 | + |
| 162 | +- [ ] **Step 1: Add the smallest failing test or assertion for startup arguments if needed** |
| 163 | + |
| 164 | +```csharp |
| 165 | +// Add only if there is a practical seam for argument verification. |
| 166 | +``` |
| 167 | + |
| 168 | +- [ ] **Step 2: Run targeted tests to verify the redesigned path fails where expected** |
| 169 | + |
| 170 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter "LlamaServerChatResponseTests|QwenModelHostTests|NativeRuntimeUpdaterTests"` |
| 171 | +Expected: FAIL until all redesigned-path changes are complete. |
| 172 | + |
| 173 | +- [ ] **Step 3: Finalize implementation** |
| 174 | + |
| 175 | +Ensure the server starts with reasoning disabled and that translation now fails loudly on empty assistant output instead of silently echoing source text. |
| 176 | + |
| 177 | +- [ ] **Step 4: Run targeted verification** |
| 178 | + |
| 179 | +Run: `dotnet test tests/LiveLingo.Core.Tests/LiveLingo.Core.Tests.csproj --filter "LlamaServerChatResponseTests|QwenModelHostTests|NativeRuntimeUpdaterTests"` |
| 180 | +Expected: PASS |
| 181 | + |
| 182 | +- [ ] **Step 5: Commit** |
| 183 | + |
| 184 | +```bash |
| 185 | +git add src/LiveLingo.Core/Processing/LlamaServerProcessManager.cs \ |
| 186 | + src/LiveLingo.Core/Engines/LlamaTranslationEngine.cs \ |
| 187 | + src/LiveLingo.Core/Processing/QwenTextProcessor.cs \ |
| 188 | + src/LiveLingo.Core/Processing/QwenModelHost.cs \ |
| 189 | + src/LiveLingo.Core/Processing/NativeRuntimeUpdater.cs |
| 190 | +git commit -m "feat: harden local llama server integration" |
| 191 | +``` |
0 commit comments