python-hwpx 3.0.0__py3-none-any.whl → 3.2.0__py3-none-any.whl
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.
- hwpx/agent/__init__.py +26 -0
- hwpx/agent/catalog.py +85 -0
- hwpx/agent/commands.py +173 -9
- hwpx/agent/document.py +36 -4
- hwpx/agent/form_plan.py +1685 -0
- hwpx/agent/model.py +24 -2
- hwpx/agent/story.py +207 -0
- hwpx/document.py +22 -11
- hwpx/oxml/_document_impl.py +108 -6824
- hwpx/oxml/_document_primitives.py +610 -0
- hwpx/oxml/document.py +3 -1
- hwpx/oxml/document_parts.py +1093 -0
- hwpx/oxml/header_part.py +1323 -5
- hwpx/oxml/memo.py +284 -5
- hwpx/oxml/numbering.py +24 -2
- hwpx/oxml/objects.py +464 -2
- hwpx/oxml/paragraph.py +913 -5
- hwpx/oxml/run.py +444 -2
- hwpx/oxml/section.py +316 -5
- hwpx/oxml/section_format.py +513 -0
- hwpx/oxml/section_story.py +586 -0
- hwpx/oxml/simple_parts.py +63 -2
- hwpx/oxml/table.py +988 -5
- hwpx/table_patch.py +125 -8
- hwpx/tools/package_validator.py +83 -16
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/METADATA +17 -5
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/RECORD +32 -26
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/WHEEL +0 -0
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/entry_points.txt +0 -0
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/licenses/LICENSE +0 -0
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/licenses/NOTICE +0 -0
- {python_hwpx-3.0.0.dist-info → python_hwpx-3.2.0.dist-info}/top_level.txt +0 -0
hwpx/agent/__init__.py
CHANGED
|
@@ -18,6 +18,20 @@ from .model import (
|
|
|
18
18
|
from .catalog import agent_catalog, agent_json_schemas, catalog_hash, human_help, node_help
|
|
19
19
|
from .commands import apply_document_commands
|
|
20
20
|
from .document import HwpxAgentDocument, NodeRecord
|
|
21
|
+
from .form_plan import (
|
|
22
|
+
MIXED_FORM_COMPILED_PLAN_SCHEMA,
|
|
23
|
+
MIXED_FORM_LOCATOR_KINDS,
|
|
24
|
+
MIXED_FORM_PLAN_SCHEMA,
|
|
25
|
+
MIXED_FORM_REQUEST_SCHEMA,
|
|
26
|
+
MixedFormPlan,
|
|
27
|
+
MixedFormResolution,
|
|
28
|
+
apply_mixed_form_fill,
|
|
29
|
+
apply_mixed_form_plan,
|
|
30
|
+
mixed_form_json_schemas,
|
|
31
|
+
plan_mixed_form_fill,
|
|
32
|
+
validate_mixed_form_plan,
|
|
33
|
+
validate_mixed_form_request,
|
|
34
|
+
)
|
|
21
35
|
from .path import PathSegment, SemanticPath, canonicalize_path, parse_path
|
|
22
36
|
from .query import QueryResult, SemanticSelector, parse_selector
|
|
23
37
|
from .blueprint import (
|
|
@@ -52,6 +66,10 @@ __all__ = [
|
|
|
52
66
|
"AGENT_COMMAND_SCHEMA",
|
|
53
67
|
"AGENT_ERROR_SCHEMA",
|
|
54
68
|
"AGENT_NODE_SCHEMA",
|
|
69
|
+
"MIXED_FORM_COMPILED_PLAN_SCHEMA",
|
|
70
|
+
"MIXED_FORM_LOCATOR_KINDS",
|
|
71
|
+
"MIXED_FORM_PLAN_SCHEMA",
|
|
72
|
+
"MIXED_FORM_REQUEST_SCHEMA",
|
|
55
73
|
"BLUEPRINT_CATALOG_SCHEMA",
|
|
56
74
|
"BLUEPRINT_REPLAY_RESULT_SCHEMA",
|
|
57
75
|
"BLUEPRINT_REPLAY_SCHEMA",
|
|
@@ -65,6 +83,8 @@ __all__ = [
|
|
|
65
83
|
"BlueprintBundle",
|
|
66
84
|
"BlueprintDumpResult",
|
|
67
85
|
"HwpxAgentDocument",
|
|
86
|
+
"MixedFormPlan",
|
|
87
|
+
"MixedFormResolution",
|
|
68
88
|
"NodeRecord",
|
|
69
89
|
"PathSegment",
|
|
70
90
|
"QueryResult",
|
|
@@ -74,6 +94,8 @@ __all__ = [
|
|
|
74
94
|
"agent_json_schemas",
|
|
75
95
|
"agent_contract_manifest",
|
|
76
96
|
"apply_document_commands",
|
|
97
|
+
"apply_mixed_form_fill",
|
|
98
|
+
"apply_mixed_form_plan",
|
|
77
99
|
"blueprint_catalog",
|
|
78
100
|
"blueprint_catalog_hash",
|
|
79
101
|
"blueprint_hash",
|
|
@@ -84,15 +106,19 @@ __all__ = [
|
|
|
84
106
|
"canonicalize_path",
|
|
85
107
|
"catalog_hash",
|
|
86
108
|
"human_help",
|
|
109
|
+
"mixed_form_json_schemas",
|
|
87
110
|
"node_help",
|
|
88
111
|
"parse_path",
|
|
89
112
|
"parse_selector",
|
|
113
|
+
"plan_mixed_form_fill",
|
|
90
114
|
"dump_document_blueprint",
|
|
91
115
|
"read_blueprint_bundle",
|
|
92
116
|
"repack_blueprint_bundle",
|
|
93
117
|
"replay_document_blueprint",
|
|
94
118
|
"validate_agent_batch",
|
|
95
119
|
"validate_agent_command",
|
|
120
|
+
"validate_mixed_form_plan",
|
|
121
|
+
"validate_mixed_form_request",
|
|
96
122
|
"validate_blueprint_manifest",
|
|
97
123
|
"validate_replay_request",
|
|
98
124
|
"write_blueprint_bundle",
|
hwpx/agent/catalog.py
CHANGED
|
@@ -9,10 +9,16 @@ from copy import deepcopy
|
|
|
9
9
|
from typing import Any
|
|
10
10
|
|
|
11
11
|
from .model import (
|
|
12
|
+
AGENT_BATCH_SCHEMA,
|
|
12
13
|
AGENT_CATALOG_SCHEMA,
|
|
14
|
+
MAX_COMMANDS,
|
|
13
15
|
AgentContractError,
|
|
14
16
|
NODE_KINDS,
|
|
15
17
|
NODE_PROPERTY_CATALOG_V1,
|
|
18
|
+
QUALITY_KEYS,
|
|
19
|
+
QUALITY_MODES,
|
|
20
|
+
REVISION_PATTERN,
|
|
21
|
+
VERIFICATION_REQUIREMENTS,
|
|
16
22
|
agent_contract_manifest,
|
|
17
23
|
)
|
|
18
24
|
|
|
@@ -78,6 +84,22 @@ def agent_json_schemas() -> dict[str, Any]:
|
|
|
78
84
|
"properties": {key: value for key, value in properties.items() if key in allowed},
|
|
79
85
|
}
|
|
80
86
|
)
|
|
87
|
+
quality_object = {
|
|
88
|
+
"type": "object",
|
|
89
|
+
"additionalProperties": False,
|
|
90
|
+
"properties": {
|
|
91
|
+
"mode": {"enum": list(QUALITY_MODES)},
|
|
92
|
+
"renderCheck": {"enum": ["off", "auto", "required"]},
|
|
93
|
+
"xsdMode": {"enum": ["off", "lint"]},
|
|
94
|
+
"overflowPolicy": {"enum": ["fail", "warn", "truncate"]},
|
|
95
|
+
"layoutLint": {"enum": ["off", "warn", "strict"]},
|
|
96
|
+
"preserveUnmodifiedParts": {"type": "boolean"},
|
|
97
|
+
"requireReferenceIntegrity": {"type": "boolean"},
|
|
98
|
+
},
|
|
99
|
+
}
|
|
100
|
+
if set(quality_object["properties"]) != set(QUALITY_KEYS): # pragma: no cover
|
|
101
|
+
raise AssertionError("quality JSON Schema drifted from QUALITY_KEYS")
|
|
102
|
+
|
|
81
103
|
return {
|
|
82
104
|
"node": {
|
|
83
105
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
@@ -113,6 +135,69 @@ def agent_json_schemas() -> dict[str, Any]:
|
|
|
113
135
|
"title": "HwpxAgentCommand v1",
|
|
114
136
|
"oneOf": command_variants,
|
|
115
137
|
},
|
|
138
|
+
"batch": {
|
|
139
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
140
|
+
"title": "HwpxAgentBatch v1",
|
|
141
|
+
"type": "object",
|
|
142
|
+
"required": [
|
|
143
|
+
"schemaVersion",
|
|
144
|
+
"input",
|
|
145
|
+
"output",
|
|
146
|
+
"commands",
|
|
147
|
+
"expectedRevision",
|
|
148
|
+
"idempotencyKey",
|
|
149
|
+
"dryRun",
|
|
150
|
+
"quality",
|
|
151
|
+
"verificationRequirements",
|
|
152
|
+
],
|
|
153
|
+
"additionalProperties": False,
|
|
154
|
+
"properties": {
|
|
155
|
+
"schemaVersion": {"const": AGENT_BATCH_SCHEMA},
|
|
156
|
+
"input": {
|
|
157
|
+
"type": "object",
|
|
158
|
+
"required": ["filename"],
|
|
159
|
+
"additionalProperties": False,
|
|
160
|
+
"properties": {"filename": {"type": "string", "minLength": 1}},
|
|
161
|
+
},
|
|
162
|
+
"output": {
|
|
163
|
+
"type": "object",
|
|
164
|
+
"required": ["filename", "overwrite"],
|
|
165
|
+
"additionalProperties": False,
|
|
166
|
+
"properties": {
|
|
167
|
+
"filename": {"type": "string", "minLength": 1},
|
|
168
|
+
"overwrite": {"type": "boolean"},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
"commands": {
|
|
172
|
+
"type": "array",
|
|
173
|
+
"minItems": 1,
|
|
174
|
+
"maxItems": MAX_COMMANDS,
|
|
175
|
+
"items": {"oneOf": deepcopy(command_variants)},
|
|
176
|
+
},
|
|
177
|
+
"expectedRevision": {
|
|
178
|
+
"type": ["string", "null"],
|
|
179
|
+
"pattern": REVISION_PATTERN.pattern,
|
|
180
|
+
},
|
|
181
|
+
"idempotencyKey": {
|
|
182
|
+
"type": ["string", "null"],
|
|
183
|
+
"minLength": 1,
|
|
184
|
+
"maxLength": 128,
|
|
185
|
+
},
|
|
186
|
+
"dryRun": {"type": "boolean"},
|
|
187
|
+
"quality": {
|
|
188
|
+
"oneOf": [
|
|
189
|
+
{"enum": list(QUALITY_MODES)},
|
|
190
|
+
deepcopy(quality_object),
|
|
191
|
+
{"type": "null"},
|
|
192
|
+
]
|
|
193
|
+
},
|
|
194
|
+
"verificationRequirements": {
|
|
195
|
+
"type": "array",
|
|
196
|
+
"items": {"enum": list(VERIFICATION_REQUIREMENTS)},
|
|
197
|
+
"uniqueItems": True,
|
|
198
|
+
},
|
|
199
|
+
},
|
|
200
|
+
},
|
|
116
201
|
"queryInput": {
|
|
117
202
|
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
118
203
|
"type": "object",
|
hwpx/agent/commands.py
CHANGED
|
@@ -21,7 +21,7 @@ from pathlib import Path
|
|
|
21
21
|
from typing import Any
|
|
22
22
|
from xml.etree import ElementTree as ET
|
|
23
23
|
|
|
24
|
-
from lxml import etree as LET
|
|
24
|
+
from lxml import etree as LET # type: ignore[reportAttributeAccessIssue] # lxml has no complete bundled typing
|
|
25
25
|
|
|
26
26
|
from hwpx.document import HwpxDocument
|
|
27
27
|
from hwpx.oxml import HwpxOxmlTable
|
|
@@ -38,6 +38,12 @@ from .model import (
|
|
|
38
38
|
validate_agent_batch,
|
|
39
39
|
)
|
|
40
40
|
from .path import parse_path
|
|
41
|
+
from .story import (
|
|
42
|
+
HEADER_STORY_EDITABLE_PROPERTIES,
|
|
43
|
+
HEADER_STORY_KIND,
|
|
44
|
+
HeaderStoryBinding,
|
|
45
|
+
try_parse_header_story_path,
|
|
46
|
+
)
|
|
41
47
|
|
|
42
48
|
_EMPTY_REVISION = "sha256:" + hashlib.sha256(b"").hexdigest()
|
|
43
49
|
_HWP_UNITS_PER_MM = 7200 / 25.4
|
|
@@ -242,6 +248,21 @@ def _validate_property_values(kind: str, properties: Mapping[str, Any], *, creat
|
|
|
242
248
|
raise AgentContractError("unknown_property", f"untyped property: {target}", target=target)
|
|
243
249
|
|
|
244
250
|
|
|
251
|
+
def _validate_header_story_properties(properties: Mapping[str, Any]) -> None:
|
|
252
|
+
unknown = sorted(set(properties) - HEADER_STORY_EDITABLE_PROPERTIES)
|
|
253
|
+
if unknown:
|
|
254
|
+
raise AgentContractError(
|
|
255
|
+
"unknown_property",
|
|
256
|
+
f"header does not support properties: {unknown}",
|
|
257
|
+
target=f"header.{unknown[0]}",
|
|
258
|
+
)
|
|
259
|
+
if "text" not in properties:
|
|
260
|
+
raise AgentContractError(
|
|
261
|
+
"invalid_syntax", "existing header set requires text", target="header.text"
|
|
262
|
+
)
|
|
263
|
+
_require_string(properties["text"], "header.text")
|
|
264
|
+
|
|
265
|
+
|
|
245
266
|
def _preflight(batch: Mapping[str, Any]) -> None:
|
|
246
267
|
"""Reject the complete static command matrix before the first mutation."""
|
|
247
268
|
|
|
@@ -254,6 +275,9 @@ def _preflight(batch: Mapping[str, Any]) -> None:
|
|
|
254
275
|
return alias_kinds[command_id][field]
|
|
255
276
|
except KeyError as exc: # validate_agent_batch already checks ordering
|
|
256
277
|
raise AgentContractError("not_found", f"unknown command alias: {value}") from exc
|
|
278
|
+
story = try_parse_header_story_path(value)
|
|
279
|
+
if story is not None:
|
|
280
|
+
return story.kind
|
|
257
281
|
parsed = parse_path(value)
|
|
258
282
|
return parsed.segments[-1].kind if parsed.segments else "document"
|
|
259
283
|
|
|
@@ -264,6 +288,9 @@ def _preflight(batch: Mapping[str, Any]) -> None:
|
|
|
264
288
|
return alias_kinds[command_id]["parentPath" if field == "path" else field]
|
|
265
289
|
except KeyError:
|
|
266
290
|
return "document"
|
|
291
|
+
story = try_parse_header_story_path(value)
|
|
292
|
+
if story is not None:
|
|
293
|
+
return "section"
|
|
267
294
|
parsed = parse_path(value)
|
|
268
295
|
return parsed.segments[-2].kind if len(parsed.segments) > 1 else "document"
|
|
269
296
|
|
|
@@ -298,6 +325,19 @@ def _preflight(batch: Mapping[str, Any]) -> None:
|
|
|
298
325
|
}
|
|
299
326
|
continue
|
|
300
327
|
source_kind = reference_kind(command["path"])
|
|
328
|
+
if source_kind == HEADER_STORY_KIND:
|
|
329
|
+
if op != "set":
|
|
330
|
+
raise AgentContractError(
|
|
331
|
+
"unsupported_operation",
|
|
332
|
+
f"{op} is unsupported for {source_kind}",
|
|
333
|
+
target=command["commandId"],
|
|
334
|
+
)
|
|
335
|
+
_validate_header_story_properties(command["properties"])
|
|
336
|
+
alias_kinds[command["commandId"]] = {
|
|
337
|
+
"path": source_kind,
|
|
338
|
+
"parentPath": "section",
|
|
339
|
+
}
|
|
340
|
+
continue
|
|
301
341
|
if op not in NODE_PROPERTY_CATALOG_V1[source_kind]["operations"]:
|
|
302
342
|
raise AgentContractError(
|
|
303
343
|
"unsupported_operation",
|
|
@@ -309,11 +349,11 @@ def _preflight(batch: Mapping[str, Any]) -> None:
|
|
|
309
349
|
destination_kind = parent_kind(command["path"])
|
|
310
350
|
if op in {"move", "copy"}:
|
|
311
351
|
destination_kind = reference_kind(command["parent"])
|
|
312
|
-
|
|
313
|
-
if destination_kind !=
|
|
352
|
+
move_parent = _MOVE_COPY_PARENTS.get(source_kind)
|
|
353
|
+
if destination_kind != move_parent:
|
|
314
354
|
raise AgentContractError(
|
|
315
355
|
"incompatible_parent",
|
|
316
|
-
f"{source_kind} requires a {
|
|
356
|
+
f"{source_kind} requires a {move_parent} parent, not {destination_kind}",
|
|
317
357
|
target=command["commandId"],
|
|
318
358
|
)
|
|
319
359
|
alias_kinds[command["commandId"]] = {
|
|
@@ -570,6 +610,42 @@ def _mark_containing_section(document: HwpxDocument, target: Any) -> None:
|
|
|
570
610
|
raise AgentContractError("not_found", "mutated element is detached")
|
|
571
611
|
|
|
572
612
|
|
|
613
|
+
def _apply_header_story_set(
|
|
614
|
+
binding: HeaderStoryBinding, properties: Mapping[str, Any]
|
|
615
|
+
) -> dict[str, Any]:
|
|
616
|
+
"""Apply the bounded existing-header mutation through its OXML owner."""
|
|
617
|
+
|
|
618
|
+
_validate_header_story_properties(properties)
|
|
619
|
+
try:
|
|
620
|
+
binding.native.set_simple_text_preserving(properties["text"])
|
|
621
|
+
except AgentContractError:
|
|
622
|
+
raise
|
|
623
|
+
except ValueError as exc:
|
|
624
|
+
# The OXML owner uses ValueError for rich/control-bearing or otherwise
|
|
625
|
+
# structurally unsafe headers. Do not let it degrade to the generic
|
|
626
|
+
# invariant envelope or fall back to the destructive whole-story setter.
|
|
627
|
+
message = str(exc) or "header content cannot be edited losslessly"
|
|
628
|
+
lowered = message.lower()
|
|
629
|
+
code = (
|
|
630
|
+
"ambiguous_target"
|
|
631
|
+
if "ambiguous" in lowered
|
|
632
|
+
else "not_found"
|
|
633
|
+
if "not found" in lowered
|
|
634
|
+
else "unsupported_content"
|
|
635
|
+
)
|
|
636
|
+
raise AgentContractError(
|
|
637
|
+
code,
|
|
638
|
+
message,
|
|
639
|
+
target=binding.path,
|
|
640
|
+
) from exc
|
|
641
|
+
return {
|
|
642
|
+
"text": {
|
|
643
|
+
"before": binding.text,
|
|
644
|
+
"after": properties["text"],
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
|
|
573
649
|
def _apply_set(document: HwpxDocument, record: NodeRecord, properties: Mapping[str, Any]) -> dict[str, Any]:
|
|
574
650
|
_ensure_operation(record, "set")
|
|
575
651
|
_validate_property_values(record.kind, properties, creating=False)
|
|
@@ -711,7 +787,10 @@ def _add(
|
|
|
711
787
|
if "pageHeightMm" in properties:
|
|
712
788
|
size_kwargs["height"] = round(properties["pageHeightMm"] * _HWP_UNITS_PER_MM)
|
|
713
789
|
if size_kwargs:
|
|
714
|
-
created.properties.set_page_size(
|
|
790
|
+
created.properties.set_page_size(
|
|
791
|
+
width=size_kwargs.get("width"),
|
|
792
|
+
height=size_kwargs.get("height"),
|
|
793
|
+
)
|
|
715
794
|
return created
|
|
716
795
|
if kind == "paragraph":
|
|
717
796
|
created = parent.native.add_paragraph(str(properties.get("text", "")))
|
|
@@ -1081,6 +1160,47 @@ def _member_diff(before: bytes, after: bytes) -> dict[str, Any]:
|
|
|
1081
1160
|
return {"ok": False, "error": f"{type(exc).__name__}: {exc}"}
|
|
1082
1161
|
|
|
1083
1162
|
|
|
1163
|
+
def _verify_header_story_candidates(
|
|
1164
|
+
candidate_data: bytes,
|
|
1165
|
+
candidate_revision: str,
|
|
1166
|
+
expectations: Mapping[str, Mapping[str, str]],
|
|
1167
|
+
) -> dict[str, Any]:
|
|
1168
|
+
receipts: list[dict[str, Any]] = []
|
|
1169
|
+
if expectations:
|
|
1170
|
+
with HwpxDocument.open(candidate_data) as reopened:
|
|
1171
|
+
view = HwpxAgentDocument.from_document(
|
|
1172
|
+
reopened, revision=candidate_revision
|
|
1173
|
+
)
|
|
1174
|
+
for expectation in expectations.values():
|
|
1175
|
+
path = expectation["path"]
|
|
1176
|
+
binding = view._resolve_header_story(path)
|
|
1177
|
+
if (
|
|
1178
|
+
binding.stable_id != expectation["stableId"]
|
|
1179
|
+
or binding.page_type != expectation["pageType"]
|
|
1180
|
+
or binding.text != expectation["text"]
|
|
1181
|
+
):
|
|
1182
|
+
raise AgentContractError(
|
|
1183
|
+
"verification_failed",
|
|
1184
|
+
"reopened header story does not match the committed binding",
|
|
1185
|
+
target=path,
|
|
1186
|
+
)
|
|
1187
|
+
receipts.append(
|
|
1188
|
+
{
|
|
1189
|
+
"commandId": expectation["commandId"],
|
|
1190
|
+
"path": binding.path,
|
|
1191
|
+
"stableId": binding.stable_id,
|
|
1192
|
+
"pageType": binding.page_type,
|
|
1193
|
+
"textMatched": True,
|
|
1194
|
+
}
|
|
1195
|
+
)
|
|
1196
|
+
return {
|
|
1197
|
+
"schemaVersion": "hwpx.agent-story-preservation/v1",
|
|
1198
|
+
"ok": True,
|
|
1199
|
+
"storyCount": len(receipts),
|
|
1200
|
+
"stories": receipts,
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1203
|
+
|
|
1084
1204
|
def _request_hash(batch: Mapping[str, Any]) -> str:
|
|
1085
1205
|
payload = json.dumps(
|
|
1086
1206
|
batch,
|
|
@@ -1192,6 +1312,7 @@ def apply_document_commands(
|
|
|
1192
1312
|
aliases: dict[str, dict[str, str]] = {}
|
|
1193
1313
|
identity_changes: list[Mapping[str, str]] = []
|
|
1194
1314
|
semantic_changes: list[Mapping[str, Any]] = []
|
|
1315
|
+
story_expectations: dict[str, Mapping[str, str]] = {}
|
|
1195
1316
|
_call_fault(fault_injector, "before_open")
|
|
1196
1317
|
with HwpxDocument.open(input_data) as document:
|
|
1197
1318
|
view = HwpxAgentDocument.from_document(document, revision=input_revision)
|
|
@@ -1203,11 +1324,24 @@ def apply_document_commands(
|
|
|
1203
1324
|
parent_path: str | None = None
|
|
1204
1325
|
changed: dict[str, Any] = {}
|
|
1205
1326
|
generated: list[dict[str, str]] = []
|
|
1327
|
+
target_native: Any | None = None
|
|
1328
|
+
story_before: HeaderStoryBinding | None = None
|
|
1206
1329
|
if op == "set":
|
|
1207
1330
|
resolved_path = _resolve_alias(command["path"], aliases)
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1331
|
+
story_path = try_parse_header_story_path(resolved_path)
|
|
1332
|
+
if story_path is not None:
|
|
1333
|
+
story_before = view._resolve_header_story(
|
|
1334
|
+
story_path, expected_revision=input_revision
|
|
1335
|
+
)
|
|
1336
|
+
changed = _apply_header_story_set(
|
|
1337
|
+
story_before, command["properties"]
|
|
1338
|
+
)
|
|
1339
|
+
else:
|
|
1340
|
+
record = view.resolve_record(
|
|
1341
|
+
resolved_path, expected_revision=input_revision
|
|
1342
|
+
)
|
|
1343
|
+
changed = _apply_set(document, record, command["properties"])
|
|
1344
|
+
target_native = record.native
|
|
1211
1345
|
elif op == "add":
|
|
1212
1346
|
parent_path = _resolve_alias(command["parent"], aliases)
|
|
1213
1347
|
parent = view.resolve_record(parent_path, expected_revision=input_revision)
|
|
@@ -1244,7 +1378,26 @@ def apply_document_commands(
|
|
|
1244
1378
|
|
|
1245
1379
|
_call_fault(fault_injector, "after_command", index)
|
|
1246
1380
|
view = HwpxAgentDocument.from_document(document, revision=input_revision)
|
|
1247
|
-
|
|
1381
|
+
result_stable_id: str | None = None
|
|
1382
|
+
if story_before is not None and resolved_path is not None:
|
|
1383
|
+
target_story = view._resolve_header_story(resolved_path)
|
|
1384
|
+
if target_story.stable_id != story_before.stable_id:
|
|
1385
|
+
raise AgentContractError(
|
|
1386
|
+
"invariant_violation",
|
|
1387
|
+
"header story identity changed during text edit",
|
|
1388
|
+
target=resolved_path,
|
|
1389
|
+
)
|
|
1390
|
+
result_path = target_story.path
|
|
1391
|
+
result_parent = target_story.parent_path
|
|
1392
|
+
result_stable_id = target_story.stable_id
|
|
1393
|
+
story_expectations[target_story.binding_key] = {
|
|
1394
|
+
"commandId": command_id,
|
|
1395
|
+
"path": target_story.path,
|
|
1396
|
+
"stableId": target_story.stable_id,
|
|
1397
|
+
"pageType": target_story.page_type,
|
|
1398
|
+
"text": command["properties"]["text"],
|
|
1399
|
+
}
|
|
1400
|
+
elif op == "set" and resolved_path is not None:
|
|
1248
1401
|
# Some native bindings (notably form fields) are request-local
|
|
1249
1402
|
# match dictionaries. A property edit is non-structural, so
|
|
1250
1403
|
# its canonical path is the durable post-edit lookup key.
|
|
@@ -1269,6 +1422,11 @@ def apply_document_commands(
|
|
|
1269
1422
|
"generatedIdentities": generated,
|
|
1270
1423
|
"warnings": [],
|
|
1271
1424
|
}
|
|
1425
|
+
if result_stable_id is not None:
|
|
1426
|
+
# Command results are already an untyped JSON mapping. This
|
|
1427
|
+
# story-only receipt carries the actual stable identity
|
|
1428
|
+
# without changing AgentBatchResult or the ToolSpec schema.
|
|
1429
|
+
result["stableId"] = result_stable_id
|
|
1272
1430
|
command_results.append(result)
|
|
1273
1431
|
semantic_changes.append(
|
|
1274
1432
|
{
|
|
@@ -1285,6 +1443,12 @@ def apply_document_commands(
|
|
|
1285
1443
|
_call_fault(fault_injector, "after_serialize")
|
|
1286
1444
|
|
|
1287
1445
|
candidate_revision = _revision(candidate_data)
|
|
1446
|
+
if story_expectations:
|
|
1447
|
+
verification["storyPreservation"] = _verify_header_story_candidates(
|
|
1448
|
+
candidate_data,
|
|
1449
|
+
candidate_revision,
|
|
1450
|
+
story_expectations,
|
|
1451
|
+
)
|
|
1288
1452
|
semantic_diff = {
|
|
1289
1453
|
"schemaVersion": "hwpx.agent-semantic-diff/v1",
|
|
1290
1454
|
"inputRevision": input_revision,
|
hwpx/agent/document.py
CHANGED
|
@@ -8,7 +8,7 @@ from collections import Counter, defaultdict
|
|
|
8
8
|
from dataclasses import dataclass, field
|
|
9
9
|
from os import PathLike
|
|
10
10
|
from pathlib import Path
|
|
11
|
-
from typing import Any
|
|
11
|
+
from typing import Any, Sequence, cast
|
|
12
12
|
|
|
13
13
|
from hwpx.document import HwpxDocument
|
|
14
14
|
from hwpx.oxml import HwpxOxmlShape
|
|
@@ -22,7 +22,14 @@ from .model import (
|
|
|
22
22
|
NODE_PROPERTY_CATALOG_V1,
|
|
23
23
|
)
|
|
24
24
|
from .path import SemanticPath, canonicalize_path, identified_segment, indexed_segment
|
|
25
|
+
from .query import QueryRecord as _QueryRecord
|
|
25
26
|
from .query import QueryResult, evaluate_selector, parse_selector
|
|
27
|
+
from .story import (
|
|
28
|
+
HeaderStoryBinding,
|
|
29
|
+
HeaderStoryPath,
|
|
30
|
+
parse_header_story_path,
|
|
31
|
+
resolve_header_story,
|
|
32
|
+
)
|
|
26
33
|
|
|
27
34
|
_HWP_UNITS_PER_MM = 7200 / 25.4
|
|
28
35
|
_SHAPE_KINDS = frozenset({"line", "rect", "ellipse", "arc", "polygon", "curve", "connectLine"})
|
|
@@ -129,7 +136,7 @@ class HwpxAgentDocument:
|
|
|
129
136
|
def __enter__(self) -> "HwpxAgentDocument":
|
|
130
137
|
return self
|
|
131
138
|
|
|
132
|
-
def __exit__(self, exc_type: Any, exc: Any, tb: Any) -> bool:
|
|
139
|
+
def __exit__(self, exc_type: Any, exc: Any, tb: Any) -> bool: # type: ignore[exit-return] # frozen public signature
|
|
133
140
|
self.close()
|
|
134
141
|
return False
|
|
135
142
|
|
|
@@ -689,6 +696,25 @@ class HwpxAgentDocument:
|
|
|
689
696
|
raise AgentContractError("volatile_target", "target has a positional path", target=canonical)
|
|
690
697
|
return record
|
|
691
698
|
|
|
699
|
+
def _resolve_header_story(
|
|
700
|
+
self,
|
|
701
|
+
path: str | HeaderStoryPath,
|
|
702
|
+
*,
|
|
703
|
+
expected_revision: str | None = None,
|
|
704
|
+
) -> HeaderStoryBinding:
|
|
705
|
+
"""Resolve the private command-only existing-header seam.
|
|
706
|
+
|
|
707
|
+
Header stories deliberately remain absent from ``records``, ``get``,
|
|
708
|
+
and ``query`` so the frozen public projection/catalog does not drift.
|
|
709
|
+
"""
|
|
710
|
+
|
|
711
|
+
parsed = parse_header_story_path(path) if isinstance(path, str) else path
|
|
712
|
+
if expected_revision is not None and expected_revision != self.revision:
|
|
713
|
+
raise AgentContractError(
|
|
714
|
+
"stale_revision", "document revision does not match", target=parsed.canonical
|
|
715
|
+
)
|
|
716
|
+
return resolve_header_story(self.document, parsed)
|
|
717
|
+
|
|
692
718
|
def _public_node(self, record: NodeRecord, *, depth: int, child_limit: int) -> AgentNode:
|
|
693
719
|
supported_total = len(record.child_paths)
|
|
694
720
|
selected_paths = record.child_paths[:child_limit] if depth > 0 else []
|
|
@@ -751,7 +777,8 @@ class HwpxAgentDocument:
|
|
|
751
777
|
if expected_revision is not None and expected_revision != self.revision:
|
|
752
778
|
raise AgentContractError("stale_revision", "document revision does not match", target="selector")
|
|
753
779
|
parsed = parse_selector(selector)
|
|
754
|
-
|
|
780
|
+
query_records = cast(Sequence[_QueryRecord], self.records)
|
|
781
|
+
matches, truncated = evaluate_selector(query_records, parsed, limit=limit)
|
|
755
782
|
if not 0 <= node_depth <= MAX_VIEW_DEPTH:
|
|
756
783
|
raise AgentContractError("resource_limit", "nodeDepth is out of bounds", target="nodeDepth")
|
|
757
784
|
if not 1 <= child_limit <= MAX_CHILDREN_PER_NODE:
|
|
@@ -760,7 +787,12 @@ class HwpxAgentDocument:
|
|
|
760
787
|
selector=selector,
|
|
761
788
|
revision=self.revision,
|
|
762
789
|
nodes=tuple(
|
|
763
|
-
self._public_node(
|
|
790
|
+
self._public_node(
|
|
791
|
+
cast(NodeRecord, record),
|
|
792
|
+
depth=node_depth,
|
|
793
|
+
child_limit=child_limit,
|
|
794
|
+
)
|
|
795
|
+
for record in matches
|
|
764
796
|
),
|
|
765
797
|
truncated=truncated,
|
|
766
798
|
)
|