readthrough 0.4.0__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.
Files changed (73) hide show
  1. readthrough/__init__.py +8 -0
  2. readthrough/__main__.py +3 -0
  3. readthrough/agents.py +344 -0
  4. readthrough/analyze.py +537 -0
  5. readthrough/assets/SKILL.md +86 -0
  6. readthrough/assets/defaults.toml +143 -0
  7. readthrough/assets/profile-reference.md +133 -0
  8. readthrough/assets/vendor/README.md +22 -0
  9. readthrough/assets/vendor/cose-base.js +3214 -0
  10. readthrough/assets/vendor/cytoscape-fcose.js +1549 -0
  11. readthrough/assets/vendor/cytoscape.min.js +31 -0
  12. readthrough/assets/vendor/layout-base.js +5230 -0
  13. readthrough/assets/vendor/licenses/cose-base-LICENSE +21 -0
  14. readthrough/assets/vendor/licenses/cytoscape-LICENSE +19 -0
  15. readthrough/assets/vendor/licenses/cytoscape-fcose-LICENSE +19 -0
  16. readthrough/assets/vendor/licenses/dompurify-LICENSE +202 -0
  17. readthrough/assets/vendor/licenses/layout-base-LICENSE +21 -0
  18. readthrough/assets/vendor/licenses/lucide-LICENSE +15 -0
  19. readthrough/assets/vendor/licenses/marked-LICENSE +44 -0
  20. readthrough/assets/vendor/lucide-icons.js +36 -0
  21. readthrough/assets/vendor/marked.umd.js +82 -0
  22. readthrough/assets/vendor/purify.min.js +7 -0
  23. readthrough/assets/web/dashboard.css +202 -0
  24. readthrough/assets/web/dashboard.html +48 -0
  25. readthrough/assets/web/dashboard.js +937 -0
  26. readthrough/assets/web/setup.css +57 -0
  27. readthrough/assets/web/setup.html +28 -0
  28. readthrough/assets/web/setup.js +342 -0
  29. readthrough/assets/web/theme.css +36 -0
  30. readthrough/assistants.py +699 -0
  31. readthrough/brief.py +145 -0
  32. readthrough/build.py +616 -0
  33. readthrough/ci.py +107 -0
  34. readthrough/cli.py +958 -0
  35. readthrough/config.py +533 -0
  36. readthrough/contract.py +394 -0
  37. readthrough/engine.py +230 -0
  38. readthrough/faults.py +92 -0
  39. readthrough/files.py +110 -0
  40. readthrough/forges/__init__.py +614 -0
  41. readthrough/forges/azure_devops.py +658 -0
  42. readthrough/forges/forgejo.py +300 -0
  43. readthrough/forges/github.py +487 -0
  44. readthrough/forges/gitlab.py +335 -0
  45. readthrough/ledger.py +61 -0
  46. readthrough/limits.py +26 -0
  47. readthrough/logs.py +169 -0
  48. readthrough/membership.py +76 -0
  49. readthrough/model.py +177 -0
  50. readthrough/onboard.py +551 -0
  51. readthrough/onboard_model.py +319 -0
  52. readthrough/process.py +169 -0
  53. readthrough/profile.py +699 -0
  54. readthrough/py.typed +0 -0
  55. readthrough/registry.py +157 -0
  56. readthrough/review.py +394 -0
  57. readthrough/rules.py +111 -0
  58. readthrough/runtimes/__init__.py +297 -0
  59. readthrough/runtimes/anthropic.py +278 -0
  60. readthrough/runtimes/copilot.py +313 -0
  61. readthrough/runtimes/openai.py +307 -0
  62. readthrough/serve.py +817 -0
  63. readthrough/study.py +327 -0
  64. readthrough/system.py +205 -0
  65. readthrough/terminals.py +168 -0
  66. readthrough/tuning.py +266 -0
  67. readthrough/usage.py +169 -0
  68. readthrough/web_setup.py +635 -0
  69. readthrough-0.4.0.dist-info/METADATA +123 -0
  70. readthrough-0.4.0.dist-info/RECORD +73 -0
  71. readthrough-0.4.0.dist-info/WHEEL +4 -0
  72. readthrough-0.4.0.dist-info/entry_points.txt +2 -0
  73. readthrough-0.4.0.dist-info/licenses/LICENSE +21 -0
readthrough/faults.py ADDED
@@ -0,0 +1,92 @@
1
+ """How calls to the outside fail, classified so the caller knows what the failure means.
2
+
3
+ One hierarchy for every forge and for ``git``: the build retries what's temporary, treats only ``NotFound``
4
+ as "absent", and turns everything else into a warning with the reason. Classification works from a
5
+ CLI's stderr (``gh``, ``glab``, ``az``) or from an HTTP status, so adapters on either transport raise the
6
+ same errors.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import re
12
+
13
+
14
+ class ForgeError(RuntimeError):
15
+ """A failed call to a forge or to ``git``. Subclasses say why, and whether trying again could help."""
16
+
17
+ retryable = False
18
+ kind = "error"
19
+
20
+ def __init__(
21
+ self, message: str, *, what: str = "", stderr: str = "", returncode: int | None = None,
22
+ headers: dict[str, str] | None = None,
23
+ ) -> None: # fmt: skip
24
+ super().__init__(message)
25
+ self.what, self.stderr, self.returncode = what, stderr, returncode
26
+ self.headers = headers or {} # an HTTP refusal's response headers (lower-cased): some say how to sign in
27
+
28
+ @property
29
+ def reason(self) -> str:
30
+ """The first meaningful line of the error output, for one-line logs."""
31
+ lines = [line.strip() for line in self.stderr.splitlines() if line.strip()]
32
+ return (lines[0] if lines else str(self))[:300]
33
+
34
+
35
+ class NetworkError(ForgeError):
36
+ retryable, kind = True, "network"
37
+
38
+
39
+ class Timeout(NetworkError):
40
+ kind = "timeout"
41
+
42
+
43
+ class RateLimited(ForgeError):
44
+ retryable, kind = True, "rate-limit"
45
+ retry_after: float | None = None # seconds the forge asked us to wait, when it said
46
+
47
+
48
+ class AuthError(ForgeError):
49
+ kind = "auth"
50
+
51
+
52
+ class NotFound(ForgeError):
53
+ kind = "not-found"
54
+
55
+
56
+ class TooLarge(ForgeError):
57
+ kind = "too-large"
58
+
59
+
60
+ _STDERR_PATTERNS: list[tuple[type[ForgeError], re.Pattern[str]]] = [
61
+ (RateLimited, re.compile(r"HTTP 429|rate limit|abuse detection", re.IGNORECASE)),
62
+ (TooLarge, re.compile(r"HTTP 406|exceeded the maximum number of|diff is taking too long|too large to", re.IGNORECASE)),
63
+ (NotFound, re.compile(r"HTTP 404|Not Found|Could not resolve to a|no pull requests? found", re.IGNORECASE)),
64
+ (AuthError, re.compile(r"HTTP 401|HTTP 403|Bad credentials|gh auth login|glab auth login|az login|SAML|single sign-on|must authenticate", re.IGNORECASE)),
65
+ (NetworkError, re.compile(
66
+ r"error connecting to|connection (?:reset|refused)|forcibly closed|actively refused|proxyconnect|dial tcp|"
67
+ r"connectex|network is unreachable|no route to host|wsarecv|wsasend|broken pipe|i/o timeout|"
68
+ r"timeout awaiting|TLS handshake|unexpected EOF|\bEOF\b|no such host|HTTP 50[0234]|Bad Gateway|"
69
+ r"Service Unavailable|Gateway Time-?out|Something went wrong while executing|stream error", re.IGNORECASE)),
70
+ ] # fmt: skip
71
+
72
+
73
+ def classify_stderr(stderr: str) -> type[ForgeError]:
74
+ for cls, pattern in _STDERR_PATTERNS:
75
+ if pattern.search(stderr):
76
+ return cls
77
+ return ForgeError
78
+
79
+
80
+ def classify_http(status: int, body: str = "") -> type[ForgeError]:
81
+ """The error class for an HTTP status. 403 with a rate-limit body is a rate limit, not an auth failure."""
82
+ if status == 429 or (status == 403 and re.search(r"rate limit", body, re.IGNORECASE)):
83
+ return RateLimited
84
+ if status in (401, 403):
85
+ return AuthError
86
+ if status == 404:
87
+ return NotFound
88
+ if status in (406, 413, 422) and re.search(r"too large|exceed|diff", body, re.IGNORECASE):
89
+ return TooLarge
90
+ if status >= 500 or status == 408:
91
+ return NetworkError
92
+ return ForgeError
readthrough/files.py ADDED
@@ -0,0 +1,110 @@
1
+ """Small filesystem helpers shared by the build, the ledger, the study log, and the live server."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import os
7
+ import sys
8
+ import tempfile
9
+ import time
10
+ from collections.abc import Generator
11
+ from pathlib import Path
12
+
13
+ LOCK_TIMEOUT_SECONDS = 10.0
14
+ LOCK_POLL_SECONDS = 0.02
15
+
16
+
17
+ def inside(root: Path, rel: str) -> Path:
18
+ """The file ``rel`` names under ``root``, once symlinks and ``..`` are followed; refused if it leads outside.
19
+
20
+ For paths a model asks for: a repository can commit a symlink, and the name being in the tree says nothing
21
+ about where it points.
22
+ """
23
+ base = root.resolve()
24
+ target = (root / rel).resolve()
25
+ if not target.is_relative_to(base):
26
+ raise ValueError(f"{rel} is outside {root}")
27
+ return target
28
+
29
+
30
+ @contextlib.contextmanager
31
+ def locked(path: Path, *, timeout: float = LOCK_TIMEOUT_SECONDS) -> Generator[None]:
32
+ """Hold an exclusive lock on ``path`` across threads and processes while a read-modify-write runs.
33
+
34
+ The live server, the CLI (``readthrough study N --note`` from a review agent's terminal), and a second
35
+ server thread can all append to the same study log or rewrite the same ledger at once; without this, two
36
+ writers both read the same state and one's update is lost (or two log entries get the same id). The lock is
37
+ a sidecar ``<name>.lock`` file, since the data file itself is replaced atomically on write; the OS releases
38
+ it if the holder dies. Waiting longer than ``timeout`` raises ``TimeoutError`` rather than hanging a request.
39
+ """
40
+ lock_path = path.with_name(path.name + ".lock")
41
+ lock_path.parent.mkdir(parents=True, exist_ok=True)
42
+ fd = os.open(lock_path, os.O_RDWR | os.O_CREAT, 0o644)
43
+ try:
44
+ deadline = time.monotonic() + timeout
45
+ while True:
46
+ try:
47
+ _try_lock(fd)
48
+ break
49
+ except OSError:
50
+ if time.monotonic() >= deadline:
51
+ raise TimeoutError(f"another readthrough is still writing {path.name}; try again") from None
52
+ time.sleep(LOCK_POLL_SECONDS)
53
+ try:
54
+ yield
55
+ finally:
56
+ _unlock(fd)
57
+ finally:
58
+ os.close(fd)
59
+
60
+
61
+ if sys.platform == "win32":
62
+ import msvcrt
63
+
64
+ def _try_lock(fd: int) -> None:
65
+ os.lseek(fd, 0, os.SEEK_SET)
66
+ msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
67
+
68
+ def _unlock(fd: int) -> None:
69
+ os.lseek(fd, 0, os.SEEK_SET)
70
+ msvcrt.locking(fd, msvcrt.LK_UNLCK, 1)
71
+
72
+ else:
73
+ import fcntl
74
+
75
+ def _try_lock(fd: int) -> None:
76
+ fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
77
+
78
+ def _unlock(fd: int) -> None:
79
+ fcntl.flock(fd, fcntl.LOCK_UN)
80
+
81
+
82
+ def write_atomic(path: Path, text: str) -> None:
83
+ """Write via a temp file and rename, so a reader (the live server) never sees a half-written file."""
84
+ path.parent.mkdir(parents=True, exist_ok=True)
85
+ fd, name = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.", suffix=".tmp")
86
+ tmp = Path(name)
87
+ try:
88
+ with os.fdopen(fd, "w", encoding="utf-8", newline="") as handle:
89
+ handle.write(text)
90
+ tmp.replace(path)
91
+ except BaseException:
92
+ tmp.unlink(missing_ok=True)
93
+ raise
94
+
95
+
96
+ def mtime(path: Path) -> float:
97
+ try:
98
+ return path.stat().st_mtime
99
+ except FileNotFoundError:
100
+ return 0.0
101
+
102
+
103
+ def stamp(path: Path) -> tuple[float, int]:
104
+ """(mtime, size): what a watcher compares to notice a change. The size catches an append that lands within
105
+ the same timestamp tick as the one before it, which coarse filesystem clocks make common."""
106
+ try:
107
+ st = path.stat()
108
+ except FileNotFoundError:
109
+ return (0.0, 0)
110
+ return (st.st_mtime, st.st_size)