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
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
"""The unit of work (AD-14): the handle given to one input's work inside the writer transaction.
|
|
2
|
+
|
|
3
|
+
Everything done through it commits or rolls back together. It is the extension point for later
|
|
4
|
+
consequences (adapter cursors, next items, triggers, actions).
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import copy
|
|
10
|
+
import uuid
|
|
11
|
+
from collections.abc import Callable, Mapping
|
|
12
|
+
from dataclasses import replace
|
|
13
|
+
from datetime import datetime
|
|
14
|
+
from typing import TYPE_CHECKING, Any
|
|
15
|
+
|
|
16
|
+
from plotroom.core.actions import Action
|
|
17
|
+
from plotroom.core.asks import reconcile_asks
|
|
18
|
+
from plotroom.core.budgets import BUDGET_ITEM, BUDGET_THRESHOLD, Budget
|
|
19
|
+
from plotroom.core.claims import Claim, reconcile_claim_pairs
|
|
20
|
+
from plotroom.core.events import (
|
|
21
|
+
AppendResult,
|
|
22
|
+
Event,
|
|
23
|
+
EventRegistry,
|
|
24
|
+
LedgerError,
|
|
25
|
+
NewEvent,
|
|
26
|
+
format_time,
|
|
27
|
+
validate_fields,
|
|
28
|
+
)
|
|
29
|
+
from plotroom.core.github import PULL_REQUESTS, FetchCursor, FetchRetry, PullRequest
|
|
30
|
+
from plotroom.core.github_projection import (
|
|
31
|
+
ASK_COMMENT_HISTORY,
|
|
32
|
+
CHECKS,
|
|
33
|
+
COMMENTS,
|
|
34
|
+
COMMIT_STATUSES,
|
|
35
|
+
ISSUES,
|
|
36
|
+
KODUS_REVIEW,
|
|
37
|
+
LABELS,
|
|
38
|
+
MAIN,
|
|
39
|
+
MAIN_CI,
|
|
40
|
+
PULL_TEXT,
|
|
41
|
+
THREADS,
|
|
42
|
+
GitHubResource,
|
|
43
|
+
)
|
|
44
|
+
from plotroom.core.lanes import Lane
|
|
45
|
+
from plotroom.core.merge_overlap import PULL_COMPARISON
|
|
46
|
+
from plotroom.core.next_items import Item, ItemKind
|
|
47
|
+
from plotroom.core.pull_overlaps import reconcile_pull_overlaps
|
|
48
|
+
from plotroom.core.roles import role_family
|
|
49
|
+
from plotroom.core.search_sources import (
|
|
50
|
+
ask_record,
|
|
51
|
+
comment_record,
|
|
52
|
+
decision_record,
|
|
53
|
+
pull_body_record,
|
|
54
|
+
pull_text_record,
|
|
55
|
+
)
|
|
56
|
+
from plotroom.core.trigger_actions import SEND
|
|
57
|
+
from plotroom.ports.clock import Clock
|
|
58
|
+
from plotroom.ports.search import (
|
|
59
|
+
BackfillPage,
|
|
60
|
+
BackfillPosition,
|
|
61
|
+
BackfillSource,
|
|
62
|
+
EmbeddingIndex,
|
|
63
|
+
IndexedEmbedding,
|
|
64
|
+
Search,
|
|
65
|
+
SearchBackfill,
|
|
66
|
+
SearchRecord,
|
|
67
|
+
)
|
|
68
|
+
from plotroom.ports.store import Transaction
|
|
69
|
+
from plotroom.ports.work_items import WorkItem
|
|
70
|
+
|
|
71
|
+
if TYPE_CHECKING:
|
|
72
|
+
from plotroom.core.ask_checks import Ask, IssueComment
|
|
73
|
+
from plotroom.core.checklists import Checklist
|
|
74
|
+
from plotroom.core.goal_projection import StoredGoal
|
|
75
|
+
from plotroom.core.human_queue import Decision
|
|
76
|
+
from plotroom.core.outbox import OutboxLease, OutboxMessage
|
|
77
|
+
from plotroom.core.triggers import Trigger
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def new_event_id() -> str:
|
|
81
|
+
return f"evt_{uuid.uuid7()}"
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class UnitOfWork:
|
|
85
|
+
"""One input's transaction, its role and the services its work may use.
|
|
86
|
+
|
|
87
|
+
``role`` is who the input comes from. ``append`` checks events against ``append_role``, which
|
|
88
|
+
is the same role for a plain event input and ``system`` inside a command (core's own handling).
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
def __init__(
|
|
92
|
+
self,
|
|
93
|
+
tx: Transaction,
|
|
94
|
+
events: EventRegistry,
|
|
95
|
+
clock: Clock,
|
|
96
|
+
*,
|
|
97
|
+
role: str,
|
|
98
|
+
items: Mapping[str, ItemKind] | None = None,
|
|
99
|
+
append_role: str | None = None,
|
|
100
|
+
id_factory: Callable[[], str] = new_event_id,
|
|
101
|
+
search: Search | None = None,
|
|
102
|
+
) -> None:
|
|
103
|
+
self._tx = tx
|
|
104
|
+
self._events = events
|
|
105
|
+
self._clock = clock
|
|
106
|
+
self._role = role
|
|
107
|
+
self._items: Mapping[str, ItemKind] = items if items is not None else {}
|
|
108
|
+
self._append_role = append_role if append_role is not None else role
|
|
109
|
+
self._id_factory = id_factory
|
|
110
|
+
self._search = search
|
|
111
|
+
self._claims_changed = False
|
|
112
|
+
self._pulls_changed = False
|
|
113
|
+
self.all_merge_pulls = False
|
|
114
|
+
self.merge_pull_numbers: set[int] = set()
|
|
115
|
+
self.merge_check_heads: set[str] = set()
|
|
116
|
+
self.merge_observations: dict[int, tuple[PullRequest, bool]] = {}
|
|
117
|
+
self._ask_repositories: set[str] = set()
|
|
118
|
+
self._tick = False
|
|
119
|
+
|
|
120
|
+
@property
|
|
121
|
+
def role(self) -> str:
|
|
122
|
+
return self._role
|
|
123
|
+
|
|
124
|
+
def now(self) -> datetime:
|
|
125
|
+
return self._clock.now()
|
|
126
|
+
|
|
127
|
+
async def find_event(self, source: str, kind: str, upstream_id: str) -> Event | None:
|
|
128
|
+
return await self._tx.find_event(source, kind, upstream_id)
|
|
129
|
+
|
|
130
|
+
async def latest_event(
|
|
131
|
+
self,
|
|
132
|
+
kind: str,
|
|
133
|
+
*,
|
|
134
|
+
fields: Mapping[str, str | bool] | None = None,
|
|
135
|
+
after: int = 0,
|
|
136
|
+
) -> Event | None:
|
|
137
|
+
return await self._tx.latest_event(kind, fields=fields, after=after)
|
|
138
|
+
|
|
139
|
+
async def latest_run_report(self, run_id: str) -> Event | None:
|
|
140
|
+
return await self._tx.latest_run_report(run_id)
|
|
141
|
+
|
|
142
|
+
async def lane_activity(self, lane: str) -> Event | None:
|
|
143
|
+
return await self._tx.lane_activity(lane)
|
|
144
|
+
|
|
145
|
+
async def find_claude_turn(self, session: str, aliases: tuple[str, ...]) -> Event | None:
|
|
146
|
+
return await self._tx.find_claude_turn(session, aliases)
|
|
147
|
+
|
|
148
|
+
async def bind_claude_turn(self, session: str, aliases: tuple[str, ...], event: str) -> None:
|
|
149
|
+
await self._tx.bind_claude_turn(session, aliases, event)
|
|
150
|
+
|
|
151
|
+
async def allocate_seq(self) -> int:
|
|
152
|
+
"""The project's next ``seq``; rolled back with the transaction."""
|
|
153
|
+
return await self._tx.allocate_seq()
|
|
154
|
+
|
|
155
|
+
async def save_embedding_batch(
|
|
156
|
+
self,
|
|
157
|
+
index: EmbeddingIndex,
|
|
158
|
+
expected: int,
|
|
159
|
+
through: int,
|
|
160
|
+
records: tuple[IndexedEmbedding, ...],
|
|
161
|
+
) -> bool:
|
|
162
|
+
if self._role != "system":
|
|
163
|
+
raise LedgerError("forbidden", "only core may update the embedding index")
|
|
164
|
+
return await index.commit(self._tx, expected, through, records)
|
|
165
|
+
|
|
166
|
+
async def save_search_backfill(
|
|
167
|
+
self,
|
|
168
|
+
index: SearchBackfill,
|
|
169
|
+
source: BackfillSource,
|
|
170
|
+
expected: BackfillPosition,
|
|
171
|
+
page: BackfillPage,
|
|
172
|
+
) -> bool:
|
|
173
|
+
if self._role != "system":
|
|
174
|
+
raise LedgerError("forbidden", "only core may update search backfill")
|
|
175
|
+
return await index.commit_backfill(self._tx, source, expected, page)
|
|
176
|
+
|
|
177
|
+
async def ready_work_items(self) -> tuple[WorkItem, ...]:
|
|
178
|
+
return await self._tx.ready_work_items()
|
|
179
|
+
|
|
180
|
+
async def replace_work_items(self, items: tuple[WorkItem, ...]) -> None:
|
|
181
|
+
await self._tx.replace_work_items(items)
|
|
182
|
+
|
|
183
|
+
async def claimed_work_items(self) -> set[str]:
|
|
184
|
+
return await self._tx.claimed_work_items()
|
|
185
|
+
|
|
186
|
+
async def find_lane(self, lane_id: str) -> Lane | None:
|
|
187
|
+
return await self._tx.find_lane(lane_id)
|
|
188
|
+
|
|
189
|
+
async def find_budget(self, run: str) -> Budget | None:
|
|
190
|
+
return await self._tx.find_budget(run)
|
|
191
|
+
|
|
192
|
+
async def insert_budget(self, budget: Budget) -> None:
|
|
193
|
+
await self._tx.insert_budget(budget)
|
|
194
|
+
|
|
195
|
+
async def reserve_openrouter_key(self, reservation: str, run: str) -> bool:
|
|
196
|
+
return await self._tx.reserve_openrouter_key(reservation, run)
|
|
197
|
+
|
|
198
|
+
async def update_budget_spend(self, budget: Budget) -> None:
|
|
199
|
+
await self._tx.update_budget_spend(budget)
|
|
200
|
+
|
|
201
|
+
async def has_routes(self) -> bool:
|
|
202
|
+
return await self._tx.has_routes()
|
|
203
|
+
|
|
204
|
+
async def find_route(self, action_kind: str) -> str | None:
|
|
205
|
+
return await self._tx.find_route(action_kind)
|
|
206
|
+
|
|
207
|
+
async def save_route(self, action_kind: str, lane: str) -> None:
|
|
208
|
+
await self._tx.save_route(action_kind, lane)
|
|
209
|
+
|
|
210
|
+
async def worker_lane(self, worker: str) -> Lane | None:
|
|
211
|
+
return await self._tx.worker_lane(worker)
|
|
212
|
+
|
|
213
|
+
async def branch_lane(self, branch: str) -> Lane | None:
|
|
214
|
+
return await self._tx.branch_lane(branch)
|
|
215
|
+
|
|
216
|
+
async def branch_lanes(self, branch: str) -> list[Lane]:
|
|
217
|
+
return await self._tx.branch_lanes(branch)
|
|
218
|
+
|
|
219
|
+
async def active_lanes(self) -> list[Lane]:
|
|
220
|
+
return await self._tx.active_lanes()
|
|
221
|
+
|
|
222
|
+
async def pull_request(self, repository: str, number: int) -> PullRequest | None:
|
|
223
|
+
return await self._tx.pull_request(repository, number)
|
|
224
|
+
|
|
225
|
+
async def open_branch_pull(self, repository: str, branch: str) -> PullRequest | None:
|
|
226
|
+
return await self._tx.open_branch_pull(repository, branch)
|
|
227
|
+
|
|
228
|
+
async def deploy_freeze(self) -> str | None:
|
|
229
|
+
return await self._tx.deploy_freeze()
|
|
230
|
+
|
|
231
|
+
async def set_deploy_freeze(self, reason: str | None) -> None:
|
|
232
|
+
await self._tx.set_deploy_freeze(reason)
|
|
233
|
+
self.all_merge_pulls = True
|
|
234
|
+
|
|
235
|
+
async def open_pulls(self, repository: str) -> tuple[PullRequest, ...]:
|
|
236
|
+
return await self._tx.open_pulls(repository)
|
|
237
|
+
|
|
238
|
+
async def detail_capacity_exceeded(self, repository: str) -> bool:
|
|
239
|
+
return await self._tx.detail_capacity_exceeded(repository)
|
|
240
|
+
|
|
241
|
+
async def lane_claims(self, lane: str | None = None) -> list[Claim]:
|
|
242
|
+
return await self._tx.lane_claims(lane)
|
|
243
|
+
|
|
244
|
+
async def save_claims(self, claims: list[Claim]) -> None:
|
|
245
|
+
await self._tx.save_claims(claims)
|
|
246
|
+
self._claims_changed = True
|
|
247
|
+
|
|
248
|
+
async def delete_claims(self, lane: str, globs: list[str] | None = None) -> None:
|
|
249
|
+
await self._tx.delete_claims(lane, globs)
|
|
250
|
+
self._claims_changed = True
|
|
251
|
+
|
|
252
|
+
def pull_overlaps_changed(self) -> None:
|
|
253
|
+
self._pulls_changed = True
|
|
254
|
+
|
|
255
|
+
async def refresh_overlaps(self, repository: str | None, tree_path: str | None) -> None:
|
|
256
|
+
if not self._claims_changed and not (self._pulls_changed and repository is not None):
|
|
257
|
+
return
|
|
258
|
+
claims = await self.lane_claims()
|
|
259
|
+
if self._claims_changed:
|
|
260
|
+
await reconcile_claim_pairs(self, claims)
|
|
261
|
+
if repository is not None:
|
|
262
|
+
await reconcile_pull_overlaps(self, repository, tree_path, claims)
|
|
263
|
+
|
|
264
|
+
async def latest_merge_verdict(self, repository: str, number: int) -> Event | None:
|
|
265
|
+
return await self._tx.latest_merge_verdict(repository, number)
|
|
266
|
+
|
|
267
|
+
async def save_merge_verdict(self, repository: str, number: int, seq: int) -> None:
|
|
268
|
+
await self._tx.save_merge_verdict(repository, number, seq)
|
|
269
|
+
|
|
270
|
+
async def save_pull_request(
|
|
271
|
+
self, pull: PullRequest, *, historical: bool = False, merge_sha_only: bool = False
|
|
272
|
+
) -> None:
|
|
273
|
+
if pull.complete and pull.state == "merged" and not historical:
|
|
274
|
+
prior = await self._tx.pull_request(pull.repository, pull.number)
|
|
275
|
+
transition = prior is None or prior.state != "merged"
|
|
276
|
+
if transition or (
|
|
277
|
+
prior is not None and prior.merge_commit_sha != pull.merge_commit_sha
|
|
278
|
+
):
|
|
279
|
+
self.merge_observations[pull.number] = (pull, transition)
|
|
280
|
+
await self._tx.save_pull_request(pull)
|
|
281
|
+
if not merge_sha_only:
|
|
282
|
+
self.merge_pull_numbers.add(pull.number)
|
|
283
|
+
self.all_merge_pulls = True # branch membership may change the first PR on a lane
|
|
284
|
+
|
|
285
|
+
async def github_resource(
|
|
286
|
+
self, repository: str, resource: str, scope: str = ""
|
|
287
|
+
) -> GitHubResource | None:
|
|
288
|
+
return await self._tx.github_resource(repository, resource, scope)
|
|
289
|
+
|
|
290
|
+
async def save_github_resource(
|
|
291
|
+
self, repository: str, resource: str, scope: str, result: GitHubResource
|
|
292
|
+
) -> None:
|
|
293
|
+
await self._tx.save_github_resource(repository, resource, scope, result)
|
|
294
|
+
if self._search is not None and result.complete:
|
|
295
|
+
if resource == COMMENTS:
|
|
296
|
+
for comment in result.records:
|
|
297
|
+
if comment.key == scope and comment.kind == "issue_comment":
|
|
298
|
+
await self.index_search_record(comment_record(repository, comment))
|
|
299
|
+
elif resource == PULL_TEXT:
|
|
300
|
+
pull = await self._tx.pull_request(repository, int(scope))
|
|
301
|
+
if pull is not None and (record := pull_text_record(pull, result)) is not None:
|
|
302
|
+
# Accepted list observations own the live text, including same-version edits.
|
|
303
|
+
await self.index_search_record(record, only_if_absent=True)
|
|
304
|
+
if resource in (MAIN, MAIN_CI):
|
|
305
|
+
self.all_merge_pulls = True
|
|
306
|
+
elif resource in (CHECKS, COMMIT_STATUSES):
|
|
307
|
+
self.merge_check_heads.add(scope)
|
|
308
|
+
elif resource in (LABELS, THREADS, PULL_COMPARISON, PULL_TEXT, KODUS_REVIEW):
|
|
309
|
+
self.merge_pull_numbers.add(int(scope))
|
|
310
|
+
if resource in {ISSUES, COMMENTS, ASK_COMMENT_HISTORY}:
|
|
311
|
+
self._ask_repositories.add(repository)
|
|
312
|
+
|
|
313
|
+
async def index_search_record(
|
|
314
|
+
self, record: SearchRecord, *, only_if_absent: bool = False
|
|
315
|
+
) -> None:
|
|
316
|
+
if self._search is not None:
|
|
317
|
+
await self._search.index_record(self._tx, record, only_if_absent=only_if_absent)
|
|
318
|
+
|
|
319
|
+
async def index_pull_body(self, repository: str, number: int, body: str) -> None:
|
|
320
|
+
await self.index_search_record(pull_body_record(repository, number, body))
|
|
321
|
+
|
|
322
|
+
async def fetch_cursor(self, repository: str, resource: str) -> FetchCursor | None:
|
|
323
|
+
return await self._tx.fetch_cursor(repository, resource)
|
|
324
|
+
|
|
325
|
+
async def save_fetch_cursor(self, repository: str, resource: str, cursor: FetchCursor) -> None:
|
|
326
|
+
await self._tx.save_fetch_cursor(repository, resource, cursor)
|
|
327
|
+
if resource == PULL_REQUESTS:
|
|
328
|
+
self.all_merge_pulls = True
|
|
329
|
+
|
|
330
|
+
async def ask_for_comment(self, repository: str, comment_id: int) -> Ask | None:
|
|
331
|
+
return await self._tx.ask_for_comment(repository, comment_id)
|
|
332
|
+
|
|
333
|
+
async def find_ask(self, ask_id: str) -> Ask | None:
|
|
334
|
+
return await self._tx.find_ask(ask_id)
|
|
335
|
+
|
|
336
|
+
async def goals_use_blockers(self) -> bool:
|
|
337
|
+
return await self._tx.goals_use_blockers()
|
|
338
|
+
|
|
339
|
+
async def save_goal(self, goal: StoredGoal) -> None:
|
|
340
|
+
await self._tx.save_goal(goal)
|
|
341
|
+
|
|
342
|
+
async def save_ask(self, ask: Ask) -> None:
|
|
343
|
+
await self._tx.save_ask(ask)
|
|
344
|
+
await self.index_search_record(ask_record(ask))
|
|
345
|
+
self._ask_repositories.add(ask.repository)
|
|
346
|
+
|
|
347
|
+
async def unsettled_asks(self, repository: str) -> list[Ask]:
|
|
348
|
+
return await self._tx.unsettled_asks(repository)
|
|
349
|
+
|
|
350
|
+
async def ask_comments(
|
|
351
|
+
self, repository: str, issues: set[int]
|
|
352
|
+
) -> dict[int, list[IssueComment]]:
|
|
353
|
+
return await self._tx.ask_comments(repository, issues)
|
|
354
|
+
|
|
355
|
+
async def ask_comment_histories(
|
|
356
|
+
self, repository: str, issues: set[int]
|
|
357
|
+
) -> dict[int, tuple[str, frozenset[int]]]:
|
|
358
|
+
return await self._tx.ask_comment_histories(repository, issues)
|
|
359
|
+
|
|
360
|
+
async def refresh_asks(
|
|
361
|
+
self, repository: str | None, human_login: str | None, stale_days: int
|
|
362
|
+
) -> None:
|
|
363
|
+
if repository is not None and (self._tick or repository in self._ask_repositories):
|
|
364
|
+
await reconcile_asks(self, repository, human_login, stale_days)
|
|
365
|
+
|
|
366
|
+
async def save_fetch_retry(
|
|
367
|
+
self, repository: str, resource: str, retry: FetchRetry | None
|
|
368
|
+
) -> None:
|
|
369
|
+
await self._tx.save_fetch_retry(repository, resource, retry)
|
|
370
|
+
|
|
371
|
+
async def save_lane(self, lane: Lane) -> None:
|
|
372
|
+
await self._tx.save_lane(lane)
|
|
373
|
+
self.all_merge_pulls = True
|
|
374
|
+
|
|
375
|
+
async def find_checklist(self, lane: str) -> Checklist | None:
|
|
376
|
+
return await self._tx.find_checklist(lane)
|
|
377
|
+
|
|
378
|
+
async def save_checklist(self, checklist: Checklist) -> None:
|
|
379
|
+
await self._tx.save_checklist(checklist)
|
|
380
|
+
|
|
381
|
+
async def set_checklist_step_completion(
|
|
382
|
+
self, lane: str, version: int, step: str, completion_ref: str
|
|
383
|
+
) -> bool:
|
|
384
|
+
return await self._tx.set_checklist_step_completion(lane, version, step, completion_ref)
|
|
385
|
+
|
|
386
|
+
async def enqueue_action(
|
|
387
|
+
self,
|
|
388
|
+
cause: Event,
|
|
389
|
+
kind: str,
|
|
390
|
+
payload: dict[str, Any],
|
|
391
|
+
*,
|
|
392
|
+
discriminator: str = "",
|
|
393
|
+
stable_key: str | None = None,
|
|
394
|
+
) -> str:
|
|
395
|
+
"""Queue an effect in its causing transaction. Reusing a key preserves the first effect."""
|
|
396
|
+
if not kind or ":" in kind:
|
|
397
|
+
raise LedgerError("invalid", "action kind must be nonempty and contain no colon")
|
|
398
|
+
if stable_key is not None:
|
|
399
|
+
budget_stop = (
|
|
400
|
+
kind == SEND
|
|
401
|
+
and cause.source == "core"
|
|
402
|
+
and cause.kind == BUDGET_THRESHOLD
|
|
403
|
+
and cause.fields.get("percent") == 100
|
|
404
|
+
and stable_key == f"{BUDGET_ITEM.name}:{cause.upstream_id}"
|
|
405
|
+
and cause.upstream_id == f"{cause.fields.get('budget_id')}:100"
|
|
406
|
+
and payload.get("lane") == cause.fields.get("lane")
|
|
407
|
+
and payload.get("text") == "stop"
|
|
408
|
+
)
|
|
409
|
+
if self._append_role != "system" or not (
|
|
410
|
+
stable_key.startswith(f"{kind}:") or budget_stop
|
|
411
|
+
):
|
|
412
|
+
raise LedgerError("forbidden", "only core may supply a stable action key")
|
|
413
|
+
key = stable_key or f"{cause.id}:{kind}:{discriminator}"
|
|
414
|
+
await self._tx.insert_action(Action(key, kind, cause.id, payload))
|
|
415
|
+
return key
|
|
416
|
+
|
|
417
|
+
async def bridge_action(
|
|
418
|
+
self, workers: tuple[str, ...], now: str, act_id: str | None = None
|
|
419
|
+
) -> Action | None:
|
|
420
|
+
return await self._tx.bridge_action(workers, now, act_id)
|
|
421
|
+
|
|
422
|
+
async def update_action(self, action: Action) -> None:
|
|
423
|
+
if self._append_role != "system":
|
|
424
|
+
raise LedgerError("forbidden", "only core may update a committed action")
|
|
425
|
+
await self._tx.update_action(action)
|
|
426
|
+
|
|
427
|
+
async def insert_outbox(self, message: OutboxMessage) -> None:
|
|
428
|
+
await self._tx.insert_outbox(message)
|
|
429
|
+
|
|
430
|
+
async def insert_decision(self, decision: Decision) -> None:
|
|
431
|
+
await self._tx.insert_decision(decision)
|
|
432
|
+
await self.index_search_record(decision_record(decision))
|
|
433
|
+
|
|
434
|
+
async def lease_outbox(self, lease_id: str, now: str, expires_at: str) -> OutboxLease | None:
|
|
435
|
+
return await self._tx.lease_outbox(lease_id, now, expires_at)
|
|
436
|
+
|
|
437
|
+
async def complete_outbox(self, lease_id: str, now: str) -> str | None:
|
|
438
|
+
return await self._tx.complete_outbox(lease_id, now)
|
|
439
|
+
|
|
440
|
+
async def retire_stale_outbox(self) -> None:
|
|
441
|
+
await self._tx.retire_stale_outbox(format_time(self.now()))
|
|
442
|
+
|
|
443
|
+
async def find_trigger(self, trigger_id: str) -> Trigger | None:
|
|
444
|
+
return await self._tx.find_trigger(trigger_id)
|
|
445
|
+
|
|
446
|
+
async def open_triggers(self) -> list[Trigger]:
|
|
447
|
+
return await self._tx.open_triggers()
|
|
448
|
+
|
|
449
|
+
async def save_trigger(self, trigger: Trigger) -> None:
|
|
450
|
+
await self._tx.save_trigger(trigger)
|
|
451
|
+
|
|
452
|
+
async def first_matching_pull(self, repository: str, pattern: str) -> int | None:
|
|
453
|
+
return await self._tx.first_matching_pull(repository, pattern)
|
|
454
|
+
|
|
455
|
+
def item_kind(self, kind: str) -> ItemKind:
|
|
456
|
+
if kind not in self._items:
|
|
457
|
+
raise LedgerError("invalid", f"unknown next item kind {kind!r}")
|
|
458
|
+
return self._items[kind]
|
|
459
|
+
|
|
460
|
+
async def find_item(self, item_id: str) -> Item | None:
|
|
461
|
+
return await self._tx.find_item(item_id)
|
|
462
|
+
|
|
463
|
+
async def save_item(self, item: Item) -> None:
|
|
464
|
+
await self._tx.save_item(item)
|
|
465
|
+
|
|
466
|
+
async def raise_item(
|
|
467
|
+
self, kind: str, subject_ref: str, role: str, context: dict[str, Any]
|
|
468
|
+
) -> Item:
|
|
469
|
+
"""Open an occurrence or update the active one when its condition changes."""
|
|
470
|
+
registered = self.item_kind(kind)
|
|
471
|
+
role_family(role)
|
|
472
|
+
key = f"{kind}:{subject_ref}"
|
|
473
|
+
current = await self._tx.active_item(key)
|
|
474
|
+
if current is not None:
|
|
475
|
+
if not registered.state_backed or current.context == context:
|
|
476
|
+
return current
|
|
477
|
+
item = replace(current, state="open", context=context, seq=await self.allocate_seq())
|
|
478
|
+
else:
|
|
479
|
+
seq = await self.allocate_seq()
|
|
480
|
+
item = Item(
|
|
481
|
+
f"item_{seq}",
|
|
482
|
+
kind,
|
|
483
|
+
subject_ref,
|
|
484
|
+
key,
|
|
485
|
+
role,
|
|
486
|
+
registered.rank_class,
|
|
487
|
+
"open",
|
|
488
|
+
seq,
|
|
489
|
+
None,
|
|
490
|
+
seq,
|
|
491
|
+
context,
|
|
492
|
+
registered.suggested_actions,
|
|
493
|
+
)
|
|
494
|
+
await self.save_item(item)
|
|
495
|
+
return item
|
|
496
|
+
|
|
497
|
+
async def active_item(self, kind: str, subject_ref: str) -> Item | None:
|
|
498
|
+
return await self._tx.active_item(f"{kind}:{subject_ref}")
|
|
499
|
+
|
|
500
|
+
async def active_item_subjects(self, kind: str) -> set[str]:
|
|
501
|
+
return await self._tx.active_item_subjects(kind)
|
|
502
|
+
|
|
503
|
+
async def clear_item(self, kind: str, subject_ref: str) -> None:
|
|
504
|
+
if not self.item_kind(kind).state_backed:
|
|
505
|
+
raise LedgerError("invalid", f"{kind!r} is not state-backed")
|
|
506
|
+
if (item := await self._tx.active_item(f"{kind}:{subject_ref}")) is not None:
|
|
507
|
+
seq = await self.allocate_seq()
|
|
508
|
+
await self.save_item(replace(item, state="closed", closed_seq=seq, seq=seq))
|
|
509
|
+
|
|
510
|
+
async def append(self, event: NewEvent) -> AppendResult:
|
|
511
|
+
"""Validate, deduplicate, number and store ``event``.
|
|
512
|
+
|
|
513
|
+
A duplicate of ``(source, kind, upstream_id)`` returns the stored event with
|
|
514
|
+
``created=False`` and touches nothing: no ``seq`` is consumed. The identity is checked
|
|
515
|
+
first, the duplicate lookup next, and the declared fields only for a new event.
|
|
516
|
+
"""
|
|
517
|
+
registered = self._events.validate_identity(self._append_role, event)
|
|
518
|
+
existing = await self._tx.find_event(event.source, event.kind, event.upstream_id)
|
|
519
|
+
if existing is not None:
|
|
520
|
+
return AppendResult(existing, False)
|
|
521
|
+
# One copy is validated and stored: the caller owns event.fields and may change it while
|
|
522
|
+
# this input awaits the store.
|
|
523
|
+
fields = copy.deepcopy(event.fields)
|
|
524
|
+
validate_fields(registered.fields, fields, event.kind)
|
|
525
|
+
stored = Event(
|
|
526
|
+
seq=await self._tx.allocate_seq(),
|
|
527
|
+
id=self._id_factory(),
|
|
528
|
+
source=event.source,
|
|
529
|
+
kind=event.kind,
|
|
530
|
+
upstream_id=event.upstream_id,
|
|
531
|
+
lane_id=event.lane_id,
|
|
532
|
+
fields=fields,
|
|
533
|
+
body=event.body,
|
|
534
|
+
observed_at=format_time(self._clock.now()),
|
|
535
|
+
)
|
|
536
|
+
await self._tx.insert_event(stored)
|
|
537
|
+
if event.kind == "core.tick":
|
|
538
|
+
self._tick = True
|
|
539
|
+
if self._search is not None:
|
|
540
|
+
await self._search.index(self._tx, stored)
|
|
541
|
+
return AppendResult(stored, True)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"""Track live wait requests and derive missing-watcher items on ticks."""
|
|
2
|
+
|
|
3
|
+
from collections import Counter
|
|
4
|
+
from collections.abc import Generator
|
|
5
|
+
from contextlib import contextmanager
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from plotroom.core.unit_of_work import UnitOfWork
|
|
10
|
+
|
|
11
|
+
MISSING_WATCHER = "watcher_not_running"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class Watchers:
|
|
15
|
+
def __init__(self, roles: tuple[str, ...]) -> None:
|
|
16
|
+
self.roles = roles
|
|
17
|
+
self._active: Counter[str] = Counter()
|
|
18
|
+
|
|
19
|
+
@contextmanager
|
|
20
|
+
def waiting(self, role: str) -> Generator[None]:
|
|
21
|
+
self._active[role] += 1
|
|
22
|
+
try:
|
|
23
|
+
yield
|
|
24
|
+
finally:
|
|
25
|
+
self._active[role] -= 1
|
|
26
|
+
if not self._active[role]:
|
|
27
|
+
del self._active[role]
|
|
28
|
+
|
|
29
|
+
async def reconcile(self, uow: UnitOfWork) -> None:
|
|
30
|
+
roles = set(self.roles)
|
|
31
|
+
for role in sorted(roles | await uow.active_item_subjects(MISSING_WATCHER)):
|
|
32
|
+
if role not in roles or self._active[role]:
|
|
33
|
+
await uow.clear_item(MISSING_WATCHER, role)
|
|
34
|
+
else:
|
|
35
|
+
await uow.raise_item(MISSING_WATCHER, role, "orchestrator", {"role": role})
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""The durable ready-work mirror and its idle-worker condition."""
|
|
2
|
+
|
|
3
|
+
from typing import TYPE_CHECKING
|
|
4
|
+
|
|
5
|
+
if TYPE_CHECKING:
|
|
6
|
+
from plotroom.core.unit_of_work import UnitOfWork
|
|
7
|
+
|
|
8
|
+
IDLE_WORKER = "idle_worker"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def refresh_idle_workers(uow: UnitOfWork, workers: frozenset[str]) -> None:
|
|
12
|
+
claimed = await uow.claimed_work_items()
|
|
13
|
+
eligible = [item.ref for item in await uow.ready_work_items() if item.ref not in claimed]
|
|
14
|
+
for worker in sorted(workers | await uow.active_item_subjects(IDLE_WORKER)):
|
|
15
|
+
if worker in workers and eligible and await uow.worker_lane(worker) is None:
|
|
16
|
+
await uow.raise_item(
|
|
17
|
+
IDLE_WORKER,
|
|
18
|
+
worker,
|
|
19
|
+
"orchestrator",
|
|
20
|
+
{"worker": worker, "work_items": eligible},
|
|
21
|
+
)
|
|
22
|
+
else:
|
|
23
|
+
await uow.clear_item(IDLE_WORKER, worker)
|