v0.23.0: Routine, first public version

This commit is contained in:
2026-11-15 02:22:09 -08:00
commit 5ea848bddc
285 changed files with 23886 additions and 0 deletions
+181
View File
@@ -0,0 +1,181 @@
#!/usr/bin/env python3
"""routine-server: the relay that keeps the desktop and R1 routine apps in step, on a machine of
your own that both can reach (a home server, a VPN address).
Standard library only, and model.py beside it (scripts/deploy-server.sh copies both). Every
request but /health needs "Authorization: Bearer <token>" (made on first start in <data>/token,
mode 600). It speaks plain HTTP: bind it to an address only your devices reach (a private network
or VPN), not the open internet.
GET /health -> {"ok": true}
POST /sync {"since": <seq>, "days": [<day>, ...], "schedule_rev": n, "schedule": {...}?,
"streaks": {...}?, "streaks_t": t?}
-> {"seq": <seq>, "days": [<every stored day changed after since>], "schedule": {...}?,
"streaks": {...}?, "streaks_t": t?}
The routine itself rides along: a client may send its schedule (the desktop does after an
edit); the server keeps the highest rev and hands it to any client whose schedule_rev is lower.
The streak base rides the same way (model.streak_base plus "t" and the desktop's streak
settings): the desktop sends it, the server keeps the one with the highest "t" and answers
every sync with that t as "streaks_t" (the desktop's receipt); a client that sends "streaks_t"
(the R1) gets the base back whenever the server's is newer. Clients that send neither (older
apps) never see it, and older servers ignore both: the R1 then counts from its own days.
A pushed day is merged into the stored copy with model.merge (last writer wins per item and
for the note), and gets a new seq only if the merge changed it. A client keeps the returned
seq and sends it as since next time; since > the server's seq (a fresh server) means "all".
The server is a relay, not the record: days older than KEEP_DAYS are dropped; the desktop keeps
everything.
python3 routine_server.py --host ADDRESS --port 8070 --data DIR
"""
import argparse
import datetime as dt
import json
import os
import secrets
import sqlite3
import sys
import threading
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import model # noqa: E402 (deployed beside this file)
KEEP_DAYS = 60
MAX_BODY = 4 * 1024 * 1024
LOCK = threading.Lock()
class Store:
def __init__(self, path):
self.db = sqlite3.connect(path, check_same_thread=False)
self.db.execute("CREATE TABLE IF NOT EXISTS days (date TEXT PRIMARY KEY, doc TEXT NOT NULL, seq INTEGER NOT NULL)")
self.db.execute("CREATE TABLE IF NOT EXISTS meta (k TEXT PRIMARY KEY, v TEXT NOT NULL)")
self.db.commit()
def seq(self):
return self.db.execute("SELECT COALESCE(MAX(seq), 0) FROM days").fetchone()[0]
def sync(self, since, pushed, schedule=None, schedule_rev=0, streaks=None, streaks_t=None):
with LOCK:
row = self.db.execute("SELECT v FROM meta WHERE k = 'streaks'").fetchone()
base = json.loads(row[0]) if row else None
if valid_streaks(streaks) and (base is None or streaks["t"] > base.get("t", 0)):
base = streaks
self.db.execute("INSERT OR REPLACE INTO meta (k, v) VALUES ('streaks', ?)", (json.dumps(base),))
row = self.db.execute("SELECT v FROM meta WHERE k = 'schedule'").fetchone()
stored = json.loads(row[0]) if row else None
if isinstance(schedule, dict) and isinstance(schedule.get("blocks"), list) and \
(stored is None or schedule.get("rev", 0) > stored.get("rev", 0)):
stored = schedule
self.db.execute("INSERT OR REPLACE INTO meta (k, v) VALUES ('schedule', ?)", (json.dumps(stored),))
cutoff = (dt.date.today() - dt.timedelta(days=KEEP_DAYS)).isoformat()
self.db.execute("DELETE FROM days WHERE date < ?", (cutoff,))
seq = self.seq()
for day in pushed:
date = day.get("date", "")
try:
dt.date.fromisoformat(date)
except (TypeError, ValueError):
continue
if date < cutoff:
continue
row = self.db.execute("SELECT doc FROM days WHERE date = ?", (date,)).fetchone()
old = json.loads(row[0]) if row else None
merged = model.merge(old, day)
if merged != old:
seq += 1
self.db.execute("INSERT OR REPLACE INTO days (date, doc, seq) VALUES (?, ?, ?)",
(date, json.dumps(merged, separators=(",", ":")), seq))
self.db.commit()
if since > seq:
since = 0
rows = self.db.execute("SELECT doc FROM days WHERE seq > ? ORDER BY date", (since,)).fetchall()
out = {"seq": seq, "days": [json.loads(r[0]) for r in rows]}
if stored is not None and stored.get("rev", 0) > schedule_rev:
out["schedule"] = stored
if base is not None:
out["streaks_t"] = base.get("t", 0)
if streaks_t is not None and base.get("t", 0) > streaks_t:
out["streaks"] = base
return out
def valid_streaks(s):
"""A streak base worth keeping: {"t": int, "asof": "YYYY-MM-DD", "items": {...}, ...}."""
if not isinstance(s, dict) or not isinstance(s.get("items"), dict) or type(s.get("t")) is not int:
return False
try:
dt.date.fromisoformat(s.get("asof"))
except (TypeError, ValueError):
return False
return True
class Handler(BaseHTTPRequestHandler):
server_version = "routine-server"
def _send(self, code, obj):
body = json.dumps(obj, separators=(",", ":")).encode()
self.send_response(code)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _authed(self):
got = self.headers.get("Authorization", "")
return secrets.compare_digest(got.encode(), ("Bearer " + self.server.token).encode())
def do_GET(self):
if self.path == "/health":
return self._send(200, {"ok": True})
self._send(404, {"error": "not found"})
def do_POST(self):
if self.path != "/sync":
return self._send(404, {"error": "not found"})
if not self._authed():
return self._send(401, {"error": "unauthorized"})
n = int(self.headers.get("Content-Length") or 0)
if n > MAX_BODY:
return self._send(413, {"error": "too large"})
try:
req = json.loads(self.rfile.read(n) or b"{}")
since = int(req.get("since", 0))
days = req.get("days", [])
if not isinstance(days, list):
raise ValueError("days")
sched, srev = req.get("schedule"), int(req.get("schedule_rev", 0))
st = int(req["streaks_t"]) if req.get("streaks_t") is not None else None
except (ValueError, TypeError) as e:
return self._send(400, {"error": "bad request: %s" % e})
self._send(200, self.server.store.sync(since, days, sched, srev, req.get("streaks"), st))
def log_message(self, fmt, *args):
sys.stderr.write("%s %s\n" % (self.log_date_time_string(), fmt % args))
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--host", default="127.0.0.1")
ap.add_argument("--port", type=int, default=8070)
ap.add_argument("--data", required=True)
a = ap.parse_args()
os.makedirs(a.data, exist_ok=True)
tok_path = os.path.join(a.data, "token")
if not os.path.exists(tok_path):
fd = os.open(tok_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
with os.fdopen(fd, "w") as f:
f.write(secrets.token_urlsafe(32))
srv = ThreadingHTTPServer((a.host, a.port), Handler)
srv.token = open(tok_path).read().strip()
srv.store = Store(os.path.join(a.data, "routine.db"))
print("routine-server on %s:%d" % (a.host, a.port), flush=True)
srv.serve_forever()
if __name__ == "__main__":
main()
Executable
+10
View File
@@ -0,0 +1,10 @@
#!/bin/bash
# The one command line for routine-server (scripts/deploy-server.sh runs it; so can a boot script).
# Settings come from server.env beside this file (written by deploy-server.sh) or the environment:
# ROUTINE_SERVER_HOST address to listen on (default 127.0.0.1)
# ROUTINE_SERVER_PORT port (default 8070)
# ROUTINE_SERVER_DATA data folder: routine.db + token (default ../data next to this folder)
cd "$(dirname "$0")" || exit 1
[ -f server.env ] && . ./server.env
exec python3 routine_server.py --host "${ROUTINE_SERVER_HOST:-127.0.0.1}" --port "${ROUTINE_SERVER_PORT:-8070}" \
--data "${ROUTINE_SERVER_DATA:-$(cd .. && pwd)/data}"