Files
OSGKeyboard/OSGKeyboardShared/Services/KeyboardInstallationProbe.swift
rockyandClaude Opus 5 0023741455 feat(app): add crash visibility, memory relief, and clipboard reply work
Squashes the working tree that had accumulated on cloud-agent. Two
strands of work, kept in one commit at the maintainer's request.

Crash and stability (this session):

- Subscribe to MetricKit so crash, hang, CPU, and disk-write diagnostics
  are recorded on device. Keyboard-extension memory kills are the most
  likely way a user loses the keyboard mid-sentence and iOS does not
  classify them as crashes, so they never reach App Store Connect.
- Upload those reports to POST /v1/analytics/diagnostics from internal
  builds only (local Debug and TestFlight), reusing the analytics
  installation identifier and skipping entirely when analytics are off.
  App Store builds keep everything on device.
- Shed the Chinese engine, English lexicon, and in-flight pipeline work
  at the 40 MiB and 48 MiB budget thresholds instead of waiting for a
  system memory warning that jetsam often preempts.
- Install audio taps with the input node's live format through one
  shared adaptive resampler, so an audio-route change mid-recording no
  longer raises an uncatchable format-mismatch exception. Covers the
  dictation preview and the Mac recorder, which still used the pattern
  the Flow capture path had already moved away from.
- Harden the librime bridge: never pass a nil string across a nonnull
  boundary when a user dictionary holds malformed UTF-8, null-check the
  engine API vector, and distrust a candidate count without its array.
- Guard the expanded candidate grid against a list that shrinks while
  the collection view is still laying out against the previous count.
- Resolve localized text without the App Group so the screen shown when
  that container is missing renders its message instead of crashing on
  launch, which is the one situation it exists to handle.
- Gate CI on the App Group and Keychain wiring that the keyboard
  extension hard-depends on, with a self-test proving the check fails.

Clipboard, reply, and semantics (pre-existing working tree):

- Unify the reply center, refresh keyboard AI features, and add native
  event and reminder exporters.
- Expand clipboard semantic routing with authored corpora and a
  product-blind holdout, plus the scripts that build and validate them.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-18 17:31:29 -07:00

64 lines
2.9 KiB
Swift

// KeyboardInstallationProbe.swift
// OSGKeyboard · Shared
//
// Reports whether the keyboard extension is currently enabled in iOS
// Settings, without needing the extension to have run.
//
// `KeyboardSetupBridge` can only report what the extension observed the last
// time it launched. Removing the keyboard or revoking Full Access never
// launches it, so that record goes stale silently. This probe covers the
// "is the keyboard added" half of the question from the host side.
import Foundation
public enum KeyboardInstallationProbe {
/// Bundle identifier of the keyboard extension.
public static let keyboardExtensionBundleID = "com.osgkeyboard.ios.keyboard"
/// `AppleKeyboards` lives in the global preferences domain, which every
/// app's `UserDefaults` search list includes. It holds one entry per
/// *enabled* keyboard — system layouts as locale strings, third-party
/// keyboards as their extension bundle identifier.
private static let enabledKeyboardsKey = "AppleKeyboards"
/// Bundle identifiers / locale tags of every keyboard enabled in Settings,
/// or `nil` when iOS does not expose the list to this process.
public static func enabledKeyboardIdentifiers() -> [String]? {
// Settings writes this list from another process while the app is
// backgrounded. `UserDefaults` caches the global domain for the life of
// the process, so a plain read keeps returning the pre-change value
// until the app is relaunched — that is what makes the state look like
// it needs several trips to Settings to settle. Going through
// CFPreferences and synchronising first drops that cache.
CFPreferencesAppSynchronize(kCFPreferencesAnyApplication)
return CFPreferencesCopyAppValue(
enabledKeyboardsKey as CFString,
kCFPreferencesAnyApplication
) as? [String]
}
/// `true` / `false` when the list is readable, `nil` when it is not.
///
/// The key is undocumented, so a missing list must degrade to "unknown"
/// rather than "not installed" — callers should keep their extension-side
/// verification as the source of truth and use this only to *contradict*
/// a stale positive.
public static func isKeyboardEnabled(
bundleID: String = keyboardExtensionBundleID
) -> Bool? {
isKeyboardEnabled(bundleID: bundleID, identifiers: enabledKeyboardIdentifiers())
}
/// Pure decision half, split out so tests can supply the list — including
/// the `nil` "not exposed to this process" case, which a test `UserDefaults`
/// cannot reproduce: its search list always includes the global domain the
/// real `AppleKeyboards` value lives in.
public static func isKeyboardEnabled(
bundleID: String = keyboardExtensionBundleID,
identifiers: [String]?
) -> Bool? {
guard let identifiers else { return nil }
return identifiers.contains(bundleID)
}
}