bitfab-py 0.78.2__tar.gz → 0.78.4__tar.gz
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.
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/PKG-INFO +1 -1
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/__init__.py +2 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/gmail_sandbox.py +234 -45
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay.py +1 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/sandbox.py +40 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/pyproject.toml +1 -1
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/README.md +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/agent_middleware.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/approval.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/assertion_categories.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/baml.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/baml_auto_trace.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/categories.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/claude_agent_sdk.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/client.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/client_patching.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/cloudReplay.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/cloud_replay_cli.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/commit_ref.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/compress.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/constants.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/datasets.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/db_snapshot.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/error_chain.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/experiment_id.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/experiments.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/finalizers.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/git_command.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/git_state.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/google_calls.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/graders.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/http.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/integration_fixture.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/jev.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/labels.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/langgraph.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/langgraph_integration.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/langgraph_runs.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/mock_matching.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/mock_override.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/openai_agent_sdk.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/openai_agents_usage.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/organization_members.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/otel.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/payload_budget.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/processor_payload.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/provider_calls.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_branch.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_cli.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_heartbeat.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_interrupt.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_invocation.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_memory.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_processes.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/replay_registry.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/seed_cli.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/selective_replay.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/serialize.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/service_calls.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/simulation_plan.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/span_meta.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/span_origin.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/spans.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/subtree.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/thread_propagation.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/timestamp.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/trace_completion.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/trace_metadata.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/trace_payload.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/traces.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/tracing.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/transport.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/transport_types.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/verdicts.py +0 -0
- {bitfab_py-0.78.2 → bitfab_py-0.78.4}/bitfab/warn_once.py +0 -0
|
@@ -155,6 +155,7 @@ from bitfab.sandbox import (
|
|
|
155
155
|
Sandbox,
|
|
156
156
|
SandboxStartCtx,
|
|
157
157
|
sandbox_active,
|
|
158
|
+
sandbox_now,
|
|
158
159
|
sandbox_state,
|
|
159
160
|
without_sandbox,
|
|
160
161
|
)
|
|
@@ -432,6 +433,7 @@ __all__ = [
|
|
|
432
433
|
"report_replay_progress",
|
|
433
434
|
"reseed_from_registry",
|
|
434
435
|
"sandbox_active",
|
|
436
|
+
"sandbox_now",
|
|
435
437
|
"sandbox_state",
|
|
436
438
|
"seed_from_registry",
|
|
437
439
|
"serialize_replay_result",
|
|
@@ -12,7 +12,12 @@ from typing import Any
|
|
|
12
12
|
|
|
13
13
|
from bitfab.google_calls import CREDENTIAL_PARAM, HTTP_ERROR_KEY, request_credentials
|
|
14
14
|
from bitfab.mock_override import MockOverrideCtx, SpanNodeMeta
|
|
15
|
-
from bitfab.sandbox import
|
|
15
|
+
from bitfab.sandbox import (
|
|
16
|
+
Sandbox,
|
|
17
|
+
SandboxStartCtx,
|
|
18
|
+
sandbox_elapsed_ms,
|
|
19
|
+
sandbox_state,
|
|
20
|
+
)
|
|
16
21
|
from bitfab.service_calls import ServiceCallHeldBackError
|
|
17
22
|
|
|
18
23
|
GMAIL_SANDBOX_NAME = "Gmail"
|
|
@@ -22,6 +27,9 @@ PROFILE_METHOD = "gmail.users.getProfile"
|
|
|
22
27
|
LABELS_LIST_METHOD = "gmail.users.labels.list"
|
|
23
28
|
LABEL_GET_METHOD = "gmail.users.labels.get"
|
|
24
29
|
SEND_AS_LIST_METHOD = "gmail.users.settings.sendAs.list"
|
|
30
|
+
DRAFT_GET_METHOD = "gmail.users.drafts.get"
|
|
31
|
+
DRAFT_LIST_METHOD = "gmail.users.drafts.list"
|
|
32
|
+
DRAFT_CREATE_METHOD = "gmail.users.drafts.create"
|
|
25
33
|
SYSTEM_LABELS = (
|
|
26
34
|
"INBOX",
|
|
27
35
|
"SENT",
|
|
@@ -167,6 +175,26 @@ def gmail_message(fields: dict[str, Any]) -> dict[str, Any]:
|
|
|
167
175
|
return message
|
|
168
176
|
|
|
169
177
|
|
|
178
|
+
def _fields_from_raw(raw: str | None) -> dict[str, Any]:
|
|
179
|
+
parsed = email.message_from_string(
|
|
180
|
+
_unb64(raw) if raw else "", policy=email.policy.default
|
|
181
|
+
)
|
|
182
|
+
body_part = (
|
|
183
|
+
parsed.get_body(preferencelist=("plain", "html"))
|
|
184
|
+
if parsed.is_multipart()
|
|
185
|
+
else parsed
|
|
186
|
+
)
|
|
187
|
+
body = body_part.get_content() if body_part is not None else ""
|
|
188
|
+
return {
|
|
189
|
+
**{
|
|
190
|
+
key: str(parsed.get(header))
|
|
191
|
+
for key, header in _PLAIN_HEADERS.items()
|
|
192
|
+
if parsed.get(header)
|
|
193
|
+
},
|
|
194
|
+
"body": str(body),
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
|
|
170
198
|
def _richer(
|
|
171
199
|
existing: dict[str, Any] | None, candidate: dict[str, Any]
|
|
172
200
|
) -> dict[str, Any]:
|
|
@@ -182,11 +210,19 @@ def mailbox_from_recorded_calls(ctx: SandboxStartCtx) -> dict[str, Any]:
|
|
|
182
210
|
|
|
183
211
|
Uses the state recorded with ``record_sandbox_state("Gmail", ...)`` when
|
|
184
212
|
there is one. Otherwise takes every message the original run read
|
|
185
|
-
(``gmail.users.messages.get`` replies)
|
|
186
|
-
``gmail.users.getProfile``,
|
|
213
|
+
(``gmail.users.messages.get`` replies), each account's address from
|
|
214
|
+
``gmail.users.getProfile``, and the drafts the run found
|
|
215
|
+
(``gmail.users.drafts.list`` and ``gmail.users.drafts.get`` replies, the
|
|
216
|
+
first read of each draft), one mailbox per credential the calls used.
|
|
217
|
+
Drafts the original run created are left out.
|
|
187
218
|
"""
|
|
188
219
|
if ctx.recorded_state is not None:
|
|
189
220
|
return as_mailboxes(ctx, ctx.recorded_state)
|
|
221
|
+
created = {
|
|
222
|
+
str(span.output.get("id"))
|
|
223
|
+
for span in ctx.recorded_spans(DRAFT_CREATE_METHOD)
|
|
224
|
+
if isinstance(span.output, dict) and span.output.get("id")
|
|
225
|
+
}
|
|
190
226
|
by_credential: dict[str | None, dict[str, Any]] = {}
|
|
191
227
|
for name in (
|
|
192
228
|
GET_METHOD,
|
|
@@ -195,6 +231,8 @@ def mailbox_from_recorded_calls(ctx: SandboxStartCtx) -> dict[str, Any]:
|
|
|
195
231
|
LABELS_LIST_METHOD,
|
|
196
232
|
LABEL_GET_METHOD,
|
|
197
233
|
SEND_AS_LIST_METHOD,
|
|
234
|
+
DRAFT_GET_METHOD,
|
|
235
|
+
DRAFT_LIST_METHOD,
|
|
198
236
|
):
|
|
199
237
|
for span in ctx.recorded_spans(name):
|
|
200
238
|
output = span.output
|
|
@@ -208,6 +246,7 @@ def mailbox_from_recorded_calls(ctx: SandboxStartCtx) -> dict[str, Any]:
|
|
|
208
246
|
"credential": credential,
|
|
209
247
|
"emailAddress": None,
|
|
210
248
|
"messages": {},
|
|
249
|
+
"drafts": {},
|
|
211
250
|
"searches": [],
|
|
212
251
|
"labels": {},
|
|
213
252
|
"sendAs": None,
|
|
@@ -233,11 +272,23 @@ def mailbox_from_recorded_calls(ctx: SandboxStartCtx) -> dict[str, Any]:
|
|
|
233
272
|
_keep_user_label(box["labels"], output)
|
|
234
273
|
if name == SEND_AS_LIST_METHOD and isinstance(output.get("sendAs"), list):
|
|
235
274
|
box["sendAs"] = output["sendAs"]
|
|
275
|
+
drafts = (
|
|
276
|
+
[output]
|
|
277
|
+
if name == DRAFT_GET_METHOD
|
|
278
|
+
else output.get("drafts") or []
|
|
279
|
+
if name == DRAFT_LIST_METHOD
|
|
280
|
+
else []
|
|
281
|
+
)
|
|
282
|
+
for draft in drafts:
|
|
283
|
+
draft_id = str(draft.get("id") or "")
|
|
284
|
+
if draft_id and draft_id not in created:
|
|
285
|
+
box["drafts"].setdefault(draft_id, draft)
|
|
236
286
|
boxes = [
|
|
237
287
|
{
|
|
238
288
|
**box,
|
|
239
289
|
"messages": list(box["messages"].values()),
|
|
240
290
|
"labels": list(box["labels"].values()),
|
|
291
|
+
"drafts": list(box["drafts"].values()),
|
|
241
292
|
}
|
|
242
293
|
for box in by_credential.values()
|
|
243
294
|
]
|
|
@@ -288,6 +339,7 @@ def as_mailboxes(ctx: SandboxStartCtx, started: Any) -> dict[str, Any]:
|
|
|
288
339
|
searches=box.get("searches"),
|
|
289
340
|
labels=box.get("labels"),
|
|
290
341
|
send_as=box.get("sendAs"),
|
|
342
|
+
clock=box.get("clock"),
|
|
291
343
|
)
|
|
292
344
|
for index, box in enumerate(boxes or [{}])
|
|
293
345
|
]
|
|
@@ -305,8 +357,15 @@ def new_mailbox(
|
|
|
305
357
|
searches: list[dict[str, Any]] | None = None,
|
|
306
358
|
labels: list[dict[str, Any] | str] | None = None,
|
|
307
359
|
send_as: list[dict[str, Any]] | None = None,
|
|
360
|
+
clock: str | None = None,
|
|
308
361
|
) -> dict[str, Any]:
|
|
309
362
|
normalized = [gmail_message(message) for message in messages or []]
|
|
363
|
+
notes: list[str] = []
|
|
364
|
+
started_drafts = [
|
|
365
|
+
_start_draft(draft, position, normalized, notes)
|
|
366
|
+
for position, draft in enumerate(drafts or [])
|
|
367
|
+
if isinstance(draft, dict)
|
|
368
|
+
]
|
|
310
369
|
for message in normalized:
|
|
311
370
|
if (
|
|
312
371
|
message.get("payload")
|
|
@@ -325,15 +384,15 @@ def new_mailbox(
|
|
|
325
384
|
"emailAddress": email_address,
|
|
326
385
|
"credential": credential,
|
|
327
386
|
"details": dict(details or {}),
|
|
328
|
-
"clock": ctx.trace_started_at,
|
|
387
|
+
"clock": clock or ctx.trace_started_at,
|
|
329
388
|
"historyId": 1000,
|
|
330
389
|
"nextId": index * 1_000_000 + 1,
|
|
331
390
|
"messages": normalized,
|
|
332
|
-
"drafts":
|
|
391
|
+
"drafts": started_drafts,
|
|
333
392
|
"searches": list(searches or []),
|
|
334
393
|
"labels": [_user_label(label) for label in labels or []],
|
|
335
394
|
"sendAs": None if send_as is None else [dict(entry) for entry in send_as],
|
|
336
|
-
"notes":
|
|
395
|
+
"notes": notes,
|
|
337
396
|
}
|
|
338
397
|
|
|
339
398
|
|
|
@@ -350,6 +409,60 @@ def _user_label(label: dict[str, Any] | str) -> dict[str, Any]:
|
|
|
350
409
|
}
|
|
351
410
|
|
|
352
411
|
|
|
412
|
+
def _draft_label(labels: Any) -> list[str]:
|
|
413
|
+
kept = [label for label in labels or [] if label != "DRAFT"]
|
|
414
|
+
return ["DRAFT", *kept]
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
def _draft_ref(message: dict[str, Any]) -> dict[str, Any]:
|
|
418
|
+
return {
|
|
419
|
+
"id": message["id"],
|
|
420
|
+
"threadId": message.get("threadId"),
|
|
421
|
+
"labelIds": message.get("labelIds"),
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _start_draft(
|
|
426
|
+
draft: dict[str, Any],
|
|
427
|
+
position: int,
|
|
428
|
+
messages: list[dict[str, Any]],
|
|
429
|
+
notes: list[str],
|
|
430
|
+
) -> dict[str, Any]:
|
|
431
|
+
draft_id = str(draft.get("id") or f"r-start{position}")
|
|
432
|
+
given = dict(draft.get("message") or {})
|
|
433
|
+
message_id = str(given.get("id") or f"m-{draft_id}")
|
|
434
|
+
if given.get("raw") and "payload" not in given:
|
|
435
|
+
given = {**given, **_fields_from_raw(given.pop("raw"))}
|
|
436
|
+
existing = next((m for m in messages if m.get("id") == message_id), None)
|
|
437
|
+
carries_content = bool(set(given) - {"id", "threadId", "labelIds", "historyId"})
|
|
438
|
+
if carries_content:
|
|
439
|
+
built = gmail_message({"labelIds": ["DRAFT"], **given, "id": message_id})
|
|
440
|
+
message = _richer(existing, built) if existing else built
|
|
441
|
+
elif existing is not None:
|
|
442
|
+
message = existing
|
|
443
|
+
else:
|
|
444
|
+
message = gmail_message(
|
|
445
|
+
{
|
|
446
|
+
"id": message_id,
|
|
447
|
+
"threadId": given.get("threadId"),
|
|
448
|
+
"labelIds": ["DRAFT"],
|
|
449
|
+
"body": "",
|
|
450
|
+
}
|
|
451
|
+
)
|
|
452
|
+
message["labelIds"] = _draft_label(
|
|
453
|
+
message.get("labelIds") if given.get("labelIds") is None else given["labelIds"]
|
|
454
|
+
)
|
|
455
|
+
if not _body_text(message.get("payload") or {}) and not message.get("snippet"):
|
|
456
|
+
notes.append(
|
|
457
|
+
f"draft {draft_id} was never read in full in the original run, so its message is empty"
|
|
458
|
+
)
|
|
459
|
+
if existing is None:
|
|
460
|
+
messages.append(message)
|
|
461
|
+
elif message is not existing:
|
|
462
|
+
messages[messages.index(existing)] = message
|
|
463
|
+
return {"id": draft_id, "message": _draft_ref(message)}
|
|
464
|
+
|
|
465
|
+
|
|
353
466
|
def _note(state: dict[str, Any], text: str) -> None:
|
|
354
467
|
notes = state.setdefault("notes", [])
|
|
355
468
|
if text not in notes:
|
|
@@ -409,9 +522,16 @@ def _shaped(
|
|
|
409
522
|
|
|
410
523
|
|
|
411
524
|
def _clock_millis(state: dict[str, Any]) -> int:
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
525
|
+
started = _millis(state.get("clock"))
|
|
526
|
+
if started is None:
|
|
527
|
+
return int(datetime.datetime.now(datetime.timezone.utc).timestamp() * 1000)
|
|
528
|
+
return started + sandbox_elapsed_ms()
|
|
529
|
+
|
|
530
|
+
|
|
531
|
+
def _created_millis(state: dict[str, Any]) -> int:
|
|
532
|
+
created = max(_clock_millis(state), int(state.get("lastCreatedAt") or 0) + 1)
|
|
533
|
+
state["lastCreatedAt"] = created
|
|
534
|
+
return created
|
|
415
535
|
|
|
416
536
|
|
|
417
537
|
def _date_bound(value: str) -> int | None:
|
|
@@ -685,29 +805,14 @@ def _bump_history(state: dict[str, Any]) -> str:
|
|
|
685
805
|
def _from_raw(
|
|
686
806
|
state: dict[str, Any], raw: str | None, thread_id: str | None, labels: list[str]
|
|
687
807
|
) -> dict[str, Any]:
|
|
688
|
-
parsed = email.message_from_string(
|
|
689
|
-
_unb64(raw) if raw else "", policy=email.policy.default
|
|
690
|
-
)
|
|
691
|
-
body_part = (
|
|
692
|
-
parsed.get_body(preferencelist=("plain", "html"))
|
|
693
|
-
if parsed.is_multipart()
|
|
694
|
-
else parsed
|
|
695
|
-
)
|
|
696
|
-
body = body_part.get_content() if body_part is not None else ""
|
|
697
|
-
fields = {
|
|
698
|
-
key: parsed.get(header)
|
|
699
|
-
for key, header in _PLAIN_HEADERS.items()
|
|
700
|
-
if parsed.get(header)
|
|
701
|
-
}
|
|
702
808
|
message_id = _next_id(state, "sandbox")
|
|
703
809
|
message = gmail_message(
|
|
704
810
|
{
|
|
705
|
-
**
|
|
811
|
+
**_fields_from_raw(raw),
|
|
706
812
|
"id": message_id,
|
|
707
813
|
"threadId": thread_id or message_id,
|
|
708
|
-
"body": str(body),
|
|
709
814
|
"labelIds": labels,
|
|
710
|
-
"internalDate":
|
|
815
|
+
"internalDate": _created_millis(state),
|
|
711
816
|
}
|
|
712
817
|
)
|
|
713
818
|
message["historyId"] = _bump_history(state)
|
|
@@ -726,31 +831,87 @@ def _send(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
|
726
831
|
}
|
|
727
832
|
|
|
728
833
|
|
|
834
|
+
def _bad_request(text: str) -> dict[str, Any]:
|
|
835
|
+
return {
|
|
836
|
+
HTTP_ERROR_KEY: {
|
|
837
|
+
"code": 400,
|
|
838
|
+
"message": text,
|
|
839
|
+
"status": "INVALID_ARGUMENT",
|
|
840
|
+
"errors": [
|
|
841
|
+
{"message": text, "domain": "global", "reason": "invalidArgument"}
|
|
842
|
+
],
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
|
|
847
|
+
def _find_draft(state: dict[str, Any], draft_id: Any) -> dict[str, Any] | None:
|
|
848
|
+
return next((d for d in state["drafts"] if d.get("id") == draft_id), None)
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
def _new_draft_message(
|
|
852
|
+
state: dict[str, Any], inner: dict[str, Any], thread_id: str | None
|
|
853
|
+
) -> dict[str, Any]:
|
|
854
|
+
message = _from_raw(
|
|
855
|
+
state, inner.get("raw"), inner.get("threadId") or thread_id, ["DRAFT"]
|
|
856
|
+
)
|
|
857
|
+
state["messages"].append(message)
|
|
858
|
+
return message
|
|
859
|
+
|
|
860
|
+
|
|
729
861
|
def _draft_create(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
730
862
|
inner = (params.get("body") or {}).get("message") or {}
|
|
731
|
-
message =
|
|
732
|
-
state
|
|
733
|
-
draft = {
|
|
734
|
-
"id": _next_id(state, "r-sandbox"),
|
|
735
|
-
"message": {
|
|
736
|
-
"id": message["id"],
|
|
737
|
-
"threadId": message["threadId"],
|
|
738
|
-
"labelIds": ["DRAFT"],
|
|
739
|
-
},
|
|
740
|
-
}
|
|
863
|
+
message = _new_draft_message(state, inner, None)
|
|
864
|
+
draft = {"id": _next_id(state, "r-sandbox"), "message": _draft_ref(message)}
|
|
741
865
|
state["drafts"].append(draft)
|
|
742
|
-
return draft
|
|
866
|
+
return copy.deepcopy(draft)
|
|
743
867
|
|
|
744
868
|
|
|
745
|
-
def
|
|
746
|
-
|
|
747
|
-
draft = next((d for d in state["drafts"] if d.get("id") == draft_id), None)
|
|
869
|
+
def _draft_get(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
870
|
+
draft = _find_draft(state, params.get("id"))
|
|
748
871
|
if draft is None:
|
|
749
|
-
return _not_found(f"draft {
|
|
750
|
-
state["drafts"].remove(draft)
|
|
872
|
+
return _not_found(f"draft {params.get('id')}")
|
|
751
873
|
message = _find(state, draft["message"]["id"])
|
|
752
874
|
if message is None:
|
|
753
875
|
return _not_found(f"message {draft['message']['id']}")
|
|
876
|
+
fmt = str(params.get("format") or "full").lower()
|
|
877
|
+
return {"id": draft["id"], "message": _shaped(message, fmt, None)}
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
def _replace_draft_message(
|
|
881
|
+
state: dict[str, Any], draft: dict[str, Any], inner: dict[str, Any]
|
|
882
|
+
) -> None:
|
|
883
|
+
old = _find(state, draft["message"]["id"])
|
|
884
|
+
if old is not None:
|
|
885
|
+
state["messages"].remove(old)
|
|
886
|
+
message = _new_draft_message(
|
|
887
|
+
state, inner, (old or draft["message"]).get("threadId")
|
|
888
|
+
)
|
|
889
|
+
draft["message"] = _draft_ref(message)
|
|
890
|
+
|
|
891
|
+
|
|
892
|
+
def _draft_update(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
893
|
+
draft = _find_draft(state, params.get("id"))
|
|
894
|
+
if draft is None:
|
|
895
|
+
return _not_found(f"draft {params.get('id')}")
|
|
896
|
+
inner = (params.get("body") or {}).get("message") or {}
|
|
897
|
+
if not inner.get("raw"):
|
|
898
|
+
return _bad_request("Missing draft message")
|
|
899
|
+
_replace_draft_message(state, draft, inner)
|
|
900
|
+
return copy.deepcopy(draft)
|
|
901
|
+
|
|
902
|
+
|
|
903
|
+
def _draft_delete(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
904
|
+
draft = _find_draft(state, params.get("id"))
|
|
905
|
+
if draft is None:
|
|
906
|
+
return _not_found(f"draft {params.get('id')}")
|
|
907
|
+
state["drafts"].remove(draft)
|
|
908
|
+
message = _find(state, draft["message"]["id"])
|
|
909
|
+
if message is not None:
|
|
910
|
+
state["messages"].remove(message)
|
|
911
|
+
return {}
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
def _mark_sent(state: dict[str, Any], message: dict[str, Any]) -> dict[str, Any]:
|
|
754
915
|
message["labelIds"] = [
|
|
755
916
|
label for label in message.get("labelIds") or [] if label != "DRAFT"
|
|
756
917
|
] + ["SENT"]
|
|
@@ -762,6 +923,26 @@ def _draft_send(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]
|
|
|
762
923
|
}
|
|
763
924
|
|
|
764
925
|
|
|
926
|
+
def _draft_send(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
927
|
+
body = params.get("body") or {}
|
|
928
|
+
inner = body.get("message") or {}
|
|
929
|
+
draft_id = body.get("id")
|
|
930
|
+
if draft_id is None:
|
|
931
|
+
if not inner.get("raw"):
|
|
932
|
+
return _bad_request("Missing draft id or message")
|
|
933
|
+
return _mark_sent(state, _new_draft_message(state, inner, None))
|
|
934
|
+
draft = _find_draft(state, draft_id)
|
|
935
|
+
if draft is None:
|
|
936
|
+
return _not_found(f"draft {draft_id}")
|
|
937
|
+
if inner.get("raw"):
|
|
938
|
+
_replace_draft_message(state, draft, inner)
|
|
939
|
+
state["drafts"].remove(draft)
|
|
940
|
+
message = _find(state, draft["message"]["id"])
|
|
941
|
+
if message is None:
|
|
942
|
+
return _not_found(f"message {draft['message']['id']}")
|
|
943
|
+
return _mark_sent(state, message)
|
|
944
|
+
|
|
945
|
+
|
|
765
946
|
def _modify(state: dict[str, Any], params: dict[str, Any]) -> dict[str, Any]:
|
|
766
947
|
message = _find(state, str(params.get("id")))
|
|
767
948
|
if message is None:
|
|
@@ -967,9 +1148,12 @@ HANDLERS: dict[str, Callable[[dict[str, Any], dict[str, Any]], dict[str, Any]]]
|
|
|
967
1148
|
"gmail.users.messages.send": _send,
|
|
968
1149
|
"gmail.users.messages.modify": _modify,
|
|
969
1150
|
"gmail.users.messages.attachments.get": _attachment,
|
|
970
|
-
|
|
1151
|
+
DRAFT_CREATE_METHOD: _draft_create,
|
|
1152
|
+
DRAFT_GET_METHOD: _draft_get,
|
|
1153
|
+
"gmail.users.drafts.update": _draft_update,
|
|
1154
|
+
"gmail.users.drafts.delete": _draft_delete,
|
|
971
1155
|
"gmail.users.drafts.send": _draft_send,
|
|
972
|
-
|
|
1156
|
+
DRAFT_LIST_METHOD: _drafts_list,
|
|
973
1157
|
"gmail.users.threads.get": _thread,
|
|
974
1158
|
"gmail.users.history.list": _history,
|
|
975
1159
|
LABELS_LIST_METHOD: _labels,
|
|
@@ -1093,9 +1277,14 @@ def gmail_sandbox(
|
|
|
1093
1277
|
send-as entries (``sendAsEmail``, ``displayName``, ``signature``,
|
|
1094
1278
|
...). Without ``sendAs``, the account's address is listed with no
|
|
1095
1279
|
signature.
|
|
1280
|
+
``clock`` (ISO 8601) is where the mailbox's time starts, in place
|
|
1281
|
+
of the original trace's start time.
|
|
1096
1282
|
Each message is a Gmail API message or plain fields as
|
|
1097
1283
|
``gmail_message`` takes them; a plain ``account`` field puts the
|
|
1098
|
-
message in that account's mailbox.
|
|
1284
|
+
message in that account's mailbox. Each draft is
|
|
1285
|
+
``{"id": ..., "message": ...}``, where the message is a Gmail API
|
|
1286
|
+
message (as ``drafts.get`` returns it, ``raw`` included), plain
|
|
1287
|
+
fields, or only the ``id`` of a message in ``messages``. For several accounts, return
|
|
1099
1288
|
``{"mailboxes": [{"emailAddress": ..., "messages": [...]}]}``.
|
|
1100
1289
|
``None`` uses ``mailbox_from_recorded_calls``.
|
|
1101
1290
|
account: With several mailboxes, names the account a call is for
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
import copy
|
|
4
|
+
import datetime
|
|
4
5
|
import json
|
|
5
6
|
import sys
|
|
6
7
|
import threading
|
|
8
|
+
import time
|
|
7
9
|
from collections.abc import Callable, Sequence
|
|
8
10
|
from dataclasses import dataclass, field
|
|
9
11
|
from typing import Any
|
|
@@ -122,10 +124,15 @@ class SandboxSession:
|
|
|
122
124
|
sandboxes: Sequence[Sandbox]
|
|
123
125
|
client: Any
|
|
124
126
|
make_start_ctx: Callable[[Sandbox], SandboxStartCtx]
|
|
127
|
+
trace_started_at: str | None = None
|
|
128
|
+
started: float = field(default_factory=time.monotonic)
|
|
125
129
|
_states: dict[str, Any] = field(default_factory=dict)
|
|
126
130
|
_reports: dict[str, dict[str, Any]] = field(default_factory=dict)
|
|
127
131
|
_lock: threading.RLock = field(default_factory=threading.RLock)
|
|
128
132
|
|
|
133
|
+
def elapsed_ms(self) -> int:
|
|
134
|
+
return int((time.monotonic() - self.started) * 1000)
|
|
135
|
+
|
|
129
136
|
def find(self, node: SpanNodeMeta) -> Sandbox | None:
|
|
130
137
|
return next(
|
|
131
138
|
(sandbox for sandbox in self.sandboxes if sandbox.match(node)), None
|
|
@@ -256,3 +263,36 @@ def sandbox_state(name: str) -> Any:
|
|
|
256
263
|
if session is None or sandbox is None:
|
|
257
264
|
return None
|
|
258
265
|
return session.state_of(sandbox, _attach_to_replay_root)
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def sandbox_elapsed_ms() -> int:
|
|
269
|
+
session = current_session()
|
|
270
|
+
return session.elapsed_ms() if session is not None else 0
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def parse_timestamp(value: str | None) -> datetime.datetime | None:
|
|
274
|
+
if not value:
|
|
275
|
+
return None
|
|
276
|
+
try:
|
|
277
|
+
parsed = datetime.datetime.fromisoformat(value.strip().replace("Z", "+00:00"))
|
|
278
|
+
except ValueError:
|
|
279
|
+
return None
|
|
280
|
+
if parsed.tzinfo is None:
|
|
281
|
+
return parsed.replace(tzinfo=datetime.timezone.utc)
|
|
282
|
+
return parsed
|
|
283
|
+
|
|
284
|
+
|
|
285
|
+
def sandbox_now() -> datetime.datetime | None:
|
|
286
|
+
"""The time sandboxes use in the current replay item.
|
|
287
|
+
|
|
288
|
+
It starts at the moment the original trace started and moves forward as
|
|
289
|
+
the replay item runs, so a sandbox answers as if the run were happening
|
|
290
|
+
again at its original time. Use it in a sandbox's ``handle`` to date what
|
|
291
|
+
the call writes. Returns ``None`` outside a replay item, or when the
|
|
292
|
+
original start time is not known. ``datetime.now()`` is not changed.
|
|
293
|
+
"""
|
|
294
|
+
session = current_session()
|
|
295
|
+
started_at = parse_timestamp(session.trace_started_at) if session else None
|
|
296
|
+
if session is None or started_at is None:
|
|
297
|
+
return None
|
|
298
|
+
return started_at + datetime.timedelta(milliseconds=session.elapsed_ms())
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|