Ariver
2026-09-01 c773fcdd1f73ca6526e11bb672b8fe33e0339a77
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Scheme 3 watcher for tagfeed.
 
This watcher is event-driven:
- it listens to vault file changes
- it ignores generated outputs that would cause self-trigger loops
- it reruns sync_excalidraw_tagfeed.py with debounce
"""
 
import os
import queue
import re
import subprocess
import sys
import threading
import time
import traceback
from datetime import datetime
from pathlib import Path
from typing import Optional
 
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
 
from tagfeed import DIARY_BACKFILL_DAYS, update_diaries
 
 
VAULT = Path(os.environ.get("EXCALIDRAW_TAGFEED_VAULT", "/Users/ar/Downloads/Syn/Ob/Ob.temp")).expanduser()
SYNC_SCRIPT = VAULT / "X2.Archived/scripts/sync_excalidraw_tagfeed.py"
LOG_PATH = Path(os.environ.get("EXCALIDRAW_TAGFEED_LOG", "/Users/ar/.excalidraw-tagfeed-watch.log"))
DEBOUNCE_SECONDS = float(os.environ.get("EXCALIDRAW_TAGFEED_DEBOUNCE", "1.5"))
OUTPUT_REL = Path("00index/00index.md")
GENERATED_DIR = Path("00index/tagfeed-md")
SKIP_DIRS = {
    ".obsidian",
    ".git",
    ".trash",
    ".hg",
    ".svn",
    ".agents",
    ".claude",
    ".copilot",
    ".smart-env",
    ".workbuddy",
    "X2.Archived",
    "X.Attachment",
}
 
 
def log(message: str) -> None:
    stamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    line = f"[{stamp}] {message}"
    print(line, flush=True)
    try:
        with LOG_PATH.open("a", encoding="utf-8") as fh:
            fh.write(line + "\n")
    except OSError:
        pass
 
 
def is_relevant(path: Path) -> bool:
    try:
        rel = path.relative_to(VAULT)
    except ValueError:
        return False
    if rel == OUTPUT_REL:
        return False
    if rel == GENERATED_DIR or GENERATED_DIR in rel.parents:
        return False
    parts = rel.parts
    if parts and parts[0] in SKIP_DIRS:
        return False
    if path.name.startswith("."):
        return False
    if path.suffix.lower() not in {".md", ".markdown", ".excalidraw", ".canvas"}:
        return False
    return True
 
 
def run_sync() -> str:
    result = subprocess.run(
        [sys.executable, str(SYNC_SCRIPT), str(VAULT)],
        capture_output=True,
        text=True,
        timeout=180,
    )
    output = (result.stdout or "").strip()
    if result.returncode != 0:
        err = (result.stderr or output).strip()
        raise RuntimeError(err[-1000:] or f"sync exited {result.returncode}")
    if output:
        log(output)
    match = re.search(r"Excalidraw-Tagfeed:\s+(\w+)\s+", output)
    return match.group(1) if match else "unknown"
 
 
def run_daily_activity_sync(known_written) -> int:
    diary_dir = VAULT / "X0.Diary"
    if not diary_dir.is_dir():
        return 0
    return update_diaries(str(VAULT), DIARY_BACKFILL_DAYS, log=log, known_written=known_written)
 
 
class DebouncedSync:
    def __init__(self, debounce_seconds: float) -> None:
        self.debounce_seconds = debounce_seconds
        self._lock = threading.Lock()
        self._timer: Optional[threading.Timer] = None
        self._running = False
        self._pending = False
        self.known_written = {}
 
    def trigger(self, reason: str) -> None:
        log(f"event: {reason}")
        with self._lock:
            if self._running:
                self._pending = True
                return
            if self._timer is not None:
                self._timer.cancel()
            self._timer = threading.Timer(self.debounce_seconds, self._fire)
            self._timer.daemon = True
            self._timer.start()
 
    def _fire(self) -> None:
        with self._lock:
            self._timer = None
            self._running = True
        try:
            status = run_sync()
            if status == "missingframe":
                log("Frame0-all unavailable; waiting for next event")
        except Exception:
            log("sync failed:\n" + traceback.format_exc().rstrip())
        try:
            changed = run_daily_activity_sync(self.known_written)
            if changed:
                log(f"Daily activity: updated {changed} diaries")
        except Exception:
            log("daily activity sync failed:\n" + traceback.format_exc().rstrip())
        finally:
            with self._lock:
                self._running = False
                pending = self._pending
                self._pending = False
        if pending:
            self.trigger("coalesced")
 
 
class VaultEventHandler(FileSystemEventHandler):
    def __init__(self, scheduler: DebouncedSync) -> None:
        self.scheduler = scheduler
 
    def on_any_event(self, event) -> None:
        if getattr(event, "is_directory", False):
            return
        src = Path(getattr(event, "src_path", "") or "")
        dest = Path(getattr(event, "dest_path", "") or "")
        if is_relevant(src) or is_relevant(dest):
            self.scheduler.trigger(f"{event.event_type}: {src or dest}")
 
 
def main() -> None:
    if not VAULT.is_dir():
        raise SystemExit(f"vault not found: {VAULT}")
    if not SYNC_SCRIPT.is_file():
        raise SystemExit(f"sync script not found: {SYNC_SCRIPT}")
 
    log(f"scheme3 watch start vault={VAULT} mode=event debounce={DEBOUNCE_SECONDS:g}s")
    scheduler = DebouncedSync(DEBOUNCE_SECONDS)
    handler = VaultEventHandler(scheduler)
    observer = Observer()
    observer.schedule(handler, str(VAULT), recursive=True)
    observer.start()
    scheduler.trigger("startup")
 
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        log("watch stopped by user")
    finally:
        observer.stop()
        observer.join(timeout=5)
 
 
if __name__ == "__main__":
    main()