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.
- plotroom/__init__.py +0 -0
- plotroom/__main__.py +8 -0
- plotroom/_secret_file.py +8 -0
- plotroom/adapters/__init__.py +0 -0
- plotroom/adapters/claude_desktop.py +219 -0
- plotroom/adapters/embedding_http.py +175 -0
- plotroom/adapters/github/__init__.py +337 -0
- plotroom/adapters/github/app_credential.py +209 -0
- plotroom/adapters/github/ci.py +738 -0
- plotroom/adapters/github/commit_statuses.py +63 -0
- plotroom/adapters/github/deployments.py +213 -0
- plotroom/adapters/github/details.py +867 -0
- plotroom/adapters/github/issue_details.py +368 -0
- plotroom/adapters/github/junit.py +210 -0
- plotroom/adapters/github/junit_xml.py +162 -0
- plotroom/adapters/github/kodus_review.py +347 -0
- plotroom/adapters/github/pr_details.py +192 -0
- plotroom/adapters/github/pull_comparison.py +108 -0
- plotroom/adapters/github/read.py +530 -0
- plotroom/adapters/github/ticket_tree.py +336 -0
- plotroom/adapters/mcp/__init__.py +5 -0
- plotroom/adapters/mcp/proxy.py +722 -0
- plotroom/adapters/mcp/server.py +931 -0
- plotroom/adapters/openrouter.py +151 -0
- plotroom/adapters/store_sqlite/__init__.py +6 -0
- plotroom/adapters/store_sqlite/actions_sql.py +148 -0
- plotroom/adapters/store_sqlite/asks_sql.py +153 -0
- plotroom/adapters/store_sqlite/backfill_sql.py +138 -0
- plotroom/adapters/store_sqlite/budgets_sql.py +102 -0
- plotroom/adapters/store_sqlite/checklists_sql.py +77 -0
- plotroom/adapters/store_sqlite/claims_sql.py +54 -0
- plotroom/adapters/store_sqlite/embedding_sql.py +275 -0
- plotroom/adapters/store_sqlite/github_sql.py +331 -0
- plotroom/adapters/store_sqlite/goals_sql.py +57 -0
- plotroom/adapters/store_sqlite/human_queue_sql.py +60 -0
- plotroom/adapters/store_sqlite/items_sql.py +129 -0
- plotroom/adapters/store_sqlite/lanes_sql.py +85 -0
- plotroom/adapters/store_sqlite/ledger_sql.py +506 -0
- plotroom/adapters/store_sqlite/migrate.py +194 -0
- plotroom/adapters/store_sqlite/migrations/env.py +36 -0
- plotroom/adapters/store_sqlite/migrations/script.py.mako +19 -0
- plotroom/adapters/store_sqlite/migrations/versions/0001_project_meta.py +23 -0
- plotroom/adapters/store_sqlite/migrations/versions/0002_event_log.py +40 -0
- plotroom/adapters/store_sqlite/migrations/versions/0003_next_items.py +49 -0
- plotroom/adapters/store_sqlite/migrations/versions/0004_actions.py +28 -0
- plotroom/adapters/store_sqlite/migrations/versions/0005_lanes.py +36 -0
- plotroom/adapters/store_sqlite/migrations/versions/0006_work_items.py +22 -0
- plotroom/adapters/store_sqlite/migrations/versions/0007_item_changes.py +33 -0
- plotroom/adapters/store_sqlite/migrations/versions/0008_claude_turn_aliases.py +30 -0
- plotroom/adapters/store_sqlite/migrations/versions/0009_github_projection.py +38 -0
- plotroom/adapters/store_sqlite/migrations/versions/0010_github_pull_freshness.py +37 -0
- plotroom/adapters/store_sqlite/migrations/versions/0011_github_resources.py +27 -0
- plotroom/adapters/store_sqlite/migrations/versions/0012_local_triggers_outbox.py +80 -0
- plotroom/adapters/store_sqlite/migrations/versions/0013_lane_claims.py +22 -0
- plotroom/adapters/store_sqlite/migrations/versions/0015_recurring_triggers.py +45 -0
- plotroom/adapters/store_sqlite/migrations/versions/0016_merge_controller.py +28 -0
- plotroom/adapters/store_sqlite/migrations/versions/0017_lane_checklists.py +36 -0
- plotroom/adapters/store_sqlite/migrations/versions/0018_guardrails.py +33 -0
- plotroom/adapters/store_sqlite/migrations/versions/0019_clerk_import_metadata.py +16 -0
- plotroom/adapters/store_sqlite/migrations/versions/0020_search.py +30 -0
- plotroom/adapters/store_sqlite/migrations/versions/0021_human_queue.py +46 -0
- plotroom/adapters/store_sqlite/migrations/versions/0022_search_records.py +77 -0
- plotroom/adapters/store_sqlite/migrations/versions/0023_operator_goals.py +25 -0
- plotroom/adapters/store_sqlite/migrations/versions/0024_embedding_rejections.py +36 -0
- plotroom/adapters/store_sqlite/outbox_sql.py +78 -0
- plotroom/adapters/store_sqlite/routes_sql.py +26 -0
- plotroom/adapters/store_sqlite/search_sql.py +116 -0
- plotroom/adapters/store_sqlite/store.py +623 -0
- plotroom/adapters/store_sqlite/triggers_sql.py +111 -0
- plotroom/adapters/store_sqlite/work_items_sql.py +36 -0
- plotroom/adapters/switchboard.py +152 -0
- plotroom/adapters/usage_fixture.py +11 -0
- plotroom/adapters/work_items_bmad.py +256 -0
- plotroom/adapters/work_items_github_issues.py +323 -0
- plotroom/app/__init__.py +0 -0
- plotroom/app/bridge.py +162 -0
- plotroom/app/clerk_import.py +349 -0
- plotroom/app/cli.py +290 -0
- plotroom/app/clock.py +12 -0
- plotroom/app/config.py +984 -0
- plotroom/app/daemon_start.py +453 -0
- plotroom/app/lock.py +77 -0
- plotroom/app/logging.py +93 -0
- plotroom/app/portfile.py +218 -0
- plotroom/app/runtime.py +518 -0
- plotroom/app/serve.py +671 -0
- plotroom/app/session_intake.py +51 -0
- plotroom/app/sqlite_floor.py +35 -0
- plotroom/app/tokens.py +265 -0
- plotroom/core/__init__.py +0 -0
- plotroom/core/actions.py +67 -0
- plotroom/core/ask_checks.py +124 -0
- plotroom/core/asks.py +123 -0
- plotroom/core/branch_updates.py +207 -0
- plotroom/core/bridge.py +112 -0
- plotroom/core/briefs.py +83 -0
- plotroom/core/budgets.py +182 -0
- plotroom/core/catalog.py +464 -0
- plotroom/core/checklists.py +151 -0
- plotroom/core/claims.py +296 -0
- plotroom/core/clerk_import.py +379 -0
- plotroom/core/commands.py +85 -0
- plotroom/core/deploy_window.py +21 -0
- plotroom/core/envelope.py +20 -0
- plotroom/core/events.py +227 -0
- plotroom/core/github.py +306 -0
- plotroom/core/github_projection.py +310 -0
- plotroom/core/goal_language.py +374 -0
- plotroom/core/goal_projection.py +146 -0
- plotroom/core/human_queue.py +119 -0
- plotroom/core/intake.py +106 -0
- plotroom/core/kodus_review.py +53 -0
- plotroom/core/lanes.py +281 -0
- plotroom/core/ledger.py +808 -0
- plotroom/core/merge_controller.py +344 -0
- plotroom/core/merge_flakes.py +422 -0
- plotroom/core/merge_gate.py +724 -0
- plotroom/core/merge_lane.py +70 -0
- plotroom/core/merge_overlap.py +76 -0
- plotroom/core/next_items.py +60 -0
- plotroom/core/operator_goals.py +103 -0
- plotroom/core/outbox.py +70 -0
- plotroom/core/pull_overlaps.py +152 -0
- plotroom/core/reports.py +62 -0
- plotroom/core/roles.py +100 -0
- plotroom/core/routing.py +55 -0
- plotroom/core/run_reports.py +53 -0
- plotroom/core/search.py +266 -0
- plotroom/core/search_sources.py +94 -0
- plotroom/core/snapshot.py +204 -0
- plotroom/core/trigger_actions.py +89 -0
- plotroom/core/triggers.py +550 -0
- plotroom/core/unit_of_work.py +541 -0
- plotroom/core/watchers.py +35 -0
- plotroom/core/work_items.py +23 -0
- plotroom/core/writer.py +164 -0
- plotroom/ports/__init__.py +6 -0
- plotroom/ports/clock.py +12 -0
- plotroom/ports/github_write.py +72 -0
- plotroom/ports/search.py +197 -0
- plotroom/ports/session_reader.py +23 -0
- plotroom/ports/store.py +323 -0
- plotroom/ports/usage_meter.py +35 -0
- plotroom/ports/work_items.py +22 -0
- plotroom/py.typed +0 -0
- plotroom-0.1.0.dist-info/METADATA +230 -0
- plotroom-0.1.0.dist-info/RECORD +150 -0
- plotroom-0.1.0.dist-info/WHEEL +4 -0
- plotroom-0.1.0.dist-info/entry_points.txt +2 -0
- plotroom-0.1.0.dist-info/licenses/LICENSE +21 -0
plotroom/app/runtime.py
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
"""Per-project ticks and post-commit action dispatch."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import logging
|
|
5
|
+
from collections.abc import Awaitable, Callable, Mapping
|
|
6
|
+
from dataclasses import replace
|
|
7
|
+
from datetime import timedelta
|
|
8
|
+
|
|
9
|
+
from plotroom.core.actions import (
|
|
10
|
+
BRANCH_UPDATE,
|
|
11
|
+
CONTROLLER_MERGE,
|
|
12
|
+
SWITCHBOARD_SEND,
|
|
13
|
+
Action,
|
|
14
|
+
finish_action,
|
|
15
|
+
record_attempt,
|
|
16
|
+
)
|
|
17
|
+
from plotroom.core.branch_updates import (
|
|
18
|
+
BRANCH_UPDATE_DECLINED,
|
|
19
|
+
BranchUpdate,
|
|
20
|
+
authorize_branch_update,
|
|
21
|
+
complete_branch_update,
|
|
22
|
+
eligible_branch_update,
|
|
23
|
+
previous_branch_update,
|
|
24
|
+
receipt_matches,
|
|
25
|
+
stage_branch_update,
|
|
26
|
+
)
|
|
27
|
+
from plotroom.core.budgets import BUDGET_ITEM, record_budget_observations
|
|
28
|
+
from plotroom.core.events import Event, NewEvent, format_time
|
|
29
|
+
from plotroom.core.ledger import Ledger
|
|
30
|
+
from plotroom.core.merge_controller import MERGE_READY
|
|
31
|
+
from plotroom.core.merge_gate import (
|
|
32
|
+
GateSnapshot,
|
|
33
|
+
authorized_label,
|
|
34
|
+
determine_main_verdict,
|
|
35
|
+
evaluate_gate,
|
|
36
|
+
load_gate_snapshot,
|
|
37
|
+
)
|
|
38
|
+
from plotroom.core.outbox import OutboxMessage
|
|
39
|
+
from plotroom.core.trigger_actions import (
|
|
40
|
+
MARK,
|
|
41
|
+
NOTIFY,
|
|
42
|
+
PROPOSE_ARCHIVE,
|
|
43
|
+
SEND,
|
|
44
|
+
DeliveryRoute,
|
|
45
|
+
apply_local_action,
|
|
46
|
+
)
|
|
47
|
+
from plotroom.core.unit_of_work import UnitOfWork
|
|
48
|
+
from plotroom.core.work_items import refresh_idle_workers
|
|
49
|
+
from plotroom.ports.clock import Clock
|
|
50
|
+
from plotroom.ports.github_write import FixtureGitHubMerge, FixtureGitHubWrite
|
|
51
|
+
from plotroom.ports.usage_meter import UsageMeter
|
|
52
|
+
from plotroom.ports.work_items import WorkItem, WorkItemSource
|
|
53
|
+
|
|
54
|
+
type ActionHandler = Callable[[Action], Awaitable[None]]
|
|
55
|
+
log = logging.getLogger("plotroom.runtime")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class _BranchUpdateDeferred(Exception):
|
|
59
|
+
"""Roll back a no-op without waking the action runner again."""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class _FixtureMergeDeferred(Exception):
|
|
63
|
+
"""Keep an unready action queued without spinning on a no-op transaction."""
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class _SendRouteDeferred(Exception):
|
|
67
|
+
"""Keep a cap stop pending until its live lane has a delivery channel."""
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
async def _worker_owns_lane(uow: UnitOfWork, worker: str, lane_id: str) -> bool:
|
|
71
|
+
lane = await uow.worker_lane(worker)
|
|
72
|
+
return lane is not None and lane.id == lane_id
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class ProjectRuntime:
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
ledger: Ledger,
|
|
79
|
+
clock: Clock,
|
|
80
|
+
*,
|
|
81
|
+
tick_seconds: int,
|
|
82
|
+
action_attempts: int,
|
|
83
|
+
handlers: Mapping[str, ActionHandler] | None = None,
|
|
84
|
+
delivery_routes: Mapping[str, DeliveryRoute] | None = None,
|
|
85
|
+
workers: frozenset[str] = frozenset(),
|
|
86
|
+
work_items: WorkItemSource | None = None,
|
|
87
|
+
inputs: tuple[Callable[[], Awaitable[None]], ...] = (),
|
|
88
|
+
fixture_github_write: FixtureGitHubWrite | None = None,
|
|
89
|
+
fixture_github_merge: FixtureGitHubMerge | None = None,
|
|
90
|
+
usage_meter: UsageMeter | None = None,
|
|
91
|
+
switchboard_bridged: bool = False,
|
|
92
|
+
) -> None:
|
|
93
|
+
self.ledger = ledger
|
|
94
|
+
self.clock = clock
|
|
95
|
+
self.interval = timedelta(seconds=tick_seconds)
|
|
96
|
+
self.next_tick = clock.now() + self.interval
|
|
97
|
+
self.action_attempts = action_attempts
|
|
98
|
+
self.handlers: Mapping[str, ActionHandler] = handlers if handlers is not None else {}
|
|
99
|
+
self.delivery_routes = dict(delivery_routes or {})
|
|
100
|
+
self.workers = workers
|
|
101
|
+
self.work_items = work_items
|
|
102
|
+
self.usage_meter = usage_meter
|
|
103
|
+
self.inputs = inputs
|
|
104
|
+
self._bridge_workers = (
|
|
105
|
+
tuple(
|
|
106
|
+
worker
|
|
107
|
+
for worker, route in self.delivery_routes.items()
|
|
108
|
+
if route.channel == "switchboard"
|
|
109
|
+
)
|
|
110
|
+
if switchboard_bridged
|
|
111
|
+
else None
|
|
112
|
+
)
|
|
113
|
+
self.fixture_github_write = fixture_github_write
|
|
114
|
+
self._fixture_github_merge = (
|
|
115
|
+
fixture_github_merge if ledger.merge_gate_policy.controller_merges is True else None
|
|
116
|
+
)
|
|
117
|
+
self._action_lock = asyncio.Lock()
|
|
118
|
+
self._tasks: list[asyncio.Task[None]] = []
|
|
119
|
+
|
|
120
|
+
async def tick_if_due(self) -> bool:
|
|
121
|
+
now = self.clock.now()
|
|
122
|
+
if now < self.next_tick:
|
|
123
|
+
return False
|
|
124
|
+
scheduled_at = format_time(self.next_tick)
|
|
125
|
+
items: tuple[WorkItem, ...] | None = ()
|
|
126
|
+
if self.work_items is not None:
|
|
127
|
+
try:
|
|
128
|
+
items = await self.work_items.ready_items()
|
|
129
|
+
except (OSError, ValueError, TimeoutError) as exc:
|
|
130
|
+
items = None # keep the last committed mirror if the source is unavailable
|
|
131
|
+
log.warning(
|
|
132
|
+
"could not refresh the project ticket tree",
|
|
133
|
+
extra={"project": self.ledger.project_id, "reason": str(exc)},
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
readings = ()
|
|
137
|
+
if self.usage_meter is not None:
|
|
138
|
+
try:
|
|
139
|
+
async with asyncio.timeout(self.interval.total_seconds()):
|
|
140
|
+
readings = await self.usage_meter.observations()
|
|
141
|
+
except OSError, ValueError, TimeoutError:
|
|
142
|
+
log.warning("could not read usage meter", extra={"project": self.ledger.project_id})
|
|
143
|
+
|
|
144
|
+
async def tick(uow: UnitOfWork) -> None:
|
|
145
|
+
if not (await uow.append(NewEvent("core", "core.tick", scheduled_at))).created:
|
|
146
|
+
return
|
|
147
|
+
if items is not None:
|
|
148
|
+
await uow.replace_work_items(items)
|
|
149
|
+
await record_budget_observations(uow, readings)
|
|
150
|
+
await uow.retire_stale_outbox()
|
|
151
|
+
await refresh_idle_workers(uow, self.workers)
|
|
152
|
+
await self.ledger.watchers.reconcile(uow)
|
|
153
|
+
uow.all_merge_pulls = True
|
|
154
|
+
|
|
155
|
+
await self.ledger.run("system", tick)
|
|
156
|
+
# Keep the cadence after delays without replaying a burst of stale ticks.
|
|
157
|
+
finished = max(now, self.clock.now())
|
|
158
|
+
self.next_tick += ((finished - self.next_tick) // self.interval + 1) * self.interval
|
|
159
|
+
return True
|
|
160
|
+
|
|
161
|
+
async def run_one_action(self) -> bool:
|
|
162
|
+
async with self._action_lock:
|
|
163
|
+
after = 0
|
|
164
|
+
while True:
|
|
165
|
+
page = await self.ledger.pending_actions(
|
|
166
|
+
after=after,
|
|
167
|
+
skip_branch_updates=self.fixture_github_write is None,
|
|
168
|
+
skip_controller_merges=self._fixture_github_merge is None,
|
|
169
|
+
skip_bridge_workers=self._bridge_workers,
|
|
170
|
+
)
|
|
171
|
+
if not page:
|
|
172
|
+
return False
|
|
173
|
+
for entry in page:
|
|
174
|
+
if await self._run_action(entry.action):
|
|
175
|
+
return True
|
|
176
|
+
after = page[-1].position
|
|
177
|
+
|
|
178
|
+
async def _run_action(self, action: Action) -> bool:
|
|
179
|
+
pinned = action.kind in (BRANCH_UPDATE, CONTROLLER_MERGE)
|
|
180
|
+
if action.attempts >= self.action_attempts and not pinned:
|
|
181
|
+
await self.ledger.run("system", lambda uow: finish_action(uow, action, "failed"))
|
|
182
|
+
return True
|
|
183
|
+
failed = False
|
|
184
|
+
try:
|
|
185
|
+
if await self._dispatch(action):
|
|
186
|
+
return True
|
|
187
|
+
except _BranchUpdateDeferred, _FixtureMergeDeferred, _SendRouteDeferred:
|
|
188
|
+
return False
|
|
189
|
+
except Exception:
|
|
190
|
+
if action.kind == BRANCH_UPDATE:
|
|
191
|
+
log.warning("fixture branch update dispatch was deferred")
|
|
192
|
+
return False
|
|
193
|
+
# Action payloads and exception text may be sensitive. The item points to the key.
|
|
194
|
+
log.warning("action attempt failed", extra={"kind": action.kind})
|
|
195
|
+
failed = True
|
|
196
|
+
|
|
197
|
+
async def commit_attempt(uow: UnitOfWork) -> None:
|
|
198
|
+
completed = await record_attempt(uow, action)
|
|
199
|
+
if not failed:
|
|
200
|
+
await finish_action(uow, completed, "done")
|
|
201
|
+
# A fixture response may have been lost on the last allowed attempt.
|
|
202
|
+
elif completed.attempts >= self.action_attempts and (
|
|
203
|
+
not pinned or action.attempts >= self.action_attempts
|
|
204
|
+
):
|
|
205
|
+
await finish_action(uow, completed, "failed")
|
|
206
|
+
|
|
207
|
+
await self.ledger.run("system", commit_attempt)
|
|
208
|
+
return True
|
|
209
|
+
|
|
210
|
+
def _eligible_update(self, uow: UnitOfWork, snapshot: GateSnapshot) -> BranchUpdate | None:
|
|
211
|
+
if self.ledger.github_error is not None:
|
|
212
|
+
return None
|
|
213
|
+
return eligible_branch_update(snapshot, self.ledger.merge_gate_policy, uow.now())
|
|
214
|
+
|
|
215
|
+
async def stage_branch_update(self, number: int) -> bool:
|
|
216
|
+
"""Queue an eligible action using the same snapshot and policy as the dry gate."""
|
|
217
|
+
if self.fixture_github_write is None:
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
async def stage(uow: UnitOfWork) -> bool:
|
|
221
|
+
snapshot = await load_gate_snapshot(uow, self.ledger.github_repository, number)
|
|
222
|
+
update = self._eligible_update(uow, snapshot)
|
|
223
|
+
return update is not None and await stage_branch_update(uow, update)
|
|
224
|
+
|
|
225
|
+
return await self.ledger.run("system", stage)
|
|
226
|
+
|
|
227
|
+
async def _dispatch_branch_update(self, action: Action) -> None:
|
|
228
|
+
port = self.fixture_github_write
|
|
229
|
+
repository = action.payload["repository"]
|
|
230
|
+
number = action.payload["number"]
|
|
231
|
+
head_sha = action.payload["head_sha"]
|
|
232
|
+
branch = action.payload.get("branch")
|
|
233
|
+
if (
|
|
234
|
+
port is None
|
|
235
|
+
or repository != self.ledger.github_repository
|
|
236
|
+
or action.key != f"{BRANCH_UPDATE}:{number}:{head_sha}"
|
|
237
|
+
):
|
|
238
|
+
raise _BranchUpdateDeferred
|
|
239
|
+
|
|
240
|
+
def allowed(update: BranchUpdate | None) -> bool:
|
|
241
|
+
return update is not None and update.key == action.key and update.branch == branch
|
|
242
|
+
|
|
243
|
+
async def prepare(uow: UnitOfWork) -> tuple[Action, Event] | None:
|
|
244
|
+
snapshot = await load_gate_snapshot(uow, repository, number)
|
|
245
|
+
if snapshot.freeze_reason is not None:
|
|
246
|
+
raise _BranchUpdateDeferred
|
|
247
|
+
if action.payload.get("bound_receipt") is not True:
|
|
248
|
+
await finish_action(
|
|
249
|
+
uow, action, "failed", failure_reason="legacy unbound branch update"
|
|
250
|
+
)
|
|
251
|
+
return None
|
|
252
|
+
prior = await previous_branch_update(uow, action)
|
|
253
|
+
if action.attempts and prior is None:
|
|
254
|
+
log.warning("fixture branch update authorization is unavailable")
|
|
255
|
+
raise _BranchUpdateDeferred
|
|
256
|
+
declined = (
|
|
257
|
+
await uow.find_event("core", BRANCH_UPDATE_DECLINED, prior.upstream_id)
|
|
258
|
+
if prior is not None
|
|
259
|
+
else None
|
|
260
|
+
)
|
|
261
|
+
outstanding = prior is not None and declined is None
|
|
262
|
+
update = self._eligible_update(uow, snapshot)
|
|
263
|
+
if not outstanding and update is not None and allowed(update):
|
|
264
|
+
calls_before = (
|
|
265
|
+
prior.fields["calls_before"] + int(declined.fields["sent"])
|
|
266
|
+
if prior is not None and declined is not None
|
|
267
|
+
else 0
|
|
268
|
+
)
|
|
269
|
+
if calls_before >= self.action_attempts:
|
|
270
|
+
await finish_action(uow, action, "failed")
|
|
271
|
+
return None
|
|
272
|
+
result = await authorize_branch_update(
|
|
273
|
+
uow, action, update, calls_before=calls_before
|
|
274
|
+
)
|
|
275
|
+
if result is not None:
|
|
276
|
+
return result
|
|
277
|
+
raise _BranchUpdateDeferred
|
|
278
|
+
|
|
279
|
+
try:
|
|
280
|
+
receipt = await port.update_branch(
|
|
281
|
+
repository=repository,
|
|
282
|
+
number=number,
|
|
283
|
+
action_key=action.key,
|
|
284
|
+
expected_head_sha=head_sha,
|
|
285
|
+
expected_main_sha=None,
|
|
286
|
+
recovery_only=True,
|
|
287
|
+
)
|
|
288
|
+
except Exception as exc:
|
|
289
|
+
log.warning("fixture branch update receipt lookup failed")
|
|
290
|
+
raise _BranchUpdateDeferred from exc
|
|
291
|
+
if receipt is not None:
|
|
292
|
+
if not outstanding or prior is None or not receipt_matches(receipt, prior):
|
|
293
|
+
log.warning("fixture branch update receipt did not match its authorization")
|
|
294
|
+
raise _BranchUpdateDeferred
|
|
295
|
+
await complete_branch_update(uow, action, receipt, branch)
|
|
296
|
+
return None
|
|
297
|
+
pull = snapshot.pull
|
|
298
|
+
if not outstanding and pull is not None and pull.complete and pull.state == "merged":
|
|
299
|
+
await finish_action(uow, action, "done")
|
|
300
|
+
return None
|
|
301
|
+
raise _BranchUpdateDeferred
|
|
302
|
+
|
|
303
|
+
prepared = await self.ledger.run("system", prepare)
|
|
304
|
+
if prepared is None:
|
|
305
|
+
return
|
|
306
|
+
attempted, authorization = prepared
|
|
307
|
+
|
|
308
|
+
async def submit(uow: UnitOfWork) -> None:
|
|
309
|
+
snapshot = await load_gate_snapshot(uow, repository, number)
|
|
310
|
+
update = self._eligible_update(uow, snapshot)
|
|
311
|
+
main_sha = authorization.fields["main_sha"]
|
|
312
|
+
if update is None or not allowed(update) or update.main_sha != main_sha:
|
|
313
|
+
# The gate moved between commits. This turn has sent nothing.
|
|
314
|
+
await uow.append(
|
|
315
|
+
NewEvent(
|
|
316
|
+
"core", BRANCH_UPDATE_DECLINED, authorization.upstream_id, {"sent": False}
|
|
317
|
+
)
|
|
318
|
+
)
|
|
319
|
+
return
|
|
320
|
+
try:
|
|
321
|
+
receipt = await port.update_branch(
|
|
322
|
+
repository=repository,
|
|
323
|
+
number=number,
|
|
324
|
+
action_key=action.key,
|
|
325
|
+
expected_head_sha=head_sha,
|
|
326
|
+
expected_main_sha=main_sha,
|
|
327
|
+
recovery_only=False,
|
|
328
|
+
)
|
|
329
|
+
except Exception:
|
|
330
|
+
log.warning("fixture branch update outcome is unknown; receipt required")
|
|
331
|
+
return
|
|
332
|
+
if receipt is None:
|
|
333
|
+
await uow.append(
|
|
334
|
+
NewEvent(
|
|
335
|
+
"core", BRANCH_UPDATE_DECLINED, authorization.upstream_id, {"sent": True}
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
elif receipt_matches(receipt, authorization):
|
|
339
|
+
await complete_branch_update(uow, attempted, receipt, branch)
|
|
340
|
+
else:
|
|
341
|
+
log.warning("fixture branch update receipt did not match its authorization")
|
|
342
|
+
|
|
343
|
+
await self.ledger.run("system", submit)
|
|
344
|
+
|
|
345
|
+
async def _dispatch_fixture_merge(self, action: Action) -> None:
|
|
346
|
+
port = self._fixture_github_merge
|
|
347
|
+
policy = self.ledger.merge_gate_policy
|
|
348
|
+
repository, number = action.payload["repository"], action.payload["number"]
|
|
349
|
+
head, main = action.payload["head_sha"], action.payload["main_sha"]
|
|
350
|
+
method = action.payload["method"]
|
|
351
|
+
effect_key = action.payload["effect_key"]
|
|
352
|
+
if port is None or repository != self.ledger.github_repository:
|
|
353
|
+
raise _FixtureMergeDeferred
|
|
354
|
+
|
|
355
|
+
async def dispatch(uow: UnitOfWork) -> None:
|
|
356
|
+
local = self.ledger.gate_availability(await load_gate_snapshot(uow, repository, number))
|
|
357
|
+
evidence = await port.merge_evidence(
|
|
358
|
+
repository=repository,
|
|
359
|
+
number=number,
|
|
360
|
+
action_key=effect_key,
|
|
361
|
+
)
|
|
362
|
+
if evidence.applied is True:
|
|
363
|
+
await finish_action(uow, action, "done")
|
|
364
|
+
return
|
|
365
|
+
|
|
366
|
+
pull = local.pull
|
|
367
|
+
if pull is None or pull.state != "open" or pull.head_sha != head:
|
|
368
|
+
await finish_action(uow, action, "done")
|
|
369
|
+
return
|
|
370
|
+
item = await uow.active_item(MERGE_READY, str(number))
|
|
371
|
+
if (
|
|
372
|
+
item is None
|
|
373
|
+
or item.id != action.payload["item_id"]
|
|
374
|
+
or method != policy.merge_method
|
|
375
|
+
):
|
|
376
|
+
await finish_action(uow, action, "done")
|
|
377
|
+
return
|
|
378
|
+
now = uow.now()
|
|
379
|
+
actual = evidence.snapshot
|
|
380
|
+
if (
|
|
381
|
+
self.ledger.github_error is not None
|
|
382
|
+
or item.state != "open"
|
|
383
|
+
or (item.context["head_sha"], item.context["main_sha"]) != (head, main)
|
|
384
|
+
or actual is None
|
|
385
|
+
or not evidence.revision
|
|
386
|
+
or actual.pull is None
|
|
387
|
+
or (actual.pull.repository, actual.pull.number) != (repository, number)
|
|
388
|
+
):
|
|
389
|
+
raise _FixtureMergeDeferred
|
|
390
|
+
|
|
391
|
+
for snapshot in (local, actual):
|
|
392
|
+
if (
|
|
393
|
+
evaluate_gate(
|
|
394
|
+
snapshot,
|
|
395
|
+
policy,
|
|
396
|
+
now,
|
|
397
|
+
expected_head_sha=head,
|
|
398
|
+
expected_main_sha=main,
|
|
399
|
+
).verdict
|
|
400
|
+
!= "MERGE"
|
|
401
|
+
or determine_main_verdict(snapshot, policy, now).kind != "green"
|
|
402
|
+
or authorized_label(snapshot, policy, now, policy.fix_label) is not None
|
|
403
|
+
):
|
|
404
|
+
raise _FixtureMergeDeferred
|
|
405
|
+
|
|
406
|
+
if action.attempts >= self.action_attempts:
|
|
407
|
+
await finish_action(uow, action, "failed")
|
|
408
|
+
return
|
|
409
|
+
if not await port.merge(
|
|
410
|
+
repository=repository,
|
|
411
|
+
number=number,
|
|
412
|
+
action_key=effect_key,
|
|
413
|
+
expected_head_sha=head,
|
|
414
|
+
expected_main_sha=main,
|
|
415
|
+
method=policy.merge_method,
|
|
416
|
+
evidence_revision=evidence.revision,
|
|
417
|
+
):
|
|
418
|
+
raise _FixtureMergeDeferred
|
|
419
|
+
await finish_action(uow, await record_attempt(uow, action), "done")
|
|
420
|
+
|
|
421
|
+
await self.ledger.run("system", dispatch)
|
|
422
|
+
|
|
423
|
+
async def _dispatch(self, action: Action) -> bool:
|
|
424
|
+
"""Dispatch the effect. Local effects record their attempt in the same commit."""
|
|
425
|
+
if action.kind == BRANCH_UPDATE:
|
|
426
|
+
await self._dispatch_branch_update(action)
|
|
427
|
+
return True
|
|
428
|
+
if action.kind == CONTROLLER_MERGE:
|
|
429
|
+
await self._dispatch_fixture_merge(action)
|
|
430
|
+
return True
|
|
431
|
+
if action.kind == SEND:
|
|
432
|
+
worker = action.payload["worker"]
|
|
433
|
+
pinned_lane = (
|
|
434
|
+
action.payload["lane"] if action.key.startswith(f"{BUDGET_ITEM.name}:") else None
|
|
435
|
+
)
|
|
436
|
+
route = self.delivery_routes.get(worker)
|
|
437
|
+
if pinned_lane is not None and (route is None or route.channel == "switchboard"):
|
|
438
|
+
|
|
439
|
+
async def check_lane(uow: UnitOfWork) -> bool:
|
|
440
|
+
current = await _worker_owns_lane(uow, worker, pinned_lane)
|
|
441
|
+
if current and route is None:
|
|
442
|
+
# Roll back the read so the action runner does not wake itself to retry.
|
|
443
|
+
raise _SendRouteDeferred
|
|
444
|
+
return current
|
|
445
|
+
|
|
446
|
+
if not await self.ledger.run("system", check_lane):
|
|
447
|
+
return False
|
|
448
|
+
if route is None:
|
|
449
|
+
raise KeyError(worker)
|
|
450
|
+
if route.channel == "switchboard":
|
|
451
|
+
payload = {"to": route.destination, "body": action.payload["text"]}
|
|
452
|
+
if pinned_lane is not None:
|
|
453
|
+
payload.update(worker=worker, lane=pinned_lane)
|
|
454
|
+
await self.handlers[SWITCHBOARD_SEND](
|
|
455
|
+
replace(action, kind=SWITCHBOARD_SEND, payload=payload)
|
|
456
|
+
)
|
|
457
|
+
return False
|
|
458
|
+
|
|
459
|
+
async def stage(uow: UnitOfWork) -> None:
|
|
460
|
+
if pinned_lane is not None and not await _worker_owns_lane(
|
|
461
|
+
uow, worker, pinned_lane
|
|
462
|
+
):
|
|
463
|
+
return
|
|
464
|
+
await uow.insert_outbox(
|
|
465
|
+
OutboxMessage(
|
|
466
|
+
action.key,
|
|
467
|
+
worker,
|
|
468
|
+
route.destination,
|
|
469
|
+
action.payload["text"],
|
|
470
|
+
await uow.allocate_seq(),
|
|
471
|
+
)
|
|
472
|
+
)
|
|
473
|
+
|
|
474
|
+
await self._commit_local(action, stage)
|
|
475
|
+
return True
|
|
476
|
+
if action.kind in (NOTIFY, MARK, PROPOSE_ARCHIVE):
|
|
477
|
+
|
|
478
|
+
async def apply(uow: UnitOfWork) -> None:
|
|
479
|
+
await apply_local_action(uow, action, self.ledger.github_repository)
|
|
480
|
+
|
|
481
|
+
await self._commit_local(action, apply)
|
|
482
|
+
return True
|
|
483
|
+
await self.handlers[action.kind](action)
|
|
484
|
+
return False
|
|
485
|
+
|
|
486
|
+
async def _commit_local(
|
|
487
|
+
self, action: Action, apply: Callable[[UnitOfWork], Awaitable[None]]
|
|
488
|
+
) -> None:
|
|
489
|
+
async def commit(uow: UnitOfWork) -> None:
|
|
490
|
+
attempted = await record_attempt(uow, action)
|
|
491
|
+
await apply(uow)
|
|
492
|
+
await finish_action(uow, attempted, "done")
|
|
493
|
+
|
|
494
|
+
await self.ledger.run("system", commit)
|
|
495
|
+
|
|
496
|
+
def start(self) -> None:
|
|
497
|
+
self._tasks = [asyncio.create_task(self._ticks()), asyncio.create_task(self._actions())]
|
|
498
|
+
self._tasks.extend(asyncio.ensure_future(read()) for read in self.inputs)
|
|
499
|
+
|
|
500
|
+
async def stop(self) -> None:
|
|
501
|
+
for task in self._tasks:
|
|
502
|
+
task.cancel()
|
|
503
|
+
results = await asyncio.gather(*self._tasks, return_exceptions=True)
|
|
504
|
+
for result in results:
|
|
505
|
+
if isinstance(result, Exception):
|
|
506
|
+
log.error("project background task failed", exc_info=result)
|
|
507
|
+
self._tasks.clear()
|
|
508
|
+
|
|
509
|
+
async def _ticks(self) -> None:
|
|
510
|
+
while True:
|
|
511
|
+
await asyncio.sleep(max(0, (self.next_tick - self.clock.now()).total_seconds()))
|
|
512
|
+
await self.tick_if_due()
|
|
513
|
+
|
|
514
|
+
async def _actions(self) -> None:
|
|
515
|
+
while True:
|
|
516
|
+
committed = self.ledger.commit_signal
|
|
517
|
+
if not await self.run_one_action():
|
|
518
|
+
await committed.wait()
|