Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
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
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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) {
Expand Down
25 changes: 11 additions & 14 deletions Cotabby/App/Coordinators/Suggestion/SuggestionCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Never>?

/// Bounded string-only memory of recent suggestions for instant re-show on rollback and
/// re-entry (see `SuggestionAnchorCache`). `cotabbyAnchorReuseDisabled` is the kill switch.
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions Cotabby/Models/Settings/SuggestionSettingsData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }
Expand Down
20 changes: 20 additions & 0 deletions Cotabby/Models/Settings/SuggestionSettingsModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -441,6 +453,7 @@ final class SuggestionSettingsModel: ObservableObject {
showFollowingWords: showFollowingWords,
autoAcceptTrailingPunctuation: autoAcceptTrailingPunctuation,
addSpaceAfterAccept: addSpaceAfterAccept,
postAcceptHoldMilliseconds: postAcceptHoldMilliseconds,
streamSuggestionsWhileGenerating: streamSuggestionsWhileGenerating,
predictAheadWhileTyping: predictAheadWhileTyping,
acceptanceGranularity: acceptanceGranularity
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions Cotabby/Models/Suggestion/SuggestionSubsystemContracts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ extension LlamaRuntimeGenerating {
protocol SuggestionSettingsProviding: AnyObject {
var snapshot: SuggestionSettingsSnapshot { get }
var snapshotPublisher: AnyPublisher<SuggestionSettingsSnapshot, Never> { 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
Expand Down
Loading