baton-proxy 0.7.0__tar.gz → 0.7.2__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.
Files changed (59) hide show
  1. baton_proxy-0.7.2/.github/workflows/test.yml +54 -0
  2. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/CHANGELOG.md +18 -1
  3. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/PKG-INFO +1 -1
  4. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/__init__.py +1 -1
  5. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/emitter.py +61 -30
  6. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/identity.py +17 -3
  7. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/scrub.py +25 -12
  8. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_identity.py +50 -2
  9. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_scrub.py +42 -0
  10. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_spec_conformance.py +3 -2
  11. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/try/kit.py +1 -1
  12. baton_proxy-0.7.0/.github/workflows/test.yml +0 -29
  13. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/.claude/settings.json +0 -0
  14. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/.github/workflows/release.yml +0 -0
  15. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/.gitignore +0 -0
  16. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/.gitmodules +0 -0
  17. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/CLAUDE.md +0 -0
  18. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/LICENSE +0 -0
  19. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/Makefile +0 -0
  20. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/README.md +0 -0
  21. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/SECURITY.md +0 -0
  22. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/examples/live-claude-invocation/instructions.md +0 -0
  23. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/examples/live-claude-invocation/result.md +0 -0
  24. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/examples/live-claude-invocation/setup.sh +0 -0
  25. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/examples/live-claude-invocation/unregister.sh +0 -0
  26. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/pyproject.toml +0 -0
  27. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/__main__.py +0 -0
  28. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/_llm_text.py +0 -0
  29. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/config.py +0 -0
  30. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/mcp_error.py +0 -0
  31. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/proxy.py +0 -0
  32. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/sinks.py +0 -0
  33. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/src/baton_proxy/transport_http.py +0 -0
  34. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/__init__.py +0 -0
  35. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/fixture_http_server.py +0 -0
  36. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/fixture_responses.py +0 -0
  37. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/fixture_server.py +0 -0
  38. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_a1_emit_e2e.py +0 -0
  39. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_a1_proxy.py +0 -0
  40. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_config.py +0 -0
  41. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_emitter.py +0 -0
  42. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_http_bridge.py +0 -0
  43. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_injection.py +0 -0
  44. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_intent_param.py +0 -0
  45. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_iserror_reclassification.py +0 -0
  46. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_llm_text.py +0 -0
  47. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_pending_eviction.py +0 -0
  48. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_proxy_env.py +0 -0
  49. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_residency_sinks.py +0 -0
  50. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_sinks.py +0 -0
  51. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_surface_snapshot.py +0 -0
  52. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_tool_list_events.py +0 -0
  53. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_tooling.py +0 -0
  54. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/tests/test_try_kit.py +0 -0
  55. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/try/.gitignore +0 -0
  56. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/try/CLAUDE.md +0 -0
  57. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/try/PROMPT.md +0 -0
  58. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/try/SECURITY.md +0 -0
  59. {baton_proxy-0.7.0 → baton_proxy-0.7.2}/uv.lock +0 -0
@@ -0,0 +1,54 @@
1
+ name: test
2
+
3
+ on:
4
+ push:
5
+ branches: [main]
6
+ pull_request:
7
+
8
+ concurrency:
9
+ group: test-${{ github.ref }}
10
+ cancel-in-progress: true
11
+
12
+ jobs:
13
+ spec-pin:
14
+ # Warns, never fails: a stale pin is how SDKs drift apart unnoticed, but
15
+ # bumping it is its own change with its own vectors to pass. Not on PRs,
16
+ # so the same warning does not sit on every unrelated one.
17
+ if: github.event_name != 'pull_request'
18
+ runs-on: ubuntu-latest
19
+ timeout-minutes: 2
20
+ continue-on-error: true
21
+ steps:
22
+ - uses: actions/checkout@v5
23
+ with:
24
+ persist-credentials: false
25
+ sparse-checkout: .gitmodules
26
+ sparse-checkout-cone-mode: false
27
+ - name: Does the baton-spec pin differ from baton-spec main?
28
+ run: |
29
+ pinned=$(git ls-tree HEAD baton-spec | awk '$1 == "160000" { print $3 }')
30
+ url=$(git config -f .gitmodules submodule.baton-spec.url || true)
31
+ latest=$(timeout 30 git ls-remote "$url" refs/heads/main | awk '$2 == "refs/heads/main" { print $1 }' || true)
32
+ if [ -z "$pinned" ] || [ -z "$latest" ]; then
33
+ echo "::warning title=baton-spec pin not checked::could not read the pin or $url"
34
+ elif [ "$pinned" != "$latest" ]; then
35
+ echo "::warning title=baton-spec pin differs from main::pinned $pinned, baton-spec main is $latest"
36
+ fi
37
+
38
+ test:
39
+ runs-on: ubuntu-latest
40
+ strategy:
41
+ fail-fast: false
42
+ matrix:
43
+ python: ["3.11", "3.12", "3.13", "3.14"]
44
+ steps:
45
+ - uses: actions/checkout@v5
46
+ with:
47
+ submodules: true
48
+ - uses: actions/setup-python@v6
49
+ with:
50
+ python-version: ${{ matrix.python }}
51
+ - run: pip install -e ".[dev,s3]"
52
+ - run: ruff check .
53
+ - run: ruff format --check .
54
+ - run: pytest
@@ -5,6 +5,23 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/).
7
7
 
8
+ ## [0.7.2] — 2026-11-01
9
+
10
+ ### Changed
11
+
12
+ - The scrubber's patterns use SPEC §7's fixed sets. A digit is `0`–`9`, a word boundary is ASCII, and whitespace is space, tab, LF, CR, FF and `U+00A0`. So `"éjane@example.com"` is now scrubbed, as in `baton-ts`, and a card or phone number in other digits, such as Arabic-Indic, is no longer matched. Any space outside the set, such as a thin space (`U+2009`), an em space or a vertical tab, no longer separates a phone number or a bearer token, so such a value is no longer redacted.
13
+
14
+ ## [0.7.1] — 2026-10-30
15
+
16
+ ### Added
17
+
18
+ - **`Organization`**, the company or workspace the caller belongs to. The
19
+ emitter's `principal=` takes a `Principal` with an `organization`, or an
20
+ `Organization` alone, and sends it as a new optional envelope member beside
21
+ `principal` (SPEC §11.4). Either one is dropped alone when unusable. The
22
+ stdio proxy resolves no caller, so it sends neither; `baton-extmcp` is the
23
+ producer that uses this.
24
+
8
25
  ## [0.7.0] — 2026-10-30
9
26
 
10
27
  ### Removed
@@ -847,7 +864,7 @@ Initial public release on PyPI.
847
864
  - Consent guard: refuses to start when an `http(s)://` sink is paired with the placeholder `BATON_CONSENT_TOKEN=local`, or when an `http(s)://` sink is configured without `BATON_API_KEY`.
848
865
  - Fail-open delivery: emission runs on a background thread; Console outage never blocks the MCP pipe.
849
866
 
850
- [Unreleased]: https://github.com/good-timing/baton-proxy/compare/v0.6.11...HEAD
867
+ [Unreleased]: https://github.com/good-timing/baton-proxy/compare/v0.7.1...HEAD
851
868
  [0.6.11]: https://github.com/good-timing/baton-proxy/compare/v0.6.10...v0.6.11
852
869
  [0.1.2]: https://github.com/good-timing/baton-proxy/compare/v0.1.1...v0.1.2
853
870
  [0.1.1]: https://github.com/good-timing/baton-proxy/compare/v0.1.0...v0.1.1
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: baton-proxy
3
- Version: 0.7.0
3
+ Version: 0.7.2
4
4
  Summary: MCP proxy that wraps a stdio server or bridges to a remote one, injects an annotation tool, and emits friction events.
5
5
  Author: Good Timing, Inc.
6
6
  License: Apache License
@@ -7,7 +7,7 @@ handshake, and emits friction events to a collector.
7
7
  See README.md for usage.
8
8
  """
9
9
 
10
- __version__ = "0.7.0"
10
+ __version__ = "0.7.2"
11
11
 
12
12
  # The product/version token, single-sourced here so the emitter's `sdk_version`
13
13
  # field and the HTTP bridge's outbound `User-Agent` header can never drift.
@@ -38,6 +38,7 @@ from baton_proxy.identity import (
38
38
  PRINCIPAL_FORMS,
39
39
  PRINCIPAL_ID_MAX_LEN,
40
40
  PRINCIPAL_SOURCE,
41
+ Organization,
41
42
  Principal,
42
43
  )
43
44
  from baton_proxy.scrub import Scrubber
@@ -104,7 +105,8 @@ class _PrincipalWire:
104
105
  """The principal AS EMITTED — the finished envelope value (SPEC §11.4).
105
106
 
106
107
  ⚠ **Not ``identity.Principal``.** That one is what a resolver HANDS US;
107
- this is what ``_wire_principal`` puts on the wire for it.
108
+ this is what ``_wire_caller`` puts on the wire for it, as ``principal`` or
109
+ ``organization``.
108
110
 
109
111
  All three members are required together — canonical wording in
110
112
  ``baton-spec/events.schema.json`` ``$defs.PrincipalWire``, checked out
@@ -124,38 +126,28 @@ class _PrincipalWire:
124
126
  _UNSENDABLE = re.compile("[\x00\ud800-\udfff]")
125
127
 
126
128
 
127
- def _wire_principal(principal: Principal | None) -> _PrincipalWire | None:
128
- """The resolver's principal as the wire object, or ``None``.
129
+ def _wire_identity(value_id: object, form: object, *, field: str) -> _PrincipalWire | None:
130
+ """One resolved identity as its wire object, or ``None``.
129
131
 
130
132
  The id is sent as given and not scrubbed: a scrubber that redacts emails
131
133
  would map every user onto one redaction constant and merge them into a
132
134
  single actor.
133
135
  """
134
- if principal is None:
135
- return None
136
- if not isinstance(principal, Principal):
137
- logger.warning(
138
- "baton-proxy: resolver returned %s, not a Principal — dropping "
139
- "principal (events still emit)",
140
- type(principal).__name__,
141
- )
142
- return None
143
- principal_id = principal.principal_id
144
- if not isinstance(principal_id, str) or not principal_id.strip():
145
- # A blank id names nobody, and every such caller would merge into one
146
- # actor.
136
+ if not isinstance(value_id, str) or not value_id.strip():
137
+ # A blank id names nobody, and every such caller would merge into one.
147
138
  logger.warning(
148
- "baton-proxy: resolver returned an empty or non-string principal_id "
149
- "— dropping principal (events still emit)"
139
+ "baton-proxy: resolver returned an empty or non-string %s — dropping it "
140
+ "(events still emit)",
141
+ field,
150
142
  )
151
143
  return None
152
- if _UNSENDABLE.search(principal_id):
144
+ if _UNSENDABLE.search(value_id):
153
145
  logger.warning(
154
- "baton-proxy: resolver returned a principal_id holding a lone surrogate "
155
- "or U+0000 — dropping principal (events still emit)"
146
+ "baton-proxy: resolver returned a %s holding a lone surrogate or U+0000 "
147
+ "— dropping it (events still emit)",
148
+ field,
156
149
  )
157
150
  return None
158
- form = principal.form
159
151
  if not isinstance(form, str) or form not in PRINCIPAL_FORMS:
160
152
  # SPEC §11.4: anything not "hashed" is personal data, so that is the
161
153
  # only safe reading of a form nobody registered.
@@ -166,8 +158,41 @@ def _wire_principal(principal: Principal | None) -> _PrincipalWire | None:
166
158
  PRINCIPAL_FORM_RAW,
167
159
  )
168
160
  form = PRINCIPAL_FORM_RAW
169
- return _PrincipalWire(
170
- id=principal_id[:PRINCIPAL_ID_MAX_LEN], source=PRINCIPAL_SOURCE, form=form
161
+ return _PrincipalWire(id=value_id[:PRINCIPAL_ID_MAX_LEN], source=PRINCIPAL_SOURCE, form=form)
162
+
163
+
164
+ def _wire_organization(organization: object) -> _PrincipalWire | None:
165
+ if organization is None:
166
+ return None
167
+ if not isinstance(organization, Organization):
168
+ logger.warning(
169
+ "baton-proxy: a Principal's organization is %s, not an Organization — "
170
+ "dropping it (events still emit)",
171
+ type(organization).__name__,
172
+ )
173
+ return None
174
+ return _wire_identity(organization.organization_id, organization.form, field="organization_id")
175
+
176
+
177
+ def _wire_caller(
178
+ resolved: Principal | Organization | None,
179
+ ) -> tuple[_PrincipalWire | None, _PrincipalWire | None]:
180
+ """The resolver's answer as ``(principal, organization)`` wire objects. An
181
+ unusable one is dropped alone (SPEC §11.4)."""
182
+ if resolved is None:
183
+ return None, None
184
+ if isinstance(resolved, Organization):
185
+ return None, _wire_organization(resolved)
186
+ if not isinstance(resolved, Principal):
187
+ logger.warning(
188
+ "baton-proxy: resolver returned %s, not a Principal or Organization — "
189
+ "dropping it (events still emit)",
190
+ type(resolved).__name__,
191
+ )
192
+ return None, None
193
+ return (
194
+ _wire_identity(resolved.principal_id, resolved.form, field="principal_id"),
195
+ _wire_organization(resolved.organization),
171
196
  )
172
197
 
173
198
 
@@ -195,6 +220,9 @@ class _Event:
195
220
  # Who was resolved behind this event (SPEC §11.4) — the id plus the two
196
221
  # facts that classify it. None → the member is omitted WHOLE.
197
222
  principal: _PrincipalWire | None = None
223
+ # The company the caller belongs to, with ``principal``'s members (SPEC
224
+ # §11.4). None → omitted WHOLE.
225
+ organization: _PrincipalWire | None = None
198
226
  call_id: str | None = None
199
227
 
200
228
  def to_json(self) -> dict[str, Any]:
@@ -215,6 +243,8 @@ class _Event:
215
243
  d["runtime_meta"] = self.runtime_meta
216
244
  if self.principal is not None:
217
245
  d["principal"] = self.principal.to_json()
246
+ if self.organization is not None:
247
+ d["organization"] = self.organization.to_json()
218
248
  if self.call_id is not None:
219
249
  d["call_id"] = self.call_id
220
250
  return d
@@ -371,7 +401,7 @@ class Emitter:
371
401
  intent_source: str | None = None,
372
402
  runtime_meta: Mapping[str, Any] | None = None,
373
403
  session_id: str | None = None,
374
- principal: Principal | None = None,
404
+ principal: Principal | Organization | None = None,
375
405
  call_id: str,
376
406
  ) -> None:
377
407
  # `call_intent` / `call_expected` / `call_workflow` are the values
@@ -439,7 +469,7 @@ class Emitter:
439
469
  duration_ms: int,
440
470
  runtime_meta: Mapping[str, Any] | None = None,
441
471
  session_id: str | None = None,
442
- principal: Principal | None = None,
472
+ principal: Principal | Organization | None = None,
443
473
  call_id: str,
444
474
  ) -> None:
445
475
  # session_id/principal are additive: the stdio proxy omits them (1-process-
@@ -465,7 +495,7 @@ class Emitter:
465
495
  result: Any = None,
466
496
  runtime_meta: Mapping[str, Any] | None = None,
467
497
  session_id: str | None = None,
468
- principal: Principal | None = None,
498
+ principal: Principal | Organization | None = None,
469
499
  call_id: str,
470
500
  ) -> None:
471
501
  """SPEC §11.4.3. ``result`` carries the full MCP envelope for a failure
@@ -733,7 +763,7 @@ class Emitter:
733
763
  tool_name: str | None = None,
734
764
  runtime_meta: Mapping[str, Any] | None = None,
735
765
  session_id: str | None = None,
736
- principal: Principal | None = None,
766
+ principal: Principal | Organization | None = None,
737
767
  signal_type: str | None = None,
738
768
  ) -> None:
739
769
  """Annotation event per SPEC §11.4; nullable keys omitted when None.
@@ -774,7 +804,7 @@ class Emitter:
774
804
  payload: dict[str, Any],
775
805
  runtime_meta: dict[str, Any] | None,
776
806
  session_id: str | None = None,
777
- principal: Principal | None = None,
807
+ principal: Principal | Organization | None = None,
778
808
  call_id: str | None = None,
779
809
  ) -> None:
780
810
  # `session_id` overrides the per-process session for callers that
@@ -785,7 +815,7 @@ class Emitter:
785
815
  if not self._config.emission_enabled or self._thread is None:
786
816
  return
787
817
 
788
- principal_wire = _wire_principal(principal)
818
+ principal_wire, organization_wire = _wire_caller(principal)
789
819
 
790
820
  # Scrub PII from the payload before anything else touches it. Both
791
821
  # the file sink and any HTTP sink will see only the scrubbed copy,
@@ -817,6 +847,7 @@ class Emitter:
817
847
  payload=payload,
818
848
  runtime_meta=runtime_meta,
819
849
  principal=principal_wire,
850
+ organization=organization_wire,
820
851
  call_id=call_id,
821
852
  )
822
853
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Baton attaches the resolved principal (``principal``) to every event so the
4
4
  Console can group by ``(tenant_id, vendor_id, principal.id)``, at whatever grain
5
- the resolver named: a person, a service account or an organisation (SPEC
5
+ the resolver named: a person or a service account (SPEC
6
6
  §11.4). The resolver decides everything about the value, including whether it
7
7
  is hashed; the emitter sends it on as stated.
8
8
 
@@ -28,6 +28,18 @@ PRINCIPAL_FORMS = frozenset({PRINCIPAL_FORM_RAW, PRINCIPAL_FORM_HASHED})
28
28
  PRINCIPAL_ID_MAX_LEN = 128
29
29
 
30
30
 
31
+ @dataclass(frozen=True)
32
+ class Organization:
33
+ """The company or workspace the caller belongs to, as the vendor sees it.
34
+
35
+ Sent as ``organization`` (SPEC §11.4), beside ``principal`` or alone. It is
36
+ never counted as a person. ``form`` describes ``organization_id`` alone.
37
+ """
38
+
39
+ organization_id: str
40
+ form: Literal["raw", "hashed"] = "raw"
41
+
42
+
31
43
  @dataclass(frozen=True)
32
44
  class Principal:
33
45
  """A resolved principal, as a resolver returns it.
@@ -48,12 +60,14 @@ class Principal:
48
60
  a pseudonym the resolver derived itself. A consumer treats anything not
49
61
  ``"hashed"`` as personal data, so leave it unset unless the resolver
50
62
  hashed the value."""
63
+ organization: Organization | None = None
51
64
 
52
65
 
53
66
  class IdentityResolver(Protocol):
54
67
  """Turns a modality-native carrier (gRPC headers / FastMCP context /
55
- host-app callback / process env) into a ``Principal``. Returns ``None`` when
68
+ host-app callback / process env) into a ``Principal`` or an
69
+ ``Organization``. Returns ``None`` when
56
70
  no identity is available — the core then omits ``principal`` WHOLE
57
71
  (fail-open)."""
58
72
 
59
- def resolve(self, carrier: Any) -> Principal | None: ...
73
+ def resolve(self, carrier: Any) -> Principal | Organization | None: ...
@@ -3,8 +3,8 @@
3
3
  The proxy emits whatever the wrapped MCP server returns — including tool
4
4
  params and results that may carry customer PII (emails in Notion search
5
5
  results, bearer tokens in error bodies, API keys pasted into chat tools).
6
- Persona B's trust pitch is that nothing unscrubbed ever leaves the
7
- customer's machine; this module is what makes that true. Every event
6
+ It is pattern matching, not a guarantee: a name or a street address passes
7
+ through. Every event
8
8
  payload runs through ``Scrubber`` in ``Emitter._enqueue`` before it lands
9
9
  in the queue, so both the local JSONL sink AND any HTTP sink see only
10
10
  scrubbed values.
@@ -21,14 +21,14 @@ scrubbed values.
21
21
  - cc — 13-19 digit candidates filtered by Luhn
22
22
 
23
23
  Plus field-name overrides: any dict key matching
24
- ``{email, phone, ssn, api_key, token, secret, password}`` (case-insensitive)
24
+ ``REDACT_FIELD_NAMES`` (case-insensitive)
25
25
  force-redacts its string value regardless of pattern.
26
26
 
27
27
  Differs from LangSmith's approach (off by default, bring-your-own regex)
28
28
  by shipping rules on by default — the right tradeoff for Persona B's
29
29
  consumer/SMB audience that won't write masking code.
30
30
 
31
- Mirrored in ``baton.scrub`` once the SDK port lands (P1 in the MVP plan).
31
+ baton-sdk's ``baton.scrub`` ships the same patterns; SPEC §7 is the rule for both.
32
32
  """
33
33
 
34
34
  from __future__ import annotations
@@ -53,25 +53,38 @@ REDACT_FIELD_NAMES: frozenset[str] = frozenset(
53
53
  {"email", "phone", "ssn", "api_key", "token", "secret", "password", "user_name"}
54
54
  )
55
55
 
56
+ # SPEC §7's fixed sets: digits are 0-9 and word boundaries are ASCII
57
+ # (re.ASCII), and whitespace is this class, because no runtime's \s matches it.
58
+ # Pinned by the SPEC §7 tests in tests/test_scrub.py.
59
+ _WS_CHARS = " \t\n\r\f\u00a0"
60
+ _WS = f"[{_WS_CHARS}]"
61
+ _PHONE_SEP = f"[{_WS_CHARS}.\\-]"
62
+
56
63
  # Ordered list of (category, pattern). Order matters where patterns can
57
64
  # overlap: JWT must run before bearer because a bare JWT can look
58
65
  # bearer-shaped. sk_key / aws_key run before email because their token
59
66
  # bodies could otherwise match the email local-part regex.
60
67
  _PATTERNS: tuple[tuple[str, re.Pattern[str]], ...] = (
61
- ("jwt", re.compile(r"\beyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\b")),
62
- ("bearer", re.compile(r"\bBearer\s+[A-Za-z0-9_\-.+/=]{16,}", re.IGNORECASE)),
63
- ("sk_key", re.compile(r"\bsk-[A-Za-z0-9_\-]{20,}\b")),
64
- ("aws_key", re.compile(r"\bAKIA[0-9A-Z]{16}\b")),
65
- ("email", re.compile(r"\b[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}\b")),
68
+ ("jwt", re.compile(r"\beyJ[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\.[A-Za-z0-9_\-]+\b", re.ASCII)),
69
+ ("bearer", re.compile(rf"\bBearer{_WS}+[A-Za-z0-9_\-.+/=]{{16,}}", re.IGNORECASE | re.ASCII)),
70
+ ("sk_key", re.compile(r"\bsk-[A-Za-z0-9_\-]{20,}\b", re.ASCII)),
71
+ ("aws_key", re.compile(r"\bAKIA[0-9A-Z]{16}\b", re.ASCII)),
72
+ ("email", re.compile(r"\b[A-Za-z0-9._%+\-]+@[A-Za-z0-9.\-]+\.[A-Za-z]{2,}\b", re.ASCII)),
66
73
  # Phone: optional ``+1``, optional area-code parens, separators
67
- # ``-`` / ``.`` / space, 10 digits. Conservative — won't catch every
74
+ # ``-``, ``.`` or whitespace, 10 digits. Conservative — won't catch every
68
75
  # international format, but won't trip on every 10-digit identifier.
69
- ("phone", re.compile(r"\b(?:\+?1[\s\-.]?)?\(?\d{3}\)?[\s\-.]?\d{3}[\s\-.]?\d{4}\b")),
76
+ (
77
+ "phone",
78
+ re.compile(
79
+ rf"\b(?:\+?1{_PHONE_SEP}?)?\(?\d{{3}}\)?{_PHONE_SEP}?\d{{3}}{_PHONE_SEP}?\d{{4}}\b",
80
+ re.ASCII,
81
+ ),
82
+ ),
70
83
  )
71
84
 
72
85
  # Credit-card candidates — Luhn-filtered before redacting to keep false
73
86
  # positives down on long numeric IDs (timestamps, order numbers).
74
- _CC_CANDIDATE = re.compile(r"\b\d{13,19}\b")
87
+ _CC_CANDIDATE = re.compile(r"\b\d{13,19}\b", re.ASCII)
75
88
 
76
89
 
77
90
  def _luhn_valid(digits: str) -> bool:
@@ -13,7 +13,7 @@ import pytest
13
13
 
14
14
  from baton_proxy.config import Config
15
15
  from baton_proxy.emitter import Emitter
16
- from baton_proxy.identity import Principal
16
+ from baton_proxy.identity import Organization, Principal
17
17
  from baton_proxy.scrub import Scrubber
18
18
 
19
19
 
@@ -29,7 +29,7 @@ def _config(path: str) -> Config:
29
29
  )
30
30
 
31
31
 
32
- def _emit_one(tmp_path, *, principal: Principal | None) -> dict:
32
+ def _emit_one(tmp_path, *, principal: Principal | Organization | None) -> dict:
33
33
  p = tmp_path / "events.jsonl"
34
34
  e = Emitter(_config(str(p)))
35
35
  e.start()
@@ -108,6 +108,54 @@ def test_no_principal_omits_the_member(tmp_path) -> None:
108
108
  assert "principal_id" not in ev
109
109
 
110
110
 
111
+ ACME_WIRE = {"id": "org_acme", "source": "asserted", "form": "hashed"}
112
+ U123_WIRE = {"id": "u123", "source": "asserted", "form": "raw"}
113
+
114
+
115
+ @pytest.mark.parametrize(
116
+ ("returned", "principal", "organization"),
117
+ [
118
+ pytest.param(
119
+ Principal(principal_id="u123", organization=Organization("org_acme", form="hashed")),
120
+ U123_WIRE,
121
+ ACME_WIRE,
122
+ id="a-person-and-their-company",
123
+ ),
124
+ pytest.param(
125
+ Organization("org_acme", form="hashed"), None, ACME_WIRE, id="only-the-company"
126
+ ),
127
+ pytest.param(
128
+ Principal(principal_id=" ", organization=Organization("org_acme", form="hashed")),
129
+ None,
130
+ ACME_WIRE,
131
+ id="an-unusable-person-keeps-the-company",
132
+ ),
133
+ pytest.param(
134
+ Principal(principal_id="u123", organization=Organization("")),
135
+ U123_WIRE,
136
+ None,
137
+ id="an-unusable-company-keeps-the-person",
138
+ ),
139
+ pytest.param(
140
+ Principal(principal_id="u123", organization="org_acme"), # type: ignore[arg-type]
141
+ U123_WIRE,
142
+ None,
143
+ id="a-company-that-is-not-an-Organization-keeps-the-person",
144
+ ),
145
+ ],
146
+ )
147
+ def test_the_organization_rides_beside_the_principal(
148
+ tmp_path, returned: Any, principal: Any, organization: Any
149
+ ) -> None:
150
+ """SPEC §11.4: either can be present without the other, and an absent one
151
+ is omitted whole."""
152
+ ev = _emit_one(tmp_path, principal=returned)
153
+ assert ev.get("principal") == principal
154
+ assert ev.get("organization") == organization
155
+ if organization is None:
156
+ assert "organization" not in ev
157
+
158
+
111
159
  # ---- scrub: user_name redacted, name-ish keys untouched --------------------
112
160
 
113
161
 
@@ -110,6 +110,48 @@ def test_multiple_pii_in_one_string_counts_each() -> None:
110
110
  assert s.counts["phone"] == 1
111
111
 
112
112
 
113
+ # =============================================================================
114
+ # SPEC §7: digits, word boundaries and whitespace are fixed sets
115
+ # =============================================================================
116
+
117
+
118
+ @pytest.mark.parametrize(
119
+ "secret",
120
+ [
121
+ "jane@example.com",
122
+ "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIn0.c2lnbmF0dXJl",
123
+ "sk-abcdefghijklmnopqrstuvwx",
124
+ "AKIAABCDEFGHIJKLMNOP",
125
+ "Bearer abcdefghijklmnop1234",
126
+ ],
127
+ )
128
+ def test_a_word_boundary_is_ascii(secret: str) -> None:
129
+ out = Scrubber()(f"é{secret}é")
130
+ assert secret not in out
131
+ assert out.startswith("é[REDACTED:")
132
+
133
+
134
+ def test_a_digit_is_0_to_9_only() -> None:
135
+ arabic_indic_card = "".join(chr(0x0660 + int(d)) for d in "4111111111111111")
136
+ assert Scrubber()(f"card {arabic_indic_card}") == f"card {arabic_indic_card}"
137
+ arabic_indic_phone = "".join(chr(0x0660 + int(d)) for d in "5551234567")
138
+ assert Scrubber()(f"call {arabic_indic_phone}") == f"call {arabic_indic_phone}"
139
+
140
+
141
+ @pytest.mark.parametrize("space", [" ", "\t", "\n", "\r", "\f", "\u00a0"])
142
+ def test_whitespace_in_the_set_separates(space: str) -> None:
143
+ s = Scrubber()
144
+ assert s(f"call 555{space}123{space}4567") == "call [REDACTED:phone]"
145
+ assert s(f"Bearer{space}abcdefghijklmnop1234") == "[REDACTED:bearer]"
146
+
147
+
148
+ @pytest.mark.parametrize("space", ["\u2009", "\u2003", "\u3000", "\x0b"])
149
+ def test_whitespace_outside_the_set_does_not(space: str) -> None:
150
+ s = Scrubber()
151
+ assert s(f"call 555{space}123{space}4567") == f"call 555{space}123{space}4567"
152
+ assert s(f"Bearer{space}abcdefghijklmnop1234") == f"Bearer{space}abcdefghijklmnop1234"
153
+
154
+
113
155
  # =============================================================================
114
156
  # Field-name override
115
157
  # =============================================================================
@@ -16,7 +16,7 @@ import pytest
16
16
 
17
17
  from baton_proxy.config import Config
18
18
  from baton_proxy.emitter import Emitter
19
- from baton_proxy.identity import Principal
19
+ from baton_proxy.identity import Organization, Principal
20
20
  from tests.fixture_responses import BAD_CURSOR
21
21
 
22
22
  HERE = Path(__file__).parent
@@ -232,7 +232,7 @@ def test_emitted_events_conform_to_shared_schema(event_schema: dict, tmp_path: P
232
232
  emitter.enqueue_tool_call_start(
233
233
  tool_name="echo",
234
234
  params={},
235
- principal=Principal(principal_id="u123"),
235
+ principal=Principal(principal_id="u123", organization=Organization("org_acme")),
236
236
  call_id="c1",
237
237
  call_expected="one record",
238
238
  call_workflow="2: lookup",
@@ -250,6 +250,7 @@ def test_emitted_events_conform_to_shared_schema(event_schema: dict, tmp_path: P
250
250
  assert principal["id"] == "u123"
251
251
  assert principal["source"] == "asserted", "the proxy verifies nothing"
252
252
  assert principal["form"] == "raw"
253
+ assert event["organization"] == {"id": "org_acme", "source": "asserted", "form": "raw"}
253
254
  assert set(EXERCISED_BY_THE_EMITTER["tool_call_start"]) <= set(event["payload"])
254
255
  jsonschema.validate(event, event_schema)
255
256
 
@@ -163,7 +163,7 @@ SETUP_URL = "https://baton.goodtiming.ai/setup/proxy"
163
163
  # it equal to `baton_proxy.__version__`. Editing the paste breaks the hash;
164
164
  # re-pinning the hash without bumping the version breaks the pair. The release
165
165
  # then carries a version the console can be updated to and named by.
166
- PASTE_VERSION = "0.7.0"
166
+ PASTE_VERSION = "0.7.2"
167
167
  PASTE_SHA256 = "6e0dbeb917a2ad6bf66aa3cfde69b516664e484b14c6a163af23114e035ad659"
168
168
 
169
169
 
@@ -1,29 +0,0 @@
1
- name: test
2
-
3
- on:
4
- push:
5
- branches: [main]
6
- pull_request:
7
-
8
- concurrency:
9
- group: test-${{ github.ref }}
10
- cancel-in-progress: true
11
-
12
- jobs:
13
- test:
14
- runs-on: ubuntu-latest
15
- strategy:
16
- fail-fast: false
17
- matrix:
18
- python: ["3.11", "3.12", "3.13", "3.14"]
19
- steps:
20
- - uses: actions/checkout@v5
21
- with:
22
- submodules: true
23
- - uses: actions/setup-python@v6
24
- with:
25
- python-version: ${{ matrix.python }}
26
- - run: pip install -e ".[dev,s3]"
27
- - run: ruff check .
28
- - run: ruff format --check .
29
- - run: pytest
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