Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# owner's private agent notes.
node_modules
app/node_modules
# The iPhone app is built by EAS, never into the server image.
mobile
dist
data
.env
Expand Down
21 changes: 21 additions & 0 deletions .easignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# EAS uploads the git root when it builds mobile/. This file replaces .gitignore
# for that upload: only the app goes, with its gitignored app.local.json (the
# install's bundle id and EAS project), and nothing of the server or its data.
/*
!/mobile

/mobile/node_modules/
/mobile/.expo/
/mobile/dist/
/mobile/web-build/
/mobile/ios/
/mobile/android/
/mobile/expo-env.d.ts
/mobile/.env*
**/*.p8
**/*.p12
**/*.key
**/*.pem
**/*.jks
**/*.mobileprovision
**/.DS_Store
8 changes: 8 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ disk. Default port **8101**.
| The day screen | `app/src/today.jsx` |
| Scan / search / recents / by hand | `app/src/add.jsx` |
| The camera | `app/src/scanner.jsx` |
| The iPhone app (Expo; its own install, read `mobile/AGENTS.md`) | `mobile/` |
| An overlay, and what it becomes on a desktop | `app/src/overlay.jsx` |
| Runtime state — **gitignored, this is the real log** | `data/state.json` |
| Owner account and settings — **0600** | `data/owner.json`, `data/settings.json` |
Expand Down Expand Up @@ -120,6 +121,13 @@ shows which build is being served.
- **Machine callers** (MCP clients, scripts) use `Authorization: Bearer <token>`:
the API token from Settings (`KRAKATOA_API_TOKEN`), or the Totem bridge
secret. A bearer opens `/api/*` and `/mcp`, never the pages.
- **The iPhone app** (`mobile/`) signs in with the owner's password like a
browser, but sends `X-Krakatoa-Client: native` and gets the session in the
JSON reply instead of a cookie (also on a password change). It sends it back
as `X-Krakatoa-Session`, which `readSession` accepts when there is no cookie.
It is a session, not a token: Settings writes work, sign-out ends it. A
browser cannot send that header cross-site without a CORS preflight, which
this server never answers. Tested in `auth.test.js` and `install.test.js`.
- **`KRAKATOA_AUTH=proxy`** turns sign-in off for an install already behind an
authenticating proxy. Then the proxy is the whole trust boundary.
- The gate is in `server.js` (`authRoutes`, then `gate`). Icons, the manifest
Expand Down
44 changes: 44 additions & 0 deletions mobile/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files

# dependencies
node_modules/

# Expo
.expo/
dist/
web-build/
expo-env.d.ts

# Native
.kotlin/
*.orig.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision

# Metro
.metro-health-check*

# debug
npm-debug.*
yarn-debug.*
yarn-error.*

# macOS
.DS_Store
*.pem

# local env files
.env*.local

# typescript
*.tsbuildinfo

# generated native folders
/ios
/android

# this install's bundle id and EAS project (app.config.js)
app.local.json
104 changes: 104 additions & 0 deletions mobile/AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# AGENTS.md — Krakatoa for iPhone

The native client for the Krakatoa server. It recreates the web app's **phone** layout
(`../app/src`, served by `../server/server.js`) one to one, and uses native mechanics where a
phone has them: overlays are page sheets, the add flow is a native stack inside its sheet (push and
swipe back), the camera reads barcodes natively, confirms and "⋯" are system sheets, some lists
swipe, the day swipes between days and pulls to sync, and logging, scanning and toggles give haptics.

It is modelled on Totem's app (`mobile/` in the Totem repo): same layout, tooling and conventions.

## Expo has changed — do not trust memory

This is Expo SDK 57 (React Native 0.86, expo-router). Before touching an Expo API, read the versioned docs:
append `.md` to any docs.expo.dev URL, starting from https://docs.expo.dev/llms.txt.
Use `npx expo install <pkg>` for dependencies so versions match the SDK.

## How it fits together

- `src/lib/` holds the web's logic, ported nearly verbatim at the **same names** as `app/src/lib/`:
`food.ts`, `macros.ts`, `combos.ts`, `basics.ts`, `restaurants.ts`, `label.ts`, `ask.ts`,
`askapi.ts`, `whoop.ts`, `server.ts`, `photo.ts`, `signout.ts`, `store.ts`. Each says "Ported from
app/src/lib/…". When the web changes one of these, port the change the same way. Only fix what React
Native needs (no `window`, `document`, `navigator`, canvas or `<input type=file>`).
- `store.ts` is the web's `useStore`, but ONE shared store instead of a hook's state: the day, the add
sheet and the food sheet are separate screens, so they subscribe to the same module. Writers, readers
and the merge rule are the web's, line for line. `startSync()` (mounted by the root layout once
signed in) pulls on open, on return to the foreground and every minute, and retries a pending push
on the same beats (there is no `online` event).
- `localStorage` is real: `src/lib/boot.ts` installs expo-sqlite's synchronous localStorage before
anything else loads, so the log (`krakatoa.state.v1`), the basics and the chain menus persist and
work offline. Sign-out clears every `krakatoa.*` key but the server address.
- `src/lib/connection.ts` holds the server's base URL and the session. Every request goes through
`apiFetch` (the web's `fetch('/api/…')`) or `imageSource` (for expo-image). It never uses the
cookie jar. A 401 from `/api` forgets the session and returns to Connect, as the web's
`installAuthRedirect` sends a browser to `/login`.
- `src/theme.ts` holds the web's CSS variables (`--panel-2` → `colors.panel2`). `src/ui/` holds the
building blocks: `T` (text), `kit.tsx` (the web's `.btn`, `.icon-btn`, `.link`, `.chip`, `.seg`,
`.text-input`, `.field`, `.panel`, `.big-action`, `.search-box`, `.stepper`, `.macro-readout` …),
`Pane.tsx` (`.pane`: head, scrolling body, a foot that rides the keyboard), `Thumb.tsx`, `menu.ts`
(system menus and confirms), `haptics.ts`, `ToastHost.tsx`.
- `src/app/` holds routes only. `index` is the day; `add/` is the add sheet's stack; `food`, `targets`
and `settings` are page sheets; `connect` is first run. Presentations are declared in
`src/app/_layout.tsx`, on the navigator.
- `src/features/<area>/` holds the screens: `today/` (the day, water, targets), `add/` (every view of
the web's AddFlow, the camera, the combo and bowl builders, the label form), `ask/`, `food/`,
`settings/`. `features/add/flow.ts` is what AddFlow kept in `useState` (mode, the combo being built,
the food picked, the barcode that missed), shared by the add sheet's screens.

## Signing in

The server signs a browser in with a cookie. The app signs in with the same username and password
(`POST /api/auth/login` with `X-Krakatoa-Client: native`), gets the session in the reply instead of a
cookie, keeps it in the keychain (expo-secure-store) and sends it as `X-Krakatoa-Session`. To the
server it is the same session: same signature and version check, Settings may be changed with it, and
signing out or changing the password ends it. Changing the password on the phone hands it the next
session in the reply. A server with `KRAKATOA_AUTH=proxy` asks for nothing, and one behind Cloudflare
Access is signed into first (`cfAccess.ts`, the same as Totem's app).

## Rules

- **The web is the spec.** Same sections, copy, order, colours and states as the web's phone layout. A
difference must be deliberate and native (a sheet, a swipe, a system menu, a haptic), not drift.
- **Never hand-draw an icon.** The web's lucide icons come from lucide-react-native, imported per icon
(`lucide-react-native/icons/<name>`, the package's own subpath) under the web's names in
`src/lib/icons.ts`. Add one there; confirm the file exists in `node_modules` first. The water's two
sine waves are the one drawn shape, as on the web (AGENTS.md §9): a liquid fill, not a glyph.
- **The app has no API of its own.** Keep it aligned with `server/server.js`. The one server change it
needed is the session header above (`server/auth.js`); test any other in `server/install.test.js`.
- **A component never holds a COPY of an entry**, the web's rule: derive it from the store every render.
- **This directory is published with the rest of Krakatoa.** No hosts, IPs, names, emails or accounts
in tracked files; the install's identity lives in the gitignored `app.local.json`.

## Running, building, shipping

```sh
cd mobile && npm install # its own install; app.local.json first (README)
npx tsc --noEmit # typecheck
npx expo export --platform ios --output-dir /tmp/x # proves the JS bundles
npm run start:tailscale # Metro on the Tailscale IP, for the dev client
```

- **Dev client:** `npx eas-cli build -p ios --profile development`, install it on the phone, then
`npm run start:tailscale`. Rebuild it whenever a native module is added or `app.json` plugins change.
Expo Go cannot run this app (the camera's scanner, keyboard-controller and the rest need the dev client).
- **TestFlight:** `npx eas-cli build -p ios --profile production --auto-submit` (build number auto-increments).
- **Over-the-air:** `npx eas-cli update --channel production -m "…"` ships JS-only changes to installed
TestFlight builds. `runtimeVersion` uses the fingerprint policy, so an update only reaches binaries
whose native code matches. If native code changed, build again instead.

## Gotchas

- iOS ATS allows plain http (`NSAllowsArbitraryLoads`) because the server is usually reached over
Tailscale at `http://<100.x>:8101`. The camera does not need https here, unlike the web's.
- `expo/fetch` is the global fetch on SDK 57; requests pass `credentials: 'omit'` so no cookie is ever
stored or sent beside the keychain's session.
- A sheet opened from another sheet waits for the first to close (`setTimeout(…, 350)` in the food
sheet's "Log it again" and the camera's hand-offs): presenting while one is dismissing is dropped.
- **The install's identity is not in the repo.** The bundle id and the EAS project id live in the gitignored
`app.local.json`, which `app.config.js` merges into `app.json`. The repo-root `.easignore` uploads only
`mobile/` to EAS, including that file.
- `eas.json` is part of the runtime fingerprint. Change it only alongside a new build, or later
`eas update`s target a runtime the installed build does not have. `eas update` needs `--environment production`.
- Every screen sits under an error boundary (`src/shell/ScreenError.tsx`), so a server whose data changed
shape shows an error on that screen instead of closing the app.
35 changes: 35 additions & 0 deletions mobile/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Krakatoa for iPhone

The native app for [Krakatoa](../README.md), a self-hosted food log. It is the web app's phone layout
rebuilt in React Native (Expo SDK 57): the same day, the same ways to add food, with page sheets, a
native barcode scanner, system menus, swipes and haptics.

## Run

Copy `app.local.example.json` to `app.local.json` and fill in your own bundle id and EAS project id
(`npx eas-cli init` creates a project). It is gitignored; `app.config.js` reads it.

```bash
cd mobile
npm install
npm run start:tailscale # Metro on this machine's Tailscale IP
```

Open it in a Krakatoa development build (Expo Go cannot load its native modules). On first launch, enter:

- **Server**: the machine running Krakatoa, e.g. `100.64.0.10` (port 8101 is assumed) or `https://food.example.com`
- **Username and password**: the owner account created at `/setup`. A server running with
`KRAKATOA_AUTH=proxy` needs neither.

The session is stored in the iOS keychain. The server must include the native sign-in (any Krakatoa
from the commit that added this app onwards).

## Build

```bash
npx eas-cli build -p ios --profile development # dev client
npx eas-cli build -p ios --profile production --auto-submit # TestFlight
npx eas-cli update --channel production -m "message" # JS-only update to installed builds
```

See `AGENTS.md` for how the code is laid out and the rules for changing it.
24 changes: 24 additions & 0 deletions mobile/app.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// app.json holds the app; this adds the install's own identity from app.local.json,
// which is gitignored: the bundle id and the EAS project the builds and updates
// belong to. Without it the app still runs in a dev client, but cannot build or
// receive updates. See app.local.example.json.
const fs = require('node:fs')
const path = require('node:path')

function local() {
const file = path.join(__dirname, 'app.local.json')
return fs.existsSync(file) ? JSON.parse(fs.readFileSync(file, 'utf8')) : {}
}

module.exports = ({ config }) => {
const { bundleIdentifier, easProjectId } = local()
return {
...config,
ios: { ...config.ios, ...(bundleIdentifier && { bundleIdentifier }) },
android: { ...config.android, ...(bundleIdentifier && { package: bundleIdentifier }) },
...(easProjectId && {
extra: { ...config.extra, eas: { projectId: easProjectId } },
updates: { ...config.updates, url: `https://u.expo.dev/${easProjectId}` },
}),
}
}
74 changes: 74 additions & 0 deletions mobile/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"expo": {
"name": "Krakatoa",
"slug": "krakatoa-mobile",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"scheme": "krakatoa",
"userInterfaceStyle": "dark",
"backgroundColor": "#0d0f12",
"ios": {
"bundleIdentifier": "com.example.krakatoa",
"supportsTablet": false,
"infoPlist": {
"NSAppTransportSecurity": {
"NSAllowsArbitraryLoads": true
},
"ITSAppUsesNonExemptEncryption": false
}
},
"android": {
"package": "com.example.krakatoa",
"adaptiveIcon": {
"backgroundColor": "#0d0f12",
"foregroundImage": "./assets/icon.png"
}
},
"web": {
"output": "single",
"favicon": "./assets/favicon.png"
},
"plugins": [
"expo-router",
[
"expo-splash-screen",
{
"backgroundColor": "#0d0f12",
"image": "./assets/splash-icon.png",
"imageWidth": 120
}
],
"expo-status-bar",
"expo-secure-store",
"expo-sqlite",
"expo-web-browser",
"expo-image",
"expo-asset",
"expo-font",
[
"expo-camera",
{
"cameraPermission": "Krakatoa uses the camera to scan barcodes and photograph nutrition labels.",
"recordAudioAndroid": false,
"barcodeScannerEnabled": true
}
],
[
"expo-image-picker",
{
"photosPermission": "Krakatoa reads a nutrition label, a receipt or a plate from a photo you pick.",
"cameraPermission": "Krakatoa uses the camera to scan barcodes and photograph nutrition labels.",
"microphonePermission": false
}
]
],
"experiments": {
"typedRoutes": false,
"reactCompiler": false
},
"runtimeVersion": {
"policy": "fingerprint"
}
}
}
4 changes: 4 additions & 0 deletions mobile/app.local.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"bundleIdentifier": "com.example.krakatoa",
"easProjectId": "00000000-0000-0000-0000-000000000000"
}
Binary file added mobile/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mobile/assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mobile/assets/images/krakatoa-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added mobile/assets/splash-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions mobile/eas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"cli": {
"version": ">= 20.2.0",
"appVersionSource": "remote"
},
"build": {
"development": {
"developmentClient": true,
"distribution": "internal",
"channel": "development"
},
"preview": {
"distribution": "internal",
"channel": "preview"
},
"production": {
"autoIncrement": true,
"channel": "production"
}
},
"submit": {
"production": {}
}
}
Loading
Loading