plotroom 0.1.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.
Files changed (150) hide show
  1. plotroom/__init__.py +0 -0
  2. plotroom/__main__.py +8 -0
  3. plotroom/_secret_file.py +8 -0
  4. plotroom/adapters/__init__.py +0 -0
  5. plotroom/adapters/claude_desktop.py +219 -0
  6. plotroom/adapters/embedding_http.py +175 -0
  7. plotroom/adapters/github/__init__.py +337 -0
  8. plotroom/adapters/github/app_credential.py +209 -0
  9. plotroom/adapters/github/ci.py +738 -0
  10. plotroom/adapters/github/commit_statuses.py +63 -0
  11. plotroom/adapters/github/deployments.py +213 -0
  12. plotroom/adapters/github/details.py +867 -0
  13. plotroom/adapters/github/issue_details.py +368 -0
  14. plotroom/adapters/github/junit.py +210 -0
  15. plotroom/adapters/github/junit_xml.py +162 -0
  16. plotroom/adapters/github/kodus_review.py +347 -0
  17. plotroom/adapters/github/pr_details.py +192 -0
  18. plotroom/adapters/github/pull_comparison.py +108 -0
  19. plotroom/adapters/github/read.py +530 -0
  20. plotroom/adapters/github/ticket_tree.py +336 -0
  21. plotroom/adapters/mcp/__init__.py +5 -0
  22. plotroom/adapters/mcp/proxy.py +722 -0
  23. plotroom/adapters/mcp/server.py +931 -0
  24. plotroom/adapters/openrouter.py +151 -0
  25. plotroom/adapters/store_sqlite/__init__.py +6 -0
  26. plotroom/adapters/store_sqlite/actions_sql.py +148 -0
  27. plotroom/adapters/store_sqlite/asks_sql.py +153 -0
  28. plotroom/adapters/store_sqlite/backfill_sql.py +138 -0
  29. plotroom/adapters/store_sqlite/budgets_sql.py +102 -0
  30. plotroom/adapters/store_sqlite/checklists_sql.py +77 -0
  31. plotroom/adapters/store_sqlite/claims_sql.py +54 -0
  32. plotroom/adapters/store_sqlite/embedding_sql.py +275 -0
  33. plotroom/adapters/store_sqlite/github_sql.py +331 -0
  34. plotroom/adapters/store_sqlite/goals_sql.py +57 -0
  35. plotroom/adapters/store_sqlite/human_queue_sql.py +60 -0
  36. plotroom/adapters/store_sqlite/items_sql.py +129 -0
  37. plotroom/adapters/store_sqlite/lanes_sql.py +85 -0
  38. plotroom/adapters/store_sqlite/ledger_sql.py +506 -0
  39. plotroom/adapters/store_sqlite/migrate.py +194 -0
  40. plotroom/adapters/store_sqlite/migrations/env.py +36 -0
  41. plotroom/adapters/store_sqlite/migrations/script.py.mako +19 -0
  42. plotroom/adapters/store_sqlite/migrations/versions/0001_project_meta.py +23 -0
  43. plotroom/adapters/store_sqlite/migrations/versions/0002_event_log.py +40 -0
  44. plotroom/adapters/store_sqlite/migrations/versions/0003_next_items.py +49 -0
  45. plotroom/adapters/store_sqlite/migrations/versions/0004_actions.py +28 -0
  46. plotroom/adapters/store_sqlite/migrations/versions/0005_lanes.py +36 -0
  47. plotroom/adapters/store_sqlite/migrations/versions/0006_work_items.py +22 -0
  48. plotroom/adapters/store_sqlite/migrations/versions/0007_item_changes.py +33 -0
  49. plotroom/adapters/store_sqlite/migrations/versions/0008_claude_turn_aliases.py +30 -0
  50. plotroom/adapters/store_sqlite/migrations/versions/0009_github_projection.py +38 -0
  51. plotroom/adapters/store_sqlite/migrations/versions/0010_github_pull_freshness.py +37 -0
  52. plotroom/adapters/store_sqlite/migrations/versions/0011_github_resources.py +27 -0
  53. plotroom/adapters/store_sqlite/migrations/versions/0012_local_triggers_outbox.py +80 -0
  54. plotroom/adapters/store_sqlite/migrations/versions/0013_lane_claims.py +22 -0
  55. plotroom/adapters/store_sqlite/migrations/versions/0015_recurring_triggers.py +45 -0
  56. plotroom/adapters/store_sqlite/migrations/versions/0016_merge_controller.py +28 -0
  57. plotroom/adapters/store_sqlite/migrations/versions/0017_lane_checklists.py +36 -0
  58. plotroom/adapters/store_sqlite/migrations/versions/0018_guardrails.py +33 -0
  59. plotroom/adapters/store_sqlite/migrations/versions/0019_clerk_import_metadata.py +16 -0
  60. plotroom/adapters/store_sqlite/migrations/versions/0020_search.py +30 -0
  61. plotroom/adapters/store_sqlite/migrations/versions/0021_human_queue.py +46 -0
  62. plotroom/adapters/store_sqlite/migrations/versions/0022_search_records.py +77 -0
  63. plotroom/adapters/store_sqlite/migrations/versions/0023_operator_goals.py +25 -0
  64. plotroom/adapters/store_sqlite/migrations/versions/0024_embedding_rejections.py +36 -0
  65. plotroom/adapters/store_sqlite/outbox_sql.py +78 -0
  66. plotroom/adapters/store_sqlite/routes_sql.py +26 -0
  67. plotroom/adapters/store_sqlite/search_sql.py +116 -0
  68. plotroom/adapters/store_sqlite/store.py +623 -0
  69. plotroom/adapters/store_sqlite/triggers_sql.py +111 -0
  70. plotroom/adapters/store_sqlite/work_items_sql.py +36 -0
  71. plotroom/adapters/switchboard.py +152 -0
  72. plotroom/adapters/usage_fixture.py +11 -0
  73. plotroom/adapters/work_items_bmad.py +256 -0
  74. plotroom/adapters/work_items_github_issues.py +323 -0
  75. plotroom/app/__init__.py +0 -0
  76. plotroom/app/bridge.py +162 -0
  77. plotroom/app/clerk_import.py +349 -0
  78. plotroom/app/cli.py +290 -0
  79. plotroom/app/clock.py +12 -0
  80. plotroom/app/config.py +984 -0
  81. plotroom/app/daemon_start.py +453 -0
  82. plotroom/app/lock.py +77 -0
  83. plotroom/app/logging.py +93 -0
  84. plotroom/app/portfile.py +218 -0
  85. plotroom/app/runtime.py +518 -0
  86. plotroom/app/serve.py +671 -0
  87. plotroom/app/session_intake.py +51 -0
  88. plotroom/app/sqlite_floor.py +35 -0
  89. plotroom/app/tokens.py +265 -0
  90. plotroom/core/__init__.py +0 -0
  91. plotroom/core/actions.py +67 -0
  92. plotroom/core/ask_checks.py +124 -0
  93. plotroom/core/asks.py +123 -0
  94. plotroom/core/branch_updates.py +207 -0
  95. plotroom/core/bridge.py +112 -0
  96. plotroom/core/briefs.py +83 -0
  97. plotroom/core/budgets.py +182 -0
  98. plotroom/core/catalog.py +464 -0
  99. plotroom/core/checklists.py +151 -0
  100. plotroom/core/claims.py +296 -0
  101. plotroom/core/clerk_import.py +379 -0
  102. plotroom/core/commands.py +85 -0
  103. plotroom/core/deploy_window.py +21 -0
  104. plotroom/core/envelope.py +20 -0
  105. plotroom/core/events.py +227 -0
  106. plotroom/core/github.py +306 -0
  107. plotroom/core/github_projection.py +310 -0
  108. plotroom/core/goal_language.py +374 -0
  109. plotroom/core/goal_projection.py +146 -0
  110. plotroom/core/human_queue.py +119 -0
  111. plotroom/core/intake.py +106 -0
  112. plotroom/core/kodus_review.py +53 -0
  113. plotroom/core/lanes.py +281 -0
  114. plotroom/core/ledger.py +808 -0
  115. plotroom/core/merge_controller.py +344 -0
  116. plotroom/core/merge_flakes.py +422 -0
  117. plotroom/core/merge_gate.py +724 -0
  118. plotroom/core/merge_lane.py +70 -0
  119. plotroom/core/merge_overlap.py +76 -0
  120. plotroom/core/next_items.py +60 -0
  121. plotroom/core/operator_goals.py +103 -0
  122. plotroom/core/outbox.py +70 -0
  123. plotroom/core/pull_overlaps.py +152 -0
  124. plotroom/core/reports.py +62 -0
  125. plotroom/core/roles.py +100 -0
  126. plotroom/core/routing.py +55 -0
  127. plotroom/core/run_reports.py +53 -0
  128. plotroom/core/search.py +266 -0
  129. plotroom/core/search_sources.py +94 -0
  130. plotroom/core/snapshot.py +204 -0
  131. plotroom/core/trigger_actions.py +89 -0
  132. plotroom/core/triggers.py +550 -0
  133. plotroom/core/unit_of_work.py +541 -0
  134. plotroom/core/watchers.py +35 -0
  135. plotroom/core/work_items.py +23 -0
  136. plotroom/core/writer.py +164 -0
  137. plotroom/ports/__init__.py +6 -0
  138. plotroom/ports/clock.py +12 -0
  139. plotroom/ports/github_write.py +72 -0
  140. plotroom/ports/search.py +197 -0
  141. plotroom/ports/session_reader.py +23 -0
  142. plotroom/ports/store.py +323 -0
  143. plotroom/ports/usage_meter.py +35 -0
  144. plotroom/ports/work_items.py +22 -0
  145. plotroom/py.typed +0 -0
  146. plotroom-0.1.0.dist-info/METADATA +230 -0
  147. plotroom-0.1.0.dist-info/RECORD +150 -0
  148. plotroom-0.1.0.dist-info/WHEEL +4 -0
  149. plotroom-0.1.0.dist-info/entry_points.txt +2 -0
  150. plotroom-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,464 @@
1
+ """The catalog: the one place later stories register event kinds and commands."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from collections.abc import Awaitable, Callable, Mapping
6
+ from dataclasses import dataclass, field
7
+ from functools import partial
8
+
9
+ from plotroom.core.actions import ACTION_FIELDS, FAILED_ACTION
10
+ from plotroom.core.ask_checks import ASK_ITEMS
11
+ from plotroom.core.asks import record_ask
12
+ from plotroom.core.branch_updates import (
13
+ BRANCH_UPDATE_ATTEMPT_PIN,
14
+ BRANCH_UPDATE_AUTHORIZED,
15
+ BRANCH_UPDATE_AUTHORIZED_FIELDS,
16
+ BRANCH_UPDATE_CONFLICT,
17
+ BRANCH_UPDATE_DECLINED,
18
+ BRANCH_UPDATE_FIELDS,
19
+ BRANCH_UPDATE_STAGED,
20
+ BRANCH_UPDATED,
21
+ BRANCH_UPDATED_FIELDS,
22
+ )
23
+ from plotroom.core.bridge import complete_bridge, lease_bridge, receive_bridge
24
+ from plotroom.core.briefs import BRIEF
25
+ from plotroom.core.budgets import (
26
+ BUDGET_ARGS,
27
+ BUDGET_ITEM,
28
+ BUDGET_THRESHOLD,
29
+ BUDGET_THRESHOLD_FIELDS,
30
+ budget_open,
31
+ validate_openrouter_budget,
32
+ )
33
+ from plotroom.core.checklists import CHECKLIST_ARG, CHECKLIST_EMPTY, CHECKLIST_EMPTY_FIELDS
34
+ from plotroom.core.claims import CLAIM_OVERLAP, PR_CLAIM_OVERLAP, TICKET_TREE_MOVE, claim, release
35
+ from plotroom.core.clerk_import import (
36
+ CLERK_IMPORT_ENTRY,
37
+ CLERK_IMPORT_ENTRY_FIELDS,
38
+ CLERK_IMPORT_ITEMS,
39
+ clerk_import,
40
+ )
41
+ from plotroom.core.commands import Command, CommandRegistry
42
+ from plotroom.core.deploy_window import freeze, unfreeze
43
+ from plotroom.core.events import EventKind, EventRegistry, FieldSpec
44
+ from plotroom.core.github import PR_FIELDS, PR_KINDS
45
+ from plotroom.core.github_projection import GITHUB_FIELDS, REPOSITORY
46
+ from plotroom.core.human_queue import DECISION_FIELDS, decide
47
+ from plotroom.core.intake import (
48
+ CLAUDE_APPENDERS,
49
+ INBOUND_MESSAGE,
50
+ SESSION_FIELD,
51
+ SESSION_MESSAGE,
52
+ TURN_FIELDS,
53
+ TURN_REPORT,
54
+ )
55
+ from plotroom.core.lanes import (
56
+ LANE_WAITING,
57
+ archive_lane,
58
+ checklist_done,
59
+ checklist_import,
60
+ owns_lane,
61
+ ready_lane,
62
+ start_lane,
63
+ update_lane,
64
+ )
65
+ from plotroom.core.merge_controller import MERGE_EVENTS, MERGE_ITEMS
66
+ from plotroom.core.merge_flakes import (
67
+ FAILURE_FIELDS,
68
+ FLAKE_CANDIDATE,
69
+ FLAKE_EVENT,
70
+ FLAKE_FIELDS,
71
+ MAIN_FLAKE_EVENT,
72
+ )
73
+ from plotroom.core.next_items import ItemKind, ack, owns_ack
74
+ from plotroom.core.operator_goals import set_goal
75
+ from plotroom.core.outbox import complete_outbox, lease_outbox
76
+ from plotroom.core.reports import DEPLOY_ARGS, DEPLOY_FIELDS, DEPLOY_STATUS, deploy_report
77
+ from plotroom.core.roles import WriterRule, WriterRules
78
+ from plotroom.core.routing import ACTION_KIND, MISSING_ROUTE, route, route_request
79
+ from plotroom.core.run_reports import RUN_FIELDS, RUN_ID, RUN_STATE, RUN_STATUS, run_report
80
+ from plotroom.core.trigger_actions import (
81
+ ARCHIVE_ITEM,
82
+ MARK_FIELDS,
83
+ NOTIFICATION_ITEM,
84
+ DeliveryRoute,
85
+ )
86
+ from plotroom.core.triggers import (
87
+ TRIGGER_FIRED,
88
+ TRIGGER_FIRED_FIELDS,
89
+ LeafRegistry,
90
+ build_leaf_registry,
91
+ cancel_trigger,
92
+ create_trigger,
93
+ )
94
+ from plotroom.core.watchers import MISSING_WATCHER
95
+ from plotroom.core.work_items import IDLE_WORKER
96
+
97
+
98
+ @dataclass(frozen=True)
99
+ class Catalog:
100
+ events: EventRegistry
101
+ commands: CommandRegistry
102
+ items: dict[str, ItemKind] = field(default_factory=lambda: {})
103
+ trigger_leaves: LeafRegistry | None = None
104
+ workers: frozenset[str] = frozenset()
105
+ read_roles: Mapping[str, frozenset[str]] = field(default_factory=lambda: {})
106
+
107
+ def register_item(self, kind: ItemKind) -> None:
108
+ if kind.name in self.items:
109
+ raise ValueError(f"next item kind {kind.name!r} is already registered")
110
+ self.items[kind.name] = kind
111
+
112
+ @property
113
+ def writer_rules(self) -> WriterRules:
114
+ return self.events.writer_rules
115
+
116
+
117
+ def build_catalog(
118
+ workers: frozenset[str] = frozenset(),
119
+ *,
120
+ github_repository: str | None = None,
121
+ human_login: str | None = None,
122
+ delivery_routes: Mapping[str, DeliveryRoute] | None = None,
123
+ image_jobs: tuple[str, ...] = (),
124
+ lane_idle_minutes: int = 15,
125
+ openrouter_bindings: Mapping[str, str] | None = None,
126
+ check_openrouter_cap: Callable[[str, float], Awaitable[bool]] | None = None,
127
+ ) -> Catalog:
128
+ """Every kind and command the server knows."""
129
+ events = EventRegistry()
130
+ leaves = build_leaf_registry(image_jobs=image_jobs, lane_idle_minutes=lane_idle_minutes)
131
+ routes: Mapping[str, DeliveryRoute] = delivery_routes if delivery_routes is not None else {}
132
+ catalog = Catalog(
133
+ events,
134
+ CommandRegistry(events),
135
+ trigger_leaves=leaves,
136
+ workers=workers,
137
+ read_roles={BRIEF: frozenset({"orchestrator"})},
138
+ )
139
+ catalog.commands.register(
140
+ Command(
141
+ "budget_open",
142
+ BUDGET_ARGS,
143
+ partial(
144
+ budget_open,
145
+ openrouter_bindings=openrouter_bindings
146
+ if check_openrouter_cap is not None
147
+ else None,
148
+ ),
149
+ preflight=partial(validate_openrouter_budget, check=check_openrouter_cap)
150
+ if check_openrouter_cap is not None
151
+ else None,
152
+ ),
153
+ rules=(WriterRule("orchestrator"),),
154
+ )
155
+ events.register(EventKind(BUDGET_THRESHOLD, BUDGET_THRESHOLD_FIELDS, frozenset({"system"})))
156
+ catalog.register_item(BUDGET_ITEM)
157
+ catalog.commands.register(
158
+ Command(
159
+ "ack",
160
+ (FieldSpec("item", "str"), FieldSpec("outcome", "str", required=False)),
161
+ ack,
162
+ record_if=lambda result: bool(result["changed"]),
163
+ ),
164
+ rules=(
165
+ WriterRule("orchestrator"),
166
+ WriterRule("worker", owns_ack),
167
+ WriterRule("clerk", owns_ack),
168
+ WriterRule("bridge", owns_ack),
169
+ ),
170
+ )
171
+ catalog.register_item(ItemKind(FAILED_ACTION, 10, False, ("Inspect the failed action",)))
172
+ catalog.register_item(
173
+ ItemKind(BRANCH_UPDATE_CONFLICT, 10, False, ("Resolve the branch update conflict",))
174
+ )
175
+ events.register(EventKind(BRANCH_UPDATE_STAGED, BRANCH_UPDATE_FIELDS, frozenset({"system"})))
176
+ events.register(
177
+ EventKind(BRANCH_UPDATE_AUTHORIZED, BRANCH_UPDATE_AUTHORIZED_FIELDS, frozenset({"system"}))
178
+ )
179
+ events.register(
180
+ EventKind(
181
+ BRANCH_UPDATE_ATTEMPT_PIN,
182
+ (FieldSpec("authorization", "str"),),
183
+ frozenset({"system"}),
184
+ )
185
+ )
186
+ events.register(
187
+ EventKind(
188
+ BRANCH_UPDATE_DECLINED,
189
+ (FieldSpec("sent", "bool"),),
190
+ frozenset({"system"}),
191
+ )
192
+ )
193
+ events.register(EventKind(BRANCH_UPDATED, BRANCH_UPDATED_FIELDS, frozenset({"system"})))
194
+ for kind, fields in MERGE_EVENTS.items():
195
+ events.register(EventKind(kind, fields, frozenset({"system"})))
196
+ for item_kind in MERGE_ITEMS:
197
+ catalog.register_item(item_kind)
198
+ events.register(EventKind("core.tick", (), frozenset({"system"})))
199
+ events.register(EventKind(CHECKLIST_EMPTY, CHECKLIST_EMPTY_FIELDS, frozenset({"system"})))
200
+ events.register(EventKind(FLAKE_EVENT, FLAKE_FIELDS, frozenset({"system"})))
201
+ events.register(EventKind(MAIN_FLAKE_EVENT, FAILURE_FIELDS, frozenset({"system"})))
202
+ catalog.register_item(ItemKind(FLAKE_CANDIDATE, 10, False, ("Investigate the flaky check",)))
203
+ for name, fields in GITHUB_FIELDS.items():
204
+ events.register(EventKind(f"github.{name}", (REPOSITORY, *fields), frozenset({"system"})))
205
+ for kind in PR_KINDS:
206
+ events.register(EventKind(kind, PR_FIELDS, frozenset({"system"})))
207
+ events.register(
208
+ EventKind(
209
+ "switchboard.message",
210
+ (FieldSpec("sender", "str"), FieldSpec("worker", "str", required=False)),
211
+ frozenset({"system"}),
212
+ )
213
+ )
214
+ events.register(EventKind(TURN_REPORT, TURN_FIELDS, CLAUDE_APPENDERS))
215
+ events.register(
216
+ EventKind(
217
+ SESSION_MESSAGE,
218
+ (SESSION_FIELD, FieldSpec("worker", "str", required=False)),
219
+ CLAUDE_APPENDERS,
220
+ )
221
+ )
222
+ catalog.register_item(
223
+ ItemKind(INBOUND_MESSAGE, 20, False, ("Read and acknowledge the message",))
224
+ )
225
+ for status in ("attempt", "done", "failed"):
226
+ events.register(EventKind(f"core.action_{status}", ACTION_FIELDS, frozenset({"system"})))
227
+ catalog.register_item(ItemKind(LANE_WAITING, 20, True, ("Reply to the worker",)))
228
+ catalog.register_item(ItemKind(IDLE_WORKER, 40, True, ("Start a lane for the worker",)))
229
+ catalog.register_item(
230
+ ItemKind(MISSING_WATCHER, 30, True, ("Start plotroom watch for this role",))
231
+ )
232
+ lane = FieldSpec("lane", "str")
233
+ orchestrator = (WriterRule("orchestrator"),)
234
+ catalog.commands.register(
235
+ Command(
236
+ "goal_set",
237
+ (FieldSpec("name", "str"), FieldSpec("text", "str")),
238
+ partial(set_goal, repository=github_repository),
239
+ ),
240
+ rules=orchestrator,
241
+ )
242
+ catalog.register_item(MISSING_ROUTE)
243
+ catalog.commands.register(Command("route", (ACTION_KIND, lane), route), rules=orchestrator)
244
+ catalog.commands.register(
245
+ Command("route_request", (ACTION_KIND,), route_request), rules=orchestrator
246
+ )
247
+ catalog.commands.register(
248
+ Command("freeze", (FieldSpec("reason", "str"),), freeze), rules=orchestrator
249
+ )
250
+ catalog.commands.register(Command("unfreeze", (), unfreeze), rules=orchestrator)
251
+ for item_kind in ASK_ITEMS:
252
+ catalog.register_item(item_kind)
253
+ catalog.commands.register(
254
+ Command(
255
+ "ask",
256
+ (
257
+ FieldSpec("issue", "int"),
258
+ FieldSpec("text", "str"),
259
+ FieldSpec("options", "array"),
260
+ FieldSpec("comment_id", "int"),
261
+ ),
262
+ partial(record_ask, repository=github_repository),
263
+ ),
264
+ rules=orchestrator,
265
+ )
266
+ catalog.commands.register(
267
+ Command(
268
+ "decide",
269
+ DECISION_FIELDS,
270
+ partial(decide, repository=github_repository, human_login=human_login),
271
+ ),
272
+ rules=orchestrator,
273
+ )
274
+ owner = (*orchestrator, WriterRule("worker", owns_lane))
275
+ events.register(EventKind(DEPLOY_STATUS, DEPLOY_FIELDS, frozenset({"system"})))
276
+ catalog.commands.register(
277
+ Command(
278
+ "deploy_report",
279
+ (lane, *DEPLOY_ARGS),
280
+ deploy_report,
281
+ record_if=lambda result: bool(result["created"]),
282
+ ),
283
+ rules=(WriterRule("worker", owns_lane),),
284
+ )
285
+ catalog.register_item(ItemKind(CLAIM_OVERLAP, 10, True, ("Coordinate the lane claims",)))
286
+ catalog.register_item(ItemKind(PR_CLAIM_OVERLAP, 10, True, ("Coordinate the PR and claim",)))
287
+ catalog.register_item(
288
+ ItemKind(TICKET_TREE_MOVE, 10, True, ("Coordinate the move with the editing PRs",))
289
+ )
290
+ globs = FieldSpec("globs", "array")
291
+ catalog.commands.register(
292
+ Command(
293
+ "claim",
294
+ (lane, globs, FieldSpec("reason", "str")),
295
+ claim,
296
+ record_if=lambda result: bool(result["changed"]),
297
+ ),
298
+ rules=owner,
299
+ )
300
+ catalog.commands.register(
301
+ Command(
302
+ "release", (lane, globs), release, record_if=lambda result: bool(result["changed"])
303
+ ),
304
+ rules=owner,
305
+ )
306
+ catalog.commands.register(
307
+ Command(
308
+ "lane_start",
309
+ (
310
+ lane,
311
+ FieldSpec("worker", "str"),
312
+ FieldSpec("branch", "str"),
313
+ FieldSpec("work_items", "array"),
314
+ FieldSpec("branch_pattern", "str", required=False),
315
+ ),
316
+ partial(start_lane, workers=workers),
317
+ ),
318
+ rules=orchestrator,
319
+ )
320
+ catalog.commands.register(
321
+ Command("lane_update", (lane, FieldSpec("waiting_on", "str")), update_lane), rules=owner
322
+ )
323
+ catalog.commands.register(
324
+ Command(
325
+ "lane_ready",
326
+ (lane, FieldSpec("pr", "int"), FieldSpec("head_sha", "str"), CHECKLIST_ARG),
327
+ partial(ready_lane, repository=github_repository),
328
+ ),
329
+ rules=owner,
330
+ )
331
+ catalog.commands.register(
332
+ Command("lane_archive", (lane,), partial(archive_lane, workers=workers)), rules=orchestrator
333
+ )
334
+ catalog.commands.register(
335
+ Command(
336
+ "checklist_done",
337
+ (lane, FieldSpec("step", "str"), FieldSpec("ref", "str", required=False)),
338
+ checklist_done,
339
+ record_if=lambda result: bool(result["changed"]),
340
+ lane_scoped=True,
341
+ ),
342
+ rules=owner,
343
+ )
344
+ catalog.commands.register(
345
+ Command(
346
+ "checklist_import",
347
+ (lane, FieldSpec("rows", "array")),
348
+ partial(checklist_import, repository=github_repository),
349
+ lane_scoped=True,
350
+ ),
351
+ rules=orchestrator,
352
+ )
353
+ events.register(EventKind(CLERK_IMPORT_ENTRY, CLERK_IMPORT_ENTRY_FIELDS, frozenset({"system"})))
354
+ for kind in CLERK_IMPORT_ITEMS:
355
+ catalog.register_item(kind)
356
+ catalog.commands.register(
357
+ Command(
358
+ "clerk_import",
359
+ (
360
+ FieldSpec("triggers", "array"),
361
+ FieldSpec("pulls", "array"),
362
+ FieldSpec("skips", "array"),
363
+ FieldSpec("ledger_repository", "str", required=False),
364
+ ),
365
+ partial(
366
+ clerk_import,
367
+ repository=github_repository,
368
+ workers=workers,
369
+ registry=leaves,
370
+ delivery_routes=routes,
371
+ ),
372
+ ),
373
+ rules=orchestrator,
374
+ )
375
+ events.register(EventKind(RUN_STATUS, RUN_FIELDS, frozenset({"system"})))
376
+ catalog.commands.register(
377
+ Command("run_report", (lane, RUN_ID, RUN_STATE), run_report),
378
+ rules=(WriterRule("worker", owns_lane),),
379
+ )
380
+ catalog.register_item(NOTIFICATION_ITEM)
381
+ catalog.register_item(ARCHIVE_ITEM)
382
+ events.register(EventKind("core.lane_marked", MARK_FIELDS, frozenset({"system"})))
383
+ events.register(EventKind(TRIGGER_FIRED, TRIGGER_FIRED_FIELDS, frozenset({"system"})))
384
+ catalog.commands.register(
385
+ Command(
386
+ "trigger",
387
+ (
388
+ FieldSpec("mode", "str", required=False),
389
+ FieldSpec("predicate", "object"),
390
+ FieldSpec("action", "object"),
391
+ FieldSpec("lane", "str", required=False),
392
+ FieldSpec("imported_fired", "bool", required=False),
393
+ ),
394
+ partial(
395
+ create_trigger,
396
+ registry=leaves,
397
+ repository=github_repository,
398
+ delivery_routes=routes,
399
+ ),
400
+ ),
401
+ rules=orchestrator,
402
+ )
403
+ catalog.commands.register(
404
+ Command("cancel", (FieldSpec("trigger_id", "str"),), cancel_trigger), rules=orchestrator
405
+ )
406
+ drain = (WriterRule("drain"),)
407
+ catalog.commands.register(
408
+ Command(
409
+ "outbox_lease",
410
+ (),
411
+ lease_outbox,
412
+ record_if=lambda result: result["lease"] is not None,
413
+ ),
414
+ rules=drain,
415
+ )
416
+ catalog.commands.register(
417
+ Command("outbox_done", (FieldSpec("act_id", "str"),), complete_outbox), rules=drain
418
+ )
419
+ return catalog
420
+
421
+
422
+ def register_bridge(
423
+ catalog: Catalog,
424
+ board: str,
425
+ senders: Mapping[str, str],
426
+ routes: Mapping[str, DeliveryRoute],
427
+ action_attempts: int = 3,
428
+ ) -> None:
429
+ bridge = (WriterRule("bridge"),)
430
+ relay_routes = {
431
+ worker: route for worker, route in routes.items() if route.channel == "switchboard"
432
+ }
433
+ catalog.commands.register(
434
+ Command(
435
+ "bridge_receive",
436
+ (
437
+ FieldSpec("board", "str"),
438
+ FieldSpec("message_id", "int"),
439
+ FieldSpec("sender", "str"),
440
+ FieldSpec("body", "str"),
441
+ ),
442
+ partial(receive_bridge, board=board, senders=senders),
443
+ ),
444
+ rules=bridge,
445
+ )
446
+ catalog.commands.register(
447
+ Command(
448
+ "bridge_lease",
449
+ (FieldSpec("board", "str"),),
450
+ partial(
451
+ lease_bridge, routes=relay_routes, board=board, action_attempts=action_attempts
452
+ ),
453
+ record_if=lambda result: result["lease"] is not None,
454
+ ),
455
+ rules=bridge,
456
+ )
457
+ catalog.commands.register(
458
+ Command(
459
+ "bridge_done",
460
+ (FieldSpec("act_id", "str"),),
461
+ partial(complete_bridge, workers=tuple(relay_routes)),
462
+ ),
463
+ rules=bridge,
464
+ )
@@ -0,0 +1,151 @@
1
+ """Typed READY checklists and their merge lifecycle."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import asdict, dataclass, replace
6
+ from typing import TYPE_CHECKING, Any, Literal, cast
7
+
8
+ from plotroom.core.events import FieldSpec, LedgerError, NewEvent, validate_fields
9
+
10
+ if TYPE_CHECKING:
11
+ from plotroom.core.github import PullRequest
12
+ from plotroom.core.lanes import Lane
13
+ from plotroom.core.unit_of_work import UnitOfWork
14
+
15
+ CHECKLIST_ARG = FieldSpec("checklist", "array", required=False)
16
+ ARCHIVE_PROPOSAL = "archive_lane"
17
+ CHECKLIST_EMPTY = "core.checklist_empty"
18
+ CHECKLIST_EMPTY_FIELDS = (FieldSpec("lane", "str"), FieldSpec("version", "int"))
19
+ _STEP_FIELDS = (FieldSpec("step", "str"), FieldSpec("owner", "str"), FieldSpec("ref", "str"))
20
+
21
+
22
+ @dataclass(frozen=True)
23
+ class ChecklistStep:
24
+ step: str
25
+ owner: str
26
+ ref: str
27
+ completion_ref: str | None = None
28
+
29
+
30
+ @dataclass(frozen=True)
31
+ class Checklist:
32
+ lane: str
33
+ version: int
34
+ repository: str
35
+ branch: str
36
+ pr: int
37
+ head_sha: str
38
+ state: Literal["pending", "active", "invalidated"]
39
+ steps: tuple[ChecklistStep, ...]
40
+
41
+ def open_steps(self) -> tuple[ChecklistStep, ...]:
42
+ return tuple(step for step in self.steps if step.completion_ref is None)
43
+
44
+ def render(self) -> dict[str, Any]:
45
+ return {
46
+ "version": self.version,
47
+ "pr": self.pr,
48
+ "head_sha": self.head_sha,
49
+ "state": self.state,
50
+ "steps": [asdict(step) for step in self.steps],
51
+ }
52
+
53
+ def matches(self, pull: PullRequest | None) -> bool:
54
+ return (
55
+ pull is not None
56
+ and pull.complete
57
+ and pull.repository == pull.head_repository == self.repository
58
+ and (pull.branch, pull.number, pull.head_sha) == (self.branch, self.pr, self.head_sha)
59
+ )
60
+
61
+
62
+ def _parse_steps(rows: list[Any]) -> tuple[ChecklistStep, ...]:
63
+ steps: list[ChecklistStep] = []
64
+ for row in rows:
65
+ if not isinstance(row, dict):
66
+ raise LedgerError("invalid", "checklist entries must contain step, owner and ref")
67
+ fields = cast(dict[str, Any], row)
68
+ validate_fields(_STEP_FIELDS, fields, "checklist entry")
69
+ step = ChecklistStep(fields["step"], fields["owner"], fields["ref"])
70
+ if not all(value.strip() for value in (step.step, step.owner, step.ref)):
71
+ raise LedgerError("invalid", "checklist step, owner and ref must be non-empty")
72
+ steps.append(step)
73
+ if len({step.step for step in steps}) != len(steps):
74
+ raise LedgerError("invalid", "checklist step names must be unique")
75
+ return tuple(steps)
76
+
77
+
78
+ async def declare_checklist(
79
+ uow: UnitOfWork, lane: Lane, repository: str | None, args: dict[str, Any]
80
+ ) -> None:
81
+ current = await uow.find_checklist(lane.id)
82
+ if current is not None and current.state == "active":
83
+ raise LedgerError("conflict", "the lane already has an active checklist")
84
+ if "checklist" not in args:
85
+ identity = (repository, lane.branch, args["pr"], args["head_sha"])
86
+ if (
87
+ current is not None
88
+ and (current.repository, current.branch, current.pr, current.head_sha) != identity
89
+ ):
90
+ await invalidate_checklist(uow, current)
91
+ return
92
+ steps = _parse_steps(args["checklist"])
93
+ if repository is None:
94
+ raise LedgerError("conflict", "a READY checklist requires a GitHub projection")
95
+ version = current.version if current is not None else 0
96
+ pending = Checklist(
97
+ lane.id, version, repository, lane.branch, args["pr"], args["head_sha"], "pending", steps
98
+ )
99
+ if pending != current:
100
+ await uow.save_checklist(replace(pending, version=version + 1))
101
+
102
+
103
+ async def invalidate_checklist(uow: UnitOfWork, checklist: Checklist | None) -> None:
104
+ if checklist is not None and checklist.state == "pending":
105
+ await uow.save_checklist(replace(checklist, state="invalidated"))
106
+
107
+
108
+ async def activate_checklist(uow: UnitOfWork, checklist: Checklist) -> None:
109
+ await uow.save_checklist(replace(checklist, state="active"))
110
+ if not checklist.steps:
111
+ await propose_lane_archive(uow, checklist.lane)
112
+
113
+
114
+ async def propose_lane_archive(uow: UnitOfWork, lane: str) -> None:
115
+ context: dict[str, Any] = {"lane": lane}
116
+ checklist = await uow.find_checklist(lane)
117
+ if checklist is not None and checklist.state == "active" and not checklist.open_steps():
118
+ # The checklist becoming actionable wakes an earlier snoozed proposal under the same ID.
119
+ context["checklist_version"] = checklist.version
120
+ await uow.raise_item(ARCHIVE_PROPOSAL, lane, "orchestrator", context)
121
+
122
+
123
+ async def complete_checklist_step(
124
+ uow: UnitOfWork, lane: str, name: str, completion_ref: str | None
125
+ ) -> dict[str, Any]:
126
+ checklist = await uow.find_checklist(lane)
127
+ if checklist is None or checklist.state != "active":
128
+ raise LedgerError("conflict", "the lane has no active checklist")
129
+ if completion_ref is not None and not completion_ref.strip():
130
+ raise LedgerError("invalid", "completion ref must be non-empty")
131
+ for step in checklist.steps:
132
+ if step.step != name:
133
+ continue
134
+ if step.completion_ref is not None:
135
+ return {"changed": False, **asdict(step)}
136
+ ref = completion_ref or step.ref
137
+ if not await uow.set_checklist_step_completion(lane, checklist.version, name, ref):
138
+ raise LedgerError("conflict", "the checklist step is no longer open for this version")
139
+ if checklist.open_steps() == (step,):
140
+ await propose_lane_archive(uow, lane)
141
+ await uow.append(
142
+ NewEvent(
143
+ "core",
144
+ CHECKLIST_EMPTY,
145
+ f"{lane}:{checklist.version}",
146
+ {"lane": lane, "version": checklist.version},
147
+ lane_id=lane,
148
+ )
149
+ )
150
+ return {"changed": True, **asdict(replace(step, completion_ref=ref))}
151
+ raise LedgerError("not_found", "checklist step not found")