Skip to content

Commit ce06fa8

Browse files
committed
feat: EXPR runtime parity with openjd-rs in the v0 session
Implement the RFC 0005-0007 runtime behaviors in the v0 session so the spec conformance suite passes the EXPR jobs fixtures, mirroring the openjd-rs session runtime. - Evaluate EXPR `let` bindings at run time on all four action paths (environment enter/exit, task run, and the private no-session-env task path). Bindings evaluate against the live symbol table via the model's evaluate_value, so a let-bound path stays a path for property access and float rendering fidelity is preserved. - Type the session symbol table like openjd-rs's session scope: Param.*/Task.Param.* carry their declared OpenJD types (a PATH is a host-format path value with path mapping applied element-wise for LIST[PATH]), RawParam.* stays the raw string form, Session.WorkingDirectory / HasPathMappingRules / PathMappingRulesFile are path/bool/path, and Job.Name / Step.Name are seeded when known (new optional Session job_name kwarg; the existing run_task step_name feeds Step.Name). - RFC 0005 §1.3.2 argument resolution, mirroring openjd-rs's resolve_action_args: a whole-field expression argument resolves typed — null skips the argument, a list flattens inline (one argument per element, engine display coercion), and a scalar becomes a single argument. - URI path mapping rules (RFC 0006 §2.3.2): PathFormat.URI with RFC 3986 semantics (case-insensitive scheme+authority, case-sensitive path, component-boundary matching), ported from openjd-expr's apply_uri. Rules are also converted to their engine form and carried on the session's symbol tables so host-context functions such as apply_path_mapping() apply them during expression evaluation. The unit suite passes (639 passed; the two known-flaky timer tests in test_runner_base.py pass serially), and mypy/black/ruff are clean. Together with the openjd-model-for-python and openjd-cli parity changes, the spec conformance suite improves from 1062/1145 to 1128/1145 against the Python CLI. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
1 parent 2c743a8 commit ce06fa8

3 files changed

Lines changed: 309 additions & 26 deletions

File tree

‎src/openjd/sessions/_path_mapping.py‎

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,43 @@
44
from enum import Enum
55
from os import name as os_name
66
from pathlib import PurePath, PurePosixPath, PureWindowsPath
7+
from typing import Optional, Union
78

89

910
class PathFormat(str, Enum):
1011
POSIX = "POSIX"
1112
WINDOWS = "WINDOWS"
13+
# RFC 0006 §2.3.2 (EXPR extension): URI-form source paths
14+
# (e.g. "s3://bucket/prefix") that map to local filesystem paths.
15+
URI = "URI"
1216

1317

1418
@dataclass(frozen=True)
1519
class PathMappingRule:
1620
source_path_format: PathFormat
17-
source_path: PurePath
21+
# URI-format rules keep the source as the raw string: URIs are not
22+
# filesystem paths, and PurePath normalization would corrupt the
23+
# "scheme://" separator.
24+
source_path: Union[PurePath, str]
1825
destination_path: PurePath
1926

2027
def __init__(
21-
self, *, source_path_format: PathFormat, source_path: PurePath, destination_path: PurePath
28+
self,
29+
*,
30+
source_path_format: PathFormat,
31+
source_path: Union[PurePath, str],
32+
destination_path: PurePath,
2233
):
2334
if source_path_format == PathFormat.POSIX:
2435
if not isinstance(source_path, PurePosixPath):
2536
raise ValueError(
2637
"Path mapping rule source_path_format does not match source_path type"
2738
)
39+
elif source_path_format == PathFormat.URI:
40+
if not isinstance(source_path, str):
41+
raise ValueError(
42+
"Path mapping rule source_path must be a string for the URI source_path_format"
43+
)
2844
else:
2945
if not isinstance(source_path, PureWindowsPath):
3046
raise ValueError(
@@ -49,9 +65,12 @@ def from_dict(rule: dict[str, str]) -> "PathMappingRule":
4965
raise ValueError(f"Path mapping rule requires the following fields: {field_names}")
5066

5167
source_path_format = PathFormat(rule["source_path_format"].upper())
52-
source_path: PurePath
68+
source_path: Union[PurePath, str]
5369
if source_path_format == PathFormat.POSIX:
5470
source_path = PurePosixPath(rule["source_path"])
71+
elif source_path_format == PathFormat.URI:
72+
# Keep URIs verbatim; PurePath would collapse "scheme://".
73+
source_path = rule["source_path"]
5574
else:
5675
source_path = PureWindowsPath(rule["source_path"])
5776
destination_path = PurePath(rule["destination_path"])
@@ -76,25 +95,90 @@ def to_dict(self) -> dict[str, str]:
7695
"destination_path": str(self.destination_path),
7796
}
7897

98+
@staticmethod
99+
def _uri_path_start(uri: str) -> Optional[int]:
100+
"""Byte offset where the path component begins in a URI (after
101+
``scheme://authority``), or None when there is no ``://`` separator.
102+
Mirrors openjd-rs's ``uri_path_start``."""
103+
scheme_sep = uri.find("://")
104+
if scheme_sep == -1:
105+
return None
106+
authority_start = scheme_sep + 3
107+
slash = uri.find("/", authority_start)
108+
return len(uri) if slash == -1 else slash
109+
110+
def _apply_uri(self, path: str) -> tuple[bool, str]:
111+
"""Apply a URI-format rule, mirroring openjd-rs's ``apply_uri``.
112+
113+
Per RFC 3986 the scheme and authority match case-insensitively while
114+
the path portion matches case-sensitively, on whole path components.
115+
The result is a local path in the host's format.
116+
"""
117+
sep = "/" if os_name == "posix" else "\\"
118+
source = str(self.source_path)
119+
src_path_start = self._uri_path_start(source)
120+
src_path_start = 0 if src_path_start is None else src_path_start
121+
inp_path_start = self._uri_path_start(path)
122+
inp_path_start = 0 if inp_path_start is None else inp_path_start
123+
124+
# Scheme+authority must match case-insensitively.
125+
if path[:inp_path_start].lower() != source[:src_path_start].lower():
126+
return False, path
127+
# Path portion must match case-sensitively, on a component boundary.
128+
src_path = source[src_path_start:]
129+
inp_path = path[inp_path_start:]
130+
if not inp_path.startswith(src_path):
131+
return False, path
132+
remainder = inp_path[len(src_path) :]
133+
if remainder and not remainder.startswith("/"):
134+
return False, path
135+
136+
child_parts = remainder[1:].split("/") if remainder else []
137+
result = str(self.destination_path)
138+
for part in child_parts:
139+
result += sep + part
140+
if path.endswith("/") and not result.endswith(sep):
141+
result += sep
142+
return True, result
143+
144+
def source_path_component_count(self) -> int:
145+
"""Number of components in the source path, used to order rules from
146+
most to least specific. For URI sources the ``scheme://authority``
147+
counts as one component plus one per path segment."""
148+
if isinstance(self.source_path, PurePath):
149+
return len(self.source_path.parts)
150+
source = str(self.source_path)
151+
path_start = self._uri_path_start(source)
152+
if path_start is None:
153+
return 1
154+
path_portion = source[path_start:].strip("/")
155+
return 1 + (len(path_portion.split("/")) if path_portion else 0)
156+
79157
def apply(self, *, path: str) -> tuple[bool, str]:
80158
"""Applies the path mapping rule on the given path, if it matches the rule.
81159
Does not collapse ".." since symbolic paths could be used.
82160
83161
Returns: tuple[bool, str] - indicating if the path matched the rule and the resulting
84162
mapped path. If it doesn't match, then it returns the original path unmodified.
85163
"""
164+
if self.source_path_format == PathFormat.URI:
165+
return self._apply_uri(path)
166+
source_path = self.source_path
167+
if not isinstance(source_path, PurePath):
168+
# The constructor guarantees non-URI rules carry PurePath sources.
169+
raise TypeError(
170+
"Path mapping rule source_path must be a PurePath for filesystem source formats"
171+
)
86172
pure_path: PurePath
87173
if self.source_path_format == PathFormat.POSIX:
88174
pure_path = PurePosixPath(path)
89175
else:
90176
pure_path = PureWindowsPath(path)
91177

92-
if not pure_path.is_relative_to(self.source_path):
178+
if not pure_path.is_relative_to(source_path):
93179
return False, path
94180

95-
remapped_parts = (
96-
self.destination_path.parts + pure_path.parts[len(self.source_path.parts) :]
97-
)
181+
remapped_parts = self.destination_path.parts + pure_path.parts[len(source_path.parts) :]
98182
if os_name == "posix":
99183
result = str(PurePosixPath(*remapped_parts))
100184
if self._has_trailing_slash(self.source_path_format, path):

‎src/openjd/sessions/_runner_base.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,23 @@ def _run_action(
554554
try:
555555
command = [action.command.resolve(symtab=symtab)]
556556
if action.args is not None:
557-
command.extend(s.resolve(symtab=symtab) for s in action.args)
557+
# RFC 0005 §1.3.2 argument semantics, mirroring openjd-rs's
558+
# resolve_action_args: a whole-field expression argument
559+
# resolves typed — a null result skips the argument, a list
560+
# result flattens inline (one argument per element, rendered
561+
# with the engine's display coercion), and a scalar becomes a
562+
# single argument. Multi-segment format strings and legacy
563+
# (non-EXPR) expressions resolve to their string form.
564+
for arg in action.args:
565+
value = arg.resolve_value(symtab=symtab)
566+
if isinstance(value, str):
567+
command.append(value)
568+
elif getattr(value, "is_null", False):
569+
continue
570+
elif str(getattr(value, "type", "")).startswith("list["):
571+
command.extend(str(element) for element in value)
572+
else:
573+
command.append(str(value))
558574
except FormatStringError as exc:
559575
# Extremely unlikely since a JobTemplate needs to have passed
560576
# validation before we could be running it, but just to be safe.

0 commit comments

Comments
 (0)