benchside 0.4.1__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 (178) hide show
  1. benchside/__init__.py +1 -0
  2. benchside/_content/app/SKILL.md +155 -0
  3. benchside/_content/app/references/app-api.md +261 -0
  4. benchside/_content/app/references/generic-agents.md +69 -0
  5. benchside/_content/app/references/metascience.md +346 -0
  6. benchside/_content/app/references/native-memory.md +429 -0
  7. benchside/_content/app/references/onboarding.md +313 -0
  8. benchside/_content/app/references/sharing.md +159 -0
  9. benchside/_content/app/scripts/benchside_app.py +311 -0
  10. benchside/_content/app-requirements.txt +197 -0
  11. benchside/_content/claude/bench-init/SKILL.md +37 -0
  12. benchside/_content/claude/bench-share/SKILL.md +44 -0
  13. benchside/_content/fixtures/.gitkeep +0 -0
  14. benchside/_content/legacy-skill-digests.json +26 -0
  15. benchside/_content/personas/journal-club.yaml +45 -0
  16. benchside/_content/personas/methods-clinic.yaml +49 -0
  17. benchside/_content/personas/peer-review.yaml +45 -0
  18. benchside/_content/personas/pi-hypothesis.yaml +121 -0
  19. benchside/_content/personas/pi.yaml +87 -0
  20. benchside/_content/personas/repro-check.yaml +45 -0
  21. benchside/_content/personas/results-meeting.yaml +46 -0
  22. benchside/_content/personas/study-design.yaml +50 -0
  23. benchside/_content/templates/app-role.md.tmpl +31 -0
  24. benchside/_content/templates/blocks/capture-contract.md +40 -0
  25. benchside/_content/templates/blocks/sovereignty.md +36 -0
  26. benchside/_content/templates/skill.md.tmpl +54 -0
  27. benchside/application/__init__.py +0 -0
  28. benchside/application/app.py +458 -0
  29. benchside/application/bridge.py +55 -0
  30. benchside/application/contributions.py +39 -0
  31. benchside/application/di.py +35 -0
  32. benchside/application/metascience.py +671 -0
  33. benchside/application/native.py +1088 -0
  34. benchside/application/packaging.py +19 -0
  35. benchside/application/sharing.py +439 -0
  36. benchside/application/submission_failures.py +28 -0
  37. benchside/cli/__init__.py +0 -0
  38. benchside/cli/hooks.py +159 -0
  39. benchside/cli/install.py +276 -0
  40. benchside/cli/journal.py +232 -0
  41. benchside/cli/main.py +761 -0
  42. benchside/cli/runtime.py +245 -0
  43. benchside/cli/sharing.py +66 -0
  44. benchside/cli/ui/__init__.py +5 -0
  45. benchside/cli/ui/_glyphs.py +21 -0
  46. benchside/cli/ui/_model.py +66 -0
  47. benchside/cli/ui/_renderer.py +110 -0
  48. benchside/cli/ui/_rich.py +75 -0
  49. benchside/config.py +122 -0
  50. benchside/domain/__init__.py +0 -0
  51. benchside/domain/authoring/__init__.py +0 -0
  52. benchside/domain/authoring/command/__init__.py +0 -0
  53. benchside/domain/authoring/command/compile.py +28 -0
  54. benchside/domain/authoring/model/__init__.py +0 -0
  55. benchside/domain/authoring/model/flow.py +27 -0
  56. benchside/domain/authoring/model/persona.py +56 -0
  57. benchside/domain/authoring/model/skill.py +32 -0
  58. benchside/domain/authoring/model/value.py +42 -0
  59. benchside/domain/authoring/port/__init__.py +5 -0
  60. benchside/domain/authoring/port/renderer.py +17 -0
  61. benchside/domain/authoring/port/repository.py +28 -0
  62. benchside/domain/authoring/port/skill_writer.py +19 -0
  63. benchside/domain/authoring/query/__init__.py +0 -0
  64. benchside/domain/authoring/query/get_persona.py +30 -0
  65. benchside/domain/authoring/query/list_personas.py +21 -0
  66. benchside/domain/authoring/service/__init__.py +0 -0
  67. benchside/domain/authoring/service/authoring.py +99 -0
  68. benchside/domain/authoring/util/__init__.py +0 -0
  69. benchside/domain/authoring/util/di/__init__.py +3 -0
  70. benchside/domain/authoring/util/di/provider.py +15 -0
  71. benchside/domain/consultation/__init__.py +0 -0
  72. benchside/domain/consultation/command/__init__.py +0 -0
  73. benchside/domain/consultation/command/capture_gap.py +28 -0
  74. benchside/domain/consultation/command/close.py +29 -0
  75. benchside/domain/consultation/command/open.py +36 -0
  76. benchside/domain/consultation/command/raise_critique.py +30 -0
  77. benchside/domain/consultation/command/record_decision.py +30 -0
  78. benchside/domain/consultation/command/resolve_critique.py +32 -0
  79. benchside/domain/consultation/command/state_hypothesis.py +28 -0
  80. benchside/domain/consultation/event/__init__.py +22 -0
  81. benchside/domain/consultation/event/closed.py +13 -0
  82. benchside/domain/consultation/event/critique_raised.py +12 -0
  83. benchside/domain/consultation/event/critique_resolved.py +12 -0
  84. benchside/domain/consultation/event/decision_recorded.py +13 -0
  85. benchside/domain/consultation/event/gap_observed.py +16 -0
  86. benchside/domain/consultation/event/hypothesis_stated.py +12 -0
  87. benchside/domain/consultation/event/opened.py +23 -0
  88. benchside/domain/consultation/event/question.py +16 -0
  89. benchside/domain/consultation/event/research_recorded.py +9 -0
  90. benchside/domain/consultation/model/__init__.py +0 -0
  91. benchside/domain/consultation/model/consultation.py +60 -0
  92. benchside/domain/consultation/model/project.py +78 -0
  93. benchside/domain/consultation/model/value.py +150 -0
  94. benchside/domain/consultation/port/__init__.py +6 -0
  95. benchside/domain/consultation/port/repository.py +32 -0
  96. benchside/domain/consultation/query/__init__.py +0 -0
  97. benchside/domain/consultation/query/get_consultation.py +28 -0
  98. benchside/domain/consultation/query/recall_project.py +97 -0
  99. benchside/domain/consultation/service/__init__.py +0 -0
  100. benchside/domain/consultation/service/consultation.py +373 -0
  101. benchside/domain/consultation/util/__init__.py +0 -0
  102. benchside/domain/consultation/util/di/__init__.py +3 -0
  103. benchside/domain/consultation/util/di/provider.py +33 -0
  104. benchside/domain/export/__init__.py +0 -0
  105. benchside/domain/export/command/__init__.py +0 -0
  106. benchside/domain/export/command/export_observations.py +31 -0
  107. benchside/domain/export/command/grant_consent.py +26 -0
  108. benchside/domain/export/command/revoke_consent.py +20 -0
  109. benchside/domain/export/event/__init__.py +19 -0
  110. benchside/domain/export/event/consent_granted.py +9 -0
  111. benchside/domain/export/event/consent_revoked.py +8 -0
  112. benchside/domain/export/event/observations_exported.py +12 -0
  113. benchside/domain/export/event/reporting.py +24 -0
  114. benchside/domain/export/model/__init__.py +0 -0
  115. benchside/domain/export/model/ledger.py +40 -0
  116. benchside/domain/export/model/metascience.py +418 -0
  117. benchside/domain/export/model/reporting.py +193 -0
  118. benchside/domain/export/model/value.py +79 -0
  119. benchside/domain/export/model/wire.py +104 -0
  120. benchside/domain/export/port/__init__.py +5 -0
  121. benchside/domain/export/port/redactor.py +27 -0
  122. benchside/domain/export/port/repository.py +25 -0
  123. benchside/domain/export/port/sink.py +19 -0
  124. benchside/domain/export/query/__init__.py +0 -0
  125. benchside/domain/export/query/consent_status.py +30 -0
  126. benchside/domain/export/query/preview_export.py +37 -0
  127. benchside/domain/export/service/__init__.py +0 -0
  128. benchside/domain/export/service/consent.py +53 -0
  129. benchside/domain/export/service/export.py +171 -0
  130. benchside/domain/export/service/reporting.py +281 -0
  131. benchside/domain/export/util/__init__.py +0 -0
  132. benchside/domain/export/util/di/__init__.py +3 -0
  133. benchside/domain/export/util/di/provider.py +22 -0
  134. benchside/domain/shared/__init__.py +0 -0
  135. benchside/domain/shared/command.py +44 -0
  136. benchside/domain/shared/error.py +51 -0
  137. benchside/domain/shared/event.py +46 -0
  138. benchside/domain/shared/model/__init__.py +31 -0
  139. benchside/domain/shared/model/aggregate.py +9 -0
  140. benchside/domain/shared/model/capture.py +33 -0
  141. benchside/domain/shared/model/entity.py +5 -0
  142. benchside/domain/shared/model/gap.py +89 -0
  143. benchside/domain/shared/model/ids.py +64 -0
  144. benchside/domain/shared/model/provenance.py +16 -0
  145. benchside/domain/shared/model/value.py +17 -0
  146. benchside/domain/shared/port/__init__.py +4 -0
  147. benchside/domain/shared/port/base.py +9 -0
  148. benchside/domain/shared/port/journal.py +36 -0
  149. benchside/domain/shared/query.py +42 -0
  150. benchside/domain/shared/service.py +31 -0
  151. benchside/infrastructure/__init__.py +9 -0
  152. benchside/infrastructure/content/__init__.py +0 -0
  153. benchside/infrastructure/content/loader.py +108 -0
  154. benchside/infrastructure/content/renderer.py +129 -0
  155. benchside/infrastructure/content/writer.py +26 -0
  156. benchside/infrastructure/files.py +43 -0
  157. benchside/infrastructure/http_sink.py +128 -0
  158. benchside/infrastructure/journal/__init__.py +0 -0
  159. benchside/infrastructure/journal/di.py +85 -0
  160. benchside/infrastructure/journal/projection.py +244 -0
  161. benchside/infrastructure/journal/registry.py +24 -0
  162. benchside/infrastructure/journal/store.py +134 -0
  163. benchside/infrastructure/placeholders.py +28 -0
  164. benchside/infrastructure/readable_records.py +211 -0
  165. benchside/infrastructure/readable_refresh.py +58 -0
  166. benchside/infrastructure/redaction.py +44 -0
  167. benchside/infrastructure/session_context.py +225 -0
  168. benchside/infrastructure/storage_layout.py +104 -0
  169. benchside/reporting.py +333 -0
  170. benchside/util/__init__.py +0 -0
  171. benchside/util/di/__init__.py +4 -0
  172. benchside/util/di/base.py +16 -0
  173. benchside/util/di/scope.py +18 -0
  174. benchside-0.4.1.dist-info/METADATA +97 -0
  175. benchside-0.4.1.dist-info/RECORD +178 -0
  176. benchside-0.4.1.dist-info/WHEEL +4 -0
  177. benchside-0.4.1.dist-info/entry_points.txt +2 -0
  178. benchside-0.4.1.dist-info/licenses/LICENSE +201 -0
@@ -0,0 +1,28 @@
1
+ from benchside.domain.consultation.service.consultation import ConsultationService
2
+ from benchside.domain.shared.command import Result
3
+ from benchside.domain.shared.model.ids import ConsultationId, PersonaId, ProjectId
4
+ from benchside.domain.shared.query import Query, QueryHandler
5
+
6
+
7
+ class GetConsultation(Query):
8
+ consultation_id: ConsultationId
9
+
10
+
11
+ class ConsultationDetail(Result):
12
+ consultation_id: ConsultationId
13
+ project_id: ProjectId
14
+ persona_id: PersonaId
15
+ state: str
16
+
17
+
18
+ class GetConsultationHandler(QueryHandler[GetConsultation, ConsultationDetail]):
19
+ consultation_service: ConsultationService
20
+
21
+ def run(self, query: GetConsultation) -> ConsultationDetail:
22
+ consultation = self.consultation_service.get(query.consultation_id)
23
+ return ConsultationDetail(
24
+ consultation_id=consultation.id,
25
+ project_id=consultation.project_id,
26
+ persona_id=consultation.persona_id,
27
+ state=consultation.state.kind,
28
+ )
@@ -0,0 +1,97 @@
1
+ """What a persona reads at the start of a sitting.
2
+
3
+ The cross-session continuity surface: computed from the project's accumulated
4
+ record, never re-summarised from conversation prose.
5
+ """
6
+
7
+ from typing import Literal
8
+
9
+ from pydantic import Field
10
+
11
+ from benchside.domain.consultation.model.value import (
12
+ Decision,
13
+ ResearchQuestion,
14
+ ResearchRecord,
15
+ Target,
16
+ )
17
+ from benchside.domain.consultation.service.consultation import ConsultationService
18
+ from benchside.domain.shared.command import Result
19
+ from benchside.domain.shared.error import NotFoundError
20
+ from benchside.domain.shared.model.ids import ProjectId
21
+ from benchside.domain.shared.query import Query, QueryHandler
22
+
23
+
24
+ class RecallProject(Query):
25
+ project_id: ProjectId
26
+ history: Literal["recent", "all"] = "recent"
27
+
28
+
29
+ class ProjectRecall(Result):
30
+ project_id: ProjectId
31
+ title: str
32
+ history: Literal["recent", "all"] = "recent"
33
+ total_decisions: int = 0
34
+ total_records: int = 0
35
+ total_questions: int = 0
36
+ open_research_questions: list[ResearchQuestion] = Field(default_factory=list)
37
+ recent_questions: list[ResearchQuestion] = Field(default_factory=list)
38
+ current_hypothesis: str | None = None
39
+ open_questions: list[str] = Field(default_factory=list)
40
+ recent_decisions: list[Decision] = Field(default_factory=list)
41
+ last_sitting_state: str | None = None
42
+ recent_records: list[ResearchRecord] = Field(default_factory=list)
43
+ open_sittings: list[dict[str, str]] = Field(default_factory=list)
44
+
45
+
46
+ class RecallProjectHandler(QueryHandler[RecallProject, ProjectRecall]):
47
+ consultation_service: ConsultationService
48
+
49
+ def run(self, query: RecallProject) -> ProjectRecall:
50
+ try:
51
+ project = self.consultation_service.get_project(query.project_id)
52
+ except NotFoundError:
53
+ # A fresh project has no journal yet. That is an empty recall,
54
+ # not an error -- SessionStart injection must survive it.
55
+ return ProjectRecall(
56
+ project_id=query.project_id, title=query.project_id.root, history=query.history
57
+ )
58
+ latest = self.consultation_service.latest_for_project(query.project_id)
59
+ current = project.current_hypothesis
60
+ return ProjectRecall(
61
+ project_id=project.id,
62
+ title=project.title,
63
+ history=query.history,
64
+ total_decisions=len(project.decisions),
65
+ total_records=len(project.research_records),
66
+ total_questions=len(project.questions),
67
+ open_research_questions=[q for q in project.questions if q.answer is None],
68
+ recent_questions=project.questions if query.history == "all" else project.questions[-5:],
69
+ current_hypothesis=current.statement if current else None,
70
+ open_questions=([q.question for q in project.questions if q.answer is None]
71
+ + [describe_target(c.target) for c in project.critiques if c.is_open]),
72
+ recent_decisions=project.decisions
73
+ if query.history == "all"
74
+ else project.decisions[-5:],
75
+ last_sitting_state=latest.state.kind if latest else None,
76
+ recent_records=(
77
+ project.research_records
78
+ if query.history == "all"
79
+ else project.research_records[-5:]
80
+ ),
81
+ open_sittings=[
82
+ {"id": c.id, "persona": c.persona_id.root}
83
+ for c in self.consultation_service.consultations.list_for_project(project.id)
84
+ if c.state.kind not in {"converged", "abandoned"}
85
+ ],
86
+ )
87
+
88
+
89
+ def describe_target(target: Target) -> str:
90
+ """Render a critique target as one line for the persona to read back."""
91
+ match target.kind:
92
+ case "claim":
93
+ return f"claim: {target.claim}; concern: {target.ground}"
94
+ case "method":
95
+ return f"method ({target.flaw}): {target.step}"
96
+ case _:
97
+ return f"interpretation: {target.result}; alternative: {target.alternative}"
File without changes
@@ -0,0 +1,373 @@
1
+ """Consultation business logic.
2
+
3
+ Every mutation follows the same shape: load the aggregates, let them enforce
4
+ the transition, append the corresponding event to the journal. The journal is
5
+ the store, so there is no separate save step.
6
+
7
+ Cross-aggregate coordination lives here: the Project owns the critique set,
8
+ the Consultation owns the state machine, and this service keeps them in step.
9
+ """
10
+
11
+ import logging
12
+ from datetime import UTC, datetime
13
+
14
+ from benchside.domain.consultation.event.closed import ConsultationClosed
15
+ from benchside.domain.consultation.event.critique_raised import CritiqueRaised
16
+ from benchside.domain.consultation.event.critique_resolved import CritiqueResolved
17
+ from benchside.domain.consultation.event.decision_recorded import DecisionRecorded
18
+ from benchside.domain.consultation.event.gap_observed import GapObserved
19
+ from benchside.domain.consultation.event.hypothesis_stated import HypothesisStated
20
+ from benchside.domain.consultation.event.opened import ConsultationOpened
21
+ from benchside.domain.consultation.event.question import QuestionAnswered, QuestionRecorded
22
+ from benchside.domain.consultation.event.research_recorded import ResearchRecorded
23
+ from benchside.domain.consultation.model.consultation import Consultation
24
+ from benchside.domain.consultation.model.project import Project
25
+ from benchside.domain.consultation.model.value import (
26
+ Critique,
27
+ Decision,
28
+ Hypothesis,
29
+ RecordKind,
30
+ ResearchQuestion,
31
+ ResearchRecord,
32
+ Resolution,
33
+ Target,
34
+ )
35
+ from benchside.domain.consultation.port.repository import (
36
+ ConsultationRepository,
37
+ ProjectRepository,
38
+ )
39
+ from benchside.domain.shared.error import InvalidStateError, NotFoundError
40
+ from benchside.domain.shared.event import new_event_id
41
+ from benchside.domain.shared.model.capture import CaptureKind, CloseRequirement
42
+ from benchside.domain.shared.model.gap import Gap
43
+ from benchside.domain.shared.model.ids import (
44
+ ConsultationId,
45
+ CritiqueId,
46
+ DecisionId,
47
+ HypothesisId,
48
+ ObservationId,
49
+ PersonaId,
50
+ ProjectId,
51
+ )
52
+ from benchside.domain.shared.port.journal import Journal
53
+ from benchside.domain.shared.service import Service
54
+
55
+ logger = logging.getLogger(__name__)
56
+
57
+
58
+ class ConsultationService(Service):
59
+ projects: ProjectRepository
60
+ consultations: ConsultationRepository
61
+ journal: Journal
62
+
63
+ def open(
64
+ self,
65
+ *,
66
+ consultation_id: ConsultationId,
67
+ project_id: ProjectId,
68
+ persona_id: PersonaId,
69
+ close_requires: tuple[CloseRequirement, ...] = (),
70
+ critique_requires_hypothesis: bool = True,
71
+ record_kind: RecordKind | None = None,
72
+ persona_version: str = "1",
73
+ host: str = "unknown",
74
+ app_version: str = "unknown",
75
+ surface: str = "unknown",
76
+ ) -> Consultation:
77
+ if self.consultations.get(consultation_id) is not None:
78
+ raise InvalidStateError(f"Consultation already exists: {consultation_id}")
79
+ consultation = Consultation(
80
+ id=consultation_id,
81
+ project_id=project_id,
82
+ persona_id=persona_id,
83
+ close_requires=close_requires,
84
+ critique_requires_hypothesis=critique_requires_hypothesis,
85
+ record_kind=record_kind,
86
+ persona_version=persona_version,
87
+ host=host,
88
+ app_version=app_version,
89
+ surface=surface,
90
+ )
91
+ self.journal.append(
92
+ project_id,
93
+ ConsultationOpened(
94
+ id=new_event_id(),
95
+ consultation_id=consultation_id,
96
+ project_id=project_id,
97
+ persona_id=persona_id,
98
+ close_requires=close_requires,
99
+ critique_requires_hypothesis=critique_requires_hypothesis,
100
+ record_kind=record_kind,
101
+ persona_version=persona_version,
102
+ host=host,
103
+ app_version=app_version,
104
+ surface=surface,
105
+ ),
106
+ )
107
+ return consultation
108
+
109
+ def state_hypothesis(
110
+ self,
111
+ *,
112
+ consultation_id: ConsultationId,
113
+ hypothesis_id: HypothesisId,
114
+ statement: str,
115
+ supersedes: HypothesisId | None = None,
116
+ ) -> Project:
117
+ consultation, project = self._load_pair(consultation_id)
118
+ project.state_hypothesis(
119
+ Hypothesis(id=hypothesis_id, statement=statement, supersedes=supersedes)
120
+ )
121
+ consultation.probe()
122
+ self.journal.append(
123
+ project.id,
124
+ HypothesisStated(
125
+ id=new_event_id(),
126
+ project_id=project.id,
127
+ consultation_id=consultation_id,
128
+ hypothesis_id=hypothesis_id,
129
+ statement=statement,
130
+ supersedes=supersedes,
131
+ ),
132
+ )
133
+ return project
134
+
135
+ def raise_critique(
136
+ self,
137
+ *,
138
+ consultation_id: ConsultationId,
139
+ critique_id: CritiqueId,
140
+ target: Target,
141
+ ) -> Project:
142
+ consultation, project = self._load_pair(consultation_id)
143
+ project.raise_critique(
144
+ Critique(id=critique_id, target=target),
145
+ requires_hypothesis=consultation.critique_requires_hypothesis,
146
+ )
147
+ consultation.settle(project.open_critiques)
148
+ self.journal.append(
149
+ project.id,
150
+ CritiqueRaised(
151
+ id=new_event_id(),
152
+ project_id=project.id,
153
+ consultation_id=consultation_id,
154
+ critique_id=critique_id,
155
+ target=target,
156
+ ),
157
+ )
158
+ return project
159
+
160
+ def resolve_critique(
161
+ self,
162
+ *,
163
+ consultation_id: ConsultationId,
164
+ critique_id: CritiqueId,
165
+ resolution: Resolution,
166
+ ) -> Project:
167
+ consultation, project = self._load_pair(consultation_id)
168
+ project.resolve_critique(critique_id, resolution)
169
+ consultation.settle(project.open_critiques)
170
+ self.journal.append(
171
+ project.id,
172
+ CritiqueResolved(
173
+ id=new_event_id(),
174
+ project_id=project.id,
175
+ consultation_id=consultation_id,
176
+ critique_id=critique_id,
177
+ resolution=resolution,
178
+ ),
179
+ )
180
+ return project
181
+
182
+ def record_decision(
183
+ self,
184
+ *,
185
+ consultation_id: ConsultationId,
186
+ decision_id: DecisionId,
187
+ decision: str,
188
+ rationale: str,
189
+ supersedes: DecisionId | None = None,
190
+ ) -> Project:
191
+ consultation, project = self._load_pair(consultation_id)
192
+ consultation._require_live()
193
+ project.record_decision(
194
+ Decision(
195
+ id=decision_id,
196
+ decision=decision,
197
+ rationale=rationale,
198
+ decided_at=datetime.now(UTC),
199
+ supersedes=supersedes,
200
+ )
201
+ )
202
+ self.journal.append(
203
+ project.id,
204
+ DecisionRecorded(
205
+ id=new_event_id(),
206
+ project_id=project.id,
207
+ consultation_id=consultation_id,
208
+ decision_id=decision_id,
209
+ decision=decision,
210
+ rationale=rationale,
211
+ supersedes=supersedes,
212
+ ),
213
+ )
214
+ return project
215
+
216
+ def capture_gap(
217
+ self,
218
+ *,
219
+ consultation_id: ConsultationId,
220
+ observation_id: ObservationId,
221
+ gap: Gap,
222
+ ) -> None:
223
+ """Record where the workflow broke down.
224
+
225
+ Journal-only: a gap is not part of the Project's scientific record and
226
+ does not touch its invariants. The export domain reads these.
227
+ """
228
+ consultation = self._load_consultation(consultation_id)
229
+ consultation._require_live()
230
+ self.journal.append(
231
+ consultation.project_id,
232
+ GapObserved(
233
+ id=new_event_id(),
234
+ project_id=consultation.project_id,
235
+ consultation_id=consultation_id,
236
+ observation_id=observation_id,
237
+ gap=gap,
238
+ ),
239
+ )
240
+
241
+ def record_research(self, *, consultation_id: ConsultationId, record: ResearchRecord) -> None:
242
+ consultation, project = self._load_pair(consultation_id)
243
+ consultation._require_live()
244
+ if consultation.record_kind != record.kind:
245
+ raise InvalidStateError(
246
+ f"This role records {consultation.record_kind}, not {record.kind}"
247
+ )
248
+ if record.kind == "literature" and not record.sources:
249
+ raise InvalidStateError("A literature record needs a supplied or retrieved source")
250
+ if any(r.id == record.id for r in project.research_records):
251
+ raise InvalidStateError(f"Research record already exists: {record.id}")
252
+ self.journal.append(
253
+ consultation.project_id,
254
+ ResearchRecorded(
255
+ id=new_event_id(),
256
+ project_id=consultation.project_id,
257
+ consultation_id=consultation_id,
258
+ record=record,
259
+ ),
260
+ )
261
+
262
+ def record_question(self, *, consultation_id: ConsultationId, question: ResearchQuestion) -> None:
263
+ consultation, project = self._load_pair(consultation_id)
264
+ consultation._require_live()
265
+ if question.answer is not None:
266
+ raise InvalidStateError("Record a question first, then answer it")
267
+ if any(q.id == question.id for q in project.questions):
268
+ raise InvalidStateError(f"Question already exists: {question.id}")
269
+ self.journal.append(project.id, QuestionRecorded(
270
+ id=new_event_id(), project_id=project.id,
271
+ consultation_id=consultation_id, question=question,
272
+ ))
273
+
274
+ def answer_question(self, *, consultation_id: ConsultationId, question_id: str, answer: str) -> None:
275
+ consultation, project = self._load_pair(consultation_id)
276
+ consultation._require_live()
277
+ question = next((q for q in project.questions if q.id == question_id), None)
278
+ if question is None:
279
+ raise NotFoundError(f"Question not found: {question_id}")
280
+ if question.answer is not None:
281
+ raise InvalidStateError(f"Question already answered: {question_id}")
282
+ self.journal.append(project.id, QuestionAnswered(
283
+ id=new_event_id(), project_id=project.id,
284
+ consultation_id=consultation_id, question_id=question_id, answer=answer,
285
+ ))
286
+
287
+ def close(
288
+ self,
289
+ *,
290
+ consultation_id: ConsultationId,
291
+ converged: bool,
292
+ detail: str,
293
+ ) -> Consultation:
294
+ consultation = self._load_consultation(consultation_id)
295
+ if converged:
296
+ # A different sitting may have raised a concern since this one last wrote.
297
+ consultation.settle(self.get_project(consultation.project_id).open_critiques)
298
+ self._enforce_close_requirements(consultation)
299
+ consultation.converge(detail)
300
+ else:
301
+ consultation.abandon(detail)
302
+ self.journal.append(
303
+ consultation.project_id,
304
+ ConsultationClosed(
305
+ id=new_event_id(),
306
+ project_id=consultation.project_id,
307
+ consultation_id=consultation_id,
308
+ state="converged" if converged else "abandoned",
309
+ detail=detail,
310
+ ),
311
+ )
312
+ return consultation
313
+
314
+ def get(self, consultation_id: ConsultationId) -> Consultation:
315
+ return self._load_consultation(consultation_id)
316
+
317
+ def get_project(self, project_id: ProjectId) -> Project:
318
+ project = self.projects.get(project_id)
319
+ if project is None:
320
+ raise NotFoundError(f"Project not found: {project_id}")
321
+ return project
322
+
323
+ def latest_for_project(self, project_id: ProjectId) -> Consultation | None:
324
+ return self.consultations.latest_for_project(project_id)
325
+
326
+ def _load_consultation(self, consultation_id: ConsultationId) -> Consultation:
327
+ consultation = self.consultations.get(consultation_id)
328
+ if consultation is None:
329
+ raise NotFoundError(f"Consultation not found: {consultation_id}")
330
+ return consultation
331
+
332
+ def _enforce_close_requirements(self, consultation: Consultation) -> None:
333
+ """A converged close must meet the requirements snapshotted at open.
334
+
335
+ Counts are derived from the sitting's own journal events, so this is
336
+ the mechanism that prevents empty journals: you cannot cleanly finish
337
+ a sitting you never recorded.
338
+ """
339
+ if not consultation.close_requires:
340
+ return
341
+ counts = self.capture_counts(consultation)
342
+ missing = [
343
+ f"{req.kind.value} (have {counts.get(req.kind, 0)}, need {req.minimum})"
344
+ for req in consultation.close_requires
345
+ if counts.get(req.kind, 0) < req.minimum
346
+ ]
347
+ if missing:
348
+ raise InvalidStateError(
349
+ "Cannot close converged; requirements unmet: " + ", ".join(missing)
350
+ )
351
+
352
+ def capture_counts(self, consultation: Consultation) -> dict[CaptureKind, int]:
353
+ """What this sitting has recorded, by capture kind (drives status/close)."""
354
+ counts: dict[CaptureKind, int] = {}
355
+ kind_by_event = {
356
+ HypothesisStated: CaptureKind.HYPOTHESIS,
357
+ CritiqueRaised: CaptureKind.CRITIQUE,
358
+ CritiqueResolved: CaptureKind.RESOLUTION,
359
+ DecisionRecorded: CaptureKind.DECISION,
360
+ GapObserved: CaptureKind.GAP,
361
+ ResearchRecorded: CaptureKind.RECORD,
362
+ QuestionRecorded: CaptureKind.QUESTION,
363
+ QuestionAnswered: CaptureKind.ANSWER,
364
+ }
365
+ for event in self.journal.read(consultation.project_id):
366
+ kind = kind_by_event.get(type(event))
367
+ if kind is not None and event.consultation_id == consultation.id:
368
+ counts[kind] = counts.get(kind, 0) + 1
369
+ return counts
370
+
371
+ def _load_pair(self, consultation_id: ConsultationId) -> tuple[Consultation, Project]:
372
+ consultation = self._load_consultation(consultation_id)
373
+ return consultation, self.get_project(consultation.project_id)
File without changes
@@ -0,0 +1,3 @@
1
+ from benchside.domain.consultation.util.di.provider import ConsultationProvider
2
+
3
+ __all__ = ["ConsultationProvider"]
@@ -0,0 +1,33 @@
1
+ """Consultation DI wiring: this domain's service and handlers.
2
+
3
+ Ports are bound to implementations in benchside/infrastructure/, not here.
4
+ """
5
+
6
+ from dishka import provide
7
+
8
+ from benchside.domain.consultation.command.capture_gap import CaptureGapHandler
9
+ from benchside.domain.consultation.command.close import CloseConsultationHandler
10
+ from benchside.domain.consultation.command.open import OpenConsultationHandler
11
+ from benchside.domain.consultation.command.raise_critique import RaiseCritiqueHandler
12
+ from benchside.domain.consultation.command.record_decision import RecordDecisionHandler
13
+ from benchside.domain.consultation.command.resolve_critique import ResolveCritiqueHandler
14
+ from benchside.domain.consultation.command.state_hypothesis import StateHypothesisHandler
15
+ from benchside.domain.consultation.query.get_consultation import GetConsultationHandler
16
+ from benchside.domain.consultation.query.recall_project import RecallProjectHandler
17
+ from benchside.domain.consultation.service.consultation import ConsultationService
18
+ from benchside.util.di import Provider, Scope
19
+
20
+
21
+ class ConsultationProvider(Provider):
22
+ service = provide(ConsultationService, scope=Scope.UOW)
23
+
24
+ open_handler = provide(OpenConsultationHandler, scope=Scope.UOW)
25
+ state_hypothesis_handler = provide(StateHypothesisHandler, scope=Scope.UOW)
26
+ raise_critique_handler = provide(RaiseCritiqueHandler, scope=Scope.UOW)
27
+ resolve_critique_handler = provide(ResolveCritiqueHandler, scope=Scope.UOW)
28
+ record_decision_handler = provide(RecordDecisionHandler, scope=Scope.UOW)
29
+ capture_gap_handler = provide(CaptureGapHandler, scope=Scope.UOW)
30
+ close_handler = provide(CloseConsultationHandler, scope=Scope.UOW)
31
+
32
+ get_consultation_handler = provide(GetConsultationHandler, scope=Scope.UOW)
33
+ recall_project_handler = provide(RecallProjectHandler, scope=Scope.UOW)
File without changes
File without changes
@@ -0,0 +1,31 @@
1
+ from pydantic import Field
2
+
3
+ from benchside.domain.export.model.value import ExportPreview
4
+ from benchside.domain.export.service.export import ExportService
5
+ from benchside.domain.shared.command import Command, CommandHandler, Result
6
+ from benchside.domain.shared.model.ids import GrantId, ObservationId
7
+
8
+
9
+ class ExportObservations(Command):
10
+ preview: ExportPreview
11
+ approved_sha256: str
12
+
13
+
14
+ class ObservationsSent(Result):
15
+ grant_id: GrantId
16
+ observation_ids: list[ObservationId] = Field(default_factory=list)
17
+ payload_sha256: str
18
+
19
+
20
+ class ExportObservationsHandler(CommandHandler[ExportObservations, ObservationsSent]):
21
+ export_service: ExportService
22
+
23
+ def run(self, cmd: ExportObservations) -> ObservationsSent:
24
+ record = self.export_service.export(
25
+ preview=cmd.preview, approved_sha256=cmd.approved_sha256
26
+ )
27
+ return ObservationsSent(
28
+ grant_id=record.grant_id,
29
+ observation_ids=list(record.observation_ids),
30
+ payload_sha256=record.payload_sha256,
31
+ )
@@ -0,0 +1,26 @@
1
+ from benchside.domain.export.service.consent import ConsentService
2
+ from benchside.domain.shared.command import Command, CommandHandler, Result
3
+ from benchside.domain.shared.model.ids import GrantId
4
+
5
+
6
+ class GrantConsent(Command):
7
+ grant_id: GrantId
8
+ gap_kinds: frozenset[str]
9
+
10
+
11
+ class ConsentRecorded(Result):
12
+ grant_id: GrantId
13
+ gap_kinds: frozenset[str]
14
+ status: str
15
+
16
+
17
+ class GrantConsentHandler(CommandHandler[GrantConsent, ConsentRecorded]):
18
+ consent_service: ConsentService
19
+
20
+ def run(self, cmd: GrantConsent) -> ConsentRecorded:
21
+ grant = self.consent_service.grant(grant_id=cmd.grant_id, gap_kinds=cmd.gap_kinds)
22
+ return ConsentRecorded(
23
+ grant_id=grant.id,
24
+ gap_kinds=grant.gap_kinds,
25
+ status=grant.status,
26
+ )
@@ -0,0 +1,20 @@
1
+ from benchside.domain.export.service.consent import ConsentService
2
+ from benchside.domain.shared.command import Command, CommandHandler, Result
3
+ from benchside.domain.shared.model.ids import GrantId
4
+
5
+
6
+ class RevokeConsent(Command):
7
+ pass
8
+
9
+
10
+ class ConsentRevocation(Result):
11
+ grant_id: GrantId
12
+ status: str
13
+
14
+
15
+ class RevokeConsentHandler(CommandHandler[RevokeConsent, ConsentRevocation]):
16
+ consent_service: ConsentService
17
+
18
+ def run(self, cmd: RevokeConsent) -> ConsentRevocation:
19
+ revoked = self.consent_service.revoke()
20
+ return ConsentRevocation(grant_id=revoked.id, status=revoked.status)
@@ -0,0 +1,19 @@
1
+ from benchside.domain.export.event.consent_granted import ConsentGranted
2
+ from benchside.domain.export.event.consent_revoked import ConsentRevoked
3
+ from benchside.domain.export.event.observations_exported import ObservationsExported
4
+ from benchside.domain.export.event.reporting import (
5
+ FeedbackRecorded,
6
+ ReportingConsentChanged,
7
+ ReportSubmitted,
8
+ UsageFailureRecorded,
9
+ )
10
+
11
+ __all__ = [
12
+ "ConsentGranted",
13
+ "ConsentRevoked",
14
+ "FeedbackRecorded",
15
+ "ObservationsExported",
16
+ "ReportSubmitted",
17
+ "ReportingConsentChanged",
18
+ "UsageFailureRecorded",
19
+ ]
@@ -0,0 +1,9 @@
1
+ from benchside.domain.shared.event import Event
2
+ from benchside.domain.shared.model.ids import GrantId
3
+
4
+
5
+ class ConsentGranted(Event):
6
+ """The scientist authorised sharing for the listed gap kinds."""
7
+
8
+ grant_id: GrantId
9
+ gap_kinds: frozenset[str]
@@ -0,0 +1,8 @@
1
+ from benchside.domain.shared.event import Event
2
+ from benchside.domain.shared.model.ids import GrantId
3
+
4
+
5
+ class ConsentRevoked(Event):
6
+ """Sharing was switched off. Prior exports remain in the ledger."""
7
+
8
+ grant_id: GrantId
@@ -0,0 +1,12 @@
1
+ from benchside.domain.shared.event import Event
2
+ from benchside.domain.shared.model.ids import GrantId, ObservationId
3
+
4
+
5
+ class ObservationsExported(Event):
6
+ """Proof of what left the machine. Content-free: ids and a digest only."""
7
+
8
+ grant_id: GrantId
9
+ observation_ids: tuple[ObservationId, ...]
10
+ payload_sha256: str
11
+ destination: str = ""
12
+ received_at: str | None = None