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.
- execweave/__init__.py +3 -0
- execweave/__main__.py +5 -0
- execweave/_conversation_records_core.py +902 -0
- execweave/_dashboard_shell_base.py +148 -0
- execweave/_http_proxy_base.py +565 -0
- execweave/_http_proxy_bounded.py +560 -0
- execweave/_http_proxy_stage.py +509 -0
- execweave/acceptance_output.py +19 -0
- execweave/agent_bootstrap.py +395 -0
- execweave/agent_topology.py +313 -0
- execweave/agent_trace.py +1162 -0
- execweave/agy_preview_sanitize.py +102 -0
- execweave/analysis.py +427 -0
- execweave/anthropic.py +228 -0
- execweave/anthropic_cli.py +171 -0
- execweave/anthropic_full_fidelity.py +385 -0
- execweave/antigravity_adapter.py +112 -0
- execweave/antigravity_adapter_base.py +479 -0
- execweave/antigravity_full_fidelity.py +656 -0
- execweave/antigravity_full_fidelity_base.py +172 -0
- execweave/antigravity_full_fidelity_collaboration_base.py +451 -0
- execweave/antigravity_hook_cli.py +209 -0
- execweave/antigravity_record.py +96 -0
- execweave/antigravity_subagent_linkage.py +449 -0
- execweave/antigravity_trace_capability.py +39 -0
- execweave/auto_specialized.py +547 -0
- execweave/backends.py +102 -0
- execweave/benchmark.py +80 -0
- execweave/claude_adapter.py +517 -0
- execweave/claude_child_transcript.py +314 -0
- execweave/claude_delegation.py +136 -0
- execweave/claude_full_fidelity.py +396 -0
- execweave/claude_hook_cli.py +209 -0
- execweave/claude_hook_contract.py +943 -0
- execweave/claude_model_observer.py +353 -0
- execweave/claude_record.py +106 -0
- execweave/cli.py +629 -0
- execweave/codex_adapter.py +455 -0
- execweave/codex_conversation.py +621 -0
- execweave/codex_full_fidelity.py +287 -0
- execweave/codex_hook_cli.py +301 -0
- execweave/codex_hook_entry.py +207 -0
- execweave/codex_hook_lifecycle.py +363 -0
- execweave/codex_message_diagnostics.py +322 -0
- execweave/codex_message_transport_diagnostics.py +399 -0
- execweave/codex_record.py +199 -0
- execweave/codex_rollout_structures.py +402 -0
- execweave/codex_rollout_trace.py +56 -0
- execweave/codex_rollout_trace_base.py +1509 -0
- execweave/collector.py +768 -0
- execweave/command.py +157 -0
- execweave/command_privacy.py +20 -0
- execweave/content_evidence.py +101 -0
- execweave/content_integrity.py +289 -0
- execweave/content_store.py +273 -0
- execweave/controlled_checks.py +561 -0
- execweave/conversation_archive.py +338 -0
- execweave/conversation_message_identity.py +264 -0
- execweave/conversation_preview.py +66 -0
- execweave/conversation_preview_antigravity.py +32 -0
- execweave/conversation_preview_claude.py +31 -0
- execweave/conversation_preview_codex.py +107 -0
- execweave/conversation_preview_common.py +180 -0
- execweave/conversation_preview_generic.py +188 -0
- execweave/conversation_preview_lines.py +133 -0
- execweave/conversation_preview_transcript.py +121 -0
- execweave/conversation_records.py +349 -0
- execweave/conversation_records_antigravity.py +370 -0
- execweave/conversation_records_codex.py +83 -0
- execweave/conversation_records_common.py +17 -0
- execweave/conversation_records_ollama.py +39 -0
- execweave/correlation.py +818 -0
- execweave/cursor_adapter.py +349 -0
- execweave/cursor_delegation.py +87 -0
- execweave/cursor_delegation_base.py +220 -0
- execweave/cursor_full_fidelity.py +489 -0
- execweave/cursor_hook_cli.py +195 -0
- execweave/cursor_hook_contract.py +439 -0
- execweave/cursor_lifecycle.py +159 -0
- execweave/cursor_record.py +96 -0
- execweave/dashboard_shell.py +283 -0
- execweave/delivery_status.py +94 -0
- execweave/entry.py +98 -0
- execweave/evidence_availability.py +113 -0
- execweave/evidence_grade.py +166 -0
- execweave/fidelity.py +304 -0
- execweave/filesystem.py +327 -0
- execweave/finalization.py +92 -0
- execweave/focus.py +118 -0
- execweave/framework_adapters/__init__.py +34 -0
- execweave/framework_adapters/autogen.py +398 -0
- execweave/framework_adapters/base.py +580 -0
- execweave/framework_adapters/camel.py +270 -0
- execweave/framework_adapters/camel_channel.py +71 -0
- execweave/framework_adapters/metagpt.py +315 -0
- execweave/framework_adapters/metagpt_ollama_compat.py +54 -0
- execweave/framework_adapters/metagpt_recording.py +342 -0
- execweave/framework_adapters/registry.py +90 -0
- execweave/graph.py +590 -0
- execweave/graph_ops.py +472 -0
- execweave/hook_command.py +32 -0
- execweave/hook_delivery.py +32 -0
- execweave/hook_input.py +16 -0
- execweave/http_proxy.py +187 -0
- execweave/http_proxy_cli.py +84 -0
- execweave/inference_gateway.py +476 -0
- execweave/inference_gateway_cli.py +211 -0
- execweave/inference_gateway_full_fidelity.py +613 -0
- execweave/inference_identity.py +76 -0
- execweave/inference_identity_cli.py +60 -0
- execweave/integrity.py +235 -0
- execweave/integrity_cli.py +55 -0
- execweave/investigation_index.py +445 -0
- execweave/litellm_callback.py +211 -0
- execweave/litellm_callback_cli.py +42 -0
- execweave/live.py +445 -0
- execweave/live_core.py +972 -0
- execweave/live_view.py +109 -0
- execweave/live_view_extra_style.py +11 -0
- execweave/live_view_markup.py +29 -0
- execweave/live_view_process_layout.py +650 -0
- execweave/live_view_readability.py +502 -0
- execweave/live_view_script_a.py +59 -0
- execweave/live_view_script_b.py +97 -0
- execweave/live_view_script_c.py +59 -0
- execweave/live_view_script_d.py +40 -0
- execweave/live_view_style.py +34 -0
- execweave/model_runtime.py +560 -0
- execweave/model_runtime_cli.py +265 -0
- execweave/model_runtime_full_fidelity.py +519 -0
- execweave/observation.py +53 -0
- execweave/observation_demo.py +69 -0
- execweave/openai_agents.py +267 -0
- execweave/openai_agents_sdk.py +453 -0
- execweave/openai_compatible.py +239 -0
- execweave/openai_compatible_cli.py +189 -0
- execweave/openai_compatible_full_fidelity.py +438 -0
- execweave/opencode_adapter.py +325 -0
- execweave/opencode_event_contract.py +734 -0
- execweave/opencode_full_fidelity.py +198 -0
- execweave/opencode_hook_cli.py +119 -0
- execweave/opencode_plugin_cli.py +150 -0
- execweave/opencode_record.py +96 -0
- execweave/opencode_task_linkage.py +225 -0
- execweave/overhead_benchmark.py +440 -0
- execweave/privacy.py +84 -0
- execweave/private_io.py +465 -0
- execweave/provider_capability.py +578 -0
- execweave/provider_lifecycle.py +282 -0
- execweave/provider_record.py +290 -0
- execweave/reader_identity.py +13 -0
- execweave/redacted_archive.py +906 -0
- execweave/risk.py +106 -0
- execweave/rule_pack.py +359 -0
- execweave/rule_pack_cli.py +57 -0
- execweave/run_assessment.py +200 -0
- execweave/runtime_endpoint_availability.py +44 -0
- execweave/scalability_benchmark.py +195 -0
- execweave/schema.py +62 -0
- execweave/scope.py +85 -0
- execweave/semantic.py +453 -0
- execweave/session_summary.py +159 -0
- execweave/sink.py +71 -0
- execweave/strace_backend.py +927 -0
- execweave/stream_assembly.py +363 -0
- execweave/task_artifacts.py +192 -0
- execweave/task_validation.py +317 -0
- execweave/theme.py +40 -0
- execweave/top.py +599 -0
- execweave/top_cli.py +150 -0
- execweave/validate.py +196 -0
- execweave/vendor/dagre.min.js +815 -0
- execweave/view_cli.py +49 -0
- execweave/viewer.py +474 -0
- execweave/viewer_agent_panel.py +630 -0
- execweave/viewer_agent_panel_antigravity.py +31 -0
- execweave/viewer_agent_panel_claude.py +9 -0
- execweave/viewer_agent_panel_codex.py +50 -0
- execweave/viewer_agent_panel_cursor.py +9 -0
- execweave/viewer_agent_panel_default.py +79 -0
- execweave/viewer_agent_panel_ollama.py +20 -0
- execweave/viewer_agent_panel_opencode.py +9 -0
- execweave/viewer_agent_panel_registry.py +20 -0
- execweave/viewer_archive_verifier.py +178 -0
- execweave/viewer_background_files.py +86 -0
- execweave/viewer_content_browser.py +307 -0
- execweave/viewer_content_health.py +208 -0
- execweave/viewer_content_inspector.py +309 -0
- execweave/viewer_controlled_checks.py +258 -0
- execweave/viewer_conversation_sync.py +115 -0
- execweave/viewer_dashboard_clean.py +405 -0
- execweave/viewer_dashboard_focus.py +127 -0
- execweave/viewer_dashboard_hardening.py +264 -0
- execweave/viewer_dashboard_hardening_v2.py +62 -0
- execweave/viewer_dashboard_projection.py +261 -0
- execweave/viewer_delivery_status.py +155 -0
- execweave/viewer_edge_decross.py +117 -0
- execweave/viewer_execution_flow.py +611 -0
- execweave/viewer_execution_flow_provider_normalization.py +107 -0
- execweave/viewer_execution_flow_safety.py +349 -0
- execweave/viewer_external_endpoints.py +307 -0
- execweave/viewer_flow_canvas.py +1140 -0
- execweave/viewer_flow_layout.py +922 -0
- execweave/viewer_fold_state.py +31 -0
- execweave/viewer_framework_flow.py +68 -0
- execweave/viewer_framework_model_output.py +133 -0
- execweave/viewer_gif.py +278 -0
- execweave/viewer_history_browser.py +225 -0
- execweave/viewer_investigation.py +268 -0
- execweave/viewer_layout_geometry.py +192 -0
- execweave/viewer_layout_v2.py +429 -0
- execweave/viewer_layout_v2_detached.py +25 -0
- execweave/viewer_limits.py +93 -0
- execweave/viewer_live_layout.py +61 -0
- execweave/viewer_message_handoffs.py +114 -0
- execweave/viewer_projection.py +488 -0
- execweave/viewer_projection_base.py +522 -0
- execweave/viewer_projection_bridges.py +214 -0
- execweave/viewer_recorded_sources.py +146 -0
- execweave/viewer_run_assessment.py +274 -0
- execweave/viewer_run_guide.py +175 -0
- execweave/viewer_runtime_evidence.py +258 -0
- execweave/viewer_semantic_projection.py +641 -0
- execweave/viewer_session_flow.py +491 -0
- execweave/viewer_structure_share.py +132 -0
- execweave/viewer_task_validation.py +241 -0
- execweave/viewer_tool_mirror_identity.py +75 -0
- execweave/viewer_workflow_mode.py +127 -0
- execweave/workflow.py +183 -0
- execweave-1.0.0.dist-info/METADATA +462 -0
- execweave-1.0.0.dist-info/RECORD +234 -0
- execweave-1.0.0.dist-info/WHEEL +4 -0
- execweave-1.0.0.dist-info/entry_points.txt +30 -0
- execweave-1.0.0.dist-info/licenses/LICENSE +16 -0
execweave/collector.py
ADDED
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import ctypes
|
|
4
|
+
import os
|
|
5
|
+
import subprocess
|
|
6
|
+
import sys
|
|
7
|
+
import time
|
|
8
|
+
from dataclasses import dataclass
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Iterable
|
|
11
|
+
|
|
12
|
+
import psutil
|
|
13
|
+
|
|
14
|
+
from . import __version__
|
|
15
|
+
from .auto_specialized import (
|
|
16
|
+
auto_specialized_launch,
|
|
17
|
+
auto_specialized_probe,
|
|
18
|
+
prepare_post_command_specialized_probe,
|
|
19
|
+
prepare_live_specialized_probe,
|
|
20
|
+
run_post_command_specialized_probe,
|
|
21
|
+
)
|
|
22
|
+
from .command import resolve_launch_command
|
|
23
|
+
from .cursor_lifecycle import (
|
|
24
|
+
is_cursor_invocation,
|
|
25
|
+
process_baseline,
|
|
26
|
+
run_cursor_handoff,
|
|
27
|
+
)
|
|
28
|
+
from .filesystem import FileWatcher
|
|
29
|
+
from .privacy import content_capture_policy
|
|
30
|
+
from .schema import Entity, RuntimeEvent
|
|
31
|
+
from .command_privacy import public_command
|
|
32
|
+
from .sink import JsonlSink
|
|
33
|
+
from .risk import privilege_process_trigger, risk_attributes
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
_PR_SET_CHILD_SUBREAPER = 36
|
|
37
|
+
_PR_GET_CHILD_SUBREAPER = 37
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(frozen=True)
|
|
41
|
+
class ProcessSnapshot:
|
|
42
|
+
pid: int
|
|
43
|
+
ppid: int
|
|
44
|
+
name: str
|
|
45
|
+
cmdline: list[str]
|
|
46
|
+
exe: str | None
|
|
47
|
+
create_time: float
|
|
48
|
+
|
|
49
|
+
@property
|
|
50
|
+
def entity(self) -> Entity:
|
|
51
|
+
identity = f"{self.pid}:{int(self.create_time * 1_000_000)}"
|
|
52
|
+
return Entity(
|
|
53
|
+
type="process",
|
|
54
|
+
id=f"process:{identity}",
|
|
55
|
+
name=self.name,
|
|
56
|
+
attributes={
|
|
57
|
+
"pid": self.pid,
|
|
58
|
+
"ppid": self.ppid,
|
|
59
|
+
"cmdline": public_command(self.cmdline),
|
|
60
|
+
"argv_redacted": True,
|
|
61
|
+
"exe": self.exe,
|
|
62
|
+
"create_time": self.create_time,
|
|
63
|
+
},
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _safe_process_snapshot(proc: psutil.Process) -> ProcessSnapshot | None:
|
|
68
|
+
try:
|
|
69
|
+
with proc.oneshot():
|
|
70
|
+
try:
|
|
71
|
+
exe = proc.exe()
|
|
72
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
73
|
+
exe = None
|
|
74
|
+
return ProcessSnapshot(
|
|
75
|
+
pid=proc.pid,
|
|
76
|
+
ppid=proc.ppid(),
|
|
77
|
+
name=proc.name(),
|
|
78
|
+
cmdline=proc.cmdline(),
|
|
79
|
+
exe=exe,
|
|
80
|
+
create_time=proc.create_time(),
|
|
81
|
+
)
|
|
82
|
+
except (
|
|
83
|
+
psutil.NoSuchProcess,
|
|
84
|
+
psutil.AccessDenied,
|
|
85
|
+
psutil.ZombieProcess,
|
|
86
|
+
# macOS can surface protected-process cmdline denial as a SystemError
|
|
87
|
+
# from psutil's C extension instead of AccessDenied. Treat that process
|
|
88
|
+
# as unobservable, just like the other permission-race cases.
|
|
89
|
+
SystemError,
|
|
90
|
+
):
|
|
91
|
+
return None
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _format_address(address: object) -> str | None:
|
|
95
|
+
if not address:
|
|
96
|
+
return None
|
|
97
|
+
ip = getattr(address, "ip", None)
|
|
98
|
+
port = getattr(address, "port", None)
|
|
99
|
+
if ip is not None and port is not None:
|
|
100
|
+
return f"{ip}:{port}"
|
|
101
|
+
if isinstance(address, tuple) and len(address) >= 2:
|
|
102
|
+
return f"{address[0]}:{address[1]}"
|
|
103
|
+
return str(address)
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def infer_agent_name(command: Iterable[str]) -> str:
|
|
107
|
+
parts = list(command)
|
|
108
|
+
if not parts:
|
|
109
|
+
return "unknown-agent"
|
|
110
|
+
basename = Path(parts[0]).name
|
|
111
|
+
executable = basename.lower()
|
|
112
|
+
for suffix in (".exe", ".cmd", ".bat", ".ps1"):
|
|
113
|
+
if executable.endswith(suffix):
|
|
114
|
+
executable = executable[: -len(suffix)]
|
|
115
|
+
break
|
|
116
|
+
known = {
|
|
117
|
+
"claude": "Claude Code",
|
|
118
|
+
"codex": "OpenAI Codex",
|
|
119
|
+
"agy": "Antigravity",
|
|
120
|
+
"antigravity": "Antigravity",
|
|
121
|
+
"cursor": "Cursor",
|
|
122
|
+
"opencode": "OpenCode",
|
|
123
|
+
"ollama": "Ollama",
|
|
124
|
+
}
|
|
125
|
+
return known.get(executable, basename)
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class RuntimeCollector:
|
|
129
|
+
"""Portable polling collector used on all platforms and as a fallback backend."""
|
|
130
|
+
|
|
131
|
+
backend_name = "portable"
|
|
132
|
+
|
|
133
|
+
def __init__(
|
|
134
|
+
self,
|
|
135
|
+
*,
|
|
136
|
+
session_id: str,
|
|
137
|
+
sink: JsonlSink,
|
|
138
|
+
watch_root: Path,
|
|
139
|
+
poll_interval: float = 0.10,
|
|
140
|
+
collect_filesystem: bool = True,
|
|
141
|
+
collect_network: bool = True,
|
|
142
|
+
) -> None:
|
|
143
|
+
self.session_id = session_id
|
|
144
|
+
self.sink = sink
|
|
145
|
+
self.watch_root = watch_root.expanduser().resolve()
|
|
146
|
+
self.poll_interval = max(0.02, poll_interval)
|
|
147
|
+
self.collect_filesystem = collect_filesystem
|
|
148
|
+
self.collect_network = collect_network
|
|
149
|
+
self._seen_processes: dict[int, ProcessSnapshot] = {}
|
|
150
|
+
self._discovered_processes: dict[int, ProcessSnapshot] = {}
|
|
151
|
+
self._seen_connections: set[tuple[str, str | None, str | None, str]] = set()
|
|
152
|
+
self._network_sample_attempts = 0
|
|
153
|
+
self._network_sample_successes = 0
|
|
154
|
+
self._network_sample_errors = 0
|
|
155
|
+
self._network_access_denied = 0
|
|
156
|
+
self._collector_children_baseline: dict[int, float] | None = None
|
|
157
|
+
self._preserved_processes: set[tuple[int, float]] = set()
|
|
158
|
+
self._cursor_handoff_info: dict[str, object] = {}
|
|
159
|
+
|
|
160
|
+
def _filesystem_excluded_roots(self) -> list[Path]:
|
|
161
|
+
"""Exclude reserved artifacts, never the caller's entire output directory."""
|
|
162
|
+
sink_path = self.sink.path.expanduser().resolve()
|
|
163
|
+
run_root = sink_path.parent
|
|
164
|
+
excluded = [(self.watch_root / ".execweave").resolve(), sink_path]
|
|
165
|
+
reserved = (
|
|
166
|
+
"events.jsonl", "events.semantic.jsonl", "semantic.jsonl", "graph.json",
|
|
167
|
+
"viewer.html", "conversations.json", "finalization.json", "content",
|
|
168
|
+
)
|
|
169
|
+
excluded.extend(run_root / name for name in reserved)
|
|
170
|
+
configured = os.environ.get("EXECWEAVE_SEMANTIC_SIDECAR")
|
|
171
|
+
if configured:
|
|
172
|
+
excluded.append(Path(configured).expanduser().resolve())
|
|
173
|
+
return excluded
|
|
174
|
+
|
|
175
|
+
def run(self, command: list[str]) -> int:
|
|
176
|
+
if not command:
|
|
177
|
+
raise ValueError("command must not be empty")
|
|
178
|
+
|
|
179
|
+
launch_command: list[str] | None = None
|
|
180
|
+
cursor_invocation = os.name == "nt" and is_cursor_invocation(command)
|
|
181
|
+
cursor_baseline: set[tuple[int, float]] = set()
|
|
182
|
+
self._preserved_processes = set()
|
|
183
|
+
self._cursor_handoff_info = {
|
|
184
|
+
"cursor_lifecycle_handoff": "launcher" if cursor_invocation else "not_applicable",
|
|
185
|
+
}
|
|
186
|
+
privacy = content_capture_policy()
|
|
187
|
+
agent_name = infer_agent_name(command)
|
|
188
|
+
agent = Entity(type="agent", id=f"agent:{agent_name}", name=agent_name)
|
|
189
|
+
session = Entity(
|
|
190
|
+
type="session",
|
|
191
|
+
id=f"session:{self.session_id}",
|
|
192
|
+
name=self.session_id,
|
|
193
|
+
attributes={
|
|
194
|
+
"command": public_command(command),
|
|
195
|
+
"argv_redacted": True,
|
|
196
|
+
"cwd": str(self.watch_root),
|
|
197
|
+
"backend": self.backend_name,
|
|
198
|
+
"execweave_version": __version__,
|
|
199
|
+
"content_capture_mode": privacy.mode,
|
|
200
|
+
"content_capture_policy_state": privacy.state,
|
|
201
|
+
},
|
|
202
|
+
)
|
|
203
|
+
self.sink.emit(
|
|
204
|
+
RuntimeEvent.create(
|
|
205
|
+
session_id=self.session_id,
|
|
206
|
+
event_type="session.started",
|
|
207
|
+
relation="STARTED_SESSION",
|
|
208
|
+
source=agent,
|
|
209
|
+
target=session,
|
|
210
|
+
attributes={
|
|
211
|
+
"collector_pid": os.getpid(),
|
|
212
|
+
"short_lived_process_loss_possible": True,
|
|
213
|
+
"filesystem_collection_enabled": self.collect_filesystem,
|
|
214
|
+
"process_coverage": "polling_not_exhaustive",
|
|
215
|
+
"backend": self.backend_name,
|
|
216
|
+
"execweave_version": __version__,
|
|
217
|
+
"python_executable": sys.executable,
|
|
218
|
+
"python_version": sys.version.split()[0],
|
|
219
|
+
"package_path": str(Path(__file__).resolve().parent),
|
|
220
|
+
"content_capture_mode": privacy.mode,
|
|
221
|
+
"content_capture_policy_state": privacy.state,
|
|
222
|
+
},
|
|
223
|
+
)
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
self._record_hook_state(command, session, phase="session_start")
|
|
227
|
+
|
|
228
|
+
watcher: FileWatcher | None = None
|
|
229
|
+
if self.collect_filesystem:
|
|
230
|
+
watcher = FileWatcher(
|
|
231
|
+
root=self.watch_root,
|
|
232
|
+
session_id=self.session_id,
|
|
233
|
+
session_entity=session,
|
|
234
|
+
sink=self.sink,
|
|
235
|
+
excluded_roots=self._filesystem_excluded_roots(),
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
process: subprocess.Popen[bytes] | None = None
|
|
239
|
+
owned: list[psutil.Process] = []
|
|
240
|
+
return_code = 1
|
|
241
|
+
cursor_root_snapshot: ProcessSnapshot | None = None
|
|
242
|
+
interrupted = False
|
|
243
|
+
collector_error_type: str | None = None
|
|
244
|
+
workload_terminated_due_to_collector_error = False
|
|
245
|
+
subreaper_previous: bool | None = None
|
|
246
|
+
live_probe_admission = None
|
|
247
|
+
post_command_probe = None
|
|
248
|
+
try:
|
|
249
|
+
if watcher is not None:
|
|
250
|
+
watcher.start()
|
|
251
|
+
|
|
252
|
+
try:
|
|
253
|
+
launch_command = resolve_launch_command(command)
|
|
254
|
+
cursor_baseline = (
|
|
255
|
+
process_baseline(launch_command[0]) if cursor_invocation else set()
|
|
256
|
+
)
|
|
257
|
+
live_probe_admission = prepare_live_specialized_probe(command)
|
|
258
|
+
post_command_probe = prepare_post_command_specialized_probe(command)
|
|
259
|
+
with auto_specialized_launch(
|
|
260
|
+
command,
|
|
261
|
+
server_relay=True,
|
|
262
|
+
) as launch_environment:
|
|
263
|
+
subreaper_previous = self._enable_linux_child_subreaper()
|
|
264
|
+
self._collector_children_baseline = (
|
|
265
|
+
self._direct_child_lifetimes()
|
|
266
|
+
if subreaper_previous is not None
|
|
267
|
+
else None
|
|
268
|
+
)
|
|
269
|
+
process = subprocess.Popen(
|
|
270
|
+
launch_command,
|
|
271
|
+
cwd=str(self.watch_root),
|
|
272
|
+
env=launch_environment,
|
|
273
|
+
)
|
|
274
|
+
root = psutil.Process(process.pid)
|
|
275
|
+
snapshot = _safe_process_snapshot(root)
|
|
276
|
+
if snapshot is not None:
|
|
277
|
+
self._record_process_start(snapshot, parent=session, relation="LAUNCHED")
|
|
278
|
+
cursor_root_snapshot = snapshot if cursor_invocation else None
|
|
279
|
+
|
|
280
|
+
with auto_specialized_probe(command, admission=live_probe_admission):
|
|
281
|
+
while process.poll() is None:
|
|
282
|
+
self._sample_process_tree(root)
|
|
283
|
+
time.sleep(self.poll_interval)
|
|
284
|
+
|
|
285
|
+
self._sample_process_tree(root)
|
|
286
|
+
self._mark_disappeared_processes(set())
|
|
287
|
+
return_code = int(process.returncode or 0)
|
|
288
|
+
|
|
289
|
+
if cursor_invocation and cursor_root_snapshot is not None:
|
|
290
|
+
run_cursor_handoff(
|
|
291
|
+
self,
|
|
292
|
+
root_snapshot=cursor_root_snapshot,
|
|
293
|
+
executable=launch_command[0],
|
|
294
|
+
baseline=cursor_baseline,
|
|
295
|
+
)
|
|
296
|
+
except KeyboardInterrupt:
|
|
297
|
+
interrupted = True
|
|
298
|
+
return_code = 130
|
|
299
|
+
|
|
300
|
+
if not interrupted:
|
|
301
|
+
run_post_command_specialized_probe(
|
|
302
|
+
post_command_probe,
|
|
303
|
+
return_code=return_code,
|
|
304
|
+
)
|
|
305
|
+
return return_code
|
|
306
|
+
except BaseException as exc:
|
|
307
|
+
collector_error_type = type(exc).__name__
|
|
308
|
+
raise
|
|
309
|
+
finally:
|
|
310
|
+
# A workload exception is a nonzero child exit, not an exception in
|
|
311
|
+
# this collector. Finalize observed descendants on every exit path,
|
|
312
|
+
# including successful launchers that leave reparented children.
|
|
313
|
+
if process is not None:
|
|
314
|
+
owned = self._owned_live_processes(process)
|
|
315
|
+
workload_terminated_due_to_collector_error = (
|
|
316
|
+
collector_error_type is not None and bool(owned)
|
|
317
|
+
)
|
|
318
|
+
self._terminate_process_tree(process, owned=owned)
|
|
319
|
+
self._restore_linux_child_subreaper(subreaper_previous)
|
|
320
|
+
if watcher is not None:
|
|
321
|
+
watcher.stop()
|
|
322
|
+
self._record_hook_state(command, session, phase="session_end")
|
|
323
|
+
self._record_observation_warnings(command, session)
|
|
324
|
+
# A failed termination must not become a false clean result merely
|
|
325
|
+
# because the selected child has since reparented out of the tree.
|
|
326
|
+
workload_alive_after_cleanup = any(
|
|
327
|
+
self._process_is_live(candidate) for candidate in owned
|
|
328
|
+
) or bool(process is not None and self._owned_live_processes(process))
|
|
329
|
+
self._collector_children_baseline = None
|
|
330
|
+
self.sink.emit(
|
|
331
|
+
RuntimeEvent.create(
|
|
332
|
+
session_id=self.session_id,
|
|
333
|
+
event_type="session.finished",
|
|
334
|
+
relation="FINISHED_SESSION",
|
|
335
|
+
source=session,
|
|
336
|
+
attributes={
|
|
337
|
+
"return_code": return_code,
|
|
338
|
+
"root_pid": process.pid if process is not None else None,
|
|
339
|
+
"backend": self.backend_name,
|
|
340
|
+
"interrupted": interrupted,
|
|
341
|
+
"collector_failed": collector_error_type is not None,
|
|
342
|
+
"collector_error_type": collector_error_type,
|
|
343
|
+
"workload_terminated_due_to_collector_error": (
|
|
344
|
+
workload_terminated_due_to_collector_error
|
|
345
|
+
),
|
|
346
|
+
"workload_alive_after_cleanup": workload_alive_after_cleanup,
|
|
347
|
+
"short_lived_process_loss_possible": True,
|
|
348
|
+
"filesystem_collection_enabled": self.collect_filesystem,
|
|
349
|
+
"process_coverage": "polling_not_exhaustive",
|
|
350
|
+
"network_collection_status": self._network_collection_status(),
|
|
351
|
+
"network_sample_attempts": self._network_sample_attempts,
|
|
352
|
+
"network_sample_successes": self._network_sample_successes,
|
|
353
|
+
"network_sample_errors": self._network_sample_errors,
|
|
354
|
+
"network_access_denied": self._network_access_denied,
|
|
355
|
+
"execweave_version": __version__,
|
|
356
|
+
**self._cursor_handoff_info,
|
|
357
|
+
},
|
|
358
|
+
)
|
|
359
|
+
)
|
|
360
|
+
|
|
361
|
+
def _record_hook_state(self, command: list[str], session: Entity, *, phase: str) -> None:
|
|
362
|
+
from .agent_bootstrap import inspect_supported_agent, supported_agent
|
|
363
|
+
|
|
364
|
+
provider = supported_agent(command)
|
|
365
|
+
if provider is None:
|
|
366
|
+
return
|
|
367
|
+
result = inspect_supported_agent(command)
|
|
368
|
+
target = Entity(
|
|
369
|
+
type="hook_configuration",
|
|
370
|
+
id=f"hook:{self.session_id}:{provider}",
|
|
371
|
+
name=provider,
|
|
372
|
+
attributes={"provider": provider, "configuration_path": result.path},
|
|
373
|
+
)
|
|
374
|
+
self.sink.emit(RuntimeEvent.create(
|
|
375
|
+
session_id=self.session_id, event_type="observation.hook_state",
|
|
376
|
+
relation="HAS_HOOK_CONFIGURATION", source=session, target=target,
|
|
377
|
+
attributes={
|
|
378
|
+
"phase": phase, "provider": provider, "status": result.status,
|
|
379
|
+
"configuration_path": result.path, "changed": result.changed,
|
|
380
|
+
"delivery_proven": False,
|
|
381
|
+
},
|
|
382
|
+
))
|
|
383
|
+
|
|
384
|
+
def _record_privilege_risk(self, snapshot: ProcessSnapshot) -> None:
|
|
385
|
+
trigger = privilege_process_trigger(
|
|
386
|
+
name=snapshot.name, cmdline=snapshot.cmdline, exe=snapshot.exe
|
|
387
|
+
)
|
|
388
|
+
if trigger is None:
|
|
389
|
+
return
|
|
390
|
+
target = Entity(
|
|
391
|
+
type="risk_record", id=f"risk:{self.session_id}:privilege:{snapshot.entity.id}",
|
|
392
|
+
name="privileged execution observed",
|
|
393
|
+
)
|
|
394
|
+
self.sink.emit(RuntimeEvent.create(
|
|
395
|
+
session_id=self.session_id, event_type="risk.privilege_process",
|
|
396
|
+
relation="HAS_RISK", source=snapshot.entity, target=target,
|
|
397
|
+
attributes=risk_attributes(
|
|
398
|
+
"privileged_execution", trigger, process_id=snapshot.entity.id
|
|
399
|
+
),
|
|
400
|
+
))
|
|
401
|
+
|
|
402
|
+
def _record_observation_warnings(self, command: list[str], session: Entity) -> None:
|
|
403
|
+
from .agent_bootstrap import inspect_supported_agent, supported_agent
|
|
404
|
+
from .hook_delivery import sidecar_delivery
|
|
405
|
+
|
|
406
|
+
def warning(reason: str, **details: object) -> None:
|
|
407
|
+
self.sink.emit(RuntimeEvent.create(
|
|
408
|
+
session_id=self.session_id, event_type="observation.warning",
|
|
409
|
+
relation="HAS_OBSERVATION_WARNING", source=session,
|
|
410
|
+
target=Entity(type="observation_warning", id=f"warning:{self.session_id}:{reason}",
|
|
411
|
+
name=reason),
|
|
412
|
+
attributes={"reason": reason, "severity": "warning", **details},
|
|
413
|
+
))
|
|
414
|
+
|
|
415
|
+
warning("short_lived_process_loss_possible", backend=self.backend_name,
|
|
416
|
+
poll_interval_seconds=self.poll_interval, missed_process_lower_bound=None)
|
|
417
|
+
provider = supported_agent(command)
|
|
418
|
+
if provider is not None:
|
|
419
|
+
configured = inspect_supported_agent(command)
|
|
420
|
+
delivery = sidecar_delivery(os.environ.get("EXECWEAVE_SEMANTIC_SIDECAR"))
|
|
421
|
+
if delivery["state"] != "records_present":
|
|
422
|
+
warning("hook_no_evidence", provider=provider,
|
|
423
|
+
hook_configuration_state=configured.status, delivery=delivery)
|
|
424
|
+
|
|
425
|
+
@staticmethod
|
|
426
|
+
def _linux_child_subreaper_state() -> bool | None:
|
|
427
|
+
if not sys.platform.startswith("linux"):
|
|
428
|
+
return None
|
|
429
|
+
try:
|
|
430
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
431
|
+
state = ctypes.c_int()
|
|
432
|
+
if libc.prctl(_PR_GET_CHILD_SUBREAPER, ctypes.byref(state), 0, 0, 0) != 0:
|
|
433
|
+
return None
|
|
434
|
+
return bool(state.value)
|
|
435
|
+
except (AttributeError, OSError):
|
|
436
|
+
return None
|
|
437
|
+
|
|
438
|
+
@staticmethod
|
|
439
|
+
def _set_linux_child_subreaper(enabled: bool) -> bool:
|
|
440
|
+
if not sys.platform.startswith("linux"):
|
|
441
|
+
return False
|
|
442
|
+
try:
|
|
443
|
+
libc = ctypes.CDLL(None, use_errno=True)
|
|
444
|
+
return libc.prctl(_PR_SET_CHILD_SUBREAPER, int(enabled), 0, 0, 0) == 0
|
|
445
|
+
except (AttributeError, OSError):
|
|
446
|
+
return False
|
|
447
|
+
|
|
448
|
+
@classmethod
|
|
449
|
+
def _enable_linux_child_subreaper(cls) -> bool | None:
|
|
450
|
+
previous = cls._linux_child_subreaper_state()
|
|
451
|
+
if previous is None:
|
|
452
|
+
return None
|
|
453
|
+
if not previous and not cls._set_linux_child_subreaper(True):
|
|
454
|
+
return None
|
|
455
|
+
return previous
|
|
456
|
+
|
|
457
|
+
@classmethod
|
|
458
|
+
def _restore_linux_child_subreaper(cls, previous: bool | None) -> None:
|
|
459
|
+
if previous is False:
|
|
460
|
+
cls._set_linux_child_subreaper(False)
|
|
461
|
+
|
|
462
|
+
@staticmethod
|
|
463
|
+
def _direct_child_lifetimes() -> dict[int, float]:
|
|
464
|
+
try:
|
|
465
|
+
children = psutil.Process(os.getpid()).children(recursive=False)
|
|
466
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
467
|
+
return {}
|
|
468
|
+
lifetimes: dict[int, float] = {}
|
|
469
|
+
for child in children:
|
|
470
|
+
try:
|
|
471
|
+
lifetimes[child.pid] = child.create_time()
|
|
472
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
473
|
+
continue
|
|
474
|
+
return lifetimes
|
|
475
|
+
|
|
476
|
+
@staticmethod
|
|
477
|
+
def _process_is_live(proc: psutil.Process) -> bool:
|
|
478
|
+
try:
|
|
479
|
+
if not proc.is_running():
|
|
480
|
+
return False
|
|
481
|
+
try:
|
|
482
|
+
return proc.status() != psutil.STATUS_ZOMBIE
|
|
483
|
+
except psutil.AccessDenied:
|
|
484
|
+
return True
|
|
485
|
+
except (psutil.NoSuchProcess, psutil.ZombieProcess):
|
|
486
|
+
return False
|
|
487
|
+
|
|
488
|
+
def _process_for_snapshot(self, snapshot: ProcessSnapshot) -> psutil.Process | None:
|
|
489
|
+
try:
|
|
490
|
+
process = psutil.Process(snapshot.pid)
|
|
491
|
+
# Cleanup needs identity, not readable exe/name/cmdline metadata.
|
|
492
|
+
# Never treat a reused PID as the process we originally observed.
|
|
493
|
+
if process.create_time() != snapshot.create_time:
|
|
494
|
+
return None
|
|
495
|
+
return process
|
|
496
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
497
|
+
return None
|
|
498
|
+
|
|
499
|
+
def _owned_live_processes(
|
|
500
|
+
self,
|
|
501
|
+
process: subprocess.Popen[bytes],
|
|
502
|
+
) -> list[psutil.Process]:
|
|
503
|
+
"""Return live processes still owned by this collector's launched workload.
|
|
504
|
+
|
|
505
|
+
A shell or launcher can exit before its children. Relying only on the current
|
|
506
|
+
root process tree then loses those reparented descendants. The collector's
|
|
507
|
+
lifetime-qualified process snapshots preserve enough ownership evidence to
|
|
508
|
+
find and clean them without risking a reused PID.
|
|
509
|
+
"""
|
|
510
|
+
owned: dict[int, psutil.Process] = {}
|
|
511
|
+
|
|
512
|
+
def add_with_descendants(candidate: psutil.Process) -> None:
|
|
513
|
+
if not self._process_is_live(candidate):
|
|
514
|
+
return
|
|
515
|
+
if (candidate.pid, candidate.create_time()) in self._preserved_processes:
|
|
516
|
+
return
|
|
517
|
+
owned[candidate.pid] = candidate
|
|
518
|
+
try:
|
|
519
|
+
descendants = candidate.children(recursive=True)
|
|
520
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
521
|
+
descendants = []
|
|
522
|
+
for child in descendants:
|
|
523
|
+
if self._process_is_live(child):
|
|
524
|
+
if (child.pid, child.create_time()) in self._preserved_processes:
|
|
525
|
+
continue
|
|
526
|
+
owned[child.pid] = child
|
|
527
|
+
|
|
528
|
+
if process.poll() is None:
|
|
529
|
+
try:
|
|
530
|
+
add_with_descendants(psutil.Process(process.pid))
|
|
531
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
532
|
+
pass
|
|
533
|
+
|
|
534
|
+
baseline = self._collector_children_baseline
|
|
535
|
+
if baseline is not None:
|
|
536
|
+
try:
|
|
537
|
+
collector_children = psutil.Process(os.getpid()).children(recursive=False)
|
|
538
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
539
|
+
collector_children = []
|
|
540
|
+
for candidate in collector_children:
|
|
541
|
+
try:
|
|
542
|
+
create_time = candidate.create_time()
|
|
543
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
544
|
+
continue
|
|
545
|
+
if baseline.get(candidate.pid) == create_time:
|
|
546
|
+
continue
|
|
547
|
+
add_with_descendants(candidate)
|
|
548
|
+
|
|
549
|
+
snapshots = {**self._seen_processes, **self._discovered_processes}
|
|
550
|
+
for snapshot in snapshots.values():
|
|
551
|
+
tracked = self._process_for_snapshot(snapshot)
|
|
552
|
+
if tracked is not None:
|
|
553
|
+
add_with_descendants(tracked)
|
|
554
|
+
|
|
555
|
+
return list(owned.values())
|
|
556
|
+
|
|
557
|
+
def _terminate_process_tree(
|
|
558
|
+
self,
|
|
559
|
+
process: subprocess.Popen[bytes],
|
|
560
|
+
*,
|
|
561
|
+
owned: list[psutil.Process] | None = None,
|
|
562
|
+
) -> None:
|
|
563
|
+
# A caller's selection must survive reparenting between discovery and
|
|
564
|
+
# termination. Re-querying here can lose children we just proved owned.
|
|
565
|
+
if owned is None:
|
|
566
|
+
owned = self._owned_live_processes(process)
|
|
567
|
+
# Terminate descendants/tracked orphans before the launcher/root when it is
|
|
568
|
+
# still alive. Holding psutil handles also keeps cleanup robust if a parent
|
|
569
|
+
# exits while the loop is running.
|
|
570
|
+
for candidate in sorted(owned, key=lambda item: item.pid == process.pid):
|
|
571
|
+
try:
|
|
572
|
+
candidate.terminate()
|
|
573
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
574
|
+
pass
|
|
575
|
+
|
|
576
|
+
if owned:
|
|
577
|
+
_, alive = psutil.wait_procs(owned, timeout=2.0)
|
|
578
|
+
for candidate in alive:
|
|
579
|
+
try:
|
|
580
|
+
candidate.kill()
|
|
581
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
582
|
+
pass
|
|
583
|
+
if alive:
|
|
584
|
+
psutil.wait_procs(alive, timeout=2.0)
|
|
585
|
+
|
|
586
|
+
try:
|
|
587
|
+
process.wait(timeout=2.0)
|
|
588
|
+
except subprocess.TimeoutExpired:
|
|
589
|
+
try:
|
|
590
|
+
process.kill()
|
|
591
|
+
except OSError:
|
|
592
|
+
return
|
|
593
|
+
try:
|
|
594
|
+
process.wait(timeout=2.0)
|
|
595
|
+
except subprocess.TimeoutExpired:
|
|
596
|
+
pass
|
|
597
|
+
|
|
598
|
+
@staticmethod
|
|
599
|
+
def _same_process_lifetime(left: ProcessSnapshot, right: ProcessSnapshot) -> bool:
|
|
600
|
+
return left.entity.id == right.entity.id
|
|
601
|
+
|
|
602
|
+
def _sample_process_tree(self, root: psutil.Process) -> None:
|
|
603
|
+
processes: list[psutil.Process] = []
|
|
604
|
+
try:
|
|
605
|
+
processes.append(root)
|
|
606
|
+
processes.extend(root.children(recursive=True))
|
|
607
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
608
|
+
pass
|
|
609
|
+
|
|
610
|
+
current: dict[int, ProcessSnapshot] = {}
|
|
611
|
+
process_objects: dict[int, psutil.Process] = {}
|
|
612
|
+
for proc in processes:
|
|
613
|
+
snapshot = _safe_process_snapshot(proc)
|
|
614
|
+
if snapshot is None:
|
|
615
|
+
continue
|
|
616
|
+
current[snapshot.pid] = snapshot
|
|
617
|
+
process_objects[snapshot.pid] = proc
|
|
618
|
+
|
|
619
|
+
# Preserve the complete discovered tree before any event/network callback
|
|
620
|
+
# can raise. _seen_processes only contains successfully registered starts;
|
|
621
|
+
# a root callback can fail after its children have already reparented.
|
|
622
|
+
# Retain only the latest sample here, not an unbounded lifetime history.
|
|
623
|
+
self._discovered_processes = current
|
|
624
|
+
for snapshot in current.values():
|
|
625
|
+
previous = self._seen_processes.get(snapshot.pid)
|
|
626
|
+
if previous is None or not self._same_process_lifetime(previous, snapshot):
|
|
627
|
+
parent_snapshot = current.get(snapshot.ppid) or self._seen_processes.get(
|
|
628
|
+
snapshot.ppid
|
|
629
|
+
)
|
|
630
|
+
parent = (
|
|
631
|
+
parent_snapshot.entity
|
|
632
|
+
if parent_snapshot is not None
|
|
633
|
+
else Entity(
|
|
634
|
+
type="process_reference",
|
|
635
|
+
id=f"process-pid:{snapshot.ppid}",
|
|
636
|
+
name=str(snapshot.ppid),
|
|
637
|
+
attributes={"pid": snapshot.ppid, "unresolved": True},
|
|
638
|
+
)
|
|
639
|
+
)
|
|
640
|
+
self._record_process_start(snapshot, parent=parent, relation="SPAWNED")
|
|
641
|
+
if self.collect_network:
|
|
642
|
+
self._sample_network(process_objects[snapshot.pid], snapshot)
|
|
643
|
+
|
|
644
|
+
self._mark_disappeared_processes(set(current))
|
|
645
|
+
|
|
646
|
+
def _record_process_start(
|
|
647
|
+
self,
|
|
648
|
+
snapshot: ProcessSnapshot,
|
|
649
|
+
*,
|
|
650
|
+
parent: Entity,
|
|
651
|
+
relation: str,
|
|
652
|
+
) -> None:
|
|
653
|
+
previous = self._seen_processes.get(snapshot.pid)
|
|
654
|
+
if previous is not None:
|
|
655
|
+
if self._same_process_lifetime(previous, snapshot):
|
|
656
|
+
return
|
|
657
|
+
self._seen_processes.pop(snapshot.pid, None)
|
|
658
|
+
self._record_process_exit(previous, reason="pid_reused")
|
|
659
|
+
|
|
660
|
+
self._seen_processes[snapshot.pid] = snapshot
|
|
661
|
+
self.sink.emit(
|
|
662
|
+
RuntimeEvent.create(
|
|
663
|
+
session_id=self.session_id,
|
|
664
|
+
event_type="process.started",
|
|
665
|
+
relation=relation,
|
|
666
|
+
source=parent,
|
|
667
|
+
target=snapshot.entity,
|
|
668
|
+
attributes={
|
|
669
|
+
"attribution": "polling",
|
|
670
|
+
"causal": relation == "LAUNCHED",
|
|
671
|
+
"backend": self.backend_name,
|
|
672
|
+
},
|
|
673
|
+
)
|
|
674
|
+
)
|
|
675
|
+
self._record_privilege_risk(snapshot)
|
|
676
|
+
|
|
677
|
+
def _record_process_exit(self, snapshot: ProcessSnapshot, *, reason: str | None = None) -> None:
|
|
678
|
+
attributes: dict[str, object] = {
|
|
679
|
+
"attribution": "polling",
|
|
680
|
+
"backend": self.backend_name,
|
|
681
|
+
}
|
|
682
|
+
if reason is not None:
|
|
683
|
+
attributes["reason"] = reason
|
|
684
|
+
self.sink.emit(
|
|
685
|
+
RuntimeEvent.create(
|
|
686
|
+
session_id=self.session_id,
|
|
687
|
+
event_type="process.exited",
|
|
688
|
+
relation="EXITED",
|
|
689
|
+
source=snapshot.entity,
|
|
690
|
+
attributes=attributes,
|
|
691
|
+
)
|
|
692
|
+
)
|
|
693
|
+
|
|
694
|
+
def _mark_disappeared_processes(self, active_pids: set[int]) -> None:
|
|
695
|
+
for pid in list(self._seen_processes):
|
|
696
|
+
if pid in active_pids:
|
|
697
|
+
continue
|
|
698
|
+
|
|
699
|
+
previous = self._seen_processes[pid]
|
|
700
|
+
if psutil.pid_exists(pid):
|
|
701
|
+
try:
|
|
702
|
+
live_process = psutil.Process(pid)
|
|
703
|
+
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
|
|
704
|
+
# pid_exists() and Process(pid) are inherently racy. Without a
|
|
705
|
+
# lifetime snapshot, do not manufacture an EXITED observation.
|
|
706
|
+
continue
|
|
707
|
+
current = _safe_process_snapshot(live_process)
|
|
708
|
+
if current is None or self._same_process_lifetime(previous, current):
|
|
709
|
+
continue
|
|
710
|
+
|
|
711
|
+
snapshot = self._seen_processes.pop(pid)
|
|
712
|
+
self._record_process_exit(snapshot)
|
|
713
|
+
|
|
714
|
+
def _network_collection_status(self) -> str:
|
|
715
|
+
if not self.collect_network:
|
|
716
|
+
return "not_requested"
|
|
717
|
+
if self._network_sample_successes and self._network_sample_errors:
|
|
718
|
+
return "degraded"
|
|
719
|
+
if self._network_sample_successes:
|
|
720
|
+
return "available"
|
|
721
|
+
if self._network_sample_errors:
|
|
722
|
+
return "unavailable"
|
|
723
|
+
return "not_sampled"
|
|
724
|
+
|
|
725
|
+
def _sample_network(self, proc: psutil.Process, snapshot: ProcessSnapshot) -> None:
|
|
726
|
+
self._network_sample_attempts += 1
|
|
727
|
+
try:
|
|
728
|
+
getter = getattr(proc, "net_connections", None)
|
|
729
|
+
connections = getter(kind="inet") if getter else proc.connections(kind="inet")
|
|
730
|
+
except psutil.AccessDenied:
|
|
731
|
+
self._network_sample_errors += 1
|
|
732
|
+
self._network_access_denied += 1
|
|
733
|
+
return
|
|
734
|
+
except (psutil.NoSuchProcess, psutil.ZombieProcess):
|
|
735
|
+
return
|
|
736
|
+
except OSError:
|
|
737
|
+
self._network_sample_errors += 1
|
|
738
|
+
return
|
|
739
|
+
|
|
740
|
+
self._network_sample_successes += 1
|
|
741
|
+
for connection in connections:
|
|
742
|
+
remote = _format_address(connection.raddr)
|
|
743
|
+
if remote is None:
|
|
744
|
+
continue
|
|
745
|
+
local = _format_address(connection.laddr)
|
|
746
|
+
status = str(getattr(connection, "status", ""))
|
|
747
|
+
key = (snapshot.entity.id, local, remote, status)
|
|
748
|
+
if key in self._seen_connections:
|
|
749
|
+
continue
|
|
750
|
+
self._seen_connections.add(key)
|
|
751
|
+
endpoint = Entity(type="network_endpoint", id=f"endpoint:{remote}", name=remote)
|
|
752
|
+
self.sink.emit(
|
|
753
|
+
RuntimeEvent.create(
|
|
754
|
+
session_id=self.session_id,
|
|
755
|
+
event_type="network.connection",
|
|
756
|
+
relation="CONNECTED_TO",
|
|
757
|
+
source=snapshot.entity,
|
|
758
|
+
target=endpoint,
|
|
759
|
+
attributes={
|
|
760
|
+
"local_address": local,
|
|
761
|
+
"remote_address": remote,
|
|
762
|
+
"status": status,
|
|
763
|
+
"attribution": "process_polling",
|
|
764
|
+
"causal": True,
|
|
765
|
+
"backend": self.backend_name,
|
|
766
|
+
},
|
|
767
|
+
)
|
|
768
|
+
)
|