xaiop 0.17.0__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 (86) hide show
  1. xaiop-0.17.0/LICENSE +21 -0
  2. xaiop-0.17.0/PKG-INFO +55 -0
  3. xaiop-0.17.0/README.md +29 -0
  4. xaiop-0.17.0/pyproject.toml +41 -0
  5. xaiop-0.17.0/setup.cfg +4 -0
  6. xaiop-0.17.0/src/xaiop/__init__.py +202 -0
  7. xaiop-0.17.0/src/xaiop/_checkpoint_ops.py +173 -0
  8. xaiop-0.17.0/src/xaiop/annotation_span.py +325 -0
  9. xaiop-0.17.0/src/xaiop/byte_parse.py +1124 -0
  10. xaiop-0.17.0/src/xaiop/checkpoint.py +870 -0
  11. xaiop-0.17.0/src/xaiop/clone.py +31 -0
  12. xaiop-0.17.0/src/xaiop/compat.py +63 -0
  13. xaiop-0.17.0/src/xaiop/control/__init__.py +54 -0
  14. xaiop-0.17.0/src/xaiop/control/control.py +844 -0
  15. xaiop-0.17.0/src/xaiop/control/resume_log.py +82 -0
  16. xaiop-0.17.0/src/xaiop/encode.py +946 -0
  17. xaiop-0.17.0/src/xaiop/engine.py +292 -0
  18. xaiop-0.17.0/src/xaiop/errors.py +22 -0
  19. xaiop-0.17.0/src/xaiop/fragment.py +24 -0
  20. xaiop-0.17.0/src/xaiop/history.py +312 -0
  21. xaiop-0.17.0/src/xaiop/label_escape.py +29 -0
  22. xaiop-0.17.0/src/xaiop/line_intercept.py +116 -0
  23. xaiop-0.17.0/src/xaiop/materialize.py +30 -0
  24. xaiop-0.17.0/src/xaiop/merge.py +106 -0
  25. xaiop-0.17.0/src/xaiop/modes.py +32 -0
  26. xaiop-0.17.0/src/xaiop/parse.py +1422 -0
  27. xaiop-0.17.0/src/xaiop/phase_encode.py +42 -0
  28. xaiop-0.17.0/src/xaiop/py.typed +0 -0
  29. xaiop-0.17.0/src/xaiop/schedule.py +11 -0
  30. xaiop-0.17.0/src/xaiop/states.py +30 -0
  31. xaiop-0.17.0/src/xaiop/stream/__init__.py +14 -0
  32. xaiop-0.17.0/src/xaiop/stream/transport.py +264 -0
  33. xaiop-0.17.0/src/xaiop/stream/xaiop_stream.py +539 -0
  34. xaiop-0.17.0/src/xaiop/types/__init__.py +43 -0
  35. xaiop-0.17.0/src/xaiop/types/_impl.py +857 -0
  36. xaiop-0.17.0/src/xaiop/ws/__init__.py +17 -0
  37. xaiop-0.17.0/src/xaiop/ws/connection.py +451 -0
  38. xaiop-0.17.0/src/xaiop/ws/hub.py +119 -0
  39. xaiop-0.17.0/src/xaiop/ws/xaiop_ws.py +52 -0
  40. xaiop-0.17.0/src/xaiop.egg-info/PKG-INFO +55 -0
  41. xaiop-0.17.0/src/xaiop.egg-info/SOURCES.txt +84 -0
  42. xaiop-0.17.0/src/xaiop.egg-info/dependency_links.txt +1 -0
  43. xaiop-0.17.0/src/xaiop.egg-info/requires.txt +9 -0
  44. xaiop-0.17.0/src/xaiop.egg-info/top_level.txt +1 -0
  45. xaiop-0.17.0/tests/test_amp_delete.py +305 -0
  46. xaiop-0.17.0/tests/test_annotation_span.py +399 -0
  47. xaiop-0.17.0/tests/test_array.py +73 -0
  48. xaiop-0.17.0/tests/test_array_select.py +188 -0
  49. xaiop-0.17.0/tests/test_array_select_history.py +334 -0
  50. xaiop-0.17.0/tests/test_bang_at.py +174 -0
  51. xaiop-0.17.0/tests/test_byte_parse.py +300 -0
  52. xaiop-0.17.0/tests/test_checkpoint.py +76 -0
  53. xaiop-0.17.0/tests/test_checkpoint_compact.py +171 -0
  54. xaiop-0.17.0/tests/test_checkpoint_window.py +190 -0
  55. xaiop-0.17.0/tests/test_compat.py +42 -0
  56. xaiop-0.17.0/tests/test_compat_fixes.py +307 -0
  57. xaiop-0.17.0/tests/test_complex.py +14 -0
  58. xaiop-0.17.0/tests/test_content.py +44 -0
  59. xaiop-0.17.0/tests/test_content_escape.py +130 -0
  60. xaiop-0.17.0/tests/test_control.py +70 -0
  61. xaiop-0.17.0/tests/test_control_coverage.py +261 -0
  62. xaiop-0.17.0/tests/test_control_resume.py +122 -0
  63. xaiop-0.17.0/tests/test_core_wire_corpus.py +93 -0
  64. xaiop-0.17.0/tests/test_encode.py +55 -0
  65. xaiop-0.17.0/tests/test_encode_options.py +41 -0
  66. xaiop-0.17.0/tests/test_encode_robust.py +195 -0
  67. xaiop-0.17.0/tests/test_encode_stability.py +127 -0
  68. xaiop-0.17.0/tests/test_engine.py +116 -0
  69. xaiop-0.17.0/tests/test_hash_annotation.py +77 -0
  70. xaiop-0.17.0/tests/test_history.py +341 -0
  71. xaiop-0.17.0/tests/test_line_intercept.py +152 -0
  72. xaiop-0.17.0/tests/test_live.py +45 -0
  73. xaiop-0.17.0/tests/test_merge.py +112 -0
  74. xaiop-0.17.0/tests/test_modes.py +44 -0
  75. xaiop-0.17.0/tests/test_ops.py +102 -0
  76. xaiop-0.17.0/tests/test_phase_encode.py +30 -0
  77. xaiop-0.17.0/tests/test_root_fragment.py +50 -0
  78. xaiop-0.17.0/tests/test_stream.py +109 -0
  79. xaiop-0.17.0/tests/test_stream_advanced.py +186 -0
  80. xaiop-0.17.0/tests/test_stream_consistency.py +178 -0
  81. xaiop-0.17.0/tests/test_stream_http.py +145 -0
  82. xaiop-0.17.0/tests/test_symbol_keys.py +98 -0
  83. xaiop-0.17.0/tests/test_types.py +236 -0
  84. xaiop-0.17.0/tests/test_version.py +10 -0
  85. xaiop-0.17.0/tests/test_ws.py +230 -0
  86. xaiop-0.17.0/tests/test_ws_typecheck.py +253 -0
xaiop-0.17.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 小萱baibai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
xaiop-0.17.0/PKG-INFO ADDED
@@ -0,0 +1,55 @@
1
+ Metadata-Version: 2.4
2
+ Name: xaiop
3
+ Version: 0.17.0
4
+ Summary: XAIOP Python SDK — official product SDK (wire 0.7.0 Frozen)
5
+ Author: XAIOP Contributors
6
+ License-Expression: MIT
7
+ Project-URL: Homepage, https://github.com/AboutUip/XAIOP
8
+ Project-URL: Documentation, https://github.com/AboutUip/XAIOP/tree/main/docs/sdk/python
9
+ Keywords: xaiop,llm,structured-output,protocol
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.10
13
+ Classifier: Programming Language :: Python :: 3.11
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Typing :: Typed
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Provides-Extra: dev
20
+ Requires-Dist: pytest>=8.0; extra == "dev"
21
+ Provides-Extra: http
22
+ Requires-Dist: httpx>=0.27; extra == "http"
23
+ Provides-Extra: ws
24
+ Requires-Dist: websockets>=12.0; extra == "ws"
25
+ Dynamic: license-file
26
+
27
+ # XAIOP Python SDK
28
+
29
+ Official product SDK (`xaiop` **0.17.0**, protocol **0.7.0** Frozen).
30
+
31
+ Guide: [../../docs/sdk/python/README.md](../../docs/sdk/python/README.md)
32
+ Parity: [../../docs/sdk/python/ALIGNMENT.md](../../docs/sdk/python/ALIGNMENT.md)
33
+ Demo: [../../demos/python/](../../demos/python/)
34
+
35
+ ## Setup
36
+
37
+ ```bash
38
+ python -m pip install -e ".[dev,http,ws]"
39
+ pytest
40
+ ```
41
+
42
+ ## Golden
43
+
44
+ ```bash
45
+ cd ../conformance && npm run golden:python
46
+ ```
47
+
48
+ ## Timing
49
+
50
+ ```bash
51
+ cd ../timing && python python/bench.py --quick
52
+ # Parse ↔ JSON: npm run bench:python:json-gate (from timing/)
53
+ ```
54
+
55
+ Hub: [../../docs/performance.md](../../docs/performance.md). Extreme-perf tip (**2026-08-09**): [../../docs/meta/release-notes-2026-08-09-sdk-extreme-perf-internal.md](../../docs/meta/release-notes-2026-08-09-sdk-extreme-perf-internal.md). Prior: [../../docs/meta/release-notes-2026-08-08-python-0.15.1-internal.md](../../docs/meta/release-notes-2026-08-08-python-0.15.1-internal.md).
xaiop-0.17.0/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # XAIOP Python SDK
2
+
3
+ Official product SDK (`xaiop` **0.17.0**, protocol **0.7.0** Frozen).
4
+
5
+ Guide: [../../docs/sdk/python/README.md](../../docs/sdk/python/README.md)
6
+ Parity: [../../docs/sdk/python/ALIGNMENT.md](../../docs/sdk/python/ALIGNMENT.md)
7
+ Demo: [../../demos/python/](../../demos/python/)
8
+
9
+ ## Setup
10
+
11
+ ```bash
12
+ python -m pip install -e ".[dev,http,ws]"
13
+ pytest
14
+ ```
15
+
16
+ ## Golden
17
+
18
+ ```bash
19
+ cd ../conformance && npm run golden:python
20
+ ```
21
+
22
+ ## Timing
23
+
24
+ ```bash
25
+ cd ../timing && python python/bench.py --quick
26
+ # Parse ↔ JSON: npm run bench:python:json-gate (from timing/)
27
+ ```
28
+
29
+ Hub: [../../docs/performance.md](../../docs/performance.md). Extreme-perf tip (**2026-08-09**): [../../docs/meta/release-notes-2026-08-09-sdk-extreme-perf-internal.md](../../docs/meta/release-notes-2026-08-09-sdk-extreme-perf-internal.md). Prior: [../../docs/meta/release-notes-2026-08-08-python-0.15.1-internal.md](../../docs/meta/release-notes-2026-08-08-python-0.15.1-internal.md).
@@ -0,0 +1,41 @@
1
+ [build-system]
2
+ requires = ["setuptools>=68", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "xaiop"
7
+ version = "0.17.0"
8
+ description = "XAIOP Python SDK — official product SDK (wire 0.7.0 Frozen)"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "XAIOP Contributors" }]
14
+ keywords = ["xaiop", "llm", "structured-output", "protocol"]
15
+ classifiers = [
16
+ "Development Status :: 5 - Production/Stable",
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Typing :: Typed",
22
+ ]
23
+ dependencies = []
24
+
25
+ [project.optional-dependencies]
26
+ dev = ["pytest>=8.0"]
27
+ http = ["httpx>=0.27"]
28
+ ws = ["websockets>=12.0"]
29
+
30
+ [project.urls]
31
+ Homepage = "https://github.com/AboutUip/XAIOP"
32
+ Documentation = "https://github.com/AboutUip/XAIOP/tree/main/docs/sdk/python"
33
+
34
+ [tool.setuptools.packages.find]
35
+ where = ["src"]
36
+
37
+ [tool.setuptools.package-data]
38
+ xaiop = ["py.typed"]
39
+
40
+ [tool.pytest.ini_options]
41
+ testpaths = ["tests"]
xaiop-0.17.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,202 @@
1
+ """XAIOP Python SDK — official product SDK (wire v0.7.0 Frozen)."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from .byte_parse import (
6
+ BYTE_MODE,
7
+ SINK_MODE,
8
+ XaiopByteError,
9
+ XaiopByteParser,
10
+ XaiopSink,
11
+ )
12
+ from .annotation_span import (
13
+ AnnotationSpan,
14
+ AnnotationSpanView,
15
+ apply_annotation_spans,
16
+ encode_as_sibling_lines,
17
+ path_escapes_type_check,
18
+ )
19
+ from .checkpoint import DotCheckpointEngine
20
+ from .clone import clone_json
21
+ from .compat import COMPAT_FIX_DEFAULTS, COMPAT_FIX_IDS, CompatPolicy, resolve_compat_options
22
+ from .control import (
23
+ CONTROL_CAPABILITY,
24
+ CONTROL_NAME,
25
+ CONTROL_NS,
26
+ ControlDemux,
27
+ ControlFrame,
28
+ ControlIngest,
29
+ ControlPlaneHost,
30
+ ControlSessionState,
31
+ ResumeWireLog,
32
+ XaiopControlError,
33
+ XaiopResumeLogError,
34
+ create_session_id,
35
+ dispatch_control_frame,
36
+ encode_ack_frame,
37
+ encode_control_frame,
38
+ encode_resume_frame,
39
+ encode_seq_frame,
40
+ encode_session_frame,
41
+ encode_snapshot_frame,
42
+ is_sdk_control_line,
43
+ parse_control_body_json,
44
+ parse_control_header,
45
+ stamp_wire_with_log_seq,
46
+ )
47
+ from .encode import DOT_POLICY, encode_sync
48
+ from .engine import PROTOCOL_VERSION, SDK_VERSION, XaiopEngine
49
+ from .errors import XaiopEncodeError, XaiopSyntaxError
50
+ from .fragment import XaiopFragment
51
+ from .history import HISTORY_NODE_KIND, ParseHistory, RangeError
52
+ from .label_escape import (
53
+ LABEL_ESCAPE_INTRODUCER,
54
+ decode_wire_label,
55
+ encode_wire_label,
56
+ key_needs_symbol_escape,
57
+ )
58
+ from .line_intercept import LINE_KIND, LineView, classify_line, run_line_intercept_chain
59
+ from .materialize import materialize, materialize_owned, materialize_snapshot
60
+ from .merge import (
61
+ MERGE_CONFLICT,
62
+ format_inject_result,
63
+ merge_json,
64
+ merge_to_json,
65
+ merge_to_xaiop,
66
+ to_mergeable_json,
67
+ )
68
+ from .modes import ALL_STREAM_MODES, STREAM_MODES, normalize_modes
69
+ from .parse import LiveParser, parse_sync
70
+ from .phase_encode import encode_phase_json, encode_phase_object
71
+ from .schedule import schedule_immediate
72
+ from .states import STREAM_IDLE_LIKE, STREAM_STATUS, is_stream_busy
73
+ from .stream import TRANSPORT_KIND, XaiopStream, chunks_of, open_transport
74
+ from .types import (
75
+ TYPE,
76
+ TYPE_SCHEMA_FRAME_PREFIX,
77
+ TypeChecker,
78
+ TypeFreezeSession,
79
+ TypeRegistry,
80
+ XaiopTypeError,
81
+ array_type,
82
+ canonicalize_type,
83
+ classify_value,
84
+ clone_type,
85
+ encode_type_schema_frame,
86
+ object_type,
87
+ parse_type_surface,
88
+ try_parse_type_schema_frame,
89
+ type_compatible,
90
+ type_to_string,
91
+ value_matches_type,
92
+ )
93
+ from .ws import XaiopWs, XaiopWsConnection, XaiopWsHub, listen as ws_listen
94
+
95
+ __version__ = "0.17.0"
96
+
97
+ __all__ = [
98
+ "COMPAT_FIX_DEFAULTS",
99
+ "COMPAT_FIX_IDS",
100
+ "CONTROL_CAPABILITY",
101
+ "CONTROL_NAME",
102
+ "CONTROL_NS",
103
+ "DOT_POLICY",
104
+ "HISTORY_NODE_KIND",
105
+ "LABEL_ESCAPE_INTRODUCER",
106
+ "LINE_KIND",
107
+ "MERGE_CONFLICT",
108
+ "PROTOCOL_VERSION",
109
+ "SDK_VERSION",
110
+ "TYPE",
111
+ "STREAM_MODES",
112
+ "STREAM_STATUS",
113
+ "STREAM_IDLE_LIKE",
114
+ "TRANSPORT_KIND",
115
+ "ALL_STREAM_MODES",
116
+ "BYTE_MODE",
117
+ "SINK_MODE",
118
+ "AnnotationSpan",
119
+ "AnnotationSpanView",
120
+ "CompatPolicy",
121
+ "ControlDemux",
122
+ "ControlFrame",
123
+ "ControlIngest",
124
+ "ControlPlaneHost",
125
+ "ControlSessionState",
126
+ "DotCheckpointEngine",
127
+ "LiveParser",
128
+ "LineView",
129
+ "ParseHistory",
130
+ "RangeError",
131
+ "ResumeWireLog",
132
+ "TypeChecker",
133
+ "TypeFreezeSession",
134
+ "TypeRegistry",
135
+ "XaiopByteError",
136
+ "XaiopByteParser",
137
+ "XaiopControlError",
138
+ "XaiopSink",
139
+ "XaiopEncodeError",
140
+ "TYPE_SCHEMA_FRAME_PREFIX",
141
+ "XaiopEngine",
142
+ "XaiopStream",
143
+ "XaiopWs",
144
+ "XaiopWsConnection",
145
+ "XaiopWsHub",
146
+ "XaiopFragment",
147
+ "XaiopResumeLogError",
148
+ "XaiopSyntaxError",
149
+ "XaiopTypeError",
150
+ "__version__",
151
+ "apply_annotation_spans",
152
+ "array_type",
153
+ "canonicalize_type",
154
+ "chunks_of",
155
+ "classify_line",
156
+ "classify_value",
157
+ "clone_json",
158
+ "clone_type",
159
+ "create_session_id",
160
+ "decode_wire_label",
161
+ "dispatch_control_frame",
162
+ "encode_ack_frame",
163
+ "encode_as_sibling_lines",
164
+ "encode_control_frame",
165
+ "encode_phase_json",
166
+ "encode_phase_object",
167
+ "encode_resume_frame",
168
+ "encode_seq_frame",
169
+ "encode_session_frame",
170
+ "encode_snapshot_frame",
171
+ "encode_sync",
172
+ "encode_type_schema_frame",
173
+ "encode_wire_label",
174
+ "format_inject_result",
175
+ "is_sdk_control_line",
176
+ "is_stream_busy",
177
+ "key_needs_symbol_escape",
178
+ "materialize",
179
+ "materialize_owned",
180
+ "materialize_snapshot",
181
+ "merge_json",
182
+ "merge_to_json",
183
+ "merge_to_xaiop",
184
+ "normalize_modes",
185
+ "object_type",
186
+ "open_transport",
187
+ "parse_control_body_json",
188
+ "parse_control_header",
189
+ "parse_sync",
190
+ "parse_type_surface",
191
+ "path_escapes_type_check",
192
+ "resolve_compat_options",
193
+ "run_line_intercept_chain",
194
+ "schedule_immediate",
195
+ "stamp_wire_with_log_seq",
196
+ "to_mergeable_json",
197
+ "try_parse_type_schema_frame",
198
+ "type_compatible",
199
+ "type_to_string",
200
+ "value_matches_type",
201
+ "ws_listen",
202
+ ]
@@ -0,0 +1,173 @@
1
+ """Package-private checkpoint helpers (Diff wire shaping · cover · line scan).
2
+
3
+ Kept out of :mod:`xaiop.checkpoint` so the engine class stays focused on buffer /
4
+ Commit / history wiring. Not part of the published API surface.
5
+ """
6
+
7
+ from __future__ import annotations
8
+
9
+ from typing import Any
10
+
11
+
12
+ def read_line(text: str, from_: int, at_eof: bool) -> dict[str, Any] | None:
13
+ if from_ >= len(text):
14
+ return None
15
+ i = from_
16
+ n = len(text)
17
+ while i < n:
18
+ if text[i] == "\n":
19
+ end = i
20
+ if end > from_ and text[end - 1] == "\r":
21
+ end -= 1
22
+ return {
23
+ "line": text[from_:end],
24
+ "end": i + 1,
25
+ "consumed_newline": True,
26
+ }
27
+ i += 1
28
+ if not at_eof:
29
+ return None
30
+ return {"line": text[from_:], "end": n, "consumed_newline": False}
31
+
32
+
33
+ def lines_to_wire(lines: list[str]) -> str:
34
+ if not lines:
35
+ return ""
36
+ return "\n".join(lines) + "\n"
37
+
38
+
39
+ def is_amp_line(line: str) -> bool:
40
+ return bool(line) and line[0] == "&"
41
+
42
+
43
+ def _split_path_segments(path: str) -> list[str]:
44
+ """Split ``a>b>c`` without ``str.split`` allocation of empty tails."""
45
+ segs: list[str] = []
46
+ start = 0
47
+ n = len(path)
48
+ for i in range(n):
49
+ if path[i] == ">":
50
+ if i > start:
51
+ segs.append(path[start:i])
52
+ start = i + 1
53
+ if start < n:
54
+ segs.append(path[start:])
55
+ return segs
56
+
57
+
58
+ def build_delete_tombstone(amps: list[str]) -> dict[str, Any]:
59
+ root: dict[str, Any] = {}
60
+ for line in amps:
61
+ path = line[1:]
62
+ segments = _split_path_segments(path)
63
+ if not segments:
64
+ continue
65
+ cur = root
66
+ for seg in segments[:-1]:
67
+ existing = cur.get(seg)
68
+ if existing is None or not isinstance(existing, dict):
69
+ cur[seg] = {}
70
+ cur = cur[seg]
71
+ cur[segments[-1]] = None
72
+ return root
73
+
74
+
75
+ def with_leading_dot(raw: str) -> str:
76
+ if raw == "." or raw.startswith(".\n") or raw.startswith(".\r\n"):
77
+ return raw
78
+ return raw if raw.startswith("\n") else f".\n{raw}"
79
+
80
+
81
+ def first_phase_line(raw: str) -> str | None:
82
+ i = 0
83
+ n = len(raw)
84
+ while i < n:
85
+ if raw[i] == "\r":
86
+ i += 1
87
+ continue
88
+ if raw[i] == "\n":
89
+ i += 1
90
+ continue
91
+ j = i
92
+ while j < n and raw[j] not in "\n\r":
93
+ j += 1
94
+ line = raw[i:j]
95
+ if line.endswith("\r"):
96
+ line = line[:-1]
97
+ if line in (".", ""):
98
+ i = j + 1
99
+ continue
100
+ return line
101
+ return None
102
+
103
+
104
+ def phase_has_bare_document_root(raw: str) -> bool:
105
+ return first_phase_line(raw) in (">", "-")
106
+
107
+
108
+ def ensure_diff_document_root(raw: str, root_kind: str | None) -> str:
109
+ if phase_has_bare_document_root(raw):
110
+ return raw
111
+ if root_kind == "array":
112
+ return raw
113
+ return f">\n{raw}"
114
+
115
+
116
+ def phase_needs_prior_tree(raw: str) -> bool:
117
+ i = 0
118
+ n = len(raw)
119
+ while i < n:
120
+ if raw[i] == "\r":
121
+ i += 1
122
+ continue
123
+ if raw[i] == "\n":
124
+ i += 1
125
+ continue
126
+ c = ord(raw[i])
127
+ if c in (61, 33, 38, 64, 63): # = ! & @ ?
128
+ return True
129
+ while i < n:
130
+ ch = raw[i]
131
+ if ch == "\n":
132
+ i += 1
133
+ break
134
+ if ch == "\r":
135
+ i += 1
136
+ if i < n and raw[i] == "\n":
137
+ i += 1
138
+ break
139
+ i += 1
140
+ return False
141
+
142
+
143
+ def is_empty_phase_wire(raw: str) -> bool:
144
+ start = 0
145
+ end = len(raw)
146
+ if start < end and raw[start] == ".":
147
+ start += 1
148
+ if start < end and raw[start] == "\r":
149
+ start += 1
150
+ if start < end and raw[start] == "\n":
151
+ start += 1
152
+ if end > start:
153
+ e = end
154
+ if e > start and raw[e - 1] == "\n":
155
+ e -= 1
156
+ if e > start and raw[e - 1] == "\r":
157
+ e -= 1
158
+ if e > start and raw[e - 1] == ".":
159
+ e -= 1
160
+ if e > start and raw[e - 1] == "\n":
161
+ e -= 1
162
+ if e > start and raw[e - 1] == "\r":
163
+ e -= 1
164
+ end = e
165
+ while start < end and raw[start] in " \t\n\r":
166
+ start += 1
167
+ while end > start and raw[end - 1] in " \t\n\r":
168
+ end -= 1
169
+ return start >= end
170
+
171
+
172
+ def normalize_empty_phase(raw: str, value: Any) -> Any:
173
+ return None if is_empty_phase_wire(raw) else value