execweave 1.0.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 (234) hide show
  1. execweave/__init__.py +3 -0
  2. execweave/__main__.py +5 -0
  3. execweave/_conversation_records_core.py +902 -0
  4. execweave/_dashboard_shell_base.py +148 -0
  5. execweave/_http_proxy_base.py +565 -0
  6. execweave/_http_proxy_bounded.py +560 -0
  7. execweave/_http_proxy_stage.py +509 -0
  8. execweave/acceptance_output.py +19 -0
  9. execweave/agent_bootstrap.py +395 -0
  10. execweave/agent_topology.py +313 -0
  11. execweave/agent_trace.py +1162 -0
  12. execweave/agy_preview_sanitize.py +102 -0
  13. execweave/analysis.py +427 -0
  14. execweave/anthropic.py +228 -0
  15. execweave/anthropic_cli.py +171 -0
  16. execweave/anthropic_full_fidelity.py +385 -0
  17. execweave/antigravity_adapter.py +112 -0
  18. execweave/antigravity_adapter_base.py +479 -0
  19. execweave/antigravity_full_fidelity.py +656 -0
  20. execweave/antigravity_full_fidelity_base.py +172 -0
  21. execweave/antigravity_full_fidelity_collaboration_base.py +451 -0
  22. execweave/antigravity_hook_cli.py +209 -0
  23. execweave/antigravity_record.py +96 -0
  24. execweave/antigravity_subagent_linkage.py +449 -0
  25. execweave/antigravity_trace_capability.py +39 -0
  26. execweave/auto_specialized.py +547 -0
  27. execweave/backends.py +102 -0
  28. execweave/benchmark.py +80 -0
  29. execweave/claude_adapter.py +517 -0
  30. execweave/claude_child_transcript.py +314 -0
  31. execweave/claude_delegation.py +136 -0
  32. execweave/claude_full_fidelity.py +396 -0
  33. execweave/claude_hook_cli.py +209 -0
  34. execweave/claude_hook_contract.py +943 -0
  35. execweave/claude_model_observer.py +353 -0
  36. execweave/claude_record.py +106 -0
  37. execweave/cli.py +629 -0
  38. execweave/codex_adapter.py +455 -0
  39. execweave/codex_conversation.py +621 -0
  40. execweave/codex_full_fidelity.py +287 -0
  41. execweave/codex_hook_cli.py +301 -0
  42. execweave/codex_hook_entry.py +207 -0
  43. execweave/codex_hook_lifecycle.py +363 -0
  44. execweave/codex_message_diagnostics.py +322 -0
  45. execweave/codex_message_transport_diagnostics.py +399 -0
  46. execweave/codex_record.py +199 -0
  47. execweave/codex_rollout_structures.py +402 -0
  48. execweave/codex_rollout_trace.py +56 -0
  49. execweave/codex_rollout_trace_base.py +1509 -0
  50. execweave/collector.py +768 -0
  51. execweave/command.py +157 -0
  52. execweave/command_privacy.py +20 -0
  53. execweave/content_evidence.py +101 -0
  54. execweave/content_integrity.py +289 -0
  55. execweave/content_store.py +273 -0
  56. execweave/controlled_checks.py +561 -0
  57. execweave/conversation_archive.py +338 -0
  58. execweave/conversation_message_identity.py +264 -0
  59. execweave/conversation_preview.py +66 -0
  60. execweave/conversation_preview_antigravity.py +32 -0
  61. execweave/conversation_preview_claude.py +31 -0
  62. execweave/conversation_preview_codex.py +107 -0
  63. execweave/conversation_preview_common.py +180 -0
  64. execweave/conversation_preview_generic.py +188 -0
  65. execweave/conversation_preview_lines.py +133 -0
  66. execweave/conversation_preview_transcript.py +121 -0
  67. execweave/conversation_records.py +349 -0
  68. execweave/conversation_records_antigravity.py +370 -0
  69. execweave/conversation_records_codex.py +83 -0
  70. execweave/conversation_records_common.py +17 -0
  71. execweave/conversation_records_ollama.py +39 -0
  72. execweave/correlation.py +818 -0
  73. execweave/cursor_adapter.py +349 -0
  74. execweave/cursor_delegation.py +87 -0
  75. execweave/cursor_delegation_base.py +220 -0
  76. execweave/cursor_full_fidelity.py +489 -0
  77. execweave/cursor_hook_cli.py +195 -0
  78. execweave/cursor_hook_contract.py +439 -0
  79. execweave/cursor_lifecycle.py +159 -0
  80. execweave/cursor_record.py +96 -0
  81. execweave/dashboard_shell.py +283 -0
  82. execweave/delivery_status.py +94 -0
  83. execweave/entry.py +98 -0
  84. execweave/evidence_availability.py +113 -0
  85. execweave/evidence_grade.py +166 -0
  86. execweave/fidelity.py +304 -0
  87. execweave/filesystem.py +327 -0
  88. execweave/finalization.py +92 -0
  89. execweave/focus.py +118 -0
  90. execweave/framework_adapters/__init__.py +34 -0
  91. execweave/framework_adapters/autogen.py +398 -0
  92. execweave/framework_adapters/base.py +580 -0
  93. execweave/framework_adapters/camel.py +270 -0
  94. execweave/framework_adapters/camel_channel.py +71 -0
  95. execweave/framework_adapters/metagpt.py +315 -0
  96. execweave/framework_adapters/metagpt_ollama_compat.py +54 -0
  97. execweave/framework_adapters/metagpt_recording.py +342 -0
  98. execweave/framework_adapters/registry.py +90 -0
  99. execweave/graph.py +590 -0
  100. execweave/graph_ops.py +472 -0
  101. execweave/hook_command.py +32 -0
  102. execweave/hook_delivery.py +32 -0
  103. execweave/hook_input.py +16 -0
  104. execweave/http_proxy.py +187 -0
  105. execweave/http_proxy_cli.py +84 -0
  106. execweave/inference_gateway.py +476 -0
  107. execweave/inference_gateway_cli.py +211 -0
  108. execweave/inference_gateway_full_fidelity.py +613 -0
  109. execweave/inference_identity.py +76 -0
  110. execweave/inference_identity_cli.py +60 -0
  111. execweave/integrity.py +235 -0
  112. execweave/integrity_cli.py +55 -0
  113. execweave/investigation_index.py +445 -0
  114. execweave/litellm_callback.py +211 -0
  115. execweave/litellm_callback_cli.py +42 -0
  116. execweave/live.py +445 -0
  117. execweave/live_core.py +972 -0
  118. execweave/live_view.py +109 -0
  119. execweave/live_view_extra_style.py +11 -0
  120. execweave/live_view_markup.py +29 -0
  121. execweave/live_view_process_layout.py +650 -0
  122. execweave/live_view_readability.py +502 -0
  123. execweave/live_view_script_a.py +59 -0
  124. execweave/live_view_script_b.py +97 -0
  125. execweave/live_view_script_c.py +59 -0
  126. execweave/live_view_script_d.py +40 -0
  127. execweave/live_view_style.py +34 -0
  128. execweave/model_runtime.py +560 -0
  129. execweave/model_runtime_cli.py +265 -0
  130. execweave/model_runtime_full_fidelity.py +519 -0
  131. execweave/observation.py +53 -0
  132. execweave/observation_demo.py +69 -0
  133. execweave/openai_agents.py +267 -0
  134. execweave/openai_agents_sdk.py +453 -0
  135. execweave/openai_compatible.py +239 -0
  136. execweave/openai_compatible_cli.py +189 -0
  137. execweave/openai_compatible_full_fidelity.py +438 -0
  138. execweave/opencode_adapter.py +325 -0
  139. execweave/opencode_event_contract.py +734 -0
  140. execweave/opencode_full_fidelity.py +198 -0
  141. execweave/opencode_hook_cli.py +119 -0
  142. execweave/opencode_plugin_cli.py +150 -0
  143. execweave/opencode_record.py +96 -0
  144. execweave/opencode_task_linkage.py +225 -0
  145. execweave/overhead_benchmark.py +440 -0
  146. execweave/privacy.py +84 -0
  147. execweave/private_io.py +465 -0
  148. execweave/provider_capability.py +578 -0
  149. execweave/provider_lifecycle.py +282 -0
  150. execweave/provider_record.py +290 -0
  151. execweave/reader_identity.py +13 -0
  152. execweave/redacted_archive.py +906 -0
  153. execweave/risk.py +106 -0
  154. execweave/rule_pack.py +359 -0
  155. execweave/rule_pack_cli.py +57 -0
  156. execweave/run_assessment.py +200 -0
  157. execweave/runtime_endpoint_availability.py +44 -0
  158. execweave/scalability_benchmark.py +195 -0
  159. execweave/schema.py +62 -0
  160. execweave/scope.py +85 -0
  161. execweave/semantic.py +453 -0
  162. execweave/session_summary.py +159 -0
  163. execweave/sink.py +71 -0
  164. execweave/strace_backend.py +927 -0
  165. execweave/stream_assembly.py +363 -0
  166. execweave/task_artifacts.py +192 -0
  167. execweave/task_validation.py +317 -0
  168. execweave/theme.py +40 -0
  169. execweave/top.py +599 -0
  170. execweave/top_cli.py +150 -0
  171. execweave/validate.py +196 -0
  172. execweave/vendor/dagre.min.js +815 -0
  173. execweave/view_cli.py +49 -0
  174. execweave/viewer.py +474 -0
  175. execweave/viewer_agent_panel.py +630 -0
  176. execweave/viewer_agent_panel_antigravity.py +31 -0
  177. execweave/viewer_agent_panel_claude.py +9 -0
  178. execweave/viewer_agent_panel_codex.py +50 -0
  179. execweave/viewer_agent_panel_cursor.py +9 -0
  180. execweave/viewer_agent_panel_default.py +79 -0
  181. execweave/viewer_agent_panel_ollama.py +20 -0
  182. execweave/viewer_agent_panel_opencode.py +9 -0
  183. execweave/viewer_agent_panel_registry.py +20 -0
  184. execweave/viewer_archive_verifier.py +178 -0
  185. execweave/viewer_background_files.py +86 -0
  186. execweave/viewer_content_browser.py +307 -0
  187. execweave/viewer_content_health.py +208 -0
  188. execweave/viewer_content_inspector.py +309 -0
  189. execweave/viewer_controlled_checks.py +258 -0
  190. execweave/viewer_conversation_sync.py +115 -0
  191. execweave/viewer_dashboard_clean.py +405 -0
  192. execweave/viewer_dashboard_focus.py +127 -0
  193. execweave/viewer_dashboard_hardening.py +264 -0
  194. execweave/viewer_dashboard_hardening_v2.py +62 -0
  195. execweave/viewer_dashboard_projection.py +261 -0
  196. execweave/viewer_delivery_status.py +155 -0
  197. execweave/viewer_edge_decross.py +117 -0
  198. execweave/viewer_execution_flow.py +611 -0
  199. execweave/viewer_execution_flow_provider_normalization.py +107 -0
  200. execweave/viewer_execution_flow_safety.py +349 -0
  201. execweave/viewer_external_endpoints.py +307 -0
  202. execweave/viewer_flow_canvas.py +1140 -0
  203. execweave/viewer_flow_layout.py +922 -0
  204. execweave/viewer_fold_state.py +31 -0
  205. execweave/viewer_framework_flow.py +68 -0
  206. execweave/viewer_framework_model_output.py +133 -0
  207. execweave/viewer_gif.py +278 -0
  208. execweave/viewer_history_browser.py +225 -0
  209. execweave/viewer_investigation.py +268 -0
  210. execweave/viewer_layout_geometry.py +192 -0
  211. execweave/viewer_layout_v2.py +429 -0
  212. execweave/viewer_layout_v2_detached.py +25 -0
  213. execweave/viewer_limits.py +93 -0
  214. execweave/viewer_live_layout.py +61 -0
  215. execweave/viewer_message_handoffs.py +114 -0
  216. execweave/viewer_projection.py +488 -0
  217. execweave/viewer_projection_base.py +522 -0
  218. execweave/viewer_projection_bridges.py +214 -0
  219. execweave/viewer_recorded_sources.py +146 -0
  220. execweave/viewer_run_assessment.py +274 -0
  221. execweave/viewer_run_guide.py +175 -0
  222. execweave/viewer_runtime_evidence.py +258 -0
  223. execweave/viewer_semantic_projection.py +641 -0
  224. execweave/viewer_session_flow.py +491 -0
  225. execweave/viewer_structure_share.py +132 -0
  226. execweave/viewer_task_validation.py +241 -0
  227. execweave/viewer_tool_mirror_identity.py +75 -0
  228. execweave/viewer_workflow_mode.py +127 -0
  229. execweave/workflow.py +183 -0
  230. execweave-1.0.0.dist-info/METADATA +462 -0
  231. execweave-1.0.0.dist-info/RECORD +234 -0
  232. execweave-1.0.0.dist-info/WHEEL +4 -0
  233. execweave-1.0.0.dist-info/entry_points.txt +30 -0
  234. execweave-1.0.0.dist-info/licenses/LICENSE +16 -0
@@ -0,0 +1,734 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Any
7
+ from .agent_topology import root_topology
8
+
9
+ OPENCODE_EVENTS_REFERENCE = "https://opencode.ai/docs/plugins/"
10
+
11
+ OFFICIAL_OPENCODE_BUS_EVENTS = frozenset(
12
+ {
13
+ "command.executed",
14
+ "file.edited",
15
+ "file.watcher.updated",
16
+ "installation.updated",
17
+ "lsp.client.diagnostics",
18
+ "lsp.updated",
19
+ "message.part.removed",
20
+ "message.part.updated",
21
+ "message.removed",
22
+ "message.updated",
23
+ "permission.asked",
24
+ "permission.replied",
25
+ "server.connected",
26
+ "session.compacted",
27
+ "session.created",
28
+ "session.deleted",
29
+ "session.diff",
30
+ "session.error",
31
+ "session.idle",
32
+ "session.status",
33
+ "session.updated",
34
+ "shell.env",
35
+ "todo.updated",
36
+ "tool.execute.after",
37
+ "tool.execute.before",
38
+ "tui.command.execute",
39
+ "tui.prompt.append",
40
+ "tui.toast.show",
41
+ }
42
+ )
43
+
44
+ _ALREADY_PROJECTED_ELSEWHERE = frozenset(
45
+ {
46
+ "session.created",
47
+ "session.updated",
48
+ "message.updated",
49
+ "message.part.updated",
50
+ "tool.execute.before",
51
+ "tool.execute.after",
52
+ }
53
+ )
54
+
55
+
56
+ def _entity(
57
+ entity_type: str,
58
+ entity_id: str,
59
+ *,
60
+ name: str | None = None,
61
+ attributes: dict[str, Any] | None = None,
62
+ ) -> dict[str, Any]:
63
+ return {
64
+ "type": entity_type,
65
+ "id": entity_id,
66
+ "name": name,
67
+ "attributes": attributes or {},
68
+ }
69
+
70
+
71
+ def _root_agent() -> dict[str, Any]:
72
+ return _entity(
73
+ "agent",
74
+ "agent:OpenCode",
75
+ name="OpenCode",
76
+ attributes={"provider": "opencode"},
77
+ )
78
+
79
+
80
+ def _session_agent(session_id: str) -> dict[str, Any]:
81
+ return _entity(
82
+ "agent",
83
+ f"agent:opencode:session:{session_id}",
84
+ name="OpenCode session",
85
+ attributes={
86
+ "provider": "opencode",
87
+ "session_id": session_id,
88
+ "identity_semantics": "provider_session_id",
89
+ **root_topology(),
90
+ },
91
+ )
92
+
93
+
94
+ def _event(
95
+ *,
96
+ timestamp: str,
97
+ event_type: str,
98
+ relation: str,
99
+ source: dict[str, Any],
100
+ target: dict[str, Any],
101
+ attributes: dict[str, Any] | None = None,
102
+ ) -> dict[str, Any]:
103
+ merged: dict[str, Any] = {
104
+ "backend": "semantic",
105
+ "provider": "opencode",
106
+ "evidence_source": "provider_plugin",
107
+ "attribution": "opencode_official_event_bus",
108
+ "causal": False,
109
+ "inferred": False,
110
+ "official_event_contract": True,
111
+ "official_event_reference": OPENCODE_EVENTS_REFERENCE,
112
+ "opencode_event_type": event_type,
113
+ }
114
+ if attributes:
115
+ merged.update(attributes)
116
+ return {
117
+ "timestamp": timestamp,
118
+ "event_type": f"semantic.opencode.{event_type.replace('.', '_')}",
119
+ "relation": relation,
120
+ "source": source,
121
+ "target": target,
122
+ "attributes": merged,
123
+ }
124
+
125
+
126
+ def _body(raw_event: dict[str, Any]) -> dict[str, Any]:
127
+ properties = raw_event.get("properties")
128
+ if isinstance(properties, dict):
129
+ return properties
130
+ data = raw_event.get("data")
131
+ if isinstance(data, dict):
132
+ return data
133
+ return raw_event
134
+
135
+
136
+ def _string(value: Any) -> str | None:
137
+ return value if isinstance(value, str) and value else None
138
+
139
+
140
+ def _session_id(body: dict[str, Any], payload: dict[str, Any]) -> str | None:
141
+ return (
142
+ _string(body.get("sessionID"))
143
+ or _string(body.get("sessionId"))
144
+ or _string(body.get("session_id"))
145
+ or _string(payload.get("sessionID"))
146
+ )
147
+
148
+
149
+ def _digest(value: Any) -> str:
150
+ raw = json.dumps(
151
+ value,
152
+ ensure_ascii=False,
153
+ sort_keys=True,
154
+ separators=(",", ":"),
155
+ default=str,
156
+ )
157
+ return hashlib.sha256(raw.encode("utf-8")).hexdigest()[:24]
158
+
159
+
160
+ def _observation(
161
+ entity_type: str,
162
+ *,
163
+ event_type: str,
164
+ body: dict[str, Any],
165
+ timestamp: str,
166
+ name: str,
167
+ attributes: dict[str, Any] | None = None,
168
+ ) -> dict[str, Any]:
169
+ ident = _digest(
170
+ {
171
+ "event_type": event_type,
172
+ "body": body,
173
+ "observed_at": timestamp,
174
+ }
175
+ )
176
+ merged = {
177
+ "provider": "opencode",
178
+ "identity_semantics": "collector_observation_hash_without_provider_event_id",
179
+ }
180
+ if attributes:
181
+ merged.update(attributes)
182
+ return _entity(
183
+ entity_type,
184
+ f"{entity_type}:opencode:{ident}",
185
+ name=name,
186
+ attributes=merged,
187
+ )
188
+
189
+
190
+ def _permission_request(session_id: str, request_id: str) -> dict[str, Any]:
191
+ return _entity(
192
+ "permission_request",
193
+ f"permission-request:opencode:{session_id}:{request_id}",
194
+ name="OpenCode permission request",
195
+ attributes={
196
+ "provider": "opencode",
197
+ "session_id": session_id,
198
+ "permission_request_id": request_id,
199
+ "identity_semantics": "provider_permission_request_id",
200
+ },
201
+ )
202
+
203
+
204
+ def _message(session_id: str, message_id: str) -> dict[str, Any]:
205
+ return _entity(
206
+ "agent_message",
207
+ f"agent-message:opencode:{session_id}:{message_id}",
208
+ name="OpenCode message",
209
+ attributes={
210
+ "provider": "opencode",
211
+ "session_id": session_id,
212
+ "message_id": message_id,
213
+ "identity_semantics": "provider_message_id",
214
+ },
215
+ )
216
+
217
+
218
+ def _file_entity(raw_path: str, payload: dict[str, Any]) -> dict[str, Any]:
219
+ candidate = Path(raw_path).expanduser()
220
+ cwd = _string(payload.get("cwd"))
221
+ if not candidate.is_absolute() and cwd is not None:
222
+ candidate = Path(cwd) / candidate
223
+ path = str(candidate)
224
+ return _entity(
225
+ "file",
226
+ f"file:{path}",
227
+ name=candidate.name or path,
228
+ attributes={
229
+ "provider": "opencode",
230
+ "provider_path": raw_path,
231
+ "declared_by_provider_event": True,
232
+ "path_resolution_semantics": (
233
+ "provider_absolute_path"
234
+ if Path(raw_path).is_absolute()
235
+ else "provider_relative_path_joined_to_plugin_cwd"
236
+ if cwd is not None
237
+ else "provider_relative_path_without_cwd"
238
+ ),
239
+ },
240
+ )
241
+
242
+
243
+ def _session_deleted(
244
+ body: dict[str, Any],
245
+ payload: dict[str, Any],
246
+ *,
247
+ timestamp: str,
248
+ event_type: str,
249
+ ) -> list[dict[str, Any]]:
250
+ session_id = _session_id(body, payload)
251
+ if session_id is None:
252
+ return []
253
+ return [
254
+ _event(
255
+ timestamp=timestamp,
256
+ event_type=event_type,
257
+ relation="OBSERVED_AGENT_SESSION_DELETED",
258
+ source=_root_agent(),
259
+ target=_session_agent(session_id),
260
+ attributes={
261
+ "provider_session_id_exact": True,
262
+ "session_info_present": isinstance(body.get("info"), dict),
263
+ },
264
+ )
265
+ ]
266
+
267
+
268
+ def _session_status(
269
+ body: dict[str, Any],
270
+ payload: dict[str, Any],
271
+ *,
272
+ timestamp: str,
273
+ event_type: str,
274
+ ) -> list[dict[str, Any]]:
275
+ session_id = _session_id(body, payload)
276
+ status = body.get("status")
277
+ if session_id is None or not isinstance(status, dict):
278
+ return []
279
+ status_type = _string(status.get("type"))
280
+ if status_type is None:
281
+ return []
282
+ attributes: dict[str, Any] = {
283
+ "provider_session_id_exact": True,
284
+ "status_type": status_type,
285
+ "status_payload_preserved_by_raw_event": True,
286
+ }
287
+ attempt = status.get("attempt")
288
+ if isinstance(attempt, int) and not isinstance(attempt, bool) and attempt >= 0:
289
+ attributes["retry_attempt"] = attempt
290
+ next_value = status.get("next")
291
+ if isinstance(next_value, (int, float)) and not isinstance(next_value, bool):
292
+ attributes["retry_next"] = next_value
293
+ target = _observation(
294
+ "provider_session_status",
295
+ event_type=event_type,
296
+ body=body,
297
+ timestamp=timestamp,
298
+ name=f"OpenCode session status: {status_type}",
299
+ attributes={"session_id": session_id, "status_type": status_type},
300
+ )
301
+ return [
302
+ _event(
303
+ timestamp=timestamp,
304
+ event_type=event_type,
305
+ relation="OBSERVED_PROVIDER_SESSION_STATUS",
306
+ source=_session_agent(session_id),
307
+ target=target,
308
+ attributes=attributes,
309
+ )
310
+ ]
311
+
312
+
313
+ def _session_idle(
314
+ body: dict[str, Any],
315
+ payload: dict[str, Any],
316
+ *,
317
+ timestamp: str,
318
+ event_type: str,
319
+ ) -> list[dict[str, Any]]:
320
+ session_id = _session_id(body, payload)
321
+ if session_id is None:
322
+ return []
323
+ target = _observation(
324
+ "provider_session_status",
325
+ event_type=event_type,
326
+ body=body,
327
+ timestamp=timestamp,
328
+ name="OpenCode session idle",
329
+ attributes={"session_id": session_id, "status_type": "idle"},
330
+ )
331
+ return [
332
+ _event(
333
+ timestamp=timestamp,
334
+ event_type=event_type,
335
+ relation="OBSERVED_PROVIDER_SESSION_IDLE",
336
+ source=_session_agent(session_id),
337
+ target=target,
338
+ attributes={"provider_session_id_exact": True},
339
+ )
340
+ ]
341
+
342
+
343
+ def _session_compacted(
344
+ body: dict[str, Any],
345
+ payload: dict[str, Any],
346
+ *,
347
+ timestamp: str,
348
+ event_type: str,
349
+ ) -> list[dict[str, Any]]:
350
+ session_id = _session_id(body, payload)
351
+ if session_id is None:
352
+ return []
353
+ target = _observation(
354
+ "context_compaction",
355
+ event_type=event_type,
356
+ body=body,
357
+ timestamp=timestamp,
358
+ name="OpenCode session compaction",
359
+ attributes={
360
+ "session_id": session_id,
361
+ "compaction_id_available": False,
362
+ },
363
+ )
364
+ return [
365
+ _event(
366
+ timestamp=timestamp,
367
+ event_type=event_type,
368
+ relation="COMPACTED_CONTEXT",
369
+ source=_session_agent(session_id),
370
+ target=target,
371
+ attributes={
372
+ "provider_session_id_exact": True,
373
+ "compaction_completion_observed_from_provider_event": True,
374
+ "compaction_id_available": False,
375
+ },
376
+ )
377
+ ]
378
+
379
+
380
+ def _session_error(
381
+ body: dict[str, Any],
382
+ payload: dict[str, Any],
383
+ *,
384
+ timestamp: str,
385
+ event_type: str,
386
+ ) -> list[dict[str, Any]]:
387
+ if "error" not in body:
388
+ return []
389
+ session_id = _session_id(body, payload)
390
+ target = _observation(
391
+ "provider_error_observation",
392
+ event_type=event_type,
393
+ body=body,
394
+ timestamp=timestamp,
395
+ name="OpenCode session error",
396
+ attributes={
397
+ "session_id": session_id,
398
+ "error_payload_preserved_by_raw_event": True,
399
+ },
400
+ )
401
+ return [
402
+ _event(
403
+ timestamp=timestamp,
404
+ event_type=event_type,
405
+ relation="OBSERVED_PROVIDER_SESSION_ERROR",
406
+ source=_session_agent(session_id) if session_id is not None else _root_agent(),
407
+ target=target,
408
+ attributes={
409
+ "provider_session_id_exact": session_id is not None,
410
+ "session_identity_available": session_id is not None,
411
+ "error_payload_preserved_by_raw_event": True,
412
+ },
413
+ )
414
+ ]
415
+
416
+
417
+ def _session_diff(
418
+ body: dict[str, Any],
419
+ payload: dict[str, Any],
420
+ *,
421
+ timestamp: str,
422
+ event_type: str,
423
+ ) -> list[dict[str, Any]]:
424
+ session_id = _session_id(body, payload)
425
+ diff = body.get("diff")
426
+ if session_id is None or not isinstance(diff, list):
427
+ return []
428
+ target = _observation(
429
+ "session_diff_observation",
430
+ event_type=event_type,
431
+ body={"sessionID": session_id, "diff_count": len(diff)},
432
+ timestamp=timestamp,
433
+ name="OpenCode session diff",
434
+ attributes={"session_id": session_id, "file_diff_count": len(diff)},
435
+ )
436
+ return [
437
+ _event(
438
+ timestamp=timestamp,
439
+ event_type=event_type,
440
+ relation="OBSERVED_SESSION_DIFF",
441
+ source=_session_agent(session_id),
442
+ target=target,
443
+ attributes={
444
+ "provider_session_id_exact": True,
445
+ "file_diff_count": len(diff),
446
+ "diff_payload_preserved_by_raw_event": True,
447
+ },
448
+ )
449
+ ]
450
+
451
+
452
+ def _permission_asked(
453
+ body: dict[str, Any],
454
+ *,
455
+ timestamp: str,
456
+ event_type: str,
457
+ ) -> list[dict[str, Any]]:
458
+ session_id = _string(body.get("sessionID"))
459
+ request_id = _string(body.get("id"))
460
+ permission = _string(body.get("permission"))
461
+ if session_id is None or request_id is None or permission is None:
462
+ return []
463
+ patterns = body.get("patterns")
464
+ always = body.get("always")
465
+ request = _permission_request(session_id, request_id)
466
+ attributes = {
467
+ "provider_session_id_exact": True,
468
+ "provider_permission_request_id_exact": True,
469
+ "permission": permission,
470
+ "pattern_count": len(patterns) if isinstance(patterns, list) else None,
471
+ "always_pattern_count": len(always) if isinstance(always, list) else None,
472
+ "request_payload_preserved_by_raw_event": True,
473
+ }
474
+ events = [
475
+ _event(
476
+ timestamp=timestamp,
477
+ event_type=event_type,
478
+ relation="OBSERVED_PERMISSION_REQUEST",
479
+ source=_session_agent(session_id),
480
+ target=request,
481
+ attributes=attributes,
482
+ )
483
+ ]
484
+ tool = body.get("tool")
485
+ if isinstance(tool, dict):
486
+ call_id = _string(tool.get("callID"))
487
+ if call_id is not None:
488
+ message_id = _string(tool.get("messageID"))
489
+ call = _entity(
490
+ "tool_call",
491
+ f"tool-call:opencode:{session_id}:{call_id}",
492
+ name="OpenCode tool call",
493
+ attributes={
494
+ "provider": "opencode",
495
+ "session_id": session_id,
496
+ "call_id": call_id,
497
+ "identity_semantics": "provider_call_id",
498
+ },
499
+ )
500
+ tool_attributes: dict[str, Any] = {
501
+ "provider_session_id_exact": True,
502
+ "provider_tool_call_id_exact": True,
503
+ }
504
+ if message_id is not None:
505
+ tool_attributes["provider_message_id_exact"] = True
506
+ tool_attributes["message_id"] = message_id
507
+ events.append(
508
+ _event(
509
+ timestamp=timestamp,
510
+ event_type=event_type,
511
+ relation="PERMISSION_TARGETS_TOOL_CALL",
512
+ source=request,
513
+ target=call,
514
+ attributes=tool_attributes,
515
+ )
516
+ )
517
+ return events
518
+
519
+
520
+ def _permission_replied(
521
+ body: dict[str, Any],
522
+ *,
523
+ timestamp: str,
524
+ event_type: str,
525
+ ) -> list[dict[str, Any]]:
526
+ session_id = _string(body.get("sessionID"))
527
+ request_id = _string(body.get("requestID"))
528
+ reply = _string(body.get("reply"))
529
+ if session_id is None or request_id is None or reply not in {"once", "always", "reject"}:
530
+ return []
531
+ return [
532
+ _event(
533
+ timestamp=timestamp,
534
+ event_type=event_type,
535
+ relation="OBSERVED_PERMISSION_REPLY",
536
+ source=_session_agent(session_id),
537
+ target=_permission_request(session_id, request_id),
538
+ attributes={
539
+ "provider_session_id_exact": True,
540
+ "provider_permission_request_id_exact": True,
541
+ "reply": reply,
542
+ },
543
+ )
544
+ ]
545
+
546
+
547
+ def _file_edited(
548
+ body: dict[str, Any],
549
+ payload: dict[str, Any],
550
+ *,
551
+ timestamp: str,
552
+ event_type: str,
553
+ ) -> list[dict[str, Any]]:
554
+ raw_path = _string(body.get("file"))
555
+ if raw_path is None:
556
+ return []
557
+ return [
558
+ _event(
559
+ timestamp=timestamp,
560
+ event_type=event_type,
561
+ relation="OBSERVED_FILE_CHANGE",
562
+ source=_root_agent(),
563
+ target=_file_entity(raw_path, payload),
564
+ attributes={"provider_file_path_exact": True},
565
+ )
566
+ ]
567
+
568
+
569
+ def _todo_updated(
570
+ body: dict[str, Any],
571
+ payload: dict[str, Any],
572
+ *,
573
+ timestamp: str,
574
+ event_type: str,
575
+ ) -> list[dict[str, Any]]:
576
+ session_id = _session_id(body, payload)
577
+ todos = body.get("todos")
578
+ if session_id is None or not isinstance(todos, list):
579
+ return []
580
+ status_counts: dict[str, int] = {}
581
+ for todo in todos:
582
+ if not isinstance(todo, dict):
583
+ continue
584
+ status = _string(todo.get("status"))
585
+ if status is not None:
586
+ status_counts[status] = status_counts.get(status, 0) + 1
587
+ target = _observation(
588
+ "todo_state",
589
+ event_type=event_type,
590
+ body={"sessionID": session_id, "todo_count": len(todos), "status_counts": status_counts},
591
+ timestamp=timestamp,
592
+ name="OpenCode todo state",
593
+ attributes={
594
+ "session_id": session_id,
595
+ "todo_count": len(todos),
596
+ "status_counts": status_counts,
597
+ },
598
+ )
599
+ return [
600
+ _event(
601
+ timestamp=timestamp,
602
+ event_type=event_type,
603
+ relation="OBSERVED_TODO_STATE",
604
+ source=_session_agent(session_id),
605
+ target=target,
606
+ attributes={
607
+ "provider_session_id_exact": True,
608
+ "todo_count": len(todos),
609
+ "todo_status_counts": status_counts,
610
+ "todo_payload_preserved_by_raw_event": True,
611
+ },
612
+ )
613
+ ]
614
+
615
+
616
+ def _message_removed(
617
+ body: dict[str, Any],
618
+ payload: dict[str, Any],
619
+ *,
620
+ timestamp: str,
621
+ event_type: str,
622
+ ) -> list[dict[str, Any]]:
623
+ session_id = _session_id(body, payload)
624
+ message_id = _string(body.get("messageID"))
625
+ if session_id is None or message_id is None:
626
+ return []
627
+ return [
628
+ _event(
629
+ timestamp=timestamp,
630
+ event_type=event_type,
631
+ relation="OBSERVED_MESSAGE_REMOVED",
632
+ source=_session_agent(session_id),
633
+ target=_message(session_id, message_id),
634
+ attributes={
635
+ "provider_session_id_exact": True,
636
+ "provider_message_id_exact": True,
637
+ },
638
+ )
639
+ ]
640
+
641
+
642
+ def _message_part_removed(
643
+ body: dict[str, Any],
644
+ payload: dict[str, Any],
645
+ *,
646
+ timestamp: str,
647
+ event_type: str,
648
+ ) -> list[dict[str, Any]]:
649
+ session_id = _session_id(body, payload)
650
+ message_id = _string(body.get("messageID"))
651
+ part_id = _string(body.get("partID"))
652
+ if session_id is None or message_id is None or part_id is None:
653
+ return []
654
+ message = _message(session_id, message_id)
655
+ part = _entity(
656
+ "message_part",
657
+ f"message-part:opencode:{session_id}:{message_id}:{part_id}",
658
+ name="OpenCode message part",
659
+ attributes={
660
+ "provider": "opencode",
661
+ "session_id": session_id,
662
+ "message_id": message_id,
663
+ "part_id": part_id,
664
+ "identity_semantics": "provider_message_and_part_ids",
665
+ },
666
+ )
667
+ return [
668
+ _event(
669
+ timestamp=timestamp,
670
+ event_type=event_type,
671
+ relation="OBSERVED_MESSAGE_PART_REMOVED",
672
+ source=message,
673
+ target=part,
674
+ attributes={
675
+ "provider_session_id_exact": True,
676
+ "provider_message_id_exact": True,
677
+ "provider_part_id_exact": True,
678
+ },
679
+ )
680
+ ]
681
+
682
+
683
+ def opencode_official_event_semantic_events(
684
+ payload: dict[str, Any],
685
+ *,
686
+ timestamp: str,
687
+ ) -> list[dict[str, Any]]:
688
+ """Project stable execution semantics from OpenCode's official event bus.
689
+
690
+ The complete raw event is preserved separately by ``opencode_full_fidelity``.
691
+ This projector keeps only stable identifiers, statuses, counts, and explicit
692
+ provider linkages; it never reconstructs message/error/todo bodies from time
693
+ proximity or duplicates those bodies into graph attributes.
694
+ """
695
+
696
+ if payload.get("hook_event_name") != "event":
697
+ return []
698
+ raw_event = payload.get("event")
699
+ if not isinstance(raw_event, dict):
700
+ return []
701
+ event_type = _string(raw_event.get("type")) or _string(payload.get("event_type"))
702
+ if (
703
+ event_type is None
704
+ or event_type not in OFFICIAL_OPENCODE_BUS_EVENTS
705
+ or event_type in _ALREADY_PROJECTED_ELSEWHERE
706
+ ):
707
+ return []
708
+ body = _body(raw_event)
709
+
710
+ handlers = {
711
+ "session.deleted": _session_deleted,
712
+ "session.status": _session_status,
713
+ "session.idle": _session_idle,
714
+ "session.compacted": _session_compacted,
715
+ "session.error": _session_error,
716
+ "session.diff": _session_diff,
717
+ "file.edited": _file_edited,
718
+ "todo.updated": _todo_updated,
719
+ "message.removed": _message_removed,
720
+ "message.part.removed": _message_part_removed,
721
+ }
722
+ if event_type == "permission.asked":
723
+ return _permission_asked(body, timestamp=timestamp, event_type=event_type)
724
+ if event_type == "permission.replied":
725
+ return _permission_replied(body, timestamp=timestamp, event_type=event_type)
726
+ handler = handlers.get(event_type)
727
+ if handler is None:
728
+ return []
729
+ return handler(
730
+ body,
731
+ payload,
732
+ timestamp=timestamp,
733
+ event_type=event_type,
734
+ )