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,489 @@
1
+ from __future__ import annotations
2
+
3
+ import hashlib
4
+ import json
5
+ from datetime import datetime, timezone
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from .content_evidence import content_observation_event, filter_transport_credentials
10
+ from .content_store import FullFidelityContentStore
11
+ from .agent_topology import EVIDENCE_SUBAGENT_LIFECYCLE_HOOK, subagent_topology
12
+
13
+ _CONTENT_FIELDS = frozenset(
14
+ {
15
+ "prompt",
16
+ "text",
17
+ "task",
18
+ "summary",
19
+ "description",
20
+ "agent_message",
21
+ "tool_input",
22
+ "tool_output",
23
+ "error_message",
24
+ "content",
25
+ "edits",
26
+ "command",
27
+ "output",
28
+ "result_json",
29
+ }
30
+ )
31
+
32
+
33
+ def _now() -> str:
34
+ return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
35
+
36
+
37
+ def _entity(kind: str, ident: str, name: str, **attrs: Any) -> dict[str, Any]:
38
+ return {"type": kind, "id": ident, "name": name, "attributes": attrs}
39
+
40
+
41
+ def _agent() -> dict[str, Any]:
42
+ return _entity("agent", "agent:Cursor", "Cursor", provider="cursor")
43
+
44
+
45
+ def _scope(payload: dict[str, Any]) -> str:
46
+ for key in ("conversation_id", "session_id", "generation_id"):
47
+ value = payload.get(key)
48
+ if isinstance(value, str) and value:
49
+ return value
50
+ return "unknown"
51
+
52
+
53
+ def _provider_session(payload: dict[str, Any]) -> dict[str, Any]:
54
+ session_id = payload.get("session_id")
55
+ if not isinstance(session_id, str) or not session_id:
56
+ raise ValueError("Cursor session hook requires session_id")
57
+ return _entity(
58
+ "provider_session",
59
+ f"provider-session:cursor:{session_id}",
60
+ session_id,
61
+ provider="cursor",
62
+ session_id=session_id,
63
+ identity_semantics="provider_session_id",
64
+ )
65
+
66
+
67
+ def _tool_call(payload: dict[str, Any], hook: str) -> dict[str, Any]:
68
+ name = (
69
+ payload.get("tool_name")
70
+ if isinstance(payload.get("tool_name"), str)
71
+ else "unknown"
72
+ )
73
+ use_id = payload.get("tool_use_id")
74
+ if isinstance(use_id, str) and use_id:
75
+ return _entity(
76
+ "tool_call",
77
+ f"tool-call:cursor:{_scope(payload)}:{use_id}",
78
+ name,
79
+ provider="cursor",
80
+ tool_name=name,
81
+ tool_use_id=use_id,
82
+ identity_semantics="provider_tool_use_id",
83
+ )
84
+ raw = json.dumps(
85
+ [
86
+ hook,
87
+ _scope(payload),
88
+ name,
89
+ payload.get("generation_id"),
90
+ payload.get("tool_input"),
91
+ ],
92
+ ensure_ascii=False,
93
+ sort_keys=True,
94
+ default=str,
95
+ )
96
+ digest = hashlib.sha256(raw.encode()).hexdigest()[:24]
97
+ return _entity(
98
+ "tool_call_observation",
99
+ f"tool-call-observation:cursor:{_scope(payload)}:{digest}",
100
+ name,
101
+ provider="cursor",
102
+ tool_name=name,
103
+ identity_semantics="provider_hook_observation_without_unique_tool_call_id",
104
+ direct_tool_call_linkage_asserted=False,
105
+ )
106
+
107
+
108
+ def _file(payload: dict[str, Any]) -> dict[str, Any]:
109
+ raw = "unknown"
110
+ for key in ("file_path", "filePath", "path"):
111
+ candidate = payload.get(key)
112
+ if isinstance(candidate, str) and candidate:
113
+ raw = candidate
114
+ break
115
+ candidate = Path(raw).expanduser() if raw != "unknown" else Path(raw)
116
+ try:
117
+ normalized = str(candidate.resolve(strict=False)) if raw != "unknown" else raw
118
+ except OSError:
119
+ normalized = str(candidate.absolute())
120
+ return _entity(
121
+ "file",
122
+ f"file:{normalized}",
123
+ Path(normalized).name or normalized,
124
+ provider="cursor",
125
+ provider_path=raw,
126
+ declared_by_provider_hook=True,
127
+ )
128
+
129
+
130
+ def _subagent(payload: dict[str, Any], hook: str) -> dict[str, Any]:
131
+ sub_id = payload.get("subagent_id")
132
+ sub_type = (
133
+ payload.get("subagent_type")
134
+ if isinstance(payload.get("subagent_type"), str)
135
+ else "Cursor subagent"
136
+ )
137
+ if isinstance(sub_id, str) and sub_id:
138
+ return _entity(
139
+ "agent",
140
+ f"agent:cursor:{_scope(payload)}:subagent:{sub_id}",
141
+ sub_type,
142
+ provider="cursor",
143
+ subagent_id=sub_id,
144
+ identity_semantics="provider_subagent_id",
145
+ **subagent_topology(
146
+ evidence=EVIDENCE_SUBAGENT_LIFECYCLE_HOOK,
147
+ parent_scope_id=_scope(payload),
148
+ ),
149
+ )
150
+ raw = json.dumps(
151
+ [
152
+ hook,
153
+ _scope(payload),
154
+ sub_type,
155
+ payload.get("task"),
156
+ payload.get("description"),
157
+ payload.get("status"),
158
+ ],
159
+ ensure_ascii=False,
160
+ sort_keys=True,
161
+ default=str,
162
+ )
163
+ digest = hashlib.sha256(raw.encode()).hexdigest()[:24]
164
+ return _entity(
165
+ "subagent_observation",
166
+ f"subagent-observation:cursor:{_scope(payload)}:{digest}",
167
+ sub_type,
168
+ provider="cursor",
169
+ identity_semantics="provider_hook_observation_without_subagent_id",
170
+ direct_start_stop_linkage_asserted=False,
171
+ )
172
+
173
+
174
+ def _observe(
175
+ events: list[dict[str, Any]],
176
+ store: FullFidelityContentStore,
177
+ *,
178
+ value: Any,
179
+ kind: str,
180
+ timestamp: str,
181
+ source: dict[str, Any],
182
+ relation: str,
183
+ field: str,
184
+ hook: str,
185
+ **attrs: Any,
186
+ ) -> None:
187
+ ref = (
188
+ store.put_text(value, content_kind=kind)
189
+ if isinstance(value, str)
190
+ else store.put_json(value, content_kind=kind)
191
+ )
192
+ events.append(
193
+ content_observation_event(
194
+ timestamp=timestamp,
195
+ provider="cursor",
196
+ source=source,
197
+ reference=ref,
198
+ relation=relation,
199
+ observed_field=field,
200
+ evidence_source="provider_hook",
201
+ attribution="cursor_hook",
202
+ attributes={"cursor_hook_event_name": hook, **attrs},
203
+ )
204
+ )
205
+
206
+
207
+ def _metadata(
208
+ payload: dict[str, Any],
209
+ store: FullFidelityContentStore,
210
+ timestamp: str,
211
+ hook: str,
212
+ ) -> dict[str, Any] | None:
213
+ raw = {
214
+ key: value
215
+ for key, value in payload.items()
216
+ if key not in _CONTENT_FIELDS
217
+ }
218
+ filtered, removed = filter_transport_credentials(raw)
219
+ if not filtered:
220
+ return None
221
+ ref = store.put_json(filtered, content_kind="cursor.provider_hook_metadata")
222
+ return content_observation_event(
223
+ timestamp=timestamp,
224
+ provider="cursor",
225
+ source=_agent(),
226
+ reference=ref,
227
+ relation="OBSERVED_PROVIDER_METADATA",
228
+ observed_field="hook_metadata",
229
+ evidence_source="provider_hook",
230
+ attribution="cursor_hook",
231
+ attributes={
232
+ "cursor_hook_event_name": hook,
233
+ "transport_credentials_excluded": removed,
234
+ },
235
+ )
236
+
237
+
238
+ def cursor_hook_to_content_events(
239
+ payload: dict[str, Any],
240
+ *,
241
+ store: FullFidelityContentStore,
242
+ timestamp: str | None = None,
243
+ ) -> list[dict[str, Any]]:
244
+ """Persist complete values supplied by Cursor hooks; never read transcript_path."""
245
+ hook = payload.get("hook_event_name")
246
+ if not isinstance(hook, str) or not hook:
247
+ raise ValueError("Cursor hook payload requires hook_event_name")
248
+ ts = timestamp or _now()
249
+ events: list[dict[str, Any]] = []
250
+ metadata = _metadata(payload, store, ts, hook)
251
+ if metadata is not None:
252
+ events.append(metadata)
253
+
254
+ if hook == "beforeSubmitPrompt" and isinstance(payload.get("prompt"), str):
255
+ _observe(
256
+ events,
257
+ store,
258
+ value=payload["prompt"],
259
+ kind="cursor.prompt_submission_candidate",
260
+ timestamp=ts,
261
+ source=_agent(),
262
+ relation="OBSERVED_PROMPT_SUBMISSION_CANDIDATE",
263
+ field="prompt",
264
+ hook=hook,
265
+ backend_submission_asserted=False,
266
+ prompt_can_be_blocked_by_hook=True,
267
+ )
268
+
269
+ if hook == "sessionEnd" and isinstance(payload.get("error_message"), str):
270
+ _observe(
271
+ events,
272
+ store,
273
+ value=payload["error_message"],
274
+ kind="cursor.session_end_error",
275
+ timestamp=ts,
276
+ source=_provider_session(payload),
277
+ relation="OBSERVED_SESSION_END_ERROR",
278
+ field="error_message",
279
+ hook=hook,
280
+ provider_reported_session_error=True,
281
+ )
282
+
283
+ if hook in {"preToolUse", "postToolUse", "postToolUseFailure"}:
284
+ call = _tool_call(payload, hook)
285
+ if "tool_input" in payload:
286
+ _observe(
287
+ events,
288
+ store,
289
+ value=payload["tool_input"],
290
+ kind="cursor.tool_input",
291
+ timestamp=ts,
292
+ source=call,
293
+ relation="OBSERVED_TOOL_INPUT",
294
+ field="tool_input",
295
+ hook=hook,
296
+ )
297
+ if isinstance(payload.get("agent_message"), str):
298
+ _observe(
299
+ events,
300
+ store,
301
+ value=payload["agent_message"],
302
+ kind="cursor.agent_message",
303
+ timestamp=ts,
304
+ source=call,
305
+ relation="OBSERVED_AGENT_MESSAGE",
306
+ field="agent_message",
307
+ hook=hook,
308
+ )
309
+ if hook == "postToolUse" and "tool_output" in payload:
310
+ _observe(
311
+ events,
312
+ store,
313
+ value=payload["tool_output"],
314
+ kind="cursor.tool_output",
315
+ timestamp=ts,
316
+ source=call,
317
+ relation="RECEIVED_TOOL_OUTPUT",
318
+ field="tool_output",
319
+ hook=hook,
320
+ provider_value_is_json_stringified_result=True,
321
+ )
322
+ if (
323
+ hook == "postToolUseFailure"
324
+ and isinstance(payload.get("error_message"), str)
325
+ ):
326
+ _observe(
327
+ events,
328
+ store,
329
+ value=payload["error_message"],
330
+ kind="cursor.tool_failure",
331
+ timestamp=ts,
332
+ source=call,
333
+ relation="RECEIVED_TOOL_ERROR",
334
+ field="error_message",
335
+ hook=hook,
336
+ )
337
+
338
+ if hook in {"beforeShellExecution", "afterShellExecution"}:
339
+ if isinstance(payload.get("command"), str):
340
+ relation = (
341
+ "OBSERVED_SHELL_COMMAND_BEFORE_EXECUTION"
342
+ if hook.startswith("before")
343
+ else "OBSERVED_SHELL_COMMAND_AFTER_EXECUTION"
344
+ )
345
+ _observe(
346
+ events,
347
+ store,
348
+ value=payload["command"],
349
+ kind="cursor.shell_command",
350
+ timestamp=ts,
351
+ source=_agent(),
352
+ relation=relation,
353
+ field="command",
354
+ hook=hook,
355
+ )
356
+ if hook == "afterShellExecution" and isinstance(payload.get("output"), str):
357
+ _observe(
358
+ events,
359
+ store,
360
+ value=payload["output"],
361
+ kind="cursor.shell_output",
362
+ timestamp=ts,
363
+ source=_agent(),
364
+ relation="RECEIVED_SHELL_OUTPUT",
365
+ field="output",
366
+ hook=hook,
367
+ )
368
+
369
+ if hook in {"beforeMCPExecution", "afterMCPExecution"}:
370
+ if isinstance(payload.get("command"), str):
371
+ _observe(
372
+ events,
373
+ store,
374
+ value=payload["command"],
375
+ kind="cursor.mcp_server_command",
376
+ timestamp=ts,
377
+ source=_agent(),
378
+ relation="OBSERVED_MCP_SERVER_COMMAND",
379
+ field="command",
380
+ hook=hook,
381
+ mcp_server_identity_asserted=False,
382
+ )
383
+ if "tool_input" in payload:
384
+ _observe(
385
+ events,
386
+ store,
387
+ value=payload["tool_input"],
388
+ kind="cursor.mcp_tool_input",
389
+ timestamp=ts,
390
+ source=_agent(),
391
+ relation="OBSERVED_MCP_TOOL_INPUT",
392
+ field="tool_input",
393
+ hook=hook,
394
+ )
395
+ if hook == "afterMCPExecution" and "result_json" in payload:
396
+ _observe(
397
+ events,
398
+ store,
399
+ value=payload["result_json"],
400
+ kind="cursor.mcp_tool_result",
401
+ timestamp=ts,
402
+ source=_agent(),
403
+ relation="RECEIVED_MCP_TOOL_RESULT",
404
+ field="result_json",
405
+ hook=hook,
406
+ )
407
+
408
+ if (
409
+ hook in {"beforeReadFile", "beforeTabFileRead"}
410
+ and isinstance(payload.get("content"), str)
411
+ ):
412
+ _observe(
413
+ events,
414
+ store,
415
+ value=payload["content"],
416
+ kind="cursor.file_content_before_read",
417
+ timestamp=ts,
418
+ source=_file(payload),
419
+ relation="OBSERVED_FILE_CONTENT_BEFORE_READ",
420
+ field="content",
421
+ hook=hook,
422
+ read_completion_asserted=False,
423
+ )
424
+ if (
425
+ hook in {"afterFileEdit", "afterTabFileEdit"}
426
+ and isinstance(payload.get("edits"), list)
427
+ ):
428
+ _observe(
429
+ events,
430
+ store,
431
+ value=payload["edits"],
432
+ kind="cursor.file_edits",
433
+ timestamp=ts,
434
+ source=_file(payload),
435
+ relation="OBSERVED_FILE_EDITS",
436
+ field="edits",
437
+ hook=hook,
438
+ complete_post_edit_file_snapshot_asserted=False,
439
+ )
440
+
441
+ if (
442
+ hook in {"afterAgentResponse", "afterAgentThought"}
443
+ and isinstance(payload.get("text"), str)
444
+ ):
445
+ thought = hook == "afterAgentThought"
446
+ _observe(
447
+ events,
448
+ store,
449
+ value=payload["text"],
450
+ kind=(
451
+ "cursor.agent_thought"
452
+ if thought
453
+ else "cursor.assistant_response"
454
+ ),
455
+ timestamp=ts,
456
+ source=_agent(),
457
+ relation=(
458
+ "OBSERVED_AGENT_THOUGHT"
459
+ if thought
460
+ else "PRODUCED_ASSISTANT_RESPONSE"
461
+ ),
462
+ field="text",
463
+ hook=hook,
464
+ **({"provider_labels_as_thinking_text": True} if thought else {}),
465
+ )
466
+
467
+ if hook in {"subagentStart", "subagentStop"}:
468
+ sub = _subagent(payload, hook)
469
+ for field, relation in (
470
+ ("task", "OBSERVED_SUBAGENT_TASK"),
471
+ ("description", "OBSERVED_SUBAGENT_DESCRIPTION"),
472
+ ("summary", "RECEIVED_SUBAGENT_SUMMARY"),
473
+ ):
474
+ if (
475
+ isinstance(payload.get(field), str)
476
+ and (field == "task" or hook == "subagentStop")
477
+ ):
478
+ _observe(
479
+ events,
480
+ store,
481
+ value=payload[field],
482
+ kind=f"cursor.subagent_{field}",
483
+ timestamp=ts,
484
+ source=sub,
485
+ relation=relation,
486
+ field=field,
487
+ hook=hook,
488
+ )
489
+ return events
@@ -0,0 +1,195 @@
1
+ from __future__ import annotations
2
+
3
+ import argparse
4
+ import json
5
+ import os
6
+ import sys
7
+ from datetime import datetime, timezone
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ from .agent_trace import cursor_agent_trace_events
12
+ from .content_store import FullFidelityContentStore
13
+ from .cursor_adapter import (
14
+ append_semantic_records,
15
+ cursor_hook_to_semantic_events,
16
+ read_hook_payload,
17
+ )
18
+ from .cursor_delegation import cursor_delegation_events
19
+ from .cursor_full_fidelity import cursor_hook_to_content_events
20
+ from .private_io import private_artifact_path
21
+ from .privacy import full_content_capture_enabled
22
+ from .cursor_hook_contract import (
23
+ OFFICIAL_CURSOR_HOOK_EVENTS,
24
+ cursor_official_hook_semantic_events,
25
+ cursor_workspace_scope,
26
+ )
27
+
28
+
29
+ def _now() -> str:
30
+ return datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
31
+
32
+
33
+ def cursor_hook_config(command: str = "execweave-cursor-hook") -> dict[str, Any]:
34
+ hooks = {
35
+ name: [{"command": command}]
36
+ for name in sorted(OFFICIAL_CURSOR_HOOK_EVENTS)
37
+ }
38
+ if set(hooks) != set(OFFICIAL_CURSOR_HOOK_EVENTS):
39
+ raise RuntimeError("Cursor hook config drifted from the official event set")
40
+ return {"version": 1, "hooks": hooks}
41
+
42
+
43
+ def _default_sidecar(payload: dict[str, Any]) -> Path:
44
+ hook = payload.get("hook_event_name")
45
+ if hook == "workspaceOpen":
46
+ workspace_scope, root = cursor_workspace_scope(payload)
47
+ return (
48
+ root
49
+ / ".execweave"
50
+ / "semantic"
51
+ / "cursor"
52
+ / f"workspace-{workspace_scope}.jsonl"
53
+ )
54
+
55
+ cwd = payload.get("cwd")
56
+ if not isinstance(cwd, str) or not cwd:
57
+ roots = payload.get("workspace_roots")
58
+ if isinstance(roots, list) and roots and isinstance(roots[0], str):
59
+ cwd = roots[0]
60
+ if not isinstance(cwd, str) or not cwd:
61
+ raise ValueError(
62
+ "Cursor hook payload has no cwd/workspace root for sidecar placement"
63
+ )
64
+ scope = next(
65
+ (
66
+ payload[key]
67
+ for key in ("conversation_id", "session_id", "generation_id")
68
+ if isinstance(payload.get(key), str) and payload[key]
69
+ ),
70
+ None,
71
+ )
72
+ if scope is None:
73
+ raise ValueError("Cursor hook payload has no conversation/session identifier")
74
+ safe_scope = "".join(
75
+ c if c.isalnum() or c in "-_." else "_" for c in scope
76
+ )
77
+ return (
78
+ Path(cwd)
79
+ / ".execweave"
80
+ / "semantic"
81
+ / "cursor"
82
+ / f"{safe_scope}.jsonl"
83
+ )
84
+
85
+
86
+ def build_parser() -> argparse.ArgumentParser:
87
+ parser = argparse.ArgumentParser(
88
+ prog="execweave-cursor-hook",
89
+ description="Capture Cursor hook input as local ExecWeave semantic telemetry.",
90
+ )
91
+ parser.add_argument("--sidecar", type=Path, default=None)
92
+ parser.add_argument("--strict", action="store_true")
93
+ parser.add_argument("--auto", action="store_true", help=argparse.SUPPRESS)
94
+ parser.add_argument("--print-config", action="store_true")
95
+ parser.add_argument("--command", default="execweave-cursor-hook")
96
+ return parser
97
+
98
+
99
+ def _official_contract_records(
100
+ payload: dict[str, Any],
101
+ *,
102
+ timestamp: str,
103
+ strict: bool,
104
+ ) -> list[dict[str, Any]]:
105
+ """Project the official contract without discarding other hook evidence.
106
+
107
+ Historical or synthetic payloads can omit fields that the current Cursor
108
+ contract documents as required. In normal fail-open mode, skip only this
109
+ contract projection rather than dropping adapter/full-fidelity evidence.
110
+ Strict mode preserves validation behavior for diagnostics and tests.
111
+ """
112
+
113
+ try:
114
+ return cursor_official_hook_semantic_events(
115
+ payload,
116
+ timestamp=timestamp,
117
+ )
118
+ except ValueError as exc:
119
+ if strict:
120
+ raise
121
+ print(
122
+ f"ExecWeave Cursor official hook contract warning: {exc}",
123
+ file=sys.stderr,
124
+ )
125
+ return []
126
+
127
+
128
+ def main(argv: list[str] | None = None) -> int:
129
+ args = build_parser().parse_args(argv)
130
+ if args.print_config:
131
+ print(json.dumps(cursor_hook_config(args.command), indent=2, sort_keys=True))
132
+ return 0
133
+ if args.auto and not os.environ.get("EXECWEAVE_SEMANTIC_SIDECAR"):
134
+ print("{}")
135
+ return 0
136
+ try:
137
+ payload = read_hook_payload()
138
+ sidecar = args.sidecar
139
+ if sidecar is None:
140
+ configured = os.environ.get("EXECWEAVE_SEMANTIC_SIDECAR")
141
+ sidecar = Path(configured) if configured else _default_sidecar(payload)
142
+ sidecar = private_artifact_path(sidecar)
143
+ observed_at = _now()
144
+ store = FullFidelityContentStore(sidecar.parent)
145
+
146
+ summary_records = cursor_hook_to_semantic_events(
147
+ payload,
148
+ timestamp=observed_at,
149
+ )
150
+ append_semantic_records(sidecar, summary_records)
151
+ append_semantic_records(
152
+ sidecar,
153
+ _official_contract_records(
154
+ payload,
155
+ timestamp=observed_at,
156
+ strict=args.strict,
157
+ ),
158
+ )
159
+ if not full_content_capture_enabled():
160
+ print("{}")
161
+ return 0
162
+ append_semantic_records(
163
+ sidecar,
164
+ cursor_hook_to_content_events(
165
+ payload,
166
+ store=store,
167
+ timestamp=observed_at,
168
+ ),
169
+ )
170
+ append_semantic_records(
171
+ sidecar,
172
+ cursor_agent_trace_events(
173
+ payload,
174
+ store=store,
175
+ timestamp=observed_at,
176
+ ),
177
+ )
178
+ append_semantic_records(
179
+ sidecar,
180
+ cursor_delegation_events(
181
+ payload,
182
+ store=store,
183
+ timestamp=observed_at,
184
+ ),
185
+ )
186
+ except (OSError, RuntimeError, TimeoutError, TypeError, ValueError) as exc:
187
+ print(f"ExecWeave Cursor hook warning: {exc}", file=sys.stderr)
188
+ if args.strict:
189
+ return 1
190
+ print("{}")
191
+ return 0
192
+
193
+
194
+ if __name__ == "__main__":
195
+ raise SystemExit(main())