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
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from copy import deepcopy
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from .viewer_background_files import CACHE_FILES_NODE_ID, collapse_python_cache_files
|
|
9
|
+
|
|
10
|
+
ORPHAN_FILES_NODE_ID = "viewer-cluster:orphan-files"
|
|
11
|
+
MODEL_RELATIONS = {"USED_MODEL": 0, "ROUTED_TO_MODEL": 1, "INVOKED_MODEL": 2, "REQUESTED_MODEL": 3}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _attrs(node: dict[str, Any]) -> dict[str, Any]:
|
|
15
|
+
value = node.get("attributes")
|
|
16
|
+
return value if isinstance(value, dict) else {}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _provider(node: dict[str, Any]) -> str:
|
|
20
|
+
attrs = _attrs(node)
|
|
21
|
+
for key in ("provider", "provider_name", "gateway", "runtime"):
|
|
22
|
+
value = attrs.get(key)
|
|
23
|
+
if isinstance(value, str) and value:
|
|
24
|
+
return value.lower()
|
|
25
|
+
parts = str(node.get("id") or "").split(":", 2)
|
|
26
|
+
return parts[1].lower() if len(parts) > 2 else "unknown"
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _entries(graph: dict[str, Any]) -> list[dict[str, Any]]:
|
|
30
|
+
source = graph.get("source_path")
|
|
31
|
+
if not isinstance(source, str) or not source:
|
|
32
|
+
return []
|
|
33
|
+
try:
|
|
34
|
+
from .conversation_records import conversation_record_entries
|
|
35
|
+
|
|
36
|
+
return conversation_record_entries(graph, Path(source).expanduser().resolve(strict=False).parent)
|
|
37
|
+
except (OSError, RuntimeError, ValueError):
|
|
38
|
+
return []
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _roots(nodes: list[dict[str, Any]], entries: list[dict[str, Any]], edges: list[dict[str, Any]] = ()) -> tuple[list[str], dict[str, list[str]]]:
|
|
42
|
+
agents = {str(n["id"]): n for n in nodes if n.get("type") == "agent" and isinstance(n.get("id"), str)}
|
|
43
|
+
roots: set[str] = set()
|
|
44
|
+
by_provider: dict[str, set[str]] = defaultdict(set)
|
|
45
|
+
for node_id, node in agents.items():
|
|
46
|
+
attrs = _attrs(node)
|
|
47
|
+
if attrs.get("agent_role") == "root" or "/root" in {
|
|
48
|
+
str(node.get("name") or ""), str(attrs.get("agent_path") or ""), str(attrs.get("root_agent_path") or "")
|
|
49
|
+
}:
|
|
50
|
+
roots.add(node_id)
|
|
51
|
+
for entry in entries:
|
|
52
|
+
preview = entry.get("conversation_preview") if isinstance(entry, dict) else None
|
|
53
|
+
source_id = entry.get("source_id") if isinstance(entry, dict) else None
|
|
54
|
+
if isinstance(preview, dict) and preview.get("is_root") is True and isinstance(source_id, str) and source_id in agents:
|
|
55
|
+
roots.add(source_id)
|
|
56
|
+
by_provider[str(entry.get("provider") or _provider(agents[source_id])).lower()].add(source_id)
|
|
57
|
+
if not roots and len(agents) == 1:
|
|
58
|
+
roots.add(next(iter(agents)))
|
|
59
|
+
if not roots:
|
|
60
|
+
# Framework agents run inside the recorded program; the program that started
|
|
61
|
+
# the recording session is still the one process-level actor its wire calls belong to.
|
|
62
|
+
launchers = {
|
|
63
|
+
str(edge["source"]) for edge in edges
|
|
64
|
+
if edge.get("relation") == "STARTED_SESSION" and edge.get("source") in agents
|
|
65
|
+
and _attrs(agents[str(edge["source"])]).get("conversation_scope") != "framework_agent"
|
|
66
|
+
}
|
|
67
|
+
if len(launchers) == 1:
|
|
68
|
+
roots.update(launchers)
|
|
69
|
+
for root in roots:
|
|
70
|
+
provider = _provider(agents[root])
|
|
71
|
+
if provider != "unknown":
|
|
72
|
+
by_provider[provider].add(root)
|
|
73
|
+
return sorted(roots), {key: sorted(value) for key, value in by_provider.items()}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _components(ids: set[str], edges: list[dict[str, Any]]) -> list[list[str]]:
|
|
77
|
+
parent = {value: value for value in ids}
|
|
78
|
+
|
|
79
|
+
def find(value: str) -> str:
|
|
80
|
+
while parent[value] != value:
|
|
81
|
+
parent[value] = parent[parent[value]]
|
|
82
|
+
value = parent[value]
|
|
83
|
+
return value
|
|
84
|
+
|
|
85
|
+
for edge in edges:
|
|
86
|
+
if edge.get("relation") != "SAME_INFERENCE_REQUEST":
|
|
87
|
+
continue
|
|
88
|
+
a, b = edge.get("source"), edge.get("target")
|
|
89
|
+
if isinstance(a, str) and a in ids and isinstance(b, str) and b in ids:
|
|
90
|
+
ra, rb = find(a), find(b)
|
|
91
|
+
if ra != rb:
|
|
92
|
+
parent[rb] = ra
|
|
93
|
+
groups: dict[str, list[str]] = defaultdict(list)
|
|
94
|
+
for value in ids:
|
|
95
|
+
groups[find(value)].append(value)
|
|
96
|
+
return [sorted(value) for value in groups.values()]
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _matching_entries(ids: set[str], entries: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
100
|
+
return [
|
|
101
|
+
entry for entry in entries if isinstance(entry, dict)
|
|
102
|
+
and (entry.get("source_id") in ids or entry.get("evidence_source_id") in ids)
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _messages(entries: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
|
107
|
+
seen: set[tuple[Any, ...]] = set()
|
|
108
|
+
out: list[dict[str, Any]] = []
|
|
109
|
+
for entry in entries:
|
|
110
|
+
preview = entry.get("conversation_preview")
|
|
111
|
+
if not isinstance(preview, dict):
|
|
112
|
+
continue
|
|
113
|
+
for message in preview.get("messages") or []:
|
|
114
|
+
if not isinstance(message, dict) or not isinstance(message.get("text"), str) or not message["text"].strip():
|
|
115
|
+
continue
|
|
116
|
+
key = (message.get("timestamp"), message.get("ordinal"), message.get("kind"), message.get("sender"), message["text"])
|
|
117
|
+
if key in seen:
|
|
118
|
+
continue
|
|
119
|
+
seen.add(key)
|
|
120
|
+
text = message["text"]
|
|
121
|
+
out.append({
|
|
122
|
+
"timestamp": message.get("timestamp"), "ordinal": message.get("ordinal"),
|
|
123
|
+
"kind": message.get("kind"), "sender": message.get("sender"),
|
|
124
|
+
"recipient": message.get("recipient"), "phase": message.get("phase"),
|
|
125
|
+
"text": text[:4000], "text_truncated": len(text) > 4000,
|
|
126
|
+
})
|
|
127
|
+
out.sort(key=lambda m: (str(m.get("timestamp") or ""), m.get("ordinal") if isinstance(m.get("ordinal"), int) else 2**63 - 1))
|
|
128
|
+
return out
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _content_refs(ids: set[str], edges: list[dict[str, Any]], node_by_id: dict[str, dict[str, Any]]) -> list[dict[str, Any]]:
|
|
132
|
+
out: list[dict[str, Any]] = []
|
|
133
|
+
seen: set[tuple[str, str]] = set()
|
|
134
|
+
for edge in edges:
|
|
135
|
+
a, b = edge.get("source"), edge.get("target")
|
|
136
|
+
if a not in ids and b not in ids:
|
|
137
|
+
continue
|
|
138
|
+
other = b if a in ids else a
|
|
139
|
+
node = node_by_id.get(str(other))
|
|
140
|
+
if not node or node.get("type") != "observed_content":
|
|
141
|
+
continue
|
|
142
|
+
key = (str(other), str(edge.get("relation") or ""))
|
|
143
|
+
if key in seen:
|
|
144
|
+
continue
|
|
145
|
+
seen.add(key)
|
|
146
|
+
attrs = _attrs(node)
|
|
147
|
+
out.append({"id": other, "relation": edge.get("relation"), "content_kind": attrs.get("content_kind"), "path": attrs.get("path"), "sha256": attrs.get("sha256")})
|
|
148
|
+
return out
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def collapse_inference_requests(nodes: list[dict[str, Any]], edges: list[dict[str, Any]], entries: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any]]:
|
|
152
|
+
node_by_id = {str(n["id"]): n for n in nodes if isinstance(n.get("id"), str)}
|
|
153
|
+
request_ids = {node_id for node_id, node in node_by_id.items() if node.get("type") == "inference_request"}
|
|
154
|
+
if not request_ids:
|
|
155
|
+
return nodes, edges, {"collapsed_request_count": 0, "logical_inference_count": 0, "direct_inference_edge_count": 0, "unresolved": []}
|
|
156
|
+
roots, roots_by_provider = _roots(nodes, entries, edges)
|
|
157
|
+
grouped: dict[tuple[str, str], list[dict[str, Any]]] = defaultdict(list)
|
|
158
|
+
unresolved: list[dict[str, Any]] = []
|
|
159
|
+
hidden_content: set[str] = set()
|
|
160
|
+
alias_candidates: set[str] = set()
|
|
161
|
+
for component in _components(request_ids, edges):
|
|
162
|
+
members = set(component)
|
|
163
|
+
matches = _matching_entries(members, entries)
|
|
164
|
+
owner = next((str(e.get("source_id")) for e in matches if node_by_id.get(str(e.get("source_id")), {}).get("type") == "agent"), None)
|
|
165
|
+
if owner is None:
|
|
166
|
+
providers = {str(e.get("provider") or "").lower() for e in matches if e.get("provider")}
|
|
167
|
+
if not providers:
|
|
168
|
+
providers = {_provider(node_by_id[value]) for value in members}
|
|
169
|
+
candidates = {root for provider in providers for root in roots_by_provider.get(provider, [])}
|
|
170
|
+
owner = next(iter(candidates)) if len(candidates) == 1 else (roots[0] if len(roots) == 1 else None)
|
|
171
|
+
model_edges = [
|
|
172
|
+
edge for edge in edges if edge.get("source") in members and edge.get("relation") in MODEL_RELATIONS
|
|
173
|
+
and isinstance(edge.get("target"), str) and node_by_id.get(str(edge.get("target")), {}).get("type") == "model"
|
|
174
|
+
]
|
|
175
|
+
model_edges.sort(key=lambda edge: MODEL_RELATIONS.get(str(edge.get("relation")), 99))
|
|
176
|
+
model_id = str(model_edges[0]["target"]) if model_edges else None
|
|
177
|
+
if owner is None or model_id is None:
|
|
178
|
+
unresolved.append({"request_ids": sorted(members), "reason": "missing_owner" if owner is None else "missing_model"})
|
|
179
|
+
continue
|
|
180
|
+
# The served model is the inference target; an alias the caller asked for stays on the occurrence.
|
|
181
|
+
requested = sorted({str(edge["target"]) for edge in model_edges if str(edge["target"]) != model_id})
|
|
182
|
+
alias_candidates.update(requested)
|
|
183
|
+
served_by = sorted({
|
|
184
|
+
str(edge["source"]) for edge in edges
|
|
185
|
+
if edge.get("relation") == "SERVED_INFERENCE" and edge.get("target") in members and isinstance(edge.get("source"), str)
|
|
186
|
+
and edge["source"] in node_by_id
|
|
187
|
+
})
|
|
188
|
+
routed_providers = sorted({
|
|
189
|
+
str(edge["target"]) for edge in edges
|
|
190
|
+
if edge.get("relation") == "ROUTED_TO_PROVIDER" and edge.get("source") in members and isinstance(edge.get("target"), str)
|
|
191
|
+
and edge["target"] in node_by_id
|
|
192
|
+
})
|
|
193
|
+
related = [edge for edge in edges if edge.get("source") in members or edge.get("target") in members]
|
|
194
|
+
first_seen_values = [
|
|
195
|
+
str(node_by_id[value].get("first_seen"))
|
|
196
|
+
for value in members
|
|
197
|
+
if node_by_id[value].get("first_seen")
|
|
198
|
+
] + [str(edge.get("first_seen")) for edge in related if edge.get("first_seen")]
|
|
199
|
+
last_seen_values = [
|
|
200
|
+
str(node_by_id[value].get("last_seen"))
|
|
201
|
+
for value in members
|
|
202
|
+
if node_by_id[value].get("last_seen")
|
|
203
|
+
] + [str(edge.get("last_seen")) for edge in related if edge.get("last_seen")]
|
|
204
|
+
first_seen = min(first_seen_values, default=None)
|
|
205
|
+
last_seen = max(last_seen_values, default=None)
|
|
206
|
+
seqs = [edge.get("first_sequence") for edge in related if isinstance(edge.get("first_sequence"), int)]
|
|
207
|
+
last_seqs = [edge.get("last_sequence") for edge in related if isinstance(edge.get("last_sequence"), int)]
|
|
208
|
+
refs = _content_refs(members, edges, node_by_id)
|
|
209
|
+
hidden_content.update(str(ref["id"]) for ref in refs if ref.get("id"))
|
|
210
|
+
providers = sorted({_provider(node_by_id[value]) for value in members})
|
|
211
|
+
grouped[(owner, model_id)].append({
|
|
212
|
+
"request_ids": sorted(members), "provider": providers[0] if len(providers) == 1 else providers,
|
|
213
|
+
"model_id": model_id, "first_seen": first_seen, "last_seen": last_seen,
|
|
214
|
+
"first_sequence": min(seqs) if seqs else None, "last_sequence": max(last_seqs) if last_seqs else None,
|
|
215
|
+
"messages": _messages(matches), "content_references": refs,
|
|
216
|
+
**({"requested_model_ids": requested, "requested_model_names": [str(node_by_id[value].get("name") or value) for value in requested]} if requested else {}),
|
|
217
|
+
**({"served_by_ids": served_by} if served_by else {}),
|
|
218
|
+
**({"routed_provider_ids": routed_providers} if routed_providers else {}),
|
|
219
|
+
})
|
|
220
|
+
hidden = set(request_ids)
|
|
221
|
+
# A requested alias whose only evidence is hidden request evidence is shown on the served model's occurrence.
|
|
222
|
+
for model in alias_candidates:
|
|
223
|
+
incident = [edge for edge in edges if edge.get("source") == model or edge.get("target") == model]
|
|
224
|
+
if incident and all((edge.get("source") in request_ids or edge.get("target") in request_ids) for edge in incident):
|
|
225
|
+
hidden.add(model)
|
|
226
|
+
# Content attached only to hidden request evidence is inspector evidence, not a main graph node.
|
|
227
|
+
for content_id in list(hidden_content):
|
|
228
|
+
incident = [edge for edge in edges if edge.get("source") == content_id or edge.get("target") == content_id]
|
|
229
|
+
if all((edge.get("source") in request_ids or edge.get("target") in request_ids) for edge in incident):
|
|
230
|
+
hidden.add(content_id)
|
|
231
|
+
kept_nodes = [deepcopy(node) for node in nodes if node.get("id") not in hidden]
|
|
232
|
+
kept_edges = [deepcopy(edge) for edge in edges if edge.get("source") not in hidden and edge.get("target") not in hidden]
|
|
233
|
+
for (owner, model_id), occurrences in grouped.items():
|
|
234
|
+
occurrences.sort(key=lambda item: (str(item.get("first_seen") or ""), item.get("first_sequence") if isinstance(item.get("first_sequence"), int) else 2**63 - 1))
|
|
235
|
+
kept_edges.append({
|
|
236
|
+
"id": f"viewer:{owner}--INFERRED-->{model_id}", "source": owner, "target": model_id,
|
|
237
|
+
"relation": "INFERRED", "count": len(occurrences), "evidence_event_count": len(occurrences),
|
|
238
|
+
"first_seen": occurrences[0].get("first_seen"), "last_seen": occurrences[-1].get("last_seen"),
|
|
239
|
+
"first_sequence": occurrences[0].get("first_sequence"), "last_sequence": occurrences[-1].get("last_sequence"),
|
|
240
|
+
"causal": False, "inferred": False, "viewer_only": True,
|
|
241
|
+
"attributions": ["viewer_provider_inference_projection"], "viewer_occurrences": occurrences,
|
|
242
|
+
})
|
|
243
|
+
return kept_nodes, kept_edges, {
|
|
244
|
+
"collapsed_request_count": len(request_ids), "logical_inference_count": sum(len(v) for v in grouped.values()),
|
|
245
|
+
"direct_inference_edge_count": len(grouped), "unresolved": unresolved,
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
RUNTIME_MODEL_RELATIONS = {"SERVES_MODEL", "LOADED_MODEL", "ADVERTISES_MODEL"}
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _seen_bounds(items: list[dict[str, Any]]) -> dict[str, Any]:
|
|
253
|
+
firsts = [str(item["first_seen"]) for item in items if item.get("first_seen")]
|
|
254
|
+
lasts = [str(item.get("last_seen") or item["first_seen"]) for item in items if item.get("last_seen") or item.get("first_seen")]
|
|
255
|
+
seqs = [item["first_sequence"] for item in items if isinstance(item.get("first_sequence"), int)]
|
|
256
|
+
last_seqs = [item["last_sequence"] for item in items if isinstance(item.get("last_sequence"), int)]
|
|
257
|
+
return {
|
|
258
|
+
"first_seen": min(firsts, default=None), "last_seen": max(lasts, default=None),
|
|
259
|
+
"first_sequence": min(seqs) if seqs else None, "last_sequence": max(last_seqs) if last_seqs else None,
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def link_inference_infrastructure(nodes: list[dict[str, Any]], edges: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any]]:
|
|
264
|
+
"""Attach the gateway/API/runtime that served an inferred model, and the provider it was routed to.
|
|
265
|
+
|
|
266
|
+
Collapsing inference requests removes the request node that carried SERVED_INFERENCE and
|
|
267
|
+
ROUTED_TO_PROVIDER, so the occurrence keeps those ids and the served model links to them here.
|
|
268
|
+
A runtime is the same server when it shares the model's endpoint scope or reports that exact
|
|
269
|
+
model node; a runtime-namespace model with the same name or catalog id on that runtime is the
|
|
270
|
+
same model seen through the runtime's catalog, so it is folded into the served model.
|
|
271
|
+
"""
|
|
272
|
+
node_by_id = {str(n["id"]): n for n in nodes if isinstance(n.get("id"), str)}
|
|
273
|
+
occurrences: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
274
|
+
for edge in edges:
|
|
275
|
+
target = edge.get("target")
|
|
276
|
+
if edge.get("relation") == "INFERRED" and isinstance(target, str) and target in node_by_id:
|
|
277
|
+
occurrences[target].extend(item for item in edge.get("viewer_occurrences") or [] if isinstance(item, dict))
|
|
278
|
+
if not occurrences:
|
|
279
|
+
return nodes, edges, {"infrastructure_edge_count": 0, "folded_runtime_model_count": 0}
|
|
280
|
+
links: dict[tuple[str, str, str], dict[str, Any]] = {}
|
|
281
|
+
|
|
282
|
+
def link(model: str, other: str, relation: str, basis: str, evidence: list[dict[str, Any]], **extra: Any) -> None:
|
|
283
|
+
item = links.setdefault((model, other, relation), {"basis": set(), "evidence": [], "edge_ids": set(), "request_ids": set(), "relations": set(), "folded": []})
|
|
284
|
+
item["basis"].add(basis)
|
|
285
|
+
item["evidence"].extend(evidence)
|
|
286
|
+
item["edge_ids"].update(extra.get("edge_ids", ()))
|
|
287
|
+
item["request_ids"].update(extra.get("request_ids", ()))
|
|
288
|
+
item["relations"].update(extra.get("relations", ()))
|
|
289
|
+
item["folded"].extend(extra.get("folded", ()))
|
|
290
|
+
|
|
291
|
+
for model, items in occurrences.items():
|
|
292
|
+
for occurrence in items:
|
|
293
|
+
request_ids = [str(value) for value in occurrence.get("request_ids") or []]
|
|
294
|
+
for server in occurrence.get("served_by_ids") or []:
|
|
295
|
+
if server in node_by_id and server != model:
|
|
296
|
+
link(model, server, "SERVED_BY", "served_inference", [occurrence], request_ids=request_ids)
|
|
297
|
+
for provider in occurrence.get("routed_provider_ids") or []:
|
|
298
|
+
if provider in node_by_id and provider != model:
|
|
299
|
+
link(model, provider, "ROUTED_TO_PROVIDER", "routed_to_provider", [occurrence], request_ids=request_ids)
|
|
300
|
+
|
|
301
|
+
runtime_edges: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
302
|
+
for edge in edges:
|
|
303
|
+
if edge.get("relation") in RUNTIME_MODEL_RELATIONS and node_by_id.get(str(edge.get("source")), {}).get("type") == "model_runtime" and edge.get("target") in node_by_id:
|
|
304
|
+
runtime_edges[str(edge["source"])].append(edge)
|
|
305
|
+
runtimes = [node_id for node_id, node in node_by_id.items() if node.get("type") == "model_runtime"]
|
|
306
|
+
replaced: set[str] = set()
|
|
307
|
+
folded: dict[str, str] = {}
|
|
308
|
+
for model in occurrences:
|
|
309
|
+
attrs = _attrs(node_by_id[model])
|
|
310
|
+
scope = attrs.get("endpoint_scope")
|
|
311
|
+
names = {str(value) for value in (node_by_id[model].get("name"), attrs.get("catalog_id")) if value}
|
|
312
|
+
for runtime in runtimes:
|
|
313
|
+
direct = [edge for edge in runtime_edges[runtime] if edge.get("target") == model]
|
|
314
|
+
scoped = bool(scope) and _attrs(node_by_id[runtime]).get("endpoint_scope") == scope
|
|
315
|
+
if not direct and not scoped:
|
|
316
|
+
continue
|
|
317
|
+
same = []
|
|
318
|
+
if scoped:
|
|
319
|
+
for edge in runtime_edges[runtime]:
|
|
320
|
+
other = str(edge["target"])
|
|
321
|
+
if other == model or other in occurrences or node_by_id[other].get("type") != "model":
|
|
322
|
+
continue
|
|
323
|
+
other_names = {str(value) for value in (node_by_id[other].get("name"), _attrs(node_by_id[other]).get("catalog_id")) if value}
|
|
324
|
+
if names & other_names:
|
|
325
|
+
same.append(edge)
|
|
326
|
+
for edge in direct + same:
|
|
327
|
+
replaced.add(str(edge["id"]))
|
|
328
|
+
for edge in same:
|
|
329
|
+
folded[str(edge["target"])] = model
|
|
330
|
+
link(
|
|
331
|
+
model, runtime, "SERVED_BY", "runtime_model" if direct else "endpoint_scope", direct + same,
|
|
332
|
+
edge_ids=[str(edge["id"]) for edge in direct + same], relations=[str(edge["relation"]) for edge in direct + same],
|
|
333
|
+
folded=[{"id": str(edge["target"]), "name": node_by_id[str(edge["target"])].get("name"), "relation": edge.get("relation"), "runtime_id": runtime} for edge in same],
|
|
334
|
+
)
|
|
335
|
+
# A folded catalog model is hidden only when its catalog edges are all it has.
|
|
336
|
+
for other in list(folded):
|
|
337
|
+
incident = [edge for edge in edges if edge.get("source") == other or edge.get("target") == other]
|
|
338
|
+
if not all(str(edge.get("id")) in replaced for edge in incident):
|
|
339
|
+
replaced.difference_update(str(edge.get("id")) for edge in incident)
|
|
340
|
+
del folded[other]
|
|
341
|
+
out_nodes = []
|
|
342
|
+
for node in nodes:
|
|
343
|
+
node_id = node.get("id")
|
|
344
|
+
if node_id in folded:
|
|
345
|
+
continue
|
|
346
|
+
absorbed = [item for (model, _, _), value in links.items() if model == node_id for item in value["folded"] if item["id"] in folded]
|
|
347
|
+
out_nodes.append({**node, "attributes": {**_attrs(node), "viewer_runtime_models": absorbed}} if absorbed else node)
|
|
348
|
+
out_edges = [edge for edge in edges if str(edge.get("id")) not in replaced]
|
|
349
|
+
for (model, other, relation), value in sorted(links.items()):
|
|
350
|
+
evidence = [item for item in value["evidence"] if isinstance(item, dict)]
|
|
351
|
+
out_edges.append({
|
|
352
|
+
"id": f"viewer:{model}--{relation}-->{other}", "source": model, "target": other, "relation": relation,
|
|
353
|
+
"count": max(1, len(value["request_ids"]) or len(evidence)), **_seen_bounds(evidence),
|
|
354
|
+
"causal": False, "inferred": "endpoint_scope" in value["basis"], "viewer_only": True,
|
|
355
|
+
"attributions": ["viewer_inference_infrastructure_projection"], "viewer_link_basis": sorted(value["basis"]),
|
|
356
|
+
**({"viewer_request_ids": sorted(value["request_ids"])} if value["request_ids"] else {}),
|
|
357
|
+
**({"evidence_edge_ids": sorted(value["edge_ids"]), "viewer_runtime_relations": sorted(value["relations"])} if value["edge_ids"] else {}),
|
|
358
|
+
})
|
|
359
|
+
return out_nodes, out_edges, {"infrastructure_edge_count": len(links), "folded_runtime_model_count": len(folded)}
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def _file_path(node: dict[str, Any]) -> str:
|
|
363
|
+
attrs = _attrs(node)
|
|
364
|
+
for value in (attrs.get("path"), attrs.get("file_path"), attrs.get("source_path"), attrs.get("target_path"), node.get("name"), node.get("id")):
|
|
365
|
+
if isinstance(value, str) and value:
|
|
366
|
+
return value.removeprefix("file:").removeprefix("directory:")
|
|
367
|
+
return "unknown"
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
def collapse_orphan_files(nodes: list[dict[str, Any]], edges: list[dict[str, Any]], root_id: str | None) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, Any] | None]:
|
|
371
|
+
degree: dict[str, int] = defaultdict(int)
|
|
372
|
+
for edge in edges:
|
|
373
|
+
for value in (edge.get("source"), edge.get("target")):
|
|
374
|
+
if isinstance(value, str):
|
|
375
|
+
degree[value] += 1
|
|
376
|
+
members = [node for node in nodes if node.get("type") in {"file", "directory"} and isinstance(node.get("id"), str) and degree.get(str(node["id"]), 0) == 0]
|
|
377
|
+
if not members:
|
|
378
|
+
return nodes, edges, None
|
|
379
|
+
entries = [{"id": n.get("id"), "type": n.get("type"), "name": n.get("name"), "path": _file_path(n), "first_seen": n.get("first_seen"), "last_seen": n.get("last_seen"), "event_count": int(n.get("event_count") or 0), "event_types": list(n.get("event_types") or [])} for n in sorted(members, key=_file_path)]
|
|
380
|
+
first = min((str(n.get("first_seen")) for n in members if n.get("first_seen")), default=None)
|
|
381
|
+
last = max((str(n.get("last_seen")) for n in members if n.get("last_seen")), default=None)
|
|
382
|
+
cluster = {"id": ORPHAN_FILES_NODE_ID, "type": "file_cluster", "name": "Files / directories", "attributes": {"viewer_only": True, "collapsed": True, "expandable": True, "reason": "orphan_file_directory_nodes", "member_count": len(members), "entries": entries, "viewer_occurrences": entries}, "first_seen": first, "last_seen": last, "event_count": sum(int(n.get("event_count") or 0) for n in members)}
|
|
383
|
+
ids = {str(n["id"]) for n in members}
|
|
384
|
+
out_nodes = [deepcopy(n) for n in nodes if n.get("id") not in ids] + [cluster]
|
|
385
|
+
out_edges = [deepcopy(e) for e in edges]
|
|
386
|
+
edge_id = None
|
|
387
|
+
if root_id:
|
|
388
|
+
edge_id = f"viewer:{root_id}--OBSERVED_FILES-->{ORPHAN_FILES_NODE_ID}"
|
|
389
|
+
out_edges.append({"id": edge_id, "source": root_id, "target": ORPHAN_FILES_NODE_ID, "relation": "OBSERVED_FILES", "count": len(members), "first_seen": first, "last_seen": last, "causal": False, "inferred": False, "viewer_only": True})
|
|
390
|
+
return out_nodes, out_edges, {"cluster_node_id": ORPHAN_FILES_NODE_ID, "cluster_edge_id": edge_id, "nodes": deepcopy(members), "edges": [], "viewer_only": True}
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def orphan_audit(nodes: list[dict[str, Any]], edges: list[dict[str, Any]]) -> dict[str, Any]:
|
|
394
|
+
degree: dict[str, int] = defaultdict(int)
|
|
395
|
+
for edge in edges:
|
|
396
|
+
for value in (edge.get("source"), edge.get("target")):
|
|
397
|
+
if isinstance(value, str):
|
|
398
|
+
degree[value] += 1
|
|
399
|
+
grouped: dict[str, list[str]] = defaultdict(list)
|
|
400
|
+
for node in nodes:
|
|
401
|
+
node_id = node.get("id")
|
|
402
|
+
if isinstance(node_id, str) and degree.get(node_id, 0) == 0:
|
|
403
|
+
grouped[str(node.get("type") or "unknown")].append(node_id)
|
|
404
|
+
by_type = [{"node_type": kind, "count": len(ids), "examples": sorted(ids)[:8]} for kind, ids in sorted(grouped.items())]
|
|
405
|
+
return {"count": sum(item["count"] for item in by_type), "by_type": by_type}
|
|
406
|
+
|
|
407
|
+
|
|
408
|
+
_FRAMEWORK_TASK_RELATIONS = frozenset(
|
|
409
|
+
{"ASSIGNED_TO", "TASK_CREATED", "TASK_STARTED", "TASK_UPDATED", "TASK_COMPLETED", "TASK_FAILED"}
|
|
410
|
+
)
|
|
411
|
+
|
|
412
|
+
|
|
413
|
+
def _framework_task_edge_score(edge: dict[str, Any]) -> tuple[int, str, str]:
|
|
414
|
+
sequence = edge.get("last_sequence")
|
|
415
|
+
if not isinstance(sequence, int) or isinstance(sequence, bool):
|
|
416
|
+
sequence = edge.get("first_sequence")
|
|
417
|
+
if not isinstance(sequence, int) or isinstance(sequence, bool):
|
|
418
|
+
sequence = -1
|
|
419
|
+
return (sequence, str(edge.get("last_seen") or edge.get("first_seen") or ""), str(edge.get("id") or ""))
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
def _framework_task_text(task: dict[str, Any]) -> str:
|
|
423
|
+
attributes = _attrs(task)
|
|
424
|
+
for value in (
|
|
425
|
+
attributes.get("task_prompt"),
|
|
426
|
+
attributes.get("prompt"),
|
|
427
|
+
attributes.get("description"),
|
|
428
|
+
task.get("name"),
|
|
429
|
+
):
|
|
430
|
+
if isinstance(value, str) and value.strip():
|
|
431
|
+
return value.strip()
|
|
432
|
+
return ""
|
|
433
|
+
|
|
434
|
+
|
|
435
|
+
def collapse_framework_tasks(
|
|
436
|
+
nodes: list[dict[str, Any]],
|
|
437
|
+
edges: list[dict[str, Any]],
|
|
438
|
+
) -> tuple[list[dict[str, Any]], list[dict[str, Any]], dict[str, int]]:
|
|
439
|
+
"""Fold framework task presentation into the owning agent, like provider views.
|
|
440
|
+
|
|
441
|
+
Framework adapters retain first-class task entities in the raw graph because task
|
|
442
|
+
lifecycle and prompt evidence must remain auditable. Provider dashboards expose
|
|
443
|
+
that same information on the agent card, however, so the viewer projection hides
|
|
444
|
+
only framework task nodes and copies the latest authoritative task text onto each
|
|
445
|
+
assigned framework agent. Provider graphs are untouched because they do not use
|
|
446
|
+
the framework-agent conversation scope.
|
|
447
|
+
"""
|
|
448
|
+
node_by_id = {
|
|
449
|
+
str(node["id"]): node
|
|
450
|
+
for node in nodes
|
|
451
|
+
if isinstance(node, dict) and isinstance(node.get("id"), str)
|
|
452
|
+
}
|
|
453
|
+
framework_agents = {
|
|
454
|
+
node_id
|
|
455
|
+
for node_id, node in node_by_id.items()
|
|
456
|
+
if node.get("type") == "agent" and _attrs(node).get("conversation_scope") == "framework_agent"
|
|
457
|
+
}
|
|
458
|
+
if not framework_agents:
|
|
459
|
+
return nodes, edges, {
|
|
460
|
+
"framework_task_node_count": 0,
|
|
461
|
+
"framework_task_content_node_count": 0,
|
|
462
|
+
"framework_task_agent_count": 0,
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
task_ids: set[str] = set()
|
|
466
|
+
tasks_by_agent: dict[str, list[tuple[tuple[int, str, str], str]]] = defaultdict(list)
|
|
467
|
+
for edge in edges:
|
|
468
|
+
if not isinstance(edge, dict) or edge.get("relation") not in _FRAMEWORK_TASK_RELATIONS:
|
|
469
|
+
continue
|
|
470
|
+
source = str(edge.get("source") or "")
|
|
471
|
+
target = str(edge.get("target") or "")
|
|
472
|
+
source_node = node_by_id.get(source)
|
|
473
|
+
target_node = node_by_id.get(target)
|
|
474
|
+
task_id = source if source_node and source_node.get("type") == "task" else target if target_node and target_node.get("type") == "task" else None
|
|
475
|
+
if task_id is None:
|
|
476
|
+
continue
|
|
477
|
+
agent_id = source if source in framework_agents else target if target in framework_agents else None
|
|
478
|
+
if agent_id is None:
|
|
479
|
+
continue
|
|
480
|
+
task_ids.add(task_id)
|
|
481
|
+
tasks_by_agent[agent_id].append((_framework_task_edge_score(edge), task_id))
|
|
482
|
+
|
|
483
|
+
if not task_ids:
|
|
484
|
+
return nodes, edges, {
|
|
485
|
+
"framework_task_node_count": 0,
|
|
486
|
+
"framework_task_content_node_count": 0,
|
|
487
|
+
"framework_task_agent_count": 0,
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
projected_nodes: list[dict[str, Any]] = []
|
|
491
|
+
for node in nodes:
|
|
492
|
+
node_id = str(node.get("id") or "")
|
|
493
|
+
if node_id in task_ids:
|
|
494
|
+
continue
|
|
495
|
+
if node.get("type") == "agent" and node_id in framework_agents:
|
|
496
|
+
candidates: dict[str, tuple[int, str, str]] = {}
|
|
497
|
+
for score, task_id in tasks_by_agent.get(node_id, []):
|
|
498
|
+
previous = candidates.get(task_id)
|
|
499
|
+
if previous is None or score > previous:
|
|
500
|
+
candidates[task_id] = score
|
|
501
|
+
ordered_ids = [
|
|
502
|
+
task_id
|
|
503
|
+
for task_id, _ in sorted(
|
|
504
|
+
candidates.items(),
|
|
505
|
+
key=lambda item: (
|
|
506
|
+
not bool(_attrs(node_by_id[item[0]]).get("action")),
|
|
507
|
+
bool(_attrs(node_by_id[item[0]]).get("task_prompt") or _attrs(node_by_id[item[0]]).get("prompt")),
|
|
508
|
+
item[1], item[0],
|
|
509
|
+
),
|
|
510
|
+
reverse=True,
|
|
511
|
+
)
|
|
512
|
+
]
|
|
513
|
+
latest_task = node_by_id.get(ordered_ids[0]) if ordered_ids else None
|
|
514
|
+
attributes = dict(_attrs(node))
|
|
515
|
+
attributes["viewer_assigned_task_ids"] = ordered_ids
|
|
516
|
+
attributes["viewer_assigned_task_count"] = len(ordered_ids)
|
|
517
|
+
if latest_task is not None:
|
|
518
|
+
attributes["viewer_assigned_task_id"] = str(latest_task.get("id") or "")
|
|
519
|
+
attributes["viewer_assigned_task_name"] = str(latest_task.get("name") or "")
|
|
520
|
+
task_text = _framework_task_text(latest_task)
|
|
521
|
+
if task_text:
|
|
522
|
+
attributes["viewer_assigned_task_prompt"] = task_text
|
|
523
|
+
projected_nodes.append({**node, "attributes": attributes})
|
|
524
|
+
continue
|
|
525
|
+
projected_nodes.append(node)
|
|
526
|
+
|
|
527
|
+
hidden_content_ids: set[str] = set()
|
|
528
|
+
for node in nodes:
|
|
529
|
+
node_id = str(node.get("id") or "")
|
|
530
|
+
if node.get("type") != "observed_content" or not node_id:
|
|
531
|
+
continue
|
|
532
|
+
incident = [
|
|
533
|
+
edge
|
|
534
|
+
for edge in edges
|
|
535
|
+
if edge.get("source") == node_id or edge.get("target") == node_id
|
|
536
|
+
]
|
|
537
|
+
if incident and all(
|
|
538
|
+
(edge.get("source") in task_ids or edge.get("target") in task_ids)
|
|
539
|
+
for edge in incident
|
|
540
|
+
):
|
|
541
|
+
hidden_content_ids.add(node_id)
|
|
542
|
+
|
|
543
|
+
hidden_ids = task_ids | hidden_content_ids
|
|
544
|
+
projected_nodes = [node for node in projected_nodes if str(node.get("id") or "") not in hidden_content_ids]
|
|
545
|
+
projected_edges = [
|
|
546
|
+
edge
|
|
547
|
+
for edge in edges
|
|
548
|
+
if edge.get("source") not in hidden_ids and edge.get("target") not in hidden_ids
|
|
549
|
+
]
|
|
550
|
+
return projected_nodes, projected_edges, {
|
|
551
|
+
"framework_task_node_count": len(task_ids),
|
|
552
|
+
"framework_task_content_node_count": len(hidden_content_ids),
|
|
553
|
+
"framework_task_agent_count": len(tasks_by_agent),
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
|
|
557
|
+
def project_provider_neutral_viewer_graph(graph: dict[str, Any]) -> dict[str, Any]:
|
|
558
|
+
"""Provider-neutral display projection; raw graph/evidence remains unchanged."""
|
|
559
|
+
from . import viewer_projection_base as base
|
|
560
|
+
from .viewer_external_endpoints import LOCAL_NODE_ID, collapse_local_endpoints
|
|
561
|
+
|
|
562
|
+
decorated = base.decorate_viewer_content_references(graph)
|
|
563
|
+
filtered, hook_nodes, hook_edges = base._without_internal_hook_processes(decorated)
|
|
564
|
+
nodes = [deepcopy(n) for n in filtered.get("nodes", []) if isinstance(n, dict)]
|
|
565
|
+
edges = [deepcopy(e) for e in filtered.get("edges", []) if isinstance(e, dict)]
|
|
566
|
+
entries = _entries(graph)
|
|
567
|
+
nodes, edges, framework_tasks = collapse_framework_tasks(nodes, edges)
|
|
568
|
+
nodes, edges, inference = collapse_inference_requests(nodes, edges, entries)
|
|
569
|
+
nodes, edges, infrastructure = link_inference_infrastructure(nodes, edges)
|
|
570
|
+
roots, _ = _roots(nodes, entries, edges)
|
|
571
|
+
nodes, edges, cache = collapse_python_cache_files(nodes, edges, roots[0] if len(roots) == 1 else None)
|
|
572
|
+
nodes, edges, files = collapse_orphan_files(nodes, edges, roots[0] if len(roots) == 1 else None)
|
|
573
|
+
nodes, edges, local = collapse_local_endpoints(nodes, edges)
|
|
574
|
+
|
|
575
|
+
per_model: dict[str, list[dict[str, Any]]] = defaultdict(list)
|
|
576
|
+
for edge in edges:
|
|
577
|
+
if edge.get("relation") == "INFERRED" and isinstance(edge.get("target"), str) and isinstance(edge.get("viewer_occurrences"), list):
|
|
578
|
+
per_model[str(edge["target"])].extend(deepcopy(edge["viewer_occurrences"]))
|
|
579
|
+
if per_model:
|
|
580
|
+
rewritten = []
|
|
581
|
+
for node in nodes:
|
|
582
|
+
node_id = node.get("id")
|
|
583
|
+
if not isinstance(node_id, str) or node_id not in per_model:
|
|
584
|
+
rewritten.append(node)
|
|
585
|
+
continue
|
|
586
|
+
occurrences = sorted(per_model[node_id], key=lambda item: (str(item.get("first_seen") or ""), item.get("first_sequence") if isinstance(item.get("first_sequence"), int) else 2**63 - 1))
|
|
587
|
+
rewritten.append({**node, "attributes": {**_attrs(node), "viewer_inference_occurrences": occurrences, "viewer_inference_count": len(occurrences)}})
|
|
588
|
+
nodes = rewritten
|
|
589
|
+
|
|
590
|
+
result = deepcopy(filtered)
|
|
591
|
+
result.update({"nodes": nodes, "edges": edges, "node_count": len(nodes), "edge_count": len(edges)})
|
|
592
|
+
audit = orphan_audit(nodes, edges)
|
|
593
|
+
topology_changed = bool(
|
|
594
|
+
hook_nodes
|
|
595
|
+
or hook_edges
|
|
596
|
+
or inference["collapsed_request_count"]
|
|
597
|
+
or local is not None
|
|
598
|
+
or files is not None
|
|
599
|
+
or cache is not None
|
|
600
|
+
or framework_tasks["framework_task_node_count"]
|
|
601
|
+
)
|
|
602
|
+
if not topology_changed:
|
|
603
|
+
if audit["count"]:
|
|
604
|
+
result["viewer_orphan_audit"] = audit
|
|
605
|
+
return result
|
|
606
|
+
|
|
607
|
+
meta = dict(result.get("viewer_projection") or {"schema_version": "0.1", "viewer_only": True})
|
|
608
|
+
semantic_projection = bool(inference["collapsed_request_count"] or local is not None or files is not None or cache is not None)
|
|
609
|
+
meta.update({
|
|
610
|
+
"kind": "provider_neutral_semantics" if semantic_projection else "internal_hook_processes",
|
|
611
|
+
"local_endpoint_policy": "all_loopback",
|
|
612
|
+
"local_endpoint_count": len(local.get("nodes") or []) if local else 0,
|
|
613
|
+
"cluster_count": (1 if local else 0) + (1 if files else 0) + (1 if cache else 0),
|
|
614
|
+
"python_cache_file_node_count": len(cache["nodes"]) if cache else 0,
|
|
615
|
+
"orphan_file_node_count": len(files.get("nodes") or []) if files else 0,
|
|
616
|
+
"inference_request_count": inference["collapsed_request_count"], "logical_inference_count": inference["logical_inference_count"],
|
|
617
|
+
"direct_inference_edge_count": inference["direct_inference_edge_count"], "unresolved_inference_requests": inference["unresolved"],
|
|
618
|
+
"inference_infrastructure_edge_count": infrastructure["infrastructure_edge_count"],
|
|
619
|
+
"folded_runtime_model_count": infrastructure["folded_runtime_model_count"],
|
|
620
|
+
"framework_task_node_count": framework_tasks["framework_task_node_count"],
|
|
621
|
+
"framework_task_content_node_count": framework_tasks["framework_task_content_node_count"],
|
|
622
|
+
"framework_task_agent_count": framework_tasks["framework_task_agent_count"],
|
|
623
|
+
"internal_hook_node_count": len(hook_nodes), "internal_hook_edge_count": len(hook_edges),
|
|
624
|
+
"projection_topology_changed": True,
|
|
625
|
+
})
|
|
626
|
+
payload = deepcopy(result.get("expansion")) if isinstance(result.get("expansion"), dict) else {}
|
|
627
|
+
clusters = deepcopy(payload.get("clusters")) if isinstance(payload.get("clusters"), dict) else {}
|
|
628
|
+
if local:
|
|
629
|
+
clusters[LOCAL_NODE_ID] = local
|
|
630
|
+
if files:
|
|
631
|
+
clusters[ORPHAN_FILES_NODE_ID] = files
|
|
632
|
+
if cache:
|
|
633
|
+
clusters[CACHE_FILES_NODE_ID] = cache
|
|
634
|
+
if clusters:
|
|
635
|
+
payload.setdefault("schema_version", "0.1")
|
|
636
|
+
payload["clusters"] = clusters
|
|
637
|
+
result["expansion"] = payload
|
|
638
|
+
meta["orphan_audit"] = audit
|
|
639
|
+
result["viewer_orphan_audit"] = audit
|
|
640
|
+
result["viewer_projection"] = meta
|
|
641
|
+
return result
|