statepatch 0.2.2__tar.gz → 0.3.1__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.
- {statepatch-0.2.2 → statepatch-0.3.1}/.gitignore +8 -0
- {statepatch-0.2.2 → statepatch-0.3.1}/PKG-INFO +9 -5
- {statepatch-0.2.2 → statepatch-0.3.1}/README.md +8 -4
- {statepatch-0.2.2 → statepatch-0.3.1}/pyproject.toml +1 -1
- {statepatch-0.2.2 → statepatch-0.3.1}/src/statepatch/__init__.py +2 -1
- {statepatch-0.2.2 → statepatch-0.3.1}/src/statepatch/_wire.py +37 -14
- {statepatch-0.2.2 → statepatch-0.3.1}/tests/test_observable.py +8 -2
- {statepatch-0.2.2 → statepatch-0.3.1}/src/statepatch/_core.py +0 -0
- {statepatch-0.2.2 → statepatch-0.3.1}/tests/test_snapshot.py +0 -0
|
@@ -2,6 +2,7 @@ node_modules
|
|
|
2
2
|
.next
|
|
3
3
|
dist
|
|
4
4
|
dist-test
|
|
5
|
+
coverage
|
|
5
6
|
.wrangler
|
|
6
7
|
.DS_Store
|
|
7
8
|
.env
|
|
@@ -28,3 +29,10 @@ apps/docs/.docs
|
|
|
28
29
|
|
|
29
30
|
MAINTAIN_EPOCH.md
|
|
30
31
|
DOCS_EPOCH.md
|
|
32
|
+
|
|
33
|
+
# terraform
|
|
34
|
+
.terraform/
|
|
35
|
+
*.tfstate
|
|
36
|
+
*.tfstate.*
|
|
37
|
+
*.tfvars
|
|
38
|
+
!*.tfvars.example
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: statepatch
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Immer-style ops over a plain JSON tree: observables that emit every mutation as an op
|
|
5
5
|
Project-URL: Repository, https://github.com/assistant-ui/harness-sdk
|
|
6
6
|
License-Expression: MIT
|
|
@@ -9,7 +9,7 @@ Description-Content-Type: text/markdown
|
|
|
9
9
|
|
|
10
10
|
# statepatch
|
|
11
11
|
|
|
12
|
-
Immer-style ops over a plain JSON tree: `observable(initial)` wraps one value, `.value` reads it, and every mutation emits the matching `replace` / `add` / `remove` op to subscribers. Observables compose: one stored inside another mounts its op stream at that path. Part of [harness-sdk](https://github.com/assistant-ui/harness-sdk), under `statewire` and
|
|
12
|
+
Immer-style ops over a plain JSON tree: `observable(initial)` wraps one value, `.value` reads it, and every mutation emits the matching `replace` / `add` / `remove` op to subscribers. Observables compose: one stored inside another mounts its op stream at that path. Part of [harness-sdk](https://github.com/assistant-ui/harness-sdk), under `statewire` and Pinboard.
|
|
13
13
|
|
|
14
14
|
## Install
|
|
15
15
|
|
|
@@ -31,8 +31,12 @@ run["queue"].append({"id": "q1"}) # forwarded as add at ["runs", 0, "queue", 0]
|
|
|
31
31
|
state["runs"].pop(0) # remove op; run is detached and reusable
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
+
## Validation
|
|
35
|
+
|
|
36
|
+
`from statepatch import WireEncodingError` exposes the `ValueError` subclass for invalid wire values.
|
|
37
|
+
|
|
34
38
|
## Docs
|
|
35
39
|
|
|
36
|
-
- Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/reference/packages/page.mdx
|
|
37
|
-
- Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/guides/statewire/host/page.mdx
|
|
38
|
-
- Spec: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/spec/statewire-protocol/statepatch/page.mdx
|
|
40
|
+
- Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/docs/reference/packages/page.mdx
|
|
41
|
+
- Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/statewire/docs/guides/statewire/host/page.mdx
|
|
42
|
+
- Spec: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/statewire/docs/spec/statewire-protocol/statepatch/page.mdx
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# statepatch
|
|
2
2
|
|
|
3
|
-
Immer-style ops over a plain JSON tree: `observable(initial)` wraps one value, `.value` reads it, and every mutation emits the matching `replace` / `add` / `remove` op to subscribers. Observables compose: one stored inside another mounts its op stream at that path. Part of [harness-sdk](https://github.com/assistant-ui/harness-sdk), under `statewire` and
|
|
3
|
+
Immer-style ops over a plain JSON tree: `observable(initial)` wraps one value, `.value` reads it, and every mutation emits the matching `replace` / `add` / `remove` op to subscribers. Observables compose: one stored inside another mounts its op stream at that path. Part of [harness-sdk](https://github.com/assistant-ui/harness-sdk), under `statewire` and Pinboard.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -22,8 +22,12 @@ run["queue"].append({"id": "q1"}) # forwarded as add at ["runs", 0, "queue", 0]
|
|
|
22
22
|
state["runs"].pop(0) # remove op; run is detached and reusable
|
|
23
23
|
```
|
|
24
24
|
|
|
25
|
+
## Validation
|
|
26
|
+
|
|
27
|
+
`from statepatch import WireEncodingError` exposes the `ValueError` subclass for invalid wire values.
|
|
28
|
+
|
|
25
29
|
## Docs
|
|
26
30
|
|
|
27
|
-
- Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/reference/packages/page.mdx
|
|
28
|
-
- Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/guides/statewire/host/page.mdx
|
|
29
|
-
- Spec: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/spec/statewire-protocol/statepatch/page.mdx
|
|
31
|
+
- Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/docs/reference/packages/page.mdx
|
|
32
|
+
- Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/statewire/docs/guides/statewire/host/page.mdx
|
|
33
|
+
- Spec: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/src/app/statewire/docs/spec/statewire-protocol/statepatch/page.mdx
|
|
@@ -7,7 +7,7 @@ stream at that path; removing it detaches it, live and reusable.
|
|
|
7
7
|
"""
|
|
8
8
|
|
|
9
9
|
from ._core import Observable, Proxy, append, observable
|
|
10
|
-
from ._wire import Listener, Op, Path, Segment, plain, snapshot
|
|
10
|
+
from ._wire import Listener, Op, Path, Segment, WireEncodingError, plain, snapshot
|
|
11
11
|
|
|
12
12
|
__all__ = [
|
|
13
13
|
"observable",
|
|
@@ -20,4 +20,5 @@ __all__ = [
|
|
|
20
20
|
"Path",
|
|
21
21
|
"Segment",
|
|
22
22
|
"Listener",
|
|
23
|
+
"WireEncodingError",
|
|
23
24
|
]
|
|
@@ -21,6 +21,10 @@ _MAX_SAFE_INT = 2**53 - 1
|
|
|
21
21
|
_MISSING: Any = object()
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
class WireEncodingError(ValueError):
|
|
25
|
+
"""A value failed Statepatch's wire validation."""
|
|
26
|
+
|
|
27
|
+
|
|
24
28
|
def _snapshot(node: Any) -> Any:
|
|
25
29
|
from ._core import Observable, Proxy
|
|
26
30
|
|
|
@@ -59,6 +63,7 @@ def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]
|
|
|
59
63
|
from ._core import Observable, Proxy
|
|
60
64
|
|
|
61
65
|
mounts: list[tuple[Path, Observable]] = []
|
|
66
|
+
ancestors: set[int] = set()
|
|
62
67
|
|
|
63
68
|
def walk(value: Any, path: Path) -> Any:
|
|
64
69
|
if isinstance(value, Observable):
|
|
@@ -69,33 +74,51 @@ def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]
|
|
|
69
74
|
if isinstance(value, bool) or value is None or isinstance(value, str):
|
|
70
75
|
return value
|
|
71
76
|
if isinstance(value, float):
|
|
77
|
+
# As JSON.stringify writes them.
|
|
72
78
|
if math.isnan(value) or math.isinf(value):
|
|
73
|
-
|
|
79
|
+
return None
|
|
80
|
+
if value.is_integer() and abs(value) > _MAX_SAFE_INT:
|
|
81
|
+
raise WireEncodingError(
|
|
82
|
+
f"the number {value} at {_at(path)} is past the JS safe integer range; send it as a string"
|
|
83
|
+
)
|
|
74
84
|
return value
|
|
75
85
|
if isinstance(value, int):
|
|
76
86
|
if abs(value) > _MAX_SAFE_INT:
|
|
77
|
-
raise
|
|
78
|
-
f"
|
|
87
|
+
raise WireEncodingError(
|
|
88
|
+
f"the number {value} at {_at(path)} is past the JS safe integer range; send it as a string"
|
|
79
89
|
)
|
|
80
90
|
return value
|
|
81
91
|
if isinstance(value, datetime):
|
|
82
92
|
if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
|
|
83
|
-
raise
|
|
93
|
+
raise WireEncodingError(f"a naive datetime at {_at(path)} is not wire-encodable; attach a timezone")
|
|
84
94
|
return value
|
|
85
|
-
if isinstance(value, (list, tuple)):
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if not isinstance(
|
|
91
|
-
|
|
92
|
-
out[
|
|
93
|
-
|
|
94
|
-
|
|
95
|
+
if isinstance(value, (list, tuple, dict)):
|
|
96
|
+
if id(value) in ancestors:
|
|
97
|
+
raise WireEncodingError(f"a cycle at {_at(path)} is not wire-encodable")
|
|
98
|
+
ancestors.add(id(value))
|
|
99
|
+
try:
|
|
100
|
+
if not isinstance(value, dict):
|
|
101
|
+
return [walk(item, path + (i,)) for i, item in enumerate(value)]
|
|
102
|
+
out: dict[str, Any] = {}
|
|
103
|
+
for key, item in value.items():
|
|
104
|
+
if not isinstance(key, str):
|
|
105
|
+
raise TypeError(f"object keys must be strings, got {type(key).__name__} at {_at(path)}")
|
|
106
|
+
out[key] = walk(item, path + (key,))
|
|
107
|
+
return out
|
|
108
|
+
finally:
|
|
109
|
+
ancestors.discard(id(value))
|
|
110
|
+
raise TypeError(f"a {type(value).__name__} at {_at(path)} is not wire-encodable")
|
|
95
111
|
|
|
96
112
|
return walk(value, base), mounts
|
|
97
113
|
|
|
98
114
|
|
|
115
|
+
def _at(path: Path) -> str:
|
|
116
|
+
out = ""
|
|
117
|
+
for key in path:
|
|
118
|
+
out += f"[{key}]" if isinstance(key, int) else (f".{key}" if out else str(key))
|
|
119
|
+
return out or "the root"
|
|
120
|
+
|
|
121
|
+
|
|
99
122
|
def _index(container: Any, key: Any) -> Any:
|
|
100
123
|
if isinstance(container, list) and isinstance(key, int) and key < 0:
|
|
101
124
|
return key + len(container)
|
|
@@ -77,8 +77,6 @@ def test_values_are_dealised_on_write():
|
|
|
77
77
|
|
|
78
78
|
def test_invalid_values_fail_fast():
|
|
79
79
|
state = observable({})
|
|
80
|
-
with pytest.raises(ValueError):
|
|
81
|
-
state["x"] = float("nan")
|
|
82
80
|
with pytest.raises(ValueError):
|
|
83
81
|
state["x"] = 2**53
|
|
84
82
|
with pytest.raises(ValueError):
|
|
@@ -90,6 +88,14 @@ def test_invalid_values_fail_fast():
|
|
|
90
88
|
state["x"] = datetime(2026, 1, 1, tzinfo=timezone.utc) # aware is fine
|
|
91
89
|
|
|
92
90
|
|
|
91
|
+
def test_non_finite_numbers_are_none_as_json_writes_them():
|
|
92
|
+
state = observable({})
|
|
93
|
+
ops = collect(state)
|
|
94
|
+
state["x"] = [float("nan"), float("inf"), 1.5]
|
|
95
|
+
assert state["x"] == [None, None, 1.5]
|
|
96
|
+
assert ops == [Op("replace", ("x",), [None, None, 1.5])]
|
|
97
|
+
|
|
98
|
+
|
|
93
99
|
def test_mount_in_initial_value_forwards_ops():
|
|
94
100
|
child = observable({"n": 0})
|
|
95
101
|
parent = observable({"child": child, "other": 1})
|
|
File without changes
|
|
File without changes
|