|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Opt-in Claude Code SessionStart notifier for MedSci Skills. |
| 3 | +
|
| 4 | +Prints a single `{"systemMessage": ...}` line when a newer release exists, or nothing. It is OFF by |
| 5 | +default — registered into ~/.claude/settings.json only on explicit opt-in |
| 6 | +(`install.py --enable-update-notify`) and removed by `install.py --disable-update-notify`. |
| 7 | +
|
| 8 | +Privacy & safety, by construction: |
| 9 | + * Does NOT read the SessionStart stdin — your cwd, transcript path, and session id are never read |
| 10 | + or transmitted. No telemetry, no analytics, no unique install id. |
| 11 | + * The only network call is a single GitHub version GET, and only when the shared 24h cache is |
| 12 | + stale. It uses a short timeout and exits SILENTLY on any error/timeout, so it never delays or |
| 13 | + blocks a session. |
| 14 | + * Honors `MEDSCI_NO_UPDATE_CHECK=1` (silent, no network). |
| 15 | + * Surfaces via `systemMessage` (shown to you), not stdout context — it adds nothing to the model's |
| 16 | + context. |
| 17 | +
|
| 18 | +Lives next to update.py / medsci_txn.py in ~/.medsci-skills/updater/ and reuses their cached, |
| 19 | +clock-sane version check. |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import json |
| 25 | +import os |
| 26 | +import sys |
| 27 | +from pathlib import Path |
| 28 | + |
| 29 | +sys.path.insert(0, str(Path(__file__).resolve().parent)) |
| 30 | +import medsci_txn # noqa: E402 |
| 31 | +import update # noqa: E402 |
| 32 | + |
| 33 | +HOOK_HTTP_TIMEOUT = 4 # keep SessionStart snappy; only the rare cache-miss path waits at all |
| 34 | + |
| 35 | + |
| 36 | +def main() -> int: |
| 37 | + # Never read stdin (privacy). Never raise. Worst case: print nothing. |
| 38 | + if os.environ.get("MEDSCI_NO_UPDATE_CHECK"): |
| 39 | + return 0 |
| 40 | + try: |
| 41 | + home = medsci_txn.state_home() |
| 42 | + inst = update.installed_version(home) |
| 43 | + inst_v = update.parse_semver(inst) |
| 44 | + if inst_v is None: |
| 45 | + return 0 # not installed via the transactional installer -> say nothing |
| 46 | + |
| 47 | + tag = update._cache_fresh(home) # clock-sane 24h cache (shared with --check-update) |
| 48 | + if tag is None: |
| 49 | + try: |
| 50 | + tag = update.resolve_latest_tag( |
| 51 | + lambda u: update._real_get_json(u, timeout=HOOK_HTTP_TIMEOUT) |
| 52 | + ) |
| 53 | + update._cache_store(home, tag) |
| 54 | + except Exception: # noqa: BLE001 - offline / slow / API error -> stay silent, never block |
| 55 | + return 0 |
| 56 | + |
| 57 | + latest_v = update.parse_semver(tag[1:] if tag.startswith("v") else tag) |
| 58 | + if latest_v and latest_v > inst_v: |
| 59 | + msg = ( |
| 60 | + f"MedSci Skills update available — installed {inst}, latest {tag}. " |
| 61 | + f"Run the updater in ~/.medsci-skills/updater/ (or the 'Update MedSci Skills' Desktop " |
| 62 | + f"launcher) to update. Set MEDSCI_NO_UPDATE_CHECK=1 to silence this notice." |
| 63 | + ) |
| 64 | + print(json.dumps({"systemMessage": msg, "suppressOutput": True})) |
| 65 | + except Exception: # noqa: BLE001 - a notifier must never disrupt a session |
| 66 | + return 0 |
| 67 | + return 0 |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + raise SystemExit(main()) |
0 commit comments