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,1162 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ from pathlib import Path
6
+ from typing import Any
7
+
8
+ from .content_evidence import content_observation_event
9
+ from .content_store import FullFidelityContentStore
10
+ from .agent_topology import EVIDENCE_SUBAGENT_LIFECYCLE_HOOK, subagent_topology
11
+ from .agent_topology import root_topology
12
+
13
+ _VISIBILITY: dict[str, dict[str, str]] = {
14
+ "claude": {
15
+ "agent_identity_visibility": "provider_exposed_subagent_id",
16
+ "subagent_visibility": "provider_exposed_lifecycle",
17
+ "reasoning_visibility": "not_exposed_by_source",
18
+ },
19
+ "codex": {
20
+ "agent_identity_visibility": "provider_exposed_thread_identity",
21
+ "subagent_visibility": "provider_exposed_rollout_graph",
22
+ "reasoning_visibility": "provider_exposed_plaintext_summary_or_encoded",
23
+ },
24
+ "cursor": {
25
+ "agent_identity_visibility": "provider_exposed_when_subagent_id_present",
26
+ "subagent_visibility": "provider_exposed_lifecycle",
27
+ "reasoning_visibility": "provider_exposed_thinking_text",
28
+ },
29
+ "opencode": {
30
+ "agent_identity_visibility": "provider_exposed_session_identity",
31
+ "subagent_visibility": "provider_exposed_session_parent_id",
32
+ "reasoning_visibility": "provider_exposed_reasoning_part",
33
+ },
34
+ "antigravity": {
35
+ "agent_identity_visibility": "provider_root_only",
36
+ "subagent_visibility": "not_exposed_by_source",
37
+ "reasoning_visibility": "not_exposed_by_source",
38
+ },
39
+ # Model runtimes expose a request/response surface, not provider-owned
40
+ # subagent lifecycle identities. Keep this explicit so the viewer does not
41
+ # confuse a deliberate root-only boundary with an unsupported provider.
42
+ "ollama": {
43
+ "agent_identity_visibility": "provider_root_only",
44
+ "subagent_visibility": "not_exposed_by_source",
45
+ "reasoning_visibility": "not_exposed_by_source",
46
+ },
47
+ "llamacpp": {
48
+ "agent_identity_visibility": "provider_root_only",
49
+ "subagent_visibility": "not_exposed_by_source",
50
+ "reasoning_visibility": "not_exposed_by_source",
51
+ },
52
+ "llama.cpp": {
53
+ "agent_identity_visibility": "provider_root_only",
54
+ "subagent_visibility": "not_exposed_by_source",
55
+ "reasoning_visibility": "not_exposed_by_source",
56
+ },
57
+ "vllm": {
58
+ "agent_identity_visibility": "provider_root_only",
59
+ "subagent_visibility": "not_exposed_by_source",
60
+ "reasoning_visibility": "not_exposed_by_source",
61
+ },
62
+ "lmstudio": {
63
+ "agent_identity_visibility": "provider_root_only",
64
+ "subagent_visibility": "not_exposed_by_source",
65
+ "reasoning_visibility": "not_exposed_by_source",
66
+ },
67
+ "openai-compatible": {
68
+ "agent_identity_visibility": "provider_root_only",
69
+ "subagent_visibility": "not_exposed_by_source",
70
+ "reasoning_visibility": "not_exposed_by_source",
71
+ },
72
+ "openai_compatible": {
73
+ "agent_identity_visibility": "provider_root_only",
74
+ "subagent_visibility": "not_exposed_by_source",
75
+ "reasoning_visibility": "not_exposed_by_source",
76
+ },
77
+ }
78
+
79
+
80
+ def agent_trace_visibility(provider: str) -> dict[str, str]:
81
+ """Return explicit source-visibility boundaries for one provider integration."""
82
+ normalized = provider.strip().lower()
83
+ return dict(
84
+ _VISIBILITY.get(
85
+ normalized,
86
+ {
87
+ "agent_identity_visibility": "unknown",
88
+ "subagent_visibility": "unknown",
89
+ "reasoning_visibility": "unknown",
90
+ },
91
+ )
92
+ )
93
+
94
+
95
+ _PROVIDER_ROOT_AGENTS: dict[str, tuple[str, str]] = {
96
+ "claude": ("agent:Claude Code", "Claude Code"),
97
+ "codex": ("agent:Codex", "Codex"),
98
+ "cursor": ("agent:Cursor", "Cursor"),
99
+ "opencode": ("agent:OpenCode", "OpenCode"),
100
+ "antigravity": ("agent:Antigravity", "Antigravity"),
101
+ "ollama": ("agent:Ollama", "Ollama"),
102
+ "llamacpp": ("agent:llama.cpp", "llama.cpp"),
103
+ "llama.cpp": ("agent:llama.cpp", "llama.cpp"),
104
+ "vllm": ("agent:vLLM", "vLLM"),
105
+ "lmstudio": ("agent:LM Studio", "LM Studio"),
106
+ "openai-compatible": ("agent:OpenAI-compatible", "OpenAI-compatible"),
107
+ "openai_compatible": ("agent:OpenAI-compatible", "OpenAI-compatible"),
108
+ }
109
+
110
+
111
+ def provider_agent_trace_visibility_event(
112
+ provider: str,
113
+ *,
114
+ timestamp: str,
115
+ source: dict[str, Any] | None = None,
116
+ attribution: str = "provider_integration",
117
+ evidence_source: str = "provider_hook",
118
+ ) -> dict[str, Any]:
119
+ """Materialize what the selected provider surface can and cannot expose."""
120
+ normalized = provider.strip().lower()
121
+ root_id, root_name = _PROVIDER_ROOT_AGENTS.get(
122
+ normalized,
123
+ (f"agent:{provider}", provider),
124
+ )
125
+ actor = source or _entity("agent", root_id, root_name, provider=normalized)
126
+ visibility = agent_trace_visibility(normalized)
127
+ capability = _entity(
128
+ "agent_trace_capability",
129
+ f"agent-trace-capability:{normalized}",
130
+ f"{root_name} trace visibility",
131
+ provider=normalized,
132
+ **visibility,
133
+ )
134
+ return _event(
135
+ timestamp=timestamp,
136
+ event_type=f"semantic.{normalized}.agent_trace.visibility",
137
+ relation="DECLARES_AGENT_TRACE_VISIBILITY",
138
+ source=actor,
139
+ target=capability,
140
+ provider=normalized,
141
+ attribution=attribution,
142
+ evidence_source=evidence_source,
143
+ attributes=visibility,
144
+ )
145
+
146
+
147
+ def _entity(
148
+ kind: str,
149
+ ident: str,
150
+ name: str,
151
+ **attributes: Any,
152
+ ) -> dict[str, Any]:
153
+ return {
154
+ "type": kind,
155
+ "id": ident,
156
+ "name": name,
157
+ "attributes": attributes,
158
+ }
159
+
160
+
161
+ def opencode_root_agent() -> dict[str, Any]:
162
+ return _entity("agent", "agent:OpenCode", "OpenCode", provider="opencode")
163
+
164
+
165
+ def opencode_session_agent(
166
+ session_id: str,
167
+ *,
168
+ agent_name: str | None = None,
169
+ ) -> dict[str, Any]:
170
+ name = agent_name if isinstance(agent_name, str) and agent_name else "OpenCode session"
171
+ return _entity(
172
+ "agent",
173
+ f"agent:opencode:session:{session_id}",
174
+ name,
175
+ provider="opencode",
176
+ session_id=session_id,
177
+ native_agent_name=agent_name,
178
+ identity_semantics="provider_session_id",
179
+ **root_topology(),
180
+ )
181
+
182
+
183
+ def _opencode_agent_for_payload(payload: dict[str, Any]) -> dict[str, Any]:
184
+ session_id = payload.get("sessionID")
185
+ agent_name = payload.get("agent")
186
+ if isinstance(session_id, str) and session_id:
187
+ return opencode_session_agent(
188
+ session_id,
189
+ agent_name=agent_name if isinstance(agent_name, str) else None,
190
+ )
191
+ return opencode_root_agent()
192
+
193
+
194
+ def _event(
195
+ *,
196
+ timestamp: str,
197
+ event_type: str,
198
+ relation: str,
199
+ source: dict[str, Any],
200
+ target: dict[str, Any],
201
+ provider: str,
202
+ attribution: str,
203
+ evidence_source: str,
204
+ attributes: dict[str, Any] | None = None,
205
+ ) -> dict[str, Any]:
206
+ merged: dict[str, Any] = {
207
+ "backend": "semantic",
208
+ "provider": provider,
209
+ "attribution": attribution,
210
+ "evidence_source": evidence_source,
211
+ "causal": False,
212
+ "inferred": False,
213
+ }
214
+ if attributes:
215
+ merged.update(attributes)
216
+ return {
217
+ "timestamp": timestamp,
218
+ "event_type": event_type,
219
+ "relation": relation,
220
+ "source": source,
221
+ "target": target,
222
+ "attributes": merged,
223
+ }
224
+
225
+
226
+ def _opencode_event_body(event: dict[str, Any]) -> dict[str, Any]:
227
+ properties = event.get("properties")
228
+ if isinstance(properties, dict):
229
+ return properties
230
+ data = event.get("data")
231
+ if isinstance(data, dict):
232
+ return data
233
+ return event
234
+
235
+
236
+ def _string(value: Any) -> str | None:
237
+ return value if isinstance(value, str) and value else None
238
+
239
+
240
+ def _declared_opencode_file(
241
+ *,
242
+ payload: dict[str, Any],
243
+ args: Any,
244
+ ) -> dict[str, Any] | None:
245
+ """Project only an explicitly supplied OpenCode file argument.
246
+
247
+ Event-bus tool parts and plugin tool hooks use the same provider file
248
+ argument contract. Keep path projection metadata-only; file contents stay
249
+ in the full-fidelity content store.
250
+ """
251
+ if not isinstance(args, dict):
252
+ return None
253
+ raw = None
254
+ for key in ("filePath", "file_path", "path"):
255
+ value = args.get(key)
256
+ if isinstance(value, str) and value:
257
+ raw = value
258
+ break
259
+ if raw is None:
260
+ return None
261
+ candidate = Path(raw).expanduser()
262
+ cwd = _string(payload.get("cwd"))
263
+ if not candidate.is_absolute() and cwd is not None:
264
+ candidate = Path(cwd) / candidate
265
+ try:
266
+ normalized = candidate.resolve(strict=False)
267
+ except OSError:
268
+ normalized = candidate.absolute()
269
+ return _entity(
270
+ "file",
271
+ f"file:{normalized}",
272
+ normalized.name or str(normalized),
273
+ provider="opencode",
274
+ declared_by_provider_event_bus=True,
275
+ )
276
+
277
+
278
+ def _session_id_from(
279
+ payload: dict[str, Any],
280
+ body: dict[str, Any],
281
+ *,
282
+ info: dict[str, Any] | None = None,
283
+ part: dict[str, Any] | None = None,
284
+ ) -> str | None:
285
+ candidates: list[Any] = []
286
+ if info is not None:
287
+ candidates.extend((info.get("sessionID"), info.get("sessionId"), info.get("session_id")))
288
+ if part is not None:
289
+ candidates.extend((part.get("sessionID"), part.get("sessionId"), part.get("session_id")))
290
+ candidates.extend(
291
+ (
292
+ body.get("sessionID"),
293
+ body.get("sessionId"),
294
+ body.get("session_id"),
295
+ payload.get("sessionID"),
296
+ )
297
+ )
298
+ if info is not None:
299
+ candidates.append(info.get("id"))
300
+ for candidate in candidates:
301
+ value = _string(candidate)
302
+ if value is not None:
303
+ return value
304
+ return None
305
+
306
+
307
+ def _content(
308
+ *,
309
+ store: FullFidelityContentStore,
310
+ value: Any,
311
+ content_kind: str,
312
+ timestamp: str,
313
+ source: dict[str, Any],
314
+ relation: str,
315
+ observed_field: str,
316
+ attributes: dict[str, Any] | None = None,
317
+ ) -> dict[str, Any]:
318
+ reference = (
319
+ store.put_text(value, content_kind=content_kind)
320
+ if isinstance(value, str)
321
+ else store.put_json(value, content_kind=content_kind)
322
+ )
323
+ return content_observation_event(
324
+ timestamp=timestamp,
325
+ provider="opencode",
326
+ source=source,
327
+ reference=reference,
328
+ relation=relation,
329
+ observed_field=observed_field,
330
+ evidence_source="provider_plugin",
331
+ attribution="opencode_event_bus",
332
+ event_type="semantic.opencode.agent_trace.content",
333
+ attributes=attributes,
334
+ )
335
+
336
+
337
+ def _message_entity(info: dict[str, Any], session_id: str) -> dict[str, Any]:
338
+ message_id = _string(info.get("id")) or _string(info.get("messageID")) or "unknown"
339
+ role = _string(info.get("role")) or "unknown"
340
+ attributes: dict[str, Any] = {
341
+ "provider": "opencode",
342
+ "session_id": session_id,
343
+ "message_id": message_id,
344
+ "role": role,
345
+ }
346
+ for key in ("agent", "modelID", "providerID", "parentID"):
347
+ value = info.get(key)
348
+ if isinstance(value, (str, int, float, bool)) and value != "":
349
+ attributes[key] = value
350
+ tokens = info.get("tokens")
351
+ if isinstance(tokens, dict):
352
+ reasoning = tokens.get("reasoning")
353
+ if isinstance(reasoning, (int, float)) and not isinstance(reasoning, bool):
354
+ attributes["reasoning_tokens"] = reasoning
355
+ return _entity(
356
+ "agent_message",
357
+ f"agent-message:opencode:{session_id}:{message_id}",
358
+ f"{role} message",
359
+ **attributes,
360
+ )
361
+
362
+
363
+ def _part_identity(part: dict[str, Any], session_id: str) -> tuple[str, str]:
364
+ message_id = _string(part.get("messageID")) or _string(part.get("messageId")) or "unknown"
365
+ part_id = _string(part.get("id"))
366
+ if part_id is None:
367
+ canonical = json.dumps(part, ensure_ascii=False, sort_keys=True, default=str)
368
+ part_id = hashlib.sha256(canonical.encode("utf-8")).hexdigest()[:24]
369
+ return message_id, part_id
370
+
371
+
372
+ def _session_events(
373
+ *,
374
+ event_type: str,
375
+ body: dict[str, Any],
376
+ payload: dict[str, Any],
377
+ timestamp: str,
378
+ ) -> list[dict[str, Any]]:
379
+ info = body.get("info")
380
+ if not isinstance(info, dict):
381
+ info = body.get("session")
382
+ if not isinstance(info, dict):
383
+ return []
384
+ session_id = _session_id_from(payload, body, info=info)
385
+ if session_id is None:
386
+ return []
387
+ native_agent = _string(info.get("agent"))
388
+ child = opencode_session_agent(session_id, agent_name=native_agent)
389
+ parent_id = _string(info.get("parentID")) or _string(info.get("parentId"))
390
+ if parent_id is not None:
391
+ return [
392
+ _event(
393
+ timestamp=timestamp,
394
+ event_type="semantic.opencode.agent_session.child",
395
+ relation="HAS_CHILD_AGENT_SESSION",
396
+ source=opencode_session_agent(parent_id),
397
+ target=child,
398
+ provider="opencode",
399
+ attribution="opencode_event_bus",
400
+ evidence_source="provider_plugin",
401
+ attributes={
402
+ "provider_parent_id_exact": True,
403
+ "opencode_event_type": event_type,
404
+ **agent_trace_visibility("opencode"),
405
+ },
406
+ )
407
+ ]
408
+ if event_type == "session.created":
409
+ return [
410
+ _event(
411
+ timestamp=timestamp,
412
+ event_type="semantic.opencode.agent_session.started",
413
+ relation="STARTED_AGENT_SESSION",
414
+ source=opencode_root_agent(),
415
+ target=child,
416
+ provider="opencode",
417
+ attribution="opencode_event_bus",
418
+ evidence_source="provider_plugin",
419
+ attributes={
420
+ "root_session_from_absence_of_parent_id": True,
421
+ "opencode_event_type": event_type,
422
+ **agent_trace_visibility("opencode"),
423
+ },
424
+ )
425
+ ]
426
+ return []
427
+
428
+
429
+ def _message_events(
430
+ *,
431
+ body: dict[str, Any],
432
+ payload: dict[str, Any],
433
+ timestamp: str,
434
+ ) -> list[dict[str, Any]]:
435
+ info = body.get("info")
436
+ if not isinstance(info, dict):
437
+ info = body.get("message")
438
+ if not isinstance(info, dict):
439
+ return []
440
+ session_id = _session_id_from(payload, body, info=info)
441
+ if session_id is None:
442
+ return []
443
+ agent_name = _string(info.get("agent"))
444
+ agent = opencode_session_agent(session_id, agent_name=agent_name)
445
+ message = _message_entity(info, session_id)
446
+ role = _string(info.get("role")) or "unknown"
447
+ events: list[dict[str, Any]] = []
448
+ if role == "assistant":
449
+ events.append(
450
+ _event(
451
+ timestamp=timestamp,
452
+ event_type="semantic.opencode.agent_message.produced",
453
+ relation="PRODUCED_ASSISTANT_MESSAGE",
454
+ source=agent,
455
+ target=message,
456
+ provider="opencode",
457
+ attribution="opencode_event_bus",
458
+ evidence_source="provider_plugin",
459
+ attributes=agent_trace_visibility("opencode"),
460
+ )
461
+ )
462
+ elif role == "user":
463
+ events.append(
464
+ _event(
465
+ timestamp=timestamp,
466
+ event_type="semantic.opencode.agent_message.delivered",
467
+ relation="DELIVERED_USER_MESSAGE",
468
+ source=message,
469
+ target=agent,
470
+ provider="opencode",
471
+ attribution="opencode_event_bus",
472
+ evidence_source="provider_plugin",
473
+ attributes=agent_trace_visibility("opencode"),
474
+ )
475
+ )
476
+ if agent_name is not None:
477
+ profile = _entity(
478
+ "agent_profile",
479
+ f"agent-profile:opencode:{agent_name}",
480
+ agent_name,
481
+ provider="opencode",
482
+ native_agent_name=agent_name,
483
+ )
484
+ events.append(
485
+ _event(
486
+ timestamp=timestamp,
487
+ event_type="semantic.opencode.agent_profile.used",
488
+ relation="USED_AGENT_PROFILE",
489
+ source=agent,
490
+ target=profile,
491
+ provider="opencode",
492
+ attribution="opencode_event_bus",
493
+ evidence_source="provider_plugin",
494
+ )
495
+ )
496
+ return events
497
+
498
+
499
+ def _reasoning_part_events(
500
+ *,
501
+ part: dict[str, Any],
502
+ session_id: str,
503
+ timestamp: str,
504
+ store: FullFidelityContentStore,
505
+ ) -> list[dict[str, Any]]:
506
+ text = part.get("text")
507
+ if not isinstance(text, str):
508
+ return []
509
+ message_id, part_id = _part_identity(part, session_id)
510
+ agent = opencode_session_agent(session_id)
511
+ return [
512
+ _content(
513
+ store=store,
514
+ value=text,
515
+ content_kind="opencode.reasoning.text",
516
+ timestamp=timestamp,
517
+ source=agent,
518
+ relation="PRODUCED_REASONING_TEXT",
519
+ observed_field="event.properties.part.text",
520
+ attributes={
521
+ "reasoning_readable": True,
522
+ "provider_labels_as_reasoning": True,
523
+ "provider_schema_type": "reasoning",
524
+ "message_id": message_id,
525
+ "part_id": part_id,
526
+ **agent_trace_visibility("opencode"),
527
+ },
528
+ )
529
+ ]
530
+
531
+
532
+ def _subtask_part_events(
533
+ *,
534
+ part: dict[str, Any],
535
+ session_id: str,
536
+ timestamp: str,
537
+ store: FullFidelityContentStore,
538
+ ) -> list[dict[str, Any]]:
539
+ message_id, part_id = _part_identity(part, session_id)
540
+ description = _string(part.get("description")) or "OpenCode subtask"
541
+ subtask = _entity(
542
+ "subtask",
543
+ f"subtask:opencode:{session_id}:{message_id}:{part_id}",
544
+ description,
545
+ provider="opencode",
546
+ session_id=session_id,
547
+ message_id=message_id,
548
+ part_id=part_id,
549
+ )
550
+ agent = opencode_session_agent(session_id)
551
+ events = [
552
+ _event(
553
+ timestamp=timestamp,
554
+ event_type="semantic.opencode.subtask.requested",
555
+ relation="REQUESTED_SUBTASK",
556
+ source=agent,
557
+ target=subtask,
558
+ provider="opencode",
559
+ attribution="opencode_event_bus",
560
+ evidence_source="provider_plugin",
561
+ attributes={"exact_child_session_linkage_asserted": False},
562
+ )
563
+ ]
564
+ target_agent = _string(part.get("agent"))
565
+ if target_agent is not None:
566
+ profile = _entity(
567
+ "agent_profile",
568
+ f"agent-profile:opencode:{target_agent}",
569
+ target_agent,
570
+ provider="opencode",
571
+ native_agent_name=target_agent,
572
+ )
573
+ events.append(
574
+ _event(
575
+ timestamp=timestamp,
576
+ event_type="semantic.opencode.subtask.targeted",
577
+ relation="TARGETS_AGENT_PROFILE",
578
+ source=subtask,
579
+ target=profile,
580
+ provider="opencode",
581
+ attribution="opencode_event_bus",
582
+ evidence_source="provider_plugin",
583
+ attributes={"exact_child_session_linkage_asserted": False},
584
+ )
585
+ )
586
+ for field, kind, relation in (
587
+ ("prompt", "opencode.subtask_prompt", "HAS_SUBTASK_PROMPT"),
588
+ ("description", "opencode.subtask_description", "HAS_SUBTASK_DESCRIPTION"),
589
+ ):
590
+ value = part.get(field)
591
+ if isinstance(value, str):
592
+ events.append(
593
+ _content(
594
+ store=store,
595
+ value=value,
596
+ content_kind=kind,
597
+ timestamp=timestamp,
598
+ source=subtask,
599
+ relation=relation,
600
+ observed_field=f"event.properties.part.{field}",
601
+ attributes={"exact_child_session_linkage_asserted": False},
602
+ )
603
+ )
604
+ return events
605
+
606
+
607
+ def _agent_part_events(
608
+ *,
609
+ part: dict[str, Any],
610
+ session_id: str,
611
+ timestamp: str,
612
+ ) -> list[dict[str, Any]]:
613
+ name = _string(part.get("name")) or _string(part.get("agent"))
614
+ if name is None:
615
+ return []
616
+ profile = _entity(
617
+ "agent_profile",
618
+ f"agent-profile:opencode:{name}",
619
+ name,
620
+ provider="opencode",
621
+ native_agent_name=name,
622
+ )
623
+ return [
624
+ _event(
625
+ timestamp=timestamp,
626
+ event_type="semantic.opencode.agent_profile.observed",
627
+ relation="USED_AGENT_PROFILE",
628
+ source=opencode_session_agent(session_id),
629
+ target=profile,
630
+ provider="opencode",
631
+ attribution="opencode_event_bus",
632
+ evidence_source="provider_plugin",
633
+ )
634
+ ]
635
+
636
+
637
+ def _tool_part_events(
638
+ *,
639
+ part: dict[str, Any],
640
+ payload: dict[str, Any],
641
+ session_id: str,
642
+ timestamp: str,
643
+ store: FullFidelityContentStore,
644
+ ) -> list[dict[str, Any]]:
645
+ call_id = _string(part.get("callID")) or _string(part.get("callId"))
646
+ tool_name = _string(part.get("tool")) or "unknown"
647
+ if call_id is None:
648
+ return []
649
+ call = _entity(
650
+ "tool_call",
651
+ f"tool-call:opencode:{session_id}:{call_id}",
652
+ tool_name,
653
+ provider="opencode",
654
+ session_id=session_id,
655
+ call_id=call_id,
656
+ tool_name=tool_name,
657
+ identity_semantics="provider_call_id",
658
+ )
659
+ tool = _entity(
660
+ "tool",
661
+ f"tool:opencode:{tool_name}",
662
+ tool_name,
663
+ provider="opencode",
664
+ native_name=tool_name,
665
+ )
666
+ agent = opencode_session_agent(session_id)
667
+ events = [
668
+ _event(
669
+ timestamp=timestamp,
670
+ event_type="semantic.opencode.tool.bus_observed",
671
+ relation="OBSERVED_TOOL_CALL",
672
+ source=agent,
673
+ target=call,
674
+ provider="opencode",
675
+ attribution="opencode_event_bus",
676
+ evidence_source="provider_plugin",
677
+ ),
678
+ _event(
679
+ timestamp=timestamp,
680
+ event_type="semantic.opencode.tool.bus_selected",
681
+ relation="USES_TOOL",
682
+ source=call,
683
+ target=tool,
684
+ provider="opencode",
685
+ attribution="opencode_event_bus",
686
+ evidence_source="provider_plugin",
687
+ ),
688
+ ]
689
+ state = part.get("state")
690
+ if isinstance(state, dict):
691
+ target = _declared_opencode_file(
692
+ payload=payload,
693
+ args=state.get("input"),
694
+ )
695
+ if target is not None:
696
+ events.append(
697
+ _event(
698
+ timestamp=timestamp,
699
+ event_type="semantic.opencode.file.declared",
700
+ relation="DECLARED_TARGET",
701
+ source=call,
702
+ target=target,
703
+ provider="opencode",
704
+ attribution="opencode_event_bus",
705
+ evidence_source="provider_plugin",
706
+ attributes={"provider_event_projection": True},
707
+ )
708
+ )
709
+ for field, kind, relation in (
710
+ ("input", "opencode.tool_state_input", "OBSERVED_TOOL_INPUT_FROM_PROVIDER_EVENT"),
711
+ ("output", "opencode.tool_state_output", "RECEIVED_TOOL_OUTPUT_FROM_PROVIDER_EVENT"),
712
+ ("error", "opencode.tool_state_error", "RECEIVED_TOOL_ERROR_FROM_PROVIDER_EVENT"),
713
+ ):
714
+ if field in state:
715
+ events.append(
716
+ _content(
717
+ store=store,
718
+ value=state[field],
719
+ content_kind=kind,
720
+ timestamp=timestamp,
721
+ source=call,
722
+ relation=relation,
723
+ observed_field=f"event.properties.part.state.{field}",
724
+ attributes={"provider_event_projection": True},
725
+ )
726
+ )
727
+ return events
728
+
729
+
730
+ def _step_part_events(
731
+ *,
732
+ part: dict[str, Any],
733
+ session_id: str,
734
+ timestamp: str,
735
+ ) -> list[dict[str, Any]]:
736
+ message_id, part_id = _part_identity(part, session_id)
737
+ step = _entity(
738
+ "agent_step",
739
+ f"agent-step:opencode:{session_id}:{message_id}:{part_id}",
740
+ "OpenCode step",
741
+ provider="opencode",
742
+ session_id=session_id,
743
+ message_id=message_id,
744
+ part_id=part_id,
745
+ )
746
+ attrs: dict[str, Any] = {}
747
+ for key in ("reason", "cost"):
748
+ value = part.get(key)
749
+ if isinstance(value, (str, int, float, bool)) and value != "":
750
+ attrs[key] = value
751
+ tokens = part.get("tokens")
752
+ if isinstance(tokens, dict):
753
+ attrs["tokens"] = tokens
754
+ return [
755
+ _event(
756
+ timestamp=timestamp,
757
+ event_type="semantic.opencode.agent_step.completed",
758
+ relation="COMPLETED_AGENT_STEP",
759
+ source=opencode_session_agent(session_id),
760
+ target=step,
761
+ provider="opencode",
762
+ attribution="opencode_event_bus",
763
+ evidence_source="provider_plugin",
764
+ attributes=attrs,
765
+ )
766
+ ]
767
+
768
+
769
+ def _compaction_part_events(
770
+ *,
771
+ part: dict[str, Any],
772
+ session_id: str,
773
+ timestamp: str,
774
+ ) -> list[dict[str, Any]]:
775
+ message_id, part_id = _part_identity(part, session_id)
776
+ compact = _entity(
777
+ "compaction",
778
+ f"compaction:opencode:{session_id}:{message_id}:{part_id}",
779
+ "OpenCode compaction",
780
+ provider="opencode",
781
+ session_id=session_id,
782
+ message_id=message_id,
783
+ part_id=part_id,
784
+ )
785
+ return [
786
+ _event(
787
+ timestamp=timestamp,
788
+ event_type="semantic.opencode.context.compacted",
789
+ relation="COMPACTED_CONTEXT",
790
+ source=opencode_session_agent(session_id),
791
+ target=compact,
792
+ provider="opencode",
793
+ attribution="opencode_event_bus",
794
+ evidence_source="provider_plugin",
795
+ )
796
+ ]
797
+
798
+
799
+ def _part_events(
800
+ *,
801
+ body: dict[str, Any],
802
+ payload: dict[str, Any],
803
+ timestamp: str,
804
+ store: FullFidelityContentStore,
805
+ ) -> list[dict[str, Any]]:
806
+ part = body.get("part")
807
+ if not isinstance(part, dict):
808
+ return []
809
+ session_id = _session_id_from(payload, body, part=part)
810
+ if session_id is None:
811
+ return []
812
+ kind = _string(part.get("type"))
813
+ if kind == "reasoning":
814
+ return _reasoning_part_events(
815
+ part=part,
816
+ session_id=session_id,
817
+ timestamp=timestamp,
818
+ store=store,
819
+ )
820
+ if kind == "subtask":
821
+ return _subtask_part_events(
822
+ part=part,
823
+ session_id=session_id,
824
+ timestamp=timestamp,
825
+ store=store,
826
+ )
827
+ if kind == "agent":
828
+ return _agent_part_events(part=part, session_id=session_id, timestamp=timestamp)
829
+ if kind == "tool":
830
+ return _tool_part_events(
831
+ part=part,
832
+ payload=payload,
833
+ session_id=session_id,
834
+ timestamp=timestamp,
835
+ store=store,
836
+ )
837
+ if kind == "step-finish":
838
+ return _step_part_events(part=part, session_id=session_id, timestamp=timestamp)
839
+ if kind == "compaction":
840
+ return _compaction_part_events(part=part, session_id=session_id, timestamp=timestamp)
841
+ return []
842
+
843
+
844
+ def opencode_agent_trace_events(
845
+ payload: dict[str, Any],
846
+ *,
847
+ store: FullFidelityContentStore,
848
+ timestamp: str,
849
+ ) -> list[dict[str, Any]]:
850
+ """Project explicit OpenCode event-bus agent/reasoning evidence into common graph semantics.
851
+
852
+ The raw bus event remains stored independently by ``opencode_full_fidelity``.
853
+ This projection never infers a delegating parent from a tool hook and only
854
+ creates parent-child agent edges when OpenCode explicitly supplies ``parentID``.
855
+ """
856
+
857
+ hook = _string(payload.get("hook_event_name"))
858
+ if hook is None:
859
+ return []
860
+
861
+ if hook in {"tool.execute.before", "tool.execute.after"}:
862
+ session_id = _string(payload.get("sessionID"))
863
+ call_id = _string(payload.get("callID"))
864
+ tool_name = _string(payload.get("tool")) or "unknown"
865
+ if session_id is None or call_id is None:
866
+ return []
867
+ call = _entity(
868
+ "tool_call",
869
+ f"tool-call:opencode:{session_id}:{call_id}",
870
+ tool_name,
871
+ provider="opencode",
872
+ session_id=session_id,
873
+ call_id=call_id,
874
+ tool_name=tool_name,
875
+ identity_semantics="provider_call_id",
876
+ )
877
+ return [
878
+ _event(
879
+ timestamp=timestamp,
880
+ event_type="semantic.opencode.agent.tool_owned",
881
+ relation="OWNED_TOOL_CALL",
882
+ source=opencode_session_agent(
883
+ session_id,
884
+ agent_name=_string(payload.get("agent")),
885
+ ),
886
+ target=call,
887
+ provider="opencode",
888
+ attribution="opencode_plugin",
889
+ evidence_source="provider_plugin",
890
+ attributes={
891
+ "provider_session_id_exact": True,
892
+ **agent_trace_visibility("opencode"),
893
+ },
894
+ )
895
+ ]
896
+
897
+ if hook == "experimental.text.complete" and isinstance(payload.get("text"), str):
898
+ session_id = _string(payload.get("sessionID"))
899
+ if session_id is None:
900
+ return []
901
+ reference = store.put_text(
902
+ payload["text"],
903
+ content_kind="opencode.completed_text",
904
+ )
905
+ return [
906
+ content_observation_event(
907
+ timestamp=timestamp,
908
+ provider="opencode",
909
+ source=opencode_session_agent(
910
+ session_id,
911
+ agent_name=_string(payload.get("agent")),
912
+ ),
913
+ reference=reference,
914
+ relation="PRODUCED_ASSISTANT_TEXT",
915
+ observed_field="text",
916
+ evidence_source="provider_plugin",
917
+ attribution="opencode_plugin",
918
+ event_type="semantic.opencode.agent.text",
919
+ attributes=agent_trace_visibility("opencode"),
920
+ )
921
+ ]
922
+
923
+ if hook != "event":
924
+ return []
925
+
926
+ raw_event = payload.get("event")
927
+ if not isinstance(raw_event, dict):
928
+ return []
929
+ event_type = _string(raw_event.get("type")) or _string(payload.get("event_type"))
930
+ if event_type is None:
931
+ return []
932
+ body = _opencode_event_body(raw_event)
933
+ if event_type in {"session.created", "session.updated"}:
934
+ events = _session_events(
935
+ event_type=event_type,
936
+ body=body,
937
+ payload=payload,
938
+ timestamp=timestamp,
939
+ )
940
+ if event_type == "session.created":
941
+ events.append(
942
+ provider_agent_trace_visibility_event(
943
+ "opencode",
944
+ timestamp=timestamp,
945
+ source=_opencode_agent_for_payload(payload),
946
+ attribution="opencode_event_bus",
947
+ evidence_source="provider_plugin",
948
+ )
949
+ )
950
+ return events
951
+ if event_type == "message.updated":
952
+ return _message_events(body=body, payload=payload, timestamp=timestamp)
953
+ if event_type == "message.part.updated":
954
+ return _part_events(
955
+ body=body,
956
+ payload=payload,
957
+ timestamp=timestamp,
958
+ store=store,
959
+ )
960
+ return []
961
+
962
+
963
+ def cursor_root_agent() -> dict[str, Any]:
964
+ return _entity("agent", "agent:Cursor", "Cursor", provider="cursor")
965
+
966
+
967
+ def cursor_subagent(
968
+ payload: dict[str, Any],
969
+ ) -> dict[str, Any] | None:
970
+ subagent_id = _string(payload.get("subagent_id"))
971
+ if subagent_id is None:
972
+ return None
973
+ scope = None
974
+ for key in ("conversation_id", "session_id", "generation_id"):
975
+ scope = _string(payload.get(key))
976
+ if scope is not None:
977
+ break
978
+ if scope is None:
979
+ scope = "unknown"
980
+ subagent_type = _string(payload.get("subagent_type")) or "Cursor subagent"
981
+ return _entity(
982
+ "agent",
983
+ f"agent:cursor:{scope}:subagent:{subagent_id}",
984
+ subagent_type,
985
+ provider="cursor",
986
+ subagent_id=subagent_id,
987
+ subagent_type=subagent_type,
988
+ identity_semantics="provider_subagent_id",
989
+ **subagent_topology(
990
+ evidence=EVIDENCE_SUBAGENT_LIFECYCLE_HOOK,
991
+ parent_scope_id=scope,
992
+ ),
993
+ )
994
+
995
+
996
+ def cursor_agent_trace_events(
997
+ payload: dict[str, Any],
998
+ *,
999
+ store: FullFidelityContentStore,
1000
+ timestamp: str,
1001
+ ) -> list[dict[str, Any]]:
1002
+ """Add exact Cursor subagent ownership/reasoning edges when the hook exposes IDs."""
1003
+
1004
+ hook = _string(payload.get("hook_event_name"))
1005
+ if hook is None:
1006
+ return []
1007
+ child = cursor_subagent(payload)
1008
+ visibility = agent_trace_visibility("cursor")
1009
+ events: list[dict[str, Any]] = []
1010
+
1011
+ if hook == "sessionStart":
1012
+ events.append(
1013
+ provider_agent_trace_visibility_event(
1014
+ "cursor",
1015
+ timestamp=timestamp,
1016
+ source=cursor_root_agent(),
1017
+ attribution="cursor_hook",
1018
+ evidence_source="provider_hook",
1019
+ )
1020
+ )
1021
+
1022
+ if hook == "subagentStart" and child is not None:
1023
+ events.append(
1024
+ _event(
1025
+ timestamp=timestamp,
1026
+ event_type="semantic.cursor.subagent.started",
1027
+ relation="SPAWNED_SUBAGENT",
1028
+ source=cursor_root_agent(),
1029
+ target=child,
1030
+ provider="cursor",
1031
+ attribution="cursor_hook",
1032
+ evidence_source="provider_hook",
1033
+ attributes={**visibility, "provider_subagent_id_exact": True},
1034
+ )
1035
+ )
1036
+ elif hook == "subagentStop" and child is not None:
1037
+ events.append(
1038
+ _event(
1039
+ timestamp=timestamp,
1040
+ event_type="semantic.cursor.subagent.finished",
1041
+ relation="RETURNED_TO",
1042
+ source=child,
1043
+ target=cursor_root_agent(),
1044
+ provider="cursor",
1045
+ attribution="cursor_hook",
1046
+ evidence_source="provider_hook",
1047
+ attributes={**visibility, "provider_subagent_id_exact": True},
1048
+ )
1049
+ )
1050
+
1051
+ if hook in {"preToolUse", "postToolUse", "postToolUseFailure"} and child is not None:
1052
+ tool_use_id = _string(payload.get("tool_use_id"))
1053
+ tool_name = _string(payload.get("tool_name")) or "unknown"
1054
+ if tool_use_id is not None:
1055
+ scope = None
1056
+ for key in ("conversation_id", "session_id", "generation_id"):
1057
+ scope = _string(payload.get(key))
1058
+ if scope is not None:
1059
+ break
1060
+ if scope is None:
1061
+ scope = "unknown"
1062
+ call = _entity(
1063
+ "tool_call",
1064
+ f"tool-call:cursor:{scope}:{tool_use_id}",
1065
+ tool_name,
1066
+ provider="cursor",
1067
+ tool_name=tool_name,
1068
+ tool_use_id=tool_use_id,
1069
+ )
1070
+ events.append(
1071
+ _event(
1072
+ timestamp=timestamp,
1073
+ event_type="semantic.cursor.subagent.tool_owned",
1074
+ relation="OWNED_TOOL_CALL",
1075
+ source=child,
1076
+ target=call,
1077
+ provider="cursor",
1078
+ attribution="cursor_hook",
1079
+ evidence_source="provider_hook",
1080
+ attributes={**visibility, "provider_subagent_id_exact": True},
1081
+ )
1082
+ )
1083
+
1084
+ if hook in {"beforeShellExecution", "afterShellExecution", "beforeMCPExecution", "afterMCPExecution"} and child is not None:
1085
+ operation = _entity(
1086
+ "agent_operation",
1087
+ "agent-operation:cursor:"
1088
+ + hashlib.sha256(
1089
+ json.dumps(
1090
+ [
1091
+ hook,
1092
+ child["id"],
1093
+ payload.get("command"),
1094
+ payload.get("tool_name"),
1095
+ payload.get("tool_use_id"),
1096
+ ],
1097
+ ensure_ascii=False,
1098
+ sort_keys=True,
1099
+ default=str,
1100
+ ).encode("utf-8")
1101
+ ).hexdigest()[:24],
1102
+ hook,
1103
+ provider="cursor",
1104
+ hook_event_name=hook,
1105
+ )
1106
+ events.append(
1107
+ _event(
1108
+ timestamp=timestamp,
1109
+ event_type="semantic.cursor.subagent.operation_owned",
1110
+ relation="OWNED_AGENT_OPERATION",
1111
+ source=child,
1112
+ target=operation,
1113
+ provider="cursor",
1114
+ attribution="cursor_hook",
1115
+ evidence_source="provider_hook",
1116
+ attributes={**visibility, "provider_subagent_id_exact": True},
1117
+ )
1118
+ )
1119
+
1120
+ if hook == "afterAgentThought" and isinstance(payload.get("text"), str):
1121
+ actor = child or cursor_root_agent()
1122
+ reference = store.put_text(payload["text"], content_kind="cursor.agent_thought")
1123
+ events.append(
1124
+ content_observation_event(
1125
+ timestamp=timestamp,
1126
+ provider="cursor",
1127
+ source=actor,
1128
+ reference=reference,
1129
+ relation="PRODUCED_REASONING_TEXT",
1130
+ observed_field="text",
1131
+ evidence_source="provider_hook",
1132
+ attribution="cursor_hook",
1133
+ event_type="semantic.cursor.reasoning.observed",
1134
+ attributes={
1135
+ "cursor_hook_event_name": hook,
1136
+ "reasoning_readable": True,
1137
+ "provider_labels_as_thinking_text": True,
1138
+ **visibility,
1139
+ },
1140
+ )
1141
+ )
1142
+ elif hook == "afterAgentResponse" and isinstance(payload.get("text"), str) and child is not None:
1143
+ reference = store.put_text(payload["text"], content_kind="cursor.subagent_response")
1144
+ events.append(
1145
+ content_observation_event(
1146
+ timestamp=timestamp,
1147
+ provider="cursor",
1148
+ source=child,
1149
+ reference=reference,
1150
+ relation="PRODUCED_ASSISTANT_RESPONSE",
1151
+ observed_field="text",
1152
+ evidence_source="provider_hook",
1153
+ attribution="cursor_hook",
1154
+ event_type="semantic.cursor.subagent.response",
1155
+ attributes={
1156
+ "cursor_hook_event_name": hook,
1157
+ "provider_subagent_id_exact": True,
1158
+ **visibility,
1159
+ },
1160
+ )
1161
+ )
1162
+ return events