Moti — technical appendix
For engineers: Moti's two apps with one character, saves built to last, builds and signing for three systems, the site, accounts and decisions with their cost.
TL;DR
Moti is two apps with one character: a Mac version in Swift 6 with SwiftUI and AppKit, and a Windows and Linux version in Python 3.12 and PySide6 (Qt 6) that takes the cat's behaviour from moti_engine, an engine built on the Python standard library. The Mac version has 73 files and 20,898 lines; the Windows and Linux version has 83 files and 24,299 lines. The apps share dictionaries in ten languages, 1,542 strings each, and 2,774 test cases. Builds for all three systems run in GitHub Actions, and the Mac version is signed with a Developer ID and notarized by Apple. The Windows and Linux version has 1,312 tests, the engine 137, the site and services 107, and the Mac version 31 scenarios with 2,207 checks.
Diagram
Both apps live by the same rules and are drawn with the same pictures. Her person picks the brain, data stays on the computer, and little goes to the site.
| Part | What it does | Technology, size |
|---|---|---|
| Mac app | the cat in the menu bar and on screen, the planner, Apple's model | Swift 6, SwiftUI, AppKit, EventKit, FoundationModels; 73 files, 20,898 lines |
| Windows and Linux app | the cat on top of windows, menus, a choice of AI, updates, statistics, reports | Python 3.12, PySide6 (Qt 6), keyring; 83 files, 24,299 lines |
moti_engine | character rules, memory, dictionaries, saving | Python standard library only; 2,638 lines |
| GNOME Shell extension | windows and the cursor on GNOME with Wayland | JavaScript, D-Bus, GNOME 45–50 |
| meetmoti.com | nine sections in ten languages, downloads, news | our own Python generator, 1,375 lines; nginx |
| Accounts | sign-in, the account page, version news in Telegram | Python, PostgreSQL, psycopg 3, OpenID Connect; 1,789 lines |
| Report and statistics receiver, admin panel | errors from the apps, daily numbers, an overview for the team | Python; 575 and 1,692 lines |
Two apps, one character
Kris's prototype is written in Swift and relies on things that exist only on the Mac: the menu bar, calendar and reminders through EventKit, Apple's on-device model, access to the elements of other apps' windows, the Keychain. Rewriting it in a cross-platform framework would have meant losing all of that, so for Windows and Linux I wrote a second app in Python and Qt. Four things keep the character the same:
- The same numbers. The Swift files point to their Python counterparts and repeat their thresholds: trust stages, for example, go up at 40, 50, 75 and 90 and down below 30, 40, 65 and 80 in both versions.
- The same dictionaries. Ten languages, 36 files and 1,542 keys each, live in the Mac app's resources, and the Windows and Linux version reads the very same files. The site build takes menu item names from them, so the site and the app call things by the same names.
- Shared test cases. The Python side writes the sets: 728 phrases from a person for the cat to parse, 490 model replies and 1,556 retellings when the language changes, 2,774 in all. Another 317 memories test answers from memory. The Swift checks read the same files and expect the same results.
- Shared saves. A Python test checks field by field that Swift can read the state the engine returns. At Kris's request, a save from version 0.2.0 is tested from both sides: Python and Swift both read it without losses.
Differences that turn up get closed on both sides. From the start, the Python engine treated the clock going back as a correction, while the Swift version refused all care as "too soon" until the clock caught up; in November the Swift version began to behave like the engine.
The Mac version can reconcile its cat with the engine. If both copies have changed, the engine's copy is saved next to the Mac's save, and only then is the Mac's cat sent to the engine. Merging the copies or letting the person choose is a decision for later, and it is Kris's.
A save that outlives versions
The cat lives for months, while versions ship every few days. So the save is built to open in both older and newer versions:
- The save is JSON. In the engine the state sits in an envelope with a revision number; on the Mac there is no envelope. Dates are written as Swift writes them, in seconds since 2001.
- Every save is stamped with the version that wrote it. A save from a newer version is first copied aside as
state.json.from-<version>. Unknown fields, as well as memories, grudges and places of an unknown kind, are read and written back as they were. - A file that will not read is kept aside as
state.json.corrupt-<time>. If at least some fields read, it is the same cat: the broken fields are replaced with fresh ones, and the cat says what did not read. If nothing reads, the cat starts over and says so. - Only one copy of the app runs. Writes in the engine go under an inter-process lock (
flockon Linux,msvcrt.lockingon Windows), and the file is written through a temporary file,fsyncand an atomic replace. - Memory and the diary are stored in Russian whatever the interface language; only the display is translated. Switching languages does not rewrite what the cat remembers.
What the system lets her see
The cat on top of the windows is a set of transparent, frameless Qt windows that stay on top and have no taskbar button. Wayland does not let an app place its own windows or keep them above others, so on Wayland the app tries XWayland first and falls back to plain Wayland only without it.
| What the cat needs | Windows | Linux X11 | GNOME on Wayland | Mac |
|---|---|---|---|---|
| Active window | GetForegroundWindow, EnumWindows; Store apps through CoreWindow | xprop, xwininfo | our own GNOME Shell extension | AppKit, CGWindowList |
| Person idle | GetLastInputInfo | XScreenSaver | org.gnome.Mutter.IdleMonitor | CGEventSource |
| Buttons and fields in a window | classic Win32 controls | AT-SPI | AT-SPI | Accessibility API |
| Calls | the app's name and meeting cues in the title | same | same, through the extension | CoreAudio's "microphone in use" flag |
| Music | no | MPRIS, state only | MPRIS | player state only |
The GNOME Shell extension gives the app, over D-Bus, the cursor position, the monitors, the active app and window, the window list and call windows, and it can move the cursor: that is how the cat bats it with her paw. It installs from the cat's menu, and GNOME turns it off on the lock screen. KDE has no such helper, so there the cat lives along the bottom of the screen.
On the Mac, exploring a window through Accessibility used to be able to stall the cat: while a hung app failed to answer, her panel, her walk and every command waited. Now it runs off the main thread, with a limit of 0.25 seconds per call, two seconds per exploration and 220 elements. The Mac version sits on windows by their size and position, without screenshots, so it needs no screen recording permission.
The brain and its limits
What Moti sends to models and how she checks replies is covered in the AI section. Technically:
- Cloud: OpenAI's Responses API with
store: false, Gemini'sgenerateContent, Anthropic's Messages API. 35 seconds per service reply, 90 seconds for a whole conversational turn. - Local: Ollama,
/api/chat, a 1,536-token context. The model downloads from the settings window. - The network never holds the window: a turn starts on the main thread, the request runs on a worker thread, the reply is applied back on the main thread, and only the main thread may change the cat's state. One watchdog covers the deadline and cancellation for the whole request: a new question cancels the old one, and the connection closes within a fraction of a second.
- An Ollama the app started is tied to the app's process: on Linux through
PR_SET_PDEATHSIG, on Windows through a Job Object. A model left behind by a crashed run is stopped by the next run only after it confirms it is the very same process: on Linux it compares the process number, its start time, the system boot and the command line.
How it builds and ships
Builds run in GitHub Actions:
- Tests on Ubuntu and Windows, Python 3.12, Qt with no screen: the engine, the app and the report receiver.
- Linux: the AppImage is built on Ubuntu 22.04 so it runs on glibc 2.35 and newer. The build checks that the xcb plugin finds all its libraries, and the AppImage itself starts under Xvfb, lives for ten seconds and saves the cat. The build tools are pinned by SHA-256. On 9 November their release links returned 504 for hours, so the build now fetches the same files through the GitHub API. The finished AppImage also starts on Ubuntu 24.04 and 26.04.
- Windows: an Inno Setup installer installs Moti for one user, without administrator rights. The build installs it silently, checks the Start menu entry and the entry in installed apps, and runs Moti for 20 seconds. It then checks that uninstalling while the cat is running changes no files (a SHA-256 comparison), and that a real uninstall removes autostart and leaves the save and settings.
- Mac: the certificate goes into a temporary keychain, then a Developer ID signature with the hardened runtime and a single entitlement, for calendars and reminders. Then notarization through
notarytool: success is decided by the status in Apple's response, not the exit code. Thenstaplerandspctl, and since 24 November the build also unpacks the finished archive and checks the execute permission and the signature before notarization and after. The whole signed build takes about two minutes.
The files live on the site's server: the repository is private, and GitHub releases would be visible only to us. A single release.json file says which version is current: the version, size and link for each system. The site draws its buttons from it, the app checks it once a day for a new version, and the site build validates its links strictly. A list of SHA-256 checksums sits next to it.
From 1 to 12 November, builds and checks used about 13,200 weighted GitHub minutes: a Windows minute counts as two, a Mac minute as ten, and more than half went to the Swift checks. Since 12 November they run by hand or for a release, the tests run on a computer before pushing, and only the site build is still automatic.
The site and accounts
The site is built by build.py: one frame for all pages, each page only its own middle, and texts from each language's dictionary. On every build it checks:
- the same keys in all ten languages;
- allowed markup in the texts;
- Cyrillic only in Russian and Ukrainian, with no Ukrainian letters in Russian and no Russian ones in Ukrainian;
- download links by rules stricter than Chrome's: across 486,378 addresses there was not one that the build accepts and Chrome rejects.
A build produces 100 pages and 10 Atom feeds. Every page has a canonical link, hreflang for all languages and an x-default pointing to English, and there is a "not found" page in every language. Before going live, 6,450 internal links and anchors were checked, with no broken ones.
Accounts are a separate service on Python's standard HTTP server with PostgreSQL:
- Sign-in through OpenID Connect. Telegram: code and PKCE, with permission for the bot to message first. Google: code, PKCE and nonce. Apple: through a verified
id_token, with no.p8private key. - A sign-in in progress is bound to the browser by a single-use cookie that lasts 10 minutes. A session lasts up to 60 idle days and no longer than a year, and the database stores only its SHA-256.
- Linking the app works like signing in on a TV: an eight-character code from a 25-character alphabet lives for 15 minutes, the app asks every three seconds whether the person has confirmed it, and then receives its own token.
- The account page has your Motis, version news in Telegram, sign-in methods (add any, remove any but the last), a JSON export of your data, signing out everywhere and deleting the account.
- Pages run without scripts, under the CSP
default-src 'none'. Limits on codes and sign-ins are enforced both in the service and in nginx. There are no request logs for these addresses, and the database is backed up every night.
Decisions and why
Two native apps instead of one cross-platform app
The alternative was one Qt app for all three systems. I chose two: the Mac version has the menu bar, EventKit, Apple's model, Accessibility and the Keychain, and Kris's prototype was already in Swift. The cost is that every feature gets written twice, and the shared dictionaries, test cases and numbers keep the two the same.
Code decides the outcome, the model picks the words
The alternative was to let the model decide what the cat did. Then a sleeping cat asks to be petted, and fullness and trust depend on what the model writes. The cost is that model replies have to be checked by rules, and rules have limits: in early November the check on her reply after being woken was wrong on 24 of 575 labelled lines. The review of known issues says it plainly: each new rule fixed some phrases and broke others.
Buttons and a follow-up question on the break prompt
The break prompt can be answered with a "Now" or "Later" button, which was Kris's decision, or in text. Without AI, rules parse the text, and the parse has three outcomes: now, later and unclear. An unclear answer gets a follow-up question and goes neither to the model nor to memory, because the model could otherwise accept or decline the break on the person's behalf. The cost is that you sometimes have to answer twice. The rules are still not flawless: on 2 November they got 34 of 727 tested phrases wrong; for example, the cat took "OK, I don't need a break" as a yes.
A save that both future and past versions can read
The alternative was to convert the save to the new format on load and drop anything unknown. Then a cat moved back to an older version would lose what the newer one had given her. The cost is save copies, separate code paths for unknown data and test saves from old versions.
Our own site generator
The site grew out of a single page by Kris. Instead of Hugo, Astro or Next.js I wrote a 1,375-line script with no dependencies that checks, on every build, what matters for a site in ten languages: keys, letters, markup, links and the release file. The cost is that we maintain this code ourselves.
No network in tests
On 6 November a window test twice sent a "cat is gone" report to the real report address: a background task outlived the mock and found the real send function. Since then the app's tests cannot connect to any address but their own computer. The cost is that everything that goes online is tested against local servers.
Builds on demand
The alternative was running everything on every push. Over 12 days that cost about 13,200 weighted minutes, more than half of them on Macs. The cost is that pull requests no longer check themselves: I run the tests before pushing, and the Swift checks run on a Mac or by hand.
Downloads from our own server
The repository is private, so GitHub releases do not work for the public. The files live on the site's server, and release.json describes the versions. The cost is that a release is built by hand and uploaded by hand.
Referrer-Policy same-origin, not no-referrer
With no-referrer the browser sent forms with Origin: null, and the origin check rejected every form in the accounts and the admin panel, the link confirmation first of all. A check in Chrome caught it. The policy is now same-origin: addresses with codes still do not leak to other sites, and the forms go through.
No IP addresses, separate numbers
The site's logs hold no IP addresses, and statistics and account linking use different install numbers. The cost is that the site does not know its unique visitors, the number of live copies is a rough estimate from update checks, and statistics cannot be tied to a particular person.
Accounts are not merged by email
The first version of accounts merged sign-ins that shared a verified email address. The review removed that: an address can pass to someone else, who would then get another person's account. The cost is that one person may end up with two accounts; a second sign-in method is added from the account page.
20 minutes for Apple's notarization
The first notarization on a new developer account hung at Apple for hours, and a Mac minute on GitHub costs as much as ten Linux minutes. The wait is now capped at 20 minutes. The cost is that if Apple has not finished, the build stops and names the submission, and after Apple answers it is run again, which takes minutes.
How it is tested
- The Windows and Linux version: 1,312 tests in 51 files, 30,995 lines of tests, more than the app itself. On 12 November, 1,334 tests including subtests passed in CI on both Ubuntu and Windows.
- The engine: 137 tests. The site and services: 107, of which 42 cover accounts on a real PostgreSQL. There, fake sign-in providers sign tokens with test RS256, ES256 and Ed25519 keys, and each security check was removed in turn to make sure the tests would catch it.
- The Mac version: no XCTest. 31
check*.shscenarios compile the needed sources withswiftctogether with a separate checking program, 2,207 assertions in all. Among them: 10,000 random care actions with 100 save-and-load cycles, 60 days of unanswered calls, and the switches to and from daylight saving time. - Simulation: the whole Windows and Linux app, with no screen, on a fake clock, with two monitors and a person who works or walks away. The 2-hour and 7-hour runs go with the tests in about 40 seconds; two days run on command in about three minutes.
- Code review: Codex reviews finished commits, and findings are sorted by level, from critical to minor. All 37 issues in the Swift version are fixed, and the manual testing on a Mac is Kris's. Kris's comments are handled point by point; 13 commits answer them.
- Idle, the Mac version used 0.3% of the CPU and 47 MB of memory.
What the project does not do
- There is no single codebase for three systems: the Mac and Windows with Linux are different apps.
- There are no checks on every push: CI builds and tests run by hand and for releases.
- Linux is x86_64 only; the Mac is Apple silicon only, macOS 14 and newer; Windows is x64, with an unsigned installer.
- There is no helper for KDE or for other Wayland desktops besides GNOME.
- Reconciling the Mac version with the engine does not merge two changed copies; it keeps both.
- The app cannot link to an account yet; only the server side exists.