@jesdi/skills 0.0.101 → 0.0.102
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.
- package/package.json +1 -1
- package/skills/babysit-pr/SKILL.md +9 -2
- package/skills/babysit-pr/babysit.py +98 -36
- package/skills/babysit-pr/exam.py +156 -0
- package/skills/babysit-pr/fixtures/exam_host.py +47 -0
- package/skills/babysit-pr/fixtures/gh +107 -11
- package/skills/babysit-pr/test_babysit.py +8 -2
- package/skills/babysit-pr/test_e2e_setup.py +1 -1
- package/skills/babysit-pr/test_exam_merge_gate.py +189 -0
- package/skills/babysit-pr/test_exam_request.py +337 -0
- package/skills/babysit-pr/test_exam_required_check.py +145 -0
- package/skills/babysit-pr/test_exam_wait.py +353 -0
- package/skills/babysit-pr/test_merge_and_handoff.py +12 -6
- package/skills/babysit-pr/test_trusted_merge_risk.py +2 -2
- package/skills/pr-exam/SKILL.md +131 -0
- package/skills/pr-exam/fixtures/README.md +20 -0
- package/skills/pr-exam/fixtures/delta-new-concept/body.md +9 -0
- package/skills/pr-exam/fixtures/delta-new-concept/diff.patch +82 -0
- package/skills/pr-exam/fixtures/delta-new-concept/exam.json +46 -0
- package/skills/pr-exam/fixtures/delta-new-concept/passed.diff +66 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0006-leader-lifecycle.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0007-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/records/0008-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/delta-new-concept/request.json +43 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/body.md +9 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/diff.patch +66 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/exam.json +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/passed.diff +66 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0006-leader-lifecycle.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0007-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/records/0008-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/delta-nothing-new/request.json +43 -0
- package/skills/pr-exam/fixtures/followup/body.md +9 -0
- package/skills/pr-exam/fixtures/followup/diff.patch +66 -0
- package/skills/pr-exam/fixtures/followup/exam.json +61 -0
- package/skills/pr-exam/fixtures/followup/records/0001-single-sync-leader.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0002-stale-leader-writes.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0003-fencing-token.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0004-lease-expiry.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0005-injected-monotonic-clock.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0006-lease-rollback.md +10 -0
- package/skills/pr-exam/fixtures/followup/records/0007-sync-blast-radius.md +10 -0
- package/skills/pr-exam/fixtures/followup/request.json +41 -0
- package/skills/pr-exam/fixtures/grading/answers.json +5 -0
- package/skills/pr-exam/fixtures/grading/attempt.json +10 -0
- package/skills/pr-exam/fixtures/grading/grades.json +14 -0
- package/skills/pr-exam/fixtures/grading/questions.json +26 -0
- package/skills/pr-exam/fixtures/grading/request.json +10 -0
- package/skills/pr-exam/fixtures/twelve-concepts/body.md +9 -0
- package/skills/pr-exam/fixtures/twelve-concepts/diff.patch +66 -0
- package/skills/pr-exam/fixtures/twelve-concepts/exam.json +195 -0
- package/skills/pr-exam/fixtures/twelve-concepts/request.json +15 -0
- package/skills/pr-exam/fixtures/typo-readme/body.md +1 -0
- package/skills/pr-exam/fixtures/typo-readme/diff.patch +7 -0
- package/skills/pr-exam/fixtures/typo-readme/exam.json +30 -0
- package/skills/pr-exam/fixtures/typo-readme/request.json +15 -0
- package/skills/pr-exam/pr_exam.py +705 -0
- package/skills/pr-exam/test_config.py +83 -0
- package/skills/pr-exam/test_exam_format.py +252 -0
- package/skills/pr-exam/test_followup_delta.py +90 -0
- package/skills/pr-exam/test_learning_records.py +267 -0
- package/skills/pr-exam/test_record_publication.py +45 -0
- package/skills/pr-exam/test_scoring.py +121 -0
- package/skills/pr-exam/test_served_shapes.py +38 -0
- package/skills/pr-exam/test_setup.py +349 -0
- package/skills/pr-exam/test_skill_fixtures.py +88 -0
- package/skills-manifest.json +9 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: stale-leader-writes
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# stale-leader-writes
|
|
9
|
+
|
|
10
|
+
After a pause (GC, network) both workers can believe they lead. Every write carries the fencing token from acquire, and write raises LeaseLost when the stored token differs, so the stale leader's late writes are refused.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: fencing-token
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# fencing-token
|
|
9
|
+
|
|
10
|
+
acquire stores the previous token plus one, so the newest holder always has the highest token and any older token no longer matches.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: lease-expiry
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# lease-expiry
|
|
9
|
+
|
|
10
|
+
acquire refuses only while the lease has not expired and belongs to someone else; renew pushes expires forward, so a holder that stops renewing loses the lease after the TTL.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: injected-monotonic-clock
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# injected-monotonic-clock
|
|
9
|
+
|
|
10
|
+
The clock defaults to time.monotonic, which never jumps with wall-clock changes, and injecting it lets tests advance time without sleeping.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: leader-lifecycle
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# leader-lifecycle
|
|
9
|
+
|
|
10
|
+
run_leader renews the lease after every unit of work, stops with False when renew raises LeaseLost, and releases the lease in a finally block so it is freed whatever happens.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: lease-rollback
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# lease-rollback
|
|
9
|
+
|
|
10
|
+
The merge-risk block gives the rollback as a revert, safe because leases expire after the TTL: no lease outlives the code that renews it.
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mode": "delta",
|
|
3
|
+
"repo": "jesdi/general-skills",
|
|
4
|
+
"pr": 240,
|
|
5
|
+
"head_sha": "dddddddddddddddddddddddddddddddddddddddd",
|
|
6
|
+
"diff": "diff.patch",
|
|
7
|
+
"body": "body.md",
|
|
8
|
+
"glossary": null,
|
|
9
|
+
"records": "records",
|
|
10
|
+
"today": "2026-11-18",
|
|
11
|
+
"only_concepts": null,
|
|
12
|
+
"prior_concepts": [
|
|
13
|
+
"single-sync-leader",
|
|
14
|
+
"stale-leader-writes",
|
|
15
|
+
"fencing-token",
|
|
16
|
+
"lease-expiry",
|
|
17
|
+
"injected-monotonic-clock",
|
|
18
|
+
"leader-lifecycle",
|
|
19
|
+
"lease-rollback",
|
|
20
|
+
"sync-blast-radius",
|
|
21
|
+
"renew-checks-ownership",
|
|
22
|
+
"write-checks-token",
|
|
23
|
+
"release-by-owner",
|
|
24
|
+
"owner-reacquire"
|
|
25
|
+
],
|
|
26
|
+
"prior_prompts": [],
|
|
27
|
+
"out": "exam.json",
|
|
28
|
+
"passed_diff": "passed.diff",
|
|
29
|
+
"passed_concepts": [
|
|
30
|
+
"single-sync-leader",
|
|
31
|
+
"stale-leader-writes",
|
|
32
|
+
"fencing-token",
|
|
33
|
+
"lease-expiry",
|
|
34
|
+
"injected-monotonic-clock",
|
|
35
|
+
"leader-lifecycle",
|
|
36
|
+
"lease-rollback",
|
|
37
|
+
"sync-blast-radius",
|
|
38
|
+
"renew-checks-ownership",
|
|
39
|
+
"write-checks-token",
|
|
40
|
+
"release-by-owner",
|
|
41
|
+
"owner-reacquire"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Adds a lease so that only one worker runs the nightly sync at a time. Before this, two workers sometimes ran it at once and overwrote each other's results.
|
|
2
|
+
|
|
3
|
+
Why: a worker that stalls past its TTL must not keep writing after another worker took over, so every write carries a fencing token and the store refuses a stale one.
|
|
4
|
+
|
|
5
|
+
```merge-risk v1
|
|
6
|
+
risk: medium
|
|
7
|
+
rollback: revert; leases expire after the TTL
|
|
8
|
+
blast-radius: the nightly sync only
|
|
9
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
diff --git a/lease.py b/lease.py
|
|
2
|
+
new file mode 100644
|
|
3
|
+
--- /dev/null
|
|
4
|
+
+++ b/lease.py
|
|
5
|
+
@@ -0,0 +1,61 @@
|
|
6
|
+
+import time
|
|
7
|
+
+
|
|
8
|
+
+
|
|
9
|
+
+class LeaseLost(Exception):
|
|
10
|
+
+ """The caller no longer owns the lease it tried to write under."""
|
|
11
|
+
+
|
|
12
|
+
+
|
|
13
|
+
+class LeaseStore:
|
|
14
|
+
+ """Single-writer lease over a key-value store.
|
|
15
|
+
+
|
|
16
|
+
+ Two workers can both believe they lead after a pause (GC, network). A
|
|
17
|
+
+ fencing token lets the store reject the older leader's late writes.
|
|
18
|
+
+ """
|
|
19
|
+
+
|
|
20
|
+
+ def __init__(self, backend, ttl=30, clock=time.monotonic):
|
|
21
|
+
+ self.backend = backend
|
|
22
|
+
+ self.ttl = ttl
|
|
23
|
+
+ self.clock = clock
|
|
24
|
+
+
|
|
25
|
+
+ def acquire(self, name, owner):
|
|
26
|
+
+ now = self.clock()
|
|
27
|
+
+ held = self.backend.get(name)
|
|
28
|
+
+ if held and held["expires"] > now and held["owner"] != owner:
|
|
29
|
+
+ return None
|
|
30
|
+
+ token = (held["token"] + 1) if held else 1
|
|
31
|
+
+ self.backend.put(name, {"owner": owner, "token": token, "expires": now + self.ttl})
|
|
32
|
+
+ return token
|
|
33
|
+
+
|
|
34
|
+
+ def renew(self, name, owner, token):
|
|
35
|
+
+ held = self.backend.get(name)
|
|
36
|
+
+ if not held or held["owner"] != owner or held["token"] != token:
|
|
37
|
+
+ raise LeaseLost(name)
|
|
38
|
+
+ held["expires"] = self.clock() + self.ttl
|
|
39
|
+
+ self.backend.put(name, held)
|
|
40
|
+
+
|
|
41
|
+
+ def write(self, name, token, key, value):
|
|
42
|
+
+ held = self.backend.get(name)
|
|
43
|
+
+ if not held or held["token"] != token:
|
|
44
|
+
+ raise LeaseLost(name)
|
|
45
|
+
+ self.backend.put(key, value)
|
|
46
|
+
+
|
|
47
|
+
+ def release(self, name, owner, token):
|
|
48
|
+
+ held = self.backend.get(name)
|
|
49
|
+
+ if held and held["owner"] == owner and held["token"] == token:
|
|
50
|
+
+ held["expires"] = 0
|
|
51
|
+
+ self.backend.put(name, held)
|
|
52
|
+
+
|
|
53
|
+
+
|
|
54
|
+
+def run_leader(store, name, owner, work, interval=10):
|
|
55
|
+
+ token = store.acquire(name, owner)
|
|
56
|
+
+ if token is None:
|
|
57
|
+
+ return False
|
|
58
|
+
+ try:
|
|
59
|
+
+ while work(token):
|
|
60
|
+
+ store.renew(name, owner, token)
|
|
61
|
+
+ time.sleep(interval)
|
|
62
|
+
+ except LeaseLost:
|
|
63
|
+
+ return False
|
|
64
|
+
+ finally:
|
|
65
|
+
+ store.release(name, owner, token)
|
|
66
|
+
+ return True
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"mode": "followup",
|
|
4
|
+
"repo": "jesdi/general-skills",
|
|
5
|
+
"pr": 240,
|
|
6
|
+
"head_sha": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
7
|
+
"concepts": [
|
|
8
|
+
{"id": "leader-lifecycle", "title": "Leader lifecycle", "kind": "architecture"},
|
|
9
|
+
{"id": "lease-rollback", "title": "Lease rollback", "kind": "risk"},
|
|
10
|
+
{"id": "sync-blast-radius", "title": "Sync blast radius", "kind": "risk"}
|
|
11
|
+
],
|
|
12
|
+
"questions": [
|
|
13
|
+
{
|
|
14
|
+
"id": "q1",
|
|
15
|
+
"concept": "leader-lifecycle",
|
|
16
|
+
"type": "text",
|
|
17
|
+
"prompt": "A leader learns that another worker has taken over its lease, and its cleanup still releases the lease on the way out. Why does that release not end the new leader's lease?"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"id": "q2",
|
|
21
|
+
"concept": "lease-rollback",
|
|
22
|
+
"type": "text",
|
|
23
|
+
"prompt": "You roll this change back with a revert. What manual cleanup do the leases left in the store need, and why?"
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"id": "q3",
|
|
27
|
+
"concept": "sync-blast-radius",
|
|
28
|
+
"type": "choice",
|
|
29
|
+
"prompt": "A bug makes the lease store refuse every attempt to take a lease. What stops running?",
|
|
30
|
+
"options": [
|
|
31
|
+
"Every worker job entirely",
|
|
32
|
+
"All backend writes everywhere",
|
|
33
|
+
"The nightly sync only",
|
|
34
|
+
"Nothing at all stops"
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
],
|
|
38
|
+
"key": {
|
|
39
|
+
"q1": {
|
|
40
|
+
"rubric": {
|
|
41
|
+
"pass": "Says release only acts when the caller's owner and token match the lease currently held, so the old leader's release, carrying its old owner or token, changes nothing for the new leader.",
|
|
42
|
+
"partial": "Says the release is harmless or ignored for a leader that lost the lease, without saying that it is the owner and token check against the held lease that makes it so."
|
|
43
|
+
},
|
|
44
|
+
"explanation": "The leader loop always releases in its cleanup, but release expires the lease only when the held record has the caller's owner and token. After a takeover the record carries the new leader's owner and a higher token, so the old leader's release does nothing.",
|
|
45
|
+
"hunk": "+ def release(self, name, owner, token):\n+ held = self.backend.get(name)\n+ if held and held[\"owner\"] == owner and held[\"token\"] == token:\n+ held[\"expires\"] = 0\n+ self.backend.put(name, held)"
|
|
46
|
+
},
|
|
47
|
+
"q2": {
|
|
48
|
+
"rubric": {
|
|
49
|
+
"pass": "Says none is needed, because every lease carries an expiry one TTL ahead and lapses on its own once nothing renews it.",
|
|
50
|
+
"partial": "Says no cleanup is needed without giving the TTL expiry as the reason, or names the TTL expiry but still asks for a manual step."
|
|
51
|
+
},
|
|
52
|
+
"explanation": "The rollback is a plain revert. A lease is stored with an expiry of now plus the TTL and only a renew extends it, so once the reverted code stops renewing, every lease lapses within one TTL without anyone deleting it.",
|
|
53
|
+
"hunk": "+ self.backend.put(name, {\"owner\": owner, \"token\": token, \"expires\": now + self.ttl})"
|
|
54
|
+
},
|
|
55
|
+
"q3": {
|
|
56
|
+
"answer": 2,
|
|
57
|
+
"explanation": "Only the nightly sync runs under the leader loop, which returns without doing any work when it cannot take the lease. Other jobs and other writes never go through the lease store, so the blast radius is the nightly sync only.",
|
|
58
|
+
"hunk": "+ token = store.acquire(name, owner)\n+ if token is None:\n+ return False"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: single-sync-leader
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# single-sync-leader
|
|
9
|
+
|
|
10
|
+
Before the lease, two workers sometimes ran the nightly sync at the same time and overwrote each other's results; the lease makes one worker the single writer.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: stale-leader-writes
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# stale-leader-writes
|
|
9
|
+
|
|
10
|
+
After a pause (GC, network) both workers can believe they lead. Every write carries the fencing token from acquire, and write raises LeaseLost when the stored token differs, so the stale leader's late writes are refused.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: fencing-token
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# fencing-token
|
|
9
|
+
|
|
10
|
+
acquire stores the previous token plus one, so the newest holder always has the highest token and any older token no longer matches.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: lease-expiry
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# lease-expiry
|
|
9
|
+
|
|
10
|
+
acquire refuses only while the lease has not expired and belongs to someone else; renew pushes expires forward, so a holder that stops renewing loses the lease after the TTL.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: injected-monotonic-clock
|
|
3
|
+
state: demonstrated
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# injected-monotonic-clock
|
|
9
|
+
|
|
10
|
+
The clock defaults to time.monotonic, which never jumps with wall-clock changes, and injecting it lets tests advance time without sleeping.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
---
|
|
2
|
+
concept: lease-rollback
|
|
3
|
+
state: misconception
|
|
4
|
+
date: 2026-11-18
|
|
5
|
+
source: jesdi/general-skills#240
|
|
6
|
+
status: active
|
|
7
|
+
---
|
|
8
|
+
# lease-rollback
|
|
9
|
+
|
|
10
|
+
The merge-risk block gives the rollback as a revert, safe because leases expire after the TTL: no lease outlives the code that renews it.
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mode": "followup",
|
|
3
|
+
"repo": "jesdi/general-skills",
|
|
4
|
+
"pr": 240,
|
|
5
|
+
"head_sha": "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
|
|
6
|
+
"diff": "diff.patch",
|
|
7
|
+
"body": "body.md",
|
|
8
|
+
"glossary": null,
|
|
9
|
+
"records": "records",
|
|
10
|
+
"today": "2026-11-18",
|
|
11
|
+
"only_concepts": [
|
|
12
|
+
"leader-lifecycle",
|
|
13
|
+
"lease-rollback",
|
|
14
|
+
"sync-blast-radius"
|
|
15
|
+
],
|
|
16
|
+
"prior_concepts": [
|
|
17
|
+
"single-sync-leader",
|
|
18
|
+
"stale-leader-writes",
|
|
19
|
+
"fencing-token",
|
|
20
|
+
"lease-expiry",
|
|
21
|
+
"injected-monotonic-clock",
|
|
22
|
+
"leader-lifecycle",
|
|
23
|
+
"lease-rollback",
|
|
24
|
+
"sync-blast-radius",
|
|
25
|
+
"renew-checks-ownership",
|
|
26
|
+
"write-checks-token",
|
|
27
|
+
"release-by-owner",
|
|
28
|
+
"owner-reacquire"
|
|
29
|
+
],
|
|
30
|
+
"prior_prompts": [
|
|
31
|
+
"Why does the nightly sync need a lease at all? Say what went wrong without one.",
|
|
32
|
+
"A worker holding the lease pauses longer than its TTL, and another worker takes over. Why is the lease alone not enough here, and what stops the first worker's late writes?",
|
|
33
|
+
"What makes a write under an old fencing token fail?",
|
|
34
|
+
"When can a second worker take a lease that another worker holds?",
|
|
35
|
+
"Why does the lease store take its clock as a parameter that defaults to a monotonic clock?",
|
|
36
|
+
"How does the leader loop keep the lease while work runs, and what happens to the lease when the work fails or the lease is lost?",
|
|
37
|
+
"If this change is reverted while a worker holds a lease, what happens to that lease?",
|
|
38
|
+
"Which part of the system can a bug in the lease store break?"
|
|
39
|
+
],
|
|
40
|
+
"out": "exam.json"
|
|
41
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
{
|
|
2
|
+
"q1": "Two workers sometimes ran the nightly sync at once and overwrote each other's results; the lease lets only one of them run it.",
|
|
3
|
+
"q2": "The paused worker still thinks it is the leader, so the lease alone does not stop it. Each write passes the fencing token from acquire and the store raises LeaseLost when it is not the current token.",
|
|
4
|
+
"q6": "It renews the lease after each round of work."
|
|
5
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"q1": "Two workers sometimes ran the nightly sync at once and overwrote each other's results; the lease lets only one of them run it.",
|
|
3
|
+
"q2": "The paused worker still thinks it is the leader, so the lease alone does not stop it. Each write passes the fencing token from acquire and the store raises LeaseLost when it is not the current token.",
|
|
4
|
+
"q3": 1,
|
|
5
|
+
"q4": 2,
|
|
6
|
+
"q5": 0,
|
|
7
|
+
"q6": "It renews the lease after each round of work.",
|
|
8
|
+
"q7": 0,
|
|
9
|
+
"q8": 0
|
|
10
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"q1": {
|
|
3
|
+
"grade": "pass",
|
|
4
|
+
"feedback": "Names both the concurrent runs overwriting each other's results and the lease limiting the sync to one worker at a time."
|
|
5
|
+
},
|
|
6
|
+
"q2": {
|
|
7
|
+
"grade": "pass",
|
|
8
|
+
"feedback": "Says the paused worker still believes it leads and that the store compares each write's fencing token with the current one, raising LeaseLost on a stale token."
|
|
9
|
+
},
|
|
10
|
+
"q6": {
|
|
11
|
+
"grade": "partial",
|
|
12
|
+
"feedback": "Names renewing between rounds of work, but leaves out that the loop stops when the lease is lost and that the lease is always released in a finally block, on failure too."
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "q1",
|
|
4
|
+
"prompt": "Why does the nightly sync need a lease at all? Say what went wrong without one.",
|
|
5
|
+
"rubric": {
|
|
6
|
+
"pass": "Says two workers could run the sync concurrently and overwrite each other's results, and that the lease lets only one worker run it at a time.",
|
|
7
|
+
"partial": "Says the lease gives a single worker, or names concurrent runs, without saying that results were overwritten."
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"id": "q2",
|
|
12
|
+
"prompt": "A worker holding the lease pauses longer than its TTL, and another worker takes over. Why is the lease alone not enough here, and what stops the first worker's late writes?",
|
|
13
|
+
"rubric": {
|
|
14
|
+
"pass": "Says the paused worker still believes it leads, and that each write carries a fencing token the store compares with the current one, refusing the stale token.",
|
|
15
|
+
"partial": "Mentions the fencing token or the stale leader, but not how the store uses the token to refuse the late write."
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "q6",
|
|
20
|
+
"prompt": "How does the leader loop keep the lease while work runs, and what happens to the lease when the work fails or the lease is lost?",
|
|
21
|
+
"rubric": {
|
|
22
|
+
"pass": "Says the loop renews the lease between units of work, stops when the lease is lost, and always releases the lease in a finally block.",
|
|
23
|
+
"partial": "Names renewing or releasing, but not both, or does not say the release happens on failure too."
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Adds a lease so that only one worker runs the nightly sync at a time. Before this, two workers sometimes ran it at once and overwrote each other's results.
|
|
2
|
+
|
|
3
|
+
Why: a worker that stalls past its TTL must not keep writing after another worker took over, so every write carries a fencing token and the store refuses a stale one.
|
|
4
|
+
|
|
5
|
+
```merge-risk v1
|
|
6
|
+
risk: medium
|
|
7
|
+
rollback: revert; leases expire after the TTL
|
|
8
|
+
blast-radius: the nightly sync only
|
|
9
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
diff --git a/lease.py b/lease.py
|
|
2
|
+
new file mode 100644
|
|
3
|
+
--- /dev/null
|
|
4
|
+
+++ b/lease.py
|
|
5
|
+
@@ -0,0 +1,61 @@
|
|
6
|
+
+import time
|
|
7
|
+
+
|
|
8
|
+
+
|
|
9
|
+
+class LeaseLost(Exception):
|
|
10
|
+
+ """The caller no longer owns the lease it tried to write under."""
|
|
11
|
+
+
|
|
12
|
+
+
|
|
13
|
+
+class LeaseStore:
|
|
14
|
+
+ """Single-writer lease over a key-value store.
|
|
15
|
+
+
|
|
16
|
+
+ Two workers can both believe they lead after a pause (GC, network). A
|
|
17
|
+
+ fencing token lets the store reject the older leader's late writes.
|
|
18
|
+
+ """
|
|
19
|
+
+
|
|
20
|
+
+ def __init__(self, backend, ttl=30, clock=time.monotonic):
|
|
21
|
+
+ self.backend = backend
|
|
22
|
+
+ self.ttl = ttl
|
|
23
|
+
+ self.clock = clock
|
|
24
|
+
+
|
|
25
|
+
+ def acquire(self, name, owner):
|
|
26
|
+
+ now = self.clock()
|
|
27
|
+
+ held = self.backend.get(name)
|
|
28
|
+
+ if held and held["expires"] > now and held["owner"] != owner:
|
|
29
|
+
+ return None
|
|
30
|
+
+ token = (held["token"] + 1) if held else 1
|
|
31
|
+
+ self.backend.put(name, {"owner": owner, "token": token, "expires": now + self.ttl})
|
|
32
|
+
+ return token
|
|
33
|
+
+
|
|
34
|
+
+ def renew(self, name, owner, token):
|
|
35
|
+
+ held = self.backend.get(name)
|
|
36
|
+
+ if not held or held["owner"] != owner or held["token"] != token:
|
|
37
|
+
+ raise LeaseLost(name)
|
|
38
|
+
+ held["expires"] = self.clock() + self.ttl
|
|
39
|
+
+ self.backend.put(name, held)
|
|
40
|
+
+
|
|
41
|
+
+ def write(self, name, token, key, value):
|
|
42
|
+
+ held = self.backend.get(name)
|
|
43
|
+
+ if not held or held["token"] != token:
|
|
44
|
+
+ raise LeaseLost(name)
|
|
45
|
+
+ self.backend.put(key, value)
|
|
46
|
+
+
|
|
47
|
+
+ def release(self, name, owner, token):
|
|
48
|
+
+ held = self.backend.get(name)
|
|
49
|
+
+ if held and held["owner"] == owner and held["token"] == token:
|
|
50
|
+
+ held["expires"] = 0
|
|
51
|
+
+ self.backend.put(name, held)
|
|
52
|
+
+
|
|
53
|
+
+
|
|
54
|
+
+def run_leader(store, name, owner, work, interval=10):
|
|
55
|
+
+ token = store.acquire(name, owner)
|
|
56
|
+
+ if token is None:
|
|
57
|
+
+ return False
|
|
58
|
+
+ try:
|
|
59
|
+
+ while work(token):
|
|
60
|
+
+ store.renew(name, owner, token)
|
|
61
|
+
+ time.sleep(interval)
|
|
62
|
+
+ except LeaseLost:
|
|
63
|
+
+ return False
|
|
64
|
+
+ finally:
|
|
65
|
+
+ store.release(name, owner, token)
|
|
66
|
+
+ return True
|