# The sun — real sunrise and sunset, on the device Status: built on `feature/sun`. ROADMAP Phase 1, *the sun*. Review: [`feature-sun.md`](../reviews/feature-sun.md). The first piece of Phase 1, and the one the spike leaves behind even if the answer is no (ROADMAP §0: *a reusable sentence library and a sun module*). --- ## 1. Why and what, in the player's terms Every day, at the player's **real** local sunset, the game invites them to put the phone down and look at the real one (CLAUDE.md §1). The rose's sentence is best opened then. The in-game sunset says when tonight's real one is. All of that needs one honest answer: *when does the sun set here, today?* What the player gets from this piece alone: nothing on screen yet. What it guarantees: - **The time is real.** It is computed from the sun's position and checked against a published almanac for six cities on four continents, both hemispheres, the equator and the Arctic. - **It never needs the network.** It is maths on the device. No lookup, no service. - **It never needs GPS.** The place is a city the player chose, or a guess from the device's time zone (CLAUDE.md §5.6 rule 6). Choosing that city is the app's job, later in Phase 1. - **The far north is not forgotten.** Where the sun does not set in summer or rise in winter, the engine still gives a gentle moment for the invitation (D4), never an error and never silence. - **A clock change is the clock's doing.** The sun moves a minute or two a day. When the clocks change, the sunset on the clock jumps an hour while the sun itself does nothing. The engine works in instants, so the jump only appears where the clock is read. ## 2. Technical architecture ```mermaid flowchart LR app["apps/rosea (later in Phase 1)
the chosen city or the device zone"] -->|"place, day"| sun subgraph engine["packages/engine (pure)"] sun["sun/sun.ts
sunDay(day, place)
invitation(sunDay)"] time["time/localDay.ts
the only place a day is computed"] sun --> time end golden["engine/test/fixtures/sun-usno.json
28 published days"] -.->|golden tests| sun script["scripts/fetch-sun-golden.ts
run by hand, never by a test"] -.->|records| golden ``` - **`sunDay(day, place)`** takes a `LocalDay` and `{ lat, lon, timeZone }` and returns one of three shapes: `sun-sets` (sunrise, solar noon, sunset), `polar-day` (solar noon, lowest light) or `polar-night` (solar noon). Every time is an epoch-milliseconds instant. - **`invitation(sunDay)`** is the instant the game invites the player outside: the sunset, or the polar alternative (D4). The app renders it and decides nothing. - **The algorithm** is NOAA's solar calculator (after Meeus, *Astronomical Algorithms*): the sun's declination and the equation of time at a given instant, then the hour angle at which its centre is 0.833° below the horizon (refraction and the sun's radius). Each event is refined by recomputing at its own instant. ### Decisions | # | Decision | Why | |:--|:--|:--| | D1 | NOAA's algorithm, in TypeScript, in the engine. No library, no WASM. | About 80 lines of public-domain maths, accurate to about a minute below 72° latitude. CLAUDE.md §2: WASM only with a measurement that says it must. | | D2 | **"Today's sunset" is the one after today's solar noon**, even when it falls after midnight. "Today" is the local day whose solar noon it is. | Reykjavík on 21 June sets at 00:04 on the 22nd. The published table for the 21st lists the *previous* evening's 00:04. A player in Reykjavík means tonight's sunset, not last night's. | | D3 | The engine returns instants. Clock times are read in the app, with the city's zone. | One instant has one meaning. The DST jump is then only the clock's (§1), and the engine cannot be wrong about it. | | D4 | **Polar day**: the invitation is the *lowest light*, solar midnight. **Polar night**: the invitation is solar noon, when the twilight is brightest. | CLAUDE.md §8 asks for a gentle alternative, *e.g. the hour the light is lowest*. In polar night that hour is all day, so the brightest one is the moment worth going out for. **Decided by the author 2026-11-01** (Q10). | | D5 | Near the edge of polar day or night, an event whose hour angle leaves the possible range is clamped to solar midnight or noon. | On those few days the sun only grazes the horizon. A defined answer beats a NaN. Accuracy there is worse, and the tests say so. | | D6 | Golden values come from the U.S. Naval Observatory's published tables, recorded once into a fixture by `scripts/fetch-sun-golden.ts`. | CLAUDE.md §5.5 asks for published tables. Recording them keeps tests offline and deterministic, and the script shows exactly where each number came from. | | D7 | Tolerance: **2 minutes**, stated in the test. | The almanac rounds to the minute; NOAA's method is good to about a minute at these latitudes. | | D8 | Where a clock is about twelve hours off the sun, one calendar day a year can hold no solar noon. The engine then gives the nearest noon and never throws. | Found by fast-check at 179.9° W on UTC: noon crosses midnight in late December, so 26 December has none. No real zone is that far off the sun (the worst are about three hours), so the property tests give each place a zone that follows its longitude, and a unit test pins the impossible case. | ## 3. Risk analysis Risk here means a person feeling worse, a person's location, or a person becoming reachable. | Risk | Where | Answer | |:--|:--|:--| | **A wrong sunset sends someone out to a dark sky**, or too late. A broken promise from the one feature that points beyond the screen. | Correctness | Golden tests in six cities, including DST days; property tests across latitudes and days. | | **The far north gets nothing** — no sunset, so no invitation, every day for months. A person left out by geography. | Soul | D4: a defined, gentle moment on every day, tested at Tromsø and at ±80°. | | **The time is used to find someone.** A sunset time narrows down where a person lives. | Privacy | It is computed and kept on the device. This module has no I/O at all, and the engine's lint rule forbids it. Phase 4's shared sky uses a coarse cell and a day bucket only (CLAUDE.md §4), never this. | | **The test fixture holds coordinates.** | Privacy | City centres from public sources, never a player. No test uses a real person's location (§5.5). | | **A DST day records the wrong local day** for a sunset, and the rose's sentence moves to the wrong day. | Correctness | The day is only ever computed by `localDay`. Tested on all six DST transition days in the fixture. | Reachability: none. The module talks to no one. ## 4. Gap analysis and the designed solution | Gap | Solution | State | |:--|:--|:--| | No `engine/src/sun/` (CLAUDE.md §2, §7: *one module computes the sun*) | `sun/sun.ts`: `sunDay`, `invitation` | Built | | Polar day and night: a predicted trap (CLAUDE.md §8) | Two defined kinds and D4. **Measured**: Tromsø has no rise or set on 21 June and no events on 21 December | Built; the trap is confirmed | | DST and the local day: a predicted trap | D3 and `localDay`. **Measured**: the Prague sunset is 18:28 on 28 March and 19:29 on the 29th; the sun moved one minute | Built; the trap is confirmed | | Golden tests against published tables (§5.5) | 28 days in 6 cities, recorded from USNO | Built | | Choosing a city, the time-zone guess, showing the time | The app, with a city list; later in Phase 1 | **Open** (G1.2) | | The invitation in polar night (D4) | Noon twilight | Decided by the author (Q10) | ## 5. Verification matrix | Case | Kind | Where | |:--|:--|:--| | Sunrise, solar noon and sunset match the USNO table within 2 min, in the city's own clock, on 26 days in 6 cities | unit, golden | `engine/test/sun.test.ts` | | The fixture's offset agrees with the IANA zone on every day, so the table and the zone database tell the same story | unit, golden | same | | Tromsø 21 June is polar day; 21 December is polar night | unit, golden | same | | Reykjavík midsummer: tonight's sunset falls after midnight, on the next local day (D2) | unit, golden | same | | A DST change moves the clock's sunset by an hour while the instant moves by about a day | unit | same | | For any latitude within ±60°, any longitude and any day of 2026: sunrise < noon < sunset, noon is midway, noon is on the asked day | property | same | | Sunsets on consecutive days are never more than 5 minutes apart within ±60° | property | same | | ±80° on the solstices: polar day and night, the right way round in each hemisphere | unit | same | | The invitation is the sunset, the lowest light, or noon, by kind | unit, property | same | | A day that does not exist, a latitude above 90°, an unknown zone: refused | unit | same | | Nothing leaves the device | lint | the engine purity rule already covers `src/sun/` | | Choosing a city, and seeing the time in the app | e2e, offline | **later in Phase 1** |