mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 13:12:26 +00:00
fix(desktop): share state and window admission with exported verifiers (#12154)
* fix(desktop): share state view admission with the exported verifier Refs #12133 * fix(desktop): share keyfile syntax with exported state readers Refs #12141 * fix(desktop): share X11 window property interpretation Refs #12140
This commit is contained in:
1 parent
461cedd73c
commit
0aee26def4
13 files changed
+534
-273
No files matched your search
@@ -142,8 +142,8 @@ The class declares:
|
||||
architecture for a vendor package, since its repository may serve no other.
|
||||
A vendor package captured on any other architecture is refused. A package
|
||||
installed from its `.deb` file supports only that file's architecture.
|
||||
- A launch at boot with the captured command, ready when a window of the
|
||||
captured class appears and one of them is listed in the taskbar and is not
|
||||
- A launch at boot with the captured command, ready when a window with the
|
||||
recorded class component of X11 `WM_CLASS` is listed in the taskbar and is not
|
||||
typed as a splash: a splash screen or a dialog of the class skips the
|
||||
taskbar or says it is a splash, and an app whose splash shows first would
|
||||
otherwise count as ready before its own window opens. Arguments that are the captured home or under it, and
|
||||
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
type DesktopTask,
|
||||
} from "../../domain/desktop/appTask.js";
|
||||
import { SETTLE_TIMING } from "../../domain/desktop/appSettle.js";
|
||||
import { appStateViews } from "../../domain/desktop/appStateViews.js";
|
||||
import {
|
||||
evaluateTaskChecks,
|
||||
type TaskCheckOutcome,
|
||||
@@ -18,7 +19,6 @@ import {
|
||||
import { isScreenTask } from "../../domain/desktop/appTaskScreen.js";
|
||||
import {
|
||||
admitWitnessStage,
|
||||
appStateViews,
|
||||
stageExpectation,
|
||||
stageVerdict,
|
||||
unrestoredChecks,
|
||||
|
||||
@@ -7,6 +7,13 @@ import {
|
||||
import { PYTHON_JSON_READER } from "./appExportPythonJson.js";
|
||||
import { PYTHON_XML_READER } from "./appExportPythonXml.js";
|
||||
import { PYTHON_ZIP_HELPERS } from "./appExportPythonZip.js";
|
||||
import {
|
||||
DESKTOP_LINE_BREAK,
|
||||
KEYFILE_CONTROL_LINE,
|
||||
KEYFILE_ENTRY_LINE,
|
||||
PYTHON_ECMASCRIPT_TRIM_CHARACTERS,
|
||||
} from "./desktopTextSyntax.js";
|
||||
import { PYTHON_X11_PROPERTIES } from "./x11Windows.js";
|
||||
|
||||
/** The generated module's imports. */
|
||||
/** The class method that holds readiness until the app's own window shows, not a splash screen with its class. */
|
||||
@@ -77,8 +84,10 @@ def _opens_element(text: str) -> bool:
|
||||
elif character == ">":
|
||||
return XML_ELEMENT_OPENING.match(text[index + 1:].lstrip()) is not None
|
||||
return False
|
||||
GROUP_HEADER = re.compile(r"^\[([^\]]+)\]$")
|
||||
LINE_BREAK = re.compile(r"\r?\n")
|
||||
KEYFILE_CONTROL_LINE = re.compile(${JSON.stringify(KEYFILE_CONTROL_LINE)})
|
||||
KEYFILE_ENTRY_LINE = re.compile(${JSON.stringify(KEYFILE_ENTRY_LINE)})
|
||||
LINE_BREAK = re.compile(${JSON.stringify(DESKTOP_LINE_BREAK)})
|
||||
ECMASCRIPT_TRIM_CHARACTERS = ${PYTHON_ECMASCRIPT_TRIM_CHARACTERS}
|
||||
SQLITE_HEADER = b"SQLite format 3\x00"
|
||||
GVDB_HEADER = b"GVariant"
|
||||
ZIP_HEADER = b"PK\x03\x04"
|
||||
@@ -115,8 +124,7 @@ def _respelled_utf16(data: bytes, home: Path) -> bytes:
|
||||
return mark + _respelled(text, home).encode(codec) if SEED_HOME in text else data
|
||||
|
||||
|
||||
#: What marks a window as not yet the app's own: skipping the taskbar, as splash screens and dialogs do, or a splash type.
|
||||
SPLASH_MARKS = ("_NET_WM_STATE_SKIP_TASKBAR", "_NET_WM_WINDOW_TYPE_SPLASH")
|
||||
${PYTHON_X11_PROPERTIES}
|
||||
|
||||
|
||||
def _taskbar_window(window_class: str) -> bool:
|
||||
@@ -126,10 +134,14 @@ def _taskbar_window(window_class: str) -> bool:
|
||||
done = subprocess.run(["xprop", *args], capture_output=True, text=True)
|
||||
return done.stdout if done.returncode == 0 else ""
|
||||
|
||||
for window in re.findall(r"0x[0-9a-f]+", xprop("-root", "_NET_CLIENT_LIST").partition("#")[2]):
|
||||
props = xprop("-id", window, "WM_CLASS", "_NET_WM_STATE", "_NET_WM_WINDOW_TYPE")
|
||||
classes = re.findall(r'"((?:[^"\\]|\\.)*)"', props.partition("WM_CLASS")[2].split("\n", 1)[0])
|
||||
if window_class in classes and not any(mark in props for mark in SPLASH_MARKS):
|
||||
root = _x11_properties(xprop("-root", "_NET_CLIENT_LIST"))
|
||||
for window in _x11_client_ids(root):
|
||||
props = _x11_properties(xprop("-id", window, "WM_CLASS", "_NET_WM_STATE", "_NET_WM_WINDOW_TYPE"))
|
||||
if _x11_window_class(props) != window_class:
|
||||
continue
|
||||
states = [atom.strip(ECMASCRIPT_TRIM_CHARACTERS) for atom in props.get("_NET_WM_STATE", "").split(",")]
|
||||
types = [atom.strip(ECMASCRIPT_TRIM_CHARACTERS) for atom in props.get("_NET_WM_WINDOW_TYPE", "").split(",")]
|
||||
if "_NET_WM_STATE_SKIP_TASKBAR" not in states and "_NET_WM_WINDOW_TYPE_SPLASH" not in types:
|
||||
return True
|
||||
return False
|
||||
|
||||
@@ -332,16 +344,16 @@ def _text(text: str) -> dict:
|
||||
|
||||
def _keyfile_line(raw: str) -> tuple:
|
||||
"""A key file line as REA reads it: skip (blank or comment), group, entry or other."""
|
||||
line = raw.strip()
|
||||
if not line or line[0] in "#;":
|
||||
return ("skip",)
|
||||
header = GROUP_HEADER.match(line)
|
||||
if header:
|
||||
return ("group", header.group(1))
|
||||
key, equals, value = raw.partition("=")
|
||||
key = key.strip()
|
||||
# Only ASCII space and tab immediately after '=' are padding.
|
||||
return ("entry", key, value.lstrip(" \t")) if equals and key else ("other",)
|
||||
control = KEYFILE_CONTROL_LINE.fullmatch(raw.strip(ECMASCRIPT_TRIM_CHARACTERS))
|
||||
if control is not None:
|
||||
name = control.group(1)
|
||||
return ("skip",) if name is None else ("group", name)
|
||||
entry = KEYFILE_ENTRY_LINE.fullmatch(raw)
|
||||
if entry is not None:
|
||||
key = entry.group(1).strip(ECMASCRIPT_TRIM_CHARACTERS)
|
||||
if key:
|
||||
return ("entry", key, entry.group(2))
|
||||
return ("other",)
|
||||
|
||||
|
||||
def _each_line(text: str):
|
||||
@@ -367,7 +379,7 @@ def _is_keyfile(text: str) -> bool:
|
||||
|
||||
def _keyfile(text: str) -> dict:
|
||||
groups, group = {}, ""
|
||||
for line in map(_keyfile_line, LINE_BREAK.split(text)):
|
||||
for line in map(_keyfile_line, _each_line(text)):
|
||||
if line[0] == "group":
|
||||
group = line[1]
|
||||
groups.setdefault(group, {})
|
||||
@@ -383,7 +395,7 @@ ${PYTHON_JSON_READER}
|
||||
def _text_view(name: str, text: str, size: int) -> dict:
|
||||
"""The format REA's state view detects for decoded text of size bytes, and its view; a JSON or key file over the
|
||||
parsed-view limit is refused before its value is built."""
|
||||
trimmed = re.sub(r"^[\s]+", "", text)
|
||||
trimmed = text.lstrip(ECMASCRIPT_TRIM_CHARACTERS)
|
||||
if name.lower().endswith(".json") or trimmed[:1] in ("[", "{"):
|
||||
view = _json_view(text, size)
|
||||
if view is not None:
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { appStateViews } from "./appStateViews.js";
|
||||
import { parseDesktopTask } from "./appTask.js";
|
||||
import { evaluateTaskChecks } from "./appTaskChecks.js";
|
||||
|
||||
describe("app state views", () => {
|
||||
it.each(["__proto__", "constructor", "ordinary"])(
|
||||
"grades the exact own state path %s instead of treating it as absent",
|
||||
(path) => {
|
||||
const view = JSON.parse('{"count":7,"__proto__":"retained value"}');
|
||||
const parts = [
|
||||
{
|
||||
kind: "data",
|
||||
data: {
|
||||
state: Object.fromEntries([[path, { format: "json", view }]]),
|
||||
},
|
||||
},
|
||||
];
|
||||
const task = parseDesktopTask({
|
||||
name: "check-state",
|
||||
prompt: "Check the selected file and its value.",
|
||||
checks: [
|
||||
{ kind: "file_present", file: path },
|
||||
{ kind: "file_absent", file: path },
|
||||
{ kind: "value_equals", file: path, pointer: "/count", value: 7 },
|
||||
],
|
||||
});
|
||||
const parsed = appStateViews(parts);
|
||||
if (!task.ok) throw new Error(task.error);
|
||||
if (!parsed.ok) throw new Error(parsed.error);
|
||||
expect([...parsed.value.keys()]).toEqual([path]);
|
||||
expect(parsed.value.get(path)).toEqual({
|
||||
format: "json",
|
||||
view,
|
||||
error: null,
|
||||
});
|
||||
expect(
|
||||
evaluateTaskChecks(task.value.checks, parsed.value).map(
|
||||
({ passed }) => passed,
|
||||
),
|
||||
).toEqual([true, false, true]);
|
||||
expect(Object.hasOwn(parts[0]!.data.state, path)).toBe(true);
|
||||
expect(Object.hasOwn(view, "__proto__")).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(
|
||||
["__proto__", "ordinary"].flatMap((path) => [
|
||||
{ path, entry: { format: 7, view: null } },
|
||||
{ path, entry: { format: "json" } },
|
||||
{ path, entry: { format: null, view: null, error: 7 } },
|
||||
]),
|
||||
)("rejects a malformed view under $path: $entry", ({ path, entry }) => {
|
||||
expect(
|
||||
appStateViews([
|
||||
{ kind: "data", data: { state: Object.fromEntries([[path, entry]]) } },
|
||||
]),
|
||||
).toMatchObject({ ok: false });
|
||||
});
|
||||
|
||||
it("reads the app's data/get state into the views the checks read, keeping each file's error", () => {
|
||||
expect(
|
||||
appStateViews([
|
||||
{ kind: "file", file: { bytes: "eA==" } },
|
||||
{
|
||||
kind: "data",
|
||||
data: {
|
||||
state: {
|
||||
"Documents/todo.txt": {
|
||||
format: "text",
|
||||
view: { lines: ["milk"], truncated: false },
|
||||
truncated: false,
|
||||
},
|
||||
"Documents/big.bin": {
|
||||
format: null,
|
||||
view: null,
|
||||
error: "larger than 64 MiB, the most the view reads",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
).toEqual({
|
||||
ok: true,
|
||||
value: new Map([
|
||||
[
|
||||
"Documents/todo.txt",
|
||||
{
|
||||
format: "text",
|
||||
view: { lines: ["milk"], truncated: false },
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Documents/big.bin",
|
||||
{
|
||||
format: null,
|
||||
view: null,
|
||||
error: "larger than 64 MiB, the most the view reads",
|
||||
},
|
||||
],
|
||||
]),
|
||||
});
|
||||
expect(appStateViews([{ kind: "file" }])).toMatchObject({
|
||||
ok: false,
|
||||
error: expect.stringContaining("no state views"),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,98 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { jsonRecordSchema } from "../jsonRecord.js";
|
||||
import { jsonValueSchema, MAX_JSON_DEPTH } from "../jsonValue.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import type { TaskFileView } from "./appTaskChecks.js";
|
||||
|
||||
const fileViewSchema = z.object({
|
||||
format: z.string().nullable(),
|
||||
view: jsonValueSchema.nullable(),
|
||||
error: z
|
||||
.string()
|
||||
.nullish()
|
||||
.transform((error) => error ?? null),
|
||||
});
|
||||
const statePartSchema = z.looseObject({
|
||||
kind: z.literal("data"),
|
||||
data: z.looseObject({ state: jsonRecordSchema(fileViewSchema) }),
|
||||
});
|
||||
|
||||
/** The first valid set of typed file views returned by an exported app's data/get. */
|
||||
export const appStateViews = (
|
||||
parts: readonly unknown[],
|
||||
): Result<ReadonlyMap<string, TaskFileView>, string> => {
|
||||
for (const part of parts) {
|
||||
const parsed = statePartSchema.safeParse(part);
|
||||
if (parsed.success)
|
||||
return ok(new Map(Object.entries(parsed.data.data.state)));
|
||||
}
|
||||
return err(
|
||||
"The app's data/get returned no state views; witness a bundle exported by REA, whose app serves them.",
|
||||
);
|
||||
};
|
||||
|
||||
/** Keep the detached verifier's field admission tied to the host reader's input schema. */
|
||||
export const renderPythonAppStateViews = (): string => {
|
||||
const { properties, required } = z.toJSONSchema(fileViewSchema, {
|
||||
io: "input",
|
||||
});
|
||||
if (properties === undefined || required === undefined)
|
||||
throw new TypeError(
|
||||
"Expected the app state file view to define its fields.",
|
||||
);
|
||||
const nullableTextFields = Object.entries(properties)
|
||||
.filter(([, property]) => {
|
||||
const type = typeof property === "object" ? property.type : undefined;
|
||||
return Array.isArray(type) && type.includes("string");
|
||||
})
|
||||
.map(([field]) => field);
|
||||
return String.raw`
|
||||
_STATE_REQUIRED_FIELDS = ${JSON.stringify(required)}
|
||||
_STATE_TEXT_FIELDS = ${JSON.stringify(nullableTextFields)}
|
||||
|
||||
|
||||
def _valid_state_json(state) -> bool:
|
||||
# Stack of iterators avoids copying the decoded reply or relying on Python's
|
||||
# recursion limit. The state map is the root of the same bound as in REA.
|
||||
frames = [iter((state,))]
|
||||
while frames:
|
||||
value = next(frames[-1], MISSING)
|
||||
if value is MISSING:
|
||||
frames.pop()
|
||||
continue
|
||||
if len(frames) - 1 > ${String(MAX_JSON_DEPTH)}:
|
||||
return False
|
||||
if isinstance(value, dict):
|
||||
if not all(isinstance(key, str) for key in value):
|
||||
return False
|
||||
frames.append(iter(value.values()))
|
||||
elif isinstance(value, list):
|
||||
frames.append(iter(value))
|
||||
elif isinstance(value, (int, float)):
|
||||
try:
|
||||
if not math.isfinite(value):
|
||||
return False
|
||||
except OverflowError:
|
||||
return False
|
||||
elif value is not None and not isinstance(value, str):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _app_state_views(parts):
|
||||
for part in parts:
|
||||
if part.kind != "data":
|
||||
continue
|
||||
data = getattr(part, "data", None)
|
||||
state = data.get("state") if isinstance(data, dict) else None
|
||||
if not isinstance(state, dict):
|
||||
continue
|
||||
if all(isinstance(view, dict)
|
||||
and all(field in view for field in _STATE_REQUIRED_FIELDS)
|
||||
and all(view.get(field) is None or isinstance(view[field], str) for field in _STATE_TEXT_FIELDS)
|
||||
for view in state.values()) and _valid_state_json(state):
|
||||
return state
|
||||
return None
|
||||
`;
|
||||
};
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { JsonValue } from "../jsonValue.js";
|
||||
import { SETTLE_TIMING } from "./appSettle.js";
|
||||
import { renderPythonAppStateViews } from "./appStateViews.js";
|
||||
import type { DesktopTask } from "./appTask.js";
|
||||
import { PYTHON_POINTER } from "./appTaskPythonPointer.js";
|
||||
import { PYTHON_ECMASCRIPT_TRIM_CHARACTERS } from "./desktopTextSyntax.js";
|
||||
import {
|
||||
PYTHON_READER_VIEWS,
|
||||
renderReaderConstants,
|
||||
@@ -23,7 +25,7 @@ WHITESPACE_RUN = re.compile(r"[ \t\r\n]+")
|
||||
|
||||
def _trim(text: str) -> str:
|
||||
"""ECMAScript String.trim(), as REA uses for numeral and saved-time recognition."""
|
||||
return text.strip("\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff")
|
||||
return text.strip(${PYTHON_ECMASCRIPT_TRIM_CHARACTERS})
|
||||
|
||||
|
||||
def _shown(value) -> str:
|
||||
@@ -464,11 +466,10 @@ def _files(answer) -> list:
|
||||
|
||||
async def _read(url):
|
||||
answer = await protocol.get_data(url, timeout=120)
|
||||
states = [part.data["state"] for part in answer.parts
|
||||
if part.kind == "data" and isinstance(part.data.get("state"), dict)]
|
||||
if not states:
|
||||
views = _app_state_views(answer.parts)
|
||||
if views is None:
|
||||
raise RuntimeError(f"the {APP} app's data/get returned no state views")
|
||||
return states[0], _files(answer)
|
||||
return views, _files(answer)
|
||||
|
||||
|
||||
async def _settled_views(url):
|
||||
@@ -551,7 +552,7 @@ SETTLE_WINDOW_S = ${pythonSeconds(SETTLE_TIMING.windowMs)}
|
||||
SETTLE_BOUND_S = ${pythonSeconds(SETTLE_TIMING.boundMs)}
|
||||
#: Where a data/get file part names its path in the home.
|
||||
DATA_OBJECTS = "urn:agentenv:data-objects/v1"
|
||||
${task.readers === undefined ? "" : renderReaderConstants(task.readers)}${PYTHON_TASK_EVALUATOR}${PYTHON_SETTLE}${task.readers === undefined ? "" : PYTHON_READER_VIEWS}
|
||||
${task.readers === undefined ? "" : renderReaderConstants(task.readers)}${PYTHON_TASK_EVALUATOR}${renderPythonAppStateViews()}${PYTHON_SETTLE}${task.readers === undefined ? "" : PYTHON_READER_VIEWS}
|
||||
|
||||
async def verify(mcp_url, env_url):
|
||||
card = await protocol.get_card(env_url)
|
||||
|
||||
@@ -2,14 +2,11 @@ import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
admitWitnessStage,
|
||||
appStateViews,
|
||||
stageVerdict,
|
||||
type WitnessActionLog,
|
||||
type WitnessStageRecord,
|
||||
} from "./buildWitness.js";
|
||||
import type { ComputerAction } from "./computerActions.js";
|
||||
import { parseDesktopTask } from "./appTask.js";
|
||||
import { evaluateTaskChecks } from "./appTaskChecks.js";
|
||||
|
||||
type Logged = {
|
||||
readonly action: ComputerAction;
|
||||
@@ -351,108 +348,3 @@ describe("build witness over a long session", () => {
|
||||
).toMatchObject({ ok: false, error: expect.stringContaining("reset") });
|
||||
});
|
||||
});
|
||||
|
||||
describe("build witness state views", () => {
|
||||
it.each(["__proto__", "constructor", "ordinary"])(
|
||||
"grades the exact own state path %s instead of treating it as absent",
|
||||
(path) => {
|
||||
const view = JSON.parse('{"count":7,"__proto__":"retained value"}');
|
||||
const parts = [
|
||||
{
|
||||
kind: "data",
|
||||
data: {
|
||||
state: Object.fromEntries([[path, { format: "json", view }]]),
|
||||
},
|
||||
},
|
||||
];
|
||||
const task = parseDesktopTask({
|
||||
name: "check-state",
|
||||
prompt: "Check the selected file and its value.",
|
||||
checks: [
|
||||
{ kind: "file_present", file: path },
|
||||
{ kind: "file_absent", file: path },
|
||||
{ kind: "value_equals", file: path, pointer: "/count", value: 7 },
|
||||
],
|
||||
});
|
||||
const parsed = appStateViews(parts);
|
||||
if (!task.ok) throw new Error(task.error);
|
||||
if (!parsed.ok) throw new Error(parsed.error);
|
||||
expect([...parsed.value.keys()]).toEqual([path]);
|
||||
expect(parsed.value.get(path)).toEqual({
|
||||
format: "json",
|
||||
view,
|
||||
error: null,
|
||||
});
|
||||
expect(
|
||||
evaluateTaskChecks(task.value.checks, parsed.value).map(
|
||||
({ passed }) => passed,
|
||||
),
|
||||
).toEqual([true, false, true]);
|
||||
expect(Object.hasOwn(parts[0]!.data.state, path)).toBe(true);
|
||||
expect(Object.hasOwn(view, "__proto__")).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
it.each(
|
||||
["__proto__", "ordinary"].flatMap((path) => [
|
||||
{ path, entry: { format: 7, view: null } },
|
||||
{ path, entry: { format: "json" } },
|
||||
{ path, entry: { format: null, view: null, error: 7 } },
|
||||
]),
|
||||
)("rejects a malformed view under $path: $entry", ({ path, entry }) => {
|
||||
expect(
|
||||
appStateViews([
|
||||
{ kind: "data", data: { state: Object.fromEntries([[path, entry]]) } },
|
||||
]),
|
||||
).toMatchObject({ ok: false });
|
||||
});
|
||||
|
||||
it("reads the app's data/get state into the views the checks read, keeping each file's error", () => {
|
||||
expect(
|
||||
appStateViews([
|
||||
{ kind: "file", file: { bytes: "eA==" } },
|
||||
{
|
||||
kind: "data",
|
||||
data: {
|
||||
state: {
|
||||
"Documents/todo.txt": {
|
||||
format: "text",
|
||||
view: { lines: ["milk"], truncated: false },
|
||||
truncated: false,
|
||||
},
|
||||
"Documents/big.bin": {
|
||||
format: null,
|
||||
view: null,
|
||||
error: "larger than 64 MiB, the most the view reads",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
).toEqual({
|
||||
ok: true,
|
||||
value: new Map([
|
||||
[
|
||||
"Documents/todo.txt",
|
||||
{
|
||||
format: "text",
|
||||
view: { lines: ["milk"], truncated: false },
|
||||
error: null,
|
||||
},
|
||||
],
|
||||
[
|
||||
"Documents/big.bin",
|
||||
{
|
||||
format: null,
|
||||
view: null,
|
||||
error: "larger than 64 MiB, the most the view reads",
|
||||
},
|
||||
],
|
||||
]),
|
||||
});
|
||||
expect(appStateViews([{ kind: "file" }])).toMatchObject({
|
||||
ok: false,
|
||||
error: expect.stringContaining("no state views"),
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,5 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { jsonValueSchema } from "../jsonValue.js";
|
||||
import { jsonRecordSchema } from "../jsonRecord.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import type { TaskCheckOutcome, TaskFileView } from "./appTaskChecks.js";
|
||||
import type { TaskCheckOutcome } from "./appTaskChecks.js";
|
||||
import type { ComputerAction } from "./computerActions.js";
|
||||
|
||||
/**
|
||||
@@ -221,33 +217,3 @@ export const admitWitnessStage = (
|
||||
);
|
||||
return ok(log.total);
|
||||
};
|
||||
|
||||
const fileViewSchema = z.object({
|
||||
format: z.string().nullable(),
|
||||
view: jsonValueSchema.nullable(),
|
||||
error: z
|
||||
.string()
|
||||
.nullish()
|
||||
.transform((error) => error ?? null),
|
||||
});
|
||||
const statePartSchema = z.looseObject({
|
||||
kind: z.literal("data"),
|
||||
data: z.looseObject({ state: jsonRecordSchema(fileViewSchema) }),
|
||||
});
|
||||
|
||||
/**
|
||||
* The typed views an exported app's `data/get` returns beside its files,
|
||||
* keyed by home path: the shape the task checks read.
|
||||
*/
|
||||
export const appStateViews = (
|
||||
parts: readonly unknown[],
|
||||
): Result<ReadonlyMap<string, TaskFileView>, string> => {
|
||||
for (const part of parts) {
|
||||
const parsed = statePartSchema.safeParse(part);
|
||||
if (parsed.success)
|
||||
return ok(new Map(Object.entries(parsed.data.data.state)));
|
||||
}
|
||||
return err(
|
||||
"The app's data/get returned no state views; witness a bundle exported by REA, whose app serves them.",
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
/** Desktop text records use LF or CRLF. Other characters stay in the record. */
|
||||
export const DESKTOP_LINE_BREAK = String.raw`\r?\n`;
|
||||
export const desktopLineBreak = new RegExp(DESKTOP_LINE_BREAK, "gu");
|
||||
|
||||
/** Python spelling of the ECMAScript String.trim() character set, without Python-only whitespace. */
|
||||
export const PYTHON_ECMASCRIPT_TRIM_CHARACTERS = String.raw`"\t\n\v\f\r \u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000\ufeff"`;
|
||||
|
||||
/** On a trimmed keyfile line, match a blank/comment or capture a group name. */
|
||||
export const KEYFILE_CONTROL_LINE = String.raw`^(?:[#;][\s\S]*)?$|^\[([^\]]+)\]$`;
|
||||
|
||||
/** On a raw keyfile line, capture the key and value. Only ASCII space and tab after '=' are padding. */
|
||||
export const KEYFILE_ENTRY_LINE = String.raw`^([^=]*)=[ \t]*([\s\S]*)$`;
|
||||
|
||||
type KeyfileLine =
|
||||
| { readonly kind: "skip" }
|
||||
| { readonly kind: "group"; readonly name: string }
|
||||
| { readonly kind: "entry"; readonly key: string; readonly value: string }
|
||||
| { readonly kind: "other" };
|
||||
|
||||
const keyfileControlLine = new RegExp(KEYFILE_CONTROL_LINE, "u");
|
||||
const keyfileEntryLine = new RegExp(KEYFILE_ENTRY_LINE, "u");
|
||||
|
||||
const keyfileLine = (raw: string): KeyfileLine => {
|
||||
const control = keyfileControlLine.exec(raw.trim());
|
||||
if (control !== null) {
|
||||
const name = control[1];
|
||||
return name === undefined ? { kind: "skip" } : { kind: "group", name };
|
||||
}
|
||||
const entry = keyfileEntryLine.exec(raw);
|
||||
const key = entry?.[1]?.trim();
|
||||
const value = entry?.[2];
|
||||
return key === undefined || key === "" || value === undefined
|
||||
? { kind: "other" }
|
||||
: { kind: "entry", key, value };
|
||||
};
|
||||
|
||||
/** Read keyfile lines lazily so a plain text file can be rejected immediately. */
|
||||
export function* keyfileLines(text: string): Generator<KeyfileLine> {
|
||||
let start = 0;
|
||||
for (const delimiter of text.matchAll(desktopLineBreak)) {
|
||||
yield keyfileLine(text.slice(start, delimiter.index));
|
||||
start = delimiter.index + delimiter[0].length;
|
||||
}
|
||||
yield keyfileLine(text.slice(start));
|
||||
}
|
||||
|
||||
/** A keyfile has at least one group and nothing a keyfile view would drop. */
|
||||
export const isKeyfile = (text: string): boolean => {
|
||||
let group = false;
|
||||
for (const { kind } of keyfileLines(text)) {
|
||||
if (kind === "other") return false;
|
||||
if (kind === "group") group = true;
|
||||
}
|
||||
return group;
|
||||
};
|
||||
@@ -16,6 +16,11 @@ import {
|
||||
parseXmlDocument,
|
||||
} from "../xmlDocument.js";
|
||||
import { expatXmlName } from "./expatXmlNames.js";
|
||||
import {
|
||||
desktopLineBreak,
|
||||
isKeyfile,
|
||||
keyfileLines,
|
||||
} from "./desktopTextSyntax.js";
|
||||
import { decodeStateUtf16 } from "./stateTextEncoding.js";
|
||||
import { decodeStrictUtf8 } from "../strictUtf8.js";
|
||||
|
||||
@@ -47,49 +52,6 @@ export const XML_POLICY = {
|
||||
nameSupported: expatXmlName,
|
||||
} as const;
|
||||
|
||||
type KeyfileLine =
|
||||
| { readonly kind: "skip" }
|
||||
| { readonly kind: "group"; readonly name: string }
|
||||
| { readonly kind: "entry"; readonly key: string; readonly value: string }
|
||||
| { readonly kind: "other" };
|
||||
|
||||
/** One key file line: blank or comment, `[group]`, `key=value`, or anything else. */
|
||||
const keyfileLine = (raw: string): KeyfileLine => {
|
||||
const line = raw.trim();
|
||||
if (line.length === 0 || line.startsWith("#") || line.startsWith(";"))
|
||||
return { kind: "skip" };
|
||||
const name = /^\[(?<name>[^\]]+)\]$/u.exec(line)?.groups?.name;
|
||||
if (name !== undefined) return { kind: "group", name };
|
||||
const equals = raw.indexOf("=");
|
||||
if (equals < 0) return { kind: "other" };
|
||||
const key = raw.slice(0, equals).trim();
|
||||
return key.length === 0
|
||||
? { kind: "other" }
|
||||
: {
|
||||
kind: "entry",
|
||||
key,
|
||||
// Only ASCII space and tab immediately after '=' are padding.
|
||||
value: raw.slice(equals + 1).replace(/^[ \t]+/u, ""),
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* A key file has at least one group and nothing a key file view would drop.
|
||||
* Lines are read one at a time, stopping at the first that is neither.
|
||||
*/
|
||||
const isKeyfile = (text: string): boolean => {
|
||||
let group = false;
|
||||
for (let start = 0; start <= text.length;) {
|
||||
const end = text.indexOf("\n", start);
|
||||
const kind = keyfileLine(text.slice(start, end < 0 ? undefined : end)).kind;
|
||||
if (kind === "other") return false;
|
||||
if (kind === "group") group = true;
|
||||
if (end < 0) break;
|
||||
start = end + 1;
|
||||
}
|
||||
return group;
|
||||
};
|
||||
|
||||
/** Decode UTF-8 and BOM-marked UTF-16, or return undefined for binary content. */
|
||||
export const decodeStateText = (bytes: Uint8Array): string | undefined => {
|
||||
const utf16 = decodeStateUtf16(bytes);
|
||||
@@ -415,7 +377,7 @@ export const textView = (
|
||||
text: string,
|
||||
policy: DesktopViewPolicy = DESKTOP_VIEW_DEFAULTS,
|
||||
): JsonValue => {
|
||||
const lines = text.split(/\r?\n/u);
|
||||
const lines = text.split(desktopLineBreak);
|
||||
if (lines.at(-1) === "") lines.pop();
|
||||
return {
|
||||
lines: lines.slice(0, policy.text_lines),
|
||||
@@ -437,8 +399,7 @@ export const keyfileView = (
|
||||
): Record<string, Record<string, string>> => {
|
||||
const groups = plainRecord<Record<string, string>>();
|
||||
let group = "";
|
||||
for (const raw of text.split(/\r?\n/u)) {
|
||||
const line = keyfileLine(raw);
|
||||
for (const line of keyfileLines(text)) {
|
||||
if (line.kind === "group") {
|
||||
group = line.name;
|
||||
groups[group] ??= plainRecord();
|
||||
|
||||
@@ -17,7 +17,14 @@ export const X11_WINDOW_PROPERTY_NAMES = [
|
||||
] as const;
|
||||
|
||||
const PROPERTY_LINE =
|
||||
/^(?<name>[A-Z_]+)(?:\([A-Z0-9_]+\))?(?<rest>:| =)(?<value>.*)$/u;
|
||||
/^([A-Z_]+)(?:\([A-Z0-9_]+\))?(:| =)([^\r\n\u2028\u2029]*)$/u;
|
||||
const WINDOW_ID = /0[xX][0-9a-fA-F]+/gu;
|
||||
const QUOTED_STRING = /"((?:[^"\\]|\\[^\r\n\u2028\u2029])*)"/gu;
|
||||
const STRING_ESCAPE = /\\([0-7]{3}|[^\r\n\u2028\u2029])/gu;
|
||||
const OCTAL_BYTE = /^[0-7]{3}$/u;
|
||||
const MAX_OCTAL_BYTE = 0xff;
|
||||
// ICCCM WM_CLASS stores the instance first and the class second.
|
||||
const CLASS_INDEX = 1;
|
||||
|
||||
/** What `xprop` prints for a property that is not set, or an atom never interned. */
|
||||
const UNSET = new Set(["not found.", "no such atom on any window."]);
|
||||
@@ -26,8 +33,8 @@ const propertyValues = (output: string): ReadonlyMap<string, string> => {
|
||||
const values = new Map<string, string>();
|
||||
for (const line of output.split("\n")) {
|
||||
const match = PROPERTY_LINE.exec(line);
|
||||
if (match?.groups === undefined) continue;
|
||||
const { name, rest, value } = match.groups;
|
||||
if (match === null) continue;
|
||||
const [, name, rest, value] = match;
|
||||
if (name === undefined || value === undefined) continue;
|
||||
if (rest === ":" && UNSET.has(value.trim())) continue;
|
||||
values.set(name, value.trim());
|
||||
@@ -35,38 +42,34 @@ const propertyValues = (output: string): ReadonlyMap<string, string> => {
|
||||
return values;
|
||||
};
|
||||
|
||||
const XPROP_STRING_ESCAPES: ReadonlyMap<string, string> = new Map([
|
||||
["n", "\n"],
|
||||
["t", "\t"],
|
||||
["\\", "\\"],
|
||||
['"', '"'],
|
||||
]);
|
||||
const XPROP_STRING_ESCAPES: Readonly<Record<string, string>> = {
|
||||
n: "\n",
|
||||
t: "\t",
|
||||
"\\": "\\",
|
||||
'"': '"',
|
||||
};
|
||||
|
||||
/** xprop emits LF/TAB escapes, quoted delimiters, and three-octal-digit bytes. */
|
||||
const decodeQuotedString = (source: string): string | null => {
|
||||
let valid = true;
|
||||
const decoded = source.replace(
|
||||
/\\([0-7]{3}|.)/gu,
|
||||
(_match, escape: string) => {
|
||||
if (/^[0-7]{3}$/u.test(escape)) {
|
||||
const byte = Number.parseInt(escape, 8);
|
||||
if (byte <= 0xff) return String.fromCharCode(byte);
|
||||
} else {
|
||||
const value = XPROP_STRING_ESCAPES.get(escape);
|
||||
if (value !== undefined) return value;
|
||||
}
|
||||
valid = false;
|
||||
return "";
|
||||
},
|
||||
);
|
||||
const decoded = source.replace(STRING_ESCAPE, (_match, escape: string) => {
|
||||
if (OCTAL_BYTE.test(escape)) {
|
||||
const byte = Number.parseInt(escape, 8);
|
||||
if (byte <= MAX_OCTAL_BYTE) return String.fromCharCode(byte);
|
||||
} else {
|
||||
const value = XPROP_STRING_ESCAPES[escape];
|
||||
if (value !== undefined) return value;
|
||||
}
|
||||
valid = false;
|
||||
return "";
|
||||
});
|
||||
return valid ? decoded : null;
|
||||
};
|
||||
|
||||
/** Read `xprop`'s quoted list; an uninterpretable member keeps its position unknown. */
|
||||
const quotedStrings = (value: string): readonly (string | null)[] => {
|
||||
const strings: (string | null)[] = [];
|
||||
const pattern = /"((?:[^"\\]|\\.)*)"/gu;
|
||||
for (const match of value.matchAll(pattern))
|
||||
for (const match of value.matchAll(QUOTED_STRING))
|
||||
strings.push(decodeQuotedString(match[1] ?? ""));
|
||||
return strings;
|
||||
};
|
||||
@@ -87,9 +90,7 @@ export const parseX11ClientList = (
|
||||
.some((atom) => atom.trim() === "_NET_CLIENT_LIST")
|
||||
? []
|
||||
: undefined;
|
||||
return [...value.matchAll(/0x[0-9a-f]+/giu)].map((match) =>
|
||||
match[0].toLowerCase(),
|
||||
);
|
||||
return [...value.matchAll(WINDOW_ID)].map((match) => match[0].toLowerCase());
|
||||
};
|
||||
|
||||
/** Parse one window's class, title, owner pid and type from `xprop -id`. */
|
||||
@@ -97,7 +98,7 @@ export const parseX11WindowProperties = (
|
||||
output: string,
|
||||
): X11WindowProperties => {
|
||||
const values = propertyValues(output);
|
||||
const [instance, class_] = quotedStrings(values.get("WM_CLASS") ?? "");
|
||||
const classes = quotedStrings(values.get("WM_CLASS") ?? "");
|
||||
const title =
|
||||
quotedStrings(values.get("_NET_WM_NAME") ?? "")[0] ??
|
||||
quotedStrings(values.get("WM_NAME") ?? "")[0] ??
|
||||
@@ -105,8 +106,8 @@ export const parseX11WindowProperties = (
|
||||
const pid = Number(values.get("_NET_WM_PID"));
|
||||
const windowType = values.get("_NET_WM_WINDOW_TYPE")?.split(",")[0]?.trim();
|
||||
return {
|
||||
instance: instance ?? null,
|
||||
class: class_ ?? null,
|
||||
instance: classes[0] ?? null,
|
||||
class: classes[CLASS_INDEX] ?? null,
|
||||
title,
|
||||
pid: Number.isSafeInteger(pid) && pid > 0 ? pid : null,
|
||||
window_type:
|
||||
@@ -115,3 +116,51 @@ export const parseX11WindowProperties = (
|
||||
: windowType.replace(/^_NET_WM_WINDOW_TYPE_/u, "").toLowerCase(),
|
||||
};
|
||||
};
|
||||
|
||||
/** The detached export reads the same xprop properties using Python's standard library. */
|
||||
export const PYTHON_X11_PROPERTIES = String.raw`
|
||||
XPROP_PROPERTY_LINE = re.compile(${JSON.stringify(PROPERTY_LINE.source)})
|
||||
XPROP_WINDOW_ID = re.compile(${JSON.stringify(WINDOW_ID.source)})
|
||||
XPROP_QUOTED_STRING = re.compile(${JSON.stringify(QUOTED_STRING.source)})
|
||||
XPROP_STRING_ESCAPE = re.compile(${JSON.stringify(STRING_ESCAPE.source)})
|
||||
XPROP_OCTAL_BYTE = re.compile(${JSON.stringify(OCTAL_BYTE.source)})
|
||||
XPROP_STRING_ESCAPES = ${JSON.stringify(XPROP_STRING_ESCAPES)}
|
||||
XPROP_UNSET = ${JSON.stringify([...UNSET])}
|
||||
|
||||
|
||||
def _x11_properties(output: str) -> dict[str, str]:
|
||||
properties = {}
|
||||
for line in output.split("\n"):
|
||||
match = XPROP_PROPERTY_LINE.fullmatch(line)
|
||||
if match is None:
|
||||
continue
|
||||
name, delimiter, value = match.groups()
|
||||
value = value.strip(ECMASCRIPT_TRIM_CHARACTERS)
|
||||
if delimiter != ":" or value not in XPROP_UNSET:
|
||||
properties[name] = value
|
||||
return properties
|
||||
|
||||
|
||||
def _x11_client_ids(properties: dict[str, str]) -> list[str]:
|
||||
return [match.group().lower() for match in XPROP_WINDOW_ID.finditer(properties.get("_NET_CLIENT_LIST", ""))]
|
||||
|
||||
|
||||
def _x11_window_class(properties: dict[str, str]) -> str | None:
|
||||
def decode(escape_match):
|
||||
escape = escape_match.group(1)
|
||||
if XPROP_OCTAL_BYTE.fullmatch(escape):
|
||||
byte = int(escape, 8)
|
||||
if byte <= ${String(MAX_OCTAL_BYTE)}:
|
||||
return chr(byte)
|
||||
elif escape in XPROP_STRING_ESCAPES:
|
||||
return XPROP_STRING_ESCAPES[escape]
|
||||
raise ValueError("unknown xprop string escape")
|
||||
|
||||
for index, match in enumerate(XPROP_QUOTED_STRING.finditer(properties.get("WM_CLASS", ""))):
|
||||
if index == ${String(CLASS_INDEX)}:
|
||||
try:
|
||||
return XPROP_STRING_ESCAPE.sub(decode, match.group(1))
|
||||
except ValueError:
|
||||
return None
|
||||
return None
|
||||
`;
|
||||
@@ -29,7 +29,6 @@ import {
|
||||
} from "../../../src/domain/desktop/appExportPython.js";
|
||||
import {
|
||||
detectStateFormat,
|
||||
keyfileView,
|
||||
textSyntaxFormat,
|
||||
textualView,
|
||||
} from "../../../src/domain/desktop/stateFormats.js";
|
||||
@@ -447,7 +446,8 @@ windows = json.loads(${JSON.stringify(table)})
|
||||
if sys.argv[1:3] == ["-root", "_NET_CLIENT_LIST"]:
|
||||
print("_NET_CLIENT_LIST(WINDOW): window id # " + ", ".join(windows))
|
||||
else:
|
||||
print(windows[sys.argv[2]])
|
||||
window = sys.argv[2].lower()
|
||||
print(next((props for xid, props in windows.items() if xid.lower() == window), ""))
|
||||
`,
|
||||
);
|
||||
await chmod(join(bin, "xprop"), 0o755);
|
||||
@@ -501,6 +501,56 @@ it("holds readiness until a window of the app's class that the taskbar lists is
|
||||
).toBe("False");
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
"canonicalizes a hexadecimal window id",
|
||||
{ "0xA00007": 'WM_CLASS(STRING) = "editor", "ModelEditor"' },
|
||||
"ModelEditor",
|
||||
"True",
|
||||
],
|
||||
[
|
||||
"decodes an escaped class",
|
||||
{ "0x1": String.raw`WM_CLASS(STRING) = "editor", "Model\042Editor"` },
|
||||
'Model"Editor',
|
||||
"True",
|
||||
],
|
||||
[
|
||||
"requires the class field",
|
||||
{ "0x1": 'WM_CLASS(STRING) = "ModelEditor", "OtherApp"' },
|
||||
"ModelEditor",
|
||||
"False",
|
||||
],
|
||||
[
|
||||
"finds no class property",
|
||||
{ "0x1": "WM_CLASS: not found." },
|
||||
"ModelEditor",
|
||||
"False",
|
||||
],
|
||||
[
|
||||
"cannot decode the class property",
|
||||
{ "0x1": String.raw`WM_CLASS(STRING) = "editor", "Model\qEditor"` },
|
||||
"ModelEditor",
|
||||
"False",
|
||||
],
|
||||
[
|
||||
"observes a differently cased class",
|
||||
{ "0x1": 'WM_CLASS(STRING) = "editor", "modeleditor"' },
|
||||
"ModelEditor",
|
||||
"False",
|
||||
],
|
||||
[
|
||||
"reads splash markers from their own properties",
|
||||
{ "0x1": 'WM_CLASS(STRING) = "editor", "_NET_WM_STATE_SKIP_TASKBAR"' },
|
||||
"_NET_WM_STATE_SKIP_TASKBAR",
|
||||
"True",
|
||||
],
|
||||
])(
|
||||
"interprets X11 readiness when xprop %s",
|
||||
async (_, windows, class_, ready) => {
|
||||
expect(taskbarWindow(await fakeXprop(windows), class_)).toBe(ready);
|
||||
},
|
||||
);
|
||||
|
||||
/** The exported module's JSON verdict for each text, with only its standard library imports: its view or its error. */
|
||||
const pythonJson = (texts: readonly string[]) => {
|
||||
const imports = PYTHON_IMPORTS.split("\n")
|
||||
@@ -803,29 +853,43 @@ with sqlite3.connect(":memory:") as db:
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps the same keyfile whitespace as the retained state view", async () => {
|
||||
const inputs = [
|
||||
"[ui]\nsuffix=hello \nunicode=\u00a0hello\u00a0\npadded= hello\n",
|
||||
"[ui]\r\nsuffix=hello \r\nunicode=\u00a0hello\u00a0\u00a0\r\npadded=\thello\r\n",
|
||||
];
|
||||
const imports = PYTHON_IMPORTS.split("\n")
|
||||
.filter((line) => !line.includes("agentenv_protocol"))
|
||||
.join("\n");
|
||||
const program = `${imports}${PYTHON_HELPERS}
|
||||
print(json.dumps([_text_view("settings.ini", text, len(text.encode()))["view"]["groups"] for text in json.load(sys.stdin)]))
|
||||
`;
|
||||
const root = await createTestTempDirectory("rea-keyfile-python-");
|
||||
const script = join(root, "fixture.py");
|
||||
await writeFile(script, program, "utf8");
|
||||
const output = execFileSync(
|
||||
process.platform === "win32" ? "python" : "python3",
|
||||
["-I", "-B", "-X", "utf8", script],
|
||||
it("agrees on keyfile whitespace in both state readers", () => {
|
||||
const files = [
|
||||
["settings.ini", "[ui]\n\u0085\nzoom=2\n"],
|
||||
["settings.ini", "[ui]\n\ufeffzoom=2\n"],
|
||||
[
|
||||
"settings.ini",
|
||||
"[ui]\r\nsuffix=hello \r\nunicode=\u00a0hello\u00a0\u00a0\r\npadded=\thello\r\n",
|
||||
],
|
||||
] as const;
|
||||
const expected = [
|
||||
{
|
||||
input: JSON.stringify(inputs),
|
||||
encoding: "utf8",
|
||||
format: "text",
|
||||
view: {
|
||||
lines: ["[ui]", "\u0085", "zoom=2"],
|
||||
line_count: 3,
|
||||
truncated: false,
|
||||
},
|
||||
error: null,
|
||||
},
|
||||
);
|
||||
expect(JSON.parse(output)).toEqual(inputs.map(keyfileView));
|
||||
{ format: "keyfile", view: { groups: { ui: { zoom: "2" } } }, error: null },
|
||||
{
|
||||
format: "keyfile",
|
||||
view: {
|
||||
groups: {
|
||||
ui: {
|
||||
suffix: "hello ",
|
||||
unicode: "\u00a0hello\u00a0\u00a0",
|
||||
padded: "hello",
|
||||
},
|
||||
},
|
||||
},
|
||||
error: null,
|
||||
},
|
||||
];
|
||||
const { python, rea } = textViews(files);
|
||||
expect(rea).toEqual(expected);
|
||||
expect(python).toEqual(expected);
|
||||
});
|
||||
|
||||
const hasPydantic2 =
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
readerAnswerView,
|
||||
readerCommand,
|
||||
} from "../../../src/domain/desktop/appTaskReaders.js";
|
||||
import { appStateViews } from "../../../src/domain/desktop/appStateViews.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
/** The protocol client calls the verifier makes, replaced so it reads the given views. */
|
||||
@@ -109,7 +110,8 @@ async def get_data(url, **_):
|
||||
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})])
|
||||
state_parts = scenario.get("state_parts", [{"kind": "data", "data": {"state": views}}])
|
||||
return types.SimpleNamespace(parts=[*files, *(types.SimpleNamespace(**part) for part in state_parts)])
|
||||
|
||||
|
||||
async def sleep(seconds):
|
||||
@@ -205,6 +207,7 @@ const runVerifier = async (
|
||||
readonly answers?: Readonly<Record<string, unknown>>;
|
||||
readonly retryAnswers?: Readonly<Record<string, unknown>>;
|
||||
readonly fileContents?: Readonly<Record<string, string>>;
|
||||
readonly stateParts?: readonly Record<string, unknown>[];
|
||||
readonly exec?: boolean;
|
||||
readonly cleanupFailures?: number;
|
||||
readonly retry?: boolean;
|
||||
@@ -251,6 +254,7 @@ const runVerifier = async (
|
||||
answers: computer.answers,
|
||||
retry_answers: computer.retryAnswers,
|
||||
file_contents: computer.fileContents,
|
||||
state_parts: computer.stateParts,
|
||||
exec: computer.exec ?? true,
|
||||
cleanup_failures: computer.cleanupFailures,
|
||||
retry: computer.retry,
|
||||
@@ -285,6 +289,55 @@ const linesPresent = (...lines: string[]): DesktopTaskCheck => ({
|
||||
const graded = ({ results }: VerifierOutput) =>
|
||||
results.map(({ result, settled, read_cut }) => [result, settled, read_cut]);
|
||||
|
||||
it("grades the first valid state part with the same view admission as the host witness", async () => {
|
||||
const file = "Documents/today.txt";
|
||||
const checks: DesktopTaskCheck[] = [
|
||||
{ id: "present", kind: "file_present", file },
|
||||
linesPresent("milk"),
|
||||
];
|
||||
const statePart = (state: unknown) => ({ kind: "data", data: { state } });
|
||||
const malformed = [
|
||||
statePart({ [file]: { format: "text" } }),
|
||||
statePart({ [file]: { format: 7, view: null } }),
|
||||
statePart({ [file]: { format: "text", view: null, error: 7 } }),
|
||||
];
|
||||
const host = appStateViews([...malformed, statePart({})]);
|
||||
if (!host.ok) throw new Error(host.error);
|
||||
expect(
|
||||
evaluateTaskChecks(checks, host.value).map(({ passed }) => passed),
|
||||
).toEqual([false, false]);
|
||||
const empty = await runVerifier(checks, [[0, {}]], [], {
|
||||
stateParts: [...malformed, statePart({})],
|
||||
});
|
||||
expect(empty.results).toEqual([
|
||||
expect.objectContaining({ result: false }),
|
||||
expect.objectContaining({
|
||||
result: false,
|
||||
description: expect.stringContaining("has no line"),
|
||||
}),
|
||||
]);
|
||||
|
||||
const valid = [statePart(today("milk")), statePart({})];
|
||||
const first = await runVerifier(checks, [[0, {}]], [], { stateParts: valid });
|
||||
expect(first.results.map(({ result }) => result)).toEqual([true, true]);
|
||||
const unreadable = await runVerifier(checks, [[0, {}]], [], {
|
||||
stateParts: [
|
||||
statePart({ [file]: { format: null, view: null, error: "read failed" } }),
|
||||
statePart(today("milk")),
|
||||
],
|
||||
});
|
||||
expect(unreadable.results[1]).toMatchObject({
|
||||
result: false,
|
||||
description: expect.stringContaining("read failed"),
|
||||
});
|
||||
const invalid = await runVerifier(checks, [[0, {}]], [], {
|
||||
stateParts: [{ kind: "data", data: null }, ...malformed],
|
||||
reportFailure: true,
|
||||
});
|
||||
expect(invalid.results).toEqual([]);
|
||||
expect(invalid.first_error).toMatch(/returned no state views/u);
|
||||
});
|
||||
|
||||
it("evaluates every kind of file check without computer-exec as REA does", async () => {
|
||||
const checks: DesktopTaskCheck[] = CHECK_CASES.map(([check], index) => ({
|
||||
...check,
|
||||
|
||||
Reference in new issue
Block a user