From 1688e09938e19baec5b3e1ec66e5b50a4725c646 Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:48:41 -0700 Subject: [PATCH] Start leave-by times from the home address on the owner's Contacts card The owner chose home as the starting point, read from his own Contacts card. eta_from now takes contacts:home (or contacts:work), resolved each run through a new read-only ek me-address, so the address is never saved and stays right after a move. A card with no home address gives a note and no trips. Tested against a real card and Apple Maps, printing only a yes and the minutes; three new tests. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + docs/SETUP.md | 4 +++- engine/RULES.md | 2 +- engine/tools/ek.swift | 28 +++++++++++++++++++++++++++- engine/tools/eta.py | 27 +++++++++++++++++++++++---- tests/test_eta.py | 28 ++++++++++++++++++++++++++-- 6 files changed, 81 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cdf28a8..c6fbc1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # Changelog ## Unreleased +- **Leave-by times start from home on your Contacts card.** Set `"eta_from": "contacts:home"` and Theseus reads the home address on your own card (My Card) each time, through the new `ek me-address`. The address isn't saved anywhere, so it stays right if you move. If the card has no home address, the briefing says so and shows no leave-by times. Tested: read from a real card and routed through Apple Maps; three new tests cover the card, a missing address and nothing being saved. - **Leave-by times from Apple Maps** (optional). Set `eta_from` to where your trips start, and the briefing shows when to leave for each event with a place, using Apple Maps' travel time for arriving at its start. Traffic is predicted for that time: the same 23 km test route between two public places took 34 minutes arriving at a weekday rush hour and 19 minutes early on a Sunday. It reads every calendar the Calendar app has, skips online, declined and all-day events, and makes at most 12 lookups a day, refreshing one in the two hours before an event. The cache holds no places or titles. `ek eta` does the lookup and `ek events` now gives each event's map location when it has one. Tested: 12 tests with a stand-in for ek, and real lookups by address, by place name, by coordinates and by transit. Not tested: a real day's events from your calendar. - **A fast Skylight sync for groceries and chores, without Claude** (optional, off until you turn it on). Every 2 minutes it brings the grocery list and the chores linked in `data/chore-map.json` into step with Reminders, both ways: a change on one side is copied, done beats not-done, the newer rename wins when both sides say when, and deletions never cross. It never creates, dates or reschedules a chore. The Skylight login lives in the Keychain. Setup is in docs/SETUP.md §3. Tested: 42 tests for the matching and conflict rules, chores by date and the connection to skylight-mcp (against a stand-in). Not tested: a real Skylight account, which waits on the Keychain login. - **`ek add-list --title T`** adds a Reminders list. Tested: it refuses a name that already exists. Not tested: adding one, so as not to change a real Reminders account. diff --git a/docs/SETUP.md b/docs/SETUP.md index 3d41b29..b350258 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -78,7 +78,9 @@ the top of `engine/tools/skylight_sync.py`. 4. `python3 engine/tools/skylight_sync.py status` shows the last pass and any alert. If Skylight asks you to sign in again, the sync stops until you've done that and run `python3 engine/tools/skylight_sync.py resume`. -**Leave-by times** (optional): set `"eta_from"` in config.json to where your trips start, as an address or `LAT,LON`. +**Leave-by times** (optional): set `"eta_from"` in config.json to where your trips start: `"contacts:home"` reads the home +address on your own card in Contacts (My Card) each time and never saves it, or give an address or `LAT,LON`. If your card +has no home address, the briefing says so and shows none. The first run asks for Contacts access. The evening briefing then shows "Leave by" for tomorrow's events that have a place, from Apple Maps travel times with traffic predicted for when you need to arrive. No key or account. Your starting point and event places go only to Apple. At most 12 lookups a day (`eta_daily_lookups`), plus 10 minutes for parking (`eta_buffer_minutes`). Try it with diff --git a/engine/RULES.md b/engine/RULES.md index aa7aebc..77a3e4e 100644 --- a/engine/RULES.md +++ b/engine/RULES.md @@ -325,7 +325,7 @@ by scripts, so the note uses typed ☐ characters. Tell the user once: "change 📅 every event in time order (time · title · where), first one bolded; flag early starts, back-to-backs, travel time, free blocks ≥ 60 min. For travel time, run `python3 engine/tools/eta.py day --date ` and show its "Leave by" line under each event. Use only the times it gives (from Apple Maps, with traffic predicted for the arrival time); never estimate one. If it says it needs - `eta_from` or hit its limit, say so once. + `eta_from`, found no home address on the owner's Contacts card, or hit its limit, say so once, in its words. 🔥 Top 3 · ⏰ Due · 🧹 Chores · ⏰ Alarms (§7c, flag mismatches) 💬 Still waiting: texts, email, Slack (count + top 3) 🎒 Prep tonight (only if needed) · ❓ Needs you diff --git a/engine/tools/ek.swift b/engine/tools/ek.swift index d45bee3..2307063 100644 --- a/engine/tools/ek.swift +++ b/engine/tools/ek.swift @@ -25,7 +25,11 @@ // [--arrive "2026-11-05 14:00"] [--transport car|walk|transit] // Travel time from Apple Maps (no key, no account). With --arrive, Apple predicts traffic for that time. // Addresses go only to Apple. Nothing is stored. +// ek me-address [--label home|work] +// The address on your own Contacts card (the "me" card), for leave-by times. Read only; nothing is stored. +// Prints {"found": false, "reason": ...} when there's no me card or no address with that label. import Foundation +import Contacts import EventKit import MapKit import CoreLocation @@ -392,11 +396,33 @@ case "eta": "meters": Int(eta.distance.rounded()), "depart": iso(eta.expectedDepartureDate), "arrive": iso(eta.expectedArrivalDate), "transport": opt("transport") ?? "car"]) +case "me-address": + let wanted = (opt("label") ?? "home").lowercased() + let contacts = CNContactStore() + var granted: Bool? + contacts.requestAccess(for: .contacts) { ok, _ in granted = ok } + let deadline = Date().addingTimeInterval(30) + while granted == nil, Date() < deadline { RunLoop.current.run(mode: .default, before: Date().addingTimeInterval(0.05)) } + guard granted == true else { emit(["found": false, "reason": "Contacts access isn't allowed"]); exit(0) } + let keys = [CNContactPostalAddressesKey as CNKeyDescriptor] + guard let me = try? contacts.unifiedMeContactWithKeys(toFetch: keys) else { + emit(["found": false, "reason": "no card is set as My Card in Contacts"]); exit(0) + } + let label: String = wanted == "work" ? CNLabelWork : CNLabelHome + guard let hit = me.postalAddresses.first(where: { $0.label == label }) else { + emit(["found": false, "reason": "your Contacts card has no \(wanted) address"]); exit(0) + } + let a = hit.value + let line = [a.street, a.city, [a.state, a.postalCode].filter { !$0.isEmpty }.joined(separator: " "), a.country] + .map { $0.replacingOccurrences(of: "\n", with: ", ").trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }.joined(separator: ", ") + guard !line.isEmpty else { emit(["found": false, "reason": "the \(wanted) address on your Contacts card is empty"]); exit(0) } + emit(["found": true, "address": line, "label": wanted]) + default: print(""" ek — Calendar & Reminders bridge. Commands: access | calendars | events | add-event | lists | reminders | add-reminder | - complete | uncomplete | update | delete | add-list | eta + complete | uncomplete | update | delete | add-list | eta | me-address See the header of tools/ek.swift for options. """) } diff --git a/engine/tools/eta.py b/engine/tools/eta.py index 2f39165..e0f3aeb 100644 --- a/engine/tools/eta.py +++ b/engine/tools/eta.py @@ -10,7 +10,9 @@ 34 minutes arriving at a weekday rush hour and 19 minutes early on a Sunday). config.json: - "eta_from": where trips start, an address or "LAT,LON". Required; without it nothing is looked up. + "eta_from": where trips start: "contacts:home" (the home address on your own Contacts card, read fresh each + time and never saved), "contacts:work", an address, or "LAT,LON". Required; without it nothing is + looked up. If the card has no such address, that's said and nothing is guessed. "eta_transport": "car" (default), "walk" or "transit". "eta_buffer_minutes": added to the travel time for parking and walking in (default 10). "eta_daily_lookups": the most Apple Maps lookups a day (default 12). @@ -89,11 +91,28 @@ def lookup_args(e: dict, origin: str, transport: str) -> list[str]: return args +def resolve_origin(setting: str, ask=ek) -> tuple[str, str | None]: + """The starting address, and why there isn't one. "contacts:home" reads the owner's own card each time.""" + m = re.fullmatch(r"contacts:(home|work)", setting.strip().lower()) + if not m: + return setting.strip(), None + try: + got = ask("me-address", "--label", m.group(1)) or {} + except RuntimeError as err: + return "", f"Couldn't read your Contacts card for leave-by times: {err}" + if got.get("found") and (got.get("address") or "").strip(): + return got["address"].strip(), None + return "", f"No leave-by times: {got.get('reason') or 'no ' + m.group(1) + ' address on your Contacts card'}. Add it to your card in Contacts." + + def plan(day: dt.date, cfg: dict, now: dt.datetime | None = None, ask=ek) -> dict: now = now or dt.datetime.now().astimezone() - origin = (cfg.get("eta_from") or "").strip() - if not origin: - return {"trips": [], "note": 'Set "eta_from" in config.json (an address or LAT,LON) for leave-by times.'} + setting = (cfg.get("eta_from") or "").strip() + if not setting: + return {"trips": [], "note": 'Set "eta_from" in config.json ("contacts:home", an address or LAT,LON) for leave-by times.'} + origin, missing = resolve_origin(setting, ask) + if missing: + return {"trips": [], "note": missing} transport = cfg.get("eta_transport") or "car" buffer = int(cfg.get("eta_buffer_minutes", 10)) limit = int(cfg.get("eta_daily_lookups", 12)) diff --git a/tests/test_eta.py b/tests/test_eta.py index 2a07609..23e3aad 100644 --- a/tests/test_eta.py +++ b/tests/test_eta.py @@ -23,12 +23,15 @@ def ev(title, hour, location="", minute=0, **extra): class Fake: """Stands in for bin/ek: returns the events, and a travel time for each lookup.""" - def __init__(self, events, minutes=25, fail=False): - self.events, self.minutes, self.fail, self.lookups = events, minutes, fail, [] + def __init__(self, events, minutes=25, fail=False, me=None): + self.events, self.minutes, self.fail, self.lookups, self.me, self.asked_me = events, minutes, fail, [], me, [] def __call__(self, *args): if args[0] == "events": return self.events + if args[0] == "me-address": + self.asked_me.append(args) + return self.me self.lookups.append(args) if self.fail: raise RuntimeError("Apple Maps couldn't find it") @@ -122,6 +125,27 @@ def test_leave_now_when_its_already_past_the_leave_by_time(self): self.assertTrue(r["trips"][0]["late"]) self.assertIn("Leave now", eta.text(r)) + def test_home_from_the_contacts_card(self): + fake = Fake([ev("Dentist", 10, "Made Up Dental")], me={"found": True, "address": "1 Made Up St, Somewhere", "label": "home"}) + r = self.run_plan(fake, cfg={"eta_from": "contacts:home"}) + self.assertEqual(fake.asked_me[0], ("me-address", "--label", "home")) + args = fake.lookups[0] + self.assertEqual(args[args.index("--from") + 1], "1 Made Up St, Somewhere") + self.assertEqual(len(r["trips"]), 1) + + def test_no_home_address_on_the_card_is_said_not_guessed(self): + fake = Fake([ev("Dentist", 10, "Made Up Dental")], me={"found": False, "reason": "your Contacts card has no home address"}) + r = self.run_plan(fake, cfg={"eta_from": "contacts:home"}) + self.assertEqual(fake.lookups, []) + self.assertEqual(r["trips"], []) + self.assertIn("no home address", r["note"]) + self.assertIn("Add it to your card", r["note"]) + + def test_the_home_address_is_never_saved(self): + fake = Fake([ev("Dentist", 10, "Made Up Dental")], me={"found": True, "address": "1 Made Up St, Somewhere", "label": "home"}) + self.run_plan(fake, cfg={"eta_from": "contacts:home"}) + self.assertNotIn("Made Up St", eta.CACHE.read_text()) + def test_the_cache_holds_no_places_or_titles(self): self.run_plan(Fake([ev("Dentist", 10, "Made Up Dental")])) saved = eta.CACHE.read_text()