feat(m4-m6): session, panel, Replace, settings and onboarding

M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions.

M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate.

M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup.

Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
This commit is contained in:
schihei committed 2026-11-26 14:03:16 +01:00
1 parent 64078e2f04
commit a78572adc3
47 files changed
+4963 -79

No files matched your search

+4 -2
View File
@@ -516,7 +516,7 @@ If the detected source language equals the target with reasonable confidence, th
## 10. Translation session and state machine
`TranslationSession` is an actor in `SprachmittlerCore`; `PanelViewModel` is `@MainActor @Observable` and mirrors it.
`TranslationSession` is an actor in `SprachmittlerCore`; `PanelViewModel` is `@MainActor @Observable` in Core and mirrors it. The App target owns the non-activating `NSPanel` controller.
```mermaid
stateDiagram-v2
@@ -540,7 +540,7 @@ stateDiagram-v2
- Every session has an epoch (`UInt64`). A new trigger increments it; events from an older epoch are ignored.
- `Copy` never changes the session state; it writes the clipboard and keeps the panel open.
- `Replace` is disabled while `.translating` or `.failed`, and when validation found a blocking warning.
- The session owns no disk state. It releases source and translation when the panel closes.
- The session owns no disk state. Its working copy is released when the panel closes; `AppEnvironment` may keep the last source, translation and insertion target in memory until quit, solely for the status menu's Copy/Replace Last actions (ADR-007). Nothing is written to disk.
---
@@ -571,6 +571,7 @@ Accessibility: every control has an `accessibilityLabel`; the panel exposes sour
| `model` | String | `hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M` (provisional until T7.4) |
| `fastModel` | String? | `hf.co/tencent/Hy-MT3-1.8B-GGUF:Q4_K_M` (if installed) |
| `targetLanguage` | LanguageCode | system language if supported, else `en` |
| `recentTargetLanguages` | [String] | last three targets, most recent first |
| `doubleCopyWindowMs` | Int | 500 (300–1000) |
| `maxCharactersLatin` | Int | 4000 |
| `maxCharactersCJK` | Int | 2000 |
@@ -587,6 +588,7 @@ Accessibility: every control has an `accessibilityLabel`; the panel exposes sour
| `showSourcePreview` | Bool | true |
| `launchAtLogin` | Bool | false |
| `keepRecentTranslationInMemory` | Bool | true (cleared on quit; no disk) |
| `hasCompletedOnboarding` | Bool | false |
| `settingsVersion` | Int | 1 |
Never store source text, translations, pasteboard contents or app names in settings.
+14
View File
@@ -129,3 +129,17 @@ Status: Accepted
Context: Double copy, clipboard snapshot/restore and Services all touch `NSPasteboard`, but the state machine and watcher need deterministic tests without real time or the user's clipboard. The Services path must work with no Accessibility permission.
Decision: `SprachmittlerCapture` defines `PasteboardAccessing` and `PasteboardTicking`. `SystemPasteboard` is the only production `NSPasteboard` user; `FakePasteboard` and `ManualPasteboardTicker` live in `SprachmittlerTestSupport`. `DoubleCopyDetector` is a pure state machine over change events and an injected date. `PasteboardWatcher` polls every 120 ms, reads the string only after `changeCount` changes, suppresses own/transient changes for 750 ms, and enforces the 4000/2000 limit. `ServicesProvider` receives the system pasteboard and needs no polling or Accessibility. `AppDelegate` installs it and calls `NSUpdateDynamicServices`.
Consequences: Capture logic is unit-testable and no global key tap is needed. The Services and double-copy paths both produce `CapturedText`. The 10-app compatibility matrix remains a manual task.
## ADR-019 — AX first, paste fallback, target re-verified at Replace
Status: Accepted
Context: Replace must be safe and reversible, but Accessibility support differs by app. AX calls can block, so they must stay off the main actor. Tests need deterministic insertion without a real app or clipboard.
Decision: `InsertionCoordinator` lives in `SprachmittlerCapture` and uses `AccessibilityTextEditing` and `EventPosting` seams. At trigger time the app captures the frontmost PID/bundle and, when trusted, reads only the focused selection; at Replace it re-verifies the frontmost app and the selected text (`InsertionPolicy`). It refuses secure fields and its own PID. AX direct replacement is tried first; if unsupported, it snapshots the clipboard, writes the translation with own/transient markers, posts ⌘V to the target PID, waits up to 300 ms and restores only when `changeCount` still matches the write. Blocking warnings disable Replace; failures keep the panel open and offer Copy.
Consequences: Most apps use the no-clipboard AX path; the fallback preserves the previous clipboard in the common case. Manual testing is still required because AX support and paste delivery are app-specific.
## ADR-020 — UI state models in Core, AppKit controllers in App
Status: Accepted
Context: Panel, Settings, Onboarding and Diagnostics contain branching worth testing, but their windows are AppKit/SwiftUI. The app target must stay thin.
Decision: `PanelViewModel`, `SettingsViewModel`, `OnboardingViewModel` and `DiagnosticsViewModel` are `@MainActor @Observable` types in `SprachmittlerCore`; `SettingsStore` uses a `SettingsDataStoring` seam. `App` owns only `NSPanel`/`NSWindow` controllers, SwiftUI views, the composition root (`AppEnvironment`) and lifecycle glue. `TranslationSession` uses an injected pipeline factory so endpoint/settings changes apply on the next request without rebuilding the UI.
Consequences: Logic is covered by package tests; AppKit remains untested by `make verify` and requires the manual checklist.
+34 -16
View File
@@ -37,26 +37,26 @@ Legend: `[ ]` open · `[~]` in progress · `[x]` done · `[!]` blocked (explain)
- [~] T3.5 Manual double-copy matrix — 2026-11-26 · code complete; 10-app matrix needs a person
## M4 — Translation session and panel
- [ ] T4.1 TranslationSession actor and epochs
- [ ] T4.2 Panel controller and view model
- [ ] T4.3 Panel view with Copy and Replace
- [ ] T4.4 Target language, persistence, same-language
- [ ] T4.5 Error messages and recovery actions
- [ ] T4.6 Panel keyboard and accessibility
- [x] T4.1 TranslationSession actor and epochs — 2026-11-26
- [~] T4.2 Panel controller and view model — 2026-11-26 · code/test complete; manual focus/position pending
- [~] T4.3 Panel view with Copy and Replace — 2026-11-26 · code complete; manual streaming/Copy/Replace pending
- [x] T4.4 Target language, persistence, same-language — 2026-11-26
- [x] T4.5 Error messages and recovery actions — 2026-11-26
- [~] T4.6 Panel keyboard and accessibility — 2026-11-26 · shortcuts/labels in code; VoiceOver manual pending
## M5 — Replace
- [ ] T5.1 InsertionCoordinator and target capture
- [ ] T5.2 Accessibility direct replacement
- [ ] T5.3 Paste fallback and clipboard restore
- [ ] T5.4 Safety checks and Copy fallback
- [ ] T5.5 Manual insertion matrix and permission onboarding
- [x] T5.1 InsertionCoordinator and target capture — 2026-11-26
- [x] T5.2 Accessibility direct replacement — 2026-11-26
- [x] T5.3 Paste fallback and clipboard restore — 2026-11-26
- [x] T5.4 Safety checks and Copy fallback — 2026-11-26
- [~] T5.5 Manual insertion matrix and permission onboarding — 2026-11-26 · 10-app matrix needs a person
## M6 — Settings, onboarding, menu bar
- [ ] T6.1 AppSettings and persistence
- [ ] T6.2 Onboarding checks and window
- [ ] T6.3 Settings UI
- [ ] T6.4 Status menu, launch at login, diagnostics
- [ ] T6.5 End-to-end integration checkpoint (gate)
- [x] T6.1 AppSettings and persistence — 2026-11-26
- [~] T6.2 Onboarding checks and window — 2026-11-26 · code complete; manual checks pending
- [~] T6.3 Settings UI — 2026-11-26 · code complete; manual endpoint/licence checks pending
- [~] T6.4 Status menu, launch at login, diagnostics — 2026-11-26 · code complete; manual login/diagnostics checks pending
- [~] T6.5 End-to-end integration checkpoint (gate) — 2026-11-26 · needs the user's manual 10-app pass
## M7 — Evaluation and the default-model gate
- [ ] T7.1 Corpus manifest and eval CLI
@@ -120,3 +120,21 @@ Legend: `[ ]` open · `[~]` in progress · `[x]` done · `[!]` blocked (explain)
2026-11-26 · T3.4 · Added `ServicesProvider` and wired `NSApp.servicesProvider` + `NSUpdateDynamicServices`; the menu-bar “Translate Clipboard Text” item now delivers a `.menu` capture. The app has a log-only sink until the M4 panel.
2026-11-26 · M3 · The capture matrix (T3.5) is intentionally still unchecked; it needs a person in 10 real apps. No Accessibility is requested on the double-copy or Services path.
2026-11-26 · M2/M3 · Review fixes: `OllamaError` is mapped to `SprachmittlerError` at the Core boundary and the CLI maps domain errors to exit 2/3/4; the chunker preserves leading empty separators; `LanguageMatching` moved to Engines and `keepAlive` is supplied by the composition root; `CapturePolicy` enforces the 4000/2000 limit and own markers for menu/Services; golden prompt tests remove exactly one fixture terminator; docs aligned (TestSupport deps, warning enum, 15 common targets, Fixtures path).
2026-11-26 · T4.1 · Added `TranslationSession` actor with epochs, one in-flight task, delta forwarding, cancellation and stale-event dropping; tests cover streaming, replacement, cancel and factory failures.
2026-11-26 · T4.2 · Added `PanelViewModel` in Core and a non-activating `NSPanel` controller in the app; the panel positions near the mouse and never calls `NSApp.activate`.
2026-11-26 · T4.3 · Added the SwiftUI panel: source preview, streaming translation, warning/error banners, Copy, Retry, Replace, close; Replace is disabled while translating and for blocking warnings.
2026-11-26 · T4.4 · Added searchable language picker, recent targets, target persistence and retranslation on change; same-language advisory with “Translate anyway”.
2026-11-26 · T4.5 · Added `SprachmittlerError.userMessage`, `RecoveryAction` and an exhaustive test; raw server/model text is never shown.
2026-11-26 · T4.6 · Added Esc, ⌘C, ⌘↩, ⌘R, ⌘, paths and accessibility labels; VoiceOver verification remains manual.
2026-11-26 · T5.1 · Target provider captures the frontmost PID/bundle at trigger and reads only the focused selection when Accessibility is trusted; the coordinator verifies the target again at Replace.
2026-11-26 · T5.2 · Added `AXTextEditor` (focused element, selected text, secure-field check, direct set) behind a testable protocol; AX work runs on an actor, never the main actor.
2026-11-26 · T5.3 · Added `CGEventPoster` ⌘V to the target PID, full snapshot/write/restore and a 300 ms settle; own writes suppress the watcher.
2026-11-26 · T5.4 · Added `InsertionPolicy` frontmost/selection checks, secure-field refusal, self-insertion refusal, blocking-warning gate and Copy fallback.
2026-11-26 · T5.5 · The insertion matrix and real-app permission flow remain manual; unit/component tests cover the strategies and failure paths.
2026-11-26 · T6.1 · Added versioned `AppSettings` with v0 migration, clamping, unknown-key tolerance, recent targets, `SettingsStore` over a UserDefaults seam and reset.
2026-11-26 · T6.2 · Added onboarding for Ollama/version, Hy-MT3 install, target and optional Accessibility, with fix actions; first-run and menu reopen wired.
2026-11-26 · T6.3 · Added General/Model/Translation/Privacy/Diagnostics tabs, endpoint validation, model/digest/licence display and reset.
2026-11-26 · T6.4 · Added the status menu (translate, target, copy/replace last, setup, diagnostics, about, settings, quit), launch-at-login via `SMAppService`, and a text-free diagnostics window.
2026-11-26 · T6.5 · The end-to-end gate is ready to run but not marked complete: it needs the user's manual double-copy → panel → Copy/Replace pass.
2026-11-26 · M4/M5/M6 smoke · Launched the Debug app and sent two identical clipboard copies; the non-activating panel appeared on screen without a crash. Full focus, Copy/Replace, VoiceOver, onboarding and 10-app matrices remain manual.
2026-11-26 · M4/M5/M6 review · Cleared the previous translation when a new capture arrives (menu Replace cannot pair old text with a new selection); restored the clipboard on cancelled/failed paste; reset the panel before showing a new capture; mapped blocked Replace to the real warning; mapped endpoint/transport errors in onboarding and settings; surfaced launch-item errors; moved Fast-model detection to Engines; suppressed retry deltas and streamed chunk separators; made settings updates atomic and fastModel nullable; fixed refresh endpoint validation; localized warning strings; used the captured selection for an early safety check; the event observer now sees only current-epoch events and only a validated `.ready` result can become the status-menu last translation; insertion-target enrichment is generation-guarded. `make verify` green.
+1 -1
View File
@@ -160,7 +160,7 @@ Info.plist:
**This table is filled in by a person, on a real Mac.** Double copy, Accessibility replacement and clipboard restoration differ in every app and cannot be tested headlessly. Until a row is tested its cells stay ☐; an untested cell is not a claim.
Implementation status (2026-11-26): T3.1–T3.4 are implemented and covered by component tests (`DoubleCopyDetector`, `PasteboardWatcher`, `ClipboardSnapshot`, `ServicesProvider`). The 4000/2000 limit and own-marker check apply to double copy, the menu item and Services (`CapturePolicy`). The matrix below is still untested; it is the T3.5 manual task.
Implementation status (2026-11-26): T3.1–T3.4 and T5.1–T5.4 are implemented and covered by component tests (`DoubleCopyDetector`, `PasteboardWatcher`, `ClipboardSnapshot`, `ServicesProvider`, `InsertionCoordinator`). The 4000/2000 limit and own-marker check apply to double copy, the menu item and Services (`CapturePolicy`). Replace captures the target at trigger time, re-verifies it, prefers AX and falls back to a snapshot/⌘V/restore path. The 10-app double-copy and insertion matrices remain untested; they are the T3.5/T5.5 manual tasks.
For each app test:
+3
View File
@@ -68,6 +68,9 @@ Evaluation:
- [ ] Translate Markdown and JSON: keys/formatting survive.
- [ ] Change the target language while streaming: the old request is cancelled and the new one starts.
- [ ] Services ▸ Translate with Sprachmittler works without Accessibility.
- [ ] Settings: an invalid remote `http://` endpoint is blocked; reset restores defaults; model/digest/licence are shown.
- [ ] Onboarding reopens from the menu; Ollama stopped, model missing and Accessibility denied each show a fix action.
- [ ] Launch at login follows `SMAppService`; Diagnostics shows versions, digests, timings and error kinds but no text.
- [ ] VoiceOver can operate the panel and Settings; every action has a keyboard path.
- [ ] Console.app shows no selected text, translation, pasteboard content, app name or path.
- [ ] Quit during a translation: no orphaned request; `ollama ps` shows no hanging request.