-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathwatcher.py
More file actions
251 lines (211 loc) · 9.23 KB
/
Copy pathwatcher.py
File metadata and controls
251 lines (211 loc) · 9.23 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
"""Brain Repo — File watcher with debounce for auto-sync."""
import logging
import threading
from collections.abc import Callable
from pathlib import Path
from watchdog.events import FileSystemEventHandler
from watchdog.observers import Observer
log = logging.getLogger(__name__)
DEBOUNCE_SECONDS_DEFAULT = 30
WATCH_PATHS = [
"memory",
"workspace",
"customizations",
"config-safe",
]
EXCLUDE_PATHS = [
"memory/raw-transcripts",
]
class _ChangeHandler(FileSystemEventHandler):
"""Watchdog event handler that debounces filesystem events."""
def __init__(
self,
install_dir: Path,
sync_fn: Callable[[], None],
debounce_seconds: int,
) -> None:
super().__init__()
self._install_dir = install_dir
self._sync_fn = sync_fn
self._debounce = debounce_seconds
self._timer: threading.Timer | None = None
self._lock = threading.Lock()
def on_any_event(self, event) -> None:
self._on_change(event)
def _on_change(self, event) -> None:
# Ignore directory events (we only care about file changes)
if event.is_directory:
return
# Check exclude paths
src = str(getattr(event, "src_path", ""))
for excl in EXCLUDE_PATHS:
excl_abs = str(self._install_dir / excl)
if src.startswith(excl_abs):
log.debug("watcher: ignoring excluded path %s", src)
return
with self._lock:
if self._timer is not None:
self._timer.cancel()
self._timer = threading.Timer(self._debounce, self._flush)
self._timer.daemon = True
self._timer.start()
def _flush(self) -> None:
log.info("watcher: debounce elapsed, triggering sync")
try:
self._sync_fn()
except Exception as exc:
log.error("watcher: sync_fn raised exception: %s", exc)
def cancel(self) -> None:
with self._lock:
if self._timer is not None:
self._timer.cancel()
self._timer = None
class BrainRepoWatcher:
"""Watches brain repo directories and triggers sync_fn after debounce."""
def __init__(
self,
install_dir: Path,
brain_repo_dir: Path,
sync_fn: Callable[[], None],
debounce_seconds: int = DEBOUNCE_SECONDS_DEFAULT,
) -> None:
self._install_dir = install_dir
self._brain_repo_dir = brain_repo_dir
self._sync_fn = sync_fn
self._debounce = debounce_seconds
self._observer: Observer | None = None
self._handler: _ChangeHandler | None = None
def start(self) -> None:
"""Start watching. Only observes paths that exist."""
self._handler = _ChangeHandler(
self._install_dir,
self._sync_fn,
self._debounce,
)
self._observer = Observer()
watched_any = False
for rel_path in WATCH_PATHS:
# Watch the SOURCE (install_dir / working tree) — that's where the user
# actually edits files. Pre-fix this pointed at brain_repo_dir, which is
# the SYNC TARGET and only changes when copytree runs (no useful signal),
# so auto-sync never fired for user edits.
watch_target = self._install_dir / rel_path
if watch_target.exists():
self._observer.schedule(
self._handler,
str(watch_target),
recursive=True,
)
log.debug("watcher: watching %s", watch_target)
watched_any = True
else:
log.debug("watcher: skipping non-existent path %s", watch_target)
if watched_any:
self._observer.start()
log.info("BrainRepoWatcher started (debounce=%ds)", self._debounce)
else:
log.warning("BrainRepoWatcher: no valid watch paths found, not started")
def stop(self) -> None:
"""Stop the observer gracefully."""
if self._handler is not None:
self._handler.cancel()
if self._observer is not None and self._observer.is_alive():
self._observer.stop()
self._observer.join(timeout=5)
log.info("BrainRepoWatcher stopped")
def start_brain_watcher(install_dir: Path, flask_app=None) -> "BrainRepoWatcher | None":
"""Create and start a BrainRepoWatcher if sync is enabled.
Reads BrainRepoConfig from the database (requires Flask app context).
Returns None if no enabled config is found.
Args:
install_dir: workspace root — what the watcher observes.
flask_app: Flask app instance for DB queries. MUST be passed from
app.py after ``db.init_app(app)``. The old code used
``from app import app`` which hit a circular import with a
half-initialised SQLAlchemy, producing the startup warning
"current Flask app is not registered with this 'SQLAlchemy'
instance" — and the watcher silently gave up. Passing ``app``
explicitly avoids the circular dependency.
"""
try:
if flask_app is None:
# Legacy fallback for callers that haven't been updated yet.
# Emits a clear warning so the issue is visible in logs.
try:
from app import app as flask_app # type: ignore[import]
except Exception as exc:
log.error(
"start_brain_watcher: flask_app not provided and fallback import failed: %s",
exc,
)
return None
from models import BrainRepoConfig # type: ignore[import]
from brain_repo.github_oauth import get_master_key # type: ignore[import]
with flask_app.app_context():
config = BrainRepoConfig.query.filter_by(sync_enabled=True).first()
if config is None:
log.info("start_brain_watcher: no enabled brain repo config found")
return None
if not config.local_path:
log.warning("start_brain_watcher: config has no local_path")
return None
brain_repo_dir = Path(config.local_path)
if not config.github_token_encrypted:
log.warning("start_brain_watcher: no encrypted token in config")
return None
# Crypto-readiness guard. Without a master key every enqueue_sync
# call below would fail on token decryption and the watcher would
# log the same error every 30 s forever. Better to refuse to start
# and surface the configuration problem at boot.
if not get_master_key():
log.critical(
"start_brain_watcher: BRAIN_REPO_MASTER_KEY missing — refusing "
"to start watcher (stored tokens cannot be decrypted)",
)
return None
# Capture user_id for the closure; the job runner re-loads the
# config inside each job to pick up fresh state (e.g. a disconnect
# that happened after start but before the first debounce fired).
_user_id = config.user_id
def _sync_fn() -> None:
"""Enqueue a sync through the job runner.
Earlier versions of this callback ran ``git commit`` + ``git
push`` inline, which caused two problems:
1. it raced with manual sync_force / tag_milestone (no lock),
so tags created by the manual path sometimes never got
pushed because the watcher commit overwrote the working
tree in between.
2. it bypassed the mirror-exclusion rules (``workspace/projects``
etc.), re-copying multi-GB git repos on every file change.
Now the watcher funnels everything through ``job_runner.enqueue_sync``
which acquires the global lock, applies exclusions, and handles
cancel requests uniformly. If a job is already running,
``enqueue_sync`` returns False and we just skip this tick —
the next debounce (or the watcher on the next change) retries.
"""
try:
from brain_repo import job_runner
except ImportError as exc:
log.warning("watcher _sync_fn: job_runner unavailable: %s", exc)
return
enqueued = job_runner.enqueue_sync(
flask_app,
_user_id,
install_dir,
kind=job_runner.JOB_KIND_WATCHER,
commit_message="auto: file watcher sync",
)
if not enqueued:
log.debug(
"watcher _sync_fn: another job is running, skipping this tick",
)
watcher = BrainRepoWatcher(
install_dir=install_dir,
brain_repo_dir=brain_repo_dir,
sync_fn=_sync_fn,
)
watcher.start()
return watcher
except Exception as exc:
log.error("start_brain_watcher failed: %s", exc)
return None