forked from Alishahryar1/free-claude-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_replay_envelope.py
More file actions
285 lines (253 loc) · 8.42 KB
/
Copy pathtest_replay_envelope.py
File metadata and controls
285 lines (253 loc) · 8.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import base64
import json
from collections.abc import Mapping
import pytest
from free_claude_code.core.inference import (
ReplayArtifact,
ReplayArtifactKind,
ReplayArtifactOrigin,
ReplayAttachment,
ReplayCompatibilityScope,
)
from free_claude_code.core.replay_envelope import (
REPLAY_ENVELOPE_PREFIX,
ReplayEnvelopeError,
decode_replay_envelope,
encode_replay_envelope,
)
def _carrier(payload: object) -> str:
raw = json.dumps(payload, separators=(",", ":")).encode()
token = base64.urlsafe_b64encode(raw).decode().rstrip("=")
return f"{REPLAY_ENVELOPE_PREFIX}{token}"
def _artifact_payload(
*,
origin: str = "openai",
kind: str = "encrypted_reasoning",
attachment: str = "reasoning",
scope: object = "openai_responses:model",
payload: object = "opaque",
) -> dict[str, object]:
return {
"origin": origin,
"kind": kind,
"attachment": attachment,
"scope": scope,
"payload": payload,
}
@pytest.mark.parametrize(
("kind", "attachment"),
[
(ReplayArtifactKind.THINKING_SIGNATURE, ReplayAttachment.REASONING),
(ReplayArtifactKind.REDACTED_THINKING, ReplayAttachment.REASONING),
(ReplayArtifactKind.ENCRYPTED_REASONING, ReplayAttachment.REASONING),
(ReplayArtifactKind.REASONING_DETAILS, ReplayAttachment.REASONING),
(ReplayArtifactKind.THOUGHT_SIGNATURE, ReplayAttachment.TOOL_CALL),
(ReplayArtifactKind.TOOL_EXTRA_CONTENT, ReplayAttachment.TOOL_CALL),
],
)
def test_each_replay_artifact_kind_round_trips(
kind: ReplayArtifactKind,
attachment: ReplayAttachment,
) -> None:
artifact = ReplayArtifact(
origin=ReplayArtifactOrigin.OPENAI_COMPATIBLE,
kind=kind,
attachment=attachment,
scope=ReplayCompatibilityScope("provider:model"),
payload={"opaque": [kind.value, 1, True, None]},
)
assert decode_replay_envelope(
encode_replay_envelope((artifact,)), attachment=attachment
) == (artifact,)
@pytest.mark.parametrize(
"attachment",
[ReplayAttachment.REASONING, ReplayAttachment.TOOL_CALL],
)
def test_multiple_artifacts_keep_order_and_encode_deterministically(
attachment: ReplayAttachment,
) -> None:
origins = tuple(ReplayArtifactOrigin)
kinds = (
(
ReplayArtifactKind.THINKING_SIGNATURE,
ReplayArtifactKind.REDACTED_THINKING,
ReplayArtifactKind.ENCRYPTED_REASONING,
ReplayArtifactKind.REASONING_DETAILS,
)
if attachment is ReplayAttachment.REASONING
else (
ReplayArtifactKind.THOUGHT_SIGNATURE,
ReplayArtifactKind.TOOL_EXTRA_CONTENT,
)
)
artifacts = tuple(
ReplayArtifact(
origin=origins[index % len(origins)],
kind=kind,
attachment=attachment,
scope=(ReplayCompatibilityScope("provider:model") if index else None),
payload={"index": index},
)
for index, kind in enumerate(kinds)
)
first = encode_replay_envelope(artifacts)
second = encode_replay_envelope(artifacts)
assert first == second
assert decode_replay_envelope(first, attachment=attachment) == artifacts
def test_legacy_raw_replay_value_is_not_claimed_by_the_envelope_codec() -> None:
assert (
decode_replay_envelope(
"provider-owned-opaque-value",
attachment=ReplayAttachment.REASONING,
)
is None
)
def test_empty_artifact_sequence_cannot_be_encoded() -> None:
with pytest.raises(ValueError, match="at least one artifact"):
encode_replay_envelope(())
@pytest.mark.parametrize(
("value", "match"),
[
(REPLAY_ENVELOPE_PREFIX, "payload is empty"),
(f"{REPLAY_ENVELOPE_PREFIX}*", "valid base64url"),
(
f"{REPLAY_ENVELOPE_PREFIX}"
+ base64.urlsafe_b64encode(b"\xff").decode().rstrip("="),
"valid UTF-8 JSON",
),
(
f"{REPLAY_ENVELOPE_PREFIX}"
+ base64.urlsafe_b64encode(b"{").decode().rstrip("="),
"valid UTF-8 JSON",
),
(_carrier([]), "top-level schema"),
(_carrier({"version": 1}), "top-level schema"),
(_carrier({"version": 2, "artifacts": []}), "version is unsupported"),
(
_carrier({"version": 1, "artifacts": []}),
"artifacts must be a non-empty list",
),
(
_carrier({"version": 1, "artifacts": ["opaque"]}),
"artifacts\\[0\\] has an invalid schema",
),
(
_carrier(
{
"version": 1,
"artifacts": [{**_artifact_payload(), "extra": True}],
}
),
"artifacts\\[0\\] has an invalid schema",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(origin="unknown")],
}
),
"unknown artifact value",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(kind="unknown")],
}
),
"unknown artifact value",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(attachment="unknown")],
}
),
"unknown artifact value",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(scope="")],
}
),
"scope must be null or a non-empty string",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(scope=42)],
}
),
"scope must be null or a non-empty string",
),
(
_carrier(
{
"version": 1,
"artifacts": [_artifact_payload(payload=float("nan"))],
}
),
"payload is not valid JSON",
),
],
)
def test_malformed_envelopes_fail_with_stable_safe_errors(
value: str,
match: str,
) -> None:
with pytest.raises(ReplayEnvelopeError, match=match):
decode_replay_envelope(value, attachment=ReplayAttachment.REASONING)
def test_carrier_attachment_must_match_every_artifact() -> None:
value = _carrier(
{
"version": 1,
"artifacts": [_artifact_payload(attachment="tool_call")],
}
)
with pytest.raises(ReplayEnvelopeError, match="does not match its carrier"):
decode_replay_envelope(value, attachment=ReplayAttachment.REASONING)
def test_encode_rejects_non_finite_json_numbers() -> None:
artifact = ReplayArtifact(
origin=ReplayArtifactOrigin.OPENAI,
kind=ReplayArtifactKind.ENCRYPTED_REASONING,
attachment=ReplayAttachment.REASONING,
payload=float("inf"),
)
with pytest.raises(ValueError, match="Out of range float values"):
encode_replay_envelope((artifact,))
def test_encoded_and_decoded_size_limits_are_enforced() -> None:
artifact = ReplayArtifact(
origin=ReplayArtifactOrigin.OPENAI,
kind=ReplayArtifactKind.ENCRYPTED_REASONING,
attachment=ReplayAttachment.REASONING,
payload="x" * 1_048_576,
)
with pytest.raises(ValueError, match="size limit"):
encode_replay_envelope((artifact,))
oversized = base64.urlsafe_b64encode(b"x" * 1_048_577).decode().rstrip("=")
with pytest.raises(ReplayEnvelopeError, match="size limit"):
decode_replay_envelope(
f"{REPLAY_ENVELOPE_PREFIX}{oversized}",
attachment=ReplayAttachment.REASONING,
)
def test_errors_and_logs_do_not_expose_replay_payload(
caplog: pytest.LogCaptureFixture,
) -> None:
secret = "private-replay-material"
raw_artifact = _artifact_payload(payload={"secret": secret})
assert isinstance(raw_artifact, Mapping)
value = _carrier(
{
"version": 1,
"artifacts": [{**raw_artifact, "unexpected": True}],
}
)
with pytest.raises(ReplayEnvelopeError) as captured:
decode_replay_envelope(value, attachment=ReplayAttachment.REASONING)
assert secret not in str(captured.value)
assert secret not in caplog.text