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>
This commit is contained in:
2026-09-18 17:31:29 -07:00
co-authored by Claude Opus 5
parent 628e983794
commit 0023741455
210 changed files with 14731 additions and 2940 deletions
+3 -3
View File
@@ -111,7 +111,7 @@ struct DashboardView: View {
// 总览:累积所有会话文本,内部滚动、自动贴底显示最新一句。
ScrollView {
Text(viewModel.homePreviewText)
.font(.system(size: 20, weight: .regular))
.font(TypeStyle.title3.weight(.regular))
.foregroundStyle(palette.textPrimary)
.lineSpacing(4)
.textSelection(.enabled)
@@ -216,7 +216,7 @@ struct BottomDictationBar: View {
.truncationMode(.tail)
Spacer(minLength: Spacing.xs)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .semibold))
.font(TypeStyle.caption2.weight(.semibold))
.foregroundStyle(palette.textTertiary)
}
.font(MacSettingsType.control)
@@ -249,7 +249,7 @@ struct BottomDictationBar: View {
private var optionHoldHint: some View {
HStack(spacing: Spacing.xxs) {
Image(systemName: "option")
.font(.system(size: 12, weight: .semibold))
.font(TypeStyle.caption.weight(.semibold))
Text(MacL10n.string("mac.hint.holdOption.compact", language: lang))
.font(TypeStyle.caption)
.lineLimit(1)
+26 -28
View File
@@ -23,11 +23,9 @@ final class MacAudioRecorder: MacAudioRecording, @unchecked Sendable {
var errorDescription: String? {
switch self {
case .converterUnavailable:
return "无法初始化音频转换器 / Failed to initialize audio converter"
return MacL10n.string("mac.error.converterUnavailable")
case .microphoneAccessDenied:
return "麦克风权限被拒绝——请在「系统设置 → 隐私与安全性 → 麦克风」中启用"
+ " / Microphone access denied — enable it in System Settings"
+ " → Privacy & Security → Microphone"
return MacL10n.string("mac.error.microphoneAccessDenied")
}
}
}
@@ -39,7 +37,11 @@ final class MacAudioRecorder: MacAudioRecording, @unchecked Sendable {
channels: 1,
interleaved: false
)!
private var converter: AVAudioConverter?
/// Route-adaptive resampler shared with the iOS capture paths. It rebuilds
/// its converter from the format of each incoming buffer, so switching
/// between the built-in mic, AirPods and a USB interface mid-recording
/// resamples correctly instead of feeding a converter a stale rate.
private var downsampler: FlowAdaptiveDownsampler?
private let lock = NSLock()
private var samples: [Float] = []
private var snapshotContinuation: AsyncStream<AudioBufferSnapshot>.Continuation?
@@ -63,9 +65,6 @@ final class MacAudioRecorder: MacAudioRecording, @unchecked Sendable {
/// whole excess in one move instead.
private static let trimHysteresisSamples = 30 * 16_000
private var smoothedLevel: Float = 0
/// One-shot flag for the converter pull block. Taps are serialized per
/// bus, so a plain instance property (not a captured local) is safe here.
private var didProvideInput = false
/// Normalised input level (0…1), smoothed for a calm waveform.
/// Read from the main thread by a polling timer while recording.
@@ -162,12 +161,20 @@ final class MacAudioRecorder: MacAudioRecording, @unchecked Sendable {
let input = engine.inputNode
let inputFormat = input.outputFormat(forBus: 0)
guard let converter = AVAudioConverter(from: inputFormat, to: targetFormat) else {
// Pre-flight only: the converter actually used on the audio thread is
// built by the downsampler from the first delivered buffer. Probing here
// still turns "no usable input device" into a clean thrown error.
guard AVAudioConverter(from: inputFormat, to: targetFormat) != nil else {
throw RecorderError.converterUnavailable
}
self.converter = converter
self.downsampler = FlowAdaptiveDownsampler(targetFormat: targetFormat)
input.installTap(onBus: 0, bufferSize: 4_096, format: inputFormat) { [weak self] buffer, _ in
// `format: nil` — NEVER pass an explicit format. `installTap` raises an
// uncatchable NSException ("Failed to create tap due to format
// mismatch") when the format read above has already been superseded by
// a route change, and that kills the whole app. `nil` always resolves
// to the node's live format.
input.installTap(onBus: 0, bufferSize: 4_096, format: nil) { [weak self] buffer, _ in
self?.appendResampled(buffer)
}
engine.prepare()
@@ -202,26 +209,17 @@ final class MacAudioRecorder: MacAudioRecording, @unchecked Sendable {
}
private func appendResampled(_ buffer: AVAudioPCMBuffer) {
guard let converter else { return }
let ratio = targetFormat.sampleRate / buffer.format.sampleRate
let capacity = AVAudioFrameCount(Double(buffer.frameLength) * ratio) + 1_024
guard let output = AVAudioPCMBuffer(pcmFormat: targetFormat, frameCapacity: capacity) else { return }
didProvideInput = false
var conversionError: NSError?
converter.convert(to: output, error: &conversionError) { [self] _, statusPointer in
if didProvideInput {
statusPointer.pointee = .noDataNow
return nil
}
didProvideInput = true
statusPointer.pointee = .haveData
return buffer
}
guard conversionError == nil, let channel = output.floatChannelData else { return }
guard let downsampler else { return }
// The downsampler owns the frame-count arithmetic (it clamps before
// converting to `AVAudioFrameCount`, which traps on NaN/overflow) and
// the one-shot pull flag.
guard case .converted(let output) = downsampler.convertReusingScratch(buffer),
let channel = output.floatChannelData else { return }
let frameCount = Int(output.frameLength)
guard frameCount > 0 else { return }
// `output` is the downsampler's reusable scratch buffer, valid only
// until the next callback — copy the samples out synchronously.
let chunk = Array(UnsafeBufferPointer(start: channel[0], count: frameCount))
// RMS → rough 0…1 level with an attack/decay smoothing so the UI
+1 -1
View File
@@ -341,7 +341,7 @@ private struct MacDictionaryRow: View {
Spacer(minLength: Spacing.sm)
Button(action: delete) {
Image(systemName: "trash")
.font(.system(size: 13, weight: .medium))
.font(TypeStyle.footnote.weight(.medium))
.frame(width: 24, height: 24)
}
.buttonStyle(.borderless)
+1 -1
View File
@@ -203,7 +203,7 @@ private struct MacHistoryRow: View {
Spacer(minLength: Spacing.sm)
Button(action: delete) {
Image(systemName: "trash")
.font(.system(size: 13, weight: .medium))
.font(TypeStyle.footnote.weight(.medium))
.frame(width: 24, height: 24)
}
.buttonStyle(.borderless)
+1 -1
View File
@@ -87,7 +87,7 @@ private struct MacOpenSourceLicenseDetailView: View {
.multilineTextAlignment(.leading)
Spacer(minLength: 0)
Image(systemName: "arrow.up.right.square")
.font(.system(size: 12))
.font(TypeStyle.caption.weight(.regular))
.foregroundStyle(palette.textTertiary)
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ enum MacLocalASRError: Error, LocalizedError {
case .qwen3InferenceFailed(let detail):
return MacL10n.format("mac.error.qwen3InferenceFailed", detail)
case .speechDenied:
return "Speech recognition permission denied"
return MacL10n.string("mac.error.speechDenied")
case .speechFailed(let detail):
return detail
case .emptyTranscript:
+4 -4
View File
@@ -334,10 +334,10 @@ struct MacOnboardingView: View {
private func featureRow(_ icon: String, _ text: String) -> some View {
HStack(spacing: Spacing.md) {
Image(systemName: icon)
.font(.system(size: 15, weight: .semibold))
.font(TypeStyle.body.weight(.semibold))
.foregroundStyle(palette.accent)
.frame(width: 26, height: 26)
.background(palette.accentMuted, in: RoundedRectangle(cornerRadius: 8, style: .continuous))
.background(palette.accentMuted, in: RoundedRectangle(cornerRadius: Radius.small, style: .continuous))
Text(text)
.font(TypeStyle.footnote)
@@ -355,7 +355,7 @@ struct MacOnboardingView: View {
private func permissionCard(isGranted: Bool, grantedText: String, neededText: String) -> some View {
HStack(spacing: Spacing.sm) {
Image(systemName: isGranted ? "checkmark.seal.fill" : "exclamationmark.circle.fill")
.font(.system(size: 20, weight: .semibold))
.font(TypeStyle.title3)
.foregroundStyle(isGranted ? palette.accent : palette.warning)
Text(isGranted ? grantedText : neededText)
@@ -520,7 +520,7 @@ struct MacOnboardingView: View {
VStack(alignment: .leading, spacing: Spacing.md) {
HStack(spacing: Spacing.sm) {
Image(systemName: model.isDefaultModelInstalled ? "checkmark.circle.fill" : "shippingbox.fill")
.font(.system(size: 20, weight: .medium))
.font(TypeStyle.title3.weight(.medium))
.foregroundStyle(model.isDefaultModelInstalled ? palette.accent : palette.textTertiary)
VStack(alignment: .leading, spacing: 2) {
+16 -5
View File
@@ -75,10 +75,10 @@ struct MacPolishStylesView: View {
title: MacL10n.string("mac.styles.fun", language: lang),
packs: PolishStylePackCatalog.BuiltinStyleGroup.fun.packs
)
if !catalog.entries.isEmpty {
if !handWrittenPacks.isEmpty {
styleSection(
title: MacL10n.string("mac.styles.custom", language: lang),
packs: catalog.entries
packs: handWrittenPacks
)
}
}
@@ -126,6 +126,13 @@ struct MacPolishStylesView: View {
PolishStyleLearningCorpusBuilder.build(from: history.snapshot())
}
/// Distilled personal styles drive AI replies, never dictation, so they are
/// not selectable here. macOS has no reply skills of its own — the pack is
/// generated from Mac dictation and takes effect on iPhone through iCloud.
private var handWrittenPacks: [PolishStylePack] {
catalog.entries.filter { !PolishStylePackCatalog.isPersonalReplyStyle($0) }
}
private var styleLearningCard: some View {
let corpus = styleLearningCorpus
let required = PolishStyleLearningCorpusBuilder.requiredEffectiveCharacterCount
@@ -134,7 +141,7 @@ struct MacPolishStylesView: View {
return VStack(alignment: .leading, spacing: Spacing.md) {
HStack(alignment: .top, spacing: Spacing.md) {
Image(systemName: "waveform")
.font(.system(size: 20, weight: .semibold))
.font(TypeStyle.title3)
.foregroundStyle(palette.accent)
.frame(width: 42, height: 42)
.background(
@@ -330,7 +337,11 @@ struct MacPolishStylesView: View {
do {
try updated.upsert(pack)
store.setPolishStyleCatalog(updated)
store.setActivePolishStyleId(pack.id)
if PolishStylePackCatalog.isPersonalReplyStyle(pack) {
store.setPersonalReplyStyleId(pack.id)
} else {
store.setActivePolishStyleId(pack.id)
}
viewModel.refreshPolishStyles()
Task {
try? await MacICloudSyncBootstrap.polishStyleSync.pushLocalIfEnabled(updated)
@@ -402,7 +413,7 @@ private struct MacPolishStyleCard: View {
// Pencil opens the prompt (built-in, read-only) or the editor (custom).
Button(action: primaryAction) {
Image(systemName: "pencil")
.font(.system(size: 15, weight: .semibold))
.font(TypeStyle.body.weight(.semibold))
.foregroundStyle(palette.textSecondary)
.frame(width: 30, height: 30)
.background(palette.background.opacity(isHovering ? 0.9 : 0.72), in: Circle())
+1 -1
View File
@@ -131,7 +131,7 @@ private struct MacSidebarRow: View {
.padding(.vertical, 9)
.background(
rowBackground,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
in: RoundedRectangle(cornerRadius: Radius.small, style: .continuous)
)
.contentShape(Rectangle())
}
+3 -3
View File
@@ -42,7 +42,7 @@ private struct MacPickerFieldBox: View {
Spacer(minLength: Spacing.xs)
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .semibold))
.font(TypeStyle.caption2.weight(.semibold))
.foregroundStyle(palette.textTertiary)
}
.padding(.horizontal, Spacing.sm)
@@ -360,7 +360,7 @@ struct MacProviderModelRow: View {
isDropdownOpen.toggle()
} label: {
Image(systemName: "chevron.up.chevron.down")
.font(.system(size: 11, weight: .semibold))
.font(TypeStyle.caption2.weight(.semibold))
.foregroundStyle(palette.textTertiary)
.frame(width: chevronWidth, height: MacMetrics.settingsControlHeight)
.contentShape(Rectangle())
@@ -427,7 +427,7 @@ struct MacProviderModelRow: View {
Spacer(minLength: 0)
if isSelected {
Image(systemName: "checkmark")
.font(.system(size: 11, weight: .semibold))
.font(TypeStyle.caption2.weight(.semibold))
.foregroundStyle(palette.textPrimary)
}
}
+2 -2
View File
@@ -598,10 +598,10 @@ struct MacSettingsView: View {
}
private var accessibilityStatusGranted: String {
lang.resolvedLanguageCode().hasPrefix("zh") ? "已授权" : "Granted"
MacL10n.string("mac.accessibility.status.granted", language: lang)
}
private var accessibilityStatusNeeded: String {
lang.resolvedLanguageCode().hasPrefix("zh") ? "未授权" : "Needed"
MacL10n.string("mac.accessibility.status.needed", language: lang)
}
}
+1 -1
View File
@@ -254,7 +254,7 @@ private struct MacMenuBarPopover: View {
private var onboardingPrompt: some View {
VStack(spacing: Spacing.md) {
Image(systemName: "sparkles")
.font(.system(size: 30, weight: .semibold))
.font(TypeStyle.pageTitle)
.foregroundStyle(.accent)
Text(MacL10n.string("mac.onboarding.popover.title", language: viewModel.config.uiLanguage))
@@ -0,0 +1,4 @@
/* Permission prompts (macOS) — English */
"NSMicrophoneUsageDescription" = "OSGKeyboard uses the microphone to transcribe your voice via your configured cloud speech provider.";
"NSSpeechRecognitionUsageDescription" = "OSGKeyboard uses on-device speech recognition for local dictation mode.";
@@ -0,0 +1,4 @@
/* 权限说明(macOS)— 简体中文 */
"NSMicrophoneUsageDescription" = "OSGKeyboard 使用麦克风,通过你配置的云端语音服务商将语音转为文字。";
"NSSpeechRecognitionUsageDescription" = "OSGKeyboard 在本地听写模式下使用设备端语音识别。";