plotroom 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. plotroom/__init__.py +0 -0
  2. plotroom/__main__.py +8 -0
  3. plotroom/_secret_file.py +8 -0
  4. plotroom/adapters/__init__.py +0 -0
  5. plotroom/adapters/claude_desktop.py +219 -0
  6. plotroom/adapters/embedding_http.py +175 -0
  7. plotroom/adapters/github/__init__.py +337 -0
  8. plotroom/adapters/github/app_credential.py +209 -0
  9. plotroom/adapters/github/ci.py +738 -0
  10. plotroom/adapters/github/commit_statuses.py +63 -0
  11. plotroom/adapters/github/deployments.py +213 -0
  12. plotroom/adapters/github/details.py +867 -0
  13. plotroom/adapters/github/issue_details.py +368 -0
  14. plotroom/adapters/github/junit.py +210 -0
  15. plotroom/adapters/github/junit_xml.py +162 -0
  16. plotroom/adapters/github/kodus_review.py +347 -0
  17. plotroom/adapters/github/pr_details.py +192 -0
  18. plotroom/adapters/github/pull_comparison.py +108 -0
  19. plotroom/adapters/github/read.py +530 -0
  20. plotroom/adapters/github/ticket_tree.py +336 -0
  21. plotroom/adapters/mcp/__init__.py +5 -0
  22. plotroom/adapters/mcp/proxy.py +722 -0
  23. plotroom/adapters/mcp/server.py +931 -0
  24. plotroom/adapters/openrouter.py +151 -0
  25. plotroom/adapters/store_sqlite/__init__.py +6 -0
  26. plotroom/adapters/store_sqlite/actions_sql.py +148 -0
  27. plotroom/adapters/store_sqlite/asks_sql.py +153 -0
  28. plotroom/adapters/store_sqlite/backfill_sql.py +138 -0
  29. plotroom/adapters/store_sqlite/budgets_sql.py +102 -0
  30. plotroom/adapters/store_sqlite/checklists_sql.py +77 -0
  31. plotroom/adapters/store_sqlite/claims_sql.py +54 -0
  32. plotroom/adapters/store_sqlite/embedding_sql.py +275 -0
  33. plotroom/adapters/store_sqlite/github_sql.py +331 -0
  34. plotroom/adapters/store_sqlite/goals_sql.py +57 -0
  35. plotroom/adapters/store_sqlite/human_queue_sql.py +60 -0
  36. plotroom/adapters/store_sqlite/items_sql.py +129 -0
  37. plotroom/adapters/store_sqlite/lanes_sql.py +85 -0
  38. plotroom/adapters/store_sqlite/ledger_sql.py +506 -0
  39. plotroom/adapters/store_sqlite/migrate.py +194 -0
  40. plotroom/adapters/store_sqlite/migrations/env.py +36 -0
  41. plotroom/adapters/store_sqlite/migrations/script.py.mako +19 -0
  42. plotroom/adapters/store_sqlite/migrations/versions/0001_project_meta.py +23 -0
  43. plotroom/adapters/store_sqlite/migrations/versions/0002_event_log.py +40 -0
  44. plotroom/adapters/store_sqlite/migrations/versions/0003_next_items.py +49 -0
  45. plotroom/adapters/store_sqlite/migrations/versions/0004_actions.py +28 -0
  46. plotroom/adapters/store_sqlite/migrations/versions/0005_lanes.py +36 -0
  47. plotroom/adapters/store_sqlite/migrations/versions/0006_work_items.py +22 -0
  48. plotroom/adapters/store_sqlite/migrations/versions/0007_item_changes.py +33 -0
  49. plotroom/adapters/store_sqlite/migrations/versions/0008_claude_turn_aliases.py +30 -0
  50. plotroom/adapters/store_sqlite/migrations/versions/0009_github_projection.py +38 -0
  51. plotroom/adapters/store_sqlite/migrations/versions/0010_github_pull_freshness.py +37 -0
  52. plotroom/adapters/store_sqlite/migrations/versions/0011_github_resources.py +27 -0
  53. plotroom/adapters/store_sqlite/migrations/versions/0012_local_triggers_outbox.py +80 -0
  54. plotroom/adapters/store_sqlite/migrations/versions/0013_lane_claims.py +22 -0
  55. plotroom/adapters/store_sqlite/migrations/versions/0015_recurring_triggers.py +45 -0
  56. plotroom/adapters/store_sqlite/migrations/versions/0016_merge_controller.py +28 -0
  57. plotroom/adapters/store_sqlite/migrations/versions/0017_lane_checklists.py +36 -0
  58. plotroom/adapters/store_sqlite/migrations/versions/0018_guardrails.py +33 -0
  59. plotroom/adapters/store_sqlite/migrations/versions/0019_clerk_import_metadata.py +16 -0
  60. plotroom/adapters/store_sqlite/migrations/versions/0020_search.py +30 -0
  61. plotroom/adapters/store_sqlite/migrations/versions/0021_human_queue.py +46 -0
  62. plotroom/adapters/store_sqlite/migrations/versions/0022_search_records.py +77 -0
  63. plotroom/adapters/store_sqlite/migrations/versions/0023_operator_goals.py +25 -0
  64. plotroom/adapters/store_sqlite/migrations/versions/0024_embedding_rejections.py +36 -0
  65. plotroom/adapters/store_sqlite/outbox_sql.py +78 -0
  66. plotroom/adapters/store_sqlite/routes_sql.py +26 -0
  67. plotroom/adapters/store_sqlite/search_sql.py +116 -0
  68. plotroom/adapters/store_sqlite/store.py +623 -0
  69. plotroom/adapters/store_sqlite/triggers_sql.py +111 -0
  70. plotroom/adapters/store_sqlite/work_items_sql.py +36 -0
  71. plotroom/adapters/switchboard.py +152 -0
  72. plotroom/adapters/usage_fixture.py +11 -0
  73. plotroom/adapters/work_items_bmad.py +256 -0
  74. plotroom/adapters/work_items_github_issues.py +323 -0
  75. plotroom/app/__init__.py +0 -0
  76. plotroom/app/bridge.py +162 -0
  77. plotroom/app/clerk_import.py +349 -0
  78. plotroom/app/cli.py +290 -0
  79. plotroom/app/clock.py +12 -0
  80. plotroom/app/config.py +984 -0
  81. plotroom/app/daemon_start.py +453 -0
  82. plotroom/app/lock.py +77 -0
  83. plotroom/app/logging.py +93 -0
  84. plotroom/app/portfile.py +218 -0
  85. plotroom/app/runtime.py +518 -0
  86. plotroom/app/serve.py +671 -0
  87. plotroom/app/session_intake.py +51 -0
  88. plotroom/app/sqlite_floor.py +35 -0
  89. plotroom/app/tokens.py +265 -0
  90. plotroom/core/__init__.py +0 -0
  91. plotroom/core/actions.py +67 -0
  92. plotroom/core/ask_checks.py +124 -0
  93. plotroom/core/asks.py +123 -0
  94. plotroom/core/branch_updates.py +207 -0
  95. plotroom/core/bridge.py +112 -0
  96. plotroom/core/briefs.py +83 -0
  97. plotroom/core/budgets.py +182 -0
  98. plotroom/core/catalog.py +464 -0
  99. plotroom/core/checklists.py +151 -0
  100. plotroom/core/claims.py +296 -0
  101. plotroom/core/clerk_import.py +379 -0
  102. plotroom/core/commands.py +85 -0
  103. plotroom/core/deploy_window.py +21 -0
  104. plotroom/core/envelope.py +20 -0
  105. plotroom/core/events.py +227 -0
  106. plotroom/core/github.py +306 -0
  107. plotroom/core/github_projection.py +310 -0
  108. plotroom/core/goal_language.py +374 -0
  109. plotroom/core/goal_projection.py +146 -0
  110. plotroom/core/human_queue.py +119 -0
  111. plotroom/core/intake.py +106 -0
  112. plotroom/core/kodus_review.py +53 -0
  113. plotroom/core/lanes.py +281 -0
  114. plotroom/core/ledger.py +808 -0
  115. plotroom/core/merge_controller.py +344 -0
  116. plotroom/core/merge_flakes.py +422 -0
  117. plotroom/core/merge_gate.py +724 -0
  118. plotroom/core/merge_lane.py +70 -0
  119. plotroom/core/merge_overlap.py +76 -0
  120. plotroom/core/next_items.py +60 -0
  121. plotroom/core/operator_goals.py +103 -0
  122. plotroom/core/outbox.py +70 -0
  123. plotroom/core/pull_overlaps.py +152 -0
  124. plotroom/core/reports.py +62 -0
  125. plotroom/core/roles.py +100 -0
  126. plotroom/core/routing.py +55 -0
  127. plotroom/core/run_reports.py +53 -0
  128. plotroom/core/search.py +266 -0
  129. plotroom/core/search_sources.py +94 -0
  130. plotroom/core/snapshot.py +204 -0
  131. plotroom/core/trigger_actions.py +89 -0
  132. plotroom/core/triggers.py +550 -0
  133. plotroom/core/unit_of_work.py +541 -0
  134. plotroom/core/watchers.py +35 -0
  135. plotroom/core/work_items.py +23 -0
  136. plotroom/core/writer.py +164 -0
  137. plotroom/ports/__init__.py +6 -0
  138. plotroom/ports/clock.py +12 -0
  139. plotroom/ports/github_write.py +72 -0
  140. plotroom/ports/search.py +197 -0
  141. plotroom/ports/session_reader.py +23 -0
  142. plotroom/ports/store.py +323 -0
  143. plotroom/ports/usage_meter.py +35 -0
  144. plotroom/ports/work_items.py +22 -0
  145. plotroom/py.typed +0 -0
  146. plotroom-0.1.0.dist-info/METADATA +230 -0
  147. plotroom-0.1.0.dist-info/RECORD +150 -0
  148. plotroom-0.1.0.dist-info/WHEEL +4 -0
  149. plotroom-0.1.0.dist-info/entry_points.txt +2 -0
  150. plotroom-0.1.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,204 @@
1
+ """Budgeted rendering of project board, next-item and search pages (AD-13)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from collections import Counter
7
+ from collections.abc import Iterable
8
+ from dataclasses import asdict
9
+ from typing import Any
10
+
11
+ from plotroom.core.claims import OVERLAP_KINDS, Claim
12
+ from plotroom.core.envelope import ok
13
+ from plotroom.core.events import LedgerError
14
+ from plotroom.core.github import GitHubSummary
15
+ from plotroom.core.lanes import Lane
16
+ from plotroom.core.next_items import Item
17
+ from plotroom.ports.search import SearchPage
18
+
19
+ DEFAULT_SNAPSHOT_BUDGET = 2000
20
+ MIN_BOARD_BUDGET = 256
21
+
22
+
23
+ def estimate_tokens(text: str) -> int:
24
+ """Approximate tokens by counting UTF-8 bytes in groups of four."""
25
+ return (len(text.encode("utf-8")) + 3) // 4
26
+
27
+
28
+ def _fits(data: dict[str, Any], budget: int) -> bool:
29
+ # The MCP SDK emits this indented tool envelope as text.
30
+ return estimate_tokens(json.dumps(ok(data), ensure_ascii=False, indent=2)) <= budget
31
+
32
+
33
+ def _add(
34
+ data: dict[str, Any], rows: list[dict[str, Any]], row: dict[str, Any], budget: int
35
+ ) -> bool:
36
+ rows.append(row)
37
+ if _fits(data, budget):
38
+ return True
39
+ rows.pop()
40
+ return False
41
+
42
+
43
+ def render_search(page: SearchPage, budget: int) -> dict[str, Any]:
44
+ hits: list[dict[str, Any]] = []
45
+ data: dict[str, Any] = {"hits": hits, "omitted": page.total}
46
+ if page.mode is not None:
47
+ data["mode"] = page.mode
48
+ if page.fallback_reason is not None:
49
+ data["fallback_reason"] = page.fallback_reason
50
+ if not _fits(data, budget):
51
+ raise LedgerError("unavailable", "search reply exceeds the configured token budget")
52
+ for hit in page.hits:
53
+ data["omitted"] = page.total - len(hits) - 1
54
+ row = {"id": hit.id, "kind": hit.kind, "excerpt": hit.excerpt}
55
+ if _add(data, hits, row, budget):
56
+ continue
57
+ if not hits:
58
+ excerpt = hit.excerpt
59
+ if hit.match_span is not None:
60
+ start, end = hit.match_span
61
+ excerpt = excerpt[start:end]
62
+ row["excerpt"] = excerpt
63
+ if _add(data, hits, row, budget):
64
+ continue
65
+ if len(excerpt) > 80:
66
+ row["excerpt"] = excerpt[:80] + "…"
67
+ if _add(data, hits, row, budget):
68
+ continue
69
+ data["omitted"] += 1
70
+ break
71
+ return data
72
+
73
+
74
+ def render_snapshot(
75
+ project_id: str,
76
+ cursor: int,
77
+ items: list[Item],
78
+ lanes: dict[str, int],
79
+ budget: int,
80
+ *,
81
+ lane_details: list[Lane] | None = None,
82
+ lane_offset: int = 0,
83
+ github: GitHubSummary | None = None,
84
+ github_error: str | None = None,
85
+ claim_count: int = 0,
86
+ trigger_counts: dict[str, int] | None = None,
87
+ freeze_reason: str | None = None,
88
+ ) -> dict[str, Any]:
89
+ listed: list[dict[str, Any]] = []
90
+ board: dict[str, Any] = {
91
+ "project": project_id,
92
+ "cursor": cursor,
93
+ "lanes": lanes,
94
+ "open_items": dict(Counter(item.kind for item in items)),
95
+ "claims": claim_count,
96
+ "overlaps": sum(item.kind in OVERLAP_KINDS for item in items),
97
+ "items": listed,
98
+ "left_out": len(items),
99
+ }
100
+ if trigger_counts:
101
+ board["triggers"] = trigger_counts
102
+ if github is not None:
103
+ board["open_prs"] = github.open_prs
104
+ board["github"] = {"available": github_error is None, "as_of": github.as_of}
105
+ if github_error is not None:
106
+ board["github"]["error"] = github_error
107
+ page: list[dict[str, Any]] = []
108
+ if lane_details:
109
+ board["lane_details"] = page
110
+ if lane_offset < len(lane_details):
111
+ board["next_lane_offset"] = lane_offset
112
+ if freeze_reason is not None:
113
+ board["freezes"] = {"deploy": {"reason": freeze_reason}}
114
+ if not _fits(board, budget):
115
+ board["freezes"] = {"deploy": {"reason_omitted": True}}
116
+ if not _fits(board, budget):
117
+ raise LedgerError("unavailable", "snapshot summary exceeds the configured token budget")
118
+ if lane_details:
119
+ for index, lane in enumerate(lane_details[lane_offset:], start=lane_offset):
120
+ board["next_lane_offset"] = index + 1
121
+ if not _add(board, page, lane.render(), budget) and not _add(
122
+ board, page, {"id": lane.id, "details_omitted": True}, budget
123
+ ):
124
+ if not page:
125
+ raise LedgerError(
126
+ "unavailable", "lane summary exceeds the configured token budget"
127
+ )
128
+ board["next_lane_offset"] = index
129
+ break
130
+ else:
131
+ board.pop("next_lane_offset", None)
132
+ for item in items:
133
+ board["left_out"] = len(items) - len(listed) - 1
134
+ if not _add(board, listed, asdict(item), budget):
135
+ board["left_out"] += 1
136
+ break
137
+ return board
138
+
139
+
140
+ def render_next(
141
+ cursor: int,
142
+ items: list[Item],
143
+ offset: int,
144
+ budget: int,
145
+ *,
146
+ has_more: bool = False,
147
+ ) -> dict[str, Any]:
148
+ return _render_page(
149
+ cursor,
150
+ ((asdict(item), {"id": item.id, "kind": item.kind}) for item in items),
151
+ "items",
152
+ offset,
153
+ budget,
154
+ has_more=has_more,
155
+ )
156
+
157
+
158
+ def render_claims(
159
+ cursor: int,
160
+ claims: list[Claim],
161
+ offset: int,
162
+ budget: int,
163
+ *,
164
+ has_more: bool,
165
+ ) -> dict[str, Any]:
166
+ return _render_page(
167
+ cursor,
168
+ ((claim.render(), {"offset": index}) for index, claim in enumerate(claims, offset)),
169
+ "claims",
170
+ offset,
171
+ budget,
172
+ has_more=has_more,
173
+ omit_after_rows=False,
174
+ )
175
+
176
+
177
+ def _render_page(
178
+ cursor: int,
179
+ rows: Iterable[tuple[dict[str, Any], dict[str, Any]]],
180
+ field: str,
181
+ offset: int,
182
+ budget: int,
183
+ *,
184
+ has_more: bool,
185
+ omit_after_rows: bool = True,
186
+ ) -> dict[str, Any]:
187
+ page: list[dict[str, Any]] = []
188
+ data: dict[str, Any] = {"cursor": cursor, field: page}
189
+ for index, (row, minimal) in enumerate(rows, start=offset):
190
+ data["next_offset"] = index + 1
191
+ if _add(data, page, row, budget):
192
+ continue
193
+ if (not page or omit_after_rows) and _add(
194
+ data, page, {**minimal, "details_omitted": True}, budget
195
+ ):
196
+ continue
197
+ if not page:
198
+ raise LedgerError("unavailable", "row identity exceeds the configured token budget")
199
+ data["next_offset"] = index
200
+ break
201
+ else:
202
+ if not has_more:
203
+ data.pop("next_offset", None)
204
+ return data
@@ -0,0 +1,89 @@
1
+ """Validation and dispatch for the closed set of local workflow actions."""
2
+
3
+ from dataclasses import dataclass
4
+ from typing import TYPE_CHECKING, Any, Literal, cast
5
+
6
+ from plotroom.core.actions import Action
7
+ from plotroom.core.checklists import ARCHIVE_PROPOSAL, propose_lane_archive
8
+ from plotroom.core.events import FieldSpec, LedgerError, validate_fields
9
+ from plotroom.core.lanes import mark_lane
10
+ from plotroom.core.next_items import ItemKind
11
+
12
+ if TYPE_CHECKING:
13
+ from collections.abc import Mapping
14
+
15
+ from plotroom.core.unit_of_work import UnitOfWork
16
+
17
+ NOTIFY = "notify_orchestrator"
18
+ SEND = "send"
19
+ MARK = "mark"
20
+ PROPOSE_ARCHIVE = "propose_archive"
21
+ NOTIFICATION_ITEM = ItemKind("trigger_notification", 20, False, ("Read the notification",))
22
+ ARCHIVE_ITEM = ItemKind(ARCHIVE_PROPOSAL, 30, True, ("Review the lane for archival",))
23
+ MARK_FIELDS = (FieldSpec("lane", "str"), FieldSpec("state", "str"))
24
+ MARK_STATES = frozenset({"active", "ready"})
25
+ _SCHEMAS = {
26
+ NOTIFY: (FieldSpec("text", "str"),),
27
+ SEND: (FieldSpec("worker", "str"), FieldSpec("text", "str")),
28
+ MARK: MARK_FIELDS,
29
+ PROPOSE_ARCHIVE: (FieldSpec("lane", "str"),),
30
+ }
31
+
32
+
33
+ @dataclass(frozen=True)
34
+ class DeliveryRoute:
35
+ channel: Literal["switchboard", "claude"]
36
+ destination: str
37
+
38
+
39
+ async def validate_action(
40
+ uow: UnitOfWork, action: dict[str, Any], routes: Mapping[str, DeliveryRoute]
41
+ ) -> tuple[str, dict[str, Any], str | None]:
42
+ if len(action) != 1:
43
+ raise LedgerError("invalid", "action must name exactly one registered action")
44
+ kind, payload = next(iter(action.items()))
45
+ schema = _SCHEMAS.get(kind)
46
+ if schema is None:
47
+ raise LedgerError("invalid", f"unknown action {kind!r}")
48
+ if not isinstance(payload, dict):
49
+ raise LedgerError("invalid", f"{kind} arguments must be an object")
50
+ args = cast("dict[str, Any]", payload)
51
+ validate_fields(schema, args, kind)
52
+ for value in args.values():
53
+ if not value.strip():
54
+ raise LedgerError("invalid", f"{kind} arguments must be nonempty")
55
+ try:
56
+ value.encode("utf-8")
57
+ except UnicodeError:
58
+ raise LedgerError("invalid", f"{kind} arguments must be valid UTF-8") from None
59
+ if kind == MARK and args["state"] not in MARK_STATES:
60
+ raise LedgerError("invalid", "mark state must be active or ready")
61
+ if "lane" in args:
62
+ lane = await uow.find_lane(args["lane"])
63
+ if lane is None or lane.state == "archived":
64
+ raise LedgerError("invalid", "action target must be a current lane")
65
+ return kind, args, lane.id
66
+ if kind == SEND:
67
+ worker = args["worker"]
68
+ if worker not in routes:
69
+ raise LedgerError("invalid", "send target must have a configured delivery channel")
70
+ lane = await uow.worker_lane(worker)
71
+ return kind, args, lane.id if lane else None
72
+ return kind, args, None
73
+
74
+
75
+ async def apply_local_action(uow: UnitOfWork, action: Action, repository: str | None) -> None:
76
+ """Apply an effect within the transaction that records its successful attempt."""
77
+ if action.kind == NOTIFY:
78
+ await uow.raise_item(
79
+ NOTIFICATION_ITEM.name,
80
+ action.key,
81
+ "orchestrator",
82
+ {"text": action.payload["text"], "action_key": action.key},
83
+ )
84
+ elif action.kind == MARK:
85
+ await mark_lane(uow, action, repository=repository)
86
+ elif action.kind == PROPOSE_ARCHIVE:
87
+ lane = await uow.find_lane(action.payload["lane"])
88
+ if lane is not None and lane.state != "archived":
89
+ await propose_lane_archive(uow, lane.id)