mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
fix(desktop): bound exported XML selector traversal (#12105)
This commit is contained in:
1 parent
e3713bea60
commit
c6033aaaca
2 files changed
+69
-26
No files matched your search
@@ -6,15 +6,15 @@ export const PYTHON_POINTER = String.raw`def _is_element(value) -> bool:
|
||||
and isinstance(value.get("children"), list))
|
||||
|
||||
|
||||
def _element_text(element, attribute=None) -> str:
|
||||
def _element_text(element, attribute=None, omit_stamps=False) -> str:
|
||||
"""An element's text as XPath's string() reads it from what the view keeps: every text node among its children
|
||||
and its descendants', in document order. With attribute, the values of that attribute on it and its descendants
|
||||
instead, in document order, and no text node."""
|
||||
instead, in document order, and no text node. With omit_stamps, skip text nodes recognized as save times."""
|
||||
parts, pending = [], [element]
|
||||
while pending:
|
||||
node = pending.pop()
|
||||
if isinstance(node, str):
|
||||
if attribute is None:
|
||||
if attribute is None and (not omit_stamps or not _is_saved_time(node)):
|
||||
parts.append(node)
|
||||
elif _is_element(node):
|
||||
if attribute is not None and isinstance(node["attributes"].get(attribute), str):
|
||||
@@ -59,11 +59,7 @@ def _is_saved_time(text: str) -> bool:
|
||||
|
||||
def _text_without_stamps(node) -> str:
|
||||
"""An element's text with save-time leaves removed, matching the selector that named it."""
|
||||
if isinstance(node, str):
|
||||
return "" if _is_saved_time(node) else node
|
||||
if not _is_element(node):
|
||||
return ""
|
||||
return "".join(_text_without_stamps(child) for child in node["children"])
|
||||
return _element_text(node, omit_stamps=True)
|
||||
|
||||
|
||||
def _condition_text(child) -> str:
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
CHECK_VIEWS,
|
||||
} from "../../../src/domain/desktop/appTaskChecks.fixture.js";
|
||||
import { xmlView } from "../../../src/domain/desktop/stateFormats.js";
|
||||
import { desktopViewPolicy } from "../../../src/domain/desktop/desktopViewPolicy.js";
|
||||
import {
|
||||
draftOf,
|
||||
statePath,
|
||||
@@ -103,8 +104,10 @@ async def get_data(url, **_):
|
||||
reads.append(clock[0])
|
||||
clock[0] += durations[len(reads) - 1] if len(reads) <= len(durations) else READ_S
|
||||
views = views_at(clock[0])
|
||||
file_contents = scenario.get("file_contents") or {}
|
||||
files = [types.SimpleNamespace(kind="file", metadata={"urn:agentenv:data-objects/v1": {"path": path}},
|
||||
file=types.SimpleNamespace(bytes=base64.b64encode(json.dumps(view).encode()).decode()))
|
||||
file=types.SimpleNamespace(bytes=base64.b64encode(
|
||||
(file_contents[path] if path in file_contents else json.dumps(view)).encode()).decode()))
|
||||
for path, view in views.items()]
|
||||
return types.SimpleNamespace(parts=[*files, types.SimpleNamespace(kind="data", data={"state": views})])
|
||||
|
||||
@@ -201,6 +204,7 @@ const runVerifier = async (
|
||||
readonly readers?: readonly DesktopTaskReader[];
|
||||
readonly answers?: Readonly<Record<string, unknown>>;
|
||||
readonly retryAnswers?: Readonly<Record<string, unknown>>;
|
||||
readonly fileContents?: Readonly<Record<string, string>>;
|
||||
readonly exec?: boolean;
|
||||
readonly cleanupFailures?: number;
|
||||
readonly retry?: boolean;
|
||||
@@ -234,24 +238,31 @@ const runVerifier = async (
|
||||
"notes",
|
||||
),
|
||||
);
|
||||
// Node writes the JSON input as UTF-8; isolated Python must decode it the
|
||||
// same way even when the host's default pipe encoding differs.
|
||||
return JSON.parse(
|
||||
execFileSync("python3", ["-I", "-B", "-c", DRIVER, folder, verifier], {
|
||||
input: JSON.stringify({
|
||||
saves,
|
||||
durations,
|
||||
answers: computer.answers,
|
||||
retry_answers: computer.retryAnswers,
|
||||
exec: computer.exec ?? true,
|
||||
cleanup_failures: computer.cleanupFailures,
|
||||
retry: computer.retry,
|
||||
cancel_cleanup: computer.cancelCleanup,
|
||||
reload_verifier: computer.reloadVerifier,
|
||||
concurrent: computer.concurrent,
|
||||
parallel_retry: computer.parallelRetry,
|
||||
report_failure: computer.reportFailure,
|
||||
}),
|
||||
encoding: "utf8",
|
||||
}),
|
||||
execFileSync(
|
||||
"python3",
|
||||
["-I", "-B", "-X", "utf8", "-c", DRIVER, folder, verifier],
|
||||
{
|
||||
input: JSON.stringify({
|
||||
saves,
|
||||
durations,
|
||||
answers: computer.answers,
|
||||
retry_answers: computer.retryAnswers,
|
||||
file_contents: computer.fileContents,
|
||||
exec: computer.exec ?? true,
|
||||
cleanup_failures: computer.cleanupFailures,
|
||||
retry: computer.retry,
|
||||
cancel_cleanup: computer.cancelCleanup,
|
||||
reload_verifier: computer.reloadVerifier,
|
||||
concurrent: computer.concurrent,
|
||||
parallel_retry: computer.parallelRetry,
|
||||
report_failure: computer.reportFailure,
|
||||
}),
|
||||
encoding: "utf8",
|
||||
},
|
||||
),
|
||||
) as VerifierOutput;
|
||||
};
|
||||
|
||||
@@ -372,6 +383,42 @@ it.each([
|
||||
},
|
||||
);
|
||||
|
||||
it.each([64, 350, 490])(
|
||||
"grades nested XML selector text at depth %i like REA",
|
||||
async (depth) => {
|
||||
const file = "Documents/deep.xml";
|
||||
const xml =
|
||||
"<root><record><name>ma" +
|
||||
"<n>".repeat(depth) +
|
||||
"rke" +
|
||||
"</n>".repeat(depth) +
|
||||
"r<saved>2026-11-13T10:00:00Z</saved></name></record></root>";
|
||||
const view = xmlView(xml, desktopViewPolicy({ xml_depth: 494 }));
|
||||
if (view === undefined) throw new Error("Expected XML fixture");
|
||||
expect(view).toMatchObject({ truncated: false });
|
||||
const views = {
|
||||
[file]: { format: "xml" as const, view, truncated: false },
|
||||
};
|
||||
const checks: DesktopTaskCheck[] = [
|
||||
{
|
||||
id: "deep",
|
||||
kind: "value_equals",
|
||||
file,
|
||||
pointer: "/root/children/[>name=marker]/name",
|
||||
value: "record",
|
||||
},
|
||||
];
|
||||
expect(
|
||||
evaluateTaskChecks(checks, new Map(Object.entries(views)))[0]?.passed,
|
||||
).toBe(true);
|
||||
const output = await runVerifier(checks, [[0, views]], [], {
|
||||
exec: false,
|
||||
fileContents: { [file]: xml },
|
||||
});
|
||||
expect(output.results.map(({ result }) => result)).toEqual([true]);
|
||||
},
|
||||
);
|
||||
|
||||
it("decodes JSON pointer escapes once in the exported verifier", async () => {
|
||||
const checks: DesktopTaskCheck[] = [
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user