hex/ios/HEXMobile/ExperimentsCard.swift
Ryan Vogel 71c8fe2ade ios: record raw typing logs from Hex's keyboard (experiment)
What changed
- Experiments › Record typing logs (off by default, App Group key
  experiments.typingLogs). It is separate from Learn how I type, which is
  unchanged. While it is on, the keyboard writes NDJSON day files to
  TypingLogs/yyyy-MM-dd.ndjson in the App Group. Events:
  session_start/session_end (field traits, keyboard size and safe area,
  orientation, appearance, device, build, locale); layout (every key's frame
  and hit frame, the strip and its suggestion slots, and a hash, with the key
  list written once per file); touch (began/moved/ended/cancelled with raw
  x/y, majorRadius and tolerance, force, UITouch.timestamp, the key the engine
  resolved, and the key under the point when they differ; moves are throttled
  to 2 pt and 8 ms); edit (insert, delete, word delete, autocorrect,
  autocorrect undo, suggestion, double-space period, return and send, paste,
  undo dictation, trackpad cursor); candidates; context
  (documentContextBefore/AfterInput capped at 200 characters, after each word,
  return and dictation and once host changes settle, marked cleared when the
  host empties the field); ui (accents, trackpad, the Hex key and its menu,
  strip chips, globe); dictation (raw, learned fixes, spelling, tagger, Apple
  Intelligence, translation, formatting, inserted text, durations, engine and
  locale, from in-keyboard and app dictation); and dictation_outcome (from a
  second DictationEditTracker). The schema is in ios/docs/typing-logs.md.
- Password, new password, one-time code, every credit card content type, and
  secure fields write one skipped line and nothing else. A new field, or new
  traits, starts a new session.
- TypingLogger buffers events on the main thread. TypingLogStore encodes and
  appends them on a serial utility queue every 2 s, when the keyboard goes
  away, and on a memory warning. Each day is capped at 64 MB with one
  cap_reached line, and files are kept 14 days. When the App Group can't be
  reached (no Full Access), events are dropped silently. Nothing is sent.
- Hooks: FullKeyboardEngine.typingLog, and the KeyboardKeyPlaneView touch
  path, which the UITouch overrides now reach through
  began/moved/ended/cancelled(id:sample:). Every call is skipped while the
  logger is nil, so typing is unchanged with the experiment off.
  KeyboardSnapshot gains optional stages, sent only while the experiment is
  on. KeyboardSpeech.transcribe also returns its locale.
- App: the toggle and a Typing logs screen (each day with its size and event
  count, Share per day, Share all through the share sheet, Delete all with a
  confirmation). Also --snap-state experiments and typing-logs scenes, and an
  --experiment-typing-logs launch argument.

Why
Ryan wants to export a day of raw typing to his Mac, to test and train
prediction models and a per-user touch model (adaptive key targets).

Verified
- xcodebuild test on the iPhone 18 Pro simulator: 177 tests, 0 failures.
  This includes 9 new TypingLogTests: line format and escaping; day rotation
  with layouts in full once per file; Delete all while the writer holds the
  file open; the daily cap (tested at 4 KB) across two writers; 14-day
  retention; the skip rule; a skipped session writing one line; and a real
  FullKeyboardView typing 25 taps through the plane's touch path
  (autocorrect, delete, double-space period, the 123 layer, return, a
  long-press é). That log's edits replay to the field's text, "Don't go yet.
  7 ok\nCafé". An independent Python replay of the same file matched, and
  every lifted key's point was inside its logged hit frame.
- Overhead, simulator Debug, 3,000 taps: about 9 µs a tap on the main thread
  (38 → 47 µs) and about 1 KB a tap (5.2 lines), so about 12 MB an hour at
  200 characters a minute.
- The Release generic-iOS build with CODE_SIGNING_ALLOWED=NO succeeds.
- Simulator screenshots of the toggle and the Typing logs screen.
- Not verified on a physical iPhone: real touch radii and sampling rates, how
  often hosts call textDidChange, whether documentIdentifier stays stable,
  the extension's write cost, and AirDrop from the share sheet.
2026-11-19 21:11:20 -05:00

246 lines
11 KiB
Swift

import SwiftUI
/// Unfinished features, off until turned on here. They live in the App Group so the
/// keyboard reads them too.
struct ExperimentsCard: View {
@State private var fullKeyboard = FullKeyboardSettings().isEnabled
@State private var cleanupTagger = CleanupTaggerSettings().isEnabled
@State private var appleIntelligenceCleanup = AppleIntelligenceCleanupSettings().isEnabled
@State private var customVocabulary = CustomVocabularySettings().isEnabled
@State private var cleanupAvailability = AppleIntelligenceCleanupAvailability.current
@State private var autocorrect = FullKeyboardSettings().autocorrect
@State private var learnTyping = TypingJournalSettings().isEnabled
@State private var typingLogs = TypingLogSettings().isEnabled
@State private var showTypingLogs = false
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text("Experiments")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textMuted)
.padding(.leading, 16)
VStack(spacing: 0) {
Toggle(isOn: $fullKeyboard) {
VStack(alignment: .leading, spacing: 2) {
Text("Full keyboard")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Type with Hex, with a Hex key to dictate")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.onChange(of: fullKeyboard) { _, enabled in
FullKeyboardSettings().isEnabled = enabled
}
if fullKeyboard {
Divider()
.overlay(HexTheme.borderWeak)
.padding(.leading, 16)
Toggle(isOn: $autocorrect) {
VStack(alignment: .leading, spacing: 2) {
Text("Autocorrect")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Fix contractions, shortcuts, and one-edit typos on space")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.onChange(of: autocorrect) { _, enabled in
FullKeyboardSettings().autocorrect = enabled
}
}
Divider()
.overlay(HexTheme.borderWeak)
.padding(.leading, 16)
// Not only for the full keyboard: it also learns from dictations the
// dictation bar inserts.
Toggle(isOn: $learnTyping) {
VStack(alignment: .leading, spacing: 2) {
Text("Learn how I type")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Keeps where you tap and the words you fix, typed or dictated, on this iPhone, never what you write")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.onChange(of: learnTyping) { _, enabled in
TypingJournalSettings().isEnabled = enabled
}
if learnTyping {
TypingInsightsView()
.padding(.horizontal, 16)
.padding(.bottom, 12)
}
Divider()
.overlay(HexTheme.borderWeak)
.padding(.leading, 16)
// Apart from Learn how I type: this keeps everything, to export and train on.
Toggle(isOn: $typingLogs) {
VStack(alignment: .leading, spacing: 2) {
Text("Record typing logs")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Saves every tap, where it lands, and the text you type or dictate with Hex's keyboard on this iPhone for 14 days, so you can export it. Nothing is sent.")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.onChange(of: typingLogs) { _, enabled in
TypingLogSettings().isEnabled = enabled
}
if typingLogs {
Button {
showTypingLogs = true
} label: {
HStack {
Text("Typing logs")
.font(.system(size: 13, weight: .medium))
.foregroundStyle(HexTheme.textBase)
Spacer()
Image(systemName: "chevron.right")
.font(.system(size: 12, weight: .semibold))
.foregroundStyle(HexTheme.textFaint)
}
.contentShape(Rectangle())
}
.buttonStyle(.plain)
.padding(.horizontal, 16)
.padding(.bottom, 12)
.sheet(isPresented: $showTypingLogs) {
TypingLogsView()
}
}
}
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
.onChange(of: fullKeyboard) { _, enabled in
FullKeyboardSettings().isEnabled = enabled
}
Toggle(isOn: $cleanupTagger) {
VStack(alignment: .leading, spacing: 2) {
Text("Clean up what I say")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Removes ums, repeats and things you corrected (3pm, actually 4pm), on this iPhone")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
.onChange(of: cleanupTagger) { _, enabled in
CleanupTaggerSettings().isEnabled = enabled
}
Toggle(isOn: $appleIntelligenceCleanup) {
VStack(alignment: .leading, spacing: 2) {
Text("Clean up with Apple Intelligence")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Fix misheard words and spoken corrections on device")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
if let reason = cleanupAvailability.reason {
Text(reason)
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
}
.tint(HexTheme.success)
.disabled(!cleanupAvailability.isAvailable)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
.onChange(of: appleIntelligenceCleanup) { _, enabled in
AppleIntelligenceCleanupSettings().isEnabled = enabled
}
Toggle(isOn: $customVocabulary) {
VStack(alignment: .leading, spacing: 2) {
Text("Custom vocabulary")
.font(.system(size: 15, weight: .regular))
.foregroundStyle(HexTheme.textBase)
Text("Spell names and terms your way")
.font(.system(size: 12))
.foregroundStyle(HexTheme.textMuted)
}
}
.tint(HexTheme.success)
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
.onChange(of: customVocabulary) { _, enabled in
CustomVocabularySettings().isEnabled = enabled
}
TranslateDictationRow()
.padding(.horizontal, 16)
.padding(.vertical, 10)
.background(
HexTheme.bgBase,
in: RoundedRectangle(cornerRadius: HexTheme.cardRadius, style: .continuous)
)
if customVocabulary {
CustomVocabularyEditor()
.padding(12)
.background(
HexTheme.bgBase,
in: RoundedRectangle(
cornerRadius: HexTheme.cardRadius,
style: .continuous
)
)
}
}
.onAppear {
fullKeyboard = FullKeyboardSettings().isEnabled
cleanupTagger = CleanupTaggerSettings().isEnabled
appleIntelligenceCleanup = AppleIntelligenceCleanupSettings().isEnabled
customVocabulary = CustomVocabularySettings().isEnabled
cleanupAvailability = AppleIntelligenceCleanupAvailability.current
autocorrect = FullKeyboardSettings().autocorrect
learnTyping = TypingJournalSettings().isEnabled
typingLogs = TypingLogSettings().isEnabled
}
}
}