File size: 6,108 Bytes
d009f44
 
d78e437
 
 
d009f44
d78e437
 
d009f44
 
 
 
d78e437
 
 
 
 
 
 
 
 
 
d009f44
d78e437
 
 
 
 
 
 
 
 
d009f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d78e437
 
d009f44
d78e437
 
d009f44
 
d78e437
 
 
d009f44
 
d78e437
d009f44
 
 
 
 
d78e437
d009f44
 
d78e437
 
 
 
 
 
 
d009f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d78e437
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
"""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()