diff --git a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Acceptance.swift b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Acceptance.swift index 887bc6741..b08ac81f2 100644 --- a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Acceptance.swift +++ b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Acceptance.swift @@ -219,7 +219,7 @@ extension SuggestionCoordinator { // hosts would otherwise map the published caret into pre-insert run frames for up to a // throttle window. Stamp the acceptance so the stability gate can scope its // backward-drift hold to the frames-catching-up window. - lastAcceptanceAt = Date() + lastAcceptanceAt = ProcessInfo.processInfo.systemUptime focusModel.invalidateTransientCaretCaches() if preparedContinuation == nil, sessionForAcceptance.advancing(by: acceptedChunk.count).isExhausted { @@ -475,7 +475,7 @@ extension SuggestionCoordinator { return false } - lastAcceptanceAt = Date() + lastAcceptanceAt = ProcessInfo.processInfo.systemUptime focusModel.invalidateTransientCaretCaches() if preparedContinuation == nil { prepareContinuation(after: session, rawContext: rawContext) } cancelPredictionWork(preservingContinuation: true) @@ -696,6 +696,9 @@ extension SuggestionCoordinator { clearSuggestion(clearDiagnostics: clearDiagnostics) hideOverlay(reason: reason) state = .idle + // The tail a deferred screen-context change was waiting to retire is gone, and the caller + // decides whether to predict; left pending, it would cancel that newer work on waking. + settleDeferredScreenContextChange() } func completeActiveSuggestion( @@ -709,6 +712,8 @@ extension SuggestionCoordinator { clearSuggestion(clearDiagnostics: false, preservingContinuation: preparedContinuation?.awaitingCommit == true) hideOverlay(reason: reason) state = .idle + // Same as invalidation: the tail is consumed and the prediction below must not be cancelled. + settleDeferredScreenContextChange() logStage(stage, workID: currentWorkID, generation: generation, message: message) if scheduleNextPrediction { diff --git a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Input.swift b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Input.swift index 4ec63409a..d26446bdd 100644 --- a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Input.swift +++ b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Input.swift @@ -259,6 +259,8 @@ extension SuggestionCoordinator { clearSuggestion(clearDiagnostics: true) hideOverlay(reason: "Overlay hidden because the emoji picker is active.") } + // The picker owns input now; a deferred screen-context change must not predict under it. + settleDeferredScreenContextChange() return false } @@ -320,6 +322,8 @@ extension SuggestionCoordinator { // Always kill pending work: a stale host-publish chain or debounce from the previous // keystroke must not fire a prediction this event meant to cancel. cancelPredictionWork() + // Nor may a deferred screen-context change: this input decides whether to predict. + settleDeferredScreenContextChange() if hasSuggestionArtifactsToClear { clearSuggestion(clearDiagnostics: true) hideOverlay(reason: SuggestionSessionReconciler.overlayHideReason(for: event)) diff --git a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Lifecycle.swift b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Lifecycle.swift index ef774b5b4..ded55b6b6 100644 --- a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Lifecycle.swift +++ b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Lifecycle.swift @@ -17,6 +17,8 @@ extension SuggestionCoordinator { func stop() { CotabbyLogger.suggestion.info("Suggestion coordinator stopping") suggestionPresentationTiming.clear() + // A deferred screen-context retirement would otherwise wake after stop and schedule work. + settleDeferredScreenContextChange() cancelPredictionWork() resetCachedGenerationContext() visualContextCoordinator.cancel(resetState: true) @@ -34,6 +36,9 @@ extension SuggestionCoordinator { func prepareForRuntimeModelSwitch() { CotabbyLogger.suggestion.info("Preparing for runtime model switch, clearing active state") suggestionPresentationTiming.clear() + // The visual-context reset below makes a deferred screen-context change moot; left pending, + // it would wake after the switch and retire whatever the new model shows. + settleDeferredScreenContextChange() cancelPredictionWork() resetCachedGenerationContext() interactionState.resetAll() @@ -54,6 +59,9 @@ extension SuggestionCoordinator { CotabbyLogger.suggestion.info("Settings changed, resetting suggestion state") settingsSnapshot = snapshot + // Same as a model switch: the reset below restarts visual context and schedules its own + // prediction, which a deferred screen-context change would otherwise retire on waking. + settleDeferredScreenContextChange() cancelPredictionWork() resetCachedGenerationContext() clearSuggestion(clearDiagnostics: true) diff --git a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Prediction.swift b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Prediction.swift index 3853fd634..135d68592 100644 --- a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Prediction.swift +++ b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator+Prediction.swift @@ -1206,7 +1206,7 @@ extension SuggestionCoordinator { // the pre-insertion one; re-anchoring to it is the left-then-right accept jitter. isAwaitingPostInsertionSync: interactionState.isAwaitingPostInsertionSync, millisecondsSinceLastAcceptance: lastAcceptanceAt.map { - Int(Date().timeIntervalSince($0) * 1000) + Int((ProcessInfo.processInfo.systemUptime - $0) * 1000) } ) { presentOverlay( @@ -1422,6 +1422,69 @@ extension SuggestionCoordinator { // MARK: - Visual Context + /// Reacts to a changed screen-context excerpt for `identity`: retires the visible tail and + /// cached/async work, then regenerates against the new excerpt. + /// + /// A host may expose identical URL/title/geometry for two chats, so changed screen text is the + /// navigation signal there; keeping an old tail stable would let it outlive arbitrarily many + /// refreshes. Accepting words changes the screen too, though, so within the post-accept hold + /// the retirement is deferred, not dropped: the excerpt is already stored and a later refresh + /// of the same text would not fire again, so dropping it would lose a real navigation for good. + /// The deferred call re-runs this method, which defers again while accepts keep landing and + /// otherwise retires exactly as an undeferred change would. The identity guard turns a + /// deferral that outlived its field into a no-op. + func handleInjectedContextReady(_ identity: FocusedInputIdentity) { + guard focusModel.snapshot.context?.identity == identity else { return } + + let remainingMilliseconds = SuggestionAvailabilityEvaluator.postAcceptanceHoldRemainingMilliseconds( + lastAcceptanceUptime: lastAcceptanceAt, + nowUptime: ProcessInfo.processInfo.systemUptime, + holdMilliseconds: suggestionSettings.postAcceptHoldMilliseconds + ) + deferredScreenContextRetirement?.cancel() + deferredScreenContextRetirement = nil + if remainingMilliseconds > 0 { + logStage( + "screen-context-held", + workID: currentWorkID, + generation: latestGenerationNumber, + message: "Deferred a screen-context change for \(remainingMilliseconds)ms after an accept." + ) + // `Task` inherits the coordinator's main actor; cancellation (a newer change, or stop) + // wakes the sleep early, and the `isCancelled` check keeps that wake from acting. + deferredScreenContextRetirement = Task { [weak self] in + try? await Task.sleep(nanoseconds: UInt64(remainingMilliseconds) * 1_000_000) + guard !Task.isCancelled else { return } + self?.handleInjectedContextReady(identity) + } + return + } + + suggestionAnchorCache = SuggestionAnchorCache() + clipboardPrefaceMemo = nil + cancelPredictionWork() + clearSuggestion() + hideOverlay(reason: "Overlay hidden because screen context changed.") + schedulePredictionForCurrentFocusIfPossible(matching: identity) + } + + /// Resolves a deferred screen-context change without predicting. The deferral exists to retire + /// the tail the user was accepting, so anything that ends or completes that tail settles it: + /// invalidation (a caret move, dismissal, mismatched typing, shortcut), typing through it, + /// input with no session, the emoji picker, and a settings change, model switch, or stop. Each + /// of those already decides whether to predict, and a deferral waking later would either show + /// a suggestion nobody typed for or cancel the newer request. Only a keystroke that matches, + /// and so keeps the tail alive, leaves it pending. The caches the change would have retired + /// are still dropped, so a navigation it stood for cannot resurface through a rollback or a + /// pinned preface. + func settleDeferredScreenContextChange() { + guard let deferred = deferredScreenContextRetirement else { return } + deferred.cancel() + deferredScreenContextRetirement = nil + suggestionAnchorCache = SuggestionAnchorCache() + clipboardPrefaceMemo = nil + } + /// Once screenshot context becomes ready, regenerate only if the user is still in the same /// field and there is enough typed text for a real inline completion request. func schedulePredictionForCurrentFocusIfPossible(matching identity: FocusedInputIdentity) { diff --git a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator.swift b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator.swift index 298ec3811..7ea5a3460 100644 --- a/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator.swift +++ b/Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator.swift @@ -147,10 +147,16 @@ final class SuggestionCoordinator: ObservableObject { /// accept on the last word. See `SuggestionSessionReconciler.isStaleAcceptanceEcho`. var lastAcceptedTail: AcceptedSuggestionTail? - /// Wall-clock moment of the most recent committed acceptance. The stability gate uses its age - /// to scope the backward-drift hold: only geometry read shortly after our own insert can be - /// the stale-frame kind, so older backward corrections stay re-anchorable. - var lastAcceptanceAt: Date? + /// `ProcessInfo.systemUptime` of the most recent committed acceptance. Monotonic, so a clock + /// change cannot stretch either window keyed on its age. The stability gate uses it to scope + /// the backward-drift hold: only geometry read shortly after our own insert can be the + /// stale-frame kind, so older backward corrections stay re-anchorable. The post-accept hold + /// uses it to defer screen-context retirement while the user is still accepting. + var lastAcceptanceAt: TimeInterval? + + /// The pending re-check of a screen-context change deferred by the post-accept hold. Replaced + /// by each newer change and cancelled on stop, so at most one retirement is ever owed. + var deferredScreenContextRetirement: Task? /// Bounded string-only memory of recent suggestions for instant re-show on rollback and /// re-entry (see `SuggestionAnchorCache`). `cotabbyAnchorReuseDisabled` is the kill switch. @@ -336,16 +342,7 @@ final class SuggestionCoordinator: ObservableObject { } visualContextCoordinator.onInjectedContextReady = { [weak self] identity in - guard let self, self.focusModel.snapshot.context?.identity == identity else { return } - // A host may expose identical URL/title/geometry for two chats. Changed screen text - // is then our next navigation signal. Retire visible tails as well as cached/async - // work; keeping an old tail stable would let it outlive arbitrarily many refreshes. - self.suggestionAnchorCache = SuggestionAnchorCache() - self.clipboardPrefaceMemo = nil - self.cancelPredictionWork() - self.clearSuggestion() - self.hideOverlay(reason: "Overlay hidden because screen context changed.") - self.schedulePredictionForCurrentFocusIfPossible(matching: identity) + self?.handleInjectedContextReady(identity) } visualContextCoordinator.refreshContextProvider = { [weak self] in self?.currentVisualRefreshContext() diff --git a/Cotabby/Models/Settings/SuggestionSettingsData.swift b/Cotabby/Models/Settings/SuggestionSettingsData.swift index ef8882ef6..9c3445102 100644 --- a/Cotabby/Models/Settings/SuggestionSettingsData.swift +++ b/Cotabby/Models/Settings/SuggestionSettingsData.swift @@ -50,6 +50,8 @@ struct SuggestionCompletionSettings: Equatable { var showFollowingWords: Bool var autoAcceptTrailingPunctuation: Bool var addSpaceAfterAccept: Bool + /// How long after an accept a screen-context change may not replace the live suggestion; 0 is off. + var postAcceptHoldMilliseconds: Int var streamSuggestionsWhileGenerating: Bool /// Keeps an on-device request alive through matching typing, independently of partial display. var predictAheadWhileTyping: Bool @@ -290,6 +292,11 @@ extension SuggestionSettingsData { set { completion.addSpaceAfterAccept = newValue } } + var postAcceptHoldMilliseconds: Int { + get { completion.postAcceptHoldMilliseconds } + set { completion.postAcceptHoldMilliseconds = newValue } + } + var predictAheadWhileTyping: Bool { get { completion.predictAheadWhileTyping } set { completion.predictAheadWhileTyping = newValue } diff --git a/Cotabby/Models/Settings/SuggestionSettingsModel.swift b/Cotabby/Models/Settings/SuggestionSettingsModel.swift index ca481a454..9c7aec8c9 100644 --- a/Cotabby/Models/Settings/SuggestionSettingsModel.swift +++ b/Cotabby/Models/Settings/SuggestionSettingsModel.swift @@ -131,6 +131,10 @@ final class SuggestionSettingsModel: ObservableObject { @Published private(set) var preferredEmojiGender: EmojiGender @Published private(set) var autoAcceptTrailingPunctuation: Bool @Published private(set) var addSpaceAfterAccept: Bool + /// How long after an accept a screen-context change waits before it may replace the suggestion. + /// Read live by the coordinator; not part of `snapshot`, because each snapshot change resets the + /// pipeline (KV cache, OCR session) and the slider would emit one per tick while dragged. + @Published private(set) var postAcceptHoldMilliseconds: Int @Published private(set) var streamSuggestionsWhileGenerating: Bool /// The store owns durability; snapshots carry this live choice to the coordinator. @Published private(set) var predictAheadWhileTyping: Bool @@ -195,6 +199,12 @@ final class SuggestionSettingsModel: ObservableObject { static let minimumGhostTextOpacity = SuggestionSettingsStore.minimumGhostTextOpacity static let maximumGhostTextOpacity = SuggestionSettingsStore.maximumGhostTextOpacity static let ghostTextOpacityStep = SuggestionSettingsStore.ghostTextOpacityStep + static let minimumPostAcceptHoldMilliseconds = + SuggestionSettingsStore.minimumPostAcceptHoldMilliseconds + static let maximumPostAcceptHoldMilliseconds = + SuggestionSettingsStore.maximumPostAcceptHoldMilliseconds + static let postAcceptHoldStepMilliseconds = + SuggestionSettingsStore.postAcceptHoldStepMilliseconds static let minimumGhostTextSizeMultiplier = SuggestionSettingsStore.minimumGhostTextSizeMultiplier static let maximumGhostTextSizeMultiplier = SuggestionSettingsStore.maximumGhostTextSizeMultiplier static let ghostTextSizeMultiplierStep = SuggestionSettingsStore.ghostTextSizeMultiplierStep @@ -279,6 +289,7 @@ final class SuggestionSettingsModel: ObservableObject { preferredEmojiGender = data.preferredEmojiGender autoAcceptTrailingPunctuation = data.autoAcceptTrailingPunctuation addSpaceAfterAccept = data.addSpaceAfterAccept + postAcceptHoldMilliseconds = data.postAcceptHoldMilliseconds streamSuggestionsWhileGenerating = data.streamSuggestionsWhileGenerating predictAheadWhileTyping = data.predictAheadWhileTyping fadeInSuggestions = data.fadeInSuggestions @@ -365,6 +376,7 @@ final class SuggestionSettingsModel: ObservableObject { preferredEmojiGender = data.preferredEmojiGender autoAcceptTrailingPunctuation = data.autoAcceptTrailingPunctuation addSpaceAfterAccept = data.addSpaceAfterAccept + postAcceptHoldMilliseconds = data.postAcceptHoldMilliseconds streamSuggestionsWhileGenerating = data.streamSuggestionsWhileGenerating predictAheadWhileTyping = data.predictAheadWhileTyping fadeInSuggestions = data.fadeInSuggestions @@ -441,6 +453,7 @@ final class SuggestionSettingsModel: ObservableObject { showFollowingWords: showFollowingWords, autoAcceptTrailingPunctuation: autoAcceptTrailingPunctuation, addSpaceAfterAccept: addSpaceAfterAccept, + postAcceptHoldMilliseconds: postAcceptHoldMilliseconds, streamSuggestionsWhileGenerating: streamSuggestionsWhileGenerating, predictAheadWhileTyping: predictAheadWhileTyping, acceptanceGranularity: acceptanceGranularity @@ -1039,6 +1052,13 @@ final class SuggestionSettingsModel: ObservableObject { store.saveAddSpaceAfterAccept(enabled) } + func setPostAcceptHoldMilliseconds(_ milliseconds: Int) { + let clamped = SuggestionSettingsStore.clampedPostAcceptHoldMilliseconds(milliseconds) + guard postAcceptHoldMilliseconds != clamped else { return } + postAcceptHoldMilliseconds = clamped + store.savePostAcceptHoldMilliseconds(clamped) + } + func setPredictAheadWhileTyping(_ enabled: Bool) { guard predictAheadWhileTyping != enabled else { return } predictAheadWhileTyping = enabled diff --git a/Cotabby/Models/Suggestion/SuggestionSubsystemContracts.swift b/Cotabby/Models/Suggestion/SuggestionSubsystemContracts.swift index f6dd28718..2571a9248 100644 --- a/Cotabby/Models/Suggestion/SuggestionSubsystemContracts.swift +++ b/Cotabby/Models/Suggestion/SuggestionSubsystemContracts.swift @@ -199,6 +199,9 @@ extension LlamaRuntimeGenerating { protocol SuggestionSettingsProviding: AnyObject { var snapshot: SuggestionSettingsSnapshot { get } var snapshotPublisher: AnyPublisher { get } + /// Read live when a screen-context change arrives, deliberately outside `snapshot`: every + /// snapshot change resets the pipeline, and a slider drag would otherwise do that per tick. + var postAcceptHoldMilliseconds: Int { get } } @MainActor diff --git a/Cotabby/Services/Visual/VisualContextCoordinator.swift b/Cotabby/Services/Visual/VisualContextCoordinator.swift index a5d09c5ae..ea307e1b3 100644 --- a/Cotabby/Services/Visual/VisualContextCoordinator.swift +++ b/Cotabby/Services/Visual/VisualContextCoordinator.swift @@ -202,22 +202,29 @@ final class VisualContextCoordinator { guard !Task.isCancelled else { return } + // The field's text on both sides of the screenshot: an accept can land after the + // read above and before the pixels are taken, and its words must still read as the + // user's own edit rather than new screen text. + var fieldTexts = [snapshotContext.precedingText + snapshotContext.trailingText] if let provider = refreshContextProvider { let liveContext = provider() guard activeAugmentationSession?.sessionID == session.sessionID else { return } - guard screenRecordingPermissionProvider(), liveContext?.identity == snapshotContext.identity, - liveContext?.sessionIdentity == snapshotContext.sessionIdentity, - liveContext?.isSecure == false else { + guard screenRecordingPermissionProvider(), let liveContext, + liveContext.identity == snapshotContext.identity, + liveContext.sessionIdentity == snapshotContext.sessionIdentity, + !liveContext.isSecure else { cancel(resetState: true) return } + fieldTexts.append(liveContext.precedingText + liveContext.trailingText) } applyExcerpt( excerpt, for: session.sessionID, identity: snapshotContext.identity, - capturedAt: capturedAt + capturedAt: capturedAt, + fieldTexts: fieldTexts ) } catch is CancellationError { CotabbyLogger.app.debug("Visual context generation cancelled") @@ -336,7 +343,8 @@ final class VisualContextCoordinator { _ excerpt: VisualContextExcerpt, for sessionID: UUID, identity: FocusedInputIdentity, - capturedAt: TimeInterval + capturedAt: TimeInterval, + fieldTexts: [String] ) { guard activeAugmentationSession?.sessionID == sessionID, activeAugmentationSession?.elementIdentifier == identity.elementIdentifier, @@ -350,7 +358,18 @@ final class VisualContextCoordinator { return } - let changed = activeAugmentationSession?.excerpt?.text != excerpt.text + let previousExcerptText = activeAugmentationSession?.excerpt?.text + let changed = previousExcerptText != excerpt.text + // A changed excerpt is reported as possible navigation so the coordinator retires the + // visible tail. Only a change that shows text the old screen and the field's own text do not + // account for qualifies: accepted words landing on screen and OCR wobble are not navigation, + // and reporting them replaced the suggestion mid-accept whenever the user paused between + // Tabs. An edit cannot hide a navigation that brings lines of its own. The excerpt is stored + // either way, so the next request uses it. A first excerpt is always reported: it is new + // context, not a comparison. + let isNavigation = previousExcerptText.map { + OCRTextHygiene.showsUnexplainedText(excerpt.text, previous: $0, fieldTexts: fieldTexts) + } ?? true activeAugmentationSession?.status = .ready activeAugmentationSession?.excerpt = excerpt status = .ready @@ -359,7 +378,11 @@ final class VisualContextCoordinator { scheduleExpiry(sessionID: sessionID, capturedAt: capturedAt) CotabbyLogger.app.debug("Visual context ready: \(excerpt.text.count) chars") publishState() - if changed { onInjectedContextReady?(identity) } + if isNavigation { + onInjectedContextReady?(identity) + } else if changed { + CotabbyLogger.app.debug("Visual context changed only by the field's own text or OCR noise; not navigation") + } } /// Timer-driven expiry updates the UI and request-driven expiry covers a delayed timer. Both diff --git a/Cotabby/Support/Context/OCRTextHygiene.swift b/Cotabby/Support/Context/OCRTextHygiene.swift index ee899ac39..c6084d525 100644 --- a/Cotabby/Support/Context/OCRTextHygiene.swift +++ b/Cotabby/Support/Context/OCRTextHygiene.swift @@ -252,6 +252,44 @@ nonisolated enum OCRTextHygiene { return String(joined.prefix(max(0, maxChars))) } + // MARK: - Screen change classification + + /// Whether `excerpt` shows a line that neither `previous` nor the focused field's own text + /// accounts for: evidence that something other than the user's typing changed the screen. + /// + /// A changed excerpt is the navigation signal on hosts that keep one field identity across + /// conversations, but most changes between refreshes are not navigation. Accepting words puts + /// them on screen, and OCR disagrees with itself (`Settings ...` vs `Settings...`, the caret + /// read as a trailing `I` or `l`, a counter ticking). So lines compare as lowercase alphanumeric + /// words, a line with fewer than four letters is noise, and a line that is the field's text, + /// with or without one trailing caret glyph (joined to the last word or read as a word of its + /// own), is the user's own edit. Only new lines count. A line that disappears is not evidence: + /// a growing field scrolls the page, and OCR drops lines it read last time. + /// ponytail: a switch to a conversation that shows nothing new (empty, sharing all its chrome) + /// goes unreported; count lost substantive lines too if that case turns up in practice. + /// + /// Pass the field's text from both sides of the screenshot (read before capture and after OCR), + /// so words accepted while the capture was in flight still read as the user's own. + static func showsUnexplainedText(_ excerpt: String, previous: String, fieldTexts: [String]) -> Bool { + let previousLines = Set(previous.split(separator: "\n").map { comparable(String($0)) }) + let fields = fieldTexts.map(comparable) + return excerpt.split(separator: "\n").contains { rawLine in + let line = comparable(String(rawLine)) + guard line.filter(\.isLetter).count >= 4, !previousLines.contains(line) else { return false } + // Trim after dropping the glyph: a caret read as its own word leaves `attached ` behind, + // which the field's `attached` at its end never contains. + let withoutCaret = line.last.map { "il1".contains($0) } == true + ? String(line.dropLast()).trimmingCharacters(in: .whitespaces) : line + return !fields.contains { $0.contains(line) || $0.contains(withoutCaret) } + } + } + + /// Lowercase alphanumeric words joined by single spaces. Punctuation and spacing are where OCR + /// of the same pixels most often differs between captures. + private static func comparable(_ text: String) -> String { + text.lowercased().split { !$0.isLetter && !$0.isNumber }.joined(separator: " ") + } + // MARK: - Tokenization /// Splits a line into whitespace-delimited tokens. diff --git a/Cotabby/Support/Settings/SuggestionSettingsStore.swift b/Cotabby/Support/Settings/SuggestionSettingsStore.swift index 3bc74f9f2..55f03c05e 100644 --- a/Cotabby/Support/Settings/SuggestionSettingsStore.swift +++ b/Cotabby/Support/Settings/SuggestionSettingsStore.swift @@ -38,6 +38,13 @@ struct SuggestionSettingsStore { static let defaultGhostTextOpacity: Double = 1.0 static let ghostTextOpacityStep: Double = 0.1 + /// Bounds for the post-accept hold. 500ms covers the gap between Tab presses in a normal accept + /// burst (measured 150-370ms in a Gmail repro) without keeping a stale tail much past it. + static let minimumPostAcceptHoldMilliseconds = 0 + static let maximumPostAcceptHoldMilliseconds = 2000 + static let defaultPostAcceptHoldMilliseconds = 500 + static let postAcceptHoldStepMilliseconds = 50 + /// Multiplier the overlay applies on top of the caret-approximated ghost-text size. 1.0 is the /// out-of-box default (the unchanged best-approximation). The band is symmetric around 1.0 with /// real shrink room because "suggestions look too big" is the common complaint, and is kept @@ -174,6 +181,8 @@ struct SuggestionSettingsStore { private static let preferredEmojiGenderDefaultsKey = "cotabbyPreferredEmojiGender" private static let autoAcceptTrailingPunctuationDefaultsKey = "cotabbyAutoAcceptTrailingPunctuation" private static let addSpaceAfterAcceptDefaultsKey = "cotabbyAddSpaceAfterAccept" + private static let postAcceptHoldMillisecondsDefaultsKey = + "cotabbyPostAcceptRegenerationHoldMilliseconds" private static let predictAheadWhileTypingDefaultsKey = "cotabbyPredictAheadWhileTyping" private static let streamWhileGeneratingDefaultsKey = "cotabbyStreamSuggestionsWhileGenerating" private static let fadeInSuggestionsDefaultsKey = "cotabbyFadeInSuggestions" @@ -258,6 +267,7 @@ struct SuggestionSettingsStore { preferredEmojiGenderDefaultsKey, autoAcceptTrailingPunctuationDefaultsKey, addSpaceAfterAcceptDefaultsKey, + postAcceptHoldMillisecondsDefaultsKey, streamWhileGeneratingDefaultsKey, predictAheadWhileTypingDefaultsKey, fadeInSuggestionsDefaultsKey, @@ -486,6 +496,10 @@ struct SuggestionSettingsStore { // trailing space is opt-in from Settings. let resolvedAddSpaceAfterAccept = userDefaults.object(forKey: Self.addSpaceAfterAcceptDefaultsKey) as? Bool ?? false + let resolvedPostAcceptHoldMilliseconds = Self.clampedPostAcceptHoldMilliseconds( + userDefaults.object(forKey: Self.postAcceptHoldMillisecondsDefaultsKey) as? Int + ?? Self.defaultPostAcceptHoldMilliseconds + ) // Missing keys opt existing installations into prediction reuse; an explicit false survives reload. let resolvedPredictAheadWhileTyping = userDefaults.object(forKey: Self.predictAheadWhileTypingDefaultsKey) as? Bool ?? true @@ -611,6 +625,7 @@ struct SuggestionSettingsStore { showFollowingWords: resolvedShowFollowingWords, autoAcceptTrailingPunctuation: resolvedAutoAcceptTrailingPunctuation, addSpaceAfterAccept: resolvedAddSpaceAfterAccept, + postAcceptHoldMilliseconds: resolvedPostAcceptHoldMilliseconds, streamSuggestionsWhileGenerating: resolvedStreamSuggestionsWhileGenerating, predictAheadWhileTyping: resolvedPredictAheadWhileTyping, acceptanceGranularity: resolvedAcceptanceGranularity @@ -720,6 +735,7 @@ struct SuggestionSettingsStore { savePreferredEmojiGender(data.preferredEmojiGender) saveAutoAcceptTrailingPunctuation(data.autoAcceptTrailingPunctuation) saveAddSpaceAfterAccept(data.addSpaceAfterAccept) + savePostAcceptHoldMilliseconds(data.postAcceptHoldMilliseconds) saveStreamSuggestionsWhileGenerating(data.streamSuggestionsWhileGenerating) savePredictAheadWhileTyping(data.predictAheadWhileTyping) saveFadeInSuggestions(data.fadeInSuggestions) @@ -1041,6 +1057,10 @@ struct SuggestionSettingsStore { userDefaults.set(enabled, forKey: Self.addSpaceAfterAcceptDefaultsKey) } + func savePostAcceptHoldMilliseconds(_ value: Int) { + userDefaults.set(value, forKey: Self.postAcceptHoldMillisecondsDefaultsKey) + } + func savePredictAheadWhileTyping(_ enabled: Bool) { userDefaults.set(enabled, forKey: Self.predictAheadWhileTypingDefaultsKey) } @@ -1201,6 +1221,10 @@ struct SuggestionSettingsStore { return trimmed.isEmpty ? fallbackBundleIdentifier : trimmed } + static func clampedPostAcceptHoldMilliseconds(_ value: Int) -> Int { + min(maximumPostAcceptHoldMilliseconds, max(minimumPostAcceptHoldMilliseconds, value)) + } + static func clampedGhostTextOpacity(_ value: Double) -> Double { guard value.isFinite else { return defaultGhostTextOpacity diff --git a/Cotabby/Support/Suggestion/Request/SuggestionAvailabilityEvaluator.swift b/Cotabby/Support/Suggestion/Request/SuggestionAvailabilityEvaluator.swift index 6e36b27c2..416cd3754 100644 --- a/Cotabby/Support/Suggestion/Request/SuggestionAvailabilityEvaluator.swift +++ b/Cotabby/Support/Suggestion/Request/SuggestionAvailabilityEvaluator.swift @@ -143,6 +143,28 @@ enum SuggestionAvailabilityEvaluator { ) } + /// How many milliseconds a changed screen-context excerpt must wait before it may retire the + /// suggestion, because the user accepted within the last `holdMilliseconds`. 0 means act now. + /// + /// A changed excerpt normally retires the visible tail and regenerates: on a host that exposes + /// identical field identity for two conversations, it is the only navigation signal. But + /// accepting words changes the screen too, so the ~3s OCR refresh read a Tab burst as navigation + /// and replaced the suggestion between presses (and the next Tab, finding no ghost or armed + /// post-exhaustion window, reached the host). The caller defers the retirement by the returned + /// amount instead of dropping it, so a real navigation is still acted on once accepting stops. + /// + /// Both instants are `ProcessInfo.systemUptime` seconds: a wall-clock step backwards would + /// otherwise make the elapsed time negative and hold the navigation signal for that long. + static func postAcceptanceHoldRemainingMilliseconds( + lastAcceptanceUptime: TimeInterval?, + nowUptime: TimeInterval, + holdMilliseconds: Int + ) -> Int { + guard let lastAcceptanceUptime, holdMilliseconds > 0 else { return 0 } + let elapsedMilliseconds = Int(((nowUptime - lastAcceptanceUptime) * 1_000).rounded(.down)) + return max(0, holdMilliseconds - max(0, elapsedMilliseconds)) + } + /// Returns the first app, domain, or terminal rule that disables the focused field. private static func locationDisabledReason( disabledAppBundleIdentifiers: Set, diff --git a/Cotabby/UI/Settings/Panes/WritingPaneView.swift b/Cotabby/UI/Settings/Panes/WritingPaneView.swift index 348d71c7e..9ee3ec45b 100644 --- a/Cotabby/UI/Settings/Panes/WritingPaneView.swift +++ b/Cotabby/UI/Settings/Panes/WritingPaneView.swift @@ -46,8 +46,9 @@ struct WritingPaneView: View { } } - // What an accept inserts beyond the bare word. These shape the written output, so they sit - // with Writing rather than General; the accept keys themselves live in the Shortcuts pane. + // What an accept inserts beyond the bare word, and how long the accepted suggestion stays + // put. These shape the written output, so they sit with Writing rather than General; the + // accept keys themselves live in the Shortcuts pane. Section("Acceptance") { Toggle(isOn: autoAcceptTrailingPunctuationBinding) { SettingsRowLabel( @@ -68,6 +69,32 @@ struct WritingPaneView: View { ) } .settingsItem(.addSpaceAfterAccept) + + LabeledContent { + HStack(spacing: 10) { + TickMarkSlider( + value: postAcceptHoldBinding, + range: Double(SuggestionSettingsModel.minimumPostAcceptHoldMilliseconds) + ... Double(SuggestionSettingsModel.maximumPostAcceptHoldMilliseconds), + step: Double(SuggestionSettingsModel.postAcceptHoldStepMilliseconds) + ) + .frame(width: 180) + + Text(postAcceptHoldLabel) + .font(.callout) + .monospacedDigit() + .foregroundStyle(.secondary) + .frame(width: 64, alignment: .trailing) + } + } label: { + SettingsRowLabel( + title: "Keep Suggestion After Accepting", + description: "After you accept a word, wait this long before a change on screen can " + + "replace the suggestion, so accepting word by word isn't interrupted.", + systemImage: "timer" + ) + } + .settingsItem(.keepSuggestionAfterAccepting) } // Typo handling is a dependency chain, so the UI discloses it progressively rather than @@ -182,6 +209,19 @@ struct WritingPaneView: View { ) } + /// The slider works in `Double`; the setting stores whole milliseconds, so round on the way in. + private var postAcceptHoldBinding: Binding { + Binding( + get: { Double(suggestionSettings.postAcceptHoldMilliseconds) }, + set: { suggestionSettings.setPostAcceptHoldMilliseconds(Int($0.rounded())) } + ) + } + + private var postAcceptHoldLabel: String { + let milliseconds = suggestionSettings.postAcceptHoldMilliseconds + return milliseconds == 0 ? "Off" : "\(milliseconds) ms" + } + private var suppressCompletionsOnTypoBinding: Binding { Binding( get: { suggestionSettings.suppressCompletionsOnTypo }, diff --git a/Cotabby/UI/Settings/SettingsIndex.swift b/Cotabby/UI/Settings/SettingsIndex.swift index b4e4fdfae..358b3a3b0 100644 --- a/Cotabby/UI/Settings/SettingsIndex.swift +++ b/Cotabby/UI/Settings/SettingsIndex.swift @@ -50,6 +50,7 @@ enum SettingsItem: String, CaseIterable, Identifiable { case length case acceptPunctuation case addSpaceAfterAccept + case keepSuggestionAfterAccepting case name case languages case customRules @@ -122,6 +123,7 @@ enum SettingsItem: String, CaseIterable, Identifiable { case .showFollowingWords: return "Show following words" case .acceptPunctuation: return "Accept Punctuation With Word" case .addSpaceAfterAccept: return "Add Space After Accepting" + case .keepSuggestionAfterAccepting: return "Keep Suggestion After Accepting" case .inlineMacros: return "Inline Macros" case .onboarding: return "Onboarding" case .resetAllSettings: return "Reset All Settings" @@ -210,6 +212,7 @@ enum SettingsItem: String, CaseIterable, Identifiable { case .showFollowingWords: return "text.word.spacing" case .acceptPunctuation: return "textformat.abc" case .addSpaceAfterAccept: return "space" + case .keepSuggestionAfterAccepting: return "timer" case .inlineMacros: return "slash.circle" case .onboarding: return "graduationcap" case .resetAllSettings: return "arrow.counterclockwise" @@ -300,8 +303,8 @@ enum SettingsItem: String, CaseIterable, Identifiable { return .appearance case .emojiPicker, .emojiSkinTone, .emojiPeopleStyle, .emojiHistory: return .emoji - case .length, .acceptPunctuation, .addSpaceAfterAccept, .name, .languages, .customRules, - .hideSuggestionsOnTypo, .offerTypoCorrections, .spellingDictionaries, .automaticallyFixTypos: + case .length, .acceptPunctuation, .addSpaceAfterAccept, .keepSuggestionAfterAccepting, .name, + .languages, .customRules, .hideSuggestionsOnTypo, .offerTypoCorrections, .spellingDictionaries, .automaticallyFixTypos: return .writing case .extendedContext, .contextLivePreview, .typingHistory: return .context @@ -340,6 +343,7 @@ enum SettingsItem: String, CaseIterable, Identifiable { case .showFollowingWords: return "Preview the following phrase, or reveal one word at a time as you type or accept." case .acceptPunctuation: return "Also accept trailing commas and periods with a word." case .addSpaceAfterAccept: return "Add a space when an accept finishes a word." + case .keepSuggestionAfterAccepting: return "Wait after an accept before a screen change can replace the suggestion." case .inlineMacros: return "Type / for dates, math, units, currency, and randoms." case .onboarding: return "Replay the first-run setup walkthrough." case .resetAllSettings: return "Restore every Cotabby setting to its original default." @@ -458,6 +462,9 @@ enum SettingsItem: String, CaseIterable, Identifiable { case .addSpaceAfterAccept: return ["space", "spacebar", "trailing space", "auto space", "add space", "accept", "after accept", "whitespace", "gap", "separator"] + case .keepSuggestionAfterAccepting: + return ["hold", "keep", "delay", "pause", "regenerate", "regeneration", "tab", "accept", + "after accept", "lost", "replaced", "milliseconds", "screen context"] case .inlineMacros: return ["macro", "macros", "math", "convert", "currency", "date", "random", "expansion", "slash", "snippet", "shortcut", "formula", "calculator"] diff --git a/CotabbyTests/App/Coordinators/Suggestion/SuggestionCoordinatorAcceptanceTests.swift b/CotabbyTests/App/Coordinators/Suggestion/SuggestionCoordinatorAcceptanceTests.swift index 189fbc0e5..d39986b39 100644 --- a/CotabbyTests/App/Coordinators/Suggestion/SuggestionCoordinatorAcceptanceTests.swift +++ b/CotabbyTests/App/Coordinators/Suggestion/SuggestionCoordinatorAcceptanceTests.swift @@ -189,6 +189,153 @@ final class SuggestionCoordinatorAcceptanceTests: SuggestionCoordinatorRigTestCa XCTAssertNil(rig.interactionState.activeSession) } + // MARK: - Post-accept hold against screen-context refresh + + func test_screenContextChangeRightAfterAnAcceptKeepsTheTailBeingAccepted() { + // Regression from a Gmail repro: the ~3s OCR refresh landed mid Tab burst, read the user's + // own accepted words as navigation, and replaced the suggestion; the next Tab, finding no + // ghost, went to Gmail and moved focus out of the compose body. + let rig = retained(makeCoordinatorRig()) + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + XCTAssertTrue(rig.coordinator.overlayState.isVisible) + XCTAssertEqual(rig.interactionState.activeSession?.remainingText, " world again") + XCTAssertNotNil(rig.coordinator.deferredScreenContextRetirement, "The change is deferred, not dropped") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion(), "The next Tab must continue the same suggestion") + XCTAssertEqual(rig.inserter.insertedChunks, [" hello", " world"]) + } + + func test_deferredScreenContextChangeRetiresTheTailOnceAcceptingStops() async { + // The excerpt is stored when it arrives and an identical later refresh does not fire again, + // so a held change must still be acted on: otherwise a real navigation is lost for good. + let rig = retained(makeCoordinatorRig()) + rig.settingsProvider.postAcceptHoldMilliseconds = 50 + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + XCTAssertNotNil(rig.interactionState.activeSession) + + await waitUntil { rig.interactionState.activeSession == nil } + XCTAssertEqual(rig.overlayController.hideReasons.last, "Overlay hidden because screen context changed.") + } + + func test_caretMoveDuringTheHoldSettlesTheDeferredChangeWithoutPredicting() async { + // The arrow key retires the tail and leaves Cotabby idle. The deferred change must not wake + // after it and start a prediction the user never typed for. + let rig = retained(makeCoordinatorRig()) + rig.settingsProvider.postAcceptHoldMilliseconds = 50 + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + _ = rig.coordinator.handleInputEvent(CotabbyTestFixtures.inputEvent(kind: .navigation)) + XCTAssertNil(rig.coordinator.deferredScreenContextRetirement) + + try? await Task.sleep(nanoseconds: 200_000_000) + XCTAssertEqual(rig.coordinator.state, .idle) + XCTAssertNil(rig.interactionState.activeSession) + XCTAssertTrue(rig.engine.requests.isEmpty) + } + + func test_inputThatEndsTheTailDuringTheHoldSettlesTheDeferredChange() { + // A mismatched keystroke retires the tail and schedules its own prediction; the emoji picker + // takes over input. Either way a deferral waking later would cancel newer work or predict + // under the picker, so it must be settled on the spot. + let cases: [(name: String, prepare: (CoordinatorRig) -> Void, event: CapturedInputEvent)] = [ + ("mismatched typing", { _ in }, CotabbyTestFixtures.inputEvent(kind: .textMutation, characters: "x")), + ("emoji picker", { $0.coordinator.emojiInputObserver = { _ in true } }, + CotabbyTestFixtures.inputEvent(kind: .textMutation, characters: ":")) + ] + for testCase in cases { + let rig = retained(makeCoordinatorRig()) + testCase.prepare(rig) + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion(), testCase.name) + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + XCTAssertNotNil(rig.coordinator.deferredScreenContextRetirement, testCase.name) + + _ = rig.coordinator.handleInputEvent(testCase.event) + + XCTAssertNil(rig.interactionState.activeSession, testCase.name) + XCTAssertNil(rig.coordinator.deferredScreenContextRetirement, testCase.name) + } + } + + func test_matchingTypingDuringTheHoldStillRetiresTheTailOnceItLapses() async { + // A keystroke that matches the tail keeps it alive, so it must not settle the deferred + // change: on a navigation with identical field identity, that change is the only thing that + // retires the old conversation's tail. + let rig = retained(makeCoordinatorRig()) + rig.settingsProvider.postAcceptHoldMilliseconds = 50 + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + _ = rig.coordinator.handleInputEvent(CotabbyTestFixtures.inputEvent(kind: .textMutation, characters: " ")) + XCTAssertEqual(rig.interactionState.activeSession?.remainingText, "world again", "The keystroke matched") + XCTAssertNotNil(rig.coordinator.deferredScreenContextRetirement) + + await waitUntil { rig.overlayController.hideReasons.last == "Overlay hidden because screen context changed." } + } + + func test_settingsChangeDuringTheHoldSettlesTheDeferredChange() { + // The settings reset restarts visual context and schedules its own prediction; a deferred + // change left pending would wake and retire that replacement. + let rig = retained(makeCoordinatorRig()) + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + XCTAssertNotNil(rig.coordinator.deferredScreenContextRetirement) + + rig.coordinator.handleSuggestionSettingsChange(CotabbyTestFixtures.settingsSnapshot(debounceMilliseconds: 2)) + + XCTAssertNil(rig.coordinator.deferredScreenContextRetirement) + } + + func test_screenContextChangeAfterTheFinalChunkKeepsOwningTab() { + // After the last word there is no session, only the post-exhaustion window that keeps a fast + // follow-up Tab from reaching the host. An undeferred retirement hides the (already hidden) + // overlay, which ends that window and hands Tab back mid burst. + let rig = retained(makeCoordinatorRig(snapshot: finalChunkSnapshot)) + startVisibleSession(in: rig, fullText: " today") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + XCTAssertTrue(rig.coordinator.postExhaustionAcceptanceState.isArmed) + + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + XCTAssertTrue(rig.coordinator.postExhaustionAcceptanceState.isArmed) + XCTAssertTrue(rig.inputMonitor.shouldConsumeAcceptKeyProvider(), "A follow-up Tab must still be owned") + } + + func test_screenContextChangeAfterTheHoldLapsesRetiresTheTailImmediately() { + let rig = retained(makeCoordinatorRig()) + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + rig.coordinator.lastAcceptanceAt = ProcessInfo.processInfo.systemUptime - 1 + + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + XCTAssertFalse(rig.coordinator.overlayState.isVisible) + XCTAssertNil(rig.interactionState.activeSession) + XCTAssertNil(rig.coordinator.deferredScreenContextRetirement) + } + + func test_zeroHoldRestoresImmediateRetirementAfterAnAccept() { + let rig = retained(makeCoordinatorRig()) + rig.settingsProvider.postAcceptHoldMilliseconds = 0 + startVisibleSession(in: rig, fullText: " hello world again") + XCTAssertTrue(rig.coordinator.acceptCurrentSuggestion()) + + rig.visualContext.onInjectedContextReady?(rig.focusProvider.snapshot.context!.identity) + + XCTAssertFalse(rig.coordinator.overlayState.isVisible) + XCTAssertNil(rig.interactionState.activeSession) + } + private func visibleText(of state: OverlayState) -> String? { guard case let .visible(text, _, _) = state else { return nil } return text diff --git a/CotabbyTests/Models/Settings/SuggestionSettingsModelTests.swift b/CotabbyTests/Models/Settings/SuggestionSettingsModelTests.swift index b2631e4c2..e5cc37ab8 100644 --- a/CotabbyTests/Models/Settings/SuggestionSettingsModelTests.swift +++ b/CotabbyTests/Models/Settings/SuggestionSettingsModelTests.swift @@ -229,6 +229,7 @@ final class SuggestionSettingsModelTests: XCTestCase { model.setPreferredEmojiGender(.female) model.setAutoAcceptTrailingPunctuation(false) model.setAddSpaceAfterAccept(true) + model.setPostAcceptHoldMilliseconds(1500) model.setStreamSuggestionsWhileGenerating(true) model.setPredictAheadWhileTyping(false) model.setAcceptanceGranularity(.phrase) @@ -286,6 +287,7 @@ final class SuggestionSettingsModelTests: XCTestCase { XCTAssertEqual(model.preferredEmojiGender, pristine.preferredEmojiGender) XCTAssertEqual(model.autoAcceptTrailingPunctuation, pristine.autoAcceptTrailingPunctuation) XCTAssertEqual(model.addSpaceAfterAccept, pristine.addSpaceAfterAccept) + XCTAssertEqual(model.postAcceptHoldMilliseconds, pristine.postAcceptHoldMilliseconds) XCTAssertEqual(model.streamSuggestionsWhileGenerating, pristine.streamSuggestionsWhileGenerating) XCTAssertEqual(model.predictAheadWhileTyping, pristine.predictAheadWhileTyping) XCTAssertEqual(model.acceptanceGranularity, pristine.acceptanceGranularity) @@ -788,6 +790,34 @@ final class SuggestionSettingsModelTests: XCTestCase { XCTAssertEqual(model.ghostTextSizeMultiplier, SuggestionSettingsModel.minimumGhostTextSizeMultiplier) } + func test_postAcceptHold_defaultsTo500AndClampsToDocumentedBounds() { + let model = makeModel() + XCTAssertEqual(model.postAcceptHoldMilliseconds, 500) + + model.setPostAcceptHoldMilliseconds(10_000) + XCTAssertEqual( + model.postAcceptHoldMilliseconds, + SuggestionSettingsModel.maximumPostAcceptHoldMilliseconds + ) + model.setPostAcceptHoldMilliseconds(-50) + XCTAssertEqual(model.postAcceptHoldMilliseconds, 0, "0 turns the hold off") + } + + func test_postAcceptHold_changesDoNotPublishASnapshot() { + // Every snapshot change resets the suggestion pipeline (KV cache, OCR session); a slider + // drag emits one value per tick, so the hold is read live instead of riding the snapshot. + let model = makeModel() + var emissions = 0 + let subscription = model.snapshotPublisher.dropFirst().sink { _ in emissions += 1 } + defer { subscription.cancel() } + + model.setPostAcceptHoldMilliseconds(1500) + model.setPostAcceptHoldMilliseconds(250) + + XCTAssertEqual(emissions, 0) + XCTAssertEqual(model.postAcceptHoldMilliseconds, 250) + } + func test_ghostTextAppearanceDefaultsAreFullyOpaqueAndUnscaled() { let model = makeModel() XCTAssertEqual(model.ghostTextOpacity, 1.0) diff --git a/CotabbyTests/Services/Visual/VisualContextCoordinatorTests.swift b/CotabbyTests/Services/Visual/VisualContextCoordinatorTests.swift index a5846f0e9..b50b485ce 100644 --- a/CotabbyTests/Services/Visual/VisualContextCoordinatorTests.swift +++ b/CotabbyTests/Services/Visual/VisualContextCoordinatorTests.swift @@ -23,6 +23,55 @@ final class VisualContextCoordinatorTests: XCTestCase { XCTAssertEqual(coordinator.latestExcerpt, generator.text) } + func test_screenChangeExplainedByTheFieldsOwnEditIsNotReportedAsNavigation() async throws { + // Accepting words puts them on screen. A refresh that sees both new field text and new screen + // text must not look like navigation, or it retires the suggestion the user is accepting. + let generator = StubVisualContextGenerator() + let coordinator = makeCoordinator(generator) + var live = CotabbyTestFixtures.focusedInputSnapshot(precedingText: "Please review") + coordinator.refreshContextProvider = { live } + var notifications = 0 + coordinator.onInjectedContextReady = { _ in notifications += 1 } + coordinator.startSessionIfNeeded(for: live, configuration: .local) + defer { coordinator.cancel(resetState: true) } + try await waitUntil { notifications == 1 } + + live = CotabbyTestFixtures.focusedInputSnapshot(precedingText: "Please review the attached") + generator.text = "Please review the attached" + try await waitUntil { coordinator.latestExcerpt == "Please review the attached" } + XCTAssertEqual(notifications, 1, "The user's own accepted words changed the screen") + + // An edit does not explain the rest of the screen: the same draft carried into another + // conversation shows that conversation's lines, and that is still navigation. + live = CotabbyTestFixtures.focusedInputSnapshot(precedingText: "Please review the attached file") + generator.text = "Please review the attached file\nA new message arrived" + try await waitUntil { notifications == 2 } + } + + func test_acceptLandingMidCaptureIsStillTheFieldsOwnEdit() async throws { + // The refresh reads the field, then an accept lands before the screenshot. The pixels show + // words the pre-capture text lacks; the text read after OCR must still claim them. + let generator = StubVisualContextGenerator() + let coordinator = makeCoordinator(generator) + var live = CotabbyTestFixtures.focusedInputSnapshot(precedingText: "Please review") + coordinator.refreshContextProvider = { live } + var notifications = 0 + coordinator.onInjectedContextReady = { _ in notifications += 1 } + coordinator.startSessionIfNeeded(for: live, configuration: .local) + defer { coordinator.cancel(resetState: true); generator.pending?.resume(); generator.pending = nil } + try await waitUntil { notifications == 1 } + + generator.suspendNext = true + try await waitUntil { generator.pending != nil } + XCTAssertEqual(generator.contexts.last?.precedingText, "Please review") + live = CotabbyTestFixtures.focusedInputSnapshot(precedingText: "Please review the attached") + generator.text = "Project agenda and deadline\nPlease review the attached" + generator.pending?.resume() + generator.pending = nil + try await waitUntil { coordinator.latestExcerpt == generator.text } + XCTAssertEqual(notifications, 1, "Words accepted during the capture are the user's own edit") + } + func test_permissionRevocationStopsRefreshAndClearsExcerpt() async throws { let generator = StubVisualContextGenerator() var allowed = true diff --git a/CotabbyTests/Support/Context/OCRTextHygieneTests.swift b/CotabbyTests/Support/Context/OCRTextHygieneTests.swift index dcaa7d9ff..57f4cc1b5 100644 --- a/CotabbyTests/Support/Context/OCRTextHygieneTests.swift +++ b/CotabbyTests/Support/Context/OCRTextHygieneTests.swift @@ -218,4 +218,35 @@ final class OCRTextHygieneTests: XCTestCase { let result = OCRTextHygiene.clean(lines: input, fieldText: "") XCTAssertEqual(result.components(separatedBy: "\n"), texts(input)) } + + // MARK: - Screen change classification + + private let chat = "Team Settings ...\n16\nWrite a reply, mention someone with @" + + func test_showsUnexplainedText_ignoresAcceptedWordsCaretGlyphsAndOCRWobble() { + // Each change here is the user's accept or OCR re-reading the same pixels differently. + let next = "Team Settings...\n20\nWrite a reply mention someone with @\nThe launch moved to Thursdal" + XCTAssertFalse(OCRTextHygiene.showsUnexplainedText(next, previous: chat, fieldTexts: ["The launch moved to Thursday"])) + // The caret read as a word of its own after the field's last word. + XCTAssertFalse(OCRTextHygiene.showsUnexplainedText( + chat + "\nThe launch moved to Thursday I", previous: chat, fieldTexts: ["The launch moved to Thursday"] + )) + } + + func test_showsUnexplainedText_reportsANewConversationEvenWhenTheFieldChanged() { + let next = chat + "\nThe launch moved to Thursday\nSee you at the station tomorrow" + XCTAssertTrue(OCRTextHygiene.showsUnexplainedText(next, previous: chat, fieldTexts: ["The launch", "The launch moved to Thursday"])) + } + + func test_showsUnexplainedText_needsTheTextReadAfterCaptureForAMidCaptureAccept() { + let next = chat + "\nplease review the attached" + XCTAssertTrue(OCRTextHygiene.showsUnexplainedText(next, previous: chat, fieldTexts: ["Please review"])) + XCTAssertFalse(OCRTextHygiene.showsUnexplainedText( + next, previous: chat, fieldTexts: ["Please review", "Please review the attached"] + )) + } + + func test_showsUnexplainedText_ignoresLinesThatOnlyDisappear() { + XCTAssertFalse(OCRTextHygiene.showsUnexplainedText("16", previous: chat, fieldTexts: [])) + } } diff --git a/CotabbyTests/Support/Suggestion/Request/SuggestionAvailabilityEvaluatorTests.swift b/CotabbyTests/Support/Suggestion/Request/SuggestionAvailabilityEvaluatorTests.swift index fcdcaaec8..a38311523 100644 --- a/CotabbyTests/Support/Suggestion/Request/SuggestionAvailabilityEvaluatorTests.swift +++ b/CotabbyTests/Support/Suggestion/Request/SuggestionAvailabilityEvaluatorTests.swift @@ -384,4 +384,29 @@ final class SuggestionAvailabilityEvaluatorTests: XCTestCase { ) } } + + func test_postAcceptanceHoldReportsTheTimeLeftAfterARecentAccept() { + let now: TimeInterval = 1_000 + let cases: [(name: String, lastAcceptanceUptime: TimeInterval?, holdMilliseconds: Int, expected: Int)] = [ + ("never accepted", nil, 500, 0), + ("accepted 370ms ago", now - 0.37, 500, 130), + ("accepted exactly at the limit", now - 0.5, 500, 0), + ("accepted 1s ago", now - 1, 500, 0), + ("hold off", now - 0.01, 0, 0), + // Uptime is monotonic, but a caller passing a future instant must not extend the hold. + ("acceptance in the future", now + 5, 500, 500) + ] + + for testCase in cases { + XCTAssertEqual( + SuggestionAvailabilityEvaluator.postAcceptanceHoldRemainingMilliseconds( + lastAcceptanceUptime: testCase.lastAcceptanceUptime, + nowUptime: now, + holdMilliseconds: testCase.holdMilliseconds + ), + testCase.expected, + testCase.name + ) + } + } } diff --git a/CotabbyTests/TestSupport/SuggestionCoordinatorTestSupport.swift b/CotabbyTests/TestSupport/SuggestionCoordinatorTestSupport.swift index ed385a0c4..aea2aa428 100644 --- a/CotabbyTests/TestSupport/SuggestionCoordinatorTestSupport.swift +++ b/CotabbyTests/TestSupport/SuggestionCoordinatorTestSupport.swift @@ -203,6 +203,7 @@ final class RigSuggestionEngine: SuggestionGenerating { @MainActor final class RigSettingsProvider: SuggestionSettingsProviding { var snapshot: SuggestionSettingsSnapshot + var postAcceptHoldMilliseconds = SuggestionSettingsStore.defaultPostAcceptHoldMilliseconds let snapshotSubject = PassthroughSubject()