@jesdi/skills 0.0.101 → 0.0.103
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 +151 -0
- package/skills/pr-exam/fixtures/README.md +23 -0
- package/skills/pr-exam/fixtures/chat-answer/prompt.md +92 -0
- package/skills/pr-exam/fixtures/chat-answer/reply.md +7 -0
- package/skills/pr-exam/fixtures/chat-answer/turn.json +1 -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/tip-choice/prompt.md +97 -0
- package/skills/pr-exam/fixtures/tip-choice/reply.md +1 -0
- package/skills/pr-exam/fixtures/tip-choice/turn.json +1 -0
- package/skills/pr-exam/fixtures/tip-text/prompt.md +97 -0
- package/skills/pr-exam/fixtures/tip-text/reply.md +1 -0
- package/skills/pr-exam/fixtures/tip-text/turn.json +1 -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 +887 -0
- package/skills/pr-exam/test_config.py +83 -0
- package/skills/pr-exam/test_context.py +174 -0
- package/skills/pr-exam/test_exam_format.py +252 -0
- package/skills/pr-exam/test_flag_shapes.py +18 -0
- package/skills/pr-exam/test_followup_delta.py +90 -0
- package/skills/pr-exam/test_learning_records.py +355 -0
- package/skills/pr-exam/test_record_publication.py +45 -0
- package/skills/pr-exam/test_scoring.py +287 -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 +136 -0
- package/skills/pr-exam/test_turn_shapes.py +34 -0
- package/skills-manifest.json +9 -3
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: pr-exam
|
|
3
|
+
description: >-
|
|
4
|
+
Deterministic rules for a PR exam: validate a generated exam.json against its
|
|
5
|
+
generation request, project the served (answer-free) exam, score an attempt,
|
|
6
|
+
read and write learning records, and validate a repo's .pr-exam.json. Use when
|
|
7
|
+
generating, serving or grading a PR exam, or when the user says "pr-exam".
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# pr-exam
|
|
11
|
+
|
|
12
|
+
Every exam rule lives in `pr_exam.py`, next to this file. Never re-implement one: run the script.
|
|
13
|
+
Each command reads files and prints JSON, or exits non-zero with every error on
|
|
14
|
+
stderr.
|
|
15
|
+
|
|
16
|
+
| Command | Does |
|
|
17
|
+
|---|---|
|
|
18
|
+
| `python3 pr_exam.py validate --request <request.json> <exam.json>` | Checks a generated exam against its request; prints its summary. |
|
|
19
|
+
| `python3 pr_exam.py public <exam.json>` | Prints the exam the page may show: no key, concepts or answers. |
|
|
20
|
+
| `python3 pr_exam.py score --exam <exam.json> --answers <answers.json> --grades <grades.json> [--flags <flags.json>]` | Scores an attempt and names what was missed. `--flags` holds `{"bypassed": [id], "assisted": [id, ...]}`: one bypassed question leaves the score; both are reported as `help`. |
|
|
21
|
+
| `python3 pr_exam.py learned --records <dir> --today <YYYY-MM-DD>` | Lists concepts known, due or wrong. |
|
|
22
|
+
| `python3 pr_exam.py record --records <dir> --exam <exam.json> --result <result.json> --today <YYYY-MM-DD>` | Writes learning records for a scored attempt: `demonstrated`, `assisted` (a full point with help, always `due`) or `misconception`. An older `pr_exam.py` refuses an `assisted` record, so never roll the host pin back past this version. |
|
|
23
|
+
| `python3 pr_exam.py context --exam <exam.json> --diff <diff> --body <body> --turn <turn.json>` | Prints the prompt for one tip or chat turn: the section below for its kind, the diff, the body, then the turn. |
|
|
24
|
+
| `python3 pr_exam.py config [<.pr-exam.json>]` | Reads and validates the repo's exam config. |
|
|
25
|
+
| `python3 pr_exam.py setup [--check] <owner/repo>` | Run by the operator: makes `exam` a required check of the default branch. `--check` only reports whether it is one. |
|
|
26
|
+
|
|
27
|
+
## Generating an exam
|
|
28
|
+
|
|
29
|
+
You get a generation request, a JSON file with these fields:
|
|
30
|
+
|
|
31
|
+
| Field | Holds |
|
|
32
|
+
|---|---|
|
|
33
|
+
| `mode` | `first`, `followup` or `delta`: see "Modes". |
|
|
34
|
+
| `repo`, `pr`, `head_sha` | The PR. Copy them into the exam unchanged. |
|
|
35
|
+
| `diff` | Path of the PR's diff. |
|
|
36
|
+
| `body` | Path of the PR's body. Its merge-risk block names the risks. |
|
|
37
|
+
| `glossary` | Path of the repo's `GLOSSARY.md`, or null. When set, use its terms in titles and prompts. |
|
|
38
|
+
| `records`, `today` | The learning-record directory and the date: the arguments of `learned`. |
|
|
39
|
+
| `only_concepts`, `prior_prompts` | `followup` only: the concepts to ask, and the prompts already asked. |
|
|
40
|
+
| `prior_concepts` | `delta` only: the concepts an earlier exam of this PR covered. |
|
|
41
|
+
| `passed_diff` | `delta` only, when present: path of the PR's diff as it was when the exam was passed. |
|
|
42
|
+
| `out` | Where to write `exam.json`. |
|
|
43
|
+
|
|
44
|
+
Write this shape to `out`. A question has no field other than the ones shown:
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"version": 1,
|
|
49
|
+
"mode": "first",
|
|
50
|
+
"repo": "owner/name", "pr": 240, "head_sha": "<from the request>",
|
|
51
|
+
"concepts": [
|
|
52
|
+
{"id": "merge-lease", "title": "Merge lease", "kind": "why"},
|
|
53
|
+
{"id": "retry-budget", "title": "Retry budget", "kind": "concept"}
|
|
54
|
+
],
|
|
55
|
+
"questions": [
|
|
56
|
+
{"id": "q1", "concept": "merge-lease", "type": "text", "prompt": "…"},
|
|
57
|
+
{"id": "q2", "concept": "retry-budget", "type": "choice", "prompt": "…", "options": ["…", "…"]}
|
|
58
|
+
],
|
|
59
|
+
"key": {
|
|
60
|
+
"q1": {"rubric": {"pass": "…", "partial": "…"}, "explanation": "…", "hunk": "…"},
|
|
61
|
+
"q2": {"answer": 1, "explanation": "…", "hunk": "…"}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
1. List every concept you find in `concepts`, asked or not. A concept `id` is a slug (lowercase
|
|
67
|
+
letters, digits, single hyphens). Its `kind` is one of these, highest priority first:
|
|
68
|
+
- `why`: the reason behind a decision;
|
|
69
|
+
- `concept`: a new concept or domain term;
|
|
70
|
+
- `architecture`: a change of structure or boundary;
|
|
71
|
+
- `risk`: a risky area, from the merge-risk block of the body;
|
|
72
|
+
- `behaviour`: behaviour a user can see.
|
|
73
|
+
|
|
74
|
+
Never list or ask implementation trivia: names, line counts, formatting, syntax.
|
|
75
|
+
2. One question per concept, 8 at most. With more than 8 concepts, ask the 8 of the highest-priority
|
|
76
|
+
kinds and leave the others listed, unasked. A trivial PR (a typo, a rename, a comment) gets 1 question.
|
|
77
|
+
3. Run `python3 pr_exam.py learned --records <records> --today <today>` and reuse the id of a concept
|
|
78
|
+
already listed there. `skip`: list it, do not ask it. `due`: ask it as `choice` only.
|
|
79
|
+
`misconception`: list it and ask it, unless the cap of 8 drops it.
|
|
80
|
+
4. Type mix: `choice` (multiple choice) and `text` (free text). With 2 questions or more use both;
|
|
81
|
+
with 3 or more, at least one `text` question is on a `why` concept.
|
|
82
|
+
5. A `choice` has at least 2 options with the same word count, no two equal, and nothing in the
|
|
83
|
+
formatting that hints at the answer. This is retrieval practice: ask what the reader must recall,
|
|
84
|
+
not what the diff shows.
|
|
85
|
+
6. Write the key before any answer exists, one entry per question id. Every entry has an
|
|
86
|
+
`explanation` and a `hunk` copied verbatim from the diff. A `choice` entry has `answer` (an index
|
|
87
|
+
into `options`) and no rubric. A `text` entry has a `rubric` with `pass` and `partial`, saying
|
|
88
|
+
what a passing and a partial answer must contain, and no answer.
|
|
89
|
+
7. Run `python3 pr_exam.py validate --request <request.json> <exam.json>` and fix every error it names,
|
|
90
|
+
then run it again. Finish only on exit 0.
|
|
91
|
+
|
|
92
|
+
### Modes
|
|
93
|
+
|
|
94
|
+
- `first`: the rules above.
|
|
95
|
+
- `followup`: one new question per concept in `only_concepts`, and no other concept: `concepts` lists
|
|
96
|
+
exactly those. Do not repeat a prompt in `prior_prompts`: ask the concept from another angle. The
|
|
97
|
+
type mix of step 4 does not apply.
|
|
98
|
+
- `delta`: list and ask only concepts absent from `prior_concepts`: the ones the last push added.
|
|
99
|
+
Compare `diff` with `passed_diff` to find them. The type mix of step 4 does not apply. When
|
|
100
|
+
nothing is new, write an empty exam: zero questions, and `questions`, `concepts` and `key` empty.
|
|
101
|
+
|
|
102
|
+
## Grading answers
|
|
103
|
+
|
|
104
|
+
One fresh session grades every `text` question of one attempt. You get a grading request, a JSON
|
|
105
|
+
file with these fields:
|
|
106
|
+
|
|
107
|
+
| Field | Holds |
|
|
108
|
+
|---|---|
|
|
109
|
+
| `mode` | `grade`. |
|
|
110
|
+
| `diff` | Path of the PR's diff. |
|
|
111
|
+
| `questions` | Path of a JSON list, one entry per `text` question: `{"id": "q1", "prompt": "…", "rubric": {"pass": "…", "partial": "…"}}`. |
|
|
112
|
+
| `answers` | Path of a JSON object: the answer to each of those questions, by question id. |
|
|
113
|
+
| `out` | Where to write `grades.json`. |
|
|
114
|
+
|
|
115
|
+
That is all you get: the diff, the questions, their rubrics and the answers, and
|
|
116
|
+
nothing else from generation. No key, no explanation, no earlier attempt.
|
|
117
|
+
|
|
118
|
+
1. Judge each answer against its rubric only: `pass` when it contains what `rubric.pass` asks for,
|
|
119
|
+
`partial` when it contains only what `rubric.partial` asks for, `fail` otherwise. Read the diff
|
|
120
|
+
to check what an answer claims. Wording, length and confidence earn nothing.
|
|
121
|
+
2. Write `grades.json` to `out`, one entry per question in `questions` and no other entry:
|
|
122
|
+
`{"q1": {"grade": "pass", "feedback": "…"}}`. `feedback` is one line of feedback: what the answer
|
|
123
|
+
got right or left out.
|
|
124
|
+
3. Check the shape of what you wrote: an entry for every question id, each `grade` one of `pass`,
|
|
125
|
+
`partial`, `fail`, each `feedback` a one-line string. Then finish.
|
|
126
|
+
|
|
127
|
+
`choice` questions are never graded by the session, and you do not score the attempt: the host runs
|
|
128
|
+
`pr_exam.py score`, which marks them from the key and refuses a grade for one.
|
|
129
|
+
|
|
130
|
+
## Tips
|
|
131
|
+
|
|
132
|
+
A tip helps a reader who is stuck on one question. You get the question, its key, the draft
|
|
133
|
+
and the earlier tips. Climb one rung per tip number:
|
|
134
|
+
|
|
135
|
+
1. Where to look: point at a file or hunk, nothing more.
|
|
136
|
+
2. The missing idea: name what the draft leaves out.
|
|
137
|
+
3. One step away: bring the reader next to the answer, never onto it.
|
|
138
|
+
|
|
139
|
+
React to the draft. Never state the answer, never quote the key. On a `choice` question never
|
|
140
|
+
quote, name or rule out an option. Do not repeat an earlier tip. Reply in two sentences at most.
|
|
141
|
+
|
|
142
|
+
## Chat
|
|
143
|
+
|
|
144
|
+
The chat answers freely, the key included: explain, compare, and say whether the draft is
|
|
145
|
+
right. After a submit, use the submitted answer, its points and its feedback to explain the
|
|
146
|
+
result. Ground every claim in the diff and the PR body, and say so when they do not settle it.
|
|
147
|
+
Be brief.
|
|
148
|
+
|
|
149
|
+
## Never
|
|
150
|
+
|
|
151
|
+
Never apply a bypass label. Never post an `exam` status. The host decides both.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Recorded runs of the `pr-exam` skill
|
|
2
|
+
|
|
3
|
+
Each directory holds the inputs and outputs of one headless session that followed `SKILL.md`, started
|
|
4
|
+
with the prompt the host uses (agent-ops `dispatcher/exam_runner.py`). One live run per mode:
|
|
5
|
+
|
|
6
|
+
- `typo-readme/`: `first`, on a typo-only README diff → `exam.json`, 1 question.
|
|
7
|
+
- `twelve-concepts/`: `first`, on a lease store with fencing tokens, a diff with 12 concepts → `exam.json`,
|
|
8
|
+
8 questions; the 4 behaviour concepts are listed but dropped by priority.
|
|
9
|
+
- `grading/`: one session grading the three `text` answers of an attempt at the twelve-concept exam
|
|
10
|
+
(`request.json`, `questions.json`, `answers.json`) → `grades.json`. `attempt.json` holds every answer
|
|
11
|
+
of that attempt; `score` on it fails the attempt and names three missed concepts.
|
|
12
|
+
- `followup/`: `followup`, on those three concepts, with the learning records `record` wrote for the
|
|
13
|
+
failed attempt → `exam.json`, 3 new questions.
|
|
14
|
+
- `delta-new-concept/`: `delta`, after a pass: the push adds a retry budget (`diff.patch` against
|
|
15
|
+
`passed.diff`) → `exam.json`, 2 questions on concepts no earlier exam covered.
|
|
16
|
+
- `delta-nothing-new/`: `delta`, after a pass: the push only rewords a docstring and renames a
|
|
17
|
+
variable → an empty `exam.json`.
|
|
18
|
+
- `tip-choice/`, `tip-text/`, `chat-answer/`: three live turns on the twelve-concept exam (a tip on a
|
|
19
|
+
`choice` question, tip 1 on a `text` question, a `question` chat turn "what is the answer?"). Each has
|
|
20
|
+
`turn.json`, `prompt.md` (what `context` prints for it) and `reply.md` (`claude -p --tools ''` on the prompt).
|
|
21
|
+
|
|
22
|
+
The requests are written as the host writes them, with paths made relative. Every generation run
|
|
23
|
+
finished on `validate` exit 0. `test_skill_fixtures.py` checks the outputs again.
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
The chat answers freely, the key included: explain, compare, and say whether the draft is
|
|
2
|
+
right. After a submit, use the submitted answer, its points and its feedback to explain the
|
|
3
|
+
result. Ground every claim in the diff and the PR body, and say so when they do not settle it.
|
|
4
|
+
Be brief.
|
|
5
|
+
|
|
6
|
+
Diff:
|
|
7
|
+
diff --git a/lease.py b/lease.py
|
|
8
|
+
new file mode 100644
|
|
9
|
+
--- /dev/null
|
|
10
|
+
+++ b/lease.py
|
|
11
|
+
@@ -0,0 +1,61 @@
|
|
12
|
+
+import time
|
|
13
|
+
+
|
|
14
|
+
+
|
|
15
|
+
+class LeaseLost(Exception):
|
|
16
|
+
+ """The caller no longer owns the lease it tried to write under."""
|
|
17
|
+
+
|
|
18
|
+
+
|
|
19
|
+
+class LeaseStore:
|
|
20
|
+
+ """Single-writer lease over a key-value store.
|
|
21
|
+
+
|
|
22
|
+
+ Two workers can both believe they lead after a pause (GC, network). A
|
|
23
|
+
+ fencing token lets the store reject the older leader's late writes.
|
|
24
|
+
+ """
|
|
25
|
+
+
|
|
26
|
+
+ def __init__(self, backend, ttl=30, clock=time.monotonic):
|
|
27
|
+
+ self.backend = backend
|
|
28
|
+
+ self.ttl = ttl
|
|
29
|
+
+ self.clock = clock
|
|
30
|
+
+
|
|
31
|
+
+ def acquire(self, name, owner):
|
|
32
|
+
+ now = self.clock()
|
|
33
|
+
+ held = self.backend.get(name)
|
|
34
|
+
+ if held and held["expires"] > now and held["owner"] != owner:
|
|
35
|
+
+ return None
|
|
36
|
+
+ token = (held["token"] + 1) if held else 1
|
|
37
|
+
+ self.backend.put(name, {"owner": owner, "token": token, "expires": now + self.ttl})
|
|
38
|
+
+ return token
|
|
39
|
+
+
|
|
40
|
+
+ def renew(self, name, owner, token):
|
|
41
|
+
+ held = self.backend.get(name)
|
|
42
|
+
+ if not held or held["owner"] != owner or held["token"] != token:
|
|
43
|
+
+ raise LeaseLost(name)
|
|
44
|
+
+ held["expires"] = self.clock() + self.ttl
|
|
45
|
+
+ self.backend.put(name, held)
|
|
46
|
+
+
|
|
47
|
+
+ def write(self, name, token, key, value):
|
|
48
|
+
+ held = self.backend.get(name)
|
|
49
|
+
+ if not held or held["token"] != token:
|
|
50
|
+
+ raise LeaseLost(name)
|
|
51
|
+
+ self.backend.put(key, value)
|
|
52
|
+
+
|
|
53
|
+
+ def release(self, name, owner, token):
|
|
54
|
+
+ held = self.backend.get(name)
|
|
55
|
+
+ if held and held["owner"] == owner and held["token"] == token:
|
|
56
|
+
+ held["expires"] = 0
|
|
57
|
+
+ self.backend.put(name, held)
|
|
58
|
+
+
|
|
59
|
+
+
|
|
60
|
+
+def run_leader(store, name, owner, work, interval=10):
|
|
61
|
+
+ token = store.acquire(name, owner)
|
|
62
|
+
+ if token is None:
|
|
63
|
+
+ return False
|
|
64
|
+
+ try:
|
|
65
|
+
+ while work(token):
|
|
66
|
+
+ store.renew(name, owner, token)
|
|
67
|
+
+ time.sleep(interval)
|
|
68
|
+
+ except LeaseLost:
|
|
69
|
+
+ return False
|
|
70
|
+
+ finally:
|
|
71
|
+
+ store.release(name, owner, token)
|
|
72
|
+
+ return True
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
PR body:
|
|
76
|
+
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.
|
|
77
|
+
|
|
78
|
+
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.
|
|
79
|
+
|
|
80
|
+
```merge-risk v1
|
|
81
|
+
risk: medium
|
|
82
|
+
rollback: revert; leases expire after the TTL
|
|
83
|
+
blast-radius: the nightly sync only
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
Question: {"id": "q3", "concept": "fencing-token", "type": "choice", "prompt": "What makes a write under an old fencing token fail?", "options": ["The token encodes the owner's process identifier", "Every new acquisition increments the stored token", "Each token carries its own signed expiry"]}
|
|
88
|
+
Key: {"explanation": "acquire stores the previous token plus one, so the newest holder always has the highest token and any older token no longer matches.", "hunk": "+ token = (held[\"token\"] + 1) if held else 1", "answer": 1}
|
|
89
|
+
|
|
90
|
+
Draft:
|
|
91
|
+
|
|
92
|
+
Message: what is the answer?
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
The answer is **"Every new acquisition increments the stored token."**
|
|
2
|
+
|
|
3
|
+
- **Why that's right:** `acquire` stores `held["token"] + 1`, so each new holder gets a higher token than the last. `write` then checks `held["token"] != token` and raises `LeaseLost`. An older leader's token no longer matches the stored one, so its late write is refused. This is the behaviour the PR body describes: "the store refuses a stale one."
|
|
4
|
+
- **Why not the owner's process identifier:** the token is a plain integer counter. `write` never looks at the owner at all.
|
|
5
|
+
- **Why not a signed expiry:** the expiry is stored in the lease record, not in the token, and nothing is signed. `write` doesn't check expiry either. It only compares tokens.
|
|
6
|
+
|
|
7
|
+
One caveat from the diff: `write` checks for an exact match, not "is this the highest token." It works here only because the stored token only ever goes up.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"kind":"question","question":"q3","draft":"","thread":[],"message":"what is the answer?"}
|
|
@@ -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,82 @@
|
|
|
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,77 @@
|
|
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 acquire_with_retry(store, name, owner, attempts=5, base_delay=0.5, sleep=time.sleep):
|
|
55
|
+
+ """Take the lease within a retry budget, backing off between tries.
|
|
56
|
+
+
|
|
57
|
+
+ A worker that starts while another one leads used to give up at once, so a
|
|
58
|
+
+ leader that died left the sync unrun until the next night. Retrying forever
|
|
59
|
+
+ would hammer the store instead. The budget bounds both: after `attempts`
|
|
60
|
+
+ tries with doubling delays the caller skips this run.
|
|
61
|
+
+ """
|
|
62
|
+
+ for attempt in range(attempts):
|
|
63
|
+
+ token = store.acquire(name, owner)
|
|
64
|
+
+ if token is not None:
|
|
65
|
+
+ return token
|
|
66
|
+
+ sleep(base_delay * 2 ** attempt)
|
|
67
|
+
+ return None
|
|
68
|
+
+
|
|
69
|
+
+
|
|
70
|
+
+def run_leader(store, name, owner, work, interval=10):
|
|
71
|
+
+ token = acquire_with_retry(store, name, owner)
|
|
72
|
+
+ if token is None:
|
|
73
|
+
+ return False
|
|
74
|
+
+ try:
|
|
75
|
+
+ while work(token):
|
|
76
|
+
+ store.renew(name, owner, token)
|
|
77
|
+
+ time.sleep(interval)
|
|
78
|
+
+ except LeaseLost:
|
|
79
|
+
+ return False
|
|
80
|
+
+ finally:
|
|
81
|
+
+ store.release(name, owner, token)
|
|
82
|
+
+ return True
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"mode": "delta",
|
|
4
|
+
"repo": "jesdi/general-skills",
|
|
5
|
+
"pr": 240,
|
|
6
|
+
"head_sha": "cccccccccccccccccccccccccccccccccccccccc",
|
|
7
|
+
"concepts": [
|
|
8
|
+
{"id": "retry-budget", "title": "Retry budget for taking the lease", "kind": "why"},
|
|
9
|
+
{"id": "exponential-backoff", "title": "Exponential backoff between acquire attempts", "kind": "concept"}
|
|
10
|
+
],
|
|
11
|
+
"questions": [
|
|
12
|
+
{
|
|
13
|
+
"id": "q1",
|
|
14
|
+
"concept": "retry-budget",
|
|
15
|
+
"type": "text",
|
|
16
|
+
"prompt": "A worker that finds the lease held now retries within a bounded budget instead of trying once. Why is one attempt not enough, why is retrying without limit not acceptable either, and what does the worker do when the budget runs out?"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"id": "q2",
|
|
20
|
+
"concept": "exponential-backoff",
|
|
21
|
+
"type": "choice",
|
|
22
|
+
"prompt": "While a worker keeps failing to take the lease, how does the wait between its attempts change?",
|
|
23
|
+
"options": [
|
|
24
|
+
"It stays the same throughout",
|
|
25
|
+
"It grows by fixed steps",
|
|
26
|
+
"It doubles after each failure",
|
|
27
|
+
"It shrinks after each failure"
|
|
28
|
+
]
|
|
29
|
+
}
|
|
30
|
+
],
|
|
31
|
+
"key": {
|
|
32
|
+
"q1": {
|
|
33
|
+
"rubric": {
|
|
34
|
+
"pass": "Names both failure modes the budget bounds: (1) giving up after a single attempt means that if the current leader dies, nobody takes over and the sync stays unrun until the next night; (2) retrying forever would hammer the store. Also says that once the budget is exhausted the worker skips this run (does not lead, returns without syncing).",
|
|
35
|
+
"partial": "Names only one of the two failure modes (the sync left unrun after a leader dies, or the store being hammered by endless retries), or names both but does not say that the worker skips the run when the budget is exhausted."
|
|
36
|
+
},
|
|
37
|
+
"explanation": "With a single attempt, a worker that starts while another leads gives up at once, so a leader that later dies leaves the sync unrun until the next night. Unlimited retries would hammer the store. The budget bounds both: after a fixed number of tries the worker gets no token and skips this run.",
|
|
38
|
+
"hunk": "+ A worker that starts while another one leads used to give up at once, so a\n+ leader that died left the sync unrun until the next night. Retrying forever\n+ would hammer the store instead. The budget bounds both: after `attempts`\n+ tries with doubling delays the caller skips this run."
|
|
39
|
+
},
|
|
40
|
+
"q2": {
|
|
41
|
+
"answer": 2,
|
|
42
|
+
"explanation": "The delay is the base delay times two to the power of the attempt number, so it doubles after every failed attempt: exponential backoff, which eases the load on the store the longer the lease stays held.",
|
|
43
|
+
"hunk": "+ sleep(base_delay * 2 ** attempt)"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -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,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: 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": "cccccccccccccccccccccccccccccccccccccccc",
|
|
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
|
+
```
|