Add sealed Epiphany telemetry artifacts
This commit is contained in:
parent
1a29816ad1
commit
8a16cadb70
13 changed files with 416 additions and 6 deletions
273
tools/epiphany_agent_telemetry.py
Normal file
273
tools/epiphany_agent_telemetry.py
Normal file
|
|
@ -0,0 +1,273 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from collections import Counter
|
||||
from collections import defaultdict
|
||||
from datetime import datetime
|
||||
from datetime import timezone
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
|
||||
TEXTLIKE_KEYS = {
|
||||
"activeTranscript",
|
||||
"body",
|
||||
"content",
|
||||
"input",
|
||||
"inputTranscript",
|
||||
"items",
|
||||
"message",
|
||||
"note",
|
||||
"output",
|
||||
"prompt",
|
||||
"raw",
|
||||
"rawResult",
|
||||
"reasoning",
|
||||
"result",
|
||||
"summary",
|
||||
"text",
|
||||
"turns",
|
||||
}
|
||||
|
||||
SAFE_KEYS = {
|
||||
"action",
|
||||
"artifactPath",
|
||||
"backendJobId",
|
||||
"bindingId",
|
||||
"changedFields",
|
||||
"cwd",
|
||||
"ephemeral",
|
||||
"expectedRevision",
|
||||
"id",
|
||||
"jobId",
|
||||
"kind",
|
||||
"level",
|
||||
"maxRuntimeSeconds",
|
||||
"method",
|
||||
"mode",
|
||||
"path",
|
||||
"recommendedAction",
|
||||
"recommendedSceneAction",
|
||||
"revision",
|
||||
"roleId",
|
||||
"source",
|
||||
"stateStatus",
|
||||
"status",
|
||||
"targetRole",
|
||||
"threadId",
|
||||
"turnId",
|
||||
"type",
|
||||
"verdict",
|
||||
}
|
||||
|
||||
|
||||
def json_dump(value: Any) -> str:
|
||||
return json.dumps(value, indent=2, ensure_ascii=False) + "\n"
|
||||
|
||||
|
||||
def scalar_summary(key: str, value: Any) -> Any:
|
||||
if value is None or isinstance(value, (bool, int, float)):
|
||||
return value
|
||||
if isinstance(value, str):
|
||||
if key in SAFE_KEYS:
|
||||
return value
|
||||
return {"sealed": True, "kind": "text", "chars": len(value)}
|
||||
return None
|
||||
|
||||
|
||||
def summarize_value(value: Any, *, key: str = "") -> Any:
|
||||
scalar = scalar_summary(key, value)
|
||||
if scalar is not None:
|
||||
return scalar
|
||||
|
||||
if key in TEXTLIKE_KEYS:
|
||||
return sealed_summary(key, value)
|
||||
|
||||
if isinstance(value, list):
|
||||
return {
|
||||
"kind": "list",
|
||||
"count": len(value),
|
||||
"items": [summarize_value(item) for item in value[:8]],
|
||||
"truncated": len(value) > 8,
|
||||
}
|
||||
|
||||
if isinstance(value, dict):
|
||||
result: dict[str, Any] = {}
|
||||
for child_key, child_value in value.items():
|
||||
if child_key in TEXTLIKE_KEYS and child_key not in SAFE_KEYS:
|
||||
result[child_key] = sealed_summary(child_key, child_value)
|
||||
elif child_key in SAFE_KEYS:
|
||||
result[child_key] = summarize_value(child_value, key=child_key)
|
||||
elif isinstance(child_value, (dict, list)):
|
||||
result[child_key] = summarize_value(child_value, key=child_key)
|
||||
elif isinstance(child_value, str):
|
||||
result[child_key] = {"sealed": True, "kind": "text", "chars": len(child_value)}
|
||||
else:
|
||||
result[child_key] = child_value
|
||||
return result
|
||||
|
||||
return {"sealed": True, "kind": type(value).__name__}
|
||||
|
||||
|
||||
def sealed_summary(key: str, value: Any) -> dict[str, Any]:
|
||||
summary: dict[str, Any] = {
|
||||
"sealed": True,
|
||||
"key": key,
|
||||
"reason": "direct agent or transcript content is excluded from telemetry",
|
||||
}
|
||||
if isinstance(value, str):
|
||||
summary["chars"] = len(value)
|
||||
elif isinstance(value, list):
|
||||
summary["items"] = len(value)
|
||||
elif isinstance(value, dict):
|
||||
summary["keys"] = sorted(str(k) for k in value.keys())[:24]
|
||||
summary["keyCount"] = len(value)
|
||||
return summary
|
||||
|
||||
|
||||
def collect_strings(value: Any, key_names: set[str]) -> list[str]:
|
||||
found: list[str] = []
|
||||
if isinstance(value, dict):
|
||||
for key, item in value.items():
|
||||
if key in key_names and isinstance(item, str):
|
||||
found.append(item)
|
||||
elif isinstance(item, (dict, list)):
|
||||
found.extend(collect_strings(item, key_names))
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
found.extend(collect_strings(item, key_names))
|
||||
return found
|
||||
|
||||
|
||||
def telemetry_event(index: int, record: dict[str, Any]) -> dict[str, Any]:
|
||||
direction = "unknown"
|
||||
payload: Any = record
|
||||
if "sent" in record:
|
||||
direction = "sent"
|
||||
payload = record["sent"]
|
||||
elif "received" in record:
|
||||
direction = "received"
|
||||
payload = record["received"]
|
||||
|
||||
event: dict[str, Any] = {"index": index, "direction": direction}
|
||||
if not isinstance(payload, dict):
|
||||
event["payload"] = sealed_summary("payload", payload)
|
||||
return event
|
||||
|
||||
if "id" in payload:
|
||||
event["id"] = payload["id"]
|
||||
if "method" in payload:
|
||||
event["method"] = payload["method"]
|
||||
if "error" in payload:
|
||||
event["error"] = summarize_value(payload["error"])
|
||||
if "params" in payload:
|
||||
event["params"] = summarize_value(payload["params"], key="params")
|
||||
if "result" in payload:
|
||||
event["result"] = summarize_value(payload["result"], key="responseResult")
|
||||
|
||||
names = sorted(set(collect_strings(payload, {"toolName", "tool", "functionName", "name"})))
|
||||
if names:
|
||||
event["functionNames"] = names[:16]
|
||||
event["functionNameCount"] = len(names)
|
||||
|
||||
paths = sorted(set(collect_strings(payload, {"path", "cwd", "artifactPath"})))
|
||||
if paths:
|
||||
event["paths"] = paths[:16]
|
||||
event["pathCount"] = len(paths)
|
||||
|
||||
return event
|
||||
|
||||
|
||||
def build_telemetry(transcript_path: Path) -> dict[str, Any]:
|
||||
events: list[dict[str, Any]] = []
|
||||
method_counts: Counter[str] = Counter()
|
||||
direction_counts: Counter[str] = Counter()
|
||||
function_counts: Counter[str] = Counter()
|
||||
decode_errors: list[dict[str, Any]] = []
|
||||
|
||||
if not transcript_path.exists():
|
||||
return {
|
||||
"transcriptPath": str(transcript_path),
|
||||
"generatedAt": datetime.now(timezone.utc).isoformat(),
|
||||
"status": "missing",
|
||||
"events": [],
|
||||
"counts": {},
|
||||
}
|
||||
|
||||
for index, line in enumerate(transcript_path.read_text(encoding="utf-8").splitlines()):
|
||||
if not line.strip():
|
||||
continue
|
||||
try:
|
||||
record = json.loads(line)
|
||||
except json.JSONDecodeError as exc:
|
||||
decode_errors.append({"index": index, "error": str(exc), "chars": len(line)})
|
||||
continue
|
||||
if not isinstance(record, dict):
|
||||
events.append({"index": index, "payload": sealed_summary("record", record)})
|
||||
continue
|
||||
event = telemetry_event(index, record)
|
||||
events.append(event)
|
||||
direction_counts[event.get("direction", "unknown")] += 1
|
||||
method = event.get("method")
|
||||
if isinstance(method, str):
|
||||
method_counts[method] += 1
|
||||
for name in event.get("functionNames", []):
|
||||
function_counts[name] += 1
|
||||
|
||||
request_latency_shape: dict[str, dict[str, int]] = defaultdict(lambda: {"sent": 0, "received": 0})
|
||||
for event in events:
|
||||
event_id = event.get("id")
|
||||
if event_id is None:
|
||||
continue
|
||||
direction = str(event.get("direction", "unknown"))
|
||||
if direction in {"sent", "received"}:
|
||||
request_latency_shape[str(event_id)][direction] += 1
|
||||
|
||||
return {
|
||||
"transcriptPath": str(transcript_path),
|
||||
"generatedAt": datetime.now(timezone.utc).isoformat(),
|
||||
"status": "ok",
|
||||
"policy": {
|
||||
"directThoughtsSealed": True,
|
||||
"note": "Telemetry summarizes function/API shape only; text, raw results, and transcript payloads are sealed.",
|
||||
},
|
||||
"counts": {
|
||||
"events": len(events),
|
||||
"directions": dict(direction_counts),
|
||||
"methods": dict(method_counts),
|
||||
"functionNames": dict(function_counts),
|
||||
"decodeErrors": len(decode_errors),
|
||||
},
|
||||
"requestShape": dict(request_latency_shape),
|
||||
"decodeErrors": decode_errors,
|
||||
"events": events,
|
||||
}
|
||||
|
||||
|
||||
def write_transcript_telemetry(transcript_path: Path, telemetry_path: Path) -> dict[str, Any]:
|
||||
telemetry = build_telemetry(transcript_path)
|
||||
telemetry_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
telemetry_path.write_text(json_dump(telemetry), encoding="utf-8")
|
||||
return telemetry
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate operator-safe function/API telemetry from a sealed app-server transcript."
|
||||
)
|
||||
parser.add_argument("transcript", type=Path)
|
||||
parser.add_argument("--output", type=Path)
|
||||
args = parser.parse_args()
|
||||
|
||||
telemetry = build_telemetry(args.transcript.resolve())
|
||||
if args.output:
|
||||
args.output.parent.mkdir(parents=True, exist_ok=True)
|
||||
args.output.write_text(json_dump(telemetry), encoding="utf-8")
|
||||
else:
|
||||
print(json_dump(telemetry), end="")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
|
|
@ -9,6 +9,7 @@ import time
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from epiphany_agent_telemetry import write_transcript_telemetry
|
||||
from epiphany_mvp_status import DEFAULT_APP_SERVER
|
||||
from epiphany_mvp_status import collect_status
|
||||
from epiphany_mvp_status import render_status
|
||||
|
|
@ -202,6 +203,7 @@ def run_action(args: argparse.Namespace) -> dict[str, Any]:
|
|||
artifact_dir = args.artifact_root.resolve() / f"{args.action}-{time.time_ns()}-{os.getpid()}"
|
||||
transcript_path = artifact_dir / "transcript.jsonl"
|
||||
stderr_path = artifact_dir / "server.stderr.log"
|
||||
telemetry_path = artifact_dir / "agent-function-telemetry.json"
|
||||
cwd = args.cwd.resolve()
|
||||
codex_home.mkdir(parents=True, exist_ok=True)
|
||||
artifact_dir.mkdir(parents=True, exist_ok=True)
|
||||
|
|
@ -331,8 +333,10 @@ def run_action(args: argparse.Namespace) -> dict[str, Any]:
|
|||
"reason": "sealed app-server diagnostics; inspect only for explicit debugging",
|
||||
},
|
||||
],
|
||||
"telemetryPath": str(telemetry_path),
|
||||
}
|
||||
write_json(artifact_dir / "gui-action-summary.json", result)
|
||||
write_transcript_telemetry(transcript_path, telemetry_path)
|
||||
return result
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import time
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from epiphany_agent_telemetry import write_transcript_telemetry
|
||||
from epiphany_mvp_status import DEFAULT_APP_SERVER
|
||||
from epiphany_mvp_status import collect_status
|
||||
from epiphany_mvp_status import render_status
|
||||
|
|
@ -236,6 +237,7 @@ def run_coordinator(args: argparse.Namespace) -> dict[str, Any]:
|
|||
reset_artifact_dir(artifact_dir)
|
||||
transcript_path = artifact_dir / "epiphany-transcript.jsonl"
|
||||
stderr_path = artifact_dir / "epiphany-server.stderr.log"
|
||||
telemetry_path = artifact_dir / "agent-function-telemetry.json"
|
||||
steps_path = artifact_dir / "coordinator-steps.jsonl"
|
||||
cwd = args.cwd.resolve()
|
||||
codex_home = args.codex_home.resolve()
|
||||
|
|
@ -497,6 +499,7 @@ def run_coordinator(args: argparse.Namespace) -> dict[str, Any]:
|
|||
"coordinator-final-status.json",
|
||||
"coordinator-final-status.txt",
|
||||
"coordinator-final-action.txt",
|
||||
"agent-function-telemetry.json",
|
||||
*snapshots,
|
||||
],
|
||||
"sealedArtifactManifest": [
|
||||
|
|
@ -517,6 +520,7 @@ def run_coordinator(args: argparse.Namespace) -> dict[str, Any]:
|
|||
artifact_dir / "coordinator-final-action.txt",
|
||||
json.dumps(operator_final_action, indent=2, ensure_ascii=False) + "\n",
|
||||
)
|
||||
write_transcript_telemetry(transcript_path, telemetry_path)
|
||||
return summary
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import shutil
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from epiphany_agent_telemetry import write_transcript_telemetry
|
||||
from epiphany_mvp_status import DEFAULT_APP_SERVER
|
||||
from epiphany_mvp_status import collect_status
|
||||
from epiphany_mvp_status import render_status
|
||||
|
|
@ -234,6 +235,7 @@ def run_dogfood(args: argparse.Namespace) -> dict[str, Any]:
|
|||
workspace = artifact_dir / "workspace"
|
||||
transcript_path = artifact_dir / "epiphany-transcript.jsonl"
|
||||
stderr_path = artifact_dir / "epiphany-server.stderr.log"
|
||||
telemetry_path = artifact_dir / "agent-function-telemetry.json"
|
||||
codex_home.mkdir(parents=True, exist_ok=True)
|
||||
watched_file = prepare_workspace(workspace)
|
||||
|
||||
|
|
@ -394,6 +396,7 @@ def run_dogfood(args: argparse.Namespace) -> dict[str, Any]:
|
|||
"epiphany-final-status.json",
|
||||
"epiphany-final-status.txt",
|
||||
"epiphany-snapshots.json",
|
||||
"agent-function-telemetry.json",
|
||||
"vanilla-reference-prompt.md",
|
||||
"vanilla-reference.md",
|
||||
"comparison.md",
|
||||
|
|
@ -428,6 +431,7 @@ def run_dogfood(args: argparse.Namespace) -> dict[str, Any]:
|
|||
write_json(artifact_dir / "epiphany-final-status.json", operator_final_status)
|
||||
write_text(artifact_dir / "epiphany-final-status.txt", final_rendered)
|
||||
write_json(artifact_dir / "epiphany-dogfood-summary.json", summary)
|
||||
write_transcript_telemetry(transcript_path, telemetry_path)
|
||||
write_json(
|
||||
artifact_dir / "artifact-manifest.json",
|
||||
{
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import time
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from epiphany_agent_telemetry import write_transcript_telemetry
|
||||
from epiphany_mvp_status import DEFAULT_APP_SERVER
|
||||
from epiphany_mvp_status import collect_status
|
||||
from epiphany_mvp_status import render_status
|
||||
|
|
@ -114,6 +115,7 @@ def run_live_specialist(args: argparse.Namespace) -> dict[str, Any]:
|
|||
workspace = artifact_dir / "workspace"
|
||||
transcript_path = artifact_dir / "epiphany-transcript.jsonl"
|
||||
stderr_path = artifact_dir / "epiphany-server.stderr.log"
|
||||
telemetry_path = artifact_dir / "agent-function-telemetry.json"
|
||||
prepare_workspace(workspace)
|
||||
|
||||
role_results: list[dict[str, Any]] = []
|
||||
|
|
@ -183,6 +185,7 @@ def run_live_specialist(args: argparse.Namespace) -> dict[str, Any]:
|
|||
"live-specialist-summary.json",
|
||||
"epiphany-final-status.json",
|
||||
"epiphany-final-status.txt",
|
||||
"agent-function-telemetry.json",
|
||||
],
|
||||
"sealedArtifactManifest": [
|
||||
{
|
||||
|
|
@ -199,6 +202,7 @@ def run_live_specialist(args: argparse.Namespace) -> dict[str, Any]:
|
|||
write_json(artifact_dir / "live-specialist-summary.json", summary)
|
||||
write_json(artifact_dir / "epiphany-final-status.json", operator_final_status)
|
||||
write_text(artifact_dir / "epiphany-final-status.txt", final_rendered)
|
||||
write_transcript_telemetry(transcript_path, telemetry_path)
|
||||
return summary
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import os
|
|||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from epiphany_agent_telemetry import write_transcript_telemetry
|
||||
from epiphany_phase5_smoke import AppServerClient
|
||||
from epiphany_phase5_smoke import DEFAULT_APP_SERVER
|
||||
from epiphany_phase5_smoke import ROOT
|
||||
|
|
@ -285,6 +286,7 @@ def run(args: argparse.Namespace) -> dict[str, Any]:
|
|||
)
|
||||
|
||||
status = sanitize_for_operator(status)
|
||||
write_transcript_telemetry(transcript_path, transcript_path.with_suffix(".telemetry.json"))
|
||||
if args.result is not None:
|
||||
result_path = args.result.resolve()
|
||||
result_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
|
@ -292,6 +294,7 @@ def run(args: argparse.Namespace) -> dict[str, Any]:
|
|||
json.dumps(status, indent=2, ensure_ascii=False) + "\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
write_transcript_telemetry(transcript_path, result_path.with_suffix(".telemetry.json"))
|
||||
return status
|
||||
|
||||
|
||||
|
|
|
|||
Reference in a new issue