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,931 @@
|
|
|
1
|
+
"""The MCP tool surface on stateless streamable HTTP (AD-7)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import Awaitable, Callable, Mapping, Sequence
|
|
8
|
+
from dataclasses import asdict, dataclass
|
|
9
|
+
from typing import Annotated, Any, Literal
|
|
10
|
+
|
|
11
|
+
from mcp.server.mcpserver import Context, MCPServer
|
|
12
|
+
from mcp.server.transport_security import TransportSecuritySettings
|
|
13
|
+
from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt
|
|
14
|
+
from starlette.applications import Starlette
|
|
15
|
+
from starlette.datastructures import Headers
|
|
16
|
+
from starlette.requests import Request
|
|
17
|
+
from starlette.responses import JSONResponse, Response
|
|
18
|
+
from starlette.routing import Route
|
|
19
|
+
from starlette.types import ASGIApp, Receive, Scope, Send
|
|
20
|
+
|
|
21
|
+
from plotroom.core.clerk_import import PullImportRow, SkipImportRow, TriggerImportRow
|
|
22
|
+
from plotroom.core.envelope import err, ok
|
|
23
|
+
from plotroom.core.events import LedgerError
|
|
24
|
+
from plotroom.core.intake import record_claude
|
|
25
|
+
from plotroom.core.ledger import Ledger
|
|
26
|
+
from plotroom.core.outbox import lease_request_id
|
|
27
|
+
from plotroom.core.roles import Identity
|
|
28
|
+
from plotroom.core.snapshot import (
|
|
29
|
+
DEFAULT_SNAPSHOT_BUDGET,
|
|
30
|
+
render_claims,
|
|
31
|
+
render_next,
|
|
32
|
+
render_search,
|
|
33
|
+
render_snapshot,
|
|
34
|
+
)
|
|
35
|
+
from plotroom.ports.search import DEFAULT_SEARCH_LIMIT
|
|
36
|
+
from plotroom.ports.session_reader import SessionEvent
|
|
37
|
+
from plotroom.ports.work_items import WorkItemSource
|
|
38
|
+
|
|
39
|
+
_NEXT_PAGE_SIZE = 64
|
|
40
|
+
|
|
41
|
+
type Authenticate = Callable[[str], Awaitable[Identity | None]]
|
|
42
|
+
type Backup = Callable[[str], Awaitable[str]]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ReadyChecklistEntry(BaseModel):
|
|
46
|
+
model_config = ConfigDict(extra="forbid", strict=True)
|
|
47
|
+
|
|
48
|
+
step: str
|
|
49
|
+
owner: str
|
|
50
|
+
ref: str
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class GitHubDecisionCitation(BaseModel):
|
|
54
|
+
model_config = ConfigDict(extra="forbid", strict=True)
|
|
55
|
+
|
|
56
|
+
kind: Literal["github_comment"]
|
|
57
|
+
repository: str
|
|
58
|
+
issue: int
|
|
59
|
+
comment_id: int
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class ChatDecisionCitation(BaseModel):
|
|
63
|
+
model_config = ConfigDict(extra="forbid", strict=True)
|
|
64
|
+
|
|
65
|
+
kind: Literal["chat"]
|
|
66
|
+
ref: str
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
DecisionCitation = Annotated[
|
|
70
|
+
GitHubDecisionCitation | ChatDecisionCitation, Field(discriminator="kind")
|
|
71
|
+
]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
@dataclass(frozen=True)
|
|
75
|
+
class ProjectRequest:
|
|
76
|
+
identity: Identity
|
|
77
|
+
ledger: Ledger | None
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _allowed_host(host: str, patterns: Sequence[str]) -> bool:
|
|
81
|
+
if host in patterns:
|
|
82
|
+
return bool(host)
|
|
83
|
+
for pattern in patterns:
|
|
84
|
+
if pattern.endswith(":*") and host.startswith(pattern[:-1]):
|
|
85
|
+
port = host[len(pattern) - 1 :]
|
|
86
|
+
if port.isascii() and port.isdecimal() and len(port) <= 5 and 0 < int(port) <= 65535:
|
|
87
|
+
return True
|
|
88
|
+
return False
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class _AccessMiddleware:
|
|
92
|
+
"""Check the request's address and target project before the MCP SDK sees its body."""
|
|
93
|
+
|
|
94
|
+
def __init__(
|
|
95
|
+
self,
|
|
96
|
+
app: ASGIApp,
|
|
97
|
+
*,
|
|
98
|
+
projects: frozenset[str],
|
|
99
|
+
ledgers: Mapping[str, Ledger],
|
|
100
|
+
allowed_hosts: Sequence[str],
|
|
101
|
+
authenticate: Authenticate,
|
|
102
|
+
) -> None:
|
|
103
|
+
self._app = app
|
|
104
|
+
self._projects = projects
|
|
105
|
+
self._ledgers = ledgers
|
|
106
|
+
self._allowed_hosts = allowed_hosts
|
|
107
|
+
self._authenticate = authenticate
|
|
108
|
+
|
|
109
|
+
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
|
110
|
+
if scope["type"] == "http":
|
|
111
|
+
headers = Headers(scope=scope)
|
|
112
|
+
hosts = headers.getlist("host")
|
|
113
|
+
if len(hosts) != 1 or not _allowed_host(hosts[0], self._allowed_hosts):
|
|
114
|
+
await Response(status_code=421)(scope, receive, send)
|
|
115
|
+
return
|
|
116
|
+
path = scope["path"]
|
|
117
|
+
if scope["method"] in ("GET", "HEAD") and (
|
|
118
|
+
path in ("/healthz", "/readyz")
|
|
119
|
+
or (path.startswith("/readyz/") and "/" not in path[len("/readyz/") :])
|
|
120
|
+
):
|
|
121
|
+
await self._app(scope, receive, send)
|
|
122
|
+
return
|
|
123
|
+
authorization = headers.getlist("authorization")
|
|
124
|
+
identity = None
|
|
125
|
+
if len(authorization) == 1:
|
|
126
|
+
scheme, _, bearer = authorization[0].partition(" ")
|
|
127
|
+
if scheme.lower() == "bearer" and bearer:
|
|
128
|
+
identity = await self._authenticate(bearer.lstrip(" "))
|
|
129
|
+
if identity is None or identity.project_id not in self._projects:
|
|
130
|
+
await Response(status_code=401)(scope, receive, send)
|
|
131
|
+
return
|
|
132
|
+
scope.setdefault("state", {})["plotroom"] = ProjectRequest(
|
|
133
|
+
identity, self._ledgers.get(identity.project_id)
|
|
134
|
+
)
|
|
135
|
+
await self._app(scope, receive, send)
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _project_request(ctx: Context[Any, Request]) -> ProjectRequest:
|
|
139
|
+
request = ctx.request_context.request
|
|
140
|
+
if request is None:
|
|
141
|
+
raise RuntimeError("MCP tool called without an HTTP request")
|
|
142
|
+
caller: ProjectRequest = request.state.plotroom
|
|
143
|
+
return caller
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def _private_local_request(ctx: Context[Any, Request]) -> bool:
|
|
147
|
+
request = ctx.request_context.request
|
|
148
|
+
if request is None:
|
|
149
|
+
return False
|
|
150
|
+
# Uvicorn supplies the Unix socket path and no port for the private listener.
|
|
151
|
+
address = request.scope.get("server")
|
|
152
|
+
return address is not None and address[1] is None and address[0].startswith("/")
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
async def execute_command(
|
|
156
|
+
ctx: Context[Any, Request], command: str, request_id: str, args: dict[str, Any]
|
|
157
|
+
) -> dict[str, Any]:
|
|
158
|
+
"""Run a tool's command as the authenticated role on the authenticated project's ledger."""
|
|
159
|
+
caller = _project_request(ctx)
|
|
160
|
+
if caller.ledger is None:
|
|
161
|
+
return err("unavailable", "the project ledger is unavailable")
|
|
162
|
+
try:
|
|
163
|
+
result = await caller.ledger.execute(caller.identity.role, command, request_id, args)
|
|
164
|
+
except LedgerError as exc:
|
|
165
|
+
return err(exc.code, exc.message)
|
|
166
|
+
return ok(result.result)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def build_app(
|
|
170
|
+
project_ids: tuple[str, ...],
|
|
171
|
+
allowed_hosts: Sequence[str],
|
|
172
|
+
*,
|
|
173
|
+
ledgers: Mapping[str, Ledger],
|
|
174
|
+
authenticate: Authenticate,
|
|
175
|
+
backup: Backup | None = None,
|
|
176
|
+
bridge_enabled: bool = False,
|
|
177
|
+
work_item_sources: Mapping[str, WorkItemSource] | None = None,
|
|
178
|
+
unavailable: str | None = None,
|
|
179
|
+
snapshot_budget: int = DEFAULT_SNAPSHOT_BUDGET,
|
|
180
|
+
next_budget: int = DEFAULT_SNAPSHOT_BUDGET,
|
|
181
|
+
search_budget: int = DEFAULT_SNAPSHOT_BUDGET,
|
|
182
|
+
) -> Starlette:
|
|
183
|
+
"""The ASGI app serving the tool set at ``/mcp``.
|
|
184
|
+
|
|
185
|
+
``unavailable`` is set when the server is up but cannot honor calls (SQLite below the floor);
|
|
186
|
+
every tool then answers ``unavailable``.
|
|
187
|
+
"""
|
|
188
|
+
server = MCPServer("plotroom")
|
|
189
|
+
|
|
190
|
+
def board_access(ctx: Context[Any, Request]) -> ProjectRequest:
|
|
191
|
+
caller = _project_request(ctx)
|
|
192
|
+
if unavailable is not None:
|
|
193
|
+
raise LedgerError("unavailable", unavailable)
|
|
194
|
+
if caller.identity.role == "drain":
|
|
195
|
+
raise LedgerError("forbidden", "the drain role may only use wait and outbox tools")
|
|
196
|
+
return caller
|
|
197
|
+
|
|
198
|
+
@server.tool()
|
|
199
|
+
async def snapshot( # pyright: ignore[reportUnusedFunction]
|
|
200
|
+
ctx: Context[Any, Request],
|
|
201
|
+
lane_offset: int = 0,
|
|
202
|
+
lane: str | None = None,
|
|
203
|
+
) -> dict[str, Any]:
|
|
204
|
+
"""Summarize the board, page through current lanes, or read a named lane in full."""
|
|
205
|
+
try:
|
|
206
|
+
caller = board_access(ctx)
|
|
207
|
+
if lane_offset < 0:
|
|
208
|
+
return err("invalid", "lane_offset must be nonnegative")
|
|
209
|
+
if caller.ledger is None:
|
|
210
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
211
|
+
(
|
|
212
|
+
(cursor, items, lanes, details),
|
|
213
|
+
github,
|
|
214
|
+
trigger_counts,
|
|
215
|
+
claim_count,
|
|
216
|
+
freeze_reason,
|
|
217
|
+
github_error,
|
|
218
|
+
) = await caller.ledger.snapshot_observation(caller.identity.role, checklist_lane=lane)
|
|
219
|
+
if lane is not None:
|
|
220
|
+
for detail in details:
|
|
221
|
+
if detail.id == lane:
|
|
222
|
+
return ok(detail.render())
|
|
223
|
+
return err("not_found", "current lane not found")
|
|
224
|
+
return ok(
|
|
225
|
+
render_snapshot(
|
|
226
|
+
caller.identity.project_id,
|
|
227
|
+
cursor,
|
|
228
|
+
items,
|
|
229
|
+
lanes,
|
|
230
|
+
snapshot_budget,
|
|
231
|
+
lane_details=details,
|
|
232
|
+
lane_offset=lane_offset,
|
|
233
|
+
github=github,
|
|
234
|
+
github_error=github_error,
|
|
235
|
+
claim_count=claim_count,
|
|
236
|
+
trigger_counts=trigger_counts,
|
|
237
|
+
freeze_reason=freeze_reason,
|
|
238
|
+
)
|
|
239
|
+
)
|
|
240
|
+
except LedgerError as exc:
|
|
241
|
+
return err(exc.code, exc.message)
|
|
242
|
+
|
|
243
|
+
async def next_items(
|
|
244
|
+
ctx: Context[Any, Request],
|
|
245
|
+
offset: int = 0,
|
|
246
|
+
item: str | None = None,
|
|
247
|
+
) -> dict[str, Any]:
|
|
248
|
+
"""Page through visible next items in rank order, or read one open item in full."""
|
|
249
|
+
try:
|
|
250
|
+
caller = board_access(ctx)
|
|
251
|
+
if not 0 <= offset <= 2**63 - 1:
|
|
252
|
+
return err("invalid", "offset must be a nonnegative SQLite integer")
|
|
253
|
+
if caller.ledger is None:
|
|
254
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
255
|
+
role = caller.identity.role
|
|
256
|
+
if item is not None:
|
|
257
|
+
entry = await caller.ledger.read_open_item(role, item)
|
|
258
|
+
if entry is None:
|
|
259
|
+
return err("not_found", "open next item not found")
|
|
260
|
+
return ok(asdict(entry))
|
|
261
|
+
cursor, items = await caller.ledger.read_next(role, offset, _NEXT_PAGE_SIZE + 1)
|
|
262
|
+
return ok(
|
|
263
|
+
render_next(
|
|
264
|
+
cursor,
|
|
265
|
+
items[:_NEXT_PAGE_SIZE],
|
|
266
|
+
offset,
|
|
267
|
+
next_budget,
|
|
268
|
+
has_more=len(items) > _NEXT_PAGE_SIZE,
|
|
269
|
+
)
|
|
270
|
+
)
|
|
271
|
+
except LedgerError as exc:
|
|
272
|
+
return err(exc.code, exc.message)
|
|
273
|
+
|
|
274
|
+
server.tool(name="next")(next_items)
|
|
275
|
+
|
|
276
|
+
@server.tool()
|
|
277
|
+
async def search( # pyright: ignore[reportUnusedFunction]
|
|
278
|
+
ctx: Context[Any, Request],
|
|
279
|
+
text: str,
|
|
280
|
+
kinds: list[str] | None = None,
|
|
281
|
+
limit: int = DEFAULT_SEARCH_LIMIT,
|
|
282
|
+
) -> dict[str, Any]:
|
|
283
|
+
"""Find a word or phrase in the current project's visible committed records."""
|
|
284
|
+
try:
|
|
285
|
+
caller = board_access(ctx)
|
|
286
|
+
if caller.ledger is None:
|
|
287
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
288
|
+
page = await caller.ledger.search(
|
|
289
|
+
text, None if kinds is None else tuple(kinds), limit, role=caller.identity.role
|
|
290
|
+
)
|
|
291
|
+
return ok(render_search(page, search_budget))
|
|
292
|
+
except LedgerError as exc:
|
|
293
|
+
return err(exc.code, exc.message)
|
|
294
|
+
|
|
295
|
+
@server.tool()
|
|
296
|
+
async def brief( # pyright: ignore[reportUnusedFunction]
|
|
297
|
+
ctx: Context[Any, Request], work_item: str, worker: str
|
|
298
|
+
) -> dict[str, Any]:
|
|
299
|
+
"""Read a proposed lane brief filled from the ticket and current path claims."""
|
|
300
|
+
try:
|
|
301
|
+
caller = board_access(ctx)
|
|
302
|
+
if caller.ledger is None:
|
|
303
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
304
|
+
source = (
|
|
305
|
+
None
|
|
306
|
+
if work_item_sources is None
|
|
307
|
+
else work_item_sources.get(caller.identity.project_id)
|
|
308
|
+
)
|
|
309
|
+
text = await caller.ledger.brief(caller.identity.role, work_item, worker, source)
|
|
310
|
+
return ok({"brief": text})
|
|
311
|
+
except LedgerError as exc:
|
|
312
|
+
return err(exc.code, exc.message)
|
|
313
|
+
|
|
314
|
+
@server.tool()
|
|
315
|
+
async def merge_gate( # pyright: ignore[reportUnusedFunction]
|
|
316
|
+
ctx: Context[Any, Request],
|
|
317
|
+
pr: int,
|
|
318
|
+
expected_head_sha: str | None = None,
|
|
319
|
+
expected_main_sha: str | None = None,
|
|
320
|
+
) -> dict[str, Any]:
|
|
321
|
+
"""Read the current merge readiness, checked head, and observation watermark for a PR."""
|
|
322
|
+
try:
|
|
323
|
+
caller = board_access(ctx)
|
|
324
|
+
if caller.ledger is None:
|
|
325
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
326
|
+
verdict = await caller.ledger.merge_gate(
|
|
327
|
+
caller.identity.role,
|
|
328
|
+
pr,
|
|
329
|
+
expected_head_sha=expected_head_sha,
|
|
330
|
+
expected_main_sha=expected_main_sha,
|
|
331
|
+
)
|
|
332
|
+
return ok(asdict(verdict))
|
|
333
|
+
except LedgerError as exc:
|
|
334
|
+
return err(exc.code, exc.message)
|
|
335
|
+
|
|
336
|
+
@server.tool()
|
|
337
|
+
async def claims( # pyright: ignore[reportUnusedFunction]
|
|
338
|
+
ctx: Context[Any, Request],
|
|
339
|
+
lane: str | None = None,
|
|
340
|
+
offset: int = 0,
|
|
341
|
+
detail: bool = False,
|
|
342
|
+
) -> dict[str, Any]:
|
|
343
|
+
"""Page through visible claims. Use detail with a row's offset for its full reason."""
|
|
344
|
+
try:
|
|
345
|
+
caller = board_access(ctx)
|
|
346
|
+
if not 0 <= offset <= 2**63 - 1:
|
|
347
|
+
return err("invalid", "offset must be a nonnegative SQLite integer")
|
|
348
|
+
if caller.ledger is None:
|
|
349
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
350
|
+
cursor, entries = await caller.ledger.read_claims(
|
|
351
|
+
caller.identity.role, lane, offset, 1 if detail else _NEXT_PAGE_SIZE + 1
|
|
352
|
+
)
|
|
353
|
+
if detail:
|
|
354
|
+
return ok({"cursor": cursor, "claims": [entry.render() for entry in entries]})
|
|
355
|
+
return ok(
|
|
356
|
+
render_claims(
|
|
357
|
+
cursor,
|
|
358
|
+
entries[:_NEXT_PAGE_SIZE],
|
|
359
|
+
offset,
|
|
360
|
+
next_budget,
|
|
361
|
+
has_more=len(entries) > _NEXT_PAGE_SIZE,
|
|
362
|
+
)
|
|
363
|
+
)
|
|
364
|
+
except LedgerError as exc:
|
|
365
|
+
return err(exc.code, exc.message)
|
|
366
|
+
|
|
367
|
+
@server.tool()
|
|
368
|
+
async def overlaps( # pyright: ignore[reportUnusedFunction]
|
|
369
|
+
ctx: Context[Any, Request],
|
|
370
|
+
pr: int | None = None,
|
|
371
|
+
lane: str | None = None,
|
|
372
|
+
offset: int = 0,
|
|
373
|
+
) -> dict[str, Any]:
|
|
374
|
+
"""Page through visible overlaps for one PR or lane. Use next(item=...) for full detail."""
|
|
375
|
+
try:
|
|
376
|
+
caller = board_access(ctx)
|
|
377
|
+
if (lane is None) == (pr is None):
|
|
378
|
+
return err("invalid", "provide exactly one of pr or lane")
|
|
379
|
+
if pr is not None and not 0 < pr <= 2**63 - 1:
|
|
380
|
+
return err("invalid", "pr must be a positive SQLite integer")
|
|
381
|
+
if not 0 <= offset <= 2**63 - 1:
|
|
382
|
+
return err("invalid", "offset must be a nonnegative SQLite integer")
|
|
383
|
+
if caller.ledger is None:
|
|
384
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
385
|
+
cursor, items = await caller.ledger.read_overlaps(
|
|
386
|
+
caller.identity.role, lane, pr, offset, _NEXT_PAGE_SIZE + 1
|
|
387
|
+
)
|
|
388
|
+
return ok(
|
|
389
|
+
render_next(
|
|
390
|
+
cursor,
|
|
391
|
+
items[:_NEXT_PAGE_SIZE],
|
|
392
|
+
offset,
|
|
393
|
+
next_budget,
|
|
394
|
+
has_more=len(items) > _NEXT_PAGE_SIZE,
|
|
395
|
+
)
|
|
396
|
+
)
|
|
397
|
+
except LedgerError as exc:
|
|
398
|
+
return err(exc.code, exc.message)
|
|
399
|
+
|
|
400
|
+
@server.tool()
|
|
401
|
+
async def ingest( # pyright: ignore[reportUnusedFunction]
|
|
402
|
+
ctx: Context[Any, Request],
|
|
403
|
+
kind: str,
|
|
404
|
+
session_id: str,
|
|
405
|
+
body: str,
|
|
406
|
+
lane: str | None = None,
|
|
407
|
+
upstream_id: str | None = None,
|
|
408
|
+
turn_index: int | None = None,
|
|
409
|
+
) -> dict[str, Any]:
|
|
410
|
+
"""Report a Claude turn or session message from an authenticated project session."""
|
|
411
|
+
caller = _project_request(ctx)
|
|
412
|
+
if caller.ledger is None:
|
|
413
|
+
return err("unavailable", unavailable or "the project ledger is unavailable")
|
|
414
|
+
report = SessionEvent(kind, session_id, body, upstream_id, turn_index)
|
|
415
|
+
session_worker = caller.ledger.session_workers.get(session_id)
|
|
416
|
+
try:
|
|
417
|
+
result = await caller.ledger.run(
|
|
418
|
+
caller.identity.role,
|
|
419
|
+
lambda uow: record_claude(uow, report, lane=lane, session_worker=session_worker),
|
|
420
|
+
)
|
|
421
|
+
except LedgerError as exc:
|
|
422
|
+
return err(exc.code, exc.message)
|
|
423
|
+
return ok({"event": result.event.id, "created": result.created})
|
|
424
|
+
|
|
425
|
+
@server.tool()
|
|
426
|
+
async def ack( # pyright: ignore[reportUnusedFunction]
|
|
427
|
+
ctx: Context[Any, Request],
|
|
428
|
+
item: str,
|
|
429
|
+
request_id: str,
|
|
430
|
+
outcome: str | None = None,
|
|
431
|
+
) -> dict[str, Any]:
|
|
432
|
+
"""Acknowledge a next item."""
|
|
433
|
+
args: dict[str, Any] = {"item": item}
|
|
434
|
+
if outcome is not None:
|
|
435
|
+
args["outcome"] = outcome
|
|
436
|
+
if unavailable is not None:
|
|
437
|
+
return err("unavailable", unavailable)
|
|
438
|
+
return await execute_command(ctx, "ack", request_id, args)
|
|
439
|
+
|
|
440
|
+
@server.tool()
|
|
441
|
+
async def wait( # pyright: ignore[reportUnusedFunction]
|
|
442
|
+
ctx: Context[Any, Request],
|
|
443
|
+
cursor: int | None = None,
|
|
444
|
+
timeout: float | None = None, # noqa: ASYNC109 - MCP tool argument
|
|
445
|
+
) -> dict[str, Any]:
|
|
446
|
+
"""Wait for the next visible board change, or return a cursor on timeout."""
|
|
447
|
+
caller = _project_request(ctx)
|
|
448
|
+
if caller.ledger is None:
|
|
449
|
+
return err("unavailable", unavailable or "the project ledger is unavailable")
|
|
450
|
+
|
|
451
|
+
async def progress(elapsed: float, total: float) -> None:
|
|
452
|
+
await ctx.report_progress(elapsed, total, "Waiting for the next item")
|
|
453
|
+
|
|
454
|
+
try:
|
|
455
|
+
position, item = await caller.ledger.wait_for_item(
|
|
456
|
+
caller.identity.role, cursor, timeout, progress
|
|
457
|
+
)
|
|
458
|
+
except LedgerError as exc:
|
|
459
|
+
return err(exc.code, exc.message)
|
|
460
|
+
return ok({"cursor": position, "item": item})
|
|
461
|
+
|
|
462
|
+
@server.tool()
|
|
463
|
+
async def lane_start( # pyright: ignore[reportUnusedFunction]
|
|
464
|
+
ctx: Context[Any, Request],
|
|
465
|
+
lane: str,
|
|
466
|
+
worker: str,
|
|
467
|
+
branch: str,
|
|
468
|
+
work_items: list[str],
|
|
469
|
+
request_id: str,
|
|
470
|
+
branch_pattern: str | None = None,
|
|
471
|
+
) -> dict[str, Any]:
|
|
472
|
+
"""Start a lane with an optional case-sensitive branch glob for merge checks."""
|
|
473
|
+
args = {"lane": lane, "worker": worker, "branch": branch, "work_items": work_items}
|
|
474
|
+
if branch_pattern is not None:
|
|
475
|
+
args["branch_pattern"] = branch_pattern
|
|
476
|
+
return await execute_command(
|
|
477
|
+
ctx,
|
|
478
|
+
"lane_start",
|
|
479
|
+
request_id,
|
|
480
|
+
args,
|
|
481
|
+
)
|
|
482
|
+
|
|
483
|
+
@server.tool()
|
|
484
|
+
async def lane_update( # pyright: ignore[reportUnusedFunction]
|
|
485
|
+
ctx: Context[Any, Request],
|
|
486
|
+
lane: str,
|
|
487
|
+
waiting_on: str | None,
|
|
488
|
+
request_id: str,
|
|
489
|
+
) -> dict[str, Any]:
|
|
490
|
+
"""Report who the lane is waiting on. Pass null when it can continue."""
|
|
491
|
+
return await execute_command(
|
|
492
|
+
ctx,
|
|
493
|
+
"lane_update",
|
|
494
|
+
request_id,
|
|
495
|
+
{"lane": lane, "waiting_on": waiting_on or ""},
|
|
496
|
+
)
|
|
497
|
+
|
|
498
|
+
@server.tool()
|
|
499
|
+
async def lane_ready( # pyright: ignore[reportUnusedFunction]
|
|
500
|
+
ctx: Context[Any, Request],
|
|
501
|
+
lane: str,
|
|
502
|
+
pr: int,
|
|
503
|
+
head_sha: str,
|
|
504
|
+
request_id: str,
|
|
505
|
+
checklist: list[ReadyChecklistEntry] | None = None,
|
|
506
|
+
) -> dict[str, Any]:
|
|
507
|
+
"""Report the pull request and head ready for review with optional post-merge steps."""
|
|
508
|
+
args: dict[str, Any] = {"lane": lane, "pr": pr, "head_sha": head_sha}
|
|
509
|
+
if checklist is not None:
|
|
510
|
+
args["checklist"] = [entry.model_dump() for entry in checklist]
|
|
511
|
+
return await execute_command(ctx, "lane_ready", request_id, args)
|
|
512
|
+
|
|
513
|
+
@server.tool()
|
|
514
|
+
async def lane_archive( # pyright: ignore[reportUnusedFunction]
|
|
515
|
+
ctx: Context[Any, Request],
|
|
516
|
+
lane: str,
|
|
517
|
+
request_id: str,
|
|
518
|
+
) -> dict[str, Any]:
|
|
519
|
+
"""Archive the lane and release its worker."""
|
|
520
|
+
return await execute_command(ctx, "lane_archive", request_id, {"lane": lane})
|
|
521
|
+
|
|
522
|
+
@server.tool()
|
|
523
|
+
async def deploy_report( # pyright: ignore[reportUnusedFunction]
|
|
524
|
+
ctx: Context[Any, Request],
|
|
525
|
+
lane: str,
|
|
526
|
+
environment: str,
|
|
527
|
+
sha: str,
|
|
528
|
+
state: str,
|
|
529
|
+
request_id: str,
|
|
530
|
+
) -> dict[str, Any]:
|
|
531
|
+
"""Report a deployment status for the calling worker's lane."""
|
|
532
|
+
return await execute_command(
|
|
533
|
+
ctx,
|
|
534
|
+
"deploy_report",
|
|
535
|
+
request_id,
|
|
536
|
+
{"lane": lane, "environment": environment, "sha": sha, "state": state},
|
|
537
|
+
)
|
|
538
|
+
|
|
539
|
+
@server.tool()
|
|
540
|
+
async def clerk_import( # pyright: ignore[reportUnusedFunction]
|
|
541
|
+
ctx: Context[Any, Request],
|
|
542
|
+
triggers: list[TriggerImportRow],
|
|
543
|
+
pulls: list[PullImportRow],
|
|
544
|
+
skips: list[SkipImportRow],
|
|
545
|
+
ledger_repository: str | None = None,
|
|
546
|
+
) -> dict[str, Any]:
|
|
547
|
+
"""Import typed clerk template rows over the private local listener."""
|
|
548
|
+
if not _private_local_request(ctx):
|
|
549
|
+
return err("forbidden", "clerk import requires the private local listener")
|
|
550
|
+
args: dict[str, Any] = {"triggers": triggers, "pulls": pulls, "skips": skips}
|
|
551
|
+
if ledger_repository is not None:
|
|
552
|
+
args["ledger_repository"] = ledger_repository
|
|
553
|
+
canonical = json.dumps(args, sort_keys=True, separators=(",", ":"), allow_nan=False)
|
|
554
|
+
request_id = "clerk-import:" + hashlib.sha256(canonical.encode()).hexdigest()
|
|
555
|
+
return await execute_command(ctx, "clerk_import", request_id, args)
|
|
556
|
+
|
|
557
|
+
@server.tool()
|
|
558
|
+
async def checklist_import( # pyright: ignore[reportUnusedFunction]
|
|
559
|
+
ctx: Context[Any, Request],
|
|
560
|
+
lane: str,
|
|
561
|
+
rows: list[ReadyChecklistEntry],
|
|
562
|
+
request_id: str,
|
|
563
|
+
) -> dict[str, Any]:
|
|
564
|
+
"""Import checklist rows for the current projected PR of a lane."""
|
|
565
|
+
if not _private_local_request(ctx):
|
|
566
|
+
return err("forbidden", "checklist import requires the private local listener")
|
|
567
|
+
return await execute_command(
|
|
568
|
+
ctx,
|
|
569
|
+
"checklist_import",
|
|
570
|
+
request_id,
|
|
571
|
+
{"lane": lane, "rows": [row.model_dump() for row in rows]},
|
|
572
|
+
)
|
|
573
|
+
|
|
574
|
+
@server.tool()
|
|
575
|
+
async def checklist_done( # pyright: ignore[reportUnusedFunction]
|
|
576
|
+
ctx: Context[Any, Request],
|
|
577
|
+
lane: str,
|
|
578
|
+
step: str,
|
|
579
|
+
request_id: str,
|
|
580
|
+
ref: str | None = None,
|
|
581
|
+
) -> dict[str, Any]:
|
|
582
|
+
"""Complete an active checklist step. With no ref, use the step's declared ref."""
|
|
583
|
+
args = {"lane": lane, "step": step}
|
|
584
|
+
if ref is not None:
|
|
585
|
+
args["ref"] = ref
|
|
586
|
+
return await execute_command(ctx, "checklist_done", request_id, args)
|
|
587
|
+
|
|
588
|
+
@server.tool()
|
|
589
|
+
async def freeze( # pyright: ignore[reportUnusedFunction]
|
|
590
|
+
ctx: Context[Any, Request], reason: str, request_id: str
|
|
591
|
+
) -> dict[str, Any]:
|
|
592
|
+
"""Set this project's deploy window and give the reason shown by the merge gate."""
|
|
593
|
+
return await execute_command(ctx, "freeze", request_id, {"reason": reason})
|
|
594
|
+
|
|
595
|
+
@server.tool()
|
|
596
|
+
async def unfreeze( # pyright: ignore[reportUnusedFunction]
|
|
597
|
+
ctx: Context[Any, Request], request_id: str
|
|
598
|
+
) -> dict[str, Any]:
|
|
599
|
+
"""Clear this project's deploy window."""
|
|
600
|
+
return await execute_command(ctx, "unfreeze", request_id, {})
|
|
601
|
+
|
|
602
|
+
@server.tool()
|
|
603
|
+
async def claim( # pyright: ignore[reportUnusedFunction]
|
|
604
|
+
ctx: Context[Any, Request], lane: str, globs: list[str], reason: str, request_id: str
|
|
605
|
+
) -> dict[str, Any]:
|
|
606
|
+
"""Claim one or more paths for a lane and record the reason."""
|
|
607
|
+
return await execute_command(
|
|
608
|
+
ctx, "claim", request_id, {"lane": lane, "globs": globs, "reason": reason}
|
|
609
|
+
)
|
|
610
|
+
|
|
611
|
+
@server.tool()
|
|
612
|
+
async def release( # pyright: ignore[reportUnusedFunction]
|
|
613
|
+
ctx: Context[Any, Request], lane: str, globs: list[str], request_id: str
|
|
614
|
+
) -> dict[str, Any]:
|
|
615
|
+
"""Release the requested path claims for a lane."""
|
|
616
|
+
return await execute_command(ctx, "release", request_id, {"lane": lane, "globs": globs})
|
|
617
|
+
|
|
618
|
+
@server.tool()
|
|
619
|
+
async def route( # pyright: ignore[reportUnusedFunction]
|
|
620
|
+
ctx: Context[Any, Request], action_kind: str, lane: str, request_id: str
|
|
621
|
+
) -> dict[str, Any]:
|
|
622
|
+
"""Assign a lane to a hands-on action kind."""
|
|
623
|
+
return await execute_command(
|
|
624
|
+
ctx, "route", request_id, {"action_kind": action_kind, "lane": lane}
|
|
625
|
+
)
|
|
626
|
+
|
|
627
|
+
@server.tool()
|
|
628
|
+
async def route_request( # pyright: ignore[reportUnusedFunction]
|
|
629
|
+
ctx: Context[Any, Request], action_kind: str, request_id: str
|
|
630
|
+
) -> dict[str, Any]:
|
|
631
|
+
"""Return the assigned lane or raise a next item for the missing route."""
|
|
632
|
+
return await execute_command(ctx, "route_request", request_id, {"action_kind": action_kind})
|
|
633
|
+
|
|
634
|
+
@server.tool()
|
|
635
|
+
async def budget_open( # pyright: ignore[reportUnusedFunction]
|
|
636
|
+
ctx: Context[Any, Request],
|
|
637
|
+
run: str,
|
|
638
|
+
lane: str,
|
|
639
|
+
cap_usd: StrictFloat,
|
|
640
|
+
provider: str,
|
|
641
|
+
key_scope: str,
|
|
642
|
+
request_id: str,
|
|
643
|
+
) -> dict[str, Any]:
|
|
644
|
+
"""Register a run's spend cap and owning lane. Supply only the name of the key scope."""
|
|
645
|
+
return await execute_command(
|
|
646
|
+
ctx,
|
|
647
|
+
"budget_open",
|
|
648
|
+
request_id,
|
|
649
|
+
{
|
|
650
|
+
"run": run,
|
|
651
|
+
"lane": lane,
|
|
652
|
+
"cap_usd": cap_usd,
|
|
653
|
+
"provider": provider,
|
|
654
|
+
"key_scope": key_scope,
|
|
655
|
+
},
|
|
656
|
+
)
|
|
657
|
+
|
|
658
|
+
@server.tool()
|
|
659
|
+
async def budget_status( # pyright: ignore[reportUnusedFunction]
|
|
660
|
+
ctx: Context[Any, Request], run: str | None = None
|
|
661
|
+
) -> dict[str, Any]:
|
|
662
|
+
"""Show current cumulative spend against caps, optionally for one run."""
|
|
663
|
+
try:
|
|
664
|
+
caller = board_access(ctx)
|
|
665
|
+
if caller.ledger is None:
|
|
666
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
667
|
+
return ok({"budgets": await caller.ledger.budget_status(caller.identity.role, run)})
|
|
668
|
+
except LedgerError as exc:
|
|
669
|
+
return err(exc.code, exc.message)
|
|
670
|
+
|
|
671
|
+
@server.tool()
|
|
672
|
+
async def run_report( # pyright: ignore[reportUnusedFunction]
|
|
673
|
+
ctx: Context[Any, Request], lane: str, run_id: str, state: str, request_id: str
|
|
674
|
+
) -> dict[str, Any]:
|
|
675
|
+
"""Record an observation of a run owned by the calling worker's lane."""
|
|
676
|
+
return await execute_command(
|
|
677
|
+
ctx, "run_report", request_id, {"lane": lane, "run_id": run_id, "state": state}
|
|
678
|
+
)
|
|
679
|
+
|
|
680
|
+
@server.tool()
|
|
681
|
+
async def decide( # pyright: ignore[reportUnusedFunction]
|
|
682
|
+
ctx: Context[Any, Request],
|
|
683
|
+
who: str,
|
|
684
|
+
exact_words: str,
|
|
685
|
+
where_ref: DecisionCitation,
|
|
686
|
+
governs: list[str],
|
|
687
|
+
settles: list[str],
|
|
688
|
+
request_id: str,
|
|
689
|
+
) -> dict[str, Any]:
|
|
690
|
+
"""Record the human's verbatim decision and the tickets and asks it applies to.
|
|
691
|
+
|
|
692
|
+
Cite a projected comment with kind github_comment, repository, issue, and comment_id.
|
|
693
|
+
Cite a chat source with kind chat and its opaque ref.
|
|
694
|
+
"""
|
|
695
|
+
return await execute_command(
|
|
696
|
+
ctx,
|
|
697
|
+
"decide",
|
|
698
|
+
request_id,
|
|
699
|
+
{
|
|
700
|
+
"who": who,
|
|
701
|
+
"exact_words": exact_words,
|
|
702
|
+
"where_ref": where_ref.model_dump(),
|
|
703
|
+
"governs": governs,
|
|
704
|
+
"settles": settles,
|
|
705
|
+
},
|
|
706
|
+
)
|
|
707
|
+
|
|
708
|
+
@server.tool()
|
|
709
|
+
async def decisions( # pyright: ignore[reportUnusedFunction]
|
|
710
|
+
ctx: Context[Any, Request], governs: str | None = None
|
|
711
|
+
) -> dict[str, Any]:
|
|
712
|
+
"""List recorded decisions, optionally filtered by one exact governed ticket reference."""
|
|
713
|
+
try:
|
|
714
|
+
caller = board_access(ctx)
|
|
715
|
+
if caller.ledger is None:
|
|
716
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
717
|
+
return ok(
|
|
718
|
+
{"decisions": await caller.ledger.list_decisions(caller.identity.role, governs)}
|
|
719
|
+
)
|
|
720
|
+
except LedgerError as exc:
|
|
721
|
+
return err(exc.code, exc.message)
|
|
722
|
+
|
|
723
|
+
@server.tool()
|
|
724
|
+
async def ask( # pyright: ignore[reportUnusedFunction]
|
|
725
|
+
ctx: Context[Any, Request],
|
|
726
|
+
issue: StrictInt,
|
|
727
|
+
text: str,
|
|
728
|
+
options: list[str],
|
|
729
|
+
comment_id: StrictInt,
|
|
730
|
+
request_id: str,
|
|
731
|
+
) -> dict[str, Any]:
|
|
732
|
+
"""Record an ask citing an existing projected comment on a GitHub issue."""
|
|
733
|
+
return await execute_command(
|
|
734
|
+
ctx,
|
|
735
|
+
"ask",
|
|
736
|
+
request_id,
|
|
737
|
+
{"issue": issue, "text": text, "options": options, "comment_id": comment_id},
|
|
738
|
+
)
|
|
739
|
+
|
|
740
|
+
@server.tool()
|
|
741
|
+
async def asks( # pyright: ignore[reportUnusedFunction]
|
|
742
|
+
ctx: Context[Any, Request], stale_only: bool = False
|
|
743
|
+
) -> dict[str, Any]:
|
|
744
|
+
"""List recorded asks, optionally limiting results to active stale conditions."""
|
|
745
|
+
try:
|
|
746
|
+
caller = board_access(ctx)
|
|
747
|
+
if caller.ledger is None:
|
|
748
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
749
|
+
return ok({"asks": await caller.ledger.list_asks(caller.identity.role, stale_only)})
|
|
750
|
+
except LedgerError as exc:
|
|
751
|
+
return err(exc.code, exc.message)
|
|
752
|
+
|
|
753
|
+
@server.tool()
|
|
754
|
+
async def goal_set( # pyright: ignore[reportUnusedFunction]
|
|
755
|
+
ctx: Context[Any, Request], name: str, text: str, request_id: str
|
|
756
|
+
) -> dict[str, Any]:
|
|
757
|
+
"""Store a validated operator goal under the given name."""
|
|
758
|
+
return await execute_command(ctx, "goal_set", request_id, {"name": name, "text": text})
|
|
759
|
+
|
|
760
|
+
@server.tool()
|
|
761
|
+
async def goal( # pyright: ignore[reportUnusedFunction]
|
|
762
|
+
ctx: Context[Any, Request], name: str | None = None
|
|
763
|
+
) -> dict[str, Any]:
|
|
764
|
+
"""Evaluate a named goal or all stored goals and report every remaining issue."""
|
|
765
|
+
try:
|
|
766
|
+
caller = board_access(ctx)
|
|
767
|
+
if caller.ledger is None:
|
|
768
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
769
|
+
return ok(await caller.ledger.goal(caller.identity.role, name))
|
|
770
|
+
except LedgerError as exc:
|
|
771
|
+
return err(exc.code, exc.message)
|
|
772
|
+
|
|
773
|
+
@server.tool()
|
|
774
|
+
async def trigger( # pyright: ignore[reportUnusedFunction]
|
|
775
|
+
ctx: Context[Any, Request],
|
|
776
|
+
predicate: dict[str, Any],
|
|
777
|
+
action: dict[str, Any],
|
|
778
|
+
request_id: str,
|
|
779
|
+
lane: str | None = None,
|
|
780
|
+
mode: Literal["once", "every"] = "once",
|
|
781
|
+
imported_fired: StrictBool = False,
|
|
782
|
+
) -> dict[str, Any]:
|
|
783
|
+
"""Register a once or every trigger. A predicate can be {"pr_merged":{"number":42}} or use
|
|
784
|
+
branch_glob instead of number. all_of and any_of take arrays of predicates.
|
|
785
|
+
Use {"checklist_empty":{"lane":"name"}} to react when the last active step closes.
|
|
786
|
+
An action object names one of notify_orchestrator (text), send (worker, text),
|
|
787
|
+
mark (lane, state), or propose_archive (lane). On the private local listener,
|
|
788
|
+
imported_fired records a previously fired once trigger without delivering it.
|
|
789
|
+
"""
|
|
790
|
+
args: dict[str, Any] = {"predicate": predicate, "action": action}
|
|
791
|
+
if lane is not None:
|
|
792
|
+
args["lane"] = lane
|
|
793
|
+
if mode != "once":
|
|
794
|
+
args["mode"] = mode
|
|
795
|
+
if imported_fired:
|
|
796
|
+
if not _private_local_request(ctx):
|
|
797
|
+
return err("forbidden", "imported_fired requires the private local listener")
|
|
798
|
+
args["imported_fired"] = True
|
|
799
|
+
return await execute_command(ctx, "trigger", request_id, args)
|
|
800
|
+
|
|
801
|
+
@server.tool()
|
|
802
|
+
async def triggers( # pyright: ignore[reportUnusedFunction]
|
|
803
|
+
ctx: Context[Any, Request], lane: str | None = None
|
|
804
|
+
) -> dict[str, Any]:
|
|
805
|
+
"""List trigger states and bindings, optionally for a single lane."""
|
|
806
|
+
try:
|
|
807
|
+
caller = board_access(ctx)
|
|
808
|
+
if caller.ledger is None:
|
|
809
|
+
raise LedgerError("unavailable", "the project ledger is unavailable")
|
|
810
|
+
return ok({"triggers": await caller.ledger.list_triggers(caller.identity.role, lane)})
|
|
811
|
+
except LedgerError as exc:
|
|
812
|
+
return err(exc.code, exc.message)
|
|
813
|
+
|
|
814
|
+
@server.tool()
|
|
815
|
+
async def cancel( # pyright: ignore[reportUnusedFunction]
|
|
816
|
+
ctx: Context[Any, Request], trigger_id: str, request_id: str
|
|
817
|
+
) -> dict[str, Any]:
|
|
818
|
+
"""Cancel a trigger before it fires."""
|
|
819
|
+
return await execute_command(ctx, "cancel", request_id, {"trigger_id": trigger_id})
|
|
820
|
+
|
|
821
|
+
@server.tool()
|
|
822
|
+
async def outbox_lease( # pyright: ignore[reportUnusedFunction]
|
|
823
|
+
ctx: Context[Any, Request], request_id: str, consumer_id: str
|
|
824
|
+
) -> dict[str, Any]:
|
|
825
|
+
"""Lease the next Claude outbox message. Generate a UUIDv4 consumer ID per drain process.
|
|
826
|
+
|
|
827
|
+
Use a fresh request ID for new work. A retry from the same consumer returns its original
|
|
828
|
+
lease and expiry. Deliver only while the lease is current.
|
|
829
|
+
"""
|
|
830
|
+
try:
|
|
831
|
+
scoped_request = lease_request_id(consumer_id, request_id)
|
|
832
|
+
except LedgerError as exc:
|
|
833
|
+
return err(exc.code, exc.message)
|
|
834
|
+
return await execute_command(ctx, "outbox_lease", scoped_request, {})
|
|
835
|
+
|
|
836
|
+
@server.tool()
|
|
837
|
+
async def outbox_done( # pyright: ignore[reportUnusedFunction]
|
|
838
|
+
ctx: Context[Any, Request], act_id: str, request_id: str
|
|
839
|
+
) -> dict[str, Any]:
|
|
840
|
+
"""Complete the current lease after delivery. Pass its opaque act_id."""
|
|
841
|
+
return await execute_command(ctx, "outbox_done", request_id, {"act_id": act_id})
|
|
842
|
+
|
|
843
|
+
if bridge_enabled:
|
|
844
|
+
|
|
845
|
+
@server.tool()
|
|
846
|
+
async def bridge_receive( # pyright: ignore[reportUnusedFunction]
|
|
847
|
+
ctx: Context[Any, Request], board: str, message_id: StrictInt, sender: str, body: str
|
|
848
|
+
) -> dict[str, Any]:
|
|
849
|
+
"""Commit a claimed Switchboard message before acknowledging it on the relay."""
|
|
850
|
+
return await execute_command(
|
|
851
|
+
ctx,
|
|
852
|
+
"bridge_receive",
|
|
853
|
+
f"{board}:{message_id}",
|
|
854
|
+
{"board": board, "message_id": message_id, "sender": sender, "body": body},
|
|
855
|
+
)
|
|
856
|
+
|
|
857
|
+
@server.tool()
|
|
858
|
+
async def bridge_lease( # pyright: ignore[reportUnusedFunction]
|
|
859
|
+
ctx: Context[Any, Request], request_id: str, consumer_id: str, board: str
|
|
860
|
+
) -> dict[str, Any]:
|
|
861
|
+
"""Claim the next pending Switchboard delivery for this bridge."""
|
|
862
|
+
try:
|
|
863
|
+
scoped = lease_request_id(consumer_id, request_id)
|
|
864
|
+
except LedgerError as exc:
|
|
865
|
+
return err(exc.code, exc.message)
|
|
866
|
+
return await execute_command(ctx, "bridge_lease", f"{board}:{scoped}", {"board": board})
|
|
867
|
+
|
|
868
|
+
@server.tool()
|
|
869
|
+
async def bridge_done( # pyright: ignore[reportUnusedFunction]
|
|
870
|
+
ctx: Context[Any, Request], act_id: str
|
|
871
|
+
) -> dict[str, Any]:
|
|
872
|
+
"""Finish the current delivery lease after the relay accepts the send."""
|
|
873
|
+
return await execute_command(ctx, "bridge_done", act_id, {"act_id": act_id})
|
|
874
|
+
|
|
875
|
+
app = server.streamable_http_app(
|
|
876
|
+
stateless_http=True,
|
|
877
|
+
transport_security=TransportSecuritySettings(allowed_hosts=list(allowed_hosts)),
|
|
878
|
+
)
|
|
879
|
+
|
|
880
|
+
async def health(_request: Request) -> Response:
|
|
881
|
+
return JSONResponse({"status": "ok"})
|
|
882
|
+
|
|
883
|
+
async def readiness(request: Request) -> Response:
|
|
884
|
+
project = request.path_params.get("project")
|
|
885
|
+
if project is not None and project not in project_ids:
|
|
886
|
+
return JSONResponse({"status": "unknown_project"}, status_code=404)
|
|
887
|
+
selected = project_ids if project is None else (str(project),)
|
|
888
|
+
states = {
|
|
889
|
+
name: "ready" if unavailable is None and name in ledgers else "unavailable"
|
|
890
|
+
for name in selected
|
|
891
|
+
}
|
|
892
|
+
ready = bool(states) and all(state == "ready" for state in states.values())
|
|
893
|
+
return JSONResponse(
|
|
894
|
+
{"status": "ready" if ready else "unavailable", "projects": states},
|
|
895
|
+
status_code=200 if ready else 503,
|
|
896
|
+
)
|
|
897
|
+
|
|
898
|
+
async def backup_project(request: Request) -> Response:
|
|
899
|
+
if backup is None:
|
|
900
|
+
return Response(status_code=404)
|
|
901
|
+
caller: ProjectRequest = request.state.plotroom
|
|
902
|
+
if caller.identity.role != "orchestrator":
|
|
903
|
+
return Response(status_code=403)
|
|
904
|
+
if caller.ledger is None:
|
|
905
|
+
return JSONResponse({"status": "unavailable"}, status_code=503)
|
|
906
|
+
project = caller.identity.project_id
|
|
907
|
+
expected = request.query_params.get("project")
|
|
908
|
+
if expected is not None and expected != project:
|
|
909
|
+
return Response(status_code=403)
|
|
910
|
+
try:
|
|
911
|
+
name = await backup(project)
|
|
912
|
+
except LedgerError:
|
|
913
|
+
return JSONResponse({"status": "unavailable"}, status_code=503)
|
|
914
|
+
return JSONResponse({"project": project, "file": name}, status_code=201)
|
|
915
|
+
|
|
916
|
+
app.routes.extend(
|
|
917
|
+
[
|
|
918
|
+
Route("/backup", backup_project, methods=["POST"]),
|
|
919
|
+
Route("/healthz", health),
|
|
920
|
+
Route("/readyz", readiness),
|
|
921
|
+
Route("/readyz/{project}", readiness),
|
|
922
|
+
]
|
|
923
|
+
)
|
|
924
|
+
app.add_middleware(
|
|
925
|
+
_AccessMiddleware,
|
|
926
|
+
projects=frozenset(project_ids),
|
|
927
|
+
ledgers=ledgers,
|
|
928
|
+
allowed_hosts=allowed_hosts,
|
|
929
|
+
authenticate=authenticate,
|
|
930
|
+
)
|
|
931
|
+
return app
|