"""Update Fintan's code and learning engine to the newest version, keeping everything it has learnt, and add the news updates we have published since. python scripts/update.py (run it in the model's folder; then restart the server) python scripts/update.py --check (only say whether anything would change) python scripts/update.py --no-news (code only: no new news updates) It downloads from Hugging Face only when you run it. Replaced: door/, scripts/, learn/, mcp/, relay/, the job loop code, Fintan's manual, README.md and RESULTS.md. Kept as they are: updates/ (everything it learnt), notes, the inbox, settings, your jobs/workplace.json and the job loop's state, usage and calls.log. manifest.json is kept too; a news update we published that this folder does not have yet is added to it (its files go to updates/), and the fact list of a news update you already have is refreshed (for example its web addresses). An update you took out of use stays out. """ import hashlib import json import os import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) REPOS = {"fintan-9b": "fintanlabs/Fintan-9B", "fintan-4b": "fintanlabs/Fintan-4B"} PATTERNS = ["door/*", "scripts/*", "learn/*", "mcp/*", "relay/*", "jobs/*.py", "jobs/workplace.example.json", "self/manual.json", "README.md", "RESULTS.md", "LICENSE"] NEWS_FILES = ("adapter_model.safetensors", "adapter_config.json", "facts.json", "questions.json", "check_questions.json") def digest(path): h = hashlib.sha256() with open(path, "rb") as f: h.update(f.read()) return h.hexdigest() def news_plan(local, published): """(2 Dec 2026) what the published manifest has that this folder should get: [("add", night)] for a news update it does not have (not in use and not taken out of use), [("facts", night)] for one it has (its fact list may have changed). A folder that serves whole GGUF files per update (the Ollama download) gets none: an update needs its own model file there.""" gguf = local.get("gguf") or {} if any(k != local.get("base") for k in gguf): return [] have = {n.get("model"): n for n in (local.get("nights") or [])} gone = {n.get("model") for n in (local.get("removed") or [])} out = [] for n in published.get("nights") or []: if not n.get("facts") or n.get("skill") or not n.get("path") or not n.get("model"): continue if n["model"] in have: if have[n["model"]].get("path") == n["path"]: out.append(("facts", n)) elif n["model"] not in gone: out.append(("add", n)) return out def add_nights(local, nights): """append published nights to this manifest, each with a number no update here has used""" used = [int(x.get("night") or 0) for x in (local.get("nights") or []) + (local.get("removed") or [])] for n in nights: e = {k: v for k, v in n.items() if k != "learnt_here"} e["night"] = max(used + [0]) + 1 used.append(e["night"]) local.setdefault("nights", []).append(e) return local def main(): try: from huggingface_hub import HfApi, hf_hub_download, snapshot_download except ImportError: sys.exit("pip install -U huggingface_hub, then run this again") man_p = os.path.join(ROOT, "manifest.json") man = json.load(open(man_p, encoding="utf-8")) args = [a for a in sys.argv[1:] if not a.startswith("--")] repo = args[0] if args else REPOS.get(man.get("base") or "fintan-9b", "fintanlabs/Fintan-9B") import fnmatch listed = HfApi().list_repo_files(repo) files = [f for f in listed if any(fnmatch.fnmatch(f, p) for p in PATTERNS)] before = {f: digest(os.path.join(ROOT, f)) for f in files if os.path.exists(os.path.join(ROOT, f))} plan = [] if "--no-news" not in sys.argv: pub = json.load(open(hf_hub_download(repo, "manifest.json"), encoding="utf-8")) plan = news_plan(man, pub) adds = [n for k, n in plan if k == "add"] if "--check" in sys.argv: print("%s: %d files would be checked; %d new news update(s): %s. Run without --check to update." % ( repo, len(files), len(adds), ", ".join(n["model"] for n in adds) or "none")) return snapshot_download(repo, local_dir=ROOT, allow_patterns=PATTERNS) changed = [f for f in files if before.get(f) != digest(os.path.join(ROOT, f))] if changed: print("Updated %d files from %s:" % (len(changed), repo)) for f in changed[:30]: print(" " + f) # news: files first, then the manifest, so a stopped download never leaves an entry without its files refreshed, added = [], [] for kind, n in plan: if kind == "facts": f = n["facts"] if f in listed: old = digest(os.path.join(ROOT, f)) if os.path.exists(os.path.join(ROOT, f)) else None snapshot_download(repo, local_dir=ROOT, allow_patterns=[f]) if digest(os.path.join(ROOT, f)) != old: refreshed.append(n["model"]) continue pats = [n["path"].rstrip("/") + "/" + x for x in NEWS_FILES] if not any(p in listed for p in pats[:1]): continue snapshot_download(repo, local_dir=ROOT, allow_patterns=pats) added.append(n) if added: man = json.load(open(man_p, encoding="utf-8")) add_nights(man, added) tmp = man_p + ".tmp" json.dump(man, open(tmp, "w", encoding="utf-8"), indent=1, ensure_ascii=False) os.replace(tmp, man_p) for n in added: nf = len(json.load(open(os.path.join(ROOT, n["facts"]), encoding="utf-8"))) print("Added the news update %s (%d facts)." % (n["model"], nf)) for m in refreshed: print("Refreshed the fact list of %s." % m) if changed or added or refreshed: print("What it learnt is kept. Restart the server to use the new version.") else: print("Already up to date (%s)." % repo) if __name__ == "__main__": main()