traceyard-cli 0.1.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.
- traceyard_cli/__init__.py +3 -0
- traceyard_cli/adapters.py +93 -0
- traceyard_cli/agent.py +168 -0
- traceyard_cli/app.py +457 -0
- traceyard_cli/config.py +182 -0
- traceyard_cli/events.py +214 -0
- traceyard_cli/queue.py +127 -0
- traceyard_cli/repository.py +175 -0
- traceyard_cli-0.1.0.dist-info/METADATA +97 -0
- traceyard_cli-0.1.0.dist-info/RECORD +13 -0
- traceyard_cli-0.1.0.dist-info/WHEEL +4 -0
- traceyard_cli-0.1.0.dist-info/entry_points.txt +2 -0
- traceyard_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"""Provider payload adapters. Core capture never branches on provider payload shape."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from .events import Provider, Status
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True)
|
|
13
|
+
class AdapterEvent:
|
|
14
|
+
provider: Provider
|
|
15
|
+
session_id: str
|
|
16
|
+
root: Path
|
|
17
|
+
status: Status
|
|
18
|
+
summary: str | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def adapt_payload(provider: Provider, payload: dict[str, Any]) -> AdapterEvent:
|
|
22
|
+
if provider == "codex":
|
|
23
|
+
return _codex(payload)
|
|
24
|
+
if provider == "claude_code":
|
|
25
|
+
return _claude(payload)
|
|
26
|
+
return _cursor(payload)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _required_text(payload: dict[str, Any], *keys: str) -> str:
|
|
30
|
+
for key in keys:
|
|
31
|
+
value = payload.get(key)
|
|
32
|
+
if isinstance(value, str) and value.strip():
|
|
33
|
+
return value.strip()
|
|
34
|
+
raise ValueError(f"missing required provider field: {' or '.join(keys)}")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _codex(payload: dict[str, Any]) -> AdapterEvent:
|
|
38
|
+
event = _required_text(payload, "hook_event_name")
|
|
39
|
+
status: Status = {
|
|
40
|
+
"SessionStart": "started",
|
|
41
|
+
"Stop": "working",
|
|
42
|
+
"Interrupt": "interrupted",
|
|
43
|
+
"SessionEnd": "completed",
|
|
44
|
+
}.get(event, "working") # type: ignore[assignment]
|
|
45
|
+
return AdapterEvent(
|
|
46
|
+
provider="codex",
|
|
47
|
+
session_id=_required_text(payload, "session_id"),
|
|
48
|
+
root=Path(_required_text(payload, "cwd")),
|
|
49
|
+
status=status,
|
|
50
|
+
summary=payload.get("last_assistant_message")
|
|
51
|
+
if isinstance(payload.get("last_assistant_message"), str)
|
|
52
|
+
else None,
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _claude(payload: dict[str, Any]) -> AdapterEvent:
|
|
57
|
+
event = _required_text(payload, "hook_event_name")
|
|
58
|
+
status: Status = {
|
|
59
|
+
"SessionStart": "started",
|
|
60
|
+
"Notification": "waiting",
|
|
61
|
+
"Stop": "working",
|
|
62
|
+
"SessionEnd": "completed",
|
|
63
|
+
}.get(event, "working") # type: ignore[assignment]
|
|
64
|
+
return AdapterEvent(
|
|
65
|
+
provider="claude_code",
|
|
66
|
+
session_id=_required_text(payload, "session_id"),
|
|
67
|
+
root=Path(_required_text(payload, "cwd")),
|
|
68
|
+
status=status,
|
|
69
|
+
summary=payload.get("last_assistant_message")
|
|
70
|
+
if isinstance(payload.get("last_assistant_message"), str)
|
|
71
|
+
else None,
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _cursor(payload: dict[str, Any]) -> AdapterEvent:
|
|
76
|
+
event_type = _required_text(payload, "type")
|
|
77
|
+
subtype = str(payload.get("subtype", ""))
|
|
78
|
+
if event_type == "system" and subtype == "init":
|
|
79
|
+
status: Status = "started"
|
|
80
|
+
elif event_type == "result":
|
|
81
|
+
status = (
|
|
82
|
+
"failed" if payload.get("is_error") is True or subtype != "success" else "completed"
|
|
83
|
+
)
|
|
84
|
+
else:
|
|
85
|
+
status = "working"
|
|
86
|
+
summary = payload.get("result") if isinstance(payload.get("result"), str) else None
|
|
87
|
+
return AdapterEvent(
|
|
88
|
+
provider="cursor",
|
|
89
|
+
session_id=_required_text(payload, "session_id"),
|
|
90
|
+
root=Path(_required_text(payload, "cwd") if "cwd" in payload else "."),
|
|
91
|
+
status=status,
|
|
92
|
+
summary=summary,
|
|
93
|
+
)
|
traceyard_cli/agent.py
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"""Provider hook installation and safe diagnostics."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import shutil
|
|
8
|
+
import stat
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Literal
|
|
11
|
+
|
|
12
|
+
from .config import CredentialStore, config_home
|
|
13
|
+
|
|
14
|
+
PROVIDERS = ("codex", "claude_code", "cursor")
|
|
15
|
+
MANAGED_COMMAND_PREFIX = "traceyard agent emit --provider "
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
CaptureMode = Literal["metadata_only", "summary", "full"]
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def install_hooks(
|
|
22
|
+
provider: str, repository_root: Path, capture_mode: CaptureMode = "metadata_only"
|
|
23
|
+
) -> tuple[Path, ...]:
|
|
24
|
+
selected = PROVIDERS if provider == "all" else (provider,)
|
|
25
|
+
if any(item not in PROVIDERS for item in selected):
|
|
26
|
+
raise ValueError(f"Unsupported provider: {provider}")
|
|
27
|
+
adapter_root = config_home() / "adapters"
|
|
28
|
+
adapter_root.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
29
|
+
installed: list[Path] = []
|
|
30
|
+
for item in selected:
|
|
31
|
+
descriptor = adapter_root / f"{item}.json"
|
|
32
|
+
descriptor.write_text(
|
|
33
|
+
json.dumps(
|
|
34
|
+
{
|
|
35
|
+
"adapter_version": "1.0",
|
|
36
|
+
"provider": item,
|
|
37
|
+
"capture_mode": capture_mode,
|
|
38
|
+
"command": f"{MANAGED_COMMAND_PREFIX}{item}",
|
|
39
|
+
},
|
|
40
|
+
indent=2,
|
|
41
|
+
)
|
|
42
|
+
+ "\n",
|
|
43
|
+
encoding="utf-8",
|
|
44
|
+
)
|
|
45
|
+
os.chmod(descriptor, stat.S_IRUSR | stat.S_IWUSR)
|
|
46
|
+
installed.append(descriptor)
|
|
47
|
+
if item == "codex":
|
|
48
|
+
installed.append(_merge_hook_file(repository_root / ".codex" / "hooks.json", item))
|
|
49
|
+
elif item == "claude_code":
|
|
50
|
+
installed.append(_merge_hook_file(repository_root / ".claude" / "settings.json", item))
|
|
51
|
+
else:
|
|
52
|
+
installed.append(_write_cursor_adapter(repository_root))
|
|
53
|
+
return tuple(installed)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def configured_capture_mode(provider: str) -> CaptureMode:
|
|
57
|
+
"""Read the explicit local mode; invalid or absent configuration fails private."""
|
|
58
|
+
|
|
59
|
+
path = config_home() / "adapters" / f"{provider}.json"
|
|
60
|
+
try:
|
|
61
|
+
value = json.loads(path.read_text(encoding="utf-8")).get("capture_mode")
|
|
62
|
+
except OSError, json.JSONDecodeError, AttributeError:
|
|
63
|
+
return "metadata_only"
|
|
64
|
+
return value if value in {"metadata_only", "summary", "full"} else "metadata_only"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _merge_hook_file(path: Path, provider: str) -> Path:
|
|
68
|
+
if path.exists():
|
|
69
|
+
try:
|
|
70
|
+
document = json.loads(path.read_text(encoding="utf-8"))
|
|
71
|
+
except json.JSONDecodeError as error:
|
|
72
|
+
raise ValueError(f"Refusing to replace invalid JSON: {path}") from error
|
|
73
|
+
if not isinstance(document, dict):
|
|
74
|
+
raise ValueError(f"Refusing to replace non-object JSON: {path}")
|
|
75
|
+
else:
|
|
76
|
+
document = {}
|
|
77
|
+
hooks = document.setdefault("hooks", {})
|
|
78
|
+
if not isinstance(hooks, dict):
|
|
79
|
+
raise TypeError(f"Refusing to replace invalid hooks object: {path}")
|
|
80
|
+
events: tuple[str, ...] = ("SessionStart", "Stop", "SessionEnd")
|
|
81
|
+
if provider == "claude_code":
|
|
82
|
+
events = (*events, "Notification")
|
|
83
|
+
command = f"{MANAGED_COMMAND_PREFIX}{provider}"
|
|
84
|
+
for event in events:
|
|
85
|
+
groups = hooks.setdefault(event, [])
|
|
86
|
+
if not isinstance(groups, list):
|
|
87
|
+
raise TypeError(f"Refusing to replace invalid {event} hooks: {path}")
|
|
88
|
+
if not _contains_command(groups, command):
|
|
89
|
+
groups.append({"hooks": [{"type": "command", "command": command, "timeout": 3}]})
|
|
90
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
91
|
+
path.write_text(json.dumps(document, indent=2) + "\n", encoding="utf-8")
|
|
92
|
+
return path
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _contains_command(groups: list[object], command: str) -> bool:
|
|
96
|
+
for group in groups:
|
|
97
|
+
if not isinstance(group, dict) or not isinstance(group.get("hooks"), list):
|
|
98
|
+
continue
|
|
99
|
+
for handler in group["hooks"]:
|
|
100
|
+
if isinstance(handler, dict) and handler.get("command") == command:
|
|
101
|
+
return True
|
|
102
|
+
return False
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _write_cursor_adapter(root: Path) -> Path:
|
|
106
|
+
path = root / ".traceyard" / "cursor-adapter.json"
|
|
107
|
+
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
108
|
+
path.write_text(
|
|
109
|
+
json.dumps(
|
|
110
|
+
{
|
|
111
|
+
"adapter_version": "1.0",
|
|
112
|
+
"provider": "cursor",
|
|
113
|
+
"input": "cursor-agent --print --output-format stream-json",
|
|
114
|
+
"collector": "traceyard agent stream --provider cursor",
|
|
115
|
+
},
|
|
116
|
+
indent=2,
|
|
117
|
+
)
|
|
118
|
+
+ "\n",
|
|
119
|
+
encoding="utf-8",
|
|
120
|
+
)
|
|
121
|
+
return path
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def doctor(store: CredentialStore, repository_root: Path | None = None) -> dict[str, object]:
|
|
125
|
+
adapter_root = config_home() / "adapters"
|
|
126
|
+
adapters: dict[str, str] = {}
|
|
127
|
+
for provider in PROVIDERS:
|
|
128
|
+
path = adapter_root / f"{provider}.json"
|
|
129
|
+
if not path.exists():
|
|
130
|
+
adapters[provider] = "not_installed"
|
|
131
|
+
continue
|
|
132
|
+
try:
|
|
133
|
+
document = json.loads(path.read_text(encoding="utf-8"))
|
|
134
|
+
adapters[provider] = "ok" if document.get("provider") == provider else "invalid"
|
|
135
|
+
except OSError, json.JSONDecodeError:
|
|
136
|
+
adapters[provider] = "invalid"
|
|
137
|
+
hooks: dict[str, str] = {}
|
|
138
|
+
if repository_root is not None:
|
|
139
|
+
for provider, relative in {
|
|
140
|
+
"codex": ".codex/hooks.json",
|
|
141
|
+
"claude_code": ".claude/settings.json",
|
|
142
|
+
"cursor": ".traceyard/cursor-adapter.json",
|
|
143
|
+
}.items():
|
|
144
|
+
hooks[provider] = "ok" if (repository_root / relative).is_file() else "not_installed"
|
|
145
|
+
token_available = store.get_token() is not None
|
|
146
|
+
git_available = shutil.which("git") is not None
|
|
147
|
+
healthy = (
|
|
148
|
+
token_available
|
|
149
|
+
and git_available
|
|
150
|
+
and all(value in {"ok", "not_installed"} for value in adapters.values())
|
|
151
|
+
)
|
|
152
|
+
remediation: list[str] = []
|
|
153
|
+
if not token_available:
|
|
154
|
+
remediation.append("Run `traceyard login` or set TRACEYARD_TOKEN for this process.")
|
|
155
|
+
if not git_available:
|
|
156
|
+
remediation.append("Install Git and ensure it is on PATH.")
|
|
157
|
+
if "invalid" in adapters.values():
|
|
158
|
+
remediation.append("Re-run `traceyard agent install --provider <provider>`.")
|
|
159
|
+
return {
|
|
160
|
+
"status": "ok" if healthy else "failed",
|
|
161
|
+
"checks": {
|
|
162
|
+
"credential": "ok" if token_available else "missing",
|
|
163
|
+
"git": "ok" if git_available else "missing",
|
|
164
|
+
"adapters": adapters,
|
|
165
|
+
"hooks": hooks,
|
|
166
|
+
},
|
|
167
|
+
"remediation": remediation,
|
|
168
|
+
}
|