aisync-cli 0.2.7__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ai_sync/__init__.py +2 -0
- ai_sync/__main__.py +5 -0
- ai_sync/board.py +285 -0
- ai_sync/cli.py +932 -0
- ai_sync/collab.py +476 -0
- ai_sync/core.py +974 -0
- ai_sync/demo.py +285 -0
- ai_sync/enforcement.py +328 -0
- ai_sync/gate.py +265 -0
- ai_sync/guard.py +390 -0
- ai_sync/hooks.py +577 -0
- ai_sync/instincts.py +113 -0
- ai_sync/landq.py +182 -0
- ai_sync/layers.py +113 -0
- ai_sync/mcp_server.py +134 -0
- ai_sync/nonvacuous.py +110 -0
- ai_sync/rulecheck.py +153 -0
- ai_sync/ruleids.py +162 -0
- ai_sync/rulespec.py +202 -0
- ai_sync/tidy.py +145 -0
- ai_sync/ui.py +162 -0
- ai_sync/update.py +73 -0
- ai_sync/watch.py +191 -0
- ai_sync/webinbox.py +173 -0
- ai_sync/worktree.py +168 -0
- aisync_cli-0.2.7.dist-info/METADATA +308 -0
- aisync_cli-0.2.7.dist-info/RECORD +31 -0
- aisync_cli-0.2.7.dist-info/WHEEL +5 -0
- aisync_cli-0.2.7.dist-info/entry_points.txt +2 -0
- aisync_cli-0.2.7.dist-info/licenses/LICENSE +21 -0
- aisync_cli-0.2.7.dist-info/top_level.txt +1 -0
ai_sync/core.py
ADDED
|
@@ -0,0 +1,974 @@
|
|
|
1
|
+
"""aisync core.
|
|
2
|
+
|
|
3
|
+
What: the hub every AI coding tool (Claude Code, Codex, Cursor, ...) reads and writes.
|
|
4
|
+
Why: one source of truth for shared rules + one append-only, hash-chained journal, so every
|
|
5
|
+
agent can see what is going on, who did it, when, and which files changed.
|
|
6
|
+
How: a sync *cycle* = harvest (git commits -> journal) -> distribute (shared rule block ->
|
|
7
|
+
each tool's file, 3-way) -> render (current.json + agents.md progress) -> flush (git
|
|
8
|
+
commit, optional push). The watcher (watch.py) runs a cycle on file events, debounced.
|
|
9
|
+
|
|
10
|
+
Every step is isolated (one failure never blocks the others), every cycle is logged to
|
|
11
|
+
state/runs.jsonl, and repeated failures pause pushing / cycles and raise an alert.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import contextlib
|
|
16
|
+
import fcntl
|
|
17
|
+
import hashlib
|
|
18
|
+
import json
|
|
19
|
+
import os
|
|
20
|
+
import re
|
|
21
|
+
import subprocess
|
|
22
|
+
import sys
|
|
23
|
+
import tempfile
|
|
24
|
+
import time
|
|
25
|
+
import tomllib
|
|
26
|
+
from dataclasses import dataclass, field
|
|
27
|
+
from datetime import datetime
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
BLOCK_START = "<!-- owner-preferences:start"
|
|
31
|
+
BLOCK_END = "<!-- owner-preferences:end -->"
|
|
32
|
+
PROG_START = "<!-- aisync:progress:start -->"
|
|
33
|
+
PROG_END = "<!-- aisync:progress:end -->"
|
|
34
|
+
KINDS = ("dev_log", "decision", "blocked", "unblocked", "rule_change", "commit", "note", "claim", "release")
|
|
35
|
+
# Only these files go into git; logs/backups/locks stay local (see .gitignore).
|
|
36
|
+
TRACKED = ["journal.jsonl", "preferences.md", "rules-archive.md", "rules-enforcement.toml", "agents.md", "config.toml", "state/current.json",
|
|
37
|
+
".gitignore"]
|
|
38
|
+
RUNS_MAX_BYTES = 5 * 1024 * 1024
|
|
39
|
+
BACKUPS_PER_TARGET = 20
|
|
40
|
+
SEEN_COMMITS_MAX = 5000
|
|
41
|
+
DEFAULT_ROSTER = ("claude-code", "codex", "cursor")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class GitError(RuntimeError):
|
|
45
|
+
pass
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def now_iso() -> str:
|
|
49
|
+
return datetime.now().astimezone().isoformat(timespec="seconds")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def home() -> Path:
|
|
53
|
+
return Path(os.environ.get("AISYNC_HOME") or "~/.ai-sync").expanduser()
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _h(text: str) -> str:
|
|
57
|
+
return hashlib.sha256(text.encode()).hexdigest()[:12]
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
# ───────────────────────────── config ─────────────────────────────
|
|
61
|
+
|
|
62
|
+
@dataclass
|
|
63
|
+
class Target:
|
|
64
|
+
label: str
|
|
65
|
+
path: Path
|
|
66
|
+
slim: bool = False # the copy omits the "### 已取代" history section (every session pays for it)
|
|
67
|
+
layer: str = "" # "core": only core_topics in full, other topics folded and injected on demand (layers.py)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass
|
|
71
|
+
class ProgressTarget:
|
|
72
|
+
label: str
|
|
73
|
+
path: Path
|
|
74
|
+
mode: str = "block" # "block" = <!-- aisync:progress --> section inside the file; "file" = whole file
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
@dataclass
|
|
78
|
+
class Config:
|
|
79
|
+
root: Path
|
|
80
|
+
debounce_secs: float = 30.0 # quiet time after the last event before a cycle runs
|
|
81
|
+
max_wait_secs: float = 120.0 # a busy stream still syncs at least this often
|
|
82
|
+
min_flush_interval_secs: float = 30.0
|
|
83
|
+
reconcile_secs: float = 600.0 # safety net in case a file event was missed
|
|
84
|
+
git_timeout_secs: float = 20.0
|
|
85
|
+
git_retries: int = 3
|
|
86
|
+
backoff_base_secs: float = 1.0
|
|
87
|
+
fail_threshold: int = 5
|
|
88
|
+
remote: str = "" # empty = local-only git
|
|
89
|
+
remote_branch: str = "main"
|
|
90
|
+
targets: list[Target] = field(default_factory=list)
|
|
91
|
+
projects: list[Path] = field(default_factory=list)
|
|
92
|
+
scan_dirs: list[Path] = field(default_factory=list) # every git repo/worktree directly inside
|
|
93
|
+
rule_sources: list[str] = field(default_factory=list) # repos whose AGENTS.md "Meta rule" feeds the shared rules
|
|
94
|
+
progress_targets: list[ProgressTarget] = field(default_factory=list) # files tools auto-load
|
|
95
|
+
roster: list[str] = field(default_factory=lambda: list(DEFAULT_ROSTER)) # tools `report` expects to hear from
|
|
96
|
+
web_inbox_secs: float = 0.0 # poll the web-inbox branch this often; 0 = off (webinbox.py)
|
|
97
|
+
web_inbox_remote: str = "origin"
|
|
98
|
+
web_inbox_branch: str = "web-inbox"
|
|
99
|
+
gate_hook: bool = False # install the push-time test gate in the git pre-push hook (gate.py); off by default
|
|
100
|
+
guard_hook: bool = False # wire the one-session-per-repo guard (guard.py) into Claude Code; off by default
|
|
101
|
+
instincts_path: str = "" # optional habit list the hooks inject (one line each); empty = off
|
|
102
|
+
errors_path: str = "" # optional error log the habits cite; empty = off
|
|
103
|
+
rule_spec: bool = False # rule add refuses a rule not in the When/Do/Not shape (rulespec.py)
|
|
104
|
+
require_enforcer: bool = False # rule add refuses a rule that names no --enforced-by / --judgment-only
|
|
105
|
+
core_topics: list[str] = field(default_factory=list) # topic prefixes a layered target carries in full
|
|
106
|
+
shelf: dict = field(default_factory=dict) # topic prefix -> keywords that pull it in (layers.py)
|
|
107
|
+
tidy_usage_globs: list[str] = field(default_factory=lambda: list(TIDY_GLOBS)) # files whose R-### mentions count as use
|
|
108
|
+
core_budget_chars: int = 26000 # tidy flags the core layer above this many characters of rule text
|
|
109
|
+
tidy_daily: bool = False # the watch cycle starts one tidy a day; off unless the hub config says so
|
|
110
|
+
progress_file: str = "" # write live progress here (hub-relative) instead of into agents.md, which then
|
|
111
|
+
# never changes and stays cacheable; hooks inject it at session start
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def state(self) -> Path:
|
|
115
|
+
return self.root / "state"
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def project_list(cfg: "Config") -> list[Path]:
|
|
119
|
+
"""Explicit projects + repos found in scan_dirs, re-read every cycle (worktrees come and go)."""
|
|
120
|
+
found = list(cfg.projects)
|
|
121
|
+
for d in cfg.scan_dirs:
|
|
122
|
+
if d.is_dir():
|
|
123
|
+
found += sorted(p for p in d.iterdir() if p.is_dir() and (p / ".git").exists())
|
|
124
|
+
seen, out = set(), []
|
|
125
|
+
for p in found:
|
|
126
|
+
if p not in seen:
|
|
127
|
+
seen.add(p)
|
|
128
|
+
out.append(p)
|
|
129
|
+
return out
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
TIDY_GLOBS: tuple[str, ...] = () # no transcript scan unless config sets tidy_usage_globs (e.g. "~/.claude/projects/*/*.jsonl")
|
|
133
|
+
|
|
134
|
+
_NUM_KEYS = ("debounce_secs", "max_wait_secs", "min_flush_interval_secs", "reconcile_secs",
|
|
135
|
+
"git_timeout_secs", "git_retries", "backoff_base_secs", "fail_threshold", "web_inbox_secs",
|
|
136
|
+
"core_budget_chars")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def load_config(root: Path | None = None) -> Config:
|
|
140
|
+
root = Path(root) if root else home()
|
|
141
|
+
cfg = Config(root=root)
|
|
142
|
+
path = root / "config.toml"
|
|
143
|
+
data = tomllib.loads(path.read_text()) if path.exists() else {}
|
|
144
|
+
for key in _NUM_KEYS + ("remote", "remote_branch", "web_inbox_remote", "web_inbox_branch",
|
|
145
|
+
"instincts_path", "errors_path", "progress_file"):
|
|
146
|
+
if key in data:
|
|
147
|
+
setattr(cfg, key, data[key])
|
|
148
|
+
cfg.targets = [Target(t["label"], Path(t["path"]).expanduser(), bool(t.get("slim", False)), str(t.get("layer", "")))
|
|
149
|
+
for t in data.get("target", [])]
|
|
150
|
+
cfg.core_topics = list(data.get("core_topics", []))
|
|
151
|
+
cfg.shelf = {k: list(v) for k, v in data.get("shelf", {}).items()}
|
|
152
|
+
cfg.tidy_daily = bool(data.get("tidy_daily", False))
|
|
153
|
+
cfg.gate_hook = bool(data.get("gate_hook", False))
|
|
154
|
+
cfg.guard_hook = bool(data.get("guard_hook", False))
|
|
155
|
+
if "tidy_usage_globs" in data:
|
|
156
|
+
cfg.tidy_usage_globs = list(data["tidy_usage_globs"])
|
|
157
|
+
cfg.projects = [Path(p["path"]).expanduser() for p in data.get("project", [])]
|
|
158
|
+
cfg.scan_dirs = [Path(p).expanduser() for p in data.get("scan_dirs", [])]
|
|
159
|
+
cfg.rule_sources = list(data.get("rule_sources", []))
|
|
160
|
+
if "roster" in data:
|
|
161
|
+
cfg.roster = [str(r) for r in data["roster"]]
|
|
162
|
+
cfg.rule_spec = bool(data.get("rule_spec", False))
|
|
163
|
+
cfg.require_enforcer = bool(data.get("require_enforcer", False))
|
|
164
|
+
cfg.progress_targets = [ProgressTarget(t["label"], Path(t["path"]).expanduser(), t.get("mode", "block"))
|
|
165
|
+
for t in data.get("progress_target", [])]
|
|
166
|
+
return cfg
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
# ───────────────────────────── small io helpers ─────────────────────────────
|
|
170
|
+
|
|
171
|
+
def load_json(path: Path, default):
|
|
172
|
+
try:
|
|
173
|
+
return json.loads(path.read_text())
|
|
174
|
+
except (FileNotFoundError, json.JSONDecodeError):
|
|
175
|
+
return default
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def save_json(path: Path, data) -> None:
|
|
179
|
+
atomic_write(path, json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True) + "\n")
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def atomic_write(path: Path, text: str) -> None:
|
|
183
|
+
"""Write via temp file + rename on the *real* path, so a symlinked target stays a symlink."""
|
|
184
|
+
real = Path(os.path.realpath(path))
|
|
185
|
+
real.parent.mkdir(parents=True, exist_ok=True)
|
|
186
|
+
mode = real.stat().st_mode & 0o777 if real.exists() else 0o644
|
|
187
|
+
fd, tmp = tempfile.mkstemp(dir=real.parent, prefix=f".{real.name}.", suffix=".tmp")
|
|
188
|
+
try:
|
|
189
|
+
with os.fdopen(fd, "w") as fh:
|
|
190
|
+
fh.write(text)
|
|
191
|
+
fh.flush()
|
|
192
|
+
os.fsync(fh.fileno())
|
|
193
|
+
os.chmod(tmp, mode)
|
|
194
|
+
os.replace(tmp, real)
|
|
195
|
+
except BaseException:
|
|
196
|
+
with contextlib.suppress(FileNotFoundError):
|
|
197
|
+
os.unlink(tmp)
|
|
198
|
+
raise
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
@contextlib.contextmanager
|
|
202
|
+
def locked(cfg: Config):
|
|
203
|
+
"""Cross-process lock: every tool may call `aisync log` at the same moment."""
|
|
204
|
+
cfg.state.mkdir(parents=True, exist_ok=True)
|
|
205
|
+
with open(cfg.state / ".lock", "a") as fh:
|
|
206
|
+
fcntl.flock(fh, fcntl.LOCK_EX)
|
|
207
|
+
try:
|
|
208
|
+
yield
|
|
209
|
+
finally:
|
|
210
|
+
fcntl.flock(fh, fcntl.LOCK_UN)
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
# ───────────────────────────── journal (hash chain) ─────────────────────────────
|
|
214
|
+
|
|
215
|
+
def _event_id(prev: str, body: dict) -> str:
|
|
216
|
+
body = {k: v for k, v in body.items() if k != "id"}
|
|
217
|
+
canon = json.dumps(body, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
|
|
218
|
+
return hashlib.sha256((prev + "\n" + canon).encode()).hexdigest()[:16]
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def read_events(cfg: Config) -> list[dict]:
|
|
222
|
+
path = cfg.root / "journal.jsonl"
|
|
223
|
+
if not path.exists():
|
|
224
|
+
return []
|
|
225
|
+
out = []
|
|
226
|
+
for line in path.read_text().splitlines():
|
|
227
|
+
if line.strip():
|
|
228
|
+
with contextlib.suppress(json.JSONDecodeError):
|
|
229
|
+
out.append(json.loads(line))
|
|
230
|
+
return out
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def append_event(cfg: Config, *, agent: str, kind: str, summary: str, project: str = "",
|
|
234
|
+
files=(), refs: dict | None = None, ts: str | None = None) -> dict:
|
|
235
|
+
agent, summary = (agent or "").strip(), (summary or "").strip()
|
|
236
|
+
if not agent:
|
|
237
|
+
raise ValueError("agent is required (pass --agent or set AISYNC_AGENT)")
|
|
238
|
+
if not summary:
|
|
239
|
+
raise ValueError("summary is required")
|
|
240
|
+
if kind not in KINDS:
|
|
241
|
+
raise ValueError(f"kind must be one of {', '.join(KINDS)}")
|
|
242
|
+
with locked(cfg):
|
|
243
|
+
events = read_events(cfg)
|
|
244
|
+
prev = events[-1].get("id", "genesis") if events else "genesis"
|
|
245
|
+
body = {"ts": ts or now_iso(), "agent": agent, "project": project, "kind": kind,
|
|
246
|
+
"summary": summary[:2000], "files": [str(f) for f in files][:200],
|
|
247
|
+
"refs": refs or {}, "prev": prev}
|
|
248
|
+
body["id"] = _event_id(prev, body)
|
|
249
|
+
with open(cfg.root / "journal.jsonl", "a") as fh:
|
|
250
|
+
fh.write(json.dumps(body, ensure_ascii=False) + "\n")
|
|
251
|
+
fh.flush()
|
|
252
|
+
os.fsync(fh.fileno())
|
|
253
|
+
return body
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def verify_chain(cfg: Config) -> list[str]:
|
|
257
|
+
"""Empty list = intact. Reports hand edits, lost events and forks."""
|
|
258
|
+
path = cfg.root / "journal.jsonl"
|
|
259
|
+
if not path.exists():
|
|
260
|
+
return []
|
|
261
|
+
problems, prev = [], "genesis"
|
|
262
|
+
for i, line in enumerate(path.read_text().splitlines(), 1):
|
|
263
|
+
if not line.strip():
|
|
264
|
+
continue
|
|
265
|
+
try:
|
|
266
|
+
ev = json.loads(line)
|
|
267
|
+
except json.JSONDecodeError:
|
|
268
|
+
problems.append(f"line {i}: not valid JSON")
|
|
269
|
+
continue
|
|
270
|
+
if ev.get("prev") != prev:
|
|
271
|
+
problems.append(f"line {i}: prev link broken (event lost, reordered or forked)")
|
|
272
|
+
if _event_id(ev.get("prev", ""), ev) != ev.get("id"):
|
|
273
|
+
problems.append(f"line {i}: id mismatch (event edited by hand)")
|
|
274
|
+
prev = ev.get("id", "")
|
|
275
|
+
return problems
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
# ───────────────────────────── shared-rule block distribution ─────────────────────────────
|
|
279
|
+
|
|
280
|
+
def extract_block(text: str) -> str | None:
|
|
281
|
+
s = text.find(BLOCK_START)
|
|
282
|
+
if s < 0:
|
|
283
|
+
return None
|
|
284
|
+
e = text.find(BLOCK_END, s)
|
|
285
|
+
return None if e < 0 else text[s:e + len(BLOCK_END)]
|
|
286
|
+
|
|
287
|
+
|
|
288
|
+
def replace_block(text: str, new_block: str) -> str:
|
|
289
|
+
s = text.find(BLOCK_START)
|
|
290
|
+
e = text.find(BLOCK_END, s)
|
|
291
|
+
return text[:s] + new_block + text[e + len(BLOCK_END):]
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
SUPERSEDED_HEAD = "### 已取代"
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _superseded_span(lines: list[str]) -> tuple[int, int] | None:
|
|
298
|
+
"""[start, end) of the 已取代 section inside a block: from its heading to the next heading or the block end."""
|
|
299
|
+
start = next((i for i, l in enumerate(lines) if l.strip() == SUPERSEDED_HEAD), None)
|
|
300
|
+
if start is None:
|
|
301
|
+
return None
|
|
302
|
+
for j in range(start + 1, len(lines)):
|
|
303
|
+
if lines[j].startswith(("### ", "## ")) or lines[j].startswith(BLOCK_END):
|
|
304
|
+
return start, j
|
|
305
|
+
return start, len(lines)
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def strip_superseded(block: str) -> str:
|
|
309
|
+
"""The rules block without its 已取代 section. That section is history, not rules; the truth keeps it, the
|
|
310
|
+
copies every session loads do not (it was 30 KB of every session's context)."""
|
|
311
|
+
lines = block.split("\n")
|
|
312
|
+
span = _superseded_span(lines)
|
|
313
|
+
if span is None:
|
|
314
|
+
return block
|
|
315
|
+
s, e = span
|
|
316
|
+
head = lines[:s]
|
|
317
|
+
while head and head[-1].strip() == "":
|
|
318
|
+
head.pop()
|
|
319
|
+
tail = lines[e:]
|
|
320
|
+
if tail and tail[0].startswith(("### ", "## ")):
|
|
321
|
+
head.append("")
|
|
322
|
+
return "\n".join(head + tail)
|
|
323
|
+
|
|
324
|
+
|
|
325
|
+
def merge_back(slim_block: str, truth_block: str) -> str:
|
|
326
|
+
"""A slim copy edited by hand, with the truth's 已取代 section put back (so adopting the edit never deletes history)."""
|
|
327
|
+
t_lines = truth_block.split("\n")
|
|
328
|
+
span = _superseded_span(t_lines)
|
|
329
|
+
if span is None:
|
|
330
|
+
return slim_block
|
|
331
|
+
s, e = span
|
|
332
|
+
if _superseded_span(slim_block.split("\n")) is not None:
|
|
333
|
+
return slim_block
|
|
334
|
+
while s > 0 and t_lines[s - 1].strip() == "":
|
|
335
|
+
s -= 1
|
|
336
|
+
section = t_lines[s:e]
|
|
337
|
+
lines = slim_block.split("\n")
|
|
338
|
+
at = next((i for i, l in enumerate(lines) if l.startswith(BLOCK_END)), len(lines))
|
|
339
|
+
return "\n".join(lines[:at] + section + lines[at:])
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
_RULE_ID = re.compile(r"^- \[R-\d{3,}\] ", re.M)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _drop_ids(block: str) -> str:
|
|
346
|
+
"""The block without rule IDs ([R-001]); a copy that differs from the truth only there was not edited."""
|
|
347
|
+
return _RULE_ID.sub("- ", block)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
def _want(t: "Target", truth: str, cfg: "Config | None" = None) -> str:
|
|
351
|
+
want = strip_superseded(truth) if t.slim else truth
|
|
352
|
+
if t.layer == "core" and cfg is not None and cfg.core_topics:
|
|
353
|
+
from . import layers
|
|
354
|
+
want = layers.core_view(want, cfg.core_topics, cfg.shelf)
|
|
355
|
+
return want
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _backup(cfg: Config, label: str, text: str) -> None:
|
|
359
|
+
bdir = cfg.root / "backups"
|
|
360
|
+
bdir.mkdir(exist_ok=True)
|
|
361
|
+
stamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
|
|
362
|
+
(bdir / f"{label}-{stamp}.bak").write_text(text)
|
|
363
|
+
old = sorted(bdir.glob(f"{label}-*.bak"))
|
|
364
|
+
for p in old[:-BACKUPS_PER_TARGET]:
|
|
365
|
+
p.unlink()
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def distribute(cfg: Config) -> dict:
|
|
369
|
+
"""3-way sync of the owner-preferences block between preferences.md (truth) and targets.
|
|
370
|
+
|
|
371
|
+
For each target we remember the hash we last wrote. Then:
|
|
372
|
+
target == truth -> in sync
|
|
373
|
+
target == last written -> target untouched, truth moved: overwrite target
|
|
374
|
+
target edited, truth unchanged, and
|
|
375
|
+
every edited copy agrees -> fast-forward: adopt the edit into truth
|
|
376
|
+
anything else -> conflict: leave files alone, alert the owner
|
|
377
|
+
"""
|
|
378
|
+
rep = {"in_sync": [], "updated": [], "adopted": [], "conflicts": [], "missing": [], "errors": []}
|
|
379
|
+
truth_path = cfg.root / "preferences.md"
|
|
380
|
+
st = load_json(cfg.state / "distribute.json", {})
|
|
381
|
+
conflicts = load_json(cfg.state / "conflicts.json", {})
|
|
382
|
+
truth_text = truth_path.read_text()
|
|
383
|
+
truth = extract_block(truth_text)
|
|
384
|
+
if truth is None:
|
|
385
|
+
rep["errors"].append("preferences.md has no owner-preferences block")
|
|
386
|
+
return rep
|
|
387
|
+
|
|
388
|
+
blocks: dict[str, tuple[Target, str, str]] = {}
|
|
389
|
+
for t in cfg.targets:
|
|
390
|
+
try:
|
|
391
|
+
text = t.path.read_text()
|
|
392
|
+
except FileNotFoundError:
|
|
393
|
+
rep["missing"].append(t.label)
|
|
394
|
+
continue
|
|
395
|
+
except OSError as ex:
|
|
396
|
+
rep["errors"].append(f"{t.label}: {ex}")
|
|
397
|
+
continue
|
|
398
|
+
b = extract_block(text)
|
|
399
|
+
if b is None:
|
|
400
|
+
rep["missing"].append(t.label)
|
|
401
|
+
continue
|
|
402
|
+
blocks[t.label] = (t, text, b)
|
|
403
|
+
|
|
404
|
+
last_truth = st.get("_truth")
|
|
405
|
+
truth_changed = last_truth is not None and _h(truth) != last_truth
|
|
406
|
+
drifted = {lbl for lbl, (t, _, b) in blocks.items()
|
|
407
|
+
if lbl not in conflicts and _h(b) != _h(_want(t, truth, cfg)) and _h(b) != st.get(lbl)
|
|
408
|
+
and _drop_ids(b) != _drop_ids(_want(t, truth, cfg))} # only rule IDs differ: not an edit, overwrite
|
|
409
|
+
|
|
410
|
+
def full_view(lbl: str) -> str: # a slim copy compared as if it still carried the history section
|
|
411
|
+
t, _, b = blocks[lbl]
|
|
412
|
+
if t.layer == "core" and cfg.core_topics: # a layered copy: put the truth's folded topics back first
|
|
413
|
+
from . import layers
|
|
414
|
+
b = layers.merge_back(b, truth, cfg.core_topics)
|
|
415
|
+
return merge_back(b, truth) if t.slim else b
|
|
416
|
+
|
|
417
|
+
if drifted:
|
|
418
|
+
variants = {_h(full_view(l)) for l in drifted}
|
|
419
|
+
if last_truth is not None and not truth_changed and len(variants) == 1:
|
|
420
|
+
new_block = full_view(sorted(drifted)[0])
|
|
421
|
+
atomic_write(truth_path, replace_block(truth_text, new_block))
|
|
422
|
+
truth = new_block
|
|
423
|
+
rep["adopted"] = sorted(drifted)
|
|
424
|
+
append_event(cfg, agent=",".join(sorted(drifted)), kind="rule_change",
|
|
425
|
+
summary=f"shared rules edited in the {', '.join(sorted(drifted))} copy; adopted into preferences.md",
|
|
426
|
+
files=["preferences.md"] + [str(blocks[l][0].path) for l in sorted(drifted)])
|
|
427
|
+
else:
|
|
428
|
+
reason = ("rules changed in preferences.md and in this copy" if truth_changed
|
|
429
|
+
else "copies were edited differently" if len(variants) > 1
|
|
430
|
+
else "no sync history for this copy yet")
|
|
431
|
+
for lbl in sorted(drifted):
|
|
432
|
+
conflicts[lbl] = {"path": str(blocks[lbl][0].path), "target_hash": _h(blocks[lbl][2]),
|
|
433
|
+
"truth_hash": _h(truth), "ts": now_iso(), "reason": reason}
|
|
434
|
+
rep["conflicts"].append(lbl)
|
|
435
|
+
|
|
436
|
+
for lbl, (t, text, b) in blocks.items():
|
|
437
|
+
if lbl in conflicts:
|
|
438
|
+
continue
|
|
439
|
+
want = _want(t, truth, cfg)
|
|
440
|
+
if _h(b) == _h(want):
|
|
441
|
+
rep["in_sync"].append(lbl)
|
|
442
|
+
else:
|
|
443
|
+
_backup(cfg, lbl, text)
|
|
444
|
+
atomic_write(t.path, replace_block(text, want))
|
|
445
|
+
rep["updated"].append(lbl)
|
|
446
|
+
st[lbl] = _h(want)
|
|
447
|
+
st["_truth"] = _h(truth)
|
|
448
|
+
save_json(cfg.state / "distribute.json", st)
|
|
449
|
+
save_json(cfg.state / "conflicts.json", conflicts)
|
|
450
|
+
rep["open_conflicts"] = sorted(conflicts)
|
|
451
|
+
return rep
|
|
452
|
+
|
|
453
|
+
|
|
454
|
+
def resolve(cfg: Config, label: str, take: str) -> dict:
|
|
455
|
+
"""Owner's ruling on a conflict: keep the truth (overwrite the copy) or take the copy."""
|
|
456
|
+
conflicts = load_json(cfg.state / "conflicts.json", {})
|
|
457
|
+
if label not in conflicts:
|
|
458
|
+
raise ValueError(f"no open conflict for {label!r}")
|
|
459
|
+
target = next((t for t in cfg.targets if t.label == label), None)
|
|
460
|
+
if target is None:
|
|
461
|
+
raise ValueError(f"unknown target {label!r}")
|
|
462
|
+
st = load_json(cfg.state / "distribute.json", {})
|
|
463
|
+
block = extract_block(target.path.read_text())
|
|
464
|
+
if block is None:
|
|
465
|
+
raise ValueError(f"{target.path} lost its owner-preferences block")
|
|
466
|
+
if take == "truth":
|
|
467
|
+
st[label] = _h(block) # looks "untouched" -> next distribute overwrites it
|
|
468
|
+
elif take == "target":
|
|
469
|
+
truth_path = cfg.root / "preferences.md"
|
|
470
|
+
new_truth = merge_back(block, extract_block(truth_path.read_text()) or block) if target.slim else block
|
|
471
|
+
atomic_write(truth_path, replace_block(truth_path.read_text(), new_truth))
|
|
472
|
+
st[label] = _h(block)
|
|
473
|
+
st["_truth"] = _h(new_truth)
|
|
474
|
+
append_event(cfg, agent="owner", kind="rule_change",
|
|
475
|
+
summary=f"conflict resolved: took the {label} copy as the shared rules",
|
|
476
|
+
files=["preferences.md", str(target.path)])
|
|
477
|
+
else:
|
|
478
|
+
raise ValueError("take must be 'truth' or 'target'")
|
|
479
|
+
del conflicts[label]
|
|
480
|
+
save_json(cfg.state / "distribute.json", st)
|
|
481
|
+
save_json(cfg.state / "conflicts.json", conflicts)
|
|
482
|
+
return distribute(cfg)
|
|
483
|
+
|
|
484
|
+
|
|
485
|
+
# ───────────────────────────── render (what is going on) ─────────────────────────────
|
|
486
|
+
|
|
487
|
+
def build_current(events: list[dict], tail: int = 15) -> dict:
|
|
488
|
+
agents, projects, blocked = {}, {}, {}
|
|
489
|
+
for e in events:
|
|
490
|
+
brief = {k: e.get(k) for k in ("ts", "agent", "project", "kind", "summary")}
|
|
491
|
+
agents[e.get("agent", "?")] = brief
|
|
492
|
+
if e.get("project"):
|
|
493
|
+
projects[e["project"]] = brief
|
|
494
|
+
if e.get("kind") == "blocked":
|
|
495
|
+
blocked[e["id"]] = brief
|
|
496
|
+
elif e.get("kind") == "unblocked":
|
|
497
|
+
target = (e.get("refs") or {}).get("blocked_id")
|
|
498
|
+
for bid in [b for b, v in blocked.items() if b == target or (not target and v["project"] == e.get("project"))]:
|
|
499
|
+
del blocked[bid]
|
|
500
|
+
return {"event_count": len(events), "agents": agents, "projects": projects,
|
|
501
|
+
"open_blocked": list(blocked.values()), "tail": [
|
|
502
|
+
{k: e.get(k) for k in ("ts", "agent", "project", "kind", "summary", "files")} for e in events[-tail:]]}
|
|
503
|
+
|
|
504
|
+
|
|
505
|
+
def _short(s: str, n: int = 110) -> str:
|
|
506
|
+
s = " ".join((s or "").split())
|
|
507
|
+
return s if len(s) <= n else s[: n - 1] + "…"
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
BLOCKER_DETAIL_DAYS = 7
|
|
511
|
+
BLOCKER_DETAIL_MAX = 6 # every session loads agents.md, so only the latest few blockers in full
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
def _split_blockers(blockers: list[dict], now: "datetime | None" = None) -> tuple[list[dict], list[dict]]:
|
|
515
|
+
"""(blockers from the last BLOCKER_DETAIL_DAYS days, older ones); unparsable times count as recent."""
|
|
516
|
+
from datetime import datetime, timedelta
|
|
517
|
+
now = now or datetime.now().astimezone()
|
|
518
|
+
recent, old = [], []
|
|
519
|
+
for b in blockers:
|
|
520
|
+
try:
|
|
521
|
+
ts = datetime.fromisoformat(b["ts"])
|
|
522
|
+
ts = ts if ts.tzinfo else ts.astimezone()
|
|
523
|
+
(old if now - ts > timedelta(days=BLOCKER_DETAIL_DAYS) else recent).append(b)
|
|
524
|
+
except (KeyError, TypeError, ValueError):
|
|
525
|
+
recent.append(b)
|
|
526
|
+
return recent, old
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
def render(cfg: Config) -> dict:
|
|
530
|
+
from . import collab
|
|
531
|
+
cur = build_current(read_events(cfg))
|
|
532
|
+
claims = collab.load_claims(cfg)
|
|
533
|
+
hints = collab.report(cfg)["hints"]
|
|
534
|
+
cur["claims"] = claims
|
|
535
|
+
cur_path = cfg.state / "current.json"
|
|
536
|
+
changed = []
|
|
537
|
+
old_cur = load_json(cur_path, {})
|
|
538
|
+
if any(old_cur.get(k) != cur[k] for k in ("tail", "event_count", "claims")):
|
|
539
|
+
save_json(cur_path, dict(cur, generated_at=now_iso()))
|
|
540
|
+
changed.append("state/current.json")
|
|
541
|
+
|
|
542
|
+
lines = [PROG_START, f"_Updated {now_iso()} · {cur['event_count']} events · `aisync tail` for more_", "",
|
|
543
|
+
"| agent | last seen | project | what |", "|---|---|---|---|"]
|
|
544
|
+
for a, e in sorted(cur["agents"].items(), key=lambda kv: kv[1]["ts"] or "", reverse=True):
|
|
545
|
+
lines.append(f"| {a} | {e['ts']} | {e['project'] or '-'} | {_short(e['summary']).replace('|', '/')} |")
|
|
546
|
+
if claims:
|
|
547
|
+
lines += ["", "**Working right now** (claims; coordinate before touching these files)"] + [
|
|
548
|
+
f"- {c['agent']} @ {c['project']}: {_short(c['summary'])}"
|
|
549
|
+
f"{' [' + ', '.join(c['files'][:6]) + ']' if c['files'] else ' [whole repo]'} (until {c['expires'][11:16]})"
|
|
550
|
+
for c in claims]
|
|
551
|
+
if cur["open_blocked"]:
|
|
552
|
+
recent, old = _split_blockers(cur["open_blocked"])
|
|
553
|
+
shown = recent[-BLOCKER_DETAIL_MAX:]
|
|
554
|
+
lines += ["", "**Open blockers**"] + [f"- {b['ts']} {b['agent']} @ {b['project'] or '-'}: {_short(b['summary'])}"
|
|
555
|
+
for b in shown]
|
|
556
|
+
hidden = len(recent) - len(shown) + len(old)
|
|
557
|
+
if hidden: # older ones only as a count, details via aisync tail
|
|
558
|
+
lines.append(f"- {hidden} more open (older than {BLOCKER_DETAIL_DAYS} days, or beyond the latest {BLOCKER_DETAIL_MAX}); full list: `open_blocked` in `~/.ai-sync/state/current.json`")
|
|
559
|
+
lines += ["", "**Latest**"] + [f"- {e['ts']} · {e['agent']} · {e['kind']} · {e['project'] or '-'}: {_short(e['summary'])}"
|
|
560
|
+
for e in reversed(cur["tail"][-8:])]
|
|
561
|
+
if hints:
|
|
562
|
+
lines += ["", "**Self-check hints** (`aisync report`; reported only, never auto-applied)"] + [f"- {h}" for h in hints[:5]]
|
|
563
|
+
lines.append(PROG_END)
|
|
564
|
+
if cfg.progress_file:
|
|
565
|
+
return _render_split(cfg, lines, changed, cur)
|
|
566
|
+
agents_md = cfg.root / "agents.md"
|
|
567
|
+
text = agents_md.read_text() if agents_md.exists() else f"# Context hub\n\n{PROG_START}\n{PROG_END}\n"
|
|
568
|
+
s, e = text.find(PROG_START), text.find(PROG_END)
|
|
569
|
+
if s < 0 or e < 0:
|
|
570
|
+
text = text.rstrip() + f"\n\n{PROG_START}\n{PROG_END}\n"
|
|
571
|
+
s, e = text.find(PROG_START), text.find(PROG_END)
|
|
572
|
+
old_section = text[s:e + len(PROG_END)]
|
|
573
|
+
# Ignore the timestamp line when deciding whether anything really changed.
|
|
574
|
+
if old_section.split("\n", 2)[2:] != "\n".join(lines).split("\n", 2)[2:]:
|
|
575
|
+
atomic_write(agents_md, text[:s] + "\n".join(lines) + text[e + len(PROG_END):])
|
|
576
|
+
changed.append("agents.md")
|
|
577
|
+
return {"changed": changed, "agents": len(cur["agents"])}
|
|
578
|
+
|
|
579
|
+
|
|
580
|
+
PROG_POINTER = ("_Live progress (who is doing what, claims, blockers, latest events) is not in this file, so the file "
|
|
581
|
+
"never changes and stays in the prompt cache (2026-11-08: one journal event made every new session "
|
|
582
|
+
"rewrite ~50k cached tokens). Hooks inject it at session start; otherwise read `{path}` or run `aisync status`._")
|
|
583
|
+
|
|
584
|
+
|
|
585
|
+
def progress_path(cfg: Config) -> Path:
|
|
586
|
+
return Path(cfg.progress_file).expanduser() if cfg.progress_file.startswith(("/", "~")) else cfg.root / cfg.progress_file
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
def _render_split(cfg: Config, lines: list[str], changed: list[str], cur: dict) -> dict:
|
|
590
|
+
"""progress_file mode: the table goes to its own file; agents.md keeps only a fixed pointer."""
|
|
591
|
+
p = progress_path(cfg)
|
|
592
|
+
body = "\n".join(l for l in lines if l not in (PROG_START, PROG_END)) + "\n"
|
|
593
|
+
old = p.read_text() if p.exists() else ""
|
|
594
|
+
if old.split("\n", 1)[1:] != body.split("\n", 1)[1:]: # ignore the timestamp line
|
|
595
|
+
atomic_write(p, body)
|
|
596
|
+
changed.append(str(p.name))
|
|
597
|
+
agents_md = cfg.root / "agents.md"
|
|
598
|
+
text = agents_md.read_text() if agents_md.exists() else "# Context hub\n"
|
|
599
|
+
pointer = PROG_POINTER.format(path=str(p).replace(str(Path.home()), "~"))
|
|
600
|
+
s, e = text.find(PROG_START), text.find(PROG_END)
|
|
601
|
+
body = (text[:s].rstrip() + "\n\n" + pointer + "\n" + text[e + len(PROG_END):].lstrip("\n")) if s >= 0 and e >= 0 else text
|
|
602
|
+
# exactly one pointer: an old writer re-adding the block left one more pointer each time (4 by 2026-11-08)
|
|
603
|
+
head, sep, tail = body.partition(pointer)
|
|
604
|
+
new = (head + sep + re.sub(r"\n*" + re.escape(pointer), "", tail)) if sep else body.rstrip() + "\n\n" + pointer + "\n"
|
|
605
|
+
if new != text:
|
|
606
|
+
atomic_write(agents_md, new)
|
|
607
|
+
changed.append("agents.md")
|
|
608
|
+
return {"changed": changed, "agents": len(cur["agents"])}
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
def progress_now(cfg: Config) -> str:
|
|
612
|
+
"""The live progress text for a hook to inject ('' when progress_file is off or unreadable)."""
|
|
613
|
+
if not cfg.progress_file:
|
|
614
|
+
return ""
|
|
615
|
+
try:
|
|
616
|
+
return progress_path(cfg).read_text().strip()
|
|
617
|
+
except OSError:
|
|
618
|
+
return ""
|
|
619
|
+
|
|
620
|
+
|
|
621
|
+
# ───────────────────────────── harvest (git commits -> journal) ─────────────────────────────
|
|
622
|
+
|
|
623
|
+
_AGENT_HINTS = (("claude", "claude-code"), ("codex", "codex"), ("cursor", "cursor"),
|
|
624
|
+
("trae", "trae"), ("kiro", "kiro"), ("windsurf", "windsurf"))
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
def infer_agent(message: str, author: str) -> str:
|
|
628
|
+
"""Attribute a commit by its trailers only (subjects that merely mention a tool don't count)."""
|
|
629
|
+
for line in message.splitlines():
|
|
630
|
+
low = line.strip().lower()
|
|
631
|
+
if low.startswith("agent:"):
|
|
632
|
+
return low.split(":", 1)[1].strip() or f"git:{author}"
|
|
633
|
+
if low.startswith(("co-authored-by:", "generated with", "generated-by:", "🤖")):
|
|
634
|
+
for needle, agent in _AGENT_HINTS:
|
|
635
|
+
if needle in low:
|
|
636
|
+
return agent
|
|
637
|
+
return f"git:{author}"
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
def _gitdir(project: Path) -> Path | None:
|
|
641
|
+
dot = project / ".git"
|
|
642
|
+
if dot.is_dir():
|
|
643
|
+
return dot
|
|
644
|
+
if dot.is_file():
|
|
645
|
+
line = dot.read_text().strip()
|
|
646
|
+
if line.startswith("gitdir:"):
|
|
647
|
+
p = Path(line.split(":", 1)[1].strip())
|
|
648
|
+
return p if p.is_absolute() else (project / p).resolve()
|
|
649
|
+
return None
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
def head_log(project: Path) -> Path | None:
|
|
653
|
+
gd = _gitdir(project)
|
|
654
|
+
return gd / "logs" / "HEAD" if gd else None
|
|
655
|
+
|
|
656
|
+
|
|
657
|
+
def _git(args: list[str], cwd: Path, timeout: float) -> subprocess.CompletedProcess:
|
|
658
|
+
return subprocess.run(["git", *args], cwd=cwd, capture_output=True, text=True, timeout=timeout)
|
|
659
|
+
|
|
660
|
+
|
|
661
|
+
def harvest(cfg: Config) -> dict:
|
|
662
|
+
st = load_json(cfg.state / "harvest.json", {"cursors": {}, "seen": []})
|
|
663
|
+
seen = set(st["seen"])
|
|
664
|
+
rep = {"events": 0, "projects": 0, "errors": []}
|
|
665
|
+
for proj in project_list(cfg):
|
|
666
|
+
if _gitdir(proj) is None:
|
|
667
|
+
continue
|
|
668
|
+
key = str(proj)
|
|
669
|
+
try:
|
|
670
|
+
r = _git(["rev-parse", "HEAD"], proj, cfg.git_timeout_secs)
|
|
671
|
+
if r.returncode != 0:
|
|
672
|
+
continue # empty repo
|
|
673
|
+
head = r.stdout.strip()
|
|
674
|
+
rep["projects"] += 1
|
|
675
|
+
last = st["cursors"].get(key)
|
|
676
|
+
if last is None or last == head:
|
|
677
|
+
st["cursors"][key] = head # first sight: start from now, don't flood history
|
|
678
|
+
continue
|
|
679
|
+
r = _git(["log", "--no-merges", "--max-count=50", "--name-only",
|
|
680
|
+
"--format=%x1e%H%x1f%an%x1f%aI%x1f%B%x1f", f"{last}..{head}"], proj, cfg.git_timeout_secs)
|
|
681
|
+
if r.returncode != 0: # cursor commit gone (rebase/gc): restart from HEAD
|
|
682
|
+
st["cursors"][key] = head
|
|
683
|
+
rep["errors"].append(f"{proj.name}: cursor reset ({r.stderr.strip()[:120]})")
|
|
684
|
+
continue
|
|
685
|
+
for rec in reversed([x for x in r.stdout.split("\x1e") if x.strip()]):
|
|
686
|
+
sha, author, date, body, files = (rec.split("\x1f") + [""] * 5)[:5]
|
|
687
|
+
if sha in seen:
|
|
688
|
+
continue
|
|
689
|
+
subject = body.strip().splitlines()[0] if body.strip() else "(no message)"
|
|
690
|
+
append_event(cfg, agent=infer_agent(body, author), kind="commit", project=proj.name,
|
|
691
|
+
summary=subject, files=[f for f in files.split("\n") if f.strip()][:50],
|
|
692
|
+
refs={"commit": sha[:12]}, ts=date)
|
|
693
|
+
seen.add(sha)
|
|
694
|
+
st["seen"].append(sha)
|
|
695
|
+
rep["events"] += 1
|
|
696
|
+
st["cursors"][key] = head
|
|
697
|
+
except (subprocess.TimeoutExpired, OSError) as ex:
|
|
698
|
+
rep["errors"].append(f"{proj.name}: {type(ex).__name__}") # isolate: next project still runs
|
|
699
|
+
st["seen"] = st["seen"][-SEEN_COMMITS_MAX:]
|
|
700
|
+
save_json(cfg.state / "harvest.json", st)
|
|
701
|
+
return rep
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
# ───────────────────────────── health, alerts, run log ─────────────────────────────
|
|
705
|
+
|
|
706
|
+
def load_health(cfg: Config) -> dict:
|
|
707
|
+
return load_json(cfg.state / "health.json", {"push_failures": 0, "push_paused": False,
|
|
708
|
+
"cycle_failures": 0, "cycles_paused": False})
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
def save_health(cfg: Config, h: dict) -> None:
|
|
712
|
+
save_json(cfg.state / "health.json", h)
|
|
713
|
+
|
|
714
|
+
|
|
715
|
+
def alert(cfg: Config, title: str, message: str) -> None:
|
|
716
|
+
"""Never fail silently: ALERT.md + run log + desktop notification (best effort)."""
|
|
717
|
+
cfg.state.mkdir(parents=True, exist_ok=True)
|
|
718
|
+
with open(cfg.state / "ALERT.md", "a") as fh:
|
|
719
|
+
fh.write(f"- {now_iso()} **{title}**: {message}\n")
|
|
720
|
+
log_run(cfg, {"ts": now_iso(), "level": "alert", "title": title, "message": message})
|
|
721
|
+
if os.environ.get("AISYNC_NO_NOTIFY"):
|
|
722
|
+
return
|
|
723
|
+
if sys.platform == "darwin":
|
|
724
|
+
script = f'display notification {json.dumps(message[:200])} with title {json.dumps("aisync: " + title)}'
|
|
725
|
+
cmd = ["osascript", "-e", script]
|
|
726
|
+
else:
|
|
727
|
+
cmd = ["notify-send", f"aisync: {title}", message[:200]]
|
|
728
|
+
with contextlib.suppress(Exception):
|
|
729
|
+
subprocess.run(cmd, capture_output=True, timeout=5)
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
def log_run(cfg: Config, rec: dict) -> None:
|
|
733
|
+
path = cfg.state / "runs.jsonl"
|
|
734
|
+
cfg.state.mkdir(parents=True, exist_ok=True)
|
|
735
|
+
if path.exists() and path.stat().st_size > RUNS_MAX_BYTES:
|
|
736
|
+
os.replace(path, path.with_suffix(".jsonl.1"))
|
|
737
|
+
with open(path, "a") as fh:
|
|
738
|
+
fh.write(json.dumps(rec, ensure_ascii=False) + "\n")
|
|
739
|
+
|
|
740
|
+
|
|
741
|
+
def read_runs(cfg: Config, n: int = 50) -> list[dict]:
|
|
742
|
+
path = cfg.state / "runs.jsonl"
|
|
743
|
+
if not path.exists():
|
|
744
|
+
return []
|
|
745
|
+
out = []
|
|
746
|
+
for line in path.read_text().splitlines()[-n:]:
|
|
747
|
+
with contextlib.suppress(json.JSONDecodeError):
|
|
748
|
+
out.append(json.loads(line))
|
|
749
|
+
return out
|
|
750
|
+
|
|
751
|
+
|
|
752
|
+
# ───────────────────────────── git flush ─────────────────────────────
|
|
753
|
+
|
|
754
|
+
STALE_LOCK_SECS = 600 # git holds a lock for seconds; a lock this old was left by a crash (twice on 2026-11-08)
|
|
755
|
+
_LOCK_RE = re.compile(r"Unable to create '([^']+\.lock)': File exists")
|
|
756
|
+
|
|
757
|
+
|
|
758
|
+
def clear_stale_lock(err: str) -> bool:
|
|
759
|
+
"""Remove the lock git complains about when it is older than STALE_LOCK_SECS; True when one was removed."""
|
|
760
|
+
m = _LOCK_RE.search(err or "")
|
|
761
|
+
if not m:
|
|
762
|
+
return False
|
|
763
|
+
p = Path(m.group(1))
|
|
764
|
+
try:
|
|
765
|
+
if time.time() - p.stat().st_mtime < STALE_LOCK_SECS:
|
|
766
|
+
return False
|
|
767
|
+
p.unlink()
|
|
768
|
+
except OSError:
|
|
769
|
+
return False
|
|
770
|
+
return True
|
|
771
|
+
|
|
772
|
+
|
|
773
|
+
def git_run(cfg: Config, args: list[str], retries: int = 1) -> subprocess.CompletedProcess:
|
|
774
|
+
"""Timeout on every call; exponential backoff between attempts."""
|
|
775
|
+
err = ""
|
|
776
|
+
for attempt in range(max(1, retries)):
|
|
777
|
+
try:
|
|
778
|
+
r = _git(args, cfg.root, cfg.git_timeout_secs)
|
|
779
|
+
if r.returncode == 0:
|
|
780
|
+
return r
|
|
781
|
+
err = (r.stderr or r.stdout).strip()[:300]
|
|
782
|
+
if clear_stale_lock(err): # a crash left the lock: drop it and try again at once
|
|
783
|
+
r = _git(args, cfg.root, cfg.git_timeout_secs)
|
|
784
|
+
if r.returncode == 0:
|
|
785
|
+
return r
|
|
786
|
+
err = (r.stderr or r.stdout).strip()[:300]
|
|
787
|
+
except subprocess.TimeoutExpired:
|
|
788
|
+
err = f"timeout after {cfg.git_timeout_secs}s"
|
|
789
|
+
if attempt < retries - 1:
|
|
790
|
+
time.sleep(min(cfg.backoff_base_secs * 2 ** attempt, 60))
|
|
791
|
+
raise GitError(f"git {args[0]}: {err}")
|
|
792
|
+
|
|
793
|
+
|
|
794
|
+
def ensure_repo(cfg: Config) -> None:
|
|
795
|
+
if not (cfg.root / ".git").exists():
|
|
796
|
+
git_run(cfg, ["init", "-q", "-b", "main"])
|
|
797
|
+
# The hub commits on its own; give it a local identity when the machine has none.
|
|
798
|
+
if _git(["config", "user.email"], cfg.root, cfg.git_timeout_secs).returncode != 0:
|
|
799
|
+
git_run(cfg, ["config", "user.name", "aisync"])
|
|
800
|
+
git_run(cfg, ["config", "user.email", "aisync@localhost"])
|
|
801
|
+
|
|
802
|
+
|
|
803
|
+
def flush(cfg: Config) -> dict:
|
|
804
|
+
ensure_repo(cfg)
|
|
805
|
+
files = [f for f in TRACKED if (cfg.root / f).exists()]
|
|
806
|
+
git_run(cfg, ["add", "--", *files])
|
|
807
|
+
staged = _git(["diff", "--cached", "--name-only"], cfg.root, cfg.git_timeout_secs).stdout.split()
|
|
808
|
+
rep = {"committed": None, "files": staged, "pushed": None}
|
|
809
|
+
health = load_health(cfg)
|
|
810
|
+
if staged:
|
|
811
|
+
events = read_events(cfg)
|
|
812
|
+
last_sha = load_json(cfg.state / "flush.json", {}).get("last_event_id")
|
|
813
|
+
ids = [e.get("id") for e in events]
|
|
814
|
+
new = events[ids.index(last_sha) + 1:] if last_sha in ids else events[-20:]
|
|
815
|
+
who = sorted({e.get("agent", "?") for e in new})
|
|
816
|
+
git_run(cfg, ["commit", "-q", "-m", f"chore(aisync): {len(new)} event(s) from {', '.join(who) or 'n/a'}",
|
|
817
|
+
"-m", "\n".join(f"- {e.get('agent')} {e.get('kind')}: {_short(e.get('summary', ''), 90)}"
|
|
818
|
+
for e in new[-30:]) or "state refresh"])
|
|
819
|
+
rep["committed"] = git_run(cfg, ["rev-parse", "--short", "HEAD"]).stdout.strip()
|
|
820
|
+
save_json(cfg.state / "flush.json", {"last_event_id": ids[-1] if ids else None, "ts": now_iso()})
|
|
821
|
+
if cfg.remote and not health.get("push_paused"):
|
|
822
|
+
ahead = _git(["rev-list", "--count", f"{cfg.remote}/{cfg.remote_branch}..HEAD"], cfg.root, cfg.git_timeout_secs)
|
|
823
|
+
if rep["committed"] or ahead.returncode != 0 or ahead.stdout.strip() not in ("", "0"):
|
|
824
|
+
try:
|
|
825
|
+
git_run(cfg, ["push", "-q", cfg.remote, f"HEAD:{cfg.remote_branch}"], retries=cfg.git_retries)
|
|
826
|
+
rep["pushed"] = True
|
|
827
|
+
health.update(push_failures=0, last_push_ok=now_iso())
|
|
828
|
+
except GitError as ex:
|
|
829
|
+
rep["pushed"] = False
|
|
830
|
+
health["push_failures"] = health.get("push_failures", 0) + 1
|
|
831
|
+
health["last_push_error"] = str(ex)
|
|
832
|
+
if health["push_failures"] >= cfg.fail_threshold:
|
|
833
|
+
health["push_paused"] = True
|
|
834
|
+
alert(cfg, "push paused", f"{health['push_failures']} pushes failed in a row; local journaling continues. "
|
|
835
|
+
f"Fix the remote, then `aisync resume`. Last error: {ex}")
|
|
836
|
+
elif health.get("push_paused"):
|
|
837
|
+
rep["pushed"] = "paused"
|
|
838
|
+
save_health(cfg, health)
|
|
839
|
+
return rep
|
|
840
|
+
|
|
841
|
+
|
|
842
|
+
# ───────────────────────────── the cycle ─────────────────────────────
|
|
843
|
+
|
|
844
|
+
def run_cycle(cfg: Config, reason: str = "manual") -> dict:
|
|
845
|
+
t0 = time.monotonic()
|
|
846
|
+
rec = {"ts": now_iso(), "reason": reason, "steps": {}}
|
|
847
|
+
from . import collab, ruleids
|
|
848
|
+
steps = (("harvest", harvest), ("rules", collab.harvest_rules), ("ids", ruleids.normalize), ("distribute", distribute),
|
|
849
|
+
("render", render), ("progress", collab.render_progress), ("tidy", tidy_kick),
|
|
850
|
+
("flush", flush))
|
|
851
|
+
for name, fn in steps:
|
|
852
|
+
s = time.monotonic()
|
|
853
|
+
try:
|
|
854
|
+
rec["steps"][name] = {"ok": True, "result": fn(cfg)}
|
|
855
|
+
except Exception as ex: # failure isolation: keep going
|
|
856
|
+
rec["steps"][name] = {"ok": False, "error": f"{type(ex).__name__}: {ex}"[:400]}
|
|
857
|
+
rec["steps"][name]["ms"] = round((time.monotonic() - s) * 1000)
|
|
858
|
+
rec["ok"] = all(s["ok"] for s in rec["steps"].values())
|
|
859
|
+
rec["ms"] = round((time.monotonic() - t0) * 1000)
|
|
860
|
+
|
|
861
|
+
health = load_health(cfg)
|
|
862
|
+
dist = rec["steps"]["distribute"].get("result") or {}
|
|
863
|
+
new_conf = sorted(set(dist.get("conflicts", [])))
|
|
864
|
+
if new_conf:
|
|
865
|
+
alert(cfg, "rule conflict", f"shared rules conflict in {', '.join(new_conf)}; "
|
|
866
|
+
f"run `aisync status` then `aisync resolve <label> --take truth|target`")
|
|
867
|
+
if rec["ok"]:
|
|
868
|
+
health["cycle_failures"] = 0
|
|
869
|
+
health["last_cycle_ok"] = rec["ts"]
|
|
870
|
+
else:
|
|
871
|
+
health["cycle_failures"] = health.get("cycle_failures", 0) + 1
|
|
872
|
+
health["last_cycle_error"] = {k: v.get("error") for k, v in rec["steps"].items() if not v["ok"]}
|
|
873
|
+
if health["cycle_failures"] >= cfg.fail_threshold and not health.get("cycles_paused"):
|
|
874
|
+
health["cycles_paused"] = True
|
|
875
|
+
alert(cfg, "sync paused", f"{health['cycle_failures']} cycles failed in a row: "
|
|
876
|
+
f"{health['last_cycle_error']}. Fix it, then `aisync resume`.")
|
|
877
|
+
save_health(cfg, health)
|
|
878
|
+
log_run(cfg, rec)
|
|
879
|
+
return rec
|
|
880
|
+
|
|
881
|
+
|
|
882
|
+
# ───────────────────────────── status ─────────────────────────────
|
|
883
|
+
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
def under_test() -> bool:
|
|
887
|
+
"""pytest, plain `python -m unittest`, or a test-launched child (AISYNC_UNDER_TEST); a real hub process imports none."""
|
|
888
|
+
return bool(os.environ.get("PYTEST_CURRENT_TEST") or os.environ.get("AISYNC_UNDER_TEST")) or "unittest" in sys.modules
|
|
889
|
+
|
|
890
|
+
|
|
891
|
+
def tidy_kick(cfg: Config) -> dict:
|
|
892
|
+
"""Once a day, start `aisync tidy --write` detached (it scans weeks of transcripts; the cycle never waits, R-177)."""
|
|
893
|
+
# 2026-11-08 EB-074: tests (test_wheel runs `aisync sync` in a temp hub) each spawned a scan of 11 GB of real
|
|
894
|
+
# transcripts; they piled up past 16 GB RAM and the full swap, and the Mac panicked. Four guards now:
|
|
895
|
+
if not cfg.tidy_daily:
|
|
896
|
+
return {"started": False, "reason": "tidy_daily is off"}
|
|
897
|
+
if os.environ.get("AISYNC_NO_TIDY") or under_test():
|
|
898
|
+
return {"started": False, "reason": "disabled here (AISYNC_NO_TIDY or a test)"}
|
|
899
|
+
day = f"{datetime.now().astimezone():%Y-%m-%d}"
|
|
900
|
+
if (cfg.root / "tidy" / f"{day}.md").exists():
|
|
901
|
+
return {"started": False, "reason": "today's report exists"}
|
|
902
|
+
mark = cfg.state / "tidy-started"
|
|
903
|
+
if mark.exists() and mark.read_text().strip() == day:
|
|
904
|
+
return {"started": False, "reason": "already started today"}
|
|
905
|
+
pidf = cfg.state / "tidy.pid"
|
|
906
|
+
try:
|
|
907
|
+
if pidf.exists() and _pid_alive(int(pidf.read_text().strip() or 0)):
|
|
908
|
+
return {"started": False, "reason": "a tidy is still running"}
|
|
909
|
+
except ValueError:
|
|
910
|
+
pass
|
|
911
|
+
mark.parent.mkdir(parents=True, exist_ok=True)
|
|
912
|
+
mark.write_text(day)
|
|
913
|
+
p = subprocess.Popen(["nice", "-n", "15", sys.executable, "-m", "ai_sync", "tidy", "--write"], start_new_session=True,
|
|
914
|
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, env={**os.environ, "AISYNC_HOME": str(cfg.root)})
|
|
915
|
+
pidf.write_text(str(p.pid))
|
|
916
|
+
return {"started": True}
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
def _pid_alive(pid) -> bool:
|
|
920
|
+
try:
|
|
921
|
+
os.kill(int(pid), 0)
|
|
922
|
+
return True
|
|
923
|
+
except (OSError, TypeError, ValueError):
|
|
924
|
+
return False
|
|
925
|
+
|
|
926
|
+
|
|
927
|
+
def status(cfg: Config) -> dict:
|
|
928
|
+
events = read_events(cfg)
|
|
929
|
+
health = load_health(cfg)
|
|
930
|
+
runs = [r for r in read_runs(cfg, 200) if "steps" in r]
|
|
931
|
+
porcelain = _git(["status", "--porcelain", "--", *TRACKED], cfg.root, cfg.git_timeout_secs).stdout \
|
|
932
|
+
if (cfg.root / ".git").exists() else ""
|
|
933
|
+
truth = extract_block((cfg.root / "preferences.md").read_text()) if (cfg.root / "preferences.md").exists() else None
|
|
934
|
+
targets = {}
|
|
935
|
+
for t in cfg.targets:
|
|
936
|
+
try:
|
|
937
|
+
b = extract_block(t.path.read_text())
|
|
938
|
+
targets[t.label] = "in sync" if b and truth and _h(b) == _h(truth) else ("no block" if b is None else "differs")
|
|
939
|
+
except OSError:
|
|
940
|
+
targets[t.label] = "missing"
|
|
941
|
+
hb = health.get("watcher_heartbeat_epoch", 0)
|
|
942
|
+
watcher_alive = _pid_alive(health.get("watcher_pid")) and time.time() - hb < max(180, cfg.reconcile_secs * 2)
|
|
943
|
+
failed = [r for r in runs if not r.get("ok")]
|
|
944
|
+
return {
|
|
945
|
+
"home": str(cfg.root),
|
|
946
|
+
"watcher": {"alive": watcher_alive, "pid": health.get("watcher_pid"),
|
|
947
|
+
"heartbeat": health.get("watcher_heartbeat")},
|
|
948
|
+
"journal": {"events": len(events), "chain_problems": verify_chain(cfg)},
|
|
949
|
+
"agents": build_current(events)["agents"],
|
|
950
|
+
"unflushed_files": [l[3:] for l in porcelain.splitlines()],
|
|
951
|
+
"targets": targets,
|
|
952
|
+
"conflicts": load_json(cfg.state / "conflicts.json", {}),
|
|
953
|
+
"health": {k: v for k, v in health.items() if not k.startswith("watcher_")},
|
|
954
|
+
"metrics": {"cycles": len(runs), "failed": len(failed),
|
|
955
|
+
"failure_rate": round(len(failed) / len(runs), 3) if runs else 0.0,
|
|
956
|
+
"avg_ms": round(sum(r.get("ms", 0) for r in runs) / len(runs)) if runs else 0,
|
|
957
|
+
"commits_harvested": sum((r["steps"].get("harvest", {}).get("result") or {}).get("events", 0) for r in runs),
|
|
958
|
+
"targets_updated": sum(len((r["steps"].get("distribute", {}).get("result") or {}).get("updated", []))
|
|
959
|
+
for r in runs),
|
|
960
|
+
"last_cycle": runs[-1] if runs else None},
|
|
961
|
+
"alerts_file": str(cfg.state / "ALERT.md") if (cfg.state / "ALERT.md").exists() else None,
|
|
962
|
+
"web_inbox": {k: v for k, v in load_json(cfg.state / "web_inbox.json", {}).items()
|
|
963
|
+
if k not in ("seen", "rejected")} | {"every_secs": cfg.web_inbox_secs},
|
|
964
|
+
# non-vacuous: what this status really looked at; 0 of any is a failure in `aisync status`
|
|
965
|
+
"checked": {"journal events": len(events), "rule copies": len(targets),
|
|
966
|
+
"rule block lines": sum(1 for ln in (truth or "").splitlines()[1:-1] if ln.strip())},
|
|
967
|
+
}
|
|
968
|
+
|
|
969
|
+
|
|
970
|
+
def resume(cfg: Config) -> dict:
|
|
971
|
+
h = load_health(cfg)
|
|
972
|
+
h.update(push_failures=0, push_paused=False, cycle_failures=0, cycles_paused=False)
|
|
973
|
+
save_health(cfg, h)
|
|
974
|
+
return h
|