From 317316293d2be1052a591e34fd532cdbd77a368c Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:36:17 -0700 Subject: [PATCH 1/3] Add a fast Skylight sync for groceries and chores, without Claude (D212) The owner approved a small non-Claude sync that keeps chores and the grocery list within a couple of minutes of Skylight, both ways. A LaunchAgent runs engine/tools/skylight_sync.py every 2 minutes. It talks to skylight-mcp 1.7.0 over stdio, with its own token file, and reads the login from the Keychain. A change on one side is copied; done beats not-done; the newer rename wins when both sides have times, otherwise both are kept and listed; deletions never cross, and a delete's survivor isn't re-created. Chores match by the reminder's due date, and only chore-map links sync. Nothing is created, dated or rescheduled on a chore. Off until config says "skylight_sync": "tool". Also ek add-list. Tested: 42 tests for the rules and the stdio client against a stand-in MCP server, the CI checks, and ek add-list refusing an existing name. Not tested: a real Skylight account or Reminders writes, which wait on the Keychain login and the owner's go to switch it on. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 4 + docs/SETUP.md | 13 + engine/RULES.md | 3 + engine/templates/config.json | 1 + engine/tools/ek.swift | 15 + engine/tools/skylight_sync.py | 585 ++++++++++++++++++++++++++++++++++ scripts/ci-checks.sh | 1 + tests/test_skylight_sync.py | 340 ++++++++++++++++++++ 8 files changed, 962 insertions(+) create mode 100644 engine/tools/skylight_sync.py create mode 100644 tests/test_skylight_sync.py diff --git a/CHANGELOG.md b/CHANGELOG.md index fbadd29..911bb65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased +- **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. + ## 0.6.5 - **Theseus is in the Dock now,** like LeafJuice, with its menu bar icon as well. Right-click the Dock icon and choose Quit, or use Cmd-Q, Theseus > Quit, or the menu bar icon's Quit. Closing the window keeps Theseus running. - **Every Quit really quits.** Before, Quit stopped the helper that gives Remote Control a terminal but not the `claude remote-control` it had started, so that kept running, and macOS called Theseus "in use" when you tried to replace it. Some Quits also didn't leave the note that tells the watchdog to leave Theseus closed, so it came back within a minute. Now every Quit leaves that note and stops everything Theseus started, politely first and by force after three seconds. If anything holds Quit up, Theseus exits anyway after eight seconds. Opening Theseus clears the note, so the watchdog looks after it again. diff --git a/docs/SETUP.md b/docs/SETUP.md index 3c09c8a..e2f9125 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -64,6 +64,19 @@ claude mcp add skylight --scope user -e SKYLIGHT_EMAIL=you@example.com -e SKYLIG For family events to show on the frame, turn on Skylight's two-way calendar sync (Skylight app → Sync Calendars) and set `family_calendar` in config.json. +**Fast Skylight sync** (optional): keeps groceries and the chores in `data/chore-map.json` within about 2 minutes of +Skylight, both ways, without Claude. It never deletes anything and never dates or creates a chore. Rules and limits are at +the top of `engine/tools/skylight_sync.py`. +1. Put your Skylight login in the Keychain. The password is typed without showing and never lands in a file: + ```bash + read -rs "pw?Skylight password: " && security add-generic-password -U -s "Theseus Skylight" -a you@example.com -w "$pw"; unset pw + ``` +2. Check what it would do first: set `"skylight_sync": "tool"` in config.json, then run + `python3 engine/tools/skylight_sync.py run --dry-run` and read the list. +3. Start it every 2 minutes: run the command that `python3 engine/tools/skylight_sync.py install` prints. +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`. + **Things 3 migration:** nothing to install. `/migrate` reads Things read-only. Things is never changed. ## 4. First run diff --git a/engine/RULES.md b/engine/RULES.md index 4a0211d..f1d484b 100644 --- a/engine/RULES.md +++ b/engine/RULES.md @@ -199,6 +199,9 @@ A `was:
` on a done item is where it came from, for the app's reopen. Le Skylight-only. When a mapped chore is added or changed, keep both sides and the map in step. **Changing a chore's schedule:** never move a Skylight series' start date, because that erases its past occurrences. End the old series (`recurring_until`), create a new one, and update the map. + **Fast sync** (`skylight_sync: "tool"`, off by default): `engine/tools/skylight_sync.py` keeps groceries (5) and the chore-map links (6) + in step every 2 minutes, without Claude, from a LaunchAgent. While it's on, syncs don't mirror groceries or chores themselves. They run + `python3 engine/tools/skylight_sync.py status` and list its alerts under ⚠️ Needs attention, then clear the ones the user has dealt with. 7. **Title edits**: the most recently modified version wins. If unsure, keep the master version and log it. 8. **Calendar**: never edit or delete events the user didn't ask about. Create events via `calendar_source`, routed by `calendar_routes` (default `default_calendar`). Household events go on `family_calendar` (which shows on Skylight if it's synced there). diff --git a/engine/templates/config.json b/engine/templates/config.json index 071055a..783c580 100644 --- a/engine/templates/config.json +++ b/engine/templates/config.json @@ -86,6 +86,7 @@ "grocery_reminders_list": "Groceries", "skylight_todo_list": "To Do", "skylight_grocery_list": "Grocery", + "skylight_sync": "off", "skylight_todo_categories": [ "#Home" ], diff --git a/engine/tools/ek.swift b/engine/tools/ek.swift index 4537718..9a88f94 100644 --- a/engine/tools/ek.swift +++ b/engine/tools/ek.swift @@ -12,6 +12,7 @@ // ek add-event --title T --start "2026-11-05 14:00" [--end ...|--minutes 60] // [--cal Name] [--location L] [--notes N] [--allday] [--alert 15] // ek lists +// ek add-list --title T (a new Reminders list, in the same account as the default list) // ek reminders [--list "A,B"] [--completed-since 2026-10-01] [--all-lists] // ek add-reminder --title T [--list Name] [--due "2026-11-05 09:00"|--due 2026-11-05] // [--notes N] [--priority high|medium|low|none] [--url U] @@ -224,6 +225,20 @@ case "lists": ensure(.reminder) emit(store.calendars(for: .reminder).map(calDict)) +case "add-list": + ensure(.reminder) + guard let title = opt("title")?.trimmingCharacters(in: .whitespaces), !title.isEmpty else { fail("--title is required") } + if store.calendars(for: .reminder).contains(where: { $0.title.lowercased() == title.lowercased() }) { + fail("a list called '\(title)' already exists") + } + guard let source = store.defaultCalendarForNewReminders()?.source else { fail("no default Reminders account to add the list to") } + let list = EKCalendar(for: .reminder, eventStore: store) + list.title = title + list.source = source + do { try store.saveCalendar(list, commit: true) } catch { fail("couldn't add the list: \(error.localizedDescription)") } + var made = calDict(list); made["created"] = true + emit(made) + case "events": ensure(.event) let (from, _) = opt("from") != nil ? needDate("from") : (Calendar.current.startOfDay(for: Date()), false) diff --git a/engine/tools/skylight_sync.py b/engine/tools/skylight_sync.py new file mode 100644 index 0000000..ee33ca9 --- /dev/null +++ b/engine/tools/skylight_sync.py @@ -0,0 +1,585 @@ +#!/usr/bin/env python3 +""" +skylight_sync.py: keeps chores and groceries in step between Skylight and Reminders, without Claude (D212). + + python3 engine/tools/skylight_sync.py run [--dry-run] one pass (the LaunchAgent runs this every 2 minutes) + python3 engine/tools/skylight_sync.py status what the last pass did, and any alert + python3 engine/tools/skylight_sync.py install print the LaunchAgent install command + python3 engine/tools/skylight_sync.py uninstall print the command that removes it + +It does nothing unless config.json has "skylight_sync": "tool". Then the Claude syncs leave chores and groceries to it +(RULES §3.5 and §3.6). + +What it syncs, and how: +- Groceries: the Skylight grocery list and one Reminders list per store (config "grocery_reminders_list"). A store is + a section of the Skylight list (or as mapped in "grocery_store_sections"). Items are linked by id once matched; + the first match is by normalized title and store, ignoring quantities. A Skylight item with no section goes to the + first store and gets that section. +- Chores: only the links in data/chore-map.json. A Reminders occurrence and a Skylight occurrence match by the + reminder's current due date, never by "the next open one". A link with several chores completes its reminder + only once all of them are done in Skylight. + +The rules, all covered by tests: +- A change on one side since the last pass is copied to the other. +- If both sides changed in opposite ways, done beats not-done. +- If both sides renamed an item, the newer edit wins when both sides say when they changed. Otherwise both are kept + and the conflict is listed for the owner. +- Deletions never cross (RULES 0.4): an item gone from one side is unlinked and logged, and the other copy stays. +- Nothing new is dated on a kid's chore, no chore series is changed, and no chore is created. Those are listed. + +The Skylight login is read from the Keychain (service "Theseus Skylight", account = the login email) and handed to +skylight-mcp in its environment. It is never written to disk or to a log. skylight-mcp keeps its own token in +data/.skylight-tokens.json (0600), apart from the one Claude Code's connector uses. + +Every write goes to data/log.md. Each pass adds one short line to logs/skylight-sync.log. Problems that need a person +(an expired login, a conflict) are kept in data/skylight-sync.json under "alerts", and the next Claude run shows them. +""" +from __future__ import annotations + +import datetime as dt +import fcntl +import json +import os +import re +import subprocess +import sys +import time +import unicodedata +from pathlib import Path + +WS = Path(os.environ.get("THESEUS_HOME") or Path(__file__).resolve().parents[2]) +STATE = WS / "data" / "skylight-sync.json" +TOKENS = WS / "data" / ".skylight-tokens.json" +CHORE_MAP = WS / "data" / "chore-map.json" +LOG = WS / "logs" / "skylight-sync.log" +WRITE_LOG = WS / "data" / "log.md" +LOCK = WS / "data" / ".skylight-sync.lock" +KEYCHAIN_SERVICE = "Theseus Skylight" +AGENT = "com.theseus.skylight-sync" +MCP_COMMAND = ["npx", "-y", "skylight-mcp@1.7.0"] +CHORE_DAYS_BACK, CHORE_DAYS_AHEAD = 7, 7 +DONE_WORDS = {"complete", "completed", "done", "checked"} + + +# ---------------------------------------------------------------- matching + +_QTY = re.compile(r"^\s*(\d+(\.\d+)?\s*(x|×)?\s+|x\s*\d+\s+)|(\s+(x|×)\s*\d+|\s*\(\s*\d+\s*\)|\s+\d+\s*(x|×))\s*$", re.I) + + +def norm(title: str) -> str: + """A title for matching: case, spacing, curly quotes and a leading or trailing quantity don't count.""" + t = unicodedata.normalize("NFKC", title or "").replace("’", "'").replace("‘", "'").strip().lower() + prev = None + while prev != t: + prev, t = t, _QTY.sub(" ", t).strip() + return re.sub(r"\s+", " ", t) + + +def local_date(stamp) -> str | None: + """The local calendar date of an ISO time, or the date itself if it's already YYYY-MM-DD.""" + if not stamp: + return None + s = str(stamp) + if re.fullmatch(r"\d{4}-\d{2}-\d{2}", s): + return s + try: + d = dt.datetime.fromisoformat(s.replace("Z", "+00:00")) + except ValueError: + return None + return (d.astimezone() if d.tzinfo else d).date().isoformat() + + +def when(stamp) -> float | None: + if not stamp: + return None + try: + return dt.datetime.fromisoformat(str(stamp).replace("Z", "+00:00")).timestamp() + except ValueError: + return None + + +def is_done(status) -> bool: + return str(status or "").strip().lower() in DONE_WORDS + + +# ---------------------------------------------------------------- groceries + +def stores(cfg: dict) -> list[str]: + raw = cfg.get("grocery_reminders_list") or "Groceries" + return [raw] if isinstance(raw, str) else [s for s in raw if isinstance(s, str) and s] + + +def section_for(cfg: dict, store: str) -> str: + return (cfg.get("grocery_store_sections") or {}).get(store, store) + + +def store_for(cfg: dict, section) -> str | None: + """The store a Skylight section belongs to, or None for no section. A section that isn't a store is None too.""" + if not section: + return None + for st in stores(cfg): + if norm(section_for(cfg, st)) == norm(section): + return st + return None + + +def plan_groceries(cfg: dict, links: list[dict], sky: list[dict], rems: list[dict], orphans: list[str] | None = None): + """ + What to do so the two grocery lists agree. + + sky: [{"id", "label", "done", "section", "updated"}] the Skylight grocery list + rems: [{"id", "title", "done", "list", "modified"}] reminders in the store lists, open and recently completed + links: the last pass's links, [{"sky", "rem", "store", "label", "title", "sky_done", "rem_done"}] + + orphans: ids of items whose other copy was deleted. They stay single: re-creating the deleted copy would undo + the person's delete. + + Returns (actions, links, notes, orphans). Actions are tuples for apply(). New links for items still to be created + carry "pending": the action index that will create the missing side. + """ + shops = stores(cfg) + first = shops[0] + by_sky = {s["id"]: s for s in sky} + by_rem = {r["id"]: r for r in rems if r.get("list") in shops} + actions, notes, kept = [], [], [] + alive = set(by_sky) | set(by_rem) + single = {o for o in (orphans or []) if o in alive} + + for ln in links: + s, r = by_sky.get(ln["sky"]), by_rem.get(ln["rem"]) + if s is None or r is None: + gone, other = ("Skylight", "Reminders") if s is None else ("Reminders", "Skylight") + settled = ln.get("sky_done") and ln.get("rem_done") + if not settled and not (s is None and r is None): + notes.append(f"'{ln['title']}' is gone from {gone}; the {other} copy was left alone (deletions never cross)") + single.add(ln["rem"] if s is None else ln["sky"]) + continue + new = dict(ln) + # Done or not: a change on one side is copied; opposite changes on both sides go to done. + ds, dr = s["done"] != ln["sky_done"], r["done"] != ln["rem_done"] + if s["done"] != r["done"]: + if ds and not dr: + actions.append(("rem_done", r["id"], s["done"])) + r = {**r, "done": s["done"]} + elif dr and not ds: + actions.append(("sky_done", s["id"], r["done"])) + s = {**s, "done": r["done"]} + else: # both changed, or out of step from an earlier failure: done beats not-done + if not s["done"]: + actions.append(("sky_done", s["id"], True)) + s = {**s, "done": True} + else: + actions.append(("rem_done", r["id"], True)) + r = {**r, "done": True} + # Titles: one side's edit is copied; edits on both sides go to the newer, if both sides say when. + cs, cr = s["label"] != ln["label"], r["title"] != ln["title"] + if s["label"] != r["title"]: + if cs and not cr: + actions.append(("rem_title", r["id"], s["label"])) + r = {**r, "title": s["label"]} + elif cr and not cs: + actions.append(("sky_label", s["id"], r["title"])) + s = {**s, "label": r["title"]} + elif cs and cr: + ts, tr = when(s.get("updated")), when(r.get("modified")) + if ts is not None and tr is not None: + if ts >= tr: + actions.append(("rem_title", r["id"], s["label"])) + r = {**r, "title": s["label"]} + else: + actions.append(("sky_label", s["id"], r["title"])) + s = {**s, "label": r["title"]} + else: + notes.append(f"Renamed on both sides: Skylight '{s['label']}', Reminders '{r['title']}'. Both kept; pick one") + # Store: a Skylight section change moves the reminder; a reminder moved to another store sets the section. + sky_store = store_for(cfg, s.get("section")) + if r["list"] != ln["store"] and sky_store == ln["store"]: + actions.append(("sky_section", s["id"], section_for(cfg, r["list"]))) + new["store"] = r["list"] + elif sky_store and sky_store != ln["store"] and r["list"] == ln["store"]: + actions.append(("rem_list", r["id"], sky_store)) + new["store"] = sky_store + new.update(label=s["label"], title=r["title"], sky_done=s["done"], rem_done=r["done"]) + kept.append(new) + + linked_sky = {ln["sky"] for ln in kept} + linked_rem = {ln["rem"] for ln in kept} + open_rems = [r for r in by_rem.values() if r["id"] not in linked_rem and r["id"] not in single and not r["done"]] + + for s in sky: + if s["id"] in linked_sky or s["id"] in single or s["done"]: + continue + store = store_for(cfg, s.get("section")) + if store is None: + store = first + actions.append(("sky_section", s["id"], section_for(cfg, first))) + match = next((r for r in open_rems if r["list"] == store and norm(r["title"]) == norm(s["label"])), None) + if match: + open_rems.remove(match) + kept.append({"sky": s["id"], "rem": match["id"], "store": store, "label": s["label"], "title": match["title"], + "sky_done": False, "rem_done": False}) + else: + actions.append(("rem_add", store, s["label"])) + kept.append({"sky": s["id"], "rem": None, "store": store, "label": s["label"], "title": s["label"], + "sky_done": False, "rem_done": False, "pending": len(actions) - 1}) + linked_sky.add(s["id"]) + + for r in open_rems: + actions.append(("sky_add", r["title"], section_for(cfg, r["list"]))) + kept.append({"sky": None, "rem": r["id"], "store": r["list"], "label": r["title"], "title": r["title"], + "sky_done": False, "rem_done": False, "pending": len(actions) - 1}) + return actions, kept, notes, sorted(single) + + +# ---------------------------------------------------------------- chores + +def occurrences(rows: list[dict]) -> dict: + """{(chore id, YYYY-MM-DD): done} from skylight_list_chores rows.""" + out = {} + for row in rows: + day = local_date(row.get("start") or row.get("date") or row.get("instance_date")) + cid = str(row.get("chore_id") or row.get("recurring_chore_id") or row.get("id") or "") + if cid and day: + out[(cid, day)] = is_done(row.get("status")) or bool(row.get("completed")) + return out + + +def plan_chores(chore_map: dict, rems: dict, occ: dict, seen: dict): + """ + What to do for each chore-map link. + + rems: {reminder id: {"id", "title", "done", "due", "recurring"}} the linked reminders + occ: {(chore id, date): done} Skylight occurrences in the read window + seen: {reminder id: {"due": date, "done": bool}} the last pass + + Returns (actions, seen, notes). + """ + actions, notes, new_seen = [], [], {} + for ln in chore_map.get("links", []): + rid = ln.get("reminder") or ln.get("reminder_id") + chores = [str(c) for c in ln.get("chores") or ln.get("chore_ids") or []] + r = rems.get(rid) + if not rid or not chores: + continue + if r is None: + notes.append(f"Chore '{ln.get('title', rid)}' has no reminder any more; the Skylight chore was left alone") + continue + due, last = local_date(r.get("due")), seen.get(rid) or {} + # Reminders → Skylight. A repeating reminder that moved on was done for the date it had; a one-off that was + # ticked was done for its date. Only that date's Skylight occurrences are completed. + done_on = None + if r.get("recurring") and last.get("due") and due and due > last["due"]: + done_on = last["due"] + elif not r.get("recurring") and r["done"] and not last.get("done"): + done_on = due + if done_on: + for c in chores: + if occ.get((c, done_on)) is False: + actions.append(("chore_done", c, done_on)) + occ = {**occ, (c, done_on): True} + # Skylight → Reminders: every linked chore is done for the reminder's current date. + elif due and not r["done"]: + states = [occ.get((c, due)) for c in chores] + if states and all(st is True for st in states): + actions.append(("rem_done", rid, True)) + if not r.get("recurring"): + r = {**r, "done": True} + new_seen[rid] = {"due": due, "done": r["done"]} + return actions, new_seen, notes + + +# ---------------------------------------------------------------- the outside world + +class SyncError(Exception): + """A problem that stops the pass and needs a person, such as a login that has expired.""" + + +def keychain_login() -> dict: + """SKYLIGHT_EMAIL and SKYLIGHT_PASSWORD from the Keychain. The values are never printed.""" + meta = subprocess.run(["/usr/bin/security", "find-generic-password", "-s", KEYCHAIN_SERVICE], capture_output=True, text=True) + if meta.returncode != 0: + raise SyncError(f"No Skylight login in the Keychain (service \"{KEYCHAIN_SERVICE}\"). Add it with the command in docs/SETUP.md.") + m = re.search(r'"acct"="([^"]*)"', meta.stdout) + pw = subprocess.run(["/usr/bin/security", "find-generic-password", "-s", KEYCHAIN_SERVICE, "-w"], capture_output=True, text=True) + if not m or pw.returncode != 0 or not pw.stdout.strip(): + raise SyncError("The Skylight Keychain item has no account or password.") + return {"SKYLIGHT_EMAIL": m.group(1), "SKYLIGHT_PASSWORD": pw.stdout.rstrip("\n")} + + +class Skylight: + """skylight-mcp over stdio: plain JSON-RPC, no Claude. Login, refresh and retries are skylight-mcp's own code.""" + + def __init__(self, command=None, env=None): + TOKENS.parent.mkdir(parents=True, exist_ok=True) + full_env = {**os.environ, **(env or {}), "SKYLIGHT_TOKEN_FILE": str(TOKENS), "MCP_CONFIRM_MODE": "token"} + self.p = subprocess.Popen(command or MCP_COMMAND, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, text=True, env=full_env, cwd=WS) + self.n = 0 + self._rpc("initialize", {"protocolVersion": "2025-06-18", "capabilities": {}, + "clientInfo": {"name": "theseus-skylight-sync", "version": "1"}}) + self._send({"jsonrpc": "2.0", "method": "notifications/initialized"}) + + def _send(self, msg): + self.p.stdin.write(json.dumps(msg) + "\n") + self.p.stdin.flush() + + def _rpc(self, method, params): + self.n += 1 + self._send({"jsonrpc": "2.0", "id": self.n, "method": method, "params": params}) + while True: + line = self.p.stdout.readline() + if not line: + raise SyncError("skylight-mcp stopped without answering") + try: + msg = json.loads(line) + except ValueError: + continue + if msg.get("id") == self.n: + if "error" in msg: + raise SyncError(f"skylight-mcp: {msg['error'].get('message', 'error')}") + return msg.get("result") or {} + + def call(self, tool, **args): + res = self._rpc("tools/call", {"name": tool, "arguments": args}) + text = "".join(c.get("text", "") for c in res.get("content", []) if c.get("type") == "text") + if res.get("isError"): + low = text.lower() + if any(w in low for w in ("login", "challenge", "unauthor", "401", "credential", "missing:")): + raise SyncError("Skylight needs you to sign in again (run /login-skylight, or see docs/SETUP.md).") + raise SyncError(f"Skylight {tool} failed: {text[:200]}") + try: + return json.loads(text) + except ValueError: + return text + + def close(self): + try: + self.p.stdin.close() + self.p.wait(timeout=5) + except Exception: + self.p.kill() + finally: + self.p.stdout.close() + + +def ek(*args) -> object: + p = subprocess.run([str(WS / "bin/ek"), *args], capture_output=True, text=True, timeout=60) + if p.returncode != 0: + raise SyncError(f"bin/ek {args[0]} failed: {(p.stderr or p.stdout).strip()[:200]}") + return json.loads(p.stdout or "null") + + +# ---------------------------------------------------------------- state, logs, alerts + +def load(path: Path, default): + try: + return json.loads(path.read_text()) + except (OSError, ValueError): + return default + + +def save_state(state: dict): + tmp = STATE.with_suffix(".tmp") + tmp.write_text(json.dumps(state, indent=1, ensure_ascii=False)) + tmp.replace(STATE) + + +def write_log(action: str, what: str, why: str = "skylight sync"): + WRITE_LOG.parent.mkdir(parents=True, exist_ok=True) + with WRITE_LOG.open("a") as f: + f.write(f"{dt.datetime.now():%Y-%m-%d %H:%M} · {action} · {what} · {why}\n") + + +def pass_log(line: str): + LOG.parent.mkdir(parents=True, exist_ok=True) + with LOG.open("a") as f: + f.write(f"{dt.datetime.now():%Y-%m-%d %H:%M:%S} {line}\n") + try: + if LOG.stat().st_size > 2_000_000: + keep = LOG.read_bytes()[-500_000:] + LOG.write_bytes(keep[keep.find(b"\n") + 1:]) + except OSError: + pass + + +def add_alerts(state: dict, texts: list[str]): + have = {a["text"] for a in state.setdefault("alerts", [])} + for t in texts: + if t not in have: + state["alerts"].append({"at": dt.datetime.now().isoformat(timespec="minutes"), "text": t}) + have.add(t) + state["alerts"] = state["alerts"][-30:] + + +# ---------------------------------------------------------------- one pass + +def read_sky_groceries(sk: Skylight, cfg: dict): + want = norm(cfg.get("skylight_grocery_list") or "Grocery List") + lists = sk.call("skylight_list_lists") + lists = lists if isinstance(lists, list) else [] + found = next((l for l in lists if norm(l.get("label") or l.get("name") or "") in (want, norm("Grocery List"), norm("Grocery"))), None) + if not found: + raise SyncError(f"No Skylight list called \"{cfg.get('skylight_grocery_list') or 'Grocery List'}\"") + rows = sk.call("skylight_get_list_items", listId=str(found["id"])) + rows = rows if isinstance(rows, list) else [] + items = [{"id": str(r["id"]), "label": r.get("label") or "", "done": is_done(r.get("status")), + "section": r.get("section"), "updated": r.get("updated_at") or r.get("updatedAt")} for r in rows if r.get("id")] + return str(found["id"]), items + + +def read_rems(lists: list[str] | None, days: int = 30) -> list[dict]: + """Open reminders plus those completed in the last `days`. No lists means every list.""" + since = (dt.date.today() - dt.timedelta(days=days)).isoformat() + rows = ek("reminders", *(["--list", ",".join(lists)] if lists else []), "--completed-since", since) + return [{"id": r["id"], "title": r.get("title") or "", "done": bool(r.get("completed")), "list": r.get("list"), + "modified": r.get("modified"), "due": r.get("due"), "recurring": bool(r.get("recurring"))} for r in rows or []] + + +def apply(actions, sk: Skylight | None, list_id: str | None, links: list[dict], dry: bool) -> list[str]: + """Carries out the actions, logs each write, and fills in the ids of items it created.""" + done = [] + for i, a in enumerate(actions): + kind = a[0] + desc = f"{kind} {' · '.join(str(x) for x in a[1:])}" + if dry: + done.append(desc) + continue + if kind == "rem_done": + ek("complete" if a[2] else "uncomplete", "--id", a[1]) + elif kind == "rem_title": + ek("update", "--id", a[1], "--title", a[2]) + elif kind == "rem_list": + ek("update", "--id", a[1], "--list", a[2]) + elif kind == "rem_add": + made = ek("add-reminder", "--title", a[2], "--list", a[1]) + for ln in links: + if ln.get("pending") == i: + ln["rem"] = made["id"] + ln.pop("pending") + elif kind == "sky_done": + sk.call("skylight_update_list_item", listId=list_id, itemId=a[1], checked=bool(a[2])) + elif kind == "sky_label": + sk.call("skylight_update_list_item", listId=list_id, itemId=a[1], label=a[2]) + elif kind == "sky_section": + sk.call("skylight_update_list_item", listId=list_id, itemId=a[1], section=a[2]) + elif kind == "sky_add": + made = sk.call("skylight_add_list_item", listId=list_id, label=a[1]) + new_id = str(made.get("id")) if isinstance(made, dict) and made.get("id") else None + if new_id: + sk.call("skylight_update_list_item", listId=list_id, itemId=new_id, section=a[2]) + for ln in links: + if ln.get("pending") == i: + ln["sky"] = new_id + ln.pop("pending") + elif kind == "chore_done": + sk.call("skylight_complete_chore_instance", id=a[1], instance_date=a[2]) + write_log(kind.replace("_", " "), " · ".join(str(x) for x in a[1:])) + done.append(desc) + return done + + +def run(dry: bool = False) -> int: + cfg = load(WS / "config.json", {}) + if cfg.get("skylight_sync") != "tool": + print('skylight sync is off (config.json "skylight_sync" is not "tool")') + return 0 + LOCK.parent.mkdir(parents=True, exist_ok=True) + lock = LOCK.open("w") + try: + fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB) + except BlockingIOError: + return 0 # the previous pass is still running + state = load(STATE, {}) + if state.get("stopped") and not dry: + pass_log(f"stopped: {state['stopped']}") + return 1 + started = time.time() + sk = None + try: + sk = Skylight(env=keychain_login()) + list_id, sky = read_sky_groceries(sk, cfg) + shops = stores(cfg) + rems = read_rems(shops) + g_actions, g_links, g_notes, g_single = plan_groceries(cfg, [l for l in state.get("grocery_links", []) if l.get("sky") and l.get("rem")], + sky, rems, state.get("grocery_single", [])) + chore_map = load(CHORE_MAP, {"links": []}) + today = dt.date.today() + rows = sk.call("skylight_list_chores", after=(today - dt.timedelta(days=CHORE_DAYS_BACK)).isoformat(), + before=(today + dt.timedelta(days=CHORE_DAYS_AHEAD)).isoformat()) + occ = occurrences(rows if isinstance(rows, list) else []) + linked = {ln.get("reminder") or ln.get("reminder_id") for ln in chore_map.get("links", [])} + # The linked reminders can sit in any list ("Kids" today, maybe one per kid later), so every list is read. + chore_rems = {r["id"]: r for r in read_rems(None, days=CHORE_DAYS_BACK) if r["id"] in linked} + c_actions, c_seen, c_notes = plan_chores(chore_map, chore_rems, occ, state.get("chore_seen", {})) + did = apply(g_actions, sk, list_id, g_links, dry) + apply(c_actions, sk, list_id, [], dry) + if dry: + print(json.dumps({"would_do": did, "notes": g_notes + c_notes}, indent=1, ensure_ascii=False)) + return 0 + state.update(grocery_links=[l for l in g_links if l.get("sky") and l.get("rem")], grocery_single=g_single, chore_seen=c_seen, + last_pass=dt.datetime.now().isoformat(timespec="seconds"), last_changes=len(did)) + add_alerts(state, g_notes + c_notes) + save_state(state) + pass_log(f"ok in {time.time() - started:.1f}s: {len(did)} change(s)") + return 0 + except SyncError as e: + msg = str(e) + if "sign in" in msg or "Keychain" in msg: + state["stopped"] = msg # don't keep knocking on the login; a person has to act + add_alerts(state, [msg]) + if not dry: + save_state(state) + pass_log(f"error: {msg}") + print(msg, file=sys.stderr) + return 1 + finally: + if sk: + sk.close() + + +def agent_plist(python: str = "/usr/bin/python3") -> str: + return f""" + + + Label{AGENT} + ProgramArguments{python}{WS / 'engine/tools/skylight_sync.py'}run + WorkingDirectory{WS} + EnvironmentVariablesTHESEUS_HOME{WS}PATH/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin + StartInterval120 + RunAtLoad + ProcessTypeBackground + StandardOutPath{LOG} + StandardErrorPath{LOG} + +""" + + +def main(argv: list[str]) -> int: + cmd = argv[1] if len(argv) > 1 else "help" + if cmd == "run": + return run(dry="--dry-run" in argv) + if cmd == "status": + st = load(STATE, {}) + print(json.dumps({k: st.get(k) for k in ("last_pass", "last_changes", "stopped", "alerts")}, indent=1, ensure_ascii=False)) + return 0 + if cmd == "resume": + st = load(STATE, {}) + st.pop("stopped", None) + save_state(st) + print("The next pass will try again.") + return 0 + if cmd in ("install", "uninstall"): + path = Path.home() / "Library/LaunchAgents" / f"{AGENT}.plist" + dom = f"gui/{os.getuid()}" + if cmd == "install": + out = WS / "data" / f"{AGENT}.plist" + out.write_text(agent_plist()) + print(f"cp '{out}' '{path}' && launchctl bootstrap {dom} '{path}'") + else: + print(f"launchctl bootout {dom} '{path}'; rm -f '{path}'") + return 0 + print(__doc__) + return 0 if cmd in ("help", "-h", "--help") else 2 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/scripts/ci-checks.sh b/scripts/ci-checks.sh index bc27650..8a256a3 100755 --- a/scripts/ci-checks.sh +++ b/scripts/ci-checks.sh @@ -14,6 +14,7 @@ PY swiftc -typecheck engine/tools/ek.swift node --test apps/desktop/src/*.test.ts PYTHONDONTWRITEBYTECODE=1 /usr/bin/python3 tests/test_engine.py +PYTHONDONTWRITEBYTECODE=1 /usr/bin/python3 tests/test_skylight_sync.py bash scripts/check-shared.sh cmp <(sed '/^SRC=/d' install.sh) <(sed '/^SRC=/d' engine/install.sh) || { echo "install.sh and engine/install.sh differ"; exit 1; } echo "✅ checks passed" diff --git a/tests/test_skylight_sync.py b/tests/test_skylight_sync.py new file mode 100644 index 0000000..0a8b853 --- /dev/null +++ b/tests/test_skylight_sync.py @@ -0,0 +1,340 @@ +"""Tests for the Skylight sync (D212): the matching and conflict rules, chores by date, and the stdio client. +Stdlib only: python3 tests/test_skylight_sync.py. Nothing here talks to Skylight or Reminders.""" +import json +import os +import shutil +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "engine/tools")) +sys.dont_write_bytecode = True +import skylight_sync as ss # noqa: E402 + +CFG = {"grocery_reminders_list": ["Fry’s", "Sam’s Club"]} + + +def sky(i, label, done=False, section="Fry’s", updated=None): + return {"id": i, "label": label, "done": done, "section": section, "updated": updated} + + +def rem(i, title, done=False, lst="Fry’s", modified=None): + return {"id": i, "title": title, "done": done, "list": lst, "modified": modified} + + +def link(s, r, title, store="Fry’s", sky_done=False, rem_done=False, label=None): + return {"sky": s, "rem": r, "store": store, "label": label or title, "title": title, "sky_done": sky_done, "rem_done": rem_done} + + +def plan(*a, **k): + """plan_groceries without the orphans, for the tests that don't look at them.""" + actions, links, notes, _ = ss.plan_groceries(*a, **k) + return actions, links, notes + + +def kinds(actions): + return [a[0] for a in actions] + + +class Matching(unittest.TestCase): + def test_quantities_case_and_curly_quotes_dont_count(self): + for a, b in [("Milk (2)", "milk"), ("2 x Eggs", "eggs"), ("Bananas x3", "bananas"), ("Trader Joe’s salsa", "trader joe's salsa"), + (" Bread ", "bread"), ("3 limes", "limes")]: + self.assertEqual(ss.norm(a), ss.norm(b), a) + + def test_different_items_stay_different(self): + self.assertNotEqual(ss.norm("7up"), ss.norm("up")) + self.assertNotEqual(ss.norm("Milk"), ss.norm("Oat milk")) + + def test_local_date_reads_dates_and_times(self): + self.assertEqual(ss.local_date("2026-11-15"), "2026-11-15") + self.assertIsNone(ss.local_date(None)) + self.assertIsNone(ss.local_date("not a date")) + + +class GroceriesFirstPass(unittest.TestCase): + def test_same_item_on_both_sides_is_linked_without_writes(self): + actions, links, notes = plan(CFG, [], [sky("s1", "Milk (2)")], [rem("r1", "milk")]) + self.assertEqual(actions, []) + self.assertEqual((links[0]["sky"], links[0]["rem"]), ("s1", "r1")) + + def test_a_skylight_item_is_added_to_its_store_list(self): + actions, links, _ = plan(CFG, [], [sky("s1", "Rotisserie chicken", section="Sam’s Club")], []) + self.assertEqual(actions, [("rem_add", "Sam’s Club", "Rotisserie chicken")]) + self.assertEqual(links[0]["pending"], 0) + + def test_an_item_with_no_section_goes_to_the_first_store_and_gets_that_section(self): + actions, _, _ = plan(CFG, [], [sky("s1", "Apples", section=None)], []) + self.assertIn(("sky_section", "s1", "Fry’s"), actions) + self.assertIn(("rem_add", "Fry’s", "Apples"), actions) + + def test_a_reminder_is_added_to_skylight_in_its_store_section(self): + actions, _, _ = plan(CFG, [], [], [rem("r1", "Paper towels", lst="Sam’s Club")]) + self.assertEqual(actions, [("sky_add", "Paper towels", "Sam’s Club")]) + + def test_matching_respects_the_store(self): + actions, _, _ = plan(CFG, [], [sky("s1", "Milk", section="Sam’s Club")], [rem("r1", "Milk", lst="Fry’s")]) + self.assertIn(("rem_add", "Sam’s Club", "Milk"), actions) + self.assertIn(("sky_add", "Milk", "Fry’s"), actions) + + def test_done_items_are_not_copied_on_a_first_pass(self): + actions, links, _ = plan(CFG, [], [sky("s1", "Old", done=True)], [rem("r1", "Older", done=True)]) + self.assertEqual(actions, []) + self.assertEqual(links, []) + + def test_store_sections_can_be_mapped(self): + cfg = {**CFG, "grocery_store_sections": {"Fry’s": "Frys"}} + actions, links, _ = plan(cfg, [], [sky("s1", "Milk", section="Frys")], [rem("r1", "Milk")]) + self.assertEqual(actions, []) + self.assertEqual(len(links), 1) + + +class GroceriesDone(unittest.TestCase): + def test_done_in_skylight_is_copied(self): + actions, links, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Milk", done=True)], [rem("r1", "Milk")]) + self.assertEqual(actions, [("rem_done", "r1", True)]) + self.assertTrue(links[0]["rem_done"]) + + def test_done_in_reminders_is_copied(self): + actions, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Milk")], [rem("r1", "Milk", done=True)]) + self.assertEqual(actions, [("sky_done", "s1", True)]) + + def test_reopened_on_one_side_is_copied(self): + prev = link("s1", "r1", "Milk", sky_done=True, rem_done=True) + actions, _, _ = plan(CFG, [prev], [sky("s1", "Milk", done=False)], [rem("r1", "Milk", done=True)]) + self.assertEqual(actions, [("rem_done", "r1", False)]) + + def test_done_beats_not_done_when_both_sides_changed(self): + prev = link("s1", "r1", "Milk", sky_done=True, rem_done=False) + # Skylight reopened it while Reminders ticked it: done wins. + actions, links, _ = plan(CFG, [prev], [sky("s1", "Milk", done=False)], [rem("r1", "Milk", done=True)]) + self.assertEqual(actions, [("sky_done", "s1", True)]) + self.assertTrue(links[0]["sky_done"] and links[0]["rem_done"]) + + def test_out_of_step_with_no_recorded_change_goes_to_done(self): + prev = link("s1", "r1", "Milk", sky_done=True, rem_done=False) + actions, _, _ = plan(CFG, [prev], [sky("s1", "Milk", done=True)], [rem("r1", "Milk", done=False)]) + self.assertEqual(actions, [("rem_done", "r1", True)]) + + +class GroceriesTitles(unittest.TestCase): + def test_a_rename_on_one_side_is_copied(self): + a1, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Whole milk")], [rem("r1", "Milk")]) + self.assertEqual(a1, [("rem_title", "r1", "Whole milk")]) + a2, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Milk")], [rem("r1", "Oat milk")]) + self.assertEqual(a2, [("sky_label", "s1", "Oat milk")]) + + def test_renamed_on_both_sides_the_newer_wins(self): + s = sky("s1", "Whole milk", updated="2026-11-15T10:05:00-07:00") + r = rem("r1", "Oat milk", modified="2026-11-15T10:00:00-07:00") + actions, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [s], [r]) + self.assertEqual(actions, [("rem_title", "r1", "Whole milk")]) + r2 = rem("r1", "Oat milk", modified="2026-11-15T10:10:00-07:00") + actions, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [s], [r2]) + self.assertEqual(actions, [("sky_label", "s1", "Oat milk")]) + + def test_renamed_on_both_sides_without_times_keeps_both_and_says_so(self): + actions, links, notes = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Whole milk")], [rem("r1", "Oat milk", modified="2026-11-15T10:00:00Z")]) + self.assertEqual(actions, []) + self.assertEqual(len(notes), 1) + # Recorded as seen, so the same conflict isn't raised every pass. + self.assertEqual((links[0]["label"], links[0]["title"]), ("Whole milk", "Oat milk")) + + +class GroceriesDeletionsAndStores(unittest.TestCase): + def test_deleted_on_one_side_unlinks_and_never_deletes_the_other(self): + for sky_side, rem_side in [([], [rem("r1", "Milk")]), ([sky("s1", "Milk")], [])]: + actions, links, notes = plan(CFG, [link("s1", "r1", "Milk")], sky_side, rem_side) + self.assertFalse(any("delete" in k for k in kinds(actions))) + self.assertFalse(any(ln.get("sky") == "s1" and ln.get("rem") == "r1" for ln in links)) + self.assertEqual(len(notes), 1) + self.assertEqual(actions, [], "the survivor isn't re-created on the side it was deleted from") + + def test_the_survivor_of_a_delete_stays_single_on_later_passes(self): + _, links, _, single = ss.plan_groceries(CFG, [link("s1", "r1", "Milk")], [], [rem("r1", "Milk")]) + self.assertEqual(single, ["r1"]) + actions, links, notes, single = ss.plan_groceries(CFG, links, [], [rem("r1", "Milk")], single) + self.assertEqual((actions, notes, single), ([], [], ["r1"])) + # Once the survivor is gone too, it's forgotten. + self.assertEqual(ss.plan_groceries(CFG, links, [], [], single)[3], []) + + def test_a_finished_item_that_drops_off_both_sides_goes_quietly(self): + actions, links, notes = plan(CFG, [link("s1", "r1", "Milk", sky_done=True, rem_done=True)], [], []) + self.assertEqual((actions, links, notes), ([], [], [])) + + def test_moving_the_skylight_section_moves_the_reminder(self): + actions, links, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Milk", section="Sam’s Club")], [rem("r1", "Milk")]) + self.assertEqual(actions, [("rem_list", "r1", "Sam’s Club")]) + self.assertEqual(links[0]["store"], "Sam’s Club") + + def test_moving_the_reminder_sets_the_skylight_section(self): + actions, _, _ = plan(CFG, [link("s1", "r1", "Milk")], [sky("s1", "Milk")], [rem("r1", "Milk", lst="Sam’s Club")]) + self.assertEqual(actions, [("sky_section", "s1", "Sam’s Club")]) + + def test_no_plan_ever_deletes(self): + cases = [([], [sky("s1", "A")], [rem("r1", "B")]), ([link("s1", "r1", "A")], [], [])] + for links, s, r in cases: + actions, _, _ = plan(CFG, links, s, r) + self.assertTrue(all(k in {"rem_done", "rem_title", "rem_list", "rem_add", "sky_done", "sky_label", "sky_section", "sky_add"} for k in kinds(actions))) + + +def chore_rem(rid, due, done=False, recurring=True): + return {"id": rid, "title": "Feed cat", "done": done, "due": due, "recurring": recurring} + + +class Chores(unittest.TestCase): + MAP = {"links": [{"title": "Feed cat", "reminder": "R", "chores": ["C1"]}]} + MULTI = {"links": [{"title": "Dishes", "reminder": "R", "chores": ["C1", "C2"]}]} + + def test_a_repeating_reminder_done_completes_only_that_dates_occurrence(self): + occ = {("C1", "2026-11-14"): False, ("C1", "2026-11-21"): False} + actions, seen, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-21")}, occ, {"R": {"due": "2026-11-14"}}) + self.assertEqual(actions, [("chore_done", "C1", "2026-11-14")]) + self.assertEqual(seen["R"]["due"], "2026-11-21") + + def test_a_late_skylight_completion_doesnt_tick_the_next_occurrence(self): + # Last Saturday's occurrence was done late in Skylight; the reminder has already moved on to this Saturday. + occ = {("C1", "2026-11-14"): True, ("C1", "2026-11-21"): False} + actions, _, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-21")}, occ, {"R": {"due": "2026-11-21"}}) + self.assertEqual(actions, []) + + def test_done_in_skylight_for_the_reminders_date_completes_the_reminder(self): + occ = {("C1", "2026-11-21"): True} + actions, _, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-21")}, occ, {"R": {"due": "2026-11-21"}}) + self.assertEqual(actions, [("rem_done", "R", True)]) + + def test_several_chores_complete_the_reminder_only_when_all_are_done(self): + rems = {"R": chore_rem("R", "2026-11-21")} + some = {("C1", "2026-11-21"): True, ("C2", "2026-11-21"): False} + self.assertEqual(ss.plan_chores(self.MULTI, rems, some, {})[0], []) + missing = {("C1", "2026-11-21"): True} + self.assertEqual(ss.plan_chores(self.MULTI, rems, missing, {})[0], []) + both = {("C1", "2026-11-21"): True, ("C2", "2026-11-21"): True} + self.assertEqual(ss.plan_chores(self.MULTI, rems, both, {})[0], [("rem_done", "R", True)]) + + def test_a_reminder_done_marks_every_linked_chore(self): + occ = {("C1", "2026-11-14"): False, ("C2", "2026-11-14"): True} + actions, _, _ = ss.plan_chores(self.MULTI, {"R": chore_rem("R", "2026-11-21")}, occ, {"R": {"due": "2026-11-14"}}) + self.assertEqual(actions, [("chore_done", "C1", "2026-11-14")]) + + def test_a_one_off_reminder_ticked_completes_its_dates_occurrence(self): + occ = {("C1", "2026-11-20"): False} + actions, _, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-20", done=True, recurring=False)}, occ, {"R": {"due": "2026-11-20", "done": False}}) + self.assertEqual(actions, [("chore_done", "C1", "2026-11-20")]) + + def test_no_occurrence_means_nothing_is_created(self): + actions, _, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-21")}, {}, {"R": {"due": "2026-11-14"}}) + self.assertEqual(actions, []) + + def test_a_missing_reminder_is_reported_and_the_chore_left_alone(self): + actions, _, notes = ss.plan_chores(self.MAP, {}, {("C1", "2026-11-21"): True}, {}) + self.assertEqual(actions, []) + self.assertEqual(len(notes), 1) + + def test_the_first_pass_records_without_completing_anything_on_skylight(self): + occ = {("C1", "2026-11-14"): False} + actions, seen, _ = ss.plan_chores(self.MAP, {"R": chore_rem("R", "2026-11-21")}, occ, {}) + self.assertEqual(actions, []) + self.assertEqual(seen["R"]["due"], "2026-11-21") + + def test_the_documented_map_keys_work_too(self): + old_style = {"links": [{"reminder_id": "R", "chore_ids": ["C1"]}]} + actions, _, _ = ss.plan_chores(old_style, {"R": chore_rem("R", "2026-11-21")}, {("C1", "2026-11-21"): True}, {}) + self.assertEqual(actions, [("rem_done", "R", True)]) + + def test_occurrences_read_skylight_rows(self): + rows = [{"id": "C1", "start": "2026-11-21", "status": "complete"}, {"id": "C1", "start": "2026-11-22", "status": "pending"}, {"id": "", "start": "2026-11-22"}] + self.assertEqual(ss.occurrences(rows), {("C1", "2026-11-21"): True, ("C1", "2026-11-22"): False}) + + +FAKE_MCP = r''' +import json, os, sys +for line in sys.stdin: + msg = json.loads(line) + if "id" not in msg: + continue + if msg["method"] == "initialize": + res = {"protocolVersion": "2025-06-18", "capabilities": {}, "serverInfo": {"name": "fake", "version": "0"}} + elif msg["method"] == "tools/call": + name = msg["params"]["name"] + seen = {"email": os.environ.get("SKYLIGHT_EMAIL"), "token_file": os.environ.get("SKYLIGHT_TOKEN_FILE")} + if name == "expired": + res = {"isError": True, "content": [{"type": "text", "text": "Skylight login challenge: a code was emailed"}]} + elif name == "broken": + res = {"isError": True, "content": [{"type": "text", "text": "500 from Skylight"}]} + else: + res = {"content": [{"type": "text", "text": json.dumps({"tool": name, "args": msg["params"]["arguments"], "env": seen})}]} + print(json.dumps({"jsonrpc": "2.0", "id": msg["id"], "result": res}), flush=True) +''' + + +class Client(unittest.TestCase): + def setUp(self): + self.ws = Path(tempfile.mkdtemp(prefix="theseus-sky-")) + self.addCleanup(shutil.rmtree, self.ws, True) + self.fake = self.ws / "fake_mcp.py" + self.fake.write_text(FAKE_MCP) + self._old = (ss.WS, ss.TOKENS) + ss.WS, ss.TOKENS = self.ws, self.ws / "data/.skylight-tokens.json" + self.addCleanup(lambda: setattr(ss, "WS", self._old[0]) or setattr(ss, "TOKENS", self._old[1])) + + def client(self): + sk = ss.Skylight(command=[sys.executable, "-I", str(self.fake)], env={"SKYLIGHT_EMAIL": "someone@example.com", "SKYLIGHT_PASSWORD": "pw"}) + self.addCleanup(sk.close) + return sk + + def test_calls_a_tool_with_its_arguments_and_its_own_token_file(self): + out = self.client().call("skylight_get_list_items", listId="L1") + self.assertEqual(out["tool"], "skylight_get_list_items") + self.assertEqual(out["args"], {"listId": "L1"}) + self.assertEqual(out["env"]["email"], "someone@example.com") + self.assertEqual(out["env"]["token_file"], str(self.ws / "data/.skylight-tokens.json")) + + def test_an_expired_login_stops_with_a_message_for_a_person(self): + with self.assertRaises(ss.SyncError) as e: + self.client().call("expired") + self.assertIn("sign in again", str(e.exception)) + + def test_other_errors_say_which_call_failed(self): + with self.assertRaises(ss.SyncError) as e: + self.client().call("broken") + self.assertIn("broken", str(e.exception)) + + +class Housekeeping(unittest.TestCase): + def setUp(self): + self.ws = Path(tempfile.mkdtemp(prefix="theseus-sky-")) + self.addCleanup(shutil.rmtree, self.ws, True) + self.saved = {k: getattr(ss, k) for k in ("WS", "STATE", "LOG", "WRITE_LOG", "LOCK")} + ss.WS, ss.STATE, ss.LOG = self.ws, self.ws / "data/skylight-sync.json", self.ws / "logs/skylight-sync.log" + ss.WRITE_LOG, ss.LOCK = self.ws / "data/log.md", self.ws / "data/.skylight-sync.lock" + self.addCleanup(lambda: [setattr(ss, k, v) for k, v in self.saved.items()]) + + def test_it_does_nothing_unless_switched_on(self): + (self.ws / "config.json").write_text(json.dumps({"skylight": True})) + self.assertEqual(ss.run(), 0) + self.assertFalse(ss.STATE.exists()) + self.assertFalse(ss.LOG.exists()) + + def test_alerts_are_kept_once_each(self): + st = {} + ss.add_alerts(st, ["Log in again", "Log in again"]) + ss.add_alerts(st, ["Log in again"]) + self.assertEqual(len(st["alerts"]), 1) + + def test_the_agent_holds_no_credentials(self): + plist = ss.agent_plist() + self.assertIn("120", plist) + self.assertNotIn("SKYLIGHT_PASSWORD", plist) + self.assertNotIn("SKYLIGHT_EMAIL", plist) + + def test_a_dry_run_writes_nothing(self): + did = ss.apply([("rem_done", "r1", True), ("sky_add", "Milk", "Fry’s")], None, None, [], dry=True) + self.assertEqual(len(did), 2) + self.assertFalse(ss.WRITE_LOG.exists()) + + +if __name__ == "__main__": + unittest.main() From 3f33423e75ed1c64a3578ac3dfc53a6ff02df7f8 Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:38:45 -0700 Subject: [PATCH 2/3] Let a Skylight sync dry run work while the sync is off So the owner can read what it would change before switching it on, and the Claude syncs keep handling groceries until then. A dry run only reads. Co-Authored-By: Claude Opus 5.5 --- docs/SETUP.md | 6 +++--- engine/tools/skylight_sync.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/SETUP.md b/docs/SETUP.md index e2f9125..54b3f4c 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -71,9 +71,9 @@ the top of `engine/tools/skylight_sync.py`. ```bash read -rs "pw?Skylight password: " && security add-generic-password -U -s "Theseus Skylight" -a you@example.com -w "$pw"; unset pw ``` -2. Check what it would do first: set `"skylight_sync": "tool"` in config.json, then run - `python3 engine/tools/skylight_sync.py run --dry-run` and read the list. -3. Start it every 2 minutes: run the command that `python3 engine/tools/skylight_sync.py install` prints. +2. Check what it would do first. This only reads: `python3 engine/tools/skylight_sync.py run --dry-run`. +3. Switch it on with `"skylight_sync": "tool"` in config.json, then start it every 2 minutes with the command that + `python3 engine/tools/skylight_sync.py install` prints. 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`. diff --git a/engine/tools/skylight_sync.py b/engine/tools/skylight_sync.py index ee33ca9..7b8ba39 100644 --- a/engine/tools/skylight_sync.py +++ b/engine/tools/skylight_sync.py @@ -7,7 +7,7 @@ python3 engine/tools/skylight_sync.py install print the LaunchAgent install command python3 engine/tools/skylight_sync.py uninstall print the command that removes it -It does nothing unless config.json has "skylight_sync": "tool". Then the Claude syncs leave chores and groceries to it +It does nothing unless config.json has "skylight_sync": "tool", except --dry-run, which only reads and lists. Then the Claude syncs leave chores and groceries to it (RULES §3.5 and §3.6). What it syncs, and how: @@ -480,7 +480,7 @@ def apply(actions, sk: Skylight | None, list_id: str | None, links: list[dict], def run(dry: bool = False) -> int: cfg = load(WS / "config.json", {}) - if cfg.get("skylight_sync") != "tool": + if cfg.get("skylight_sync") != "tool" and not dry: # a dry run works while it's off, so it can be read before switching on print('skylight sync is off (config.json "skylight_sync" is not "tool")') return 0 LOCK.parent.mkdir(parents=True, exist_ok=True) From 2b831eecf4418f0ff9f23531027c26d089da02e0 Mon Sep 17 00:00:00 2001 From: Michael Gilardi <11632652+mgilardi@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:39:04 -0700 Subject: [PATCH 3/3] Let security prompt for the Skylight password itself Typing it at security's own prompt keeps it out of shell variables and the process list. Co-Authored-By: Claude Opus 5.5 --- docs/SETUP.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/SETUP.md b/docs/SETUP.md index 54b3f4c..ad0e5cd 100644 --- a/docs/SETUP.md +++ b/docs/SETUP.md @@ -67,9 +67,10 @@ set `family_calendar` in config.json. **Fast Skylight sync** (optional): keeps groceries and the chores in `data/chore-map.json` within about 2 minutes of Skylight, both ways, without Claude. It never deletes anything and never dates or creates a chore. Rules and limits are at the top of `engine/tools/skylight_sync.py`. -1. Put your Skylight login in the Keychain. The password is typed without showing and never lands in a file: +1. Put your Skylight login in the Keychain. `security` asks for the password twice without showing it, and it never lands in a file + or your shell history: ```bash - read -rs "pw?Skylight password: " && security add-generic-password -U -s "Theseus Skylight" -a you@example.com -w "$pw"; unset pw + security add-generic-password -U -s "Theseus Skylight" -a you@example.com -w ``` 2. Check what it would do first. This only reads: `python3 engine/tools/skylight_sync.py run --dry-run`. 3. Switch it on with `"skylight_sync": "tool"` in config.json, then start it every 2 minutes with the command that