kaiten-cli 0.3.3__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.
- kaiten_cli/__init__.py +5 -0
- kaiten_cli/__main__.py +9 -0
- kaiten_cli/agent_gateway.py +577 -0
- kaiten_cli/app.py +1636 -0
- kaiten_cli/completion.py +570 -0
- kaiten_cli/discovery.py +39 -0
- kaiten_cli/errors.py +79 -0
- kaiten_cli/i18n.py +92 -0
- kaiten_cli/locales/ru.json +2967 -0
- kaiten_cli/localized_click.py +160 -0
- kaiten_cli/models.py +202 -0
- kaiten_cli/profiles.py +316 -0
- kaiten_cli/registry/__init__.py +260 -0
- kaiten_cli/registry/addons.py +392 -0
- kaiten_cli/registry/audit_and_analytics.py +451 -0
- kaiten_cli/registry/automation_schema.py +44 -0
- kaiten_cli/registry/automation_schemas.json +7417 -0
- kaiten_cli/registry/automations.py +342 -0
- kaiten_cli/registry/base.py +62 -0
- kaiten_cli/registry/blockers.py +320 -0
- kaiten_cli/registry/boards.py +520 -0
- kaiten_cli/registry/card_relations.py +376 -0
- kaiten_cli/registry/card_types.py +363 -0
- kaiten_cli/registry/cards.py +1329 -0
- kaiten_cli/registry/charts.py +539 -0
- kaiten_cli/registry/checklists.py +600 -0
- kaiten_cli/registry/columns.py +558 -0
- kaiten_cli/registry/comments.py +224 -0
- kaiten_cli/registry/custom_directories.py +980 -0
- kaiten_cli/registry/custom_properties.py +1460 -0
- kaiten_cli/registry/dashboards.py +564 -0
- kaiten_cli/registry/documents.py +761 -0
- kaiten_cli/registry/external_links.py +128 -0
- kaiten_cli/registry/files.py +738 -0
- kaiten_cli/registry/github_addon.py +613 -0
- kaiten_cli/registry/iterations.py +397 -0
- kaiten_cli/registry/lanes.py +205 -0
- kaiten_cli/registry/live_contracts.py +361 -0
- kaiten_cli/registry/members.py +336 -0
- kaiten_cli/registry/module_docs.py +108 -0
- kaiten_cli/registry/projects.py +369 -0
- kaiten_cli/registry/query.py +129 -0
- kaiten_cli/registry/restrictions.py +249 -0
- kaiten_cli/registry/roles_and_groups.py +1102 -0
- kaiten_cli/registry/scim.py +263 -0
- kaiten_cli/registry/service_desk.py +1270 -0
- kaiten_cli/registry/snapshot.py +187 -0
- kaiten_cli/registry/spaces.py +272 -0
- kaiten_cli/registry/subscribers.py +168 -0
- kaiten_cli/registry/tags.py +193 -0
- kaiten_cli/registry/time_logs.py +353 -0
- kaiten_cli/registry/tree.py +352 -0
- kaiten_cli/registry/utilities.py +287 -0
- kaiten_cli/registry/webhooks.py +265 -0
- kaiten_cli/runtime/__init__.py +1 -0
- kaiten_cli/runtime/automation_validation.py +115 -0
- kaiten_cli/runtime/behaviors.py +787 -0
- kaiten_cli/runtime/cache.py +733 -0
- kaiten_cli/runtime/client.py +387 -0
- kaiten_cli/runtime/endpoints.py +59 -0
- kaiten_cli/runtime/executor.py +269 -0
- kaiten_cli/runtime/fs_security.py +168 -0
- kaiten_cli/runtime/input.py +310 -0
- kaiten_cli/runtime/output.py +59 -0
- kaiten_cli/runtime/public_validation.py +35 -0
- kaiten_cli/runtime/snapshot_build.py +527 -0
- kaiten_cli/runtime/snapshot_common.py +363 -0
- kaiten_cli/runtime/snapshot_query.py +306 -0
- kaiten_cli/runtime/snapshot_store.py +1192 -0
- kaiten_cli/runtime/snapshots.py +62 -0
- kaiten_cli/runtime/sqlite_errors.py +23 -0
- kaiten_cli/runtime/support/__init__.py +1 -0
- kaiten_cli/runtime/support/addons.py +953 -0
- kaiten_cli/runtime/support/audit.py +70 -0
- kaiten_cli/runtime/support/batch.py +234 -0
- kaiten_cli/runtime/support/card_move_url.py +213 -0
- kaiten_cli/runtime/support/cards.py +174 -0
- kaiten_cli/runtime/support/checklists.py +21 -0
- kaiten_cli/runtime/support/company_users.py +117 -0
- kaiten_cli/runtime/support/dashboards.py +35 -0
- kaiten_cli/runtime/support/documents.py +175 -0
- kaiten_cli/runtime/support/files.py +561 -0
- kaiten_cli/runtime/support/markdown_export.py +525 -0
- kaiten_cli/runtime/support/pagination.py +167 -0
- kaiten_cli/runtime/support/projects.py +66 -0
- kaiten_cli/runtime/support/relations.py +86 -0
- kaiten_cli/runtime/support/restrictions.py +62 -0
- kaiten_cli/runtime/support/spaces.py +20 -0
- kaiten_cli/runtime/support/time_logs.py +61 -0
- kaiten_cli/runtime/support/tree.py +140 -0
- kaiten_cli/runtime/support/tree_sharing.py +447 -0
- kaiten_cli/runtime/trace.py +500 -0
- kaiten_cli/runtime/transforms.py +128 -0
- kaiten_cli/schema_docs.py +22 -0
- kaiten_cli/update_check.py +519 -0
- kaiten_cli-0.3.3.dist-info/METADATA +1110 -0
- kaiten_cli-0.3.3.dist-info/RECORD +99 -0
- kaiten_cli-0.3.3.dist-info/WHEEL +4 -0
- kaiten_cli-0.3.3.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
"""Command trace helpers for CLI-level observability."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
from collections import Counter
|
|
8
|
+
from dataclasses import dataclass, field
|
|
9
|
+
from datetime import datetime, timezone
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from kaiten_cli.errors import ValidationError
|
|
14
|
+
from kaiten_cli.i18n import tr
|
|
15
|
+
from kaiten_cli.runtime.fs_security import open_private_append
|
|
16
|
+
|
|
17
|
+
REDACTED_ARG_VALUE = "[REDACTED]"
|
|
18
|
+
_SENSITIVE_FLAGS = {"--token", "--password", "--old-password"}
|
|
19
|
+
_NUMERIC_PATH_SEGMENT_RE = re.compile(r"/\d+(?=/|$)")
|
|
20
|
+
_UUID_PATH_SEGMENT_RE = re.compile(
|
|
21
|
+
r"/[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}(?=/|$)"
|
|
22
|
+
)
|
|
23
|
+
_SAFE_CANONICAL_NAME_RE = re.compile(r"^[a-z0-9_.-]+$")
|
|
24
|
+
_TRACE_SUMMARY_LIMIT = 20
|
|
25
|
+
_POPULATION_COMMANDS = frozenset(
|
|
26
|
+
{
|
|
27
|
+
"cards.list",
|
|
28
|
+
"cards.list-all",
|
|
29
|
+
"space-topology.get",
|
|
30
|
+
"space-activity-all.get",
|
|
31
|
+
}
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def path_family_for(path: str) -> str:
|
|
36
|
+
family = _UUID_PATH_SEGMENT_RE.sub("/:id", path)
|
|
37
|
+
return _NUMERIC_PATH_SEGMENT_RE.sub("/:id", family)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass(slots=True)
|
|
41
|
+
class ExecutionGroupStats:
|
|
42
|
+
source: str
|
|
43
|
+
method: str
|
|
44
|
+
path_family: str
|
|
45
|
+
http_request_count: int = 0
|
|
46
|
+
http_response_count: int = 0
|
|
47
|
+
http_error_count: int = 0
|
|
48
|
+
api_wait_ms: float = 0.0
|
|
49
|
+
http_wait_ms: float = 0.0
|
|
50
|
+
request_cache_hits: int = 0
|
|
51
|
+
request_cache_misses: int = 0
|
|
52
|
+
inflight_dedup_hits: int = 0
|
|
53
|
+
disk_cache_hits: int = 0
|
|
54
|
+
disk_cache_misses: int = 0
|
|
55
|
+
disk_cache_expired: int = 0
|
|
56
|
+
disk_cache_bypasses: int = 0
|
|
57
|
+
|
|
58
|
+
def cache_hits(self) -> dict[str, int]:
|
|
59
|
+
return {
|
|
60
|
+
"request": self.request_cache_hits,
|
|
61
|
+
"inflight_dedup": self.inflight_dedup_hits,
|
|
62
|
+
"disk": self.disk_cache_hits,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
def cache_misses(self) -> dict[str, int]:
|
|
66
|
+
return {
|
|
67
|
+
"request": self.request_cache_misses,
|
|
68
|
+
"disk": self.disk_cache_misses + self.disk_cache_expired,
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
def cache_bypasses(self) -> dict[str, int]:
|
|
72
|
+
return {"disk": self.disk_cache_bypasses}
|
|
73
|
+
|
|
74
|
+
def to_payload(self) -> dict[str, Any]:
|
|
75
|
+
return {
|
|
76
|
+
"source": self.source,
|
|
77
|
+
"method": self.method,
|
|
78
|
+
"path_family": self.path_family,
|
|
79
|
+
"http_request_count": self.http_request_count,
|
|
80
|
+
"http_response_count": self.http_response_count,
|
|
81
|
+
"http_error_count": self.http_error_count,
|
|
82
|
+
"api_wait_ms": round(self.api_wait_ms, 2),
|
|
83
|
+
"http_wait_ms": round(self.http_wait_ms, 2),
|
|
84
|
+
"cache_hits": self.cache_hits(),
|
|
85
|
+
"cache_misses": self.cache_misses(),
|
|
86
|
+
"cache_bypasses": self.cache_bypasses(),
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(slots=True)
|
|
91
|
+
class ExecutionStats:
|
|
92
|
+
cache_mode: str = "not_applicable"
|
|
93
|
+
cache_policy: str = "none"
|
|
94
|
+
cache_ttl_seconds: int | None = None
|
|
95
|
+
http_request_count: int = 0
|
|
96
|
+
http_response_count: int = 0
|
|
97
|
+
http_error_count: int = 0
|
|
98
|
+
api_wait_ms: float = 0.0
|
|
99
|
+
http_wait_ms: float = 0.0
|
|
100
|
+
retry_count: int = 0
|
|
101
|
+
request_cache_hits: int = 0
|
|
102
|
+
request_cache_misses: int = 0
|
|
103
|
+
inflight_dedup_hits: int = 0
|
|
104
|
+
disk_cache_hits: int = 0
|
|
105
|
+
disk_cache_misses: int = 0
|
|
106
|
+
disk_cache_expired: int = 0
|
|
107
|
+
disk_cache_bypasses: int = 0
|
|
108
|
+
pagination_compatibility: list[dict[str, Any]] = field(default_factory=list)
|
|
109
|
+
_groups: dict[tuple[str, str, str], ExecutionGroupStats] = field(
|
|
110
|
+
default_factory=dict, init=False
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
def _group(self, *, source: str, method: str, path_family: str) -> ExecutionGroupStats:
|
|
114
|
+
key = (source, method.upper(), path_family)
|
|
115
|
+
group = self._groups.get(key)
|
|
116
|
+
if group is None:
|
|
117
|
+
group = ExecutionGroupStats(
|
|
118
|
+
source=source, method=method.upper(), path_family=path_family
|
|
119
|
+
)
|
|
120
|
+
self._groups[key] = group
|
|
121
|
+
return group
|
|
122
|
+
|
|
123
|
+
def record_http_attempt(
|
|
124
|
+
self,
|
|
125
|
+
*,
|
|
126
|
+
source: str,
|
|
127
|
+
method: str,
|
|
128
|
+
path: str,
|
|
129
|
+
wait_ms: float,
|
|
130
|
+
status_code: int | None = None,
|
|
131
|
+
error: bool = False,
|
|
132
|
+
) -> None:
|
|
133
|
+
path_family = path_family_for(path)
|
|
134
|
+
self.http_request_count += 1
|
|
135
|
+
self.http_wait_ms += wait_ms
|
|
136
|
+
if source == "kaiten_api":
|
|
137
|
+
self.api_wait_ms += wait_ms
|
|
138
|
+
if status_code is not None:
|
|
139
|
+
self.http_response_count += 1
|
|
140
|
+
if error or (status_code is not None and status_code >= 400):
|
|
141
|
+
self.http_error_count += 1
|
|
142
|
+
group = self._group(source=source, method=method, path_family=path_family)
|
|
143
|
+
group.http_request_count += 1
|
|
144
|
+
group.http_wait_ms += wait_ms
|
|
145
|
+
if source == "kaiten_api":
|
|
146
|
+
group.api_wait_ms += wait_ms
|
|
147
|
+
if status_code is not None:
|
|
148
|
+
group.http_response_count += 1
|
|
149
|
+
if error or (status_code is not None and status_code >= 400):
|
|
150
|
+
group.http_error_count += 1
|
|
151
|
+
|
|
152
|
+
def record_cache_hit(self, *, cache: str, method: str, path_family: str) -> None:
|
|
153
|
+
group = self._group(source="kaiten_api", method=method, path_family=path_family)
|
|
154
|
+
if cache == "request":
|
|
155
|
+
self.request_cache_hits += 1
|
|
156
|
+
group.request_cache_hits += 1
|
|
157
|
+
elif cache == "inflight_dedup":
|
|
158
|
+
self.inflight_dedup_hits += 1
|
|
159
|
+
group.inflight_dedup_hits += 1
|
|
160
|
+
elif cache == "disk":
|
|
161
|
+
self.disk_cache_hits += 1
|
|
162
|
+
group.disk_cache_hits += 1
|
|
163
|
+
|
|
164
|
+
def record_cache_miss(self, *, cache: str, method: str, path_family: str) -> None:
|
|
165
|
+
group = self._group(source="kaiten_api", method=method, path_family=path_family)
|
|
166
|
+
if cache == "request":
|
|
167
|
+
self.request_cache_misses += 1
|
|
168
|
+
group.request_cache_misses += 1
|
|
169
|
+
elif cache == "disk":
|
|
170
|
+
self.disk_cache_misses += 1
|
|
171
|
+
group.disk_cache_misses += 1
|
|
172
|
+
elif cache == "disk_expired":
|
|
173
|
+
self.disk_cache_expired += 1
|
|
174
|
+
group.disk_cache_expired += 1
|
|
175
|
+
|
|
176
|
+
def record_cache_bypass(self, *, cache: str, method: str, path_family: str) -> None:
|
|
177
|
+
group = self._group(source="kaiten_api", method=method, path_family=path_family)
|
|
178
|
+
if cache == "disk":
|
|
179
|
+
self.disk_cache_bypasses += 1
|
|
180
|
+
group.disk_cache_bypasses += 1
|
|
181
|
+
|
|
182
|
+
def record_pagination_compatibility(
|
|
183
|
+
self, *, path: str, mode: str, reason: str, rows: int
|
|
184
|
+
) -> None:
|
|
185
|
+
"""Record payload-free compatibility decisions for command traces."""
|
|
186
|
+
self.pagination_compatibility.append(
|
|
187
|
+
{
|
|
188
|
+
"path_family": path_family_for(path),
|
|
189
|
+
"mode": mode,
|
|
190
|
+
"reason": reason,
|
|
191
|
+
"rows": rows,
|
|
192
|
+
}
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
def cache_hits(self) -> dict[str, int]:
|
|
196
|
+
return {
|
|
197
|
+
"request": self.request_cache_hits,
|
|
198
|
+
"inflight_dedup": self.inflight_dedup_hits,
|
|
199
|
+
"disk": self.disk_cache_hits,
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
def cache_misses(self) -> dict[str, int]:
|
|
203
|
+
return {
|
|
204
|
+
"request": self.request_cache_misses,
|
|
205
|
+
"disk": self.disk_cache_misses + self.disk_cache_expired,
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
def cache_bypasses(self) -> dict[str, int]:
|
|
209
|
+
return {"disk": self.disk_cache_bypasses}
|
|
210
|
+
|
|
211
|
+
def groups_payload(self) -> list[dict[str, Any]]:
|
|
212
|
+
return [
|
|
213
|
+
group.to_payload()
|
|
214
|
+
for group in sorted(
|
|
215
|
+
self._groups.values(),
|
|
216
|
+
key=lambda item: (item.source, item.method, item.path_family),
|
|
217
|
+
)
|
|
218
|
+
]
|
|
219
|
+
|
|
220
|
+
def to_payload(self, *, command_duration_ms: float | None = None) -> dict[str, Any]:
|
|
221
|
+
payload: dict[str, Any] = {
|
|
222
|
+
"cache": {
|
|
223
|
+
"mode": self.cache_mode,
|
|
224
|
+
"policy": self.cache_policy,
|
|
225
|
+
"ttl_seconds": self.cache_ttl_seconds,
|
|
226
|
+
},
|
|
227
|
+
"http_request_count": self.http_request_count,
|
|
228
|
+
"http_response_count": self.http_response_count,
|
|
229
|
+
"http_error_count": self.http_error_count,
|
|
230
|
+
"retry_count": self.retry_count,
|
|
231
|
+
"api_wait_ms": round(self.api_wait_ms, 2),
|
|
232
|
+
"http_wait_ms": round(self.http_wait_ms, 2),
|
|
233
|
+
"cache_hits": self.cache_hits(),
|
|
234
|
+
"cache_misses": self.cache_misses(),
|
|
235
|
+
"cache_bypasses": self.cache_bypasses(),
|
|
236
|
+
"groups": self.groups_payload(),
|
|
237
|
+
}
|
|
238
|
+
if self.pagination_compatibility:
|
|
239
|
+
payload["pagination_compatibility"] = [
|
|
240
|
+
dict(event) for event in self.pagination_compatibility
|
|
241
|
+
]
|
|
242
|
+
if command_duration_ms is not None:
|
|
243
|
+
payload["command_duration_ms"] = round(command_duration_ms, 2)
|
|
244
|
+
return payload
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def redact_argv(argv: list[str]) -> list[str]:
|
|
248
|
+
redacted: list[str] = []
|
|
249
|
+
skip_next = False
|
|
250
|
+
for value in argv:
|
|
251
|
+
if skip_next:
|
|
252
|
+
redacted.append(REDACTED_ARG_VALUE)
|
|
253
|
+
skip_next = False
|
|
254
|
+
continue
|
|
255
|
+
if value in _SENSITIVE_FLAGS:
|
|
256
|
+
redacted.append(value)
|
|
257
|
+
skip_next = True
|
|
258
|
+
continue
|
|
259
|
+
for flag in _SENSITIVE_FLAGS:
|
|
260
|
+
prefix = f"{flag}="
|
|
261
|
+
if value.startswith(prefix):
|
|
262
|
+
redacted.append(f"{prefix}{REDACTED_ARG_VALUE}")
|
|
263
|
+
break
|
|
264
|
+
else:
|
|
265
|
+
redacted.append(value)
|
|
266
|
+
return redacted
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def bulk_trace_meta(data: Any) -> dict[str, Any]:
|
|
270
|
+
if not isinstance(data, dict):
|
|
271
|
+
return {}
|
|
272
|
+
meta = data.get("meta")
|
|
273
|
+
if not isinstance(meta, dict):
|
|
274
|
+
return {}
|
|
275
|
+
payload: dict[str, Any] = {}
|
|
276
|
+
for key in ("requested_count", "unique_count", "workers", "succeeded", "failed"):
|
|
277
|
+
if key in meta:
|
|
278
|
+
payload[key] = meta[key]
|
|
279
|
+
if "trace" in meta:
|
|
280
|
+
payload["stage_trace"] = meta["trace"]
|
|
281
|
+
return payload
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def _safe_int(value: Any) -> int:
|
|
285
|
+
try:
|
|
286
|
+
return int(value)
|
|
287
|
+
except (TypeError, ValueError):
|
|
288
|
+
return 0
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
def _safe_float(value: Any) -> float:
|
|
292
|
+
try:
|
|
293
|
+
return float(value)
|
|
294
|
+
except (TypeError, ValueError):
|
|
295
|
+
return 0.0
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _counter_payload(counter: Counter[str], *, key: str) -> list[dict[str, Any]]:
|
|
299
|
+
return [
|
|
300
|
+
{key: value, "count": count}
|
|
301
|
+
for value, count in sorted(counter.items(), key=lambda item: (-item[1], item[0]))[
|
|
302
|
+
:_TRACE_SUMMARY_LIMIT
|
|
303
|
+
]
|
|
304
|
+
]
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
def summarize_trace(path: str | Path) -> dict[str, Any]:
|
|
308
|
+
"""Stream a trace file into a bounded, payload-free workflow summary."""
|
|
309
|
+
|
|
310
|
+
trace_path = Path(path)
|
|
311
|
+
command_counts: Counter[str] = Counter()
|
|
312
|
+
command_modes: dict[str, str] = {}
|
|
313
|
+
path_counts: Counter[str] = Counter()
|
|
314
|
+
working_set_signatures: Counter[tuple[str, str]] = Counter()
|
|
315
|
+
cache_hits: Counter[str] = Counter()
|
|
316
|
+
cache_misses: Counter[str] = Counter()
|
|
317
|
+
cache_bypasses: Counter[str] = Counter()
|
|
318
|
+
lines = 0
|
|
319
|
+
entries = 0
|
|
320
|
+
invalid_lines = 0
|
|
321
|
+
failures = 0
|
|
322
|
+
duration_ms = 0.0
|
|
323
|
+
http_request_count = 0
|
|
324
|
+
api_wait_ms = 0.0
|
|
325
|
+
retry_count = 0
|
|
326
|
+
refresh_count = 0
|
|
327
|
+
|
|
328
|
+
try:
|
|
329
|
+
handle = trace_path.open("r", encoding="utf-8", errors="replace")
|
|
330
|
+
except OSError as exc:
|
|
331
|
+
raise ValidationError(
|
|
332
|
+
tr("Unable to read trace file: {value_0}.", value_0=type(exc).__name__)
|
|
333
|
+
) from exc
|
|
334
|
+
|
|
335
|
+
with handle:
|
|
336
|
+
for line in handle:
|
|
337
|
+
lines += 1
|
|
338
|
+
try:
|
|
339
|
+
entry = json.loads(line)
|
|
340
|
+
except json.JSONDecodeError:
|
|
341
|
+
invalid_lines += 1
|
|
342
|
+
continue
|
|
343
|
+
if not isinstance(entry, dict):
|
|
344
|
+
invalid_lines += 1
|
|
345
|
+
continue
|
|
346
|
+
|
|
347
|
+
canonical_name = entry.get("canonical_name")
|
|
348
|
+
if not isinstance(canonical_name, str) or not _SAFE_CANONICAL_NAME_RE.fullmatch(
|
|
349
|
+
canonical_name
|
|
350
|
+
):
|
|
351
|
+
canonical_name = "unknown"
|
|
352
|
+
entries += 1
|
|
353
|
+
command_counts[canonical_name] += 1
|
|
354
|
+
command_modes[canonical_name] = str(entry.get("execution_mode") or "unknown")
|
|
355
|
+
if _safe_int(entry.get("exit_code")) != 0:
|
|
356
|
+
failures += 1
|
|
357
|
+
duration_ms += _safe_float(entry.get("duration_ms"))
|
|
358
|
+
|
|
359
|
+
stats = entry.get("stats")
|
|
360
|
+
stats = stats if isinstance(stats, dict) else {}
|
|
361
|
+
http_request_count += _safe_int(
|
|
362
|
+
stats.get("http_request_count", entry.get("http_request_count"))
|
|
363
|
+
)
|
|
364
|
+
api_wait_ms += _safe_float(stats.get("api_wait_ms"))
|
|
365
|
+
retry_count += _safe_int(stats.get("retry_count", entry.get("retry_count")))
|
|
366
|
+
|
|
367
|
+
cache = stats.get("cache")
|
|
368
|
+
cache = cache if isinstance(cache, dict) else entry.get("cache")
|
|
369
|
+
if isinstance(cache, dict) and cache.get("mode") == "refresh":
|
|
370
|
+
refresh_count += 1
|
|
371
|
+
|
|
372
|
+
for source, target in (
|
|
373
|
+
(stats.get("cache_hits", entry.get("cache_hits")), cache_hits),
|
|
374
|
+
(stats.get("cache_misses", entry.get("cache_misses")), cache_misses),
|
|
375
|
+
(stats.get("cache_bypasses", entry.get("cache_bypasses")), cache_bypasses),
|
|
376
|
+
):
|
|
377
|
+
if isinstance(source, dict):
|
|
378
|
+
for cache_name, count in source.items():
|
|
379
|
+
if isinstance(cache_name, str):
|
|
380
|
+
target[cache_name] += _safe_int(count)
|
|
381
|
+
|
|
382
|
+
groups = stats.get("groups")
|
|
383
|
+
if isinstance(groups, list):
|
|
384
|
+
for group in groups:
|
|
385
|
+
if not isinstance(group, dict):
|
|
386
|
+
continue
|
|
387
|
+
family = group.get("path_family")
|
|
388
|
+
if isinstance(family, str) and family.startswith("/"):
|
|
389
|
+
path_counts[family] += _safe_int(group.get("http_request_count"))
|
|
390
|
+
|
|
391
|
+
argv = entry.get("argv")
|
|
392
|
+
if canonical_name in _POPULATION_COMMANDS and isinstance(argv, list):
|
|
393
|
+
signature = json.dumps(argv, ensure_ascii=False, separators=(",", ":"))
|
|
394
|
+
working_set_signatures[(canonical_name, signature)] += 1
|
|
395
|
+
|
|
396
|
+
n_plus_one_commands = sorted(
|
|
397
|
+
command
|
|
398
|
+
for command, count in command_counts.items()
|
|
399
|
+
if count >= 5
|
|
400
|
+
and command_modes.get(command) not in {"local", "meta"}
|
|
401
|
+
and not command.startswith(("query.", "snapshot."))
|
|
402
|
+
)
|
|
403
|
+
n_plus_one_paths = sorted(family for family, count in path_counts.items() if count >= 10)
|
|
404
|
+
repeated_populations = sorted(
|
|
405
|
+
{command for (command, _), count in working_set_signatures.items() if count >= 2}
|
|
406
|
+
)
|
|
407
|
+
recommendations: list[dict[str, Any]] = []
|
|
408
|
+
if n_plus_one_commands or n_plus_one_paths:
|
|
409
|
+
recommendations.append(
|
|
410
|
+
{
|
|
411
|
+
"code": "prefer_batch",
|
|
412
|
+
"commands": n_plus_one_commands,
|
|
413
|
+
"path_families": n_plus_one_paths,
|
|
414
|
+
}
|
|
415
|
+
)
|
|
416
|
+
if repeated_populations:
|
|
417
|
+
recommendations.append(
|
|
418
|
+
{
|
|
419
|
+
"code": "prefer_snapshot",
|
|
420
|
+
"commands": repeated_populations,
|
|
421
|
+
}
|
|
422
|
+
)
|
|
423
|
+
if refresh_count > 1:
|
|
424
|
+
recommendations.append(
|
|
425
|
+
{
|
|
426
|
+
"code": "prefer_auto",
|
|
427
|
+
"refresh_commands": refresh_count,
|
|
428
|
+
}
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
return {
|
|
432
|
+
"lines": lines,
|
|
433
|
+
"entries": entries,
|
|
434
|
+
"invalid_lines": invalid_lines,
|
|
435
|
+
"failures": failures,
|
|
436
|
+
"duration_ms": round(duration_ms, 2),
|
|
437
|
+
"http_request_count": http_request_count,
|
|
438
|
+
"api_wait_ms": round(api_wait_ms, 2),
|
|
439
|
+
"retry_count": retry_count,
|
|
440
|
+
"cache_hits": dict(sorted(cache_hits.items())),
|
|
441
|
+
"cache_misses": dict(sorted(cache_misses.items())),
|
|
442
|
+
"cache_bypasses": dict(sorted(cache_bypasses.items())),
|
|
443
|
+
"commands": _counter_payload(command_counts, key="canonical_name"),
|
|
444
|
+
"path_families": _counter_payload(path_counts, key="path_family"),
|
|
445
|
+
"truncated": (
|
|
446
|
+
len(command_counts) > _TRACE_SUMMARY_LIMIT or len(path_counts) > _TRACE_SUMMARY_LIMIT
|
|
447
|
+
),
|
|
448
|
+
"recommendations": recommendations,
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
|
|
452
|
+
class TraceRecorder:
|
|
453
|
+
"""Append compact JSONL command traces."""
|
|
454
|
+
|
|
455
|
+
def __init__(self, path: str | Path):
|
|
456
|
+
self.path = Path(path)
|
|
457
|
+
|
|
458
|
+
def write(
|
|
459
|
+
self,
|
|
460
|
+
*,
|
|
461
|
+
canonical_name: str,
|
|
462
|
+
execution_mode: str,
|
|
463
|
+
argv: list[str],
|
|
464
|
+
exit_code: int,
|
|
465
|
+
duration_ms: float,
|
|
466
|
+
stats: ExecutionStats | None = None,
|
|
467
|
+
bulk_meta: dict[str, Any] | None = None,
|
|
468
|
+
) -> None:
|
|
469
|
+
payload: dict[str, Any] = {
|
|
470
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
471
|
+
"canonical_name": canonical_name,
|
|
472
|
+
"argv": redact_argv(argv),
|
|
473
|
+
"execution_mode": execution_mode,
|
|
474
|
+
"exit_code": exit_code,
|
|
475
|
+
"duration_ms": round(duration_ms, 2),
|
|
476
|
+
"http_request_count": 0,
|
|
477
|
+
"retry_count": 0,
|
|
478
|
+
"cache_hits": {"request": 0, "inflight_dedup": 0, "disk": 0},
|
|
479
|
+
"cache_misses": {"request": 0, "disk": 0},
|
|
480
|
+
"cache_bypasses": {"disk": 0},
|
|
481
|
+
"cache": {
|
|
482
|
+
"mode": "not_applicable",
|
|
483
|
+
"policy": "none",
|
|
484
|
+
"ttl_seconds": None,
|
|
485
|
+
},
|
|
486
|
+
}
|
|
487
|
+
stats_payload = (stats or ExecutionStats()).to_payload(command_duration_ms=duration_ms)
|
|
488
|
+
payload["stats"] = stats_payload
|
|
489
|
+
payload["cache"] = stats_payload["cache"]
|
|
490
|
+
if stats is not None:
|
|
491
|
+
payload["http_request_count"] = stats.http_request_count
|
|
492
|
+
payload["retry_count"] = stats.retry_count
|
|
493
|
+
payload["cache_hits"] = stats.cache_hits()
|
|
494
|
+
payload["cache_misses"] = stats.cache_misses()
|
|
495
|
+
payload["cache_bypasses"] = stats.cache_bypasses()
|
|
496
|
+
if bulk_meta:
|
|
497
|
+
payload.update(bulk_meta)
|
|
498
|
+
with open_private_append(self.path, repair_parent=False) as handle:
|
|
499
|
+
handle.write(json.dumps(payload, ensure_ascii=False, separators=(",", ":")))
|
|
500
|
+
handle.write("\n")
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Response transforms copied conceptually from kaiten-mcp."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
DEFAULT_LIMIT = 50
|
|
8
|
+
SIMPLIFY_FIELDS = {"owner", "responsible", "author", "user", "created_by", "updated_by"}
|
|
9
|
+
SIMPLIFY_LIST_FIELDS = {"members", "responsibles", "owners", "subscribers", "participants"}
|
|
10
|
+
STRIP_FIELDS = {"description"}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _is_base64_avatar(value: Any) -> bool:
|
|
14
|
+
return isinstance(value, str) and value.startswith("data:")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _simplify_user(user: Any) -> Any:
|
|
18
|
+
if not isinstance(user, dict):
|
|
19
|
+
return user
|
|
20
|
+
result: dict[str, Any] = {}
|
|
21
|
+
if "id" in user:
|
|
22
|
+
result["id"] = user["id"]
|
|
23
|
+
if "full_name" in user:
|
|
24
|
+
result["full_name"] = user["full_name"]
|
|
25
|
+
elif "username" in user:
|
|
26
|
+
result["full_name"] = user["username"]
|
|
27
|
+
return result or user
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _compact_dict(data: dict[str, Any]) -> dict[str, Any]:
|
|
31
|
+
result: dict[str, Any] = {}
|
|
32
|
+
for key, value in data.items():
|
|
33
|
+
if key in STRIP_FIELDS:
|
|
34
|
+
continue
|
|
35
|
+
if key == "avatar_url" and _is_base64_avatar(value):
|
|
36
|
+
continue
|
|
37
|
+
if key == "avatar" and _is_base64_avatar(value):
|
|
38
|
+
continue
|
|
39
|
+
if key in SIMPLIFY_FIELDS and isinstance(value, dict):
|
|
40
|
+
result[key] = _simplify_user(value)
|
|
41
|
+
elif key in SIMPLIFY_LIST_FIELDS and isinstance(value, list):
|
|
42
|
+
result[key] = [
|
|
43
|
+
_simplify_user(item) if isinstance(item, dict) else item for item in value
|
|
44
|
+
]
|
|
45
|
+
elif isinstance(value, dict):
|
|
46
|
+
result[key] = _compact_dict(value)
|
|
47
|
+
elif isinstance(value, list):
|
|
48
|
+
result[key] = _compact_list(value)
|
|
49
|
+
else:
|
|
50
|
+
result[key] = value
|
|
51
|
+
return result
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _compact_list(data: list[Any]) -> list[Any]:
|
|
55
|
+
result: list[Any] = []
|
|
56
|
+
for item in data:
|
|
57
|
+
if isinstance(item, dict):
|
|
58
|
+
result.append(_compact_dict(item))
|
|
59
|
+
elif isinstance(item, list):
|
|
60
|
+
result.append(_compact_list(item))
|
|
61
|
+
else:
|
|
62
|
+
result.append(item)
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def compact_response(data: Any, compact: bool = False) -> Any:
|
|
67
|
+
if not compact:
|
|
68
|
+
return data
|
|
69
|
+
if isinstance(data, dict):
|
|
70
|
+
return _compact_dict(data)
|
|
71
|
+
if isinstance(data, list):
|
|
72
|
+
return _compact_list(data)
|
|
73
|
+
return data
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _strip_b64_value(value: str) -> str:
|
|
77
|
+
size_kb = len(value) // 1024
|
|
78
|
+
return f"[base64 ~{size_kb}KB, omitted]"
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _strip_b64_dict(data: dict[str, Any], counter: list[int]) -> dict[str, Any]:
|
|
82
|
+
result: dict[str, Any] = {}
|
|
83
|
+
for key, value in data.items():
|
|
84
|
+
if isinstance(value, str) and value.startswith("data:"):
|
|
85
|
+
result[key] = _strip_b64_value(value)
|
|
86
|
+
counter[0] += 1
|
|
87
|
+
elif isinstance(value, dict):
|
|
88
|
+
result[key] = _strip_b64_dict(value, counter)
|
|
89
|
+
elif isinstance(value, list):
|
|
90
|
+
result[key] = _strip_b64_list(value, counter)
|
|
91
|
+
else:
|
|
92
|
+
result[key] = value
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _strip_b64_list(data: list[Any], counter: list[int]) -> list[Any]:
|
|
97
|
+
result: list[Any] = []
|
|
98
|
+
for item in data:
|
|
99
|
+
if isinstance(item, dict):
|
|
100
|
+
result.append(_strip_b64_dict(item, counter))
|
|
101
|
+
elif isinstance(item, list):
|
|
102
|
+
result.append(_strip_b64_list(item, counter))
|
|
103
|
+
else:
|
|
104
|
+
result.append(item)
|
|
105
|
+
return result
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def strip_base64(data: Any) -> tuple[Any, int]:
|
|
109
|
+
counter = [0]
|
|
110
|
+
if isinstance(data, dict):
|
|
111
|
+
return _strip_b64_dict(data, counter), counter[0]
|
|
112
|
+
if isinstance(data, list):
|
|
113
|
+
return _strip_b64_list(data, counter), counter[0]
|
|
114
|
+
return data, 0
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def select_fields(data: Any, fields_str: str | None) -> Any:
|
|
118
|
+
if not fields_str:
|
|
119
|
+
return data
|
|
120
|
+
|
|
121
|
+
keys = {field.strip() for field in fields_str.split(",") if field.strip()}
|
|
122
|
+
if isinstance(data, list):
|
|
123
|
+
return [
|
|
124
|
+
{k: v for k, v in item.items() if k in keys} for item in data if isinstance(item, dict)
|
|
125
|
+
]
|
|
126
|
+
if isinstance(data, dict):
|
|
127
|
+
return {k: v for k, v in data.items() if k in keys}
|
|
128
|
+
return data
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Shared nested schema traversal for terminal and generated documentation."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def schema_rows(schema, path="", required=False):
|
|
7
|
+
if path:
|
|
8
|
+
yield path, schema, required
|
|
9
|
+
for name, child in schema.get("properties", {}).items():
|
|
10
|
+
yield from schema_rows(
|
|
11
|
+
child, f"{path}.{name}" if path else name, name in schema.get("required", [])
|
|
12
|
+
)
|
|
13
|
+
if isinstance(schema.get("items"), dict):
|
|
14
|
+
child = schema["items"]
|
|
15
|
+
if child.get("properties") or child.get("x-variants"):
|
|
16
|
+
yield from schema_rows(child, path + "[]")
|
|
17
|
+
for pattern, child in schema.get("patternProperties", {}).items():
|
|
18
|
+
yield from schema_rows(child, f"{path}[key matching {pattern}]")
|
|
19
|
+
for variant, child in schema.get("x-variants", {}).items():
|
|
20
|
+
yield from schema_rows(child, f"{path}<{variant}>")
|
|
21
|
+
for index, child in enumerate(schema.get("oneOf", [])):
|
|
22
|
+
yield from schema_rows(child, f"{path}<oneOf[{index}]>")
|