Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(redaction): keep the token_source provenance label out of the redactor

shadowinlifea3f7758d7ee7
diff --git a/agent/src/tools/redaction.py b/agent/src/tools/redaction.py
index d804199cb..84898f6ce 100644
--- a/agent/src/tools/redaction.py
+++ b/agent/src/tools/redaction.py
@@ -125,16 +125,27 @@ _RESULT_SAFE_KEYS = {"content"}
#: Exact token-metadata names eligible for a VALUE-SHAPED exemption in every
#: sink. A familiar name alone is never permission to release its value:
#: counts must be nonnegative integers (or absent), usage may be a mapping of
-#: counts, and CLOB ids must have the public decimal uint256 shape. Unknown
+#: counts, ``token_source`` must be one of :data:`_TOKEN_SOURCE_LABELS` (or
+#: absent), and CLOB ids must have the public decimal uint256 shape. Unknown
#: shapes fail closed, including unprefixed secrets no text regex can detect.
_BENIGN_TOKEN_FIELD_KEYS = {
"token_budget",
"token_usage",
+ "token_source",
"total_input_tokens",
"total_output_tokens",
+ "cache_read_tokens",
+ "cache_creation_tokens",
"clob_token_id",
}
+#: The closed vocabulary a ``token_source`` provenance label may carry. The
+#: label is a fixed enum, not free text, so any other string fails closed
+#: rather than riding a familiar key name into a trace. ``None`` is the honest
+#: "unlabelled" value and is preserved. Adding a value here is the only way a
+#: new provenance label becomes surfaceable.
+_TOKEN_SOURCE_LABELS = frozenset({"provider", "estimated"})
+
def _fold_key(name: str) -> str:
"""Fold a key to its alphanumeric core (lower-case, separators stripped).
@@ -318,6 +329,11 @@ def is_sensitive_arg(
and len(value) <= 78
and int(value) < 2**256
)
+ if folded == "tokensource":
+ return not (
+ value is None
+ or (isinstance(value, str) and value in _TOKEN_SOURCE_LABELS)
+ )
if folded == "tokenusage" and isinstance(value, dict):
return False
return not (value is None or (type(value) is int and value >= 0))
@@ -391,15 +407,21 @@ def _redact_token_usage(value: Any, *, sink: str) -> Any:
sink: Sink policy for nested sensitive keys.
Returns:
- A fresh mapping containing only counts, nulls, and redaction sentinels.
+ A fresh mapping containing only counts, nulls, validated
+ ``token_source`` labels (:data:`_TOKEN_SOURCE_LABELS`), and redaction
+ sentinels.
"""
if isinstance(value, dict):
- return {
- key: _REDACTED
- if is_sensitive_arg(str(key), sink=sink, value=item)
- else _redact_token_usage(item, sink=sink)
- for key, item in value.items()
- }
+ scrubbed: dict[Any, Any] = {}
+ for key, item in value.items():
+ name = str(key)
+ if is_sensitive_arg(name, sink=sink, value=item):
+ scrubbed[key] = _REDACTED
+ elif _fold_key(name) == "tokensource":
+ scrubbed[key] = item
+ else:
+ scrubbed[key] = _redact_token_usage(item, sink=sink)
+ return scrubbed
return value if value is None or (type(value) is int and value >= 0) else _REDACTED
diff --git a/agent/tests/test_redaction_shared.py b/agent/tests/test_redaction_shared.py
index 7069e4b34..e458e8aad 100644
--- a/agent/tests/test_redaction_shared.py
+++ b/agent/tests/test_redaction_shared.py
@@ -182,6 +182,8 @@ def test_redact_payload_keeps_account_ref_provenance() -> None:
"token_usage",
"total_input_tokens",
"total_output_tokens",
+ "cache_read_tokens",
+ "cache_creation_tokens",
"clob_token_id",
],
)
@@ -195,6 +197,38 @@ def test_is_sensitive_arg_preserves_benign_token_fields(key: str) -> None:
assert is_sensitive_arg(key) is True # No value evidence means no exemption.
[email protected]("label", ["provider", "estimated", None])
+def test_is_sensitive_arg_preserves_token_source_label(label: str | None) -> None:
+ assert is_sensitive_arg("token_source", value=label) is False
+
+
+ "value", ["[redacted]", "bogus", "", 50000, {"label": "provider"}]
+)
+def test_is_sensitive_arg_fails_closed_on_unexpected_token_source(value: object) -> None:
+ assert is_sensitive_arg("token_source", value=value) is True
+
+
+def test_redact_payload_keeps_token_source_label_in_every_sink() -> None:
+ payload = {
+ "token_usage": {
+ "total_input_tokens": 4200,
+ "total_output_tokens": 1300,
+ "token_source": "provider",
+ }
+ }
+ assert redact_payload(payload) == payload
+ assert redact_payload(payload, sink=RESULT_SINK) == payload
+
+
+def test_redact_payload_redacts_unknown_token_source_label() -> None:
+ out = redact_payload(
+ {"token_usage": {"total_input_tokens": 1, "token_source": "bogus"}},
+ sink=RESULT_SINK,
+ )
+ assert out["token_usage"]["token_source"] == "[redacted]"
+
+
@pytest.mark.parametrize(
"key",
["api_key", "access_token", "refresh_token", "bearer_token", "token", "api_token"],