statewire 0.23.6__tar.gz → 0.24.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 (60) hide show
  1. {statewire-0.23.6 → statewire-0.24.2}/PKG-INFO +2 -2
  2. {statewire-0.23.6 → statewire-0.24.2}/examples/demo_app.py +4 -1
  3. statewire-0.24.2/examples/local_origin.py +60 -0
  4. {statewire-0.23.6 → statewire-0.24.2}/pyproject.toml +2 -2
  5. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/__init__.py +7 -1
  6. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/api.py +495 -37
  7. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/client.py +73 -14
  8. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/document.py +69 -17
  9. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/documents.py +20 -8
  10. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/limits.py +2 -0
  11. {statewire-0.23.6 → statewire-0.24.2}/tests/statewire_helpers.py +8 -1
  12. {statewire-0.23.6 → statewire-0.24.2}/tests/test_authorize.py +149 -27
  13. statewire-0.24.2/tests/test_client_binding.py +350 -0
  14. {statewire-0.23.6 → statewire-0.24.2}/tests/test_document.py +12 -5
  15. {statewire-0.23.6 → statewire-0.24.2}/tests/test_durable_admission.py +171 -4
  16. {statewire-0.23.6 → statewire-0.24.2}/tests/test_frames.py +71 -2
  17. {statewire-0.23.6 → statewire-0.24.2}/tests/test_lanes.py +17 -0
  18. {statewire-0.23.6 → statewire-0.24.2}/tests/test_ops.py +2 -5
  19. statewire-0.24.2/tests/test_origin.py +165 -0
  20. {statewire-0.23.6 → statewire-0.24.2}/tests/test_state_proxy.py +5 -6
  21. statewire-0.24.2/tests/test_values.py +194 -0
  22. statewire-0.23.6/tests/test_client_binding.py +0 -171
  23. {statewire-0.23.6 → statewire-0.24.2}/.gitignore +0 -0
  24. {statewire-0.23.6 → statewire-0.24.2}/README.md +0 -0
  25. {statewire-0.23.6 → statewire-0.24.2}/examples/__init__.py +0 -0
  26. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/filter.py +0 -0
  27. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/langgraph.py +0 -0
  28. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/metrics.py +0 -0
  29. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/ops.py +0 -0
  30. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/protocol.py +0 -0
  31. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/state.py +0 -0
  32. {statewire-0.23.6 → statewire-0.24.2}/src/statewire/testing.py +0 -0
  33. {statewire-0.23.6 → statewire-0.24.2}/tests/client_helpers.py +0 -0
  34. {statewire-0.23.6 → statewire-0.24.2}/tests/storage_probe.py +0 -0
  35. {statewire-0.23.6 → statewire-0.24.2}/tests/test_attach_fence.py +0 -0
  36. {statewire-0.23.6 → statewire-0.24.2}/tests/test_attach_guardrail.py +0 -0
  37. {statewire-0.23.6 → statewire-0.24.2}/tests/test_attached_hook.py +0 -0
  38. {statewire-0.23.6 → statewire-0.24.2}/tests/test_client.py +0 -0
  39. {statewire-0.23.6 → statewire-0.24.2}/tests/test_client_ws.py +0 -0
  40. {statewire-0.23.6 → statewire-0.24.2}/tests/test_context.py +0 -0
  41. {statewire-0.23.6 → statewire-0.24.2}/tests/test_date_registry.py +0 -0
  42. {statewire-0.23.6 → statewire-0.24.2}/tests/test_departure.py +0 -0
  43. {statewire-0.23.6 → statewire-0.24.2}/tests/test_eviction.py +0 -0
  44. {statewire-0.23.6 → statewire-0.24.2}/tests/test_filter.py +0 -0
  45. {statewire-0.23.6 → statewire-0.24.2}/tests/test_gap_fill.py +0 -0
  46. {statewire-0.23.6 → statewire-0.24.2}/tests/test_idle.py +0 -0
  47. {statewire-0.23.6 → statewire-0.24.2}/tests/test_langgraph.py +0 -0
  48. {statewire-0.23.6 → statewire-0.24.2}/tests/test_lifespan.py +0 -0
  49. {statewire-0.23.6 → statewire-0.24.2}/tests/test_meta.py +0 -0
  50. {statewire-0.23.6 → statewire-0.24.2}/tests/test_observer.py +0 -0
  51. {statewire-0.23.6 → statewire-0.24.2}/tests/test_protocol_negotiation.py +0 -0
  52. {statewire-0.23.6 → statewire-0.24.2}/tests/test_read_filters.py +0 -0
  53. {statewire-0.23.6 → statewire-0.24.2}/tests/test_route_auth.py +0 -0
  54. {statewire-0.23.6 → statewire-0.24.2}/tests/test_start_session.py +0 -0
  55. {statewire-0.23.6 → statewire-0.24.2}/tests/test_statewire_hostable.py +0 -0
  56. {statewire-0.23.6 → statewire-0.24.2}/tests/test_storage_bridge.py +0 -0
  57. {statewire-0.23.6 → statewire-0.24.2}/tests/test_stream.py +0 -0
  58. {statewire-0.23.6 → statewire-0.24.2}/tests/test_testing.py +0 -0
  59. {statewire-0.23.6 → statewire-0.24.2}/tests/test_writer_lease.py +0 -0
  60. {statewire-0.23.6 → statewire-0.24.2}/tests/test_ws.py +0 -0
@@ -1,13 +1,13 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: statewire
3
- Version: 0.23.6
3
+ Version: 0.24.2
4
4
  Summary: Replicate one JSON object over an SSE op stream + a command endpoint
5
5
  Project-URL: Repository, https://github.com/assistant-ui/harness-sdk
6
6
  License-Expression: MIT
7
7
  Requires-Python: <4.0,>=3.12
8
8
  Requires-Dist: fastapi>=0.115
9
9
  Requires-Dist: httpx>=0.27
10
- Requires-Dist: pinboard-sdk>=0.18.0
10
+ Requires-Dist: pinboard-sdk>=0.18.3
11
11
  Requires-Dist: statepatch<0.4,>=0.3.0
12
12
  Provides-Extra: langgraph
13
13
  Requires-Dist: langchain-core>=0.3; extra == 'langgraph'
@@ -2,7 +2,7 @@
2
2
 
3
3
  Run from python/statewire/statewire:
4
4
 
5
- uv run --with 'uvicorn[standard]' uvicorn examples.demo_app:app --port 8000
5
+ PINBOARD_DEV=1 uv run --with 'uvicorn[standard]' uvicorn examples.demo_app:app --port 8000
6
6
 
7
7
  Instances are served under /threads/{id}: POST /threads/{id}/stream attaches
8
8
  the state stream, POST /threads/{id}/frames submits frames. The demo
@@ -18,6 +18,8 @@ from fastapi import FastAPI
18
18
  from pinboard import Worker
19
19
  from statewire import StatewireReject, Statewire, append, command
20
20
 
21
+ from .local_origin import local_origin_only
22
+
21
23
  MAX_COUNT = 10
22
24
 
23
25
  HAIKU = "state flows as one stream —\nevery client, every tab,\nthe same truth appears\n"
@@ -64,3 +66,4 @@ class Counter(Statewire):
64
66
  worker = Worker(hosts={"/threads": Counter})
65
67
  app = FastAPI()
66
68
  app.include_router(worker)
69
+ app.add_middleware(local_origin_only)
@@ -0,0 +1,60 @@
1
+ """Refuse browser requests from pages that are not on this machine.
2
+
3
+ Any page can open ``ws://127.0.0.1:<port>/...`` or send it requests.
4
+
5
+ - With an Origin header, it must be local or listed in ALLOWED_ORIGINS
6
+ (comma-separated).
7
+ - Without one, browsers send Sec-Fetch-Site; ``cross-site`` is refused.
8
+ - With neither (curl, server clients, older browsers), the request needs an
9
+ ``x-local-client`` header. A page can't add one without a CORS preflight,
10
+ which carries its Origin. The pinboard gateway's own requests carry
11
+ ``pinboard-*`` headers and pass the same way.
12
+
13
+ Every Python example backend carries this file;
14
+ python/harness-sdk-python/tests/test_example_local_origin.py tests each copy.
15
+ """
16
+
17
+ import os
18
+ from urllib.parse import urlsplit
19
+
20
+ _ENV = os.environ.get("ALLOWED_ORIGINS", "").split(",")
21
+ EXTRA = {o.strip().rstrip("/") for o in _ENV if o.strip()}
22
+ LOOPBACK = ("localhost", "127.0.0.1", "::1")
23
+ LOCAL_FETCH_SITES = ("same-origin", "same-site", "none")
24
+ LOCAL_CLIENT_HEADER = b"x-local-client"
25
+
26
+
27
+ def origin_allowed(origin: str) -> bool:
28
+ try:
29
+ url = urlsplit(origin)
30
+ local = url.scheme in ("http", "https") and url.hostname in LOOPBACK
31
+ except ValueError:
32
+ local = False
33
+ return local or origin.strip().rstrip("/") in EXTRA
34
+
35
+
36
+ def request_allowed(headers: list[tuple[bytes, bytes]]) -> bool:
37
+ values = {name.lower(): value for name, value in headers}
38
+ if b"origin" in values:
39
+ return origin_allowed(values[b"origin"].decode("latin-1"))
40
+ if b"sec-fetch-site" in values:
41
+ site = values[b"sec-fetch-site"].decode("latin-1").strip().lower()
42
+ return site in LOCAL_FETCH_SITES
43
+ return LOCAL_CLIENT_HEADER in values or any(
44
+ name.startswith(b"pinboard-") for name in values
45
+ )
46
+
47
+
48
+ def local_origin_only(app):
49
+ async def call(scope, receive, send):
50
+ if scope["type"] not in ("http", "websocket") or request_allowed(
51
+ scope.get("headers", [])
52
+ ):
53
+ return await app(scope, receive, send)
54
+ if scope["type"] == "websocket":
55
+ await receive()
56
+ return await send({"type": "websocket.close", "code": 1008})
57
+ await send({"type": "http.response.start", "status": 403, "headers": []})
58
+ await send({"type": "http.response.body", "body": b"origin not allowed"})
59
+
60
+ return call
@@ -1,12 +1,12 @@
1
1
  [project]
2
2
  name = "statewire"
3
- version = "0.23.6"
3
+ version = "0.24.2"
4
4
  description = "Replicate one JSON object over an SSE op stream + a command endpoint"
5
5
  readme = "README.md"
6
6
  license = "MIT"
7
7
  requires-python = ">=3.12,<4.0"
8
8
  dependencies = [
9
- "pinboard-sdk>=0.18.0",
9
+ "pinboard-sdk>=0.18.3",
10
10
  "statepatch>=0.3.0,<0.4",
11
11
  "fastapi>=0.115",
12
12
  "httpx>=0.27",
@@ -12,7 +12,12 @@ from .api import (
12
12
  from .filter import ANY, UNFILTERED, Filter
13
13
  from .protocol import Subprotocol
14
14
  from statepatch import Proxy as StateProxy, append, plain
15
- from .document import StatewireDocument, encode_document, decode_document
15
+ from .document import (
16
+ StatewireDocument,
17
+ StatewireValueError,
18
+ decode_document,
19
+ encode_document,
20
+ )
16
21
 
17
22
  __all__ = [
18
23
  "ANY",
@@ -21,6 +26,7 @@ __all__ = [
21
26
  "HeldFrame",
22
27
  "StatewireDocument",
23
28
  "encode_document",
29
+ "StatewireValueError",
24
30
  "decode_document",
25
31
  "Statewire",
26
32
  "StatewireAttach",