statepatch 0.3.0__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.
@@ -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
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: statepatch
3
- Version: 0.3.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 `pinned`.
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
 
@@ -37,6 +37,6 @@ state["runs"].pop(0) # remove op; run is detached and reusable
37
37
 
38
38
  ## Docs
39
39
 
40
- - Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/reference/packages/page.mdx
41
- - Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/guides/statewire/host/page.mdx
42
- - 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 `pinned`.
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
 
@@ -28,6 +28,6 @@ state["runs"].pop(0) # remove op; run is detached and reusable
28
28
 
29
29
  ## Docs
30
30
 
31
- - Reference: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/reference/packages/page.mdx
32
- - Guide: https://github.com/assistant-ui/harness-sdk/blob/main/apps/docs/app/docs/guides/statewire/host/page.mdx
33
- - 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
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "statepatch"
3
- version = "0.3.0"
3
+ version = "0.3.1"
4
4
  description = "Immer-style ops over a plain JSON tree: observables that emit every mutation as an op"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -63,6 +63,7 @@ def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]
63
63
  from ._core import Observable, Proxy
64
64
 
65
65
  mounts: list[tuple[Path, Observable]] = []
66
+ ancestors: set[int] = set()
66
67
 
67
68
  def walk(value: Any, path: Path) -> Any:
68
69
  if isinstance(value, Observable):
@@ -73,33 +74,51 @@ def _adopt(value: Any, base: Path) -> tuple[Any, list[tuple[Path, "Observable"]]
73
74
  if isinstance(value, bool) or value is None or isinstance(value, str):
74
75
  return value
75
76
  if isinstance(value, float):
77
+ # As JSON.stringify writes them.
76
78
  if math.isnan(value) or math.isinf(value):
77
- raise WireEncodingError(f"{value!r} is not wire-encodable; floats must be finite")
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
+ )
78
84
  return value
79
85
  if isinstance(value, int):
80
86
  if abs(value) > _MAX_SAFE_INT:
81
87
  raise WireEncodingError(
82
- f"integer {value} exceeds the JS safe range (|n| <= 2**53-1); send it as a string"
88
+ f"the number {value} at {_at(path)} is past the JS safe integer range; send it as a string"
83
89
  )
84
90
  return value
85
91
  if isinstance(value, datetime):
86
92
  if value.tzinfo is None or value.tzinfo.utcoffset(value) is None:
87
- raise WireEncodingError("naive datetime is not wire-encodable; attach a timezone")
93
+ raise WireEncodingError(f"a naive datetime at {_at(path)} is not wire-encodable; attach a timezone")
88
94
  return value
89
- if isinstance(value, (list, tuple)):
90
- return [walk(item, path + (i,)) for i, item in enumerate(value)]
91
- if isinstance(value, dict):
92
- out: dict[str, Any] = {}
93
- for key, item in value.items():
94
- if not isinstance(key, str):
95
- raise TypeError(f"object keys must be strings, got {type(key).__name__}")
96
- out[key] = walk(item, path + (key,))
97
- return out
98
- raise TypeError(f"{type(value).__name__} is not wire-encodable")
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")
99
111
 
100
112
  return walk(value, base), mounts
101
113
 
102
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
+
103
122
  def _index(container: Any, key: Any) -> Any:
104
123
  if isinstance(container, list) and isinstance(key, int) and key < 0:
105
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})