puremethod 0.1.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 (171) hide show
  1. app/__init__.py +2 -0
  2. app/__main__.py +31 -0
  3. app/_web/404/index.html +1 -0
  4. app/_web/404.html +1 -0
  5. app/_web/__next.!KGNvbnNvbGUp.__PAGE__.txt +42 -0
  6. app/_web/__next._full.txt +36 -0
  7. app/_web/__next._tree.txt +4 -0
  8. app/_web/_next/static/6az3RZxafySyERvJdZAgV/_buildManifest.js +11 -0
  9. app/_web/_next/static/6az3RZxafySyERvJdZAgV/_clientMiddlewareManifest.js +1 -0
  10. app/_web/_next/static/6az3RZxafySyERvJdZAgV/_ssgManifest.js +1 -0
  11. app/_web/_next/static/chunks/00fylloju-1x2.js +1 -0
  12. app/_web/_next/static/chunks/03hxwfej4sm9g.js +1 -0
  13. app/_web/_next/static/chunks/0cz1d0mv5g_q7.js +1 -0
  14. app/_web/_next/static/chunks/0gus9jro9n3j3.js +1 -0
  15. app/_web/_next/static/chunks/0n12a63ieedt9.js +1 -0
  16. app/_web/_next/static/chunks/0ns7paypbayry.js +31 -0
  17. app/_web/_next/static/chunks/0s1d7y-_wn8wn.js +1 -0
  18. app/_web/_next/static/chunks/12s40azz56b3b.js +1 -0
  19. app/_web/_next/static/chunks/1b_14wnek5geq.js +1 -0
  20. app/_web/_next/static/chunks/1oderptqhsaox.css +1 -0
  21. app/_web/_next/static/chunks/1rcnm-dleh7p6.js +1 -0
  22. app/_web/_next/static/chunks/1u4klctqx_w0w.js +1 -0
  23. app/_web/_next/static/chunks/1vahkxwiu_bqp.js +22 -0
  24. app/_web/_next/static/chunks/26mp4u1wew2xu.js +1 -0
  25. app/_web/_next/static/chunks/2aq880yo-2z88.js +1 -0
  26. app/_web/_next/static/chunks/2f5ovhdilzkal.js +1 -0
  27. app/_web/_next/static/chunks/2qds0qd18u74f.js +1 -0
  28. app/_web/_next/static/chunks/2qgmxvbiean_r.js +1 -0
  29. app/_web/_next/static/chunks/3b5v1mle2gmac.js +1 -0
  30. app/_web/_next/static/chunks/3hw8x3h98ysh4.js +6 -0
  31. app/_web/_next/static/chunks/3op5__grw4752.js +1 -0
  32. app/_web/_next/static/chunks/3scuzzds0vtp7.js +1 -0
  33. app/_web/_next/static/chunks/3v_5wdpvvcxz8.js +1 -0
  34. app/_web/_next/static/chunks/3wevuf_oyh97l.js +1 -0
  35. app/_web/_next/static/chunks/turbopack-3-ry682tkyiqk.js +1 -0
  36. app/_web/_not-found/__next._full.txt +24 -0
  37. app/_web/_not-found/__next._not-found.__PAGE__.txt +30 -0
  38. app/_web/_not-found/__next._tree.txt +4 -0
  39. app/_web/_not-found/index.html +1 -0
  40. app/_web/_not-found/index.txt +24 -0
  41. app/_web/credentials/__next.!KGNvbnNvbGUp.credentials.__PAGE__.txt +46 -0
  42. app/_web/credentials/__next._full.txt +39 -0
  43. app/_web/credentials/__next._tree.txt +5 -0
  44. app/_web/credentials/index.html +1 -0
  45. app/_web/credentials/index.txt +39 -0
  46. app/_web/executors/__next.!KGNvbnNvbGUp.executors.__PAGE__.txt +46 -0
  47. app/_web/executors/__next._full.txt +39 -0
  48. app/_web/executors/__next._tree.txt +5 -0
  49. app/_web/executors/index.html +1 -0
  50. app/_web/executors/index.txt +39 -0
  51. app/_web/index.html +1 -0
  52. app/_web/index.txt +36 -0
  53. app/_web/new/__next.!KGNvbnNvbGUp.new.__PAGE__.txt +46 -0
  54. app/_web/new/__next._full.txt +39 -0
  55. app/_web/new/__next._tree.txt +5 -0
  56. app/_web/new/index.html +1 -0
  57. app/_web/new/index.txt +39 -0
  58. app/_web/profiles/__next.!KGNvbnNvbGUp.profiles.__PAGE__.txt +46 -0
  59. app/_web/profiles/__next._full.txt +39 -0
  60. app/_web/profiles/__next._tree.txt +5 -0
  61. app/_web/profiles/index.html +1 -0
  62. app/_web/profiles/index.txt +39 -0
  63. app/_web/run/__next.!KGNvbnNvbGUp.run.__PAGE__.txt +47 -0
  64. app/_web/run/__next._full.txt +40 -0
  65. app/_web/run/__next._tree.txt +5 -0
  66. app/_web/run/index.html +1 -0
  67. app/_web/run/index.txt +40 -0
  68. app/_web/services/__next.!KGNvbnNvbGUp.services.__PAGE__.txt +46 -0
  69. app/_web/services/__next._full.txt +39 -0
  70. app/_web/services/__next._tree.txt +5 -0
  71. app/_web/services/index.html +1 -0
  72. app/_web/services/index.txt +39 -0
  73. app/_web/setup/__next.!KGNvbnNvbGUp.setup.__PAGE__.txt +46 -0
  74. app/_web/setup/__next._full.txt +39 -0
  75. app/_web/setup/__next._tree.txt +5 -0
  76. app/_web/setup/index.html +1 -0
  77. app/_web/setup/index.txt +39 -0
  78. app/_web/signin/__next._full.txt +30 -0
  79. app/_web/signin/__next._tree.txt +4 -0
  80. app/_web/signin/__next.signin.__PAGE__.txt +36 -0
  81. app/_web/signin/index.html +1 -0
  82. app/_web/signin/index.txt +30 -0
  83. app/accounts.py +137 -0
  84. app/addresses.py +30 -0
  85. app/api/__init__.py +61 -0
  86. app/api/auth_routes.py +54 -0
  87. app/api/common.py +102 -0
  88. app/api/config_routes.py +562 -0
  89. app/api/links.py +22 -0
  90. app/api/run_routes.py +311 -0
  91. app/api/setup_routes.py +267 -0
  92. app/bundled.py +521 -0
  93. app/compose.py +229 -0
  94. app/db.py +186 -0
  95. app/executor_config.py +95 -0
  96. app/keys.py +29 -0
  97. app/launcher.py +389 -0
  98. app/leases.py +40 -0
  99. app/literature_sources.py +131 -0
  100. app/model_endpoints.py +104 -0
  101. app/model_search.py +252 -0
  102. app/outputs.py +77 -0
  103. app/paths.py +93 -0
  104. app/readonly.py +70 -0
  105. app/repo.py +746 -0
  106. app/scan.py +65 -0
  107. app/server.py +24 -0
  108. app/settings.py +125 -0
  109. app/setup.py +117 -0
  110. app/ssh_chain.py +102 -0
  111. app/ssh_hosts.py +244 -0
  112. app/ssh_machines.py +101 -0
  113. app/statuses.py +4 -0
  114. app/supervisor.py +87 -0
  115. app/tunnels.py +222 -0
  116. app/unseal.py +27 -0
  117. app/worker.py +657 -0
  118. executor/__init__.py +47 -0
  119. executor/base.py +161 -0
  120. executor/common.py +139 -0
  121. executor/local_docker.py +239 -0
  122. executor/local_process.py +247 -0
  123. executor/ssh.py +481 -0
  124. executor/types.py +128 -0
  125. executor/watchdog.py +108 -0
  126. ledger/__init__.py +46 -0
  127. ledger/artifacts.py +62 -0
  128. ledger/canonical.py +48 -0
  129. ledger/core.py +337 -0
  130. ledger/errors.py +36 -0
  131. ledger/model.py +107 -0
  132. ledger/payloads.py +228 -0
  133. ledger/rules.py +204 -0
  134. ledger/storage.py +226 -0
  135. llm/__init__.py +45 -0
  136. llm/backends/__init__.py +0 -0
  137. llm/backends/anthropic_backend.py +145 -0
  138. llm/backends/base.py +87 -0
  139. llm/backends/openai_backend.py +211 -0
  140. llm/core.py +416 -0
  141. llm/prices.py +98 -0
  142. llm/tokens.py +18 -0
  143. llm/types.py +106 -0
  144. loop/__init__.py +19 -0
  145. loop/config.py +108 -0
  146. loop/core.py +1160 -0
  147. loop/decide.py +61 -0
  148. loop/harness.py +132 -0
  149. loop/prompts.py +88 -0
  150. loop/view.py +202 -0
  151. puremethod-0.1.0.dist-info/METADATA +79 -0
  152. puremethod-0.1.0.dist-info/RECORD +171 -0
  153. puremethod-0.1.0.dist-info/WHEEL +4 -0
  154. puremethod-0.1.0.dist-info/entry_points.txt +2 -0
  155. puremethod-0.1.0.dist-info/licenses/LICENSE +96 -0
  156. puremethod-0.1.0.dist-info/licenses/THIRD_PARTY_NOTICES.md +173 -0
  157. purescholar/__init__.py +43 -0
  158. purescholar/client.py +380 -0
  159. purescholar/types.py +164 -0
  160. puretrace/__init__.py +49 -0
  161. puretrace/client.py +320 -0
  162. puretrace/types.py +166 -0
  163. writer/__init__.py +8 -0
  164. writer/core.py +430 -0
  165. writer/facts.py +162 -0
  166. writer/latex.py +62 -0
  167. writer/lint.py +85 -0
  168. writer/macros.py +114 -0
  169. writer/render.py +120 -0
  170. writer/tectonic.py +79 -0
  171. writer/types.py +61 -0
@@ -0,0 +1,247 @@
1
+ """`local-process` backend: isolation `none` (05-executor.md §6). Runs only with the run's
2
+ explicit opt-in (SYS-INV-6), which `loop` checks at preflight.
3
+
4
+ Each job runs under the watchdog (`watchdog.py`), a separate detached process that enforces
5
+ the deadline and kills the whole process tree, even if the engine dies (INV-2, INV-3).
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import json
11
+ import os
12
+ import shutil
13
+ import subprocess
14
+ import sys
15
+ import time
16
+ from dataclasses import dataclass
17
+ from pathlib import Path
18
+ from typing import Any
19
+
20
+ from .base import CredentialLookup, Executor
21
+ from .common import TAG_FILE, check_tag, cost, minimal_env, read_outputs, redact, unpack
22
+ from .common import classify as classify_status
23
+ from .types import (
24
+ METRICS_FILE,
25
+ EnvRef,
26
+ EnvSpec,
27
+ ExecutorInfo,
28
+ JobResult,
29
+ JobSpec,
30
+ PreflightReport,
31
+ Resources,
32
+ )
33
+
34
+
35
+ @dataclass
36
+ class _Proc:
37
+ job_dir: Path
38
+ watchdog: subprocess.Popen[bytes]
39
+ deadline_s: float
40
+
41
+
42
+ class LocalProcessExecutor(Executor):
43
+ def __init__(self, credentials: CredentialLookup, *, executor_id: str = "local-process",
44
+ root: str | Path, concurrency: int = 1,
45
+ rate_usd_per_hour: float | None = None, grace_s: float = 30.0,
46
+ python: str = sys.executable) -> None:
47
+ super().__init__(credentials)
48
+ self._info = ExecutorInfo(id=executor_id, kind="local-process", isolation="none",
49
+ network="on", concurrency=concurrency,
50
+ resources=Resources(), rate_usd_per_hour=rate_usd_per_hour)
51
+ self._root = Path(root)
52
+ self._grace = grace_s
53
+ self._python = python
54
+
55
+ def describe(self) -> ExecutorInfo:
56
+ return self._info
57
+
58
+ def preflight(self) -> PreflightReport:
59
+ problems: list[str] = []
60
+ try:
61
+ self._root.mkdir(parents=True, exist_ok=True)
62
+ except OSError as exc:
63
+ problems.append(f"root not writable: {exc}")
64
+ return PreflightReport(False, problems)
65
+ env = self.build_env(EnvSpec(base_image=None))
66
+ if env.status != "ok":
67
+ return PreflightReport(False, [f"environment: {env.log}"])
68
+ code = _tar({"main.py": b"print('ok')\n"})
69
+ handle = self.submit(JobSpec(code=code, entrypoint="python main.py", env=env, seed=0,
70
+ deadline_s=60))
71
+ deadline = time.monotonic() + 90
72
+ while self.poll(handle) != "done" and time.monotonic() < deadline:
73
+ time.sleep(0.2)
74
+ if self.poll(handle) != "done" or self.result(handle).status != "ok":
75
+ problems.append("a trivial job did not run")
76
+ return PreflightReport(not problems, problems)
77
+
78
+ def build_env(self, spec: EnvSpec) -> EnvRef:
79
+ digest = spec.digest()
80
+ if not spec.requirements_lock.strip() and spec.python_version is None:
81
+ return EnvRef(digest=digest, status="ok", ref=self._python)
82
+ venv = self._root / "envs" / digest.removeprefix("sha256:")[:24]
83
+ python = venv / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
84
+ if (venv / ".pm-ready").exists():
85
+ return EnvRef(digest=digest, status="ok", ref=str(python))
86
+ t0 = time.monotonic()
87
+ uv = shutil.which("uv")
88
+ log: list[str] = []
89
+ try:
90
+ if uv is None:
91
+ raise RuntimeError("uv is required to build local-process environments")
92
+ cmd = [uv, "venv", str(venv), "--python", spec.python_version or self._python]
93
+ log.append(_run(cmd))
94
+ if spec.requirements_lock.strip():
95
+ lock = venv / "requirements.lock"
96
+ lock.write_text(spec.requirements_lock, encoding="utf-8")
97
+ log.append(_run([uv, "pip", "install", "--python", str(python), "-r",
98
+ str(lock)]))
99
+ (venv / ".pm-ready").write_text(digest, encoding="utf-8")
100
+ except (RuntimeError, OSError, subprocess.CalledProcessError) as exc:
101
+ log.append(str(exc))
102
+ return EnvRef(digest=digest, status="failed", ref="", log="\n".join(log),
103
+ build_seconds=time.monotonic() - t0)
104
+ return EnvRef(digest=digest, status="ok", ref=str(python), log="\n".join(log),
105
+ build_seconds=time.monotonic() - t0, built=True)
106
+
107
+ # ---- backend hooks -----------------------------------------------------------------
108
+
109
+ def _start(self, job_id: str, spec: JobSpec, secrets: dict[str, str]) -> _Proc:
110
+ job_dir = self._root / "jobs" / job_id
111
+ ws = job_dir / "ws"
112
+ unpack(spec.code, ws)
113
+ if spec.tag is not None:
114
+ (job_dir / TAG_FILE).write_text(spec.tag, encoding="utf-8")
115
+ bin_dir = str(Path(spec.env.ref).parent)
116
+ env = minimal_env({"PM_SEED": str(spec.seed), **secrets})
117
+ env["PATH"] = bin_dir + os.pathsep + env.get("PATH", "")
118
+ if spec.data_mounts:
119
+ env["PM_DATA"] = json.dumps({m.name: m.source for m in spec.data_mounts})
120
+ cmd = [self._python, "-m", "executor.watchdog", "--deadline", str(spec.deadline_s),
121
+ "--grace", str(self._grace), "--cwd", str(ws),
122
+ "--stdout", str(job_dir / "stdout"), "--stderr", str(job_dir / "stderr"),
123
+ "--result", str(job_dir / "result.json"),
124
+ "--killfile", str(job_dir / "kill"), spec.entrypoint]
125
+ kwargs: dict[str, Any] = {}
126
+ if sys.platform == "win32":
127
+ kwargs["creationflags"] = (subprocess.CREATE_NEW_PROCESS_GROUP
128
+ | subprocess.DETACHED_PROCESS)
129
+ else:
130
+ kwargs["start_new_session"] = True
131
+ # The watchdog needs the engine's import path to find `executor.watchdog`; the job
132
+ # itself gets only the minimal environment above (the watchdog passes it on).
133
+ wd_env = {**env, "PYTHONPATH": os.pathsep.join(p for p in sys.path if p)}
134
+ proc = subprocess.Popen(cmd, env=wd_env, stdin=subprocess.DEVNULL, # noqa: S603
135
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, **kwargs)
136
+ (job_dir / "watchdog.pid").write_text(str(proc.pid), encoding="utf-8")
137
+ return _Proc(job_dir=job_dir, watchdog=proc, deadline_s=spec.deadline_s)
138
+
139
+ def _done(self, backend: Any) -> bool:
140
+ p: _Proc = backend
141
+ if (p.job_dir / "result.json").exists():
142
+ return True
143
+ return p.watchdog.poll() is not None # watchdog gone without a result: lost
144
+
145
+ def _collect(self, backend: Any, spec: JobSpec, secrets: dict[str, str], *,
146
+ killed: bool) -> JobResult:
147
+ p: _Proc = backend
148
+ result_file = p.job_dir / "result.json"
149
+ info: dict[str, Any] = {}
150
+ if result_file.exists():
151
+ info = json.loads(result_file.read_text(encoding="utf-8"))
152
+ stdout, n1 = redact(_read(p.job_dir / "stdout"), secrets)
153
+ stderr, n2 = redact(_read(p.job_dir / "stderr"), secrets)
154
+ outputs, n3, truncated = read_outputs(p.job_dir / "ws" / "outputs", secrets)
155
+ from .common import cap
156
+
157
+ stdout, t1 = cap(stdout)
158
+ stderr, t2 = cap(stderr)
159
+ truncated += [name for name, t in (("stdout", t1), ("stderr", t2)) if t]
160
+ reason = info.get("reason")
161
+ exit_code = info.get("exit_code")
162
+ status = classify_status(exit_code, killed=killed or reason == "killed",
163
+ timed_out=reason == "timeout", crashed=False)
164
+ if not info:
165
+ status = "lost"
166
+ if info:
167
+ # Captured and redacted above; the workspace may hold what the job wrote,
168
+ # credentials included, so it does not outlive the job. A `lost` job's
169
+ # directory is kept: its process may still be running in it.
170
+ shutil.rmtree(p.job_dir, ignore_errors=True)
171
+ usd, metered = cost(self._info.rate_usd_per_hour, info.get("duration_s"))
172
+ return JobResult(status=status, exit_code=exit_code, stdout=stdout, stderr=stderr,
173
+ outputs=outputs, metrics=outputs.get(METRICS_FILE),
174
+ started_at=info.get("started_at"), ended_at=info.get("ended_at"),
175
+ duration_s=info.get("duration_s"), cost_usd=usd, metered=metered,
176
+ env_digest=spec.env.digest, code_digest=spec.code_digest,
177
+ redactions=sorted(set(n1 + n2 + n3)), output_cap_bytes=2 * 1024 * 1024,
178
+ truncated=truncated)
179
+
180
+ def _kill(self, backend: Any) -> None:
181
+ p: _Proc = backend
182
+ if not self._stop(p.job_dir) and p.watchdog.poll() is None:
183
+ _kill_tree(p.watchdog.pid) # the watchdog did not respond: it and its tree
184
+
185
+ def _stop(self, job_dir: Path) -> bool:
186
+ """Asks the job's watchdog to kill the job; True once it has written a result."""
187
+ (job_dir / "kill").write_text("kill", encoding="utf-8")
188
+ deadline = time.monotonic() + self._grace + 10
189
+ while not (job_dir / "result.json").exists() and time.monotonic() < deadline:
190
+ time.sleep(0.1)
191
+ return (job_dir / "result.json").exists()
192
+
193
+ def reap(self, tags: list[str]) -> list[str]:
194
+ """Jobs of any engine process, found by their tag file (INV-9). Without a result
195
+ the watchdog is gone or stuck, so the watchdog's and the job's trees are killed by
196
+ their recorded pids."""
197
+ wanted = {check_tag(t) for t in tags}
198
+ found: set[str] = set()
199
+ jobs = self._root / "jobs"
200
+ for marker in jobs.glob(f"*/{TAG_FILE}") if jobs.is_dir() else []:
201
+ tag = marker.read_text(encoding="utf-8").strip()
202
+ if tag not in wanted:
203
+ continue
204
+ job_dir = marker.parent
205
+ if not (job_dir / "result.json").exists() and not self._stop(job_dir):
206
+ for name in ("watchdog.pid", "job.pid"):
207
+ pid = _read(job_dir / name).decode().strip()
208
+ if pid.isdigit():
209
+ _kill_tree(int(pid))
210
+ shutil.rmtree(job_dir, ignore_errors=True)
211
+ found.add(tag)
212
+ return sorted(found)
213
+
214
+
215
+ def _kill_tree(pid: int) -> None:
216
+ if sys.platform == "win32":
217
+ subprocess.run(["taskkill", "/F", "/T", "/PID", str(pid)], # noqa: S603, S607
218
+ capture_output=True, check=False)
219
+ else:
220
+ import signal
221
+
222
+ try:
223
+ os.killpg(pid, signal.SIGKILL)
224
+ except (ProcessLookupError, PermissionError):
225
+ pass
226
+
227
+
228
+ def _read(path: Path) -> bytes:
229
+ return path.read_bytes() if path.exists() else b""
230
+
231
+
232
+ def _run(cmd: list[str]) -> str:
233
+ out = subprocess.run(cmd, capture_output=True, text=True, check=True) # noqa: S603
234
+ return (out.stdout + out.stderr).strip()
235
+
236
+
237
+ def _tar(files: dict[str, bytes]) -> bytes:
238
+ import io
239
+ import tarfile
240
+
241
+ buf = io.BytesIO()
242
+ with tarfile.open(fileobj=buf, mode="w") as tar:
243
+ for name, data in files.items():
244
+ info = tarfile.TarInfo(name)
245
+ info.size = len(data)
246
+ tar.addfile(info, io.BytesIO(data))
247
+ return buf.getvalue()