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/ui.py ADDED
@@ -0,0 +1,162 @@
1
+ """Terminal look of the `aisync` front screens: pixel logo, gradient, wave rule, help screen.
2
+
3
+ Standard library only, and it degrades on purpose:
4
+
5
+ * colour is on when the stream is a terminal; NO_COLOR turns it off, FORCE_COLOR (not "0") turns it
6
+ on; truecolor when COLORTERM says so, else the 256-colour palette;
7
+ * a stream whose encoding is not UTF-8, or a terminal narrower than the logo, gets a one-line header,
8
+ so a pipe or a legacy console never shows broken glyphs.
9
+
10
+ Command output (status, tail, report ...) stays plain text: agents parse it.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import math
15
+ import os
16
+ import re
17
+ import shutil
18
+ import textwrap
19
+
20
+ PINK, VIOLET, CYAN = (240, 101, 149), (132, 94, 247), (34, 184, 207)
21
+ GREEN, AMBER = (64, 192, 87), (250, 176, 5)
22
+ GRAY, INK = (134, 142, 150), (233, 236, 239)
23
+ LOGO_STOPS = (PINK, VIOLET, CYAN)
24
+ TAGLINE = "Multi-Agent Context Management, Coordination & Observability"
25
+
26
+ # "AISYNC" as a 3x5 pixel font (N is 4 wide), each pixel two columns of the full block (U+2588) or two spaces:
27
+ # every monospace font draws it the same, unlike figlet-style box-drawing lettering.
28
+ _PIXELS = {
29
+ "A": (".#.", "#.#", "###", "#.#", "#.#"),
30
+ "I": ("###", ".#.", ".#.", ".#.", "###"),
31
+ "S": (".##", "#..", ".#.", "..#", "##."),
32
+ "Y": ("#.#", "#.#", ".#.", ".#.", ".#."),
33
+ "N": ("#..#", "##.#", "#.##", "#..#", "#..#"),
34
+ "C": (".##", "#..", "#..", "#..", ".##"),
35
+ }
36
+ LOGO = tuple(" ".join("".join("██" if px == "#" else " " for px in _PIXELS[ch][row]) for ch in "AISYNC")
37
+ for row in range(5))
38
+ SPARKS = "▁▂▃▄▅▆▇█"
39
+
40
+ _ANSI = re.compile(r"\x1b\[[0-9;]*m")
41
+
42
+
43
+ def strip_ansi(text: str) -> str:
44
+ return _ANSI.sub("", text)
45
+
46
+
47
+ def _lerp(a, b, t):
48
+ return tuple(round(x + (y - x) * t) for x, y in zip(a, b))
49
+
50
+
51
+ def color_at(stops, t):
52
+ """Colour at position T (0..1) along the gradient STOPS."""
53
+ t = min(1.0, max(0.0, t)) * (len(stops) - 1)
54
+ i = min(int(t), len(stops) - 2)
55
+ return _lerp(stops[i], stops[i + 1], t - i)
56
+
57
+
58
+ class Style:
59
+ """How to draw: colour on/off, unicode or ascii, truecolor, terminal width."""
60
+
61
+ def __init__(self, color=False, unicode=True, width=80, truecolor=False):
62
+ self.color, self.unicode, self.truecolor = color, unicode, truecolor
63
+ self.width = max(30, int(width))
64
+
65
+ def fg(self, rgb) -> str:
66
+ if self.truecolor:
67
+ return "38;2;%d;%d;%d" % rgb
68
+ r, g, b = (round(c / 255 * 5) for c in rgb)
69
+ return "38;5;%d" % (16 + 36 * r + 6 * g + b)
70
+
71
+ def paint(self, text, rgb=None, bold=False, dim=False) -> str:
72
+ if not self.color or not text:
73
+ return text
74
+ codes = (["1"] if bold else []) + (["2"] if dim else []) + ([self.fg(rgb)] if rgb else [])
75
+ return f"\x1b[{';'.join(codes)}m{text}\x1b[0m" if codes else text
76
+
77
+ def gradient(self, text, stops=LOGO_STOPS, bold=False, span=None) -> str:
78
+ if not self.color:
79
+ return text
80
+ n = max(1, (span or len(text)) - 1)
81
+ return "".join(ch if ch == " " else self.paint(ch, color_at(stops, i / n), bold)
82
+ for i, ch in enumerate(text))
83
+
84
+ def head(self, text) -> str:
85
+ return self.paint(text, INK, bold=True)
86
+
87
+ def muted(self, text) -> str:
88
+ return self.paint(text, GRAY)
89
+
90
+
91
+ def _env_on(env, name) -> bool:
92
+ return env.get(name, "") not in ("", "0")
93
+
94
+
95
+ def style_for(stream, env=None, width=None) -> Style:
96
+ env = os.environ if env is None else env
97
+ try:
98
+ tty = stream.isatty()
99
+ except (AttributeError, ValueError):
100
+ tty = False
101
+ color = (tty or _env_on(env, "FORCE_COLOR")) and "NO_COLOR" not in env and env.get("TERM") != "dumb"
102
+ encoding = (getattr(stream, "encoding", None) or "").lower().replace("-", "")
103
+ unicode = encoding in ("utf8", "utf_8")
104
+ truecolor = env.get("COLORTERM", "").lower() in ("truecolor", "24bit") or _env_on(env, "FORCE_COLOR")
105
+ if width is None:
106
+ width = shutil.get_terminal_size((80, 24)).columns
107
+ return Style(color=color, unicode=unicode, width=width, truecolor=truecolor)
108
+
109
+
110
+ def wave(style: Style, width: int) -> str:
111
+ """A soft sine rule under the logo, coloured along the same gradient."""
112
+ chars = "".join(SPARKS[round((math.sin(i / width * math.pi * 3) + 1) / 2 * (len(SPARKS) - 1))]
113
+ for i in range(width))
114
+ return style.gradient(chars)
115
+
116
+
117
+ def banner(style: Style, version: str) -> str:
118
+ """The logo block; one line when the stream is not UTF-8 or the terminal is narrower than the logo."""
119
+ ver = "v" + version
120
+ if style.unicode and style.width >= len(LOGO[0]) + 2:
121
+ lines = [style.gradient(row, bold=True) for row in LOGO]
122
+ lines.append(wave(style, len(LOGO[0])))
123
+ dot = style.muted(" ▪ ")
124
+ lines.append(style.head(TAGLINE) + dot + style.muted(ver)) # the tagline names the three jobs; fits 80 columns
125
+ return "\n".join(lines)
126
+ return style.head("aisync") + " " + style.muted(ver) + " - " + TAGLINE
127
+
128
+
129
+ # Command groups for the front screen; anything not listed lands in "more".
130
+ GROUPS = (
131
+ ("start", ("init", "install-hooks", "demo")),
132
+ ("every session", ("news", "claim", "release", "log", "rule", "lease", "wt")),
133
+ ("look around", ("status", "claims", "tail", "report", "board", "verify")),
134
+ ("keep the rules lean", ("tidy", "rulecheck", "instinct")),
135
+ ("keep it running", ("sync", "watch", "resolve", "resume", "hook", "mcp", "guard", "gate", "web-inbox")),
136
+ )
137
+ GROUP_COLORS = (PINK, VIOLET, CYAN, AMBER, GREEN, AMBER)
138
+
139
+
140
+ def help_screen(style: Style, version: str, commands: dict[str, str]) -> str:
141
+ """`aisync` and `aisync -h`: logo, usage, grouped commands. COMMANDS: {name: one-line help}."""
142
+ arrow = "❯" if style.unicode else ">"
143
+ width = max(len(n) for n in commands) + 2
144
+ out = [banner(style, version), "",
145
+ " " + style.head("usage") + " aisync " + style.paint("COMMAND", AMBER, bold=True)
146
+ + " [options] " + style.muted("aisync COMMAND -h for details"), ""]
147
+ listed = [n for _, names in GROUPS for n in names]
148
+ groups = [(t, [n for n in names if n in commands]) for t, names in GROUPS]
149
+ rest = [n for n in commands if n not in listed]
150
+ if rest:
151
+ groups.append(("more", rest))
152
+ for i, (title, names) in enumerate(groups):
153
+ rgb = GROUP_COLORS[i % len(GROUP_COLORS)]
154
+ out.append(" " + style.paint(arrow, rgb, bold=True) + " " + style.head(title))
155
+ for name in names:
156
+ text = textwrap.wrap(commands[name], max(20, style.width - 6 - width)) or [""]
157
+ out.append(" " + style.paint(name.ljust(width), rgb, bold=True) + style.muted(text[0]))
158
+ out += [" " * (4 + width) + style.muted(line) for line in text[1:]]
159
+ out.append("")
160
+ out.append(" " + style.muted("try it without touching anything: ") + style.paint("aisync demo", PINK, bold=True))
161
+ out.append(" " + style.muted("docs https://github.com/MaxonT/aisync"))
162
+ return "\n".join(out)
ai_sync/update.py ADDED
@@ -0,0 +1,73 @@
1
+ """`aisync update`: say whether a newer release exists and the command that installs it.
2
+
3
+ It never installs anything itself: the upgrade command depends on how aisync was installed
4
+ (uv, pipx, the install.sh virtualenv, Homebrew or pip), so it prints that one command and stops."""
5
+ from __future__ import annotations
6
+
7
+ import json
8
+ import re
9
+ import sys
10
+ import urllib.error
11
+ import urllib.request
12
+ from pathlib import Path
13
+ from typing import Callable
14
+
15
+ REPO = "MaxonT/aisync"
16
+ LATEST_URL = f"https://api.github.com/repos/{REPO}/releases/latest"
17
+
18
+
19
+ def parse_version(text: str) -> tuple[int, ...] | None:
20
+ """'v0.2.5' or '0.2.5' -> (0, 2, 5). None when the text is not a plain release number."""
21
+ m = re.fullmatch(r"v?(\d+(?:\.\d+)*)", (text or "").strip())
22
+ return tuple(int(x) for x in m.group(1).split(".")) if m else None
23
+
24
+
25
+ def fetch_latest_tag(timeout: float = 5.0) -> str:
26
+ """The newest GitHub release tag. Raises OSError (URLError is one) when GitHub cannot be reached."""
27
+ req = urllib.request.Request(LATEST_URL, headers={"Accept": "application/vnd.github+json",
28
+ "User-Agent": "aisync-update"})
29
+ try:
30
+ with urllib.request.urlopen(req, timeout=timeout) as resp:
31
+ data = json.loads(resp.read().decode("utf-8"))
32
+ except (urllib.error.URLError, ValueError) as ex:
33
+ raise OSError(str(ex)) from ex
34
+ return str(data.get("tag_name", ""))
35
+
36
+
37
+ def install_method(module_file: str | Path, executable: str = "") -> str:
38
+ """Guess how this copy was installed from where its package lives."""
39
+ p = str(module_file).replace("\\", "/")
40
+ exe = str(executable).replace("\\", "/")
41
+ if "/uv/tools/" in p:
42
+ return "uv"
43
+ if "/pipx/venvs/" in p:
44
+ return "pipx"
45
+ if "/.local/share/aisync/venv/" in p:
46
+ return "installer"
47
+ if "/Cellar/" in p or "/homebrew/" in p.lower() or "/Homebrew/" in p or "/Cellar/" in exe:
48
+ return "brew"
49
+ return "pip"
50
+
51
+
52
+ UPGRADE_COMMANDS = {
53
+ "uv": "uv tool upgrade aisync-cli",
54
+ "pipx": "pipx upgrade aisync-cli",
55
+ "installer": "curl -fsSL https://maxont.github.io/aisync/install.sh | bash",
56
+ "brew": "brew upgrade aisync",
57
+ "pip": f"{sys.executable} -m pip install --upgrade aisync-cli",
58
+ }
59
+
60
+
61
+ def check(current: str, fetch: Callable[[], str], method: str) -> tuple[int, str]:
62
+ """Return (exit code, message). 0 = up to date or newer exists, 1 = could not check."""
63
+ try:
64
+ latest_text = fetch()
65
+ except OSError as ex:
66
+ return 1, f"could not check for updates ({ex}). aisync {current} is installed."
67
+ latest, cur = parse_version(latest_text), parse_version(current)
68
+ if latest is None or cur is None:
69
+ return 1, f"could not read the latest release ({latest_text!r}). aisync {current} is installed."
70
+ if latest <= cur:
71
+ return 0, f"aisync {current} is up to date (latest release: {latest_text})."
72
+ return 0, (f"aisync {current} -> {latest_text} is available.\n"
73
+ f"upgrade with: {UPGRADE_COMMANDS[method]}")
ai_sync/watch.py ADDED
@@ -0,0 +1,191 @@
1
+ """aisync watcher: event-triggered + debounced sync (no fixed 10-second polling).
2
+
3
+ What: waits on file events (kqueue on macOS/BSD, a cheap stat poll elsewhere) for the hub files, every tool's rule file and every
4
+ project's .git/logs/HEAD; runs one sync cycle after the stream goes quiet.
5
+ Why: an idle system costs nothing; a burst of edits becomes one commit, not dozens.
6
+ How: trailing debounce (`debounce_secs` after the last event), capped by `max_wait_secs` so a
7
+ busy stream still syncs, never faster than `min_flush_interval_secs`. Every
8
+ `reconcile_secs` a safety cycle runs in case an event was missed (it commits nothing
9
+ when nothing changed). Watches are re-opened every wake, so editors that save by
10
+ atomic rename are still followed. Create `<home>/STOP` to exit cleanly.
11
+ """
12
+ from __future__ import annotations
13
+
14
+ import os
15
+ import select
16
+ import sys
17
+ import time
18
+ from pathlib import Path
19
+
20
+ from . import core, webinbox
21
+
22
+ O_EVTONLY = getattr(os, "O_EVTONLY", 0x8000)
23
+ HEARTBEAT_SECS = 60
24
+
25
+
26
+ def watch_paths(cfg: core.Config) -> list[str]:
27
+ paths = [cfg.root / n for n in ("journal.jsonl", "preferences.md", "agents.md", "config.toml")]
28
+ paths += [t.path for t in cfg.targets]
29
+ paths += [p for p in (core.head_log(proj) for proj in core.project_list(cfg)) if p]
30
+ paths += [d for d in cfg.scan_dirs] # a new worktree/clone appearing is an event too
31
+ from . import collab
32
+ paths += collab.rule_source_files(cfg) # a rule written into a home-base AGENTS.md is an event
33
+ return [os.path.realpath(p) for p in paths if os.path.exists(p)]
34
+
35
+
36
+ def snapshot(paths: list[str]) -> dict:
37
+ out = {}
38
+ for p in paths:
39
+ try:
40
+ s = os.stat(p)
41
+ out[p] = (s.st_mtime_ns, s.st_size, s.st_ino)
42
+ except OSError:
43
+ out[p] = None
44
+ return out
45
+
46
+
47
+ def _wait_events(paths: list[str], timeout: float, since: dict | None = None) -> bool:
48
+ """Block until one of `paths` changes or `timeout` passes. True = something fired.
49
+ `since` = the caller's last snapshot: a change made after it but before the watches are armed counts too
50
+ (11/02: a write in that gap was missed until the next reconcile, 10 minutes later)."""
51
+ if not hasattr(select, "kqueue"): # non-macOS fallback: cheap stat poll
52
+ before = since if since is not None else snapshot(paths)
53
+ if snapshot(paths) != before:
54
+ return True
55
+ end = time.monotonic() + timeout
56
+ while time.monotonic() < end:
57
+ time.sleep(min(2.0, max(0.05, end - time.monotonic())))
58
+ if snapshot(paths) != before:
59
+ return True
60
+ return False
61
+ kq = select.kqueue()
62
+ fds, changes = [], []
63
+ flags = select.KQ_NOTE_WRITE | select.KQ_NOTE_EXTEND | select.KQ_NOTE_DELETE | \
64
+ select.KQ_NOTE_RENAME | select.KQ_NOTE_ATTRIB
65
+ try:
66
+ for p in paths:
67
+ try:
68
+ fd = os.open(p, O_EVTONLY)
69
+ except OSError:
70
+ continue
71
+ fds.append(fd)
72
+ changes.append(select.kevent(fd, filter=select.KQ_FILTER_VNODE,
73
+ flags=select.KQ_EV_ADD | select.KQ_EV_CLEAR, fflags=flags))
74
+ kq.control(changes, 0, 0) # arm first, then look for a change that came before arming
75
+ if since is not None and snapshot(paths) != since:
76
+ return True
77
+ return bool(kq.control(None, 64, max(0.0, timeout)))
78
+ finally:
79
+ for fd in fds:
80
+ os.close(fd)
81
+ kq.close()
82
+
83
+
84
+ def _beat(cfg: core.Config, state: str) -> None:
85
+ h = core.load_health(cfg)
86
+ h.update(watcher_pid=os.getpid(), watcher_heartbeat=core.now_iso(),
87
+ watcher_heartbeat_epoch=time.time(), watcher_state=state)
88
+ core.save_health(cfg, h)
89
+
90
+
91
+ CODE_DIR = Path(__file__).resolve().parent
92
+
93
+
94
+ def code_stamp(cfg: core.Config) -> float:
95
+ """Newest mtime of the ai_sync source and the hub config: a long-running watcher compares it every loop."""
96
+ files = list(CODE_DIR.glob("*.py")) + [cfg.root / "config.toml"]
97
+ return max((f.stat().st_mtime for f in files if f.exists()), default=0.0)
98
+
99
+
100
+ def under_test() -> bool:
101
+ return core.under_test()
102
+
103
+
104
+ def restart() -> None:
105
+ """Replace this process with a fresh watcher (same PID, so launchd keeps tracking it)."""
106
+ if under_test():
107
+ raise RuntimeError("watch.restart under a test: replace it with a mock")
108
+ os.execv(sys.executable, [sys.executable, "-m", "ai_sync", "watch"])
109
+
110
+
111
+ def run_watch(cfg: core.Config, max_cycles: int | None = None, max_seconds: float | None = None) -> int:
112
+ """The watcher once ran code and config from boot for days, so deployed fixes did nothing
113
+ and an old render kept writing the progress table back; it now restarts itself when either changes."""
114
+ started = time.monotonic()
115
+ cycles = 0
116
+ stamp = code_stamp(cfg)
117
+
118
+ def cycle(reason: str) -> None:
119
+ nonlocal cycles
120
+ if core.load_health(cfg).get("cycles_paused"):
121
+ core.log_run(cfg, {"ts": core.now_iso(), "level": "info", "message": f"cycle skipped ({reason}): paused"})
122
+ else:
123
+ core.run_cycle(cfg, reason)
124
+ cycles += 1
125
+
126
+ _beat(cfg, "starting")
127
+ cycle("startup")
128
+ paths = watch_paths(cfg)
129
+ snap = snapshot(paths)
130
+ next_web = time.monotonic() if cfg.web_inbox_secs > 0 else None
131
+ first_evt = last_evt = None
132
+ last_cycle = last_beat = time.monotonic()
133
+
134
+ while True:
135
+ now = time.monotonic()
136
+ if (cfg.root / "STOP").exists():
137
+ _beat(cfg, "stopped")
138
+ return 0
139
+ if code_stamp(cfg) != stamp:
140
+ core.log_run(cfg, {"ts": core.now_iso(), "level": "info", "message": "code or config changed: restarting"})
141
+ _beat(cfg, "restarting")
142
+ restart()
143
+ return 3 # only reached when restart is replaced (tests)
144
+ if max_cycles is not None and cycles >= max_cycles:
145
+ return 0
146
+ if max_seconds is not None and now - started >= max_seconds:
147
+ return 0
148
+
149
+ if last_evt is not None:
150
+ due = min(last_evt + cfg.debounce_secs, first_evt + cfg.max_wait_secs,)
151
+ due = max(due, last_cycle + cfg.min_flush_interval_secs)
152
+ timeout = due - now
153
+ else:
154
+ timeout = min(cfg.reconcile_secs - (now - last_cycle), HEARTBEAT_SECS)
155
+ if next_web is not None:
156
+ timeout = min(timeout, next_web - now)
157
+ if max_seconds is not None:
158
+ timeout = min(timeout, max_seconds - (now - started))
159
+
160
+ if timeout > 0 and _wait_events(paths, timeout, snap):
161
+ paths = watch_paths(cfg) # pick up new/renamed files
162
+ cur = snapshot(paths)
163
+ if cur != snap:
164
+ t = time.monotonic()
165
+ first_evt = first_evt or t
166
+ last_evt = t
167
+ snap = cur
168
+
169
+ now = time.monotonic()
170
+ if next_web is not None and now >= next_web:
171
+ if webinbox.poll(cfg).get("imported"): # journal grew: sync it like any other event
172
+ first_evt = first_evt or now
173
+ last_evt = now
174
+ next_web = time.monotonic() + webinbox.next_delay(cfg)
175
+ if now - last_beat >= HEARTBEAT_SECS:
176
+ _beat(cfg, "paused" if core.load_health(cfg).get("cycles_paused") else "watching")
177
+ last_beat = now
178
+ if last_evt is not None:
179
+ due = max(min(last_evt + cfg.debounce_secs, first_evt + cfg.max_wait_secs),
180
+ last_cycle + cfg.min_flush_interval_secs)
181
+ if now >= due:
182
+ cycle("event")
183
+ first_evt = last_evt = None
184
+ last_cycle = time.monotonic()
185
+ paths = watch_paths(cfg)
186
+ snap = snapshot(paths) # our own writes during the cycle are not new events
187
+ elif now - last_cycle >= cfg.reconcile_secs:
188
+ cycle("reconcile")
189
+ last_cycle = time.monotonic()
190
+ paths = watch_paths(cfg)
191
+ snap = snapshot(paths)
ai_sync/webinbox.py ADDED
@@ -0,0 +1,173 @@
1
+ """web-inbox importer: claude.ai (web) Claude events -> the local journal.
2
+
3
+ What: the web Claude cannot run `aisync log`, so it appends JSON lines to `events.jsonl` on the
4
+ `web-inbox` branch of the hub's GitHub repo. This module fetches that branch and logs every
5
+ line whose `web_id` was not imported yet, as the web agent the line names (`claude-web` or
6
+ `chatgpt`; anything else falls back to `claude-web` with refs.web_agent kept), with the original
7
+ project / kind / summary (same validation and hash chain as `aisync log`). A kind ai-sync
8
+ does not know (e.g. "milestone") is logged as `note` with refs.web_kind, never dropped.
9
+ Why: web-Claude work shows up in ai-sync within ~30 s instead of going through the mailbox.
10
+ How: `git fetch` into a private ref (refs/aisync/web-inbox) so main, the working tree and
11
+ origin/* are never touched, and the branch itself is only read. Imported web_ids live in
12
+ state/web_inbox.json; the journal's refs.web_id is also checked, so a lost state file
13
+ never causes duplicates. Fetch failures back off exponentially (30 s .. reconcile_secs)
14
+ and raise one alert after `fail_threshold` failures in a row; success resets both.
15
+ """
16
+ from __future__ import annotations
17
+
18
+ import contextlib
19
+ import fcntl
20
+ import json
21
+ import subprocess
22
+ import time
23
+
24
+ from . import core
25
+
26
+ AGENT = "claude-web" # default when a line names no known web agent
27
+ WEB_AGENTS = ("claude-web", "chatgpt") # web tools that write the inbox; local agents cannot be claimed
28
+ REF = "refs/aisync/web-inbox"
29
+ FILE = "events.jsonl"
30
+ SEEN_MAX = 20000
31
+
32
+
33
+ def _state_path(cfg: core.Config):
34
+ return cfg.state / "web_inbox.json"
35
+
36
+
37
+ def load_state(cfg: core.Config) -> dict:
38
+ return core.load_json(_state_path(cfg), {"seen": {}, "rejected": {}, "failures": 0, "runs": 0,
39
+ "imported_total": 0})
40
+
41
+
42
+ @contextlib.contextmanager
43
+ def _import_lock(cfg: core.Config):
44
+ """The watcher and a manual `aisync web-inbox` must not import the same line twice."""
45
+ cfg.state.mkdir(parents=True, exist_ok=True)
46
+ with open(cfg.state / ".web_inbox.lock", "a") as fh:
47
+ fcntl.flock(fh, fcntl.LOCK_EX)
48
+ try:
49
+ yield
50
+ finally:
51
+ fcntl.flock(fh, fcntl.LOCK_UN)
52
+
53
+
54
+ def fetch(cfg: core.Config) -> None:
55
+ """Read-only: fetch the branch into our own ref. Retries with backoff (core.git_run)."""
56
+ core.ensure_repo(cfg)
57
+ core.git_run(cfg, ["fetch", "--quiet", "--no-tags", "--no-write-fetch-head", cfg.web_inbox_remote,
58
+ f"+refs/heads/{cfg.web_inbox_branch}:{REF}"], retries=cfg.git_retries)
59
+
60
+
61
+ def read_lines(cfg: core.Config) -> list[str]:
62
+ r = core._git(["show", f"{REF}:{FILE}"], cfg.root, cfg.git_timeout_secs)
63
+ if r.returncode != 0:
64
+ raise core.GitError(f"git show {REF}:{FILE}: {(r.stderr or r.stdout).strip()[:300]}")
65
+ return r.stdout.splitlines()
66
+
67
+
68
+ def _parse(line: str) -> tuple[dict | None, str]:
69
+ """-> (event, "") or (None, reason)."""
70
+ try:
71
+ e = json.loads(line)
72
+ except json.JSONDecodeError:
73
+ return None, "not JSON"
74
+ if not isinstance(e, dict):
75
+ return None, "not an object"
76
+ if not str(e.get("web_id") or "").strip():
77
+ return None, "no web_id"
78
+ if not str(e.get("summary") or "").strip():
79
+ return None, "empty summary"
80
+ return e, ""
81
+
82
+
83
+ def import_lines(cfg: core.Config, lines: list[str]) -> dict:
84
+ """Log every not-yet-imported line. Returns per-run counts (also the metrics record)."""
85
+ rep = {"lines": 0, "imported": [], "already": 0, "rejected": [], "kind_mapped": []}
86
+ with _import_lock(cfg):
87
+ st = load_state(cfg)
88
+ seen = dict(st.get("seen", {}))
89
+ for ev in core.read_events(cfg): # belt and braces: the journal itself
90
+ wid = (ev.get("refs") or {}).get("web_id")
91
+ if wid and wid not in seen:
92
+ seen[wid] = ev.get("id", "")
93
+ for line in lines:
94
+ if not line.strip():
95
+ continue
96
+ rep["lines"] += 1
97
+ e, why = _parse(line)
98
+ if e is None:
99
+ bad = st.setdefault("rejected", {})
100
+ if core._h(line) not in bad: # report a bad line once, not every 30 s
101
+ bad[core._h(line)] = why
102
+ rep["rejected"].append(why)
103
+ continue
104
+ wid = str(e["web_id"]).strip()
105
+ if wid in seen:
106
+ rep["already"] += 1
107
+ continue
108
+ raw_refs = e.get("refs")
109
+ refs = {k: str(v) for k, v in raw_refs.items()} if isinstance(raw_refs, dict) else {}
110
+ if isinstance(raw_refs, list) and raw_refs: # ChatGPT writes refs as a list: keep it, do not drop it
111
+ refs["web_refs"] = " | ".join(str(r) for r in raw_refs)
112
+ agent = str(e.get("agent") or "").strip()
113
+ if agent not in WEB_AGENTS:
114
+ if agent:
115
+ refs["web_agent"] = agent
116
+ agent = AGENT
117
+ refs.update(web_id=wid, web_ts=str(e.get("ts", "")))
118
+ kind = e.get("kind") or "note"
119
+ if kind not in core.KINDS: # e.g. "milestone": keep the event, log it as note
120
+ refs["web_kind"] = str(kind)
121
+ rep["kind_mapped"].append(f"{wid}: {kind} -> note")
122
+ kind = "note"
123
+ files = e.get("files") if isinstance(e.get("files"), list) else []
124
+ new = core.append_event(cfg, agent=agent, kind=kind, summary=str(e["summary"]),
125
+ project=str(e.get("project") or ""), files=files, refs=refs)
126
+ seen[wid] = new["id"]
127
+ rep["imported"].append({"web_id": wid, "id": new["id"], "agent": agent})
128
+ # save after every line: a crash mid-run never re-imports what is already logged
129
+ st["seen"] = dict(list(seen.items())[-SEEN_MAX:])
130
+ core.save_json(_state_path(cfg), st)
131
+ st["seen"] = dict(list(seen.items())[-SEEN_MAX:])
132
+ st["imported_total"] = st.get("imported_total", 0) + len(rep["imported"])
133
+ core.save_json(_state_path(cfg), st)
134
+ return rep
135
+
136
+
137
+ def poll(cfg: core.Config, do_fetch: bool = True) -> dict:
138
+ """One import run: fetch + import + metrics/health. Never raises (the watcher calls it)."""
139
+ t0 = time.monotonic()
140
+ rec = {"ts": core.now_iso(), "web_inbox": True, "ok": True}
141
+ try:
142
+ if do_fetch:
143
+ fetch(cfg)
144
+ rec.update(import_lines(cfg, read_lines(cfg)))
145
+ except (core.GitError, OSError, subprocess.SubprocessError, ValueError) as ex:
146
+ rec.update(ok=False, error=f"{type(ex).__name__}: {ex}"[:400])
147
+ rec["ms"] = round((time.monotonic() - t0) * 1000)
148
+
149
+ st = load_state(cfg)
150
+ st["runs"] = st.get("runs", 0) + 1
151
+ st["last_run"] = rec["ts"]
152
+ if rec["ok"]:
153
+ st.update(failures=0, last_ok=rec["ts"], alerted=False)
154
+ else:
155
+ st["failures"] = st.get("failures", 0) + 1
156
+ st["last_error"] = rec["error"]
157
+ if st["failures"] >= cfg.fail_threshold and not st.get("alerted"):
158
+ st["alerted"] = True
159
+ core.alert(cfg, "web-inbox import failing",
160
+ f"{st['failures']} fetches in a row failed: {rec['error']}. Still retrying with backoff; "
161
+ f"run `aisync web-inbox` to see the error.")
162
+ core.save_json(_state_path(cfg), st)
163
+ rec["failures"] = st["failures"]
164
+ # quiet runs (nothing new, no error) are not logged: 2880 identical lines a day help nobody
165
+ if not rec["ok"] or rec.get("imported") or rec.get("rejected"):
166
+ core.log_run(cfg, {**rec, "level": "info" if rec["ok"] else "error"})
167
+ return rec
168
+
169
+
170
+ def next_delay(cfg: core.Config) -> float:
171
+ """Seconds until the next poll: web_inbox_secs, doubled per consecutive failure, capped."""
172
+ fails = load_state(cfg).get("failures", 0)
173
+ return min(cfg.web_inbox_secs * 2 ** min(fails, 10), max(cfg.web_inbox_secs, cfg.reconcile_secs))