Coverage for packages/workshop/src/livery/workshop/_workflow_engine.py: 86%
Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop/tests/test_workshop_workflow.py, packages/workshop/tests/test_workshop_without_docs.py, packages/workshop/tests/test_workshop_vocabulary.py, packages/workshop/tests/test_workshop_update.py, packages/workshop/tests/test_workshop_sync.py, packages/workshop/tests/test_workshop_shells.py, packages/workshop/tests/test_workshop_release_point.py, packages/workshop/tests/test_workshop_release_fresh.py, packages/workshop/tests/test_workshop_release_driver.py, packages/workshop/tests/test_workshop_publish.py, packages/workshop/tests/test_workshop_prose.py, packages/workshop/tests/test_workshop_points.py, packages/workshop/tests/test_workshop_new_project.py, packages/workshop/tests/test_workshop_layering.py, packages/workshop/tests/test_workshop_groups.py, packages/workshop/tests/test_workshop_governance.py, packages/workshop/tests/test_workshop_e2e.py, packages/workshop/tests/test_workshop_dispatch.py, packages/workshop/tests/test_workshop_dev_release.py, packages/extensions/python/tests/test_release_driver.py, packages/extensions/nanobind/tests/test_extensions_nanobind.py, packages/extensions/docs/tests/test_task_links.py, packages/extensions/conan/tests/test_cross_kind.py ran.
125 statements
109 run
16 missing
11 excluded
5 partial
?
Keys r m x p show or hide the lines of a kindj k the next or previous highlighted chunk0 the top of the page1 the first highlighted chunk[ ] the previous or next fileu the index? show or hide this help
1 """The shared reserved-branch engine and its one kind-specific seam.
3 Releasing and updating are the same shape underneath: cut a
4 ``workflow/<name>`` branch, do some work, submit through
5 livery.workshop._submit.submit_flow, and either publish or just
6 merge. This module runs that shared lifecycle; a
7 ``WorkflowDriver`` plugs in only ``prepare`` (do the work) and
8 ``on_merged`` (a release publishes and tags; an update does
9 nothing). Re-running the engine at any point is the recovery: the
10 decision extension reads the state and does the right thing.
13 from __future__ import annotations 12 ctx
15 from dataclasses import dataclass 12 ctx
16 from pathlib import Path 12 ctx
17 from typing import Protocol 12 ctx
19 import livery . footman as footman 12 ctx
20 from livery import git 12 ctx
21 from livery . footman import fail 12 ctx
22 from livery . forge import Repository 12 ctx
23 from livery . workshop . _workflow_decision import ( 12 ctx
27 from livery . workshop . _workflow_state import ( 12 ctx
34 #: A release in one of these states parks a coexisting update's arm.
35 #: UNKNOWN counts: a gather blip must not read as "no release running".
36 PARKS_UPDATES = frozenset ( 12 ctx
38 WorkflowState . IN_PROGRESS ,
39 WorkflowState . AWAITING_REVIEW ,
40 WorkflowState . UNKNOWN ,
45 @dataclass ( frozen = True ) 12 ctx
46 class Submission : 12 ctx
47 """What a driver's prepare hands back for the shared submit.
49 None from prepare means nothing to submit (already up to date, or
50 the work completed on its own).
57 class WorkflowDriver ( Protocol ) : 12 ctx
58 """The only kind-specific seam; everything else is shared.
60 Structural, so a driver provides these members and inherits
61 nothing. ``branch`` is always ``workflow/<name>``: one active
62 workflow per name, the branch name is the identity, different
66 name : str # "release/forge+workshop" | "update/dependencies" | ... 12 ctx
67 kind : WorkflowKind 12 ctx
69 members : tuple [ str , . . . ] # a release's package directories 12 ctx
70 watch : float # seconds an armed act follows its pull request; 0 returns at once 12 ctx
73 def branch ( self ) - > str : 12 ctx
74 """The reserved branch, ``workflow/<name>``."""
78 def base ( self ) - > str : 12 ctx
79 """The branch the PR merges into."""
82 def prepare ( self ) - > Submission | None : 12 ctx
83 """Do the work; idempotent, called fresh and for recovery."""
86 def discard ( self ) - > None : 12 ctx
87 """Give the prepared branch up, so the next prepare starts from the base."""
90 def on_merged ( self ) - > None : 12 ctx
91 """After the merge: a release publishes and tags; else nothing."""
95 def run_workflow ( 12 ctx
96 driver : WorkflowDriver ,
100 current_user : str | None = None ,
102 """Drive *driver* one step: gather, decide, act.
104 Loops for MERGE_DEFAULT (re-decide on the fresh state) and for a
105 TIDY_THEN_START that removed another workflow's leftover, each
106 other name tidied at most once so the loop cannot spin. STOP
107 raises with its teaching. Everything else terminates.
109 tidied : set [ str ] = set ( ) 19 ctx
111 states = workflow_states ( repo , root , base = driver . base ) 19 ctx
113 ( s for s in states if s . name == driver . name ) ,
114 WorkflowStatus ( kind = None , state = WorkflowState . NONE ) ,
116 others = tuple ( s for s in states if s . name != driver . name ) 19 ctx
117 decision = workflow_decision ( 19 ctx
121 members = driver . members ,
122 branch = git . current_branch ( root ) ,
123 dirty = not git . is_clean ( root ) ,
124 behind_default = _behind_default ( root , driver . base ) ,
125 default_branch = driver . base ,
126 current_user = ( _forge_user ( repo ) if current_user is None else current_user ) ,
129 action = decision . action 19 ctx
131 # A bare repeat call into your own armed in-flight workflow:
132 # the idempotent submit disarms whenever the arm choice is
133 # off, so a status-check re-run would take the live schedule
134 # away while announcing a retry. Say what is true and stop.
136 action is WorkflowAction . RETRY
137 and wf . state is WorkflowState . IN_PROGRESS
138 and wf . armed is not False
142 f " { driver . name } is already in flight and armed. Follow it "
143 f " with ` { footman . prog ( ) } status --watch --workflow`, or re-run with "
144 " --armed to re-assert the schedule deliberately. "
147 message = decision . message 19 ctx
148 if action is WorkflowAction . ARM and not driver . armed : 148 ↛ 149line 148 didn't jump to line 149 because the condition on line 148 was never true 19 ctx
150 f " The ready { driver . name } PR stays unarmed; pass --armed to "
154 print ( f " { message } " ) 5 ctx
156 if action is WorkflowAction . STOP : 19 ctx
157 raise SystemExit ( f " { decision . message } " ) 1 ctx
159 if action is WorkflowAction . MERGE_DEFAULT : 18 ctx
160 _merge_default ( root , driver . base ) 1 ctx
163 # A stale set re-derives in place: the prepare starts from the
164 # base again and the push replaces the branch under the same
167 if action is WorkflowAction . REPREPARE : 167 ↛ 168line 167 didn't jump to line 168 because the condition on line 167 was never true 18 ctx
171 if action is WorkflowAction . TIDY_THEN_START : 18 ctx
172 target = decision . tidy_target 2 ctx
173 tidy_leftover ( repo , root , target or wf , base = driver . base ) 2 ctx
174 if target is not None and target . name not in tidied : 174 ↛ 184line 174 didn't jump to line 184 because the condition on line 174 was always true 2 ctx
175 tidied . add ( target . name ) 2 ctx
177 elif action is WorkflowAction . REOPEN : 18 ctx
178 _reopen ( repo , driver . branch ) 1 ctx
180 # The parking rule: while any release is live, a coexisting
181 # update submits UNARMED, and the note teaches the re-run
182 # (which also raises floors) before arming. The verify
183 # backstop holds even when someone arms anyway.
184 arm = driver . armed 18 ctx
185 if arm and driver . kind is not WorkflowKind . RELEASE : 18 ctx
189 if other . kind is WorkflowKind . RELEASE and other . state in PARKS_UPDATES
194 f " Release { parked_by [ 0 ] } is in flight: submitting "
195 " unarmed. After it lands, re-run "
196 f " ` { footman . prog ( ) } workflow.update` "
197 " (the re-run also raises floors to the fresh release), "
201 started = git . current_branch ( root ) 18 ctx
202 submission = driver . prepare ( ) 18 ctx
203 if submission is None : 18 ctx
204 print ( " nothing to submit: everything is already current " ) 2 ctx
206 from livery . workshop . _submit import submit_flow 16 ctx
208 # The engine submits and reads the immediate outcome; the
209 # long watch belongs to the drivers' own surfaces, which
210 # know their done oracle (a release watches its receipts).
214 title = submission . title ,
215 body = submission . body ,
218 armed_reason = " the workflow engine " ,
220 follow_to_verdict = False ,
223 _leave_reserved ( root , driver , started ) 16 ctx
224 if arm and driver . watch > 0 and not _merged ( repo , root , driver . branch ) : 16 ctx
225 # An armed act waits the way `submit --armed` waits: from
226 # the branch it returned to, since the pull request holds
227 # the reserved branch on the remote. A blocker exits with
228 # its code, the way the submit does.
229 from livery . workshop . _verdict import follow_pull_request 1 ctx
231 print ( f " following PR of { driver . branch } to its merge " ) 1 ctx
232 follow_pull_request ( repo , driver . branch , root , timeout = driver . watch ) 1 ctx
233 if _merged ( repo , root , driver . branch ) : 16 ctx
234 driver . on_merged ( ) 11 ctx
238 def _leave_reserved ( root : Path , driver : WorkflowDriver , started : str ) - > None : 12 ctx
239 """Return to the branch the act started from; the reserved branch lives on origin.
241 The pull request holds the reserved branch on the remote, and the
242 driver's recovery fetches it from there, so the local copy is a
243 leftover the moment the submit returns. An act started on the
244 reserved branch itself stays there: a person chose it.
248 or started == driver . branch
249 or git . current_branch ( root ) != driver . branch
252 git . switch ( root , started ) 14 ctx
253 git . delete_branch ( root , driver . branch ) 14 ctx
255 f " back on { started } ; { driver . branch } stays on origin under its pull request "
259 def tidy_leftover ( 12 ctx
260 repo : Repository , root : Path , wf : WorkflowStatus , * , base : str
262 """Tear down a finished workflow's branches; safe, it is merged.
264 *wf* is the decision's tidy target, never assumed to be the
265 driver's own branch: passing a live branch here would close its
266 live PR, the exact mistake the target field prevents.
268 if wf . branch : 268 ↛ exitline 268 didn't return from function 'tidy_leftover' because the condition on line 268 was always true 2 ctx
269 from livery . workshop . _submit import teardown_branch 2 ctx
271 teardown_branch ( repo , root , wf . branch , base ) 2 ctx
274 def _merged ( repo : Repository , root : Path , branch : str ) - > bool : 12 ctx
275 """Whether *branch*'s pull request has merged, asked of the forge.
277 A forge that does not answer is asked again within the transient
278 budget, and then the engine stops with the forge's words: a
279 failure reason is printed, never read as "not merged".
281 from livery . workshop . _forge_lane import pull_request_for 15 ctx
282 from livery . workshop . _verdict import ForgeUnreachable , follow 15 ctx
287 lambda : pull_request_for ( repo , root , branch ) ,
288 subject = f " the pull request of { branch } " ,
293 except ForgeUnreachable as exc :
294 fail ( exc . line . strip ( ) )
295 return pr is not None and pr . merged 15 ctx
298 def _forge_user ( repo : Repository ) - > str : 12 ctx
299 """Who this invocation is, in the forge's namespace.
301 The forge identity, because a workflow's author comes from the
302 PR; a local git name is a different namespace and on a bot
303 checkout can never match. Offline answers empty, which the
304 decision treats as "cannot compare", never as a refusal.
307 from livery . workshop . _extensions import workspace_root
308 from livery . workshop . _forge_lane import this_forge
310 root = workspace_root ( )
313 return this_forge ( root ) . whoami ( )
318 def _behind_default ( root : Path , base : str ) - > int : 12 ctx
319 """Commits on origin/*base* not in HEAD; 0 when none or offline."""
321 git . fetch ( root ) 19 ctx
322 return git . behind_base ( root , base ) 19 ctx
327 def _merge_default ( root : Path , base : str ) - > None : 12 ctx
328 """Merge origin/*base* in; a conflict stops with the resolution path."""
330 git . integrate ( root , base ) 2 ctx
331 except git . GitError as exc : 1 ctx
332 raise SystemExit ( 1 ctx
333 f " The merge from { base } stopped on a conflict. Resolve it, "
334 f " commit, and run the workflow again to resume. \n { exc } "
338 def _reopen ( repo : Repository , branch : str ) - > None : 12 ctx
339 """Reopen the closed PR for *branch*, reusing it over a duplicate."""
340 pr = repo . pr . find_by_head ( branch , state = " all " ) 1 ctx
341 if pr is not None and not pr . merged and pr . state == " closed " : 341 ↛ exitline 341 didn't return from function '_reopen' because the condition on line 341 was always true 1 ctx
342 repo . pr . reopen ( pr . number ) 1 ctx
343 print ( f " reopened PR # { pr . number } " ) 1 ctx