feat(m2-m3): adapter, text pipeline, double-copy capture and Services (T2.1-T2.7, T3.1-T3.4)

M2: TranslationAdapter/AdapterRegistry with documented Hy-MT3 prompts and publisher sampling; all 102 languages with BCP-47 variants, RTL and dense-script flags; normalisation, NLLanguage detection, content classification and exact-join script-aware chunking; protected-token scanning and output validation; prompt policy with one documented fallback; shared TranslationPipeline with Core error mapping. The CLI now reports sourceLanguage and warning codes.

M3: pure DoubleCopyDetector, 120 ms PasteboardWatcher with 750 ms own-write suppression, SystemPasteboard snapshot/restore and markers, CapturePolicy for the 4000/2000 limits, ServicesProvider and the menu-bar action. T3.5 (10-app manual matrix) remains intentionally open.

Docs: ARCHITECTURE, TRANSLATION_OUTPUT, HYMT3_MODEL (§7 spike), SELECTION_AND_CLIPBOARD, PROGRESS; ADR-016 superseded by ADR-017/018.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, package tests); live 7B CLI en→de/de→en/Markdown/JSON/placeholders; empty input exits 2 and unreachable exits 3.
This commit is contained in:
schihei committed 2026-11-26 13:30:32 +01:00
1 parent 26b493e7e5
commit 64078e2f04
80 files changed
+4090 -325

No files matched your search

+36 -20
View File
@@ -145,7 +145,7 @@ flowchart TD
- `SprachmittlerCapture` is the only module that imports `ApplicationServices`/`Accessibility` and touches `NSPasteboard`. It depends on `SprachmittlerCore` for domain types and protocols.
- `SprachmittlerEval` is linked into the CLI only, never into the app.
- `sprachmittler-cli` links `SprachmittlerOllama` and `SprachmittlerEngines` directly for the M1 commands; it is never linked into the app.
- `SprachmittlerTestSupport` is test-only and depends on `SprachmittlerCore` and `SprachmittlerOllama`; it is never linked into the app.
- `SprachmittlerTestSupport` is test-only and depends on `SprachmittlerCore`, `SprachmittlerCapture` and `SprachmittlerOllama`; it is never linked into the app.
- The `App` target holds no business logic. Anything with a branch worth testing belongs in a package module.
### Third-party dependencies
@@ -163,16 +163,18 @@ All other functionality uses Apple frameworks. No keyboard-shortcut library is n
```swift
// SprachmittlerCore
public struct LanguageCode: RawRepresentable, Hashable, Codable, Sendable {
public let rawValue: String // "de", "en", "zh-Hant", "pt-BR", "kk-Cyrl"
}
// Defined in SprachmittlerEngines; re-exported by SprachmittlerCore.
public typealias LanguageCode = String // "de", "en", "zh-Hant", "pt-BR", "kk-Cyrl"
public struct TargetLanguage: Hashable, Codable, Sendable, Identifiable {
public struct ModelLanguage: Hashable, Codable, Sendable, Identifiable {
public let code: LanguageCode
public let modelName: String // exact English name from the model card, e.g. "German"
public let displayName: String // localised UI name
public var id: String { code.rawValue }
public let displayName: String // English fallback; localizedDisplayName for UI
public let isRTL: Bool
public let usesDenseScript: Bool
public var id: String { code }
}
public typealias TargetLanguage = ModelLanguage
public struct CapturedText: Sendable, Equatable {
public let text: String
@@ -210,14 +212,14 @@ public struct TranslationResult: Sendable, Equatable {
}
public enum TranslationWarning: Sendable, Equatable {
case preambleDetected(line: String)
case preambleDetected(firstLine: String)
case truncated
case repetitionLoop
case missingProtectedToken(String)
case emptyOutput
case sameLanguage(source: LanguageCode, target: LanguageCode)
case chunkFailed(index: Int)
case sameLanguage(source: String, target: String)
case numericMismatch
case chunkFailed(index: Int)
}
public struct TranslationTiming: Sendable, Equatable {
@@ -228,7 +230,7 @@ public struct TranslationTiming: Sendable, Equatable {
}
public enum InsertionOutcome: Sendable, Equatable {
case replaced(by: InsertionMethod) // .accessibility, .paste
case replaced(using: InsertionMethod) // .accessibility, .paste
case copiedOnly
case failed(SprachmittlerError)
}
@@ -238,6 +240,8 @@ public enum InsertionMethod: String, Sendable {
}
```
Language and warning value types live in `SprachmittlerEngines`, because they carry model-card names and validation results: `ModelLanguage` (re-exported as `TargetLanguage`) and `TranslationWarning`. `SprachmittlerCore` re-exports both for the session and UI. `TranslationPipeline` in Core owns chunk orchestration, the one documented retry and timing.
### Error taxonomy
```swift
@@ -332,18 +336,28 @@ Full detail: `docs/SELECTION_AND_CLIPBOARD.md`.
### 7.1 Double-copy watcher (`SprachmittlerCapture`)
```swift
public protocol PasteboardWatching: Sendable {
func events() -> AsyncStream<PasteboardEvent>
@MainActor public protocol PasteboardAccessing: AnyObject {
var changeCount: Int { get }
func string() -> String?
func containsOwnWriteMarkers() -> Bool
func snapshot() -> ClipboardSnapshot?
func restore(_ snapshot: ClipboardSnapshot, onlyIfChangeCountMatches: Int) -> Bool
@discardableResult func write(_ text: String) -> Int
}
public enum PasteboardEvent: Sendable, Equatable {
case changed(changeCount: Int)
case textAvailable(String)
case ignoredOwnWrite
public struct DoubleCopyDetector { /* pure state machine */ }
@MainActor public final class PasteboardWatcher {
init(pasteboard: any PasteboardAccessing,
ticker: any PasteboardTicking = TimerPasteboardTicker())
func start(onEvent: @escaping @MainActor (PasteboardWatcherEvent) -> Void)
func stop()
func clear()
func noteOwnWrite(at: Date)
}
```
- Poll `NSPasteboard.general.changeCount` on a `@MainActor` timer every **120 ms**.
- Poll `NSPasteboard.general.changeCount` every **120 ms** on the main actor through `PasteboardTicking`.
- A candidate trigger is two changes within **500 ms** (range 300–1000 ms, setting) whose `.string` values are identical and non-empty after trimming.
- Require at least one non-whitespace character and at most `maxCharacters` (4000 Latin / 2000 CJK by default).
- Ignore changes whose pasteboard contains the app's own marker (`com.schihei.sprachmittler.own`) or `org.nspasteboard.TransientType`, and ignore all changes for 750 ms after our own write.
@@ -461,6 +475,8 @@ Thresholds are constants in one file and covered by tests. Quality failures are
Full detail: `docs/TRANSLATION_OUTPUT.md`.
`TranslationPipeline` in `SprachmittlerCore` is the shared implementation used by the CLI and the M4 session: it normalises input, detects the language, classifies content, scans protected tokens, chunks, calls the adapter, joins chunk results with the original separators, validates once, and enforces the single documented retry.
### 9.1 Input analysis (`SprachmittlerText`)
1. Normalise line endings to `\n`; strip NUL and other C0 controls except `\t` and `\n`; NFC.
@@ -474,8 +490,8 @@ Full detail: `docs/TRANSLATION_OUTPUT.md`.
| Content | Prompt |
|---|---|
| Plain prose | Documented **Default Translation** |
| Markdown / JSON / HTML | Documented **Structured Data 1** with `format_type` |
| Placeholder- or delimiter-heavy | Documented **Delimiters** |
| Markdown / JSON / HTML | Documented **Default Translation** first; **Structured Data 1** is the single fallback after a lost token |
| Placeholder- or delimiter-heavy | Documented **Default Translation** first; **Delimiters** is the single fallback after a lost token |
| User glossary (future) | Documented **Terminology** |
| User style (future) | Documented **Style** |