Skip to content

feat(coach): Apple's on-device model as a keyless, offline provider - #2935

Open
obinfalah1 wants to merge 2 commits into
ryanbr:mainfrom
obinfalah1:apple-on-device-ai
Open

obinfalah1 wants to merge 2 commits into
ryanbr:mainfrom
obinfalah1:apple-on-device-ai

Conversation

@obinfalah1

Copy link
Copy Markdown

What this PR does

A switch that runs the coach on Apple's on-device model (Foundation Models, iOS 26 / macOS 26+): no API key, no network. The prompt and the answer stay on the device, which matches NOOP's offline-first approach.

  • Strand/AI/AppleOnDeviceAI.swift (new): availability status (not enabled, still downloading, device not eligible, older OS) and respond(messages:) through LanguageModelSession. It sits behind #if canImport(FoundationModels) and #available, so older SDKs and OS versions still build and run.
  • The model's window is about 4k tokens for input and output together, so instructions are capped at 3.2k characters and the prompt at 4.2k, trimmed from the end. A plain TODAY line and this week against last week go first so they survive the trim.
  • AICoachEngine: an appleOnDevice setting. When it's on, callProvider and the streaming path answer from Apple's model, a key isn't required, and disconnect() turns it off.
  • AppleOnDeviceToggleRow (new), on the Coach setup card and in Coach settings, using StrandDesign tokens only. While it's on, the bring-your-own-model bar is hidden.
  • New strings added to Strand/Resources/Localizable.xcstrings (de, es, fr, it, pl, pt-PT, ru, zh-Hans, zh-Hant).

Tradeoff: Apple's model is small. It gives short plain-text answers from the data summary. The user's own provider is still the better choice for deep questions, and switching it off returns to that.

Type of change

  • Bug fix
  • New feature
  • Refactor / cleanup
  • Documentation
  • CI / tooling

How it was tested

  • Used on a real iPhone (iOS 27, Apple Intelligence on): the coach answers from the on-device model with the key fields hidden.
  • Not on the BLE path; no protocol, schema or scoring changes.
  • python3 Tools/i18n_audit.py --ci passes, and so does Tools/doc_comment_lint.py.
  • Apple app targets only, which are outside the parity ledger's scope. No Android counterpart is included.

Checklist

  • Swift package tests pass for any package I touched (no package touched)
  • Android unit tests pass if I touched android/ (not touched)
  • No new build warnings introduced (CI will confirm; not built locally)
  • UI changes use only StrandDesign tokens (no hardcoded colors or fonts; spacing literals match the neighbouring Coach settings rows)
  • No hardcoded hex frame bytes; protocol facts live in the schema / decoders
  • Follows the conventions in docs/CONTRIBUTING.md
  • I did not commit generated output (Strand.xcodeproj/) or any secrets/keystores

Related issues

None.

🤖 Generated with Claude Code

obinfalah1 and others added 2 commits November 22, 2026 11:25
Adds a switch that routes the coach (chat, the brief, the morning brief
and the history summary) to Apple's Foundation Models on iOS 26 /
macOS 26. No key, no network: the prompt and the answer stay on the
device. The model's window is about 4k tokens, so the instructions and
prompt are capped and a plain TODAY / week-over-week header goes first.
While it is on, the bring-your-own-model fields are hidden.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- TODAY in the model's header is the row for the Today screen's logical
  day, not the newest stored row; the two weeks are calendar windows.
- respond has a 60 s timeout that releases the coach even if generation
  ignores cancellation.
- The switch cannot be turned on while the model is unavailable, and the
  status refreshes when the app becomes active.
- The four status messages are localized in all locales.
- AICoachAppleHeaderTests pins the header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@obinfalah1

Copy link
Copy Markdown
Author

Follow-up hardening on the on-device route:

  • TODAY in the model's header is the row for the same logical day as the Today screen (Repository.logicalDayKey), not the newest stored row; with no row it says so. The two weeks are calendar windows, not the last 7 and 14 rows.
  • respond has a 60 s timeout that releases the coach even if generation ignores cancellation.
  • The switch can't be turned on while the model is unavailable (it can always be turned off), and status refreshes when the app becomes active.
  • The four status messages are in Localizable.xcstrings for all locales.

Added StrandTests/AICoachAppleHeaderTests.swift. Tools/i18n_audit.py --ci upstream/main and Tools/doc_comment_lint.py pass. The app targets were not built locally (no Xcode here), so app-build.yml on this head is the compile check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant