2323)
2424
2525
26- def _stamp_locked (page_dir : Path , page , body : str , completes : tuple [str , ...]) -> dict :
27- events = page .events
26+ def _stamp_activation (page_dir : Path , events : list ):
2827 activation = activate_source (page_dir , events , allow_transition = True )
2928 if activation .error or activation .revision is None :
3029 detail = activation .error or "index.html produced no revision"
3130 sys .exit (f"refusing to stamp index.html: { detail } " )
31+ return activation
32+
3233
34+ def _stamp_reading (page_dir : Path , events : list , activation ):
35+ revision = activation .revision
36+ if existing := stamped_version (events , revision ):
37+ sys .exit (f"revision r{ revision } is already stamped as v{ existing } " )
38+ checked = activation .check
39+ registry = checked .registry
40+ if registry is None :
41+ sys .exit ("refusing to stamp index.html: the page has no registry.json" )
42+ projection , parser , spk = page_projection (checked .html , events , registry , revision )
43+ return checked , registry , projection , parser , spk
44+
45+
46+ def _completed_work (
47+ checked ,
48+ parser ,
49+ projection ,
50+ events : list ,
51+ page ,
52+ registry : dict ,
53+ revision : int ,
54+ completes : tuple [str , ...],
55+ ) -> set [str ]:
56+ if len (set (completes )) != len (completes ):
57+ sys .exit ("--completes names each widget at most once" )
58+ completed = set (completes )
59+ widget_work = {
60+ claim ["subject" ]["id" ]: claim
61+ for claim in standing_work_claims (page .status , events )
62+ if claim ["subject" ]["kind" ] == "widget"
63+ }
64+ unearned = sorted (completed - widget_work .keys ())
65+ if unearned :
66+ sys .exit (
67+ "no active widget work claim for "
68+ + ", " .join (repr (widget ) for widget in unearned )
69+ )
70+ not_later = sorted (
71+ widget
72+ for widget in completed
73+ if revision <= work_claim_revision (widget_work [widget ], events )
74+ )
75+ if not_later :
76+ sys .exit (
77+ f"revision r{ revision } is not later than the active widget work claim for "
78+ + ", " .join (repr (widget ) for widget in not_later )
79+ )
80+ unseated = widget_work_without_seats (
81+ checked .html ,
82+ parser ,
83+ projection ,
84+ events ,
85+ page .status ,
86+ registry ,
87+ completed ,
88+ )
89+ if unseated :
90+ widgets = ", " .join (repr (widget ) for widget in unseated )
91+ sys .exit (
92+ "refusing to stamp index.html: it would remove the local seat "
93+ f"for active work on { widgets } ; pass --completes for each widget "
94+ "this version completes"
95+ )
96+ return completed
97+
98+
99+ def _settled_reports (projection , parser , spk : dict , registry : dict ) -> list [str ]:
100+ settled = []
101+ for (_widget , unit , _facet ), reports in projection .reports .items ():
102+ last , spec = reports [- 1 ]
103+ if unit in parser .overruled or markup_facet (
104+ unit , spec , parser .by_id , spk , registry
105+ ) == folded_facet (last , spec ):
106+ settled .extend (report ["id" ] for report , _ in reports )
107+ return settled
108+
109+
110+ def _stamp_event (
111+ body : str ,
112+ version : int ,
113+ revision : int ,
114+ parser ,
115+ settled_reports : list [str ],
116+ completed : set [str ],
117+ ) -> dict :
118+ event = {
119+ "kind" : "note" ,
120+ "author" : "claude" ,
121+ ** message_identity (),
122+ "version" : version ,
123+ "revision" : revision ,
124+ "text" : body ,
125+ }
126+ if parser .restated :
127+ event ["restated" ] = sorted (parser .restated )
128+ settlements = [
129+ * ({"kind" : "report" , "id" : identity } for identity in sorted (settled_reports )),
130+ * ({"kind" : "work" , "id" : identity } for identity in sorted (completed )),
131+ ]
132+ if settlements :
133+ event ["settles" ] = settlements
134+ return event
135+
136+
137+ def _stamp_locked (page_dir : Path , page , body : str , completes : tuple [str , ...]) -> dict :
138+ events = page .events
139+ activation = _stamp_activation (page_dir , events )
33140 revision = activation .revision
34141 created_revision = revision_path (page_dir , revision ) if activation .created else None
35142 created_version = None
36143 committed = False
37144 try :
38- if existing := stamped_version (events , revision ):
39- sys .exit (f"revision r{ revision } is already stamped as v{ existing } " )
40-
41- checked = activation .check
42- registry = checked .registry
43- if registry is None :
44- sys .exit ("refusing to stamp index.html: the page has no registry.json" )
45- projection , parser , spk = page_projection (
46- checked .html , events , registry , revision
145+ checked , registry , projection , parser , spk = _stamp_reading (
146+ page_dir , events , activation
47147 )
48148
49- if len (set (completes )) != len (completes ):
50- sys .exit ("--completes names each widget at most once" )
51- completed = set (completes )
52- widget_work = {
53- claim ["subject" ]["id" ]: claim
54- for claim in standing_work_claims (page .status , events )
55- if claim ["subject" ]["kind" ] == "widget"
56- }
57- unearned = sorted (completed - widget_work .keys ())
58- if unearned :
59- sys .exit (
60- "no active widget work claim for "
61- + ", " .join (repr (widget ) for widget in unearned )
62- )
63- not_later = sorted (
64- widget
65- for widget in completed
66- if revision <= work_claim_revision (widget_work [widget ], events )
67- )
68- if not_later :
69- sys .exit (
70- f"revision r{ revision } is not later than the active widget work claim for "
71- + ", " .join (repr (widget ) for widget in not_later )
72- )
73- unseated = widget_work_without_seats (
74- checked .html ,
149+ completed = _completed_work (
150+ checked ,
75151 parser ,
76152 projection ,
77153 events ,
78- page . status ,
154+ page ,
79155 registry ,
80- completed ,
156+ revision ,
157+ completes ,
81158 )
82- if unseated :
83- widgets = ", " .join (repr (widget ) for widget in unseated )
84- sys .exit (
85- "refusing to stamp index.html: it would remove the local seat "
86- f"for active work on { widgets } ; pass --completes for each widget "
87- "this version completes"
88- )
89-
90- settled_reports = []
91- for (_widget , unit , _facet ), reports in projection .reports .items ():
92- last , spec = reports [- 1 ]
93- if unit in parser .overruled or markup_facet (
94- unit , spec , parser .by_id , spk , registry
95- ) == folded_facet (last , spec ):
96- settled_reports .extend (report ["id" ] for report , _ in reports )
159+ settled_reports = _settled_reports (projection , parser , spk , registry )
97160
98161 notes = [event for event in events if event ["kind" ] == "note" ]
99162 version = max ((event ["version" ] for event in notes ), default = 0 ) + 1
@@ -103,25 +166,9 @@ def _stamp_locked(page_dir: Path, page, body: str, completes: tuple[str, ...]) -
103166 created_version .unlink (missing_ok = True )
104167 replace_files ([(created_version , checked .data , False )])
105168
106- event = {
107- "kind" : "note" ,
108- "author" : "claude" ,
109- ** message_identity (),
110- "version" : version ,
111- "revision" : revision ,
112- "text" : body ,
113- }
114- if parser .restated :
115- event ["restated" ] = sorted (parser .restated )
116- settlements = [
117- * (
118- {"kind" : "report" , "id" : identity }
119- for identity in sorted (settled_reports )
120- ),
121- * ({"kind" : "work" , "id" : identity } for identity in sorted (completed )),
122- ]
123- if settlements :
124- event ["settles" ] = settlements
169+ event = _stamp_event (
170+ body , version , revision , parser , settled_reports , completed
171+ )
125172 accepted = append_event (page , event )
126173 committed = True
127174 return accepted
0 commit comments