polyctl 0.7.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.
- polyctl/__init__.py +7 -0
- polyctl/__main__.py +5 -0
- polyctl/a2a.py +226 -0
- polyctl/a2a_mcp.py +546 -0
- polyctl/acp.py +417 -0
- polyctl/adapters/README.md +101 -0
- polyctl/adapters/aider.json +32 -0
- polyctl/adapters/amazon-q.json +15 -0
- polyctl/adapters/amp.json +16 -0
- polyctl/adapters/antigravity.json +120 -0
- polyctl/adapters/claude-code.json +229 -0
- polyctl/adapters/cloud-generic.json +57 -0
- polyctl/adapters/codex.json +47 -0
- polyctl/adapters/command-code.json +45 -0
- polyctl/adapters/copilot.json +17 -0
- polyctl/adapters/cursor-agent.json +29 -0
- polyctl/adapters/deepseek.json +13 -0
- polyctl/adapters/droid.json +34 -0
- polyctl/adapters/gemini.json +30 -0
- polyctl/adapters/grok.json +22 -0
- polyctl/adapters/hermes.json +34 -0
- polyctl/adapters/kilocode.json +30 -0
- polyctl/adapters/kimi.json +23 -0
- polyctl/adapters/kiro.json +332 -0
- polyctl/adapters/ollama.json +29 -0
- polyctl/adapters/omp.json +34 -0
- polyctl/adapters/openai-api.json +68 -0
- polyctl/adapters/opencode.json +162 -0
- polyctl/adapters/pi.json +24 -0
- polyctl/adapters/pr-agent.json +54 -0
- polyctl/adapters/profiles.json +9 -0
- polyctl/adapters/qoder.json +163 -0
- polyctl/adapters/qwen.json +27 -0
- polyctl/adapters/reasonix.json +25 -0
- polyctl/adapters/rewriters/rtk.json +24 -0
- polyctl/adapters/trae.json +31 -0
- polyctl/adapters/vendors.json +42 -0
- polyctl/adapters/warp.json +88 -0
- polyctl/allowlist.py +1756 -0
- polyctl/api_review.py +920 -0
- polyctl/cli.py +835 -0
- polyctl/conformance_harness.py +9 -0
- polyctl/demo.py +503 -0
- polyctl/drivers.py +146 -0
- polyctl/email.py +160 -0
- polyctl/features.py +98 -0
- polyctl/journal.py +144 -0
- polyctl/language.py +945 -0
- polyctl/lib.py +1296 -0
- polyctl/matrix.py +660 -0
- polyctl/mcp.py +1362 -0
- polyctl/ntfy.py +169 -0
- polyctl/outbound.py +39 -0
- polyctl/parts/__init__.py +5 -0
- polyctl/parts/cli_authority.py +2615 -0
- polyctl/parts/cli_channels.py +1325 -0
- polyctl/parts/cli_checks.py +2293 -0
- polyctl/parts/cli_completion.py +686 -0
- polyctl/parts/cli_evidence.py +141 -0
- polyctl/parts/cli_gain.py +375 -0
- polyctl/parts/cli_harness.py +392 -0
- polyctl/parts/cli_hooks.py +1816 -0
- polyctl/parts/cli_install.py +638 -0
- polyctl/parts/cli_lanes.py +106 -0
- polyctl/parts/cli_maintenance.py +2536 -0
- polyctl/parts/cli_next.py +248 -0
- polyctl/parts/cli_pr.py +53 -0
- polyctl/parts/cli_project.py +2857 -0
- polyctl/parts/cli_review.py +5158 -0
- polyctl/parts/cli_status.py +898 -0
- polyctl/parts/lib_adapters.py +2933 -0
- polyctl/parts/lib_alarms.py +2106 -0
- polyctl/parts/lib_events.py +224 -0
- polyctl/parts/lib_evidence.py +412 -0
- polyctl/parts/lib_gates.py +2099 -0
- polyctl/parts/lib_lanes.py +829 -0
- polyctl/parts/lib_mail.py +1541 -0
- polyctl/parts/lib_next.py +652 -0
- polyctl/parts/lib_pr.py +331 -0
- polyctl/parts/lib_slices.py +3337 -0
- polyctl/parts/lib_state.py +2415 -0
- polyctl/parts/lib_transcript.py +4118 -0
- polyctl/pressure.py +325 -0
- polyctl/prices.json +47 -0
- polyctl/procs.py +1194 -0
- polyctl/settings.py +505 -0
- polyctl/skill/SKILL.md +365 -0
- polyctl/skillkit.py +503 -0
- polyctl/storage.py +2055 -0
- polyctl/telegram.py +327 -0
- polyctl/tiers.py +123 -0
- polyctl/verdicts.py +41 -0
- polyctl/watchdog.py +3887 -0
- polyctl/web.py +337 -0
- polyctl-0.7.0.dist-info/METADATA +235 -0
- polyctl-0.7.0.dist-info/RECORD +99 -0
- polyctl-0.7.0.dist-info/WHEEL +4 -0
- polyctl-0.7.0.dist-info/entry_points.txt +5 -0
- polyctl-0.7.0.dist-info/licenses/LICENSE +21 -0
polyctl/email.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"""E-mail: the red alarm.
|
|
2
|
+
|
|
3
|
+
Desktop notifications are swiped away and a Telegram bot is a thing to set up;
|
|
4
|
+
both were silent for eleven hours on 2026-09-05 while a queue sat empty and an
|
|
5
|
+
architect wake failed forty times — the human had not looked at either. Mail is
|
|
6
|
+
the channel people actually check when they wake up, so it is the top of the
|
|
7
|
+
ladder: an orange condition that stands for an hour becomes a red one, and red
|
|
8
|
+
is a mail.
|
|
9
|
+
|
|
10
|
+
It goes through a provider (#74). formsubmit.co needs no server: it relays a
|
|
11
|
+
JSON POST to an address the user has verified once, and the token in
|
|
12
|
+
`~/.polyctl/email.json` is the alias formsubmit hands back after that verification.
|
|
13
|
+
Any SMTP server works too, with the user's own account. Either way the settings
|
|
14
|
+
live with credentials, 0600, never in a repository.
|
|
15
|
+
"""
|
|
16
|
+
import json
|
|
17
|
+
import os
|
|
18
|
+
import time
|
|
19
|
+
import urllib.request
|
|
20
|
+
|
|
21
|
+
from . import lib as A, storage
|
|
22
|
+
UTF8 = "utf-8" # every text file polyctl writes or reads; Windows would otherwise use cp1252
|
|
23
|
+
|
|
24
|
+
CONF = os.path.join(A.HOME, ".polyctl", "email.json")
|
|
25
|
+
ENDPOINT = "https://formsubmit.co/ajax/{token}"
|
|
26
|
+
USER_AGENT = "curl/8.7.1"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def config():
|
|
30
|
+
"""The mail channel's settings from `~/.polyctl/email.json`, or None when it cannot send.
|
|
31
|
+
|
|
32
|
+
0600, outside any repository. `provider` names one of PROVIDERS; a file written
|
|
33
|
+
before there was a choice is formsubmit's. A non-string provider disables mail,
|
|
34
|
+
where a list or object raised before send could return False (TRIAGE-2-LOW).
|
|
35
|
+
"""
|
|
36
|
+
if not os.path.exists(CONF):
|
|
37
|
+
return None
|
|
38
|
+
try:
|
|
39
|
+
c = json.load(open(CONF, encoding=UTF8))
|
|
40
|
+
except (OSError, ValueError):
|
|
41
|
+
return None
|
|
42
|
+
if not isinstance(c, dict):
|
|
43
|
+
return None
|
|
44
|
+
c.setdefault("name", "polyctl")
|
|
45
|
+
c.setdefault("provider", "formsubmit")
|
|
46
|
+
provider = PROVIDERS.get(c["provider"]) if isinstance(c["provider"], str) else None
|
|
47
|
+
if provider is None or any(not c.get(field) for field in provider.required):
|
|
48
|
+
return None
|
|
49
|
+
return c
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _write(c):
|
|
53
|
+
"""Save the settings: a file of mode 0600 from its creation, put in place of the one before whole.
|
|
54
|
+
|
|
55
|
+
Opened with the process's umask, written, and only then chmod-ed, the file and its credential were readable by
|
|
56
|
+
every user of the machine meanwhile, and stayed so when the process stopped between the two; and truncated in
|
|
57
|
+
place, a write cut short left no settings where working ones had been (HUNT-LEADS-SEC).
|
|
58
|
+
"""
|
|
59
|
+
storage.replace_file_durably(CONF, json.dumps(c, indent=2).encode(UTF8), mode=0o600)
|
|
60
|
+
return c
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def save(token, to=None, name="polyctl"):
|
|
64
|
+
return _write({"provider": "formsubmit", "token": token.strip(), "to": to or "", "name": name})
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def save_provider(provider, name="polyctl", **fields):
|
|
68
|
+
"""Save the settings of any provider; refuses one that is unknown or missing a field."""
|
|
69
|
+
if provider not in PROVIDERS:
|
|
70
|
+
raise ValueError(f"unknown mail provider {provider!r}; one of {', '.join(PROVIDERS)}")
|
|
71
|
+
missing = [field for field in PROVIDERS[provider].required if not fields.get(field)]
|
|
72
|
+
if missing:
|
|
73
|
+
raise ValueError(f"{provider} needs {', '.join('--' + field for field in missing)}")
|
|
74
|
+
return _write(dict({key: value for key, value in fields.items() if value not in (None, "")},
|
|
75
|
+
provider=provider, name=name))
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class FormSubmit:
|
|
79
|
+
"""formsubmit.co relays a JSON POST to an address verified once; no server."""
|
|
80
|
+
name = "formsubmit"
|
|
81
|
+
required = ("token",)
|
|
82
|
+
|
|
83
|
+
def send(self, c, subject, body, project, opener=None):
|
|
84
|
+
payload = {"name": f"{c['name']} · {project}",
|
|
85
|
+
"email": c.get("to") or "noreply@polyctl.local",
|
|
86
|
+
"message": body,
|
|
87
|
+
"_subject": f"[polyctl/{project}] {subject}",
|
|
88
|
+
"_template": "box"}
|
|
89
|
+
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
90
|
+
req = urllib.request.Request(ENDPOINT.format(token=c["token"]), data=data, method="POST",
|
|
91
|
+
headers={"Content-Type": "application/json",
|
|
92
|
+
"Accept": "application/json",
|
|
93
|
+
"Referer": "http://localhost:5173/",
|
|
94
|
+
"Origin": "http://localhost:5173/",
|
|
95
|
+
# Cloudflare in front of formsubmit rejects
|
|
96
|
+
# Python's default agent string (error 1010).
|
|
97
|
+
"User-Agent": USER_AGENT})
|
|
98
|
+
resp = (opener or urllib.request.urlopen)(req, timeout=20)
|
|
99
|
+
raw = resp.read().decode("utf-8", "replace") if hasattr(resp, "read") else str(resp)
|
|
100
|
+
try:
|
|
101
|
+
return str(json.loads(raw).get("success", "")).lower() in ("true", "1")
|
|
102
|
+
except ValueError:
|
|
103
|
+
return False
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class Smtp:
|
|
107
|
+
"""Any mail server that takes SMTP: the user's own provider, with its own credentials.
|
|
108
|
+
|
|
109
|
+
`tls` is "starttls" (the default, port 587), "implicit" (port 465) or "none". The
|
|
110
|
+
password is read from the file, never from a repository.
|
|
111
|
+
|
|
112
|
+
Either TLS verifies the server's certificate and that it names the host. smtplib's own
|
|
113
|
+
context for both verifies neither, and implicit TLS took it: whoever answered for the
|
|
114
|
+
host in between was handed the account's password (TRIAGE-HIGHS).
|
|
115
|
+
"""
|
|
116
|
+
name = "smtp"
|
|
117
|
+
required = ("host", "to")
|
|
118
|
+
|
|
119
|
+
def send(self, c, subject, body, project, opener=None):
|
|
120
|
+
import smtplib
|
|
121
|
+
import ssl
|
|
122
|
+
from email.message import EmailMessage
|
|
123
|
+
message = EmailMessage()
|
|
124
|
+
message["Subject"] = f"[polyctl/{project}] {subject}"
|
|
125
|
+
message["From"] = c.get("from") or c["to"]
|
|
126
|
+
message["To"] = c["to"]
|
|
127
|
+
message.set_content(body)
|
|
128
|
+
tls = c.get("tls") or "starttls"
|
|
129
|
+
port = int(c.get("port") or (465 if tls == "implicit" else 587))
|
|
130
|
+
verified = ssl.create_default_context()
|
|
131
|
+
if tls == "implicit":
|
|
132
|
+
server = (opener or smtplib.SMTP_SSL)(c["host"], port, timeout=20, context=verified)
|
|
133
|
+
else:
|
|
134
|
+
server = (opener or smtplib.SMTP)(c["host"], port, timeout=20)
|
|
135
|
+
with server:
|
|
136
|
+
if tls == "starttls":
|
|
137
|
+
server.starttls(context=verified)
|
|
138
|
+
if c.get("user"):
|
|
139
|
+
server.login(c["user"], c.get("password") or "")
|
|
140
|
+
server.send_message(message)
|
|
141
|
+
return True
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# The red channel is one of several providers, not one vendor in an endpoint string (#74).
|
|
145
|
+
PROVIDERS = {provider.name: provider for provider in (FormSubmit(), Smtp())}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def send(subject, body, root=None, opener=None):
|
|
149
|
+
"""Deliver one mail. Returns True on an accepted relay, False otherwise; never raises."""
|
|
150
|
+
c = config()
|
|
151
|
+
if not c:
|
|
152
|
+
return False
|
|
153
|
+
project = (A.project_key(root) if root else "polyctl")
|
|
154
|
+
try:
|
|
155
|
+
ok = bool(PROVIDERS[c["provider"]].send(c, subject, body, project, opener=opener))
|
|
156
|
+
except Exception:
|
|
157
|
+
ok = False
|
|
158
|
+
if root:
|
|
159
|
+
A.record_notice(root, f"mail: {subject}", body[:200], sent=ok, key="mail:" + subject[:40])
|
|
160
|
+
return ok
|
polyctl/features.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"""Features you can switch off, each with the share of spend it costs.
|
|
2
|
+
|
|
3
|
+
The honest answer to "what does this bureaucracy cost" is a menu, not a number.
|
|
4
|
+
Everything polyctl does that spends a model's quota is a feature with a switch; with
|
|
5
|
+
all of them off, polyctl is a deterministic monitor — board, mailbox, gates, commit
|
|
6
|
+
authority by digest, alarms, pings, hooks — and costs nothing. With all of them
|
|
7
|
+
on it costs about a quarter of the implementer's spend on a normal day. The
|
|
8
|
+
shares below were measured on the first pilot (`polyctl cost`) and are refined by
|
|
9
|
+
it. What each costs is measured, never estimated: `polyctl cost --features` (#10).
|
|
10
|
+
"""
|
|
11
|
+
import json
|
|
12
|
+
import os
|
|
13
|
+
UTF8 = "utf-8" # every text file polyctl writes or reads; Windows would otherwise use cp1252
|
|
14
|
+
|
|
15
|
+
# key: (label, default, what it spends). What each costs is measured by `polyctl cost --features` (#10).
|
|
16
|
+
FEATURES = {
|
|
17
|
+
"review": ("independent review of every slice", True,
|
|
18
|
+
"reviewer model reads the slice diff, 1–2 times per slice"),
|
|
19
|
+
"inventory_review": ("inventory-first review on slices that open a new surface", True,
|
|
20
|
+
"reviewer model, +1–2 reviews on such slices; none on fix slices"),
|
|
21
|
+
"nudge": ("watchdog restarts the idle implementer", True,
|
|
22
|
+
"implementer turns; only the empty ones are overhead"),
|
|
23
|
+
"architect_wake": ("anomalies and decisions wake the architect", True,
|
|
24
|
+
"one architect turn per anomaly batch"),
|
|
25
|
+
"refill": ("an empty queue wakes the architect to refill it", True,
|
|
26
|
+
"one architect turn per refill"),
|
|
27
|
+
"reports": ("implementer reports on state changes (start/done/blocked)", True,
|
|
28
|
+
"a few implementer tool calls per slice"),
|
|
29
|
+
"hunter": ("a scheduled, read-only bug hunt that mails leads to the architect", False,
|
|
30
|
+
"one hunter call per run over a bounded slice of the tree"),
|
|
31
|
+
"toast": ("a Windows toast for what reaches a person's desktop", False,
|
|
32
|
+
"nothing: a notification on this machine"),
|
|
33
|
+
"windows_job": ("a Windows job holds each reviewer and probe so what they start ends with them", True,
|
|
34
|
+
"nothing: a kernel job on this machine"),
|
|
35
|
+
}
|
|
36
|
+
ORDER = list(FEATURES)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
# A switch that weakens a guarantee is off only on a person's record, as a recorded setting is (REVIEW-TIERS):
|
|
40
|
+
# `.polyctl/config.json` is a file the implementer can write, and review turned off there let `polyctl commit-ok` grant on
|
|
41
|
+
# verification alone, so an unreviewed commit landed (FEATURE-RECORDED); `windows_job` turned off there let a
|
|
42
|
+
# reviewer leave a process tree running after the review ended (WIN-JOB).
|
|
43
|
+
RECORDED = ("review", "inventory_review", "windows_job")
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def recorded_off(cfg, key):
|
|
47
|
+
"""Whether a person's record turns `key` off on this project: the newest row the opt-in ledger holds for the switch
|
|
48
|
+
says off and names who. A project with no root, and a ledger that cannot be read, record nobody."""
|
|
49
|
+
root = cfg.get("root")
|
|
50
|
+
if not root:
|
|
51
|
+
return False
|
|
52
|
+
from . import lib as A
|
|
53
|
+
try:
|
|
54
|
+
row = A.recorded_opt_in(root, f"features.{key}")
|
|
55
|
+
except Exception:
|
|
56
|
+
return False
|
|
57
|
+
return isinstance(row, dict) and row.get("value") is False and bool(str(row.get("by") or "").strip())
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def configured(cfg):
|
|
61
|
+
"""{key: bool} as the project's config writes it, defaults otherwise, record or none."""
|
|
62
|
+
conf = cfg.get("features") or {}
|
|
63
|
+
return {k: bool(conf.get(k, FEATURES[k][1])) for k in ORDER}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def unrecorded(cfg):
|
|
67
|
+
"""The recorded switches the config turns off with no person's record behind it: they stay on."""
|
|
68
|
+
written = configured(cfg)
|
|
69
|
+
return [key for key in RECORDED if not written[key] and not recorded_off(cfg, key)]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def switches(cfg):
|
|
73
|
+
"""{key: bool} for this project: config overrides, defaults otherwise; a recorded switch is off only where a
|
|
74
|
+
person's record says so too (FEATURE-RECORDED)."""
|
|
75
|
+
on = configured(cfg)
|
|
76
|
+
for key in unrecorded(cfg):
|
|
77
|
+
on[key] = True
|
|
78
|
+
return on
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def enabled(cfg, key):
|
|
82
|
+
return switches(cfg).get(key, FEATURES.get(key, ("", True))[1])
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def set_switch(root, key, on):
|
|
86
|
+
"""Switch one feature in `.polyctl/config.json`, written whole or not at all.
|
|
87
|
+
|
|
88
|
+
A config polyctl cannot read is refused rather than rebuilt: rebuilding would drop
|
|
89
|
+
whatever else it held, `capability_matrix` among it (#56).
|
|
90
|
+
"""
|
|
91
|
+
from . import lib as A
|
|
92
|
+
document = A.project_config_document(root)
|
|
93
|
+
if document["problem"]:
|
|
94
|
+
raise ValueError(document["problem"])
|
|
95
|
+
cfg = document["config"]
|
|
96
|
+
cfg.setdefault("features", {})[key] = bool(on)
|
|
97
|
+
A.write_project_config(root, json.dumps(cfg, indent=2, ensure_ascii=False))
|
|
98
|
+
return cfg["features"]
|
polyctl/journal.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""What the watchdog starts, kept in one journal per project, so a crash never starts it twice (JOURNAL).
|
|
2
|
+
|
|
3
|
+
The watchdog started an architect and recorded it afterwards: a kill -9, a power cut or a Ctrl+C
|
|
4
|
+
between the two left a headless architect no record named, and the next cycle, seeing none, woke
|
|
5
|
+
another into the same session. A step is now claimed under a key before its process starts - the
|
|
6
|
+
session and the reports it is handed name a wake - with a lease, and the process's pid and start
|
|
7
|
+
are written the moment it has one. A claim that holds, or a process a step names that still runs,
|
|
8
|
+
is a step under way, and the same key is not started again until the lease has passed.
|
|
9
|
+
|
|
10
|
+
SQLite is in Python's standard library, and one transaction is all-or-nothing: a claim is taken by
|
|
11
|
+
one writer, two cycles or a cycle and a command alike. The journal is written ahead (WAL) and synced
|
|
12
|
+
on every commit (synchronous=FULL). It lives with the watchdog's other per-project state in ~/.polyctl.
|
|
13
|
+
Where an interpreter was built without sqlite3 there is no journal, and the watchdog falls back on
|
|
14
|
+
its own records and process scans, as before.
|
|
15
|
+
"""
|
|
16
|
+
import os
|
|
17
|
+
import time
|
|
18
|
+
|
|
19
|
+
try:
|
|
20
|
+
import sqlite3
|
|
21
|
+
except ImportError: # an interpreter built without it
|
|
22
|
+
sqlite3 = None
|
|
23
|
+
|
|
24
|
+
SCHEMA = (
|
|
25
|
+
"CREATE TABLE IF NOT EXISTS steps (key TEXT PRIMARY KEY, kind TEXT NOT NULL, state TEXT NOT NULL, "
|
|
26
|
+
"lease_until REAL NOT NULL, pid INTEGER, start REAL, at REAL NOT NULL, detail TEXT)",
|
|
27
|
+
"CREATE TABLE IF NOT EXISTS sleeps (id TEXT PRIMARY KEY, kind TEXT NOT NULL, at REAL NOT NULL, "
|
|
28
|
+
"wake_at REAL, reason TEXT, closed_at REAL, outcome TEXT)",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def available():
|
|
33
|
+
return sqlite3 is not None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def _connect(path):
|
|
37
|
+
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
|
|
38
|
+
connection = sqlite3.connect(path, timeout=10.0, isolation_level=None)
|
|
39
|
+
# Switching a new file to WAL answers SQLITE_BUSY at once, past the busy timeout, while another
|
|
40
|
+
# connection opens it: retried within the same ten seconds.
|
|
41
|
+
deadline = time.monotonic() + 10.0
|
|
42
|
+
while True:
|
|
43
|
+
try:
|
|
44
|
+
connection.execute("PRAGMA journal_mode=WAL")
|
|
45
|
+
break
|
|
46
|
+
except sqlite3.OperationalError as exc:
|
|
47
|
+
if "locked" not in str(exc) or time.monotonic() >= deadline:
|
|
48
|
+
connection.close()
|
|
49
|
+
raise
|
|
50
|
+
time.sleep(0.01)
|
|
51
|
+
connection.execute("PRAGMA synchronous=FULL")
|
|
52
|
+
for statement in SCHEMA:
|
|
53
|
+
connection.execute(statement)
|
|
54
|
+
return connection
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _transaction(path, work):
|
|
58
|
+
"""Run `work(connection)` in one immediate transaction; its result, or None where there is no journal."""
|
|
59
|
+
if sqlite3 is None:
|
|
60
|
+
return None
|
|
61
|
+
connection = _connect(path)
|
|
62
|
+
try:
|
|
63
|
+
connection.execute("BEGIN IMMEDIATE")
|
|
64
|
+
try:
|
|
65
|
+
result = work(connection)
|
|
66
|
+
except BaseException:
|
|
67
|
+
connection.execute("ROLLBACK")
|
|
68
|
+
raise
|
|
69
|
+
connection.execute("COMMIT")
|
|
70
|
+
return result
|
|
71
|
+
finally:
|
|
72
|
+
connection.close()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def claim(path, key, kind, lease, now=None):
|
|
76
|
+
"""Whether this caller now holds `key`: it is new, or the step that held it is done or its lease has passed.
|
|
77
|
+
|
|
78
|
+
The clock is the wall clock, deliberately: a lease must outlive this process and a reboot, which a
|
|
79
|
+
monotonic reading cannot. A clock step only moves when a lease ends, and the reader of a passed lease
|
|
80
|
+
re-verifies the recorded pid and start against the process table (`running`), so a jump back can delay a
|
|
81
|
+
wake and a jump forward can end a lease early, but neither starts a second turn into the same session.
|
|
82
|
+
"""
|
|
83
|
+
now = time.time() if now is None else now
|
|
84
|
+
|
|
85
|
+
def work(connection):
|
|
86
|
+
row = connection.execute("SELECT state, lease_until FROM steps WHERE key = ?", (key,)).fetchone()
|
|
87
|
+
if row and row[0] in ("claimed", "started") and row[1] > now:
|
|
88
|
+
return False
|
|
89
|
+
connection.execute("INSERT OR REPLACE INTO steps (key, kind, state, lease_until, pid, start, at) "
|
|
90
|
+
"VALUES (?, ?, 'claimed', ?, NULL, NULL, ?)", (key, kind, now + lease, now))
|
|
91
|
+
return True
|
|
92
|
+
held = _transaction(path, work)
|
|
93
|
+
return True if held is None else held
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def started(path, key, pid, start):
|
|
97
|
+
"""The process a claimed step started: its pid and when it began, so a later reader tells it from a reuse."""
|
|
98
|
+
_transaction(path, lambda c: c.execute("UPDATE steps SET state = 'started', pid = ?, start = ? WHERE key = ?",
|
|
99
|
+
(pid, start, key)))
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def finished(path, key, outcome="done"):
|
|
103
|
+
_transaction(path, lambda c: c.execute("UPDATE steps SET state = ?, lease_until = 0 WHERE key = ?",
|
|
104
|
+
(outcome, key)))
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def step(path, key):
|
|
108
|
+
"""{state, pid, start, lease_until} of one step, or None."""
|
|
109
|
+
if sqlite3 is None or not os.path.exists(path):
|
|
110
|
+
return None
|
|
111
|
+
connection = _connect(path)
|
|
112
|
+
try:
|
|
113
|
+
row = connection.execute("SELECT state, pid, start, lease_until FROM steps WHERE key = ?", (key,)).fetchone()
|
|
114
|
+
finally:
|
|
115
|
+
connection.close()
|
|
116
|
+
return dict(zip(("state", "pid", "start", "lease_until"), row)) if row else None
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def running(path, kind):
|
|
120
|
+
"""[(key, pid, start)] of the steps of `kind` started with a pid and a start, whatever their lease: a caller
|
|
121
|
+
that finds the process still the one recorded holds it as under way past its lease."""
|
|
122
|
+
if sqlite3 is None or not os.path.exists(path):
|
|
123
|
+
return []
|
|
124
|
+
connection = _connect(path)
|
|
125
|
+
try:
|
|
126
|
+
return [tuple(row) for row in connection.execute(
|
|
127
|
+
"SELECT key, pid, start FROM steps WHERE kind = ? AND state = 'started' AND pid IS NOT NULL "
|
|
128
|
+
"AND start IS NOT NULL", (kind,))]
|
|
129
|
+
finally:
|
|
130
|
+
connection.close()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def under_way(path, kind, now=None):
|
|
134
|
+
"""[(key, pid, start)] of the steps of `kind` whose claim holds; a pid is None while a start was not written."""
|
|
135
|
+
if sqlite3 is None or not os.path.exists(path):
|
|
136
|
+
return []
|
|
137
|
+
now = time.time() if now is None else now
|
|
138
|
+
connection = _connect(path)
|
|
139
|
+
try:
|
|
140
|
+
return [tuple(row) for row in connection.execute(
|
|
141
|
+
"SELECT key, pid, start FROM steps WHERE kind = ? AND state IN ('claimed', 'started') AND lease_until > ?",
|
|
142
|
+
(kind, now))]
|
|
143
|
+
finally:
|
|
144
|
+
connection.close()
|