arga-clone 0.2.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.
- arga_clone-0.2.0.dist-info/METADATA +2570 -0
- arga_clone-0.2.0.dist-info/RECORD +32 -0
- arga_clone-0.2.0.dist-info/WHEEL +5 -0
- arga_clone-0.2.0.dist-info/entry_points.txt +2 -0
- arga_clone-0.2.0.dist-info/licenses/LICENSE +21 -0
- arga_clone-0.2.0.dist-info/top_level.txt +1 -0
- argaclone/__init__.py +13 -0
- argaclone/chaos.py +280 -0
- argaclone/cli.py +229 -0
- argaclone/client.py +227 -0
- argaclone/gateway.py +323 -0
- argaclone/graders.py +244 -0
- argaclone/mcp_server.py +237 -0
- argaclone/runner.py +383 -0
- argaclone/scenarios.py +443 -0
- argaclone/server.py +557 -0
- argaclone/store.py +461 -0
- argaclone/twin_spec.py +384 -0
- argaclone/twins/__init__.py +15 -0
- argaclone/twins/base.py +436 -0
- argaclone/twins/calendar_twin.py +331 -0
- argaclone/twins/drive_twin.py +334 -0
- argaclone/twins/github_twin.py +2086 -0
- argaclone/twins/gmail_twin.py +413 -0
- argaclone/twins/hubspot_twin.py +212 -0
- argaclone/twins/jira_twin.py +265 -0
- argaclone/twins/linear_twin.py +480 -0
- argaclone/twins/notion_twin.py +415 -0
- argaclone/twins/salesforce_twin.py +456 -0
- argaclone/twins/slack_twin.py +573 -0
- argaclone/twins/stripe_twin.py +1013 -0
- argaclone/webhooks.py +209 -0
argaclone/runner.py
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
"""动作执行器:把一串动作打进 twin,并记录一份可判分的 trace。
|
|
2
|
+
|
|
3
|
+
这里最容易被写错的一点:**不同 provider 的"成功"语义不一样**。
|
|
4
|
+
Slack 失败也回 HTTP 200(看 body 的 ok 字段),Stripe 用状态码。
|
|
5
|
+
如果 runner 只看状态码,Slack 的所有失败都会在 trace 里记成成功,
|
|
6
|
+
下游 grader 就永远看不到真正的错误——这是最典型的一种"假绿"。
|
|
7
|
+
|
|
8
|
+
所以成功判定必须按 provider 分派,且这件事要写在这里而不是交给调用方。
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import hashlib
|
|
14
|
+
import json
|
|
15
|
+
import random
|
|
16
|
+
import time
|
|
17
|
+
from dataclasses import dataclass, field
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from .gateway import Gateway
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class Action:
|
|
25
|
+
provider: str
|
|
26
|
+
method: str
|
|
27
|
+
path: str
|
|
28
|
+
body: dict[str, Any] = field(default_factory=dict)
|
|
29
|
+
headers: dict[str, str] = field(default_factory=dict)
|
|
30
|
+
query: str = ""
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def from_dict(d: dict[str, Any]) -> "Action":
|
|
34
|
+
return Action(
|
|
35
|
+
provider=d["provider"],
|
|
36
|
+
method=d.get("method", "GET").upper(),
|
|
37
|
+
path=d["path"],
|
|
38
|
+
body=d.get("body") or {},
|
|
39
|
+
headers=d.get("headers") or {},
|
|
40
|
+
query=d.get("query") or "",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def is_success(provider: str, status: int, payload: Any) -> bool:
|
|
45
|
+
"""provider 感知的成功判定。"""
|
|
46
|
+
if provider == "slack":
|
|
47
|
+
# Slack:永远 200,成功看 body.ok
|
|
48
|
+
return status == 200 and isinstance(payload, dict) and payload.get("ok") is True
|
|
49
|
+
# Stripe 一类:状态码 + 没有 error 信封
|
|
50
|
+
if status >= 400:
|
|
51
|
+
return False
|
|
52
|
+
return not (isinstance(payload, dict) and "error" in payload and len(payload) == 1)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def empty_trace() -> dict[str, Any]:
|
|
56
|
+
"""没跑过任何动作时的 trace。
|
|
57
|
+
|
|
58
|
+
为什么需要显式的空 trace 而不是 None:判据里大量使用 `trace.bodies`、
|
|
59
|
+
`trace.path_sequence` 这类路径,如果把它们解析成 PATH_ERROR,
|
|
60
|
+
"什么都没做"就会被当成"判据坏了",两种缺陷混在一起分不开。
|
|
61
|
+
空 trace 表达的是真实语义——没有发生过调用,所以没有写请求、没有路径。
|
|
62
|
+
"""
|
|
63
|
+
return {
|
|
64
|
+
"calls": [],
|
|
65
|
+
"count": 0,
|
|
66
|
+
"providers": [],
|
|
67
|
+
"stub_calls": 0,
|
|
68
|
+
"absent_calls": 0,
|
|
69
|
+
"error_calls": 0,
|
|
70
|
+
"client_retries": 0,
|
|
71
|
+
"path_sequence": [],
|
|
72
|
+
"bodies": [],
|
|
73
|
+
"reads": [],
|
|
74
|
+
"writes": [],
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
# 可重试的传输级失败。429 必须在内:限流是"稍后再来",不是"你错了"。
|
|
79
|
+
# 402/400/404 这些是语义拒绝,重发只会拿到同一个答案 —— 排除它们是这条判据的价值所在。
|
|
80
|
+
RETRYABLE_STATUS = frozenset({429, 500, 502, 503, 504})
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
# Slack 把**所有**方法都做成 POST —— 读聊天记录也是 POST。
|
|
84
|
+
# 所以"method != GET 就是写"这条判据在 Slack 上会把每一次读取都记成写,
|
|
85
|
+
# 于是"只发一次公告"被判成刷屏、"回帖确认"被判成多余写。
|
|
86
|
+
# 这是真实踩到的假阴性:正确解被判 UNSAFE。
|
|
87
|
+
SLACK_READ_METHODS = (
|
|
88
|
+
"conversations.list",
|
|
89
|
+
"conversations.history",
|
|
90
|
+
"conversations.members",
|
|
91
|
+
"conversations.info",
|
|
92
|
+
"conversations.replies",
|
|
93
|
+
"users.list",
|
|
94
|
+
"users.info",
|
|
95
|
+
"auth.test",
|
|
96
|
+
"team.info",
|
|
97
|
+
"files.list",
|
|
98
|
+
"files.info",
|
|
99
|
+
"search.messages",
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _is_write(provider: str, method: str, path: str) -> bool:
|
|
104
|
+
"""这次调用是否真的改变了状态。
|
|
105
|
+
|
|
106
|
+
为什么不用 `method != GET`:Slack 的读接口也是 POST(见上)。
|
|
107
|
+
Stripe 与 GitHub 的读接口是 GET,所以那条判据对它们成立,对 Slack 不成立。
|
|
108
|
+
"""
|
|
109
|
+
if method.upper() == "GET":
|
|
110
|
+
return False
|
|
111
|
+
if provider == "slack":
|
|
112
|
+
return not any(m in path for m in SLACK_READ_METHODS)
|
|
113
|
+
return True
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class Runner:
|
|
117
|
+
"""直接在进程内打 gateway。
|
|
118
|
+
|
|
119
|
+
不走真实 socket 是刻意的:动作序列会被重放几千次,socket 只会拖慢而不会
|
|
120
|
+
让语义更真——真不真取决于 twin 的实现,不取决于有没有过网线。
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def __init__(
|
|
124
|
+
self,
|
|
125
|
+
gateway: Gateway,
|
|
126
|
+
run_id: str,
|
|
127
|
+
idempotent_writes: bool = False,
|
|
128
|
+
client_retries: int = 0,
|
|
129
|
+
retry_backoff: float = 0.0,
|
|
130
|
+
retry_backoff_cap: float = 2.0,
|
|
131
|
+
jitter: float = 0.5,
|
|
132
|
+
sleep=time.sleep,
|
|
133
|
+
) -> None:
|
|
134
|
+
self.gateway = gateway
|
|
135
|
+
self.run_id = run_id
|
|
136
|
+
self.calls: list[dict[str, Any]] = []
|
|
137
|
+
# 模拟"会正确使用幂等键的 SDK"。注意这是**内容寻址**的近似:
|
|
138
|
+
# 真实 SDK 在一次逻辑操作内部复用同一把钥匙,而这里拿不到"逻辑操作"
|
|
139
|
+
# 这个概念,只能用 (provider, method, path, body) 派生。所以它证明的是
|
|
140
|
+
# "服务端支持幂等键之后重复写入会消失",不是"模型知道该用幂等键"。
|
|
141
|
+
self.idempotent_writes = idempotent_writes
|
|
142
|
+
# 客户端重试层:模拟真实 SDK 在"一次逻辑操作"内部的传输级重试。
|
|
143
|
+
#
|
|
144
|
+
# 为什么需要它:`commit_then_fail` 之后,模型看到 500 会重试 —— 这是对的行为。
|
|
145
|
+
# 但模型的重试是**重新组织一次请求**(措辞、字段都可能变),而真实 SDK 重试的是
|
|
146
|
+
# 同一个已经序列化好的请求,钥匙和 body 都不变。两者在服务端看起来完全不同:
|
|
147
|
+
# 前者是一笔新操作(Stripe 也会写两次),后者才是幂等键能挡的那一种。
|
|
148
|
+
#
|
|
149
|
+
# 所以这个参数量的不是"模型聪不聪明",而是"**客户端建对了之后,这个环境能不能
|
|
150
|
+
# 给出恰好一次**"。0 = 现造现报(模型自己重试),>0 = 假装客户端有重试层。
|
|
151
|
+
self.client_retries = max(0, int(client_retries))
|
|
152
|
+
# 退避:真实 SDK 不会连着轰。`retry_backoff` 是指数基数(秒),第 n 次重试
|
|
153
|
+
# 等 `base * 2**(n-1)`,封顶 `cap`;`jitter` 是"全抖动"(full jitter)的幅度,
|
|
154
|
+
# 实际等待 = 上限内均匀随机 —— 固定退避会让一批客户端在同一个毫秒醒来,
|
|
155
|
+
# 把一次 5xx 变成一次自造的惊群。
|
|
156
|
+
# 0 表示不退避(老行为,测试里用它保持快)。
|
|
157
|
+
self.retry_backoff = max(0.0, float(retry_backoff))
|
|
158
|
+
self.retry_backoff_cap = max(0.0, float(retry_backoff_cap))
|
|
159
|
+
self.jitter = min(1.0, max(0.0, float(jitter)))
|
|
160
|
+
# 注入 sleep 是为了让"等了多少"可测:真睡会让一排重试测试变成秒级
|
|
161
|
+
self._sleep = sleep
|
|
162
|
+
|
|
163
|
+
def _wait_before_retry(self, attempt: int, headers: dict[str, str]) -> float:
|
|
164
|
+
"""这一次重试前该等多久。
|
|
165
|
+
|
|
166
|
+
两条规则,顺序有讲究:
|
|
167
|
+
1. **服务端说等多久就等多久**(`Retry-After`,秒数;HTTP-date 形式不解析,
|
|
168
|
+
直接忽略退回指数退避 —— 解析半个格式比不解析更危险)。
|
|
169
|
+
2. 否则指数退避 + 全抖动。
|
|
170
|
+
|
|
171
|
+
`Retry-After` 优先的原因:限流窗口是服务端定的,客户端自己猜一个更短的
|
|
172
|
+
间隔只会把 429 变成更多 429。
|
|
173
|
+
"""
|
|
174
|
+
# 退避关掉 = 完全没有节奏纪律(也不看 Retry-After)。这条分界要显式,
|
|
175
|
+
# 否则"默认不退避"的评测会在 134 次重试里真睡几分钟,而读数一点没变。
|
|
176
|
+
if self.retry_backoff <= 0:
|
|
177
|
+
return 0.0
|
|
178
|
+
raw = headers.get("Retry-After") or headers.get("retry-after")
|
|
179
|
+
if raw:
|
|
180
|
+
try:
|
|
181
|
+
secs = float(str(raw).strip())
|
|
182
|
+
except ValueError:
|
|
183
|
+
secs = None
|
|
184
|
+
if secs is not None and secs >= 0:
|
|
185
|
+
# **不拿自家 cap 去截服务端给的 `Retry-After`**。
|
|
186
|
+
# cap 管的是我们自己猜的那条指数曲线;服务端说的是事实。
|
|
187
|
+
# 实测代价:罚站 `Retry-After: 20` 被默认 cap=2.0 截成 2s,
|
|
188
|
+
# 于是"尊重 Retry-After"的客户端 2 秒后就回来继续撞 —— 看起来像
|
|
189
|
+
# 它没退避,其实是我把它的等待改小了。
|
|
190
|
+
return secs
|
|
191
|
+
ceiling = min(self.retry_backoff * (2 ** (attempt - 1)), self.retry_backoff_cap)
|
|
192
|
+
if self.jitter <= 0:
|
|
193
|
+
return ceiling
|
|
194
|
+
# full jitter:在 [ceiling*(1-jitter), ceiling] 之间均匀取值。
|
|
195
|
+
# 取上界为 ceiling 而不是"ceiling 到 2*ceiling",是为了让封顶仍然是硬上界。
|
|
196
|
+
low = ceiling * (1.0 - self.jitter)
|
|
197
|
+
return round(random.uniform(low, ceiling), 4)
|
|
198
|
+
|
|
199
|
+
def _host(self, provider: str) -> str:
|
|
200
|
+
return f"{provider}.{self.run_id}.twins.local"
|
|
201
|
+
|
|
202
|
+
def call(self, action: Action) -> dict[str, Any]:
|
|
203
|
+
raw_path = action.path + (("?" + action.query) if action.query else "")
|
|
204
|
+
body = json.dumps(action.body).encode() if action.body else b""
|
|
205
|
+
headers = {k.lower(): v for k, v in action.headers.items()}
|
|
206
|
+
headers.setdefault("content-type", "application/json")
|
|
207
|
+
if self.idempotent_writes and action.method.upper() not in ("GET", "HEAD"):
|
|
208
|
+
seed = f"{action.provider}\n{action.method.upper()}\n{action.path}\n{action.query}\n"
|
|
209
|
+
headers.setdefault(
|
|
210
|
+
"idempotency-key",
|
|
211
|
+
hashlib.sha256((seed + body.decode("utf-8", "replace")).encode()).hexdigest()[:32],
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
# 客户端重试层:只重试**传输级失败**(5xx / 响应没落地),不重试 4xx。
|
|
215
|
+
# 4xx 是语义拒绝,重发一遍只会得到同一个 4xx —— 那是日志噪声,不是韧性。
|
|
216
|
+
attempts = 0
|
|
217
|
+
retry_waits: list[float] = []
|
|
218
|
+
is_write_call = _is_write(action.provider, action.method.upper(), action.path)
|
|
219
|
+
while True:
|
|
220
|
+
resp = self.gateway.handle(
|
|
221
|
+
action.method, self._host(action.provider), raw_path, headers, body
|
|
222
|
+
)
|
|
223
|
+
payload = resp.body
|
|
224
|
+
ok = is_success(action.provider, resp.status, payload)
|
|
225
|
+
# 注意这里**不要求** idempotent_writes:不带钥匙的客户端也会重试,
|
|
226
|
+
# 而那正是重复扣款的来源。把"重试"绑在"有钥匙"上,等于让对照组消失。
|
|
227
|
+
# 读也要重试 429:限流不是"你问错了",是"稍后再问",而重发一次读
|
|
228
|
+
# 不改变世界。5xx 的读则**不**重试 —— 那一路是要让模型自己决定
|
|
229
|
+
# "是重试还是换个办法",替它决定会把 §23 那批韧性读数抹平。
|
|
230
|
+
read_throttled = (not is_write_call) and resp.status == 429
|
|
231
|
+
if (
|
|
232
|
+
(not is_write_call and not read_throttled)
|
|
233
|
+
or ok
|
|
234
|
+
or resp.status not in RETRYABLE_STATUS
|
|
235
|
+
or attempts >= self.client_retries
|
|
236
|
+
):
|
|
237
|
+
break
|
|
238
|
+
attempts += 1
|
|
239
|
+
waited = self._wait_before_retry(attempts, resp.headers)
|
|
240
|
+
retry_waits.append(waited)
|
|
241
|
+
if waited:
|
|
242
|
+
self._sleep(waited)
|
|
243
|
+
|
|
244
|
+
record = {
|
|
245
|
+
"n": len(self.calls) + 1,
|
|
246
|
+
"provider": action.provider,
|
|
247
|
+
"method": action.method,
|
|
248
|
+
"path": action.path,
|
|
249
|
+
"status": resp.status,
|
|
250
|
+
"ok": ok,
|
|
251
|
+
"stub": resp.headers.get("X-Twin-Stub") == "true",
|
|
252
|
+
# 声明的边界与欠账在结果文件里也要能分开(否则离线报表只能
|
|
253
|
+
# 拿到总数,再想拆就得重跑)
|
|
254
|
+
"declared": resp.headers.get("X-Twin-Declared") == "true",
|
|
255
|
+
# 第三态:真实 provider 里不存在的端点。它既不是欠账(不该算进覆盖率
|
|
256
|
+
# 分母),也不能算成功 —— agent 编出来的路径被 404 掉是正确行为。
|
|
257
|
+
"absent": resp.headers.get("X-Twin-Absent") == "true",
|
|
258
|
+
# 幂等重放:请求被网关挡回去了,世界没有第二次变动。判"重复写入"时
|
|
259
|
+
# 必须排除它 —— 否则"重试但安全"会被算成"重试且写了两遍",
|
|
260
|
+
# 正好把这条能力买到的效果抹掉。
|
|
261
|
+
"replayed": resp.headers.get("Idempotent-Replayed") == "true",
|
|
262
|
+
# 客户端替它重试了几次。与 replayed 分开记:一次 500 之后重试成功,
|
|
263
|
+
# 若中间那次是同一个钥匙,服务端只写了 1 遍;若钥匙变了(模型改写),
|
|
264
|
+
# 就会写 2 遍 —— 这两件事在报表上必须能分开,否则"重试"这个词
|
|
265
|
+
# 既可能是药的功劳,也可能是病的来源。
|
|
266
|
+
"client_retries": attempts,
|
|
267
|
+
# 每次重试前实际等了多久(秒)。记下来才判得开"它真的退避了"和
|
|
268
|
+
# "它只是碰巧第二次成功" —— 后者在快环境里看起来一模一样。
|
|
269
|
+
"retry_waits": retry_waits,
|
|
270
|
+
"error": None if ok else self._error_of(payload),
|
|
271
|
+
"request": action.body or None,
|
|
272
|
+
# 响应体必须留在 record 里。没有它,agent 看到的是"状态码 200、
|
|
273
|
+
# 内容空",只能靠猜下一步 —— 那测出来的是猜运,不是能力。
|
|
274
|
+
# 判据也可能要看响应(比如"返回里必须带 invoice id")。
|
|
275
|
+
# 裸字节响应(GitHub raw 文件)要转成 str:trace 会被 json.dumps
|
|
276
|
+
# 落到结果文件里,bytes 会在那一刻抛 TypeError,而那时已经跑完
|
|
277
|
+
# 所有 episode —— 一个序列化细节就能让整轮结果全废。
|
|
278
|
+
"response": payload.decode("utf-8", "replace") if isinstance(payload, bytes) else payload,
|
|
279
|
+
}
|
|
280
|
+
self.calls.append(record)
|
|
281
|
+
return record
|
|
282
|
+
|
|
283
|
+
@staticmethod
|
|
284
|
+
def _error_of(payload: Any) -> Any:
|
|
285
|
+
if isinstance(payload, dict):
|
|
286
|
+
if "error" in payload:
|
|
287
|
+
return payload["error"]
|
|
288
|
+
if payload.get("ok") is False:
|
|
289
|
+
return {k: v for k, v in payload.items() if k != "ok"}
|
|
290
|
+
return str(payload)[:200]
|
|
291
|
+
|
|
292
|
+
def _normalize_write(self, provider: str, body: Any) -> Any:
|
|
293
|
+
"""把写请求里的 Slack channel 名归一成 channel id。
|
|
294
|
+
|
|
295
|
+
为什么必须做:Slack 接受 `C_SALES_OPS` 与 `#sales-ops` 两种写法,
|
|
296
|
+
而判据只能写一个字符串。不归一的话,"发对了地方但用了名字"会被判成
|
|
297
|
+
没发 —— 判据在考格式而不是考事,pass 率就不可信了。
|
|
298
|
+
归一放在 trace 层而不是 twin 层:twin 必须保留客户端的原始写法
|
|
299
|
+
(那是保真的一部分),判据看到的才需要一个稳定形态。
|
|
300
|
+
"""
|
|
301
|
+
if provider != "slack" or not isinstance(body, dict):
|
|
302
|
+
return body
|
|
303
|
+
channel = body.get("channel")
|
|
304
|
+
if not isinstance(channel, str):
|
|
305
|
+
return body
|
|
306
|
+
try:
|
|
307
|
+
state = self.gateway.store.get(self.run_id, allow_expired=True).state["slack"]
|
|
308
|
+
except Exception: # noqa: BLE001 - 取不到就原样返回,判据自己会表现为没命中
|
|
309
|
+
return body
|
|
310
|
+
channels = state.get("channels") or {}
|
|
311
|
+
resolved = channel if channel in channels else None
|
|
312
|
+
if resolved is None:
|
|
313
|
+
name = channel[1:] if channel.startswith("#") else channel
|
|
314
|
+
for cid, ch in channels.items():
|
|
315
|
+
if ch.get("name") == name:
|
|
316
|
+
resolved = cid
|
|
317
|
+
break
|
|
318
|
+
if resolved is None or resolved == channel:
|
|
319
|
+
return body
|
|
320
|
+
out = dict(body)
|
|
321
|
+
out["channel"] = resolved
|
|
322
|
+
return out
|
|
323
|
+
|
|
324
|
+
def run(self, actions: list[Action | dict[str, Any]]) -> dict[str, Any]:
|
|
325
|
+
for a in actions:
|
|
326
|
+
self.call(a if isinstance(a, Action) else Action.from_dict(a))
|
|
327
|
+
return self.trace()
|
|
328
|
+
|
|
329
|
+
def trace(self) -> dict[str, Any]:
|
|
330
|
+
writes = [c for c in self.calls if _is_write(c["provider"], c["method"], c["path"])]
|
|
331
|
+
# 读与写必须分开数。合起来的 `trace.calls.count` 分不清两种形状完全不同的
|
|
332
|
+
# 浪费:一种是"读了八遍现状才动手",一种是"该写三处只写了两处"。
|
|
333
|
+
# 前者多数是正当取证(题面常要求先读),后者一律是没做完 —— 用同一个数字
|
|
334
|
+
# 管它们,等于让判据在两种缺陷之间二选一,只能选一个。
|
|
335
|
+
# 判定读/写用 `_is_write` 而不是 `method != "GET"`:Slack 的读接口也是 POST。
|
|
336
|
+
reads = [c for c in self.calls if not _is_write(c["provider"], c["method"], c["path"])]
|
|
337
|
+
return {
|
|
338
|
+
"calls": self.calls,
|
|
339
|
+
"count": len(self.calls),
|
|
340
|
+
"providers": sorted({c["provider"] for c in self.calls}),
|
|
341
|
+
"stub_calls": sum(1 for c in self.calls if c["stub"]),
|
|
342
|
+
"absent_calls": sum(1 for c in self.calls if c.get("absent")),
|
|
343
|
+
"error_calls": sum(1 for c in self.calls if not c["ok"]),
|
|
344
|
+
"client_retries": sum(c.get("client_retries", 0) for c in self.calls),
|
|
345
|
+
"path_sequence": [f"{c['provider']}:{c['method']} {c['path']}" for c in self.calls],
|
|
346
|
+
# 写请求的 body 被序列化成字符串列表。为什么需要这个:
|
|
347
|
+
# 只看 path_sequence 无法区分"发到了 #marketing-ops"还是"发到了 #announcements",
|
|
348
|
+
# 而"发错频道"恰恰是最常见也最危险的 agent 失误。
|
|
349
|
+
# 用字符串列表 + any_contains/none_contains 断言,既确定又不引入查询语言。
|
|
350
|
+
"bodies": [
|
|
351
|
+
json.dumps(self._normalize_write(c["provider"], c["request"]), ensure_ascii=False)
|
|
352
|
+
for c in writes
|
|
353
|
+
if c["request"]
|
|
354
|
+
],
|
|
355
|
+
# `trace.reads.count` / `trace.writes.count` 就落在这两个列表上。
|
|
356
|
+
# 它们是**真实列表**而不是现算的整数:`count` 是终结符,
|
|
357
|
+
# 作用在 list 上就是长度,语义与 `trace.calls.count` 完全一致,
|
|
358
|
+
# 不需要为它单开一个会静默取错值的特例分支。
|
|
359
|
+
"reads": [
|
|
360
|
+
{
|
|
361
|
+
"provider": c["provider"],
|
|
362
|
+
"method": c["method"],
|
|
363
|
+
"path": c["path"],
|
|
364
|
+
"status": c.get("status"),
|
|
365
|
+
}
|
|
366
|
+
for c in reads
|
|
367
|
+
],
|
|
368
|
+
"writes": [
|
|
369
|
+
{
|
|
370
|
+
"provider": c["provider"],
|
|
371
|
+
"method": c["method"],
|
|
372
|
+
"path": c["path"],
|
|
373
|
+
"body": self._normalize_write(c["provider"], c["request"]),
|
|
374
|
+
# 重放标记要跟着写记录走。否则 trace 里两个 200 长得一样,
|
|
375
|
+
# 而一个是"世界变了两次"、一个"世界只变了一次"。
|
|
376
|
+
"replayed": c.get("replayed", False),
|
|
377
|
+
# 客户端在这个动作内部替调用方重试了几次。它和 replayed 一起
|
|
378
|
+
# 才说明"恰好一次"是怎么来的:重试提供结果,钥匙提供唯一性。
|
|
379
|
+
"client_retries": c.get("client_retries", 0),
|
|
380
|
+
}
|
|
381
|
+
for c in writes
|
|
382
|
+
],
|
|
383
|
+
}
|